From 778057bce859e8427182b5ff49fb65da677f5e97 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Tue, 22 Oct 2024 09:21:36 +0700 Subject: [PATCH] override mysql rename table --- src/connections/index.ts | 2 +- src/connections/mongodb.ts | 6 +++--- src/connections/mysql.ts | 14 ++++++++++++++ src/connections/sql-base.ts | 12 +++++------- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/connections/index.ts b/src/connections/index.ts index d9b593a..4e76ee1 100644 --- a/src/connections/index.ts +++ b/src/connections/index.ts @@ -36,7 +36,7 @@ export abstract class Connection { // Retrieve metadata about the database, useful for introspection. abstract fetchDatabaseSchema(): Promise; abstract raw(query: string): Promise; - abstract testConnection(): Promise; + abstract testConnection(): Promise<{ error?: string }>; // Connection common operations that will be used by Outerbase abstract insert( diff --git a/src/connections/mongodb.ts b/src/connections/mongodb.ts index b8914ae..08a97d4 100644 --- a/src/connections/mongodb.ts +++ b/src/connections/mongodb.ts @@ -88,13 +88,13 @@ export class MongoDBConnection implements Connection { throw new Error('Method not implemented.'); } - async testConnection(): Promise { + async testConnection(): Promise<{ error?: string }> { try { await this.connect(); await this.disconnect(); - return true; + return {}; } catch { - return false; + return { error: 'Failed to connect to MongoDB' }; } } diff --git a/src/connections/mysql.ts b/src/connections/mysql.ts index 542b296..5088d11 100644 --- a/src/connections/mysql.ts +++ b/src/connections/mysql.ts @@ -290,6 +290,20 @@ export class MySQLConnection extends SqlConnection { }); } + async renameTable( + schemaName: string | undefined, + tableName: string, + newTableName: string + ): Promise { + // If we don't put the schema name when rename the table, + // it might change the schema of the table after rename. + return super.renameTable( + schemaName, + tableName, + schemaName ? `${schemaName}.${newTableName}` : newTableName + ); + } + async renameColumn( schemaName: string | undefined, tableName: string, diff --git a/src/connections/sql-base.ts b/src/connections/sql-base.ts index 693546b..38527bd 100644 --- a/src/connections/sql-base.ts +++ b/src/connections/sql-base.ts @@ -199,9 +199,7 @@ export abstract class SqlConnection extends Connection { .alterTable( schemaName ? `${schemaName}.${tableName}` : tableName ) - .renameTable( - schemaName ? `${schemaName}.${newTableName}` : newTableName - ) + .renameTable(newTableName) .toQuery() ); } @@ -259,14 +257,14 @@ export abstract class SqlConnection extends Connection { ); } - async testConnection(): Promise { + async testConnection(): Promise<{ error?: string }> { try { await this.connect(); - const { error } = await this.raw('SELECT 1;'); + const { error, data } = await this.raw('SELECT 1;'); await this.disconnect(); - return !error; + return { error: error ? error.message : undefined }; } catch (error) { - return false; + return { error: 'Unexpected error' }; } } }