summaryrefslogtreecommitdiff
path: root/db/migrate/20220213100000_remove_integration_type_triggers.rb
blob: 137e5648125945d8b6ac70bab1909af79beb7177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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