Skip to content

Commit

Permalink
override mysql rename table
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Oct 22, 2024
1 parent 978360c commit 778057b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/connections/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export abstract class Connection {
// Retrieve metadata about the database, useful for introspection.
abstract fetchDatabaseSchema(): Promise<Database>;
abstract raw(query: string): Promise<QueryResult>;
abstract testConnection(): Promise<boolean>;
abstract testConnection(): Promise<{ error?: string }>;

// Connection common operations that will be used by Outerbase
abstract insert(
Expand Down
6 changes: 3 additions & 3 deletions src/connections/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ export class MongoDBConnection implements Connection {
throw new Error('Method not implemented.');
}

async testConnection(): Promise<boolean> {
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' };
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/connections/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,20 @@ export class MySQLConnection extends SqlConnection {
});
}

async renameTable(
schemaName: string | undefined,
tableName: string,
newTableName: string
): Promise<QueryResult> {
// 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,
Expand Down
12 changes: 5 additions & 7 deletions src/connections/sql-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,7 @@ export abstract class SqlConnection extends Connection {
.alterTable(
schemaName ? `${schemaName}.${tableName}` : tableName
)
.renameTable(
schemaName ? `${schemaName}.${newTableName}` : newTableName
)
.renameTable(newTableName)
.toQuery()
);
}
Expand Down Expand Up @@ -259,14 +257,14 @@ export abstract class SqlConnection extends Connection {
);
}

async testConnection(): Promise<boolean> {
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' };
}
}
}

0 comments on commit 778057b

Please sign in to comment.