From 453ba9cba85a835e61a68d92b155b829cf019855 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Thu, 15 Aug 2024 16:13:03 +0800 Subject: [PATCH] fix: quote table name in mysql query interface (#5065) * fix: quote table name when get mysql table define * chore: test --------- Co-authored-by: SuperGoo --- .../query-interface/query-interface.test.ts | 37 +++++++++++++++++++ .../query-interface/mysql-query-interface.ts | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/core/database/src/__tests__/query-interface/query-interface.test.ts b/packages/core/database/src/__tests__/query-interface/query-interface.test.ts index 85bf7d3546..887cf447a9 100644 --- a/packages/core/database/src/__tests__/query-interface/query-interface.test.ts +++ b/packages/core/database/src/__tests__/query-interface/query-interface.test.ts @@ -9,6 +9,43 @@ import { Database, mockDatabase } from '@nocobase/database'; +describe.runIf(process.env.DB_DIALECT === 'mysql')('mysql', async () => { + let db: Database; + + beforeEach(async () => { + db = mockDatabase({ + logging: console.log, + }); + + await db.clean({ drop: true }); + }); + + afterEach(async () => { + await db.close(); + }); + + it('should show table def table name has reserved word', async () => { + const User = db.collection({ + name: 'users', + tableName: `interval`, + fields: [ + { + type: 'string', + name: 'name', + }, + ], + }); + + await db.sync(); + + const tableDef = await db.queryInterface.showTableDefinition({ + tableName: User.options.tableName, + }); + + expect(tableDef).toBeDefined(); + }); +}); + describe('query interface', async () => { let db: Database; diff --git a/packages/core/database/src/query-interface/mysql-query-interface.ts b/packages/core/database/src/query-interface/mysql-query-interface.ts index c697ead01f..fff3afa528 100644 --- a/packages/core/database/src/query-interface/mysql-query-interface.ts +++ b/packages/core/database/src/query-interface/mysql-query-interface.ts @@ -91,7 +91,7 @@ export default class MysqlQueryInterface extends QueryInterface { async showTableDefinition(tableInfo: TableInfo): Promise { const { tableName } = tableInfo; - const sql = `SHOW CREATE TABLE ${tableName}`; + const sql = `SHOW CREATE TABLE ${this.db.utils.quoteTable(tableName)}`; const results = await this.db.sequelize.query(sql, { type: 'SELECT' });