Skip to content

Commit

Permalink
Merge pull request #1 from eins78/master
Browse files Browse the repository at this point in the history
support plain sql dumps
  • Loading branch information
DrTom authored Apr 15, 2019
2 parents 88aa10e + 366e131 commit 830da1d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
37 changes: 25 additions & 12 deletions lib/pg_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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 \
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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'
Expand All @@ -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 ' \
Expand Down
2 changes: 1 addition & 1 deletion lib/pg_tasks/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
#++

module PgTasks
VERSION = '2.1.1-0'
VERSION = '2.2.0-0'
end
10 changes: 5 additions & 5 deletions lib/tasks/pg_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -38,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']
Expand Down

0 comments on commit 830da1d

Please sign in to comment.