UNPKG

@cocalc/database

Version:

CoCalc: code for working with our PostgreSQL database

57 lines 2.57 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createIndexes = exports.createIndexesQueries = void 0; const logger_1 = __importDefault(require("@cocalc/backend/logger")); const misc_1 = require("@cocalc/util/misc"); const log = (0, logger_1.default)("db:schema:indexes"); function possiblyAddParens(query) { if (query[0] == "(") { return query; } if (query.toLowerCase().startsWith("using")) { // do not add for using queries, since that violates PostgreSQL syntax return query; } return `(${query})`; } function createIndexesQueries(schema) { const v = schema.pg_indexes ?? []; if (schema.fields.expire != null && !v.includes("expire")) { v.push("expire"); } const queries = []; for (let query of v) { query = query.trim(); const name = `${schema.name}_${(0, misc_1.make_valid_name)(query)}_idx`; // this first, then... query = possiblyAddParens(query); queries.push({ name, query, unique: false }); } const w = schema.pg_unique_indexes ?? []; for (let query of w) { query = query.trim(); const name = `${schema.name}_${(0, misc_1.make_valid_name)(query)}_unique_idx`; query = possiblyAddParens(query); queries.push({ name, query, unique: true }); } return queries; } exports.createIndexesQueries = createIndexesQueries; async function createIndexes(db, schema) { log.debug("createIndexes", schema.name, " creating SQL query"); for (const { name, query, unique } of createIndexesQueries(schema)) { // Shorthand index is just the part in parens. // 2020-10-12: it makes total sense to add CONCURRENTLY to this index command to avoid locking up the table, // but the first time we tried this in production (postgres 10), it just made "invalid" indices. // the problem might be that several create index commands were issued rapidly, which threw this off // So, for now, it's probably best to either create them manually first (concurrently) or be // aware that this does lock up briefly. const fullQuery = `CREATE ${unique ? "UNIQUE" : ""} INDEX ${name} ON ${schema.name} ${query}`; log.debug("createIndexes -- creating ", name, " using ", fullQuery); await db.query(fullQuery); } } exports.createIndexes = createIndexes; //# sourceMappingURL=indexes.js.map