Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20220824175648_limit_namespaces_sync_triggers_to_traversal_ids_update.rb « migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 142744b5493abe3c311b1beec130fe22e17303bd (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
33
34
35
# frozen_string_literal: true

class LimitNamespacesSyncTriggersToTraversalIdsUpdate < Gitlab::Database::Migration[2.0]
  include Gitlab::Database::SchemaHelpers

  enable_lock_retries!

  TABLE_NAME = 'namespaces'
  EVENT_TABLE_NAME = 'namespaces_sync_events'
  FUNCTION_NAME = 'insert_namespaces_sync_event'
  OLD_TRIGGER_ON_INSERT = 'trigger_namespaces_parent_id_on_insert'
  OLD_TRIGGER_ON_UPDATE = 'trigger_namespaces_parent_id_on_update'
  NEW_TRIGGER_ON_UPDATE = 'trigger_namespaces_traversal_ids_on_update'

  def up
    create_trigger(TABLE_NAME, NEW_TRIGGER_ON_UPDATE, FUNCTION_NAME, fires: 'AFTER UPDATE') do
      <<~SQL
        WHEN (OLD.traversal_ids IS DISTINCT FROM NEW.traversal_ids)
      SQL
    end
    drop_trigger(TABLE_NAME, OLD_TRIGGER_ON_UPDATE)
    drop_trigger(TABLE_NAME, OLD_TRIGGER_ON_INSERT)
  end

  # Revert both triggers to the version defined in db/migrate/20211011141242_create_namespaces_sync_trigger.rb
  def down
    create_trigger(TABLE_NAME, OLD_TRIGGER_ON_INSERT, FUNCTION_NAME, fires: 'AFTER INSERT')
    create_trigger(TABLE_NAME, OLD_TRIGGER_ON_UPDATE, FUNCTION_NAME, fires: 'AFTER UPDATE') do
      <<~SQL
        WHEN (OLD.parent_id IS DISTINCT FROM NEW.parent_id)
      SQL
    end
    drop_trigger(TABLE_NAME, NEW_TRIGGER_ON_UPDATE)
  end
end