UNPKG

7.4 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 path_1 = require("path");
21const helpers_1 = require("@poppinss/utils/build/helpers");
22const standalone_1 = require("@adonisjs/core/build/standalone");
23class MakeMigration extends standalone_1.BaseCommand {
24 constructor() {
25 super(...arguments);
26 /**
27 * The name of the migration file. We use this to create the migration
28 * file and generate the table name
29 */
30 Object.defineProperty(this, "name", {
31 enumerable: true,
32 configurable: true,
33 writable: true,
34 value: void 0
35 });
36 /**
37 * Choose a custom pre-defined connection. Otherwise, we use the
38 * default connection
39 */
40 Object.defineProperty(this, "connection", {
41 enumerable: true,
42 configurable: true,
43 writable: true,
44 value: void 0
45 });
46 /**
47 * Pre select migration directory. If this is defined, we will ignore the paths
48 * defined inside the config.
49 */
50 Object.defineProperty(this, "folder", {
51 enumerable: true,
52 configurable: true,
53 writable: true,
54 value: void 0
55 });
56 /**
57 * Custom table name for creating a new table
58 */
59 Object.defineProperty(this, "create", {
60 enumerable: true,
61 configurable: true,
62 writable: true,
63 value: void 0
64 });
65 /**
66 * Custom table name for altering an existing table
67 */
68 Object.defineProperty(this, "table", {
69 enumerable: true,
70 configurable: true,
71 writable: true,
72 value: void 0
73 });
74 }
75 /**
76 * Not a valid connection
77 */
78 printNotAValidConnection(connection) {
79 this.logger.error(`"${connection}" is not a valid connection name. Double check "config/database" file`);
80 }
81 /**
82 * Returns the directory for creating the migration file
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 * Execute command
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 * Invalid database connection
103 */
104 if (!connection) {
105 this.printNotAValidConnection(this.connection);
106 this.exitCode = 1;
107 return;
108 }
109 /**
110 * Not allowed together, hence we must notify the user about the same
111 */
112 if (this.table && this.create) {
113 this.logger.warning('--table and --create cannot be used together. Ignoring --create');
114 }
115 /**
116 * The folder for creating the schema file
117 */
118 const folder = await this.getDirectory((connection.config.migrations || {}).paths);
119 /**
120 * Using the user defined table name or an empty string. We can attempt to
121 * build the table name from the migration file name, but let's do that
122 * later.
123 */
124 const tableName = this.table || this.create || '';
125 /**
126 * Template stub
127 */
128 const stub = (0, path_1.join)(__dirname, '..', 'templates', this.table ? 'migration-alter.txt' : 'migration-make.txt');
129 /**
130 * Prepend timestamp to keep schema files in the order they
131 * have been created
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}
156Object.defineProperty(MakeMigration, "commandName", {
157 enumerable: true,
158 configurable: true,
159 writable: true,
160 value: 'make:migration'
161});
162Object.defineProperty(MakeMigration, "description", {
163 enumerable: true,
164 configurable: true,
165 writable: true,
166 value: 'Make a new migration file'
167});
168Object.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);
198exports.default = MakeMigration;