diff --git a/README.md b/README.md index b1adea78..f296892c 100644 --- a/README.md +++ b/README.md @@ -594,6 +594,17 @@ module Apartment end ``` +## Running rails console without a connection + +Before this fork, running rails with the gem installed would connect to the database +which is different than the default behavior. To disable this initial +connection, just run with `APARTMENT_DISABLE_INIT` set to something: + +```shell +$ APARTMENT_DISABLE_INIT=true DATABASE_URL=postgresql://localhost:1234/buk_development bin/rails runner 'puts 1' +# 1 +``` + ## Contributing * In both `spec/dummy/config` and `spec/config`, you will see `database.yml.sample` files diff --git a/lib/apartment/railtie.rb b/lib/apartment/railtie.rb index ff534e20..c7d8f6b5 100644 --- a/lib/apartment/railtie.rb +++ b/lib/apartment/railtie.rb @@ -33,6 +33,7 @@ class Railtie < Rails::Railtie config.to_prepare do next if ARGV.any? { |arg| arg =~ /\Aassets:(?:precompile|clean)\z/ } next if ARGV.any? { |arg| arg == 'webpacker:compile' } + next if ENV["APARTMENT_DISABLE_INIT"] begin Apartment.connection_class.connection_pool.with_connection do diff --git a/spec/examples/generic_adapter_examples.rb b/spec/examples/generic_adapter_examples.rb index b46e4da4..767b122c 100644 --- a/spec/examples/generic_adapter_examples.rb +++ b/spec/examples/generic_adapter_examples.rb @@ -24,6 +24,24 @@ expect(num_available_connections).to eq(1) end + + it 'should not connect if env var is set' do + ENV["APARTMENT_DISABLE_INIT"] = "true" + begin + ActiveRecord::Base.connection_pool.disconnect! + + Apartment::Railtie.config.to_prepare_blocks.map(&:call) + + num_available_connections = Apartment.connection_class.connection_pool + .instance_variable_get(:@available) + .instance_variable_get(:@queue) + .size + + expect(num_available_connections).to eq(0) + ensure + ENV.delete("APARTMENT_DISABLE_INIT") + end + end end #