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
664dcd97
Commit
664dcd97
authored
Oct 21, 2020
by
Russell Dickenson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed syntax issue in code sample
parent
88cf1d73
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
8 deletions
+8
-8
doc/development/database/setting_multiple_values.md
doc/development/database/setting_multiple_values.md
+8
-8
No files found.
doc/development/database/setting_multiple_values.md
View file @
664dcd97
...
...
@@ -2,8 +2,8 @@
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/32921) in GitLab 13.5.
Frequently, we will want
to update multiple objects with new values for one
or more columns.
The obvious way to do
this is using
`Relation#update_all`
:
There's often a need
to update multiple objects with new values for one
or more columns.
One method of doing
this is using
`Relation#update_all`
:
```
ruby
user
.
issues
.
open
.
update_all
(
due_date:
7
.
days
.
from_now
)
# (1)
...
...
@@ -28,11 +28,11 @@ update issues
where
id
=
obj_id
```
The bad news:
T
here is no way to express this in ActiveRecord or even dropping
down to ARel
- the
`UpdateManager`
just
does not support
`update from`
, so this
The bad news:
t
here is no way to express this in ActiveRecord or even dropping
down to ARel
. The
`UpdateManager`
does not support
`update from`
, so this
is not expressible.
The good news:
W
e supply an abstraction to help you generate these kinds of
The good news:
w
e supply an abstraction to help you generate these kinds of
updates, called
`Gitlab::Database::BulkUpdate`
. This constructs queries such as the
above, and uses binding parameters to avoid SQL injection.
...
...
@@ -44,7 +44,7 @@ To use this, we need:
-
a mapping from object/ID to the new values to set for that object
-
a way to determine the table for each object
So f
or example, we can express the query above as:
F
or example, we can express the query above as:
```
ruby
issue_a
=
Issue
.
find
(
..
)
...
...
@@ -87,7 +87,7 @@ objects = Foo.from_union([
])
# At this point, all the objects are instances of Foo, even the ones from the
# Bar table
mapping
=
objects
.
to_h
{
|
obj
|
[
obj
,
bazzes
[
obj
.
id
]
}
mapping
=
objects
.
to_h
{
|
obj
|
[
obj
,
bazzes
[
obj
.
id
]
]
}
# Issues at most 2 queries
::
Gitlab
::
Database
::
BulkUpdate
.
execute
(
%i[baz]
,
mapping
)
do
|
obj
|
...
...
@@ -100,4 +100,4 @@ end
Note that this is a
**very low level**
tool, and operates on the raw column
values. Enumerations and state fields must be translated into their underlying
representations, for example, and nested associations are not supported. No
validations or hooks
will b
e called.
validations or hooks
ar
e called.
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