Skip to content

Commit

Permalink
Fix copying column background migration to work with primary keys add…
Browse files Browse the repository at this point in the history
…ed via `initialize_column_type_change`

Fixes #52.
  • Loading branch information
fatkodima committed Mar 28, 2024
1 parent 0a1f67f commit 3a84623
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## master (unreleased)

- Fix copying column background migration to work with primary keys added via `initialize_column_type_change`

## 0.16.0 (2024-03-28)

- Add support for asynchronous creation/removal of indexes
Expand Down
5 changes: 1 addition & 4 deletions lib/online_migrations/background_migrations/copy_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ def initialize(table_name, copy_from, copy_to, model_name = nil, type_cast_funct
end

def relation
model
.unscoped
.where(copy_to.index_with(nil))
.where.not(copy_from.index_with(nil))
model.unscoped
end

def process_batch(relation)
Expand Down
13 changes: 13 additions & 0 deletions test/background_migrations/copy_column_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ def test_relation
assert_kind_of ActiveRecord::Relation, m.relation
end

def test_relation_considers_all_rows
Project.update_all(id_for_type_change: 0) # is typically set via initialize_column_type_change

m = OnlineMigrations::BackgroundMigrations::CopyColumn.new(:projects, ["id"], ["id_for_type_change"])
m.process_batch(m.relation)

@project1.reload
@project2.reload

assert_equal @project1.id, @project1.id_for_type_change
assert_equal @project2.id, @project2.id_for_type_change
end

def test_process_batch
m = OnlineMigrations::BackgroundMigrations::CopyColumn.new(:projects, ["id"], ["id_for_type_change"], Project.name)
m.process_batch(m.relation)
Expand Down

0 comments on commit 3a84623

Please sign in to comment.