customink / secondbase

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

Seed secondbase #36

Closed jpete7683 closed 8 years ago

jpete7683 commented 8 years ago

Ability to have a seed.rb file for the secondbase db, along with rake db:secondbase:seed task to work

metaskills commented 8 years ago

Neat idea... but can your db/seeds.rb use models/factories that already know which connection to use? Seems we get this for free since models champion the connection?

jpete7683 commented 8 years ago

Not sure that I follow exactly. The rake migration commands seem to work just fine for creating the secondbase DB, couldn't the seed command look to the top level secondbase directory under db and read in a seed.rb file to populate it?

metaskills commented 8 years ago

My question is why is a DB specific seed file needed?

jpete7683 commented 8 years ago

So if I have 2 databases that require population. One database schema drives the interface interaction in the rails FE application and the other database is what it manipulates. There are some tables in both database that I would like to pre-populate using a seed command.

metaskills commented 8 years ago

There are some tables in both database that I would like to pre-populate using a seed command.

That's great... I'm trying to find out why a different file is needed. Case in point... the seed file is evaluated after your Rails application starts. So lets assume you have a model called User which subclasses the Secondbase connection and another model called Order which is in your primary ActiveRecord base connection. Then your seeds file may look like this:

@user = User.create! ...
@order = Order.create! user: @user, ...

And it would just work by using each models connection to each DB. Why do you need a seed file per DB or why Secondbase should be concerned with this feature?

jpete7683 commented 8 years ago

I am following you now. I wasn't aware that it worked in this manner. I would still like to see the enhancement if you had the time to put into it, as if the two databases have no correlation to each other and at some point you wanted to move the one database to its own standalone app you could move it's seeding file with it. Also I am using seeding as kind of a backup strategy as one database is always getting blown away (execpt for one table that is important) with certain restful calls that come across, so I wrote code to parse that table and make a seed.rb file so I can go rebuilt the schema anytime in the secondbase database and pre-populate that table with its latest data. Thoughts?

hmadison commented 8 years ago

You can make a task like:

namespace :db do
  namespace :secondbase do
    task seed: [:environment] do
        load 'db/secondbase_seeds.rb'
    end
  end
end

if you need the functionality. I feel like this doens't make sense to include into secondbase though.

jpete7683 commented 8 years ago

any specific name that I need to give the task file?

hmadison commented 8 years ago

no

metaskills commented 8 years ago

I feel like this doens't make sense to include into secondbase though.

You could also load 'db/secondbase_seeds.rb' from your db/seeds.rb file too. Closing this issue because I think we all agree this is not a Secondbase concern. Thanks Hunter for chiming in!