defmodule Radiator.Repo.Migrations.CreateLicensesForShows 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(:shows) do
      add :license_id, :uuid
    end

    create table(:licenses, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
    end

    alter table(:shows) do
      modify :license_id,
             references(:licenses,
               column: :id,
               name: "shows_license_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    alter table(:licenses) do
      add :short_name, :text, null: false
      add :name, :text, null: false
      add :url, :text, null: false

      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')")
    end
  end

  def down do
    alter table(:licenses) do
      remove :updated_at
      remove :inserted_at
      remove :url
      remove :name
      remove :short_name
    end

    drop constraint(:shows, "shows_license_id_fkey")

    alter table(:shows) do
      modify :license_id, :uuid
    end

    drop table(:licenses)

    alter table(:shows) do
      remove :license_id
    end
  end
end
