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

defmodule AshPostgres.TestRepo.Migrations.AddComplexCalculationsFolderAndItems 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(:complex_calculations_folder_items, primary_key: false) do
      add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
      add(:name, :text)
      add(:level, :bigint)
      add(:folder_id, :uuid)
    end

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

    alter table(:complex_calculations_folder_items) do
      modify(
        :folder_id,
        references(:complex_calculations_folders,
          column: :id,
          name: "complex_calculations_folder_items_folder_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end

    alter table(:complex_calculations_folders) do
      add(:some_integer_setting, :bigint)
      add(:level, :ltree)
    end
  end

  def down do
    alter table(:complex_calculations_folders) do
      remove(:level)
      remove(:some_integer_setting)
    end

    drop(
      constraint(
        :complex_calculations_folder_items,
        "complex_calculations_folder_items_folder_id_fkey"
      )
    )

    alter table(:complex_calculations_folder_items) do
      modify(:folder_id, :uuid)
    end

    drop(table(:complex_calculations_folders))

    drop(table(:complex_calculations_folder_items))
  end
end
