UNPKG

@clickup/pg-mig

Version:

PostgreSQL schema migration tool with microsharding and clustering support

63 lines 2.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateCreateIndexConcurrently = validateCreateIndexConcurrently; const escapeRegExp_1 = __importDefault(require("lodash/escapeRegExp")); const hasOtherSqlStatements_1 = require("./hasOtherSqlStatements"); const removeSqlComments_1 = require("./removeSqlComments"); const REQUIRED_VARS = [ "$parallelism_per_host", "$parallelism_global", "$run_alone", ]; function validateCreateIndexConcurrently(content, vars) { content = (0, removeSqlComments_1.removeSqlComments)(content); let hasCreateIndexConcurrently = false; const errors = []; const indexNamesQuoted = []; const regexIterator = /\bCREATE\s+(?:UNIQUE\s+)?INDEX\s+CONCURRENTLY\s+(?:IF\s+NOT\s+EXISTS\s+)?(\S+|"(?:[^"]|"")+")/gis; while (true) { const match = regexIterator.exec(content); if (!match) { break; } hasCreateIndexConcurrently = true; const indexNameQuoted = match[1]; indexNamesQuoted.push(indexNameQuoted); const rest = content.slice(match.index + match[0].length); if (match.index === 0 && !(0, hasOtherSqlStatements_1.hasOtherSqlStatements)(rest)) { return REQUIRED_VARS.some((k) => !!vars[k]) ? { type: "success-index-alone", indexNamesQuoted } : { type: "error", errors: [ `start with one of the following vars: ${REQUIRED_VARS.join(", ")}`, ], }; } if (!content.match(new RegExp(`DROP\\s+INDEX\\s+IF\\s+EXISTS\\s+${(0, escapeRegExp_1.default)(indexNameQuoted)}`, "is"))) { errors.push(`include "DROP INDEX IF EXISTS ${indexNameQuoted};" statement before "CREATE INDEX CONCURRENTLY"`); } } if (!hasCreateIndexConcurrently) { return { type: "success", indexNamesQuoted: [] }; } if (!REQUIRED_VARS.some((k) => !!vars[k])) { errors.unshift(`start with one of the following vars: ${REQUIRED_VARS.join(", ")}`); } 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 "CREATE INDEX CONCURRENTLY")'); } return errors.length > 0 ? { type: "error", errors } : { type: "success", indexNamesQuoted }; } //# sourceMappingURL=validateCreateIndexConcurrently.js.map