Class: Code0::ZeroTrack::Database::SchemaCleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/code0/zero_track/database/schema_cleaner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_schema) ⇒ SchemaCleaner

Returns a new instance of SchemaCleaner.



16
17
18
# File 'lib/code0/zero_track/database/schema_cleaner.rb', line 16

def initialize(original_schema)
  @original_schema = original_schema
end

Instance Attribute Details

#original_schemaObject (readonly)

Returns the value of attribute original_schema.



14
15
16
# File 'lib/code0/zero_track/database/schema_cleaner.rb', line 14

def original_schema
  @original_schema
end

Instance Method Details

#clean(io) ⇒ Object



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
# File 'lib/code0/zero_track/database/schema_cleaner.rb', line 20

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;'
  )

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

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

  nil
end