mirror of
https://github.com/arthur-pbty/portfolio2023.git
synced 2026-06-04 15:56:24 +02:00
220 lines
7.0 KiB
JavaScript
220 lines
7.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");
|
|
class DbWipe 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
|
|
});
|
|
/**
|
|
* 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
|
|
});
|
|
/**
|
|
* 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 wiping 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 wiping the database?';
|
|
try {
|
|
return await this.prompt.confirm(question);
|
|
}
|
|
catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
/**
|
|
* Drop all views (if asked for and supported)
|
|
*/
|
|
async performDropViews(client) {
|
|
if (!this.dropViews) {
|
|
return;
|
|
}
|
|
if (!client.dialect.supportsViews) {
|
|
this.logger.warning(`Dropping views is not supported by "${client.dialect.name}"`);
|
|
}
|
|
await client.dropAllViews();
|
|
this.logger.success('Dropped views successfully');
|
|
}
|
|
/**
|
|
* Drop all tables
|
|
*/
|
|
async performDropTables(client) {
|
|
await client.dropAllTables();
|
|
this.logger.success('Dropped tables successfully');
|
|
}
|
|
/**
|
|
* Drop all types (if asked for and supported)
|
|
*/
|
|
async performDropTypes(client) {
|
|
if (!this.dropTypes) {
|
|
return;
|
|
}
|
|
if (!client.dialect.supportsTypes) {
|
|
this.logger.warning(`Dropping types is not supported by "${client.dialect.name}"`);
|
|
}
|
|
await client.dropAllTypes();
|
|
this.logger.success('Dropped types 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 continueWipe = !this.application.inProduction || this.force;
|
|
if (!continueWipe) {
|
|
continueWipe = await this.takeProductionConstent();
|
|
}
|
|
/**
|
|
* Do not continue when in prod and the prompt was cancelled
|
|
*/
|
|
if (!continueWipe) {
|
|
return;
|
|
}
|
|
/**
|
|
* Invalid database connection
|
|
*/
|
|
if (!db.manager.has(this.connection)) {
|
|
this.printNotAValidConnection(this.connection);
|
|
this.exitCode = 1;
|
|
return;
|
|
}
|
|
await this.performDropViews(connection);
|
|
await this.performDropTables(connection);
|
|
await this.performDropTypes(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(DbWipe, "commandName", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: 'db:wipe'
|
|
});
|
|
Object.defineProperty(DbWipe, "description", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: 'Drop all tables, views and types in database'
|
|
});
|
|
Object.defineProperty(DbWipe, "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)
|
|
], DbWipe.prototype, "connection", void 0);
|
|
__decorate([
|
|
standalone_1.flags.boolean({ description: 'Drop all views' }),
|
|
__metadata("design:type", Boolean)
|
|
], DbWipe.prototype, "dropViews", void 0);
|
|
__decorate([
|
|
standalone_1.flags.boolean({ description: 'Drop all custom types (Postgres only)' }),
|
|
__metadata("design:type", Boolean)
|
|
], DbWipe.prototype, "dropTypes", void 0);
|
|
__decorate([
|
|
standalone_1.flags.boolean({ description: 'Explicitly force command to run in production' }),
|
|
__metadata("design:type", Boolean)
|
|
], DbWipe.prototype, "force", void 0);
|
|
exports.default = DbWipe;
|