Skip to content

Commit

Permalink
import from rails-on-services#182
Browse files Browse the repository at this point in the history
  • Loading branch information
shot056 committed Aug 3, 2023
1 parent fefed66 commit 35dbddc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
43 changes: 23 additions & 20 deletions lib/apartment/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/apartment/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Apartment
VERSION = '2.11.0'
VERSION = '2.11.0.1'

Check failure on line 4 in lib/apartment/version.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] lib/apartment/version.rb#L4

Layout/IndentationStyle: Tab detected in indentation.
Raw output
lib/apartment/version.rb:4:1: C: Layout/IndentationStyle: Tab detected in indentation.
	VERSION = '2.11.0.1'
^

Check failure on line 4 in lib/apartment/version.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] lib/apartment/version.rb#L4

Layout/IndentationWidth: Use 2 (not 1) spaces for indentation.
Raw output
lib/apartment/version.rb:4:1: C: Layout/IndentationWidth: Use 2 (not 1) spaces for indentation.
	VERSION = '2.11.0.1'
^
end
24 changes: 16 additions & 8 deletions spec/tenant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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 }

Expand Down Expand Up @@ -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 }

Expand All @@ -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
Expand Down

0 comments on commit 35dbddc

Please sign in to comment.