Fix `PG::ConnectionBad` error when running fixtures
Fix `PG::ConnectionBad` error when running fixtures

At first this appeared to be a multi-db bug but after some invesitgation
it was clear that this can occur just by calling `establish_connection`
from ApplicationRecord.

After some investigation we found that this only occurred when using
fixtures. The console boots fine, the server runs fine, and the tests
even run fine if we used paralellization or eager loading in the tests.

I tracked the issue down to the line that calls
`self.connection_specification_name = name` in the SchemaMigration
changes for Rails 6.0. But how can this be? That is not that major of a
change? How could `connection_specification_name` be a problem?

First `connection_specification_name` caches the name of the connection
specificatio. Second, fixtures were incorrectly holding onto a reference
to that connection.

So when you went to run the tests the models wouldn't be connected and
when the fixtures tried to load the data it would choke on that
unconnected database.

The changes here move the connection into a lambda so we can call it
when we need it rather than blowing up before the model is connected.

Fixes #36743

Co-authored-by: Aaron Patterson <7bbcc45b0f8f88a262415b9271579a59b891be2e@gmail.com>

MSP-Greg/rails,lcreid/rails,prathamesh-sonpatki/rails,kmcphillips/rails,iainbeeston/rails,MSP-Greg/rails,Erol/rails,Envek/rails,mechanicles/rails,notapatch/rails,palkan/rails,eileencodes/rails,MSP-Greg/rails,mechanicles/rails,yahonda/rails,fabianoleittes/rails,fabianoleittes/rails,gauravtiwari/rails,vipulnsward/rails,schuetzm/rails,jeremy/rails,yhirano55/rails,lcreid/rails,joonyou/rails,starknx/rails,georgeclaghorn/rails,starknx/rails,palkan/rails,betesh/rails,yalab/rails,mechanicles/rails,yawboakye/rails,rails/rails,Vasfed/rails,shioyama/rails,rafaelfranca/omg-rails,georgeclaghorn/rails,yahonda/rails,eileencodes/rails,rails/rails,rafaelfranca/omg-rails,betesh/rails,Envek/rails,arunagw/rails,betesh/rails,BlakeWilliams/rails,yhirano55/rails,kddeisz/rails,arunagw/rails,bogdanvlviv/rails,schuetzm/rails,georgeclaghorn/rails,tgxworld/rails,yawboakye/rails,notapatch/rails,arunagw/rails,yahonda/rails,bogdanvlviv/rails,Vasfed/rails,tgxworld/rails,yalab/rails,EmmaB/rails-1,kmcphillips/rails,BlakeWilliams/rails,repinel/rails,jeremy/rails,tjschuck/rails,georgeclaghorn/rails,prathamesh-sonpatki/rails,esparta/rails,esparta/rails,Erol/rails,Edouard-chin/rails,Vasfed/rails,Envek/rails,kddeisz/rails,fabianoleittes/rails,schuetzm/rails,repinel/rails,joonyou/rails,palkan/rails,Stellenticket/rails,yhirano55/rails,yalab/rails,betesh/rails,joonyou/rails,Erol/rails,Edouard-chin/rails,rails/rails,jeremy/rails,vipulnsward/rails,fabianoleittes/rails,iainbeeston/rails,kddeisz/rails,tjschuck/rails,tjschuck/rails,shioyama/rails,Edouard-chin/rails,arunagw/rails,lcreid/rails,Stellenticket/rails,esparta/rails,yawboakye/rails,BlakeWilliams/rails,kmcphillips/rails,bogdanvlviv/rails,Stellenticket/rails,eileencodes/rails,BlakeWilliams/rails,rails/rails,repinel/rails,prathamesh-sonpatki/rails,notapatch/rails,EmmaB/rails-1,yalab/rails,Edouard-chin/rails,tgxworld/rails,yahonda/rails,Vasfed/rails,lcreid/rails,joonyou/rails,bogdanvlviv/rails,yawboakye/rails,vipulnsward/rails,iainbeeston/rails,EmmaB/rails-1,schuetzm/rails,kddeisz/rails,mechanicles/rails,starknx/rails,notapatch/rails,repinel/rails,Stellenticket/rails,shioyama/rails,tgxworld/rails,eileencodes/rails,gauravtiwari/rails,palkan/rails,rafaelfranca/omg-rails,tjschuck/rails,Envek/rails,gauravtiwari/rails,Erol/rails,vipulnsward/rails,esparta/rails,MSP-Greg/rails,kmcphillips/rails,prathamesh-sonpatki/rails,iainbeeston/rails,jeremy/rails,shioyama/rails,yhirano55/rails