summaryrefslogtreecommitdiff
path: root/db/migrate/20220213100000_remove_integration_type_triggers.rb
diff options
context:
space:
mode:
Diffstat (limited to 'db/migrate/20220213100000_remove_integration_type_triggers.rb')
-rw-r--r--db/migrate/20220213100000_remove_integration_type_triggers.rb32
1 files changed, 32 insertions, 0 deletions
diff --git a/db/migrate/20220213100000_remove_integration_type_triggers.rb b/db/migrate/20220213100000_remove_integration_type_triggers.rb
new file mode 100644
index 00000000000..137e5648125
--- /dev/null
+++ b/db/migrate/20220213100000_remove_integration_type_triggers.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+class RemoveIntegrationTypeTriggers < Gitlab::Database::Migration[1.0]
+ include Gitlab::Database::SchemaHelpers
+
+ FUNCTION_NAME = 'integrations_set_type_new'
+ TRIGGER_ON_INSERT_NAME = 'trigger_type_new_on_insert'
+
+ def up
+ drop_trigger(:integrations, TRIGGER_ON_INSERT_NAME)
+ drop_function(FUNCTION_NAME)
+ end
+
+ def down
+ create_trigger_function(FUNCTION_NAME, replace: true) do
+ <<~SQL.squish
+ UPDATE integrations
+ SET type_new = COALESCE(NEW.type_new, regexp_replace(NEW.type, '\\A(.+)Service\\Z', 'Integrations::\\1'))
+ , type = COALESCE(NEW.type, regexp_replace(NEW.type_new, '\\AIntegrations::(.+)\\Z', '\\1Service'))
+ WHERE integrations.id = NEW.id;
+ RETURN NULL;
+ SQL
+ end
+
+ execute(<<~SQL)
+ CREATE TRIGGER #{TRIGGER_ON_INSERT_NAME}
+ AFTER INSERT ON integrations
+ FOR EACH ROW
+ EXECUTE FUNCTION #{FUNCTION_NAME}();
+ SQL
+ end
+end