1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var __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 | };
|
16 | var __metadata = (this && this.__metadata) || function (k, v) {
|
17 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
18 | };
|
19 | Object.defineProperty(exports, "__esModule", { value: true });
|
20 | const standalone_1 = require("@adonisjs/core/build/standalone");
|
21 | class DbWipe extends standalone_1.BaseCommand {
|
22 | constructor() {
|
23 | super(...arguments);
|
24 | |
25 |
|
26 |
|
27 |
|
28 | Object.defineProperty(this, "connection", {
|
29 | enumerable: true,
|
30 | configurable: true,
|
31 | writable: true,
|
32 | value: void 0
|
33 | });
|
34 | |
35 |
|
36 |
|
37 | Object.defineProperty(this, "dropViews", {
|
38 | enumerable: true,
|
39 | configurable: true,
|
40 | writable: true,
|
41 | value: void 0
|
42 | });
|
43 | |
44 |
|
45 |
|
46 | Object.defineProperty(this, "dropTypes", {
|
47 | enumerable: true,
|
48 | configurable: true,
|
49 | writable: true,
|
50 | value: void 0
|
51 | });
|
52 | |
53 |
|
54 |
|
55 | Object.defineProperty(this, "force", {
|
56 | enumerable: true,
|
57 | configurable: true,
|
58 | writable: true,
|
59 | value: void 0
|
60 | });
|
61 | }
|
62 | |
63 |
|
64 |
|
65 | printNotAValidConnection(connection) {
|
66 | this.logger.error(`"${connection}" is not a valid connection name. Double check "config/database" file`);
|
67 | }
|
68 | |
69 |
|
70 |
|
71 | async takeProductionConstent() {
|
72 | |
73 |
|
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 |
|
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 |
|
101 |
|
102 | async performDropTables(client) {
|
103 | await client.dropAllTables();
|
104 | this.logger.success('Dropped tables successfully');
|
105 | }
|
106 | |
107 |
|
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 |
|
121 |
|
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 |
|
129 |
|
130 |
|
131 | let continueWipe = !this.application.inProduction || this.force;
|
132 | if (!continueWipe) {
|
133 | continueWipe = await this.takeProductionConstent();
|
134 | }
|
135 | |
136 |
|
137 |
|
138 | if (!continueWipe) {
|
139 | return;
|
140 | }
|
141 | |
142 |
|
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 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | async runAsMain() {
|
160 | await this.runAsSubCommand();
|
161 | }
|
162 | |
163 |
|
164 |
|
165 | async run() {
|
166 | if (this.isMain) {
|
167 | await this.runAsMain();
|
168 | }
|
169 | else {
|
170 | await this.runAsSubCommand();
|
171 | }
|
172 | }
|
173 | |
174 |
|
175 |
|
176 |
|
177 | async completed() {
|
178 | if (this.isMain) {
|
179 | await this.application.container.use('Adonis/Lucid/Database').manager.closeAll(true);
|
180 | }
|
181 | }
|
182 | }
|
183 | Object.defineProperty(DbWipe, "commandName", {
|
184 | enumerable: true,
|
185 | configurable: true,
|
186 | writable: true,
|
187 | value: 'db:wipe'
|
188 | });
|
189 | Object.defineProperty(DbWipe, "description", {
|
190 | enumerable: true,
|
191 | configurable: true,
|
192 | writable: true,
|
193 | value: 'Drop all tables, views and types in database'
|
194 | });
|
195 | Object.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);
|
219 | exports.default = DbWipe;
|