defmodule AshHq.Repo.Migrations.MigrateResources17 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
    create table(:users, primary_key: false) do
      add(:id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true)
      add(:email, :citext, null: false)
      add(:confirmed_at, :utc_datetime_usec)
      add(:hashed_password, :text)
      add(:created_at, :utc_datetime_usec, null: false, default: fragment("now()"))
      add(:updated_at, :utc_datetime_usec, null: false, default: fragment("now()"))
    end

    create unique_index(:users, [:email], name: "users_unique_email_index")

    create table(:user_tokens, primary_key: false) do
      add(:id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true)
      add(:token, :binary)
      add(:context, :text)
      add(:sent_to, :text)
      add(:created_at, :utc_datetime_usec, null: false, default: fragment("now()"))

      add(
        :user_id,
        references(:users,
          column: :id,
          name: "user_tokens_user_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end

    create unique_index(:user_tokens, [:context, :token], name: "user_tokens_token_context_index")
  end

  def down do
    drop_if_exists(
      unique_index(:user_tokens, [:context, :token], name: "user_tokens_token_context_index")
    )

    drop(constraint(:user_tokens, "user_tokens_user_id_fkey"))

    drop(table(:user_tokens))

    drop_if_exists(unique_index(:users, [:email], name: "users_unique_email_index"))

    drop(table(:users))
  end
end
