building5 / sails-db-migrate

db-migrate integration for sails.js
MIT License
61 stars 32 forks source link

Seeding data in bootstrap crashes since migrations haven't run yet #8

Closed tobalsgithub closed 10 years ago

tobalsgithub commented 10 years ago

I'm running into an interesting situation. I'm trying to seed data into the database in the config/bootstrap.js file. This was recommended by a number of folks as the best way to handle seed data.

When running grunt db:migrate:up, sails-db-migrate lifts the sails server, which calls the bootstrap.js file, which starts trying to seed my data into the database that hasn't had the migrations run on it yet.

I'm wondering what you think a good solution for this might be. I've thought of a couple things.

  1. Just do my seed in a migration file. This works Ok, but isn't great since you can't see all your seed data in one place.
  2. Add a runtime variable when lifting sails in sails-db-migrate that I can key off in the bootstrap.js file. For example: Sails.lift({ port: -1, log: {level: 'silent'}, environment: env, migrating: true}, .... This way in bootstrap.js I can choose not to insert my seed data if the server is just getting lifted for migrations.

I'm open to other ideas as well, and definitely willing to help with a pull request. Let me know what you think might work best.

leedm777 commented 10 years ago

What timing. @chadxz just added migrating: true to the config when Grunt lifts sails, which was released in @0.4.0.

You should be able to use that, and just if (!sails.config.migrating) in your bootstrap. Let me know if you run into any problems with that workaround!

tobalsgithub commented 10 years ago

Awesome sauce. Thanks.