Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging the migrations in the SequelizeMeta table (This works) #16

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

Alevsk
Copy link

@Alevsk Alevsk commented Jan 1, 2018

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

@himanshu206
Copy link

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

@himanshu206
Copy link

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

@Alevsk
Copy link
Author

Alevsk commented Feb 12, 2018

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.

@himanshu206
Copy link

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
Copy link

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

@ohansrud
Copy link

ohansrud commented May 6, 2018

Just tried out this branch and it works great.

Did you manage to resolve this issue?

@Alevsk
Copy link
Author

Alevsk commented May 6, 2018

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

@himanshu206
Copy link

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

@ohansrud
Copy link

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

@gaganpreet
Copy link

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.

}).then(() => {
const alreadyExecuted = [];
try {
sequelize.query('SELECT * FROM "SequelizeMeta"', { type: sequelize.QueryTypes.SELECT })

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is SELECT * FROM "SequelizeMeta" correctly?

i think it should be SELECT * FROM `SequelizeMeta`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants