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
Tatuya Kamada
gitlab-ce
Commits
ed70f29f
Commit
ed70f29f
authored
Nov 16, 2016
by
Yorick Peterse
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '24506-fix-timing-out-migration' into 'master'
Fix a badly-performing migration See merge request !7483
parents
6bf87395
16674d9b
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
23 deletions
+23
-23
db/post_migrate/20161109150329_fix_project_records_with_invalid_visibility.rb
...1109150329_fix_project_records_with_invalid_visibility.rb
+23
-23
No files found.
db/post_migrate/20161109150329_fix_project_records_with_invalid_visibility.rb
View file @
ed70f29f
class
FixProjectRecordsWithInvalidVisibility
<
ActiveRecord
::
Migration
class
FixProjectRecordsWithInvalidVisibility
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
include
Gitlab
::
Database
::
MigrationHelpers
BATCH_SIZE
=
10
00
BATCH_SIZE
=
5
00
DOWNTIME
=
false
DOWNTIME
=
false
# This migration is idempotent and there's no sense in throwing away the
# This migration is idempotent and there's no sense in throwing away the
...
@@ -12,34 +12,34 @@ class FixProjectRecordsWithInvalidVisibility < ActiveRecord::Migration
...
@@ -12,34 +12,34 @@ class FixProjectRecordsWithInvalidVisibility < ActiveRecord::Migration
projects
=
Arel
::
Table
.
new
(
:projects
)
projects
=
Arel
::
Table
.
new
(
:projects
)
namespaces
=
Arel
::
Table
.
new
(
:namespaces
)
namespaces
=
Arel
::
Table
.
new
(
:namespaces
)
finder
=
finder
_sql
=
projects
.
projects
.
join
(
namespaces
,
Arel
::
Nodes
::
InnerJoin
).
join
(
namespaces
,
Arel
::
Nodes
::
InnerJoin
).
on
(
projects
[
:namespace_id
].
eq
(
namespaces
[
:id
])).
on
(
projects
[
:namespace_id
].
eq
(
namespaces
[
:id
])).
where
(
projects
[
:visibility_level
].
gt
(
namespaces
[
:visibility_level
])).
where
(
projects
[
:visibility_level
].
gt
(
namespaces
[
:visibility_level
])).
project
(
projects
[
:id
]).
project
(
projects
[
:id
],
namespaces
[
:visibility_level
]).
take
(
BATCH_SIZE
)
take
(
BATCH_SIZE
).
to_sql
#
MySQL requires a derived table to perform this query
#
Update matching rows in batches. Each batch can cause up to 3 UPDATE
nested_finder
=
# statements, in addition to the SELECT: one per visibility_level
projects
.
loop
do
from
(
finder
.
as
(
"AS projects_inner"
)).
to_update
=
connection
.
exec_query
(
finder_sql
)
project
(
projects
[
:id
])
break
if
to_update
.
rows
.
count
==
0
valuer
=
# row[0] is projects.id, row[1] is namespaces.visibility_level
namespaces
.
updates
=
to_update
.
rows
.
each_with_object
(
Hash
.
new
{
|
h
,
k
|
h
[
k
]
=
[]
})
do
|
row
,
obj
|
where
(
namespaces
[
:id
].
eq
(
projects
[
:namespace_id
])).
obj
[
row
[
1
]]
<<
row
[
0
]
project
(
namespaces
[
:visibility_level
])
end
# Update matching rows until none remain. The finder contains a limit.
updates
.
each
do
|
visibility_level
,
project_ids
|
loop
do
updater
=
Arel
::
UpdateManager
.
new
(
ActiveRecord
::
Base
).
updater
=
Arel
::
UpdateManager
.
new
(
ActiveRecord
::
Base
).
table
(
projects
).
table
(
projects
).
set
(
projects
[
:visibility_level
]
=>
Arel
::
Nodes
::
SqlLiteral
.
new
(
"(
#{
valuer
.
to_sql
}
)"
)
).
set
(
projects
[
:visibility_level
]
=>
visibility_level
).
where
(
projects
[
:id
].
in
(
nested_finder
))
where
(
projects
[
:id
].
in
(
project_ids
))
num_updated
=
connection
.
exec_update
(
updater
.
to_sql
,
self
.
class
.
name
,
[])
ActiveRecord
::
Base
.
connection
.
exec_update
(
updater
.
to_sql
,
self
.
class
.
name
,
[])
break
if
num_updated
==
0
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