FIX: Rails 6 support for multisite

FIX: Rails 6 support for multisite

Rails 6 expects to use a DatabaseConfigurations object as a holding pattern for specs, this change checks if this object is defined and then will use it where appropriate.

Test are now passing on Rails 6.

diff --git a/lib/rails_multisite/connection_management.rb b/lib/rails_multisite/connection_management.rb
index 092bd8e..39db9a4 100644
--- a/lib/rails_multisite/connection_management.rb
+++ b/lib/rails_multisite/connection_management.rb
@@ -129,7 +129,14 @@ module RailsMultisite
         v[:prepared_statements] = false if no_prepared_statements
       end
 
-      resolver = spec_klass::Resolver.new(configs)
+      resolve_configs = configs
+
+      # rails 6 needs to use a proper object for the resolver
+      if defined?(ActiveRecord::DatabaseConfigurations)
+        resolve_configs = ActiveRecord::DatabaseConfigurations.new(configs)
+      end
+
+      resolver = spec_klass::Resolver.new(resolve_configs)
       @db_spec_cache = Hash[*configs.map { |k, _| [k, resolver.spec(k.to_sym)] }.flatten]
 
       @host_spec_cache = {}
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 612dcdd..0f0e050 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -4,6 +4,8 @@ RSpec.configure do |config|
 
   require 'sqlite3'
   require 'byebug'
+  require 'active_record'
+  require 'active_record/base'
 
   class SQLite3::Database
     def self.query_log
@@ -26,7 +28,12 @@ RSpec.configure do |config|
   end
 
   config.before(:suite) do
-    ActiveRecord::Base.configurations["test"] = (YAML::load(File.open("spec/fixtures/database.yml"))['test'])
+    if defined?(ActiveRecord::DatabaseConfigurations)
+      configs = ActiveRecord::DatabaseConfigurations.new(YAML::load(File.open("spec/fixtures/database.yml")))
+      ActiveRecord::Base.configurations = configs
+    else
+      ActiveRecord::Base.configurations = YAML::load(File.open("spec/fixtures/database.yml"))
+    end
   end
 
 end

GitHub sha: 47cb5c64

1 Like