@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 CHANGED
Binary file
package/dist/index.cjs CHANGED
@@ -295,7 +295,7 @@ var CubeValidator = class {
295
295
  validTypes = ["varchar", "int", "string", "text", "boolean", "date", "datetime", "timestamp", "decimal", "float", "double", "enum", "json"];
296
296
  validOptions = ["not null", "primary", "autoincrement", "unique", "zerofill", "index", "required", "unsigned"];
297
297
  validProperties = ["type", "length", "options", "value", "defaultValue", "foreign", "enumValues", "description"];
298
- knownAnnotations = ["database", "table", "meta", "columns", "fields", "dataset", "beforeAdd", "afterAdd", "beforeUpdate", "afterUpdate", "beforeDelete", "afterDelete", "compute", "column"];
298
+ knownAnnotations = ["database", "table", "meta", "columns", "fields", "dataset", "beforeAdd", "afterAdd", "beforeUpdate", "afterUpdate", "beforeDelete", "afterDelete", "compute", "column", "changeName", "addColumn", "deleteColumn", "renameColumn", "changeType", "changeLength", "changeDefault", "changeOptions", "changeEnumValues"];
299
299
  /**
300
300
  * Validates a cube file comprehensively
301
301
  */
@@ -553,6 +553,28 @@ var CubeValidator = class {
553
553
  });
554
554
  }
555
555
  }
556
+ if (filePath.includes(".alter.cube")) {
557
+ const hasTable = lines.some((line) => line.includes("@table"));
558
+ if (!hasTable) {
559
+ errors.push({
560
+ itemName: fileName,
561
+ error: "Alter cube files require @table annotation specifying the target table",
562
+ filePath,
563
+ lineNumber: 1
564
+ });
565
+ }
566
+ const hasAlterDirective = lines.some(
567
+ (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")
568
+ );
569
+ if (!hasAlterDirective) {
570
+ errors.push({
571
+ itemName: fileName,
572
+ error: "Alter cube files must contain at least one alter directive (@changeName, @addColumn, @deleteColumn, @renameColumn, @changeType, @changeLength, @changeDefault, @changeOptions, or @changeEnumValues)",
573
+ filePath,
574
+ lineNumber: 1
575
+ });
576
+ }
577
+ }
556
578
  }
557
579
  getColumnTypeForOptions(lines, optionsLineIndex) {
558
580
  for (let i = optionsLineIndex - 1; i >= 0; i--) {
@@ -1371,6 +1393,108 @@ var Schema = class {
1371
1393
  UIUtils.showOperationSummary(summary);
1372
1394
  return totalSeedersProcessed > 0 ? { processed: totalSeedersProcessed, success: successCount, errors: errorCount } : null;
1373
1395
  }
1396
+ async executeAlters() {
1397
+ const startTime = Date.now();
1398
+ const cubesDir = import_path3.default.join(process.cwd(), "dbcube");
1399
+ if (!import_fs4.default.existsSync(cubesDir)) {
1400
+ throw new Error("\u274C The cubes folder does not exist");
1401
+ }
1402
+ const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", ".alter.cube");
1403
+ if (cubeFiles.length === 0) {
1404
+ throw new Error("\u274C There are no .alter.cube files to execute");
1405
+ }
1406
+ UIUtils.showOperationHeader("EXECUTING ALTER TABLES", this.name, "\u{1F527}");
1407
+ let totalAltersProcessed = 0;
1408
+ let successCount = 0;
1409
+ let errorCount = 0;
1410
+ const processedAlters = [];
1411
+ const errors = [];
1412
+ for (let index = 0; index < cubeFiles.length; index++) {
1413
+ const file = cubeFiles[index];
1414
+ const filePath = import_path3.default.isAbsolute(file) ? file : import_path3.default.join(cubesDir, file);
1415
+ const stats = import_fs4.default.statSync(filePath);
1416
+ if (stats.isFile()) {
1417
+ const alterName = import_path3.default.basename(file, ".alter.cube");
1418
+ await UIUtils.showItemProgress(alterName, index + 1, cubeFiles.length);
1419
+ try {
1420
+ const validation = this.validateDatabaseConfiguration(filePath);
1421
+ if (!validation.isValid && validation.error) {
1422
+ UIUtils.showItemError(alterName, validation.error.error);
1423
+ errors.push(validation.error);
1424
+ errorCount++;
1425
+ continue;
1426
+ }
1427
+ const dbResult = FileUtils_default.extractDatabaseNameFromCube(filePath);
1428
+ const cubeDbName = dbResult.status === 200 ? dbResult.message : this.name;
1429
+ if (cubeDbName !== this.name) {
1430
+ continue;
1431
+ }
1432
+ const dml = await this.engine.run("schema_engine", [
1433
+ "--action",
1434
+ "parse_alter",
1435
+ "--schema-path",
1436
+ filePath
1437
+ ]);
1438
+ if (dml.status != 200) {
1439
+ returnFormattedError(dml.status, dml.message);
1440
+ break;
1441
+ }
1442
+ const parseJson = JSON.stringify(dml.data.actions).replace(/[\r\n\t]/g, "").replace(/\\[rnt]/g, "").replace(/\s{2,}/g, " ");
1443
+ const queries = await this.engine.run("schema_engine", [
1444
+ "--action",
1445
+ "generate",
1446
+ "--mode",
1447
+ "alter",
1448
+ "--dml",
1449
+ parseJson
1450
+ ]);
1451
+ if (queries.status != 200) {
1452
+ returnFormattedError(queries.status, queries.message);
1453
+ break;
1454
+ }
1455
+ delete queries.data.database_type;
1456
+ const parseJsonQueries = JSON.stringify(queries.data);
1457
+ const response = await this.engine.run("schema_engine", [
1458
+ "--action",
1459
+ "execute",
1460
+ "--mode",
1461
+ "alter",
1462
+ "--dml",
1463
+ parseJsonQueries
1464
+ ]);
1465
+ if (response.status != 200) {
1466
+ returnFormattedError(response.status, response.message);
1467
+ break;
1468
+ }
1469
+ UIUtils.showItemSuccess(alterName);
1470
+ successCount++;
1471
+ processedAlters.push(alterName);
1472
+ totalAltersProcessed++;
1473
+ } catch (error) {
1474
+ const processError = {
1475
+ itemName: alterName,
1476
+ error: error.message,
1477
+ filePath
1478
+ };
1479
+ UIUtils.showItemError(alterName, error.message);
1480
+ errors.push(processError);
1481
+ errorCount++;
1482
+ }
1483
+ }
1484
+ }
1485
+ const summary = {
1486
+ startTime,
1487
+ totalProcessed: totalAltersProcessed,
1488
+ successCount,
1489
+ errorCount,
1490
+ processedItems: processedAlters,
1491
+ operationName: "alter tables",
1492
+ databaseName: this.name,
1493
+ errors
1494
+ };
1495
+ UIUtils.showOperationSummary(summary);
1496
+ return totalAltersProcessed > 0 ? { processed: totalAltersProcessed, success: successCount, errors: errorCount } : null;
1497
+ }
1374
1498
  async executeTriggers() {
1375
1499
  const startTime = Date.now();
1376
1500
  const cubesDir = import_path3.default.join(process.cwd(), "dbcube");