UNPKG

@clickup/pg-mig

Version:

PostgreSQL schema migration tool with microsharding and clustering support

43 lines 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateDropIndexConcurrently = validateDropIndexConcurrently; const hasOtherSqlStatements_1 = require("./hasOtherSqlStatements"); const removeSqlComments_1 = require("./removeSqlComments"); function validateDropIndexConcurrently(content) { content = (0, removeSqlComments_1.removeSqlComments)(content); let hasDropIndexConcurrently = false; const errors = []; const regexIterator = /\DROP\s+INDEX\s+CONCURRENTLY\s+(IF\s+EXISTS\s+)?([^;"]+|"(?:[^"]|"")+")+/gis; while (true) { const match = regexIterator.exec(content); if (!match) { break; } hasDropIndexConcurrently = true; const rest = content.slice(match.index + match[0].length); if (match.index === 0 && !(0, hasOtherSqlStatements_1.hasOtherSqlStatements)(rest)) { return match[1] ? { type: "success-index-alone" } : { type: "error", errors: [ "DROP INDEX CONCURRENTLY is alone in the file, so it must use IF EXISTS", ], }; } } if (!hasDropIndexConcurrently) { return { type: "success" }; } if (!content.match(/^COMMIT\s*;/is)) { errors.unshift('start with "COMMIT;"'); } if (!content.match(/\bBEGIN\s*;/is)) { errors.push('end with "BEGIN;" with other optional SQL statements after it'); } if (errors.length > 0) { errors.unshift('(due to having "DROP INDEX CONCURRENTLY")'); } return errors.length > 0 ? { type: "error", errors } : { type: "success" }; } //# sourceMappingURL=validateDropIndexConcurrently.js.map