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
Boxiang Sun
gitlab-ce
Commits
4d367dd4
Commit
4d367dd4
authored
Nov 17, 2017
by
Sean McGivern
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add computed update docs for update_column_in_batches
parent
d8be9814
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
1 deletion
+46
-1
doc/development/migration_style_guide.md
doc/development/migration_style_guide.md
+37
-1
lib/gitlab/database/migration_helpers.rb
lib/gitlab/database/migration_helpers.rb
+9
-0
No files found.
doc/development/migration_style_guide.md
View file @
4d367dd4
...
@@ -198,7 +198,43 @@ end
...
@@ -198,7 +198,43 @@ end
Keep in mind that this operation can easily take 10-15 minutes to complete on
Keep in mind that this operation can easily take 10-15 minutes to complete on
larger installations (e.g. GitLab.com). As a result you should only add default
larger installations (e.g. GitLab.com). As a result you should only add default
values if absolutely necessary.
values if absolutely necessary. There is a RuboCop cop that will fail if this
method is used on some tables that are very large on GitLab.com, which would
cause other issues.
## Updating an existing column
To update an existing column to a particular value, you can use
`update_column_in_batches`
(
`add_column_with_default`
uses this internally to
fill in the default value). This will split the updates into batches, so we
don't update too many rows at in a single statement.
This updates the column
`foo`
in the
`projects`
table to 10, where
`some_column`
is
`'hello'`
:
```
ruby
update_column_in_batches
(
:projects
,
:foo
,
10
)
do
|
table
,
query
|
query
.
where
(
table
[
:some_column
].
eq
(
'hello'
))
end
```
To perform a computed update, the value can be wrapped in
`Arel.sql`
, so Arel
treats it as an SQL literal. The below example is the same as the one above, but
the value is set to the product of the
`bar`
and
`baz`
columns:
```
ruby
update_value
=
Arel
.
sql
(
'bar * baz'
)
update_column_in_batches
(
:projects
,
:foo
,
update_value
)
do
|
table
,
query
|
query
.
where
(
table
[
:some_column
].
eq
(
'hello'
))
end
```
Like
`add_column_with_default`
, there is a RuboCop cop to detect usage of this
on large tables. In the case of
`update_column_in_batches`
, it may be acceptable
to run on a large table, as long as it is only updating a small subset of the
rows in the table, but do not ignore that without validating on the GitLab.com
staging environment - or asking someone else to do so for you - beforehand.
## Integer column type
## Integer column type
...
...
lib/gitlab/database/migration_helpers.rb
View file @
4d367dd4
...
@@ -220,6 +220,15 @@ module Gitlab
...
@@ -220,6 +220,15 @@ module Gitlab
# column - The name of the column to update.
# column - The name of the column to update.
# value - The value for the column.
# value - The value for the column.
#
#
# The `value` argument is typically a literal. To perform a computed
# update, an Arel literal can be used instead:
#
# update_value = Arel.sql('bar * baz')
#
# update_column_in_batches(:projects, :foo, update_value) do |table, query|
# query.where(table[:some_column].eq('hello'))
# end
#
# Rubocop's Metrics/AbcSize metric is disabled for this method as Rubocop
# Rubocop's Metrics/AbcSize metric is disabled for this method as Rubocop
# determines this method to be too complex while there's no way to make it
# determines this method to be too complex while there's no way to make it
# less "complex" without introducing extra methods (which actually will
# less "complex" without introducing extra methods (which actually will
...
...
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