@smartive/graphql-magic 22.3.0 → 22.4.0
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/CHANGELOG.md +4 -3
- package/dist/bin/gqm.cjs +190 -33
- package/dist/cjs/index.cjs +190 -33
- package/dist/esm/migrations/generate.d.ts +4 -0
- package/dist/esm/migrations/generate.js +180 -35
- package/dist/esm/migrations/generate.js.map +1 -1
- package/dist/esm/models/model-definitions.d.ts +1 -0
- package/package.json +3 -3
- package/src/migrations/generate.ts +220 -35
- package/src/models/model-definitions.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
## 22.
|
|
1
|
+
## 22.4.0 (2025-12-10)
|
|
2
2
|
|
|
3
|
-
* feat:
|
|
4
|
-
* chore(deps): update dependency
|
|
3
|
+
* feat: Generated fields (#390) ([f55e260](https://github.com/smartive/graphql-magic/commit/f55e260)), closes [#390](https://github.com/smartive/graphql-magic/issues/390)
|
|
4
|
+
* chore(deps): update dependency esbuild to v0.27.1 (#394) ([b89fcab](https://github.com/smartive/graphql-magic/commit/b89fcab)), closes [#394](https://github.com/smartive/graphql-magic/issues/394)
|
|
5
|
+
* chore(deps): update dependency prettier to v3.7.4 (#395) ([ec058d1](https://github.com/smartive/graphql-magic/commit/ec058d1)), closes [#395](https://github.com/smartive/graphql-magic/issues/395)
|
package/dist/bin/gqm.cjs
CHANGED
|
@@ -849,32 +849,23 @@ var MigrationGenerator = class {
|
|
|
849
849
|
up,
|
|
850
850
|
down
|
|
851
851
|
);
|
|
852
|
-
const
|
|
852
|
+
const rawExistingFields = model.fields.filter((field) => {
|
|
853
|
+
if (!field.generateAs) {
|
|
854
|
+
return false;
|
|
855
|
+
}
|
|
853
856
|
const col = this.getColumn(model.name, field.kind === "relation" ? `${field.name}Id` : field.name);
|
|
854
857
|
if (!col) {
|
|
855
858
|
return false;
|
|
856
859
|
}
|
|
857
|
-
if (
|
|
860
|
+
if (col.generation_expression !== field.generateAs) {
|
|
858
861
|
return true;
|
|
859
862
|
}
|
|
860
|
-
|
|
861
|
-
if (field.type === "Int") {
|
|
862
|
-
if (col.data_type !== "integer") {
|
|
863
|
-
return true;
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
if (field.type === "Float") {
|
|
867
|
-
if (field.double) {
|
|
868
|
-
if (col.data_type !== "double precision") {
|
|
869
|
-
return true;
|
|
870
|
-
}
|
|
871
|
-
} else if (col.data_type !== "numeric") {
|
|
872
|
-
return true;
|
|
873
|
-
}
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
|
-
return false;
|
|
863
|
+
return this.hasChanged(model, field);
|
|
877
864
|
});
|
|
865
|
+
if (rawExistingFields.length) {
|
|
866
|
+
this.updateFieldsRaw(model, rawExistingFields, up, down);
|
|
867
|
+
}
|
|
868
|
+
const existingFields = model.fields.filter((field) => !field.generateAs && this.hasChanged(model, field));
|
|
878
869
|
this.updateFields(model, existingFields, up, down);
|
|
879
870
|
}
|
|
880
871
|
if (isUpdatableModel(model)) {
|
|
@@ -1017,6 +1008,9 @@ var MigrationGenerator = class {
|
|
|
1017
1008
|
const postAlter = [];
|
|
1018
1009
|
for (const field of fields2) {
|
|
1019
1010
|
alter.push(() => this.column(field, { setNonNull: field.defaultValue !== void 0 }));
|
|
1011
|
+
if (field.generateAs) {
|
|
1012
|
+
continue;
|
|
1013
|
+
}
|
|
1020
1014
|
if (field.nonNull && field.defaultValue === void 0) {
|
|
1021
1015
|
updates.push(() => this.writer.write(`${field.name}: 'TODO',`).newLine());
|
|
1022
1016
|
postAlter.push(() => this.column(field, { alter: true, foreign: false }));
|
|
@@ -1040,12 +1034,56 @@ var MigrationGenerator = class {
|
|
|
1040
1034
|
});
|
|
1041
1035
|
down.push(() => {
|
|
1042
1036
|
this.alterTable(model.name, () => {
|
|
1043
|
-
for (const { kind, name: name2 } of fields2) {
|
|
1037
|
+
for (const { kind, name: name2 } of fields2.toReversed()) {
|
|
1044
1038
|
this.dropColumn(kind === "relation" ? `${name2}Id` : name2);
|
|
1045
1039
|
}
|
|
1046
1040
|
});
|
|
1047
1041
|
});
|
|
1048
1042
|
}
|
|
1043
|
+
updateFieldsRaw(model, fields2, up, down) {
|
|
1044
|
+
if (!fields2.length) {
|
|
1045
|
+
return;
|
|
1046
|
+
}
|
|
1047
|
+
up.push(() => {
|
|
1048
|
+
this.alterTableRaw(model.name, () => {
|
|
1049
|
+
for (const [index, field] of fields2.entries()) {
|
|
1050
|
+
this.columnRaw(field, { alter: true }, index);
|
|
1051
|
+
}
|
|
1052
|
+
});
|
|
1053
|
+
});
|
|
1054
|
+
down.push(() => {
|
|
1055
|
+
this.alterTableRaw(model.name, () => {
|
|
1056
|
+
for (const [index, field] of fields2.entries()) {
|
|
1057
|
+
this.columnRaw(field, { alter: true }, index);
|
|
1058
|
+
}
|
|
1059
|
+
});
|
|
1060
|
+
});
|
|
1061
|
+
if (isUpdatableModel(model)) {
|
|
1062
|
+
const updatableFields = fields2.filter(isUpdatableField);
|
|
1063
|
+
if (!updatableFields.length) {
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
up.push(() => {
|
|
1067
|
+
this.alterTable(`${model.name}Revision`, () => {
|
|
1068
|
+
for (const [index, field] of updatableFields.entries()) {
|
|
1069
|
+
this.columnRaw(field, { alter: true }, index);
|
|
1070
|
+
}
|
|
1071
|
+
});
|
|
1072
|
+
});
|
|
1073
|
+
down.push(() => {
|
|
1074
|
+
this.alterTable(`${model.name}Revision`, () => {
|
|
1075
|
+
for (const [index, field] of updatableFields.entries()) {
|
|
1076
|
+
this.columnRaw(
|
|
1077
|
+
field,
|
|
1078
|
+
{ alter: true },
|
|
1079
|
+
index,
|
|
1080
|
+
summonByName(this.columns[model.name], field.kind === "relation" ? `${field.name}Id` : field.name)
|
|
1081
|
+
);
|
|
1082
|
+
}
|
|
1083
|
+
});
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1049
1087
|
updateFields(model, fields2, up, down) {
|
|
1050
1088
|
if (!fields2.length) {
|
|
1051
1089
|
return;
|
|
@@ -1166,6 +1204,11 @@ var MigrationGenerator = class {
|
|
|
1166
1204
|
createTable(table, block) {
|
|
1167
1205
|
return this.writer.write(`await knex.schema.createTable('${table}', (table) => `).inlineBlock(block).write(");").newLine().blankLine();
|
|
1168
1206
|
}
|
|
1207
|
+
alterTableRaw(table, block) {
|
|
1208
|
+
this.writer.write(`await knex.raw('ALTER TABLE "${table}"`);
|
|
1209
|
+
block();
|
|
1210
|
+
this.writer.write(`');`).newLine().blankLine();
|
|
1211
|
+
}
|
|
1169
1212
|
alterTable(table, block) {
|
|
1170
1213
|
return this.writer.write(`await knex.schema.alterTable('${table}', (table) => `).inlineBlock(block).write(");").newLine().blankLine();
|
|
1171
1214
|
}
|
|
@@ -1187,25 +1230,107 @@ var MigrationGenerator = class {
|
|
|
1187
1230
|
}
|
|
1188
1231
|
return value2;
|
|
1189
1232
|
}
|
|
1190
|
-
|
|
1191
|
-
const
|
|
1192
|
-
if (what) {
|
|
1193
|
-
this.writer.write(what);
|
|
1194
|
-
}
|
|
1233
|
+
columnRaw({ name: name2, ...field }, { setNonNull = true, alter = false } = {}, index, toColumn) {
|
|
1234
|
+
const nonNull2 = () => {
|
|
1195
1235
|
if (setNonNull) {
|
|
1196
1236
|
if (toColumn) {
|
|
1197
1237
|
if (toColumn.is_nullable) {
|
|
1198
|
-
|
|
1199
|
-
} else {
|
|
1200
|
-
this.writer.write(".notNullable()");
|
|
1238
|
+
return false;
|
|
1201
1239
|
}
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1240
|
+
return true;
|
|
1241
|
+
}
|
|
1242
|
+
if (field.nonNull) {
|
|
1243
|
+
return true;
|
|
1244
|
+
}
|
|
1245
|
+
return false;
|
|
1246
|
+
}
|
|
1247
|
+
};
|
|
1248
|
+
const kind = field.kind;
|
|
1249
|
+
if (field.generateAs) {
|
|
1250
|
+
let type = "";
|
|
1251
|
+
switch (kind) {
|
|
1252
|
+
case void 0:
|
|
1253
|
+
case "primitive":
|
|
1254
|
+
switch (field.type) {
|
|
1255
|
+
case "Float":
|
|
1256
|
+
type = `decimal(${field.precision ?? "undefined"}, ${field.scale ?? "undefined"})`;
|
|
1257
|
+
break;
|
|
1258
|
+
default:
|
|
1259
|
+
throw new Error(`Generated columns of kind ${kind} and type ${field.type} are not supported yet.`);
|
|
1260
|
+
}
|
|
1261
|
+
break;
|
|
1262
|
+
default:
|
|
1263
|
+
throw new Error(`Generated columns of kind ${kind} are not supported yet.`);
|
|
1264
|
+
}
|
|
1265
|
+
if (index) {
|
|
1266
|
+
this.writer.write(`,`);
|
|
1267
|
+
}
|
|
1268
|
+
if (alter) {
|
|
1269
|
+
this.writer.write(` ALTER COLUMN "${name2}" TYPE ${type}`);
|
|
1270
|
+
if (setNonNull) {
|
|
1271
|
+
if (nonNull2()) {
|
|
1272
|
+
this.writer.write(`, ALTER COLUMN "${name2}" SET NOT NULL`);
|
|
1205
1273
|
} else {
|
|
1206
|
-
this.writer.write("
|
|
1274
|
+
this.writer.write(`, ALTER COLUMN "${name2}" DROP NOT NULL`);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
this.writer.write(`, ALTER COLUMN "${name2}" SET EXPRESSION AS (${field.generateAs})`);
|
|
1278
|
+
} else {
|
|
1279
|
+
this.writer.write(
|
|
1280
|
+
`${alter ? "ALTER" : "ADD"} COLUMN "${name2}" ${type}${nonNull2() ? " not null" : ""} GENERATED ALWAYS AS (${field.generateAs}) STORED`
|
|
1281
|
+
);
|
|
1282
|
+
}
|
|
1283
|
+
return;
|
|
1284
|
+
}
|
|
1285
|
+
throw new Error(`Only generated columns can be created with columnRaw`);
|
|
1286
|
+
}
|
|
1287
|
+
column({ name: name2, primary, list: list2, ...field }, { setUnique = true, setNonNull = true, alter = false, foreign = true, setDefault = true } = {}, toColumn) {
|
|
1288
|
+
const nonNull2 = () => {
|
|
1289
|
+
if (setNonNull) {
|
|
1290
|
+
if (toColumn) {
|
|
1291
|
+
if (toColumn.is_nullable) {
|
|
1292
|
+
return false;
|
|
1207
1293
|
}
|
|
1294
|
+
return true;
|
|
1208
1295
|
}
|
|
1296
|
+
if (field.nonNull) {
|
|
1297
|
+
return true;
|
|
1298
|
+
}
|
|
1299
|
+
return false;
|
|
1300
|
+
}
|
|
1301
|
+
};
|
|
1302
|
+
const kind = field.kind;
|
|
1303
|
+
if (field.generateAs) {
|
|
1304
|
+
let type = "";
|
|
1305
|
+
switch (kind) {
|
|
1306
|
+
case void 0:
|
|
1307
|
+
case "primitive":
|
|
1308
|
+
switch (field.type) {
|
|
1309
|
+
case "Float":
|
|
1310
|
+
type = `decimal(${field.precision ?? "undefined"}, ${field.scale ?? "undefined"})`;
|
|
1311
|
+
break;
|
|
1312
|
+
default:
|
|
1313
|
+
throw new Error(`Generated columns of kind ${kind} and type ${field.type} are not supported yet.`);
|
|
1314
|
+
}
|
|
1315
|
+
break;
|
|
1316
|
+
default:
|
|
1317
|
+
throw new Error(`Generated columns of kind ${kind} are not supported yet.`);
|
|
1318
|
+
}
|
|
1319
|
+
this.writer.write(
|
|
1320
|
+
`table.specificType('${name2}', '${type}${nonNull2() ? " not null" : ""} GENERATED ALWAYS AS (${field.generateAs}) STORED')`
|
|
1321
|
+
);
|
|
1322
|
+
if (alter) {
|
|
1323
|
+
this.writer.write(".alter()");
|
|
1324
|
+
}
|
|
1325
|
+
this.writer.write(";").newLine();
|
|
1326
|
+
return;
|
|
1327
|
+
}
|
|
1328
|
+
const col = (what) => {
|
|
1329
|
+
if (what) {
|
|
1330
|
+
this.writer.write(what);
|
|
1331
|
+
}
|
|
1332
|
+
if (setNonNull) {
|
|
1333
|
+
this.writer.write(nonNull2() ? ".notNullable()" : ".nullable()");
|
|
1209
1334
|
}
|
|
1210
1335
|
if (setDefault && field.defaultValue !== void 0) {
|
|
1211
1336
|
this.writer.write(`.defaultTo(${this.value(field.defaultValue)})`);
|
|
@@ -1220,7 +1345,6 @@ var MigrationGenerator = class {
|
|
|
1220
1345
|
}
|
|
1221
1346
|
this.writer.write(";").newLine();
|
|
1222
1347
|
};
|
|
1223
|
-
const kind = field.kind;
|
|
1224
1348
|
switch (kind) {
|
|
1225
1349
|
case void 0:
|
|
1226
1350
|
case "primitive":
|
|
@@ -1290,6 +1414,39 @@ var MigrationGenerator = class {
|
|
|
1290
1414
|
getColumn(tableName, columnName) {
|
|
1291
1415
|
return this.columns[tableName].find((col) => col.name === columnName);
|
|
1292
1416
|
}
|
|
1417
|
+
hasChanged(model, field) {
|
|
1418
|
+
const col = this.getColumn(model.name, field.kind === "relation" ? `${field.name}Id` : field.name);
|
|
1419
|
+
if (!col) {
|
|
1420
|
+
return false;
|
|
1421
|
+
}
|
|
1422
|
+
if (field.generateAs) {
|
|
1423
|
+
if (col.generation_expression !== field.generateAs) {
|
|
1424
|
+
throw new Error(
|
|
1425
|
+
`Column ${col.name} has specific type ${col.generation_expression} but expected ${field.generateAs}`
|
|
1426
|
+
);
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
if (!field.nonNull && !col.is_nullable || field.nonNull && col.is_nullable) {
|
|
1430
|
+
return true;
|
|
1431
|
+
}
|
|
1432
|
+
if (!field.kind || field.kind === "primitive") {
|
|
1433
|
+
if (field.type === "Int") {
|
|
1434
|
+
if (col.data_type !== "integer") {
|
|
1435
|
+
return true;
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
if (field.type === "Float") {
|
|
1439
|
+
if (field.double) {
|
|
1440
|
+
if (col.data_type !== "double precision") {
|
|
1441
|
+
return true;
|
|
1442
|
+
}
|
|
1443
|
+
} else if (col.data_type !== "numeric") {
|
|
1444
|
+
return true;
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
return false;
|
|
1449
|
+
}
|
|
1293
1450
|
};
|
|
1294
1451
|
var getMigrationDate = () => {
|
|
1295
1452
|
const date = /* @__PURE__ */ new Date();
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1213,32 +1213,23 @@ var MigrationGenerator = class {
|
|
|
1213
1213
|
up,
|
|
1214
1214
|
down
|
|
1215
1215
|
);
|
|
1216
|
-
const
|
|
1216
|
+
const rawExistingFields = model.fields.filter((field) => {
|
|
1217
|
+
if (!field.generateAs) {
|
|
1218
|
+
return false;
|
|
1219
|
+
}
|
|
1217
1220
|
const col = this.getColumn(model.name, field.kind === "relation" ? `${field.name}Id` : field.name);
|
|
1218
1221
|
if (!col) {
|
|
1219
1222
|
return false;
|
|
1220
1223
|
}
|
|
1221
|
-
if (
|
|
1224
|
+
if (col.generation_expression !== field.generateAs) {
|
|
1222
1225
|
return true;
|
|
1223
1226
|
}
|
|
1224
|
-
|
|
1225
|
-
if (field.type === "Int") {
|
|
1226
|
-
if (col.data_type !== "integer") {
|
|
1227
|
-
return true;
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
if (field.type === "Float") {
|
|
1231
|
-
if (field.double) {
|
|
1232
|
-
if (col.data_type !== "double precision") {
|
|
1233
|
-
return true;
|
|
1234
|
-
}
|
|
1235
|
-
} else if (col.data_type !== "numeric") {
|
|
1236
|
-
return true;
|
|
1237
|
-
}
|
|
1238
|
-
}
|
|
1239
|
-
}
|
|
1240
|
-
return false;
|
|
1227
|
+
return this.hasChanged(model, field);
|
|
1241
1228
|
});
|
|
1229
|
+
if (rawExistingFields.length) {
|
|
1230
|
+
this.updateFieldsRaw(model, rawExistingFields, up, down);
|
|
1231
|
+
}
|
|
1232
|
+
const existingFields = model.fields.filter((field) => !field.generateAs && this.hasChanged(model, field));
|
|
1242
1233
|
this.updateFields(model, existingFields, up, down);
|
|
1243
1234
|
}
|
|
1244
1235
|
if (isUpdatableModel(model)) {
|
|
@@ -1381,6 +1372,9 @@ var MigrationGenerator = class {
|
|
|
1381
1372
|
const postAlter = [];
|
|
1382
1373
|
for (const field of fields2) {
|
|
1383
1374
|
alter.push(() => this.column(field, { setNonNull: field.defaultValue !== void 0 }));
|
|
1375
|
+
if (field.generateAs) {
|
|
1376
|
+
continue;
|
|
1377
|
+
}
|
|
1384
1378
|
if (field.nonNull && field.defaultValue === void 0) {
|
|
1385
1379
|
updates.push(() => this.writer.write(`${field.name}: 'TODO',`).newLine());
|
|
1386
1380
|
postAlter.push(() => this.column(field, { alter: true, foreign: false }));
|
|
@@ -1404,12 +1398,56 @@ var MigrationGenerator = class {
|
|
|
1404
1398
|
});
|
|
1405
1399
|
down.push(() => {
|
|
1406
1400
|
this.alterTable(model.name, () => {
|
|
1407
|
-
for (const { kind, name: name2 } of fields2) {
|
|
1401
|
+
for (const { kind, name: name2 } of fields2.toReversed()) {
|
|
1408
1402
|
this.dropColumn(kind === "relation" ? `${name2}Id` : name2);
|
|
1409
1403
|
}
|
|
1410
1404
|
});
|
|
1411
1405
|
});
|
|
1412
1406
|
}
|
|
1407
|
+
updateFieldsRaw(model, fields2, up, down) {
|
|
1408
|
+
if (!fields2.length) {
|
|
1409
|
+
return;
|
|
1410
|
+
}
|
|
1411
|
+
up.push(() => {
|
|
1412
|
+
this.alterTableRaw(model.name, () => {
|
|
1413
|
+
for (const [index, field] of fields2.entries()) {
|
|
1414
|
+
this.columnRaw(field, { alter: true }, index);
|
|
1415
|
+
}
|
|
1416
|
+
});
|
|
1417
|
+
});
|
|
1418
|
+
down.push(() => {
|
|
1419
|
+
this.alterTableRaw(model.name, () => {
|
|
1420
|
+
for (const [index, field] of fields2.entries()) {
|
|
1421
|
+
this.columnRaw(field, { alter: true }, index);
|
|
1422
|
+
}
|
|
1423
|
+
});
|
|
1424
|
+
});
|
|
1425
|
+
if (isUpdatableModel(model)) {
|
|
1426
|
+
const updatableFields = fields2.filter(isUpdatableField);
|
|
1427
|
+
if (!updatableFields.length) {
|
|
1428
|
+
return;
|
|
1429
|
+
}
|
|
1430
|
+
up.push(() => {
|
|
1431
|
+
this.alterTable(`${model.name}Revision`, () => {
|
|
1432
|
+
for (const [index, field] of updatableFields.entries()) {
|
|
1433
|
+
this.columnRaw(field, { alter: true }, index);
|
|
1434
|
+
}
|
|
1435
|
+
});
|
|
1436
|
+
});
|
|
1437
|
+
down.push(() => {
|
|
1438
|
+
this.alterTable(`${model.name}Revision`, () => {
|
|
1439
|
+
for (const [index, field] of updatableFields.entries()) {
|
|
1440
|
+
this.columnRaw(
|
|
1441
|
+
field,
|
|
1442
|
+
{ alter: true },
|
|
1443
|
+
index,
|
|
1444
|
+
summonByName(this.columns[model.name], field.kind === "relation" ? `${field.name}Id` : field.name)
|
|
1445
|
+
);
|
|
1446
|
+
}
|
|
1447
|
+
});
|
|
1448
|
+
});
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1413
1451
|
updateFields(model, fields2, up, down) {
|
|
1414
1452
|
if (!fields2.length) {
|
|
1415
1453
|
return;
|
|
@@ -1530,6 +1568,11 @@ var MigrationGenerator = class {
|
|
|
1530
1568
|
createTable(table, block) {
|
|
1531
1569
|
return this.writer.write(`await knex.schema.createTable('${table}', (table) => `).inlineBlock(block).write(");").newLine().blankLine();
|
|
1532
1570
|
}
|
|
1571
|
+
alterTableRaw(table, block) {
|
|
1572
|
+
this.writer.write(`await knex.raw('ALTER TABLE "${table}"`);
|
|
1573
|
+
block();
|
|
1574
|
+
this.writer.write(`');`).newLine().blankLine();
|
|
1575
|
+
}
|
|
1533
1576
|
alterTable(table, block) {
|
|
1534
1577
|
return this.writer.write(`await knex.schema.alterTable('${table}', (table) => `).inlineBlock(block).write(");").newLine().blankLine();
|
|
1535
1578
|
}
|
|
@@ -1551,25 +1594,107 @@ var MigrationGenerator = class {
|
|
|
1551
1594
|
}
|
|
1552
1595
|
return value2;
|
|
1553
1596
|
}
|
|
1554
|
-
|
|
1555
|
-
const
|
|
1556
|
-
if (what) {
|
|
1557
|
-
this.writer.write(what);
|
|
1558
|
-
}
|
|
1597
|
+
columnRaw({ name: name2, ...field }, { setNonNull = true, alter = false } = {}, index, toColumn) {
|
|
1598
|
+
const nonNull2 = () => {
|
|
1559
1599
|
if (setNonNull) {
|
|
1560
1600
|
if (toColumn) {
|
|
1561
1601
|
if (toColumn.is_nullable) {
|
|
1562
|
-
|
|
1563
|
-
} else {
|
|
1564
|
-
this.writer.write(".notNullable()");
|
|
1602
|
+
return false;
|
|
1565
1603
|
}
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1604
|
+
return true;
|
|
1605
|
+
}
|
|
1606
|
+
if (field.nonNull) {
|
|
1607
|
+
return true;
|
|
1608
|
+
}
|
|
1609
|
+
return false;
|
|
1610
|
+
}
|
|
1611
|
+
};
|
|
1612
|
+
const kind = field.kind;
|
|
1613
|
+
if (field.generateAs) {
|
|
1614
|
+
let type = "";
|
|
1615
|
+
switch (kind) {
|
|
1616
|
+
case void 0:
|
|
1617
|
+
case "primitive":
|
|
1618
|
+
switch (field.type) {
|
|
1619
|
+
case "Float":
|
|
1620
|
+
type = `decimal(${field.precision ?? "undefined"}, ${field.scale ?? "undefined"})`;
|
|
1621
|
+
break;
|
|
1622
|
+
default:
|
|
1623
|
+
throw new Error(`Generated columns of kind ${kind} and type ${field.type} are not supported yet.`);
|
|
1624
|
+
}
|
|
1625
|
+
break;
|
|
1626
|
+
default:
|
|
1627
|
+
throw new Error(`Generated columns of kind ${kind} are not supported yet.`);
|
|
1628
|
+
}
|
|
1629
|
+
if (index) {
|
|
1630
|
+
this.writer.write(`,`);
|
|
1631
|
+
}
|
|
1632
|
+
if (alter) {
|
|
1633
|
+
this.writer.write(` ALTER COLUMN "${name2}" TYPE ${type}`);
|
|
1634
|
+
if (setNonNull) {
|
|
1635
|
+
if (nonNull2()) {
|
|
1636
|
+
this.writer.write(`, ALTER COLUMN "${name2}" SET NOT NULL`);
|
|
1569
1637
|
} else {
|
|
1570
|
-
this.writer.write("
|
|
1638
|
+
this.writer.write(`, ALTER COLUMN "${name2}" DROP NOT NULL`);
|
|
1571
1639
|
}
|
|
1572
1640
|
}
|
|
1641
|
+
this.writer.write(`, ALTER COLUMN "${name2}" SET EXPRESSION AS (${field.generateAs})`);
|
|
1642
|
+
} else {
|
|
1643
|
+
this.writer.write(
|
|
1644
|
+
`${alter ? "ALTER" : "ADD"} COLUMN "${name2}" ${type}${nonNull2() ? " not null" : ""} GENERATED ALWAYS AS (${field.generateAs}) STORED`
|
|
1645
|
+
);
|
|
1646
|
+
}
|
|
1647
|
+
return;
|
|
1648
|
+
}
|
|
1649
|
+
throw new Error(`Only generated columns can be created with columnRaw`);
|
|
1650
|
+
}
|
|
1651
|
+
column({ name: name2, primary, list: list2, ...field }, { setUnique = true, setNonNull = true, alter = false, foreign = true, setDefault = true } = {}, toColumn) {
|
|
1652
|
+
const nonNull2 = () => {
|
|
1653
|
+
if (setNonNull) {
|
|
1654
|
+
if (toColumn) {
|
|
1655
|
+
if (toColumn.is_nullable) {
|
|
1656
|
+
return false;
|
|
1657
|
+
}
|
|
1658
|
+
return true;
|
|
1659
|
+
}
|
|
1660
|
+
if (field.nonNull) {
|
|
1661
|
+
return true;
|
|
1662
|
+
}
|
|
1663
|
+
return false;
|
|
1664
|
+
}
|
|
1665
|
+
};
|
|
1666
|
+
const kind = field.kind;
|
|
1667
|
+
if (field.generateAs) {
|
|
1668
|
+
let type = "";
|
|
1669
|
+
switch (kind) {
|
|
1670
|
+
case void 0:
|
|
1671
|
+
case "primitive":
|
|
1672
|
+
switch (field.type) {
|
|
1673
|
+
case "Float":
|
|
1674
|
+
type = `decimal(${field.precision ?? "undefined"}, ${field.scale ?? "undefined"})`;
|
|
1675
|
+
break;
|
|
1676
|
+
default:
|
|
1677
|
+
throw new Error(`Generated columns of kind ${kind} and type ${field.type} are not supported yet.`);
|
|
1678
|
+
}
|
|
1679
|
+
break;
|
|
1680
|
+
default:
|
|
1681
|
+
throw new Error(`Generated columns of kind ${kind} are not supported yet.`);
|
|
1682
|
+
}
|
|
1683
|
+
this.writer.write(
|
|
1684
|
+
`table.specificType('${name2}', '${type}${nonNull2() ? " not null" : ""} GENERATED ALWAYS AS (${field.generateAs}) STORED')`
|
|
1685
|
+
);
|
|
1686
|
+
if (alter) {
|
|
1687
|
+
this.writer.write(".alter()");
|
|
1688
|
+
}
|
|
1689
|
+
this.writer.write(";").newLine();
|
|
1690
|
+
return;
|
|
1691
|
+
}
|
|
1692
|
+
const col = (what) => {
|
|
1693
|
+
if (what) {
|
|
1694
|
+
this.writer.write(what);
|
|
1695
|
+
}
|
|
1696
|
+
if (setNonNull) {
|
|
1697
|
+
this.writer.write(nonNull2() ? ".notNullable()" : ".nullable()");
|
|
1573
1698
|
}
|
|
1574
1699
|
if (setDefault && field.defaultValue !== void 0) {
|
|
1575
1700
|
this.writer.write(`.defaultTo(${this.value(field.defaultValue)})`);
|
|
@@ -1584,7 +1709,6 @@ var MigrationGenerator = class {
|
|
|
1584
1709
|
}
|
|
1585
1710
|
this.writer.write(";").newLine();
|
|
1586
1711
|
};
|
|
1587
|
-
const kind = field.kind;
|
|
1588
1712
|
switch (kind) {
|
|
1589
1713
|
case void 0:
|
|
1590
1714
|
case "primitive":
|
|
@@ -1654,6 +1778,39 @@ var MigrationGenerator = class {
|
|
|
1654
1778
|
getColumn(tableName, columnName) {
|
|
1655
1779
|
return this.columns[tableName].find((col) => col.name === columnName);
|
|
1656
1780
|
}
|
|
1781
|
+
hasChanged(model, field) {
|
|
1782
|
+
const col = this.getColumn(model.name, field.kind === "relation" ? `${field.name}Id` : field.name);
|
|
1783
|
+
if (!col) {
|
|
1784
|
+
return false;
|
|
1785
|
+
}
|
|
1786
|
+
if (field.generateAs) {
|
|
1787
|
+
if (col.generation_expression !== field.generateAs) {
|
|
1788
|
+
throw new Error(
|
|
1789
|
+
`Column ${col.name} has specific type ${col.generation_expression} but expected ${field.generateAs}`
|
|
1790
|
+
);
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
if (!field.nonNull && !col.is_nullable || field.nonNull && col.is_nullable) {
|
|
1794
|
+
return true;
|
|
1795
|
+
}
|
|
1796
|
+
if (!field.kind || field.kind === "primitive") {
|
|
1797
|
+
if (field.type === "Int") {
|
|
1798
|
+
if (col.data_type !== "integer") {
|
|
1799
|
+
return true;
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
if (field.type === "Float") {
|
|
1803
|
+
if (field.double) {
|
|
1804
|
+
if (col.data_type !== "double precision") {
|
|
1805
|
+
return true;
|
|
1806
|
+
}
|
|
1807
|
+
} else if (col.data_type !== "numeric") {
|
|
1808
|
+
return true;
|
|
1809
|
+
}
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
return false;
|
|
1813
|
+
}
|
|
1657
1814
|
};
|
|
1658
1815
|
var getMigrationDate = () => {
|
|
1659
1816
|
const date = /* @__PURE__ */ new Date();
|
|
@@ -12,19 +12,23 @@ export declare class MigrationGenerator {
|
|
|
12
12
|
generate(): Promise<string>;
|
|
13
13
|
private renameFields;
|
|
14
14
|
private createFields;
|
|
15
|
+
private updateFieldsRaw;
|
|
15
16
|
private updateFields;
|
|
16
17
|
private createRevisionTable;
|
|
17
18
|
private createRevisionFields;
|
|
18
19
|
private createEnums;
|
|
19
20
|
private migration;
|
|
20
21
|
private createTable;
|
|
22
|
+
private alterTableRaw;
|
|
21
23
|
private alterTable;
|
|
22
24
|
private dropColumn;
|
|
23
25
|
private dropTable;
|
|
24
26
|
private renameTable;
|
|
25
27
|
private renameColumn;
|
|
26
28
|
private value;
|
|
29
|
+
private columnRaw;
|
|
27
30
|
private column;
|
|
28
31
|
private getColumn;
|
|
32
|
+
private hasChanged;
|
|
29
33
|
}
|
|
30
34
|
export declare const getMigrationDate: () => string;
|