Class: Apartment::Adapters::AbstractAdapter
- Inherits:
-
Object
- Object
- Apartment::Adapters::AbstractAdapter
- Defined in:
- lib/apartment/adapters/abstract_adapter.rb
Direct Known Subclasses
AbstractJDBCAdapter, Mysql2Adapter, Mysql2SchemaAdapter, PostgresqlAdapter, PostgresqlSchemaAdapter, Sqlite3Adapter
Instance Attribute Summary collapse
-
#default_tenant ⇒ Object
(also: #default_schema)
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 ??.
-
#current_database ⇒ Object
Get the current tenant name.
-
#current_tenant ⇒ Object
Get the current tenant name.
-
#drop(tenant) ⇒ Object
Drop the tenant.
-
#each(tenants = Apartment.tenant_names) ⇒ Object
Iterate over all tenants, switch to tenant and yield tenant name.
-
#initialize(config) ⇒ AbstractAdapter
constructor
@constructor @param Hash config Database config.
-
#process(tenant = nil, &block) ⇒ Object
[Deprecated].
-
#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
11 12 13 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 11 def initialize(config) @config = config end |
Instance Attribute Details
#default_tenant ⇒ Object Also known as: default_schema
Return the original public tenant
@return {String} default tenant name
60 61 62 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 60 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
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 19 def create(tenant) 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 |
#current ⇒ Object
Note alias_method here doesn’t work with inheritence apparently ??
52 53 54 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 52 def current Apartment.connection.current_database end |
#current_database ⇒ Object
Get the current tenant name
@return {String} current tenant name
36 37 38 39 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 36 def current_database Apartment::Deprecation.warn "[Deprecation Warning] `current_database` is now deprecated, please use `current`" current end |
#current_tenant ⇒ Object
Get the current tenant name
@return {String} current tenant name
45 46 47 48 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 45 def current_tenant Apartment::Deprecation.warn "[Deprecation Warning] `current_tenant` is now deprecated, please use `current`" current end |
#drop(tenant) ⇒ Object
Drop the tenant
@param {String} tenant name
69 70 71 72 73 74 75 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 69 def drop(tenant) # Apartment.connection.drop_database note that drop_database will not throw an exception, so manually execute Apartment.connection.execute("DROP DATABASE #{environmentify(tenant)}" ) rescue *rescuable_exceptions raise TenantNotFound, "The tenant #{environmentify(tenant)} cannot be found" end |
#each(tenants = Apartment.tenant_names) ⇒ Object
Iterate over all tenants, switch to tenant and yield tenant name
117 118 119 120 121 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 117 def each(tenants = Apartment.tenant_names) tenants.each do |tenant| switch(tenant){ yield tenant } end end |
#process(tenant = nil, &block) ⇒ Object
- Deprecated
110 111 112 113 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 110 def process(tenant = nil, &block) Apartment::Deprecation.warn("[Deprecation Warning] `process` is now deprecated. Please use `switch`") switch(tenant, &block) end |
#process_excluded_models ⇒ Object
Establish a new connection for each specific excluded model
125 126 127 128 129 130 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 125 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| excluded_model.constantize.establish_connection @config end end |
#reset ⇒ Object
Reset the tenant connection to the default
134 135 136 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 134 def reset Apartment.establish_connection @config end |
#seed_data ⇒ Object Also known as: seed
Load the rails seed file into the db
140 141 142 143 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 140 def seed_data # Don't log the output of seeding the db silence_stream(STDOUT){ load_or_abort(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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 93 def switch(tenant = nil) if block_given? begin previous_tenant = current switch!(tenant) yield ensure switch!(previous_tenant) rescue reset end else Apartment::Deprecation.warn("[Deprecation Warning] `switch` now requires a block reset to the default tenant after the block. Please use `switch!` instead if you don't want this") switch!(tenant) end end |
#switch!(tenant = nil) ⇒ Object
Switch to a new tenant
@param {String} tenant name
81 82 83 84 85 86 87 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 81 def switch!(tenant = nil) return reset if tenant.nil? connect_to_new(tenant).tap do Apartment.connection.clear_query_cache end end |