@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
111 lines (110 loc) • 3.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.commandRestoreDb = void 0;
const defineCommand_1 = require("../../core/defineCommand");
const copyDb_1 = require("../../gcloud/cloudSql/copyDb");
const startProxy_1 = require("../../gcloud/cloudSql/startProxy");
exports.commandRestoreDb = (0, defineCommand_1.defineCommand)({
name: "cloudsql restore-db",
description: "restore a db from one source to another target",
group: "cloudSQL",
inputs: {
source: {
type: "string",
message: "Source instance (connection string or 'local')? 🤔 "
},
sourceLocalPort: {
type: "number",
message: "Local Port for source? 🤔 ",
default: 5432
},
sourceUsername: {
type: "string",
message: "Source Username? 🤔 ",
default: "postgres"
},
sourcePassword: {
type: "string",
message: "Source Password? 🤔 "
},
sourceDbName: {
type: "string",
message: "Source DB name? 🤔 "
},
target: {
type: "string",
message: "Target INSTANCE (connection string or 'local')? 🤔 "
},
targetLocalPort: {
type: "number",
message: "Local Port for target? 🤔 ",
default: 5432
},
targetUsername: {
type: "string",
message: "Target Username? 🤔 ",
default: "postgres"
},
targetPassword: {
type: "string",
message: "Target Password? 🤔 "
},
targetDbName: {
type: "string",
message: "Target DB name? 🤔 "
}
},
execute: async ctx => {
const source = await ctx.get("source");
let sourceProxy;
let targetProxy;
let sourcePort;
let targetPort;
if (source === "local") {
sourcePort = await ctx.get("sourceLocalPort");
} else {
sourcePort = 54399;
sourceProxy = await (0, startProxy_1.startCloudSqlProxyInBackground)({
instanceName: source,
localPort: sourcePort
});
}
const sourceUsername = await ctx.get("sourceUsername");
const sourcePassword = await ctx.get("sourcePassword");
const sourceDbName = await ctx.get("sourceDbName");
const target = await ctx.get("target");
if (target === "local") {
targetPort = await ctx.get("targetLocalPort");
} else {
targetPort = 54499;
targetProxy = await (0, startProxy_1.startCloudSqlProxyInBackground)({
instanceName: target,
localPort: targetPort
});
}
const targetUsername = await ctx.get("targetUsername");
const targetPassword = await ctx.get("targetPassword");
const targetDbName = await ctx.get("targetDbName");
const shouldContinue = await ctx.confirm(`This will drop ${target}/${targetDbName} and replace it with ${source}/${sourceDbName}. Continue? 🤔 `);
if (!shouldContinue) {
return;
}
try {
await (0, copyDb_1.spawnCopyDb)({
targetPassword,
targetPort,
targetUsername,
sourceUsername,
sourcePassword,
sourcePort,
sourceDbName,
targetDbName
});
} finally {
sourceProxy === null || sourceProxy === void 0 ? void 0 : sourceProxy.stop();
targetProxy === null || targetProxy === void 0 ? void 0 : targetProxy.stop();
}
}
});