Class: PostgresqlAdapter

Inherits:
Object
  • Object
show all
Includes:
System
Defined in:
lib/db_adapters/postgresql_adapter.rb

Instance Method Summary collapse

Methods included from System

clean, db_credentials, hostname, prompt, run, tarzip_folders, unzip_file

Constructor Details

#initialize(db_credentials) ⇒ PostgresqlAdapter

Returns a new instance of PostgresqlAdapter.



4
5
6
# File 'lib/db_adapters/postgresql_adapter.rb', line 4

def initialize(db_credentials)
  @db_credentials = db_credentials
end

Instance Method Details

#backup_extensionObject



31
32
33
# File 'lib/db_adapters/postgresql_adapter.rb', line 31

def backup_extension
  return ".tar"
end

#db_dumpObject

Creates and runs pg_dump and throws into .tar.gz file. Returns .tar.gz file



10
11
12
13
14
15
16
17
18
# 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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# 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