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

defmodule AshPostgres.TestRepo.Migrations.AddComplexCalculationTables 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_skills, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :removed, :boolean, null: false, default: false
      add :certification_id, :uuid
    end

    create table(:complex_calculations_documentations, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :status, :text, null: false
      add :documented_at, :utc_datetime_usec
      add :inserted_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")

      add :skill_id,
          references(:complex_calculations_skills,
            column: :id,
            name: "complex_calculations_documentations_skill_id_fkey",
            type: :uuid,
            prefix: "public"
          )
    end

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

    alter table(:complex_calculations_skills) do
      modify :certification_id,
             references(:complex_calculations_certifications,
               column: :id,
               name: "complex_calculations_skills_certification_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end
  end

  def down do
    drop constraint(
           :complex_calculations_skills,
           "complex_calculations_skills_certification_id_fkey"
         )

    alter table(:complex_calculations_skills) do
      modify :certification_id, :uuid
    end

    drop table(:complex_calculations_certifications)

    drop constraint(
           :complex_calculations_documentations,
           "complex_calculations_documentations_skill_id_fkey"
         )

    drop table(:complex_calculations_documentations)

    drop table(:complex_calculations_skills)
  end
end