Does Knex.js provide a mechanism to handle partial DDL failures in non-transactional dialects?
0 reputation · 14 Jan 2025, 22:40 UTC
0 reputation · 14 Jan 2025, 22:40 UTC
Knex.js manages migration state via a migrations table and a lock mechanism to prevent concurrent execution. For dialects supporting transactional DDL, such as PostgreSQL, Knex wraps migrations in transactions to ensure atomicity. However, MySQL and MariaDB do not support rolling back DDL statements.
When a migration fails on a non-transactional dialect, the migration is not recorded as applied in the database. Despite this, any DDL statements executed prior to the failure remain committed to the schema. This creates a discrepancy between the actual database state and the migration history recorded by Knex.
Because knex migrate:rollback relies on the down method of the last successful batch, it may not target the partial changes left by a failed migration attempt.
30275 reputation · 15 Jan 2025, 04:14 UTC
No, Knex.js does not provide a built‑in mechanism to detect or repair partially applied DDL when a migration fails on a dialect that does not support transactional DDL (e.g., MySQL, MariaDB).
knex_migrations table and uses a lock table to prevent concurrent runs.migrate:rollback command only reverses the last recorded batch; it never targets objects that were created before a failed migration attempt.On MySQL/MariaDB each DDL statement (CREATE, ALTER, DROP) is auto‑committed immediately. If a migration throws an error after some statements have run, those statements remain in the schema while the migration is never marked as applied in knex_migrations. A subsequent migrate:latest therefore runs the same up against an already‑mutated schema, which can cause duplicate‑object errors or leave the schema in a half‑applied state.
npx knex migrate:list or query SELECT * FROM knex_migrations;.SHOW TABLES;, SHOW COLUMNS FROM your_table; or query information_schema).up method, drop or tolerate any partially created objects, or adjust the migration to be idempotent before re‑running.knex.schema.hasTable, column addition with hasColumn, and use raw SQL like CREATE TABLE IF NOT EXISTS where supported.npx knex migrate:latest and confirm the migration appears as a new batch in knex_migrations.To know exactly which object to drop or keep, you need to identify the specific DDL statement that failed. If the error log shows the failing statement, you can target only that object; otherwise you must audit every object the migration attempts to create before re‑running.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.