Skip to content

Commit

Permalink
Add ability to use custom raw sql for `backfill_column_for_type_chang…
Browse files Browse the repository at this point in the history
…e`'s `type_cast_function`

Fixes #36.
  • Loading branch information
fatkodima committed Oct 26, 2023
1 parent 15494ed commit 25568d5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## master (unreleased)

- Add ability to use custom raw sql for `backfill_column_for_type_change`'s `type_cast_function`

```ruby
backfill_column_for_type_change(:users, :company_id, type_cast_function: Arel.sql("company_id::integer"))
```

- Fix version safety with `revert`

## 0.8.2 (2023-09-26)
Expand Down
9 changes: 8 additions & 1 deletion lib/online_migrations/change_column_type_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def revert_initialize_columns_type_change(table_name, columns_and_types, **_opti
#
# @example With type casting
# backfill_column_for_type_change(:users, :settings, type_cast_function: "jsonb")
# backfill_column_for_type_change(:users, :company_id, type_cast_function: Arel.sql("company_id::integer"))
#
# @example Additional batch options
# backfill_column_for_type_change(:files, :size, batch_size: 10_000)
Expand All @@ -202,7 +203,13 @@ def backfill_columns_for_type_change(table_name, *column_names, type_cast_functi

old_value = Arel::Table.new(table_name)[column_name]
if (type_cast_function = type_cast_functions.with_indifferent_access[column_name])
old_value = Arel::Nodes::NamedFunction.new(type_cast_function.to_s, [old_value])
old_value =
case type_cast_function
when Arel::Nodes::SqlLiteral
type_cast_function
else
Arel::Nodes::NamedFunction.new(type_cast_function.to_s, [old_value])
end
end

[tmp_column, old_value]
Expand Down
10 changes: 10 additions & 0 deletions test/schema_statements/changing_column_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def setup
t.text :settings
t.bigint :star_count
t.integer :user_id
t.string :company_id

t.index :name
t.foreign_key :users
Expand Down Expand Up @@ -207,6 +208,15 @@ def test_backfill_column_for_type_change_type_cast_function
assert_equal "value", p.reload.settings["key"]
end

def test_backfill_column_for_type_change_type_cast_function_as_literal
clear_caches
p = Project.create!(description: "Required description", company_id: "1")

change_column_type(:projects, :company_id, :integer, type_cast_function: Arel.sql("company_id::integer"))
Project.reset_column_information
assert_equal 1, p.reload.company_id
end

def test_backfill_column_for_type_change_in_background
@connection.initialize_column_type_change(:projects, :name, :string)
m = @connection.backfill_column_for_type_change_in_background(
Expand Down

1 comment on commit 25568d5

@jjb
Copy link

@jjb jjb commented on 25568d5 Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤩 🤩 🤩
🙏 🙏 🙏
🙇 🙇 🙇

Please sign in to comment.