重命名Sequelize迁移表

时间:2016-02-17 14:18:21

标签: node.js migration sequelize.js

我在migrations中使用Sequelize使用CLI命令,例如:sequelize db:migrate

如何配置Sequelize以使用不同的表名进行迁移,例如migrations代替SequelizeMeta

通过umzug看起来像it could be configured,但是,我不知道如何使用Sequelize的CLI将此配置传递给它。

3 个答案:

答案 0 :(得分:1)

Umzug Sequelize storage options是:

/**
 * @param {Object} [options]
 * @param {Object} [options.]
 * @param {Object} [options.sequelize] - configured instance of Sequelize.
 * @param {Object} [options.model] - Sequelize model - must have column name
 * matching "columnName" option.
 * @param {String} [options.modelName='SequelizeMeta'] - name of the model
 * to create if "model" option is not supplied.
 * @param {String} [options.tableName=modelName] - name of the table to create
 * if "model" option is not supplied.
 * @param {String} [options.schema=schema] - name of the schema to create
 * the table under, defaults to undefined.
 * @param {String} [options.columnName='name'] - name of the table column
 * holding migration name.
 * @param {String} [options.columnType=Sequelize.STRING] - type of the column.
 * For utf8mb4 charsets under InnoDB, you may need to set this <= 190.
 * @param {Boolean} [options.timestamps=false] - option to add timestamps to the model table
 */

因此,您可以在Umzug初始化时传递tableName选项:

import Umzug from 'umzug';

const  umzug = new Umzug({
  storage: 'sequelize',
  storageOptions: {
    sequelize, // your initialized sequelize instance
    tableName: 'sequelize_migrations', // your custom migrations table name
  },
  ... // other settings
});

答案 1 :(得分:0)

对于那些不知道的人,也花了一点时间来搞清楚。 不得不通过umzug的测试来了解正在发生的事情。

设置尽可能接近sequelize-cli(不得不深入了解sequilize-cli),因此您无需更改迁移文件。

我正在使用TypeScript,我希望理解它是如何工作的不会有问题。

import * as Sequelize from "sequelize";
import * as Umzug from "umzug";

let dbConfig = {
    "dialect": "sqlite",
    "storage": "test.db",
    "seederStorage": "sequelize"
},
let sequelize = new Sequelize(dbName, username, password, dbConfig);

let umzug = new Umzug({
    storage: "sequelize",
    storageOptions: {
        sequelize: sequelize,
        // tableName: "migrations"
        tableName: "SequelizeData" // default table name for sequelize-cli `seeder`
    },
    migrations: {
        path: "built/migrations/seeder",
        params: [sequelize.getQueryInterface(), sequelize.Sequelize] // arguments for `up` and `down`
    }
});

...

umzug.up();
umzug.down();

...

迁移文件:

module.exports = {
    up: function (queryInterface: QueryInterface, sequelize: DataTypes) {
        return queryInterface.bulkInsert("some_table", {});
    },
    down: function (queryInterface: QueryInterface, sequelize: DataTypes) {
        return queryInterface.bulkDelete("some_table", {});
    }
};

答案 2 :(得分:0)

快进(3年),但会很有用:

在config.json或config.js中(取决于您是否需要dynamic configuration)添加选项

"migrationStorageTableName": "your_name_for_migration_tables" ( Default: SequelizeMeta )

其他默认值

  "development": {
     "username": "root",
     "password": null,
     "database": "database_development",
     "host": "127.0.0.1",
     "dialect": "mysql", }

有关详细说明,请阅读manual