1

For any reason some migration might fail at some point. I was under the impression that migrations are wrapped in transactions in knex however to my dismay i realised that even though the migration has failed, the tables that were created before the error still persist in the DB.

An example migration would look like this:

exports.up = function (knex) {
    return knex.schema
        .createTable('someTable', function (table) {
            table.increments('id');
            table.string('something').notNullable();
        //throws error here for whatever reason, maybe an invalid default value
        })
        .createTable('another table', function (table) {
            table.increments('id');
            table.string('other').notNullable();
        })

};

exports.down = function (knex) {
    return knex.schema
        .dropTable("chatGroups")
        .dropTable("chatMessages");
};

Even though the error is thrown when creating someTable nothing rolls back and all changes are commited to the DB. Running the migration subsequently is now not possible. Even worse, it is not registered in knex_migrations and i cannot even rollback so i have to delete the table manually.

Am i missing something? I would like things to happen atomically, how can i achieve that?

Thank you so much

Return-1
  • 2,329
  • 3
  • 21
  • 56
  • 2
    Which database are you using? Postgres and maybe mssql should be able to DDL queries inside transaction. IIRC mysql and oracledb does implicit commit when ever schema changing queries are done. – Mikael Lepistö May 14 '20 at 09:24
  • Hey, thanks for the reply, i forgot to add that. Im using the "mysql2" client and just plain ol MySQL – Return-1 May 14 '20 at 11:07
  • 1
    Thanks Mikael, apparently it is indeed a MySQL issue – Return-1 May 14 '20 at 20:30

0 Answers0