diff --git a/lib/apartment/tenant.rb b/lib/apartment/tenant.rb index abbe87d5..9144720d 100644 --- a/lib/apartment/tenant.rb +++ b/lib/apartment/tenant.rb @@ -19,36 +19,39 @@ module Tenant # @return {subclass of Apartment::AbstractAdapter} # def adapter - Thread.current[:apartment_adapter] ||= begin - adapter_method = "#{config[:adapter]}_adapter" - - if defined?(JRUBY_VERSION) - case config[:adapter] - when /mysql/ - adapter_method = 'jdbc_mysql_adapter' - when /postgresql/ - adapter_method = 'jdbc_postgresql_adapter' - end - end + warn '****** in modified Apartment::Tenant.adapter' - begin - require "apartment/adapters/#{adapter_method}" - rescue LoadError - raise "The adapter `#{adapter_method}` is not yet supported" - end + current_adapter = Thread.current.thread_variable_get(:apartment_adapter) + return current_adapter if current_adapter - unless respond_to?(adapter_method) - raise AdapterNotFound, "database configuration specifies nonexistent #{config[:adapter]} adapter" + adapter_method = "#{config[:adapter]}_adapter" + if defined?(JRUBY_VERSION) + case config[:adapter] + when /mysql/ + adapter_method = 'jdbc_mysql_adapter' + when /postgresql/ + adapter_method = 'jdbc_postgresql_adapter' end + end + begin + require "apartment/adapters/#{adapter_method}" + rescue LoadError + raise "The adapter `#{adapter_method}` is not yet supported" + end - send(adapter_method, config) + unless respond_to?(adapter_method) + raise AdapterNotFound, "database configuration specifies nonexistent #{config[:adapter]} adapter" end + + Thread.current.thread_variable_set(:apartment_adapter, send(adapter_method, config)) end # Reset config and adapter so they are regenerated # def reload!(config = nil) - Thread.current[:apartment_adapter] = nil + warn '****** in modified Apartment::Tenant.reload!' + + Thread.current.thread_variable_set(:apartment_adapter, nil) @config = config end diff --git a/lib/apartment/version.rb b/lib/apartment/version.rb index 4a24fa5e..cac00e27 100644 --- a/lib/apartment/version.rb +++ b/lib/apartment/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Apartment - VERSION = '2.11.0' + VERSION = '2.11.0-mod1' end diff --git a/spec/tenant_spec.rb b/spec/tenant_spec.rb index 13363b9a..41211513 100644 --- a/spec/tenant_spec.rb +++ b/spec/tenant_spec.rb @@ -7,7 +7,7 @@ before { subject.reload!(config) } describe '#adapter' do - it 'should load mysql adapter' do + it 'loads mysql adapter' do subject.adapter expect(Apartment::Adapters::Mysql2Adapter).to be_a(Class) end @@ -31,7 +31,7 @@ nil end - it 'should create a new database' do + it 'creates a new database' do subject.create 'db_with_prefix' end end @@ -45,7 +45,7 @@ end describe '#adapter' do - it 'should load postgresql adapter' do + it 'loads postgresql adapter' do expect(subject.adapter).to be_a(Apartment::Adapters::PostgresqlSchemaAdapter) end @@ -68,6 +68,14 @@ thread.join expect(subject.current).to eq(db1) end + + it 'maintains the current tenant across fibers within a thread' do + subject.switch!(db1) + expect(subject.current).to eq(db1) + fiber = Fiber.new { expect(subject.current).to eq(db1) } + fiber.resume + expect(subject.current).to eq(db1) + end end end @@ -85,7 +93,7 @@ after { subject.drop db1 } describe '#create' do - it 'should seed data' do + it 'seeds data' do subject.switch! db1 expect(User.count).to be > 0 end @@ -99,7 +107,7 @@ after { subject.drop db2 } - it 'should create a model instance in the current schema' do + it 'creates a model instance in the current schema' do subject.switch! db2 db2_count = User.count + x.times { User.create } @@ -130,7 +138,7 @@ end end - it 'should create excluded models in public schema' do + it 'creates excluded models in public schema' do subject.reset # ensure we're on public schema count = Company.count + x.times { Company.create } @@ -155,14 +163,14 @@ after { subject.drop db1 } - it 'should seed from default path' do + it 'seeds from default path' do subject.create db1 subject.switch! db1 expect(User.count).to eq(3) expect(User.first.name).to eq('Some User 0') end - it 'should seed from custom path' do + it 'seeds from custom path' do Apartment.configure do |config| config.seed_data_file = Rails.root.join('db/seeds/import.rb') end