class PostgresqlAdapter
Public Class Methods
new(db_credentials)
click to toggle source
# File lib/db_adapters/postgresql_adapter.rb, line 4 def initialize(db_credentials) @db_credentials = db_credentials end
Public Instance Methods
backup_extension()
click to toggle source
# File lib/db_adapters/postgresql_adapter.rb, line 31 def backup_extension return ".tar" end
db_dump()
click to toggle source
Creates and runs pg_dump and throws into .tar.gz file. Returns .tar.gz file
# File lib/db_adapters/postgresql_adapter.rb, line 10 def db_dump dump_file = Tempfile.new("dump") username = @db_credentials['username'] password = @db_credentials['password'] database = @db_credentials['database'] cmd = "PGPASSWORD=\"#{password}\" PGUSER=\"#{username}\" pg_dump -Ft #{database} > #{dump_file.path}" System.run(cmd) dump_file end
load_db_dump(dump_file)
click to toggle source
# File lib/db_adapters/postgresql_adapter.rb, line 20 def load_db_dump(dump_file) database = @db_credentials['database'] host = @db_credentials['host'] || 'localhost' superuser = System.prompt "Postgres superuser: " su_password = System.prompt "#{superuser} password: " cmd = "PGPASSWORD=\"#{su_password}\" PGUSER=\"#{superuser}\" dropdb --host #{host} #{database} && " + "PGPASSWORD=\"#{su_password}\" PGUSER=\"#{superuser}\" createdb --host #{host} -T template0 #{database} && " + "PGPASSWORD=\"#{su_password}\" PGUSER=\"#{superuser}\" pg_restore --host #{host} -Ft --dbname=#{database} #{dump_file.path}" System.run(cmd) end