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
5cd6f8c7
Commit
5cd6f8c7
authored
Jul 13, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a foreign key to `merge_requests.head_pipeline_id`
parent
9e5c8e5d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
44 additions
and
3 deletions
+44
-3
db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb
...grate/20170713104829_add_foreign_key_to_merge_requests.rb
+28
-0
db/schema.rb
db/schema.rb
+2
-1
lib/gitlab/database/migration_helpers.rb
lib/gitlab/database/migration_helpers.rb
+3
-1
spec/lib/gitlab/database/migration_helpers_spec.rb
spec/lib/gitlab/database/migration_helpers_spec.rb
+11
-1
No files found.
db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb
0 → 100644
View file @
5cd6f8c7
class
AddForeignKeyToMergeRequests
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
disable_ddl_transaction!
def
up
unless
foreign_key_exists?
(
:merge_requests
,
:head_pipeline_id
)
add_concurrent_foreign_key
(
:merge_requests
,
:ci_pipelines
,
column: :head_pipeline_id
,
on_delete: :nullify
)
end
end
def
down
if
foreign_key_exists?
(
:merge_requests
,
:head_pipeline_id
)
remove_foreign_key
(
:merge_requests
,
column: :head_pipeline_id
)
end
end
private
def
foreign_key_exists?
(
table
,
column
)
foreign_keys
(
table
).
any?
do
|
key
|
key
.
options
[
:column
]
==
column
.
to_s
end
end
end
db/schema.rb
View file @
5cd6f8c7
...
...
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
201707
07184244
)
do
ActiveRecord
::
Schema
.
define
(
version:
201707
13104829
)
do
# These are extensions that must be enabled in order to support this database
enable_extension
"plpgsql"
...
...
@@ -1615,6 +1615,7 @@ ActiveRecord::Schema.define(version: 20170707184244) do
add_foreign_key
"merge_request_diffs"
,
"merge_requests"
,
name:
"fk_8483f3258f"
,
on_delete: :cascade
add_foreign_key
"merge_request_metrics"
,
"ci_pipelines"
,
column:
"pipeline_id"
,
on_delete: :cascade
add_foreign_key
"merge_request_metrics"
,
"merge_requests"
,
on_delete: :cascade
add_foreign_key
"merge_requests"
,
"ci_pipelines"
,
column:
"head_pipeline_id"
,
name:
"fk_fd82eae0b9"
,
on_delete: :nullify
add_foreign_key
"merge_requests"
,
"projects"
,
column:
"target_project_id"
,
name:
"fk_a6963e8447"
,
on_delete: :cascade
add_foreign_key
"merge_requests_closing_issues"
,
"issues"
,
on_delete: :cascade
add_foreign_key
"merge_requests_closing_issues"
,
"merge_requests"
,
on_delete: :cascade
...
...
lib/gitlab/database/migration_helpers.rb
View file @
5cd6f8c7
...
...
@@ -140,6 +140,8 @@ module Gitlab
return
add_foreign_key
(
source
,
target
,
column:
column
,
on_delete:
on_delete
)
else
on_delete
=
'SET NULL'
if
on_delete
==
:nullify
end
disable_statement_timeout
...
...
@@ -155,7 +157,7 @@ module Gitlab
ADD CONSTRAINT
#{
key_name
}
FOREIGN KEY (
#{
column
}
)
REFERENCES
#{
target
}
(id)
#{
on_delete
?
"ON DELETE
#{
on_delete
}
"
:
''
}
#{
on_delete
?
"ON DELETE
#{
on_delete
.
upcase
}
"
:
''
}
NOT VALID;
EOF
...
...
spec/lib/gitlab/database/migration_helpers_spec.rb
View file @
5cd6f8c7
...
...
@@ -174,13 +174,23 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
allow
(
Gitlab
::
Database
).
to
receive
(
:mysql?
).
and_return
(
false
)
end
it
'creates a concurrent foreign key'
do
it
'creates a concurrent foreign key
and validates it
'
do
expect
(
model
).
to
receive
(
:disable_statement_timeout
)
expect
(
model
).
to
receive
(
:execute
).
ordered
.
with
(
/NOT VALID/
)
expect
(
model
).
to
receive
(
:execute
).
ordered
.
with
(
/VALIDATE CONSTRAINT/
)
model
.
add_concurrent_foreign_key
(
:projects
,
:users
,
column: :user_id
)
end
it
'appends a valid ON DELETE statement'
do
expect
(
model
).
to
receive
(
:disable_statement_timeout
)
expect
(
model
).
to
receive
(
:execute
).
with
(
/ON DELETE SET NULL/
)
expect
(
model
).
to
receive
(
:execute
).
ordered
.
with
(
/VALIDATE CONSTRAINT/
)
model
.
add_concurrent_foreign_key
(
:projects
,
:users
,
column: :user_id
,
on_delete: :nullify
)
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