Skip to content

Commit 77a75fa

Browse files
authored
fix: correct order, when renaming attribute with an identity (#533)
1 parent eb0bc97 commit 77a75fa

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

lib/migration_generator/migration_generator.ex

+28
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,21 @@ defmodule AshPostgres.MigrationGenerator do
14741474
false
14751475
end
14761476

1477+
defp after?(
1478+
%Operation.RenameAttribute{
1479+
old_attribute: %{source: source},
1480+
table: table,
1481+
schema: schema
1482+
},
1483+
%Operation.RemoveUniqueIndex{
1484+
identity: %{keys: keys},
1485+
table: table,
1486+
schema: schema
1487+
}
1488+
) do
1489+
source in List.wrap(keys)
1490+
end
1491+
14771492
defp after?(
14781493
%Operation.RemoveUniqueIndex{table: table, schema: schema},
14791494
%{table: table, schema: schema}
@@ -1701,6 +1716,19 @@ defmodule AshPostgres.MigrationGenerator do
17011716
destination_attribute in keys
17021717
end
17031718

1719+
defp after?(
1720+
%Operation.AlterAttribute{
1721+
old_attribute: %{
1722+
source: source
1723+
},
1724+
table: table,
1725+
schema: schema
1726+
},
1727+
%Operation.RemoveUniqueIndex{identity: %{keys: keys}, table: table, schema: schema}
1728+
) do
1729+
source in List.wrap(keys)
1730+
end
1731+
17041732
defp after?(
17051733
%Operation.AlterAttribute{
17061734
new_attribute: %{references: %{table: table, destination_attribute: source}}

test/migration_generator_test.exs

+77
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,83 @@ defmodule AshPostgres.MigrationGeneratorTest do
10621062
assert File.read!(file2) =~ ~S[rename table(:posts), :subject, to: :title]
10631063
end
10641064

1065+
test "when renaming a field with an identity, it asks which field you are renaming it to, and updates indexes in the correct order" do
1066+
defposts do
1067+
identities do
1068+
identity(:subject, [:subject])
1069+
end
1070+
1071+
attributes do
1072+
uuid_primary_key(:id)
1073+
attribute(:subject, :string, public?: true)
1074+
end
1075+
end
1076+
1077+
defdomain([Post])
1078+
1079+
send(self(), {:mix_shell_input, :yes?, true})
1080+
send(self(), {:mix_shell_input, :prompt, "subject"})
1081+
1082+
AshPostgres.MigrationGenerator.generate(Domain,
1083+
snapshot_path: "test_snapshots_path",
1084+
migration_path: "test_migration_path",
1085+
quiet: true,
1086+
format: false
1087+
)
1088+
1089+
assert [_file1, file2] =
1090+
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
1091+
|> Enum.reject(&String.contains?(&1, "extensions"))
1092+
1093+
contents = File.read!(file2)
1094+
[up_side, down_side] = String.split(contents, "def down", parts: 2)
1095+
1096+
up_side_parts =
1097+
String.split(up_side, "\n", trim: true)
1098+
|> Enum.map(&String.trim/1)
1099+
1100+
drop_index =
1101+
Enum.find_index(up_side_parts, fn x ->
1102+
x == "drop_if_exists unique_index(:posts, [:title], name: \"posts_title_index\")"
1103+
end)
1104+
1105+
rename_table =
1106+
Enum.find_index(up_side_parts, fn x ->
1107+
x == "rename table(:posts), :title, to: :subject"
1108+
end)
1109+
1110+
create_index =
1111+
Enum.find_index(up_side_parts, fn x ->
1112+
x == "create unique_index(:posts, [:subject], name: \"posts_subject_index\")"
1113+
end)
1114+
1115+
assert drop_index < rename_table
1116+
assert rename_table < create_index
1117+
1118+
down_side_parts =
1119+
String.split(down_side, "\n", trim: true)
1120+
|> Enum.map(&String.trim/1)
1121+
1122+
drop_index =
1123+
Enum.find_index(down_side_parts, fn x ->
1124+
x ==
1125+
"drop_if_exists unique_index(:posts, [:subject], name: \"posts_subject_index\")"
1126+
end)
1127+
1128+
rename_table =
1129+
Enum.find_index(down_side_parts, fn x ->
1130+
x == "rename table(:posts), :subject, to: :title"
1131+
end)
1132+
1133+
create_index =
1134+
Enum.find_index(down_side_parts, fn x ->
1135+
x == "create unique_index(:posts, [:title], name: \"posts_title_index\")"
1136+
end)
1137+
1138+
assert drop_index < rename_table
1139+
assert rename_table < create_index
1140+
end
1141+
10651142
test "when renaming a field, it asks which field you are renaming it to, and adds it if you arent" do
10661143
defposts do
10671144
attributes do

0 commit comments

Comments
 (0)