defmodule Orcasite.Repo.Migrations.CreateNotificationSchema 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(:subscriptions, primary_key: false) do
      add :id, :uuid, null: false, primary_key: true, default: fragment("uuid_generate_v4()")
      add :name, :text
      add :meta, :map
      add :event_type, :text
      add :inserted_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :subscriber_id, :uuid
    end

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

    alter table(:subscriptions) do
      modify :subscriber_id,
             references(:subscribers,
               column: :id,
               name: "subscriptions_subscriber_id_fkey",
               type: :uuid
             )
    end

    alter table(:subscribers) do
      add :name, :text
      add :subscriber_type, :text
      add :meta, :map
      add :inserted_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")
    end

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

    alter table(:notifications) do
      add :meta, :map
      add :processed_at, :utc_datetime_usec
      add :event_type, :text
      add :inserted_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")
    end
  end

  def down do
    alter table(:notifications) do
      remove :updated_at
      remove :inserted_at
      remove :event_type
      remove :processed_at
      remove :meta
    end

    drop table(:notifications)

    alter table(:subscribers) do
      remove :updated_at
      remove :inserted_at
      remove :meta
      remove :subscriber_type
      remove :name
    end

    drop constraint(:subscriptions, "subscriptions_subscriber_id_fkey")

    alter table(:subscriptions) do
      modify :subscriber_id, :uuid
    end

    drop table(:subscribers)

    drop table(:subscriptions)
  end
end
