Skip to content

Commit eea40ef

Browse files
author
MIYASHITA, Akihiro
committed
fix
1 parent 7ed0c2e commit eea40ef

File tree

3 files changed

+16
-52
lines changed

3 files changed

+16
-52
lines changed

lib/connection.js

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class SQLiteCasette {
7878
useNullAsDefault: true
7979
});
8080
}
81+
rows(result) {
82+
return result;
83+
}
8184
explainSql(s) {
8285
return `EXPLAIN QUERY PLAN ${s}`;
8386
}
@@ -223,6 +226,9 @@ class PostgreSQLCasette {
223226
}
224227
throw Error("Failed to start PostgreSQL server");
225228
}
229+
rows(result) {
230+
return result.rows;
231+
}
226232
explainSql(s) {
227233
return `EXPLAIN ${s}`;
228234
}
@@ -264,49 +270,9 @@ class PostgreSQLCasette {
264270

265271
class MySQLCassette {
266272
async newConnection(options) {
267-
async function startServer() {
268-
return new Promise((_res, _rej) => {
269-
let done = false;
270-
const res = (...args) => {
271-
if (!done) {
272-
done = true;
273-
_res(...args);
274-
}
275-
}
276-
const rej = (...args) => {
277-
if (!done) {
278-
done = true;
279-
_rej(...args);
280-
}
281-
}
282-
sleep(10000).then(() => rej(new Error('Timeout database connection')));
283-
const p = cp.spawn('service', ['mysql', 'start'], {
284-
detached: true,
285-
});
286-
p.stdout.on('data', data => {
287-
const message = data.toString();
288-
if (options.verbose) {
289-
console.log(message);
290-
}
291-
if (message.includes('database system is ready to accept connections')) {
292-
res();
293-
}
294-
});
295-
p.stderr.on('data', data => {
296-
const message = data.toString();
297-
if (options.verbose) {
298-
console.error(message);
299-
}
300-
if (message.includes('database system is ready to accept connections')) {
301-
res();
302-
}
303-
});
304-
});
305-
}
306-
307273
for (let retries = 10; retries > 0; retries--) {
308274
try {
309-
await startServer();
275+
exec('service mysql start');
310276
const conn = knex({
311277
client: 'mysql',
312278
connection: {
@@ -321,7 +287,7 @@ class MySQLCassette {
321287
if (options && options.clean) {
322288
try {
323289
let tables = await conn.raw("SHOW TABLES");
324-
for (let table of tables.rows) {
290+
for (let table of tables[0]) {
325291
await conn.raw(`DROP TABLE IF EXISTS ${table.Tables_in_track} CASCADE`);
326292
}
327293
} catch (e2) { console.error(e2); }
@@ -333,6 +299,9 @@ class MySQLCassette {
333299
}
334300
throw Error("Failed to start MySQL server");
335301
}
302+
rows(result) {
303+
return result[0];
304+
}
336305
explainSql(s) {
337306
return `EXPLAIN ${s}`;
338307
}
@@ -534,24 +503,19 @@ class Connection {
534503
* @returns {Promise<Array<object>?>}
535504
*/
536505
async query(sql, opt_args) {
537-
538-
function rows(records) {
539-
return !!records.rows ? records.rows /* postgres */ : records /* sqlite */
540-
}
541-
542506
if (sql.trim().length === 0) {
543507
throw 'Empty query';
544508
}
545509

546510
const isSelectStatement = /^\s*SELECT /i.test(sql);
547511
if (isSelectStatement) {
548-
const count = rows(await this._conn.raw(`SELECT count(1) AS count FROM (${sql.replace(/;\s*$/, "")}) AS x`, opt_args)).length;
512+
const count = this._cassette.rows(await this._conn.raw(`SELECT count(1) AS count FROM (${sql.replace(/;\s*$/, "")}) AS x`, opt_args)).length;
549513
if (count > (this._options.maxRows || 10000)) {
550514
throw 'Too many records';
551515
}
552516
}
553517

554-
return rows(await this._conn.raw(sql, opt_args));
518+
return this._cassette.rows(await this._conn.raw(sql, opt_args));
555519
}
556520

557521
/**

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "track-db-test-library",
3-
"version": "2.6.0-rc2",
3+
"version": "2.6.0-rc3",
44
"description": "Test utility for Track database challenges",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)