class MultiAR::Database
@api private Database
functionality class.
Public Class Methods
connection_name(base_name)
click to toggle source
@return real connection name, nil in case connection is not available
# File lib/multi_ar/database.rb, line 33 def self.connection_name base_name raise "#{base_name} is not in databases configuration variable." unless MultiAR::databases.include? base_name return nil unless MultiAR::databases.include? base_name "#{base_name}_#{MultiAR.app.environment}" end
database_config()
click to toggle source
@return database configuration in a hash.
# File lib/multi_ar/database.rb, line 56 def self.database_config return @@db_config_cache unless (@@db_config_cache ||= nil).nil? db_config = MultiAR.app.db_config @@db_config_cache = Psych.load_file db_config end
initialize(db_config: "config/database.yaml", migration_dir: "db/migrate")
click to toggle source
@todo test if this @@initialized thingy actually works, I’m not sure how it in practice works
# File lib/multi_ar/database.rb, line 20 def self.initialize db_config: "config/database.yaml", migration_dir: "db/migrate" @@initialized ||= false return if @@initialized == true #Class.new(Rails::Application) unless Rails.application raise "The database configuration file was not found. You can be pass path to it with db_config attribute. Current path: #{db_config}" unless File.exist? db_config db_config_data = YAML.load(ERB.new(File.read db_config).result) include ActiveRecord::Tasks ActiveRecord::Base.configurations = ::ActiveRecord::Tasks::DatabaseTasks.database_configuration = db_config_data @@initialized = true end
mysql_client(connection_name)
click to toggle source
Expects config file to have the config in activerecord’s format.
# File lib/multi_ar/database.rb, line 40 def self.mysql_client connection_name real_connection_name = self.connection_name connection_name @@mysql_client ||= {} return @@mysql_client[real_connection_name] unless @@mysql_client[real_connection_name].nil? raise "Invalid connection name #{real_connection_name}" unless config = self.database_config[real_connection_name] client = Mysql2::Client.new( host: config["host"], username: config["username"], password: config["password"], database: config["database"] ) @@mysql_client[real_connection_name] ||= client end