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

defmodule AshPostgres.TestRepo.Migrations.MigrateResources58 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(:tags, primary_key: false) do
      add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
      add(:importance, :bigint, null: false, default: 0)
    end

    create table(:post_tags, primary_key: false) do
      add(
        :post_id,
        references(:posts,
          column: :id,
          name: "post_tags_post_id_fkey",
          type: :uuid,
          prefix: "public"
        ),
        primary_key: true,
        null: false
      )

      add(
        :tag_id,
        references(:tags,
          column: :id,
          name: "post_tags_tag_id_fkey",
          type: :uuid,
          prefix: "public"
        ),
        primary_key: true,
        null: false
      )
    end

    create(unique_index(:post_tags, [:post_id, :tag_id], name: "post_tags_unique_post_tag_index"))
  end

  def down do
    drop_if_exists(
      unique_index(:post_tags, [:post_id, :tag_id], name: "post_tags_unique_post_tag_index")
    )

    drop(constraint(:post_tags, "post_tags_post_id_fkey"))

    drop(constraint(:post_tags, "post_tags_tag_id_fkey"))

    drop(table(:post_tags))

    drop(table(:tags))
  end
end
