Sequelize.js: how to use migrations and sync

后端 未结 11 1398
一向
一向 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:07

    Friend I had the same question and managed to understand how to use them.

    I started without ORM sequelize therefore I already had a data model.
    I had to generate the models automatically with sequelize-auto and generate their migrations with this file that you create https://gist.github.com/ahelord/a7a7d293695b71aadf04157f0f7dee64 and put in sync ({Force: false})
    This is in dev.I would have to version the model and the migrations and execute them every time I pull the code.

    In production the server is only upstairs so you only have to run migrations and in each commit manage as you will version the model without stopping the backend

    0 讨论(0)
  • 2020-12-02 04:08

    Just learning this myself, but I think I would recommend using migrations now so you get used to them. I've found the best thing for figuring out what goes in the migration is to look at the sql on the tables created by sequelize.sync() and then build the migrations from there.

    migrations -c [migration name] 
    

    Will create the template migration file in a migrations directory. You can then populate it with the fields you need created. This file will need to include createdAt/updatedAt, fields needed for associations, etc.

    For initial table creation down should have:

    migration.dropTable('MyTable');
    

    But subsequent updates to the table structure can leave this out and just use alter table.

    ./node_modules/.bin/sequelize --migrate
    

    An example create would look like:

    module.exports = {
      up: function(migration, DataTypes, done) {
        migration.createTable(
            'MyTable',
            {
              id: {
                type: DataTypes.INTEGER,
                primaryKey: true,
                autoIncrement: true
              },
              bigString: {type: DataTypes.TEXT, allowNull: false},
              MyOtherTableId: DataTypes.INTEGER,
              createdAt: {
                type: DataTypes.DATE
              },
              updatedAt: {
                type: DataTypes.DATE
              }
            });
        done();
      },
      down: function(migration, DataTypes, done) {
        migration.dropTable('MyTable');
        done();
      }
    

    To redo from start:

    ./node_modules/.bin/sequelize --migrate --undo
    ./node_modules/.bin/sequelize --migrate
    

    I'm using coffee to run a seed file to populate the tables after:

    coffee server/seed.coffee
    

    This just has a create function in it that looks something like:

    user = db.User.create
      username: 'bob'
      password: 'suruncle'
      email: 'bob@bob.com'
    .success (user) ->
      console.log 'added user'
      user_id = user.id
      myTable = [
        field1: 'womp'
        field2: 'rat'
    
        subModel: [
          field1: 'womp'
         ,
          field1: 'rat'
        ]
      ]
    

    Remember to take your sync() out of index in your models or it will overwrite what the migrations and seed do.

    Docs are at http://sequelize.readthedocs.org/en/latest/docs/migrations/ of course. But the basic answer is you have to add everything in yourself to specify the fields you need. It doesn't do it for you.

    0 讨论(0)
  • 2020-12-02 04:08

    For development, there is now an option to sync the current tables by altering their structure. Using the latest version from the sequelize github repo, you can now run sync with the alter parameter.

    Table.sync({alter: true})
    

    A caveat from the docs:

    Alters tables to fit models. Not recommended for production use. Deletes data in columns that were removed or had their type changed in the model.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-02 04:10

    A bit late, and after reading the documentation, you don't need to have that first migration that you are talking about. All you have to do is to call sync in order to create the tables.

    sequelize.sync()

    You can also run a simple model synchronization by doing something like:

    Project.sync() but I think that sequelize.sync() is a more useful general case for your project (as long as you import the good models at start time).

    (taken from http://sequelizejs.com/docs/latest/models#database-synchronization)

    This will create all initial structures. Afterwards, you will only have to create migrations in order to evolve your schemas.

    hope it helps.

    0 讨论(0)
提交回复
热议问题