rake-db 2.21.8 → 2.22.1
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 +2 -2
- package/dist/index.js +169 -116
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +170 -117
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { quote, EnumColumn, defaultSchemaConfig, getColumnTypes, parseTableData, tableDataMethods, ColumnType, parseTableDataInput, UnknownColumn, raw, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, makeColumnTypes, makeColumnsByType, RawSQL, DomainColumn, CustomTypeColumn, ArrayColumn, instantiateColumn, pushTableDataCode, primaryKeyInnerToCode, indexInnerToCode, constraintInnerToCode, referencesArgsToCode, TimestampTZColumn, TimestampColumn } from 'pqb';
|
|
2
|
-
import { singleQuote, toSnakeCase, isRawSQL, toArray, snakeCaseKey, emptyObject, setCurrentColumnName, consumeColumnName, ColumnTypeBase, setDefaultLanguage, deepCompare, getImportPath, pathToLog, emptyArray, getStackTrace, toCamelCase, codeToString, addCode, quoteObjectKey, backtickQuote } from 'orchid-core';
|
|
2
|
+
import { singleQuote, toSnakeCase, isRawSQL, toArray, snakeCaseKey, emptyObject, setCurrentColumnName, consumeColumnName, ColumnTypeBase, setDefaultLanguage, deepCompare, getImportPath, pathToLog, emptyArray, getStackTrace, toCamelCase, codeToString, addCode, quoteObjectKey, columnToCode, backtickQuote } from 'orchid-core';
|
|
3
3
|
import path, { join } from 'path';
|
|
4
4
|
import { pathToFileURL, fileURLToPath } from 'node:url';
|
|
5
5
|
import fs, { mkdir, writeFile, readdir, stat, readFile } from 'fs/promises';
|
|
@@ -130,7 +130,10 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
|
|
|
130
130
|
line.push(identityToSql(item.data.identity));
|
|
131
131
|
} else if (item.data.generated) {
|
|
132
132
|
line.push(
|
|
133
|
-
`GENERATED ALWAYS AS (${item.data.generated
|
|
133
|
+
`GENERATED ALWAYS AS (${item.data.generated({
|
|
134
|
+
values,
|
|
135
|
+
snakeCase
|
|
136
|
+
})}) STORED`
|
|
134
137
|
);
|
|
135
138
|
}
|
|
136
139
|
if (item.data.primaryKey && !hasMultiplePrimaryKeys) {
|
|
@@ -222,9 +225,13 @@ const getForeignKeyTable = (fnOrTable) => {
|
|
|
222
225
|
const item = new (fnOrTable())();
|
|
223
226
|
return [item.schema, item.table];
|
|
224
227
|
};
|
|
225
|
-
const getConstraintName = (table, constraint) => {
|
|
226
|
-
if (constraint.references)
|
|
227
|
-
|
|
228
|
+
const getConstraintName = (table, constraint, snakeCase) => {
|
|
229
|
+
if (constraint.references) {
|
|
230
|
+
const { columns } = constraint.references;
|
|
231
|
+
return `${table}_${(snakeCase ? columns.map(toSnakeCase) : columns).join(
|
|
232
|
+
"_"
|
|
233
|
+
)}_fkey`;
|
|
234
|
+
}
|
|
228
235
|
if (constraint.check)
|
|
229
236
|
return `${table}_check`;
|
|
230
237
|
if (constraint.identity)
|
|
@@ -232,7 +239,7 @@ const getConstraintName = (table, constraint) => {
|
|
|
232
239
|
return `${table}_constraint`;
|
|
233
240
|
};
|
|
234
241
|
const constraintToSql = ({ name }, up, constraint, values, snakeCase) => {
|
|
235
|
-
const constraintName = constraint.name || getConstraintName(name, constraint);
|
|
242
|
+
const constraintName = constraint.name || getConstraintName(name, constraint, snakeCase);
|
|
236
243
|
if (!up) {
|
|
237
244
|
const { dropMode } = constraint;
|
|
238
245
|
return `CONSTRAINT "${constraintName}"${dropMode ? ` ${dropMode}` : ""}`;
|
|
@@ -250,10 +257,9 @@ const checkToSql = (check, values) => {
|
|
|
250
257
|
return `CHECK (${check.toSQL({ values })})`;
|
|
251
258
|
};
|
|
252
259
|
const foreignKeyToSql = (item, snakeCase) => {
|
|
253
|
-
return `FOREIGN KEY (${joinColumns(
|
|
254
|
-
item
|
|
255
|
-
|
|
256
|
-
)}`;
|
|
260
|
+
return `FOREIGN KEY (${joinColumns(
|
|
261
|
+
snakeCase ? item.columns.map(toSnakeCase) : item.columns
|
|
262
|
+
)}) ${referencesToSql(item, snakeCase)}`;
|
|
257
263
|
};
|
|
258
264
|
const referencesToSql = (references, snakeCase) => {
|
|
259
265
|
const [schema, table] = getForeignKeyTable(references.fnOrTable);
|
|
@@ -277,8 +283,16 @@ const referencesToSql = (references, snakeCase) => {
|
|
|
277
283
|
const getIndexName = (table, columns) => {
|
|
278
284
|
return `${table}_${columns.map((it) => "column" in it ? it.column : "expression").join("_")}_idx`;
|
|
279
285
|
};
|
|
280
|
-
const indexesToQuery = (up, { schema, name: tableName }, indexes, language) => {
|
|
286
|
+
const indexesToQuery = (up, { schema, name: tableName }, indexes, snakeCase, language) => {
|
|
281
287
|
return indexes.map(({ columns, options, name }) => {
|
|
288
|
+
let include = options.include ? toArray(options.include) : void 0;
|
|
289
|
+
if (snakeCase) {
|
|
290
|
+
columns = columns.map(
|
|
291
|
+
(c) => "column" in c ? __spreadProps$7(__spreadValues$9({}, c), { column: toSnakeCase(c.column) }) : c
|
|
292
|
+
);
|
|
293
|
+
if (include)
|
|
294
|
+
include = include.map(toSnakeCase);
|
|
295
|
+
}
|
|
282
296
|
const indexName = name || getIndexName(tableName, columns);
|
|
283
297
|
if (!up) {
|
|
284
298
|
return {
|
|
@@ -333,7 +347,7 @@ const indexesToQuery = (up, { schema, name: tableName }, indexes, language) => {
|
|
|
333
347
|
sql.push(`(${columnList})`);
|
|
334
348
|
if (options.include) {
|
|
335
349
|
sql.push(
|
|
336
|
-
`INCLUDE (${toArray(
|
|
350
|
+
`INCLUDE (${toArray(include).map((column) => `"${column}"`).join(", ")})`
|
|
337
351
|
);
|
|
338
352
|
}
|
|
339
353
|
if (options.nullsNotDistinct) {
|
|
@@ -597,7 +611,7 @@ const astToQueries$1 = (ast, snakeCase, language) => {
|
|
|
597
611
|
)`,
|
|
598
612
|
values
|
|
599
613
|
},
|
|
600
|
-
...indexesToQuery(true, ast, indexes, language),
|
|
614
|
+
...indexesToQuery(true, ast, indexes, snakeCase, language),
|
|
601
615
|
...commentsToQuery(ast, comments)
|
|
602
616
|
);
|
|
603
617
|
if (ast.comment) {
|
|
@@ -637,6 +651,8 @@ const resetChangeTableData = () => {
|
|
|
637
651
|
};
|
|
638
652
|
const addOrDropChanges = [];
|
|
639
653
|
function add(item, options) {
|
|
654
|
+
consumeColumnName();
|
|
655
|
+
setName(this, item);
|
|
640
656
|
if (item instanceof ColumnType) {
|
|
641
657
|
const result = addOrDrop("add", item, options);
|
|
642
658
|
if (result.type === "change")
|
|
@@ -662,6 +678,8 @@ function add(item, options) {
|
|
|
662
678
|
return void 0;
|
|
663
679
|
}
|
|
664
680
|
const drop = function(item, options) {
|
|
681
|
+
consumeColumnName();
|
|
682
|
+
setName(this, item);
|
|
665
683
|
if (item instanceof ColumnType) {
|
|
666
684
|
const result = addOrDrop("drop", item, options);
|
|
667
685
|
if (result.type === "change")
|
|
@@ -687,10 +705,6 @@ const drop = function(item, options) {
|
|
|
687
705
|
return void 0;
|
|
688
706
|
};
|
|
689
707
|
const addOrDrop = (type, item, options) => {
|
|
690
|
-
const name = consumeColumnName();
|
|
691
|
-
if (name) {
|
|
692
|
-
item.data.name = name;
|
|
693
|
-
}
|
|
694
708
|
if (item instanceof UnknownColumn) {
|
|
695
709
|
const empty = columnTypeToColumnChange({
|
|
696
710
|
type: "change",
|
|
@@ -716,24 +730,42 @@ const addOrDrop = (type, item, options) => {
|
|
|
716
730
|
dropMode: options == null ? void 0 : options.dropMode
|
|
717
731
|
};
|
|
718
732
|
};
|
|
719
|
-
const columnTypeToColumnChange = (item) => {
|
|
733
|
+
const columnTypeToColumnChange = (item, name) => {
|
|
720
734
|
if (item instanceof ColumnType) {
|
|
721
|
-
|
|
735
|
+
let column = item;
|
|
736
|
+
const foreignKeys = column.data.foreignKeys;
|
|
722
737
|
if (foreignKeys == null ? void 0 : foreignKeys.some((it) => "fn" in it)) {
|
|
723
738
|
throw new Error("Callback in foreignKey is not allowed in migration");
|
|
724
739
|
}
|
|
740
|
+
if (name && !column.data.name) {
|
|
741
|
+
column = Object.create(column);
|
|
742
|
+
column.data = __spreadProps$5(__spreadValues$7({}, column.data), { name });
|
|
743
|
+
}
|
|
725
744
|
return __spreadProps$5(__spreadValues$7({
|
|
726
|
-
column
|
|
727
|
-
type:
|
|
728
|
-
nullable:
|
|
729
|
-
},
|
|
730
|
-
primaryKey:
|
|
745
|
+
column,
|
|
746
|
+
type: column.toSQL(),
|
|
747
|
+
nullable: column.data.isNullable
|
|
748
|
+
}, column.data), {
|
|
749
|
+
primaryKey: column.data.primaryKey === void 0 ? void 0 : true,
|
|
731
750
|
foreignKeys
|
|
732
751
|
});
|
|
733
752
|
}
|
|
734
753
|
return item.to;
|
|
735
754
|
};
|
|
736
755
|
const nameKey = Symbol("name");
|
|
756
|
+
const setName = (self, item) => {
|
|
757
|
+
var _a, _b, _c, _d, _e;
|
|
758
|
+
const name = self[nameKey];
|
|
759
|
+
if (!name)
|
|
760
|
+
return;
|
|
761
|
+
if ("column" in item && item.column instanceof ColumnType) {
|
|
762
|
+
(_b = (_a = item.column.data).name) != null ? _b : _a.name = name;
|
|
763
|
+
} else if (item instanceof ColumnType) {
|
|
764
|
+
(_d = (_c = item.data).name) != null ? _d : _c.name = name;
|
|
765
|
+
} else {
|
|
766
|
+
(_e = item.name) != null ? _e : item.name = name;
|
|
767
|
+
}
|
|
768
|
+
};
|
|
737
769
|
const tableChangeMethods = __spreadProps$5(__spreadValues$7(__spreadValues$7({}, tableMethods), tableDataMethods), {
|
|
738
770
|
name(name) {
|
|
739
771
|
setCurrentColumnName(name);
|
|
@@ -744,11 +776,17 @@ const tableChangeMethods = __spreadProps$5(__spreadValues$7(__spreadValues$7({},
|
|
|
744
776
|
add,
|
|
745
777
|
drop,
|
|
746
778
|
change(from, to, using) {
|
|
779
|
+
consumeColumnName();
|
|
780
|
+
const f = columnTypeToColumnChange(from);
|
|
781
|
+
const t = columnTypeToColumnChange(to);
|
|
782
|
+
setName(this, f);
|
|
783
|
+
setName(this, t);
|
|
747
784
|
return {
|
|
748
785
|
type: "change",
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
786
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
787
|
+
name: this[nameKey],
|
|
788
|
+
from: f,
|
|
789
|
+
to: t,
|
|
752
790
|
using
|
|
753
791
|
};
|
|
754
792
|
},
|
|
@@ -851,6 +889,7 @@ const makeAst$1 = (up, name, changeData, changeTableData2, options) => {
|
|
|
851
889
|
}, up ? changeTableData2 : { add: changeTableData2.drop, drop: changeTableData2.add });
|
|
852
890
|
};
|
|
853
891
|
const astToQueries = (ast, snakeCase, language) => {
|
|
892
|
+
var _a, _b, _c, _d;
|
|
854
893
|
const queries = [];
|
|
855
894
|
if (ast.comment !== void 0) {
|
|
856
895
|
queries.push({
|
|
@@ -889,25 +928,25 @@ const astToQueries = (ast, snakeCase, language) => {
|
|
|
889
928
|
}
|
|
890
929
|
if (ast.add.primaryKey) {
|
|
891
930
|
addPrimaryKeys.name = ast.add.primaryKey.name;
|
|
892
|
-
|
|
931
|
+
const { columns } = ast.add.primaryKey;
|
|
932
|
+
addPrimaryKeys.columns.push(
|
|
933
|
+
...snakeCase ? columns.map(toSnakeCase) : columns
|
|
934
|
+
);
|
|
893
935
|
}
|
|
894
936
|
if (ast.drop.primaryKey) {
|
|
895
937
|
dropPrimaryKeys.name = ast.drop.primaryKey.name;
|
|
896
|
-
|
|
938
|
+
const { columns } = ast.drop.primaryKey;
|
|
939
|
+
dropPrimaryKeys.columns.push(
|
|
940
|
+
...snakeCase ? columns.map(toSnakeCase) : columns
|
|
941
|
+
);
|
|
897
942
|
}
|
|
898
943
|
const alterTable = [];
|
|
899
944
|
const renameItems = [];
|
|
900
945
|
const values = [];
|
|
901
|
-
const addIndexes =
|
|
902
|
-
const dropIndexes =
|
|
903
|
-
const addConstraints =
|
|
904
|
-
|
|
905
|
-
snakeCase
|
|
906
|
-
);
|
|
907
|
-
const dropConstraints = mapConstraintsToSnakeCase(
|
|
908
|
-
ast.drop.constraints,
|
|
909
|
-
snakeCase
|
|
910
|
-
);
|
|
946
|
+
const addIndexes = (_a = ast.add.indexes) != null ? _a : [];
|
|
947
|
+
const dropIndexes = (_b = ast.drop.indexes) != null ? _b : [];
|
|
948
|
+
const addConstraints = (_c = ast.add.constraints) != null ? _c : [];
|
|
949
|
+
const dropConstraints = (_d = ast.drop.constraints) != null ? _d : [];
|
|
911
950
|
const comments = [];
|
|
912
951
|
for (const key in ast.shape) {
|
|
913
952
|
const item = ast.shape[key];
|
|
@@ -983,8 +1022,8 @@ const astToQueries = (ast, snakeCase, language) => {
|
|
|
983
1022
|
if (alterTable.length) {
|
|
984
1023
|
queries.push(alterTableSql(tableName, alterTable, values));
|
|
985
1024
|
}
|
|
986
|
-
queries.push(...indexesToQuery(false, ast, dropIndexes, language));
|
|
987
|
-
queries.push(...indexesToQuery(true, ast, addIndexes, language));
|
|
1025
|
+
queries.push(...indexesToQuery(false, ast, dropIndexes, snakeCase, language));
|
|
1026
|
+
queries.push(...indexesToQuery(true, ast, addIndexes, snakeCase, language));
|
|
988
1027
|
queries.push(...commentsToQuery(ast, comments));
|
|
989
1028
|
return queries;
|
|
990
1029
|
};
|
|
@@ -1055,7 +1094,7 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
|
|
|
1055
1094
|
const name = getChangeColumnName("to", item, key, snakeCase);
|
|
1056
1095
|
const fromName = getChangeColumnName("from", item, key, snakeCase);
|
|
1057
1096
|
if (fromName !== name) {
|
|
1058
|
-
renameItems.push(renameColumnSql(fromName, name
|
|
1097
|
+
renameItems.push(renameColumnSql(fromName, name));
|
|
1059
1098
|
}
|
|
1060
1099
|
let changeType = false;
|
|
1061
1100
|
if (to.type && (from.type !== to.type || from.collate !== to.collate)) {
|
|
@@ -1171,7 +1210,9 @@ const handleTableItemChange = (key, item, ast, alterTable, renameItems, values,
|
|
|
1171
1210
|
comments.push({ column: name, comment: to.comment || null });
|
|
1172
1211
|
}
|
|
1173
1212
|
} else if (item.type === "rename") {
|
|
1174
|
-
renameItems.push(
|
|
1213
|
+
renameItems.push(
|
|
1214
|
+
snakeCase ? renameColumnSql(toSnakeCase(key), toSnakeCase(item.name)) : renameColumnSql(key, item.name)
|
|
1215
|
+
);
|
|
1175
1216
|
}
|
|
1176
1217
|
};
|
|
1177
1218
|
const getChangeColumnName = (what, change, key, snakeCase) => {
|
|
@@ -1180,22 +1221,8 @@ const getChangeColumnName = (what, change, key, snakeCase) => {
|
|
|
1180
1221
|
getColumnName(change[what].column, key, snakeCase)
|
|
1181
1222
|
) : snakeCase ? toSnakeCase(key) : key);
|
|
1182
1223
|
};
|
|
1183
|
-
const renameColumnSql = (from, to
|
|
1184
|
-
return `RENAME COLUMN "${
|
|
1185
|
-
};
|
|
1186
|
-
const mapIndexesForSnakeCase = (indexes, snakeCase) => {
|
|
1187
|
-
return (indexes == null ? void 0 : indexes.map((index) => __spreadProps$5(__spreadValues$7({}, index), {
|
|
1188
|
-
columns: snakeCase ? index.columns.map(
|
|
1189
|
-
(item) => "column" in item ? __spreadProps$5(__spreadValues$7({}, item), { column: toSnakeCase(item.column) }) : item
|
|
1190
|
-
) : index.columns
|
|
1191
|
-
}))) || [];
|
|
1192
|
-
};
|
|
1193
|
-
const mapConstraintsToSnakeCase = (foreignKeys, snakeCase) => {
|
|
1194
|
-
return (foreignKeys == null ? void 0 : foreignKeys.map((item) => __spreadProps$5(__spreadValues$7({}, item), {
|
|
1195
|
-
references: item.references ? snakeCase ? __spreadProps$5(__spreadValues$7({}, item.references), {
|
|
1196
|
-
columns: item.references.columns.map(toSnakeCase)
|
|
1197
|
-
}) : item.references : void 0
|
|
1198
|
-
}))) || [];
|
|
1224
|
+
const renameColumnSql = (from, to) => {
|
|
1225
|
+
return `RENAME COLUMN "${from}" TO "${to}"`;
|
|
1199
1226
|
};
|
|
1200
1227
|
|
|
1201
1228
|
const createView = async (migration, up, name, options, sql) => {
|
|
@@ -2108,7 +2135,10 @@ class Migration {
|
|
|
2108
2135
|
async columnExists(tableName, columnName) {
|
|
2109
2136
|
return queryExists(this, {
|
|
2110
2137
|
text: `SELECT 1 FROM "information_schema"."columns" WHERE "table_name" = $1 AND "column_name" = $2`,
|
|
2111
|
-
values: [
|
|
2138
|
+
values: [
|
|
2139
|
+
tableName,
|
|
2140
|
+
this.options.snakeCase ? toSnakeCase(columnName) : columnName
|
|
2141
|
+
]
|
|
2112
2142
|
});
|
|
2113
2143
|
}
|
|
2114
2144
|
/**
|
|
@@ -4275,17 +4305,20 @@ const tableToAst = (ctx, data, table, action, domains) => {
|
|
|
4275
4305
|
name: tableName,
|
|
4276
4306
|
shape: makeDbStructureColumnsShape(ctx, data, domains, table, tableData),
|
|
4277
4307
|
noPrimaryKey: tableData.primaryKey ? "error" : "ignore",
|
|
4278
|
-
primaryKey: primaryKey && primaryKey.columns.length > 1 ? primaryKey : void 0,
|
|
4308
|
+
primaryKey: primaryKey && primaryKey.columns.length > 1 ? __spreadProps$1(__spreadValues$2({}, primaryKey), { columns: primaryKey.columns.map(toCamelCase) }) : void 0,
|
|
4279
4309
|
indexes: indexes.reduce((acc, index) => {
|
|
4310
|
+
var _b;
|
|
4280
4311
|
if (index.columns.length > 1 || index.columns.some((it) => "expression" in it)) {
|
|
4281
4312
|
const _a = makeIndexOptions(tableName, index), { name } = _a, options = __objRest(_a, ["name"]);
|
|
4282
4313
|
acc.push({
|
|
4283
|
-
columns: index.columns.map((it) => __spreadProps$1(__spreadValues$2({}, "column" in it ? { column: it.column } : { expression: it.expression }), {
|
|
4314
|
+
columns: index.columns.map((it) => __spreadProps$1(__spreadValues$2({}, "column" in it ? { column: toCamelCase(it.column) } : { expression: it.expression }), {
|
|
4284
4315
|
collate: it.collate,
|
|
4285
4316
|
opclass: it.opclass,
|
|
4286
4317
|
order: it.order
|
|
4287
4318
|
})),
|
|
4288
|
-
options,
|
|
4319
|
+
options: __spreadProps$1(__spreadValues$2({}, options), {
|
|
4320
|
+
include: (_b = index.include) == null ? void 0 : _b.map(toCamelCase)
|
|
4321
|
+
}),
|
|
4289
4322
|
name
|
|
4290
4323
|
});
|
|
4291
4324
|
}
|
|
@@ -4341,7 +4374,7 @@ const constraintToAst = (ctx, item) => {
|
|
|
4341
4374
|
if (check) {
|
|
4342
4375
|
result.check = raw({ raw: check.expression });
|
|
4343
4376
|
}
|
|
4344
|
-
if (item.name && item.name !== getConstraintName(item.tableName, result)) {
|
|
4377
|
+
if (item.name && item.name !== getConstraintName(item.tableName, result, ctx.snakeCase)) {
|
|
4345
4378
|
result.name = item.name;
|
|
4346
4379
|
if ((_a = result.references) == null ? void 0 : _a.options) {
|
|
4347
4380
|
result.references.options.name = item.name;
|
|
@@ -4407,6 +4440,7 @@ const getDbTableColumnsChecks = (tableData) => tableData.constraints.reduce((acc
|
|
|
4407
4440
|
const dbColumnToAst = (ctx, data, domains, tableName, item, table, tableData, checks) => {
|
|
4408
4441
|
var _a, _b, _c, _d, _f, _g, _h, _i, _j;
|
|
4409
4442
|
let column = instantiateDbColumn(ctx, data, domains, item);
|
|
4443
|
+
column.data.name = item.name;
|
|
4410
4444
|
if (item.identity) {
|
|
4411
4445
|
column.data.identity = item.identity;
|
|
4412
4446
|
if (!item.identity.always)
|
|
@@ -4477,7 +4511,7 @@ const dbConstraintToTableConstraint = (ctx, table, item) => {
|
|
|
4477
4511
|
} : void 0,
|
|
4478
4512
|
check: check ? raw({ raw: check.expression }) : void 0
|
|
4479
4513
|
};
|
|
4480
|
-
const name = item.name && item.name !== getConstraintName(table.name, constraint) ? item.name : void 0;
|
|
4514
|
+
const name = item.name && item.name !== getConstraintName(table.name, constraint, ctx.snakeCase) ? item.name : void 0;
|
|
4481
4515
|
if (name) {
|
|
4482
4516
|
constraint.name = name;
|
|
4483
4517
|
if ((_a = constraint.references) == null ? void 0 : _a.options) {
|
|
@@ -4516,10 +4550,10 @@ const checkIfIsOuterRecursiveFkey = (data, table, references) => {
|
|
|
4516
4550
|
return false;
|
|
4517
4551
|
};
|
|
4518
4552
|
|
|
4519
|
-
const astToGenerateItems = (asts, currentSchema) => {
|
|
4520
|
-
return asts.map((ast) => astToGenerateItem(ast, currentSchema));
|
|
4553
|
+
const astToGenerateItems = (config, asts, currentSchema) => {
|
|
4554
|
+
return asts.map((ast) => astToGenerateItem(config, ast, currentSchema));
|
|
4521
4555
|
};
|
|
4522
|
-
const astToGenerateItem = (ast, currentSchema) => {
|
|
4556
|
+
const astToGenerateItem = (config, ast, currentSchema) => {
|
|
4523
4557
|
var _a, _b, _c, _d, _e;
|
|
4524
4558
|
const add = [];
|
|
4525
4559
|
const drop = [];
|
|
@@ -4548,6 +4582,7 @@ const astToGenerateItem = (ast, currentSchema) => {
|
|
|
4548
4582
|
([name, column]) => [keys, name, { column }]
|
|
4549
4583
|
);
|
|
4550
4584
|
analyzeTableColumns(
|
|
4585
|
+
config,
|
|
4551
4586
|
currentSchema,
|
|
4552
4587
|
schema,
|
|
4553
4588
|
table,
|
|
@@ -4556,7 +4591,15 @@ const astToGenerateItem = (ast, currentSchema) => {
|
|
|
4556
4591
|
columns
|
|
4557
4592
|
);
|
|
4558
4593
|
if (ast.type === "table") {
|
|
4559
|
-
analyzeTableData(
|
|
4594
|
+
analyzeTableData(
|
|
4595
|
+
config,
|
|
4596
|
+
currentSchema,
|
|
4597
|
+
schema,
|
|
4598
|
+
table,
|
|
4599
|
+
keys,
|
|
4600
|
+
deps,
|
|
4601
|
+
ast
|
|
4602
|
+
);
|
|
4560
4603
|
} else {
|
|
4561
4604
|
deps.push(
|
|
4562
4605
|
...ast.deps.map(({ schemaName, name }) => `${schemaName}.${name}`)
|
|
@@ -4579,6 +4622,7 @@ const astToGenerateItem = (ast, currentSchema) => {
|
|
|
4579
4622
|
}
|
|
4580
4623
|
}
|
|
4581
4624
|
analyzeTableColumns(
|
|
4625
|
+
config,
|
|
4582
4626
|
currentSchema,
|
|
4583
4627
|
schema,
|
|
4584
4628
|
table,
|
|
@@ -4586,8 +4630,24 @@ const astToGenerateItem = (ast, currentSchema) => {
|
|
|
4586
4630
|
resolveType,
|
|
4587
4631
|
columns
|
|
4588
4632
|
);
|
|
4589
|
-
analyzeTableData(
|
|
4590
|
-
|
|
4633
|
+
analyzeTableData(
|
|
4634
|
+
config,
|
|
4635
|
+
currentSchema,
|
|
4636
|
+
schema,
|
|
4637
|
+
table,
|
|
4638
|
+
add,
|
|
4639
|
+
deps,
|
|
4640
|
+
ast.add
|
|
4641
|
+
);
|
|
4642
|
+
analyzeTableData(
|
|
4643
|
+
config,
|
|
4644
|
+
currentSchema,
|
|
4645
|
+
schema,
|
|
4646
|
+
table,
|
|
4647
|
+
drop,
|
|
4648
|
+
deps,
|
|
4649
|
+
ast.drop
|
|
4650
|
+
);
|
|
4591
4651
|
}
|
|
4592
4652
|
break;
|
|
4593
4653
|
}
|
|
@@ -4633,7 +4693,7 @@ const astToGenerateItem = (ast, currentSchema) => {
|
|
|
4633
4693
|
}
|
|
4634
4694
|
case "constraint": {
|
|
4635
4695
|
const { tableSchema = currentSchema, tableName } = ast;
|
|
4636
|
-
const name = `${tableSchema}.${(_e = ast.name) != null ? _e : getConstraintName(tableName, ast)}`;
|
|
4696
|
+
const name = `${tableSchema}.${(_e = ast.name) != null ? _e : getConstraintName(tableName, ast, config.snakeCase)}`;
|
|
4637
4697
|
(ast.action === "create" ? add : drop).push(name);
|
|
4638
4698
|
deps.push(tableSchema, `${tableSchema}.${tableName}`);
|
|
4639
4699
|
break;
|
|
@@ -4655,7 +4715,7 @@ const astToGenerateItem = (ast, currentSchema) => {
|
|
|
4655
4715
|
deps: new Set(deps)
|
|
4656
4716
|
};
|
|
4657
4717
|
};
|
|
4658
|
-
const analyzeTableColumns = (currentSchema, schema, table, deps, resolveType, columns) => {
|
|
4718
|
+
const analyzeTableColumns = (config, currentSchema, schema, table, deps, resolveType, columns) => {
|
|
4659
4719
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
4660
4720
|
for (const [keys, name, change] of columns) {
|
|
4661
4721
|
const { column } = change;
|
|
@@ -4695,9 +4755,13 @@ const analyzeTableColumns = (currentSchema, schema, table, deps, resolveType, co
|
|
|
4695
4755
|
if (foreignKeys) {
|
|
4696
4756
|
for (const fkey of foreignKeys) {
|
|
4697
4757
|
keys.push(
|
|
4698
|
-
((_h = fkey.options) == null ? void 0 : _h.name) ? `${schema}.${fkey.options.name}` : getConstraintName(
|
|
4699
|
-
|
|
4700
|
-
|
|
4758
|
+
((_h = fkey.options) == null ? void 0 : _h.name) ? `${schema}.${fkey.options.name}` : getConstraintName(
|
|
4759
|
+
table,
|
|
4760
|
+
{
|
|
4761
|
+
references: { columns: [(_j = (_i = change.column) == null ? void 0 : _i.data.name) != null ? _j : name] }
|
|
4762
|
+
},
|
|
4763
|
+
config.snakeCase
|
|
4764
|
+
)
|
|
4701
4765
|
);
|
|
4702
4766
|
const [s = currentSchema, t] = getForeignKeyTable(fkey.fnOrTable);
|
|
4703
4767
|
deps.push(`${s}.${t}`);
|
|
@@ -4705,7 +4769,7 @@ const analyzeTableColumns = (currentSchema, schema, table, deps, resolveType, co
|
|
|
4705
4769
|
}
|
|
4706
4770
|
}
|
|
4707
4771
|
};
|
|
4708
|
-
const analyzeTableData = (currentSchema, schema, table, keys, deps, data) => {
|
|
4772
|
+
const analyzeTableData = (config, currentSchema, schema, table, keys, deps, data) => {
|
|
4709
4773
|
if (data.primaryKey) {
|
|
4710
4774
|
const name = data.primaryKey.name;
|
|
4711
4775
|
keys.push(name ? `${schema}.${name}` : `${table}_pkey`);
|
|
@@ -4720,7 +4784,7 @@ const analyzeTableData = (currentSchema, schema, table, keys, deps, data) => {
|
|
|
4720
4784
|
if (data.constraints) {
|
|
4721
4785
|
for (const constraint of data.constraints) {
|
|
4722
4786
|
keys.push(
|
|
4723
|
-
constraint.name ? `${schema}.${constraint.name}` : getConstraintName(table, constraint)
|
|
4787
|
+
constraint.name ? `${schema}.${constraint.name}` : getConstraintName(table, constraint, config.snakeCase)
|
|
4724
4788
|
);
|
|
4725
4789
|
if (constraint.references) {
|
|
4726
4790
|
const [s = currentSchema, t] = getForeignKeyTable(
|
|
@@ -4734,7 +4798,7 @@ const analyzeTableData = (currentSchema, schema, table, keys, deps, data) => {
|
|
|
4734
4798
|
|
|
4735
4799
|
const astToMigration = (currentSchema, config, asts) => {
|
|
4736
4800
|
var _a, _b, _c;
|
|
4737
|
-
const items = astToGenerateItems(asts, currentSchema);
|
|
4801
|
+
const items = astToGenerateItems(config, asts, currentSchema);
|
|
4738
4802
|
const toBeAdded = /* @__PURE__ */ new Set();
|
|
4739
4803
|
const toBeDropped = /* @__PURE__ */ new Set();
|
|
4740
4804
|
const remainingDeps = /* @__PURE__ */ new Map();
|
|
@@ -4884,7 +4948,6 @@ const astEncoders = {
|
|
|
4884
4948
|
);
|
|
4885
4949
|
}
|
|
4886
4950
|
const timestamps = getHasTimestamps(
|
|
4887
|
-
config,
|
|
4888
4951
|
ast.shape.createdAt,
|
|
4889
4952
|
ast.shape.updatedAt
|
|
4890
4953
|
);
|
|
@@ -4892,14 +4955,15 @@ const astEncoders = {
|
|
|
4892
4955
|
if (timestamps.hasAnyTimestamps && (key === "createdAt" || key === "updatedAt"))
|
|
4893
4956
|
continue;
|
|
4894
4957
|
const line = [`${quoteObjectKey(key)}: `];
|
|
4895
|
-
|
|
4958
|
+
const columnCode = columnToCode(key, ast.shape[key], config.snakeCase);
|
|
4959
|
+
for (const part of columnCode) {
|
|
4896
4960
|
addCode(line, part);
|
|
4897
4961
|
}
|
|
4898
4962
|
addCode(line, ",");
|
|
4899
4963
|
code.push(line);
|
|
4900
4964
|
}
|
|
4901
4965
|
if (timestamps.hasAnyTimestamps) {
|
|
4902
|
-
code.push([`...${timestampsToCode(
|
|
4966
|
+
code.push([`...${timestampsToCode(timestamps)},`]);
|
|
4903
4967
|
}
|
|
4904
4968
|
if (isShifted) {
|
|
4905
4969
|
addCode(code, "}),");
|
|
@@ -4912,6 +4976,7 @@ const astEncoders = {
|
|
|
4912
4976
|
return result;
|
|
4913
4977
|
},
|
|
4914
4978
|
changeTable(ast, config, currentSchema) {
|
|
4979
|
+
var _a;
|
|
4915
4980
|
let code = [];
|
|
4916
4981
|
const result = code;
|
|
4917
4982
|
const schemaTable = quoteSchemaTable({
|
|
@@ -4933,10 +4998,9 @@ const astEncoders = {
|
|
|
4933
4998
|
}
|
|
4934
4999
|
const [addTimestamps, dropTimestamps] = ["add", "drop"].map(
|
|
4935
5000
|
(type) => {
|
|
4936
|
-
var
|
|
5001
|
+
var _a2, _b;
|
|
4937
5002
|
return getHasTimestamps(
|
|
4938
|
-
|
|
4939
|
-
ast.shape.createdAt && "type" in ast.shape.createdAt && ((_a = ast.shape.createdAt) == null ? void 0 : _a.type) === type ? ast.shape.createdAt.item : void 0,
|
|
5003
|
+
ast.shape.createdAt && "type" in ast.shape.createdAt && ((_a2 = ast.shape.createdAt) == null ? void 0 : _a2.type) === type ? ast.shape.createdAt.item : void 0,
|
|
4940
5004
|
ast.shape.updatedAt && "type" in ast.shape.updatedAt && ((_b = ast.shape.updatedAt) == null ? void 0 : _b.type) === type ? ast.shape.updatedAt.item : void 0
|
|
4941
5005
|
);
|
|
4942
5006
|
}
|
|
@@ -4949,9 +5013,11 @@ const astEncoders = {
|
|
|
4949
5013
|
continue;
|
|
4950
5014
|
const recreate = changes.length > 1;
|
|
4951
5015
|
const line = [
|
|
4952
|
-
recreate ? `...t.${change.type}(t.name(${singleQuote(
|
|
5016
|
+
recreate ? `...t.${change.type}(t.name(${singleQuote(
|
|
5017
|
+
(_a = change.item.data.name) != null ? _a : key
|
|
5018
|
+
)})` : `${quoteObjectKey(key)}: t.${change.type}(`
|
|
4953
5019
|
];
|
|
4954
|
-
const columnCode = change.item
|
|
5020
|
+
const columnCode = columnToCode(key, change.item, config.snakeCase);
|
|
4955
5021
|
for (let i = 0; i < columnCode.length; i++) {
|
|
4956
5022
|
let part = columnCode[i];
|
|
4957
5023
|
if (recreate && !i)
|
|
@@ -4966,11 +5032,17 @@ const astEncoders = {
|
|
|
4966
5032
|
const line = [
|
|
4967
5033
|
`${quoteObjectKey(key)}: t${change.name ? `.name(${singleQuote(change.name)})` : ""}.change(`
|
|
4968
5034
|
];
|
|
4969
|
-
|
|
5035
|
+
const fromCode = columnToCode(
|
|
5036
|
+
key,
|
|
5037
|
+
change.from.column,
|
|
5038
|
+
config.snakeCase
|
|
5039
|
+
);
|
|
5040
|
+
for (const part of fromCode) {
|
|
4970
5041
|
addCode(line, part);
|
|
4971
5042
|
}
|
|
4972
5043
|
addCode(line, ", ");
|
|
4973
|
-
|
|
5044
|
+
const toCode = columnToCode(key, change.to.column, config.snakeCase);
|
|
5045
|
+
for (const part of toCode) {
|
|
4974
5046
|
addCode(line, part);
|
|
4975
5047
|
}
|
|
4976
5048
|
if (change.using) {
|
|
@@ -4999,9 +5071,7 @@ const astEncoders = {
|
|
|
4999
5071
|
for (const key of ["drop", "add"]) {
|
|
5000
5072
|
const timestamps = key === "add" ? addTimestamps : dropTimestamps;
|
|
5001
5073
|
if (timestamps.hasAnyTimestamps) {
|
|
5002
|
-
addCode(code, [
|
|
5003
|
-
`...t.${key}(${timestampsToCode(config, timestamps)}),`
|
|
5004
|
-
]);
|
|
5074
|
+
addCode(code, [`...t.${key}(${timestampsToCode(timestamps)}),`]);
|
|
5005
5075
|
}
|
|
5006
5076
|
const { primaryKey, indexes, constraints } = ast[key];
|
|
5007
5077
|
if (primaryKey) {
|
|
@@ -5191,41 +5261,24 @@ const isTimestamp = (column, type) => {
|
|
|
5191
5261
|
column instanceof type && !column.data.isNullable && def && typeof def === "object" && isRawSQL(def) && (typeof def._sql === "object" ? def._sql[0][0] : def._sql) === "now()"
|
|
5192
5262
|
);
|
|
5193
5263
|
};
|
|
5194
|
-
const getHasTimestamps = (
|
|
5195
|
-
const timestamps = getTimestampsInfo(
|
|
5196
|
-
config,
|
|
5197
|
-
createdAt,
|
|
5198
|
-
updatedAt,
|
|
5199
|
-
TimestampTZColumn
|
|
5200
|
-
);
|
|
5264
|
+
const getHasTimestamps = (createdAt, updatedAt) => {
|
|
5265
|
+
const timestamps = getTimestampsInfo(createdAt, updatedAt, TimestampTZColumn);
|
|
5201
5266
|
const timestampsNoTZ = getTimestampsInfo(
|
|
5202
|
-
config,
|
|
5203
5267
|
createdAt,
|
|
5204
5268
|
updatedAt,
|
|
5205
5269
|
TimestampColumn
|
|
5206
5270
|
);
|
|
5207
5271
|
return {
|
|
5208
|
-
hasTZTimestamps: timestamps
|
|
5209
|
-
hasAnyTimestamps: timestamps
|
|
5210
|
-
hasAnyCamelCaseTimestamps: timestamps.camelCaseTimestamps || timestampsNoTZ.camelCaseTimestamps
|
|
5272
|
+
hasTZTimestamps: timestamps,
|
|
5273
|
+
hasAnyTimestamps: timestamps || timestampsNoTZ
|
|
5211
5274
|
};
|
|
5212
5275
|
};
|
|
5213
|
-
const getTimestampsInfo = (
|
|
5214
|
-
|
|
5215
|
-
const camelCaseTimestamps = !config.snakeCase && hasTimestamps && !(createdAt == null ? void 0 : createdAt.data.name) && !(updatedAt == null ? void 0 : updatedAt.data.name);
|
|
5216
|
-
const snakeCaseTimestamps = hasTimestamps && !camelCaseTimestamps && (!config.snakeCase && (createdAt == null ? void 0 : createdAt.data.name) === "created_at" && (updatedAt == null ? void 0 : updatedAt.data.name) === "updated_at" || config.snakeCase && !(createdAt == null ? void 0 : createdAt.data.name) && !(updatedAt == null ? void 0 : updatedAt.data.name));
|
|
5217
|
-
if (!camelCaseTimestamps && !snakeCaseTimestamps) {
|
|
5218
|
-
hasTimestamps = false;
|
|
5219
|
-
}
|
|
5220
|
-
return {
|
|
5221
|
-
hasTimestamps,
|
|
5222
|
-
camelCaseTimestamps,
|
|
5223
|
-
snakeCaseTimestamps
|
|
5224
|
-
};
|
|
5276
|
+
const getTimestampsInfo = (createdAt, updatedAt, type) => {
|
|
5277
|
+
return isTimestamp(createdAt, type) && isTimestamp(updatedAt, type) && (!(createdAt == null ? void 0 : createdAt.data.name) || (createdAt == null ? void 0 : createdAt.data.name) === "created_at") && (!(updatedAt == null ? void 0 : updatedAt.data.name) || (updatedAt == null ? void 0 : updatedAt.data.name) === "updated_at");
|
|
5225
5278
|
};
|
|
5226
|
-
const timestampsToCode = (
|
|
5279
|
+
const timestampsToCode = ({ hasTZTimestamps }) => {
|
|
5227
5280
|
const key = hasTZTimestamps ? "timestamps" : "timestampsNoTZ";
|
|
5228
|
-
return `t.${
|
|
5281
|
+
return `t.${key}()`;
|
|
5229
5282
|
};
|
|
5230
5283
|
|
|
5231
5284
|
const pullDbStructure = async (options, config) => {
|