Skip to content

Instantly share code, notes, and snippets.

@ILoGM
Created August 4, 2016 10:23
Show Gist options
  • Save ILoGM/98aff9402d0edc164de38fede9cdd23c to your computer and use it in GitHub Desktop.
Save ILoGM/98aff9402d0edc164de38fede9cdd23c to your computer and use it in GitHub Desktop.
AR 4.1.6 monkey patch for pg 9.5.3
# HACK:
# There is a deprecation of '-i' flag for pg_dump command, appeared in Postgres 9.5
# The following rake task overrides the default `rake db:structure:dump` to be able to dump db structure without upgrading ActiveRecord.
# @ref: https://github.com/rails/rails/issues/23030#issuecomment-171084067
require 'shellwords'
module ActiveRecord
module Tasks # :nodoc:
class PostgreSQLDatabaseTasks # :nodoc:
def structure_dump(filename)
set_psql_env
search_path = configuration['schema_search_path']
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 -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 #{ActiveRecord::Base.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