Class: Bootstrap::Db::Postgres

Inherits:
Adapter
  • Object
show all
Defined in:
lib/bootstrap/db/adapter/postgres.rb

Constant Summary collapse

LOAD_COMMAND =
"pg_restore"
CONNECTION_PARAMS =
[ :username, :password, :host, :port ]
CONFIG_DEFAULTS =
{
  :port => 5423
}

Instance Attribute Summary

Attributes inherited from Adapter

#config, #file_name, #file_path

Instance Method Summary collapse

Methods inherited from Adapter

#additional_parameters, #command_string, #current_db_time, #ignore_tables, #initialize

Methods included from Command

#display_and_execute, #execute_command

Constructor Details

This class inherits a constructor from Bootstrap::Db::Adapter

Instance Method Details

#connection_stringObject

Compile connection string



14
15
16
17
18
19
20
21
22
# File 'lib/bootstrap/db/adapter/postgres.rb', line 14

def connection_string
  @connection_string ||= begin
    CONNECTION_PARAMS.inject([]) do |result, element|
      config_value = config.settings[element.to_s] || CONFIG_DEFAULTS[element]
      result << "--#{element}='#{config_value}'" if config_value
      result
    end.join(' ')
  end
end

#dump!Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bootstrap/db/adapter/postgres.rb', line 24

def dump!
  #pg_dump --help
  dump_command = [
    "pg_dump",
    "--create --format=c",
    "--file=#{file_path}",
    connection_string
  ]

  if ignore_tables.present?
    ignore_tables.each do |table_name|
      dump_command << "--exclude-table='#{config.settings["database"]}.#{table_name.strip}'"
    end
  end

  if additional_parameters.present?
    additional_parameters.each do |param|
      dump_command << param
    end
  end

  dump_command << config.settings['database']

  result = display_and_execute(dump_command.join(' '))

  save_generated_time!

  result
end

#load!Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bootstrap/db/adapter/postgres.rb', line 54

def load!
  #pg_restore --help
  load_command = [
    "pg_restore",
    "--single-transaction --format=c",
    "--dbname='#{config.settings["database"]}'",
    connection_string,
    file_path
  ]

  display_and_execute(load_command.join(' '))
end

#rebase!Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bootstrap/db/adapter/postgres.rb', line 67

def rebase!
  generated_time = load_generated_time!

  load_rebase_functions!

  start_point = generated_time
  rebase_to   = current_db_time

  rebase_cmd = "SELECT rebase_db_time('#{start_point}'::timestamp, '#{rebase_to}'::timestamp);"
  display_and_execute("#{psql_execute} --command=#{rebase_cmd.shellescape}")
end