class DbAgent::DbHandler

Attributes

backup_folder[R]
config[R]
data_folder[R]
migrations_folder[R]
schema_folder[R]
superconfig[R]
viewpoints_folder[R]

Public Class Methods

factor(options) click to toggle source
# File lib/db_agent/db_handler.rb, line 94
def self.factor(options)
  case options[:config][:adapter]
  when 'postgres'
    PostgreSQL.new(options)
  when 'mssql'
    MSSQL.new(options)
  when 'mysql'
    MySQL.new(options)
  else
    PostgreSQL.new(options)
  end
end
new(options) click to toggle source
# File lib/db_agent/db_handler.rb, line 4
def initialize(options)
  @config = options[:config]
  @superconfig = options[:superconfig]
  @root_folder = options[:root]
  @backup_folder = options[:backup] || options[:root]/'backups'
  @schema_folder = options[:schema] || options[:root]/'schema'
  @migrations_folder = options[:migrations] || options[:root]/'migrations'
  @data_folder = options[:data] || options[:root]/'data'
  @viewpoints_folder = options[:viewpoints] || options[:root]/'viewpoints'
  require_viewpoints!
end

Public Instance Methods

backup() click to toggle source
# File lib/db_agent/db_handler.rb, line 33
def backup
  raise NotImplementedError
end
create() click to toggle source
# File lib/db_agent/db_handler.rb, line 25
def create
  raise NotImplementedError
end
drop() click to toggle source
# File lib/db_agent/db_handler.rb, line 29
def drop
  raise NotImplementedError
end
migrate(version = nil) click to toggle source
# File lib/db_agent/db_handler.rb, line 78
def migrate(version = nil)
  Sequel.extension :migration
  if (sf = migrations_folder/'superuser').exists?
    Sequel::Migrator.run(sequel_superdb, migrations_folder/'superuser', table: 'superuser_migrations', target: version)
  end
  Sequel::Migrator.run(sequel_db, migrations_folder, target: version)
end
ping() click to toggle source
# File lib/db_agent/db_handler.rb, line 19
def ping
  puts "Using #{config}"
  sequel_db.test_connection
  puts "Everything seems fine!"
end
repl() click to toggle source
# File lib/db_agent/db_handler.rb, line 37
def repl
  raise NotImplementedError
end
require_viewpoints!() click to toggle source
# File lib/db_agent/db_handler.rb, line 121
def require_viewpoints!
  f = viewpoints_folder.expand_path
  Path.require_tree(f) if f.directory?
end
restore(t, args) click to toggle source
# File lib/db_agent/db_handler.rb, line 74
def restore(t, args)
  raise NotImplementedError
end
sequel_db() click to toggle source
# File lib/db_agent/db_handler.rb, line 107
def sequel_db
  @sequel_db ||= ::Sequel.connect(config)
end
sequel_superdb() click to toggle source
# File lib/db_agent/db_handler.rb, line 111
def sequel_superdb
  raise "No superconfig set" if superconfig.nil?
  @sequel_superdb ||= ::Sequel.connect(superconfig)
end
spy() click to toggle source
# File lib/db_agent/db_handler.rb, line 90
def spy
  raise NotImplementedError
end
system(cmd, *args) click to toggle source
# File lib/db_agent/db_handler.rb, line 116
def system(cmd, *args)
  puts cmd
  ::Kernel.system(cmd, *args)
end
wait() click to toggle source
# File lib/db_agent/db_handler.rb, line 60
def wait
  15.downto(0) do |i|
    begin
      puts "Using #{config}"
      sequel_db.test_connection
      puts "Database is there. Great."
      break
    rescue Sequel::Error
      raise if i==0
      sleep(1)
    end
  end
end
wait_server() click to toggle source
# File lib/db_agent/db_handler.rb, line 41
def wait_server
  require 'net/ping'
  raise "No host found" unless config[:host]
  check = Net::Ping::External.new(config[:host])
  puts "Trying to ping `#{config[:host]}`"
  15.downto(0) do |i|
    print "."
    if check.ping?
      print "\nServer found.\n"
      break
    elsif i == 0
      print "\n"
      raise "Server not found, I give up."
    else
      sleep(1)
    end
  end
end