Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/track-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Usage:

Options:
--clean Remove all objects before execution.
-c --client <client> Kind of Database Client [sqlite, postgres; default sqlite]
-c --client <client> Kind of Database Client [sqlite, postgres, mysql; default sqlite]
Copy link
Contributor Author

@Syogo-Suganoya Syogo-Suganoya Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debugコマンドのUSAGEのclientオプションにmysqlを追加しました。

-r --result <result> Which results will be displayed [last, full; default last]
-l --limit <limit> Maximum row count that will be displayed [default 10]
`;
Expand Down
24 changes: 15 additions & 9 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,11 @@ class MySQLCassette {
if (options && options.clean) {
try {
let tables = await conn.raw("SHOW TABLES");
await conn.raw("SET foreign_key_checks = 0");
for (let table of tables[0]) {
await conn.raw(`DROP TABLE IF EXISTS ${table.Tables_in_track} CASCADE`);
await conn.raw(`DROP TABLE IF EXISTS ${table.Tables_in_track}`);
}
await conn.raw("SET foreign_key_checks = 1");
Comment on lines +238 to +242
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テーブルを削除する際に、外部キーをオフにしてから、削除するようにしました。
MySQLにCASCADEがないためです。

} catch (e2) { console.error(e2); }
}
return conn;
Expand Down Expand Up @@ -267,21 +269,22 @@ class MySQLCassette {
`;
}
updateAutoIncrementSql(table, column) {
return `ALTER TABLE ${table} AUTO_INCREMENT = ? + 1`;
return `ALTER TABLE ${table} AUTO_INCREMENT = ?`;
Comment on lines -270 to +272
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MySQLではプレースホルダを使った計算式はエラーが出るようなので、+1を削除しました。
updateAutoIncrement の方で、MySQLCassetteの場合のみ、count+1するようにしました。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既存のSQLiteチャレンジでも、影響なく正常に動作することを確認です。

}
lastValueSql(table, idCol) {
return `SELECT * FROM ${table} WHERE ${idCol} = LAST_INSERT_ID()`;
}
async listFk(table, conn) {
return await conn.query(`
SELECT
kcu.column_name AS column,
ccu.table_name AS foreign_table,
ccu.column_name AS foreign_column
FROM information_schema.table_constraints AS tc
INNER JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
INNER JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = '${table}'
COLUMN_NAME AS \`column\`,
REFERENCED_TABLE_NAME AS foreign_table,
REFERENCED_COLUMN_NAME AS foreign_column
FROM information_schema.KEY_COLUMN_USAGE
WHERE
TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = '${table}'
AND REFERENCED_TABLE_NAME IS NOT NULL
Comment on lines -278 to +287
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listFkのSQL文をMySQLのものに修正しました。

`);
}
errorOf(e) {
Expand Down Expand Up @@ -591,6 +594,9 @@ class Connection {

async updateAutoIncrement(table, column, count) {
const sql = this._cassette.updateAutoIncrementSql(table, column);
if (this._cassette instanceof MySQLCassette) {
count = (count || 0) + 1;
}
return await this.query(sql, [count]);
}

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "track-db-test-library",
"version": "2.6.0-rc7",
"version": "2.6.0-rc8",
"description": "Test utility for Track database challenges",
"main": "index.js",
"scripts": {
Expand Down
Loading