Skip to content

Instantly share code, notes, and snippets.

@maca
Created September 2, 2015 23:23
Show Gist options
  • Save maca/1b9af55dc49109b17e0c to your computer and use it in GitHub Desktop.
Save maca/1b9af55dc49109b17e0c to your computer and use it in GitHub Desktop.
postgre_sql_database_tasks monkey patch to exclude schemas from dump.
module ActiveRecord
module Tasks # :nodoc:
class PostgreSQLDatabaseTasks # :nodoc:
def structure_dump(filename)
set_psql_env
puts "The monkeypatch located in #{__FILE__} should be removed when upgrading to Rails 5.0"
search_path = nil # Dump all schemas
unless search_path.blank?
search_path = search_path.split(",").map{|search_path_part| "--schema=#{Shellwords.escape(search_path_part.strip)}" }.join(" ")
end
command = "pg_dump --exclude-schema='tenant*' -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(configuration['database'])}"
raise 'Error dumping database' unless Kernel.system(command)
File.open(filename, "a") { |f| f << "SET search_path TO #{connection.schema_search_path};\n\n" }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment