"use strict";
/*
 * @adonisjs/lucid
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestsMigrator = void 0;
/**
 * Migrator class to be used for testing.
 */
class TestsMigrator {
    constructor(ace, connectionName) {
        Object.defineProperty(this, "ace", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: ace
        });
        Object.defineProperty(this, "connectionName", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: connectionName
        });
    }
    async runCommand(commandName) {
        const args = ['--compact-output'];
        if (this.connectionName) {
            args.push(`--connection=${this.connectionName}`);
        }
        const command = await this.ace.exec(commandName, args);
        if (command.exitCode) {
            if (command.error) {
                throw command.error;
            }
            else {
                throw new Error(`"${commandName}" failed`);
            }
        }
    }
    async run() {
        await this.runCommand('migration:run');
        return () => this.runCommand('migration:rollback');
    }
}
exports.TestsMigrator = TestsMigrator;
