@dbcube/schema-builder 5.0.13 → 5.1.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/bun.lockb +0 -0
- package/dist/index.cjs +125 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +125 -1
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -255,7 +255,7 @@ var CubeValidator = class {
|
|
|
255
255
|
validTypes = ["varchar", "int", "string", "text", "boolean", "date", "datetime", "timestamp", "decimal", "float", "double", "enum", "json"];
|
|
256
256
|
validOptions = ["not null", "primary", "autoincrement", "unique", "zerofill", "index", "required", "unsigned"];
|
|
257
257
|
validProperties = ["type", "length", "options", "value", "defaultValue", "foreign", "enumValues", "description"];
|
|
258
|
-
knownAnnotations = ["database", "table", "meta", "columns", "fields", "dataset", "beforeAdd", "afterAdd", "beforeUpdate", "afterUpdate", "beforeDelete", "afterDelete", "compute", "column"];
|
|
258
|
+
knownAnnotations = ["database", "table", "meta", "columns", "fields", "dataset", "beforeAdd", "afterAdd", "beforeUpdate", "afterUpdate", "beforeDelete", "afterDelete", "compute", "column", "changeName", "addColumn", "deleteColumn", "renameColumn", "changeType", "changeLength", "changeDefault", "changeOptions", "changeEnumValues"];
|
|
259
259
|
/**
|
|
260
260
|
* Validates a cube file comprehensively
|
|
261
261
|
*/
|
|
@@ -513,6 +513,28 @@ var CubeValidator = class {
|
|
|
513
513
|
});
|
|
514
514
|
}
|
|
515
515
|
}
|
|
516
|
+
if (filePath.includes(".alter.cube")) {
|
|
517
|
+
const hasTable = lines.some((line) => line.includes("@table"));
|
|
518
|
+
if (!hasTable) {
|
|
519
|
+
errors.push({
|
|
520
|
+
itemName: fileName,
|
|
521
|
+
error: "Alter cube files require @table annotation specifying the target table",
|
|
522
|
+
filePath,
|
|
523
|
+
lineNumber: 1
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
const hasAlterDirective = lines.some(
|
|
527
|
+
(line) => line.includes("@changeName") || line.includes("@addColumn") || line.includes("@deleteColumn") || line.includes("@renameColumn") || line.includes("@changeType") || line.includes("@changeLength") || line.includes("@changeDefault") || line.includes("@changeOptions") || line.includes("@changeEnumValues")
|
|
528
|
+
);
|
|
529
|
+
if (!hasAlterDirective) {
|
|
530
|
+
errors.push({
|
|
531
|
+
itemName: fileName,
|
|
532
|
+
error: "Alter cube files must contain at least one alter directive (@changeName, @addColumn, @deleteColumn, @renameColumn, @changeType, @changeLength, @changeDefault, @changeOptions, or @changeEnumValues)",
|
|
533
|
+
filePath,
|
|
534
|
+
lineNumber: 1
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
}
|
|
516
538
|
}
|
|
517
539
|
getColumnTypeForOptions(lines, optionsLineIndex) {
|
|
518
540
|
for (let i = optionsLineIndex - 1; i >= 0; i--) {
|
|
@@ -1331,6 +1353,108 @@ var Schema = class {
|
|
|
1331
1353
|
UIUtils.showOperationSummary(summary);
|
|
1332
1354
|
return totalSeedersProcessed > 0 ? { processed: totalSeedersProcessed, success: successCount, errors: errorCount } : null;
|
|
1333
1355
|
}
|
|
1356
|
+
async executeAlters() {
|
|
1357
|
+
const startTime = Date.now();
|
|
1358
|
+
const cubesDir = path4.join(process.cwd(), "dbcube");
|
|
1359
|
+
if (!fs5.existsSync(cubesDir)) {
|
|
1360
|
+
throw new Error("\u274C The cubes folder does not exist");
|
|
1361
|
+
}
|
|
1362
|
+
const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", ".alter.cube");
|
|
1363
|
+
if (cubeFiles.length === 0) {
|
|
1364
|
+
throw new Error("\u274C There are no .alter.cube files to execute");
|
|
1365
|
+
}
|
|
1366
|
+
UIUtils.showOperationHeader("EXECUTING ALTER TABLES", this.name, "\u{1F527}");
|
|
1367
|
+
let totalAltersProcessed = 0;
|
|
1368
|
+
let successCount = 0;
|
|
1369
|
+
let errorCount = 0;
|
|
1370
|
+
const processedAlters = [];
|
|
1371
|
+
const errors = [];
|
|
1372
|
+
for (let index = 0; index < cubeFiles.length; index++) {
|
|
1373
|
+
const file = cubeFiles[index];
|
|
1374
|
+
const filePath = path4.isAbsolute(file) ? file : path4.join(cubesDir, file);
|
|
1375
|
+
const stats = fs5.statSync(filePath);
|
|
1376
|
+
if (stats.isFile()) {
|
|
1377
|
+
const alterName = path4.basename(file, ".alter.cube");
|
|
1378
|
+
await UIUtils.showItemProgress(alterName, index + 1, cubeFiles.length);
|
|
1379
|
+
try {
|
|
1380
|
+
const validation = this.validateDatabaseConfiguration(filePath);
|
|
1381
|
+
if (!validation.isValid && validation.error) {
|
|
1382
|
+
UIUtils.showItemError(alterName, validation.error.error);
|
|
1383
|
+
errors.push(validation.error);
|
|
1384
|
+
errorCount++;
|
|
1385
|
+
continue;
|
|
1386
|
+
}
|
|
1387
|
+
const dbResult = FileUtils_default.extractDatabaseNameFromCube(filePath);
|
|
1388
|
+
const cubeDbName = dbResult.status === 200 ? dbResult.message : this.name;
|
|
1389
|
+
if (cubeDbName !== this.name) {
|
|
1390
|
+
continue;
|
|
1391
|
+
}
|
|
1392
|
+
const dml = await this.engine.run("schema_engine", [
|
|
1393
|
+
"--action",
|
|
1394
|
+
"parse_alter",
|
|
1395
|
+
"--schema-path",
|
|
1396
|
+
filePath
|
|
1397
|
+
]);
|
|
1398
|
+
if (dml.status != 200) {
|
|
1399
|
+
returnFormattedError(dml.status, dml.message);
|
|
1400
|
+
break;
|
|
1401
|
+
}
|
|
1402
|
+
const parseJson = JSON.stringify(dml.data.actions).replace(/[\r\n\t]/g, "").replace(/\\[rnt]/g, "").replace(/\s{2,}/g, " ");
|
|
1403
|
+
const queries = await this.engine.run("schema_engine", [
|
|
1404
|
+
"--action",
|
|
1405
|
+
"generate",
|
|
1406
|
+
"--mode",
|
|
1407
|
+
"alter",
|
|
1408
|
+
"--dml",
|
|
1409
|
+
parseJson
|
|
1410
|
+
]);
|
|
1411
|
+
if (queries.status != 200) {
|
|
1412
|
+
returnFormattedError(queries.status, queries.message);
|
|
1413
|
+
break;
|
|
1414
|
+
}
|
|
1415
|
+
delete queries.data.database_type;
|
|
1416
|
+
const parseJsonQueries = JSON.stringify(queries.data);
|
|
1417
|
+
const response = await this.engine.run("schema_engine", [
|
|
1418
|
+
"--action",
|
|
1419
|
+
"execute",
|
|
1420
|
+
"--mode",
|
|
1421
|
+
"alter",
|
|
1422
|
+
"--dml",
|
|
1423
|
+
parseJsonQueries
|
|
1424
|
+
]);
|
|
1425
|
+
if (response.status != 200) {
|
|
1426
|
+
returnFormattedError(response.status, response.message);
|
|
1427
|
+
break;
|
|
1428
|
+
}
|
|
1429
|
+
UIUtils.showItemSuccess(alterName);
|
|
1430
|
+
successCount++;
|
|
1431
|
+
processedAlters.push(alterName);
|
|
1432
|
+
totalAltersProcessed++;
|
|
1433
|
+
} catch (error) {
|
|
1434
|
+
const processError = {
|
|
1435
|
+
itemName: alterName,
|
|
1436
|
+
error: error.message,
|
|
1437
|
+
filePath
|
|
1438
|
+
};
|
|
1439
|
+
UIUtils.showItemError(alterName, error.message);
|
|
1440
|
+
errors.push(processError);
|
|
1441
|
+
errorCount++;
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
const summary = {
|
|
1446
|
+
startTime,
|
|
1447
|
+
totalProcessed: totalAltersProcessed,
|
|
1448
|
+
successCount,
|
|
1449
|
+
errorCount,
|
|
1450
|
+
processedItems: processedAlters,
|
|
1451
|
+
operationName: "alter tables",
|
|
1452
|
+
databaseName: this.name,
|
|
1453
|
+
errors
|
|
1454
|
+
};
|
|
1455
|
+
UIUtils.showOperationSummary(summary);
|
|
1456
|
+
return totalAltersProcessed > 0 ? { processed: totalAltersProcessed, success: successCount, errors: errorCount } : null;
|
|
1457
|
+
}
|
|
1334
1458
|
async executeTriggers() {
|
|
1335
1459
|
const startTime = Date.now();
|
|
1336
1460
|
const cubesDir = path4.join(process.cwd(), "dbcube");
|