BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
10e732d2
Commit
10e732d2
authored
Jun 27, 2017
by
Lin Jen-Shin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename instead of delete, feedback:
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/12363#note_33449374
parent
6ff162cf
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
48 deletions
+47
-48
20170622135451_remove_duplicated_variable.rb
db/migrate/20170622135451_remove_duplicated_variable.rb
+0
-45
20170622135451_rename_duplicated_variable_key.rb
db/migrate/20170622135451_rename_duplicated_variable_key.rb
+35
-0
remove_duplicated_variable_spec.rb
spec/migrations/remove_duplicated_variable_spec.rb
+12
-3
No files found.
db/migrate/20170622135451_remove_duplicated_variable.rb
deleted
100644 → 0
View file @
6ff162cf
class
RemoveDuplicatedVariable
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
disable_ddl_transaction!
def
up
if
Gitlab
::
Database
.
postgresql?
execute
<<~
SQL
DELETE FROM ci_variables var USING (
#{
duplicated_ids
}
) dup
#{
join_conditions
}
SQL
else
execute
<<~
SQL
DELETE var FROM ci_variables var INNER JOIN (
#{
duplicated_ids
}
) dup
#{
join_conditions
}
SQL
end
end
def
down
# noop
end
def
duplicated_ids
<<~
SQL
SELECT MAX(id) AS id,
#{
key
}
, project_id
FROM ci_variables GROUP BY
#{
key
}
, project_id
SQL
end
def
join_conditions
<<~
SQL
WHERE var.key = dup.key
AND var.project_id = dup.project_id
AND var.id <> dup.id
SQL
end
def
key
# key needs to be quoted in MySQL
quote_column_name
(
'key'
)
end
end
db/migrate/20170622135451_rename_duplicated_variable_key.rb
0 → 100644
View file @
10e732d2
class
RenameDuplicatedVariableKey
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
disable_ddl_transaction!
def
up
execute
(
<<~
SQL
)
UPDATE ci_variables SET
#{
key
}
= CONCAT(
#{
key
}
,
#{
underscore
}
, id)
WHERE id IN (
SELECT * FROM ( -- MySQL requires an extra layer
SELECT dup.id FROM ci_variables dup
INNER JOIN (SELECT max(id) AS id,
#{
key
}
, project_id
FROM ci_variables tmp
GROUP BY
#{
key
}
, project_id) var
USING (
#{
key
}
, project_id) where dup.id <> var.id
) dummy
)
SQL
end
def
down
# noop
end
def
key
# key needs to be quoted in MySQL
quote_column_name
(
'key'
)
end
def
underscore
quote
(
'_'
)
end
end
spec/migrations/remove_duplicated_variable_spec.rb
View file @
10e732d2
require
'spec_helper'
require
Rails
.
root
.
join
(
'db'
,
'migrate'
,
'20170622135451_re
move_duplicated_variable
.rb'
)
require
Rails
.
root
.
join
(
'db'
,
'migrate'
,
'20170622135451_re
name_duplicated_variable_key
.rb'
)
describe
Re
moveDuplicatedVariable
,
:migration
do
describe
Re
nameDuplicatedVariableKey
,
:migration
do
let
(
:variables
)
{
table
(
:ci_variables
)
}
let
(
:projects
)
{
table
(
:projects
)
}
...
...
@@ -20,6 +20,15 @@ describe RemoveDuplicatedVariable, :migration do
it
'correctly remove duplicated records with smaller id'
do
migrate!
expect
(
variables
.
pluck
(
:id
)).
to
contain_exactly
(
1
,
2
,
6
,
7
,
8
)
expect
(
variables
.
pluck
(
:id
,
:key
)).
to
contain_exactly
(
[
1
,
'key1'
],
[
2
,
'key2'
],
[
3
,
'keyX_3'
],
[
4
,
'keyX_4'
],
[
5
,
'keyY_5'
],
[
6
,
'keyX'
],
[
7
,
'key7'
],
[
8
,
'keyY'
]
)
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