Update FromMySQL2PostgreSQL.js
This commit is contained in:
parent
3f042a6c3a
commit
accb67ad8a
|
@ -90,7 +90,7 @@ FromMySQL2PostgreSQL.prototype.boot = function(self) {
|
||||||
self.readDataTypesMap
|
self.readDataTypesMap
|
||||||
).then(
|
).then(
|
||||||
function() {
|
function() {
|
||||||
return new Promise(function(resolveBoot) {
|
return new Promise(function(resolveBoot, rejectBoot) {
|
||||||
console.log('\t--[boot] Boot is accomplished...');
|
console.log('\t--[boot] Boot is accomplished...');
|
||||||
resolveBoot(self);
|
resolveBoot(self);
|
||||||
});
|
});
|
||||||
|
@ -231,9 +231,11 @@ FromMySQL2PostgreSQL.prototype.createTemporaryDirectory = function(self) {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} else if (!stat.isDirectory()) {
|
} else if (!stat.isDirectory()) {
|
||||||
self.log(self, '\t--[createTemporaryDirectory] Cannot perform a migration due to unexpected error');
|
self.log(self, '\t--[createTemporaryDirectory] Cannot perform a migration due to unexpected error');
|
||||||
reject();
|
reject();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
self.log(self, '\t--[createTemporaryDirectory] Temporary directory already exists...');
|
self.log(self, '\t--[createTemporaryDirectory] Temporary directory already exists...');
|
||||||
resolve(self);
|
resolve(self);
|
||||||
|
@ -249,12 +251,12 @@ FromMySQL2PostgreSQL.prototype.createTemporaryDirectory = function(self) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
FromMySQL2PostgreSQL.prototype.removeTemporaryDirectory = function(self) {
|
FromMySQL2PostgreSQL.prototype.removeTemporaryDirectory = function(self) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve, reject) {
|
||||||
fs.rmdir(self._tempDirPath, function(error) {
|
fs.rmdir(self._tempDirPath, function(error) {
|
||||||
var msg;
|
var msg;
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
msg = '\t--[removeTemporaryDirectory] Note, TemporaryDirectory located at "' + self._tempDirPath + '" is not removed';
|
msg = '\t--[removeTemporaryDirectory] Note, TemporaryDirectory located at "' + self._tempDirPath + '" is nor removed';
|
||||||
} else {
|
} else {
|
||||||
msg = '\t--[removeTemporaryDirectory] TemporaryDirectory located at "' + self._tempDirPath + '" is removed';
|
msg = '\t--[removeTemporaryDirectory] TemporaryDirectory located at "' + self._tempDirPath + '" is removed';
|
||||||
}
|
}
|
||||||
|
@ -283,14 +285,17 @@ FromMySQL2PostgreSQL.prototype.createLogsDirectory = function(self) {
|
||||||
+ '"logs_directory": ' + self._logsDirPath
|
+ '"logs_directory": ' + self._logsDirPath
|
||||||
);
|
);
|
||||||
reject();
|
reject();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
self.log(self, '\t--[createLogsDirectory] Logs directory is created...');
|
self.log(self, '\t--[createLogsDirectory] Logs directory is created...');
|
||||||
resolve(self);
|
resolve(self);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} else if (!stat.isDirectory()) {
|
} else if (!stat.isDirectory()) {
|
||||||
console.log('\t--[createLogsDirectory] Cannot perform a migration due to unexpected error');
|
console.log('\t--[createLogsDirectory] Cannot perform a migration due to unexpected error');
|
||||||
reject();
|
reject();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
self.log(self, '\t--[createLogsDirectory] Logs directory already exists...');
|
self.log(self, '\t--[createLogsDirectory] Logs directory already exists...');
|
||||||
resolve(self);
|
resolve(self);
|
||||||
|
@ -310,9 +315,9 @@ FromMySQL2PostgreSQL.prototype.createLogsDirectory = function(self) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
FromMySQL2PostgreSQL.prototype.log = function(self, log, isErrorLog) {
|
FromMySQL2PostgreSQL.prototype.log = function(self, log, isErrorLog) {
|
||||||
var buffer = new Buffer(log + '\n\n');
|
var buffer = new Buffer(log + '\n\n');
|
||||||
|
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve, reject) {
|
||||||
if (isErrorLog === undefined || isErrorLog === false) {
|
if (isErrorLog === undefined || isErrorLog === false) {
|
||||||
console.log(log);
|
console.log(log);
|
||||||
}
|
}
|
||||||
|
@ -321,42 +326,47 @@ FromMySQL2PostgreSQL.prototype.log = function(self, log, isErrorLog) {
|
||||||
fs.open(self._allLogsPath, 'a', self._0777, function(error, fd) {
|
fs.open(self._allLogsPath, 'a', self._0777, function(error, fd) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
self._allLogsPathFd = fd;
|
self._allLogsPathFd = fd;
|
||||||
fs.write(self._allLogsPathFd, buffer, 0, buffer.length, null, function() {
|
fs.write(self._allLogsPathFd, buffer, 0, buffer.length, null, function(error) {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
fs.write(self._allLogsPathFd, buffer, 0, buffer.length, null, function() {
|
fs.write(self._allLogsPathFd, buffer, 0, buffer.length, null, function(error) {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}).then(
|
}).then(
|
||||||
function() {
|
function(self) {
|
||||||
return new Promise(function(resolveTableLog) {
|
return new Promise(function(resolveTableLog, rejectTableLog) {
|
||||||
if (self._clonedSelfTableNamePath === undefined) {
|
if (self._clonedSelfTableNamePath === undefined) {
|
||||||
resolveTableLog(self);
|
resolveTableLog(self);
|
||||||
} else if (self._clonedSelfTableNamePathFd === undefined) {
|
} else if (self._clonedSelfTableNamePathFd === undefined) {
|
||||||
fs.open(self._clonedSelfTableNamePath, 'a', self._0777, function(error, fd) {
|
fs.open(self._clonedSelfTableNamePath, 'a', self._0777, function(error, fd) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
self._clonedSelfTableNamePathFd = fd;
|
self._clonedSelfTableNamePathFd = fd;
|
||||||
fs.write(self._clonedSelfTableNamePathFd, buffer, 0, buffer.length, null, function() {
|
fs.write(self._clonedSelfTableNamePathFd, buffer, 0, buffer.length, null, function(error) {
|
||||||
resolveTableLog(self);
|
resolveTableLog(self);
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
resolveTableLog(self);
|
} else {
|
||||||
}
|
resolveTableLog(self);
|
||||||
});
|
}
|
||||||
} else {
|
});
|
||||||
fs.write(self._clonedSelfTableNamePathFd, buffer, 0, buffer.length, null, function() {
|
|
||||||
resolveTableLog(self);
|
} else {
|
||||||
});
|
fs.write(self._clonedSelfTableNamePathFd, buffer, 0, buffer.length, null, function(error) {
|
||||||
|
resolveTableLog(self);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -368,7 +378,7 @@ FromMySQL2PostgreSQL.prototype.log = function(self, log, isErrorLog) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
FromMySQL2PostgreSQL.prototype.generateError = function(self, message, sql) {
|
FromMySQL2PostgreSQL.prototype.generateError = function(self, message, sql) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve, reject) {
|
||||||
message += sql === undefined ? '' : '\n\tSQL: ' + sql + '\n\n';
|
message += sql === undefined ? '' : '\n\tSQL: ' + sql + '\n\n';
|
||||||
var buffer = new Buffer(message);
|
var buffer = new Buffer(message);
|
||||||
self.log(self, message, true);
|
self.log(self, message, true);
|
||||||
|
@ -377,7 +387,7 @@ FromMySQL2PostgreSQL.prototype.generateError = function(self, message, sql) {
|
||||||
fs.open(self._errorLogsPath, 'a', self._0777, function(error, fd) {
|
fs.open(self._errorLogsPath, 'a', self._0777, function(error, fd) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
self._errorLogsPathFd = fd;
|
self._errorLogsPathFd = fd;
|
||||||
fs.write(self._errorLogsPathFd, buffer, 0, buffer.length, null, function() {
|
fs.write(self._errorLogsPathFd, buffer, 0, buffer.length, null, function(error) {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -387,7 +397,7 @@ FromMySQL2PostgreSQL.prototype.generateError = function(self, message, sql) {
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
fs.write(self._errorLogsPathFd, buffer, 0, buffer.length, null, function() {
|
fs.write(self._errorLogsPathFd, buffer, 0, buffer.length, null, function(error) {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -416,6 +426,7 @@ FromMySQL2PostgreSQL.prototype.connect = function(self) {
|
||||||
self.log(self, '\t--[connect] Cannot connect to MySQL server...');
|
self.log(self, '\t--[connect] Cannot connect to MySQL server...');
|
||||||
reject(self);
|
reject(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
}
|
}
|
||||||
|
@ -457,6 +468,7 @@ FromMySQL2PostgreSQL.prototype.createSchema = function(self) {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
}
|
}
|
||||||
|
@ -473,15 +485,16 @@ FromMySQL2PostgreSQL.prototype.createSchema = function(self) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
FromMySQL2PostgreSQL.prototype.loadStructureToMigrate = function(self) {
|
FromMySQL2PostgreSQL.prototype.loadStructureToMigrate = function(self) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve, reject) {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
}).then(
|
}).then(
|
||||||
self.connect,
|
self.connect,
|
||||||
function() {
|
function() {
|
||||||
self.log(self, '\t--[loadStructureToMigrate] Cannot establish DB connections...');
|
self.log(self, '\t--[loadStructureToMigrate] Cannot establish DB connections...');
|
||||||
}
|
}
|
||||||
|
|
||||||
).then(
|
).then(
|
||||||
function() {
|
function(self) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
var sql = 'SHOW FULL TABLES IN `' + self._mySqlDbName + '`;';
|
var sql = 'SHOW FULL TABLES IN `' + self._mySqlDbName + '`;';
|
||||||
self._mysql.getConnection(function(error, connection) {
|
self._mysql.getConnection(function(error, connection) {
|
||||||
|
@ -503,7 +516,6 @@ FromMySQL2PostgreSQL.prototype.loadStructureToMigrate = function(self) {
|
||||||
var createViewPromises = [];
|
var createViewPromises = [];
|
||||||
|
|
||||||
for (var i = 0; i < rows.length; i++) {
|
for (var i = 0; i < rows.length; i++) {
|
||||||
//if (i < 3) // TODO.
|
|
||||||
if (rows[i].Table_type === 'BASE TABLE') {
|
if (rows[i].Table_type === 'BASE TABLE') {
|
||||||
self._tablesToMigrate.push(rows[i]);
|
self._tablesToMigrate.push(rows[i]);
|
||||||
tablesCnt++;
|
tablesCnt++;
|
||||||
|
@ -527,19 +539,15 @@ FromMySQL2PostgreSQL.prototype.loadStructureToMigrate = function(self) {
|
||||||
|
|
||||||
Promise.all(
|
Promise.all(
|
||||||
processTablePromises
|
processTablePromises
|
||||||
).then(
|
).then(
|
||||||
function() {
|
self.cleanupLocal, // TODO.
|
||||||
resolve(self);
|
|
||||||
},
|
|
||||||
function() {
|
function() {
|
||||||
reject();
|
reject();
|
||||||
}
|
}
|
||||||
).then(
|
).then(
|
||||||
// TODO: treat FKs, views etc....
|
function(self) {
|
||||||
function() {
|
|
||||||
resolve(self);
|
resolve(self);
|
||||||
},
|
},
|
||||||
// TODO: probably one o tables was not created, do not create FKs, views etc....
|
|
||||||
function() {
|
function() {
|
||||||
reject();
|
reject();
|
||||||
}
|
}
|
||||||
|
@ -553,6 +561,26 @@ FromMySQL2PostgreSQL.prototype.loadStructureToMigrate = function(self) {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cleanup resources related to given table.
|
||||||
|
*
|
||||||
|
* @param {FromMySQL2PostgreSQL} self
|
||||||
|
* @returns {Promise}
|
||||||
|
*/
|
||||||
|
FromMySQL2PostgreSQL.prototype.cleanupLocal = function(self) {
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
if (self._clonedSelfTableNamePathFd === undefined) {
|
||||||
|
self.log(self, '\t--[cleanupLocal] Finished processing table `' + self._clonedSelfTableName + '`...');
|
||||||
|
resolve(self);
|
||||||
|
} else {
|
||||||
|
fs.close(self._clonedSelfTableNamePathFd, function() {
|
||||||
|
self.log(self, '\t--[cleanupLocal] Finished processing table `' + self._clonedSelfTableName + '`...');
|
||||||
|
resolve(self);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Migrates structure of a single table to PostgreSql server.
|
* Migrates structure of a single table to PostgreSql server.
|
||||||
*
|
*
|
||||||
|
@ -560,12 +588,12 @@ FromMySQL2PostgreSQL.prototype.loadStructureToMigrate = function(self) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
FromMySQL2PostgreSQL.prototype.createTable = function(self) {
|
FromMySQL2PostgreSQL.prototype.createTable = function(self) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve, reject) {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
}).then(
|
}).then(
|
||||||
self.connect
|
self.connect
|
||||||
).then(
|
).then(
|
||||||
function() {
|
function(self) {
|
||||||
return new Promise(function(resolveCreateTable, rejectCreateTable) {
|
return new Promise(function(resolveCreateTable, rejectCreateTable) {
|
||||||
self.log(self, '\t--[createTable] Currently creating table: `' + self._clonedSelfTableName + '`');
|
self.log(self, '\t--[createTable] Currently creating table: `' + self._clonedSelfTableName + '`');
|
||||||
var sql = 'SHOW COLUMNS FROM `' + self._clonedSelfTableName + '`;';
|
var sql = 'SHOW COLUMNS FROM `' + self._clonedSelfTableName + '`;';
|
||||||
|
@ -629,12 +657,12 @@ FromMySQL2PostgreSQL.prototype.createTable = function(self) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
FromMySQL2PostgreSQL.prototype.populateTable = function(self) {
|
FromMySQL2PostgreSQL.prototype.populateTable = function(self) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve, reject) {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
}).then(
|
}).then(
|
||||||
self.connect
|
self.connect
|
||||||
).then(
|
).then(
|
||||||
function() {
|
function(self) {
|
||||||
return new Promise(function(resolvePopulateTable, rejectPopulateTable) {
|
return new Promise(function(resolvePopulateTable, rejectPopulateTable) {
|
||||||
self.log(self, '\t--[populateTable] Currently populating table: `' + self._clonedSelfTableName + '`');
|
self.log(self, '\t--[populateTable] Currently populating table: `' + self._clonedSelfTableName + '`');
|
||||||
|
|
||||||
|
@ -685,7 +713,7 @@ FromMySQL2PostgreSQL.prototype.populateTable = function(self) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Promise.all(populateTableWorkers).then(
|
Promise.all(populateTableWorkers).then(
|
||||||
function() {
|
function(self) {
|
||||||
resolvePopulateTable(self);
|
resolvePopulateTable(self);
|
||||||
},
|
},
|
||||||
function() {
|
function() {
|
||||||
|
@ -716,13 +744,13 @@ FromMySQL2PostgreSQL.prototype.populateTable = function(self) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
FromMySQL2PostgreSQL.prototype.populateTableWorker = function(self, offset, rowsInChunk, rowsCnt) {
|
FromMySQL2PostgreSQL.prototype.populateTableWorker = function(self, offset, rowsInChunk, rowsCnt) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve, reject) {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
}).then(
|
}).then(
|
||||||
self.connect
|
self.connect
|
||||||
).then(
|
).then(
|
||||||
function() {
|
function(self) {
|
||||||
return new Promise(function(resolvePopulateTableWorker) {
|
return new Promise(function(resolvePopulateTableWorker, rejectPopulateTableWorker) {
|
||||||
var csvAddr = self._tempDirPath + '/' + self._clonedSelfTableName + offset + '.csv';
|
var csvAddr = self._tempDirPath + '/' + self._clonedSelfTableName + offset + '.csv';
|
||||||
var sql = 'SELECT * FROM `' + self._clonedSelfTableName + '` LIMIT ' + offset + ',' + rowsInChunk + ';';
|
var sql = 'SELECT * FROM `' + self._clonedSelfTableName + '` LIMIT ' + offset + ',' + rowsInChunk + ';';
|
||||||
|
|
||||||
|
@ -743,7 +771,7 @@ FromMySQL2PostgreSQL.prototype.populateTableWorker = function(self, offset, rows
|
||||||
// Sanitize records.
|
// Sanitize records.
|
||||||
// When sanitized - write them to a csv file.
|
// When sanitized - write them to a csv file.
|
||||||
rowsInChunk = rows.length; // Must check amount of rows BEFORE sanitizing.
|
rowsInChunk = rows.length; // Must check amount of rows BEFORE sanitizing.
|
||||||
var sanitizedRecords = [];
|
var sanitizedRecords = [];
|
||||||
|
|
||||||
for (var cnt = 0; cnt < rows.length; cnt++) {
|
for (var cnt = 0; cnt < rows.length; cnt++) {
|
||||||
var sanitizedRecord = Object.create(null);
|
var sanitizedRecord = Object.create(null);
|
||||||
|
@ -785,24 +813,23 @@ FromMySQL2PostgreSQL.prototype.populateTableWorker = function(self, offset, rows
|
||||||
done();
|
done();
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
self.generateError(self, '\t--[populateTableWorker] ' + err, sql);
|
self.generateError(self, '\t--[populateTableWorker] ' + err, sql);
|
||||||
resolvePopulateTableWorker(self);
|
resolvePopulateTableWorker(self);
|
||||||
} else {
|
} else {
|
||||||
self._totalRowsInserted += result.rowCount;
|
self._totalRowsInserted += result.rowCount;
|
||||||
var msg = '\t--[populateTableWorker] For now inserted: ' + self._totalRowsInserted + ' rows, '
|
var msg = '\t--[populateTableWorker] For now inserted: ' + self._totalRowsInserted + ' rows, '
|
||||||
+ 'Total rows to insert into "' + self._schema + '"."' + self._clonedSelfTableName + '": ' + rowsCnt;
|
+ 'Total rows in "' + self._schema + '"."' + self._clonedSelfTableName + '": ' + rowsCnt;
|
||||||
|
|
||||||
self.log(self, msg);
|
self.log(self, msg);
|
||||||
|
|
||||||
fs.unlink(csvAddr, function() {
|
fs.unlink(csvAddr, function() {
|
||||||
fs.close(fd, function() {
|
fs.close(fd, function() {
|
||||||
resolvePopulateTableWorker(self);
|
resolvePopulateTableWorker(self);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -843,12 +870,13 @@ FromMySQL2PostgreSQL.prototype.sanitizeValue = function(value) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
FromMySQL2PostgreSQL.prototype.processTable = function(self, tableName) {
|
FromMySQL2PostgreSQL.prototype.processTable = function(self, tableName) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve, reject) {
|
||||||
self = self.clone(self);
|
self = self.clone(self);
|
||||||
self._clonedSelfTableName = tableName;
|
self._clonedSelfTableName = tableName;
|
||||||
self._totalRowsInserted = 0;
|
self._totalRowsInserted = 0;
|
||||||
self._clonedSelfTableNamePath = self._logsDirPath + '/' + tableName + '.log';
|
self._clonedSelfTableNamePath = self._logsDirPath + '/' + tableName + '.log';
|
||||||
resolve(self);
|
resolve(self);
|
||||||
|
|
||||||
}).then(
|
}).then(
|
||||||
self.connect
|
self.connect
|
||||||
).then(
|
).then(
|
||||||
|
@ -857,36 +885,13 @@ FromMySQL2PostgreSQL.prototype.processTable = function(self, tableName) {
|
||||||
self.log(self, '\t--[processTable] Cannot establish DB connections...');
|
self.log(self, '\t--[processTable] Cannot establish DB connections...');
|
||||||
}
|
}
|
||||||
).then(
|
).then(
|
||||||
self.populateTable, // TODO: Promise chain MUST continue, even after unsuccessfull 'self.populateTable'.
|
self.populateTable,
|
||||||
function() {
|
function() {
|
||||||
self.log(self, '\t--[processTable] Cannot create table "' + self._schema + '"."' + self._clonedSelfTableName + '"...');
|
self.log(self, '\t--[processTable] Cannot create table "' + self._schema + '"."' + self._clonedSelfTableName + '"...');
|
||||||
self.cleanupLocal(self);
|
|
||||||
}
|
}
|
||||||
).then(
|
|
||||||
self.cleanupLocal
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Cleanup resources related to given table.
|
|
||||||
*
|
|
||||||
* @param {FromMySQL2PostgreSQL} self
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
FromMySQL2PostgreSQL.prototype.cleanupLocal = function(self) {
|
|
||||||
return new Promise(function(resolve) {
|
|
||||||
if (self._clonedSelfTableNamePathFd === undefined) {
|
|
||||||
self.log(self, '\t--[cleanupLocal] Finished processing table `' + self._clonedSelfTableName + '`...');
|
|
||||||
resolve(self);
|
|
||||||
} else {
|
|
||||||
fs.close(self._clonedSelfTableNamePathFd, function() {
|
|
||||||
self.log(self, '\t--[cleanupLocal] Finished processing table `' + self._clonedSelfTableName + '`...');
|
|
||||||
resolve(self);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes DB connections.
|
* Closes DB connections.
|
||||||
*
|
*
|
||||||
|
@ -894,7 +899,7 @@ FromMySQL2PostgreSQL.prototype.cleanupLocal = function(self) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
FromMySQL2PostgreSQL.prototype.closeConnections = function(self) {
|
FromMySQL2PostgreSQL.prototype.closeConnections = function(self) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve, reject) {
|
||||||
if (self._mysql) {
|
if (self._mysql) {
|
||||||
self._mysql.end(function(error) {
|
self._mysql.end(function(error) {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -905,6 +910,7 @@ FromMySQL2PostgreSQL.prototype.closeConnections = function(self) {
|
||||||
pg.end();
|
pg.end();
|
||||||
resolve(self);
|
resolve(self);
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
self.log(self, '\t--[closeConnections] All DB connections to both MySQL and PostgreSQL servers have been closed...');
|
self.log(self, '\t--[closeConnections] All DB connections to both MySQL and PostgreSQL servers have been closed...');
|
||||||
pg.end();
|
pg.end();
|
||||||
|
@ -920,7 +926,7 @@ FromMySQL2PostgreSQL.prototype.closeConnections = function(self) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
FromMySQL2PostgreSQL.prototype.closeLogFiles = function(self) {
|
FromMySQL2PostgreSQL.prototype.closeLogFiles = function(self) {
|
||||||
return new Promise(function(resolveAllLogs) {
|
return new Promise(function(resolveAllLogs, rejectAllLogs) {
|
||||||
if (self._allLogsPathFd) {
|
if (self._allLogsPathFd) {
|
||||||
fs.close(self._allLogsPathFd, function() {
|
fs.close(self._allLogsPathFd, function() {
|
||||||
resolveAllLogs(self);
|
resolveAllLogs(self);
|
||||||
|
@ -929,8 +935,8 @@ FromMySQL2PostgreSQL.prototype.closeLogFiles = function(self) {
|
||||||
resolveAllLogs(self);
|
resolveAllLogs(self);
|
||||||
}
|
}
|
||||||
}).then(
|
}).then(
|
||||||
function() {
|
function(self) {
|
||||||
return new Promise(function(resolveErrorLogs) {
|
return new Promise(function(resolveErrorLogs, rejectErrorLogs) {
|
||||||
if (self._errorLogsPathFd) {
|
if (self._errorLogsPathFd) {
|
||||||
fs.close(self._errorLogsPathFd, function() {
|
fs.close(self._errorLogsPathFd, function() {
|
||||||
resolveErrorLogs(self);
|
resolveErrorLogs(self);
|
||||||
|
@ -950,7 +956,7 @@ FromMySQL2PostgreSQL.prototype.closeLogFiles = function(self) {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
FromMySQL2PostgreSQL.prototype.cleanup = function(self) {
|
FromMySQL2PostgreSQL.prototype.cleanup = function(self) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve, reject) {
|
||||||
self.log(self, '\t--[cleanup] Cleanup resources...');
|
self.log(self, '\t--[cleanup] Cleanup resources...');
|
||||||
resolve(self);
|
resolve(self);
|
||||||
}).then(
|
}).then(
|
||||||
|
@ -960,9 +966,9 @@ FromMySQL2PostgreSQL.prototype.cleanup = function(self) {
|
||||||
).then(
|
).then(
|
||||||
self.closeLogFiles
|
self.closeLogFiles
|
||||||
).then(
|
).then(
|
||||||
function() {
|
function(self) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve, reject) {
|
||||||
self.log(self, '\t--[cleanup] Cleanup finished...');
|
self.log(self, '\t--[cleanup] Cleanup finished...');
|
||||||
resolve(self);
|
resolve(self);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -978,7 +984,7 @@ FromMySQL2PostgreSQL.prototype.cleanup = function(self) {
|
||||||
FromMySQL2PostgreSQL.prototype.run = function(config) {
|
FromMySQL2PostgreSQL.prototype.run = function(config) {
|
||||||
var self = this;
|
var self = this;
|
||||||
self._config = config;
|
self._config = config;
|
||||||
var promise = new Promise(function(resolve) {
|
var promise = new Promise(function(resolve, reject) {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -989,20 +995,23 @@ FromMySQL2PostgreSQL.prototype.run = function(config) {
|
||||||
function() {
|
function() {
|
||||||
console.log('\t--[run] Failed to boot migration');
|
console.log('\t--[run] Failed to boot migration');
|
||||||
}
|
}
|
||||||
|
|
||||||
).then(
|
).then(
|
||||||
self.createTemporaryDirectory,
|
self.createTemporaryDirectory,
|
||||||
function() {
|
function() {
|
||||||
self.log(self, '\t--[run] Logs directory was not created...');
|
self.log(self, '\t--[run] Logs directory was not created...');
|
||||||
}
|
}
|
||||||
|
|
||||||
).then(
|
).then(
|
||||||
self.createSchema,
|
self.createSchema,
|
||||||
function() {
|
function() {
|
||||||
self.log(self, '\t--[run] Temporary directory was not created...');
|
self.log(self, '\t--[run] Temporary directory was not created...');
|
||||||
}
|
}
|
||||||
|
|
||||||
).then(
|
).then(
|
||||||
self.loadStructureToMigrate,
|
self.loadStructureToMigrate,
|
||||||
function() {
|
function() {
|
||||||
return new Promise(function(resolveError) {
|
return new Promise(function(resolveError, rejectError) {
|
||||||
resolveError(self);
|
resolveError(self);
|
||||||
}).then(
|
}).then(
|
||||||
function() {
|
function() {
|
||||||
|
@ -1011,21 +1020,21 @@ FromMySQL2PostgreSQL.prototype.run = function(config) {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
).then(
|
).then(
|
||||||
function() {
|
function() {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve, reject) {
|
||||||
resolve(self);
|
resolve(self);
|
||||||
}).then(
|
}).then(
|
||||||
self.cleanup
|
self.cleanup
|
||||||
).then(
|
).then(
|
||||||
function() {
|
function(self) {
|
||||||
var timeTaken = (new Date()) - self._timeBegin;
|
var timeTaken = (new Date()) - self._timeBegin;
|
||||||
var hours = Math.floor(timeTaken / 1000 / 3600);
|
var hours = Math.floor(timeTaken / 1000 / 3600);
|
||||||
timeTaken -= hours * 1000 * 3600;
|
timeTaken -= hours * 1000 * 3600;
|
||||||
var minutes = Math.floor(timeTaken / 1000 / 60);
|
var minutes = Math.floor(timeTaken / 1000 / 60);
|
||||||
timeTaken -= minutes * 1000 * 60;
|
timeTaken -= minutes * 1000 * 60;
|
||||||
var seconds = Math.ceil(timeTaken / 1000);
|
var seconds = Math.ceil(timeTaken / 1000);
|
||||||
|
|
||||||
hours = hours < 10 ? '0' + hours : hours;
|
hours = hours < 10 ? '0' + hours : hours;
|
||||||
minutes = minutes < 10 ? '0' + minutes : minutes;
|
minutes = minutes < 10 ? '0' + minutes : minutes;
|
||||||
seconds = seconds < 10 ? '0' + seconds : seconds;
|
seconds = seconds < 10 ? '0' + seconds : seconds;
|
||||||
|
@ -1038,7 +1047,7 @@ FromMySQL2PostgreSQL.prototype.run = function(config) {
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
function() {
|
function() {
|
||||||
return new Promise(function(resolveErr) {
|
return new Promise(function(resolveErr, rejectErr) {
|
||||||
resolveErr(self);
|
resolveErr(self);
|
||||||
}).then(
|
}).then(
|
||||||
function() {
|
function() {
|
||||||
|
@ -1052,7 +1061,6 @@ FromMySQL2PostgreSQL.prototype.run = function(config) {
|
||||||
var minutes = Math.floor(timeTaken / 1000 / 60);
|
var minutes = Math.floor(timeTaken / 1000 / 60);
|
||||||
timeTaken -= minutes * 1000 * 60;
|
timeTaken -= minutes * 1000 * 60;
|
||||||
var seconds = Math.ceil(timeTaken / 1000);
|
var seconds = Math.ceil(timeTaken / 1000);
|
||||||
|
|
||||||
hours = hours < 10 ? '0' + hours : hours;
|
hours = hours < 10 ? '0' + hours : hours;
|
||||||
minutes = minutes < 10 ? '0' + minutes : minutes;
|
minutes = minutes < 10 ? '0' + minutes : minutes;
|
||||||
seconds = seconds < 10 ? '0' + seconds : seconds;
|
seconds = seconds < 10 ? '0' + seconds : seconds;
|
||||||
|
@ -1068,5 +1076,3 @@ FromMySQL2PostgreSQL.prototype.run = function(config) {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.FromMySQL2PostgreSQL = FromMySQL2PostgreSQL;
|
module.exports.FromMySQL2PostgreSQL = FromMySQL2PostgreSQL;
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue