From 4b9e445f0df4113ecf510de02fd5c7b5733d3554 Mon Sep 17 00:00:00 2001 From: AnatolyUss Date: Sat, 20 May 2017 14:16:19 +0300 Subject: [PATCH] major refactoring --- .gitignore | 4 + README.md | 6 +- migration/fmtp/Main.js | 115 ------------------ nmig.js | 89 -------------- package.json | 5 +- {migration/fmtp => src}/BootProcessor.js | 2 +- {migration/fmtp => src}/CleanupProcessor.js | 0 .../fmtp => src}/ColumnsDataArranger.js | 0 {migration/fmtp => src}/CommentsProcessor.js | 0 {migration/fmtp => src}/Connector.js | 0 .../fmtp => src}/ConsistencyEnforcer.js | 0 .../fmtp => src}/ConstraintsProcessor.js | 0 {migration/fmtp => src}/Conversion.js | 0 .../fmtp => src}/CsvStringifyModified.js | 0 .../fmtp => src}/DataChunksProcessor.js | 0 {migration/fmtp => src}/DataLoader.js | 0 {migration/fmtp => src}/DataPipeManager.js | 0 {migration/fmtp => src}/DataPoolManager.js | 16 +-- {migration/fmtp => src}/DataTypesMapReader.js | 17 +-- {migration/fmtp => src}/DefaultProcessor.js | 0 {migration/fmtp => src}/DirectoriesManager.js | 20 +-- {migration/fmtp => src}/EnumProcessor.js | 0 {migration/fmtp => src}/ErrorGenerator.js | 0 .../fmtp => src}/ExtraConfigProcessor.js | 0 .../fmtp => src}/ForeignKeyProcessor.js | 0 .../fmtp => src}/IndexAndKeyProcessor.js | 0 {migration/fmtp => src}/IntegerValidator.js | 0 {migration/fmtp => src}/Logger.js | 0 src/Main.js | 114 +++++++++++++++++ .../fmtp => src}/MessageToDataLoader.js | 0 {migration/fmtp => src}/MessageToMaster.js | 0 .../fmtp => src}/MigrationStateManager.js | 14 +-- {migration/fmtp => src}/NewBuffer.js | 0 {migration/fmtp => src}/NullProcessor.js | 0 {migration/fmtp => src}/OldBuffer.js | 0 {migration/fmtp => src}/ReportGenerator.js | 0 {migration/fmtp => src}/SchemaProcessor.js | 12 +- {migration/fmtp => src}/SequencesProcessor.js | 0 {migration/fmtp => src}/StructureLoader.js | 10 +- {migration/fmtp => src}/Table.js | 0 {migration/fmtp => src}/TableProcessor.js | 0 {migration/fmtp => src}/VacuumProcessor.js | 0 {migration/fmtp => src}/ViewGenerator.js | 0 43 files changed, 168 insertions(+), 256 deletions(-) create mode 100755 .gitignore delete mode 100644 migration/fmtp/Main.js delete mode 100644 nmig.js rename {migration/fmtp => src}/BootProcessor.js (98%) rename {migration/fmtp => src}/CleanupProcessor.js (100%) rename {migration/fmtp => src}/ColumnsDataArranger.js (100%) rename {migration/fmtp => src}/CommentsProcessor.js (100%) rename {migration/fmtp => src}/Connector.js (100%) rename {migration/fmtp => src}/ConsistencyEnforcer.js (100%) rename {migration/fmtp => src}/ConstraintsProcessor.js (100%) rename {migration/fmtp => src}/Conversion.js (100%) rename {migration/fmtp => src}/CsvStringifyModified.js (100%) rename {migration/fmtp => src}/DataChunksProcessor.js (100%) rename {migration/fmtp => src}/DataLoader.js (100%) rename {migration/fmtp => src}/DataPipeManager.js (100%) rename {migration/fmtp => src}/DataPoolManager.js (93%) rename {migration/fmtp => src}/DataTypesMapReader.js (73%) rename {migration/fmtp => src}/DefaultProcessor.js (100%) rename {migration/fmtp => src}/DirectoriesManager.js (93%) rename {migration/fmtp => src}/EnumProcessor.js (100%) rename {migration/fmtp => src}/ErrorGenerator.js (100%) rename {migration/fmtp => src}/ExtraConfigProcessor.js (100%) rename {migration/fmtp => src}/ForeignKeyProcessor.js (100%) rename {migration/fmtp => src}/IndexAndKeyProcessor.js (100%) rename {migration/fmtp => src}/IntegerValidator.js (100%) rename {migration/fmtp => src}/Logger.js (100%) create mode 100644 src/Main.js rename {migration/fmtp => src}/MessageToDataLoader.js (100%) rename {migration/fmtp => src}/MessageToMaster.js (100%) rename {migration/fmtp => src}/MigrationStateManager.js (95%) rename {migration/fmtp => src}/NewBuffer.js (100%) rename {migration/fmtp => src}/NullProcessor.js (100%) rename {migration/fmtp => src}/OldBuffer.js (100%) rename {migration/fmtp => src}/ReportGenerator.js (100%) rename {migration/fmtp => src}/SchemaProcessor.js (89%) rename {migration/fmtp => src}/SequencesProcessor.js (100%) rename {migration/fmtp => src}/StructureLoader.js (96%) rename {migration/fmtp => src}/Table.js (100%) rename {migration/fmtp => src}/TableProcessor.js (100%) rename {migration/fmtp => src}/VacuumProcessor.js (100%) rename {migration/fmtp => src}/ViewGenerator.js (100%) diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..6df7846 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea +node_modules +logs_directory +temporary_directory diff --git a/README.md b/README.md index 4b81e73..f6f410e 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,11 @@ from MySQL to PostgreSQL as easy and smooth as possible.

    Sample:
$ cd /path/to/nmig

$ npm install

-
$ node nmig.js

+
$ npm start

5. If a disaster took place during migration (for what ever reason) - simply restart the process -$ node nmig.js
    NMIG will restart from the point it was stopped at. +$ npm start
    NMIG will restart from the point it was stopped at.

6. At the end of migration check log files, if necessary.
    @@ -61,7 +61,7 @@ from MySQL to PostgreSQL as easy and smooth as possible.

anatolyuss@gmail.com

VERSION

-

Current version is 2.6.1
+

Current version is 3.0.0
(major version . improvements . bug fixes)

diff --git a/migration/fmtp/Main.js b/migration/fmtp/Main.js deleted file mode 100644 index bf22bd9..0000000 --- a/migration/fmtp/Main.js +++ /dev/null @@ -1,115 +0,0 @@ -/* - * This file is a part of "NMIG" - the database migration tool. - * - * Copyright (C) 2016 - present, Anatoly Khaytovich - * - * 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 . - * - * @author Anatoly Khaytovich - */ -'use strict'; - -const readDataTypesMap = require('./DataTypesMapReader'); -const log = require('./Logger'); -const generateError = require('./ErrorGenerator'); -const Conversion = require('./Conversion'); -const migrationStateManager = require('./MigrationStateManager'); -const createSchema = require('./SchemaProcessor'); -const cleanup = require('./CleanupProcessor'); -const dataPoolManager = require('./DataPoolManager'); -const directoriesManager = require('./DirectoriesManager'); -const loadStructureToMigrate = require('./StructureLoader'); -const pipeData = require('./DataPipeManager'); -const boot = require('./BootProcessor'); - -/** - * Runs migration according to user's configuration. - * - * @param {Object} config - * - * @returns {undefined} - */ -module.exports = config => { - const self = new Conversion(config); - - boot(self).then(() => { - return readDataTypesMap(self); - }).then( - () => { - return directoriesManager.createLogsDirectory(self); - }, - () => { - // Braces are essential. Without them promises-chain will continue execution. - console.log('\t--[Main] Failed to boot migration'); - } - ).then( - () => { - return directoriesManager.createTemporaryDirectory(self); - }, - () => { - // Braces are essential. Without them promises-chain will continue execution. - log(self, '\t--[Main] Logs directory was not created...'); - } - ).then( - () => { - return createSchema(self); - }, - () => { - const msg = '\t--[Main] The temporary directory [' + self._tempDirPath + '] already exists...' - + '\n\t Please, remove this directory and rerun NMIG...'; - - log(self, msg); - } - ).then( - () => { - return migrationStateManager.createStateLogsTable(self); - }, - () => { - generateError(self, '\t--[Main] Cannot create new DB schema...'); - return cleanup(self); - } - ).then( - () => { - return dataPoolManager.createDataPoolTable(self); - }, - () => { - generateError(self, '\t--[Main] Cannot create execution_logs table...'); - return cleanup(self); - } - ).then( - () => { - return loadStructureToMigrate(self); - }, - () => { - generateError(self, '\t--[Main] Cannot create data-pool...'); - return cleanup(self); - } - ).then( - () => { - return dataPoolManager.readDataPool(self); - }, - () => { - generateError(self, '\t--[Main] NMIG cannot load source database structure...'); - return cleanup(self); - } - ).then( - () => { - pipeData(self); - }, - () => { - generateError(self, '\t--[Main] NMIG failed to load Data-Units pool...'); - return cleanup(self); - } - ); -}; diff --git a/nmig.js b/nmig.js deleted file mode 100644 index d53fcf3..0000000 --- a/nmig.js +++ /dev/null @@ -1,89 +0,0 @@ -/* - * This file is a part of "NMIG" - the database migration tool. - * - * Copyright (C) 2016 - present, Anatoly Khaytovich - * - * 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 . - * - * @author Anatoly Khaytovich - */ -'use strict'; - -const fs = require('fs'); -const path = require('path'); -const main = require('./migration/fmtp/Main'); - -/** - * Read the configuration file. - * - * @returns {Promise} - */ -const readConfig = () => { - return new Promise((resolve, reject) => { - const strPathToConfig = path.join(__dirname, 'config.json'); - - fs.readFile(strPathToConfig, (error, data) => { - if (error) { - reject('\n\t--Cannot run migration\nCannot read configuration info from ' + strPathToConfig); - } else { - try { - const config = JSON.parse(data.toString()); - config.tempDirPath = path.join(__dirname, 'temporary_directory'); - config.logsDirPath = path.join(__dirname, 'logs_directory'); - config.dataTypesMapAddr = path.join(__dirname, 'DataTypesMap.json'); - resolve(config); - } catch (err) { - reject('\n\t--Cannot parse JSON from ' + strPathToConfig); - } - } - }); - - }); -} - -/** - * Read the extra configuration file, if necessary. - * - * @param {Object} config - * - * @returns {Promise} - */ -const readExtraConfig = config => { - return new Promise((resolve, reject) => { - if (config.enable_extra_config !== true) { - config.extraConfig = null; - return resolve(config); - } - - const strPathToExtraConfig = path.join(__dirname, 'extra_config.json'); - - fs.readFile(strPathToExtraConfig, (error, data) => { - if (error) { - reject('\n\t--Cannot run migration\nCannot read configuration info from ' + strPathToExtraConfig); - } else { - try { - config.extraConfig = JSON.parse(data.toString()); - resolve(config); - } catch (err) { - reject('\n\t--Cannot parse JSON from ' + strPathToExtraConfig); - } - } - }); - }); -} - -readConfig() - .then(readExtraConfig) - .then(config => main(config)) - .catch(error => console.log(error)); diff --git a/package.json b/package.json index 44ed669..2c5b5b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nmig", - "version": "2.6.1", + "version": "3.0.0", "description": "The database migration app", "author": "Anatoly Khaytovich", "license": "GPL-3.0", @@ -12,5 +12,8 @@ "mysql": "*", "pg": "*", "pg-copy-streams": "*" + }, + "scripts": { + "start": "node ./src/Main.js" } } diff --git a/migration/fmtp/BootProcessor.js b/src/BootProcessor.js similarity index 98% rename from migration/fmtp/BootProcessor.js rename to src/BootProcessor.js index c2bc277..f475b69 100644 --- a/migration/fmtp/BootProcessor.js +++ b/src/BootProcessor.js @@ -76,7 +76,7 @@ module.exports = self => { } if (stdin.indexOf('Y') !== -1) { - resolve(); + resolve(self); } }); } diff --git a/migration/fmtp/CleanupProcessor.js b/src/CleanupProcessor.js similarity index 100% rename from migration/fmtp/CleanupProcessor.js rename to src/CleanupProcessor.js diff --git a/migration/fmtp/ColumnsDataArranger.js b/src/ColumnsDataArranger.js similarity index 100% rename from migration/fmtp/ColumnsDataArranger.js rename to src/ColumnsDataArranger.js diff --git a/migration/fmtp/CommentsProcessor.js b/src/CommentsProcessor.js similarity index 100% rename from migration/fmtp/CommentsProcessor.js rename to src/CommentsProcessor.js diff --git a/migration/fmtp/Connector.js b/src/Connector.js similarity index 100% rename from migration/fmtp/Connector.js rename to src/Connector.js diff --git a/migration/fmtp/ConsistencyEnforcer.js b/src/ConsistencyEnforcer.js similarity index 100% rename from migration/fmtp/ConsistencyEnforcer.js rename to src/ConsistencyEnforcer.js diff --git a/migration/fmtp/ConstraintsProcessor.js b/src/ConstraintsProcessor.js similarity index 100% rename from migration/fmtp/ConstraintsProcessor.js rename to src/ConstraintsProcessor.js diff --git a/migration/fmtp/Conversion.js b/src/Conversion.js similarity index 100% rename from migration/fmtp/Conversion.js rename to src/Conversion.js diff --git a/migration/fmtp/CsvStringifyModified.js b/src/CsvStringifyModified.js similarity index 100% rename from migration/fmtp/CsvStringifyModified.js rename to src/CsvStringifyModified.js diff --git a/migration/fmtp/DataChunksProcessor.js b/src/DataChunksProcessor.js similarity index 100% rename from migration/fmtp/DataChunksProcessor.js rename to src/DataChunksProcessor.js diff --git a/migration/fmtp/DataLoader.js b/src/DataLoader.js similarity index 100% rename from migration/fmtp/DataLoader.js rename to src/DataLoader.js diff --git a/migration/fmtp/DataPipeManager.js b/src/DataPipeManager.js similarity index 100% rename from migration/fmtp/DataPipeManager.js rename to src/DataPipeManager.js diff --git a/migration/fmtp/DataPoolManager.js b/src/DataPoolManager.js similarity index 93% rename from migration/fmtp/DataPoolManager.js rename to src/DataPoolManager.js index fee9503..981e283 100644 --- a/migration/fmtp/DataPoolManager.js +++ b/src/DataPoolManager.js @@ -33,11 +33,11 @@ const generateError = require('./ErrorGenerator'); */ module.exports.createDataPoolTable = self => { return connect(self).then(() => { - return new Promise((resolve, reject) => { + return new Promise(resolve => { self._pg.connect((error, client, done) => { if (error) { generateError(self, '\t--[DataPoolManager.createDataPoolTable] Cannot connect to PostgreSQL server...\n' + error); - reject(); + process.exit(); } else { const sql = 'CREATE TABLE IF NOT EXISTS "' + self._schema + '"."data_pool_' + self._schema + self._mySqlDbName + '"("id" BIGSERIAL, "json" TEXT, "is_started" BOOLEAN);'; @@ -47,10 +47,10 @@ module.exports.createDataPoolTable = self => { if (err) { generateError(self, '\t--[DataPoolManager.createDataPoolTable] ' + err, sql); - reject(); + process.exit(); } else { log(self, '\t--[DataPoolManager.createDataPoolTable] table "' + self._schema + '"."data_pool_' + self._schema + self._mySqlDbName + '" is created...'); - resolve(); + resolve(self); } }); } @@ -102,11 +102,11 @@ module.exports.dropDataPoolTable = self => { */ module.exports.readDataPool = self => { return connect(self).then(() => { - return new Promise((resolve, reject) => { + return new Promise(resolve => { self._pg.connect((error, client, done) => { if (error) { generateError(self, '\t--[DataPoolManager.readDataPool] Cannot connect to PostgreSQL server...\n' + error); - reject(); + process.exit(); } else { const sql = 'SELECT id AS id, json AS json FROM "' + self._schema + '"."data_pool_' + self._schema + self._mySqlDbName + '";'; client.query(sql, (err, arrDataPool) => { @@ -114,7 +114,7 @@ module.exports.readDataPool = self => { if (err) { generateError(self, '\t--[DataPoolManager.readDataPool] ' + err, sql); - return reject(); + process.exit(); } for (let i = 0; i < arrDataPool.rows.length; ++i) { @@ -124,7 +124,7 @@ module.exports.readDataPool = self => { } log(self, '\t--[DataPoolManager.readDataPool] Data-Pool is loaded...'); - resolve(); + resolve(self); }); } }); diff --git a/migration/fmtp/DataTypesMapReader.js b/src/DataTypesMapReader.js similarity index 73% rename from migration/fmtp/DataTypesMapReader.js rename to src/DataTypesMapReader.js index c1e931a..f70e938 100644 --- a/migration/fmtp/DataTypesMapReader.js +++ b/src/DataTypesMapReader.js @@ -31,21 +31,16 @@ const fs = require('fs'); * @returns {Promise} */ module.exports = self => { - return new Promise((resolve, reject) => { + return new Promise(resolve => { fs.readFile(self._dataTypesMapAddr, (error, data) => { if (error) { console.log('\t--[readDataTypesMap] Cannot read "DataTypesMap" from ' + self._dataTypesMapAddr); - reject(); - } else { - try { - self._dataTypesMap = JSON.parse(data.toString()); - console.log('\t--[readDataTypesMap] Data Types Map is loaded...'); - resolve(); - } catch (err) { - console.log('\t--[readDataTypesMap] Cannot parse JSON from' + self._dataTypesMapAddr); - reject(); - } + process.exit(); } + + self._dataTypesMap = JSON.parse(data); + console.log('\t--[readDataTypesMap] Data Types Map is loaded...'); + resolve(self); }); }); }; diff --git a/migration/fmtp/DefaultProcessor.js b/src/DefaultProcessor.js similarity index 100% rename from migration/fmtp/DefaultProcessor.js rename to src/DefaultProcessor.js diff --git a/migration/fmtp/DirectoriesManager.js b/src/DirectoriesManager.js similarity index 93% rename from migration/fmtp/DirectoriesManager.js rename to src/DirectoriesManager.js index 248bd1d..1b799d3 100644 --- a/migration/fmtp/DirectoriesManager.js +++ b/src/DirectoriesManager.js @@ -32,7 +32,7 @@ const log = require('./Logger'); * @returns {Promise} */ module.exports.createTemporaryDirectory = self => { - return new Promise((resolve, reject) => { + return new Promise(resolve => { log(self, '\t--[DirectoriesManager.createTemporaryDirectory] Creating temporary directory...'); fs.stat(self._tempDirPath, (directoryDoesNotExist, stat) => { if (directoryDoesNotExist) { @@ -42,17 +42,17 @@ module.exports.createTemporaryDirectory = self => { + '"temporary_directory": ' + self._tempDirPath; log(self, msg); - reject(); + process.exit(); } else { log(self, '\t--[DirectoriesManager.createTemporaryDirectory] Temporary directory is created...'); - resolve(); + resolve(self); } }); } else if (!stat.isDirectory()) { log(self, '\t--[DirectoriesManager.createTemporaryDirectory] Cannot perform a migration due to unexpected error'); - reject(); + process.exit(); } else { - resolve(); + resolve(self); } }); }); @@ -113,7 +113,7 @@ module.exports.removeTemporaryDirectory = self => { * @returns {Promise} */ module.exports.createLogsDirectory = self => { - return new Promise((resolve, reject) => { + return new Promise(resolve => { console.log('\t--[DirectoriesManager.createLogsDirectory] Creating logs directory...'); fs.stat(self._logsDirPath, (directoryDoesNotExist, stat) => { if (directoryDoesNotExist) { @@ -123,18 +123,18 @@ module.exports.createLogsDirectory = self => { + '"logs_directory": ' + self._logsDirPath; console.log(msg); - reject(); + process.exit(); } else { log(self, '\t--[DirectoriesManager.createLogsDirectory] Logs directory is created...'); - resolve(); + resolve(self); } }); } else if (!stat.isDirectory()) { console.log('\t--[DirectoriesManager.createLogsDirectory] Cannot perform a migration due to unexpected error'); - reject(); + process.exit(); } else { log(self, '\t--[DirectoriesManager.createLogsDirectory] Logs directory already exists...'); - resolve(); + resolve(self); } }); }); diff --git a/migration/fmtp/EnumProcessor.js b/src/EnumProcessor.js similarity index 100% rename from migration/fmtp/EnumProcessor.js rename to src/EnumProcessor.js diff --git a/migration/fmtp/ErrorGenerator.js b/src/ErrorGenerator.js similarity index 100% rename from migration/fmtp/ErrorGenerator.js rename to src/ErrorGenerator.js diff --git a/migration/fmtp/ExtraConfigProcessor.js b/src/ExtraConfigProcessor.js similarity index 100% rename from migration/fmtp/ExtraConfigProcessor.js rename to src/ExtraConfigProcessor.js diff --git a/migration/fmtp/ForeignKeyProcessor.js b/src/ForeignKeyProcessor.js similarity index 100% rename from migration/fmtp/ForeignKeyProcessor.js rename to src/ForeignKeyProcessor.js diff --git a/migration/fmtp/IndexAndKeyProcessor.js b/src/IndexAndKeyProcessor.js similarity index 100% rename from migration/fmtp/IndexAndKeyProcessor.js rename to src/IndexAndKeyProcessor.js diff --git a/migration/fmtp/IntegerValidator.js b/src/IntegerValidator.js similarity index 100% rename from migration/fmtp/IntegerValidator.js rename to src/IntegerValidator.js diff --git a/migration/fmtp/Logger.js b/src/Logger.js similarity index 100% rename from migration/fmtp/Logger.js rename to src/Logger.js diff --git a/src/Main.js b/src/Main.js new file mode 100644 index 0000000..b2c9ccf --- /dev/null +++ b/src/Main.js @@ -0,0 +1,114 @@ +/* + * This file is a part of "NMIG" - the database migration tool. + * + * Copyright (C) 2016 - present, Anatoly Khaytovich + * + * 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 . + * + * @author Anatoly Khaytovich + */ +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const readDataTypesMap = require('./DataTypesMapReader'); +const Conversion = require('./Conversion'); +const createSchema = require('./SchemaProcessor'); +const loadStructureToMigrate = require('./StructureLoader'); +const pipeData = require('./DataPipeManager'); +const boot = require('./BootProcessor'); +const { createStateLogsTable } = require('./MigrationStateManager'); +const { createDataPoolTable, readDataPool } = require('./DataPoolManager'); +const { createLogsDirectory, createTemporaryDirectory } = require('./DirectoriesManager'); + +/** + * Read the configuration file. + * + * @returns {Promise} + */ +const readConfig = () => { + return new Promise(resolve => { + const strPathToConfig = path.join(__dirname, '..', 'config.json'); + + fs.readFile(strPathToConfig, (error, data) => { + if (error) { + console.log('\n\t--Cannot run migration\nCannot read configuration info from ' + strPathToConfig); + process.exit(); + } + + const config = JSON.parse(data); + config.tempDirPath = path.join(__dirname, '..', 'temporary_directory'); + config.logsDirPath = path.join(__dirname, '..', 'logs_directory'); + config.dataTypesMapAddr = path.join(__dirname, '..', 'DataTypesMap.json'); + resolve(config); + }); + + }); +}; + +/** + * Read the extra configuration file, if necessary. + * + * @param {Object} config + * + * @returns {Promise} + */ +const readExtraConfig = config => { + return new Promise(resolve => { + if (config.enable_extra_config !== true) { + config.extraConfig = null; + return resolve(config); + } + + const strPathToExtraConfig = path.join(__dirname, '..', 'extra_config.json'); + + fs.readFile(strPathToExtraConfig, (error, data) => { + if (error) { + console.log('\n\t--Cannot run migration\nCannot read configuration info from ' + strPathToExtraConfig); + process.exit(); + } + + config.extraConfig = JSON.parse(data); + resolve(config); + }); + }); +}; + +/** + * Initialize Conversion instance. + * + * @param {Object} config + * + * @returns {Promise} + */ +const initializeConversion = config => { + return new Promise(resolve => { + resolve(new Conversion(config)); + }); +}; + +readConfig() + .then(readExtraConfig) + .then(initializeConversion) + .then(boot) + .then(readDataTypesMap) + .then(createLogsDirectory) + .then(createTemporaryDirectory) + .then(createSchema) + .then(createStateLogsTable) + .then(createDataPoolTable) + .then(loadStructureToMigrate) + .then(readDataPool) + .then(pipeData) + .catch(error => console.log(error)); diff --git a/migration/fmtp/MessageToDataLoader.js b/src/MessageToDataLoader.js similarity index 100% rename from migration/fmtp/MessageToDataLoader.js rename to src/MessageToDataLoader.js diff --git a/migration/fmtp/MessageToMaster.js b/src/MessageToMaster.js similarity index 100% rename from migration/fmtp/MessageToMaster.js rename to src/MessageToMaster.js diff --git a/migration/fmtp/MigrationStateManager.js b/src/MigrationStateManager.js similarity index 95% rename from migration/fmtp/MigrationStateManager.js rename to src/MigrationStateManager.js index 0a1e716..4083abf 100644 --- a/migration/fmtp/MigrationStateManager.js +++ b/src/MigrationStateManager.js @@ -101,11 +101,11 @@ module.exports.set = (self, param) => { */ module.exports.createStateLogsTable = self => { return connect(self).then(() => { - return new Promise((resolve, reject) => { + return new Promise(resolve => { self._pg.connect((error, client, done) => { if (error) { generateError(self, '\t--[createStateLogsTable] Cannot connect to PostgreSQL server...\n' + error); - reject(); + process.exit(); } else { let sql = 'CREATE TABLE IF NOT EXISTS "' + self._schema + '"."state_logs_' + self._schema + self._mySqlDbName + '"(' @@ -119,14 +119,14 @@ module.exports.createStateLogsTable = self => { if (err) { done(); generateError(self, '\t--[createStateLogsTable] ' + err, sql); - reject(); + process.exit(); } else { sql = 'SELECT COUNT(1) AS cnt FROM "' + self._schema + '"."state_logs_' + self._schema + self._mySqlDbName + '";'; client.query(sql, (errorCount, result) => { if (errorCount) { done(); generateError(self, '\t--[createStateLogsTable] ' + errorCount, sql); - reject(); + process.exit(); } else if (+result.rows[0].cnt === 0) { sql = 'INSERT INTO "' + self._schema + '"."state_logs_' + self._schema + self._mySqlDbName + '" VALUES(FALSE, FALSE, FALSE, FALSE);'; @@ -136,13 +136,13 @@ module.exports.createStateLogsTable = self => { if (errorInsert) { generateError(self, '\t--[createStateLogsTable] ' + errorInsert, sql); - reject(); + process.exit(); } else { const msg = '\t--[createStateLogsTable] table "' + self._schema + '"."state_logs_' + self._schema + self._mySqlDbName + '" is created...'; log(self, msg); - resolve(); + resolve(self); } }); } else { @@ -150,7 +150,7 @@ module.exports.createStateLogsTable = self => { + self._schema + self._mySqlDbName + '" is created...'; log(self, msg2); - resolve(); + resolve(self); } }); } diff --git a/migration/fmtp/NewBuffer.js b/src/NewBuffer.js similarity index 100% rename from migration/fmtp/NewBuffer.js rename to src/NewBuffer.js diff --git a/migration/fmtp/NullProcessor.js b/src/NullProcessor.js similarity index 100% rename from migration/fmtp/NullProcessor.js rename to src/NullProcessor.js diff --git a/migration/fmtp/OldBuffer.js b/src/OldBuffer.js similarity index 100% rename from migration/fmtp/OldBuffer.js rename to src/OldBuffer.js diff --git a/migration/fmtp/ReportGenerator.js b/src/ReportGenerator.js similarity index 100% rename from migration/fmtp/ReportGenerator.js rename to src/ReportGenerator.js diff --git a/migration/fmtp/SchemaProcessor.js b/src/SchemaProcessor.js similarity index 89% rename from migration/fmtp/SchemaProcessor.js rename to src/SchemaProcessor.js index 321470b..c9cc4ca 100644 --- a/migration/fmtp/SchemaProcessor.js +++ b/src/SchemaProcessor.js @@ -33,18 +33,18 @@ const generateError = require('./ErrorGenerator'); */ module.exports = self => { return connect(self).then(() => { - return new Promise((resolve, reject) => { + return new Promise(resolve => { self._pg.connect((error, client, done) => { if (error) { generateError(self, '\t--[createSchema] Cannot connect to PostgreSQL server...\n' + error); - reject(); + process.exit(); } else { let sql = "SELECT schema_name FROM information_schema.schemata WHERE schema_name = '" + self._schema + "';"; client.query(sql, (err, result) => { if (err) { done(); generateError(self, '\t--[createSchema] ' + err, sql); - reject(); + process.exit(); } else if (result.rows.length === 0) { sql = 'CREATE SCHEMA "' + self._schema + '";'; client.query(sql, err => { @@ -52,13 +52,13 @@ module.exports = self => { if (err) { generateError(self, '\t--[createSchema] ' + err, sql); - reject(); + process.exit(); } else { - resolve(); + resolve(self); } }); } else { - resolve(); + resolve(self); } }); } diff --git a/migration/fmtp/SequencesProcessor.js b/src/SequencesProcessor.js similarity index 100% rename from migration/fmtp/SequencesProcessor.js rename to src/SequencesProcessor.js diff --git a/migration/fmtp/StructureLoader.js b/src/StructureLoader.js similarity index 96% rename from migration/fmtp/StructureLoader.js rename to src/StructureLoader.js index a226827..1b0406f 100644 --- a/migration/fmtp/StructureLoader.js +++ b/src/StructureLoader.js @@ -96,12 +96,12 @@ const getMySqlVersion = self => { module.exports = self => { return getMySqlVersion(self).then(() => { return migrationStateManager.get(self, 'tables_loaded').then(haveTablesLoaded => { - return new Promise((resolve, reject) => { + return new Promise(resolve => { self._mysql.getConnection((error, connection) => { if (error) { // The connection is undefined. generateError(self, '\t--[loadStructureToMigrate] Cannot connect to MySQL server...\n' + error); - reject(); + process.exit(); } else { const sql = 'SHOW FULL TABLES IN `' + self._mySqlDbName + '`;'; connection.query(sql, (strErr, rows) => { @@ -109,7 +109,7 @@ module.exports = self => { if (strErr) { generateError(self, '\t--[loadStructureToMigrate] ' + strErr, sql); - reject(); + process.exit(); } else { let tablesCnt = 0; let viewsCnt = 0; @@ -141,9 +141,9 @@ module.exports = self => { Promise.all(processTablePromises).then( () => { - migrationStateManager.set(self, 'tables_loaded').then(() => resolve()); + migrationStateManager.set(self, 'tables_loaded').then(() => resolve(self)); }, - () => reject() + () => process.exit() ); } }); diff --git a/migration/fmtp/Table.js b/src/Table.js similarity index 100% rename from migration/fmtp/Table.js rename to src/Table.js diff --git a/migration/fmtp/TableProcessor.js b/src/TableProcessor.js similarity index 100% rename from migration/fmtp/TableProcessor.js rename to src/TableProcessor.js diff --git a/migration/fmtp/VacuumProcessor.js b/src/VacuumProcessor.js similarity index 100% rename from migration/fmtp/VacuumProcessor.js rename to src/VacuumProcessor.js diff --git a/migration/fmtp/ViewGenerator.js b/src/ViewGenerator.js similarity index 100% rename from migration/fmtp/ViewGenerator.js rename to src/ViewGenerator.js