Sequelize.js: how to use migrations and sync

后端 未结 11 1454
一向
一向 2020-12-02 03:20

I\'m close to having my project ready to launch. I have big plans for after launch and the database structure is going to change -- new columns in existing tables as well as

11条回答
  •  余生分开走
    2020-12-02 04:09

    Sequelize can run arbitrary SQL asynchronously.

    What I would do is:

    • Generate a Migration (To use as first migration);
    • Dump your database, something like: mysql_dump -uUSER -pPASS DBNAME > FILE.SQL
    • Either paste the full dump as text (Dangerous) or load a File with the full dump in Node:
      • var baseSQL = "LOTS OF SQL and it's EVIL because you gotta put \ backslashes before line breakes and \"quotes\" and/or sum" + " one string for each line, or everything will break";
      • var baseSQL = fs.readFileSync('../seed/baseDump.sql');
    • Run this dump on Sequelize Migration:
    module.exports = {
      up: function (migration, DataTypes) {
        var baseSQL = "whatever" // I recommend loading a file
        migration.migrator.sequelize.query(baseSQL);
      }
    }
    

    That should take care of setting up the database, albeit the async thing may become a problem. If that happens, I'd look at a way to defer returning the up sequelize function until the async query function is finished.

    More about mysql_dump: http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
    More about Sequelize Migrations: http://sequelize.readthedocs.org/en/latest/docs/migrations/
    More about Running SQL from within Sequelize Migration: https://github.com/sequelize/sequelize/issues/313

提交回复
热议问题