Skip to content

Commit e19c93a

Browse files
committed
chore: fix tests
1 parent 8936bc1 commit e19c93a

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

lib/migration_generator/migration_generator.ex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2136,7 +2136,17 @@ defmodule AshPostgres.MigrationGenerator do
21362136
must_add_primary_key? =
21372137
must_drop_pkey? &&
21382138
Enum.any?(snapshot.attributes, fn attribute ->
2139-
attribute.primary_key?
2139+
attribute.primary_key? &&
2140+
!Enum.any?(attribute_operations, fn
2141+
%Operation.AlterAttribute{} = operation ->
2142+
operation.new_attribute.source == attribute.source
2143+
2144+
%Operation.AddAttribute{} = operation ->
2145+
operation.attribute.source == attribute.source
2146+
2147+
_ ->
2148+
false
2149+
end)
21402150
end)
21412151

21422152
must_add_primary_key_in_down? =

test/migration_generator_test.exs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,105 @@ defmodule AshPostgres.MigrationGeneratorTest do
393393
end
394394
end
395395

396+
describe "creating follow up migrations with a composite primary key" do
397+
setup do
398+
on_exit(fn ->
399+
File.rm_rf!("test_snapshots_path")
400+
File.rm_rf!("test_migration_path")
401+
end)
402+
403+
defposts do
404+
postgres do
405+
schema("example")
406+
end
407+
408+
attributes do
409+
uuid_primary_key(:id)
410+
attribute(:title, :string, public?: true, primary_key?: true, allow_nil?: false)
411+
end
412+
end
413+
414+
defdomain([Post])
415+
416+
AshPostgres.MigrationGenerator.generate(Domain,
417+
snapshot_path: "test_snapshots_path",
418+
migration_path: "test_migration_path",
419+
quiet: true,
420+
format: false
421+
)
422+
423+
:ok
424+
end
425+
426+
test "when removing an element, it recreates the primary key" do
427+
defposts do
428+
postgres do
429+
schema("example")
430+
end
431+
432+
attributes do
433+
uuid_primary_key(:id)
434+
end
435+
end
436+
437+
defdomain([Post])
438+
439+
send(self(), {:mix_shell_input, :yes?, true})
440+
441+
AshPostgres.MigrationGenerator.generate(Domain,
442+
snapshot_path: "test_snapshots_path",
443+
migration_path: "test_migration_path",
444+
quiet: true,
445+
format: false
446+
)
447+
448+
assert [_file1, file2] =
449+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
450+
|> Enum.reject(&String.contains?(&1, "extensions"))
451+
452+
contents = File.read!(file2)
453+
454+
[up_side, down_side] = String.split(contents, "def down", parts: 2)
455+
456+
assert up_side =~ ~S[execute("ALTER TABLE \"example.posts\" ADD PRIMARY KEY (id)")]
457+
assert down_side =~ ~S[execute("ALTER TABLE \"example.posts\" DROP constraint posts_pkey")]
458+
assert down_side =~ ~S[execute("ALTER TABLE \"example.posts\" ADD PRIMARY KEY (id, title)")]
459+
460+
defposts do
461+
postgres do
462+
schema("example")
463+
end
464+
465+
attributes do
466+
uuid_primary_key(:id)
467+
attribute(:title, :string, public?: true, primary_key?: true, allow_nil?: false)
468+
end
469+
end
470+
471+
defdomain([Post])
472+
473+
send(self(), {:mix_shell_input, :yes?, true})
474+
475+
AshPostgres.MigrationGenerator.generate(Domain,
476+
snapshot_path: "test_snapshots_path",
477+
migration_path: "test_migration_path",
478+
quiet: true,
479+
format: false
480+
)
481+
482+
assert [_file1, _file2, file3] =
483+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
484+
|> Enum.reject(&String.contains?(&1, "extensions"))
485+
486+
contents = File.read!(file3)
487+
488+
[up_side, down_side] = String.split(contents, "def down", parts: 2)
489+
490+
assert up_side =~ ~S[execute("ALTER TABLE \"example.posts\" ADD PRIMARY KEY (id, title)")]
491+
assert down_side =~ ~S[execute("ALTER TABLE \"example.posts\" ADD PRIMARY KEY (id)")]
492+
end
493+
end
494+
396495
describe "creating follow up migrations with a schema" do
397496
setup do
398497
on_exit(fn ->

0 commit comments

Comments
 (0)