Skip to content

Commit 8fd3bfb

Browse files
authored
fix: use schema when changing reference deferrability (#519)
1 parent a72ad7e commit 8fd3bfb

File tree

2 files changed

+108
-6
lines changed

2 files changed

+108
-6
lines changed

lib/migration_generator/operation.ex

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,16 +454,34 @@ defmodule AshPostgres.MigrationGenerator.Operation do
454454
@moduledoc false
455455
defstruct [:table, :schema, :references, :direction, no_phase: true]
456456

457-
def up(%{direction: :up, table: table, references: %{name: name, deferrable: true}}) do
458-
"execute(\"ALTER TABLE #{table} alter CONSTRAINT #{name} DEFERRABLE INITIALLY IMMEDIATE\");"
457+
defp prefix_name(name, prefix) do
458+
if prefix do
459+
"#{prefix}.#{name}"
460+
else
461+
name
462+
end
463+
end
464+
465+
def up(%{
466+
direction: :up,
467+
schema: schema,
468+
table: table,
469+
references: %{name: name, deferrable: true}
470+
}) do
471+
"execute(\"ALTER TABLE #{prefix_name(table, schema)} ALTER CONSTRAINT #{name} DEFERRABLE INITIALLY IMMEDIATE\");"
459472
end
460473

461-
def up(%{direction: :up, table: table, references: %{name: name, deferrable: :initially}}) do
462-
"execute(\"ALTER TABLE #{table} alter CONSTRAINT #{name} DEFERRABLE INITIALLY DEFERRED\");"
474+
def up(%{
475+
direction: :up,
476+
schema: schema,
477+
table: table,
478+
references: %{name: name, deferrable: :initially}
479+
}) do
480+
"execute(\"ALTER TABLE #{prefix_name(table, schema)} ALTER CONSTRAINT #{name} DEFERRABLE INITIALLY DEFERRED\");"
463481
end
464482

465-
def up(%{direction: :up, table: table, references: %{name: name}}) do
466-
"execute(\"ALTER TABLE #{table} alter CONSTRAINT #{name} NOT DEFERRABLE\");"
483+
def up(%{direction: :up, schema: schema, table: table, references: %{name: name}}) do
484+
"execute(\"ALTER TABLE #{prefix_name(table, schema)} ALTER CONSTRAINT #{name} NOT DEFERRABLE\");"
467485
end
468486

469487
def up(_), do: ""

test/migration_generator_test.exs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,90 @@ defmodule AshPostgres.MigrationGeneratorTest do
14991499
assert File.read!(file) =~ ~S{create index(:posts, [:post_id])}
15001500
end
15011501

1502+
test "references with deferrable modifications generate changes with the correct schema" do
1503+
defposts do
1504+
attributes do
1505+
uuid_primary_key(:id)
1506+
attribute(:key_id, :uuid, allow_nil?: false, public?: true)
1507+
attribute(:foobar, :string, public?: true)
1508+
end
1509+
1510+
postgres do
1511+
schema "example"
1512+
end
1513+
end
1514+
1515+
defposts Post2 do
1516+
attributes do
1517+
uuid_primary_key(:id)
1518+
attribute(:name, :string, public?: true)
1519+
attribute(:related_key_id, :uuid, public?: true)
1520+
end
1521+
1522+
relationships do
1523+
belongs_to(:post, Post) do
1524+
public?(true)
1525+
end
1526+
end
1527+
1528+
postgres do
1529+
schema "example"
1530+
1531+
references do
1532+
reference(:post, index?: true, deferrable: :initially)
1533+
end
1534+
end
1535+
end
1536+
1537+
defdomain([Post, Post2])
1538+
1539+
AshPostgres.MigrationGenerator.generate(Domain,
1540+
snapshot_path: "test_snapshots_path",
1541+
migration_path: "test_migration_path",
1542+
quiet: true,
1543+
format: false
1544+
)
1545+
1546+
defposts Post2 do
1547+
attributes do
1548+
uuid_primary_key(:id)
1549+
attribute(:name, :string, public?: true)
1550+
attribute(:related_key_id, :uuid, public?: true)
1551+
end
1552+
1553+
relationships do
1554+
belongs_to(:post, Post) do
1555+
public?(true)
1556+
end
1557+
end
1558+
1559+
postgres do
1560+
schema "example"
1561+
1562+
references do
1563+
reference(:post, index?: true, deferrable: true)
1564+
end
1565+
end
1566+
end
1567+
1568+
AshPostgres.MigrationGenerator.generate(Domain,
1569+
snapshot_path: "test_snapshots_path",
1570+
migration_path: "test_migration_path",
1571+
quiet: true,
1572+
format: false
1573+
)
1574+
1575+
assert file =
1576+
"test_migration_path/**/*_migrate_resources*.exs"
1577+
|> Path.wildcard()
1578+
|> Enum.reject(&String.contains?(&1, "extensions"))
1579+
|> Enum.sort()
1580+
|> Enum.at(1)
1581+
|> File.read!()
1582+
1583+
assert file =~ ~S{execute("ALTER TABLE example.posts ALTER CONSTRAINT}
1584+
end
1585+
15021586
test "index generated by index? true also adds column when using attribute multitenancy" do
15031587
defresource Org, "orgs" do
15041588
attributes do

0 commit comments

Comments
 (0)