"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);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const standalone_1 = require("@adonisjs/core/build/standalone");
const Base_1 = __importDefault(require("./Base"));
/**
 * The command is meant to migrate the database by executing migrations
 * in `up` direction.
 */
class Migrate extends Base_1.default {
    constructor() {
        super(...arguments);
        Object.defineProperty(this, "migrator", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        /**
         * Custom connection for running migrations.
         */
        Object.defineProperty(this, "connection", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        /**
         * Force run migrations 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
        });
        /**
         * Display migrations result in one compact single-line output
         */
        Object.defineProperty(this, "compactOutput", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: false
        });
        /**
         * Disable advisory locks
         */
        Object.defineProperty(this, "disableLocks", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
    }
    /**
     * Instantiating the migrator instance
     */
    instantiateMigrator() {
        const db = this.application.container.use('Adonis/Lucid/Database');
        const Migrator = this.application.container.resolveBinding('Adonis/Lucid/Migrator');
        this.migrator = new Migrator(db, this.application, {
            direction: 'up',
            connectionName: this.connection,
            dryRun: this.dryRun,
            disableLocks: this.disableLocks,
        });
    }
    /**
     * Run as a subcommand. Never close database connections or exit
     * process inside this method
     */
    async runAsSubCommand() {
        const db = this.application.container.use('Adonis/Lucid/Database');
        this.connection = this.connection || db.primaryConnectionName;
        /**
         * Continue with migrations when not in prod or force flag
         * is passed
         */
        let continueMigrations = !this.application.inProduction || this.force;
        if (!continueMigrations) {
            continueMigrations = await this.takeProductionConstent();
        }
        /**
         * Do not continue when in prod and the prompt was cancelled
         */
        if (!continueMigrations) {
            return;
        }
        /**
         * Invalid database connection
         */
        if (!db.manager.has(this.connection)) {
            this.printNotAValidConnection(this.connection);
            this.exitCode = 1;
            return;
        }
        this.instantiateMigrator();
        await this.runMigrations(this.migrator, this.connection);
    }
    /**
     * Branching out, so that if required we can implement
     * "runAsMain" separately from "runAsSubCommand".
     *
     * For now, they both are the same
     */
    async runAsMain() {
        await this.runAsSubCommand();
    }
    /**
     * Handle command
     */
    async run() {
        if (this.isMain) {
            await this.runAsMain();
        }
        else {
            await this.runAsSubCommand();
        }
    }
    /**
     * Lifecycle method invoked by ace after the "run"
     * method.
     */
    async completed() {
        if (this.migrator && this.isMain) {
            await this.migrator.close();
        }
    }
}
Object.defineProperty(Migrate, "commandName", {
    enumerable: true,
    configurable: true,
    writable: true,
    value: 'migration:run'
});
Object.defineProperty(Migrate, "description", {
    enumerable: true,
    configurable: true,
    writable: true,
    value: 'Migrate database by running pending migrations'
});
Object.defineProperty(Migrate, "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)
], Migrate.prototype, "connection", void 0);
__decorate([
    standalone_1.flags.boolean({ description: 'Explicitly force to run migrations in production' }),
    __metadata("design:type", Boolean)
], Migrate.prototype, "force", void 0);
__decorate([
    standalone_1.flags.boolean({ description: 'Do not run actual queries. Instead view the SQL output' }),
    __metadata("design:type", Boolean)
], Migrate.prototype, "dryRun", void 0);
__decorate([
    standalone_1.flags.boolean({ description: 'A compact single-line output' }),
    __metadata("design:type", Boolean)
], Migrate.prototype, "compactOutput", void 0);
__decorate([
    standalone_1.flags.boolean({ description: 'Disable locks acquired to run migrations safely' }),
    __metadata("design:type", Boolean)
], Migrate.prototype, "disableLocks", void 0);
exports.default = Migrate;
