From 0ae5389b8ca326f8780043a15c9b6f0266065bfd Mon Sep 17 00:00:00 2001 From: Artiom Lunev Date: Fri, 28 Mar 2025 12:23:58 +0000 Subject: [PATCH] fix: use schema when changing reference deferrability --- lib/migration_generator/operation.ex | 30 ++++++++-- test/migration_generator_test.exs | 84 ++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 6 deletions(-) diff --git a/lib/migration_generator/operation.ex b/lib/migration_generator/operation.ex index 87cea347..14f7b52b 100644 --- a/lib/migration_generator/operation.ex +++ b/lib/migration_generator/operation.ex @@ -454,16 +454,34 @@ defmodule AshPostgres.MigrationGenerator.Operation do @moduledoc false defstruct [:table, :schema, :references, :direction, no_phase: true] - def up(%{direction: :up, table: table, references: %{name: name, deferrable: true}}) do - "execute(\"ALTER TABLE #{table} alter CONSTRAINT #{name} DEFERRABLE INITIALLY IMMEDIATE\");" + defp prefix_name(name, prefix) do + if prefix do + "#{prefix}.#{name}" + else + name + end + end + + def up(%{ + direction: :up, + schema: schema, + table: table, + references: %{name: name, deferrable: true} + }) do + "execute(\"ALTER TABLE #{prefix_name(table, schema)} ALTER CONSTRAINT #{name} DEFERRABLE INITIALLY IMMEDIATE\");" end - def up(%{direction: :up, table: table, references: %{name: name, deferrable: :initially}}) do - "execute(\"ALTER TABLE #{table} alter CONSTRAINT #{name} DEFERRABLE INITIALLY DEFERRED\");" + def up(%{ + direction: :up, + schema: schema, + table: table, + references: %{name: name, deferrable: :initially} + }) do + "execute(\"ALTER TABLE #{prefix_name(table, schema)} ALTER CONSTRAINT #{name} DEFERRABLE INITIALLY DEFERRED\");" end - def up(%{direction: :up, table: table, references: %{name: name}}) do - "execute(\"ALTER TABLE #{table} alter CONSTRAINT #{name} NOT DEFERRABLE\");" + def up(%{direction: :up, schema: schema, table: table, references: %{name: name}}) do + "execute(\"ALTER TABLE #{prefix_name(table, schema)} ALTER CONSTRAINT #{name} NOT DEFERRABLE\");" end def up(_), do: "" diff --git a/test/migration_generator_test.exs b/test/migration_generator_test.exs index d9efc296..8c613055 100644 --- a/test/migration_generator_test.exs +++ b/test/migration_generator_test.exs @@ -1499,6 +1499,90 @@ defmodule AshPostgres.MigrationGeneratorTest do assert File.read!(file) =~ ~S{create index(:posts, [:post_id])} end + test "references with deferrable modifications generate changes with the correct schema" do + defposts do + attributes do + uuid_primary_key(:id) + attribute(:key_id, :uuid, allow_nil?: false, public?: true) + attribute(:foobar, :string, public?: true) + end + + postgres do + schema "example" + end + end + + defposts Post2 do + attributes do + uuid_primary_key(:id) + attribute(:name, :string, public?: true) + attribute(:related_key_id, :uuid, public?: true) + end + + relationships do + belongs_to(:post, Post) do + public?(true) + end + end + + postgres do + schema "example" + + references do + reference(:post, index?: true, deferrable: :initially) + end + end + end + + defdomain([Post, Post2]) + + AshPostgres.MigrationGenerator.generate(Domain, + snapshot_path: "test_snapshots_path", + migration_path: "test_migration_path", + quiet: true, + format: false + ) + + defposts Post2 do + attributes do + uuid_primary_key(:id) + attribute(:name, :string, public?: true) + attribute(:related_key_id, :uuid, public?: true) + end + + relationships do + belongs_to(:post, Post) do + public?(true) + end + end + + postgres do + schema "example" + + references do + reference(:post, index?: true, deferrable: true) + end + end + end + + AshPostgres.MigrationGenerator.generate(Domain, + snapshot_path: "test_snapshots_path", + migration_path: "test_migration_path", + quiet: true, + format: false + ) + + assert file = + "test_migration_path/**/*_migrate_resources*.exs" + |> Path.wildcard() + |> Enum.reject(&String.contains?(&1, "extensions")) + |> Enum.sort() + |> Enum.at(1) + |> File.read!() + + assert file =~ ~S{execute("ALTER TABLE example.posts ALTER CONSTRAINT} + end + test "index generated by index? true also adds column when using attribute multitenancy" do defresource Org, "orgs" do attributes do