1

I am planning to use Postgres as my database and sequelize as my ORM framework one of my NodeJS project. I need your suggestion on how to accomplish database table changes in production enviroment.

For example , we deployed our version-1 application with some database schema. Later , there is a change in my schema in Version-2. Say one of the table is altered and few columns are dropped and few columns are added. Also one of the table itself is not required in Version-2.

In my production , I have some database with data. When I install Version-2 of my NodeJS application , I need to keep the old data as it is and apply the new database schema of Version-2.

How to achieve this , Let me point out some reference to this.

Thanks

JavaUser
  • 25,542
  • 46
  • 113
  • 139

1 Answers1

1

Sequelize (or rather, sequelize-cli) has built in support for migrations.

You can write migrations using the Sequelize js api (e.g. queryInterface.dropColumn('users', 'address')) or write raw SQL and pass it to sequelize.query()

Callum
  • 1,004
  • 2
  • 12
  • 18
Tom Jardine-McNamara
  • 2,418
  • 13
  • 24
  • Do I need to write my own script file to handle this case . "Sequelize" won't do it automatically? – JavaUser Oct 21 '16 at 05:06
  • If you have `sequelize-cli` installed you can have it generate migration skeletons for you (run `sequelize db:migration:create`) which you then modify to make the required database changes - is that what you're looking for? – Tom Jardine-McNamara Oct 21 '16 at 08:52
  • Yes I want a kind of solution which auto generate migration script using the sequelize data model. Can you point me some reference page or suggest some solution? – JavaUser Oct 21 '16 at 08:56
  • 1
    I see what you mean now. I would also like to generate migrations from model definitions, but unfortunately it's not possible! Take a look at http://stackoverflow.com/questions/27835801/how-to-auto-generate-migrations-with-sequelize-cli-from-sequelize-models for some workarounds and advice – Tom Jardine-McNamara Oct 21 '16 at 09:17