DEV Community

Matt Kenefick
Matt Kenefick

Posted on

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

Oldest comments (0)