2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:20:18 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2019-07-30 21:29:24 +00:00
|
|
|
Rake::Task['db:structure:dump'].clear
|
2018-03-25 17:30:42 +00:00
|
|
|
namespace :db do
|
|
|
|
namespace :structure do
|
2019-07-30 21:29:24 +00:00
|
|
|
desc 'Overriding the task db:structure:dump task to remove -i option from pg_dump to make postgres 9.5 compatible'
|
|
|
|
task dump: %i[environment load_config] do
|
2018-03-25 17:30:42 +00:00
|
|
|
config = ActiveRecord::Base.configurations[Rails.env]
|
|
|
|
set_psql_env(config)
|
2019-07-30 21:29:24 +00:00
|
|
|
filename = File.join(Rails.root, 'db', 'structure.sql')
|
|
|
|
database = config['database']
|
2018-03-25 17:30:42 +00:00
|
|
|
command = "pg_dump -s -x -O -f #{Shellwords.escape(filename)} #{Shellwords.escape(database)}"
|
|
|
|
raise 'Error dumping database' unless Kernel.system(command)
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
File.open(filename, 'a') { |f| f << "SET search_path TO #{ActiveRecord::Base.connection.schema_search_path};\n\n" }
|
2018-03-25 17:30:42 +00:00
|
|
|
if ActiveRecord::Base.connection.supports_migrations?
|
2019-07-30 21:29:24 +00:00
|
|
|
File.open(filename, 'a') do |f|
|
2018-03-25 17:30:42 +00:00
|
|
|
f.puts ActiveRecord::Base.connection.dump_schema_information
|
|
|
|
f.print "\n"
|
|
|
|
end
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
Rake::Task['db:structure:dump'].reenable
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_psql_env(configuration)
|
|
|
|
ENV['PGHOST'] = configuration['host'] if configuration['host']
|
|
|
|
ENV['PGPORT'] = configuration['port'].to_s if configuration['port']
|
|
|
|
ENV['PGPASSWORD'] = configuration['password'].to_s if configuration['password']
|
|
|
|
ENV['PGUSER'] = configuration['username'].to_s if configuration['username']
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|