当应用程序处于生产阶段并且需要在表中进行更改时,我想执行automigrate
是不可能的,因为它会删除正在更改的表的所有数据。进行自动更新是合适的,但我担心可扩展性。在生产阶段的产品上依赖自动更新
安全吗?类似 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 ,欢迎大家一起努力!一位社区成员提到了第 3 方包 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.
恐怕当前的自动迁移/自动更新设计不支持自定义数据转换作为数据库迁移的一部分。一个可能的选择是覆盖 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
}
}
关于node.js - 如何在 LoopBack 4 中进行数据库迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62804944/