Skip to content

Commit

Permalink
Merge branch 'feature/copy-data-from-memory'
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyUss committed Jul 9, 2017
2 parents ce18739 + 31b56e9 commit 7c53394
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 302 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ from MySQL to PostgreSQL as easy and smooth as possible.</p>

<h3>USAGE</h3>
<p><b>1.</b> Create a new database.<br />
<b>Sample:</b>&nbsp;<code> CREATE DATABASE my_postgresql_database;</code></p>
<b>Sample:</b>&nbsp;<code> CREATE DATABASE my_postgresql_database;</code><br />
If you are planning to migrate spatial data (geometry type columns), then <b>PostGIS</b> should be installed and enabled.
</p>

<p><b>2.</b> Download NMIG package and put it on the machine running your PostgreSQL (not mandatory, but preferably).<br />
<b>Sample:</b>&nbsp;<code>/path/to/nmig</code></p>
Expand Down Expand Up @@ -61,7 +63,7 @@ from MySQL to PostgreSQL as easy and smooth as possible.</p>
<a href="mailto:[email protected]?subject=NMIG">[email protected]</a></p>

<h3>VERSION</h3>
<p>Current version is 3.0.0<br />
<p>Current version is 3.1.0<br />
(major version . improvements . bug fixes)</p>

<h3>REMARKS</h3>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nmig",
"version": "3.0.0",
"version": "3.1.0",
"description": "The database migration app",
"author": "Anatoly Khaytovich<[email protected]>",
"license": "GPL-3.0",
Expand Down
2 changes: 1 addition & 1 deletion src/BootProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module.exports = self => {
+ '\n\t\\| \\/ /_|/______/'
+ '\n\n\tNMIG - the database migration tool'
+ '\n\tCopyright (C) 2016 - present, Anatoly Khaytovich <[email protected]>\n\n'
+ '\t--[boot] Configuration has been just loaded.'
+ '\t--[boot] Configuration has being just loaded.'
+ message;

console.log(logo);
Expand Down
68 changes: 68 additions & 0 deletions src/Classes/BufferStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* This file is a part of "NMIG" - the database migration tool.
*
* Copyright (C) 2016 - present, Anatoly Khaytovich <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (please see the "LICENSE.md" file).
* If not, see <http://www.gnu.org/licenses/gpl.txt>.
*
* @author Anatoly Khaytovich <[email protected]>
*/
'use strict';

const { Readable } = require('stream');

module.exports = class BufferStream extends Readable {
/**
* BufferStream constructor.
*
* @param {Buffer} source
*/
constructor(source) {
super();
this._source = source;
this._offset = 0;

// When source buffer consumed entirely, then the 'end' event is emitted.
this.on('end', this.destroy.bind(this));
}

/**
* BufferStream destructor.
*
* @returns {undefined}
*/
destroy() {
this._source = null;
this._offset = null;
}

/**
* Read chunks from the source buffer into the underlying stream buffer.
*
* @param {Number} size
*
* @returns {undefined}
*/
_read(size) {
// Push the next chunk onto the internal stream buffer.
if (this._offset < this._source.length) {
this.push(this._source.slice(this._offset, this._offset + size));
this._offset += size;
return;
}

// When the source ends, then the EOF - signaling `null` chunk should be pushed.
this.push(null);
}
};
33 changes: 7 additions & 26 deletions src/CleanupProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,33 @@
*/
'use strict';

const log = require('./Logger');
const generateError = require('./ErrorGenerator');
const directoriesManager = require('./DirectoriesManager');
const log = require('./Logger');

/**
* Closes DB connections.
* Closes DB connections and removes the "./temporary_directory".
*
* @param {Conversion} self
*
* @returns {Promise}
*/
const closeConnections = self => {
module.exports = self => {
log(self, '\t--[cleanup] Cleanup resources...');

return new Promise(resolve => {
self._pg = null;

if (self._mysql) {
self._mysql.end(error => {
if (error) {
log(self, '\t--[closeConnections] ' + error);
}

log(self, '\t--[closeConnections] All DB connections to both MySQL and PostgreSQL servers have been closed...');
self._pg = null;
resolve();
});
} else {
log(self, '\t--[closeConnections] All DB connections to both MySQL and PostgreSQL servers have been closed...');
self._pg = null;
resolve();
}
});
}

/**
* Closes DB connections and removes the "./temporary_directory".
*
* @param {Conversion} self
*
* @returns {Promise}
*/
module.exports = self => {
return new Promise(resolve => {
log(self, '\t--[cleanup] Cleanup resources...');
return directoriesManager.removeTemporaryDirectory(self).then(() => {
return closeConnections(self);
}).then(() => {
log(self, '\t--[cleanup] Cleanup finished...');
resolve();
});
});
};
92 changes: 64 additions & 28 deletions src/ColumnsDataArranger.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,53 @@
*/
'use strict';

/**
* Define if given type is one of MySQL spacial types.
*
* @param {String} type
*
* @returns {Boolean}
*/
const isSpacial = type => {
return type.indexOf('geometry') !== -1
|| type.indexOf('point') !== -1
|| type.indexOf('linestring') !== -1
|| type.indexOf('polygon') !== -1;
};

/**
* Define if given type is one of MySQL binary types.
*
* @param {String} type
*
* @returns {Boolean}
*/
const isBinary = type => {
return type.indexOf('blob') !== -1 || type.indexOf('binary') !== -1;
};

/**
* Define if given type is one of MySQL bit types.
*
* @param {String} type
*
* @returns {Boolean}
*/
const isBit = type => {
return type.indexOf('bit') !== -1;
};

/**
* Define if given type is one of MySQL date-time types.
*
* @param {String} type
*
* @returns {Boolean}
*/
const isDateTime = type => {
return type.indexOf('timestamp') !== -1 || type.indexOf('date') !== -1;
};

/**
* Arranges columns data before loading.
*
Expand All @@ -29,36 +76,25 @@
* @returns {String}
*/
module.exports = (arrTableColumns, mysqlVersion) => {
let strRetVal = '';
let strRetVal = '';
const arrTableColumnsLength = arrTableColumns.length;
const wkbFunc = mysqlVersion >= 5.76 ? 'ST_AsWKB' : 'AsWKB';

for (let i = 0; i < arrTableColumnsLength; ++i) {
const field = arrTableColumns[i].Field;
const type = arrTableColumns[i].Type;

for (let i = 0; i < arrTableColumns.length; ++i) {
if (
arrTableColumns[i].Type.indexOf('geometry') !== -1
|| arrTableColumns[i].Type.indexOf('point') !== -1
|| arrTableColumns[i].Type.indexOf('linestring') !== -1
|| arrTableColumns[i].Type.indexOf('polygon') !== -1
) {
strRetVal += mysqlVersion >= 5.76
? 'HEX(ST_AsWKB(`' + arrTableColumns[i].Field + '`)),'
: 'HEX(AsWKB(`' + arrTableColumns[i].Field + '`)),';
} else if (
arrTableColumns[i].Type.indexOf('blob') !== -1
|| arrTableColumns[i].Type.indexOf('binary') !== -1
) {
strRetVal += 'HEX(`' + arrTableColumns[i].Field + '`),';
} else if (
arrTableColumns[i].Type.indexOf('bit') !== -1
) {
strRetVal += 'BIN(`' + arrTableColumns[i].Field + '`),';
} else if (
arrTableColumns[i].Type.indexOf('timestamp') !== -1
|| arrTableColumns[i].Type.indexOf('date') !== -1
) {
strRetVal += 'IF(`' + arrTableColumns[i].Field
+ '` IN(\'0000-00-00\', \'0000-00-00 00:00:00\'), \'-INFINITY\', CAST(`'
+ arrTableColumns[i].Field + '` AS CHAR)),';
if (isSpacial(type)) {
strRetVal += 'HEX(' + wkbFunc + '(`' + field + '`)) AS `' + field + '`,';
} else if (isBinary(type)) {
strRetVal += 'HEX(`' + field + '`) AS `' + field + '`,';
} else if (isBit(type)) {
strRetVal += 'BIN(`' + field + '`) AS `' + field + '`,';
} else if (isDateTime(type)) {
strRetVal += 'IF(`' + field + '` IN(\'0000-00-00\', \'0000-00-00 00:00:00\'), \'-INFINITY\', CAST(`'
+ field + '` AS CHAR)) AS `' + field + '`,';
} else {
strRetVal += '`' + arrTableColumns[i].Field + '`,';
strRetVal += '`' + field + '` AS `' + field + '`,';
}
}

Expand Down
Loading

0 comments on commit 7c53394

Please sign in to comment.