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

20230706080234_add_trigger_on_organizations.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e21a5e0eb23b4e5e8c89c846c807446c11c59095 (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
# frozen_string_literal: true

class AddTriggerOnOrganizations < Gitlab::Database::Migration[2.1]
  include Gitlab::Database::SchemaHelpers

  TABLE_NAME = 'organizations'
  FUNCTION_NAME = 'prevent_delete_of_default_organization'
  TRIGGER_NAME = 'prevent_delete_of_default_organization_before_destroy'

  def up
    default_org_id = Organizations::Organization::DEFAULT_ORGANIZATION_ID

    create_trigger_function(FUNCTION_NAME) do
      <<~SQL
        IF OLD.id = #{default_org_id} THEN
          RAISE EXCEPTION 'Deletion of the default Organization is not allowed.';
        END IF;
        RETURN OLD;
      SQL
    end

    create_trigger(TABLE_NAME, TRIGGER_NAME, FUNCTION_NAME, fires: 'BEFORE DELETE')
  end

  def down
    drop_trigger(TABLE_NAME, TRIGGER_NAME)
    drop_function(FUNCTION_NAME)
  end
end