Multiple migration statements in one migration file

Multiple migration statements in one migration file

sequelize migration
sequelize migration add foreign key
sequelize migration insert data
sequelize update model migration
sequelize migration change multiple columns
sequelize migration primary key
sequelize seed specific file
sequelize create migration from existing model

I am trying to execute multiple migration statements in a single migration file in order to make changes to multiple columns of same table in one go.

I want to know that whether I am doing it in a write way or not or is there a better and more appropriate way to do it:

Migration Code
module.exports = {
    up: function(queryInterface, Sequelize, done) {

        queryInterface.changeColumn('users', 'name', {
            type: Sequelize.STRING,
            allowNull: false,
            require: true,
            unique: true
        }).success(function() {
            queryInterface.changeColumn('users', 'address', {
                type: Sequelize.STRING,
                allowNull: false,
                require: true,
                unique: true
            }).success(function() {
                queryInterface.changeColumn('users', 'city', {
                    type: Sequelize.STRING,
                    allowNull: false,
                    require: true,
                    unique: true
                }).success(function() {
                    queryInterface.changeColumn('users', 'state', {
                        type: Sequelize.STRING,
                        allowNull: false,
                        require: true,
                        defaultValue: "ncjnbcb"
                    });
                    done();
                });
            });
        });
    }
};

But I face an error which says:

TypeError: undefined is not a function

Since i couldn't find any way of debugging error in migrations, it will be great if someone helps me out in resolving it or if possible, tell about the way as of how can we figure out the errors in a migration.


Your TypeError is probably because you're not returning anything. The docs say that each migration function should return a Promise. No mention of a done callback.

To that end, try the following:

return Promise.all([
  queryInterface.changeColumn..., 
  queryInterface.changeColumn...
]);

Support for multiple statements when using external SQL file � Issue , Support for multiple statements when using external SQL file #505 --sql-file -e mysql — I get an error that suggests multiple statements aren't But that node-db -migrate doesn't apply the multipleStatements config option� The SharePoint Migration Tool (SPMT) provides the ability to scan your files and provide assessment reports . To find any issues with your file before migration, turn on the setting Only perform scanning. If you have multiple sources you wish to assess, consider using the bulk process by creating a .JSON or .CSV file.


module.exports = {
  up: async (queryInterface, Sequelize) => {
    try {
      await queryInterface.addColumn('User', 'name', {
        type: Sequelize.STRING
      });
      await queryInterface.addColumn('User', 'nickname', {
        type: Sequelize.STRING
      });
      return Promise.resolve();
    } catch (e) {
      return Promise.reject(e);
    }
  },

  down: async (queryInterface, Sequelize) => {
    try {
      await queryInterface.removeColumn('Challenges', 'name');
      await queryInterface.removeColumn('Challenges', 'nickname');
      return Promise.resolve();
    } catch (e) {
      return Promise.reject(e);
    }
  }
};

Migration failed with multiple statements wrapped in BEGIN , I get an error when I try to run a migration with multiple statements My migrations might contain multiple statements within a single file. At first� To allow for multiple statements in a single migration, you can use the `x-multi-statement` param. Note that this simply splits the migration into separately-executed statements by a semi-colon ';'. The queries are also not executed in any sort of transaction/batch, meaning you are responsible for fixing partial migrations. dhui on Jul 21, 2018


Using Promise.all with transactions (safer migration) :

module.exports = {
  up: async (queryInterface, Sequelize) => {
    return queryInterface.sequelize.transaction(t => {
      return Promise.all([
        queryInterface.changeColumn('users', 'name', 
          { type: Sequelize.STRING },
          { transaction: t }
        ),
        queryInterface.changeColumn('users', 'address', 
          { type: Sequelize.STRING },
          { transaction: t }
        ),
        queryInterface.changeColumn('users', 'city', 
          { type: Sequelize.STRING },
          { transaction: t }
        )
      ]);
    });
  },

  down: async (queryInterface, Sequelize) => {
    return queryInterface.sequelize.transaction((t) => {
      return Promise.all([
        queryInterface.removeColumn('users', 'name', { transaction: t }),
        queryInterface.removeColumn('users', 'address', { transaction: t }),
        queryInterface.removeColumn('users', 'city', { transaction: t })
      ])
    })
  }
};

Using Promise.all without transactions would cause issues if some of the queries are rejected. It is safe to use transactions so that all operations would be executed successfully or none of the changes would be made.

Usage, In these newer versions, the create command will generate a file containing the Executing multiple statements against the database within a single migration� Folders or file with multiple owners (co-admin in Box) Content should be migrated to the appropriate shared library. Any user who has access to the shared library and follows the associated SharePoint team site, will have the SharePoint team site appear on the left side navigation of OneDrive on the web.


So this is a combination of 2 answers.

@Firmino Changani - Works great but will complete some of the migrations even if some fail

@Aswin Sanakan - Means they all work or none migrate but if the second migration depends on the first it will not work

I was creating a table and adding a special index to that table. So I ended up combining them and the below works for me:

'use strict';
module.exports = {
  up: async (queryInterface, Sequelize) => {
    return queryInterface.sequelize.transaction(async t => {
      try {
        await queryInterface.createTable(
          'phonenumbers',
          {
            id: {
              allowNull: false,
              autoIncrement: true,
              primaryKey: true,
              type: Sequelize.INTEGER
            },
            full_number: {
              type: Sequelize.STRING,
              unique: true
            },
            phone: {
              type: Sequelize.STRING
            },
            extension: {
              type: Sequelize.INTEGER,
            },
            country_id: {
              type: Sequelize.INTEGER
            },
            is_valid_format: {
              type: Sequelize.BOOLEAN
            },
            type: {
              type: Sequelize.STRING
            },
            createdAt: {
              allowNull: false,
              type: Sequelize.DATE
            },
            updatedAt: {
              allowNull: false,
              type: Sequelize.DATE
            },
          },
          { transaction: t }
        ),
        await queryInterface.addIndex(
          'phonenumbers',
          ['phone'],
          {
            name: 'constraint-phone-extension',
            where: {extension: null},
            transaction: t
          }
        )
        return Promise.resolve();
      } catch (e) {
        return Promise.reject(e);
      }
    });
  },
  down: async (queryInterface, Sequelize) => {
    return queryInterface.sequelize.transaction(async t => {
      try {
        await queryInterface.dropTable('phonenumbers', { transaction: t }),
        await queryInterface.removeIndex('phonenumbers', 'constraint-phone-extension', { transaction: t })
        return Promise.resolve();
      } catch (e) {
        return Promise.reject(e);
      }
    })
  }
};

Commands, This will execute 5 migrations, but you can be also more specific. To execute one migration by its name you can pass the name like the following: db-migrate up� 2 Creating a Migration 2.1 Creating a Standalone Migration. Migrations are stored as files in the db/migrate directory, one for each migration class. The name of the file is of the form YYYYMMDDHHMMSS_create_products.rb, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration.


Package, Executing multiple statements against the database within a single migration to always specify the sql-file option in your pg-db-migrate create commands, you� You may want to store your migrations in a different assembly than the one containing your DbContext. You can also use this strategy to maintain multiple sets of migrations, for example, one for development and another for release-to-release upgrades.


Migrations - Migrations, Within a single migration run, repeatable migrations are always applied last, after all can be written either in SQL or in Java and can consist of multiple statements. definition can then simply be maintained in a single file in version control. Setup Migration Manager agents. 7/14/2020; 4 minutes to read +1; In this article. The Migration Manager centralizes the management of large file share migrations by configuring one or more computers or virtual machines (VMs) as migration "agents". To do this, you download and run a setup file on each computer.


Migration Style Guide, This file is automatically generated by Rails, so you normally should not edit this file In some cases, these DDL statements require a specific database lock. Calling more than one migration helper is not a problem if they're executed on the� Before the actual migration, setup a testing environment and execute the migration tools you have prepared on a couple of sample data as a way to estimate the time you may need for the process and the tools that may be necessary for its completion.You may also see baseline project plans. 5. Using the Migration Data and the Data Repair Process.