Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a function to remove a table #67

Merged
merged 3 commits into from
Nov 18, 2024
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
8 changes: 7 additions & 1 deletion src/tables/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ export default class Table {
this.name = name;
this.integration = integration;
}

/**
* Removes this table from its integration.
* @throws {MindsDbError} - Something went wrong removing this table.
*/
async removeTable(): Promise<void> {
await this.tablesApiClient.removeTable(this.name, this.integration);
}
/**
* Deletes this table from its integration.
* @throws {MindsDbError} - Something went wrong deleting this table.
Expand Down
10 changes: 10 additions & 0 deletions src/tables/tablesApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ export default abstract class TablesApiClient {
*/
abstract deleteTable(name: string, integration: string): Promise<void>;
}

/**
* Removes a table from its integration.
* @param {string} name - Name of the table to be removed.
* @param {string} integration - Name of the integration the table belongs to.
* @returns {Promise<void>} - Resolves when the table is successfully removed.
* @throws {MindsDbError} - Something went wrong removing the table.
*/
abstract removeTable(name: string, integration: string): Promise<void>;

24 changes: 24 additions & 0 deletions src/tables/tablesRestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,27 @@ export default class TablesRestApiClient extends TablesApiClient {
}
}
}

/**
* Removes a table from its integration.
* @param {string} name - Name of the table to be removed.
* @param {string} integration - Name of the integration the table belongs to.
* @returns {Promise<void>} - Resolves when the table is successfully removed.
* @throws {MindsDbError} - Something went wrong removing the table.
*/
override async removeTable(
name: string,
integration: string
): Promise<void> {
// Construct the SQL query to drop the table
const sqlQuery = `DROP TABLE ${mysql.escapeId(integration)}.${mysql.escapeId(name)}`;

// Execute the SQL query using the sqlClient
const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery);

// Check for errors
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}