defmodule Orcasite.Repo.Migrations.CreateListenerCounts 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(:feed_listener_counts, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true
      add :count, :bigint, null: false
      add :timestamp, :utc_datetime, 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')")

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

    create index(:feed_listener_counts, [:feed_id])

    create index(:feed_listener_counts, [:timestamp])

    create index(:feed_listener_counts, [:count])
  end

  def down do
    drop constraint(:feed_listener_counts, "feed_listener_counts_feed_id_fkey")

    drop_if_exists index(:feed_listener_counts, [:count])

    drop_if_exists index(:feed_listener_counts, [:timestamp])

    drop_if_exists index(:feed_listener_counts, [:feed_id])

    drop table(:feed_listener_counts)
  end
end
