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

schema_cleaner.rb « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c8d0a4eb6db9ab25d16cfcc74bca3f8a9ce315d (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

module Gitlab
  module Database
    class SchemaCleaner
      attr_reader :original_schema

      def initialize(original_schema)
        @original_schema = original_schema
      end

      def clean(io)
        structure = original_schema.dup

        # Remove noise
        structure.gsub!(/^COMMENT ON EXTENSION.*/, '')
        structure.gsub!(/^SET.+/, '')
        structure.gsub!(/^SELECT pg_catalog\.set_config\('search_path'.+/, '')
        structure.gsub!(/^--.*/, "\n")

        # We typically don't assume we're working with the public schema.
        # pg_dump uses fully qualified object names though, since we have multiple schemas
        # in the database.
        #
        # The intention here is to not introduce an assumption about the standard schema,
        # unless we have a good reason to do so.
        structure.gsub!(/public\.(\w+)/, '\1')
        structure.gsub!(
          /CREATE EXTENSION IF NOT EXISTS (\w+) WITH SCHEMA public;/,
          'CREATE EXTENSION IF NOT EXISTS \1;'
        )

        # Table lock-writes triggers should not be added to the schema
        # These triggers are added by the rake task gitlab:db:lock_writes for a decomposed database.
        structure.gsub!(
          %r{
            ^CREATE.TRIGGER.gitlab_schema_write_trigger_\w+
            \s
            BEFORE.INSERT.OR.DELETE.OR.UPDATE.OR.TRUNCATE.ON.\w+
            \s
            FOR.EACH.STATEMENT.EXECUTE.FUNCTION.gitlab_schema_prevent_write\(\);$
          }x,
          ''
        )

        structure.gsub!(/\n{3,}/, "\n\n")

        io << structure.strip
        io << "\n"

        nil
      end
    end
  end
end