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
Jérome Perrin
gitlab-ce
Commits
479a15e7
Commit
479a15e7
authored
May 08, 2018
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add database foreign key between pipelines and builds
parent
0a46bf70
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
0 deletions
+65
-0
db/migrate/20180420010016_add_pipeline_build_foreign_key.rb
db/migrate/20180420010016_add_pipeline_build_foreign_key.rb
+24
-0
spec/migrations/add_pipeline_build_foreign_key_spec.rb
spec/migrations/add_pipeline_build_foreign_key_spec.rb
+30
-0
spec/support/helpers/migrations_helpers.rb
spec/support/helpers/migrations_helpers.rb
+11
-0
No files found.
db/migrate/20180420010016_add_pipeline_build_foreign_key.rb
0 → 100644
View file @
479a15e7
class
AddPipelineBuildForeignKey
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
disable_ddl_transaction!
def
up
execute
<<~
SQL
DELETE FROM ci_builds WHERE NOT EXISTS
(SELECT true FROM ci_pipelines WHERE ci_pipelines.id = ci_builds.commit_id)
SQL
return
if
foreign_key_exists?
(
:ci_builds
,
:ci_pipelines
,
column: :commit_id
)
add_concurrent_foreign_key
(
:ci_builds
,
:ci_pipelines
,
column: :commit_id
)
end
def
down
return
unless
foreign_key_exists?
(
:ci_builds
,
:ci_pipelines
,
column: :commit_id
)
remove_foreign_key
(
:ci_builds
,
column: :commit_id
)
end
end
spec/migrations/add_pipeline_build_foreign_key_spec.rb
0 → 100644
View file @
479a15e7
require
'spec_helper'
require
Rails
.
root
.
join
(
'db'
,
'migrate'
,
'20180420010016_add_pipeline_build_foreign_key.rb'
)
describe
AddPipelineBuildForeignKey
,
:migration
do
let
(
:namespaces
)
{
table
(
:namespaces
)
}
let
(
:projects
)
{
table
(
:projects
)
}
let
(
:pipelines
)
{
table
(
:ci_pipelines
)
}
let
(
:builds
)
{
table
(
:ci_builds
)
}
before
do
namespaces
.
create
(
id:
10
,
name:
'gitlab-org'
,
path:
'gitlab-org'
)
projects
.
create!
(
id:
11
,
namespace_id:
10
,
name:
'gitlab'
,
path:
'gitlab'
)
pipelines
.
create!
(
id:
12
,
project_id:
11
,
ref:
'master'
,
sha:
'adf43c3a'
)
builds
.
create!
(
id:
101
,
commit_id:
12
,
project_id:
11
)
builds
.
create!
(
id:
102
,
commit_id:
222
,
project_id:
11
)
builds
.
create!
(
id:
103
,
commit_id:
333
,
project_id:
11
)
builds
.
create!
(
id:
104
,
commit_id:
12
,
project_id:
11
)
end
it
'adds foreign key after removing orphans'
do
expect
(
builds
.
all
.
count
).
to
eq
4
expect
(
foreign_key_exists?
(
:ci_builds
,
:ci_pipelines
,
column: :commit_id
)).
to
be_falsey
migrate!
expect
(
builds
.
all
.
pluck
(
:id
)).
to
eq
[
101
,
104
]
expect
(
foreign_key_exists?
(
:ci_builds
,
:ci_pipelines
,
column: :commit_id
)).
to
be_truthy
end
end
spec/support/helpers/migrations_helpers.rb
View file @
479a15e7
...
...
@@ -24,6 +24,17 @@ module MigrationsHelpers
end
end
def
foreign_key_exists?
(
source
,
target
=
nil
,
column:
nil
)
ActiveRecord
::
Base
.
connection
.
foreign_keys
(
source
).
any?
do
|
key
|
if
column
key
.
options
[
:column
].
to_s
==
column
.
to_s
else
key
.
to_table
.
to_s
==
target
.
to_s
end
end
end
def
reset_column_in_all_models
clear_schema_cache!
...
...
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