Skip to content

fix: use schema when changing reference deferrability #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions lib/migration_generator/operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""
Expand Down
84 changes: 84 additions & 0 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading