UNPKG

ddl-manager

Version:

store postgres procedures and triggers in files

139 lines 4.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Comment = void 0; class Comment { constructor(params) { this.objectType = params.objectType; this.frozen = !!params.frozen; this.cacheSignature = params.cacheSignature; this.cacheSelect = params.cacheSelect; this.legacyInfo = params.legacyInfo; this.migrationIsStarted = params.migrationIsStarted; this.migrationIsFinished = params.migrationIsFinished; } static empty(objectType) { return new Comment({ objectType, frozen: false }); } static frozen(objectType) { return new Comment({ objectType, frozen: true }); } static fromTotalString(objectType, total) { total = total || ""; const frozen = (!total.includes("ddl-manager-sync") && !total.includes("ddl-manager-helper")); const cacheSignature = parseCacheSignature(total); const cacheSelect = parseCacheSelect(total); const legacyInfo = parseLegacyInfo(total); const migrationIsStarted = total.includes("ddl-manager-migration-is-started") || undefined; const migrationIsFinished = total.includes("ddl-manager-migration-is-finished") || undefined; const comment = new Comment({ objectType, frozen, cacheSignature, cacheSelect, legacyInfo, migrationIsStarted, migrationIsFinished }); return comment; } static fromFs(params) { return new Comment(Object.assign(Object.assign({}, params), { frozen: false })); } isEmpty() { return this.frozen && !this.cacheSelect; } equal(otherComment) { return (this.cacheSelect == otherComment.cacheSelect && this.cacheSignature == otherComment.cacheSignature); } markAsFrozen(legacyInfo) { return new Comment(Object.assign(Object.assign({}, this), { frozen: true, legacyInfo })); } hasNotFinishedMigration() { return this.migrationIsStarted && !this.migrationIsFinished; } startMigration() { return new Comment(Object.assign(Object.assign({}, this), { migrationIsStarted: true, migrationIsFinished: false })); } finishMigration() { return new Comment(Object.assign(Object.assign({}, this), { migrationIsStarted: true, migrationIsFinished: true })); } toString() { let comment = ""; if (!this.frozen) { comment += "\nddl-manager-sync"; } if (this.cacheSelect) { comment += `\nddl-manager-select(${this.cacheSelect})`; } if (this.cacheSignature) { comment += `\nddl-manager-cache(${this.cacheSignature})`; } if (this.legacyInfo) { comment += `\nddl-manager-legacy-info(${JSON.stringify(this.legacyInfo)})`; } if (this.migrationIsStarted) { comment += `\nddl-manager-migration-is-started`; } if (this.migrationIsFinished) { comment += `\nddl-manager-migration-is-finished`; } return comment; } } exports.Comment = Comment; function parseCacheSignature(totalComment) { const comment = (totalComment || "").trim(); const matchedResult = comment.match(/ddl-manager-cache\(([^\\)]+)\)/) || []; const cacheSignature = matchedResult[1]; return cacheSignature; } function parseCacheSelect(totalComment) { return extractContent({ totalComment, codePhrase: "ddl-manager-select" }); } function parseLegacyInfo(totalComment) { const legacyInfoStr = extractContent({ totalComment, codePhrase: "ddl-manager-legacy-info" }); if (legacyInfoStr) { try { return JSON.parse(legacyInfoStr); } catch (_a) { } } } function extractContent({ totalComment, codePhrase }) { const comment = (totalComment || "").trim(); const startParsing = comment.indexOf(codePhrase + "("); if (startParsing === -1) { return undefined; } let content = ""; let openedBracketsCount = 1; for (let i = startParsing + codePhrase.length + 1; i < comment.length; i++) { const symbol = comment[i]; if (symbol === "(") { openedBracketsCount++; } if (symbol === ")") { openedBracketsCount--; if (openedBracketsCount === 0) { break; } } content += symbol; } return content; } //# sourceMappingURL=Comment.js.map