defmodule Realworld.Repo.Migrations.AddUniqueFollows 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(:created_at, :utc_datetime_usec, default: fragment("(now() AT TIME ZONE 'utc')"))
      modify(:id, :uuid, default: fragment("gen_random_uuid()"))
    end

    alter table(:tokens) do
      modify(:created_at, :utc_datetime_usec, default: fragment("(now() AT TIME ZONE 'utc')"))
      modify(:updated_at, :utc_datetime_usec, default: fragment("(now() AT TIME ZONE 'utc')"))
    end

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

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

    create unique_index(:follows, [:target_id, :user_id], name: "follows_unique_follow_index")

    alter table(:favorites) do
      modify(:updated_at, :utc_datetime_usec, default: fragment("(now() AT TIME ZONE 'utc')"))
      modify(:created_at, :utc_datetime_usec, default: fragment("(now() AT TIME ZONE 'utc')"))
    end

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

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

  def down do
    alter table(:articles) do
      modify(:id, :uuid, default: nil)
      modify(:created_at, :utc_datetime_usec, default: fragment("now()"))
      modify(:updated_at, :utc_datetime_usec, default: fragment("now()"))
    end

    alter table(:comments) do
      modify(:id, :uuid, default: nil)
      modify(:created_at, :utc_datetime_usec, default: fragment("now()"))
      modify(:updated_at, :utc_datetime_usec, default: fragment("now()"))
    end

    alter table(:favorites) do
      modify(:created_at, :utc_datetime_usec, default: fragment("now()"))
      modify(:updated_at, :utc_datetime_usec, default: fragment("now()"))
    end

    drop_if_exists(
      unique_index(:follows, [:target_id, :user_id], name: "follows_unique_follow_index")
    )

    alter table(:follows) do
      modify(:id, :uuid, default: nil)
      modify(:created_at, :utc_datetime_usec, default: fragment("now()"))
    end

    alter table(:tags) do
      modify(:id, :uuid, default: nil)
      modify(:created_at, :utc_datetime_usec, default: fragment("now()"))
      modify(:updated_at, :utc_datetime_usec, default: fragment("now()"))
    end

    alter table(:tokens) do
      modify(:updated_at, :utc_datetime_usec, default: fragment("now()"))
      modify(:created_at, :utc_datetime_usec, default: fragment("now()"))
    end

    alter table(:users) do
      modify(:id, :uuid, default: nil)
      modify(:created_at, :utc_datetime_usec, default: fragment("now()"))
      modify(:updated_at, :utc_datetime_usec, default: fragment("now()"))
    end
  end
end
