2018-05-05 13:03:27 -07:00
|
|
|
require 'sequel'
|
2018-05-05 13:25:15 -07:00
|
|
|
require 'support/database_helper'
|
2018-05-05 13:03:27 -07:00
|
|
|
|
|
|
|
def db_config
|
2018-05-05 13:25:15 -07:00
|
|
|
SequelHelper.new.send(:db_config)
|
2018-05-05 13:03:27 -07:00
|
|
|
end
|
|
|
|
|
2018-05-05 13:25:15 -07:00
|
|
|
class SequelHelper < DatabaseHelper
|
2018-05-22 06:02:13 -07:00
|
|
|
attr_reader :connection
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def establish_connection
|
|
|
|
@connection = ::Sequel.connect(config[:url], config[:connection_options])
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_db
|
2018-05-05 13:20:24 -07:00
|
|
|
if config[:url] == "sqlite:///"
|
|
|
|
# NO-OP
|
|
|
|
elsif config[:url] == "postgres:///"
|
2018-04-26 14:14:28 -07:00
|
|
|
::Sequel.connect(config[:url], config[:connection_options].merge('database' => 'postgres')) do |db|
|
|
|
|
begin
|
|
|
|
db.execute "CREATE DATABASE #{database}"
|
|
|
|
rescue ::Sequel::DatabaseError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
::Sequel.connect(config[:url], config[:connection_options].merge('database' => nil)) do |db|
|
|
|
|
db.execute "DROP DATABASE IF EXISTS #{database}"
|
|
|
|
db.execute "CREATE DATABASE #{database}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-22 06:39:18 -07:00
|
|
|
def drop_db
|
|
|
|
if config[:url] == "sqlite:///"
|
|
|
|
begin
|
|
|
|
File.unlink(db_config['sqlite3']['database'])
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
end
|
|
|
|
elsif config[:url] == "postgres:///"
|
|
|
|
::Sequel.connect(config[:url], config[:connection_options]) do |db|
|
|
|
|
db.execute "DROP TABLE IF EXISTS users"
|
|
|
|
db.execute "DROP TABLE IF EXISTS agents"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
::Sequel.connect(config[:url], config[:connection_options].merge('database' => nil)) do |db|
|
|
|
|
db.execute "DROP DATABASE IF EXISTS #{database}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-22 06:02:13 -07:00
|
|
|
def load_schema
|
2018-05-22 06:11:07 -07:00
|
|
|
connection.create_table!(:users) { primary_key :id }
|
|
|
|
connection.create_table!(:agents) { primary_key :id }
|
2018-04-26 14:14:28 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def database
|
|
|
|
config[:connection_options]['database']
|
|
|
|
end
|
|
|
|
end
|