UNPKG

5.64 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/lucid
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
11 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
12 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
13 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;
14 return c > 3 && r && Object.defineProperty(target, key, r), r;
15};
16var __metadata = (this && this.__metadata) || function (k, v) {
17 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
18};
19Object.defineProperty(exports, "__esModule", { value: true });
20const standalone_1 = require("@adonisjs/core/build/standalone");
21class DbTruncate extends standalone_1.BaseCommand {
22 constructor() {
23 super(...arguments);
24 /**
25 * Choose a custom pre-defined connection. Otherwise, we use the
26 * default connection
27 */
28 Object.defineProperty(this, "connection", {
29 enumerable: true,
30 configurable: true,
31 writable: true,
32 value: void 0
33 });
34 /**
35 * Force command execution in production
36 */
37 Object.defineProperty(this, "force", {
38 enumerable: true,
39 configurable: true,
40 writable: true,
41 value: void 0
42 });
43 }
44 /**
45 * Not a valid connection
46 */
47 printNotAValidConnection(connection) {
48 this.logger.error(`"${connection}" is not a valid connection name. Double check "config/database" file`);
49 }
50 /**
51 * Prompts to take consent when truncating the database in production
52 */
53 async takeProductionConstent() {
54 /**
55 * Do not prompt when CLI is not interactive
56 */
57 if (!this.isInteractive) {
58 return false;
59 }
60 const question = 'You are in production environment. Want to continue truncating the database?';
61 try {
62 return await this.prompt.confirm(question);
63 }
64 catch (error) {
65 return false;
66 }
67 }
68 /**
69 * Truncate all tables except adonis migrations table
70 */
71 async performTruncate(client) {
72 let tables = await client.getAllTables(['public']);
73 tables = tables.filter((table) => !['adonis_schema', 'adonis_schema_versions'].includes(table));
74 await Promise.all(tables.map((table) => client.truncate(table, true)));
75 this.logger.success('Truncated tables successfully');
76 }
77 /**
78 * Run as a subcommand. Never close database connections or exit
79 * process inside this method
80 */
81 async runAsSubCommand() {
82 const db = this.application.container.use('Adonis/Lucid/Database');
83 this.connection = this.connection || db.primaryConnectionName;
84 const connection = db.connection(this.connection || db.primaryConnectionName);
85 /**
86 * Continue with clearing the database when not in production
87 * or force flag is passed
88 */
89 let continueTruncate = !this.application.inProduction || this.force;
90 if (!continueTruncate) {
91 continueTruncate = await this.takeProductionConstent();
92 }
93 /**
94 * Do not continue when in prod and the prompt was cancelled
95 */
96 if (!continueTruncate) {
97 return;
98 }
99 /**
100 * Invalid database connection
101 */
102 if (!db.manager.has(this.connection)) {
103 this.printNotAValidConnection(this.connection);
104 this.exitCode = 1;
105 return;
106 }
107 await this.performTruncate(connection);
108 }
109 /**
110 * Branching out, so that if required we can implement
111 * "runAsMain" separately from "runAsSubCommand".
112 *
113 * For now, they both are the same
114 */
115 async runAsMain() {
116 await this.runAsSubCommand();
117 }
118 /**
119 * Handle command
120 */
121 async run() {
122 if (this.isMain) {
123 await this.runAsMain();
124 }
125 else {
126 await this.runAsSubCommand();
127 }
128 }
129 /**
130 * Lifecycle method invoked by ace after the "run"
131 * method.
132 */
133 async completed() {
134 if (this.isMain) {
135 await this.application.container.use('Adonis/Lucid/Database').manager.closeAll(true);
136 }
137 }
138}
139Object.defineProperty(DbTruncate, "commandName", {
140 enumerable: true,
141 configurable: true,
142 writable: true,
143 value: 'db:truncate'
144});
145Object.defineProperty(DbTruncate, "description", {
146 enumerable: true,
147 configurable: true,
148 writable: true,
149 value: 'Truncate all tables in database'
150});
151Object.defineProperty(DbTruncate, "settings", {
152 enumerable: true,
153 configurable: true,
154 writable: true,
155 value: {
156 loadApp: true,
157 }
158});
159__decorate([
160 standalone_1.flags.string({ description: 'Define a custom database connection', alias: 'c' }),
161 __metadata("design:type", String)
162], DbTruncate.prototype, "connection", void 0);
163__decorate([
164 standalone_1.flags.boolean({ description: 'Explicitly force command to run in production' }),
165 __metadata("design:type", Boolean)
166], DbTruncate.prototype, "force", void 0);
167exports.default = DbTruncate;