Skip to content

Commit

Permalink
feat: extension hook for DB delete (#24191)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida authored May 25, 2023
1 parent f8ec5f2 commit 9df8d8d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,12 @@ export interface DatabaseConnectionExtension {
enabled: () => boolean;

/**
* Callback for onsave
* Callbacks
*/
// TODO: we need to move the db types to superset-ui/core in order to import them correctly
onSave: (componentState: any, db: any) => any;

/**
* Used for parent to store data
*/
onEdit?: (componentState: any) => void;
onDelete?: (db: any) => void;
}

export type Extensions = Partial<{
Expand All @@ -124,6 +121,7 @@ export type Extensions = Partial<{
'welcome.main.replacement': React.ComponentType;
'ssh_tunnel.form.switch': React.ComponentType<SwitchProps>;
'databaseconnection.extraOption': DatabaseConnectionExtension;
/* Custom components to show in the database and dataset delete modals */
'database.delete.related': React.ComponentType<DatabaseDeleteRelatedExtensionProps>;
'dataset.delete.related': React.ComponentType<DatasetDeleteRelatedExtensionProps>;
}>;
Expand Down
11 changes: 10 additions & 1 deletion superset-frontend/src/pages/DatabaseList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const extensionsRegistry = getExtensionsRegistry();
const DatabaseDeleteRelatedExtension = extensionsRegistry.get(
'database.delete.related',
);
const dbConfigExtraExtension = extensionsRegistry.get(
'databaseconnection.extraOption',
);

const PAGE_SIZE = 25;

Expand Down Expand Up @@ -160,14 +163,20 @@ function DatabaseList({ addDangerToast, addSuccessToast }: DatabaseListProps) {
),
);

function handleDatabaseDelete({ id, database_name: dbName }: DatabaseObject) {
function handleDatabaseDelete(database: DatabaseObject) {
const { id, database_name: dbName } = database;
SupersetClient.delete({
endpoint: `/api/v1/database/${id}`,
}).then(
() => {
refreshData();
addSuccessToast(t('Deleted: %s', dbName));

// Remove any extension-related data
if (dbConfigExtraExtension?.onDelete) {
dbConfigExtraExtension.onDelete(database);
}

// Delete user-selected db from local storage
setItem(LocalStorageKeys.db, null);

Expand Down

0 comments on commit 9df8d8d

Please sign in to comment.