defmodule Helpcenter.Repo.TenantMigrations.AddUserGroupTable 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(:user_groups, primary_key: false, prefix: prefix()) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true

      add :inserted_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :group_id,
          references(:groups,
            column: :id,
            name: "user_groups_group_id_fkey",
            type: :uuid,
            prefix: prefix()
          ),
          null: false

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

    create unique_index(:user_groups, [:group_id, :user_id],
             name: "user_groups_unique_name_index"
           )
  end

  def down do
    drop_if_exists unique_index(:user_groups, [:group_id, :user_id],
                     name: "user_groups_unique_name_index"
                   )

    drop constraint(:user_groups, "user_groups_group_id_fkey")

    drop constraint(:user_groups, "user_groups_user_id_fkey")

    drop table(:user_groups, prefix: prefix())
  end
end
