copybase
Version:
Copy or backup databases quickly
71 lines (70 loc) • 3.44 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const BaseProvider_1 = __importDefault(require("./BaseProvider"));
class PostgreSQL extends BaseProvider_1.default {
constructor(config) {
super(Object.assign({ port: 5432 }, config));
}
dump(outputFile) {
return __awaiter(this, void 0, void 0, function* () {
yield this.checkCommandExists("pg_dump", ["--version"]);
const { database, hostname, port, username, password, pg_dump: pdDumpOptions, } = this.config;
const customOptions = this.getCustomOptions(pdDumpOptions);
this.logInfo(`Dump ${database}`);
return this.execCommand("pg_dump", [
port ? `--port=${port}` : "",
username ? `--username=${username}` : "",
hostname ? `--host=${hostname}` : "",
`--dbname=${database}`,
`--file=${outputFile}`,
...customOptions,
], { env: { PGPASSWORD: password } });
});
}
restore(inputFile) {
return __awaiter(this, void 0, void 0, function* () {
yield this.checkCommandExists("psql", ["--version"]);
const { database, hostname, port, username, password, psql: psqlOptions, } = this.config;
const customOptions = this.getCustomOptions(psqlOptions);
this.logInfo(`Restore ${database}`);
return this.execCommand("psql", [
"--echo-errors",
"--quiet",
port ? `--port=${port}` : "",
username ? `--username=${username}` : "",
hostname ? `--host=${hostname}` : "",
`--dbname=${database}`,
`--file=${inputFile}`,
...customOptions,
"> /dev/null",
], { env: { PGPASSWORD: password } });
});
}
listTables() {
return __awaiter(this, void 0, void 0, function* () {
yield this.checkCommandExists("psql", ["--version"]);
const { database, hostname, port, username, password, psql: psqlOptions, } = this.config;
const customOptions = this.getCustomOptions(psqlOptions);
return this.execCommand("psql", [
port ? `--port=${port}` : "",
username ? `--username=${username}` : "",
hostname ? `--host=${hostname}` : "",
...customOptions,
`--command="SELECT * FROM pg_catalog.pg_tables;"`,
], { env: { PGPASSWORD: password } });
});
}
}
exports.default = PostgreSQL;