class Gleis::Database

The class implements the methods required for the databases of a gleis app

Public Class Methods

backup(app_name) click to toggle source
# File lib/gleis/database.rb, line 4
def self.backup(app_name)
  token = Token.check
  Utils.check_for_local_pg_command('pg_dump')
  url = Config.get_env_var(app_name, token, 'DATABASE_URL')
  abort('There is no database configured under the DATABASE_URL variable.') unless url
  db_name = url.split('/').last
  timestamp = Time.now.strftime('%Y%m%d%H%M%S')
  backup_file = File.join(Dir.home, "#{app_name}_#{db_name}_#{timestamp}.pgdump")
  if system("pg_dump -f #{backup_file} #{url}")
    puts "Database configured at DATABASE_URL succesfully backed up locally in #{backup_file}"
  else
    puts 'Failed to backup database configured under DATABASE_URL'
  end
end
delete(app_name, env_var_name) click to toggle source
# File lib/gleis/database.rb, line 19
def self.delete(app_name, env_var_name)
  token = Token.check
  if Utils.prompt_confirmation("Are you sure you want to delete the database at #{env_var_name}?")
    body = API.request('delete', "db/#{app_name}/#{env_var_name}", token)
    if body['success'] == 1
      puts "Successfully deleted database configured at #{env_var_name}."
    else
      puts "Failed to delete database: #{body['message']}"
    end
  else
    puts 'Command cancelled'
  end
end
info(app_name) click to toggle source
# File lib/gleis/database.rb, line 33
def self.info(app_name)
  token = Token.check
  url = Config.get_env_var(app_name, token, 'DATABASE_URL')
  abort_message = 'You do not have a database or you did not promote it yet. '\
    'You can create one with the db:new command and promote it with db:promote.'
  abort(abort_message) unless url
  # Get database info
  db_name = url.split('/').last
  body = API.request('get', "database/#{db_name}", token)
  return unless body['success'] == 1

  db = body['database']
  puts "Info about database at DATABASE_URL:\n\n"
  puts "\tName:\t\t#{db['name']}\n" \
    "\tCreated on:\t#{Time.parse(db['created_at']).strftime('%c')}"
  if body['available']
    puts "\tStatus:\t\tavailable\n" \
      "\tDatabase:\t#{body['version']}\n" \
      "\tConnections:\t#{body['connections']}"
  else
    puts "\tStatus:\t\tnot available"
  end
end
new(app_name) click to toggle source
# File lib/gleis/database.rb, line 57
def self.new(app_name)
  token = Token.check
  body = API.request('post', 'db', token, 'name': app_name)
  if body['success'] == 1
    puts "Successfully created new database for #{app_name} available as config variable #{body['message']}"
  else
    puts 'Failed to create new database'
  end
end
promote(app_name, env_var_name) click to toggle source
# File lib/gleis/database.rb, line 67
def self.promote(app_name, env_var_name)
  token = Token.check
  body = API.request('post', 'db/promote', token, 'name': app_name, 'var': env_var_name)
  if body['success'] == 1
    puts "Succesfully promoted database environment variable #{env_var_name} to DATABASE_URL"
  else
    puts 'Failed to promote database environment variable'
  end
end
psql(app_name) click to toggle source
# File lib/gleis/database.rb, line 103
def self.psql(app_name)
  token = Token.check
  abort('The PostgreSQL client psql is not installed on this system.') unless Utils.which('psql')
  url = Config.get_env_var(app_name, token, 'DATABASE_URL')
  abort('There is no database configured under the DATABASE_URL variable.') unless url
  ENV['PGCONNECT_TIMEOUT'] = '5'
  exec("psql #{url}")
end
push(app_name, local_name) click to toggle source
# File lib/gleis/database.rb, line 77
def self.push(app_name, local_name)
  token = Token.check
  Utils.check_for_local_pg_command('pg_dump')
  Utils.check_for_local_pg_command('pg_restore')
  url = Config.get_env_var(app_name, token, 'DATABASE_URL')
  abort('There is no database configured under the DATABASE_URL variable.') unless url
  ENV['PGCONNECT_TIMEOUT'] = '5'
  # Check if database is empty
  sql_statement = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='public'"
  table_count = `psql #{url} -t -A -c "#{sql_statement}"`.chomp
  if table_count.to_i.zero?
    # Check connection to local database
    unless system("psql -c 'SELECT 1' #{local_name} >/dev/null 2>&1")
      abort("Failed to connect to local database #{local_name}, please check " \
        'that the database name is correct and that you have access to it.')
    end
    if system("pg_dump -Fc -x #{local_name} | pg_restore -O -n public -d #{url}")
      puts "Successfully pushed local database #{local_name} to database configured at DATABASE_URL."
    else
      puts "Failed to push local database #{local_name} to DATABASE_URL."
    end
  else
    puts 'The database configured at DATABASE_URL already contains data, please empty it first.'
  end
end
reset(app_name, env_var_name) click to toggle source
# File lib/gleis/database.rb, line 112
def self.reset(app_name, env_var_name)
  token = Token.check
  if Utils.prompt_confirmation("Are you sure you want to reset the database at #{env_var_name}?")
    body = API.request('put', 'db', token, 'name': app_name, 'var': env_var_name)
    if body['success'] == 1
      puts "Successfully reset database configured at #{env_var_name}."
    else
      puts "Failed to delete database: #{body['message']}"
    end
  else
    puts 'Command cancelled'
  end
end