class MultitenancyTools::DumpCleaner

This class receives an SQL string and removes statements that should not be present on dumps generated by {SchemaDumper} and {TableDumper}.

Public Class Methods

new(sql, schema_name = '') click to toggle source

@param [String] sql

# File lib/multitenancy_tools/dump_cleaner.rb, line 6
def initialize(sql, schema_name = '')
  @schema_name = schema_name
  @sql = sql.dup
end

Public Instance Methods

clean() click to toggle source

Returns a new cleaned string.

@return String

# File lib/multitenancy_tools/dump_cleaner.rb, line 14
def clean
  @sql.gsub!(/CREATE SCHEMA .*;\n/, '')
  @sql.gsub!(/SET search_path .*;\n/, '')
  @sql.gsub!(/SET statement_timeout .*;\n/, '')
  @sql.gsub!(/SET lock_timeout .*;\n/, '')
  @sql.gsub!(/^--(?:.*)\n+/, '')
  @sql.gsub!(/\n+/, "\n")
  clean_schema_names!
  clean_catalog_overwrites!
  @sql
end

Private Instance Methods

clean_catalog_overwrites!() click to toggle source

Removes system administrators configs overwrites

# File lib/multitenancy_tools/dump_cleaner.rb, line 42
def clean_catalog_overwrites!
  @sql.gsub!(/SELECT pg_catalog\.set_config.*;\n/, '')
end
clean_schema_names!() click to toggle source

Matches namespaces containing the given schema_name

For instance: CREATE TABLE schema1.posts ();

Will be replaced by: CREATE TABLE posts ();

# File lib/multitenancy_tools/dump_cleaner.rb, line 35
def clean_schema_names!
  return if @schema_name.blank?

  @sql.gsub!(/\b#{@schema_name}\.([\S\D]*)/, '\1')
end