sequelize-auto-migrations icon indicating copy to clipboard operation
sequelize-auto-migrations copied to clipboard

Logging the migrations in the SequelizeMeta table (This works)

Open Alevsk opened this issue 6 years ago • 10 comments

Hi I looked into Thedeceptio's pull request about logging the migrations on the sequelizeMeta table but did not work due to several asynchronous bugs, I went ahead and fixed all of them and now when you launch runmigration.js, if not exists, it will create the table and log the script names there, next time you try to run more migrations the script will ignore those that were already executed

Alevsk avatar Jan 01 '18 23:01 Alevsk

Hi, this is a great improvement, for now (means till this MR merge), how to achieve this ?

himanshu206 avatar Feb 12 '18 10:02 himanshu206

As I am trying, it is executing all migrations one by one, which is creating problems :(

himanshu206 avatar Feb 12 '18 10:02 himanshu206

Hi @himanshu206, please can you tell me if the code is throwing some errors? or what are the issues you are seeing?, The idea is to execute the migrations one by one, because if the current migration fails we don't have a way to know if the new created/deleted table/column/data is going to be need it for the next migrations, so we need to stop all the execution.

Alevsk avatar Feb 12 '18 16:02 Alevsk

Hi, this is my initial user model:

'use strict';
module.exports = (sequelize, DataTypes) => {
    var User = sequelize.define('User', {
        firstName: DataTypes.STRING,
        lastName: DataTypes.STRING,
        email: DataTypes.STRING,
    }, {
        classMethods: {
            associate: function(models) {
                // associations can be defined here
            }
        }
    });
    return User;
};

Migration 1:


'use strict';

var Sequelize = require('sequelize');

/**
 * Actions summary:
 *
 * createTable "Users", deps: []
 *
 **/

var info = {
    "revision": 1,
    "name": "user-creation",
    "created": "2018-02-14T09:41:03.806Z",
    "comment": ""
};

var migrationCommands = [{
    fn: "createTable",
    params: [
        "Users",
        {
            "id": {
                "type": Sequelize.INTEGER,
                "autoIncrement": true,
                "primaryKey": true,
                "allowNull": false
            },
            "firstName": {
                "type": Sequelize.STRING
            },
            "lastName": {
                "type": Sequelize.STRING
            },
            "email": {
                "type": Sequelize.STRING
            },
            "createdAt": {
                "type": Sequelize.DATE,
                "allowNull": false
            },
            "updatedAt": {
                "type": Sequelize.DATE,
                "allowNull": false
            }
        },
        {}
    ]
}];

module.exports = {
    pos: 0,
    up: function(queryInterface, Sequelize)
    {
        var index = this.pos;
        return new Promise(function(resolve, reject) {
            function next() {
                if (index < migrationCommands.length)
                {
                    let command = migrationCommands[index];
                    console.log("[#"+index+"] execute: " + command.fn);
                    index++;
                    queryInterface[command.fn].apply(queryInterface, command.params).then(next, reject);
                }
                else
                    resolve();
            }
            next();
        });
    },
    info: info
};

Migration 2:

'use strict';

var Sequelize = require('sequelize');

/**
 * Actions summary:
 *
 * addColumn "phone" to table "Users"
 *
 **/

var info = {
    "revision": 2,
    "name": "user-model-updation",
    "created": "2018-02-14T09:42:42.716Z",
    "comment": ""
};

var migrationCommands = [{
    fn: "addColumn",
    params: [
        "Users",
        "phone",
        {
            "type": Sequelize.STRING
        }
    ]
}];

module.exports = {
    pos: 0,
    up: function(queryInterface, Sequelize)
    {
        var index = this.pos;
        return new Promise(function(resolve, reject) {
            function next() {
                if (index < migrationCommands.length)
                {
                    let command = migrationCommands[index];
                    console.log("[#"+index+"] execute: " + command.fn);
                    index++;
                    queryInterface[command.fn].apply(queryInterface, command.params).then(next, reject);
                }
                else
                    resolve();
            }
            next();
        });
    },
    info: info
};

Migration 3:

'use strict';

var Sequelize = require('sequelize');

/**
 * Actions summary:
 *
 * removeColumn "phone" from table "Users"
 *
 **/

var info = {
    "revision": 3,
    "name": "user-model-updation",
    "created": "2018-02-14T09:43:46.544Z",
    "comment": ""
};

var migrationCommands = [{
    fn: "removeColumn",
    params: ["Users", "phone"]
}];

module.exports = {
    pos: 0,
    up: function(queryInterface, Sequelize)
    {
        var index = this.pos;
        return new Promise(function(resolve, reject) {
            function next() {
                if (index < migrationCommands.length)
                {
                    let command = migrationCommands[index];
                    console.log("[#"+index+"] execute: " + command.fn);
                    index++;
                    queryInterface[command.fn].apply(queryInterface, command.params).then(next, reject);
                }
                else
                    resolve();
            }
            next();
        });
    },
    info: info
};

Error on applying Migration 3:

Migrations to execute:
	1-user-creation.js
	2-user-model-updation.js
	3-user-model-updation.js
Execute migration from file: 1-user-creation.js
[#0] execute: createTable
Executing (default): CREATE TABLE IF NOT EXISTS `Users` (`id` INTEGER NOT NULL auto_increment , `firstName` VARCHAR(255), `lastName` VARCHAR(255), `email` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Execute migration from file: 2-user-model-updation.js
[#0] execute: addColumn
Executing (default): ALTER TABLE `Users` ADD `phone` VARCHAR(255);
{ SequelizeDatabaseError: Duplicate column name 'phone'
    at Query.formatError (/Users/himanshugupta/Desktop/YFS/api/node_modules/sequelize/lib/dialects/mysql/query.js:247:16)
    at Query.handler [as onResult] (/Users/himanshugupta/Desktop/YFS/api/node_modules/sequelize/lib/dialects/mysql/query.js:68:23)
    at Query.Command.execute (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/commands/command.js:30:12)
    at Connection.handlePacket (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/connection.js:502:28)
    at PacketParser.onPacket (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/connection.js:81:16)
    at PacketParser.executeStart (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/packet_parser.js:77:14)
    at Socket.<anonymous> (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/connection.js:89:29)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:189:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:551:20)
  name: 'SequelizeDatabaseError',
  parent: 
   { Error: Duplicate column name 'phone'
       at Packet.asError (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/packets/packet.js:713:13)
       at Query.Command.execute (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/commands/command.js:28:22)
       at Connection.handlePacket (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/connection.js:502:28)
       at PacketParser.onPacket (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/connection.js:81:16)
       at PacketParser.executeStart (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/packet_parser.js:77:14)
       at Socket.<anonymous> (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/connection.js:89:29)
       at emitOne (events.js:96:13)
       at Socket.emit (events.js:189:7)
       at readableAddChunk (_stream_readable.js:176:18)
       at Socket.Readable.push (_stream_readable.js:134:10)
       at TCP.onread (net.js:551:20)
     code: 'ER_DUP_FIELDNAME',
     errno: 1060,
     sqlState: '42S21',
     sqlMessage: 'Duplicate column name \'phone\'',
     sql: 'ALTER TABLE `Users` ADD `phone` VARCHAR(255);' },
  original: 
   { Error: Duplicate column name 'phone'
       at Packet.asError (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/packets/packet.js:713:13)
       at Query.Command.execute (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/commands/command.js:28:22)
       at Connection.handlePacket (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/connection.js:502:28)
       at PacketParser.onPacket (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/connection.js:81:16)
       at PacketParser.executeStart (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/packet_parser.js:77:14)
       at Socket.<anonymous> (/Users/himanshugupta/Desktop/YFS/api/node_modules/mysql2/lib/connection.js:89:29)
       at emitOne (events.js:96:13)
       at Socket.emit (events.js:189:7)
       at readableAddChunk (_stream_readable.js:176:18)
       at Socket.Readable.push (_stream_readable.js:134:10)
       at TCP.onread (net.js:551:20)
     code: 'ER_DUP_FIELDNAME',
     errno: 1060,
     sqlState: '42S21',
     sqlMessage: 'Duplicate column name \'phone\'',
     sql: 'ALTER TABLE `Users` ADD `phone` VARCHAR(255);' },
  sql: 'ALTER TABLE `Users` ADD `phone` VARCHAR(255);' }

himanshu206 avatar Feb 14 '18 09:02 himanshu206

The problem is : it is applying migrations again from the beginning, but it should apply only the current migration ?

himanshu206 avatar Feb 14 '18 09:02 himanshu206

Just tried out this branch and it works great.

Did you manage to resolve this issue?

ohansrud avatar May 06 '18 18:05 ohansrud

@himanshu206 Hi, can you tell me if the SequelizeMeta table exists in your database?

Alevsk avatar May 06 '18 18:05 Alevsk

Hi Alevsk, that question of mine is too old, now forgot what actually was the problem, will look into again and will tell here :)

himanshu206 avatar May 07 '18 04:05 himanshu206

Is this library still active maintained? 6 months since this PR was created.

ohansrud avatar Jun 26 '18 15:06 ohansrud

It's a little disappointing that this very useful project makes one thing easy by generating migrations from diffs, but on the other hand now I have to manually remember which migrations to run when I deploy some code. I'd be glad to help resolve the merge conflicts if that's what preventing this feature from being merged.

gaganpreet avatar Mar 07 '19 11:03 gaganpreet