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
378b2bad
Commit
378b2bad
authored
Feb 14, 2018
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate pipeline stages in batches instead of single row
parent
8488c98e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
64 deletions
+32
-64
lib/gitlab/background_migration/migrate_build_stage.rb
lib/gitlab/background_migration/migrate_build_stage.rb
+30
-54
spec/lib/gitlab/background_migration/migrate_build_stage_spec.rb
...b/gitlab/background_migration/migrate_build_stage_spec.rb
+2
-10
No files found.
lib/gitlab/background_migration/migrate_build_stage.rb
View file @
378b2bad
...
@@ -5,69 +5,45 @@
...
@@ -5,69 +5,45 @@
module
Gitlab
module
Gitlab
module
BackgroundMigration
module
BackgroundMigration
class
MigrateBuildStage
class
MigrateBuildStage
def
perform
(
id
)
module
Migratable
DatabaseBuild
.
find_by
(
id:
id
).
try
do
|
build
|
class
Stage
<
ActiveRecord
::
Base
MigratableStage
.
new
(
build
).
tap
do
|
stage
|
self
.
table_name
=
'ci_stages'
break
if
stage
.
exists?
||
stage
.
legacy?
stage
.
ensure!
stage
.
migrate_reference!
stage
.
migrate_status!
end
end
end
class
DatabaseStage
<
ActiveRecord
::
Base
self
.
table_name
=
'ci_stages'
end
class
DatabaseBuild
<
ActiveRecord
::
Base
self
.
table_name
=
'ci_builds'
end
class
MigratableStage
def
initialize
(
build
)
@build
=
build
end
def
exists?
@build
.
reload
.
stage_id
.
present?
end
end
##
class
Build
<
ActiveRecord
::
Base
# We can have some very old stages that do not have `ci_builds.stage` set.
self
.
table_name
=
'ci_builds'
#
# In that case we just don't migrate such stage.
#
def
legacy?
@build
.
stage
.
nil?
end
def
ensure!
def
ensure_stage!
find
||
create!
find
||
create!
end
rescue
ActiveRecord
::
RecordNotUnique
# TODO
end
def
find
def
find
DatabaseStage
.
find_by
(
name:
@build
.
stage
,
Stage
.
find_by
(
name:
self
.
stage
,
pipeline_id:
@build
.
commit_id
,
pipeline_id:
self
.
commit_id
,
project_id:
@build
.
project_id
)
project_id:
self
.
project_id
)
end
end
def
create!
def
create!
DatabaseStage
.
create!
(
name:
@build
.
stage
,
Stage
.
create!
(
name:
self
.
stage
||
'test'
,
pipeline_id:
@build
.
commit_id
,
pipeline_id:
self
.
commit_id
,
project_id:
@build
.
project_id
)
project_id:
self
.
project_id
)
end
end
end
end
def
migrate_reference!
def
perform
(
start_id
,
stop_id
)
MigrateBuildStageIdReference
.
new
.
perform
(
@build
.
id
,
@build
.
id
)
# TODO, should we disable_statement_timeout?
end
# TODO, use plain SQL query?
def
migrate_status!
stages
=
Migratable
::
Build
.
where
(
'stage_id IS NULL'
)
raise
ArgumentError
unless
exists?
.
where
(
"id BETWEEN
#{
start_id
.
to_i
}
AND
#{
stop_id
.
to_i
}
"
)
.
map
{
|
build
|
build
.
ensure_stage!
}
.
compact
.
map
(
&
:id
)
MigrateStageStatus
.
new
.
perform
(
@build
.
stage_id
,
@build
.
stage
_id
)
MigrateBuildStageIdReference
.
new
.
perform
(
start_id
,
stop
_id
)
end
MigrateStageStatus
.
new
.
perform
(
stages
.
min
,
stages
.
max
)
end
end
end
end
end
end
...
...
spec/lib/gitlab/background_migration/migrate_build_stage_spec.rb
View file @
378b2bad
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
BackgroundMigration
::
MigrateBuildStage
,
:migration
,
schema:
20180
105
101928
do
describe
Gitlab
::
BackgroundMigration
::
MigrateBuildStage
,
:migration
,
schema:
20180
212
101928
do
let
(
:projects
)
{
table
(
:projects
)
}
let
(
:projects
)
{
table
(
:projects
)
}
let
(
:pipelines
)
{
table
(
:ci_pipelines
)
}
let
(
:pipelines
)
{
table
(
:ci_pipelines
)
}
let
(
:stages
)
{
table
(
:ci_stages
)
}
let
(
:stages
)
{
table
(
:ci_stages
)
}
...
@@ -10,15 +10,9 @@ describe Gitlab::BackgroundMigration::MigrateBuildStage, :migration, schema: 201
...
@@ -10,15 +10,9 @@ describe Gitlab::BackgroundMigration::MigrateBuildStage, :migration, schema: 201
failed:
4
,
canceled:
5
,
skipped:
6
,
manual:
7
}.
freeze
failed:
4
,
canceled:
5
,
skipped:
6
,
manual:
7
}.
freeze
before
do
before
do
##
# Dependencies
#
projects
.
create!
(
id:
123
,
name:
'gitlab'
,
path:
'gitlab-ce'
)
projects
.
create!
(
id:
123
,
name:
'gitlab'
,
path:
'gitlab-ce'
)
pipelines
.
create!
(
id:
1
,
project_id:
123
,
ref:
'master'
,
sha:
'adf43c3a'
)
pipelines
.
create!
(
id:
1
,
project_id:
123
,
ref:
'master'
,
sha:
'adf43c3a'
)
##
# CI/CD jobs
#
jobs
.
create!
(
id:
1
,
commit_id:
1
,
project_id:
123
,
jobs
.
create!
(
id:
1
,
commit_id:
1
,
project_id:
123
,
stage_idx:
2
,
stage:
'build'
,
status: :success
)
stage_idx:
2
,
stage:
'build'
,
status: :success
)
jobs
.
create!
(
id:
2
,
commit_id:
1
,
project_id:
123
,
jobs
.
create!
(
id:
2
,
commit_id:
1
,
project_id:
123
,
...
@@ -36,9 +30,7 @@ describe Gitlab::BackgroundMigration::MigrateBuildStage, :migration, schema: 201
...
@@ -36,9 +30,7 @@ describe Gitlab::BackgroundMigration::MigrateBuildStage, :migration, schema: 201
it
'correctly migrates builds stages'
do
it
'correctly migrates builds stages'
do
expect
(
stages
.
count
).
to
be_zero
expect
(
stages
.
count
).
to
be_zero
jobs
.
all
.
find_each
do
|
job
|
described_class
.
new
.
perform
(
1
,
6
)
described_class
.
new
.
perform
(
job
.
id
)
end
expect
(
stages
.
count
).
to
eq
3
expect
(
stages
.
count
).
to
eq
3
expect
(
stages
.
all
.
pluck
(
:name
)).
to
match_array
%w[test build deploy]
expect
(
stages
.
all
.
pluck
(
:name
)).
to
match_array
%w[test build deploy]
...
...
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