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
1059b3ab
Commit
1059b3ab
authored
Mar 21, 2018
by
Andreas Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Guard creation/removal with existence checks.
parent
5f8abe47
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
6 deletions
+16
-6
db/migrate/20180320182229_add_indexes_for_user_activity_queries.rb
...e/20180320182229_add_indexes_for_user_activity_queries.rb
+16
-6
No files found.
db/migrate/20180320182229_add_indexes_for_user_activity_queries.rb
View file @
1059b3ab
...
...
@@ -6,25 +6,35 @@ class AddIndexesForUserActivityQueries < ActiveRecord::Migration
disable_ddl_transaction!
def
up
add_concurrent_index
:events
,
[
:author_id
,
:project_id
]
add_concurrent_index
:user_interacted_projects
,
:user_id
add_concurrent_index
:events
,
[
:author_id
,
:project_id
]
unless
index_exists?
(
:events
,
[
:author_id
,
:project_id
])
add_concurrent_index
:user_interacted_projects
,
:user_id
unless
index_exists?
(
:user_interacted_projects
,
:user_id
)
end
def
down
remove_concurrent_index
:events
,
[
:author_id
,
:project_id
]
remove_concurrent_index
:events
,
[
:author_id
,
:project_id
]
if
index_exists?
(
:events
,
[
:author_id
,
:project_id
])
patch_foreign_keys
do
remove_concurrent_index
:user_interacted_projects
,
:user_id
remove_concurrent_index
:user_interacted_projects
,
:user_id
if
index_exists?
(
:user_interacted_projects
,
:user_id
)
end
end
private
def
patch_foreign_keys
return
yield
if
Gitlab
::
Database
.
postgresql?
# MySQL doesn't like to remove the index with a foreign key using it.
remove_foreign_key
:user_interacted_projects
,
:users
unless
Gitlab
::
Database
.
postgresql?
remove_foreign_key
:user_interacted_projects
,
:users
if
fk_exists?
(
:user_interacted_projects
,
:user_id
)
yield
# Let's re-add the foreign key using the existing index on (user_id, project_id)
add_concurrent_foreign_key
:user_interacted_projects
,
:users
,
column: :user_id
unless
Gitlab
::
Database
.
postgresql?
add_concurrent_foreign_key
:user_interacted_projects
,
:users
,
column: :user_id
unless
fk_exists?
(
:user_interacted_projects
,
:user_id
)
end
def
fk_exists?
(
table
,
column
)
foreign_keys
(
table
).
any?
do
|
key
|
key
.
options
[
:column
]
==
column
.
to_s
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