问题
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