summaryrefslogtreecommitdiff
path: root/db/migrate/20220722150231_create_function_gitlab_schema_prevent_write.rb
blob: 29c6d9ce87d4d80bbff6511f252834eac1d4b566 (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 CreateFunctionGitlabSchemaPreventWrite < Gitlab::Database::Migration[2.0]
  TRIGGER_FUNCTION_NAME = 'gitlab_schema_prevent_write'

  enable_lock_retries!

  # This migration is only to make sure that the lock-write trigger function
  # matches what we already have on staging/production for Gitlab.com

  def up
    execute(<<~SQL)
      CREATE OR REPLACE FUNCTION #{TRIGGER_FUNCTION_NAME}()
        RETURNS TRIGGER AS
        $$
      BEGIN
          RAISE EXCEPTION 'Table: "%" is write protected within this Gitlab database.', TG_TABLE_NAME
            USING ERRCODE = 'modifying_sql_data_not_permitted',
            HINT = 'Make sure you are using the right database connection';
      END
      $$ LANGUAGE PLPGSQL
    SQL
  end

  def down
    return if Gitlab.com?

    execute(<<~SQL)
      DROP FUNCTION #{TRIGGER_FUNCTION_NAME} CASCADE
    SQL
  end
end