ddl-manager
Version:
store postgres procedures and triggers in files
70 lines • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildDependencyMatrix = exports.groupByTables = void 0;
const lodash_1 = require("lodash");
function groupByTables(columns) {
const byTables = {};
for (const column of columns) {
const table = column.getTableId();
const tableColumns = byTables[table] || [];
byTables[table] = tableColumns;
tableColumns.push(column);
}
return byTables;
}
exports.groupByTables = groupByTables;
function buildDependencyMatrix(rootColumns) {
const matrix = [];
addNextLevels(matrix, rootColumns);
return removeDuplicates(matrix);
}
exports.buildDependencyMatrix = buildDependencyMatrix;
function addNextLevels(matrix, prevLevel) {
const circularLevels = splitByCircularDeps(prevLevel);
matrix.push(...circularLevels);
const nextLevel = lodash_1.flatMap(lodash_1.flatten(circularLevels), column => column.findNotCircularUses());
if (nextLevel.length > 0) {
addNextLevels(matrix, nextLevel);
}
}
function splitByCircularDeps(level) {
const prevLevel = [];
const nextLevel = [];
for (const column of level) {
const circularDeps = column.findCircularUses();
if (circularDeps.length === 0) {
prevLevel.push(column);
continue;
}
if (column.hasForeignTablesDeps()) {
prevLevel.push(column);
nextLevel.push(...circularDeps);
}
else {
prevLevel.push(...circularDeps);
nextLevel.push(column);
}
}
return [prevLevel, nextLevel].filter(level => level.length > 0);
}
function removeDuplicates(matrix) {
const known = {};
let newMatrix = [];
for (let n = matrix.length, i = n - 1; i >= 0; i--) {
const line = matrix[i];
const newLine = [];
for (let m = line.length, j = m - 1; j >= 0; j--) {
const column = line[j];
const isDuplicate = column.getId() in known;
if (isDuplicate) {
continue;
}
newLine.unshift(column);
known[column.getId()] = true;
}
newMatrix[i] = newLine;
}
newMatrix = newMatrix.filter(line => line.length > 0);
return newMatrix;
}
//# sourceMappingURL=utils.js.map