1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 | 1×
1×
5×
5×
5×
1×
1×
1×
1×
3×
3×
3×
3×
3×
3×
1×
1×
2×
2×
2×
3×
3×
3×
1×
7×
7×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
3×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
14×
14×
2×
14×
14×
1×
2×
2×
2×
2×
2×
2×
1×
14×
14×
14×
14×
14×
14×
1×
2×
1×
| var DatabaseManager = require('./DatabaseManager').default
, classUtils = require('./class-utils')
, mysql = require('mysql')
, Promise = require('bluebird')
, _ = require('lodash');
/**
* @constructor
* @extends DatabaseManager
*
* Notes:
* - Even though the method signature implicates that _masterConnectionUrl returns
* an URL string, it actually returns an object because MySQL node lib
* assumes that the database name is defined in the URL format.
*
*/
function MySqlDatabaseManager() {
DatabaseManager.apply(this, arguments);
this._masterClient = null;
this._cachedTableNames = null;
}
classUtils.inherits(MySqlDatabaseManager, DatabaseManager);
/**
* @Override
*/
MySqlDatabaseManager.prototype.createDbOwnerIfNotExist = function() {
return this._masterQuery("CREATE USER IF NOT EXISTS ?@'%' IDENTIFIED BY ?", [this.config.knex.connection.user, this.config.knex.connection.password])
};
/**
* @Override
*/
MySqlDatabaseManager.prototype.createDb = function(databaseName) {
databaseName = databaseName || this.config.knex.connection.database;
var collate = this.config.dbManager.collate;
var owner = this.config.knex.connection.user;
var self = this;
var promise = Promise.reject();
if (_.isEmpty(collate)) {
promise = promise.catch(function () {
return self._masterQuery("CREATE DATABASE ?? DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci", [databaseName]);
});
} else {
// Try to create with each collate. Use the first one that works. This is kind of a hack
// but seems to be the only reliable way to make this work with both windows and unix.
_.each(collate, function(locale) {
promise = promise.catch(function() {
return self._masterQuery("CREATE DATABASE ?? DEFAULT CHARACTER SET utf8 DEFAULT COLLATE ?", [databaseName, locale]);
});
});
}
promise = promise.then(function () {
return self._masterQuery('GRANT ALL PRIVILEGES ON ??.* TO ??', [databaseName, owner]);
});
return promise;
};
/**
* Drops database with name if db exists.
*
* @Override
*/
MySqlDatabaseManager.prototype.dropDb = function(databaseName) {
databaseName = databaseName || this.config.knex.connection.database;
return this._masterQuery("DROP DATABASE IF EXISTS ??", [databaseName]);
};
/**
* @Override
*/
MySqlDatabaseManager.prototype.truncateDb = function(ignoreTables) {
var knex = this.knexInstance();
var config = this.config;
Eif (!this._cachedTableNames) {
this._updateTableNameCache(knex, config);
}
return this._cachedTableNames.then(function (tableNames) {
Eif (!_.isEmpty(tableNames)) {
return knex.transaction(function(trx) {
return knex.raw('SET FOREIGN_KEY_CHECKS = 0').transacting(trx)
.then(function() {
// ignore the tables based on `ignoreTables`
var filteredTables = _.differenceWith(tableNames, ignoreTables, _.isEqual);
return Promise.map(filteredTables, function(tableName) {
return knex.table(tableName).truncate().transacting(trx);
}, {concurrency: 1});
});
});
}
});
};
/**
* @private
*/
MySqlDatabaseManager.prototype._updateTableNameCache = function(knex, config) {
this._cachedTableNames = knex('information_schema.tables')
.select('table_name')
.where('table_schema', config.knex.connection.database)
.then(function (tables) {
return _.without(_.map(tables, 'table_name'), config.knex.migrations.tableName);
});
};
/**
* @Override
*/
MySqlDatabaseManager.prototype.close = function() {
var disconnectAll = [this.closeKnex()];
Eif (this._masterClient) {
disconnectAll.push(this._masterClient.then(function(client) {
client.end();
}));
this._masterClient = null;
}
return Promise.all(disconnectAll);
};
/**
* @private
* @returns {Promise}
*/
MySqlDatabaseManager.prototype._masterQuery = function(query, params) {
var self = this;
if (!this._masterClient) {
this._masterClient = this.create_masterClient();
}
return this._masterClient.then(function(client) {
return self.perform_masterQuery(client, query, params);
});
};
/**
* @private
* @returns {Promise}
*/
MySqlDatabaseManager.prototype.create_masterClient = function() {
var self = this;
return new Promise(function(resolve, reject) {
var client = mysql.createConnection(self._masterConnectionUrl());
client.connect(function(err) {
Iif (err) {
reject(err);
} else {
resolve(client);
}
});
});
};
/**
* @private
* @returns {Promise}
*/
MySqlDatabaseManager.prototype.perform_masterQuery = function(client, query, params) {
return new Promise(function(resolve, reject) {
Eif (params) {
query = mysql.format(query, params);
}
client.query(query, function(err, result) {
Iif (err) {
reject(err);
} else {
resolve(result);
}
});
});
};
/**
* @private
* @returns {String}
*/
MySqlDatabaseManager.prototype._masterConnectionUrl = function() {
return {
host : this.config.knex.connection.host,
port : this.config.knex.connection.port || 3306,
user : this.config.dbManager.superUser,
password : this.config.dbManager.superPassword
};
};
module.exports = {
default: MySqlDatabaseManager,
MySqlDatabaseManager: MySqlDatabaseManager
}; |