janko-m/sinatra-activerecord — pull request #37 by ablagoev (2014-04-23 14:18:47+00:00)
Title: Fix DatabaseTasks db configuration on database spec change

The load_config task in activerecord (https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake)

currently contains the following:

```
task :load_config do
ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
end
```

ActiveRecord::Base.configurations is overwritten each time the config is loaded.

Sinatra-Activerecord sets properly the ActiveRecord::Tasks::DatabaseTasks.database_configuration on load (https://github.com/janko-m/sinatra-activerecord/blob/master/lib/sinatra/activerecord/rake/activerecord_4.rb):

```
ActiveRecord::Tasks::DatabaseTasks.tap do |config|
config.root = Rake.application.original_dir
config.env = ENV["RACK_ENV"] || "development"
config.db_dir = "db"
config.migrations_paths = ["db/migrate"]
config.fixtures_path = "test/fixtures"
config.seed_loader = seed_loader
config.database_configuration = ActiveRecord::Base.configurations
end
```

The problem is if you require the sinatra-activerecord gem before setting the database spec, either through set :database or set :database_file. If you require the gem before setting the configuration, database_configuration will be empty and will overwrite ActiveRecord::Base.configurations each time load_config is run.

I have changed the spec, to illustrate the problem and have attached a possible fix.

I am not sure, if this is the right way to fix it though, since Rake code does not belong there. Not sure, there is a better way though. What do you think?