database_cleaner/spec/support/sequel/sequel_helper.rb

62 lines
1.6 KiB
Ruby
Raw Normal View History

require 'sequel'
require 'support/database_helper'
def db_config
SequelHelper.new.send(:db_config)
end
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
if config[:url] == "sqlite:///"
# NO-OP
elsif config[:url] == "postgres:///"
::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
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 }
end
def database
config[:connection_options]['database']
end
end