Class: Gleis::Database
- Inherits:
-
Object
- Object
- Gleis::Database
- Defined in:
- lib/gleis/database.rb
Overview
The class implements the methods required for the databases of a gleis app
Class Method Summary collapse
- .backup(app_name) ⇒ Object
- .delete(app_name, env_var_name) ⇒ Object
- .info(app_name) ⇒ Object
- .new(app_name) ⇒ Object
- .promote(app_name, env_var_name) ⇒ Object
- .psql(app_name) ⇒ Object
- .push(app_name, local_name) ⇒ Object
- .reset(app_name, env_var_name) ⇒ Object
Class Method Details
.backup(app_name) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# 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 = 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) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# 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') = '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() 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) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# 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) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# 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) ⇒ Object
103 104 105 106 107 108 109 110 |
# 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) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# 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 |