summaryrefslogtreecommitdiff
path: root/db/migrate/20211118103439_remove_hardcoded_partition_from_loose_fk_trigger_function.rb
diff options
context:
space:
mode:
Diffstat (limited to 'db/migrate/20211118103439_remove_hardcoded_partition_from_loose_fk_trigger_function.rb')
-rw-r--r--db/migrate/20211118103439_remove_hardcoded_partition_from_loose_fk_trigger_function.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/db/migrate/20211118103439_remove_hardcoded_partition_from_loose_fk_trigger_function.rb b/db/migrate/20211118103439_remove_hardcoded_partition_from_loose_fk_trigger_function.rb
new file mode 100644
index 00000000000..33159167b19
--- /dev/null
+++ b/db/migrate/20211118103439_remove_hardcoded_partition_from_loose_fk_trigger_function.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+class RemoveHardcodedPartitionFromLooseFkTriggerFunction < Gitlab::Database::Migration[1.0]
+ include Gitlab::Database::MigrationHelpers::LooseForeignKeyHelpers
+
+ enable_lock_retries!
+
+ def up
+ execute(<<~SQL)
+ CREATE OR REPLACE FUNCTION #{DELETED_RECORDS_INSERT_FUNCTION_NAME}()
+ RETURNS TRIGGER AS
+ $$
+ BEGIN
+ INSERT INTO loose_foreign_keys_deleted_records
+ (fully_qualified_table_name, primary_key_value)
+ SELECT TG_TABLE_SCHEMA || '.' || TG_TABLE_NAME, old_table.id FROM old_table;
+
+ RETURN NULL;
+ END
+ $$ LANGUAGE PLPGSQL
+ SQL
+ end
+
+ def down
+ execute(<<~SQL)
+ CREATE OR REPLACE FUNCTION #{DELETED_RECORDS_INSERT_FUNCTION_NAME}()
+ RETURNS TRIGGER AS
+ $$
+ BEGIN
+ INSERT INTO loose_foreign_keys_deleted_records
+ (partition, fully_qualified_table_name, primary_key_value)
+ SELECT 1, TG_TABLE_SCHEMA || '.' || TG_TABLE_NAME, old_table.id FROM old_table
+ ON CONFLICT DO NOTHING;
+
+ RETURN NULL;
+ END
+ $$ LANGUAGE PLPGSQL
+ SQL
+ end
+end