Last active
June 21, 2016 20:57
-
-
Save bacarini/66d5e9b27c5fbc7a0a53391ee35e1ba9 to your computer and use it in GitHub Desktop.
config/initializers/sequel_init.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require ‘sequel’ | |
module MyApp | |
class SequelDb | |
class << self | |
attr_reader :main_db, :alt_db | |
def start_connections | |
@main_db ||= establish_connection | |
@alt_db ||= establish_connection('alternative-database-name') | |
end | |
def disconnect_all | |
main_db.disconnect | |
alt_db.disconnect | |
end | |
private | |
def establish_connection(database = nil) | |
Sequel.connect(params_to_connect(database)) | |
end | |
def params_to_connect(database = nil) | |
options = db_config | |
options.merge!(database: database) if database | |
options | |
end | |
def db_config | |
YAML.load_file(File.expand_path('../../database.yml', __FILE__))[Rails.env] | |
end | |
end | |
end | |
end | |
MyApp::SequelDb.start_connections |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment