Apartment::Railtie calls Apartment::Tenant.init during to_prepare, which is executed during application boot. This means that a connection is established before user code gets to run.
This is problematic as it disallows booting the rails application if the database server is down or unavailable:
% DATABASE_URL=postgresql://asdf:asdf@localhost:1234/buk_development bin/rails runner 'puts 1'
<snip>
`initialize': could not connect to server: Connection refused (PG::ConnectionBad)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 1234?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 1234?
This means that certain tasks, which shouldn't require a database, now do.
Steps to reproduce
Boot an application with
bin/rails r "puts ActiveRecord::Base.connected?"
Expected behavior
The above command to print false
Actual behavior
It prints true
System configuration
Database: Postgres 11.3
Apartment version: 2.2.0
Apartment config (in
config/initializers/apartment.rb
or so):use_schemas
:true
Rails (or ActiveRecord) version: 5.1
Ruby version: 2.5.5
Apartment::Railtie
callsApartment::Tenant.init
duringto_prepare
, which is executed during application boot. This means that a connection is established before user code gets to run.This is problematic as it disallows booting the rails application if the database server is down or unavailable:
This means that certain tasks, which shouldn't require a database, now do.