Module: ConnectionManager::ConnectionHelpers
- Included in:
- ActiveRecord::Base
- Defined in:
- lib/connection_manager/helpers/connection_helpers.rb
Constant Summary collapse
- @@managed_connections =
HashWithIndifferentAccess.new
Instance Method Summary collapse
- #add_managed_connections(yml_key, value) ⇒ Object
-
#current_database_name ⇒ Object
(also: #database_name, #current_schema_name, #schema_name)
Returns the database_name of the connection unless set otherwise.
-
#current_database_name=(current_database_name) ⇒ Object
(also: #database_name=, #current_schema_name=, #schema_name=)
Sometimes we need to manually set the database name, like when the connection has a database but our table is in a different database/schema but on the same DMS.
-
#establish_managed_connection(yml_key, opts = {}) ⇒ Object
Establishes and checks in a connection, normally for abstract classes AKA connection classes.
- #managed_connection_classes ⇒ Object
-
#managed_connections ⇒ Object
A place to store managed connections.
-
#readonly=(readonly) ⇒ Object
Allow setting of readonly at the model level.
-
#readonly? ⇒ Boolean
Returns true if this is a readonly only a readonly model If the connection.readonly? then the model that uses the connection must be readonly.
-
#set_to_readonly ⇒ Object
Override ActiveRecord::Base instance method readonly? to force readonly connections.
-
#use_database(database_name = nil, opts = {}) ⇒ Object
(also: #use_schema)
Tell Active Record to use a different database/schema on this model.
- #yml_key ⇒ Object
Instance Method Details
#add_managed_connections(yml_key, value) ⇒ Object
43 44 45 46 47 |
# File 'lib/connection_manager/helpers/connection_helpers.rb', line 43 def add_managed_connections(yml_key,value) @@managed_connections[yml_key] ||= [] @@managed_connections[yml_key] << value unless @@managed_connections[yml_key].include?(value) @@managed_connections end |
#current_database_name ⇒ Object Also known as: database_name, current_schema_name, schema_name
Returns the database_name of the connection unless set otherwise
7 8 9 10 |
# File 'lib/connection_manager/helpers/connection_helpers.rb', line 7 def current_database_name return "#{connection.config[:database].to_s}" if @current_database_name.blank? @current_database_name end |
#current_database_name=(current_database_name) ⇒ Object Also known as: database_name=, current_schema_name=, schema_name=
Sometimes we need to manually set the database name, like when the connection has a database but our table is in a different database/schema but on the same DMS.
19 20 21 |
# File 'lib/connection_manager/helpers/connection_helpers.rb', line 19 def current_database_name=current_database_name @current_database_name= current_database_name end |
#establish_managed_connection(yml_key, opts = {}) ⇒ Object
Establishes and checks in a connection, normally for abstract classes AKA connection classes.
Options:
-
:abstract_class - used the set #abstract_class, default is true
-
:readonly - force all instances to readonly
-
:class_name - name of connection class name, default is current class name
-
:table_name_prefix - prefix to append to table name for cross database joins,
default is the "#{self.database_name}."
EX:
class MyConnection < ActiveRecord::Base
establish_managed_connection :key_from_db_yml,{:readonly => true}
end
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/connection_manager/helpers/connection_helpers.rb', line 99 def establish_managed_connection(yml_key,opts={}) @yml_key = yml_key opts = {:class_name => self.name, :abstract_class => true}.merge(opts) establish_connection(yml_key) self.abstract_class = opts[:abstract_class] set_to_readonly if (readonly? || opts[:readonly] || self.connection.readonly?) add_managed_connections(yml_key,opts[:class_name]) use_database(self.current_database_name,opts) end |
#managed_connection_classes ⇒ Object
49 50 51 |
# File 'lib/connection_manager/helpers/connection_helpers.rb', line 49 def managed_connection_classes managed_connections.values.flatten end |
#managed_connections ⇒ Object
A place to store managed connections
39 40 41 |
# File 'lib/connection_manager/helpers/connection_helpers.rb', line 39 def managed_connections @@managed_connections end |
#readonly=(readonly) ⇒ Object
Allow setting of readonly at the model level
34 35 36 |
# File 'lib/connection_manager/helpers/connection_helpers.rb', line 34 def readonly=readonly @readonly = readonly end |
#readonly? ⇒ Boolean
Returns true if this is a readonly only a readonly model If the connection.readonly? then the model that uses the connection must be readonly.
29 30 31 |
# File 'lib/connection_manager/helpers/connection_helpers.rb', line 29 def readonly? ((@readonly == true)||connection.readonly?) end |
#set_to_readonly ⇒ Object
Override ActiveRecord::Base instance method readonly? to force readonly connections.
112 113 114 115 116 117 |
# File 'lib/connection_manager/helpers/connection_helpers.rb', line 112 def set_to_readonly self.readonly = true define_method(:readonly?) do true end end |
#use_database(database_name = nil, opts = {}) ⇒ Object Also known as: use_schema
Tell Active Record to use a different database/schema on this model. You may call #use_database when your schemas reside on the same database server and you do not want to create extra connection class and database.yml entries.
Options:
-
:table_name_prefix - the prefix required for making cross database/schema
joins for you database management system. By default table_name_prefix is the database/schema name followed by a period EX: “my_database.”
-
:table_name - the table name for the model if it does not match ActiveRecord
naming conventions
EX: class LegacyUser < ActiveRecord::Base
use_database('DBUser', :table_name => 'UserData')
end
LegacyUser.limit(1).to_sql => "SELECT * FROM `BDUser`.`UserData` LIMIT 1
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/connection_manager/helpers/connection_helpers.rb', line 74 def use_database(database_name=nil,opts={}) self.current_database_name = database_name if database_name opts[:table_name_prefix] = "#{self.current_database_name}." if opts[:table_name_prefix].blank? && self.connection.cross_database_support? unless self.abstract_class? || self.name == "ActiveRecord::Base" opts[:table_name] = self.table_name if opts[:table_name].blank? opts[:table_name].gsub!(self.table_name_prefix,'') unless self.table_name_prefix.blank? self.table_name = "#{opts[:table_name_prefix]}#{opts[:table_name]}" end self.table_name_prefix = opts[:table_name_prefix] unless opts[:table_name_prefix].blank? end |
#yml_key ⇒ Object
53 54 55 |
# File 'lib/connection_manager/helpers/connection_helpers.rb', line 53 def yml_key @yml_key end |