如何在 LoopBack 4 中进行数据库迁移

How to do database migrations in LoopBack 4

当应用程序处于生产阶段并且需要对表进行更改时,我想做 automigrate 是不可能的,因为它会删除正在更改的表的所有数据。做 autoupdate 是合适的,但我担心可伸缩性。依赖 autoupdate 生产阶段的产品安全吗? Rails 类迁移的一个优点是保留更改记录,以确保数据库的每个实例或环境都在完全相同的模式中。在 LoopBack 中是否有任何完善的方法来实现这一点?

不仅因为这个,而且如果需要在列更改期间对数据进行规范化,那么在 LoopBack 中将如何完成?我没有看到对这种迁移的支持。

来自 LoopBack 团队的问候

Doing autoupdate would be appropriate, but I'm concerned about scalability. Is it safe to rely on autoupdate on a product in the production stage? One advantage of Rails-like migrations is keeping a record of changes to ensure that every instance or environment of the database will be in the exact same schema. Is there any well-developed way to achieve this in LoopBack?

你是对的,运行对实时生产数据进行自动魔术数据库迁移(如 autoupdate 提供的那样)是有风险的。我们正在 GitHub 问题 loopback-next#487, feel free to join the effort! One of the community members mentioned a 3rd-party package loopback4-migration 中讨论一个更强大的框架,你可能想看看它。

Not only because of this but if it's needed to normalize data during a column change, how would it be done in LoopBack? I didn't see support for this kind of migration.

恐怕当前的 automigrate/autoupdate 设计不支持将自定义数据转换作为数据库迁移的一部分。一个可能的选择是在执行自动迁移之前或之后覆盖 app.migrateSchema 到 运行 附加数据库命令。

class MyApplication extends RepositoryMixin(RestApplication) {
 async migrateSchema(options: SchemaMigrationOptions = {}): Promise<void> {
   // add code to normalize data before column definitions are changed
   await super.migrateSchema(options);
   // add code to normalize data after column definitions were changed
  }
}