class SP::Duh::Db::Transfer::Backup

Attributes

status[R]

Public Class Methods

new(source_pg_connection) click to toggle source
# File lib/sp/duh/db/transfer/backup.rb, line 14
def initialize(source_pg_connection)
  @connection = source_pg_connection
end

Public Instance Methods

execute(company_id, dump_file = nil) { |:before_execute| ... } click to toggle source
# File lib/sp/duh/db/transfer/backup.rb, line 18
def execute(company_id, dump_file = nil)
  self.status = :ok
  @company_id = company_id
  yield(:before_execute) if block_given?
  before_execute
  yield(:do_execute) if block_given?
  do_execute(dump_file) if self.status == :ok
  yield(:after_execute) if block_given?
  after_execute if self.status == :ok
end

Protected Instance Methods

after_execute() click to toggle source
# File lib/sp/duh/db/transfer/backup.rb, line 53
def after_execute
  @ended_at = Time.now
  SP::Duh::Db::Transfer.log_with_time "FINISHED backing up company #{@company_id} in #{(@ended_at - @started_at).round(2)}s"
end
before_execute() click to toggle source
# File lib/sp/duh/db/transfer/backup.rb, line 31
def before_execute
  SP::Duh::Db::Transfer.log_with_time "STARTED backing up company #{@company_id}"
  SP::Duh::Db::Transfer.log_with_time "Preparing backup..."
  @started_at = Time.now
  @schemas = @connection.exec %Q[
    SELECT * FROM transfer.backup_before_execute(#{@company_id});
  ]
  @schemas = @schemas.map { |result| result['schema_name'] }
end
do_execute(dump_file = nil) click to toggle source
# File lib/sp/duh/db/transfer/backup.rb, line 41
def do_execute(dump_file = nil)
  SP::Duh::Db::Transfer.log_with_time "Executing backup..."
  dump_file = "#{Time.now.strftime('%Y%m%d%H%M')}_c#{@company_id}.dump"  if dump_file.nil?
  command = "pg_dump -Fc -O --quote-all-identifiers --data-only -n #{@schemas.join(' -n ')} -h #{@connection.host} -p #{@connection.port} -U #{@connection.user} #{@connection.db} > #{dump_file}"
  SP::Duh::Db::Transfer.log_with_time command
  %x[ #{command} ]
  if $?.exitstatus != 0
    File.delete dump_file
    self.status = :error_dumping_company
  end
end
status=(value) click to toggle source
# File lib/sp/duh/db/transfer/backup.rb, line 58
def status=(value)
  @status = value
  if value != :ok
    @ended_at = Time.now
    SP::Duh::Db::Transfer.log_with_time "CANCELED backing up company #{@company_id} in #{(@ended_at - @started_at).round(2)}s"
  end
end