Skip to content

Commit

Permalink
Fix backfill_column_for_type_change_in_background for cast expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Nov 16, 2023
1 parent 27507df commit 049b7f2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## master (unreleased)

- Fix `backfill_column_for_type_change_in_background` for cast expressions
- Fix copying indexes with long names when changing column type
- Enhance error messages with the link to the detailed description

Expand Down
18 changes: 12 additions & 6 deletions lib/online_migrations/background_migrations/copy_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ def process_batch(relation)
old_values = copy_from.map do |from_column|
old_value = arel_table[from_column]
if (type_cast_function = type_cast_functions[from_column])
if Utils.ar_version <= 5.2
# Active Record <= 5.2 does not support quoting of Arel::Nodes::NamedFunction
old_value = Arel.sql("#{type_cast_function}(#{connection.quote_column_name(from_column)})")
else
old_value = Arel::Nodes::NamedFunction.new(type_cast_function, [old_value])
end
old_value =
if type_cast_function =~ /\A\w+\z/
if Utils.ar_version <= 5.2
# Active Record <= 5.2 does not support quoting of Arel::Nodes::NamedFunction
Arel.sql("#{type_cast_function}(#{connection.quote_column_name(from_column)})")
else
Arel::Nodes::NamedFunction.new(type_cast_function, [old_value])
end
else
# We got a cast expression.
Arel.sql(type_cast_function)
end
end
old_value
end
Expand Down
8 changes: 8 additions & 0 deletions test/background_migrations/copy_column_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def test_process_batch_type_cast_function
assert_equal "value", @project1.settings_for_type_change["key"]
end

def test_process_batch_type_cast_expression
m = OnlineMigrations::BackgroundMigrations::CopyColumn.new(:projects, ["settings"], ["settings_for_type_change"], nil, { "settings" => "CAST(settings AS jsonb)" })
m.process_batch(m.relation)

@project1.reload
assert_equal "value", @project1.settings_for_type_change["key"]
end

def test_count
m = OnlineMigrations::BackgroundMigrations::CopyColumn.new(:projects, ["id"], ["id_for_type_change"])
assert_kind_of Integer, m.count
Expand Down

0 comments on commit 049b7f2

Please sign in to comment.