@dbcube/schema-builder 1.0.17 → 1.0.19
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.cjs +372 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +46 -1
- package/dist/index.d.ts +46 -1
- package/dist/index.js +371 -40
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -6,9 +6,9 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
6
6
|
});
|
|
7
7
|
|
|
8
8
|
// src/lib/Schema.ts
|
|
9
|
-
import
|
|
9
|
+
import fs5 from "fs";
|
|
10
10
|
import { Engine, TableProcessor, Config as ConfigClass } from "@dbcube/core";
|
|
11
|
-
import
|
|
11
|
+
import path4 from "path";
|
|
12
12
|
|
|
13
13
|
// src/lib/FileUtils.ts
|
|
14
14
|
import * as fs from "fs";
|
|
@@ -373,7 +373,7 @@ var CubeValidator = class {
|
|
|
373
373
|
filePath,
|
|
374
374
|
lineNumber
|
|
375
375
|
});
|
|
376
|
-
} else if (!this.isOptionCompatibleWithType(option, columnType)) {
|
|
376
|
+
} else if (columnType !== "unknown" && !this.isOptionCompatibleWithType(option, columnType)) {
|
|
377
377
|
errors.push({
|
|
378
378
|
itemName: fileName,
|
|
379
379
|
error: `Option '${option}' is not compatible with type '${columnType}'`,
|
|
@@ -392,6 +392,18 @@ var CubeValidator = class {
|
|
|
392
392
|
if (/^\s*[a-zA-Z_][a-zA-Z0-9_]*\s*:\s*\{/.test(line)) {
|
|
393
393
|
return;
|
|
394
394
|
}
|
|
395
|
+
if (this.isInsideForeignKeyObject(content, lineNumber - 1)) {
|
|
396
|
+
const validForeignKeyProperties = ["table", "column"];
|
|
397
|
+
if (!validForeignKeyProperties.includes(propertyName)) {
|
|
398
|
+
errors.push({
|
|
399
|
+
itemName: fileName,
|
|
400
|
+
error: `Invalid foreign key property '${propertyName}'. Valid foreign key properties: ${validForeignKeyProperties.join(", ")}`,
|
|
401
|
+
filePath,
|
|
402
|
+
lineNumber
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
395
407
|
if (this.isInsideColumnsBlock(content, lineNumber - 1)) {
|
|
396
408
|
if (!this.validProperties.includes(propertyName)) {
|
|
397
409
|
errors.push({
|
|
@@ -560,6 +572,227 @@ var CubeValidator = class {
|
|
|
560
572
|
}
|
|
561
573
|
return columnsStartLine !== -1 && columnsEndLine !== -1 && lineIndex > columnsStartLine && lineIndex < columnsEndLine;
|
|
562
574
|
}
|
|
575
|
+
isInsideForeignKeyObject(content, lineIndex) {
|
|
576
|
+
const lines = content.split("\n");
|
|
577
|
+
for (let i = lineIndex; i >= 0; i--) {
|
|
578
|
+
const line = lines[i];
|
|
579
|
+
if (/foreign\s*:\s*\{/.test(line)) {
|
|
580
|
+
let braceCount = 0;
|
|
581
|
+
for (let j = i; j <= lineIndex; j++) {
|
|
582
|
+
const currentLine = lines[j];
|
|
583
|
+
const openBraces = (currentLine.match(/\{/g) || []).length;
|
|
584
|
+
const closeBraces = (currentLine.match(/\}/g) || []).length;
|
|
585
|
+
braceCount += openBraces - closeBraces;
|
|
586
|
+
if (braceCount === 0 && j > i) {
|
|
587
|
+
return false;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
return braceCount > 0;
|
|
591
|
+
}
|
|
592
|
+
if (line.trim() === "}" || line.includes("};")) {
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
return false;
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
// src/lib/DependencyResolver.ts
|
|
601
|
+
import fs4 from "fs";
|
|
602
|
+
import path3 from "path";
|
|
603
|
+
var DependencyResolver = class {
|
|
604
|
+
/**
|
|
605
|
+
* Resolves table dependencies and creates execution order
|
|
606
|
+
*/
|
|
607
|
+
static resolveDependencies(cubeFiles, cubeType = "table") {
|
|
608
|
+
const tableDependencies = this.extractDependencies(cubeFiles, cubeType);
|
|
609
|
+
const orderedTables = this.topologicalSort(tableDependencies);
|
|
610
|
+
const executionOrder = {
|
|
611
|
+
tables: cubeType === "table" ? orderedTables : [],
|
|
612
|
+
seeders: cubeType === "seeder" ? orderedTables : [],
|
|
613
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
614
|
+
};
|
|
615
|
+
this.saveExecutionOrder(executionOrder);
|
|
616
|
+
return executionOrder;
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Extracts dependencies from cube files
|
|
620
|
+
*/
|
|
621
|
+
static extractDependencies(cubeFiles, cubeType) {
|
|
622
|
+
const dependencies = [];
|
|
623
|
+
for (const file of cubeFiles) {
|
|
624
|
+
let filePath;
|
|
625
|
+
if (path3.isAbsolute(file)) {
|
|
626
|
+
filePath = file;
|
|
627
|
+
} else if (fs4.existsSync(file)) {
|
|
628
|
+
filePath = path3.resolve(file);
|
|
629
|
+
} else {
|
|
630
|
+
filePath = path3.join(process.cwd(), "dbcube", "cubes", file);
|
|
631
|
+
}
|
|
632
|
+
try {
|
|
633
|
+
const tableNameResult = FileUtils_default.extracTableNameFromCube(filePath);
|
|
634
|
+
const tableName = tableNameResult.status === 200 ? tableNameResult.message : path3.basename(file, `.${cubeType}.cube`);
|
|
635
|
+
const deps = this.extractForeignKeyReferences(filePath);
|
|
636
|
+
dependencies.push({
|
|
637
|
+
tableName,
|
|
638
|
+
filePath,
|
|
639
|
+
dependencies: deps
|
|
640
|
+
});
|
|
641
|
+
} catch (error) {
|
|
642
|
+
console.error(`Error processing ${filePath}:`, error);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
return dependencies;
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Extracts foreign key references from a cube file
|
|
649
|
+
*/
|
|
650
|
+
static extractForeignKeyReferences(filePath) {
|
|
651
|
+
const dependencies = [];
|
|
652
|
+
try {
|
|
653
|
+
const content = fs4.readFileSync(filePath, "utf8");
|
|
654
|
+
const lines = content.split("\n");
|
|
655
|
+
let insideForeignKey = false;
|
|
656
|
+
let braceCount = 0;
|
|
657
|
+
for (const line of lines) {
|
|
658
|
+
if (/foreign\s*:\s*\{/.test(line)) {
|
|
659
|
+
insideForeignKey = true;
|
|
660
|
+
braceCount = 1;
|
|
661
|
+
const sameLineMatch = line.match(/table\s*:\s*["']([^"']+)["']/);
|
|
662
|
+
if (sameLineMatch) {
|
|
663
|
+
dependencies.push(sameLineMatch[1]);
|
|
664
|
+
insideForeignKey = false;
|
|
665
|
+
braceCount = 0;
|
|
666
|
+
}
|
|
667
|
+
continue;
|
|
668
|
+
}
|
|
669
|
+
if (insideForeignKey) {
|
|
670
|
+
braceCount += (line.match(/\{/g) || []).length;
|
|
671
|
+
braceCount -= (line.match(/\}/g) || []).length;
|
|
672
|
+
const tableMatch = line.match(/table\s*:\s*["']([^"']+)["']/);
|
|
673
|
+
if (tableMatch) {
|
|
674
|
+
dependencies.push(tableMatch[1]);
|
|
675
|
+
}
|
|
676
|
+
if (braceCount === 0) {
|
|
677
|
+
insideForeignKey = false;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
} catch (error) {
|
|
682
|
+
console.error(`Error reading file ${filePath}:`, error);
|
|
683
|
+
}
|
|
684
|
+
return dependencies;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Performs topological sort to determine execution order
|
|
688
|
+
*/
|
|
689
|
+
static topologicalSort(dependencies) {
|
|
690
|
+
const graph = /* @__PURE__ */ new Map();
|
|
691
|
+
const inDegree = /* @__PURE__ */ new Map();
|
|
692
|
+
const tableMap = /* @__PURE__ */ new Map();
|
|
693
|
+
for (const dep of dependencies) {
|
|
694
|
+
graph.set(dep.tableName, dep.dependencies);
|
|
695
|
+
inDegree.set(dep.tableName, 0);
|
|
696
|
+
tableMap.set(dep.tableName, dep);
|
|
697
|
+
}
|
|
698
|
+
for (const dep of dependencies) {
|
|
699
|
+
for (const dependency of dep.dependencies) {
|
|
700
|
+
if (inDegree.has(dependency)) {
|
|
701
|
+
inDegree.set(dep.tableName, (inDegree.get(dep.tableName) || 0) + 1);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
const queue = [];
|
|
706
|
+
const result = [];
|
|
707
|
+
for (const [table, degree] of inDegree) {
|
|
708
|
+
if (degree === 0) {
|
|
709
|
+
queue.push(table);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
while (queue.length > 0) {
|
|
713
|
+
const current = queue.shift();
|
|
714
|
+
result.push(current);
|
|
715
|
+
const currentDeps = graph.get(current) || [];
|
|
716
|
+
for (const neighbor of currentDeps) {
|
|
717
|
+
if (inDegree.has(neighbor)) {
|
|
718
|
+
const newDegree = (inDegree.get(neighbor) || 0) - 1;
|
|
719
|
+
inDegree.set(neighbor, newDegree);
|
|
720
|
+
if (newDegree === 0) {
|
|
721
|
+
queue.push(neighbor);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
if (result.length !== dependencies.length) {
|
|
727
|
+
for (const dep of dependencies) {
|
|
728
|
+
if (!result.includes(dep.tableName)) {
|
|
729
|
+
result.push(dep.tableName);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
return result;
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* Saves the execution order to .dbcube/orderexecute.json
|
|
737
|
+
*/
|
|
738
|
+
static saveExecutionOrder(order) {
|
|
739
|
+
try {
|
|
740
|
+
const projectRoot = process.cwd();
|
|
741
|
+
const dbcubeDir = path3.join(projectRoot, ".dbcube");
|
|
742
|
+
const orderFile = path3.join(dbcubeDir, "orderexecute.json");
|
|
743
|
+
if (!fs4.existsSync(dbcubeDir)) {
|
|
744
|
+
fs4.mkdirSync(dbcubeDir, { recursive: true });
|
|
745
|
+
}
|
|
746
|
+
fs4.writeFileSync(orderFile, JSON.stringify(order, null, 2), "utf8");
|
|
747
|
+
} catch (error) {
|
|
748
|
+
console.error("\u274C Failed to save execution order:", error);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Loads the execution order from .dbcube/orderexecute.json
|
|
753
|
+
*/
|
|
754
|
+
static loadExecutionOrder() {
|
|
755
|
+
try {
|
|
756
|
+
const projectRoot = process.cwd();
|
|
757
|
+
const orderFile = path3.join(projectRoot, ".dbcube", "orderexecute.json");
|
|
758
|
+
if (!fs4.existsSync(orderFile)) {
|
|
759
|
+
return null;
|
|
760
|
+
}
|
|
761
|
+
const content = fs4.readFileSync(orderFile, "utf8");
|
|
762
|
+
return JSON.parse(content);
|
|
763
|
+
} catch (error) {
|
|
764
|
+
console.error("\u274C Failed to load execution order:", error);
|
|
765
|
+
return null;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
/**
|
|
769
|
+
* Orders cube files based on saved execution order
|
|
770
|
+
*/
|
|
771
|
+
static orderCubeFiles(cubeFiles, cubeType) {
|
|
772
|
+
const executionOrder = this.loadExecutionOrder();
|
|
773
|
+
if (!executionOrder) {
|
|
774
|
+
return cubeFiles;
|
|
775
|
+
}
|
|
776
|
+
const orderList = cubeType === "table" ? executionOrder.tables : executionOrder.seeders;
|
|
777
|
+
const orderedFiles = [];
|
|
778
|
+
const fileMap = /* @__PURE__ */ new Map();
|
|
779
|
+
for (const file of cubeFiles) {
|
|
780
|
+
const filePath = path3.isAbsolute(file) ? file : path3.join(process.cwd(), "dbcube", "cubes", file);
|
|
781
|
+
const tableNameResult = FileUtils_default.extracTableNameFromCube(filePath);
|
|
782
|
+
const tableName = tableNameResult.status === 200 ? tableNameResult.message : path3.basename(file, `.${cubeType}.cube`);
|
|
783
|
+
fileMap.set(tableName, file);
|
|
784
|
+
}
|
|
785
|
+
for (const tableName of orderList) {
|
|
786
|
+
if (fileMap.has(tableName)) {
|
|
787
|
+
orderedFiles.push(fileMap.get(tableName));
|
|
788
|
+
fileMap.delete(tableName);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
for (const [, file] of fileMap) {
|
|
792
|
+
orderedFiles.push(file);
|
|
793
|
+
}
|
|
794
|
+
return orderedFiles;
|
|
795
|
+
}
|
|
563
796
|
};
|
|
564
797
|
|
|
565
798
|
// src/lib/Schema.ts
|
|
@@ -591,7 +824,7 @@ var Schema = class {
|
|
|
591
824
|
return {
|
|
592
825
|
isValid: false,
|
|
593
826
|
error: {
|
|
594
|
-
itemName:
|
|
827
|
+
itemName: path4.basename(filePath, path4.extname(filePath)),
|
|
595
828
|
error: `Error reading database directive: ${dbResult.message}`,
|
|
596
829
|
filePath,
|
|
597
830
|
lineNumber: this.findDatabaseLineNumber(filePath)
|
|
@@ -600,7 +833,7 @@ var Schema = class {
|
|
|
600
833
|
}
|
|
601
834
|
const cubeDbName = dbResult.message;
|
|
602
835
|
const configInstance = new ConfigClass();
|
|
603
|
-
const configFilePath =
|
|
836
|
+
const configFilePath = path4.resolve(process.cwd(), "dbcube.config.js");
|
|
604
837
|
const configFn = __require(configFilePath);
|
|
605
838
|
if (typeof configFn === "function") {
|
|
606
839
|
configFn(configInstance);
|
|
@@ -627,7 +860,7 @@ var Schema = class {
|
|
|
627
860
|
return {
|
|
628
861
|
isValid: false,
|
|
629
862
|
error: {
|
|
630
|
-
itemName:
|
|
863
|
+
itemName: path4.basename(filePath, path4.extname(filePath)),
|
|
631
864
|
error: `Database configuration '${cubeDbName}' not found in dbcube.config.js. Available: ${availableText}`,
|
|
632
865
|
filePath,
|
|
633
866
|
lineNumber: this.findDatabaseLineNumber(filePath)
|
|
@@ -639,7 +872,7 @@ var Schema = class {
|
|
|
639
872
|
return {
|
|
640
873
|
isValid: false,
|
|
641
874
|
error: {
|
|
642
|
-
itemName:
|
|
875
|
+
itemName: path4.basename(filePath, path4.extname(filePath)),
|
|
643
876
|
error: `Database configuration validation failed: ${error.message}`,
|
|
644
877
|
filePath,
|
|
645
878
|
lineNumber: this.findDatabaseLineNumber(filePath)
|
|
@@ -652,7 +885,7 @@ var Schema = class {
|
|
|
652
885
|
*/
|
|
653
886
|
findDatabaseLineNumber(filePath) {
|
|
654
887
|
try {
|
|
655
|
-
const content =
|
|
888
|
+
const content = fs5.readFileSync(filePath, "utf8");
|
|
656
889
|
const lines = content.split("\n");
|
|
657
890
|
for (let i = 0; i < lines.length; i++) {
|
|
658
891
|
if (lines[i].includes("@database")) {
|
|
@@ -664,9 +897,65 @@ var Schema = class {
|
|
|
664
897
|
return 1;
|
|
665
898
|
}
|
|
666
899
|
}
|
|
900
|
+
/**
|
|
901
|
+
* Extracts foreign key dependencies from a cube file
|
|
902
|
+
*/
|
|
903
|
+
extractForeignKeyDependencies(filePath) {
|
|
904
|
+
const dependencies = [];
|
|
905
|
+
try {
|
|
906
|
+
const content = fs5.readFileSync(filePath, "utf8");
|
|
907
|
+
const lines = content.split("\n");
|
|
908
|
+
let insideForeignKey = false;
|
|
909
|
+
let braceCount = 0;
|
|
910
|
+
for (const line of lines) {
|
|
911
|
+
if (/foreign\s*:\s*\{/.test(line)) {
|
|
912
|
+
insideForeignKey = true;
|
|
913
|
+
braceCount = 1;
|
|
914
|
+
const sameLineMatch = line.match(/table\s*:\s*["']([^"']+)["']/);
|
|
915
|
+
if (sameLineMatch) {
|
|
916
|
+
dependencies.push(sameLineMatch[1]);
|
|
917
|
+
insideForeignKey = false;
|
|
918
|
+
braceCount = 0;
|
|
919
|
+
}
|
|
920
|
+
continue;
|
|
921
|
+
}
|
|
922
|
+
if (insideForeignKey) {
|
|
923
|
+
braceCount += (line.match(/\{/g) || []).length;
|
|
924
|
+
braceCount -= (line.match(/\}/g) || []).length;
|
|
925
|
+
const tableMatch = line.match(/table\s*:\s*["']([^"']+)["']/);
|
|
926
|
+
if (tableMatch) {
|
|
927
|
+
dependencies.push(tableMatch[1]);
|
|
928
|
+
}
|
|
929
|
+
if (braceCount === 0) {
|
|
930
|
+
insideForeignKey = false;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
} catch (error) {
|
|
935
|
+
console.error(`Error reading dependencies from ${filePath}:`, error);
|
|
936
|
+
}
|
|
937
|
+
return dependencies;
|
|
938
|
+
}
|
|
939
|
+
/**
|
|
940
|
+
* Finds the line number where a foreign key table reference is located
|
|
941
|
+
*/
|
|
942
|
+
findForeignKeyLineNumber(filePath, tableName) {
|
|
943
|
+
try {
|
|
944
|
+
const content = fs5.readFileSync(filePath, "utf8");
|
|
945
|
+
const lines = content.split("\n");
|
|
946
|
+
for (let i = 0; i < lines.length; i++) {
|
|
947
|
+
if (lines[i].includes(`table: "${tableName}"`) || lines[i].includes(`table: '${tableName}'`)) {
|
|
948
|
+
return i + 1;
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
return 1;
|
|
952
|
+
} catch {
|
|
953
|
+
return 1;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
667
956
|
async createDatabase() {
|
|
668
957
|
const startTime = Date.now();
|
|
669
|
-
const rootPath =
|
|
958
|
+
const rootPath = path4.resolve(process.cwd());
|
|
670
959
|
UIUtils.showOperationHeader(" CREATING DATABASE", this.name, "\u{1F5C4}\uFE0F");
|
|
671
960
|
await UIUtils.showItemProgress("Preparando e instalando base de datos", 1, 1);
|
|
672
961
|
try {
|
|
@@ -710,33 +999,52 @@ var Schema = class {
|
|
|
710
999
|
}
|
|
711
1000
|
async refreshTables() {
|
|
712
1001
|
const startTime = Date.now();
|
|
713
|
-
const cubesDir =
|
|
714
|
-
if (!
|
|
1002
|
+
const cubesDir = path4.join(process.cwd(), "dbcube", "cubes");
|
|
1003
|
+
if (!fs5.existsSync(cubesDir)) {
|
|
715
1004
|
throw new Error("\u274C The cubes folder does not exist");
|
|
716
1005
|
}
|
|
717
1006
|
const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", "table.cube");
|
|
718
1007
|
if (cubeFiles.length === 0) {
|
|
719
1008
|
throw new Error("\u274C There are no cubes to execute");
|
|
720
1009
|
}
|
|
1010
|
+
DependencyResolver.resolveDependencies(cubeFiles, "table");
|
|
1011
|
+
const orderedCubeFiles = DependencyResolver.orderCubeFiles(cubeFiles, "table");
|
|
721
1012
|
UIUtils.showOperationHeader("EXECUTING REFRESH TABLES", this.name, "\u{1F504}");
|
|
722
1013
|
let totalTablesProcessed = 0;
|
|
723
1014
|
let successCount = 0;
|
|
724
1015
|
let errorCount = 0;
|
|
725
1016
|
const processedTables = [];
|
|
726
1017
|
const errors = [];
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
const
|
|
730
|
-
const
|
|
1018
|
+
const failedTables = /* @__PURE__ */ new Set();
|
|
1019
|
+
for (let index = 0; index < orderedCubeFiles.length; index++) {
|
|
1020
|
+
const file = orderedCubeFiles[index];
|
|
1021
|
+
const filePath = path4.isAbsolute(file) ? file : path4.join(cubesDir, file);
|
|
1022
|
+
const stats = fs5.statSync(filePath);
|
|
731
1023
|
if (stats.isFile()) {
|
|
732
1024
|
const getTableName = FileUtils_default.extracTableNameFromCube(filePath);
|
|
733
|
-
const tableName = getTableName.status === 200 ? getTableName.message :
|
|
734
|
-
await UIUtils.showItemProgress(tableName, index + 1,
|
|
1025
|
+
const tableName = getTableName.status === 200 ? getTableName.message : path4.basename(file, ".table.cube");
|
|
1026
|
+
await UIUtils.showItemProgress(tableName, index + 1, orderedCubeFiles.length);
|
|
735
1027
|
try {
|
|
736
1028
|
const validation = this.validateDatabaseConfiguration(filePath);
|
|
737
1029
|
if (!validation.isValid && validation.error) {
|
|
738
1030
|
UIUtils.showItemError(tableName, validation.error.error);
|
|
739
1031
|
errors.push(validation.error);
|
|
1032
|
+
failedTables.add(tableName);
|
|
1033
|
+
errorCount++;
|
|
1034
|
+
continue;
|
|
1035
|
+
}
|
|
1036
|
+
const dependencies = this.extractForeignKeyDependencies(filePath);
|
|
1037
|
+
const missingDependencies = dependencies.filter((dep) => failedTables.has(dep));
|
|
1038
|
+
if (missingDependencies.length > 0) {
|
|
1039
|
+
const dependencyError = {
|
|
1040
|
+
itemName: tableName,
|
|
1041
|
+
error: `Cannot refresh table '${tableName}' because it depends on failed table(s): ${missingDependencies.join(", ")}`,
|
|
1042
|
+
filePath,
|
|
1043
|
+
lineNumber: this.findForeignKeyLineNumber(filePath, missingDependencies[0])
|
|
1044
|
+
};
|
|
1045
|
+
UIUtils.showItemError(tableName, dependencyError.error);
|
|
1046
|
+
errors.push(dependencyError);
|
|
1047
|
+
failedTables.add(tableName);
|
|
740
1048
|
errorCount++;
|
|
741
1049
|
continue;
|
|
742
1050
|
}
|
|
@@ -793,6 +1101,7 @@ var Schema = class {
|
|
|
793
1101
|
};
|
|
794
1102
|
UIUtils.showItemError(tableName, error.message);
|
|
795
1103
|
errors.push(processError);
|
|
1104
|
+
failedTables.add(tableName);
|
|
796
1105
|
errorCount++;
|
|
797
1106
|
}
|
|
798
1107
|
}
|
|
@@ -812,33 +1121,52 @@ var Schema = class {
|
|
|
812
1121
|
}
|
|
813
1122
|
async freshTables() {
|
|
814
1123
|
const startTime = Date.now();
|
|
815
|
-
const cubesDir =
|
|
816
|
-
if (!
|
|
1124
|
+
const cubesDir = path4.join(process.cwd(), "dbcube", "cubes");
|
|
1125
|
+
if (!fs5.existsSync(cubesDir)) {
|
|
817
1126
|
throw new Error("\u274C The cubes folder does not exist");
|
|
818
1127
|
}
|
|
819
1128
|
const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", "table.cube");
|
|
820
1129
|
if (cubeFiles.length === 0) {
|
|
821
1130
|
throw new Error("\u274C There are no cubes to execute");
|
|
822
1131
|
}
|
|
1132
|
+
DependencyResolver.resolveDependencies(cubeFiles, "table");
|
|
1133
|
+
const orderedCubeFiles = DependencyResolver.orderCubeFiles(cubeFiles, "table");
|
|
823
1134
|
UIUtils.showOperationHeader("EXECUTING FRESH TABLES", this.name);
|
|
824
1135
|
let totalTablesProcessed = 0;
|
|
825
1136
|
let successCount = 0;
|
|
826
1137
|
let errorCount = 0;
|
|
827
1138
|
const processedTables = [];
|
|
828
1139
|
const errors = [];
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
const
|
|
832
|
-
const
|
|
1140
|
+
const failedTables = /* @__PURE__ */ new Set();
|
|
1141
|
+
for (let index = 0; index < orderedCubeFiles.length; index++) {
|
|
1142
|
+
const file = orderedCubeFiles[index];
|
|
1143
|
+
const filePath = path4.isAbsolute(file) ? file : path4.join(cubesDir, file);
|
|
1144
|
+
const stats = fs5.statSync(filePath);
|
|
833
1145
|
if (stats.isFile()) {
|
|
834
1146
|
const getTableName = FileUtils_default.extracTableNameFromCube(filePath);
|
|
835
|
-
const tableName = getTableName.status === 200 ? getTableName.message :
|
|
836
|
-
await UIUtils.showItemProgress(tableName, index + 1,
|
|
1147
|
+
const tableName = getTableName.status === 200 ? getTableName.message : path4.basename(file, ".table.cube");
|
|
1148
|
+
await UIUtils.showItemProgress(tableName, index + 1, orderedCubeFiles.length);
|
|
837
1149
|
try {
|
|
838
1150
|
const validation = this.validateDatabaseConfiguration(filePath);
|
|
839
1151
|
if (!validation.isValid && validation.error) {
|
|
840
1152
|
UIUtils.showItemError(tableName, validation.error.error);
|
|
841
1153
|
errors.push(validation.error);
|
|
1154
|
+
failedTables.add(tableName);
|
|
1155
|
+
errorCount++;
|
|
1156
|
+
continue;
|
|
1157
|
+
}
|
|
1158
|
+
const dependencies = this.extractForeignKeyDependencies(filePath);
|
|
1159
|
+
const missingDependencies = dependencies.filter((dep) => failedTables.has(dep));
|
|
1160
|
+
if (missingDependencies.length > 0) {
|
|
1161
|
+
const dependencyError = {
|
|
1162
|
+
itemName: tableName,
|
|
1163
|
+
error: `Cannot create table '${tableName}' because it depends on failed table(s): ${missingDependencies.join(", ")}`,
|
|
1164
|
+
filePath,
|
|
1165
|
+
lineNumber: this.findForeignKeyLineNumber(filePath, missingDependencies[0])
|
|
1166
|
+
};
|
|
1167
|
+
UIUtils.showItemError(tableName, dependencyError.error);
|
|
1168
|
+
errors.push(dependencyError);
|
|
1169
|
+
failedTables.add(tableName);
|
|
842
1170
|
errorCount++;
|
|
843
1171
|
continue;
|
|
844
1172
|
}
|
|
@@ -893,6 +1221,7 @@ var Schema = class {
|
|
|
893
1221
|
};
|
|
894
1222
|
UIUtils.showItemError(tableName, error.message);
|
|
895
1223
|
errors.push(processError);
|
|
1224
|
+
failedTables.add(tableName);
|
|
896
1225
|
errorCount++;
|
|
897
1226
|
}
|
|
898
1227
|
}
|
|
@@ -912,28 +1241,29 @@ var Schema = class {
|
|
|
912
1241
|
}
|
|
913
1242
|
async executeSeeders() {
|
|
914
1243
|
const startTime = Date.now();
|
|
915
|
-
const cubesDir =
|
|
916
|
-
if (!
|
|
1244
|
+
const cubesDir = path4.join(process.cwd(), "dbcube", "cubes");
|
|
1245
|
+
if (!fs5.existsSync(cubesDir)) {
|
|
917
1246
|
throw new Error("\u274C The cubes folder does not exist");
|
|
918
1247
|
}
|
|
919
1248
|
const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", "seeder.cube");
|
|
920
1249
|
if (cubeFiles.length === 0) {
|
|
921
1250
|
throw new Error("\u274C There are no cubes to execute");
|
|
922
1251
|
}
|
|
1252
|
+
const orderedCubeFiles = DependencyResolver.orderCubeFiles(cubeFiles, "seeder");
|
|
923
1253
|
UIUtils.showOperationHeader("EXECUTING SEEDERS", this.name, "\u{1F331}");
|
|
924
1254
|
let totalSeedersProcessed = 0;
|
|
925
1255
|
let successCount = 0;
|
|
926
1256
|
let errorCount = 0;
|
|
927
1257
|
const processedSeeders = [];
|
|
928
1258
|
const errors = [];
|
|
929
|
-
for (let index = 0; index <
|
|
930
|
-
const file =
|
|
931
|
-
const filePath =
|
|
932
|
-
const stats =
|
|
1259
|
+
for (let index = 0; index < orderedCubeFiles.length; index++) {
|
|
1260
|
+
const file = orderedCubeFiles[index];
|
|
1261
|
+
const filePath = path4.isAbsolute(file) ? file : path4.join(cubesDir, file);
|
|
1262
|
+
const stats = fs5.statSync(filePath);
|
|
933
1263
|
if (stats.isFile()) {
|
|
934
1264
|
const getSeederName = FileUtils_default.extracTableNameFromCube(filePath);
|
|
935
|
-
const seederName = getSeederName.status === 200 ? getSeederName.message :
|
|
936
|
-
await UIUtils.showItemProgress(seederName, index + 1,
|
|
1265
|
+
const seederName = getSeederName.status === 200 ? getSeederName.message : path4.basename(file, ".seeder.cube");
|
|
1266
|
+
await UIUtils.showItemProgress(seederName, index + 1, orderedCubeFiles.length);
|
|
937
1267
|
try {
|
|
938
1268
|
const validation = this.validateDatabaseConfiguration(filePath);
|
|
939
1269
|
if (!validation.isValid && validation.error) {
|
|
@@ -983,9 +1313,9 @@ var Schema = class {
|
|
|
983
1313
|
}
|
|
984
1314
|
async executeTriggers() {
|
|
985
1315
|
const startTime = Date.now();
|
|
986
|
-
const cubesDir =
|
|
987
|
-
const triggersDirExit =
|
|
988
|
-
if (!
|
|
1316
|
+
const cubesDir = path4.join(process.cwd(), "dbcube", "cubes");
|
|
1317
|
+
const triggersDirExit = path4.join(process.cwd(), "dbcube", "triggers");
|
|
1318
|
+
if (!fs5.existsSync(cubesDir)) {
|
|
989
1319
|
throw new Error("\u274C The cubes folder does not exist");
|
|
990
1320
|
}
|
|
991
1321
|
const cubeFiles = FileUtils_default.getCubeFilesRecursively("dbcube", "trigger.cube");
|
|
@@ -1000,11 +1330,11 @@ var Schema = class {
|
|
|
1000
1330
|
const errors = [];
|
|
1001
1331
|
for (let index = 0; index < cubeFiles.length; index++) {
|
|
1002
1332
|
const file = cubeFiles[index];
|
|
1003
|
-
const filePath =
|
|
1004
|
-
const stats =
|
|
1333
|
+
const filePath = path4.isAbsolute(file) ? file : path4.join(cubesDir, file);
|
|
1334
|
+
const stats = fs5.statSync(filePath);
|
|
1005
1335
|
if (stats.isFile()) {
|
|
1006
1336
|
const getTriggerName = FileUtils_default.extracTableNameFromCube(filePath);
|
|
1007
|
-
const triggerName = getTriggerName.status === 200 ? getTriggerName.message :
|
|
1337
|
+
const triggerName = getTriggerName.status === 200 ? getTriggerName.message : path4.basename(file, ".trigger.cube");
|
|
1008
1338
|
await UIUtils.showItemProgress(triggerName, index + 1, cubeFiles.length);
|
|
1009
1339
|
try {
|
|
1010
1340
|
const validation = this.validateDatabaseConfiguration(filePath);
|
|
@@ -1075,7 +1405,7 @@ ${chalk2.red("\u{1F6AB}")} ${chalk2.bold.red("ERRORS FOUND")}`);
|
|
|
1075
1405
|
const errorLocation = `${filePath}:${lineStr}:${columnStr}`;
|
|
1076
1406
|
console.log(`${chalk2.cyan("[code]")} ${chalk2.yellow(errorLocation)}`);
|
|
1077
1407
|
try {
|
|
1078
|
-
const codeLines =
|
|
1408
|
+
const codeLines = fs5.readFileSync(filePath, "utf-8").split("\n");
|
|
1079
1409
|
const start = Math.max(0, lineNum - 3);
|
|
1080
1410
|
const end = Math.min(codeLines.length, lineNum + 2);
|
|
1081
1411
|
for (let i = start; i < end; i++) {
|
|
@@ -1097,6 +1427,7 @@ ${chalk2.red("\u{1F6AB}")} ${chalk2.bold.red("ERRORS FOUND")}`);
|
|
|
1097
1427
|
var index_default = Schema;
|
|
1098
1428
|
export {
|
|
1099
1429
|
CubeValidator,
|
|
1430
|
+
DependencyResolver,
|
|
1100
1431
|
Schema,
|
|
1101
1432
|
UIUtils,
|
|
1102
1433
|
index_default as default
|