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
35a3e918
Commit
35a3e918
authored
Dec 14, 2016
by
Nick Thomas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make the index on environment name and project id unique, fixing up any duplicates
parent
2d1dfae9
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
74 additions
and
3 deletions
+74
-3
db/migrate/20161207231620_fixup_environment_name_uniqueness.rb
...grate/20161207231620_fixup_environment_name_uniqueness.rb
+53
-0
db/migrate/20161207231621_create_environment_name_unique_index.rb
...te/20161207231621_create_environment_name_unique_index.rb
+18
-0
db/schema.rb
db/schema.rb
+3
-3
No files found.
db/migrate/20161207231620_fixup_environment_name_uniqueness.rb
0 → 100644
View file @
35a3e918
class
FixupEnvironmentNameUniqueness
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
true
DOWNTIME_REASON
=
'Renaming non-unique environments'
def
up
environments
=
Arel
::
Table
.
new
(
:environments
)
# Get all [project_id, name] pairs that occur more than once
finder_sql
=
environments
.
group
(
environments
[
:project_id
],
environments
[
:name
]).
having
(
Arel
.
sql
(
"COUNT(1)"
).
gt
(
1
)).
project
(
environments
[
:project_id
],
environments
[
:name
]).
to_sql
conflicting
=
connection
.
exec_query
(
finder_sql
)
conflicting
.
rows
.
each
do
|
project_id
,
name
|
fix_duplicates
(
project_id
,
name
)
end
end
def
down
# Nothing to do
end
# Rename conflicting environments by appending "-#{id}" to all but the first
def
fix_duplicates
(
project_id
,
name
)
environments
=
Arel
::
Table
.
new
(
:environments
)
finder_sql
=
environments
.
where
(
environments
[
:project_id
].
eq
(
project_id
)).
where
(
environments
[
:name
].
eq
(
name
)).
order
(
environments
[
:id
].
asc
).
project
(
environments
[
:id
],
environments
[
:name
]).
to_sql
# Now we have the data for all the conflicting rows
conflicts
=
connection
.
exec_query
(
finder_sql
).
rows
conflicts
.
shift
# Leave the first row alone
conflicts
.
each
do
|
id
,
name
|
update_sql
=
Arel
::
UpdateManager
.
new
(
ActiveRecord
::
Base
).
table
(
environments
).
set
(
environments
[
:name
]
=>
name
+
"-"
+
id
.
to_s
).
where
(
environments
[
:id
].
eq
(
id
)).
to_sql
connection
.
exec_update
(
update_sql
,
self
.
class
.
name
,
[])
end
end
end
db/migrate/20161207231621_create_environment_name_unique_index.rb
0 → 100644
View file @
35a3e918
class
CreateEnvironmentNameUniqueIndex
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
disable_ddl_transaction!
DOWNTIME
=
true
DOWNTIME_REASON
=
'Making a non-unique index into a unique index'
def
up
remove_index
:environments
,
[
:project_id
,
:name
]
add_concurrent_index
:environments
,
[
:project_id
,
:name
],
unique:
true
end
def
down
remove_index
:environments
,
[
:project_id
,
:name
],
unique:
true
add_concurrent_index
:environments
,
[
:project_id
,
:name
]
end
end
db/schema.rb
View file @
35a3e918
...
...
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
201612
12142807
)
do
ActiveRecord
::
Schema
.
define
(
version:
201612
07231621
)
do
# These are extensions that must be enabled in order to support this database
enable_extension
"plpgsql"
...
...
@@ -430,7 +430,7 @@ ActiveRecord::Schema.define(version: 20161212142807) do
t
.
string
"state"
,
default:
"available"
,
null:
false
end
add_index
"environments"
,
[
"project_id"
,
"name"
],
name:
"index_environments_on_project_id_and_name"
,
using: :btree
add_index
"environments"
,
[
"project_id"
,
"name"
],
name:
"index_environments_on_project_id_and_name"
,
u
nique:
true
,
u
sing: :btree
create_table
"events"
,
force: :cascade
do
|
t
|
t
.
string
"target_type"
...
...
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