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

defmodule AshPostgres.TestRepo.Migrations.MigrateResources51 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(:string_points, primary_key: false) do
      add(:id, :text, null: false, primary_key: true)
    end

    create(unique_index(:string_points, [:id], name: "string_points_id_index"))

    create table(:points, primary_key: false) do
      add(:id, {:array, :float}, null: false, primary_key: true)
    end

    create(unique_index(:points, [:id], name: "points_id_index"))

    alter table(:posts) do
      add(:string_point, :text)

      add(
        :db_point_id,
        references(:points,
          column: :id,
          name: "posts_db_point_id_fkey",
          type: {:array, :float},
          prefix: "public"
        )
      )

      add(
        :db_string_point_id,
        references(:string_points,
          column: :id,
          name: "posts_db_string_point_id_fkey",
          type: :text,
          prefix: "public"
        )
      )
    end
  end

  def down do
    drop(constraint(:posts, "posts_db_point_id_fkey"))

    drop(constraint(:posts, "posts_db_string_point_id_fkey"))

    alter table(:posts) do
      remove(:db_string_point_id)
      remove(:db_point_id)
      remove(:string_point)
    end

    drop_if_exists(unique_index(:points, [:id], name: "points_id_index"))

    drop(table(:points))

    drop_if_exists(unique_index(:string_points, [:id], name: "string_points_id_index"))

    drop(table(:string_points))
  end
end
