Commit e4d42a62 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Raise if updating columns in batches within a transaction

parent 4e8d6507
...@@ -222,6 +222,12 @@ module Gitlab ...@@ -222,6 +222,12 @@ module Gitlab
# #
# rubocop: disable Metrics/AbcSize # rubocop: disable Metrics/AbcSize
def update_column_in_batches(table, column, value) def update_column_in_batches(table, column, value)
if transaction_open?
raise 'update_column_in_batches can not be run inside a transaction, ' \
'you can disable transactions by calling disable_ddl_transaction! ' \
'in the body of your migration class'
end
table = Arel::Table.new(table) table = Arel::Table.new(table)
count_arel = table.project(Arel.star.count.as('count')) count_arel = table.project(Arel.star.count.as('count'))
......
...@@ -262,7 +262,10 @@ describe Gitlab::Database::MigrationHelpers, lib: true do ...@@ -262,7 +262,10 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
end end
describe '#update_column_in_batches' do describe '#update_column_in_batches' do
context 'when running outside of a transaction' do
before do before do
expect(model).to receive(:transaction_open?).and_return(false)
create_list(:empty_project, 5) create_list(:empty_project, 5)
end end
...@@ -299,6 +302,17 @@ describe Gitlab::Database::MigrationHelpers, lib: true do ...@@ -299,6 +302,17 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
end end
end end
context 'when running inside the transaction' do
it 'raises RuntimeError' do
expect(model).to receive(:transaction_open?).and_return(true)
expect do
model.update_column_in_batches(:projects, :star_count, Arel.sql('1+1'))
end.to raise_error(RuntimeError)
end
end
end
describe '#add_column_with_default' do describe '#add_column_with_default' do
context 'outside of a transaction' do context 'outside of a transaction' do
context 'when a column limit is not set' do context 'when a column limit is not set' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment