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

defmodule AshPostgres.TestRepo.Migrations.AddCsvResource 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
    alter table(:users) do
      remove(:org_id)
      modify(:role_list, {:array, :text}, null: false)
      modify(:role, :text, null: false)
    end

    create table(:csv, primary_key: false) do
      add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
      add(:column_mapping_embedded, :jsonb)
      add(:column_mapping_new_type, :jsonb)
    end
  end

  def down do
    drop(table(:csv))

    alter table(:users) do
      modify(:role, :text, null: true)
      modify(:role_list, {:array, :text}, null: true)

      add(
        :org_id,
        references(:multitenant_orgs,
          column: :id,
          name: "users_org_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end
  end
end
