3

When getting started with sequelize, I simply used force:true to keep the database working for development, but now that I'm transitioning to test/production this is obviously no longer a good choice.

I've defined my schema in my code like such:

this.Users = this.sequelize.define('Users', {
  email: Sequelize.STRING,
  display: Sequelize.STRING,
  lastlogin: Sequelize.DATE,
});

this.Patterns = this.sequelize.define('Patterns', {
  name: Sequelize.STRING,
  type: Sequelize.STRING,
});
this.Patterns.belongsTo(this.Users, {as: 'Owner'});

... //a number of other models and relationships

What's the "correct" way to turn this into a database initialization workflow? In particular, I'm thinking that it makes sense to make this take place in an "init" migration so that future migrations and code changes work off of it and so that any time in the future I can just run the stack of migrations to get a fully up-to-date schema.

Edit: Things that I have considered and would likely work, but don't seem like sufficiently "elegant" solutions.

  • Initting a new database with force:true and capturing the generated SQL to create a "setup.sql" that I can run on a new database.
  • Rewriting my entire schema in "migration notation" which is identical in what it describes but annoyingly different in syntax.
Julian
  • 424
  • 2
  • 8

1 Answers1

0

For your initial schema and development, you can use sync. Then once your app is live and you need to change your schema, you should write a migration file along with it that will be run in production (and also allows to revert the process). It can be run by sequelize-cli.

felixfbecker
  • 2,273
  • 1
  • 19
  • 24
  • So.. say I have an app that's got a bunch of migrations and a schema and I have a dev database that I've been working with.. How do I boot up a test database from scratch without A. Copying the dev database and B. editing my code to include force sync? – Julian Sep 22 '16 at 22:42
  • If you want to keep the data in your database, use migrations. If you don't care, use sync. You don't need to "edit your code", you just need a simple file that calls it that you can run. Even an npm script or a gulp task could do this. – felixfbecker Sep 23 '16 at 21:26