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 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.
-
#initialize(config) ⇒ AbstractAdapter
constructor
@constructor @param Hash config Database config.
-
#process(tenant = nil) ⇒ Object
Connect to tenant, do your biz, switch back to previous tenant.
-
#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
Switch to new connection (or schema if appopriate).
Constructor Details
#initialize(config) ⇒ AbstractAdapter
@constructor
@param {Hash} config Database config
8 9 10 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 8 def initialize(config) @config = config end |
Instance Method Details
#create(tenant) ⇒ Object
Create a new tenant, import schema, seed if appropriate
@param {String} tenant Tenant name
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 16 def create(tenant) create_tenant(tenant) process(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 ??
48 49 50 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 48 def current current_tenant end |
#current_database ⇒ Object
Get the current tenant name
@return {String} current tenant name
33 34 35 36 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 33 def current_database warn "[Deprecation Warning] `current_database` is now deprecated, please use `current_tenant`" current_tenant end |
#current_tenant ⇒ Object
Get the current tenant name
@return {String} current tenant name
42 43 44 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 42 def current_tenant Apartment.connection.current_database end |
#drop(tenant) ⇒ Object
Drop the tenant
@param {String} tenant Database name
56 57 58 59 60 61 62 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 56 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 DatabaseNotFound, "The tenant #{environmentify(tenant)} cannot be found" end |
#process(tenant = nil) ⇒ Object
Connect to tenant, do your biz, switch back to previous tenant
@param {String?} tenant Database or schema to connect to
68 69 70 71 72 73 74 75 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 68 def process(tenant = nil) previous_tenant = current_tenant switch(tenant) yield if block_given? ensure switch(previous_tenant) rescue reset end |
#process_excluded_models ⇒ Object
Establish a new connection for each specific excluded model
79 80 81 82 83 84 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 79 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
88 89 90 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 88 def reset Apartment.establish_connection @config end |
#seed_data ⇒ Object Also known as: seed
Load the rails seed file into the db
107 108 109 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 107 def seed_data silence_stream(STDOUT){ load_or_abort("#{Rails.root}/db/seeds.rb") } # Don't log the output of seeding the db end |
#switch(tenant = nil) ⇒ Object
Switch to new connection (or schema if appopriate)
@param {String} tenant Database name
96 97 98 99 100 101 102 103 |
# File 'lib/apartment/adapters/abstract_adapter.rb', line 96 def switch(tenant = nil) # Just connect to default db and return return reset if tenant.nil? connect_to_new(tenant).tap do ActiveRecord::Base.connection.clear_query_cache end end |