module MultitenancyTools
  # This class receives an SQL string and removes statements that should not
  # be present on dumps generated by {SchemaDumper} and {TableDumper}.
  class DumpCleaner
    # @param [String] sql
    def initialize(sql)
      @sql = sql.dup
    end

    # Returns a new cleaned string.
    #
    # @return String
    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")
      @sql
    end
  end
end
