AdonisJS Refresh Command

mattkenefick

Matt Kenefick

Posted on January 7, 2022

AdonisJS Refresh Command

The following command should exist in commands/DatabaseRefresh.ts and is executed like: node ace db:refresh

It will rollback migrations, run new migrations, then seed the database fresh.

import execa from 'execa';
import { BaseCommand } from '@adonisjs/core/build/standalone';

export default class DatabaseRefresh extends BaseCommand {
    /**
     * Command name is used to run the command
     */
    public static commandName = 'db:refresh';

    /**
     * Command description is displayed in the "help" output
     */
    public static description = 'Rolls back migrations, migrates new, seeds database.';

    /**
     * @return void
     */
    public async run() {
        await execa.node('ace', ['migration:rollback', '--batch=0'], { stdio: 'inherit' });
        await execa.node('ace', ['migration:run'], { stdio: 'inherit' });
        await execa.node('ace', ['db:seed'], { stdio: 'inherit' });
    }
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
mattkenefick
Matt Kenefick

Posted on January 7, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related