Skip to content

Commit baf4e32

Browse files
authored
fix: Use new attribute source in down migration (#604)
closes: #582 When an attribute is renamed, alongside other changes to it (like changing default or not null constraint), the generated up migration first renames the column, then modifies it. For the generated down migration, the order of operations is reversed, so we need to use the new column name when modifying the column, before renaming it back.
1 parent 9963270 commit baf4e32

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/migration_generator/operation.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
752752
up(%{
753753
op
754754
| old_attribute: op.new_attribute,
755-
new_attribute: op.old_attribute,
755+
new_attribute: Map.put(op.old_attribute, :source, op.new_attribute.source),
756756
old_multitenancy: op.multitenancy,
757757
multitenancy: op.old_multitenancy
758758
})

test/migration_generator_test.exs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,41 @@ defmodule AshPostgres.MigrationGeneratorTest do
825825
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))
826826
|> Enum.reject(&String.contains?(&1, "extensions"))
827827

828-
assert File.read!(file2) =~ ~S[rename table(:posts, prefix: "example"), :title, to: :name]
829-
assert File.read!(file2) =~ ~S[modify :title, :text, null: true, default: nil]
828+
contents = File.read!(file2)
829+
830+
[up_side, down_side] = String.split(contents, "def down", parts: 2)
831+
832+
up_side_parts =
833+
String.split(up_side, "\n", trim: true)
834+
|> Enum.map(&String.trim/1)
835+
836+
up_rename_index =
837+
Enum.find_index(up_side_parts, fn x ->
838+
x == ~S[rename table(:posts, prefix: "example"), :title, to: :name]
839+
end)
840+
841+
up_modify_index =
842+
Enum.find_index(up_side_parts, fn x ->
843+
x == ~S[modify :name, :text, null: false, default: "fred"]
844+
end)
845+
846+
assert up_rename_index < up_modify_index
847+
848+
down_side_parts =
849+
String.split(down_side, "\n", trim: true)
850+
|> Enum.map(&String.trim/1)
851+
852+
down_modify_index =
853+
Enum.find_index(down_side_parts, fn x ->
854+
x == ~S[modify :name, :text, null: true, default: nil]
855+
end)
856+
857+
down_rename_index =
858+
Enum.find_index(down_side_parts, fn x ->
859+
x == ~S[rename table(:posts, prefix: "example"), :name, to: :title]
860+
end)
861+
862+
assert down_modify_index < down_rename_index
830863
end
831864
end
832865

0 commit comments

Comments
 (0)