Skip to content

Commit

Permalink
after
Browse files Browse the repository at this point in the history
  • Loading branch information
tonxxd committed Feb 2, 2021
1 parent a36e43c commit e6f4623
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ docs
node_modules
.idea
.DS_Store
.vscode
124 changes: 69 additions & 55 deletions src/DbDumper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const GzipCompressor = require('./Compressors/GzipCompressor');
const CannotSetParameter = require('./Exceptions/CannotSetParameter');
const DumpFailed = require('./Exceptions/DumpFailed');
const fs = require('fs');
const os = require('os');
const path = require('path');
const GzipCompressor = require("./Compressors/GzipCompressor");
const CannotSetParameter = require("./Exceptions/CannotSetParameter");
const DumpFailed = require("./Exceptions/DumpFailed");
const fs = require("fs");
const os = require("os");
const path = require("path");

class DbDumper {
constructor() {
Expand All @@ -17,19 +17,19 @@ class DbDumper {
this.password = null;

/** @var string */
this.host = 'localhost';
this.host = "localhost";

/** @var int */
this.port = 5432;

/** @var string */
this.socket = '';
this.socket = "";

/** @var int */
this.timeout = 0;

/** @var string */
this.dumpBinaryPath = '';
this.dumpBinaryPath = "";

/** @var array */
this.includeTables = [];
Expand All @@ -47,33 +47,35 @@ class DbDumper {
this.compressor = null;

this.tempFilePath = null;

this.after = () => {};
}

static create() {
return new this();
};
}

getDbName() {
return this.dbName;
};
}

/**
*
* @return this
* @param dbName
*/
setDbName (dbName) {
setDbName(dbName) {
this.dbName = dbName;

return this;
};
}

/**
*
* @return this
* @param userName
*/
setUserName (userName) {
setUserName(userName) {
this.userName = userName;

return this;
Expand All @@ -84,7 +86,7 @@ class DbDumper {
* @return this
* @param password
*/
setPassword (password) {
setPassword(password) {
this.password = password;

return this;
Expand All @@ -95,13 +97,18 @@ class DbDumper {
* @return this
* @param host
*/
setHost (host) {
setHost(host) {
this.host = host;

return this;
}

getHost () {
setAfter(callback) {
this.after = callback;
return this;
}

getHost() {
return this.host;
}

Expand All @@ -110,7 +117,7 @@ class DbDumper {
*
* @return this
*/
setPort (port) {
setPort(port) {
this.port = port;

return this;
Expand All @@ -121,7 +128,7 @@ class DbDumper {
*
* @return this
*/
setSocket (socket) {
setSocket(socket) {
this.socket = socket;

return this;
Expand All @@ -132,15 +139,15 @@ class DbDumper {
* @return this
* @param timeout
*/
setTimeout (timeout) {
setTimeout(timeout) {
this.timeout = timeout;

return this;
}

setDumpBinaryPath (dumpBinaryPath) {
if (dumpBinaryPath !== '' && dumpBinaryPath.substr(-1) !== '/') {
dumpBinaryPath += '/';
setDumpBinaryPath(dumpBinaryPath) {
if (dumpBinaryPath !== "" && dumpBinaryPath.substr(-1) !== "/") {
dumpBinaryPath += "/";
}

this.dumpBinaryPath = dumpBinaryPath;
Expand All @@ -153,7 +160,7 @@ class DbDumper {
*
* @return this
*/
enableCompression () {
enableCompression() {
this.compressor = new GzipCompressor();

return this;
Expand All @@ -163,7 +170,7 @@ class DbDumper {
return this.compressor.useExtension();
}

useCompressor (compressor) {
useCompressor(compressor) {
this.compressor = compressor;

return this;
Expand All @@ -175,13 +182,16 @@ class DbDumper {
*
* @param includeTables
*/
setIncludedTables (includeTables) {
setIncludedTables(includeTables) {
if (this.excludeTables.length) {
throw CannotSetParameter.conflictingParameters('includeTables', 'excludeTables');
throw CannotSetParameter.conflictingParameters(
"includeTables",
"excludeTables"
);
}

if (!Array.isArray(includeTables)) {
includeTables = includeTables.split(',');
includeTables = includeTables.split(",");
}

this.includeTables = includeTables;
Expand All @@ -195,13 +205,16 @@ class DbDumper {
*
* @param excludeTables
*/
setExcludedTables (excludeTables) {
setExcludedTables(excludeTables) {
if (this.includeTables.length) {
throw CannotSetParameter.conflictingParameters('excludeTables', 'includeTables');
throw CannotSetParameter.conflictingParameters(
"excludeTables",
"includeTables"
);
}

if (!Array.isArray(excludeTables)) {
excludeTables = excludeTables.split(',');
excludeTables = excludeTables.split(",");
}

this.excludeTables = excludeTables;
Expand All @@ -210,14 +223,14 @@ class DbDumper {
}

fwrite(file, content) {
const log = fs.createWriteStream(file, {flags: 'a'});
const log = fs.createWriteStream(file, { flags: "a" });
log.write(content);
log.end();
}

tempFile(fileName) {
let fullPath = path.join(os.tmpdir(), fileName);
fs.writeFile(fullPath, '', function (err) {
fs.writeFile(fullPath, "", function (err) {
if (err) {
throw err;
}
Expand All @@ -230,7 +243,7 @@ class DbDumper {
* @return this
* @param extraOption
*/
addExtraOption (extraOption) {
addExtraOption(extraOption) {
if (extraOption.length) {
this.extraOptions.push(extraOption);
}
Expand All @@ -243,18 +256,17 @@ class DbDumper {
* @return this
* @param extraOptionAfterDbName
*/
addExtraOptionAfterDbName (extraOptionAfterDbName) {
addExtraOptionAfterDbName(extraOptionAfterDbName) {
if (extraOptionAfterDbName.length) {
this.extraOptionsAfterDbName.push(extraOptionAfterDbName);
}

return this;
}

dumpToFile (dumpFile) {
};
dumpToFile(dumpFile) {}

async checkIfDumpWasSuccessFul (error, outputFile) {
async checkIfDumpWasSuccessFul(error, outputFile) {
if (error) {
throw DumpFailed.processDidNotEndSuccessfully(error);
}
Expand All @@ -263,23 +275,24 @@ class DbDumper {
throw DumpFailed.dumpfileWasNotCreated();
}

if (await this.filesize(outputFile) === 0) {
if ((await this.filesize(outputFile)) === 0) {
throw DumpFailed.dumpfileWasEmpty();
}

try {
fs.unlinkSync(this.tempFilePath)
fs.unlinkSync(this.tempFilePath);
} catch (e) {
//ignore
}
this.after();
}

filesize (filename) {
var stats = fs.statSync(filename)
return stats["size"]
filesize(filename) {
var stats = fs.statSync(filename);
return stats["size"];
}

getCompressCommand (command, dumpFile) {
getCompressCommand(command, dumpFile) {
let compressCommand = this.compressor.useCommand();

if (this.isWindows()) {
Expand All @@ -289,32 +302,33 @@ class DbDumper {
return `((((${command}; echo $\? >&3) | ${compressCommand} > ${dumpFile}) 3>&1) | (read x; exit \$x))`;
}

echoToFile (command, dumpFile) {
echoToFile(command, dumpFile) {
dumpFile = '"' + this.addcslashes(dumpFile, '\\"') + '"';

if (this.compressor) {
return this.getCompressCommand(command, dumpFile);
}

return command + ' > ' + dumpFile;
return command + " > " + dumpFile;
}

addcslashes (string) {
return string.replace(/\\/g, '\\\\')
.replace(/\u0008/g, '\\b')
.replace(/\t/g, '\\t')
.replace(/\n/g, '\\n')
.replace(/\f/g, '\\f')
.replace(/\r/g, '\\r')
.replace(/'/g, '\\\'')
addcslashes(string) {
return string
.replace(/\\/g, "\\\\")
.replace(/\u0008/g, "\\b")
.replace(/\t/g, "\\t")
.replace(/\n/g, "\\n")
.replace(/\f/g, "\\f")
.replace(/\r/g, "\\r")
.replace(/'/g, "\\'")
.replace(/"/g, '\\"');
}

determineQuote() {
return this.isWindows() ? '"' : "'";
}

isWindows () {
isWindows() {
return process.platform === "win32";
}
}
Expand Down

0 comments on commit e6f4623

Please sign in to comment.