knex迁移失败,但不还原更改 [英] knex migration fail, but doesnt revert changes

查看:73
本文介绍了knex迁移失败,但不还原更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的ts项目中使用knex. 迁移看起来像这样:

I want to use knex in my ts project. migration looks like this:

        export async function up(knex: Knex): Promise<any> {
            return knex.schema.createTable('first_table', (t) => {
            t.integer('first_table_id').primary();
                 }).createTable('second_table', (t) => {
            t.integer('id');
            // simulation sql error, duplicate in this case.
            t.integer('id').
     })
}

迁移已失败.我等待事务回滚以进行所有更改,但是我已经成功创建了first_table.我不明白某事,或者Knex的行为不正确?

Migration already fail. I wait transaction rollback for all changes but i have successfully created first_table. I do not understand something or Knex behaves incorrectly?

推荐答案

当mysql执行DDL查询时,它会进行隐式提交.因此,在创建表事务的每个查询之后,都会自动提交.

When mysql does DDL queries it does implicit commit. So after each query that creates a table transaction is committed automatically.

除了在运行迁移之前进行数据库转储并在迁移失败后进行恢复之外,没有真正简单的方法.

There is no really easy way around it except for taking DB dump before running migration and restoring it if the migration fails.

https://dev.mysql.com/doc /refman/8.0/en/implicit-commit.html

例如,如果执行以下操作,则使用mysql:

For example with mysql if you do following:

* start transaction
* create table 1
* rollback

表1仍将被创建,并且没有任何回滚,因为mysql实际上在内部:

Table 1 will still be created and nothing was rolled back, because internally mysql does actually:

* start transaction
* create table 1
* implicit commit
* implicit start transaction for rollback query 
* rollback of automatically started empty transaction

这篇关于knex迁移失败,但不还原更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆