lib/apartment/adapters/mysql2_adapter.rb in apartment-0.26.1 vs lib/apartment/adapters/mysql2_adapter.rb in apartment-1.0.0
- old
+ new
@@ -11,10 +11,16 @@
end
module Adapters
class Mysql2Adapter < AbstractAdapter
+ def initialize(config)
+ super
+
+ @default_tenant = config[:database]
+ end
+
protected
# Connect to new tenant
# Abstract adapter will catch generic ActiveRecord error
# Catch specific adapter errors here
@@ -23,25 +29,23 @@
#
def connect_to_new(tenant = nil)
super
rescue Mysql2::Error
Apartment::Tenant.reset
- raise DatabaseNotFound, "Cannot find tenant #{environmentify(tenant)}"
+ raise TenantNotFound, "Cannot find tenant #{environmentify(tenant)}"
end
end
class Mysql2SchemaAdapter < AbstractAdapter
- attr_reader :default_tenant
-
def initialize(config)
super
@default_tenant = config[:database]
reset
end
- # Reset current_tenant to the default_tenant
+ # Reset current tenant to the default_tenant
#
def reset
Apartment.connection.execute "use #{default_tenant}"
end
@@ -51,19 +55,19 @@
Apartment.excluded_models.each{ |model| process_excluded_model(model) }
end
protected
- # Set schema current_tenant to new db
+ # Connect to new tenant
#
def connect_to_new(tenant)
return reset if tenant.nil?
Apartment.connection.execute "use #{environmentify(tenant)}"
rescue ActiveRecord::StatementInvalid
Apartment::Tenant.reset
- raise DatabaseNotFound, "Cannot find tenant #{environmentify(tenant)}"
+ raise TenantNotFound, "Cannot find tenant #{environmentify(tenant)}"
end
def process_excluded_model(model)
model.constantize.tap do |klass|
# Ensure that if a schema *was* set, we override