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
8e668076
Commit
8e668076
authored
Mar 05, 2018
by
Andreas Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve robustness of migration.
parent
d270b857
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
6 deletions
+39
-6
db/post_migrate/20180223124427_build_user_interacted_projects_table.rb
...te/20180223124427_build_user_interacted_projects_table.rb
+39
-6
No files found.
db/post_migrate/20180223124427_build_user_interacted_projects_table.rb
View file @
8e668076
...
...
@@ -13,23 +13,56 @@ class BuildUserInteractedProjectsTable < ActiveRecord::Migration
MysqlStrategy
.
new
end
.
up
add_concurrent_index
:user_interacted_projects
,
[
:project_id
,
:user_id
],
unique:
true
unless
index_exists?
(
:user_interacted_projects
,
[
:project_id
,
:user_id
])
add_concurrent_index
:user_interacted_projects
,
[
:project_id
,
:user_id
],
unique:
true
end
unless
foreign_key_exists?
(
:user_interacted_projects
,
:user_id
)
add_concurrent_foreign_key
:user_interacted_projects
,
:users
,
column: :user_id
,
on_delete: :cascade
end
add_concurrent_foreign_key
:user_interacted_projects
,
:users
,
column: :user_id
,
on_delete: :cascade
add_concurrent_foreign_key
:user_interacted_projects
,
:projects
,
column: :project_id
,
on_delete: :cascade
unless
foreign_key_exists?
(
:user_interacted_projects
,
:project_id
)
add_concurrent_foreign_key
:user_interacted_projects
,
:projects
,
column: :project_id
,
on_delete: :cascade
end
end
def
down
execute
"TRUNCATE user_interacted_projects"
remove_foreign_key
:user_interacted_projects
,
:users
remove_foreign_key
:user_interacted_projects
,
:projects
if
foreign_key_exists?
(
:user_interacted_projects
,
:user_id
)
remove_foreign_key
:user_interacted_projects
,
:users
end
if
foreign_key_exists?
(
:user_interacted_projects
,
:project_id
)
remove_foreign_key
:user_interacted_projects
,
:projects
end
remove_concurrent_index_by_name
:user_interacted_projects
,
'index_user_interacted_projects_on_project_id_and_user_id'
if
index_exists_by_name?
(
:user_interacted_projects
,
'index_user_interacted_projects_on_project_id_and_user_id'
)
remove_concurrent_index_by_name
:user_interacted_projects
,
'index_user_interacted_projects_on_project_id_and_user_id'
end
end
private
# Rails' index_exists? doesn't work when you only give it a table and index
# name. As such we have to use some extra code to check if an index exists for
# a given name.
def
index_exists_by_name?
(
table
,
index
)
indexes_for_table
[
table
].
include?
(
index
)
end
def
indexes_for_table
@indexes_for_table
||=
Hash
.
new
do
|
hash
,
table_name
|
hash
[
table_name
]
=
indexes
(
table_name
).
map
(
&
:name
)
end
end
def
foreign_key_exists?
(
table
,
column
)
foreign_keys
(
table
).
any?
do
|
key
|
key
.
options
[
:column
]
==
column
.
to_s
end
end
class
PostgresStrategy
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
...
...
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