Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
e4d42a62
Commit
e4d42a62
authored
Jun 21, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Raise if updating columns in batches within a transaction
parent
4e8d6507
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
21 deletions
+41
-21
lib/gitlab/database/migration_helpers.rb
lib/gitlab/database/migration_helpers.rb
+6
-0
spec/lib/gitlab/database/migration_helpers_spec.rb
spec/lib/gitlab/database/migration_helpers_spec.rb
+35
-21
No files found.
lib/gitlab/database/migration_helpers.rb
View file @
e4d42a62
...
@@ -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'
))
...
...
spec/lib/gitlab/database/migration_helpers_spec.rb
View file @
e4d42a62
...
@@ -262,39 +262,53 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
...
@@ -262,39 +262,53 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
end
end
describe
'#update_column_in_batches'
do
describe
'#update_column_in_batches'
do
before
do
context
'when running outside of a transaction'
do
create_list
(
:empty_project
,
5
)
before
do
end
expect
(
model
).
to
receive
(
:transaction_open?
).
and_return
(
false
)
it
'updates all the rows in a table'
do
create_list
(
:empty_project
,
5
)
model
.
update_column_in_batches
(
:projects
,
:import_error
,
'foo'
)
end
expect
(
Project
.
where
(
import_error:
'foo'
).
count
).
to
eq
(
5
)
it
'updates all the rows in a table'
do
end
model
.
update_column_in_batches
(
:projects
,
:import_error
,
'foo'
)
it
'updates boolean values correctly'
do
expect
(
Project
.
where
(
import_error:
'foo'
).
count
).
to
eq
(
5
)
model
.
update_column_in_batches
(
:projects
,
:archived
,
true
)
end
expect
(
Project
.
where
(
archived:
true
).
count
).
to
eq
(
5
)
it
'updates boolean values correctly'
do
end
model
.
update_column_in_batches
(
:projects
,
:archived
,
true
)
expect
(
Project
.
where
(
archived:
true
).
count
).
to
eq
(
5
)
end
context
'when a block is supplied'
do
context
'when a block is supplied'
do
it
'yields an Arel table and query object to the supplied block'
do
it
'yields an Arel table and query object to the supplied block'
do
first_id
=
Project
.
first
.
id
first_id
=
Project
.
first
.
id
model
.
update_column_in_batches
(
:projects
,
:archived
,
true
)
do
|
t
,
query
|
model
.
update_column_in_batches
(
:projects
,
:archived
,
true
)
do
|
t
,
query
|
query
.
where
(
t
[
:id
].
eq
(
first_id
))
query
.
where
(
t
[
:id
].
eq
(
first_id
))
end
expect
(
Project
.
where
(
archived:
true
).
count
).
to
eq
(
1
)
end
end
end
expect
(
Project
.
where
(
archived:
true
).
count
).
to
eq
(
1
)
context
'when the value is Arel.sql (Arel::Nodes::SqlLiteral)'
do
it
'updates the value as a SQL expression'
do
model
.
update_column_in_batches
(
:projects
,
:star_count
,
Arel
.
sql
(
'1+1'
))
expect
(
Project
.
sum
(
:star_count
)).
to
eq
(
2
*
Project
.
count
)
end
end
end
end
end
context
'when
the value is Arel.sql (Arel::Nodes::SqlLiteral)
'
do
context
'when
running inside the transaction
'
do
it
'
updates the value as a SQL expression
'
do
it
'
raises RuntimeError
'
do
model
.
update_column_in_batches
(
:projects
,
:star_count
,
Arel
.
sql
(
'1+1'
)
)
expect
(
model
).
to
receive
(
:transaction_open?
).
and_return
(
true
)
expect
(
Project
.
sum
(
:star_count
)).
to
eq
(
2
*
Project
.
count
)
expect
do
model
.
update_column_in_batches
(
:projects
,
:star_count
,
Arel
.
sql
(
'1+1'
))
end
.
to
raise_error
(
RuntimeError
)
end
end
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment