Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Establishes connection during application boot #607

Open
fsateler opened this issue Jun 20, 2019 · 1 comment · Fixed by rails-on-services/apartment#39 · May be fixed by #612
Open

Establishes connection during application boot #607

fsateler opened this issue Jun 20, 2019 · 1 comment · Fixed by rails-on-services/apartment#39 · May be fixed by #612

Comments

@fsateler
Copy link

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):

config.excluded_models = %w[Tenant Delayed::Job]
config.tenant_names = lambda { Tenant.pluck :name }
config.use_schemas = true
config.persistent_schemas = %w[shared_extensions]
config.prepend_environment = !Rails.env.production?
Rails.application.config.middleware.insert_before Warden::Manager, Apartment::Elevators::Subdomain
  • use_schemas: true

  • Rails (or ActiveRecord) version: 5.1

  • Ruby version: 2.5.5

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.

@mikecmpbll
Copy link
Collaborator

this is one of my bugbears too.

@ArthurWD ArthurWD linked a pull request Aug 2, 2019 that will close this issue
rpbaltazar referenced this issue in rails-on-services/apartment Jun 2, 2020
Prepare Release - 2.6.1

# Enhancements

- N/a

# Bugfixes

- [Resolves influitive#607] Avoid early connection
  - #39
  - #53
  - #51
- [Resolves #52] Rake db:setup tries to seed non existent tenant - #54
- [Resolves #56] DB rollback uses second last migration - #57

# Chores

- N/a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants