# SPDX-FileCopyrightText: 2020 Zach Daniel
#
# SPDX-License-Identifier: MIT

defmodule AshPostgres.TestRepo.Migrations.MigrateResources60 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(:messages, primary_key: false) do
      add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
      add(:content, :text)
      add(:sent_at, :utc_datetime, default: fragment("(now() AT TIME ZONE 'utc')"))
      add(:read_at, :utc_datetime)
      add(:chat_id, :uuid, null: false)
    end

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

    alter table(:messages) do
      modify(
        :chat_id,
        references(:chats,
          column: :id,
          name: "messages_chat_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end

    alter table(:chats) do
      add(:name, :text)
    end
  end

  def down do
    alter table(:chats) do
      remove(:name)
    end

    drop(constraint(:messages, "messages_chat_id_fkey"))

    alter table(:messages) do
      modify(:chat_id, :uuid)
    end

    drop(table(:chats))

    drop(table(:messages))
  end
end
