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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
f855f9b8
Commit
f855f9b8
authored
Aug 25, 2019
by
Reuben Pereira
Committed by
Stan Hu
Aug 25, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add helper to exactly undo cleanup_concurrent_column_rename
- Also add helper to undo rename_column_concurrently.
parent
8112fb37
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
148 additions
and
3 deletions
+148
-3
db/migrate/20190726101050_rename_allow_local_requests_from_hooks_and_services_application_setting.rb
...l_requests_from_hooks_and_services_application_setting.rb
+1
-1
db/post_migrate/20190801114109_cleanup_allow_local_requests_from_hooks_and_services_application_setting_rename.rb
...sts_from_hooks_and_services_application_setting_rename.rb
+1
-1
lib/gitlab/database/migration_helpers.rb
lib/gitlab/database/migration_helpers.rb
+40
-1
spec/lib/gitlab/database/migration_helpers_spec.rb
spec/lib/gitlab/database/migration_helpers_spec.rb
+106
-0
No files found.
db/migrate/20190726101050_rename_allow_local_requests_from_hooks_and_services_application_setting.rb
View file @
f855f9b8
...
...
@@ -12,6 +12,6 @@ class RenameAllowLocalRequestsFromHooksAndServicesApplicationSetting < ActiveRec
end
def
down
cleanup_concurrent_column_rename
:application_settings
,
:allow_local_requests_from_web_hooks_and_services
,
:allow_local_requests_from
_hooks_and_services
undo_rename_column_concurrently
:application_settings
,
:allow_local_requests_from_hooks_and_services
,
:allow_local_requests_from_web
_hooks_and_services
end
end
db/post_migrate/20190801114109_cleanup_allow_local_requests_from_hooks_and_services_application_setting_rename.rb
View file @
f855f9b8
...
...
@@ -12,6 +12,6 @@ class CleanupAllowLocalRequestsFromHooksAndServicesApplicationSettingRename < Ac
end
def
down
rename_column_concurrently
:application_settings
,
:allow_local_requests_from_web_hooks_and_services
,
:allow_local_requests_from
_hooks_and_services
undo_cleanup_concurrent_column_rename
:application_settings
,
:allow_local_requests_from_hooks_and_services
,
:allow_local_requests_from_web
_hooks_and_services
end
end
lib/gitlab/database/migration_helpers.rb
View file @
f855f9b8
...
...
@@ -470,7 +470,7 @@ module Gitlab
# We set the default value _after_ adding the column so we don't end up
# updating any existing data with the default value. This isn't
# necessary since we copy over old values further down.
change_column_default
(
table
,
new
,
old_col
.
default
)
if
old_col
.
default
change_column_default
(
table
,
new
,
old_col
.
default
)
unless
old_col
.
default
.
nil?
install_rename_triggers
(
table
,
old
,
new
)
...
...
@@ -482,6 +482,16 @@ module Gitlab
copy_foreign_keys
(
table
,
old
,
new
)
end
def
undo_rename_column_concurrently
(
table
,
old
,
new
)
trigger_name
=
rename_trigger_name
(
table
,
old
,
new
)
check_trigger_permissions!
(
table
)
remove_rename_triggers_for_postgresql
(
table
,
trigger_name
)
remove_column
(
table
,
new
)
end
# Installs triggers in a table that keep a new column in sync with an old
# one.
#
...
...
@@ -547,6 +557,35 @@ module Gitlab
remove_column
(
table
,
old
)
end
def
undo_cleanup_concurrent_column_rename
(
table
,
old
,
new
,
type:
nil
)
if
transaction_open?
raise
'undo_cleanup_concurrent_column_rename can not be run inside a transaction'
end
check_trigger_permissions!
(
table
)
new_column
=
column_for
(
table
,
new
)
add_column
(
table
,
old
,
type
||
new_column
.
type
,
limit:
new_column
.
limit
,
precision:
new_column
.
precision
,
scale:
new_column
.
scale
)
# We set the default value _after_ adding the column so we don't end up
# updating any existing data with the default value. This isn't
# necessary since we copy over old values further down.
change_column_default
(
table
,
old
,
new_column
.
default
)
unless
new_column
.
default
.
nil?
install_rename_triggers
(
table
,
old
,
new
)
update_column_in_batches
(
table
,
old
,
Arel
::
Table
.
new
(
table
)[
new
])
change_column_null
(
table
,
old
,
false
)
unless
new_column
.
null
copy_indexes
(
table
,
new
,
old
)
copy_foreign_keys
(
table
,
new
,
old
)
end
# Changes the column type of a table using a background migration.
#
# Because this method uses a background migration it's more suitable for
...
...
spec/lib/gitlab/database/migration_helpers_spec.rb
View file @
f855f9b8
...
...
@@ -576,6 +576,38 @@ describe Gitlab::Database::MigrationHelpers do
model
.
rename_column_concurrently
(
:users
,
:old
,
:new
)
end
context
'when default is false'
do
let
(
:old_column
)
do
double
(
:column
,
type: :boolean
,
limit:
nil
,
default:
false
,
null:
false
,
precision:
nil
,
scale:
nil
)
end
it
'copies the default to the new column'
do
expect
(
model
).
to
receive
(
:change_column_default
)
.
with
(
:users
,
:new
,
old_column
.
default
)
model
.
rename_column_concurrently
(
:users
,
:old
,
:new
)
end
end
end
end
describe
'#undo_rename_column_concurrently'
do
it
'reverses the operations of rename_column_concurrently'
do
expect
(
model
).
to
receive
(
:check_trigger_permissions!
).
with
(
:users
)
expect
(
model
).
to
receive
(
:remove_rename_triggers_for_postgresql
)
.
with
(
:users
,
/trigger_.{12}/
)
expect
(
model
).
to
receive
(
:remove_column
).
with
(
:users
,
:new
)
model
.
undo_rename_column_concurrently
(
:users
,
:old
,
:new
)
end
end
...
...
@@ -592,6 +624,80 @@ describe Gitlab::Database::MigrationHelpers do
end
end
describe
'#undo_cleanup_concurrent_column_rename'
do
context
'in a transaction'
do
it
'raises RuntimeError'
do
allow
(
model
).
to
receive
(
:transaction_open?
).
and_return
(
true
)
expect
{
model
.
undo_cleanup_concurrent_column_rename
(
:users
,
:old
,
:new
)
}
.
to
raise_error
(
RuntimeError
)
end
end
context
'outside a transaction'
do
let
(
:new_column
)
do
double
(
:column
,
type: :integer
,
limit:
8
,
default:
0
,
null:
false
,
precision:
5
,
scale:
1
)
end
let
(
:trigger_name
)
{
model
.
rename_trigger_name
(
:users
,
:old
,
:new
)
}
before
do
allow
(
model
).
to
receive
(
:transaction_open?
).
and_return
(
false
)
allow
(
model
).
to
receive
(
:column_for
).
and_return
(
new_column
)
end
it
'reverses the operations of cleanup_concurrent_column_rename'
do
expect
(
model
).
to
receive
(
:check_trigger_permissions!
).
with
(
:users
)
expect
(
model
).
to
receive
(
:install_rename_triggers_for_postgresql
)
.
with
(
trigger_name
,
'"users"'
,
'"old"'
,
'"new"'
)
expect
(
model
).
to
receive
(
:add_column
)
.
with
(
:users
,
:old
,
:integer
,
limit:
new_column
.
limit
,
precision:
new_column
.
precision
,
scale:
new_column
.
scale
)
expect
(
model
).
to
receive
(
:change_column_default
)
.
with
(
:users
,
:old
,
new_column
.
default
)
expect
(
model
).
to
receive
(
:update_column_in_batches
)
expect
(
model
).
to
receive
(
:change_column_null
).
with
(
:users
,
:old
,
false
)
expect
(
model
).
to
receive
(
:copy_indexes
).
with
(
:users
,
:new
,
:old
)
expect
(
model
).
to
receive
(
:copy_foreign_keys
).
with
(
:users
,
:new
,
:old
)
model
.
undo_cleanup_concurrent_column_rename
(
:users
,
:old
,
:new
)
end
context
'when default is false'
do
let
(
:new_column
)
do
double
(
:column
,
type: :boolean
,
limit:
nil
,
default:
false
,
null:
false
,
precision:
nil
,
scale:
nil
)
end
it
'copies the default to the old column'
do
expect
(
model
).
to
receive
(
:change_column_default
)
.
with
(
:users
,
:old
,
new_column
.
default
)
model
.
undo_cleanup_concurrent_column_rename
(
:users
,
:old
,
:new
)
end
end
end
end
describe
'#change_column_type_concurrently'
do
it
'changes the column type'
do
expect
(
model
).
to
receive
(
:rename_column_concurrently
)
...
...
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