Created
May 16, 2014 21:22
-
-
Save phurni/caac3efcbb43a38b8946 to your computer and use it in GitHub Desktop.
override active_record.initialize_database in rails (rails4)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.expand_path('../boot', __FILE__) | |
require 'rails/all' | |
if ar_initdb = ActiveRecord::Railtie.initializers.find {|item| item.name == 'active_record.initialize_database'} | |
ActiveRecord::Railtie.initializers.delete(ar_initdb) | |
ActiveRecord::Railtie.initializer :after => ar_initdb.after do | |
ActiveSupport.on_load(:active_record) do | |
self.configurations = Rails.application.config.database_configuration | |
begin | |
establish_connection | |
rescue ActiveRecord::NoDatabaseError, ActiveRecord::AdapterNotSpecified, ActiveRecord::AdapterNotFound | |
if app.config.database_not_required | |
app.config.middleware.delete "ActiveRecord::QueryCache" | |
app.config.middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement" | |
else | |
warn <<-end_warning | |
Oops - You have a database configured, but it doesn't exist yet! | |
Here's how to get started: | |
1. Configure your database in config/database.yml. | |
2. Run `bin/rake db:create` to create the database. | |
3. Run `bin/rake db:setup` to load your database schema. | |
end_warning | |
raise | |
end | |
end | |
end | |
end | |
end | |
# Rest of your application.rb file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment