defmodule Radiator.Repo.Migrations.AddEpisodeChapters 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(:chapters, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :start_time_seconds, :bigint, null: false
      add :title, :text, null: false
      add :link, :text

      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 :episode_id,
          references(:episodes,
            column: :id,
            name: "chapters_episode_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          null: false
    end

    create unique_index(:chapters, [:start_time_seconds, :episode_id],
             name: "chapters_start_time_index"
           )
  end

  def down do
    drop_if_exists unique_index(:chapters, [:start_time_seconds, :episode_id],
                     name: "chapters_start_time_index"
                   )

    drop constraint(:chapters, "chapters_episode_id_fkey")

    drop table(:chapters)
  end
end
