Skip to content

Commit 454ecdd

Browse files
authored
Merge pull request rails#54394 from fatkodima/fix-inverting-rename_enum_value
Fix inverting `rename_enum_value` when `:from`/`:to` are provided
2 parents 20cbe19 + af7bc30 commit 454ecdd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

activerecord/lib/active_record/migration/command_recorder.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ def invert_rename_enum_value(args)
382382
raise ActiveRecord::IrreversibleMigration, "rename_enum_value is only reversible if given a :from and :to option."
383383
end
384384

385-
[:rename_enum_value, [type_name, from: options[:to], to: options[:from]]]
385+
options[:to], options[:from] = options[:from], options[:to]
386+
[:rename_enum_value, [type_name, options]]
386387
end
387388

388389
def invert_drop_virtual_table(args)

activerecord/test/cases/adapters/postgresql/invertible_migration_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ def change
3535
end
3636
end
3737

38+
class RenameEnumValueMigration < SilentMigration
39+
def change
40+
rename_enum_value :color, from: "blue", to: "red"
41+
end
42+
end
43+
3844
class AddAndValidateCheckConstraint < SilentMigration
3945
def change
4046
add_check_constraint :settings, "value >= 0", name: "positive_value", validate: false
@@ -97,6 +103,17 @@ def test_migrate_revert_drop_enum
97103
assert_equal [["color", ["blue", "green"]]], @connection.enum_types
98104
end
99105

106+
def test_migrate_revert_rename_enum_value
107+
CreateEnumMigration.new.migrate(:up)
108+
assert_equal [["color", ["blue", "green"]]], @connection.enum_types
109+
110+
RenameEnumValueMigration.new.migrate(:up)
111+
assert_equal [["color", ["red", "green"]]], @connection.enum_types
112+
113+
RenameEnumValueMigration.new.migrate(:down)
114+
assert_equal [["color", ["blue", "green"]]], @connection.enum_types
115+
end
116+
100117
def test_migrate_revert_add_and_validate_check_constraint
101118
@connection.create_table(:settings) do |t|
102119
t.integer :value

0 commit comments

Comments
 (0)