2020-03-22 11:09:49 -04:00
|
|
|
# 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-05 05:08:05 -04:00
|
|
|
structure.gsub!(/^COMMENT ON EXTENSION.*/, '')
|
2020-03-22 11:09:49 -04:00
|
|
|
structure.gsub!(/^SET.+/, '')
|
|
|
|
structure.gsub!(/^SELECT pg_catalog\.set_config\('search_path'.+/, '')
|
|
|
|
structure.gsub!(/^--.*/, "\n")
|
|
|
|
|
2020-09-18 08:09:50 -04:00
|
|
|
# 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;')
|
2020-03-22 11:09:49 -04:00
|
|
|
|
2020-06-05 05:08:05 -04:00
|
|
|
structure.gsub!(/\n{3,}/, "\n\n")
|
2020-03-22 11:09:49 -04:00
|
|
|
|
2020-09-18 08:09:50 -04:00
|
|
|
io << structure.strip
|
2021-07-06 20:07:23 -04:00
|
|
|
io << "\n"
|
2020-03-22 11:09:49 -04:00
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|