Skip to content

Commit

Permalink
2.0.0-0
Browse files Browse the repository at this point in the history
  • Loading branch information
DrTom committed Dec 2, 2016
1 parent 2a115a6 commit 305cd92
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 35 deletions.
44 changes: 32 additions & 12 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ gem 'pg_tasks', '>= 1.1.0', '< 2.0.0'

### Rake Tasks

The following rake tasks are available:
The following rake tasks are available:

```shell
rake db:pg:data:dump FILE=...
rake db:pg:data:restore FILE=...
rake db:pg:structure_and_data:dump FILE=...
rake db:pg:structure_and_data:restore FILE=...
rake db:pg:data:restore FILE=...
rake db:pg:terminate_connections
rake db:pg:truncate_tables
```
Expand All @@ -31,15 +30,40 @@ The FILE environment variable is optional. There are defaults.
### Access from Ruby Code

```ruby
PgTasks.data_dump(filename = nil)
PgTasks.data_restore(filename = nil)
PgTasks.structure_and_data_dump(filename = nil)
PgTasks.structure_and_data_restore(filename = nil)
PgTasks.data_restore(filename = nil)
PgTasks.terminate_connections()
PgTasks.truncate_tables()
```

## Internals and Caveats
## Breaking Changes from Version 1.x to 2.x

The task `db:pg:data:dump` and the corresponding command `PgTasks.data_dump`
has been removed.

The task `db:pg:data:restore` and the corresponding command
`PgTasks.data_restore` operate now only on dumps also including the schema.

The task `db:pg:data:restore` and the corresponding command
`PgTasks.data_restore` do not disable triggers!


### Motivation

* The integrity of the data is now checked also when restoring data only.

* Most use cases require now only one dump (`structure_and_data`) instead of
two. This translates in ½ the time to create dumps and ½ the space required
to store them.

You might ask why it hasn't been this way from the start? The facilities
PostgreSQL provides clashes a bit with how ActiveRecord works with respect the
`schema_migrations` table and you have to perform "a bit of a dance" to
make it work this way.


## Internals and Caveats

This library uses and extends `ActiveRecord::Tasks::DatabaseTasks` as well as
`ActiveRecord::Tasks::PostgreSQLDatabaseTasks`.
Expand All @@ -52,13 +76,9 @@ Restoring a complete database doesn't play well if there are open connections.
Restore is carried out in a single transaction add will abort immediately if
there is a problem.

`data_dump` might warn you about circular dependencies. Never mind, `restore`
disables the triggers temporarily. The role for restore requieres permissions
to disable triggers temporarily.

`data_dump` as well as `truncate_tables` leaves the `schema_migrations` table
`data_restore` as well as `truncate_tables` leaves the `schema_migrations` table
alone. This is the desired behavior for a library designed to interact with
*ruby on rails* respectively *active record*.

Calling `PgTasks.terminate_connections()` will also terminate the current
connection.
connection.
46 changes: 24 additions & 22 deletions lib/pg_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module PgTasks
DEFAULT_BINARY_STRUCTURE_AND_DATA_FILE_NAME = 'structure_and_data.pgbin'

class << self
%w(data_dump data_restore).each do |method_name|
%w(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 @@ -70,30 +70,32 @@ def filename_or_default_binary_structure_and_data_file(filename)
module ActiveRecord
module Tasks
class PostgreSQLDatabaseTasks
def data_dump(filename)
set_psql_env
command = 'pg_dump -F c -a -T schema_migrations -x -O -f ' \
"#{Shellwords.escape(filename.to_s)} " \
"#{Shellwords.escape(configuration['database'])}"
unless Kernel.system(command)
raise 'Error during data_dump'
else
$stdout.puts "The data of '#{configuration['database']} " \
"has been dumped to '#{filename}'"
end
end

def data_restore(filename)
set_psql_env
command = 'pg_restore --disable-triggers --exit-on-error ' \
'--single-transaction -a -x -O ' \
"-d #{Shellwords.escape(configuration['database'])} " \
"#{Shellwords.escape(filename.to_s)}"
unless Kernel.system(command)
raise 'Error during data_restore '
else
$stdout.puts "Data from '#{filename}' has been restored to \
shell_filename= Shellwords.escape filename.to_s
list_cmd = "pg_restore -l #{shell_filename}"
pg_list = `#{list_cmd}`
raise "Command #{list_cmd} failed " if $?.exitstatus != 0
restore_list = pg_list.split(/\n/) \
.reject{|l| l =~ /TABLE DATA .* schema_migrations/}.join("\n")
begin
restore_list_file = Tempfile.new 'pg_restore_list'
restore_list_file.write restore_list

set_psql_env
command = 'pg_restore --data-only --exit-on-error ' \
' --single-transaction --no-privileges --no-owner ' \
" -d #{Shellwords.escape(configuration['database'])} " \
" #{shell_filename }"
unless Kernel.system(command)
raise 'Error during data_restore '
else
$stdout.puts "Data from '#{filename}' has been restored to \
'#{configuration['database']}'"
end
ensure
restore_list_file.close
restore_list_file.unlink
end
end

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 = '1.3.3'
VERSION = '2.0.0-0'
end

0 comments on commit 305cd92

Please sign in to comment.