Skip to content

Commit 14c7fd2

Browse files
authored
Merge pull request #58 from givery-technology/fix/#53
MySQL修正
2 parents 5185c3d + 22ea4ce commit 14c7fd2

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

bin/track-db.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Usage:
2727
2828
Options:
2929
--clean Remove all objects before execution.
30-
-c --client <client> Kind of Database Client [sqlite, postgres; default sqlite]
30+
-c --client <client> Kind of Database Client [sqlite, postgres, mysql; default sqlite]
3131
-r --result <result> Which results will be displayed [last, full; default last]
3232
-l --limit <limit> Maximum row count that will be displayed [default 10]
3333
`;

lib/connection.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,11 @@ class MySQLCassette {
235235
if (options && options.clean) {
236236
try {
237237
let tables = await conn.raw("SHOW TABLES");
238+
await conn.raw("SET foreign_key_checks = 0");
238239
for (let table of tables[0]) {
239-
await conn.raw(`DROP TABLE IF EXISTS ${table.Tables_in_track} CASCADE`);
240+
await conn.raw(`DROP TABLE IF EXISTS ${table.Tables_in_track}`);
240241
}
242+
await conn.raw("SET foreign_key_checks = 1");
241243
} catch (e2) { console.error(e2); }
242244
}
243245
return conn;
@@ -267,21 +269,22 @@ class MySQLCassette {
267269
`;
268270
}
269271
updateAutoIncrementSql(table, column) {
270-
return `ALTER TABLE ${table} AUTO_INCREMENT = ? + 1`;
272+
return `ALTER TABLE ${table} AUTO_INCREMENT = ?`;
271273
}
272274
lastValueSql(table, idCol) {
273275
return `SELECT * FROM ${table} WHERE ${idCol} = LAST_INSERT_ID()`;
274276
}
275277
async listFk(table, conn) {
276278
return await conn.query(`
277279
SELECT
278-
kcu.column_name AS column,
279-
ccu.table_name AS foreign_table,
280-
ccu.column_name AS foreign_column
281-
FROM information_schema.table_constraints AS tc
282-
INNER JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
283-
INNER JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
284-
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = '${table}'
280+
COLUMN_NAME AS \`column\`,
281+
REFERENCED_TABLE_NAME AS foreign_table,
282+
REFERENCED_COLUMN_NAME AS foreign_column
283+
FROM information_schema.KEY_COLUMN_USAGE
284+
WHERE
285+
TABLE_SCHEMA = DATABASE()
286+
AND TABLE_NAME = '${table}'
287+
AND REFERENCED_TABLE_NAME IS NOT NULL
285288
`);
286289
}
287290
errorOf(e) {
@@ -591,6 +594,9 @@ class Connection {
591594

592595
async updateAutoIncrement(table, column, count) {
593596
const sql = this._cassette.updateAutoIncrementSql(table, column);
597+
if (this._cassette instanceof MySQLCassette) {
598+
count = (count || 0) + 1;
599+
}
594600
return await this.query(sql, [count]);
595601
}
596602

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-rc7",
3+
"version": "2.6.0-rc8",
44
"description": "Test utility for Track database challenges",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)