Class: Apartment::Adapters::AbstractAdapter
- Inherits:
-
Object
- Object
- Apartment::Adapters::AbstractAdapter
- Includes:
- ActiveSupport::Callbacks
- Defined in:
- lib/apartment/adapters/abstract_adapter.rb
Overview
Abstract adapter from which all the Apartment DB related adapters will inherit the base logic
Direct Known Subclasses
AbstractJDBCAdapter, Mysql2Adapter, Mysql2SchemaAdapter, PostgresqlAdapter, PostgresqlSchemaAdapter, Sqlite3Adapter
Defined Under Namespace
Classes: SeparateDbConnectionHandler
Instance Attribute Summary collapse
-
#default_tenant ⇒ Object
Return the original public tenant.
Instance Method Summary collapse
-
#create(tenant) ⇒ Object
Create a new tenant, import schema, seed if appropriate.
-
#current ⇒ Object
Note alias_method here doesn’t work with inheritence apparently ??.
-
#drop(tenant) ⇒ Object
Drop the tenant.
-
#each(tenants = Apartment.tenant_names) ⇒ Object
Iterate over all tenants, switch to tenant and yield tenant name.
-
#environmentify(tenant) ⇒ Object
Prepend the environment if configured and the environment isn’t already there.
-
#init ⇒ Object
Initialize Apartment config options such as excluded_models.
-
#initialize(config) ⇒ AbstractAdapter
constructor
@constructor @param Hash config Database config.
-
#process_excluded_models ⇒ Object
Establish a new connection for each specific excluded model.
-
#reset ⇒ Object
Reset the tenant connection to the default.
-
#seed_data ⇒ Object
(also: #seed)
Load the rails seed file into the db.
-
#switch(tenant = nil) ⇒ Object
Connect to tenant, do your biz, switch back to previous tenant.
-
#switch!(tenant = nil) ⇒ Object
Switch to a new tenant.
Constructor Details
#initialize(config) ⇒ AbstractAdapter
@constructor
@param {Hash} config Database config
16 17 18 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 16 def initialize(config) @config = config end |
Instance Attribute Details
#default_tenant ⇒ Object
Return the original public tenant
@return {String} default tenant name
55 56 57 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 55 def default_tenant @default_tenant || Apartment.default_tenant end |
Instance Method Details
#create(tenant) ⇒ Object
Create a new tenant, import schema, seed if appropriate
@param {String} tenant Tenant name
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 24 def create(tenant) run_callbacks(:create) do create_tenant(tenant) switch(tenant) do import_database_schema # Seed data if appropriate seed_data if Apartment.seed_after_create yield if block_given? end end end |
#current ⇒ Object
Note alias_method here doesn’t work with inheritence apparently ??
47 48 49 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 47 def current Apartment.connection.current_database end |
#drop(tenant) ⇒ Object
Drop the tenant
@param {String} tenant name
63 64 65 66 67 68 69 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 63 def drop(tenant) with_neutral_connection(tenant) do |conn| drop_command(conn, tenant) end rescue *rescuable_exceptions => e raise_drop_tenant_error!(tenant, e) end |
#each(tenants = Apartment.tenant_names) ⇒ Object
Iterate over all tenants, switch to tenant and yield tenant name
103 104 105 106 107 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 103 def each(tenants = Apartment.tenant_names) tenants.each do |tenant| switch(tenant) { yield(tenant) } end end |
#environmentify(tenant) ⇒ Object
Prepend the environment if configured and the environment isn’t already there
@param {String} tenant Database name
@return {String} tenant name with Rails environment *optionally* prepended
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 138 def environmentify(tenant) return tenant if tenant.nil? || tenant.include?(Rails.env) if Apartment.prepend_environment "#{Rails.env}_#{tenant}" elsif Apartment.append_environment "#{tenant}_#{Rails.env}" else tenant end end |
#init ⇒ Object
Initialize Apartment config options such as excluded_models
41 42 43 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 41 def init process_excluded_models end |
#process_excluded_models ⇒ Object
Establish a new connection for each specific excluded model
111 112 113 114 115 116 117 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 111 def process_excluded_models # All other models will shared a connection (at Apartment.connection_class) # and we can modify at will Apartment.excluded_models.each do |excluded_model| process_excluded_model(excluded_model) end end |
#reset ⇒ Object
Reset the tenant connection to the default
121 122 123 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 121 def reset Apartment.establish_connection(@config) end |
#seed_data ⇒ Object Also known as: seed
Load the rails seed file into the db
127 128 129 130 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 127 def seed_data # Don't log the output of seeding the db silence_warnings { load_or_raise(Apartment.seed_data_file) } if Apartment.seed_data_file end |
#switch(tenant = nil) ⇒ Object
Connect to tenant, do your biz, switch back to previous tenant
@param {String?} tenant to connect to
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 87 def switch(tenant = nil) previous_tenant = current switch!(tenant) yield ensure # Always attempt rollback to previous tenant, even if block raised begin switch!(previous_tenant) rescue StandardError => _e # If rollback fails (tenant was dropped, connection lost), fall back to default reset end end |
#switch!(tenant = nil) ⇒ Object
Switch to a new tenant
@param {String} tenant name
75 76 77 78 79 80 81 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 75 def switch!(tenant = nil) run_callbacks(:switch) do connect_to_new(tenant).tap do Apartment.connection.clear_query_cache end end end |