@clickup/pg-microsharding
Version:
Microshards support for PostgreSQL
33 lines • 1.56 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.advanceSequences = advanceSequences;
const compact_1 = __importDefault(require("lodash/compact"));
const quote_1 = require("./quote");
const runSql_1 = require("./runSql");
/**
* Advances all sequences on the destination (including sequences in other
* schemas), so their values won't conflict with the ones on the source.
*/
async function advanceSequences({ fromDsn, toDsn, }) {
const fromSequences = await (0, runSql_1.runSql)(fromDsn, (0, quote_1.sql) `
SELECT
quote_ident(sequence_schema) || '.' || quote_ident(sequence_name),
nextval(quote_ident(sequence_schema) || '.' || quote_ident(sequence_name))::text
FROM information_schema.sequences
ORDER BY 1
`);
const toSequences = await runSql_1.runSql.column(toDsn, (0, quote_1.sql) `
SELECT quote_ident(sequence_schema) || '.' || quote_ident(sequence_name)
FROM information_schema.sequences
`);
const toSql = (0, compact_1.default)(fromSequences.map(([sequence, value]) => toSequences.includes(sequence)
? (0, quote_1.sql) `SELECT setval(${sequence}, GREATEST(nextval(${sequence}), ${value}) + 1000);`
: null));
if (toSql.length > 0) {
await (0, runSql_1.runSql)(toDsn, (0, quote_1.join)(toSql, "\n"), "Advancing destination sequences");
}
}
//# sourceMappingURL=advanceSequences.js.map