"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.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const standalone_1 = require("@adonisjs/core/build/standalone");
/**
 * This command reset the database by rolling back to batch 0 and then
 * re-run all migrations.
 */
class Refresh extends standalone_1.BaseCommand {
    constructor() {
        super(...arguments);
        /**
         * Custom connection for running migrations.
         */
        Object.defineProperty(this, "connection", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        /**
         * Force command execution in production
         */
        Object.defineProperty(this, "force", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        /**
         * Perform dry run
         */
        Object.defineProperty(this, "dryRun", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        /**
         * Run seeders
         */
        Object.defineProperty(this, "seed", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        /**
         * Disable advisory locks
         */
        Object.defineProperty(this, "disableLocks", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
    }
    /**
     * Converting command properties to arguments
     */
    getArgs() {
        const args = [];
        if (this.force) {
            args.push('--force');
        }
        if (this.connection) {
            args.push(`--connection="${this.connection}"`);
        }
        if (this.dryRun) {
            args.push('--dry-run');
        }
        if (this.disableLocks) {
            args.push('--disable-locks');
        }
        return args;
    }
    /**
     * Reset all migrations
     */
    async resetMigrations() {
        const reset = await this.kernel.exec('migration:reset', this.getArgs());
        this.exitCode = reset.exitCode;
        this.error = reset.error;
    }
    /**
     * Run migrations
     */
    async runMigrations() {
        const migrate = await this.kernel.exec('migration:run', this.getArgs());
        this.exitCode = migrate.exitCode;
        this.error = migrate.error;
    }
    /**
     * Run seeders
     */
    async runDbSeed() {
        const args = [];
        if (this.connection) {
            args.push(`--connection="${this.connection}"`);
        }
        const dbSeed = await this.kernel.exec('db:seed', args);
        this.exitCode = dbSeed.exitCode;
        this.error = dbSeed.error;
    }
    /**
     * Handle command
     */
    async run() {
        await this.resetMigrations();
        if (this.exitCode) {
            return;
        }
        await this.runMigrations();
        if (this.exitCode) {
            return;
        }
        if (this.seed) {
            await this.runDbSeed();
        }
    }
}
Object.defineProperty(Refresh, "commandName", {
    enumerable: true,
    configurable: true,
    writable: true,
    value: 'migration:refresh'
});
Object.defineProperty(Refresh, "description", {
    enumerable: true,
    configurable: true,
    writable: true,
    value: 'Rollback and migrate database'
});
Object.defineProperty(Refresh, "settings", {
    enumerable: true,
    configurable: true,
    writable: true,
    value: {
        loadApp: true,
    }
});
__decorate([
    standalone_1.flags.string({ description: 'Define a custom database connection', alias: 'c' }),
    __metadata("design:type", String)
], Refresh.prototype, "connection", void 0);
__decorate([
    standalone_1.flags.boolean({ description: 'Explicitly force command to run in production' }),
    __metadata("design:type", Boolean)
], Refresh.prototype, "force", void 0);
__decorate([
    standalone_1.flags.boolean({ description: 'Do not run actual queries. Instead view the SQL output' }),
    __metadata("design:type", Boolean)
], Refresh.prototype, "dryRun", void 0);
__decorate([
    standalone_1.flags.boolean({ description: 'Run seeders' }),
    __metadata("design:type", Boolean)
], Refresh.prototype, "seed", void 0);
__decorate([
    standalone_1.flags.boolean({ description: 'Disable locks acquired to run migrations safely' }),
    __metadata("design:type", Boolean)
], Refresh.prototype, "disableLocks", void 0);
exports.default = Refresh;
