Class: Kaboom::DatabaseHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/kaboom/helpers/database_helper.rb

Class Method Summary collapse

Class Method Details

.apply_backup(config, backup_file) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kaboom/helpers/database_helper.rb', line 19

def self.apply_backup(config, backup_file)
  local_connection_string = "postgresql://#{config[:username]}:#{config[:password]}@localhost/template1"
  
  commands = [
    %Q{psql #{local_connection_string} -c "DROP DATABASE IF EXISTS #{config[:name]}" >/dev/null 2>&1},
    %Q{psql #{local_connection_string} -c "CREATE DATABASE #{config[:name]};" >/dev/null 2>&1},
    %Q{psql #{local_connection_string.gsub('template1', config[:name])} < #{backup_file} >/dev/null 2>&1},
    %Q{psql #{local_connection_string.gsub('template1', config[:name])} -c "UPDATE ar_internal_metadata SET value = 'development' WHERE key = 'environment';" >/dev/null 2>&1},
    %Q{psql #{local_connection_string} -c "ALTER DATABASE #{config[:name]} OWNER TO #{config[:username]};" >/dev/null 2>&1},
  ]

  commands.all? { |cmd| system(cmd) }
end

.connection_string(config) ⇒ Object



3
4
5
# File 'lib/kaboom/helpers/database_helper.rb', line 3

def self.connection_string(config)
  "postgresql://#{config[:username]}:#{config[:password]}@127.0.0.2/#{config[:name]}"
end

.database_size(connection_string, destination) ⇒ Object



7
8
9
10
11
12
# File 'lib/kaboom/helpers/database_helper.rb', line 7

def self.database_size(connection_string, destination)
  query = "SELECT pg_size_pretty(pg_database_size(current_database()));"
  command = %Q{kamal accessory exec db --reuse -i -q "psql #{connection_string} -c \\"#{query}\\"" -d #{destination} 2>/dev/null}
  result = `#{command}`.strip
  result.match(/\n\s*(.+?)\s*\n\(/) ? $1 : "Unknown MB"
end

.pull_backup(connection_string, destination, backup_file) ⇒ Object



14
15
16
17
# File 'lib/kaboom/helpers/database_helper.rb', line 14

def self.pull_backup(connection_string, destination, backup_file)
  command = %Q{kamal accessory exec db --reuse -i -q "pg_dump #{connection_string}" -d #{destination} > db/#{backup_file}.sql 2>/dev/null}
  system(command)
end