Commit 9ac62f19 authored by Markus Koller's avatar Markus Koller

Merge branch '328261-rename-for-postgresql' into 'master'

Rename *_for_postgresql migration helpers

See merge request gitlab-org/gitlab!60462
parents 2df8eb00 12bee0c8
...@@ -14,7 +14,7 @@ class InitializeConversionOfEventsIdToBigint < ActiveRecord::Migration[6.0] ...@@ -14,7 +14,7 @@ class InitializeConversionOfEventsIdToBigint < ActiveRecord::Migration[6.0]
def down def down
trigger_name = rename_trigger_name(:events, :id, :id_convert_to_bigint) trigger_name = rename_trigger_name(:events, :id, :id_convert_to_bigint)
remove_rename_triggers_for_postgresql :events, trigger_name remove_rename_triggers :events, trigger_name
remove_column :events, :id_convert_to_bigint remove_column :events, :id_convert_to_bigint
end end
......
...@@ -14,7 +14,7 @@ class InitializeConversionOfPushEventPayloadsEventIdToBigint < ActiveRecord::Mig ...@@ -14,7 +14,7 @@ class InitializeConversionOfPushEventPayloadsEventIdToBigint < ActiveRecord::Mig
def down def down
trigger_name = rename_trigger_name(:push_event_payloads, :event_id, :event_id_convert_to_bigint) trigger_name = rename_trigger_name(:push_event_payloads, :event_id, :event_id_convert_to_bigint)
remove_rename_triggers_for_postgresql :push_event_payloads, trigger_name remove_rename_triggers :push_event_payloads, trigger_name
remove_column :push_event_payloads, :event_id_convert_to_bigint remove_column :push_event_payloads, :event_id_convert_to_bigint
end end
......
...@@ -10,7 +10,7 @@ class InitializeConversionOfCiBuildNeedsToBigint < ActiveRecord::Migration[6.0] ...@@ -10,7 +10,7 @@ class InitializeConversionOfCiBuildNeedsToBigint < ActiveRecord::Migration[6.0]
def down def down
trigger_name = rename_trigger_name(:ci_build_needs, :build_id, :build_id_convert_to_bigint) trigger_name = rename_trigger_name(:ci_build_needs, :build_id, :build_id_convert_to_bigint)
remove_rename_triggers_for_postgresql :ci_build_needs, trigger_name remove_rename_triggers :ci_build_needs, trigger_name
remove_column :ci_build_needs, :build_id_convert_to_bigint remove_column :ci_build_needs, :build_id_convert_to_bigint
end end
......
...@@ -13,7 +13,7 @@ class InitializeConversionOfCiJobArtifactsToBigint < ActiveRecord::Migration[6.0 ...@@ -13,7 +13,7 @@ class InitializeConversionOfCiJobArtifactsToBigint < ActiveRecord::Migration[6.0
def down def down
trigger_name = rename_trigger_name(TABLE, COLUMNS, TARGET_COLUMNS) trigger_name = rename_trigger_name(TABLE, COLUMNS, TARGET_COLUMNS)
remove_rename_triggers_for_postgresql TABLE, trigger_name remove_rename_triggers TABLE, trigger_name
TARGET_COLUMNS.each do |column| TARGET_COLUMNS.each do |column|
remove_column TABLE, column remove_column TABLE, column
......
...@@ -11,7 +11,7 @@ class InitializeConversionOfCiSourcesPipelinesSourceJobIdToBigint < ActiveRecord ...@@ -11,7 +11,7 @@ class InitializeConversionOfCiSourcesPipelinesSourceJobIdToBigint < ActiveRecord
def down def down
trigger_name = rename_trigger_name(:ci_sources_pipelines, :source_job_id, :source_job_id_convert_to_bigint) trigger_name = rename_trigger_name(:ci_sources_pipelines, :source_job_id, :source_job_id_convert_to_bigint)
remove_rename_triggers_for_postgresql :ci_sources_pipelines, trigger_name remove_rename_triggers :ci_sources_pipelines, trigger_name
remove_column :ci_sources_pipelines, :source_job_id_convert_to_bigint remove_column :ci_sources_pipelines, :source_job_id_convert_to_bigint
end end
......
...@@ -565,7 +565,7 @@ module Gitlab ...@@ -565,7 +565,7 @@ module Gitlab
check_trigger_permissions!(table) check_trigger_permissions!(table)
remove_rename_triggers_for_postgresql(table, trigger_name) remove_rename_triggers(table, trigger_name)
remove_column(table, new) remove_column(table, new)
end end
...@@ -576,8 +576,19 @@ module Gitlab ...@@ -576,8 +576,19 @@ module Gitlab
# table - The name of the table to install the trigger in. # table - The name of the table to install the trigger in.
# old_column - The name of the old column. # old_column - The name of the old column.
# new_column - The name of the new column. # new_column - The name of the new column.
def install_rename_triggers(table, old_column, new_column) # trigger_name - The name of the trigger to use (optional).
install_rename_triggers_for_postgresql(table, old_column, new_column) def install_rename_triggers(table, old, new, trigger_name: nil)
Gitlab::Database::UnidirectionalCopyTrigger.on_table(table).create(old, new, trigger_name: trigger_name)
end
# Removes the triggers used for renaming a column concurrently.
def remove_rename_triggers(table, trigger)
Gitlab::Database::UnidirectionalCopyTrigger.on_table(table).drop(trigger)
end
# Returns the (base) name to use for triggers when renaming columns.
def rename_trigger_name(table, old, new)
Gitlab::Database::UnidirectionalCopyTrigger.on_table(table).name(old, new)
end end
# Changes the type of a column concurrently. # Changes the type of a column concurrently.
...@@ -690,7 +701,7 @@ module Gitlab ...@@ -690,7 +701,7 @@ module Gitlab
check_trigger_permissions!(table) check_trigger_permissions!(table)
remove_rename_triggers_for_postgresql(table, trigger_name) remove_rename_triggers(table, trigger_name)
remove_column(table, old) remove_column(table, old)
end end
...@@ -982,7 +993,7 @@ module Gitlab ...@@ -982,7 +993,7 @@ module Gitlab
temporary_columns = columns.map { |column| convert_to_bigint_column(column) } temporary_columns = columns.map { |column| convert_to_bigint_column(column) }
trigger_name = rename_trigger_name(table, columns, temporary_columns) trigger_name = rename_trigger_name(table, columns, temporary_columns)
remove_rename_triggers_for_postgresql(table, trigger_name) remove_rename_triggers(table, trigger_name)
temporary_columns.each { |column| remove_column(table, column) } temporary_columns.each { |column| remove_column(table, column) }
end end
...@@ -1079,21 +1090,6 @@ module Gitlab ...@@ -1079,21 +1090,6 @@ module Gitlab
execute("DELETE FROM batched_background_migrations WHERE #{conditions}") execute("DELETE FROM batched_background_migrations WHERE #{conditions}")
end end
# Performs a concurrent column rename when using PostgreSQL.
def install_rename_triggers_for_postgresql(table, old, new, trigger_name: nil)
Gitlab::Database::UnidirectionalCopyTrigger.on_table(table).create(old, new, trigger_name: trigger_name)
end
# Removes the triggers used for renaming a PostgreSQL column concurrently.
def remove_rename_triggers_for_postgresql(table, trigger)
Gitlab::Database::UnidirectionalCopyTrigger.on_table(table).drop(trigger)
end
# Returns the (base) name to use for triggers when renaming columns.
def rename_trigger_name(table, old, new)
Gitlab::Database::UnidirectionalCopyTrigger.on_table(table).name(old, new)
end
# Returns an Array containing the indexes for the given column # Returns an Array containing the indexes for the given column
def indexes_for(table, column) def indexes_for(table, column)
column = column.to_s column = column.to_s
......
...@@ -835,7 +835,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -835,7 +835,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
it 'renames a column concurrently' do it 'renames a column concurrently' do
expect(model).to receive(:check_trigger_permissions!).with(:users) expect(model).to receive(:check_trigger_permissions!).with(:users)
expect(model).to receive(:install_rename_triggers_for_postgresql) expect(model).to receive(:install_rename_triggers)
.with(:users, :old, :new) .with(:users, :old, :new)
expect(model).to receive(:add_column) expect(model).to receive(:add_column)
...@@ -947,7 +947,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -947,7 +947,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
it 'reverses the operations of 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(:check_trigger_permissions!).with(:users)
expect(model).to receive(:remove_rename_triggers_for_postgresql) expect(model).to receive(:remove_rename_triggers)
.with(:users, /trigger_.{12}/) .with(:users, /trigger_.{12}/)
expect(model).to receive(:remove_column).with(:users, :new) expect(model).to receive(:remove_column).with(:users, :new)
...@@ -960,7 +960,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -960,7 +960,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
it 'cleans up the renaming procedure' do it 'cleans up the renaming procedure' do
expect(model).to receive(:check_trigger_permissions!).with(:users) expect(model).to receive(:check_trigger_permissions!).with(:users)
expect(model).to receive(:remove_rename_triggers_for_postgresql) expect(model).to receive(:remove_rename_triggers)
.with(:users, /trigger_.{12}/) .with(:users, /trigger_.{12}/)
expect(model).to receive(:remove_column).with(:users, :old) expect(model).to receive(:remove_column).with(:users, :old)
...@@ -1000,7 +1000,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -1000,7 +1000,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
it 'reverses the operations of cleanup_concurrent_column_rename' do it 'reverses the operations of cleanup_concurrent_column_rename' do
expect(model).to receive(:check_trigger_permissions!).with(:users) expect(model).to receive(:check_trigger_permissions!).with(:users)
expect(model).to receive(:install_rename_triggers_for_postgresql) expect(model).to receive(:install_rename_triggers)
.with(:users, :old, :new) .with(:users, :old, :new)
expect(model).to receive(:add_column) expect(model).to receive(:add_column)
...@@ -1095,7 +1095,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -1095,7 +1095,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
it 'reverses the operations of change_column_type_concurrently' do it 'reverses the operations of change_column_type_concurrently' do
expect(model).to receive(:check_trigger_permissions!).with(:users) expect(model).to receive(:check_trigger_permissions!).with(:users)
expect(model).to receive(:remove_rename_triggers_for_postgresql) expect(model).to receive(:remove_rename_triggers)
.with(:users, /trigger_.{12}/) .with(:users, /trigger_.{12}/)
expect(model).to receive(:remove_column).with(:users, "old_for_type_change") expect(model).to receive(:remove_column).with(:users, "old_for_type_change")
...@@ -1160,7 +1160,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -1160,7 +1160,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
expect(model).to receive(:rename_column) expect(model).to receive(:rename_column)
.with(:users, temp_undo_cleanup_column, :old) .with(:users, temp_undo_cleanup_column, :old)
expect(model).to receive(:install_rename_triggers_for_postgresql) expect(model).to receive(:install_rename_triggers)
.with(:users, :old, 'old_for_type_change') .with(:users, :old, 'old_for_type_change')
model.undo_cleanup_concurrent_column_type_change(:users, :old, :string) model.undo_cleanup_concurrent_column_type_change(:users, :old, :string)
...@@ -1186,7 +1186,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -1186,7 +1186,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
expect(model).to receive(:rename_column) expect(model).to receive(:rename_column)
.with(:users, temp_undo_cleanup_column, :old) .with(:users, temp_undo_cleanup_column, :old)
expect(model).to receive(:install_rename_triggers_for_postgresql) expect(model).to receive(:install_rename_triggers)
.with(:users, :old, 'old_for_type_change') .with(:users, :old, 'old_for_type_change')
model.undo_cleanup_concurrent_column_type_change( model.undo_cleanup_concurrent_column_type_change(
...@@ -1207,8 +1207,8 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -1207,8 +1207,8 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
end end
end end
describe '#install_rename_triggers_for_postgresql' do describe '#install_rename_triggers' do
it 'installs the triggers for PostgreSQL' do it 'installs the triggers' do
copy_trigger = double('copy trigger') copy_trigger = double('copy trigger')
expect(Gitlab::Database::UnidirectionalCopyTrigger).to receive(:on_table) expect(Gitlab::Database::UnidirectionalCopyTrigger).to receive(:on_table)
...@@ -1216,11 +1216,11 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -1216,11 +1216,11 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
expect(copy_trigger).to receive(:create).with(:old, :new, trigger_name: 'foo') expect(copy_trigger).to receive(:create).with(:old, :new, trigger_name: 'foo')
model.install_rename_triggers_for_postgresql(:users, :old, :new, trigger_name: 'foo') model.install_rename_triggers(:users, :old, :new, trigger_name: 'foo')
end end
end end
describe '#remove_rename_triggers_for_postgresql' do describe '#remove_rename_triggers' do
it 'removes the function and trigger' do it 'removes the function and trigger' do
copy_trigger = double('copy trigger') copy_trigger = double('copy trigger')
...@@ -1229,7 +1229,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -1229,7 +1229,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
expect(copy_trigger).to receive(:drop).with('foo') expect(copy_trigger).to receive(:drop).with('foo')
model.remove_rename_triggers_for_postgresql('bar', 'foo') model.remove_rename_triggers('bar', 'foo')
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment