Class: MultiAR::Database Private
- Inherits:
-
Object
- Object
- MultiAR::Database
- Defined in:
- lib/multi_ar/database.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Database functionality class.
Class Method Summary collapse
-
.connection_name(base_name) ⇒ Object
private
Real connection name, nil in case connection is not available.
-
.database_config ⇒ Object
private
Database configuration in a hash.
- .initialize(db_config: "config/database.yaml", migration_dir: "db/migrate") ⇒ Object private
-
.mysql_client(connection_name) ⇒ Object
private
Expects config file to have the config in activerecord’s format.
Class Method Details
.connection_name(base_name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns real connection name, nil in case connection is not available.
34 35 36 37 38 |
# File 'lib/multi_ar/database.rb', line 34 def self.connection_name base_name raise "#{base_name} is not in databases configuration variable." unless MultiAR.app.databases.include? base_name return nil unless MultiAR.app.databases.include? base_name "#{base_name}_#{MultiAR.app.environment}" end |
.database_config ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns database configuration in a hash.
57 58 59 60 61 62 |
# File 'lib/multi_ar/database.rb', line 57 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") ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
test if this @@initialized thingy actually works, I’m not sure how it in practice works
20 21 22 23 24 25 26 27 28 29 30 31 |
# 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 ActiveRecord::Tasks::DatabaseTasks.migrations_paths = [ migration_dir ] # this is only used by db:new_migration rake task. # TODO: because of that, maybe we want to handle this differently? @@initialized = true end |
.mysql_client(connection_name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Expects config file to have the config in activerecord’s format.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/multi_ar/database.rb', line 41 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 |