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
15 16 17 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 15 def initialize(config) @config = config end |
Instance Attribute Details
#default_tenant ⇒ Object
Return the original public tenant
@return {String} default tenant name
54 55 56 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 54 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
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 23 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 ??
46 47 48 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 46 def current Apartment.connection.current_database end |
#drop(tenant) ⇒ Object
Drop the tenant
@param {String} tenant name
62 63 64 65 66 67 68 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 62 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
100 101 102 103 104 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 100 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
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 135 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
40 41 42 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 40 def init process_excluded_models end |
#process_excluded_models ⇒ Object
Establish a new connection for each specific excluded model
108 109 110 111 112 113 114 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 108 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
118 119 120 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 118 def reset Apartment.establish_connection @config end |
#seed_data ⇒ Object Also known as: seed
Load the rails seed file into the db
124 125 126 127 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 124 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
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 86 def switch(tenant = nil) previous_tenant = current switch!(tenant) yield ensure begin switch!(previous_tenant) rescue StandardError => _e reset end end |
#switch!(tenant = nil) ⇒ Object
Switch to a new tenant
@param {String} tenant name
74 75 76 77 78 79 80 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 74 def switch!(tenant = nil) run_callbacks :switch do connect_to_new(tenant).tap do Apartment.connection.clear_query_cache end end end |