@clickup/pg-microsharding
Version:
Microshards support for PostgreSQL
106 lines • 4.54 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.actionMove = actionMove;
const path_1 = require("path");
const chalk_1 = __importDefault(require("chalk"));
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 getSchemaName_1 = require("../internal/getSchemaName");
const isTrueValue_1 = require("../internal/isTrueValue");
const logging_1 = require("../internal/logging");
const names_1 = require("../internal/names");
const normalizeDsns_1 = require("../internal/normalizeDsns");
const runInTmux_1 = require("../internal/runInTmux");
/**
* Moves a shard from one database to another with no downtime.
*/
async function actionMove(args) {
var _a, _b;
await (0, runInTmux_1.tryReattachToTmuxSession)();
const dsns = await (0, normalizeDsns_1.normalizeDsns)(args["dsns"]);
let shard;
if ((_a = args["shard"]) === null || _a === void 0 ? void 0 : _a.match(/^(\d+)$/)) {
shard = parseInt(args["shard"], 10);
}
else {
throw "Please provide --shard, a microshard number to move";
}
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 activateOnDestination = (0, isTrueValue_1.isTrueValue)(args["activate-on-destination"]);
if (activateOnDestination === undefined) {
throw "Please provide --activate-on-destination=yes or --activate-on-destination=no";
}
const deactivateSQL = String(args["deactivate-sql"] || "") || undefined;
const maxReplicationLagSec = Number((_b = args["max-replication-lag-sec"]) !== null && _b !== void 0 ? _b : "") || undefined;
const wait = args["wait"];
if ((0, runInTmux_1.isInTmuxSession)()) {
const unpipe = logging_1.stdlog.pipeToNewLogFile(`move-${shard}`);
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);
}
const schema = await (0, getSchemaName_1.getSchemaName)(fromDsns[0], shard);
if (!schema) {
throw `Can't determine schema name for microshard number ${shard}`;
}
await (0, move_1.move)({
schema,
fromDsn: fromDsns[0],
toDsn: toDsns[0],
commitAction: activateOnDestination
? "deactivate-activate"
: "rename-to-new",
deactivateSQL,
maxReplicationLagSec,
wait,
});
if (!activateOnDestination) {
(0, logging_1.print)("\n" +
chalk_1.default.yellow("ATTENTION: the schema has been copied, but NOT activated on the destination. " +
"So effectively, it was a dry-run, and the data still lives in the source DB. " +
"To activate the schema on the destination and deactivate on the source, run " +
"the tool with --activate-on-destination=yes option."));
}
}
finally {
await unpipe();
}
}
else {
await (0, runInTmux_1.runInTmux)([
[
{
key: shard.toString(),
title: `microshard ${shard} | ${(0, names_1.dsnFromToShort)(fromDsns[0], toDsns[0])}`,
command: process.argv,
},
],
]);
}
return true;
}
//# sourceMappingURL=actionMove.js.map