Skip to content

Instantly share code, notes, and snippets.

@hgmnz
Last active December 14, 2015 08:38
Show Gist options
  • Save hgmnz/5058728 to your computer and use it in GitHub Desktop.
Save hgmnz/5058728 to your computer and use it in GitHub Desktop.
# drop this in an initializer
class ActiveRecordOverrideRailtie < Rails::Railtie
initializer "active_record.initialize_database.override" do |app|
ActiveRecord::Base.connection_pool.disconnect!
ActiveSupport.on_load(:active_record) do
if url = ENV['DATABASE_URL']
parsed_url = URI.parse(url)
establish_connection( {
adapter: 'postgres',
host: parsed_url.host,
encoding: 'unicode',
database: parsed_url.path.split("/")[-1],
port: parsed_url.port,
username: parsed_url.user,
password: parsed_url.password,
pool: ENV['DB_POOL'] || 2,
statement_timeout: 30000,
reaping_frequency: 10
})
else
ActiveRecord::Base.configurations = app.config.database_configuration
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment