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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
2917f486
Commit
2917f486
authored
Jul 06, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract `execute_in_batches` migrations helper
parent
e036a374
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
22 deletions
+60
-22
lib/gitlab/database/migration_helpers.rb
lib/gitlab/database/migration_helpers.rb
+24
-15
spec/lib/gitlab/database/migration_helpers_spec.rb
spec/lib/gitlab/database/migration_helpers_spec.rb
+36
-7
No files found.
lib/gitlab/database/migration_helpers.rb
View file @
2917f486
...
@@ -248,7 +248,7 @@ module Gitlab
...
@@ -248,7 +248,7 @@ module Gitlab
# rows for GitLab.com.
# rows for GitLab.com.
batch_size
=
max_size
if
batch_size
>
max_size
batch_size
=
max_size
if
batch_size
>
max_size
walk_tabl
e_in_batches
(
table
,
of:
batch_size
,
scope:
scope
)
do
execut
e_in_batches
(
table
,
of:
batch_size
,
scope:
scope
)
do
Arel
::
UpdateManager
.
new
(
ActiveRecord
::
Base
)
Arel
::
UpdateManager
.
new
(
ActiveRecord
::
Base
)
.
table
(
table_arel
)
.
table
(
table_arel
)
.
set
([[
table_arel
[
column
],
value
]])
.
set
([[
table_arel
[
column
],
value
]])
...
@@ -281,23 +281,32 @@ module Gitlab
...
@@ -281,23 +281,32 @@ module Gitlab
stop_id
=
exec_query
(
stop_arel
.
to_sql
)
stop_id
=
exec_query
(
stop_arel
.
to_sql
)
.
to_hash
.
first
.
to_h
[
'id'
].
to_i
.
to_hash
.
first
.
to_h
[
'id'
].
to_i
action
=
yield
(
batch
,
start_id
,
stop_id
)
yield
batch
,
start_id
,
stop_id
if
action
.
is_a?
(
Arel
::
TreeManager
)
stop_id
.
zero?
?
break
:
start_id
=
stop_id
exec_arel
=
action
.
where
(
table
[
:id
].
gteq
(
start_id
))
end
exec_arel
=
exec_arel
.
where
(
table
[
:id
].
lt
(
stop_id
))
if
stop_id
.
nonzero?
exec_arel
=
scope
.
call
(
table
,
exec_arel
)
if
scope
execute
(
exec_arel
.
to_sql
)
end
end
if
stop_id
.
zero?
def
execute_in_batches
(
table
,
of:
1000
,
scope:
nil
)
# there are no more rows left to update
if
transaction_open?
break
raise
<<-
MSG
else
execute_in_batches helper can not be run inside a transaction.
# next loop
You can disable transactions by calling `disable_ddl_transaction!`
start_id
=
stop_id
method in the body of your migration class.
MSG
end
end
# raise 'This method requires a block!' unless block_given?
table_arel
=
Arel
::
Table
.
new
(
table
)
walk_table_in_batches
(
table
,
of:
of
,
scope:
scope
)
do
|
_batch
,
start_id
,
stop_id
|
exec_arel
=
yield
table_arel
exec_arel
=
exec_arel
.
where
(
table_arel
[
:id
].
gteq
(
start_id
))
exec_arel
=
exec_arel
.
where
(
table_arel
[
:id
].
lt
(
stop_id
))
if
stop_id
.
nonzero?
exec_arel
=
scope
.
call
(
table_arel
,
exec_arel
)
if
scope
execute
(
exec_arel
.
to_sql
)
end
end
end
end
...
...
spec/lib/gitlab/database/migration_helpers_spec.rb
View file @
2917f486
...
@@ -262,7 +262,8 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
...
@@ -262,7 +262,8 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
describe
'#update_column_in_batches'
do
describe
'#update_column_in_batches'
do
context
'when running outside of a transaction'
do
context
'when running outside of a transaction'
do
before
do
before
do
expect
(
model
).
to
receive
(
:transaction_open?
).
twice
.
and_return
(
false
)
expect
(
model
).
to
receive
(
:transaction_open?
)
.
at_least
(
:once
).
and_return
(
false
)
create_list
(
:empty_project
,
5
)
create_list
(
:empty_project
,
5
)
end
end
...
@@ -336,10 +337,39 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
...
@@ -336,10 +337,39 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
first_id
=
Project
.
first
.
id
first_id
=
Project
.
first
.
id
scope
=
->
(
table
,
query
)
{
query
.
where
(
table
[
:id
].
eq
(
first_id
))
}
scope
=
->
(
table
,
query
)
{
query
.
where
(
table
[
:id
].
eq
(
first_id
))
}
model
.
walk_table_in_batches
(
:projects
,
scope:
scope
)
do
expect
{
|
b
|
model
.
walk_table_in_batches
(
:projects
,
of:
1
,
scope:
scope
,
&
b
)
}
.
to
yield_control
.
exactly
(
:once
)
end
end
end
context
'when running inside the transaction'
do
it
'raises RuntimeError'
do
expect
(
model
).
to
receive
(
:transaction_open?
).
and_return
(
true
)
expect
{
model
.
walk_table_in_batches
(
:projects
,
of:
2
)
}
.
to
raise_error
(
RuntimeError
)
end
end
end
describe
'#execute_in_batches'
do
context
'when running outside of a transaction'
do
before
do
expect
(
model
).
to
receive
(
:transaction_open?
)
.
at_least
(
:once
).
and_return
(
false
)
create_list
(
:empty_project
,
6
)
end
context
'when a scope is provided'
do
it
'limits the scope of the statement provided inside the block'
do
first_id
=
Project
.
first
.
id
scope
=
->
(
table
,
query
)
{
query
.
where
(
table
[
:id
].
eq
(
first_id
))
}
model
.
execute_in_batches
(
:projects
,
scope:
scope
)
do
|
table
|
Arel
::
UpdateManager
.
new
(
ActiveRecord
::
Base
)
Arel
::
UpdateManager
.
new
(
ActiveRecord
::
Base
)
.
table
(
Arel
::
Table
.
new
(
:projects
))
.
table
(
table
).
set
([[
table
[
:archived
],
true
]])
.
set
([[
Arel
::
Table
.
new
(
:projects
)[
:archived
],
true
]])
end
end
expect
(
Project
.
where
(
archived:
true
).
count
).
to
eq
(
1
)
expect
(
Project
.
where
(
archived:
true
).
count
).
to
eq
(
1
)
...
@@ -351,9 +381,8 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
...
@@ -351,9 +381,8 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
it
'raises RuntimeError'
do
it
'raises RuntimeError'
do
expect
(
model
).
to
receive
(
:transaction_open?
).
and_return
(
true
)
expect
(
model
).
to
receive
(
:transaction_open?
).
and_return
(
true
)
expect
do
expect
{
model
.
execute_in_batches
(
:projects
)}
model
.
update_column_in_batches
(
:projects
,
:star_count
,
Arel
.
sql
(
'1+1'
))
.
to
raise_error
(
RuntimeError
)
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