Class: Dumpcar::Pg

Inherits:
Object
  • Object
show all
Defined in:
lib/dumpcar/pg.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Pg

Returns a new instance of Pg.



5
6
7
8
# File 'lib/dumpcar/pg.rb', line 5

def initialize(connection)
  require "terrapin"
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/dumpcar/pg.rb', line 3

def connection
  @connection
end

Class Method Details

.defaultsObject



40
41
42
43
44
45
# File 'lib/dumpcar/pg.rb', line 40

def self.defaults
  {
    host: "localhost",
    port: "5432"
  }
end

Instance Method Details

#dump(filename) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/dumpcar/pg.rb', line 21

def dump(filename)
  with_database_config do |config|
    config => {password:, host:, port:, username:, database:}
    line = Terrapin::CommandLine.new("pg_dump",
      "--host :host  --port :port --username :username --clean --format=c --create  --if-exists --no-owner --no-acl :database --file=:filename",
      environment: {"PGPASSWORD" => password},
      logger:)
    line.run(password:, host:, port:, username:, database:, filename:)
  end
end

#loggerObject



36
37
38
# File 'lib/dumpcar/pg.rb', line 36

def logger
  Dumpcar::Util.logger
end

#restore(filename) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/dumpcar/pg.rb', line 10

def restore(filename)
  with_database_config do |config|
    config => {password:, host:, port:, username:, database:}
    line = Terrapin::CommandLine.new("pg_restore",
      "--verbose --clean --no-acl --no-owner -h :host -U :username -d :database -p :port :filename",
      environment: {"PGPASSWORD" => password},
      logger:)
    line.run(password:, host:, port:, username:, database:, filename:)
  end
end

#with_database_config {|self.class.defaults.merge(connection)| ... } ⇒ Object

Yields:



32
33
34
# File 'lib/dumpcar/pg.rb', line 32

def with_database_config
  yield self.class.defaults.merge(connection)
end