|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Promise = require('bluebird'); |
| 4 | +const Async = require('async'); |
| 5 | + |
| 6 | +class MysqlDatabaseSetupDao { |
| 7 | + |
| 8 | + /** |
| 9 | + * @param {MysqlService} mysqlService |
| 10 | + * @param {string} schemaSourceDatabase |
| 11 | + */ |
| 12 | + constructor(mysqlService, schemaSourceDatabase) { |
| 13 | + this._mysqlService = mysqlService; |
| 14 | + this._schemaSourceDatabase = schemaSourceDatabase; |
| 15 | + } |
| 16 | + |
| 17 | + replicateAllTables(done) { |
| 18 | + const self = this; |
| 19 | + |
| 20 | + Async.auto({ |
| 21 | + tables: (next) => { |
| 22 | + self.getAllTablesFromSourceDatabase(next) |
| 23 | + }, |
| 24 | + dropTables: ['tables', function(results, next) { |
| 25 | + const tables = results.tables; |
| 26 | + const dropTablesSql = self.getDropTablesSql(tables); |
| 27 | + |
| 28 | + self._mysqlService.executeGenericQuery(dropTablesSql) |
| 29 | + .then(() => next()); |
| 30 | + }], |
| 31 | + createTables: ['dropTables', function(results, next) { |
| 32 | + const tables = results.tables; |
| 33 | + self.getCreateTablesSql(tables, (err, sql) => { |
| 34 | + self._mysqlService.executeGenericQuery(sql) |
| 35 | + .then(() => next()); |
| 36 | + }) |
| 37 | + }] |
| 38 | + }, (err, results) => { |
| 39 | + done(); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @returns {Promise.<String[]>} - an array of table names |
| 45 | + */ |
| 46 | + getAllTablesFromSourceDatabase(done) { |
| 47 | + const allTablesQuery = `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '${this._schemaSourceDatabase}';`; |
| 48 | + return this._mysqlService.selectAll(allTablesQuery) |
| 49 | + .then(allTables => { |
| 50 | + done(null, allTables.map(tableRow => tableRow.TABLE_NAME)) |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param tables |
| 56 | + * @returns {string} |
| 57 | + */ |
| 58 | + getDropTablesSql(tables) { |
| 59 | + const dropTableQueries = ['SET foreign_key_checks = 0;']; |
| 60 | + tables.forEach(table => dropTableQueries.push(`DROP TABLE IF EXISTS ${table};`)); |
| 61 | + dropTableQueries.push('SET foreign_key_checks = 1;'); |
| 62 | + |
| 63 | + return dropTableQueries.join("\n"); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param {String[]} tables |
| 68 | + * @param {function(err, string)} done |
| 69 | + */ |
| 70 | + getCreateTablesSql(tables, done) { |
| 71 | + const createTablesQueries = ['SET foreign_key_checks = 0;']; |
| 72 | + const self = this; |
| 73 | + |
| 74 | + Async.eachSeries(tables, (table, eachNext) => { |
| 75 | + self.getCreateTableSql(table, (err, sql) => { |
| 76 | + createTablesQueries.push(sql); |
| 77 | + eachNext(); |
| 78 | + }) |
| 79 | + }, () => { |
| 80 | + createTablesQueries.push('SET foreign_key_checks = 1;'); |
| 81 | + done(null, createTablesQueries.join("\n")); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + getCreateTableSql(table, done) { |
| 86 | + this.getCreateTableStatement(table, (err, sql) => { |
| 87 | + done(null, sql); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + getCreateTableStatement(table, done) { |
| 92 | + const getCreateTableSql = `SHOW CREATE TABLE ${this._schemaSourceDatabase}.${table};`; |
| 93 | + this._mysqlService.executeGenericQuery(getCreateTableSql) |
| 94 | + .then(createTableResult => done(null, createTableResult[0]['Create Table'] + ';')) |
| 95 | + ; |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +module.exports = MysqlDatabaseSetupDao; |
0 commit comments