sequelize.js迁移执行顺序

时间:2019-03-21 11:16:57

标签: sequelize.js sequelize-cli

我要在同一迁移中执行几个查询。它们是相关的,因此必须按特定顺序执行。这是我的迁移方法:

up: (queryInterface, Sequelize) =>
queryInterface.sequelize.transaction(transaction =>
  Promise.all([
    queryInterface.renameColumn(table, 'status', 'status_temp', { transaction }),
    queryInterface.sequelize.query(
      'ALTER TYPE enum_users_status RENAME TO enum_users_status_temp;',
      {
        transaction
      }
    ),
    queryInterface.addColumn(
      table,
      'isActive',
      {
        allowNull: false,
        defaultValue: false,
        type: Sequelize.BOOLEAN
      },
      { transaction }
    ),
    queryInterface.bulkUpdate(
      table,
      {
        isActive: true
      },
      {
        status: 'active'
      },
      { transaction }
    ),
    queryInterface.addColumn(
      table,
      'status',
      {
        allowNull: false,
        defaultValue: STATUS_FREE,
        type: Sequelize.ENUM(STATUS_FREE, STATUS_BUSY, STATUS_DELETED)
      },
      { transaction }
    ),
  ])
),

当我执行它时,订单以一种奇怪的方式混合:

Executing (591dc838-4659-4de4-b605-54b7c03c527f): START TRANSACTION;
...
Executing (591dc838-4659-4de4-b605-54b7c03c527f): ALTER TYPE     enum_users_status RENAME TO enum_users_status_temp;
Executing (591dc838-4659-4de4-b605-54b7c03c527f): ALTER TABLE "public"."users" ADD COLUMN "isActive" BOOLEAN NOT NULL DEFAULT false;
Executing (591dc838-4659-4de4-b605-54b7c03c527f): UPDATE "users" SET "isActive"=true WHERE "status" = 'active'
Executing (591dc838-4659-4de4-b605-54b7c03c527f): CREATE TYPE "public"."enum_users_status" AS ENUM('free', 'busy', 'deleted');ALTER TABLE "public"."users" ADD COLUMN "status" "public"."enum_users_status" NOT NULL DEFAULT 'free';
Executing (591dc838-4659-4de4-b605-54b7c03c527f): ALTER TABLE "users" RENAME COLUMN "status" TO "status_temp";
Executing (591dc838-4659-4de4-b605-54b7c03c527f): ROLLBACK;

我因此而出错:

ERROR: column "status" of relation "users" already exists

有没有办法使这些查询按特定顺序运行?

UPD: 我的预期结果是这样的

      START TRANSACTION;
      ALTER TABLE "users" RENAME COLUMN "status" TO "status_temp";
      ALTER TYPE enum_users_status RENAME TO enum_users_status_temp;
      ALTER TABLE "public"."users" ADD COLUMN "isNew" BOOLEAN NOT NULL DEFAULT true;
      UPDATE "users" SET "isNew"=false WHERE "status_temp" = 'active';
      CREATE TYPE "public"."enum_users_status" AS ENUM('free', 'busy', 'deleted');
      ALTER TABLE "public"."users" ADD COLUMN "status" "public"."enum_users_status" NOT NULL DEFAULT 'free';
      UPDATE "users" SET "status"='busy' WHERE "isFree" = false;
      UPDATE "users" SET "status"='deleted' WHERE "status_temp" = 'deleted';
      ALTER TABLE "public"."users" DROP COLUMN "isFree";
      ALTER TABLE "public"."users" DROP COLUMN "status_temp";

3 个答案:

答案 0 :(得分:0)

之所以会这样,是因为您正在使用Promise.all来并行执行promise。不能保证您的诺言将按其定义的顺序执行。因此,您需要使用.then方法来相应地执行工作流程。

up: (queryInterface, Sequelize) =>
queryInterface.sequelize.transaction(transaction =>
  queryInterface.renameColumn(table, 'status', 'status_temp', { transaction })
    .then(() => queryInterface.sequelize.query(
      'ALTER TYPE enum_users_status RENAME TO enum_users_status_temp;',
      {
        transaction
      }
    ))
    .then(...);
),

答案 1 :(得分:0)

正如Eugene指出的那样,您的问题是您正在使用Promise.all调用所有迁移。我强烈建议您进行单个迁移文件。这样,您的项目结构会更好,新开发者也更容易理解。

创建迁移的命令:

sequelize migration:generate --name [name-of-your-migration]

按照创建顺序执行所有迁移,以及尚未运行的迁移:

sequelize db:migrate

答案 2 :(得分:0)

可以通过将bluebirdjs Promise.map函数与{concurrency: 1}选项一起使用,如下所示:

const Promise = require('bluebird');
...

up: function (queryInterface, Sequelize) {
    return Promise.map([
            queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 0'),
            queryInterface.addColumn(
                'table1',
                'column1',
                {
                    type: Sequelize.INTEGER,
                    after: 'id',
                    defaultValue: 0
                }),
            queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 1'),
            queryInterface.addColumn(
                'table2',
                'column2',
                {
                    type: Sequelize.BOOLEAN,
                    after: 'column3',
                    defaultValue: 0
                })
        ],
        (promise) => promise,
        {concurrency: 1}
    );
}

注意:您可以将concurrency选项设置为n,以便同时执行n承诺