FeathersJS Knex migration down function safety after schema changes
0 reputation · 26 Oct 2021, 14:08 UTC
0 reputation · 26 Oct 2021, 14:08 UTC
To guarantee that rolling back a FeathersJS migration will not leave orphaned columns, indexes, or corrupted data when a schema change is applied.
Knex migrations require manual down functions; FeathersJS does not enforce any checks that the down logic fully reverses the up logic. This can lead to incomplete rollbacks, especially after data‑transforming operations or type changes. The current documentation lacks a built‑in helper to validate that a down script restores the original schema state and preserves unrelated data.
down migration restores the exact pre‑migration schema and data state?up migration when performing a rollback?down function that passes a safety audit before deployment?FeathersJS and Knex do not provide native mechanisms to automatically verify that a down migration perfectly restores a database to its pre-migration state. Because Knex treats the down function as an arbitrary JavaScript block, the responsibility for schema and data integrity during a rollback rests entirely with the developer.
To mitigate the risk of orphaned columns or corrupted data, you can implement a Snapshot-and-Compare pattern within your CI/CD pipeline rather than inside the application code. This ensures that the production environment is not used as a testing ground for rollback logic.
The recommended workflow for verifying a migration is as follows:
knex migrate:latest.knex migrate:rollback.knex.schema.hasColumn and knex.schema.hasTable to compare the final state against the baseline.When an up migration transforms data (e.g., splitting a full_name column into first_name and last_name), a simple dropColumn in the down function will result in permanent data loss. To prevent this, use a Temporary Backup Table pattern:
up: Create a temporary table (e.g., backup_users_v1) to store the original column values before transformation.down: Join the main table with the backup table to restore the original values before dropping the new columns and deleting the backup table.There is no FeathersJS configuration flag to enforce the presence or validity of a down function. To enforce this, you must implement a linting rule or a pre-deployment script that scans migration files for the presence of an exported down function. If the function is missing or empty, the build should fail.
To manually verify that a rollback leaves no orphaned artifacts in a development environment, you can query the information schema (example for PostgreSQL):
-- Check for columns that should have been removed
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'your_table_name'
AND column_name = 'orphaned_column_name';
Assumption: This guidance assumes you are using a relational database supported by Knex (e.g., PostgreSQL, MySQL, SQLite) and that migrations are managed via the Knex CLI.
Diagnostic Detail Needed: Are you performing destructive type changes (e.g., converting a String to an Integer) that require casting, as this significantly changes the backup strategy required for the down function?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.