UNPKG

@clickup/pg-microsharding

Version:
31 lines 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.startCopyingTables = startCopyingTables; const names_1 = require("./names"); const quote_1 = require("./quote"); const runSql_1 = require("./runSql"); /** * Copies DDL and creates publication/subscription which starts copying the * data. The data continues copying in background. */ async function startCopyingTables({ fromDsn, tables, toDsn, schema, }) { if (tables.length === 0) { return; } await (0, runSql_1.runSql)(fromDsn, // "FOR TABLES IN SCHEMA ..." is supported since PG15+ only, so enumerating // all the tables explicitly. (0, quote_1.sql) `CREATE PUBLICATION ${(0, quote_1.ident)((0, names_1.pubName)(schema))} FOR TABLE ${(0, quote_1.join)(tables.map(({ schema, name }) => (0, quote_1.sql) `${(0, quote_1.ident)(schema)}.${(0, quote_1.ident)(name)}`), ", ")}`, "Creating source publication"); // We create the replication slot separately only to be able to test the tool // on dev: without a replication slot, when using the same database as a // source and destination, CREATE SUBSCRIPTION operation hangs (weird // documented behavior). await (0, runSql_1.runSql)(fromDsn, (0, quote_1.sql) `SELECT pg_create_logical_replication_slot(${(0, names_1.subName)(schema)}, 'pgoutput')`); await (0, runSql_1.runSql)(toDsn, (0, quote_1.sql) ` CREATE SUBSCRIPTION ${(0, quote_1.ident)((0, names_1.subName)(schema))} CONNECTION ${fromDsn} PUBLICATION ${(0, quote_1.ident)((0, names_1.pubName)(schema))} WITH (create_slot=false, streaming=on) `, "Creating destination subscription"); } //# sourceMappingURL=startCopyingTables.js.map