@clickup/pg-microsharding
Version:
Microshards support for PostgreSQL
86 lines • 3.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.actionCopy = actionCopy;
const path_1 = require("path");
const compact_1 = __importDefault(require("lodash/compact"));
const move_1 = require("../api/move");
const cli_1 = require("../cli");
const chooseDsns_1 = require("../internal/chooseDsns");
const logging_1 = require("../internal/logging");
const names_1 = require("../internal/names");
const normalizeDsns_1 = require("../internal/normalizeDsns");
const runInTmux_1 = require("../internal/runInTmux");
/**
* Copies a PG from one database to another with no downtime.
*/
async function actionCopy(args) {
var _a;
await (0, runInTmux_1.tryReattachToTmuxSession)();
const dsns = await (0, normalizeDsns_1.normalizeDsns)(args["dsns"]);
let schema;
if (args["schema"]) {
schema = args["schema"];
}
else {
throw "Please provide --schema, a schema name to copy";
}
const [fromDsns, fromWarnings] = (0, chooseDsns_1.chooseDsns)({
patterns: (0, compact_1.default)([args["from"]]),
existingDsns: dsns,
requireMatch: false,
});
if (fromDsns.length === 0) {
throw "Please provide --from, source DB DSN, as postgresql://user:pass@host/db?options, or a DSN prefix";
}
const [toDsns, toWarnings] = (0, chooseDsns_1.chooseDsns)({
patterns: (0, compact_1.default)([args["to"]]),
existingDsns: dsns,
requireMatch: false,
});
if (toDsns.length === 0) {
throw "Please provide --to, destination DB DSN, as postgresql://user:pass@host/db?options, or a DSN prefix";
}
const maxReplicationLagSec = Number((_a = args["max-replication-lag-sec"]) !== null && _a !== void 0 ? _a : "") || undefined;
const wait = args["wait"];
const validateFKs = args["validate-fks"];
if ((0, runInTmux_1.isInTmuxSession)()) {
const unpipe = logging_1.stdlog.pipeToNewLogFile(`copy-${schema}`);
try {
(0, logging_1.log)("$ " +
[(0, path_1.basename)(process.argv[1]), ...process.argv.slice(2)]
.map(cli_1.shellQuote)
.join(" "));
for (const warning of [...fromWarnings, ...toWarnings]) {
(0, logging_1.print)(warning);
}
await (0, move_1.move)({
schema,
fromDsn: fromDsns[0],
toDsn: toDsns[0],
commitAction: null,
maxReplicationLagSec,
wait,
validateFKs,
});
}
finally {
await unpipe();
}
}
else {
await (0, runInTmux_1.runInTmux)([
[
{
key: schema.toString(),
title: `schema ${schema} | ${(0, names_1.dsnFromToShort)(fromDsns[0], toDsns[0])}`,
command: process.argv,
},
],
]);
}
return true;
}
//# sourceMappingURL=actionCopy.js.map