Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f3c3278

Browse files
authoredMar 21, 2025··
fix: order when renaming attribute with an index (#514)
1 parent c228e0b commit f3c3278

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
 

‎lib/migration_generator/migration_generator.ex

+13
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,19 @@ defmodule AshPostgres.MigrationGenerator do
13751375
true
13761376
end
13771377

1378+
defp after?(
1379+
%Operation.AddCustomIndex{
1380+
table: table,
1381+
schema: schema
1382+
},
1383+
%Operation.RenameAttribute{
1384+
table: table,
1385+
schema: schema
1386+
}
1387+
) do
1388+
true
1389+
end
1390+
13781391
defp after?(
13791392
%Operation.AddReferenceIndex{
13801393
table: table,

‎test/migration_generator_test.exs

+81
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,87 @@ defmodule AshPostgres.MigrationGeneratorTest do
393393
end
394394
end
395395

396+
describe "custom_indexes with follow up migrations" 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+
custom_indexes do
406+
index([:title])
407+
end
408+
end
409+
410+
attributes do
411+
uuid_primary_key(:id)
412+
attribute(:title, :string, public?: true)
413+
end
414+
end
415+
416+
defdomain([Post])
417+
418+
AshPostgres.MigrationGenerator.generate(Domain,
419+
snapshot_path: "test_snapshots_path",
420+
migration_path: "test_migration_path",
421+
quiet: true,
422+
format: false
423+
)
424+
end
425+
426+
test "it changes attribute and index in the correct order" do
427+
defposts do
428+
postgres do
429+
custom_indexes do
430+
index([:title_short])
431+
end
432+
end
433+
434+
attributes do
435+
uuid_primary_key(:id)
436+
attribute(:title_short, :string, public?: true)
437+
end
438+
end
439+
440+
defdomain([Post])
441+
442+
send(self(), {:mix_shell_input, :yes?, true})
443+
444+
AshPostgres.MigrationGenerator.generate(Domain,
445+
snapshot_path: "test_snapshots_path",
446+
migration_path: "test_migration_path",
447+
quiet: true,
448+
format: false
449+
)
450+
451+
assert [_file1, file2] =
452+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
453+
|> Enum.reject(&String.contains?(&1, "extensions"))
454+
455+
contents = File.read!(file2)
456+
457+
[up_side, down_side] = String.split(contents, "def down", parts: 2)
458+
459+
up_side_parts = String.split(up_side, "\n", trim: true)
460+
461+
assert Enum.find_index(up_side_parts, fn x ->
462+
x == "rename table(:posts), :title, to: :title_short"
463+
end) <
464+
Enum.find_index(up_side_parts, fn x ->
465+
x == "create index(:posts, [:title_short])"
466+
end)
467+
468+
down_side_parts = String.split(down_side, "\n", trim: true)
469+
470+
assert Enum.find_index(down_side_parts, fn x ->
471+
x == "rename table(:posts), :title_short, to: :title"
472+
end) <
473+
Enum.find_index(down_side_parts, fn x -> x == "create index(:posts, [:title])" end)
474+
end
475+
end
476+
396477
describe "creating follow up migrations with a composite primary key" do
397478
setup do
398479
on_exit(fn ->

0 commit comments

Comments
 (0)
Please sign in to comment.