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
2deeac56
Commit
2deeac56
authored
Jul 18, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use batching to clear orphans in head_pipeline migration
parent
4aab52b2
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
3 deletions
+51
-3
db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb
...grate/20170713104829_add_foreign_key_to_merge_requests.rb
+12
-3
spec/migrations/add_foreign_key_to_merge_requests_spec.rb
spec/migrations/add_foreign_key_to_merge_requests_spec.rb
+39
-0
No files found.
db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb
View file @
2deeac56
...
...
@@ -5,15 +5,24 @@ class AddForeignKeyToMergeRequests < ActiveRecord::Migration
disable_ddl_transaction!
class
MergeRequest
<
ActiveRecord
::
Base
self
.
table_name
=
'merge_requests'
include
::
EachBatch
end
def
up
execute
<<-
SQL
.
strip_heredoc
UPDATE merge_requests SET head_pipeline_id = null
WHERE
NOT EXISTS (
scope
=
<<-
SQL
.
strip_heredoc
head_pipeline_id IS NOT NULL
AND
NOT EXISTS (
SELECT 1 FROM ci_pipelines
WHERE ci_pipelines.id = merge_requests.head_pipeline_id
)
SQL
MergeRequest
.
where
(
scope
).
each_batch
(
of:
1000
)
do
|
merge_requests
|
merge_requests
.
update_all
(
head_pipeline_id:
nil
)
end
unless
foreign_key_exists?
(
:merge_requests
,
:head_pipeline_id
)
add_concurrent_foreign_key
(
:merge_requests
,
:ci_pipelines
,
column: :head_pipeline_id
,
on_delete: :nullify
)
...
...
spec/migrations/add_foreign_key_to_merge_requests_spec.rb
0 → 100644
View file @
2deeac56
require
'spec_helper'
require
Rails
.
root
.
join
(
'db'
,
'migrate'
,
'20170713104829_add_foreign_key_to_merge_requests.rb'
)
describe
AddForeignKeyToMergeRequests
,
:migration
do
let
(
:projects
)
{
table
(
:projects
)
}
let
(
:merge_requests
)
{
table
(
:merge_requests
)
}
let
(
:pipelines
)
{
table
(
:ci_pipelines
)
}
before
do
projects
.
create!
(
name:
'gitlab'
,
path:
'gitlab-org/gitlab-ce'
)
pipelines
.
create!
(
project_id:
projects
.
first
.
id
,
ref:
'some-branch'
,
sha:
'abc12345'
)
# merge request without a pipeline
create_merge_request
(
head_pipeline_id:
nil
)
# merge request with non-existent pipeline
create_merge_request
(
head_pipeline_id:
1234
)
# merge reqeust with existing pipeline assigned
create_merge_request
(
head_pipeline_id:
pipelines
.
first
.
id
)
end
it
'correctly adds a foreign key to head_pipeline_id'
do
migrate!
expect
(
merge_requests
.
first
.
head_pipeline_id
).
to
be_nil
expect
(
merge_requests
.
second
.
head_pipeline_id
).
to
be_nil
expect
(
merge_requests
.
third
.
head_pipeline_id
).
to
eq
pipelines
.
first
.
id
end
def
create_merge_request
(
**
opts
)
merge_requests
.
create!
(
source_project_id:
projects
.
first
.
id
,
target_project_id:
projects
.
first
.
id
,
source_branch:
'some-branch'
,
target_branch:
'master'
,
**
opts
)
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