"Invalid APP_KEY" error after a failed AdonisJS 5 upgrade — safe recovery path for app and migrations
0 reputation · 17 May 2025, 07:18 UTC
0 reputation · 17 May 2025, 07:18 UTC
While upgrading an AdonisJS 5 application, the process can fail partway through and leave the app unable to boot. One documented failure condition is the Invalid APP_KEY error thrown on startup when the .env key has an unexpected length or format, and a separate symptom is Lucid reporting that the database schema is not up to date because migrations were applied only partially.
The uncertainty is around recovery order and scope. The framework does not appear to roll back migrations automatically when an upgrade fails, so the schema can sit between two versions while the application refuses to start. It is also unclear whether fixing the APP_KEY first and then re-running migrations is sufficient, or whether partially applied migration files must be reverted manually before re-running node ace migration:run.
Assume AdonisJS 5.x with Lucid and a relational database, verified via node ace migration:list to inspect the current migration state.
APP_KEY, then inspect and re-run migrations, or revert first?31775 reputation · 17 May 2025, 11:39 UTC
When an upgrade stops midway, the first thing that blocks the application is the Invalid APP_KEY error. The framework cannot decrypt any encrypted values, so the server never reaches the migration layer. Therefore the safe order is:
APP_KEY in .env (or config/app.ts if you generated a key with node ace generate:key).node ace serve should run without the key error.node ace migration:list or node ace migration:status.Attempting to rollback before the app can start is risky because the CLI commands rely on a working database connection that is established during boot. If the APP_KEY is invalid, the database connection may still succeed, but any encrypted configuration values (e.g., JWT secret) will be unusable. Fixing the key first guarantees that all subsequent commands run in a consistent environment.
AdonisJS stores the migration history in the migrations table. Each applied migration is recorded with a batch number. A partially applied batch appears as a set of rows that were inserted but whose corresponding migration file never finished executing.
To locate it:
# List all migrations with their batch numbers
node ace migration:list
Look for a batch that contains fewer rows than the number of migration files that share the same timestamp directory. The batch column will show the same number for all rows in that group.
Once identified, roll back that specific batch:
# Replace with the number you found above
node ace migration:rollback --batch=
This command will:
down method for each migration in that batch.migrations table.After the rollback, verify that the migrations table no longer contains the batch rows:
node ace migration:list
Finally, re‑run the pending migrations:
node ace migration:run
All remaining migration files will be applied in the correct order.
AdonisJS 5 does not ship a pre‑startup consistency validator that compares the migration table against the filesystem. The framework only reports migration status via the CLI. Therefore, before restarting the server you should manually confirm that the migration table matches the set of migration files:
node ace migration:list and ensure that every file in database/migrations appears as either Pending or Applied.migrations table, or use node ace migration:rollback --force to correct the state.Once the migration state is verified, the application can be started normally without encountering the Invalid APP_KEY error.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.