# SPDX-FileCopyrightText: 2020 Zach Daniel
#
# SPDX-License-Identifier: MIT

defmodule Demo.Repo.Migrations.MigrateResources5 do
  @moduledoc """
  Updates resources based on their most recent snapshots.

  This file was autogenerated with `mix ash_postgres.generate_migrations`
  """

  use Ecto.Migration

  def up do
    alter table(:users) do
      modify(:updated_at, :utc_datetime_usec, default: fragment("(now() AT TIME ZONE 'utc')"))
      modify(:inserted_at, :utc_datetime_usec, default: fragment("(now() AT TIME ZONE 'utc')"))
      modify(:id, :uuid, default: fragment("gen_random_uuid()"))
      add(:org, :text)
    end

    alter table(:tickets) do
      modify(:updated_at, :utc_datetime_usec, default: fragment("(now() AT TIME ZONE 'utc')"))
      modify(:inserted_at, :utc_datetime_usec, default: fragment("(now() AT TIME ZONE 'utc')"))
      modify(:id, :uuid, default: fragment("gen_random_uuid()"))
    end

    drop(constraint(:ticket_comments, "ticket_comments_resource_id_fkey"))

    alter table(:ticket_comments) do
      modify(:id, :uuid, default: fragment("gen_random_uuid()"))

      modify(
        :resource_id,
        references(:tickets, column: :id, name: "ticket_comments_resource_id_fkey", type: :uuid)
      )
    end

    drop(constraint(:representative_comments, "representative_comments_resource_id_fkey"))

    alter table(:representative_comments) do
      modify(:id, :uuid, default: fragment("gen_random_uuid()"))

      modify(
        :resource_id,
        references(:users,
          column: :id,
          name: "representative_comments_resource_id_fkey",
          type: :uuid
        )
      )
    end

    alter table(:organizations) do
      modify(:id, :uuid, default: fragment("gen_random_uuid()"))
    end

    execute(
      "ALTER TABLE representative_comments alter CONSTRAINT representative_comments_resource_id_fkey NOT DEFERRABLE"
    )

    execute(
      "ALTER TABLE ticket_comments alter CONSTRAINT ticket_comments_resource_id_fkey NOT DEFERRABLE"
    )
  end

  def down do
    alter table(:organizations) do
      modify(:id, :uuid, default: fragment("uuid_generate_v4()"))
    end

    drop(constraint(:representative_comments, "representative_comments_resource_id_fkey"))

    alter table(:representative_comments) do
      modify(
        :resource_id,
        references(:users,
          column: :id,
          name: "representative_comments_resource_id_fkey",
          type: :uuid
        )
      )

      modify(:id, :uuid, default: fragment("uuid_generate_v4()"))
    end

    drop(constraint(:ticket_comments, "ticket_comments_resource_id_fkey"))

    alter table(:ticket_comments) do
      modify(
        :resource_id,
        references(:tickets, column: :id, name: "ticket_comments_resource_id_fkey", type: :uuid)
      )

      modify(:id, :uuid, default: fragment("uuid_generate_v4()"))
    end

    alter table(:tickets) do
      modify(:id, :uuid, default: fragment("uuid_generate_v4()"))
      modify(:inserted_at, :utc_datetime_usec, default: fragment("now()"))
      modify(:updated_at, :utc_datetime_usec, default: fragment("now()"))
    end

    alter table(:users) do
      remove(:org)
      modify(:id, :uuid, default: fragment("uuid_generate_v4()"))
      modify(:inserted_at, :utc_datetime_usec, default: fragment("now()"))
      modify(:updated_at, :utc_datetime_usec, default: fragment("now()"))
    end
  end
end
