Skip to main content
Version: v6 - stable

迁移

就像你使用 版本控制 系统(例如 Git)来管理源代码中的更改一样,你可以使用迁移来跟踪数据库的更改。通过迁移,你可以将现有数据库转移到另一个状态,反之亦然:这些状态转换保存在迁移文件中,这些文件描述了如何到达新状态以及如何恢复更改以返回到旧状态。

¥Just like you use version control systems such as Git to manage changes in your source code, you can use migrations to keep track of changes to the database. With migrations you can transfer your existing database into another state and vice versa: Those state transitions are saved in migration files, which describe how to get to the new state and how to revert the changes in order to get back to the old state.

你将需要 Sequelize 命令行接口 (CLI)。CLI 支持迁移和项目引导。

¥You will need the Sequelize Command-Line Interface (CLI). The CLI ships support for migrations and project bootstrapping.

Sequelize 中的迁移是一个 javascript 文件,它导出两个函数 updown,指示如何执行迁移和撤消迁移。你手动定义这些函数,但不手动调用它们;CLI 将自动调用它们。在这些函数中,你只需借助 sequelize.query 以及 Sequelize 提供给你的其他方法即可执行你需要的任何查询。除此之外没有额外的魔法。

¥A Migration in Sequelize is a javascript file which exports two functions, up and down, that dictates how to perform the migration and undo it. You define those functions manually, but you don't call them manually; they will be called automatically by the CLI. In these functions, you should simply perform whatever queries you need, with the help of sequelize.query and whichever other methods Sequelize provides to you. There is no extra magic beyond that.

安装 CLI

¥Installing the CLI

要安装 Sequelize CLI:

¥To install the Sequelize CLI:

npm install --save-dev sequelize-cli

详细信息请参见 CLI GitHub 存储库

¥For details see the CLI GitHub repository.

项目引导

¥Project bootstrapping

要创建一个空项目,你需要执行 init 命令

¥To create an empty project you will need to execute init command

npx sequelize-cli init

这将创建以下文件夹

¥This will create following folders

  • config,包含配置文件,告诉 CLI 如何连接数据库

    ¥config, contains config file, which tells CLI how to connect with database

  • models,包含你项目的所有模型

    ¥models, contains all models for your project

  • migrations,包含所有迁移文件

    ¥migrations, contains all migration files

  • seeders,包含所有种子文件

    ¥seeders, contains all seed files

配置

¥Configuration

在继续之前,我们需要告诉 CLI 如何连接到数据库。为此,我们打开默认配置文件 config/config.json。它看起来像这样:

¥Before continuing further we will need to tell the CLI how to connect to the database. To do that let's open default config file config/config.json. It looks something like this:

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

请注意,Sequelize CLI 默认假定使用 mysql。如果你使用其他方言,则需要更改 "dialect" 选项的内容。

¥Note that the Sequelize CLI assumes mysql by default. If you're using another dialect, you need to change the content of the "dialect" option.

现在编辑此文件并设置正确的数据库凭据和方言。对象的键(例如 "development")在 model/index.js 上用于匹配 process.env.NODE_ENV(未定义时,"development" 是默认值)。

¥Now edit this file and set correct database credentials and dialect. The keys of the objects (e.g. "development") are used on model/index.js for matching process.env.NODE_ENV (When undefined, "development" is a default value).

Sequelize 将使用每种方言的默认连接端口(例如,对于 postgres,它是端口 5432)。如果你需要指定不同的端口,请使用 "port" 字段(默认情况下 config/config.js 中不存在该字段,但你可以简单地添加它)。

¥Sequelize will use the default connection port for each dialect (for example, for postgres, it is port 5432). If you need to specify a different port, use the "port" field (it is not present by default in config/config.js but you can simply add it).

注意:如果你的数据库还不存在,你可以调用 db:create 命令。通过适当的访问,它将为你创建该数据库。

¥Note: If your database doesn't exist yet, you can just call db:create command. With proper access it will create that database for you.

创建第一个模型(和迁移)

¥Creating the first Model (and Migration)

正确配置 CLI 配置文件后,你就可以创建第一个迁移了。就像执行一个简单的命令一样简单。

¥Once you have properly configured CLI config file you are ready to create your first migration. It's as simple as executing a simple command.

我们将使用 model:generate 命令。该命令需要两个选项:

¥We will use model:generate command. This command requires two options:

  • name:型号名称;

    ¥name: the name of the model;

  • attributes:模型属性列表。

    ¥attributes: the list of model attributes.

让我们创建一个名为 User 的模型。

¥Let's create a model named User.

npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string

这会:

¥This will:

  • models 文件夹下创建模型文件 user

    ¥Create a model file user in models folder;

  • migrations 文件夹中创建名称类似于 XXXXXXXXXXXXXX-create-user.js 的迁移文件。

    ¥Create a migration file with name like XXXXXXXXXXXXXX-create-user.js in migrations folder.

注意:Sequelize 将仅使用模型文件,它是表表示。另一方面,迁移文件是该模型或更具体地说由 CLI 使用的表的更改。将迁移视为数据库中某些更改的提交或日志。

¥Note: Sequelize will only use Model files, it's the table representation. On the other hand, the migration file is a change in that model or more specifically that table, used by CLI. Treat migrations like a commit or a log for some change in database.

运行迁移

¥Running Migrations

直到这一步,我们还没有向数据库中插入任何内容。我们刚刚为我们的第一个模型 User 创建了所需的模型和迁移文件。现在要在数据库中实际创建该表,你需要运行 db:migrate 命令。

¥Until this step, we haven't inserted anything into the database. We have just created the required model and migration files for our first model, User. Now to actually create that table in the database you need to run db:migrate command.

npx sequelize-cli db:migrate

该命令将执行以下步骤:

¥This command will execute these steps:

  • 将确保数据库中有一个名为 SequelizeMeta 的表。该表用于记录当前数据库上运行了哪些迁移

    ¥Will ensure a table called SequelizeMeta in database. This table is used to record which migrations have run on the current database

  • 开始查找尚未运行的所有迁移文件。这可以通过检查 SequelizeMeta 表来实现。在这种情况下,它将运行我们在上一步中创建的 XXXXXXXXXXXXXX-create-user.js 迁移。

    ¥Start looking for any migration files which haven't run yet. This is possible by checking SequelizeMeta table. In this case it will run XXXXXXXXXXXXXX-create-user.js migration, which we created in last step.

  • 创建一个名为 Users 的表,其中包含其迁移文件中指定的所有列。

    ¥Creates a table called Users with all columns as specified in its migration file.

撤消迁移

¥Undoing Migrations

现在我们的表已经创建并保存在数据库中。通过迁移,你只需运行命令即可恢复到旧状态。

¥Now our table has been created and saved in the database. With migration you can revert to old state by just running a command.

你可以使用 db:migrate:undo,该命令将恢复最近的迁移。

¥You can use db:migrate:undo, this command will revert the most recent migration.

npx sequelize-cli db:migrate:undo

你可以通过使用 db:migrate:undo:all 命令撤消所有迁移来恢复到初始状态。你还可以通过使用 --to 选项传递迁移名称来恢复到特定迁移。

¥You can revert back to the initial state by undoing all migrations with the db:migrate:undo:all command. You can also revert back to a specific migration by passing its name with the --to option.

npx sequelize-cli db:migrate:undo:all --to XXXXXXXXXXXXXX-create-posts.js

创建第一个种子

¥Creating the first Seed

假设我们想默认向几个表中插入一些数据。如果我们继续前面的示例,我们可以考虑为 User 表创建一个演示用户。

¥Suppose we want to insert some data into a few tables by default. If we follow up on the previous example we can consider creating a demo user for the User table.

要管理所有数据迁移,你可以使用播种器。种子文件是数据的一些更改,可用于使用示例或测试数据填充数据库表。

¥To manage all data migrations you can use seeders. Seed files are some change in data that can be used to populate database tables with sample or test data.

让我们创建一个种子文件,它将向 User 表添加一个演示用户。

¥Let's create a seed file which will add a demo user to our User table.

npx sequelize-cli seed:generate --name demo-user

此命令将在 seeders 文件夹中创建一个种子文件。文件名类似于 XXXXXXXXXXXXXX-demo-user.js。它遵循与迁移文件相同的 up / down 语义。

¥This command will create a seed file in seeders folder. File name will look something like XXXXXXXXXXXXXX-demo-user.js. It follows the same up / down semantics as the migration files.

现在我们应该编辑此文件以将演示用户插入到 User 表中。

¥Now we should edit this file to insert demo user to User table.

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('Users', [
{
firstName: 'John',
lastName: 'Doe',
email: 'example@example.com',
createdAt: new Date(),
updatedAt: new Date(),
},
]);
},
down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('Users', null, {});
},
};

运行种子

¥Running Seeds

在上一步中,你创建了一个种子文件;但是,它尚未提交到数据库。为此,我们运行一个简单的命令。

¥In last step you created a seed file; however, it has not been committed to the database. To do that we run a simple command.

npx sequelize-cli db:seed:all

这将执行该种子文件,并且演示用户将被插入到 User 表中。

¥This will execute that seed file and a demo user will be inserted into the User table.

注意:与使用 SequelizeMeta 表的迁移不同,播种器执行历史记录不会存储在任何地方。如果你想更改此行为,请阅读 Storage 部分。

¥Note: Seeder execution history is not stored anywhere, unlike migrations, which use the SequelizeMeta table. If you wish to change this behavior, please read the Storage section.

消除种子

¥Undoing Seeds

如果播种机正在使用任何存储空间,则可以撤消播种机。有两个命令可用于此:

¥Seeders can be undone if they are using any storage. There are two commands available for that:

如果你想撤消最近的种子:

¥If you wish to undo the most recent seed:

npx sequelize-cli db:seed:undo

如果你想撤消特定种子:

¥If you wish to undo a specific seed:

npx sequelize-cli db:seed:undo --seed name-of-seed-as-in-data

如果你想撤消所有种子:

¥If you wish to undo all seeds:

npx sequelize-cli db:seed:undo:all

迁移骨架

¥Migration Skeleton

以下框架显示了典型的迁移文件。

¥The following skeleton shows a typical migration file.

module.exports = {
up: (queryInterface, Sequelize) => {
// logic for transforming into the new state
},
down: (queryInterface, Sequelize) => {
// logic for reverting the changes
},
};

我们可以使用 migration:generate 生成这个文件。这将在你的迁移文件夹中创建 xxx-migration-skeleton.js

¥We can generate this file using migration:generate. This will create xxx-migration-skeleton.js in your migration folder.

npx sequelize-cli migration:generate --name migration-skeleton

传递的 queryInterface 对象可用于修改数据库。Sequelize 对象存储可用的数据类型,例如 STRINGINTEGER。函数 updown 应返回 Promise。让我们看一个例子:

¥The passed queryInterface object can be used to modify the database. The Sequelize object stores the available data types such as STRING or INTEGER. Function up or down should return a Promise. Let's look at an example:

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Person', {
name: Sequelize.DataTypes.STRING,
isBetaMember: {
type: Sequelize.DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
},
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Person');
},
};

以下是在数据库中执行两项更改的迁移示例,使用自动管理事务来确保所有指令成功执行或在失败时回滚:

¥The following is an example of a migration that performs two changes in the database, using an automatically-managed transaction to ensure that all instructions are successfully executed or rolled back in case of failure:

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction(t => {
return Promise.all([
queryInterface.addColumn(
'Person',
'petName',
{
type: Sequelize.DataTypes.STRING,
},
{ transaction: t },
),
queryInterface.addColumn(
'Person',
'favoriteColor',
{
type: Sequelize.DataTypes.STRING,
},
{ transaction: t },
),
]);
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction(t => {
return Promise.all([
queryInterface.removeColumn('Person', 'petName', { transaction: t }),
queryInterface.removeColumn('Person', 'favoriteColor', {
transaction: t,
}),
]);
});
},
};

下一个示例是具有外键的迁移。你可以使用引用来指定外键:

¥The next example is of a migration that has a foreign key. You can use references to specify a foreign key:

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Person', {
name: Sequelize.DataTypes.STRING,
isBetaMember: {
type: Sequelize.DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
},
userId: {
type: Sequelize.DataTypes.INTEGER,
references: {
model: {
tableName: 'users',
schema: 'schema',
},
key: 'id',
},
allowNull: false,
},
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Person');
},
};

下一个示例是使用 async/await 的迁移,其中你使用手动管理的事务在新列上创建唯一索引:

¥The next example is of a migration that uses async/await where you create an unique index on a new column, with a manually-managed transaction:

module.exports = {
async up(queryInterface, Sequelize) {
const transaction = await queryInterface.sequelize.transaction();
try {
await queryInterface.addColumn(
'Person',
'petName',
{
type: Sequelize.DataTypes.STRING,
},
{ transaction },
);
await queryInterface.addIndex('Person', 'petName', {
fields: 'petName',
unique: true,
transaction,
});
await transaction.commit();
} catch (err) {
await transaction.rollback();
throw err;
}
},
async down(queryInterface, Sequelize) {
const transaction = await queryInterface.sequelize.transaction();
try {
await queryInterface.removeColumn('Person', 'petName', { transaction });
await transaction.commit();
} catch (err) {
await transaction.rollback();
throw err;
}
},
};

下一个示例是一个迁移,它创建一个由多个带有条件的字段组成的唯一索引,该索引允许一个关系存在多次,但只有一个可以满足条件:

¥The next example is of a migration that creates an unique index composed of multiple fields with a condition, which allows a relation to exist multiple times but only one can satisfy the condition:

module.exports = {
up: (queryInterface, Sequelize) => {
queryInterface
.createTable('Person', {
name: Sequelize.DataTypes.STRING,
bool: {
type: Sequelize.DataTypes.BOOLEAN,
defaultValue: false,
},
})
.then((queryInterface, Sequelize) => {
queryInterface.addIndex('Person', ['name', 'bool'], {
indicesType: 'UNIQUE',
where: { bool: 'true' },
});
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Person');
},
};

.sequelizerc 文件

¥The .sequelizerc file

这是一个特殊的配置文件。它允许你指定通常作为参数传递给 CLI 的以下选项:

¥This is a special configuration file. It lets you specify the following options that you would usually pass as arguments to CLI:

  • env:运行命令的环境

    ¥env: The environment to run the command in

  • config:配置文件的路径

    ¥config: The path to the config file

  • options-path:带有附加选项的 JSON 文件的路径

    ¥options-path: The path to a JSON file with additional options

  • migrations-path:迁移文件夹的路径

    ¥migrations-path: The path to the migrations folder

  • seeders-path:播种者文件夹的路径

    ¥seeders-path: The path to the seeders folder

  • models-path:模型文件夹的路径

    ¥models-path: The path to the models folder

  • url:要使用的数据库连接字符串。使用 --config 文件的替代方法

    ¥url: The database connection string to use. Alternative to using --config files

  • debug:当可用时显示各种调试信息

    ¥debug: When available show various debug information

一些可以使用它的场景:

¥Some scenarios where you can use it:

  • 你想要覆盖 migrationsmodelsseedersconfig 文件夹的默认路径。

    ¥You want to override default path to migrations, models, seeders or config folder.

  • 你想将 config.json 重命名为 database.json 等其他名称

    ¥You want to rename config.json to something else like database.json

还有更多。让我们看看如何使用此文件进行自定义配置。

¥And a whole lot more. Let's see how you can use this file for custom configuration.

首先,我们在项目的根目录中创建 .sequelizerc 文件,其中包含以下内容:

¥To begin, let's create the .sequelizerc file in the root directory of your project, with the following content:

// .sequelizerc

const path = require('path');

module.exports = {
config: path.resolve('config', 'database.json'),
'models-path': path.resolve('db', 'models'),
'seeders-path': path.resolve('db', 'seeders'),
'migrations-path': path.resolve('db', 'migrations'),
};

通过此配置,你将告诉 CLI:

¥With this config you are telling the CLI to:

  • 使用 config/database.json 文件进行配置设置;

    ¥Use config/database.json file for config settings;

  • 使用 db/models 作为模型文件夹;

    ¥Use db/models as models folder;

  • 使用 db/seeders 作为播种文件夹;

    ¥Use db/seeders as seeders folder;

  • 使用 db/migrations 作为迁移文件夹。

    ¥Use db/migrations as migrations folder.

动态配置

¥Dynamic configuration

默认情况下,配置文件是一个名为 config.json.json 的 JSON 文件。但有时你需要动态配置,例如访问环境变量或执行其他代码来确定配置。

¥The configuration file is by default a JSON file called config.json. But sometimes you need a dynamic configuration, for example to access environment variables or execute some other code to determine the configuration.

值得庆幸的是,Sequelize CLI 可以读取 .json.js 文件。这可以使用 .sequelizerc 文件进行设置。你只需提供 .js 文件的路径作为导出对象的 config 选项:

¥Thankfully, the Sequelize CLI can read from both .json and .js files. This can be setup with .sequelizerc file. You just have to provide the path to your .js file as the config option of your exported object:

const path = require('path');

module.exports = {
config: path.resolve('config', 'config.js'),
};

现在 Sequelize CLI 将加载 config/config.js 以获取配置选项。

¥Now the Sequelize CLI will load config/config.js for getting configuration options.

config/config.js 文件的示例:

¥An example of config/config.js file:

const fs = require('fs');

module.exports = {
development: {
username: 'database_dev',
password: 'database_dev',
database: 'database_dev',
host: '127.0.0.1',
port: 3306,
dialect: 'mysql',
dialectOptions: {
bigNumberStrings: true,
},
},
test: {
username: process.env.CI_DB_USERNAME,
password: process.env.CI_DB_PASSWORD,
database: process.env.CI_DB_NAME,
host: '127.0.0.1',
port: 3306,
dialect: 'mysql',
dialectOptions: {
bigNumberStrings: true,
},
},
production: {
username: process.env.PROD_DB_USERNAME,
password: process.env.PROD_DB_PASSWORD,
database: process.env.PROD_DB_NAME,
host: process.env.PROD_DB_HOSTNAME,
port: process.env.PROD_DB_PORT,
dialect: 'mysql',
dialectOptions: {
bigNumberStrings: true,
ssl: {
ca: fs.readFileSync(__dirname + '/mysql-ca-main.crt'),
},
},
},
};

上面的示例还展示了如何将自定义方言选项添加到配置中。

¥The example above also shows how to add custom dialect options to the configuration.

使用 Babel

¥Using Babel

要在迁移和播种器中启用更现代的结构,你只需安装 babel-register 并在 .sequelizerc 开始时需要它:

¥To enable more modern constructions in your migrations and seeders, you can simply install babel-register and require it at the beginning of .sequelizerc:

npm i --save-dev babel-register
// .sequelizerc

require('babel-register');

const path = require('path');

module.exports = {
config: path.resolve('config', 'config.json'),
'models-path': path.resolve('models'),
'seeders-path': path.resolve('seeders'),
'migrations-path': path.resolve('migrations'),
};

当然,结果将取决于你的 babel 配置(例如在 .babelrc 文件中)。欲了解更多信息,请访问 babeljs.io

¥Of course, the outcome will depend upon your babel configuration (such as in a .babelrc file). Learn more at babeljs.io.

安全提示

¥Security tip

使用环境变量进行配置设置。这是因为诸如密码之类的秘密永远不应该成为源代码的一部分(尤其是不应该提交到版本控制)。

¥Use environment variables for config settings. This is because secrets such as passwords should never be part of the source code (and especially not committed to version control).

存储

¥Storage

你可以使用三种类型的存储:sequelizejsonnone

¥There are three types of storage that you can use: sequelize, json, and none.

  • sequelize :将迁移和种子存储在 sequelize 数据库的表中

    ¥sequelize : stores migrations and seeds in a table on the sequelize database

  • json :将迁移和种子存储在 json 文件中

    ¥json : stores migrations and seeds on a json file

  • none:不存储任何迁移/种子

    ¥none : does not store any migration/seed

迁移存储

¥Migration Storage

默认情况下,CLI 将在数据库中创建一个名为 SequelizeMeta 的表,其中包含每个执行的迁移的条目。要更改此行为,你可以将三个选项添加到配置文件中。使用 migrationStorage,你可以选择用于迁移的存储类型。如果选择 json,则可以使用 migrationStoragePath 指定文件的路径,否则 CLI 将写入文件 sequelize-meta.json。如果你想将信息保留在数据库中,请使用 sequelize,但想使用不同的表,你可以使用 migrationStorageTableName 更改表名。你还可以通过提供 migrationStorageTableSchema 属性为 SequelizeMeta 表定义不同的架构。

¥By default the CLI will create a table in your database called SequelizeMeta containing an entry for each executed migration. To change this behavior, there are three options you can add to the configuration file. Using migrationStorage, you can choose the type of storage to be used for migrations. If you choose json, you can specify the path of the file using migrationStoragePath or the CLI will write to the file sequelize-meta.json. If you want to keep the information in the database, using sequelize, but want to use a different table, you can change the table name using migrationStorageTableName. Also you can define a different schema for the SequelizeMeta table by providing the migrationStorageTableSchema property.

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

// Use a different storage type. Default: sequelize
"migrationStorage": "json",

// Use a different file name. Default: sequelize-meta.json
"migrationStoragePath": "sequelizeMeta.json",

// Use a different table name. Default: SequelizeMeta
"migrationStorageTableName": "sequelize_meta",

// Use a different schema for the SequelizeMeta table
"migrationStorageTableSchema": "custom_schema"
}
}

注意:不建议使用 none 存储作为迁移存储。如果你决定使用它,请注意没有迁移已运行或未运行的记录的影响。

¥Note: The none storage is not recommended as a migration storage. If you decide to use it, be aware of the implications of having no record of what migrations did or didn't run.

种子储存

¥Seed Storage

默认情况下,CLI 不会保存任何执行的种子。如果你选择更改此行为(!),则可以在配置文件中使用 seederStorage 来更改存储类型。如果选择 json,则可以使用 seederStoragePath 指定文件的路径,否则 CLI 将写入文件 sequelize-data.json。如果要保留数据库中的信息,使用 sequelize,可以使用 seederStorageTableName 指定表名,否则默认为 SequelizeData

¥By default the CLI will not save any seed that is executed. If you choose to change this behavior (!), you can use seederStorage in the configuration file to change the storage type. If you choose json, you can specify the path of the file using seederStoragePath or the CLI will write to the file sequelize-data.json. If you want to keep the information in the database, using sequelize, you can specify the table name using seederStorageTableName, or it will default to SequelizeData.

{
"development": {
"username": "root",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "mysql",
// Use a different storage. Default: none
"seederStorage": "json",
// Use a different file name. Default: sequelize-data.json
"seederStoragePath": "sequelizeData.json",
// Use a different table name. Default: SequelizeData
"seederStorageTableName": "sequelize_data"
}
}

配置连接字符串

¥Configuration Connection String

作为使用定义数据库的配置文件的 --config 选项的替代方案,你可以使用 --url 选项传入连接字符串。例如:

¥As an alternative to the --config option with configuration files defining your database, you can use the --url option to pass in a connection string. For example:

npx sequelize-cli db:migrate --url 'mysql://root:password@mysql_host.com/database_name'

如果将 package.json 脚本与 npm 一起使用,请确保在使用标志时在命令中使用额外的 --。例如:

¥If utilizing package.json scripts with npm, make sure to use the extra -- in your command when using flags. For example:

// package.json

...
"scripts": {
"migrate:up": "npx sequelize-cli db:migrate",
"migrate:undo": "npx sequelize-cli db:migrate:undo"
},
...

使用命令如下:npm run migrate:up -- --url <url>

¥Use the command like so: npm run migrate:up -- --url <url>

程序化使用

¥Programmatic usage

Sequelize 有一个名为 umzug 的姊妹库,用于以编程方式处理迁移任务的执行和日志记录。

¥Sequelize has a sister library called umzug for programmatically handling execution and logging of migration tasks.