From ca23142fdde22346bc901191866276f046ac6b4c Mon Sep 17 00:00:00 2001 From: Max Albrecht Date: Mon, 15 Apr 2019 13:57:30 +0200 Subject: [PATCH 1/2] rake: remove obsolete task --- lib/tasks/pg_tasks.rake | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/tasks/pg_tasks.rake b/lib/tasks/pg_tasks.rake index 186a71a..9013d41 100644 --- a/lib/tasks/pg_tasks.rake +++ b/lib/tasks/pg_tasks.rake @@ -21,11 +21,6 @@ namespace :db do end namespace :data do - desc "Call pg_dump to dump data without structure and without schema_migrations, honors ENV['FILE']" - task dump: [:environment, :load_config] do - PgTasks.data_dump ENV['FILE'] - end - desc "Call pg_restore with parameters apt to load dumps w.o. structure, honors ENV['FILE']" task restore: [:environment, :load_config] do PgTasks.data_restore ENV['FILE'] From 366e131338a2862d34d08bc1a51549b3c14a549d Mon Sep 17 00:00:00 2001 From: Max Albrecht Date: Mon, 15 Apr 2019 13:58:30 +0200 Subject: [PATCH 2/2] support plain sql dumps --- lib/pg_tasks.rb | 37 +++++++++++++++++++++++++------------ lib/pg_tasks/version.rb | 2 +- lib/tasks/pg_tasks.rake | 5 +++++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/pg_tasks.rb b/lib/pg_tasks.rb index 8ddd6eb..df4d15e 100644 --- a/lib/pg_tasks.rb +++ b/lib/pg_tasks.rb @@ -12,8 +12,9 @@ module PgTasks require 'pg_tasks/railtie' if defined?(Rails) - DEFAULT_BINARY_DATA_FILE_NAME = 'data.pgbin' - DEFAULT_BINARY_STRUCTURE_AND_DATA_FILE_NAME = 'structure_and_data.pgbin' + DEFAULT_BINARY_DATA_FILE_NAME = 'data.pgbin'.freeze + DEFAULT_BINARY_STRUCTURE_AND_DATA_FILE_NAME = 'structure_and_data.pgbin'.freeze + DEFAULT_SQL_STRUCTURE_AND_DATA_FILE_NAME = 'structure_and_data.sql'.freeze class << self %w(data_restore).each do |method_name| @@ -25,7 +26,8 @@ class << self end end - %w(structure_and_data_dump structure_and_data_restore).each do |method_name| + %w(structure_and_data_dump structure_and_data_dump_sql structure_and_data_restore) + .each do |method_name| define_method method_name do |filename = nil| ActiveRecord::Tasks::DatabaseTasks \ .perform_pg_db_task_for_config_and_filename \ @@ -53,17 +55,23 @@ def current_config ActiveRecord::Tasks::DatabaseTasks.current_config end + def filename_or_default(filename, default) + (filename.present? && filename) \ + || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, default) + end + def filename_or_default_binary_data_file(filename) - (filename.present? && filename) || \ - File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, - DEFAULT_BINARY_DATA_FILE_NAME) + filename_or_default(filename, DEFAULT_BINARY_DATA_FILE_NAME) end def filename_or_default_binary_structure_and_data_file(filename) - (filename.present? && filename) || \ - File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, - DEFAULT_BINARY_STRUCTURE_AND_DATA_FILE_NAME) + filename_or_default(filename, DEFAULT_BINARY_STRUCTURE_AND_DATA_FILE_NAME) + end + + def filename_or_default_sql_structure_and_data_file(filename) + filename_or_default(filename, DEFAULT_SQL_STRUCTURE_AND_DATA_FILE_NAME) end + end end @@ -99,10 +107,11 @@ def data_restore(filename) end end - def structure_and_data_dump(filename) + def structure_and_data_dump(filename, plain_text = false) set_psql_env - command = "pg_dump -F c -x -O -f \ - #{Shellwords.escape(filename)} \ + command = "pg_dump -x -O \ + -F #{plain_text ? 'p' : 'c'} \ + -f #{Shellwords.escape(filename)} \ #{Shellwords.escape(configuration['database'])}" unless Kernel.system(command) raise 'Error during structure_and_data_dump' @@ -112,6 +121,10 @@ def structure_and_data_dump(filename) end end + def structure_and_data_dump_sql(filename) + structure_and_data_dump(filename, true) + end + def structure_and_data_restore(filename) set_psql_env command = 'pg_restore --disable-triggers --exit-on-error ' \ diff --git a/lib/pg_tasks/version.rb b/lib/pg_tasks/version.rb index fd189a3..14d937d 100644 --- a/lib/pg_tasks/version.rb +++ b/lib/pg_tasks/version.rb @@ -7,5 +7,5 @@ #++ module PgTasks - VERSION = '2.1.1-0' + VERSION = '2.2.0-0' end diff --git a/lib/tasks/pg_tasks.rake b/lib/tasks/pg_tasks.rake index 9013d41..7e8c3dd 100644 --- a/lib/tasks/pg_tasks.rake +++ b/lib/tasks/pg_tasks.rake @@ -33,6 +33,11 @@ namespace :db do PgTasks.structure_and_data_dump ENV['FILE'] end + desc "Call pg_dump to dump all data and structure as SQL, honors ENV['FILE']" + task dump_sql: [:environment, :load_config] do + PgTasks.structure_and_data_dump_sql ENV['FILE'] + end + desc "Call pg_restore with parameters apt to load data and structure, honors ENV['FILE']" task restore: [:environment, :load_config] do PgTasks.structure_and_data_restore ENV['FILE']