debian-mirror-gitlab/lib/gitlab/database/schema_cleaner.rb

56 lines
1.6 KiB
Ruby
Raw Normal View History

2020-04-22 19:07:51 +05:30
# 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
2020-06-23 00:09:42 +05:30
structure.gsub!(/^COMMENT ON EXTENSION.*/, '')
2020-04-22 19:07:51 +05:30
structure.gsub!(/^SET.+/, '')
structure.gsub!(/^SELECT pg_catalog\.set_config\('search_path'.+/, '')
structure.gsub!(/^--.*/, "\n")
2020-11-24 15:15:51 +05:30
# 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')
2023-03-04 22:38:38 +05:30
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,
''
)
2020-04-22 19:07:51 +05:30
2020-06-23 00:09:42 +05:30
structure.gsub!(/\n{3,}/, "\n\n")
2020-04-22 19:07:51 +05:30
2020-11-24 15:15:51 +05:30
io << structure.strip
2021-09-30 23:02:18 +05:30
io << "\n"
2020-04-22 19:07:51 +05:30
nil
end
end
end
end