UNPKG

dbgate-tools

Version:

Auxiliary tools for other DbGate packages.

158 lines (157 loc) 9.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCreateObjectScript = exports.computeObjectDiffOperations = exports.getOperationDisplay = exports.computeTableDiffColumns = exports.computeDbDiffRows = exports.DbDiffCompareDefs = exports.computeDiffRowsCore = void 0; const diffTools_1 = require("./diffTools"); const lodash_1 = __importDefault(require("lodash")); const structureTools_1 = require("./structureTools"); const alterPlan_1 = require("./alterPlan"); function computeDiffRowsCore(sourceList, targetList, testEqual) { const res = []; for (const obj of sourceList) { const paired = targetList.find(x => x.pairingId == obj.pairingId); if (paired) { const isEqual = testEqual(obj, paired); res.push({ source: obj, target: paired, state: isEqual ? 'equal' : 'changed', __isChanged: !isEqual, }); } else { res.push({ source: obj, state: 'added', __isAdded: true, }); } } for (const obj of targetList) { const paired = sourceList.find(x => x.pairingId == obj.pairingId); if (!paired) { res.push({ target: obj, state: 'removed', __isDeleted: true, }); } } return res; } exports.computeDiffRowsCore = computeDiffRowsCore; exports.DbDiffCompareDefs = { tables: { test: diffTools_1.testEqualTables, name: 'Table', plural: 'Tables', icon: 'img table', }, views: { test: diffTools_1.testEqualSqlObjects, name: 'View', plural: 'Views', icon: 'img view', }, matviews: { test: diffTools_1.testEqualSqlObjects, name: 'Materialized view', plural: 'Materialized views', icon: 'img view', }, procedures: { test: diffTools_1.testEqualSqlObjects, name: 'Procedure', plural: 'Procedures', icon: 'img procedure', }, functions: { test: diffTools_1.testEqualSqlObjects, name: 'Function', plural: 'Functions', icon: 'img function', }, }; function computeDbDiffRows(sourceDb, targetDb, opts, driver) { if (!sourceDb || !targetDb || !driver) return []; const res = []; for (const objectTypeField of ['tables', 'views', 'procedures', 'matviews', 'functions']) { const defs = exports.DbDiffCompareDefs[objectTypeField]; res.push(...lodash_1.default.sortBy(computeDiffRowsCore(sourceDb[objectTypeField], targetDb[objectTypeField], (a, b) => defs.test(a, b, opts, sourceDb, targetDb, driver)).map(row => { var _a, _b, _c, _d, _e, _f, _g, _h; return ({ ...row, sourceSchemaName: (_a = row === null || row === void 0 ? void 0 : row.source) === null || _a === void 0 ? void 0 : _a.schemaName, sourcePureName: (_b = row === null || row === void 0 ? void 0 : row.source) === null || _b === void 0 ? void 0 : _b.pureName, targetSchemaName: (_c = row === null || row === void 0 ? void 0 : row.target) === null || _c === void 0 ? void 0 : _c.schemaName, targetPureName: (_d = row === null || row === void 0 ? void 0 : row.target) === null || _d === void 0 ? void 0 : _d.pureName, typeName: defs.name, typeIcon: defs.icon, identifier: `${((_e = row === null || row === void 0 ? void 0 : row.source) === null || _e === void 0 ? void 0 : _e.schemaName) || ((_f = row === null || row === void 0 ? void 0 : row.target) === null || _f === void 0 ? void 0 : _f.schemaName)}.${((_g = row === null || row === void 0 ? void 0 : row.source) === null || _g === void 0 ? void 0 : _g.pureName) || ((_h = row === null || row === void 0 ? void 0 : row.target) === null || _h === void 0 ? void 0 : _h.pureName)}`, objectTypeField, }); }), 'identifier')); } return res; } exports.computeDbDiffRows = computeDbDiffRows; function computeTableDiffColumns(sourceTable, targetTable, opts, driver) { if (!driver) return []; return computeDiffRowsCore((sourceTable === null || sourceTable === void 0 ? void 0 : sourceTable.columns) || [], (targetTable === null || targetTable === void 0 ? void 0 : targetTable.columns) || [], (a, b) => (0, diffTools_1.testEqualColumns)(a, b, true, true, opts)).map(row => { var _a, _b, _c, _d, _e, _f, _g, _h; return ({ ...row, sourceColumnName: (_a = row === null || row === void 0 ? void 0 : row.source) === null || _a === void 0 ? void 0 : _a.columnName, targetColumnName: (_b = row === null || row === void 0 ? void 0 : row.target) === null || _b === void 0 ? void 0 : _b.columnName, sourceDataType: (_c = row === null || row === void 0 ? void 0 : row.source) === null || _c === void 0 ? void 0 : _c.dataType, targetDataType: (_d = row === null || row === void 0 ? void 0 : row.target) === null || _d === void 0 ? void 0 : _d.dataType, sourceAutoIncrement: (_e = row === null || row === void 0 ? void 0 : row.source) === null || _e === void 0 ? void 0 : _e.autoIncrement, targetAutoIncrement: (_f = row === null || row === void 0 ? void 0 : row.target) === null || _f === void 0 ? void 0 : _f.autoIncrement, sourceNotNull: (_g = row === null || row === void 0 ? void 0 : row.source) === null || _g === void 0 ? void 0 : _g.notNull, targetNotNull: (_h = row === null || row === void 0 ? void 0 : row.target) === null || _h === void 0 ? void 0 : _h.notNull, }); }); } exports.computeTableDiffColumns = computeTableDiffColumns; function getOperationDisplay(operation, driver) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v; const op = operation; const name = (_u = (_s = (_q = (_o = (_l = (_j = (_g = (_e = (_c = (_a = op === null || op === void 0 ? void 0 : op.newName) !== null && _a !== void 0 ? _a : (_b = op === null || op === void 0 ? void 0 : op.newObject) === null || _b === void 0 ? void 0 : _b.columnName) !== null && _c !== void 0 ? _c : (_d = op === null || op === void 0 ? void 0 : op.newObject) === null || _d === void 0 ? void 0 : _d.constraintName) !== null && _e !== void 0 ? _e : (_f = op === null || op === void 0 ? void 0 : op.newObject) === null || _f === void 0 ? void 0 : _f.pureName) !== null && _g !== void 0 ? _g : (_h = op === null || op === void 0 ? void 0 : op.oldObject) === null || _h === void 0 ? void 0 : _h.columnName) !== null && _j !== void 0 ? _j : (_k = op === null || op === void 0 ? void 0 : op.oldObject) === null || _k === void 0 ? void 0 : _k.constraintName) !== null && _l !== void 0 ? _l : (_m = op === null || op === void 0 ? void 0 : op.oldObject) === null || _m === void 0 ? void 0 : _m.pureName) !== null && _o !== void 0 ? _o : (_p = op === null || op === void 0 ? void 0 : op.table) === null || _p === void 0 ? void 0 : _p.pureName) !== null && _q !== void 0 ? _q : (_r = op === null || op === void 0 ? void 0 : op.object) === null || _r === void 0 ? void 0 : _r.columnName) !== null && _s !== void 0 ? _s : (_t = op === null || op === void 0 ? void 0 : op.object) === null || _t === void 0 ? void 0 : _t.constraintName) !== null && _u !== void 0 ? _u : (_v = op === null || op === void 0 ? void 0 : op.object) === null || _v === void 0 ? void 0 : _v.pureName; const dmp = driver.createDumper(); (0, alterPlan_1.runAlterOperation)(operation, dmp); return { operationType: operation.operationType, name, sqlScript: dmp.s, identifier: dmp.s, }; } exports.getOperationDisplay = getOperationDisplay; function computeObjectDiffOperations(sourceObject, targetObject, sourceDb, targetDb, opts, driver) { if (!driver) return []; const srcdb = sourceObject ? (0, structureTools_1.extendDatabaseInfo)({ [sourceObject.objectTypeField]: [sourceObject] }) : (0, structureTools_1.extendDatabaseInfo)({}); const dstdb = targetObject ? (0, structureTools_1.extendDatabaseInfo)({ [targetObject.objectTypeField]: [targetObject] }) : (0, structureTools_1.extendDatabaseInfo)({}); const plan = (0, diffTools_1.createAlterDatabasePlan)(dstdb, srcdb, opts, targetDb, sourceDb, driver); return plan.operations.map(item => getOperationDisplay(item, driver)); } exports.computeObjectDiffOperations = computeObjectDiffOperations; function getCreateObjectScript(obj, driver) { if (!obj || !driver) return ''; if (obj.objectTypeField == 'tables') { const dmp = driver.createDumper(); dmp.createTable(obj); return dmp.s; } return obj.createSql || ''; } exports.getCreateObjectScript = getCreateObjectScript;