Database Migrations

本文介绍了在数据库模型发生变化时如何使用自定义的Sequelize迁移策略进行数据库迁移。内容包括创建迁移文件的命名规范和结构,以及应用迁移的过程。特别强调了在写迁移时要注意确保可逆性,并谨慎处理添加约束的情况,以防导致平台启动失败或需要手动干预。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

It is important that any changes made to the database models include migrations that can modify the database state from one state to another.

Whilst we use Sequelize as our ORM layer, we do not use the migration tooling it provides - we have our own.

Creating migrations

Filename

A migration is provided as a JavaScript in the directory forge/db/migrations. Its file name must follow the pattern:

YYYYMMDD-nn-description.js
  • YYYYMMDD - the date the migration is added
  • nn - a two digit number
  • description - a name for the migration

For example 20220204-01-add-billing.js.

This ensures the migrations have a natural order to be applied. The nn part of the name allows multiple migrations to be added on the same day, but kept in the right order.

Structure

The migration code should use the following layout:

module.exports = {
    up: async (context) => {
        // Apply the migration
    },
    down: async (context) => {
        // Remove the migration
    }
}

The up function applies the migration. This can be to create new tables, add columns to existing ones - whatever is needed.

The down function reverses the migration. It should restore the database back to how it was prior to the migration.

The context argument is an instance of Sequelize.QueryInterface that can be used to perform operations on the database.

Applying migrations

Migrations are applied automatically on start of the FlowForge application. Down migrations are not yet supported.

Considerations when writing migrations

Whilst migrations give us the ability to make changes to the database, they must be used with great care. A failing migration will prevent the platform from starting and may require manual intervention. Everything should be done to avoid that from happening.

There are certain types of migration that need particular guidance and care over.

Adding constraints

If a migration is adding a new constraint to an existing table, you need to consider very carefully what impact that could have on an existing system with real data.

For example, adding a new 'unique' constraint where you cannot guarantee that constraint hasn't already been broken. What strategy will you use to guard against that or to help recover from it? What additional testing is needed of the migration to verify its behaviour in those situations.

The preferred method to add a new unique constraint is by adding a new index to the database. This is because sqlite doesn't provide a way to alter columns that doesn't involve dropping the whole table and triggering any cascade triggers.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值