UNPKG

@clickup/pg-microsharding

Version:
31 lines 1.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cleanup = cleanup; const getSchemaName_1 = require("../internal/getSchemaName"); const names_1 = require("../internal/names"); const quote_1 = require("../internal/quote"); const runSql_1 = require("../internal/runSql"); /** * Removes old and semi-migrated schemas. */ async function cleanup({ dsn, noOldShards, confirm, }) { const dummyNo = 101; const dummySchemaName = await (0, getSchemaName_1.getSchemaName)(dsn, dummyNo); const schemaNameRe = dummySchemaName.replace(new RegExp(`0*${dummyNo}0*`), "\\d+"); const oldSchemaNameRe = (0, names_1.schemaCleanupRe)(schemaNameRe); const allSchemas = await runSql_1.runSql.column(dsn, (0, quote_1.sql) `SELECT nspname FROM pg_namespace`); const oldSchemas = allSchemas.filter((schema) => schema.match(oldSchemaNameRe)); if (oldSchemas.length === 0) { await (noOldShards === null || noOldShards === void 0 ? void 0 : noOldShards(oldSchemaNameRe)); return; } if (confirm && !(await confirm(oldSchemas))) { return; } // Remove sequentially, otherwise there is a high chance of having // shared_buffers OOM. for (const schema of oldSchemas) { await (0, runSql_1.runSql)(dsn, (0, quote_1.sql) `DROP SCHEMA ${(0, quote_1.ident)(schema)} CASCADE`, `Dropping redundant schema ${schema}`); } } //# sourceMappingURL=cleanup.js.map