knex migration fail, but doesnt revert changes

六眼飞鱼酱① 提交于 2021-02-11 13:54:07

问题


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').
     })
}

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?


回答1:


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

For example with mysql if you do following:

* start transaction
* create table 1
* rollback

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


来源:https://stackoverflow.com/questions/59425666/knex-migration-fail-but-doesnt-revert-changes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!