rake-db 2.23.11 → 2.23.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +381 -702
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +381 -702
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -16,10 +16,8 @@ const getFirstWordAndRest = (input) => {
|
|
|
16
16
|
};
|
|
17
17
|
const getTextAfterRegExp = (input, regex, length) => {
|
|
18
18
|
let index = input.search(regex);
|
|
19
|
-
if (index === -1)
|
|
20
|
-
|
|
21
|
-
if (input[index] === "-" || input[index] === "_")
|
|
22
|
-
index++;
|
|
19
|
+
if (index === -1) return;
|
|
20
|
+
if (input[index] === "-" || input[index] === "_") index++;
|
|
23
21
|
index += length;
|
|
24
22
|
const start = input[index] == "-" || input[index] === "_" ? index + 1 : index;
|
|
25
23
|
const text = input.slice(start);
|
|
@@ -95,25 +93,6 @@ const clearChanges = () => {
|
|
|
95
93
|
const getCurrentChanges = () => currentChanges;
|
|
96
94
|
const pushChange = (fn) => currentChanges.push(fn);
|
|
97
95
|
|
|
98
|
-
var __defProp$9 = Object.defineProperty;
|
|
99
|
-
var __defProps$7 = Object.defineProperties;
|
|
100
|
-
var __getOwnPropDescs$7 = Object.getOwnPropertyDescriptors;
|
|
101
|
-
var __getOwnPropSymbols$9 = Object.getOwnPropertySymbols;
|
|
102
|
-
var __hasOwnProp$9 = Object.prototype.hasOwnProperty;
|
|
103
|
-
var __propIsEnum$9 = Object.prototype.propertyIsEnumerable;
|
|
104
|
-
var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
105
|
-
var __spreadValues$9 = (a, b) => {
|
|
106
|
-
for (var prop in b || (b = {}))
|
|
107
|
-
if (__hasOwnProp$9.call(b, prop))
|
|
108
|
-
__defNormalProp$9(a, prop, b[prop]);
|
|
109
|
-
if (__getOwnPropSymbols$9)
|
|
110
|
-
for (var prop of __getOwnPropSymbols$9(b)) {
|
|
111
|
-
if (__propIsEnum$9.call(b, prop))
|
|
112
|
-
__defNormalProp$9(a, prop, b[prop]);
|
|
113
|
-
}
|
|
114
|
-
return a;
|
|
115
|
-
};
|
|
116
|
-
var __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
|
|
117
96
|
const versionToString = (config, version) => config.migrationId === "timestamp" ? `${version}` : `${version}`.padStart(config.migrationId.serial, "0");
|
|
118
97
|
const columnTypeToSql = (item) => {
|
|
119
98
|
return item.data.isOfCustomType ? item instanceof DomainColumn ? quoteNameFromString(item.dataType) : quoteCustomType(item.toSQL()) : item.toSQL();
|
|
@@ -122,7 +101,6 @@ const getColumnName = (item, key, snakeCase) => {
|
|
|
122
101
|
return item.data.name || (snakeCase ? toSnakeCase(key) : key);
|
|
123
102
|
};
|
|
124
103
|
const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
|
|
125
|
-
var _a, _b;
|
|
126
104
|
const line = [`"${name}" ${columnTypeToSql(item)}`];
|
|
127
105
|
if (item.data.compression) {
|
|
128
106
|
line.push(`COMPRESSION ${item.data.compression}`);
|
|
@@ -152,19 +130,19 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
|
|
|
152
130
|
line.push(checkToSql(item.data.check.sql, values));
|
|
153
131
|
}
|
|
154
132
|
const def = encodeColumnDefault(item.data.default, values, item);
|
|
155
|
-
if (def !== null)
|
|
156
|
-
line.push(`DEFAULT ${def}`);
|
|
133
|
+
if (def !== null) line.push(`DEFAULT ${def}`);
|
|
157
134
|
const { foreignKeys } = item.data;
|
|
158
135
|
if (foreignKeys) {
|
|
159
136
|
for (const foreignKey of foreignKeys) {
|
|
160
|
-
if (
|
|
161
|
-
line.push(`CONSTRAINT "${
|
|
137
|
+
if (foreignKey.options?.name) {
|
|
138
|
+
line.push(`CONSTRAINT "${foreignKey.options?.name}"`);
|
|
162
139
|
}
|
|
163
140
|
line.push(
|
|
164
141
|
referencesToSql(
|
|
165
|
-
|
|
166
|
-
columns: [name]
|
|
167
|
-
|
|
142
|
+
{
|
|
143
|
+
columns: [name],
|
|
144
|
+
...foreignKey
|
|
145
|
+
},
|
|
168
146
|
snakeCase
|
|
169
147
|
)
|
|
170
148
|
);
|
|
@@ -178,7 +156,7 @@ const encodeColumnDefault = (def, values, column) => {
|
|
|
178
156
|
return def.toSQL({ values });
|
|
179
157
|
} else {
|
|
180
158
|
return escapeForMigration(
|
|
181
|
-
|
|
159
|
+
column?.data.encode ? column.data.encode(def) : def
|
|
182
160
|
);
|
|
183
161
|
}
|
|
184
162
|
}
|
|
@@ -190,20 +168,13 @@ const identityToSql = (identity) => {
|
|
|
190
168
|
};
|
|
191
169
|
const sequenceOptionsToSql = (item) => {
|
|
192
170
|
const line = [];
|
|
193
|
-
if (item.dataType)
|
|
194
|
-
|
|
195
|
-
if (item.
|
|
196
|
-
|
|
197
|
-
if (item.
|
|
198
|
-
|
|
199
|
-
if (item.
|
|
200
|
-
line.push(`MAXVALUE ${item.max}`);
|
|
201
|
-
if (item.start !== void 0)
|
|
202
|
-
line.push(`START WITH ${item.start}`);
|
|
203
|
-
if (item.cache !== void 0)
|
|
204
|
-
line.push(`CACHE ${item.cache}`);
|
|
205
|
-
if (item.cycle)
|
|
206
|
-
line.push(`CYCLE`);
|
|
171
|
+
if (item.dataType) line.push(`AS ${item.dataType}`);
|
|
172
|
+
if (item.increment !== void 0) line.push(`INCREMENT BY ${item.increment}`);
|
|
173
|
+
if (item.min !== void 0) line.push(`MINVALUE ${item.min}`);
|
|
174
|
+
if (item.max !== void 0) line.push(`MAXVALUE ${item.max}`);
|
|
175
|
+
if (item.start !== void 0) line.push(`START WITH ${item.start}`);
|
|
176
|
+
if (item.cache !== void 0) line.push(`CACHE ${item.cache}`);
|
|
177
|
+
if (item.cycle) line.push(`CYCLE`);
|
|
207
178
|
if (item.ownedBy) {
|
|
208
179
|
const [schema, table] = getSchemaAndTableFromName(item.ownedBy);
|
|
209
180
|
line.push(`OWNED BY ${quoteTable(schema, table)}`);
|
|
@@ -213,9 +184,10 @@ const sequenceOptionsToSql = (item) => {
|
|
|
213
184
|
const addColumnIndex = (indexes, name, item) => {
|
|
214
185
|
if (item.data.indexes) {
|
|
215
186
|
indexes.push(
|
|
216
|
-
...item.data.indexes.map((index) =>
|
|
217
|
-
columns: [
|
|
218
|
-
|
|
187
|
+
...item.data.indexes.map((index) => ({
|
|
188
|
+
columns: [{ ...index.options, column: name }],
|
|
189
|
+
...index
|
|
190
|
+
}))
|
|
219
191
|
);
|
|
220
192
|
}
|
|
221
193
|
};
|
|
@@ -238,10 +210,8 @@ const getConstraintName = (table, constraint, snakeCase) => {
|
|
|
238
210
|
"_"
|
|
239
211
|
)}_fkey`;
|
|
240
212
|
}
|
|
241
|
-
if (constraint.check)
|
|
242
|
-
|
|
243
|
-
if (constraint.identity)
|
|
244
|
-
return `${table}_identity`;
|
|
213
|
+
if (constraint.check) return `${table}_check`;
|
|
214
|
+
if (constraint.identity) return `${table}_identity`;
|
|
245
215
|
return `${table}_constraint`;
|
|
246
216
|
};
|
|
247
217
|
const constraintToSql = ({ name }, up, constraint, values, snakeCase) => {
|
|
@@ -275,14 +245,14 @@ const referencesToSql = (references, snakeCase) => {
|
|
|
275
245
|
)})`
|
|
276
246
|
];
|
|
277
247
|
const { options } = references;
|
|
278
|
-
if (options
|
|
279
|
-
sql.push(`MATCH ${options
|
|
248
|
+
if (options?.match) {
|
|
249
|
+
sql.push(`MATCH ${options?.match.toUpperCase()}`);
|
|
280
250
|
}
|
|
281
|
-
if (options
|
|
282
|
-
sql.push(`ON DELETE ${options
|
|
251
|
+
if (options?.onDelete) {
|
|
252
|
+
sql.push(`ON DELETE ${options?.onDelete.toUpperCase()}`);
|
|
283
253
|
}
|
|
284
|
-
if (options
|
|
285
|
-
sql.push(`ON UPDATE ${options
|
|
254
|
+
if (options?.onUpdate) {
|
|
255
|
+
sql.push(`ON UPDATE ${options?.onUpdate.toUpperCase()}`);
|
|
286
256
|
}
|
|
287
257
|
return sql.join(" ");
|
|
288
258
|
};
|
|
@@ -294,10 +264,9 @@ const indexesToQuery = (up, { schema, name: tableName }, indexes, snakeCase, lan
|
|
|
294
264
|
let include = options.include ? toArray(options.include) : void 0;
|
|
295
265
|
if (snakeCase) {
|
|
296
266
|
columns = columns.map(
|
|
297
|
-
(c) => "column" in c ?
|
|
267
|
+
(c) => "column" in c ? { ...c, column: toSnakeCase(c.column) } : c
|
|
298
268
|
);
|
|
299
|
-
if (include)
|
|
300
|
-
include = include.map(toSnakeCase);
|
|
269
|
+
if (include) include = include.map(toSnakeCase);
|
|
301
270
|
}
|
|
302
271
|
const indexName = name || getIndexName(tableName, columns);
|
|
303
272
|
if (!up) {
|
|
@@ -385,7 +354,7 @@ const primaryKeyToSql = (primaryKey) => {
|
|
|
385
354
|
return `${primaryKey.name ? `CONSTRAINT "${primaryKey.name}" ` : ""}PRIMARY KEY (${joinColumns(primaryKey.columns)})`;
|
|
386
355
|
};
|
|
387
356
|
const interpolateSqlValues = ({ text, values }) => {
|
|
388
|
-
return
|
|
357
|
+
return values?.length ? text.replace(/\$(\d+)/g, (_, n) => {
|
|
389
358
|
const i = +n - 1;
|
|
390
359
|
return escapeForMigration(values[i]);
|
|
391
360
|
}) : text;
|
|
@@ -407,39 +376,7 @@ class RakeDbError extends Error {
|
|
|
407
376
|
class NoPrimaryKey extends RakeDbError {
|
|
408
377
|
}
|
|
409
378
|
|
|
410
|
-
var __defProp$8 = Object.defineProperty;
|
|
411
|
-
var __defProps$6 = Object.defineProperties;
|
|
412
|
-
var __getOwnPropDescs$6 = Object.getOwnPropertyDescriptors;
|
|
413
|
-
var __getOwnPropSymbols$8 = Object.getOwnPropertySymbols;
|
|
414
|
-
var __hasOwnProp$8 = Object.prototype.hasOwnProperty;
|
|
415
|
-
var __propIsEnum$8 = Object.prototype.propertyIsEnumerable;
|
|
416
|
-
var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
417
|
-
var __spreadValues$8 = (a, b) => {
|
|
418
|
-
for (var prop in b || (b = {}))
|
|
419
|
-
if (__hasOwnProp$8.call(b, prop))
|
|
420
|
-
__defNormalProp$8(a, prop, b[prop]);
|
|
421
|
-
if (__getOwnPropSymbols$8)
|
|
422
|
-
for (var prop of __getOwnPropSymbols$8(b)) {
|
|
423
|
-
if (__propIsEnum$8.call(b, prop))
|
|
424
|
-
__defNormalProp$8(a, prop, b[prop]);
|
|
425
|
-
}
|
|
426
|
-
return a;
|
|
427
|
-
};
|
|
428
|
-
var __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
|
|
429
|
-
var __objRest$1 = (source, exclude) => {
|
|
430
|
-
var target = {};
|
|
431
|
-
for (var prop in source)
|
|
432
|
-
if (__hasOwnProp$8.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
433
|
-
target[prop] = source[prop];
|
|
434
|
-
if (source != null && __getOwnPropSymbols$8)
|
|
435
|
-
for (var prop of __getOwnPropSymbols$8(source)) {
|
|
436
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum$8.call(source, prop))
|
|
437
|
-
target[prop] = source[prop];
|
|
438
|
-
}
|
|
439
|
-
return target;
|
|
440
|
-
};
|
|
441
379
|
const createTable = async (migration, up, tableName, first, second, third) => {
|
|
442
|
-
var _a;
|
|
443
380
|
let options;
|
|
444
381
|
let fn;
|
|
445
382
|
let dataFn;
|
|
@@ -465,7 +402,7 @@ const createTable = async (migration, up, tableName, first, second, third) => {
|
|
|
465
402
|
shape = getColumnTypes(
|
|
466
403
|
types,
|
|
467
404
|
fn,
|
|
468
|
-
|
|
405
|
+
migration.options.baseTable?.nowSQL,
|
|
469
406
|
language
|
|
470
407
|
);
|
|
471
408
|
tableData = parseTableData(dataFn);
|
|
@@ -482,15 +419,14 @@ const createTable = async (migration, up, tableName, first, second, third) => {
|
|
|
482
419
|
);
|
|
483
420
|
fn && validatePrimaryKey(ast);
|
|
484
421
|
const queries = astToQueries$1(ast, snakeCase, language);
|
|
485
|
-
for (const
|
|
486
|
-
const _c = _b, { then } = _c, query = __objRest$1(_c, ["then"]);
|
|
422
|
+
for (const { then, ...query } of queries) {
|
|
487
423
|
const result = await migration.adapter.arrays(interpolateSqlValues(query));
|
|
488
|
-
then
|
|
424
|
+
then?.(result);
|
|
489
425
|
}
|
|
490
426
|
let table;
|
|
491
427
|
return {
|
|
492
428
|
get table() {
|
|
493
|
-
return table
|
|
429
|
+
return table ?? (table = migration(
|
|
494
430
|
tableName,
|
|
495
431
|
shape,
|
|
496
432
|
void 0,
|
|
@@ -498,7 +434,7 @@ const createTable = async (migration, up, tableName, first, second, third) => {
|
|
|
498
434
|
noPrimaryKey: options.noPrimaryKey ? "ignore" : void 0,
|
|
499
435
|
snakeCase
|
|
500
436
|
}
|
|
501
|
-
);
|
|
437
|
+
));
|
|
502
438
|
}
|
|
503
439
|
};
|
|
504
440
|
};
|
|
@@ -512,24 +448,24 @@ const makeAst$2 = (up, tableName, shape, tableData, options, noPrimaryKey) => {
|
|
|
512
448
|
}
|
|
513
449
|
const { primaryKey } = tableData;
|
|
514
450
|
const [schema, table] = getSchemaAndTableFromName(tableName);
|
|
515
|
-
return
|
|
451
|
+
return {
|
|
516
452
|
type: "table",
|
|
517
453
|
action: up ? "create" : "drop",
|
|
518
454
|
schema,
|
|
519
455
|
name: table,
|
|
520
|
-
shape
|
|
521
|
-
|
|
522
|
-
primaryKey: shapePKeys.length <= 1 ? primaryKey : primaryKey ?
|
|
456
|
+
shape,
|
|
457
|
+
...tableData,
|
|
458
|
+
primaryKey: shapePKeys.length <= 1 ? primaryKey : primaryKey ? {
|
|
459
|
+
...primaryKey,
|
|
523
460
|
columns: [.../* @__PURE__ */ new Set([...shapePKeys, ...primaryKey.columns])]
|
|
524
|
-
}
|
|
525
|
-
|
|
461
|
+
} : { columns: shapePKeys },
|
|
462
|
+
...options,
|
|
526
463
|
noPrimaryKey: options.noPrimaryKey ? "ignore" : noPrimaryKey || "error"
|
|
527
|
-
}
|
|
464
|
+
};
|
|
528
465
|
};
|
|
529
466
|
const validatePrimaryKey = (ast) => {
|
|
530
|
-
var _a, _b;
|
|
531
467
|
if (ast.noPrimaryKey !== "ignore") {
|
|
532
|
-
let hasPrimaryKey = !!
|
|
468
|
+
let hasPrimaryKey = !!ast.primaryKey?.columns?.length;
|
|
533
469
|
if (!hasPrimaryKey) {
|
|
534
470
|
for (const key in ast.shape) {
|
|
535
471
|
if (ast.shape[key].data.primaryKey) {
|
|
@@ -552,13 +488,11 @@ You can suppress this error by setting { noPrimaryKey: true } after a table name
|
|
|
552
488
|
}
|
|
553
489
|
};
|
|
554
490
|
const astToQueries$1 = (ast, snakeCase, language) => {
|
|
555
|
-
var _a, _b;
|
|
556
491
|
const queries = [];
|
|
557
492
|
const { shape } = ast;
|
|
558
493
|
for (const key in shape) {
|
|
559
494
|
const item = shape[key];
|
|
560
|
-
if (!(item instanceof EnumColumn))
|
|
561
|
-
continue;
|
|
495
|
+
if (!(item instanceof EnumColumn)) continue;
|
|
562
496
|
queries.push(makePopulateEnumQuery(item));
|
|
563
497
|
}
|
|
564
498
|
if (ast.action === "drop") {
|
|
@@ -592,30 +526,36 @@ const astToQueries$1 = (ast, snakeCase, language) => {
|
|
|
592
526
|
})}`
|
|
593
527
|
);
|
|
594
528
|
}
|
|
595
|
-
|
|
529
|
+
ast.constraints?.forEach((item) => {
|
|
596
530
|
lines.push(
|
|
597
531
|
`
|
|
598
532
|
${constraintToSql(
|
|
599
533
|
ast,
|
|
600
534
|
true,
|
|
601
|
-
|
|
602
|
-
|
|
535
|
+
{
|
|
536
|
+
...item,
|
|
537
|
+
references: item.references ? {
|
|
538
|
+
...item.references,
|
|
603
539
|
columns: item.references.columns.map(
|
|
604
540
|
(column) => getColumnName(shape[column], column, snakeCase)
|
|
605
541
|
)
|
|
606
|
-
}
|
|
607
|
-
}
|
|
542
|
+
} : void 0
|
|
543
|
+
},
|
|
608
544
|
values,
|
|
609
545
|
snakeCase
|
|
610
546
|
)}`
|
|
611
547
|
);
|
|
612
548
|
});
|
|
613
549
|
indexes.push(
|
|
614
|
-
...
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
550
|
+
...ast.indexes?.map((index) => ({
|
|
551
|
+
...index,
|
|
552
|
+
columns: index.columns.map((item) => ({
|
|
553
|
+
...item,
|
|
554
|
+
..."column" in item ? {
|
|
555
|
+
column: getColumnName(shape[item.column], item.column, snakeCase)
|
|
556
|
+
} : {}
|
|
557
|
+
}))
|
|
558
|
+
})) || []
|
|
619
559
|
);
|
|
620
560
|
queries.push(
|
|
621
561
|
{
|
|
@@ -636,25 +576,6 @@ const astToQueries$1 = (ast, snakeCase, language) => {
|
|
|
636
576
|
return queries;
|
|
637
577
|
};
|
|
638
578
|
|
|
639
|
-
var __defProp$7 = Object.defineProperty;
|
|
640
|
-
var __defProps$5 = Object.defineProperties;
|
|
641
|
-
var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
|
|
642
|
-
var __getOwnPropSymbols$7 = Object.getOwnPropertySymbols;
|
|
643
|
-
var __hasOwnProp$7 = Object.prototype.hasOwnProperty;
|
|
644
|
-
var __propIsEnum$7 = Object.prototype.propertyIsEnumerable;
|
|
645
|
-
var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
646
|
-
var __spreadValues$7 = (a, b) => {
|
|
647
|
-
for (var prop in b || (b = {}))
|
|
648
|
-
if (__hasOwnProp$7.call(b, prop))
|
|
649
|
-
__defNormalProp$7(a, prop, b[prop]);
|
|
650
|
-
if (__getOwnPropSymbols$7)
|
|
651
|
-
for (var prop of __getOwnPropSymbols$7(b)) {
|
|
652
|
-
if (__propIsEnum$7.call(b, prop))
|
|
653
|
-
__defNormalProp$7(a, prop, b[prop]);
|
|
654
|
-
}
|
|
655
|
-
return a;
|
|
656
|
-
};
|
|
657
|
-
var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
|
|
658
579
|
const newChangeTableData = () => ({
|
|
659
580
|
add: {},
|
|
660
581
|
drop: {}
|
|
@@ -669,8 +590,7 @@ function add(item, options) {
|
|
|
669
590
|
setName(this, item);
|
|
670
591
|
if (item instanceof ColumnType) {
|
|
671
592
|
const result = addOrDrop("add", item, options);
|
|
672
|
-
if (result.type === "change")
|
|
673
|
-
return result;
|
|
593
|
+
if (result.type === "change") return result;
|
|
674
594
|
addOrDropChanges.push(result);
|
|
675
595
|
return addOrDropChanges.length - 1;
|
|
676
596
|
}
|
|
@@ -681,7 +601,7 @@ function add(item, options) {
|
|
|
681
601
|
result[key2] = {
|
|
682
602
|
type: "add",
|
|
683
603
|
item: item[key2],
|
|
684
|
-
dropMode: options
|
|
604
|
+
dropMode: options?.dropMode
|
|
685
605
|
};
|
|
686
606
|
}
|
|
687
607
|
return result;
|
|
@@ -696,8 +616,7 @@ const drop = function(item, options) {
|
|
|
696
616
|
setName(this, item);
|
|
697
617
|
if (item instanceof ColumnType) {
|
|
698
618
|
const result = addOrDrop("drop", item, options);
|
|
699
|
-
if (result.type === "change")
|
|
700
|
-
return result;
|
|
619
|
+
if (result.type === "change") return result;
|
|
701
620
|
addOrDropChanges.push(result);
|
|
702
621
|
return addOrDropChanges.length - 1;
|
|
703
622
|
}
|
|
@@ -708,7 +627,7 @@ const drop = function(item, options) {
|
|
|
708
627
|
result[key2] = {
|
|
709
628
|
type: "drop",
|
|
710
629
|
item: item[key2],
|
|
711
|
-
dropMode: options
|
|
630
|
+
dropMode: options?.dropMode
|
|
712
631
|
};
|
|
713
632
|
}
|
|
714
633
|
return result;
|
|
@@ -732,55 +651,53 @@ const addOrDrop = (type, item, options) => {
|
|
|
732
651
|
check: item.data.check
|
|
733
652
|
}
|
|
734
653
|
});
|
|
735
|
-
return
|
|
654
|
+
return {
|
|
736
655
|
type: "change",
|
|
737
656
|
from: type === "add" ? empty : add2,
|
|
738
|
-
to: type === "add" ? add2 : empty
|
|
739
|
-
|
|
657
|
+
to: type === "add" ? add2 : empty,
|
|
658
|
+
...options
|
|
659
|
+
};
|
|
740
660
|
}
|
|
741
661
|
return {
|
|
742
662
|
type,
|
|
743
663
|
item,
|
|
744
|
-
dropMode: options
|
|
664
|
+
dropMode: options?.dropMode
|
|
745
665
|
};
|
|
746
666
|
};
|
|
747
667
|
const columnTypeToColumnChange = (item, name) => {
|
|
748
668
|
if (item instanceof ColumnType) {
|
|
749
669
|
let column = item;
|
|
750
670
|
const foreignKeys = column.data.foreignKeys;
|
|
751
|
-
if (foreignKeys
|
|
671
|
+
if (foreignKeys?.some((it) => "fn" in it)) {
|
|
752
672
|
throw new Error("Callback in foreignKey is not allowed in migration");
|
|
753
673
|
}
|
|
754
|
-
|
|
755
|
-
column = Object.create(column);
|
|
756
|
-
column.data = __spreadProps$5(__spreadValues$7({}, column.data), { name });
|
|
757
|
-
}
|
|
758
|
-
return __spreadProps$5(__spreadValues$7({
|
|
674
|
+
return {
|
|
759
675
|
column,
|
|
760
676
|
type: column.toSQL(),
|
|
761
|
-
nullable: column.data.isNullable
|
|
762
|
-
|
|
677
|
+
nullable: column.data.isNullable,
|
|
678
|
+
...column.data,
|
|
763
679
|
primaryKey: column.data.primaryKey === void 0 ? void 0 : true,
|
|
764
680
|
foreignKeys
|
|
765
|
-
}
|
|
681
|
+
};
|
|
766
682
|
}
|
|
767
683
|
return item.to;
|
|
768
684
|
};
|
|
769
685
|
const nameKey = Symbol("name");
|
|
770
686
|
const setName = (self, item) => {
|
|
771
|
-
var _a, _b
|
|
687
|
+
var _a, _b;
|
|
772
688
|
const name = self[nameKey];
|
|
773
|
-
if (!name)
|
|
774
|
-
return;
|
|
689
|
+
if (!name) return;
|
|
775
690
|
if ("column" in item && item.column instanceof ColumnType) {
|
|
776
|
-
(
|
|
691
|
+
(_a = item.column.data).name ?? (_a.name = name);
|
|
777
692
|
} else if (item instanceof ColumnType) {
|
|
778
|
-
(
|
|
693
|
+
(_b = item.data).name ?? (_b.name = name);
|
|
779
694
|
} else {
|
|
780
|
-
|
|
695
|
+
item.name ?? (item.name = name);
|
|
781
696
|
}
|
|
782
697
|
};
|
|
783
|
-
const tableChangeMethods =
|
|
698
|
+
const tableChangeMethods = {
|
|
699
|
+
...tableMethods,
|
|
700
|
+
...tableDataMethods,
|
|
784
701
|
name(name) {
|
|
785
702
|
setCurrentColumnName(name);
|
|
786
703
|
const types = Object.create(this);
|
|
@@ -846,9 +763,8 @@ const tableChangeMethods = __spreadProps$5(__spreadValues$7(__spreadValues$7({},
|
|
|
846
763
|
rename(name) {
|
|
847
764
|
return { type: "rename", name };
|
|
848
765
|
}
|
|
849
|
-
}
|
|
766
|
+
};
|
|
850
767
|
const changeTable = async (migration, up, tableName, options, fn) => {
|
|
851
|
-
var _a;
|
|
852
768
|
const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
|
|
853
769
|
const language = "language" in options ? options.language : migration.options.language;
|
|
854
770
|
setDefaultLanguage(language);
|
|
@@ -859,12 +775,12 @@ const changeTable = async (migration, up, tableName, options, fn) => {
|
|
|
859
775
|
Object.assign(tableChanger, tableChangeMethods);
|
|
860
776
|
tableChanger[snakeCaseKey] = snakeCase;
|
|
861
777
|
addOrDropChanges.length = 0;
|
|
862
|
-
const changeData =
|
|
778
|
+
const changeData = fn?.(tableChanger) || {};
|
|
863
779
|
const ast = makeAst$1(up, tableName, changeData, changeTableData, options);
|
|
864
780
|
const queries = astToQueries(ast, snakeCase, language);
|
|
865
781
|
for (const query of queries) {
|
|
866
782
|
const result = await migration.adapter.arrays(interpolateSqlValues(query));
|
|
867
|
-
|
|
783
|
+
query.then?.(result);
|
|
868
784
|
}
|
|
869
785
|
};
|
|
870
786
|
const makeAst$1 = (up, name, changeData, changeTableData2, options) => {
|
|
@@ -884,23 +800,23 @@ const makeAst$1 = (up, name, changeData, changeTableData2, options) => {
|
|
|
884
800
|
shape[key] = item;
|
|
885
801
|
} else {
|
|
886
802
|
if (item.type === "rename") {
|
|
887
|
-
shape[item.name] =
|
|
803
|
+
shape[item.name] = { ...item, name: key };
|
|
888
804
|
} else {
|
|
889
|
-
shape[key] = item.type === "add" ?
|
|
805
|
+
shape[key] = item.type === "add" ? { ...item, type: "drop" } : item.type === "drop" ? { ...item, type: "add" } : item.type === "change" ? {
|
|
806
|
+
...item,
|
|
890
807
|
from: item.to,
|
|
891
808
|
to: item.from,
|
|
892
809
|
using: item.using && {
|
|
893
810
|
usingUp: item.using.usingDown,
|
|
894
811
|
usingDown: item.using.usingUp
|
|
895
812
|
}
|
|
896
|
-
}
|
|
813
|
+
} : item;
|
|
897
814
|
}
|
|
898
815
|
}
|
|
899
816
|
}
|
|
900
817
|
}
|
|
901
818
|
for (let i = 0; i < addOrDropChanges.length; i++) {
|
|
902
|
-
if (consumedChanges[i])
|
|
903
|
-
continue;
|
|
819
|
+
if (consumedChanges[i]) continue;
|
|
904
820
|
const change = addOrDropChanges[i];
|
|
905
821
|
const name2 = change.item.data.name;
|
|
906
822
|
if (!name2) {
|
|
@@ -908,21 +824,21 @@ const makeAst$1 = (up, name, changeData, changeTableData2, options) => {
|
|
|
908
824
|
}
|
|
909
825
|
const arr = shape[name2] ? toArray(shape[name2]) : [];
|
|
910
826
|
arr[up ? "push" : "unshift"](
|
|
911
|
-
up ? change :
|
|
827
|
+
up ? change : { ...change, type: change.type === "add" ? "drop" : "add" }
|
|
912
828
|
);
|
|
913
829
|
shape[name2] = arr;
|
|
914
830
|
}
|
|
915
831
|
const [schema, table] = getSchemaAndTableFromName(name);
|
|
916
|
-
return
|
|
832
|
+
return {
|
|
917
833
|
type: "changeTable",
|
|
918
834
|
schema,
|
|
919
835
|
name: table,
|
|
920
836
|
comment: comment ? up ? Array.isArray(comment) ? comment[1] : comment : Array.isArray(comment) ? comment[0] : null : void 0,
|
|
921
|
-
shape
|
|
922
|
-
|
|
837
|
+
shape,
|
|
838
|
+
...up ? changeTableData2 : { add: changeTableData2.drop, drop: changeTableData2.add }
|
|
839
|
+
};
|
|
923
840
|
};
|
|
924
841
|
const astToQueries = (ast, snakeCase, language) => {
|
|
925
|
-
var _a, _b, _c, _d;
|
|
926
842
|
const queries = [];
|
|
927
843
|
if (ast.comment !== void 0) {
|
|
928
844
|
queries.push({
|
|
@@ -978,10 +894,10 @@ const astToQueries = (ast, snakeCase, language) => {
|
|
|
978
894
|
const alterTable = [];
|
|
979
895
|
const renameItems = [];
|
|
980
896
|
const values = [];
|
|
981
|
-
const addIndexes =
|
|
982
|
-
const dropIndexes =
|
|
983
|
-
const addConstraints =
|
|
984
|
-
const dropConstraints =
|
|
897
|
+
const addIndexes = ast.add.indexes ?? [];
|
|
898
|
+
const dropIndexes = ast.drop.indexes ?? [];
|
|
899
|
+
const addConstraints = ast.add.constraints ?? [];
|
|
900
|
+
const dropConstraints = ast.drop.constraints ?? [];
|
|
985
901
|
const comments = [];
|
|
986
902
|
for (const key in ast.shape) {
|
|
987
903
|
const item = ast.shape[key];
|
|
@@ -1110,7 +1026,6 @@ const handlePrerequisitesForTableItem = (key, item, queries, addPrimaryKeys, dro
|
|
|
1110
1026
|
}
|
|
1111
1027
|
};
|
|
1112
1028
|
const handleTableItemChange = (key, item, ast, alterTable, renameItems, values, addPrimaryKeys, addIndexes, dropIndexes, addConstraints, dropConstraints, comments, snakeCase) => {
|
|
1113
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
|
|
1114
1029
|
if (item.type === "add") {
|
|
1115
1030
|
const column = item.item;
|
|
1116
1031
|
const name = getColumnName(column, key, snakeCase);
|
|
@@ -1142,7 +1057,7 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
|
|
|
1142
1057
|
changeType = true;
|
|
1143
1058
|
const type = !to.column || to.column.data.isOfCustomType ? to.column && to.column instanceof DomainColumn ? quoteNameFromString(to.type) : quoteCustomType(to.type) : to.type;
|
|
1144
1059
|
alterTable.push(
|
|
1145
|
-
`ALTER COLUMN "${name}" TYPE ${type}${to.collate ? ` COLLATE ${quoteNameFromString(to.collate)}` : ""}${
|
|
1060
|
+
`ALTER COLUMN "${name}" TYPE ${type}${to.collate ? ` COLLATE ${quoteNameFromString(to.collate)}` : ""}${item.using?.usingUp ? ` USING ${item.using.usingUp.toSQL({ values })}` : to.column instanceof EnumColumn ? ` USING "${name}"::text::${type}` : ""}`
|
|
1146
1061
|
);
|
|
1147
1062
|
}
|
|
1148
1063
|
if (typeof from.identity !== typeof to.identity || !deepCompare(from.identity, to.identity)) {
|
|
@@ -1188,62 +1103,66 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
|
|
|
1188
1103
|
}
|
|
1189
1104
|
}
|
|
1190
1105
|
const foreignKeysLen = Math.max(
|
|
1191
|
-
|
|
1192
|
-
|
|
1106
|
+
from.foreignKeys?.length || 0,
|
|
1107
|
+
to.foreignKeys?.length || 0
|
|
1193
1108
|
);
|
|
1194
1109
|
for (let i = 0; i < foreignKeysLen; i++) {
|
|
1195
|
-
const fromFkey =
|
|
1196
|
-
const toFkey =
|
|
1197
|
-
if ((fromFkey || toFkey) && (!fromFkey || !toFkey ||
|
|
1110
|
+
const fromFkey = from.foreignKeys?.[i];
|
|
1111
|
+
const toFkey = to.foreignKeys?.[i];
|
|
1112
|
+
if ((fromFkey || toFkey) && (!fromFkey || !toFkey || fromFkey.options?.name !== toFkey.options?.name || fromFkey.options?.match !== toFkey.options?.match || fromFkey.options?.onUpdate !== toFkey.options?.onUpdate || fromFkey.options?.onDelete !== toFkey.options?.onDelete || fromFkey.options?.dropMode !== toFkey.options?.dropMode || fromFkey.fnOrTable !== toFkey.fnOrTable)) {
|
|
1198
1113
|
if (fromFkey) {
|
|
1199
1114
|
dropConstraints.push({
|
|
1200
|
-
name:
|
|
1201
|
-
dropMode:
|
|
1202
|
-
references:
|
|
1203
|
-
columns: [name]
|
|
1204
|
-
|
|
1115
|
+
name: fromFkey.options?.name,
|
|
1116
|
+
dropMode: fromFkey.options?.dropMode,
|
|
1117
|
+
references: {
|
|
1118
|
+
columns: [name],
|
|
1119
|
+
...fromFkey,
|
|
1205
1120
|
foreignColumns: snakeCase ? fromFkey.foreignColumns.map(toSnakeCase) : fromFkey.foreignColumns
|
|
1206
|
-
}
|
|
1121
|
+
}
|
|
1207
1122
|
});
|
|
1208
1123
|
}
|
|
1209
1124
|
if (toFkey) {
|
|
1210
1125
|
addConstraints.push({
|
|
1211
|
-
name:
|
|
1212
|
-
dropMode:
|
|
1213
|
-
references:
|
|
1214
|
-
columns: [name]
|
|
1215
|
-
|
|
1126
|
+
name: toFkey.options?.name,
|
|
1127
|
+
dropMode: toFkey.options?.dropMode,
|
|
1128
|
+
references: {
|
|
1129
|
+
columns: [name],
|
|
1130
|
+
...toFkey,
|
|
1216
1131
|
foreignColumns: snakeCase ? toFkey.foreignColumns.map(toSnakeCase) : toFkey.foreignColumns
|
|
1217
|
-
}
|
|
1132
|
+
}
|
|
1218
1133
|
});
|
|
1219
1134
|
}
|
|
1220
1135
|
}
|
|
1221
1136
|
}
|
|
1222
1137
|
const indexesLen = Math.max(
|
|
1223
|
-
|
|
1224
|
-
|
|
1138
|
+
from.indexes?.length || 0,
|
|
1139
|
+
to.indexes?.length || 0
|
|
1225
1140
|
);
|
|
1226
1141
|
for (let i = 0; i < indexesLen; i++) {
|
|
1227
|
-
const fromIndex =
|
|
1228
|
-
const toIndex =
|
|
1142
|
+
const fromIndex = from.indexes?.[i];
|
|
1143
|
+
const toIndex = to.indexes?.[i];
|
|
1229
1144
|
if ((fromIndex || toIndex) && (!fromIndex || !toIndex || !deepCompare(fromIndex, toIndex))) {
|
|
1230
1145
|
if (fromIndex) {
|
|
1231
|
-
dropIndexes.push(
|
|
1146
|
+
dropIndexes.push({
|
|
1147
|
+
...fromIndex,
|
|
1232
1148
|
columns: [
|
|
1233
|
-
|
|
1234
|
-
column: name
|
|
1235
|
-
|
|
1149
|
+
{
|
|
1150
|
+
column: name,
|
|
1151
|
+
...fromIndex.options
|
|
1152
|
+
}
|
|
1236
1153
|
]
|
|
1237
|
-
})
|
|
1154
|
+
});
|
|
1238
1155
|
}
|
|
1239
1156
|
if (toIndex) {
|
|
1240
|
-
addIndexes.push(
|
|
1157
|
+
addIndexes.push({
|
|
1158
|
+
...toIndex,
|
|
1241
1159
|
columns: [
|
|
1242
|
-
|
|
1243
|
-
column: name
|
|
1244
|
-
|
|
1160
|
+
{
|
|
1161
|
+
column: name,
|
|
1162
|
+
...toIndex.options
|
|
1163
|
+
}
|
|
1245
1164
|
]
|
|
1246
|
-
})
|
|
1165
|
+
});
|
|
1247
1166
|
}
|
|
1248
1167
|
}
|
|
1249
1168
|
}
|
|
@@ -1291,36 +1210,29 @@ const astToQuery = (ast) => {
|
|
|
1291
1210
|
const { options } = ast;
|
|
1292
1211
|
if (ast.action === "create") {
|
|
1293
1212
|
sql.push("CREATE");
|
|
1294
|
-
if (options
|
|
1295
|
-
|
|
1296
|
-
if (options
|
|
1297
|
-
sql.push("TEMPORARY");
|
|
1298
|
-
if (options == null ? void 0 : options.recursive)
|
|
1299
|
-
sql.push("RECURSIVE");
|
|
1213
|
+
if (options?.createOrReplace) sql.push("OR REPLACE");
|
|
1214
|
+
if (options?.temporary) sql.push("TEMPORARY");
|
|
1215
|
+
if (options?.recursive) sql.push("RECURSIVE");
|
|
1300
1216
|
sql.push(`VIEW "${ast.name}"`);
|
|
1301
|
-
if (options
|
|
1217
|
+
if (options?.columns) {
|
|
1302
1218
|
sql.push(
|
|
1303
1219
|
`(${options.columns.map((column) => `"${column}"`).join(", ")})`
|
|
1304
1220
|
);
|
|
1305
1221
|
}
|
|
1306
|
-
if (options
|
|
1222
|
+
if (options?.with) {
|
|
1307
1223
|
const list = [];
|
|
1308
1224
|
if (options.with.checkOption)
|
|
1309
1225
|
list.push(`check_option = ${singleQuote(options.with.checkOption)}`);
|
|
1310
|
-
if (options.with.securityBarrier)
|
|
1311
|
-
|
|
1312
|
-
if (options.with.securityInvoker)
|
|
1313
|
-
list.push(`security_invoker = true`);
|
|
1226
|
+
if (options.with.securityBarrier) list.push(`security_barrier = true`);
|
|
1227
|
+
if (options.with.securityInvoker) list.push(`security_invoker = true`);
|
|
1314
1228
|
sql.push(`WITH ( ${list.join(", ")} )`);
|
|
1315
1229
|
}
|
|
1316
1230
|
sql.push(`AS (${ast.sql.toSQL({ values })})`);
|
|
1317
1231
|
} else {
|
|
1318
1232
|
sql.push("DROP VIEW");
|
|
1319
|
-
if (options
|
|
1320
|
-
sql.push(`IF EXISTS`);
|
|
1233
|
+
if (options?.dropIfExists) sql.push(`IF EXISTS`);
|
|
1321
1234
|
sql.push(`"${ast.name}"`);
|
|
1322
|
-
if (options
|
|
1323
|
-
sql.push(options.dropMode);
|
|
1235
|
+
if (options?.dropMode) sql.push(options.dropMode);
|
|
1324
1236
|
}
|
|
1325
1237
|
return {
|
|
1326
1238
|
text: sql.join(" "),
|
|
@@ -1328,33 +1240,13 @@ const astToQuery = (ast) => {
|
|
|
1328
1240
|
};
|
|
1329
1241
|
};
|
|
1330
1242
|
|
|
1331
|
-
var __defProp$6 = Object.defineProperty;
|
|
1332
|
-
var __defProps$4 = Object.defineProperties;
|
|
1333
|
-
var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
|
|
1334
|
-
var __getOwnPropSymbols$6 = Object.getOwnPropertySymbols;
|
|
1335
|
-
var __hasOwnProp$6 = Object.prototype.hasOwnProperty;
|
|
1336
|
-
var __propIsEnum$6 = Object.prototype.propertyIsEnumerable;
|
|
1337
|
-
var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1338
|
-
var __spreadValues$6 = (a, b) => {
|
|
1339
|
-
for (var prop in b || (b = {}))
|
|
1340
|
-
if (__hasOwnProp$6.call(b, prop))
|
|
1341
|
-
__defNormalProp$6(a, prop, b[prop]);
|
|
1342
|
-
if (__getOwnPropSymbols$6)
|
|
1343
|
-
for (var prop of __getOwnPropSymbols$6(b)) {
|
|
1344
|
-
if (__propIsEnum$6.call(b, prop))
|
|
1345
|
-
__defNormalProp$6(a, prop, b[prop]);
|
|
1346
|
-
}
|
|
1347
|
-
return a;
|
|
1348
|
-
};
|
|
1349
|
-
var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
|
|
1350
1243
|
const createMigrationInterface = (tx, up, config) => {
|
|
1351
|
-
var _a;
|
|
1352
1244
|
const adapter = new TransactionAdapter(
|
|
1353
1245
|
tx,
|
|
1354
1246
|
tx.client,
|
|
1355
1247
|
tx.types
|
|
1356
1248
|
);
|
|
1357
|
-
adapter.schema =
|
|
1249
|
+
adapter.schema = adapter.adapter.schema ?? "public";
|
|
1358
1250
|
const { query, arrays } = adapter;
|
|
1359
1251
|
const log = logParamToLogObject(config.logger || console, config.log);
|
|
1360
1252
|
adapter.query = (q, types) => {
|
|
@@ -2208,7 +2100,7 @@ const wrapWithLog = async (log, query, fn) => {
|
|
|
2208
2100
|
if (!log) {
|
|
2209
2101
|
return fn();
|
|
2210
2102
|
} else {
|
|
2211
|
-
const sql = typeof query === "string" ? { text: query, values: [] } : query.values ? query :
|
|
2103
|
+
const sql = typeof query === "string" ? { text: query, values: [] } : query.values ? query : { ...query, values: [] };
|
|
2212
2104
|
const logData = log.beforeQuery(sql);
|
|
2213
2105
|
try {
|
|
2214
2106
|
const result = await fn();
|
|
@@ -2226,16 +2118,24 @@ const addColumn = (migration, up, tableName, columnName, fn) => {
|
|
|
2226
2118
|
}));
|
|
2227
2119
|
};
|
|
2228
2120
|
const addIndex = (migration, up, tableName, columns, args) => {
|
|
2229
|
-
return changeTable(migration, up, tableName, {}, (t) =>
|
|
2121
|
+
return changeTable(migration, up, tableName, {}, (t) => ({
|
|
2122
|
+
...t.add(t.index(columns, ...args))
|
|
2123
|
+
}));
|
|
2230
2124
|
};
|
|
2231
2125
|
const addForeignKey = (migration, up, tableName, columns, foreignTable, foreignColumns, options) => {
|
|
2232
|
-
return changeTable(migration, up, tableName, {}, (t) =>
|
|
2126
|
+
return changeTable(migration, up, tableName, {}, (t) => ({
|
|
2127
|
+
...t.add(t.foreignKey(columns, foreignTable, foreignColumns, options))
|
|
2128
|
+
}));
|
|
2233
2129
|
};
|
|
2234
2130
|
const addPrimaryKey = (migration, up, tableName, columns, name) => {
|
|
2235
|
-
return changeTable(migration, up, tableName, {}, (t) =>
|
|
2131
|
+
return changeTable(migration, up, tableName, {}, (t) => ({
|
|
2132
|
+
...t.add(t.primaryKey(columns, name))
|
|
2133
|
+
}));
|
|
2236
2134
|
};
|
|
2237
2135
|
const addCheck = (migration, up, tableName, check) => {
|
|
2238
|
-
return changeTable(migration, up, tableName, {}, (t) =>
|
|
2136
|
+
return changeTable(migration, up, tableName, {}, (t) => ({
|
|
2137
|
+
...t.add(t.check(check))
|
|
2138
|
+
}));
|
|
2239
2139
|
};
|
|
2240
2140
|
const createSchema = async (migration, up, name) => {
|
|
2241
2141
|
const ast = {
|
|
@@ -2249,12 +2149,13 @@ const createSchema = async (migration, up, name) => {
|
|
|
2249
2149
|
};
|
|
2250
2150
|
const createExtension = async (migration, up, fullName, options) => {
|
|
2251
2151
|
const [schema, name] = getSchemaAndTableFromName(fullName);
|
|
2252
|
-
const ast =
|
|
2152
|
+
const ast = {
|
|
2253
2153
|
type: "extension",
|
|
2254
2154
|
action: up ? "create" : "drop",
|
|
2255
2155
|
schema,
|
|
2256
|
-
name
|
|
2257
|
-
|
|
2156
|
+
name,
|
|
2157
|
+
...options
|
|
2158
|
+
};
|
|
2258
2159
|
let query;
|
|
2259
2160
|
if (ast.action === "drop") {
|
|
2260
2161
|
query = `DROP EXTENSION${ast.dropIfExists ? " IF EXISTS" : ""} "${ast.name}"${ast.cascade ? " CASCADE" : ""}`;
|
|
@@ -2265,13 +2166,14 @@ const createExtension = async (migration, up, fullName, options) => {
|
|
|
2265
2166
|
};
|
|
2266
2167
|
const createEnum = async (migration, up, name, values, options = {}) => {
|
|
2267
2168
|
const [schema, enumName] = getSchemaAndTableFromName(name);
|
|
2268
|
-
const ast =
|
|
2169
|
+
const ast = {
|
|
2269
2170
|
type: "enum",
|
|
2270
2171
|
action: up ? "create" : "drop",
|
|
2271
2172
|
schema,
|
|
2272
2173
|
name: enumName,
|
|
2273
|
-
values
|
|
2274
|
-
|
|
2174
|
+
values,
|
|
2175
|
+
...options
|
|
2176
|
+
};
|
|
2275
2177
|
let query;
|
|
2276
2178
|
const quotedName = quoteWithSchema(ast);
|
|
2277
2179
|
if (ast.action === "create") {
|
|
@@ -2313,12 +2215,13 @@ DEFAULT ${encodeColumnDefault(column.data.default, values)}` : ""}${!column.data
|
|
|
2313
2215
|
};
|
|
2314
2216
|
const createCollation = async (migration, up, name, options) => {
|
|
2315
2217
|
const [schema, collationName] = getSchemaAndTableFromName(name);
|
|
2316
|
-
const ast =
|
|
2218
|
+
const ast = {
|
|
2317
2219
|
type: "collation",
|
|
2318
2220
|
action: up ? "create" : "drop",
|
|
2319
2221
|
schema,
|
|
2320
|
-
name: collationName
|
|
2321
|
-
|
|
2222
|
+
name: collationName,
|
|
2223
|
+
...options
|
|
2224
|
+
};
|
|
2322
2225
|
let query;
|
|
2323
2226
|
const quotedName = quoteWithSchema(ast);
|
|
2324
2227
|
if (ast.action === "create") {
|
|
@@ -2327,18 +2230,13 @@ const createCollation = async (migration, up, name, options) => {
|
|
|
2327
2230
|
query += `FROM ${quoteNameFromString(ast.fromExisting)}`;
|
|
2328
2231
|
} else {
|
|
2329
2232
|
const config = [];
|
|
2330
|
-
if (ast.locale)
|
|
2331
|
-
|
|
2332
|
-
if (ast.
|
|
2333
|
-
|
|
2334
|
-
if (ast.lcCType)
|
|
2335
|
-
config.push(`lc_ctype = '${ast.lcCType}'`);
|
|
2336
|
-
if (ast.provider)
|
|
2337
|
-
config.push(`provider = ${ast.provider}`);
|
|
2233
|
+
if (ast.locale) config.push(`locale = '${ast.locale}'`);
|
|
2234
|
+
if (ast.lcCollate) config.push(`lc_collate = '${ast.lcCollate}'`);
|
|
2235
|
+
if (ast.lcCType) config.push(`lc_ctype = '${ast.lcCType}'`);
|
|
2236
|
+
if (ast.provider) config.push(`provider = ${ast.provider}`);
|
|
2338
2237
|
if (ast.deterministic !== void 0)
|
|
2339
2238
|
config.push(`deterministic = ${ast.deterministic}`);
|
|
2340
|
-
if (ast.version)
|
|
2341
|
-
config.push(`version = '${ast.version}'`);
|
|
2239
|
+
if (ast.version) config.push(`version = '${ast.version}'`);
|
|
2342
2240
|
query += `(
|
|
2343
2241
|
${config.join(",\n ")}
|
|
2344
2242
|
)`;
|
|
@@ -2352,7 +2250,6 @@ const queryExists = (db, sql) => {
|
|
|
2352
2250
|
return db.adapter.query(sql).then(({ rowCount }) => rowCount > 0);
|
|
2353
2251
|
};
|
|
2354
2252
|
const renameType = async (migration, from, to, kind) => {
|
|
2355
|
-
var _a;
|
|
2356
2253
|
const [fromSchema, f] = getSchemaAndTableFromName(migration.up ? from : to);
|
|
2357
2254
|
const [toSchema, t] = getSchemaAndTableFromName(migration.up ? to : from);
|
|
2358
2255
|
const ast = {
|
|
@@ -2370,7 +2267,7 @@ const renameType = async (migration, from, to, kind) => {
|
|
|
2370
2267
|
}
|
|
2371
2268
|
if (ast.fromSchema !== ast.toSchema) {
|
|
2372
2269
|
await migration.adapter.query(
|
|
2373
|
-
`ALTER ${ast.kind} ${quoteTable(ast.fromSchema, ast.to)} SET SCHEMA "${
|
|
2270
|
+
`ALTER ${ast.kind} ${quoteTable(ast.fromSchema, ast.to)} SET SCHEMA "${ast.toSchema ?? migration.adapter.schema}"`
|
|
2374
2271
|
);
|
|
2375
2272
|
}
|
|
2376
2273
|
};
|
|
@@ -2385,7 +2282,6 @@ const renameTableItem = async (migration, tableName, from, to, kind) => {
|
|
|
2385
2282
|
);
|
|
2386
2283
|
};
|
|
2387
2284
|
const addOrDropEnumValues = async (migration, up, enumName, values, options) => {
|
|
2388
|
-
var _a;
|
|
2389
2285
|
const [schema, name] = getSchemaAndTableFromName(enumName);
|
|
2390
2286
|
const quotedName = quoteTable(schema, name);
|
|
2391
2287
|
const ast = {
|
|
@@ -2394,9 +2290,9 @@ const addOrDropEnumValues = async (migration, up, enumName, values, options) =>
|
|
|
2394
2290
|
schema,
|
|
2395
2291
|
name,
|
|
2396
2292
|
values,
|
|
2397
|
-
place:
|
|
2398
|
-
relativeTo:
|
|
2399
|
-
ifNotExists: options
|
|
2293
|
+
place: options?.before ? "before" : options?.after ? "after" : void 0,
|
|
2294
|
+
relativeTo: options?.before ?? options?.after,
|
|
2295
|
+
ifNotExists: options?.ifNotExists
|
|
2400
2296
|
};
|
|
2401
2297
|
if (ast.action === "add") {
|
|
2402
2298
|
await Promise.all(
|
|
@@ -2456,7 +2352,7 @@ JOIN pg_catalog.pg_namespace n ON n.oid = relnamespace
|
|
|
2456
2352
|
JOIN pg_attribute a ON a.attrelid = c.oid
|
|
2457
2353
|
JOIN pg_type t ON a.atttypid = t.oid AND t.typname = ${singleQuote(name)}
|
|
2458
2354
|
JOIN pg_namespace tn ON tn.oid = t.typnamespace AND tn.nspname = ${singleQuote(
|
|
2459
|
-
schema
|
|
2355
|
+
schema ?? defaultSchema
|
|
2460
2356
|
)}
|
|
2461
2357
|
GROUP BY n.nspname, c.relname`
|
|
2462
2358
|
);
|
|
@@ -2488,7 +2384,6 @@ GROUP BY n.nspname, c.relname`
|
|
|
2488
2384
|
};
|
|
2489
2385
|
|
|
2490
2386
|
const writeMigrationFile = async (config, version, name, migrationCode) => {
|
|
2491
|
-
var _a;
|
|
2492
2387
|
await mkdir(config.migrationsPath, { recursive: true });
|
|
2493
2388
|
const filePath = path.resolve(config.migrationsPath, `${version}_${name}.ts`);
|
|
2494
2389
|
const importPath = getImportPath(
|
|
@@ -2500,11 +2395,10 @@ const writeMigrationFile = async (config, version, name, migrationCode) => {
|
|
|
2500
2395
|
`import { change } from '${importPath}';
|
|
2501
2396
|
${migrationCode}`
|
|
2502
2397
|
);
|
|
2503
|
-
|
|
2398
|
+
config.logger?.log(`Created ${pathToLog(filePath)}`);
|
|
2504
2399
|
};
|
|
2505
2400
|
const newMigration = async (config, [name]) => {
|
|
2506
|
-
if (!name)
|
|
2507
|
-
throw new Error("Migration name is missing");
|
|
2401
|
+
if (!name) throw new Error("Migration name is missing");
|
|
2508
2402
|
const version = await makeFileVersion({}, config);
|
|
2509
2403
|
await writeMigrationFile(config, version, name, makeContent(name));
|
|
2510
2404
|
};
|
|
@@ -2563,15 +2457,13 @@ const fileNamesToChangeMigrationIdMap = Object.fromEntries(
|
|
|
2563
2457
|
Object.entries(fileNamesToChangeMigrationId).map(([_, name]) => [name, true])
|
|
2564
2458
|
);
|
|
2565
2459
|
const changeIds = async (options, config, [arg, digitsArg]) => {
|
|
2566
|
-
var _a, _b, _c, _d;
|
|
2567
2460
|
if (arg !== "serial" && arg !== "timestamp") {
|
|
2568
2461
|
throw new Error(
|
|
2569
2462
|
`Pass "serial" or "timestamp" argument to the "change-ids" command`
|
|
2570
2463
|
);
|
|
2571
2464
|
}
|
|
2572
2465
|
let digits = digitsArg && parseInt(digitsArg);
|
|
2573
|
-
if (!digits || isNaN(digits))
|
|
2574
|
-
digits = 4;
|
|
2466
|
+
if (!digits || isNaN(digits)) digits = 4;
|
|
2575
2467
|
const data = await getMigrations({}, config, true, false, (_, filePath) => {
|
|
2576
2468
|
const fileName = path.basename(filePath);
|
|
2577
2469
|
const match = fileName.match(/^(\d+)\D/);
|
|
@@ -2584,7 +2476,7 @@ const changeIds = async (options, config, [arg, digitsArg]) => {
|
|
|
2584
2476
|
});
|
|
2585
2477
|
if (data.renameTo) {
|
|
2586
2478
|
if (arg === "serial" && typeof data.renameTo.to === "object" && digits === data.renameTo.to.serial || arg === "timestamp" && data.renameTo.to === "timestamp") {
|
|
2587
|
-
|
|
2479
|
+
config.logger?.log(
|
|
2588
2480
|
config.migrations ? "`renameMigrations` setting is already set" : `${fileNamesToChangeMigrationId[arg]} already exists`
|
|
2589
2481
|
);
|
|
2590
2482
|
return;
|
|
@@ -2604,7 +2496,7 @@ const changeIds = async (options, config, [arg, digitsArg]) => {
|
|
|
2604
2496
|
);
|
|
2605
2497
|
if (config.migrations) {
|
|
2606
2498
|
const to = arg === "timestamp" ? `'${arg}'` : `{ serial: ${digits} }`;
|
|
2607
|
-
|
|
2499
|
+
config.logger?.log(
|
|
2608
2500
|
`Save the following settings into your rake-db config under the \`migrations\` setting, it will instruct rake-db to rename migration entries during the next deploy:
|
|
2609
2501
|
${arg !== "serial" || digits !== 4 ? `
|
|
2610
2502
|
migrationId: ${to},` : ""}
|
|
@@ -2622,16 +2514,14 @@ renameMigrations: {
|
|
|
2622
2514
|
const values = data.migrations.map(
|
|
2623
2515
|
(item, i) => {
|
|
2624
2516
|
let newVersion = String(version + i);
|
|
2625
|
-
if (arg === "serial")
|
|
2626
|
-
newVersion = newVersion.padStart(digits, "0");
|
|
2517
|
+
if (arg === "serial") newVersion = newVersion.padStart(digits, "0");
|
|
2627
2518
|
const name = path.basename(item.path).slice(item.version.length + 1);
|
|
2628
2519
|
return [item.version, name, newVersion];
|
|
2629
2520
|
}
|
|
2630
2521
|
);
|
|
2631
|
-
if (!values.length)
|
|
2632
|
-
return;
|
|
2522
|
+
if (!values.length) return;
|
|
2633
2523
|
if (config.migrations) {
|
|
2634
|
-
|
|
2524
|
+
config.logger?.log(
|
|
2635
2525
|
`If your migrations are stored in files, navigate to migrations directory and run the following commands to rename them:
|
|
2636
2526
|
|
|
2637
2527
|
${values.map(
|
|
@@ -2659,7 +2549,7 @@ After setting \`renameMigrations\` (see above) and renaming the files, run the d
|
|
|
2659
2549
|
() => adapter.close()
|
|
2660
2550
|
);
|
|
2661
2551
|
});
|
|
2662
|
-
|
|
2552
|
+
config.logger?.log(
|
|
2663
2553
|
`Migration files were renamed, a config file ${fileNamesToChangeMigrationId[arg]} for renaming migrations after deploy was created, and migrations in local db were renamed successfully.
|
|
2664
2554
|
|
|
2665
2555
|
${arg === "timestamp" || digits !== 4 ? `Set \`migrationId\`: ${arg === "timestamp" ? `'timestamp'` : `{ serial: ${digits} }`}` : `Remove \`migrationId\``} setting in the rake-db config`
|
|
@@ -2676,32 +2566,12 @@ const renameMigrationVersionsInDb = async (config, adapter, values) => {
|
|
|
2676
2566
|
});
|
|
2677
2567
|
};
|
|
2678
2568
|
|
|
2679
|
-
var __defProp$5 = Object.defineProperty;
|
|
2680
|
-
var __defProps$3 = Object.defineProperties;
|
|
2681
|
-
var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
|
|
2682
|
-
var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
|
|
2683
|
-
var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
|
|
2684
|
-
var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
|
|
2685
|
-
var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
2686
|
-
var __spreadValues$5 = (a, b) => {
|
|
2687
|
-
for (var prop in b || (b = {}))
|
|
2688
|
-
if (__hasOwnProp$5.call(b, prop))
|
|
2689
|
-
__defNormalProp$5(a, prop, b[prop]);
|
|
2690
|
-
if (__getOwnPropSymbols$5)
|
|
2691
|
-
for (var prop of __getOwnPropSymbols$5(b)) {
|
|
2692
|
-
if (__propIsEnum$5.call(b, prop))
|
|
2693
|
-
__defNormalProp$5(a, prop, b[prop]);
|
|
2694
|
-
}
|
|
2695
|
-
return a;
|
|
2696
|
-
};
|
|
2697
|
-
var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
|
|
2698
2569
|
const getMigrations = async (ctx, config, up, allowDuplicates, getVersion = getMigrationVersionOrThrow) => {
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
__spreadProps$3(__spreadValues$5({}, config), { migrations: config.migrations }),
|
|
2570
|
+
return (ctx.migrationsPromise ?? (ctx.migrationsPromise = config.migrations ? getMigrationsFromConfig(
|
|
2571
|
+
{ ...config, migrations: config.migrations },
|
|
2702
2572
|
allowDuplicates,
|
|
2703
2573
|
getVersion
|
|
2704
|
-
) : getMigrationsFromFiles(config, allowDuplicates, getVersion)).then(
|
|
2574
|
+
) : getMigrationsFromFiles(config, allowDuplicates, getVersion))).then(
|
|
2705
2575
|
(data) => up ? data : {
|
|
2706
2576
|
renameTo: data.renameTo,
|
|
2707
2577
|
migrations: [...data.migrations].reverse()
|
|
@@ -2741,8 +2611,7 @@ async function getMigrationsFromFiles(config, allowDuplicates, getVersion = getM
|
|
|
2741
2611
|
const versions = {};
|
|
2742
2612
|
const result = entries.reduce(
|
|
2743
2613
|
(data, file) => {
|
|
2744
|
-
if (!file.isFile())
|
|
2745
|
-
return data;
|
|
2614
|
+
if (!file.isFile()) return data;
|
|
2746
2615
|
if (fileNamesToChangeMigrationIdMap[file.name]) {
|
|
2747
2616
|
if (data.renameTo) {
|
|
2748
2617
|
throw new Error(
|
|
@@ -2825,8 +2694,7 @@ function checkExt(filePath) {
|
|
|
2825
2694
|
function getMigrationVersionOrThrow(config, filePath) {
|
|
2826
2695
|
const name = path.basename(filePath);
|
|
2827
2696
|
const value = getMigrationVersion(config, name);
|
|
2828
|
-
if (value)
|
|
2829
|
-
return value;
|
|
2697
|
+
if (value) return value;
|
|
2830
2698
|
if (config.migrationId === "timestamp") {
|
|
2831
2699
|
throw new Error(
|
|
2832
2700
|
`Migration file name should start with 14 digit timestamp, received ${name}`
|
|
@@ -2840,12 +2708,10 @@ To keep using timestamp ids, set \`migrationId\` option of rake-db to 'timestamp
|
|
|
2840
2708
|
}
|
|
2841
2709
|
}
|
|
2842
2710
|
function getMigrationVersion(config, name) {
|
|
2843
|
-
|
|
2844
|
-
return (_a = config.migrationId === "timestamp" ? name.match(/^(\d{14})(_|\b)/) : name.match(/^(\d{4})(_|\b)/)) == null ? void 0 : _a[1];
|
|
2711
|
+
return (config.migrationId === "timestamp" ? name.match(/^(\d{14})(_|\b)/) : name.match(/^(\d{4})(_|\b)/))?.[1];
|
|
2845
2712
|
}
|
|
2846
2713
|
function getDigitsPrefix(name) {
|
|
2847
|
-
|
|
2848
|
-
const value = (_a = name.match(/^(\d+)\D/)) == null ? void 0 : _a[1];
|
|
2714
|
+
const value = name.match(/^(\d+)\D/)?.[1];
|
|
2849
2715
|
if (!value) {
|
|
2850
2716
|
throw new Error(
|
|
2851
2717
|
`Migration file should be prefixed with a serial number, received ${name}`
|
|
@@ -2926,8 +2792,7 @@ async function renameMigrations(config, trx, versions, renameTo) {
|
|
|
2926
2792
|
first = version;
|
|
2927
2793
|
break;
|
|
2928
2794
|
}
|
|
2929
|
-
if (!first || getMigrationVersion(config, first))
|
|
2930
|
-
return versions;
|
|
2795
|
+
if (!first || getMigrationVersion(config, first)) return versions;
|
|
2931
2796
|
const values = [];
|
|
2932
2797
|
const updatedVersions = {};
|
|
2933
2798
|
const data = await renameTo.map();
|
|
@@ -2951,12 +2816,11 @@ async function renameMigrations(config, trx, versions, renameTo) {
|
|
|
2951
2816
|
}
|
|
2952
2817
|
|
|
2953
2818
|
const createMigrationsTable = async (db, config) => {
|
|
2954
|
-
var _a, _b, _c;
|
|
2955
2819
|
const { schema } = db;
|
|
2956
2820
|
if (schema && schema !== "public") {
|
|
2957
2821
|
try {
|
|
2958
2822
|
await db.query(`CREATE SCHEMA "${schema}"`);
|
|
2959
|
-
|
|
2823
|
+
config.logger?.log(`Created schema ${schema}`);
|
|
2960
2824
|
} catch (err) {
|
|
2961
2825
|
if (err.code !== "42P06") {
|
|
2962
2826
|
throw err;
|
|
@@ -2967,10 +2831,10 @@ const createMigrationsTable = async (db, config) => {
|
|
|
2967
2831
|
await db.query(
|
|
2968
2832
|
`CREATE TABLE "${config.migrationsTable}" ( version TEXT NOT NULL, name TEXT NOT NULL )`
|
|
2969
2833
|
);
|
|
2970
|
-
|
|
2834
|
+
config.logger?.log("Created versions table");
|
|
2971
2835
|
} catch (err) {
|
|
2972
2836
|
if (err.code === "42P07") {
|
|
2973
|
-
|
|
2837
|
+
config.logger?.log("Versions table exists");
|
|
2974
2838
|
} else {
|
|
2975
2839
|
throw err;
|
|
2976
2840
|
}
|
|
@@ -2980,7 +2844,6 @@ const createMigrationsTable = async (db, config) => {
|
|
|
2980
2844
|
const RAKE_DB_LOCK_KEY = "8582141715823621641";
|
|
2981
2845
|
function makeMigrateFn(defaultCount, up, fn) {
|
|
2982
2846
|
return async (ctx, options, config, args = [], adapters = options.map((opts) => new Adapter(opts)), dontClose) => {
|
|
2983
|
-
var _a;
|
|
2984
2847
|
const set = await getMigrations(ctx, config, up);
|
|
2985
2848
|
const arg = args[0];
|
|
2986
2849
|
let force = arg === "force";
|
|
@@ -3011,7 +2874,7 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
3011
2874
|
conf,
|
|
3012
2875
|
set,
|
|
3013
2876
|
versions,
|
|
3014
|
-
count
|
|
2877
|
+
count ?? defaultCount,
|
|
3015
2878
|
force
|
|
3016
2879
|
);
|
|
3017
2880
|
});
|
|
@@ -3031,7 +2894,7 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
3031
2894
|
config2,
|
|
3032
2895
|
set,
|
|
3033
2896
|
versions,
|
|
3034
|
-
count
|
|
2897
|
+
count ?? defaultCount,
|
|
3035
2898
|
force
|
|
3036
2899
|
);
|
|
3037
2900
|
});
|
|
@@ -3039,10 +2902,9 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
3039
2902
|
throw err;
|
|
3040
2903
|
}
|
|
3041
2904
|
} finally {
|
|
3042
|
-
if (!dontClose)
|
|
3043
|
-
await adapter.close();
|
|
2905
|
+
if (!dontClose) await adapter.close();
|
|
3044
2906
|
}
|
|
3045
|
-
|
|
2907
|
+
config.afterChangeCommit?.({
|
|
3046
2908
|
options: opts,
|
|
3047
2909
|
up,
|
|
3048
2910
|
migrations
|
|
@@ -3083,7 +2945,6 @@ const redo = makeMigrateFn(
|
|
|
3083
2945
|
);
|
|
3084
2946
|
const getDb = (adapter) => createDb$1({ adapter });
|
|
3085
2947
|
const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, force, skipLock) => {
|
|
3086
|
-
var _a, _b, _c, _d;
|
|
3087
2948
|
const { sequence, map: versionsMap } = versions;
|
|
3088
2949
|
if (up) {
|
|
3089
2950
|
const rollbackTo = checkMigrationOrder(config, set, versions, force);
|
|
@@ -3095,8 +2956,7 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, f
|
|
|
3095
2956
|
break;
|
|
3096
2957
|
}
|
|
3097
2958
|
}
|
|
3098
|
-
if (i < 0)
|
|
3099
|
-
i = 0;
|
|
2959
|
+
if (i < 0) i = 0;
|
|
3100
2960
|
set.migrations.reverse();
|
|
3101
2961
|
await migrateOrRollback(
|
|
3102
2962
|
trx,
|
|
@@ -3110,15 +2970,14 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, f
|
|
|
3110
2970
|
set.migrations.reverse();
|
|
3111
2971
|
}
|
|
3112
2972
|
}
|
|
3113
|
-
if (!skipLock)
|
|
3114
|
-
await queryLock(trx);
|
|
2973
|
+
if (!skipLock) await queryLock(trx);
|
|
3115
2974
|
let db;
|
|
3116
2975
|
const beforeMigrate = config[up ? "beforeMigrate" : "beforeRollback"];
|
|
3117
2976
|
if (beforeMigrate || config.beforeChange) {
|
|
3118
|
-
db
|
|
2977
|
+
db ?? (db = getDb(trx));
|
|
3119
2978
|
const { migrations: migrations2 } = set;
|
|
3120
|
-
await
|
|
3121
|
-
await
|
|
2979
|
+
await beforeMigrate?.({ db, migrations: migrations2 });
|
|
2980
|
+
await config.beforeChange?.({ db, migrations: migrations2, up, redo: redo2 });
|
|
3122
2981
|
}
|
|
3123
2982
|
let loggedAboutStarting = false;
|
|
3124
2983
|
let migrations;
|
|
@@ -3126,17 +2985,16 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, f
|
|
|
3126
2985
|
if (up && versionsMap[file.version] || !up && !versionsMap[file.version]) {
|
|
3127
2986
|
continue;
|
|
3128
2987
|
}
|
|
3129
|
-
if (count-- <= 0)
|
|
3130
|
-
break;
|
|
2988
|
+
if (count-- <= 0) break;
|
|
3131
2989
|
if (!loggedAboutStarting && (!redo2 || !up)) {
|
|
3132
2990
|
loggedAboutStarting = true;
|
|
3133
|
-
|
|
2991
|
+
config.logger?.log(
|
|
3134
2992
|
`${redo2 ? "Reapplying migrations for" : up ? "Migrating" : "Rolling back"} database ${trx.config.connectionString ? new URL(trx.config.connectionString).pathname.slice(1) : trx.config.database}
|
|
3135
2993
|
`
|
|
3136
2994
|
);
|
|
3137
2995
|
}
|
|
3138
2996
|
await runMigration(trx, up, file, config);
|
|
3139
|
-
(migrations
|
|
2997
|
+
(migrations ?? (migrations = [])).push(file);
|
|
3140
2998
|
if (up) {
|
|
3141
2999
|
const name = path.basename(file.path);
|
|
3142
3000
|
versionsMap[file.version] = name;
|
|
@@ -3145,17 +3003,17 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo2, f
|
|
|
3145
3003
|
versionsMap[file.version] = void 0;
|
|
3146
3004
|
sequence.pop();
|
|
3147
3005
|
}
|
|
3148
|
-
|
|
3006
|
+
config.logger?.log(
|
|
3149
3007
|
`${up ? "Migrated" : "Rolled back"} ${pathToLog(file.path)}
|
|
3150
3008
|
`
|
|
3151
3009
|
);
|
|
3152
3010
|
}
|
|
3153
|
-
migrations
|
|
3011
|
+
migrations ?? (migrations = emptyArray);
|
|
3154
3012
|
const afterMigrate = config[up ? "afterMigrate" : "afterRollback"];
|
|
3155
3013
|
if (config.afterChange || afterMigrate) {
|
|
3156
|
-
db
|
|
3157
|
-
await
|
|
3158
|
-
await
|
|
3014
|
+
db ?? (db = getDb(trx));
|
|
3015
|
+
await config.afterChange?.({ db, up, redo: redo2, migrations });
|
|
3016
|
+
await afterMigrate?.({ db, migrations });
|
|
3159
3017
|
}
|
|
3160
3018
|
return migrations;
|
|
3161
3019
|
};
|
|
@@ -3164,8 +3022,7 @@ const checkMigrationOrder = (config, set, { sequence, map }, force) => {
|
|
|
3164
3022
|
if (last) {
|
|
3165
3023
|
for (const file of set.migrations) {
|
|
3166
3024
|
const version = +file.version;
|
|
3167
|
-
if (version > last || map[file.version])
|
|
3168
|
-
continue;
|
|
3025
|
+
if (version > last || map[file.version]) continue;
|
|
3169
3026
|
if (!force) {
|
|
3170
3027
|
throw new Error(
|
|
3171
3028
|
`Cannot migrate ${path.basename(
|
|
@@ -3185,7 +3042,7 @@ const runMigration = async (trx, up, file, config) => {
|
|
|
3185
3042
|
let changes = changeCache[file.path];
|
|
3186
3043
|
if (!changes) {
|
|
3187
3044
|
const module = await file.load();
|
|
3188
|
-
const exported =
|
|
3045
|
+
const exported = module?.default && toArray(module.default);
|
|
3189
3046
|
if (config.forceDefaultExports && !exported) {
|
|
3190
3047
|
throw new RakeDbError(
|
|
3191
3048
|
`Missing a default export in ${file.path} migration`
|
|
@@ -3211,22 +3068,6 @@ const runMigration = async (trx, up, file, config) => {
|
|
|
3211
3068
|
);
|
|
3212
3069
|
};
|
|
3213
3070
|
|
|
3214
|
-
var __defProp$4 = Object.defineProperty;
|
|
3215
|
-
var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
|
|
3216
|
-
var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
|
|
3217
|
-
var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
|
|
3218
|
-
var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3219
|
-
var __spreadValues$4 = (a, b) => {
|
|
3220
|
-
for (var prop in b || (b = {}))
|
|
3221
|
-
if (__hasOwnProp$4.call(b, prop))
|
|
3222
|
-
__defNormalProp$4(a, prop, b[prop]);
|
|
3223
|
-
if (__getOwnPropSymbols$4)
|
|
3224
|
-
for (var prop of __getOwnPropSymbols$4(b)) {
|
|
3225
|
-
if (__propIsEnum$4.call(b, prop))
|
|
3226
|
-
__defNormalProp$4(a, prop, b[prop]);
|
|
3227
|
-
}
|
|
3228
|
-
return a;
|
|
3229
|
-
};
|
|
3230
3071
|
const migrationConfigDefaults = {
|
|
3231
3072
|
schemaConfig: defaultSchemaConfig,
|
|
3232
3073
|
migrationsPath: path.join("src", "db", "migrations"),
|
|
@@ -3241,8 +3082,7 @@ const migrationConfigDefaults = {
|
|
|
3241
3082
|
}
|
|
3242
3083
|
};
|
|
3243
3084
|
const processRakeDbConfig = (config) => {
|
|
3244
|
-
|
|
3245
|
-
const result = __spreadValues$4(__spreadValues$4({}, migrationConfigDefaults), config);
|
|
3085
|
+
const result = { ...migrationConfigDefaults, ...config };
|
|
3246
3086
|
if (!result.recurrentPath) {
|
|
3247
3087
|
result.recurrentPath = path.join(result.migrationsPath, "recurrent");
|
|
3248
3088
|
}
|
|
@@ -3250,7 +3090,7 @@ const processRakeDbConfig = (config) => {
|
|
|
3250
3090
|
delete result.logger;
|
|
3251
3091
|
}
|
|
3252
3092
|
if (!result.basePath || !result.dbScript) {
|
|
3253
|
-
let filePath =
|
|
3093
|
+
let filePath = getStackTrace()?.[3].getFileName();
|
|
3254
3094
|
if (!filePath) {
|
|
3255
3095
|
throw new Error(
|
|
3256
3096
|
"Failed to determine path to db script. Please set basePath option of rakeDb"
|
|
@@ -3280,10 +3120,8 @@ const processRakeDbConfig = (config) => {
|
|
|
3280
3120
|
if ("baseTable" in config && config.baseTable) {
|
|
3281
3121
|
const { types, snakeCase, language } = config.baseTable.prototype;
|
|
3282
3122
|
result.columnTypes = types || makeColumnTypes(defaultSchemaConfig);
|
|
3283
|
-
if (snakeCase)
|
|
3284
|
-
|
|
3285
|
-
if (language)
|
|
3286
|
-
result.language = language;
|
|
3123
|
+
if (snakeCase) result.snakeCase = true;
|
|
3124
|
+
if (language) result.language = language;
|
|
3287
3125
|
} else {
|
|
3288
3126
|
const ct = "columnTypes" in config && config.columnTypes;
|
|
3289
3127
|
result.columnTypes = (typeof ct === "function" ? ct(
|
|
@@ -3354,11 +3192,9 @@ const prompt = async ({
|
|
|
3354
3192
|
cursor: showCursor
|
|
3355
3193
|
}) => {
|
|
3356
3194
|
stdin.resume();
|
|
3357
|
-
if (stdin.isTTY)
|
|
3358
|
-
stdin.setRawMode(true);
|
|
3195
|
+
if (stdin.isTTY) stdin.setRawMode(true);
|
|
3359
3196
|
stdin.setEncoding("utf-8");
|
|
3360
|
-
if (!showCursor)
|
|
3361
|
-
stdout.write(cursorHide);
|
|
3197
|
+
if (!showCursor) stdout.write(cursorHide);
|
|
3362
3198
|
return new Promise((res) => {
|
|
3363
3199
|
let prevText;
|
|
3364
3200
|
const ctx = {
|
|
@@ -3366,16 +3202,13 @@ const prompt = async ({
|
|
|
3366
3202
|
submitted: false,
|
|
3367
3203
|
render() {
|
|
3368
3204
|
let text = (ctx.submitted ? colors.greenBold("\u2714") : colors.yellowBold("?")) + " " + render(ctx);
|
|
3369
|
-
if (ctx.submitted)
|
|
3370
|
-
text += "\n";
|
|
3205
|
+
if (ctx.submitted) text += "\n";
|
|
3371
3206
|
stdout.write(prevText ? clear(prevText) + "\r" + text : text);
|
|
3372
3207
|
prevText = text;
|
|
3373
3208
|
},
|
|
3374
3209
|
submit(value2) {
|
|
3375
|
-
if (value2 !== void 0)
|
|
3376
|
-
|
|
3377
|
-
if (ctx.value === void 0 || validate && !(validate == null ? void 0 : validate(ctx)))
|
|
3378
|
-
return;
|
|
3210
|
+
if (value2 !== void 0) ctx.value = value2;
|
|
3211
|
+
if (ctx.value === void 0 || validate && !validate?.(ctx)) return;
|
|
3379
3212
|
ctx.submitted = true;
|
|
3380
3213
|
ctx.render();
|
|
3381
3214
|
close();
|
|
@@ -3383,16 +3216,14 @@ const prompt = async ({
|
|
|
3383
3216
|
}
|
|
3384
3217
|
};
|
|
3385
3218
|
const close = () => {
|
|
3386
|
-
if (!showCursor)
|
|
3387
|
-
|
|
3388
|
-
if (stdin.isTTY)
|
|
3389
|
-
stdin.setRawMode(false);
|
|
3219
|
+
if (!showCursor) stdout.write(cursorShow);
|
|
3220
|
+
if (stdin.isTTY) stdin.setRawMode(false);
|
|
3390
3221
|
stdin.off("data", keypress);
|
|
3391
3222
|
stdin.pause();
|
|
3392
3223
|
};
|
|
3393
3224
|
const keypress = (s) => {
|
|
3394
3225
|
if (s === "" || s === "") {
|
|
3395
|
-
close
|
|
3226
|
+
close?.();
|
|
3396
3227
|
process.exit(0);
|
|
3397
3228
|
}
|
|
3398
3229
|
if (s === "\r" || s === "\n" || s === "\r\n") {
|
|
@@ -3440,10 +3271,8 @@ ${ctx.submitted ? `> ${ctx.value ? colors.greenBold("Yes") : colors.yellowBold("
|
|
|
3440
3271
|
},
|
|
3441
3272
|
onKeyPress(ctx, s) {
|
|
3442
3273
|
let ok;
|
|
3443
|
-
if (s === "y" || s === "Y")
|
|
3444
|
-
|
|
3445
|
-
else if (s === "n" || s === "N")
|
|
3446
|
-
ok = false;
|
|
3274
|
+
if (s === "y" || s === "Y") ok = true;
|
|
3275
|
+
else if (s === "n" || s === "N") ok = false;
|
|
3447
3276
|
if (ok !== void 0) {
|
|
3448
3277
|
ctx.submit(ok);
|
|
3449
3278
|
}
|
|
@@ -3465,8 +3294,7 @@ const promptText = ({
|
|
|
3465
3294
|
render(ctx) {
|
|
3466
3295
|
let text = `${colors.bright(message)}
|
|
3467
3296
|
> ${ctx.submitted ? renderValue(ctx) : showDefault ? colors.pale(def) + "\b".repeat(def.length) : ctx.value}`;
|
|
3468
|
-
if (ctx.submitted)
|
|
3469
|
-
text += "\n";
|
|
3297
|
+
if (ctx.submitted) text += "\n";
|
|
3470
3298
|
return text;
|
|
3471
3299
|
},
|
|
3472
3300
|
onKeyPress(ctx, s) {
|
|
@@ -3478,8 +3306,7 @@ const promptText = ({
|
|
|
3478
3306
|
stdout.write(value[x]);
|
|
3479
3307
|
x++;
|
|
3480
3308
|
}
|
|
3481
|
-
if (s !== "\x7F" && s !== "\x1B[3~" && !visibleChars(s))
|
|
3482
|
-
return;
|
|
3309
|
+
if (s !== "\x7F" && s !== "\x1B[3~" && !visibleChars(s)) return;
|
|
3483
3310
|
if (showDefault) {
|
|
3484
3311
|
showDefault = false;
|
|
3485
3312
|
stdout.write(" ".repeat(def.length) + "\b".repeat(def.length));
|
|
@@ -3508,25 +3335,6 @@ const promptText = ({
|
|
|
3508
3335
|
});
|
|
3509
3336
|
};
|
|
3510
3337
|
|
|
3511
|
-
var __defProp$3 = Object.defineProperty;
|
|
3512
|
-
var __defProps$2 = Object.defineProperties;
|
|
3513
|
-
var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
|
|
3514
|
-
var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
|
|
3515
|
-
var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
|
|
3516
|
-
var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
|
|
3517
|
-
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3518
|
-
var __spreadValues$3 = (a, b) => {
|
|
3519
|
-
for (var prop in b || (b = {}))
|
|
3520
|
-
if (__hasOwnProp$3.call(b, prop))
|
|
3521
|
-
__defNormalProp$3(a, prop, b[prop]);
|
|
3522
|
-
if (__getOwnPropSymbols$3)
|
|
3523
|
-
for (var prop of __getOwnPropSymbols$3(b)) {
|
|
3524
|
-
if (__propIsEnum$3.call(b, prop))
|
|
3525
|
-
__defNormalProp$3(a, prop, b[prop]);
|
|
3526
|
-
}
|
|
3527
|
-
return a;
|
|
3528
|
-
};
|
|
3529
|
-
var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
|
|
3530
3338
|
const setAdapterOptions = (options, set) => {
|
|
3531
3339
|
if (options.databaseURL) {
|
|
3532
3340
|
const url = new URL(options.databaseURL);
|
|
@@ -3539,9 +3347,12 @@ const setAdapterOptions = (options, set) => {
|
|
|
3539
3347
|
if (set.password !== void 0) {
|
|
3540
3348
|
url.password = set.password;
|
|
3541
3349
|
}
|
|
3542
|
-
return
|
|
3350
|
+
return { ...options, databaseURL: url.toString() };
|
|
3543
3351
|
} else {
|
|
3544
|
-
return
|
|
3352
|
+
return {
|
|
3353
|
+
...options,
|
|
3354
|
+
...set
|
|
3355
|
+
};
|
|
3545
3356
|
}
|
|
3546
3357
|
};
|
|
3547
3358
|
const setAdminCredentialsToOptions = async (options, create) => {
|
|
@@ -3588,18 +3399,17 @@ const execute = async (options, sql) => {
|
|
|
3588
3399
|
}
|
|
3589
3400
|
};
|
|
3590
3401
|
const createOrDrop = async (options, adminOptions, config, args) => {
|
|
3591
|
-
var _a, _b, _c, _d;
|
|
3592
3402
|
const params = getDatabaseAndUserFromOptions(options);
|
|
3593
3403
|
const result = await execute(
|
|
3594
3404
|
setAdapterOptions(adminOptions, { database: "postgres" }),
|
|
3595
3405
|
args.sql(params)
|
|
3596
3406
|
);
|
|
3597
3407
|
if (result === "ok") {
|
|
3598
|
-
|
|
3408
|
+
config.logger?.log(args.successMessage(params));
|
|
3599
3409
|
} else if (result === "already") {
|
|
3600
|
-
|
|
3410
|
+
config.logger?.log(args.alreadyMessage(params));
|
|
3601
3411
|
} else if (result === "ssl required") {
|
|
3602
|
-
|
|
3412
|
+
config.logger?.log(
|
|
3603
3413
|
"SSL is required: append ?ssl=true to the database url string"
|
|
3604
3414
|
);
|
|
3605
3415
|
return;
|
|
@@ -3611,20 +3421,18 @@ const createOrDrop = async (options, adminOptions, config, args) => {
|
|
|
3611
3421
|
message += `
|
|
3612
3422
|
Don't use this command for database service providers, only for a local db.`;
|
|
3613
3423
|
}
|
|
3614
|
-
|
|
3424
|
+
config.logger?.log(message);
|
|
3615
3425
|
const updatedOptions = await setAdminCredentialsToOptions(
|
|
3616
3426
|
options,
|
|
3617
3427
|
args.create
|
|
3618
3428
|
);
|
|
3619
|
-
if (!updatedOptions)
|
|
3620
|
-
return;
|
|
3429
|
+
if (!updatedOptions) return;
|
|
3621
3430
|
await createOrDrop(options, updatedOptions, config, args);
|
|
3622
3431
|
return;
|
|
3623
3432
|
} else {
|
|
3624
3433
|
throw result.error;
|
|
3625
3434
|
}
|
|
3626
|
-
if (!args.create)
|
|
3627
|
-
return;
|
|
3435
|
+
if (!args.create) return;
|
|
3628
3436
|
const db = new Adapter(options);
|
|
3629
3437
|
await createMigrationsTable(db, config);
|
|
3630
3438
|
await db.close();
|
|
@@ -4046,7 +3854,6 @@ const sql = `SELECT (${schemasSql}) AS "schemas", ${jsonAgg(
|
|
|
4046
3854
|
"collations"
|
|
4047
3855
|
)}`;
|
|
4048
3856
|
async function introspectDbSchema(db) {
|
|
4049
|
-
var _a;
|
|
4050
3857
|
const data = await db.query(sql);
|
|
4051
3858
|
const result = data.rows[0];
|
|
4052
3859
|
for (const domain of result.domains) {
|
|
@@ -4055,8 +3862,7 @@ async function introspectDbSchema(db) {
|
|
|
4055
3862
|
for (const table of result.tables) {
|
|
4056
3863
|
for (const column of table.columns) {
|
|
4057
3864
|
nullsToUndefined(column);
|
|
4058
|
-
if (column.identity)
|
|
4059
|
-
nullsToUndefined(column.identity);
|
|
3865
|
+
if (column.identity) nullsToUndefined(column.identity);
|
|
4060
3866
|
if (column.compression) {
|
|
4061
3867
|
column.compression = column.compression === "p" ? "pglz" : "lz4";
|
|
4062
3868
|
}
|
|
@@ -4065,8 +3871,7 @@ async function introspectDbSchema(db) {
|
|
|
4065
3871
|
for (const index of result.indexes) {
|
|
4066
3872
|
nullsToUndefined(index);
|
|
4067
3873
|
for (const column of index.columns) {
|
|
4068
|
-
if (!("expression" in column))
|
|
4069
|
-
continue;
|
|
3874
|
+
if (!("expression" in column)) continue;
|
|
4070
3875
|
const s = column.expression;
|
|
4071
3876
|
const columnR = `"?\\w+"?`;
|
|
4072
3877
|
const langR = `(${columnR}|'\\w+'::regconfig)`;
|
|
@@ -4081,16 +3886,15 @@ async function introspectDbSchema(db) {
|
|
|
4081
3886
|
const match = s.match(
|
|
4082
3887
|
new RegExp(`^([\\(]*${setWeightOrTsVectorR}[\\)]*( ${concatR} )?)+$`)
|
|
4083
3888
|
);
|
|
4084
|
-
if (!match)
|
|
4085
|
-
continue;
|
|
3889
|
+
if (!match) continue;
|
|
4086
3890
|
let language;
|
|
4087
3891
|
let languageColumn;
|
|
4088
|
-
const tokens =
|
|
3892
|
+
const tokens = match[0].match(
|
|
4089
3893
|
new RegExp(
|
|
4090
3894
|
`setweight\\(|to_tsvector\\(${langR}|[:']?${columnR}\\(?`,
|
|
4091
3895
|
"g"
|
|
4092
3896
|
)
|
|
4093
|
-
)
|
|
3897
|
+
)?.reduce((acc, token) => {
|
|
4094
3898
|
if (token === "setweight(" || token === "COALESCE(" || token[0] === ":")
|
|
4095
3899
|
return acc;
|
|
4096
3900
|
if (token.startsWith("to_tsvector(")) {
|
|
@@ -4102,14 +3906,12 @@ async function introspectDbSchema(db) {
|
|
|
4102
3906
|
} else if (token[0] === "'") {
|
|
4103
3907
|
acc.push({ kind: "weight", value: token[1] });
|
|
4104
3908
|
} else {
|
|
4105
|
-
if (token[0] === '"')
|
|
4106
|
-
token = token.slice(1, -1);
|
|
3909
|
+
if (token[0] === '"') token = token.slice(1, -1);
|
|
4107
3910
|
acc.push({ kind: "column", value: token });
|
|
4108
3911
|
}
|
|
4109
3912
|
return acc;
|
|
4110
3913
|
}, []);
|
|
4111
|
-
if (!tokens)
|
|
4112
|
-
continue;
|
|
3914
|
+
if (!tokens) continue;
|
|
4113
3915
|
index.language = language;
|
|
4114
3916
|
index.languageColumn = languageColumn;
|
|
4115
3917
|
index.tsVector = true;
|
|
@@ -4134,37 +3936,6 @@ const nullsToUndefined = (obj) => {
|
|
|
4134
3936
|
}
|
|
4135
3937
|
};
|
|
4136
3938
|
|
|
4137
|
-
var __defProp$2 = Object.defineProperty;
|
|
4138
|
-
var __defProps$1 = Object.defineProperties;
|
|
4139
|
-
var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
|
|
4140
|
-
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
|
|
4141
|
-
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
|
|
4142
|
-
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
|
|
4143
|
-
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4144
|
-
var __spreadValues$2 = (a, b) => {
|
|
4145
|
-
for (var prop in b || (b = {}))
|
|
4146
|
-
if (__hasOwnProp$2.call(b, prop))
|
|
4147
|
-
__defNormalProp$2(a, prop, b[prop]);
|
|
4148
|
-
if (__getOwnPropSymbols$2)
|
|
4149
|
-
for (var prop of __getOwnPropSymbols$2(b)) {
|
|
4150
|
-
if (__propIsEnum$2.call(b, prop))
|
|
4151
|
-
__defNormalProp$2(a, prop, b[prop]);
|
|
4152
|
-
}
|
|
4153
|
-
return a;
|
|
4154
|
-
};
|
|
4155
|
-
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
4156
|
-
var __objRest = (source, exclude) => {
|
|
4157
|
-
var target = {};
|
|
4158
|
-
for (var prop in source)
|
|
4159
|
-
if (__hasOwnProp$2.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
4160
|
-
target[prop] = source[prop];
|
|
4161
|
-
if (source != null && __getOwnPropSymbols$2)
|
|
4162
|
-
for (var prop of __getOwnPropSymbols$2(source)) {
|
|
4163
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum$2.call(source, prop))
|
|
4164
|
-
target[prop] = source[prop];
|
|
4165
|
-
}
|
|
4166
|
-
return target;
|
|
4167
|
-
};
|
|
4168
3939
|
const matchMap = {
|
|
4169
3940
|
s: void 0,
|
|
4170
3941
|
f: "FULL",
|
|
@@ -4189,8 +3960,7 @@ const structureToAst = async (ctx, adapter, config) => {
|
|
|
4189
3960
|
const ast = [];
|
|
4190
3961
|
const data = await introspectDbSchema(adapter);
|
|
4191
3962
|
for (const name of data.schemas) {
|
|
4192
|
-
if (name === "public")
|
|
4193
|
-
continue;
|
|
3963
|
+
if (name === "public") continue;
|
|
4194
3964
|
ast.push({
|
|
4195
3965
|
type: "schema",
|
|
4196
3966
|
action: "create",
|
|
@@ -4198,17 +3968,16 @@ const structureToAst = async (ctx, adapter, config) => {
|
|
|
4198
3968
|
});
|
|
4199
3969
|
}
|
|
4200
3970
|
for (const it of data.collations) {
|
|
4201
|
-
ast.push(
|
|
3971
|
+
ast.push({
|
|
4202
3972
|
type: "collation",
|
|
4203
|
-
action: "create"
|
|
4204
|
-
|
|
3973
|
+
action: "create",
|
|
3974
|
+
...it,
|
|
4205
3975
|
schema: it.schemaName === ctx.currentSchema ? void 0 : it.schemaName
|
|
4206
|
-
})
|
|
3976
|
+
});
|
|
4207
3977
|
}
|
|
4208
3978
|
const domains = makeDomainsMap(ctx, data);
|
|
4209
3979
|
for (const table of data.tables) {
|
|
4210
|
-
if (table.name === config.migrationsTable)
|
|
4211
|
-
continue;
|
|
3980
|
+
if (table.name === config.migrationsTable) continue;
|
|
4212
3981
|
ast.push(tableToAst(ctx, data, table, "create", domains));
|
|
4213
3982
|
}
|
|
4214
3983
|
for (const it of data.extensions) {
|
|
@@ -4241,12 +4010,13 @@ const structureToAst = async (ctx, adapter, config) => {
|
|
|
4241
4010
|
for (const table of data.tables) {
|
|
4242
4011
|
for (const fkey of data.constraints) {
|
|
4243
4012
|
if (fkey.references && fkey.tableName === table.name && fkey.schemaName === table.schemaName && checkIfIsOuterRecursiveFkey(data, table, fkey.references)) {
|
|
4244
|
-
ast.push(
|
|
4013
|
+
ast.push({
|
|
4014
|
+
...constraintToAst(ctx, fkey),
|
|
4245
4015
|
type: "constraint",
|
|
4246
4016
|
action: "create",
|
|
4247
4017
|
tableSchema: table.schemaName === ctx.currentSchema ? void 0 : table.schemaName,
|
|
4248
4018
|
tableName: fkey.tableName
|
|
4249
|
-
})
|
|
4019
|
+
});
|
|
4250
4020
|
}
|
|
4251
4021
|
}
|
|
4252
4022
|
}
|
|
@@ -4290,10 +4060,10 @@ const getDbColumnIsSerial = (item) => {
|
|
|
4290
4060
|
return false;
|
|
4291
4061
|
};
|
|
4292
4062
|
const instantiateDbColumn = (ctx, data, domains, dbColumn) => {
|
|
4293
|
-
var _a, _b
|
|
4063
|
+
var _a, _b;
|
|
4294
4064
|
const isSerial = getDbColumnIsSerial(dbColumn);
|
|
4295
4065
|
if (isSerial) {
|
|
4296
|
-
dbColumn =
|
|
4066
|
+
dbColumn = { ...dbColumn, default: void 0 };
|
|
4297
4067
|
}
|
|
4298
4068
|
let column;
|
|
4299
4069
|
const col = instantiateColumnByDbType(ctx, dbColumn.type, isSerial, dbColumn);
|
|
@@ -4326,15 +4096,14 @@ const instantiateDbColumn = (ctx, data, domains, dbColumn) => {
|
|
|
4326
4096
|
typeId,
|
|
4327
4097
|
dbColumn.extension
|
|
4328
4098
|
);
|
|
4329
|
-
((
|
|
4099
|
+
((_a = ctx.unsupportedTypes)[_b = dbColumn.type] ?? (_a[_b] = [])).push(
|
|
4330
4100
|
`${dbColumn.schemaName}${dbColumn.tableName ? `.${dbColumn.tableName}` : ""}.${dbColumn.name}`
|
|
4331
4101
|
);
|
|
4332
4102
|
}
|
|
4333
4103
|
}
|
|
4334
4104
|
}
|
|
4335
4105
|
column.data.name = void 0;
|
|
4336
|
-
if (!column.data.isNullable)
|
|
4337
|
-
column.data.isNullable = void 0;
|
|
4106
|
+
if (!column.data.isNullable) column.data.isNullable = void 0;
|
|
4338
4107
|
if (dbColumn.arrayDims) {
|
|
4339
4108
|
const arr = new ArrayColumn(
|
|
4340
4109
|
ctx.columnSchemaConfig,
|
|
@@ -4366,28 +4135,28 @@ const tableToAst = (ctx, data, table, action, domains) => {
|
|
|
4366
4135
|
name: tableName,
|
|
4367
4136
|
shape: makeDbStructureColumnsShape(ctx, data, domains, table, tableData),
|
|
4368
4137
|
noPrimaryKey: tableData.primaryKey ? "error" : "ignore",
|
|
4369
|
-
primaryKey: primaryKey && primaryKey.columns.length > 1 ?
|
|
4138
|
+
primaryKey: primaryKey && primaryKey.columns.length > 1 ? { ...primaryKey, columns: primaryKey.columns.map(toCamelCase) } : void 0,
|
|
4370
4139
|
indexes: indexes.reduce((acc, index) => {
|
|
4371
|
-
var _b;
|
|
4372
4140
|
if (index.columns.length > 1 || index.columns.some((it) => "expression" in it)) {
|
|
4373
|
-
const
|
|
4141
|
+
const { name, ...options } = makeIndexOptions(tableName, index);
|
|
4374
4142
|
acc.push({
|
|
4375
|
-
columns: index.columns.map((it) =>
|
|
4143
|
+
columns: index.columns.map((it) => ({
|
|
4144
|
+
..."column" in it ? { column: toCamelCase(it.column) } : { expression: it.expression },
|
|
4376
4145
|
collate: it.collate,
|
|
4377
4146
|
opclass: it.opclass,
|
|
4378
4147
|
order: it.order
|
|
4379
4148
|
})),
|
|
4380
|
-
options:
|
|
4381
|
-
|
|
4382
|
-
|
|
4149
|
+
options: {
|
|
4150
|
+
...options,
|
|
4151
|
+
include: index.include?.map(toCamelCase)
|
|
4152
|
+
},
|
|
4383
4153
|
name
|
|
4384
4154
|
});
|
|
4385
4155
|
}
|
|
4386
4156
|
return acc;
|
|
4387
4157
|
}, []),
|
|
4388
4158
|
constraints: constraints.reduce((acc, it) => {
|
|
4389
|
-
|
|
4390
|
-
if (it.check && it.references || it.check && ((_a = it.check.columns) == null ? void 0 : _a.length) !== 1 || it.references && it.references.columns.length !== 1 && !checkIfIsOuterRecursiveFkey(data, table, it.references)) {
|
|
4159
|
+
if (it.check && it.references || it.check && it.check.columns?.length !== 1 || it.references && it.references.columns.length !== 1 && !checkIfIsOuterRecursiveFkey(data, table, it.references)) {
|
|
4391
4160
|
acc.push(dbConstraintToTableConstraint(ctx, table, it));
|
|
4392
4161
|
}
|
|
4393
4162
|
return acc;
|
|
@@ -4400,7 +4169,7 @@ const getDbStructureTableData = (data, { name, schemaName }) => {
|
|
|
4400
4169
|
);
|
|
4401
4170
|
const primaryKey = constraints.find((c) => c.primaryKey);
|
|
4402
4171
|
return {
|
|
4403
|
-
primaryKey:
|
|
4172
|
+
primaryKey: primaryKey?.primaryKey ? {
|
|
4404
4173
|
columns: primaryKey.primaryKey,
|
|
4405
4174
|
name: primaryKey.name === `${name}_pkey` ? void 0 : primaryKey.name
|
|
4406
4175
|
} : void 0,
|
|
@@ -4411,7 +4180,6 @@ const getDbStructureTableData = (data, { name, schemaName }) => {
|
|
|
4411
4180
|
};
|
|
4412
4181
|
};
|
|
4413
4182
|
const constraintToAst = (ctx, item) => {
|
|
4414
|
-
var _a;
|
|
4415
4183
|
const result = {};
|
|
4416
4184
|
const { references, check } = item;
|
|
4417
4185
|
if (references) {
|
|
@@ -4423,21 +4191,18 @@ const constraintToAst = (ctx, item) => {
|
|
|
4423
4191
|
options
|
|
4424
4192
|
};
|
|
4425
4193
|
const match = matchMap[references.match];
|
|
4426
|
-
if (match)
|
|
4427
|
-
options.match = match;
|
|
4194
|
+
if (match) options.match = match;
|
|
4428
4195
|
const onUpdate = fkeyActionMap[references.onUpdate];
|
|
4429
|
-
if (onUpdate)
|
|
4430
|
-
options.onUpdate = onUpdate;
|
|
4196
|
+
if (onUpdate) options.onUpdate = onUpdate;
|
|
4431
4197
|
const onDelete = fkeyActionMap[references.onDelete];
|
|
4432
|
-
if (onDelete)
|
|
4433
|
-
options.onDelete = onDelete;
|
|
4198
|
+
if (onDelete) options.onDelete = onDelete;
|
|
4434
4199
|
}
|
|
4435
4200
|
if (check) {
|
|
4436
4201
|
result.check = raw({ raw: check.expression });
|
|
4437
4202
|
}
|
|
4438
4203
|
if (item.name && item.name !== getConstraintName(item.tableName, result, ctx.snakeCase)) {
|
|
4439
4204
|
result.name = item.name;
|
|
4440
|
-
if (
|
|
4205
|
+
if (result.references?.options) {
|
|
4441
4206
|
result.references.options.name = item.name;
|
|
4442
4207
|
}
|
|
4443
4208
|
}
|
|
@@ -4447,14 +4212,12 @@ const getReferencesTable = (ctx, references) => {
|
|
|
4447
4212
|
return references.foreignSchema !== ctx.currentSchema ? `${references.foreignSchema}.${references.foreignTable}` : references.foreignTable;
|
|
4448
4213
|
};
|
|
4449
4214
|
const isColumnCheck = (it) => {
|
|
4450
|
-
|
|
4451
|
-
return !it.references && ((_b = (_a = it.check) == null ? void 0 : _a.columns) == null ? void 0 : _b.length) === 1;
|
|
4215
|
+
return !it.references && it.check?.columns?.length === 1;
|
|
4452
4216
|
};
|
|
4453
4217
|
const viewToAst = (ctx, data, domains, view) => {
|
|
4454
4218
|
const shape = makeDbStructureColumnsShape(ctx, data, domains, view);
|
|
4455
4219
|
const options = {};
|
|
4456
|
-
if (view.isRecursive)
|
|
4457
|
-
options.recursive = true;
|
|
4220
|
+
if (view.isRecursive) options.recursive = true;
|
|
4458
4221
|
if (view.with) {
|
|
4459
4222
|
const withOptions = {};
|
|
4460
4223
|
options.with = withOptions;
|
|
@@ -4499,48 +4262,48 @@ const getDbTableColumnsChecks = (tableData) => tableData.constraints.reduce((acc
|
|
|
4499
4262
|
return acc;
|
|
4500
4263
|
}, {});
|
|
4501
4264
|
const dbColumnToAst = (ctx, data, domains, tableName, item, table, tableData, checks) => {
|
|
4502
|
-
var _a
|
|
4265
|
+
var _a;
|
|
4503
4266
|
let column = instantiateDbColumn(ctx, data, domains, item);
|
|
4504
4267
|
column.data.name = item.name;
|
|
4505
4268
|
if (item.identity) {
|
|
4506
4269
|
column.data.identity = item.identity;
|
|
4507
|
-
if (!item.identity.always)
|
|
4508
|
-
(_a = column.data.identity) == null ? true : delete _a.always;
|
|
4270
|
+
if (!item.identity.always) delete column.data.identity?.always;
|
|
4509
4271
|
}
|
|
4510
|
-
if (
|
|
4272
|
+
if (tableData?.primaryKey?.columns?.length === 1 && tableData?.primaryKey?.columns[0] === item.name) {
|
|
4511
4273
|
column = column.primaryKey();
|
|
4512
4274
|
}
|
|
4513
|
-
if (tableData
|
|
4514
|
-
const columnIndexes = tableData
|
|
4275
|
+
if (tableData?.indexes) {
|
|
4276
|
+
const columnIndexes = tableData?.indexes.filter(
|
|
4515
4277
|
(it) => it.columns.length === 1 && "column" in it.columns[0] && it.columns[0].column === item.name
|
|
4516
4278
|
);
|
|
4517
4279
|
for (const index of columnIndexes) {
|
|
4518
4280
|
const columnOptions = index.columns[0];
|
|
4519
|
-
const
|
|
4520
|
-
((
|
|
4521
|
-
options:
|
|
4281
|
+
const { name, ...indexOptions } = makeIndexOptions(tableName, index);
|
|
4282
|
+
((_a = column.data).indexes ?? (_a.indexes = [])).push({
|
|
4283
|
+
options: {
|
|
4522
4284
|
collate: columnOptions.collate,
|
|
4523
4285
|
opclass: columnOptions.opclass,
|
|
4524
|
-
order: columnOptions.order
|
|
4525
|
-
|
|
4286
|
+
order: columnOptions.order,
|
|
4287
|
+
...indexOptions
|
|
4288
|
+
},
|
|
4526
4289
|
name
|
|
4527
4290
|
});
|
|
4528
4291
|
}
|
|
4529
4292
|
}
|
|
4530
4293
|
if (table) {
|
|
4531
4294
|
for (const it of data.constraints) {
|
|
4532
|
-
if (it.tableName !== table.name || it.schemaName !== table.schemaName || it.check ||
|
|
4295
|
+
if (it.tableName !== table.name || it.schemaName !== table.schemaName || it.check || it.references?.columns.length !== 1 || it.references.columns[0] !== item.name || checkIfIsOuterRecursiveFkey(data, table, it.references)) {
|
|
4533
4296
|
continue;
|
|
4534
4297
|
}
|
|
4535
4298
|
const c = dbConstraintToTableConstraint(ctx, table, it);
|
|
4536
4299
|
column = column.foreignKey(
|
|
4537
|
-
|
|
4300
|
+
c.references?.fnOrTable,
|
|
4538
4301
|
it.references.foreignColumns[0],
|
|
4539
|
-
|
|
4302
|
+
c.references?.options
|
|
4540
4303
|
);
|
|
4541
4304
|
}
|
|
4542
4305
|
}
|
|
4543
|
-
const check = checks
|
|
4306
|
+
const check = checks?.[item.name];
|
|
4544
4307
|
if (check) {
|
|
4545
4308
|
column.data.check = {
|
|
4546
4309
|
sql: new RawSQL([[check]])
|
|
@@ -4549,15 +4312,13 @@ const dbColumnToAst = (ctx, data, domains, tableName, item, table, tableData, ch
|
|
|
4549
4312
|
const camelCaseName = toCamelCase(item.name);
|
|
4550
4313
|
if (ctx.snakeCase) {
|
|
4551
4314
|
const snakeCaseName = toSnakeCase(camelCaseName);
|
|
4552
|
-
if (snakeCaseName !== item.name)
|
|
4553
|
-
column.data.name = item.name;
|
|
4315
|
+
if (snakeCaseName !== item.name) column.data.name = item.name;
|
|
4554
4316
|
} else if (camelCaseName !== item.name) {
|
|
4555
4317
|
column.data.name = item.name;
|
|
4556
4318
|
}
|
|
4557
4319
|
return [camelCaseName, column];
|
|
4558
4320
|
};
|
|
4559
4321
|
const dbConstraintToTableConstraint = (ctx, table, item) => {
|
|
4560
|
-
var _a;
|
|
4561
4322
|
const { references, check } = item;
|
|
4562
4323
|
const constraint = {
|
|
4563
4324
|
references: references ? {
|
|
@@ -4575,7 +4336,7 @@ const dbConstraintToTableConstraint = (ctx, table, item) => {
|
|
|
4575
4336
|
const name = item.name && item.name !== getConstraintName(table.name, constraint, ctx.snakeCase) ? item.name : void 0;
|
|
4576
4337
|
if (name) {
|
|
4577
4338
|
constraint.name = name;
|
|
4578
|
-
if (
|
|
4339
|
+
if (constraint.references?.options) {
|
|
4579
4340
|
constraint.references.options.name = name;
|
|
4580
4341
|
}
|
|
4581
4342
|
}
|
|
@@ -4594,14 +4355,13 @@ const makeIndexOptions = (tableName, index) => {
|
|
|
4594
4355
|
};
|
|
4595
4356
|
};
|
|
4596
4357
|
const checkIfIsOuterRecursiveFkey = (data, table, references) => {
|
|
4597
|
-
var _a;
|
|
4598
4358
|
const referencesId = `${references.foreignSchema}.${references.foreignTable}`;
|
|
4599
4359
|
const tableId = `${table.schemaName}.${table.name}`;
|
|
4600
4360
|
for (const other of data.tables) {
|
|
4601
4361
|
const id = `${other.schemaName}.${other.name}`;
|
|
4602
4362
|
if (referencesId === id) {
|
|
4603
4363
|
for (const c of data.constraints) {
|
|
4604
|
-
if (c.tableName === other.name && c.schemaName === other.schemaName &&
|
|
4364
|
+
if (c.tableName === other.name && c.schemaName === other.schemaName && c.references?.foreignTable === table.name && c.references.foreignSchema === table.schemaName && tableId < id) {
|
|
4605
4365
|
return true;
|
|
4606
4366
|
}
|
|
4607
4367
|
}
|
|
@@ -4615,7 +4375,6 @@ const astToGenerateItems = (config, asts, currentSchema) => {
|
|
|
4615
4375
|
return asts.map((ast) => astToGenerateItem(config, ast, currentSchema));
|
|
4616
4376
|
};
|
|
4617
4377
|
const astToGenerateItem = (config, ast, currentSchema) => {
|
|
4618
|
-
var _a, _b, _c, _d, _e;
|
|
4619
4378
|
const add = [];
|
|
4620
4379
|
const drop = [];
|
|
4621
4380
|
const deps = [];
|
|
@@ -4633,7 +4392,7 @@ const astToGenerateItem = (config, ast, currentSchema) => {
|
|
|
4633
4392
|
case "table":
|
|
4634
4393
|
case "changeTable":
|
|
4635
4394
|
case "view": {
|
|
4636
|
-
const schema =
|
|
4395
|
+
const schema = ast.schema ?? currentSchema;
|
|
4637
4396
|
const table = `${schema}.${ast.name}`;
|
|
4638
4397
|
if (ast.type === "table" || ast.type === "view") {
|
|
4639
4398
|
const keys = ast.action === "create" ? add : drop;
|
|
@@ -4731,7 +4490,7 @@ const astToGenerateItem = (config, ast, currentSchema) => {
|
|
|
4731
4490
|
case "enum":
|
|
4732
4491
|
case "collation":
|
|
4733
4492
|
case "extension": {
|
|
4734
|
-
const schema =
|
|
4493
|
+
const schema = ast.schema ?? currentSchema;
|
|
4735
4494
|
(ast.action === "create" ? add : drop).push(`${schema}.${ast.name}`);
|
|
4736
4495
|
deps.push(schema);
|
|
4737
4496
|
break;
|
|
@@ -4739,22 +4498,21 @@ const astToGenerateItem = (config, ast, currentSchema) => {
|
|
|
4739
4498
|
case "enumValues":
|
|
4740
4499
|
case "renameEnumValues":
|
|
4741
4500
|
case "changeEnumValues": {
|
|
4742
|
-
const schema =
|
|
4501
|
+
const schema = ast.schema ?? currentSchema;
|
|
4743
4502
|
deps.push(schema, `${schema}.${ast.name}`);
|
|
4744
4503
|
break;
|
|
4745
4504
|
}
|
|
4746
4505
|
case "domain": {
|
|
4747
|
-
const schema =
|
|
4506
|
+
const schema = ast.schema ?? currentSchema;
|
|
4748
4507
|
(ast.action === "create" ? add : drop).push(`${schema}.${ast.name}`);
|
|
4749
4508
|
const column = ast.baseType;
|
|
4750
4509
|
deps.push(schema, resolveType(column.dataType));
|
|
4751
|
-
if (column.data.collate)
|
|
4752
|
-
deps.push(column.data.collate);
|
|
4510
|
+
if (column.data.collate) deps.push(column.data.collate);
|
|
4753
4511
|
break;
|
|
4754
4512
|
}
|
|
4755
4513
|
case "constraint": {
|
|
4756
4514
|
const { tableSchema = currentSchema, tableName } = ast;
|
|
4757
|
-
const name = `${tableSchema}.${
|
|
4515
|
+
const name = `${tableSchema}.${ast.name ?? getConstraintName(tableName, ast, config.snakeCase)}`;
|
|
4758
4516
|
(ast.action === "create" ? add : drop).push(name);
|
|
4759
4517
|
deps.push(tableSchema, `${tableSchema}.${tableName}`);
|
|
4760
4518
|
break;
|
|
@@ -4777,7 +4535,6 @@ const astToGenerateItem = (config, ast, currentSchema) => {
|
|
|
4777
4535
|
};
|
|
4778
4536
|
};
|
|
4779
4537
|
const analyzeTableColumns = (config, currentSchema, schema, table, deps, resolveType, columns) => {
|
|
4780
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
4781
4538
|
for (const [keys, name, change] of columns) {
|
|
4782
4539
|
const { column } = change;
|
|
4783
4540
|
if (column) {
|
|
@@ -4795,31 +4552,30 @@ const analyzeTableColumns = (config, currentSchema, schema, table, deps, resolve
|
|
|
4795
4552
|
} else if (change.type) {
|
|
4796
4553
|
deps.push(resolveType(change.type));
|
|
4797
4554
|
}
|
|
4798
|
-
const collate =
|
|
4799
|
-
if (collate)
|
|
4800
|
-
|
|
4801
|
-
const primaryKey = change.primaryKey || ((_c = change.column) == null ? void 0 : _c.data.primaryKey);
|
|
4555
|
+
const collate = change.column?.data.collate ?? change.collate;
|
|
4556
|
+
if (collate) deps.push(collate);
|
|
4557
|
+
const primaryKey = change.primaryKey || change.column?.data.primaryKey;
|
|
4802
4558
|
if (primaryKey) {
|
|
4803
4559
|
keys.push(`${table}_pkey`);
|
|
4804
4560
|
}
|
|
4805
|
-
const indexes = change.indexes ||
|
|
4561
|
+
const indexes = change.indexes || change.column?.data.indexes;
|
|
4806
4562
|
if (indexes) {
|
|
4807
4563
|
for (const index of indexes) {
|
|
4808
4564
|
keys.push(
|
|
4809
4565
|
index.name ? `${schema}.${index.name}` : getIndexName(table, [
|
|
4810
|
-
{ column:
|
|
4566
|
+
{ column: change.column?.data.name ?? name }
|
|
4811
4567
|
])
|
|
4812
4568
|
);
|
|
4813
4569
|
}
|
|
4814
4570
|
}
|
|
4815
|
-
const foreignKeys = change.foreignKeys ||
|
|
4571
|
+
const foreignKeys = change.foreignKeys || change.column?.data.foreignKeys;
|
|
4816
4572
|
if (foreignKeys) {
|
|
4817
4573
|
for (const fkey of foreignKeys) {
|
|
4818
4574
|
keys.push(
|
|
4819
|
-
|
|
4575
|
+
fkey.options?.name ? `${schema}.${fkey.options.name}` : getConstraintName(
|
|
4820
4576
|
table,
|
|
4821
4577
|
{
|
|
4822
|
-
references: { columns: [
|
|
4578
|
+
references: { columns: [change.column?.data.name ?? name] }
|
|
4823
4579
|
},
|
|
4824
4580
|
config.snakeCase
|
|
4825
4581
|
)
|
|
@@ -4858,7 +4614,6 @@ const analyzeTableData = (config, currentSchema, schema, table, keys, deps, data
|
|
|
4858
4614
|
};
|
|
4859
4615
|
|
|
4860
4616
|
const astToMigration = (currentSchema, config, asts) => {
|
|
4861
|
-
var _a, _b, _c;
|
|
4862
4617
|
const items = astToGenerateItems(config, asts, currentSchema);
|
|
4863
4618
|
const toBeAdded = /* @__PURE__ */ new Set();
|
|
4864
4619
|
const toBeDropped = /* @__PURE__ */ new Set();
|
|
@@ -4879,12 +4634,11 @@ const astToMigration = (currentSchema, config, asts) => {
|
|
|
4879
4634
|
}
|
|
4880
4635
|
}
|
|
4881
4636
|
for (const dep of item.deps) {
|
|
4882
|
-
remainingDeps.set(dep, (
|
|
4637
|
+
remainingDeps.set(dep, (remainingDeps.get(dep) ?? 0) + 1);
|
|
4883
4638
|
}
|
|
4884
4639
|
}
|
|
4885
4640
|
let len = items.length;
|
|
4886
|
-
if (!len)
|
|
4887
|
-
return;
|
|
4641
|
+
if (!len) return;
|
|
4888
4642
|
for (; ; ) {
|
|
4889
4643
|
for (let i = 0; i < items.length; i++) {
|
|
4890
4644
|
const item = items[i];
|
|
@@ -4919,7 +4673,7 @@ const astToMigration = (currentSchema, config, asts) => {
|
|
|
4919
4673
|
cycleDrop.add(key);
|
|
4920
4674
|
}
|
|
4921
4675
|
for (const key of item.deps) {
|
|
4922
|
-
cycleDeps.set(key, (
|
|
4676
|
+
cycleDeps.set(key, (cycleDeps.get(key) ?? 0) + 1);
|
|
4923
4677
|
}
|
|
4924
4678
|
items.splice(i, 1);
|
|
4925
4679
|
i--;
|
|
@@ -4949,19 +4703,15 @@ const astToMigration = (currentSchema, config, asts) => {
|
|
|
4949
4703
|
dropped.add(drop);
|
|
4950
4704
|
}
|
|
4951
4705
|
for (const [key, num] of cycleDeps) {
|
|
4952
|
-
const set = (
|
|
4953
|
-
if (set)
|
|
4954
|
-
|
|
4955
|
-
else
|
|
4956
|
-
remainingDeps.delete(key);
|
|
4706
|
+
const set = (remainingDeps.get(key) ?? num) - num;
|
|
4707
|
+
if (set) remainingDeps.set(key, set);
|
|
4708
|
+
else remainingDeps.delete(key);
|
|
4957
4709
|
}
|
|
4958
4710
|
len = items.length;
|
|
4959
|
-
if (!len)
|
|
4960
|
-
break;
|
|
4711
|
+
if (!len) break;
|
|
4961
4712
|
groups.push([]);
|
|
4962
4713
|
}
|
|
4963
|
-
if (!groups[0].length)
|
|
4964
|
-
return;
|
|
4714
|
+
if (!groups[0].length) return;
|
|
4965
4715
|
let code = "";
|
|
4966
4716
|
for (const group of groups) {
|
|
4967
4717
|
code += `
|
|
@@ -4980,12 +4730,11 @@ ${group.map(
|
|
|
4980
4730
|
};
|
|
4981
4731
|
const astEncoders = {
|
|
4982
4732
|
table(ast, config) {
|
|
4983
|
-
var _a, _b;
|
|
4984
4733
|
let code = [];
|
|
4985
4734
|
const result = code;
|
|
4986
4735
|
const hasOptions = Boolean(ast.comment || ast.noPrimaryKey === "ignore");
|
|
4987
4736
|
const hasTableData = Boolean(
|
|
4988
|
-
ast.primaryKey ||
|
|
4737
|
+
ast.primaryKey || ast.indexes?.length || ast.constraints?.length
|
|
4989
4738
|
);
|
|
4990
4739
|
const isShifted = hasOptions || hasTableData;
|
|
4991
4740
|
if (isShifted) {
|
|
@@ -4997,8 +4746,7 @@ const astEncoders = {
|
|
|
4997
4746
|
const options = [];
|
|
4998
4747
|
if (ast.comment)
|
|
4999
4748
|
options.push(`comment: ${JSON.stringify(ast.comment)},`);
|
|
5000
|
-
if (ast.noPrimaryKey === "ignore")
|
|
5001
|
-
options.push(`noPrimaryKey: true,`);
|
|
4749
|
+
if (ast.noPrimaryKey === "ignore") options.push(`noPrimaryKey: true,`);
|
|
5002
4750
|
code.push("{", options, "},");
|
|
5003
4751
|
}
|
|
5004
4752
|
code.push("(t) => ({");
|
|
@@ -5034,8 +4782,7 @@ const astEncoders = {
|
|
|
5034
4782
|
}
|
|
5035
4783
|
if (isShifted) {
|
|
5036
4784
|
addCode(code, "}),");
|
|
5037
|
-
if (hasTableData)
|
|
5038
|
-
pushTableDataCode(code, ast);
|
|
4785
|
+
if (hasTableData) pushTableDataCode(code, ast);
|
|
5039
4786
|
addCode(result, ");");
|
|
5040
4787
|
} else {
|
|
5041
4788
|
addCode(result, "}));");
|
|
@@ -5043,7 +4790,6 @@ const astEncoders = {
|
|
|
5043
4790
|
return result;
|
|
5044
4791
|
},
|
|
5045
4792
|
changeTable(ast, config, currentSchema) {
|
|
5046
|
-
var _a;
|
|
5047
4793
|
let code = [];
|
|
5048
4794
|
const result = code;
|
|
5049
4795
|
const schemaTable = quoteSchemaTable({
|
|
@@ -5064,13 +4810,10 @@ const astEncoders = {
|
|
|
5064
4810
|
addCode(code, `await db.changeTable(${schemaTable}, (t) => ({`);
|
|
5065
4811
|
}
|
|
5066
4812
|
const [addTimestamps, dropTimestamps] = ["add", "drop"].map(
|
|
5067
|
-
(type) =>
|
|
5068
|
-
|
|
5069
|
-
|
|
5070
|
-
|
|
5071
|
-
ast.shape.updatedAt && "type" in ast.shape.updatedAt && ((_b = ast.shape.updatedAt) == null ? void 0 : _b.type) === type ? ast.shape.updatedAt.item : void 0
|
|
5072
|
-
);
|
|
5073
|
-
}
|
|
4813
|
+
(type) => getHasTimestamps(
|
|
4814
|
+
ast.shape.createdAt && "type" in ast.shape.createdAt && ast.shape.createdAt?.type === type ? ast.shape.createdAt.item : void 0,
|
|
4815
|
+
ast.shape.updatedAt && "type" in ast.shape.updatedAt && ast.shape.updatedAt?.type === type ? ast.shape.updatedAt.item : void 0
|
|
4816
|
+
)
|
|
5074
4817
|
);
|
|
5075
4818
|
const toCodeCtx = {
|
|
5076
4819
|
t: "t",
|
|
@@ -5087,21 +4830,19 @@ const astEncoders = {
|
|
|
5087
4830
|
const recreate = changes.length > 1;
|
|
5088
4831
|
const line = [
|
|
5089
4832
|
recreate ? `...t.${change.type}(t.name(${singleQuote(
|
|
5090
|
-
|
|
4833
|
+
change.item.data.name ?? key
|
|
5091
4834
|
)})` : `${quoteObjectKey(key)}: t.${change.type}(`
|
|
5092
4835
|
];
|
|
5093
4836
|
const columnCode = change.item.toCode(toCodeCtx, key);
|
|
5094
4837
|
for (let i = 0; i < columnCode.length; i++) {
|
|
5095
4838
|
let part = columnCode[i];
|
|
5096
|
-
if (recreate && !i)
|
|
5097
|
-
part = part.slice(1);
|
|
4839
|
+
if (recreate && !i) part = part.slice(1);
|
|
5098
4840
|
addCode(line, part);
|
|
5099
4841
|
}
|
|
5100
4842
|
addCode(line, "),");
|
|
5101
4843
|
code.push(line);
|
|
5102
4844
|
} else if (change.type === "change") {
|
|
5103
|
-
if (!change.from.column || !change.to.column)
|
|
5104
|
-
continue;
|
|
4845
|
+
if (!change.from.column || !change.to.column) continue;
|
|
5105
4846
|
const line = [
|
|
5106
4847
|
`${quoteObjectKey(key)}: t${change.name ? `.name(${singleQuote(change.name)})` : ""}.change(`
|
|
5107
4848
|
];
|
|
@@ -5180,15 +4921,14 @@ const astEncoders = {
|
|
|
5180
4921
|
return result;
|
|
5181
4922
|
},
|
|
5182
4923
|
renameType(ast, _, currentSchema) {
|
|
5183
|
-
var _a, _b;
|
|
5184
4924
|
const code = [];
|
|
5185
4925
|
const kind = ast.kind === "TABLE" ? "Table" : "Type";
|
|
5186
4926
|
if (ast.from === ast.to) {
|
|
5187
4927
|
addCode(
|
|
5188
4928
|
code,
|
|
5189
4929
|
`await db.change${kind}Schema(${singleQuote(ast.to)}, ${singleQuote(
|
|
5190
|
-
|
|
5191
|
-
)}, ${singleQuote(
|
|
4930
|
+
ast.fromSchema ?? currentSchema
|
|
4931
|
+
)}, ${singleQuote(ast.toSchema ?? currentSchema)});`
|
|
5192
4932
|
);
|
|
5193
4933
|
} else {
|
|
5194
4934
|
addCode(
|
|
@@ -5242,28 +4982,21 @@ const astEncoders = {
|
|
|
5242
4982
|
)}, [${ast.fromValues.map(singleQuote).join(", ")}], [${ast.toValues.map(singleQuote).join(", ")}]);`;
|
|
5243
4983
|
},
|
|
5244
4984
|
domain(ast) {
|
|
5245
|
-
var _a;
|
|
5246
4985
|
return `await db.${ast.action}Domain(${quoteSchemaTable(
|
|
5247
4986
|
ast
|
|
5248
4987
|
)}, (t) => ${ast.baseType.toCode(
|
|
5249
4988
|
{ t: "t", table: ast.name },
|
|
5250
|
-
|
|
4989
|
+
ast.baseType.data.name ?? ""
|
|
5251
4990
|
)});`;
|
|
5252
4991
|
},
|
|
5253
4992
|
collation(ast) {
|
|
5254
4993
|
const params = [];
|
|
5255
|
-
if (ast.locale)
|
|
5256
|
-
|
|
5257
|
-
if (ast.
|
|
5258
|
-
|
|
5259
|
-
if (ast.
|
|
5260
|
-
|
|
5261
|
-
if (ast.provider)
|
|
5262
|
-
params.push(`provider: '${ast.provider}',`);
|
|
5263
|
-
if (ast.deterministic)
|
|
5264
|
-
params.push(`deterministic: ${ast.deterministic},`);
|
|
5265
|
-
if (ast.version)
|
|
5266
|
-
params.push(`version: '${ast.version}',`);
|
|
4994
|
+
if (ast.locale) params.push(`locale: '${ast.locale}',`);
|
|
4995
|
+
if (ast.lcCollate) params.push(`lcCollate: '${ast.lcCollate}',`);
|
|
4996
|
+
if (ast.lcCType) params.push(`lcCType: '${ast.lcCType}',`);
|
|
4997
|
+
if (ast.provider) params.push(`provider: '${ast.provider}',`);
|
|
4998
|
+
if (ast.deterministic) params.push(`deterministic: ${ast.deterministic},`);
|
|
4999
|
+
if (ast.version) params.push(`version: '${ast.version}',`);
|
|
5267
5000
|
return [
|
|
5268
5001
|
`await db.createCollation(${quoteSchemaTable(ast)}, {`,
|
|
5269
5002
|
params,
|
|
@@ -5298,14 +5031,12 @@ const astEncoders = {
|
|
|
5298
5031
|
view(ast) {
|
|
5299
5032
|
const code = [`await db.createView(${quoteSchemaTable(ast)}`];
|
|
5300
5033
|
const options = [];
|
|
5301
|
-
if (ast.options.recursive)
|
|
5302
|
-
options.push("recursive: true,");
|
|
5034
|
+
if (ast.options.recursive) options.push("recursive: true,");
|
|
5303
5035
|
const w = ast.options.with;
|
|
5304
|
-
if (w
|
|
5305
|
-
|
|
5306
|
-
if (w == null ? void 0 : w.securityBarrier)
|
|
5036
|
+
if (w?.checkOption) options.push(`checkOption: '${w.checkOption}',`);
|
|
5037
|
+
if (w?.securityBarrier)
|
|
5307
5038
|
options.push(`securityBarrier: ${w.securityBarrier},`);
|
|
5308
|
-
if (w
|
|
5039
|
+
if (w?.securityInvoker)
|
|
5309
5040
|
options.push(`securityInvoker: ${w.securityInvoker},`);
|
|
5310
5041
|
if (options.length) {
|
|
5311
5042
|
addCode(code, ", {");
|
|
@@ -5335,8 +5066,7 @@ const astEncoders = {
|
|
|
5335
5066
|
}
|
|
5336
5067
|
};
|
|
5337
5068
|
const isTimestamp = (column, type) => {
|
|
5338
|
-
if (!column)
|
|
5339
|
-
return false;
|
|
5069
|
+
if (!column) return false;
|
|
5340
5070
|
const { default: def } = column.data;
|
|
5341
5071
|
return Boolean(
|
|
5342
5072
|
column instanceof type && !column.data.isNullable && def && typeof def === "object" && isRawSQL(def) && (typeof def._sql === "object" ? def._sql[0][0] : def._sql) === "now()"
|
|
@@ -5355,7 +5085,7 @@ const getHasTimestamps = (createdAt, updatedAt) => {
|
|
|
5355
5085
|
};
|
|
5356
5086
|
};
|
|
5357
5087
|
const getTimestampsInfo = (createdAt, updatedAt, type) => {
|
|
5358
|
-
return isTimestamp(createdAt, type) && isTimestamp(updatedAt, type) && (!
|
|
5088
|
+
return isTimestamp(createdAt, type) && isTimestamp(updatedAt, type) && (!createdAt?.data.name || createdAt?.data.name === "created_at") && (!updatedAt?.data.name || updatedAt?.data.name === "updated_at");
|
|
5359
5089
|
};
|
|
5360
5090
|
const timestampsToCode = ({ hasTZTimestamps }) => {
|
|
5361
5091
|
const key = hasTZTimestamps ? "timestamps" : "timestampsNoTZ";
|
|
@@ -5363,15 +5093,13 @@ const timestampsToCode = ({ hasTZTimestamps }) => {
|
|
|
5363
5093
|
};
|
|
5364
5094
|
|
|
5365
5095
|
const pullDbStructure = async (options, config) => {
|
|
5366
|
-
var _a, _b;
|
|
5367
5096
|
const adapter = new Adapter(options);
|
|
5368
5097
|
const currentSchema = adapter.schema || "public";
|
|
5369
5098
|
const ctx = makeStructureToAstCtx(config, currentSchema);
|
|
5370
5099
|
const ast = await structureToAst(ctx, adapter, config);
|
|
5371
5100
|
await adapter.close();
|
|
5372
5101
|
const result = astToMigration(currentSchema, config, ast);
|
|
5373
|
-
if (!result)
|
|
5374
|
-
return;
|
|
5102
|
+
if (!result) return;
|
|
5375
5103
|
const version = await makeFileVersion({}, config);
|
|
5376
5104
|
await writeMigrationFile(config, version, "pull", result);
|
|
5377
5105
|
const silentQueries = Object.assign(adapter, {
|
|
@@ -5383,7 +5111,7 @@ const pullDbStructure = async (options, config) => {
|
|
|
5383
5111
|
const len = unsupportedEntries.length;
|
|
5384
5112
|
if (len) {
|
|
5385
5113
|
let count = 0;
|
|
5386
|
-
|
|
5114
|
+
config.logger?.warn(
|
|
5387
5115
|
`Found unsupported types:
|
|
5388
5116
|
${unsupportedEntries.map(([type, columns]) => {
|
|
5389
5117
|
count += columns.length;
|
|
@@ -5392,17 +5120,16 @@ ${unsupportedEntries.map(([type, columns]) => {
|
|
|
5392
5120
|
Append \`as\` method manually to ${count > 1 ? "these" : "this"} column${count > 1 ? "s" : ""} to treat ${count > 1 ? "them" : "it"} as other column type`
|
|
5393
5121
|
);
|
|
5394
5122
|
}
|
|
5395
|
-
|
|
5123
|
+
config.logger?.log("Database pulled successfully");
|
|
5396
5124
|
adapter.close();
|
|
5397
5125
|
};
|
|
5398
5126
|
|
|
5399
5127
|
const runRecurrentMigrations = async (options, config) => {
|
|
5400
|
-
var _a;
|
|
5401
5128
|
let dbs;
|
|
5402
5129
|
let files = 0;
|
|
5403
5130
|
await readdirRecursive(config.recurrentPath, async (path) => {
|
|
5404
5131
|
files++;
|
|
5405
|
-
dbs
|
|
5132
|
+
dbs ?? (dbs = options.map((opts) => createDb$1({ adapter: new Adapter(opts) })));
|
|
5406
5133
|
const sql = await readFile(path, "utf-8");
|
|
5407
5134
|
await Promise.all(
|
|
5408
5135
|
dbs.map(async (db) => {
|
|
@@ -5413,7 +5140,7 @@ const runRecurrentMigrations = async (options, config) => {
|
|
|
5413
5140
|
if (dbs) {
|
|
5414
5141
|
await Promise.all(dbs.map((db) => db.close()));
|
|
5415
5142
|
if (files > 0) {
|
|
5416
|
-
|
|
5143
|
+
config.logger?.log(
|
|
5417
5144
|
`Applied ${files} recurrent migration file${files > 1 ? "s" : ""}`
|
|
5418
5145
|
);
|
|
5419
5146
|
}
|
|
@@ -5421,12 +5148,10 @@ const runRecurrentMigrations = async (options, config) => {
|
|
|
5421
5148
|
};
|
|
5422
5149
|
const readdirRecursive = async (dirPath, cb) => {
|
|
5423
5150
|
const list = await readdir(dirPath).catch((err) => {
|
|
5424
|
-
if (err.code !== "ENOENT")
|
|
5425
|
-
throw err;
|
|
5151
|
+
if (err.code !== "ENOENT") throw err;
|
|
5426
5152
|
return;
|
|
5427
5153
|
});
|
|
5428
|
-
if (!list)
|
|
5429
|
-
return;
|
|
5154
|
+
if (!list) return;
|
|
5430
5155
|
await Promise.all(
|
|
5431
5156
|
list.map(async (item) => {
|
|
5432
5157
|
const path = join(dirPath, item);
|
|
@@ -5441,7 +5166,6 @@ const readdirRecursive = async (dirPath, cb) => {
|
|
|
5441
5166
|
};
|
|
5442
5167
|
|
|
5443
5168
|
const listMigrationsStatuses = async (options, config, args) => {
|
|
5444
|
-
var _a;
|
|
5445
5169
|
const adapters = options.map((opts) => new Adapter(opts));
|
|
5446
5170
|
const ctx = {};
|
|
5447
5171
|
const [{ migrations }, ...migrated] = await Promise.all([
|
|
@@ -5522,7 +5246,7 @@ ${migration.url}
|
|
|
5522
5246
|
return log2 += showUrl ? lineSeparator : `
|
|
5523
5247
|
${lineSeparator}`;
|
|
5524
5248
|
}).join("\n\n");
|
|
5525
|
-
(
|
|
5249
|
+
(config.logger ?? console).log(log);
|
|
5526
5250
|
await Promise.all(adapters.map((adapter) => adapter.close()));
|
|
5527
5251
|
};
|
|
5528
5252
|
const makeChars = (count, char) => {
|
|
@@ -5533,27 +5257,7 @@ const makeChars = (count, char) => {
|
|
|
5533
5257
|
return chars;
|
|
5534
5258
|
};
|
|
5535
5259
|
|
|
5536
|
-
var __defProp$1 = Object.defineProperty;
|
|
5537
|
-
var __defProps = Object.defineProperties;
|
|
5538
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5539
|
-
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
5540
|
-
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
5541
|
-
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
5542
|
-
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5543
|
-
var __spreadValues$1 = (a, b) => {
|
|
5544
|
-
for (var prop in b || (b = {}))
|
|
5545
|
-
if (__hasOwnProp$1.call(b, prop))
|
|
5546
|
-
__defNormalProp$1(a, prop, b[prop]);
|
|
5547
|
-
if (__getOwnPropSymbols$1)
|
|
5548
|
-
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
5549
|
-
if (__propIsEnum$1.call(b, prop))
|
|
5550
|
-
__defNormalProp$1(a, prop, b[prop]);
|
|
5551
|
-
}
|
|
5552
|
-
return a;
|
|
5553
|
-
};
|
|
5554
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
5555
5260
|
const rebase = async (options, config) => {
|
|
5556
|
-
var _a, _b;
|
|
5557
5261
|
if (config.migrations) {
|
|
5558
5262
|
throw new Error("Cannot rebase migrations defined in the config");
|
|
5559
5263
|
}
|
|
@@ -5569,24 +5273,23 @@ const rebase = async (options, config) => {
|
|
|
5569
5273
|
...adapters.map((adapter) => getMigratedVersionsMap(ctx, adapter, config))
|
|
5570
5274
|
]);
|
|
5571
5275
|
await Promise.all(adapters.map((adapter) => adapter.close()));
|
|
5572
|
-
const files = set.migrations.map((file) =>
|
|
5276
|
+
const files = set.migrations.map((file) => ({
|
|
5277
|
+
...file,
|
|
5573
5278
|
name: path.basename(file.path),
|
|
5574
5279
|
serial: +file.version
|
|
5575
5280
|
}));
|
|
5576
|
-
if (!files.length)
|
|
5577
|
-
return;
|
|
5281
|
+
if (!files.length) return;
|
|
5578
5282
|
const start = files.findIndex(
|
|
5579
5283
|
(file, i) => i !== 0 && file.serial === files[i - 1].serial
|
|
5580
5284
|
);
|
|
5581
|
-
if (start === -1)
|
|
5582
|
-
return;
|
|
5285
|
+
if (start === -1) return;
|
|
5583
5286
|
const combinedVersionsMap = {};
|
|
5584
5287
|
for (const versions of versionMaps) {
|
|
5585
5288
|
Object.assign(combinedVersionsMap, versions.map);
|
|
5586
5289
|
}
|
|
5587
5290
|
const renames = [];
|
|
5588
5291
|
const renamesMap = {};
|
|
5589
|
-
let minVersionToMigrate =
|
|
5292
|
+
let minVersionToMigrate = files.find((file) => !combinedVersionsMap[file.version])?.serial ?? Infinity;
|
|
5590
5293
|
const migratedFiles = [];
|
|
5591
5294
|
let maxNewVersion = 0;
|
|
5592
5295
|
let move = 0;
|
|
@@ -5621,11 +5324,9 @@ const rebase = async (options, config) => {
|
|
|
5621
5324
|
let newVersion = file.serial;
|
|
5622
5325
|
if (move) {
|
|
5623
5326
|
newVersion += move;
|
|
5624
|
-
if (moveFile === prev && !renamesMap[prev.path])
|
|
5625
|
-
newVersion--;
|
|
5327
|
+
if (moveFile === prev && !renamesMap[prev.path]) newVersion--;
|
|
5626
5328
|
if (file.serial !== newVersion) {
|
|
5627
|
-
if (newVersion < minVersionToMigrate)
|
|
5628
|
-
minVersionToMigrate = newVersion;
|
|
5329
|
+
if (newVersion < minVersionToMigrate) minVersionToMigrate = newVersion;
|
|
5629
5330
|
const values = [file.path, newVersion];
|
|
5630
5331
|
renames.push(values);
|
|
5631
5332
|
renamesMap[file.path] = values;
|
|
@@ -5641,8 +5342,7 @@ const rebase = async (options, config) => {
|
|
|
5641
5342
|
const rename = renames[i2];
|
|
5642
5343
|
rename[1]--;
|
|
5643
5344
|
renames[i2] = item;
|
|
5644
|
-
if (rename[0] === prev.path)
|
|
5645
|
-
break;
|
|
5345
|
+
if (rename[0] === prev.path) break;
|
|
5646
5346
|
renamesMap[item[0]] = item;
|
|
5647
5347
|
item = rename;
|
|
5648
5348
|
}
|
|
@@ -5656,8 +5356,7 @@ const rebase = async (options, config) => {
|
|
|
5656
5356
|
maxNewVersion = newVersion;
|
|
5657
5357
|
}
|
|
5658
5358
|
}
|
|
5659
|
-
if (!renames.length && !migratedFiles.length)
|
|
5660
|
-
return;
|
|
5359
|
+
if (!renames.length && !migratedFiles.length) return;
|
|
5661
5360
|
maxNewVersion++;
|
|
5662
5361
|
renames.push(
|
|
5663
5362
|
...migratedFiles.map((file, i) => {
|
|
@@ -5666,8 +5365,7 @@ const rebase = async (options, config) => {
|
|
|
5666
5365
|
return rename;
|
|
5667
5366
|
})
|
|
5668
5367
|
);
|
|
5669
|
-
if (!renames.length)
|
|
5670
|
-
return;
|
|
5368
|
+
if (!renames.length) return;
|
|
5671
5369
|
const migrationsDown = files.filter(
|
|
5672
5370
|
(file) => combinedVersionsMap[file.version] === file.name && file.serial >= minVersionToMigrate
|
|
5673
5371
|
);
|
|
@@ -5675,13 +5373,14 @@ const rebase = async (options, config) => {
|
|
|
5675
5373
|
const rename = renamesMap[file.path];
|
|
5676
5374
|
if (rename) {
|
|
5677
5375
|
const version = String(rename[1]).padStart(4, "0");
|
|
5678
|
-
files2.push(
|
|
5376
|
+
files2.push({
|
|
5377
|
+
...file,
|
|
5679
5378
|
path: path.join(
|
|
5680
5379
|
path.dirname(rename[0]),
|
|
5681
5380
|
version + path.basename(rename[0]).slice(version.length)
|
|
5682
5381
|
),
|
|
5683
5382
|
version
|
|
5684
|
-
})
|
|
5383
|
+
});
|
|
5685
5384
|
} else if (!combinedVersionsMap[file.version] || file.serial >= minVersionToMigrate) {
|
|
5686
5385
|
files2.push(file);
|
|
5687
5386
|
}
|
|
@@ -5691,14 +5390,15 @@ const rebase = async (options, config) => {
|
|
|
5691
5390
|
await redo(
|
|
5692
5391
|
ctx,
|
|
5693
5392
|
options,
|
|
5694
|
-
|
|
5393
|
+
{
|
|
5394
|
+
...config,
|
|
5695
5395
|
async afterRollback() {
|
|
5696
5396
|
set.migrations = migrationsUp;
|
|
5697
5397
|
},
|
|
5698
5398
|
async afterMigrate() {
|
|
5699
5399
|
set.migrations = migrationsDown;
|
|
5700
5400
|
}
|
|
5701
|
-
}
|
|
5401
|
+
},
|
|
5702
5402
|
["all"]
|
|
5703
5403
|
);
|
|
5704
5404
|
for (let i = renames.length - 1; i >= 0; i--) {
|
|
@@ -5714,22 +5414,6 @@ const rebase = async (options, config) => {
|
|
|
5714
5414
|
}
|
|
5715
5415
|
};
|
|
5716
5416
|
|
|
5717
|
-
var __defProp = Object.defineProperty;
|
|
5718
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5719
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5720
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5721
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5722
|
-
var __spreadValues = (a, b) => {
|
|
5723
|
-
for (var prop in b || (b = {}))
|
|
5724
|
-
if (__hasOwnProp.call(b, prop))
|
|
5725
|
-
__defNormalProp(a, prop, b[prop]);
|
|
5726
|
-
if (__getOwnPropSymbols)
|
|
5727
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
5728
|
-
if (__propIsEnum.call(b, prop))
|
|
5729
|
-
__defNormalProp(a, prop, b[prop]);
|
|
5730
|
-
}
|
|
5731
|
-
return a;
|
|
5732
|
-
};
|
|
5733
5417
|
const rakeDb = (options, partialConfig, args = process.argv.slice(2)) => {
|
|
5734
5418
|
const config = processRakeDbConfig(partialConfig);
|
|
5735
5419
|
const promise = runCommand(
|
|
@@ -5737,9 +5421,8 @@ const rakeDb = (options, partialConfig, args = process.argv.slice(2)) => {
|
|
|
5737
5421
|
config,
|
|
5738
5422
|
args
|
|
5739
5423
|
).catch((err) => {
|
|
5740
|
-
var _a;
|
|
5741
5424
|
if (err instanceof RakeDbError) {
|
|
5742
|
-
|
|
5425
|
+
config.logger?.error(err.message);
|
|
5743
5426
|
process.exit(1);
|
|
5744
5427
|
}
|
|
5745
5428
|
throw err;
|
|
@@ -5753,7 +5436,7 @@ rakeDb.lazy = (options, partialConfig) => {
|
|
|
5753
5436
|
return {
|
|
5754
5437
|
change,
|
|
5755
5438
|
run(args, conf) {
|
|
5756
|
-
return runCommand(options, conf ?
|
|
5439
|
+
return runCommand(options, conf ? { ...config, ...conf } : config, args);
|
|
5757
5440
|
}
|
|
5758
5441
|
};
|
|
5759
5442
|
};
|
|
@@ -5768,15 +5451,14 @@ const rakeDbAliases = {
|
|
|
5768
5451
|
rec: "recurrent"
|
|
5769
5452
|
};
|
|
5770
5453
|
const runCommand = async (opts, config, args = process.argv.slice(2)) => {
|
|
5771
|
-
|
|
5772
|
-
let arg = (_a = args[0]) == null ? void 0 : _a.split(":")[0];
|
|
5454
|
+
let arg = args[0]?.split(":")[0];
|
|
5773
5455
|
if (rakeDbAliases[arg]) {
|
|
5774
5456
|
args = [...args];
|
|
5775
5457
|
arg = args[0] = rakeDbAliases[arg];
|
|
5776
5458
|
}
|
|
5777
5459
|
const options = toArray(opts);
|
|
5778
5460
|
args.shift();
|
|
5779
|
-
const command =
|
|
5461
|
+
const command = rakeDbCommands[arg]?.run ?? config.commands[arg];
|
|
5780
5462
|
if (command) {
|
|
5781
5463
|
await command(options, config, args);
|
|
5782
5464
|
} else if (config.logger) {
|
|
@@ -5787,12 +5469,9 @@ const runCommand = async (opts, config, args = process.argv.slice(2)) => {
|
|
|
5787
5469
|
for (let key in rakeDbCommands) {
|
|
5788
5470
|
const command2 = rakeDbCommands[key];
|
|
5789
5471
|
const added = addedCommands.get(command2);
|
|
5790
|
-
if (added)
|
|
5791
|
-
|
|
5792
|
-
if (
|
|
5793
|
-
max = key.length;
|
|
5794
|
-
if (added)
|
|
5795
|
-
continue;
|
|
5472
|
+
if (added) key = added[0] += `, ${key}`;
|
|
5473
|
+
if (key.length > max) max = key.length;
|
|
5474
|
+
if (added) continue;
|
|
5796
5475
|
if (command2.helpArguments) {
|
|
5797
5476
|
maxArgs = Math.max(
|
|
5798
5477
|
maxArgs,
|