Is there a way to catch migration failure in a script?


Hi everyone, I’m new to adonis. I’ve been searching about this question for a while but had no luck finding answers. I’m wondering if I can get any help here.

The command adonis migrate:refresh always returns a code 0 no matter whether it encounters an SQL error or not. I wonder if there’s a way to catch this error other than testing if there’s the word ‘Error’ in the output?

Here’s some background information:
I have a script to run a database migrate:refresh and seeding together on my server automatically. But sometimes, if someone manually modified my database schema(e.g. deleted a table), the migration:refresh will encounter SQL error and rollback all the operations. But since I couldn’t detect this (migration:refresh exited with code 0), the seeding part of my script will still run, which leads to duplicated seeding in my database.

I have a sample bash script to detect the SQL failure but I don’t think it is a good solution.

#/usr/bin/env bash

adonis migration:refresh | grep 'Error'

if [ $? -eq 1 ]
    echo "Success, can start seeding"
    echo "Failed, do not run seeding"

I’ve also set up a repo for reproducing the migration SQL error by modifying one of the migration files in the sample boilerplate. To use it, simple clone it and run the followings:

npm install
adonis migration:run
adonis migration:refresh

TL;DR: Any good way to detect the SQL error when using adonis migration:refresh in a script other than “grep” the word “Error” in the command output?


migration:refresh is just a wrapper for migration:reset & migration:run.

migration:run actually does process.exit(1) on errors while migration:reset does not. I guess you could register your own commands for reset and refresh and add that yourself.


Hi thanks for your reply! But how can I write my own command for reset? Is there a documentation about it? I know there’s ace for creating custom commands but how can I perform a database migration inside an ace command (for example, run a single migration and return exit(1) when it goes wrong?)


You can see the default commands at:

“Running” a migration already does that: see MigrationRun

You can pretty much copy & paste the default commands to your custom ace commands (change signature, description and the path to BaseMigration…) and do whatever you need in the catch block.