#5150·knex

`Migrator.rollback` may throw

Author: spencerwilsonCreated Apr 29, 2022Updated Sep 23, 2026
LabelsNG_bug

Environment

Knex version: 0.95.14, but the same thing looks possible on 1525f0f9167828125d103f8380cdf7c5c7ce2c51 Database + version: Postgres 12.8 OS: macOS 12.3

Bug

  1. Explain what kind of behaviour you are getting and how you think it should do
typescript
await knex.migrate.rollback().finally(() => console.log('hello'));

Expectation: "hello" is always printed. This depends on rollback always returning a promise. I expect rollback to surely return a promise because

Actual: rollback may throw.

  1. Error message

An example error thrown by Migrator.rollback:

Error: Unable to acquire a connection
        at Client_PG.acquireConnection (/Users/ssw/code/api/node_modules/knex/lib/client.js:296:13)
        at Runner.ensureConnection (/Users/ssw/code/api/node_modules/knex/lib/execution/runner.js:259:46)
        at Runner.run (/Users/ssw/code/api/node_modules/knex/lib/execution/runner.js:30:30)
        at SchemaBuilder.Target.then (/Users/ssw/code/api/node_modules/knex/lib/builder-interface-augmenter.js:24:43)
        at ensureTable (/Users/ssw/code/api/node_modules/knex/lib/migrations/migrate/table-creator.js:12:6)
        at listCompleted (/Users/ssw/code/api/node_modules/knex/lib/migrations/migrate/migration-list-resolver.js:12:9)
        at Object.listAllAndCompleted (/Users/ssw/code/api/node_modules/knex/lib/migrations/migrate/migration-list-resolver.js:28:5)
        at /Users/ssw/code/api/node_modules/knex/lib/migrations/migrate/Migrator.js:177:10
        at new Promise (<anonymous>)
        at Migrator.rollback (/Users/ssw/code/api/node_modules/knex/lib/migrations/migrate/Migrator.js:166:12)
  1. Reduced test code, for example in https://npm.runkit.com/knex or if it needs real database connection to MySQL or PostgreSQL, then single file example which initializes needed data and demonstrates the problem.

Don't have time right now to make this, sorry. Hopefully the above description suffices.

Possible fixes

  • Make every method on Migrator return a promise, to be consistent with the typings
  • Make rollback return a promise, to be consistent with its current typing. A small change that would accomplish this would be to add the async keyword to the method definition, making it an async method.
  • Update the docs + typings to reflect the implementation: that rollback and some other methods may throw. Document the conditions in which they throw.