Skip to content

Commit

Permalink
BP-2147 | Hotfix: Added support for Rails 6 multi-database switching (#1
Browse files Browse the repository at this point in the history
)

This fixes two issues with Apartment's compatibility with Rails 6:

1. Migrations on tenant-specific databases appear to have been broken since at least Rails v6.0.0.beta1 due to a change in the way the connections are switched.  Migrations now appear to run successfully in development after explicitly setting the connection_specification_name.

2. Testing concurrent threads with long-running transactions could result in dead-lock as of Rails v6.0.0.rc2 as threads now properly share connections during testing by default and Apartment appears to not be clearing the query caches for all active connections.  These deadlocks no longer occur with the change to clear all connections' query caches for the current thread instead of just the current connection's query cache.
  • Loading branch information
chrisyuska committed Oct 8, 2019
1 parent 16f6c97 commit dd5d4ac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
23 changes: 20 additions & 3 deletions .travis.yml
@@ -1,15 +1,32 @@
addons:
postgresql: 9.6
dist: trusty
language: ruby
cache: bundler

rvm:
- 2.2.4
- 2.3.1
- 2.6.3
- jruby-9.0.5.0

gemfile:
- gemfiles/rails_5_1.gemfile

bundler_args: --without local
before_install:
- gem install bundler -v '> 1.5.0'
- gem update --system
- gem install bundler

before_script:
- psql -c 'create database apartment_postgresql_test;' -U postgres

env:
RUBY_GC_MALLOC_LIMIT: 90000000
RUBY_FREE_MIN: 200000

matrix:
fast_finish: true

notifications:
email:
on_success: never
on_failure: always
3 changes: 2 additions & 1 deletion lib/apartment/adapters/abstract_adapter.rb
Expand Up @@ -83,7 +83,7 @@ def switch!(tenant)

@current = tenant

Apartment.connection.clear_query_cache
Apartment.connection_class.clear_query_caches_for_current_thread

tenant
end
Expand Down Expand Up @@ -143,6 +143,7 @@ def connection_switch!(config, without_keys: [])
end

Thread.current[:_apartment_connection_specification_name] = config[:name]
Apartment.connection_class.connection_specification_name = config[:name]
simple_switch(config)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/apartment/version.rb
@@ -1,3 +1,3 @@
module Apartment
VERSION = "2.0.0"
VERSION = "2.0.1"
end

0 comments on commit dd5d4ac

Please sign in to comment.