Files
2023-11-24 22:35:41 +01:00

202 lines
6.0 KiB
JavaScript

"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
});
/**
* Run seeders
*/
Object.defineProperty(this, "seed", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Drop all views in database
*/
Object.defineProperty(this, "dropViews", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Drop all types in database
*/
Object.defineProperty(this, "dropTypes", {
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.disableLocks) {
args.push('--disable-locks');
}
return args;
}
/**
* Converting command properties to db:wipe arguments
*/
getWipeArgs() {
const args = this.getArgs();
if (this.dropTypes) {
args.push('--drop-types');
}
if (this.dropViews) {
args.push('--drop-views');
}
return args;
}
/**
* Wipe the database
*/
async runDbWipe() {
const dbWipe = await this.kernel.exec('db:wipe', this.getWipeArgs());
this.exitCode = dbWipe.exitCode;
this.error = dbWipe.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.runDbWipe();
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:fresh'
});
Object.defineProperty(Refresh, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Drop all tables and re-migrate the 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: 'Run seeders' }),
__metadata("design:type", Boolean)
], Refresh.prototype, "seed", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Drop all views' }),
__metadata("design:type", Boolean)
], Refresh.prototype, "dropViews", void 0);
__decorate([
standalone_1.flags.boolean({ description: 'Drop all custom types (Postgres only)' }),
__metadata("design:type", Boolean)
], Refresh.prototype, "dropTypes", 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;