Skip to content

Commit

Permalink
Allow rails to be initialized without connecting to the database
Browse files Browse the repository at this point in the history
  • Loading branch information
lunks authored and rpbaltazar committed Nov 9, 2020
1 parent e21ce12 commit baa9975
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/apartment/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions spec/examples/generic_adapter_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand Down

0 comments on commit baa9975

Please sign in to comment.