defmodule Orcasite.Repo.Migrations.CreateFeedSegments 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 unique_index(:feed_streams, [:playlist_m3u8_path],
             name: "feed_streams_playlist_m3u8_path_index"
           )

    create table(:feed_segments, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true
      add :start_time, :utc_datetime
      add :end_time, :utc_datetime
      add :duration, :decimal
      add :bucket, :text
      add :bucket_region, :text
      add :cloudfront_url, :text
      add :playlist_timestamp, :text
      add :playlist_path, :text
      add :playlist_m3u8_path, :text
      add :segment_path, :text
      add :file_name, :text, null: false
      add :inserted_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")

      add :feed_id,
          references(:feeds, column: :id, name: "feed_segments_feed_id_fkey", type: :uuid)

      add :feed_stream_id,
          references(:feed_streams,
            column: :id,
            name: "feed_segments_feed_stream_id_fkey",
            type: :uuid
          )
    end

    create index(:feed_segments, [:bucket])

    create index(:feed_segments, [:feed_stream_id])

    create index(:feed_segments, [:feed_id])

    create index(:feed_segments, [:end_time])

    create index(:feed_segments, [:start_time])

    create unique_index(:feed_segments, [:segment_path],
             name: "feed_segments_feed_segment_path_index"
           )

    create unique_index(:feed_segments, [:feed_id, :start_time],
             name: "feed_segments_feed_segment_timestamp_index"
           )
  end

  def down do
    drop_if_exists unique_index(:feed_segments, [:feed_id, :start_time],
                     name: "feed_segments_feed_segment_timestamp_index"
                   )

    drop_if_exists unique_index(:feed_segments, [:segment_path],
                     name: "feed_segments_feed_segment_path_index"
                   )

    drop constraint(:feed_segments, "feed_segments_feed_id_fkey")

    drop constraint(:feed_segments, "feed_segments_feed_stream_id_fkey")

    drop_if_exists index(:feed_segments, [:start_time])

    drop_if_exists index(:feed_segments, [:end_time])

    drop_if_exists index(:feed_segments, [:feed_id])

    drop_if_exists index(:feed_segments, [:feed_stream_id])

    drop_if_exists index(:feed_segments, [:bucket])

    drop table(:feed_segments)

    drop_if_exists unique_index(:feed_streams, [:playlist_m3u8_path],
                     name: "feed_streams_playlist_m3u8_path_index"
                   )
  end
end
