Class: MultiAR::Database Private

Inherits:
Object
  • Object
show all
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

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.

Returns:

  • real connection name, nil in case connection is not available



33
34
35
36
37
# 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.app.databases.include? base_name
  return nil unless MultiAR.app.databases.include? base_name
  "#{base_name}_#{MultiAR.app.environment}"
end

.database_configObject

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.

Returns:

  • database configuration in a hash.



56
57
58
59
60
61
# 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") ⇒ 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.

TODO:

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
# 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) ⇒ 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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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