Skip to content
Snippets Groups Projects

Support multiple columns in column copy triggers

Merged Patrick Bair requested to merge pb-int-conversion-update-multiple-columns into master
1 unresolved thread
4 files
+ 313
59
Compare changes
  • Side-by-side
  • Inline
Files
4
@@ -577,17 +577,7 @@ def undo_rename_column_concurrently(table, old, new)
@@ -577,17 +577,7 @@ def undo_rename_column_concurrently(table, old, new)
# 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)
def install_rename_triggers(table, old_column, new_column)
trigger_name = rename_trigger_name(table, old_column, new_column)
install_rename_triggers_for_postgresql(nil, table, old_column, new_column)
quoted_table = quote_table_name(table)
quoted_old = quote_column_name(old_column)
quoted_new = quote_column_name(new_column)
install_rename_triggers_for_postgresql(
trigger_name,
quoted_table,
quoted_old,
quoted_new
)
end
end
# Changes the type of a column concurrently.
# Changes the type of a column concurrently.
@@ -1055,42 +1045,17 @@ def backfill_conversion_of_integer_to_bigint(
@@ -1055,42 +1045,17 @@ def backfill_conversion_of_integer_to_bigint(
# Performs a concurrent column rename when using PostgreSQL.
# Performs a concurrent column rename when using PostgreSQL.
def install_rename_triggers_for_postgresql(trigger, table, old, new)
def install_rename_triggers_for_postgresql(trigger, table, old, new)
execute <<-EOF.strip_heredoc
Gitlab::Database::UnidirectionalCopyTrigger.on_table(table).create(old, new, trigger_name: trigger)
CREATE OR REPLACE FUNCTION #{trigger}()
RETURNS trigger AS
$BODY$
BEGIN
NEW.#{new} := NEW.#{old};
RETURN NEW;
END;
$BODY$
LANGUAGE 'plpgsql'
VOLATILE
EOF
execute <<-EOF.strip_heredoc
DROP TRIGGER IF EXISTS #{trigger}
ON #{table}
EOF
execute <<-EOF.strip_heredoc
CREATE TRIGGER #{trigger}
BEFORE INSERT OR UPDATE
ON #{table}
FOR EACH ROW
EXECUTE FUNCTION #{trigger}()
EOF
end
end
# Removes the triggers used for renaming a PostgreSQL column concurrently.
# Removes the triggers used for renaming a PostgreSQL column concurrently.
def remove_rename_triggers_for_postgresql(table, trigger)
def remove_rename_triggers_for_postgresql(table, trigger)
execute("DROP TRIGGER IF EXISTS #{trigger} ON #{table}")
Gitlab::Database::UnidirectionalCopyTrigger.on_table(table).drop(trigger)
execute("DROP FUNCTION IF EXISTS #{trigger}()")
end
end
# Returns the (base) name to use for triggers when renaming columns.
# Returns the (base) name to use for triggers when renaming columns.
def rename_trigger_name(table, old, new)
def rename_trigger_name(table, old, new)
'trigger_' + Digest::SHA256.hexdigest("#{table}_#{old}_#{new}").first(12)
Gitlab::Database::UnidirectionalCopyTrigger.on_table(table).name(old, new)
end
end
# Returns an Array containing the indexes for the given column
# Returns an Array containing the indexes for the given column
Loading