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
918ababb
Commit
918ababb
authored
May 26, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add pipeline stages post deployment migration
parent
0f9fbae7
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
95 additions
and
11 deletions
+95
-11
db/migrate/20170525132202_create_pipeline_stages.rb
db/migrate/20170525132202_create_pipeline_stages.rb
+1
-1
db/post_migrate/20170526101042_migrate_pipeline_stages.rb
db/post_migrate/20170526101042_migrate_pipeline_stages.rb
+43
-0
db/schema.rb
db/schema.rb
+4
-1
spec/migrations/migrate_pipeline_stages_spec.rb
spec/migrations/migrate_pipeline_stages_spec.rb
+37
-7
spec/spec_helper.rb
spec/spec_helper.rb
+2
-2
spec/support/db_cleaner.rb
spec/support/db_cleaner.rb
+4
-0
spec/support/migrations_helpers.rb
spec/support/migrations_helpers.rb
+4
-0
No files found.
db/migrate/20170525132202_
migr
ate_pipeline_stages.rb
→
db/migrate/20170525132202_
cre
ate_pipeline_stages.rb
View file @
918ababb
class
Migr
atePipelineStages
<
ActiveRecord
::
Migration
class
Cre
atePipelineStages
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
DOWNTIME
=
false
...
...
db/post_migrate/20170526101042_migrate_pipeline_stages.rb
0 → 100644
View file @
918ababb
class
MigratePipelineStages
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
disable_ddl_transaction!
def
up
disable_statement_timeout
execute
<<-
SQL
.
strip_heredoc
INSERT INTO ci_stages (project_id, pipeline_id, name)
SELECT project_id, commit_id, stage FROM ci_builds
WHERE stage IS NOT NULL
GROUP BY project_id, commit_id, stage, stage_idx
ORDER BY stage_idx
SQL
add_concurrent_index
(
:ci_stages
,
[
:pipeline_id
,
:name
])
add_column
(
:ci_builds
,
:stage_id
,
:integer
)
stage_id
=
Arel
.
sql
(
'(SELECT id FROM ci_stages '
\
'WHERE ci_stages.pipeline_id = ci_builds.commit_id '
\
'AND ci_stages.name = ci_builds.stage)'
)
update_column_in_batches
(
:ci_builds
,
:stage_id
,
stage_id
)
# add_concurrent_foreign_key :ci_stages, :projects, column: :project_id, on_delete: :cascade
# add_concurrent_foreign_key :ci_builds, :ci_stages, column: :stage_id, on_delete: :cascade
end
def
down
execute
(
'TRUNCATE TABLE ci_stages'
)
if
column_exists?
(
:ci_builds
,
:stage_id
)
remove_column
(
:ci_builds
,
:stage_id
)
end
if
index_exists?
(
:ci_stages
,
[
:pipeline_id
,
:name
])
remove_index
(
:ci_stages
,
[
:pipeline_id
,
:name
])
end
end
end
db/schema.rb
View file @
918ababb
...
@@ -11,7 +11,7 @@
...
@@ -11,7 +11,7 @@
#
#
# It's strongly recommended that you check this file into your version control system.
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
2017052
513220
2
)
do
ActiveRecord
::
Schema
.
define
(
version:
2017052
610104
2
)
do
# These are extensions that must be enabled in order to support this database
# These are extensions that must be enabled in order to support this database
enable_extension
"plpgsql"
enable_extension
"plpgsql"
...
@@ -233,6 +233,7 @@ ActiveRecord::Schema.define(version: 20170525132202) do
...
@@ -233,6 +233,7 @@ ActiveRecord::Schema.define(version: 20170525132202) do
t
.
string
"coverage_regex"
t
.
string
"coverage_regex"
t
.
integer
"auto_canceled_by_id"
t
.
integer
"auto_canceled_by_id"
t
.
boolean
"retried"
t
.
boolean
"retried"
t
.
integer
"stage_id"
end
end
add_index
"ci_builds"
,
[
"auto_canceled_by_id"
],
name:
"index_ci_builds_on_auto_canceled_by_id"
,
using: :btree
add_index
"ci_builds"
,
[
"auto_canceled_by_id"
],
name:
"index_ci_builds_on_auto_canceled_by_id"
,
using: :btree
...
@@ -333,6 +334,8 @@ ActiveRecord::Schema.define(version: 20170525132202) do
...
@@ -333,6 +334,8 @@ ActiveRecord::Schema.define(version: 20170525132202) do
t
.
datetime
"updated_at"
t
.
datetime
"updated_at"
end
end
add_index
"ci_stages"
,
[
"pipeline_id"
,
"name"
],
name:
"index_ci_stages_on_pipeline_id_and_name"
,
using: :btree
create_table
"ci_trigger_requests"
,
force: :cascade
do
|
t
|
create_table
"ci_trigger_requests"
,
force: :cascade
do
|
t
|
t
.
integer
"trigger_id"
,
null:
false
t
.
integer
"trigger_id"
,
null:
false
t
.
text
"variables"
t
.
text
"variables"
...
...
spec/migrations/migrate_pipeline_stages_spec.rb
View file @
918ababb
require
'spec_helper'
require
'spec_helper'
require
Rails
.
root
.
join
(
'db'
,
'
migrate'
,
'2017052513220
2_migrate_pipeline_stages.rb'
)
require
Rails
.
root
.
join
(
'db'
,
'
post_migrate'
,
'2017052610104
2_migrate_pipeline_stages.rb'
)
describe
MigratePipelineStages
,
:migration
,
schema:
2017052
3091700
do
describe
MigratePipelineStages
,
:migration
,
schema:
2017052
5132202
do
##
##
# Create test data
# Create test data
- pipeline and CI/CD jobs.
#
#
let
(
:jobs
)
{
table
(
:ci_builds
)
}
let
(
:stages
)
{
table
(
:ci_stages
)
}
let
(
:pipelines
)
{
table
(
:ci_pipelines
)
}
before
do
before
do
table
(
:ci_pipelines
).
create!
(
ref:
'master'
,
sha:
'adf43c3a'
)
# Create CI/CD pipelines
#
pipelines
.
create!
(
id:
1
,
project_id:
123
,
ref:
'master'
,
sha:
'adf43c3a'
)
pipelines
.
create!
(
id:
2
,
project_id:
456
,
ref:
'feature'
,
sha:
'21a3deb'
)
# Create CI/CD jobs
#
jobs
.
create!
(
id:
1
,
commit_id:
1
,
project_id:
123
,
stage_idx:
2
,
stage:
'build'
)
jobs
.
create!
(
id:
2
,
commit_id:
1
,
project_id:
123
,
stage_idx:
2
,
stage:
'build'
)
jobs
.
create!
(
id:
3
,
commit_id:
1
,
project_id:
123
,
stage_idx:
1
,
stage:
'test'
)
jobs
.
create!
(
id:
4
,
commit_id:
1
,
project_id:
123
,
stage_idx:
1
,
stage:
'test'
)
jobs
.
create!
(
id:
5
,
commit_id:
1
,
project_id:
123
,
stage_idx:
3
,
stage:
'deploy'
)
jobs
.
create!
(
id:
6
,
commit_id:
2
,
project_id:
456
,
stage_idx:
3
,
stage:
'deploy'
)
jobs
.
create!
(
id:
7
,
commit_id:
2
,
project_id:
456
,
stage_idx:
2
,
stage:
'test:2'
)
jobs
.
create!
(
id:
8
,
commit_id:
2
,
project_id:
456
,
stage_idx:
1
,
stage:
'test:1'
)
jobs
.
create!
(
id:
9
,
commit_id:
2
,
project_id:
456
,
stage_idx:
1
,
stage:
'test:1'
)
jobs
.
create!
(
id:
10
,
commit_id:
2
,
project_id:
456
,
stage_idx:
2
,
stage:
'test:2'
)
jobs
.
create!
(
id:
11
,
commit_id:
3
,
project_id:
789
,
stage_idx:
3
,
stage:
'deploy'
)
end
end
it
'correctly migrates pipeline stages'
do
|
migration
,
meta
|
it
'correctly migrates pipeline stages'
do
expect
(
ActiveRecord
::
Base
.
connection
.
table_exists?
(
'ci_stages'
)).
to
eq
false
expect
(
stages
.
count
).
to
be_zero
migrate!
migrate!
expect
(
ActiveRecord
::
Base
.
connection
.
table_exists?
(
'ci_stages'
)).
to
eq
true
expect
(
stages
.
count
).
to
eq
7
expect
(
stages
.
all
.
pluck
(
:name
))
.
to
match_array
%w[test build deploy test:1 test:2 deploy deploy]
expect
(
stages
.
where
(
pipeline_id:
1
).
order
(
:id
).
pluck
(
:name
))
.
to
eq
%w[test build deploy]
expect
(
stages
.
where
(
pipeline_id:
2
).
order
(
:id
).
pluck
(
:name
))
.
to
eq
%w[test:1 test:2 deploy]
expect
(
jobs
.
where
(
stage_id:
nil
)).
to
be_empty
end
end
end
end
spec/spec_helper.rb
View file @
918ababb
...
@@ -95,8 +95,8 @@ RSpec.configure do |config|
...
@@ -95,8 +95,8 @@ RSpec.configure do |config|
end
end
config
.
around
(
:example
,
migration:
true
)
do
|
example
|
config
.
around
(
:example
,
migration:
true
)
do
|
example
|
ActiveRecord
::
Migrator
schema_version
=
example
.
metadata
.
fetch
(
:schema
)
.
migrate
(
migrations_paths
,
example
.
metadata
.
fetch
(
:schema
)
)
ActiveRecord
::
Migrator
.
migrate
(
migrations_paths
,
schema_version
)
example
.
run
example
.
run
...
...
spec/support/db_cleaner.rb
View file @
918ababb
...
@@ -19,6 +19,10 @@ RSpec.configure do |config|
...
@@ -19,6 +19,10 @@ RSpec.configure do |config|
DatabaseCleaner
.
strategy
=
:truncation
DatabaseCleaner
.
strategy
=
:truncation
end
end
config
.
before
(
:each
,
:migration
)
do
DatabaseCleaner
.
strategy
=
:truncation
end
config
.
before
(
:each
)
do
config
.
before
(
:each
)
do
DatabaseCleaner
.
start
DatabaseCleaner
.
start
end
end
...
...
spec/support/migrations_helpers.rb
View file @
918ababb
...
@@ -7,6 +7,10 @@ module MigrationsHelpers
...
@@ -7,6 +7,10 @@ module MigrationsHelpers
ActiveRecord
::
Migrator
.
migrations_paths
ActiveRecord
::
Migrator
.
migrations_paths
end
end
def
table_exists?
(
name
)
ActiveRecord
::
Base
.
connection
.
table_exists?
(
name
)
end
def
migrate!
def
migrate!
ActiveRecord
::
Migrator
.
up
(
migrations_paths
)
do
|
migration
|
ActiveRecord
::
Migrator
.
up
(
migrations_paths
)
do
|
migration
|
migration
.
name
==
described_class
.
name
migration
.
name
==
described_class
.
name
...
...
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