customink / secondbase

Seamless second database integration for Rails.
MIT License
220 stars 30 forks source link

Changing the config_key -- db:create fails #24

Closed RyanNaughton closed 8 years ago

RyanNaughton commented 8 years ago

I am setting the configs in my initializer this way:

Rails.application.config.second_base.path = 'db/warehouse'
Rails.application.config.second_base.config_key = 'warehouse'

When I run rake db:create, here is the stacktrace:

TypeError: can't dup NilClass
gems/activerecord-4.2.5.1/lib/active_record/connection_handling.rb:62:in `dup'
gems/activerecord-4.2.5.1/lib/active_record/connection_handling.rb:62:in `initialize'
gems/activerecord-4.2.5.1/lib/active_record/core.rb:46:in `new'
gems/activerecord-4.2.5.1/lib/active_record/core.rb:46:in `configurations='
gems/secondbase-1.0.0/lib/second_base/on_base.rb:17:in `on_base'
gems/secondbase-1.0.0/lib/second_base/databases.rake:11:in `block (3 levels) in <top (required)>'
gems/secondbase-1.0.0/lib/second_base/databases.rake:125:in `block (2 levels) in <top (required)>'

When I move those configs to application.rb it works fine:

config.second_base.path = 'db/warehouse'
config.second_base.config_key = 'warehouse'

Once the DB is created, the initializer works fine when I run commands in a rails console session. Thus my guess is how the load/init process happens with the rake task and your railtie combo are the likely culprit -- just a guess though.

hmadison commented 8 years ago

Do rake tasks still work with the settings in an initializer after the db was created?

jeffdeville commented 8 years ago

It doesn't appear that db:create works, but the rest of the commands do.

metaskills commented 8 years ago

I did some tests and found out that these configs do not work in an initializer. Rather than digging deeper into the Rails boot lifecycle for db tasks, I would like to recommend that you put them in application.rb

config.second_base.path = 'db/warehouse'
config.second_base.config_key = 'warehouse'

Once I added them there, I found it worked really well. If you can confirm the same for you, I will make a note in the projects README#configurations about it.

jeffdeville commented 8 years ago

Sorry @metaskills, my needs for the other db were so modest and temporary, that I used what I'd learned from your blog posts, and just rolled my own that skipped active record entirely. I'm sure if it worked for you, it would have worked for me as well though.

metaskills commented 8 years ago

No sweat! Fixed by d3d30a3ca979764c955fdd9fbb55498a359eb925