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 path_1 = require("path");
|
21 | const helpers_1 = require("@poppinss/utils/build/helpers");
|
22 | const standalone_1 = require("@adonisjs/core/build/standalone");
|
23 | class MakeMigration extends standalone_1.BaseCommand {
|
24 | constructor() {
|
25 | super(...arguments);
|
26 | |
27 |
|
28 |
|
29 |
|
30 | Object.defineProperty(this, "name", {
|
31 | enumerable: true,
|
32 | configurable: true,
|
33 | writable: true,
|
34 | value: void 0
|
35 | });
|
36 | |
37 |
|
38 |
|
39 |
|
40 | Object.defineProperty(this, "connection", {
|
41 | enumerable: true,
|
42 | configurable: true,
|
43 | writable: true,
|
44 | value: void 0
|
45 | });
|
46 | |
47 |
|
48 |
|
49 |
|
50 | Object.defineProperty(this, "folder", {
|
51 | enumerable: true,
|
52 | configurable: true,
|
53 | writable: true,
|
54 | value: void 0
|
55 | });
|
56 | |
57 |
|
58 |
|
59 | Object.defineProperty(this, "create", {
|
60 | enumerable: true,
|
61 | configurable: true,
|
62 | writable: true,
|
63 | value: void 0
|
64 | });
|
65 | |
66 |
|
67 |
|
68 | Object.defineProperty(this, "table", {
|
69 | enumerable: true,
|
70 | configurable: true,
|
71 | writable: true,
|
72 | value: void 0
|
73 | });
|
74 | }
|
75 | |
76 |
|
77 |
|
78 | printNotAValidConnection(connection) {
|
79 | this.logger.error(`"${connection}" is not a valid connection name. Double check "config/database" file`);
|
80 | }
|
81 | |
82 |
|
83 |
|
84 | async getDirectory(migrationPaths) {
|
85 | if (this.folder) {
|
86 | return this.folder;
|
87 | }
|
88 | let directories = migrationPaths?.length ? migrationPaths : ['database/migrations'];
|
89 | if (directories.length === 1) {
|
90 | return directories[0];
|
91 | }
|
92 | return this.prompt.choice('Select the migrations folder', directories, { name: 'folder' });
|
93 | }
|
94 | |
95 |
|
96 |
|
97 | async run() {
|
98 | const db = this.application.container.use('Adonis/Lucid/Database');
|
99 | this.connection = this.connection || db.primaryConnectionName;
|
100 | const connection = db.getRawConnection(this.connection || db.primaryConnectionName);
|
101 | |
102 |
|
103 |
|
104 | if (!connection) {
|
105 | this.printNotAValidConnection(this.connection);
|
106 | this.exitCode = 1;
|
107 | return;
|
108 | }
|
109 | |
110 |
|
111 |
|
112 | if (this.table && this.create) {
|
113 | this.logger.warning('--table and --create cannot be used together. Ignoring --create');
|
114 | }
|
115 | |
116 |
|
117 |
|
118 | const folder = await this.getDirectory((connection.config.migrations || {}).paths);
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 | const tableName = this.table || this.create || '';
|
125 | |
126 |
|
127 |
|
128 | const stub = (0, path_1.join)(__dirname, '..', 'templates', this.table ? 'migration-alter.txt' : 'migration-make.txt');
|
129 | |
130 |
|
131 |
|
132 |
|
133 | const prefix = `${new Date().getTime()}_`;
|
134 | this.generator
|
135 | .addFile(this.name, { pattern: 'snakecase', form: 'plural', prefix })
|
136 | .stub(stub)
|
137 | .destinationDir(folder)
|
138 | .appRoot(this.application.cliCwd || this.application.appRoot)
|
139 | .useMustache()
|
140 | .apply({
|
141 | toClassName() {
|
142 | return function (filename, render) {
|
143 | const migrationClassName = helpers_1.string.camelCase(tableName || render(filename).replace(prefix, ''));
|
144 | return `${migrationClassName.charAt(0).toUpperCase()}${migrationClassName.slice(1)}`;
|
145 | };
|
146 | },
|
147 | toTableName() {
|
148 | return function (filename, render) {
|
149 | return tableName || helpers_1.string.snakeCase(render(filename).replace(prefix, ''));
|
150 | };
|
151 | },
|
152 | });
|
153 | await this.generator.run();
|
154 | }
|
155 | }
|
156 | Object.defineProperty(MakeMigration, "commandName", {
|
157 | enumerable: true,
|
158 | configurable: true,
|
159 | writable: true,
|
160 | value: 'make:migration'
|
161 | });
|
162 | Object.defineProperty(MakeMigration, "description", {
|
163 | enumerable: true,
|
164 | configurable: true,
|
165 | writable: true,
|
166 | value: 'Make a new migration file'
|
167 | });
|
168 | Object.defineProperty(MakeMigration, "settings", {
|
169 | enumerable: true,
|
170 | configurable: true,
|
171 | writable: true,
|
172 | value: {
|
173 | loadApp: true,
|
174 | }
|
175 | });
|
176 | __decorate([
|
177 | standalone_1.args.string({ description: 'Name of the migration file' }),
|
178 | __metadata("design:type", String)
|
179 | ], MakeMigration.prototype, "name", void 0);
|
180 | __decorate([
|
181 | standalone_1.flags.string({
|
182 | description: 'The connection flag is used to lookup the directory for the migration file',
|
183 | }),
|
184 | __metadata("design:type", String)
|
185 | ], MakeMigration.prototype, "connection", void 0);
|
186 | __decorate([
|
187 | standalone_1.flags.string({ description: 'Pre-select a migration directory' }),
|
188 | __metadata("design:type", String)
|
189 | ], MakeMigration.prototype, "folder", void 0);
|
190 | __decorate([
|
191 | standalone_1.flags.string({ description: 'Define the table name for creating a new table' }),
|
192 | __metadata("design:type", String)
|
193 | ], MakeMigration.prototype, "create", void 0);
|
194 | __decorate([
|
195 | standalone_1.flags.string({ description: 'Define the table name for altering an existing table' }),
|
196 | __metadata("design:type", String)
|
197 | ], MakeMigration.prototype, "table", void 0);
|
198 | exports.default = MakeMigration;
|