mirror of
https://github.com/arthur-pbty/portfolio2023.git
synced 2026-06-04 07:46:21 +02:00
168 lines
5.5 KiB
JavaScript
168 lines
5.5 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");
|
|
class DbTruncate extends standalone_1.BaseCommand {
|
|
constructor() {
|
|
super(...arguments);
|
|
/**
|
|
* Choose a custom pre-defined connection. Otherwise, we use the
|
|
* default connection
|
|
*/
|
|
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
|
|
});
|
|
}
|
|
/**
|
|
* Not a valid connection
|
|
*/
|
|
printNotAValidConnection(connection) {
|
|
this.logger.error(`"${connection}" is not a valid connection name. Double check "config/database" file`);
|
|
}
|
|
/**
|
|
* Prompts to take consent when truncating the database in production
|
|
*/
|
|
async takeProductionConstent() {
|
|
/**
|
|
* Do not prompt when CLI is not interactive
|
|
*/
|
|
if (!this.isInteractive) {
|
|
return false;
|
|
}
|
|
const question = 'You are in production environment. Want to continue truncating the database?';
|
|
try {
|
|
return await this.prompt.confirm(question);
|
|
}
|
|
catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
/**
|
|
* Truncate all tables except adonis migrations table
|
|
*/
|
|
async performTruncate(client) {
|
|
let tables = await client.getAllTables(['public']);
|
|
tables = tables.filter((table) => !['adonis_schema', 'adonis_schema_versions'].includes(table));
|
|
await Promise.all(tables.map((table) => client.truncate(table, true)));
|
|
this.logger.success('Truncated tables successfully');
|
|
}
|
|
/**
|
|
* 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;
|
|
const connection = db.connection(this.connection || db.primaryConnectionName);
|
|
/**
|
|
* Continue with clearing the database when not in production
|
|
* or force flag is passed
|
|
*/
|
|
let continueTruncate = !this.application.inProduction || this.force;
|
|
if (!continueTruncate) {
|
|
continueTruncate = await this.takeProductionConstent();
|
|
}
|
|
/**
|
|
* Do not continue when in prod and the prompt was cancelled
|
|
*/
|
|
if (!continueTruncate) {
|
|
return;
|
|
}
|
|
/**
|
|
* Invalid database connection
|
|
*/
|
|
if (!db.manager.has(this.connection)) {
|
|
this.printNotAValidConnection(this.connection);
|
|
this.exitCode = 1;
|
|
return;
|
|
}
|
|
await this.performTruncate(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.isMain) {
|
|
await this.application.container.use('Adonis/Lucid/Database').manager.closeAll(true);
|
|
}
|
|
}
|
|
}
|
|
Object.defineProperty(DbTruncate, "commandName", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: 'db:truncate'
|
|
});
|
|
Object.defineProperty(DbTruncate, "description", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: 'Truncate all tables in database'
|
|
});
|
|
Object.defineProperty(DbTruncate, "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)
|
|
], DbTruncate.prototype, "connection", void 0);
|
|
__decorate([
|
|
standalone_1.flags.boolean({ description: 'Explicitly force command to run in production' }),
|
|
__metadata("design:type", Boolean)
|
|
], DbTruncate.prototype, "force", void 0);
|
|
exports.default = DbTruncate;
|