UNPKG

7.13 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 DbWipe 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 * Drop all views in database
36 */
37 Object.defineProperty(this, "dropViews", {
38 enumerable: true,
39 configurable: true,
40 writable: true,
41 value: void 0
42 });
43 /**
44 * Drop all types in database
45 */
46 Object.defineProperty(this, "dropTypes", {
47 enumerable: true,
48 configurable: true,
49 writable: true,
50 value: void 0
51 });
52 /**
53 * Force command execution in production
54 */
55 Object.defineProperty(this, "force", {
56 enumerable: true,
57 configurable: true,
58 writable: true,
59 value: void 0
60 });
61 }
62 /**
63 * Not a valid connection
64 */
65 printNotAValidConnection(connection) {
66 this.logger.error(`"${connection}" is not a valid connection name. Double check "config/database" file`);
67 }
68 /**
69 * Prompts to take consent when wiping the database in production
70 */
71 async takeProductionConstent() {
72 /**
73 * Do not prompt when CLI is not interactive
74 */
75 if (!this.isInteractive) {
76 return false;
77 }
78 const question = 'You are in production environment. Want to continue wiping the database?';
79 try {
80 return await this.prompt.confirm(question);
81 }
82 catch (error) {
83 return false;
84 }
85 }
86 /**
87 * Drop all views (if asked for and supported)
88 */
89 async performDropViews(client) {
90 if (!this.dropViews) {
91 return;
92 }
93 if (!client.dialect.supportsViews) {
94 this.logger.warning(`Dropping views is not supported by "${client.dialect.name}"`);
95 }
96 await client.dropAllViews();
97 this.logger.success('Dropped views successfully');
98 }
99 /**
100 * Drop all tables
101 */
102 async performDropTables(client) {
103 await client.dropAllTables();
104 this.logger.success('Dropped tables successfully');
105 }
106 /**
107 * Drop all types (if asked for and supported)
108 */
109 async performDropTypes(client) {
110 if (!this.dropTypes) {
111 return;
112 }
113 if (!client.dialect.supportsTypes) {
114 this.logger.warning(`Dropping types is not supported by "${client.dialect.name}"`);
115 }
116 await client.dropAllTypes();
117 this.logger.success('Dropped types successfully');
118 }
119 /**
120 * Run as a subcommand. Never close database connections or exit
121 * process inside this method
122 */
123 async runAsSubCommand() {
124 const db = this.application.container.use('Adonis/Lucid/Database');
125 this.connection = this.connection || db.primaryConnectionName;
126 const connection = db.connection(this.connection || db.primaryConnectionName);
127 /**
128 * Continue with clearing the database when not in production
129 * or force flag is passed
130 */
131 let continueWipe = !this.application.inProduction || this.force;
132 if (!continueWipe) {
133 continueWipe = await this.takeProductionConstent();
134 }
135 /**
136 * Do not continue when in prod and the prompt was cancelled
137 */
138 if (!continueWipe) {
139 return;
140 }
141 /**
142 * Invalid database connection
143 */
144 if (!db.manager.has(this.connection)) {
145 this.printNotAValidConnection(this.connection);
146 this.exitCode = 1;
147 return;
148 }
149 await this.performDropViews(connection);
150 await this.performDropTables(connection);
151 await this.performDropTypes(connection);
152 }
153 /**
154 * Branching out, so that if required we can implement
155 * "runAsMain" separately from "runAsSubCommand".
156 *
157 * For now, they both are the same
158 */
159 async runAsMain() {
160 await this.runAsSubCommand();
161 }
162 /**
163 * Handle command
164 */
165 async run() {
166 if (this.isMain) {
167 await this.runAsMain();
168 }
169 else {
170 await this.runAsSubCommand();
171 }
172 }
173 /**
174 * Lifecycle method invoked by ace after the "run"
175 * method.
176 */
177 async completed() {
178 if (this.isMain) {
179 await this.application.container.use('Adonis/Lucid/Database').manager.closeAll(true);
180 }
181 }
182}
183Object.defineProperty(DbWipe, "commandName", {
184 enumerable: true,
185 configurable: true,
186 writable: true,
187 value: 'db:wipe'
188});
189Object.defineProperty(DbWipe, "description", {
190 enumerable: true,
191 configurable: true,
192 writable: true,
193 value: 'Drop all tables, views and types in database'
194});
195Object.defineProperty(DbWipe, "settings", {
196 enumerable: true,
197 configurable: true,
198 writable: true,
199 value: {
200 loadApp: true,
201 }
202});
203__decorate([
204 standalone_1.flags.string({ description: 'Define a custom database connection', alias: 'c' }),
205 __metadata("design:type", String)
206], DbWipe.prototype, "connection", void 0);
207__decorate([
208 standalone_1.flags.boolean({ description: 'Drop all views' }),
209 __metadata("design:type", Boolean)
210], DbWipe.prototype, "dropViews", void 0);
211__decorate([
212 standalone_1.flags.boolean({ description: 'Drop all custom types (Postgres only)' }),
213 __metadata("design:type", Boolean)
214], DbWipe.prototype, "dropTypes", void 0);
215__decorate([
216 standalone_1.flags.boolean({ description: 'Explicitly force command to run in production' }),
217 __metadata("design:type", Boolean)
218], DbWipe.prototype, "force", void 0);
219exports.default = DbWipe;