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 DbTruncate 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, "force", {
|
38 | enumerable: true,
|
39 | configurable: true,
|
40 | writable: true,
|
41 | value: void 0
|
42 | });
|
43 | }
|
44 | |
45 |
|
46 |
|
47 | printNotAValidConnection(connection) {
|
48 | this.logger.error(`"${connection}" is not a valid connection name. Double check "config/database" file`);
|
49 | }
|
50 | |
51 |
|
52 |
|
53 | async takeProductionConstent() {
|
54 | |
55 |
|
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 |
|
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 |
|
79 |
|
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 |
|
87 |
|
88 |
|
89 | let continueTruncate = !this.application.inProduction || this.force;
|
90 | if (!continueTruncate) {
|
91 | continueTruncate = await this.takeProductionConstent();
|
92 | }
|
93 | |
94 |
|
95 |
|
96 | if (!continueTruncate) {
|
97 | return;
|
98 | }
|
99 | |
100 |
|
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 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | async runAsMain() {
|
116 | await this.runAsSubCommand();
|
117 | }
|
118 | |
119 |
|
120 |
|
121 | async run() {
|
122 | if (this.isMain) {
|
123 | await this.runAsMain();
|
124 | }
|
125 | else {
|
126 | await this.runAsSubCommand();
|
127 | }
|
128 | }
|
129 | |
130 |
|
131 |
|
132 |
|
133 | async completed() {
|
134 | if (this.isMain) {
|
135 | await this.application.container.use('Adonis/Lucid/Database').manager.closeAll(true);
|
136 | }
|
137 | }
|
138 | }
|
139 | Object.defineProperty(DbTruncate, "commandName", {
|
140 | enumerable: true,
|
141 | configurable: true,
|
142 | writable: true,
|
143 | value: 'db:truncate'
|
144 | });
|
145 | Object.defineProperty(DbTruncate, "description", {
|
146 | enumerable: true,
|
147 | configurable: true,
|
148 | writable: true,
|
149 | value: 'Truncate all tables in database'
|
150 | });
|
151 | Object.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);
|
167 | exports.default = DbTruncate;
|