PostgreSQL Tasks and Functions for Ruby on Rails
Add this line to your application's Gemfile:
gem 'pg_tasks', '>= 1.1.0', '< 2.0.0'
The following rake tasks are available:
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:terminate_connections rake db:pg:truncate_tables
The meaning should be clear from the names.
The FILE environment variable is optional. There are defaults.
Access from Ruby Code
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.terminate_connections() PgTasks.truncate_tables()
Internals and Caveats
This library uses and extends
ActiveRecord::Tasks::DatabaseTasks as well as
The dump and restore tasks communicate with Postgresql via the system binaries
pg_restore (in the same way as some of the original rails tasks
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,
disables the triggers temporarily. The role for restore requieres permissions
to disable triggers temporarily.
data_dump as well as
truncate_tables leaves the
alone. This is the desired behavior for a library designed to interact with
ruby on rails respectively active record.
PgTasks.terminate_connections() will also terminate the current