Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Make workspaces deletable from any database state (GHY-3954) (#76350)

Eric Normanda87f3e0defa1
diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj
index d19b3473825..26aa37443a7 100644
--- a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj
+++ b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj
@@ -122,12 +122,29 @@
(present-workspace (api/check-404 (ws/get-workspace id))))
(api.macros/defendpoint :delete "/:id"
- :- [:map [:id ms/PositiveInt] [:deleted :boolean]]
- "Delete a Workspace. Deprovisions all databases first (blocking)."
- [{:keys [id]} :- [:map [:id ms/PositiveInt]]]
+ :- [:map
+ [:id ms/PositiveInt]
+ [:deleted :boolean]
+ [:message {:optional true} :string]
+ [:orphaned_resources {:optional true}
+ [:sequential [:map
+ [:workspace_database_id ms/PositiveInt]
+ [:database_id ms/PositiveInt]
+ [:driver :keyword]
+ [:schema :string]
+ [:user :string]
+ [:reason {:optional true} [:maybe :string]]]]]]
+ "Delete a Workspace. Tears down each `:provisioned` database's warehouse isolation
+ first (blocking). Refuses with a 409 if any database is still `:provisioning`/
+ `:deprovisioning` unless `ignore_pending=true`, in which case those databases are
+ left in the warehouse and only their app-DB rows are removed. If the warehouse was
+ unreachable for some `:provisioned` databases, the response includes
+ `:orphaned_resources` and a `:message` describing the inert schema/user objects
+ left behind for manual cleanup."
+ [{:keys [id]} :- [:map [:id ms/PositiveInt]]
+ {ignore-pending? :ignore-pending} :- [:map [:ignore-pending {:default false} [:maybe ms/BooleanValue]]]]
(api/write-check :model/Workspace id)
- (ws/delete-workspace! id)
- {:id id :deleted true})
+ (assoc (ws/delete-workspace! id (boolean ignore-pending?)) :id id))
;;; ------------------------------------------- Config download --------------------------------------------------
diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/core.clj b/enterprise/backend/src/metabase_enterprise/workspaces/core.clj
index 9a9b8753afa..e5d6db945b8 100644
--- a/enterprise/backend/src/metabase_enterprise/workspaces/core.clj
+++ b/enterprise/backend/src/metabase_enterprise/workspaces/core.clj
@@ -31,6 +31,7 @@
│ ▼
└──────────────────────────────────────── provisioned"
(:require
+ [clojure.string :as str]
[metabase-enterprise.workspaces.models.workspace :as workspace]
[metabase-enterprise.workspaces.models.workspace-database :as workspace-database]
[metabase-enterprise.workspaces.provisioning :as provisioning]
@@ -38,6 +39,7 @@
[metabase.driver.sql :as driver.sql]
[metabase.premium-features.core :refer [defenterprise]]
[metabase.settings.core :as setting]
+ [metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.workspaces.core :as ws]
[toucan2.core :as t2]))
@@ -222,10 +224,59 @@
(provisioning/provision-single! wsd-id))
(workspace/get-workspace (:id ws))))))
+(defn- orphaned-resources-message
+ [workspace-id failures]
+ (str (format "Workspace %d was deleted, but warehouse cleanup failed for %d database(s). "
+ workspace-id (count failures))
+ "These resources were left in place and may need to be removed manually:\n"
+ (str/join "\n"
+ (for [{:keys [database_id driver schema user reason]} failures]
+ (format " - database %d (%s): schema \"%s\", user \"%s\" — not removed because: %s"
+ database_id (name driver) schema user reason)))))
+
+(defn- pending-database?
+ "True for a WorkspaceDatabase that is mid-provision/deprovision — a state we can't
+ safely tear down because warehouse work may still be in flight on another node."
+ [wsd]
+ (contains? #{:provisioning :deprovisioning} (:status wsd)))
+
(defn delete-workspace!
- "Deprovision all databases (blocking), then delete the workspace."
- [id]
- (let [ws (assert-workspace-exists id)]
- (when (some #(= :provisioned (:status %)) (:databases ws))
- (provisioning/deprovision-workspace! id))
- (workspace/delete-workspace! id)))
+ "Tear down each `:provisioned` database's warehouse isolation (best-effort,
+ blocking), then delete the workspace.
+
+ `ignore-pending?` (default false) controls databases still `:provisioning` or
+ `:deprovisioning`: when false, refuses with a 409 (`:pending_databases` lists
+ them) since their warehouse work may be in flight; when true, those databases are
+ left untouched in the warehouse and only their app-DB rows are removed.
+
+ Returns `{:deleted true}` on clean teardown, or
+ `{:deleted true :orphaned_resources [...] :message ..}` when the warehouse was
+ unreachable for some `:provisioned` rows and inert schema/user objects were left
+ behind. App-DB `TableRemapping` rows are always cleared, so query routing is never
+ left dangling."
+ ([id] (delete-workspace! id false))
+ ([id ignore-pending?]
+ (let [ws (assert-workspace-exists id)
+ databases (:databases ws)
+ pending (filter pending-database? databases)
+ active (filter #(= :provisioned (:status %)) databases)]
+ (when (and (seq pending) (not ignore-pending?))
+ (throw (ex-info "Cannot delete a workspace while some of its databases are still provisioning or deprovisioning"
+ {:status-code 409
+ :workspace_id id
+ :pending_databases (mapv (fn [wsd]
+ {:database_id (:database_id wsd)
+ :name (:name (:database wsd))
+ :status (:status wsd)})
+ pending)})))
+ (let [results (mapv #(provisioning/force-teardown-for-delete!
+ % provisioning/dispatching-provisioner)
+ active)
+ failures (filterv #(= :failure (:status %)) results)]
+ (run! provisioning/ignore-pending-database! pending)
+ (workspace/delete-workspace! id)
+ (if (seq failures)
+ (let [message (orphaned-resources-message id failures)]
+ (log/warn message)
+ {:deleted true :orphaned_resources failures :message message})
+ {:deleted true})))))
diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj
index dbef266436d..a8fcb6a0b2d 100644
--- a/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj
+++ b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj
@@ -207,6 +207,74 @@
(log/warnf t "Failed to provision WorkspaceDatabase %s" id)))))
triggered)))
+(defn force-teardown-for-delete!
+ "Best-effort warehouse teardown for a single WorkspaceDatabase, used only by the
+ delete path. Drops the iso schema + user (idempotent), always clears the row's
+ app-DB `TableRemapping` rows, then forces the row to `:unprovisioned` regardless
+ of the warehouse outcome.
+
+ Works from ANY non-`:unprovisioned` state: the iso schema and user names are
+ deterministic from the row id (see [[metabase.driver.util/workspace-isolation-namespace-name]]),
+ so the destroy target is reconstructable even for a row stuck in `:provisioning`
+ that never stored its `:output_namespace`/`:database_details`.
+
+ Returns `{:status :success}` when the warehouse objects were dropped, or
+ `{:status :failure :workspace_database_id .. :database_id .. :driver .. :schema ..
+ :user .. :reason ..}` when the warehouse was unreachable and inert objects were
+ left behind for manual cleanup."
+ [wsd provisioner]
+ (with-workspace-database-lock (:id wsd)
+ (let [db (t2/select-one :model/Database :id (:database_id wsd))
+ driver (driver.u/database->driver db)
+ iso-ws {:id (:id wsd) :name (str "wsd-" (:id wsd))}
+ schema (or (not-empty (:output_namespace wsd))
+ (driver.u/workspace-isolation-namespace-name iso-ws))
+ user (or (get-in wsd [:database_details :user])
+ (driver.u/workspace-isolation-user-name iso-ws))
+ ;; prefer the stored details verbatim (some drivers stash more than :user
+ ;; there); synthesize a minimal map only for a crashed :provisioning row
+ ;; that never recorded anything.
+ details (if (seq (:database_details wsd))
+ (:database_details wsd)
+ {:user user})
+ workspace (assoc iso-ws :schema schema :database_details details)
+ result (try
+ (destroy! provisioner driver db workspace)
+ {:status :success}
+ (catch Throwable t
+ (log/warnf t (str "Workspace database %d: warehouse cleanup failed; leaving schema \"%s\" "
+ "and user \"%s\" on database %d for manual removal")
+ (:id wsd) schema user (:database_id wsd))
+ {:status :failure
+ :workspace_database_id (:id wsd)
+ :database_id (:database_id wsd)
+ :driver driver
+ :schema schema
+ :user user
+ :reason (ex-message t)}))]
+ ;; App-DB cleanup needs no warehouse connection, so it ALWAYS runs — stale
+ ;; remappings would otherwise rewrite queries to a dropped schema and 500 the
+ ;; QP. Fresh autocommit connection to survive any surrounding tx rollback
+ ;; (mirrors `deprovision-workspace-database!`).
+ (binding [t2.connection/*current-connectable* nil]
+ (ws.remapping-cleanup/clear-mappings-for-iso! db (:database_id wsd) schema))
+ (t2/update! :model/WorkspaceDatabase {:id (:id wsd)}
+ {:output_namespace "" :database_details {} :status :unprovisioned})
+ result)))
+
+(defn ignore-pending-database!
+ "Delete-path helper for a WorkspaceDatabase the admin chose to delete while it is
+ still `:provisioning`/`:deprovisioning`. Does NOT touch the warehouse — the row may
+ be in flight on another node — it only clears the row's app-DB `TableRemapping`
+ rows and forces it to `:unprovisioned` so it can be cascade-deleted. Any warehouse
+ schema/user is intentionally left in place."
+ [wsd]
+ (with-workspace-database-lock (:id wsd)
+ (let [db (t2/select-one :model/Database :id (:database_id wsd))]
+ (binding [t2.connection/*current-connectable* nil]
+ (ws.remapping-cleanup/clear-mappings-for-iso! db (:database_id wsd) (:output_namespace wsd)))
+ (t2/update! :model/WorkspaceDatabase {:id (:id wsd)} {:status :unprovisioned}))))
+
(defn deprovision-workspace!
"Flip every `:provisioned` WorkspaceDatabase under `workspace-id` to
`:deprovisioning`, then deprovision each one synchronously (blocking).
diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_manager_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_manager_test.clj
index d374ee0bd40..842fab8bdca 100644
--- a/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_manager_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_manager_test.clj
@@ -8,7 +8,8 @@
[metabase-enterprise.workspaces.provisioning :as provisioning]
[metabase.permissions.test-util :as perms.test-util]
[metabase.test :as mt]
- [metabase.test.fixtures :as fixtures]))
+ [metabase.test.fixtures :as fixtures]
+ [toucan2.core :as t2]))
(use-fixtures :once (fixtures/initialize :db))
@@ -50,6 +51,27 @@
(mt/user-http-request :crowberto :delete 200 (str "ee/workspace-manager/" ws-id))))
(mt/user-http-request :crowberto :get 404 (str "ee/workspace-manager/" ws-id)))))))))
+(deftest delete-workspace-pending-databases-test
+ (testing "DELETE refuses a workspace with a pending database unless ignore_pending=true"
+ (mt/with-temp [:model/Database {db-id :id} {:engine :postgres
+ :details {}
+ :settings {:database-enable-workspaces true}}]
+ (mt/with-model-cleanup [:model/Workspace]
+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]
+ (let [{ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"
+ {:name "Pending" :database_ids [db-id]})
+ wsd-id (t2/select-one-pk :model/WorkspaceDatabase :workspace_id ws-id)]
+ (t2/update! :model/WorkspaceDatabase {:id wsd-id} {:status :provisioning})
+ (testing "refused by default with a 409 listing the pending databases"
+ (is (=? {:pending_databases [{:database_id db-id :status "provisioning"}]}
+ (mt/user-http-request :crowberto :delete 409 (str "ee/workspace-manager/" ws-id))))
+ (mt/user-http-request :crowberto :get 200 (str "ee/workspace-manager/" ws-id)))
+ (testing "ignore-pending=true deletes anyway"
+ (is (=? {:id ws-id :deleted true}
+ (mt/user-http-request :crowberto :delete 200
+ (str "ee/workspace-manager/" ws-id "?ignore-pending=true"))))
+ (mt/user-http-request :crowberto :get 404 (str "ee/workspace-manager/" ws-id)))))))))
+
(deftest create-workspace-with-database-ids-test
(testing "POST / attaches and provisions the given databases (each must be eligible) with their schemas"
(mt/with-temp [:model/Database {eligible-id :id} {:engine :postgres
diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj
index 45016580f22..78883f1f155 100644
--- a/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj
@@ -122,9 +122,73 @@
(testing "delete workspace with no databases"
(mt/with-model-cleanup [:model/Workspace]
(let [ws (ws/create-workspace! {:name "Empty WS" :creator_id (mt/user->id :crowberto)})]
- (ws/delete-workspace! (:id ws))
+ (is (= {:deleted true} (ws/delete-workspace! (:id ws))))
(is (nil? (ws/get-workspace (:id ws))))))))
+(deftest delete-workspace-with-pending-status-test
+ (testing "GHY-3954: a workspace with a database still :provisioning/:deprovisioning"
+ (doseq [pending-status [:provisioning :deprovisioning]]
+ (testing (str "status " pending-status)
+ (mt/with-model-cleanup [:model/Workspace]
+ (let [ws (ws/create-workspace! {:name (str "Pending " (name pending-status))
+ :creator_id (mt/user->id :crowberto)})
+ _ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]
+ (add-database! (:id ws) (mt/id) ["PUBLIC"]))
+ wsd-id (t2/select-one-pk :model/WorkspaceDatabase :workspace_id (:id ws))]
+ ;; force the row into the pending state. :provisioning rows that crashed
+ ;; never stored their iso details, so clear them too.
+ (t2/update! :model/WorkspaceDatabase {:id wsd-id}
+ (cond-> {:status pending-status}
+ (= pending-status :provisioning)
+ (assoc :output_namespace "" :database_details {})))
+ (testing "is refused by default, listing the pending databases"
+ (is (thrown-with-msg?
+ ExceptionInfo #"still provisioning or deprovisioning"
+ (ws/delete-workspace! (:id ws))))
+ (is (=? {:status-code 409
+ :pending_databases [{:database_id (mt/id) :status pending-status}]}
+ (try (ws/delete-workspace! (:id ws))
+ (catch ExceptionInfo e (ex-data e)))))
+ (is (some? (ws/get-workspace (:id ws)))
+ "the workspace is left intact when the delete is refused"))
+ (testing "is deleted when ignore-pending? is true; the warehouse is left untouched"
+ ;; destroy! must NOT be called for a pending row — fail loudly if it is.
+ (let [exploding (reify provisioning/Provisioner
+ (init! [_ _ _ _] nil)
+ (grant! [_ _ _ _ _] nil)
+ (destroy! [_ _ _ _] (throw (ex-info "destroy! must not run for a pending row" {}))))]
+ (with-redefs [provisioning/dispatching-provisioner exploding]
+ (is (= {:deleted true} (ws/delete-workspace! (:id ws) true)))))
+ (is (nil? (ws/get-workspace (:id ws))))
+ (is (not (t2/exists? :model/WorkspaceDatabase :id wsd-id))))))))))
+
+(deftest delete-workspace-warehouse-unreachable-test
+ (testing "GHY-3954: delete still succeeds when warehouse teardown fails; the result"
+ (testing "names the orphaned schema/user and why they were left behind"
+ (mt/with-model-cleanup [:model/Workspace]
+ (let [ws (ws/create-workspace! {:name "Unreachable WS" :creator_id (mt/user->id :crowberto)})
+ _ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]
+ (add-database! (:id ws) (mt/id) ["PUBLIC"]))
+ wsd-id (t2/select-one-pk :model/WorkspaceDatabase :workspace_id (:id ws))
+ boom (reify provisioning/Provisioner
+ (init! [_ _ _ _] nil)
+ (grant! [_ _ _ _ _] nil)
+ (destroy! [_ _ _ _] (throw (ex-info "Connection refused" {}))))
+ result (with-redefs [provisioning/dispatching-provisioner boom]
+ (ws/delete-workspace! (:id ws)))]
+ (is (true? (:deleted result)))
+ (is (nil? (ws/get-workspace (:id ws)))
+ "the workspace is deleted even though warehouse cleanup failed")
+ (is (=? [{:status :failure
+ :workspace_database_id wsd-id
+ :database_id (mt/id)
+ :schema "mb_iso_stub"
+ :user "stub_user"
+ :reason "Connection refused"}]
+ (:orphaned_resources result)))
+ (is (re-find #"was deleted, but warehouse cleanup failed" (:message result)))
+ (is (re-find #"Connection refused" (:message result))))))))
+
;;; -------------------------------------------- Remappings ----------------------------------------------------
(deftest list-remappings-test
diff --git a/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts b/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts
index b88945463a6..586ebc805ca 100644
--- a/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts
+++ b/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts
@@ -1,5 +1,7 @@
import type {
CreateWorkspaceRequest,
+ DeleteWorkspaceRequest,
+ DeleteWorkspaceResponse,
UpdateWorkspaceRequest,
Workspace,
WorkspaceId,
@@ -22,6 +24,14 @@ export const workspaceManagerApi = EnterpriseApi.injectEndpoints({
}),
providesTags: (workspaces = []) => provideWorkspaceListTags(workspaces),
}),
+ getWorkspace: builder.query<Workspace, WorkspaceId>({
+ query: (id) => ({
+ method: "GET",
+ url: `/api/ee/workspace-manager/${id}`,
+ }),
+ providesTags: (workspace) =>
+ workspace ? [idTag("workspace", workspace.id)] : [],
+ }),
createWorkspace: builder.mutation<Workspace, CreateWorkspaceRequest>({
query: (body) => ({
method: "POST",
@@ -40,12 +50,16 @@ export const workspaceManagerApi = EnterpriseApi.injectEndpoints({
invalidatesTags: (_, error, { id }) =>
invalidateTags(error, [listTag("workspace"), idTag("workspace", id)]),
}),
- deleteWorkspace: builder.mutation<void, WorkspaceId>({
- query: (id) => ({
+ deleteWorkspace: builder.mutation<
+ DeleteWorkspaceResponse,
+ DeleteWorkspaceRequest
+ >({
+ query: ({ id, ignorePending }) => ({
method: "DELETE",
url: `/api/ee/workspace-manager/${id}`,
+ params: ignorePending ? { "ignore-pending": true } : undefined,
}),
- invalidatesTags: (_, error, id) =>
+ invalidatesTags: (_, error, { id }) =>
invalidateTags(error, [listTag("workspace"), idTag("workspace", id)]),
}),
}),
@@ -53,6 +67,7 @@ export const workspaceManagerApi = EnterpriseApi.injectEndpoints({
export const {
useListWorkspacesQuery,
+ useGetWorkspaceQuery,
useCreateWorkspaceMutation,
useUpdateWorkspaceMutation,
useDeleteWorkspaceMutation,
diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.tsx
index 510e7d6761b..a2e99ddfa58 100644
--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.tsx
@@ -1,14 +1,26 @@
import { t } from "ttag";
+import { useToast } from "metabase/common/hooks/use-toast";
import {
Form,
FormErrorMessage,
FormProvider,
FormSubmitButton,
} from "metabase/forms";
-import { Button, FocusTrap, Group, Modal, Stack, Text } from "metabase/ui";
-import { useDeleteWorkspaceMutation } from "metabase-enterprise/api";
-import type { Workspace } from "metabase-types/api";
+import {
+ Button,
+ FocusTrap,
+ Group,
+ List,
+ Modal,
+ Stack,
+ Text,
+} from "metabase/ui";
+import {
+ useDeleteWorkspaceMutation,
+ useGetWorkspaceQuery,
+} from "metabase-enterprise/api";
+import type { Workspace, WorkspaceDatabase } from "metabase-types/api";
export type DeleteWorkspaceModalProps = {
workspace: Workspace;
@@ -17,6 +29,11 @@ export type DeleteWorkspaceModalProps = {
onClose: () => void;
};
+function isPending(workspaceDatabase: WorkspaceDatabase) {
+ const { status } = workspaceDatabase;
+ return status === "provisioning" || status === "deprovisioning";
+}
+
export function DeleteWorkspaceModal({
workspace,
opened,
@@ -33,6 +50,7 @@ export function DeleteWorkspaceModal({
<FocusTrap.InitialFocus />
<DeleteWorkspaceForm
workspace={workspace}
+ opened={opened}
onDelete={onDelete}
onClose={onClose}
/>
@@ -42,19 +60,46 @@ export function DeleteWorkspaceModal({
type DeleteWorkspaceFormProps = {
workspace: Workspace;
+ opened: boolean;
onDelete: () => void;
onClose: () => void;
};
function DeleteWorkspaceForm({
workspace,
+ opened,
onDelete,
onClose,
}: DeleteWorkspaceFormProps) {
const [deleteWorkspace] = useDeleteWorkspaceMutation();
+ const [sendToast] = useToast();
+ // The list page omits databases, so fetch the hydrated workspace to learn which
+ // databases are still provisioning/deprovisioning. Skipped while the modal is closed.
+ const { data: hydratedWorkspace, isLoading } = useGetWorkspaceQuery(
+ workspace.id,
+ { skip: !opened },
+ );
+
+ const pendingDatabases = (hydratedWorkspace?.databases ?? []).filter(
+ isPending,
+ );
+ const hasPendingDatabases = pendingDatabases.length > 0;
const handleSubmit = async () => {
- await deleteWorkspace(workspace.id).unwrap();
+ const result = await deleteWorkspace({
+ id: workspace.id,
+ ignorePending: hasPendingDatabases,
+ }).unwrap();
+ // The workspace is deleted even when warehouse teardown partly fails; warn the
+ // admin so the leftover schema/user objects can be removed manually.
+ if (result.orphaned_resources?.length) {
+ sendToast({
+ message: result.message,
+ icon: "warning",
+ toastColor: "error",
+ timeout: null,
+ });
+ }
onDelete();
};
@@ -62,9 +107,25 @@ function DeleteWorkspaceForm({
<FormProvider initialValues={{}} onSubmit={handleSubmit}>
<Form>
<Stack gap="lg">
- <Text>
- {t`This will delete the workspace as well as the temporary database users and schemas that were created for this workspace. This can't be undone.`}
- </Text>
+ {hasPendingDatabases ? (
+ <Stack gap="sm">
+ <Text>
+ {t`Some of this workspace's databases are still being set up or torn down. Deleting now will remove the workspace, but their temporary database users and schemas will be left in place and must be removed manually:`}
+ </Text>
+ <List>
+ {pendingDatabases.map((workspaceDatabase) => (
+ <List.Item key={workspaceDatabase.database_id}>
+ {workspaceDatabase.database?.name ??
+ t`Database ${workspaceDatabase.database_id}`}
+ </List.Item>
+ ))}
+ </List>
+ </Stack>
+ ) : (
+ <Text>
+ {t`This will delete the workspace as well as the temporary database users and schemas that were created for this workspace. This can't be undone.`}
+ </Text>
+ )}
<FormErrorMessage />
<Group justify="flex-end">
<Button onClick={onClose}>{t`Cancel`}</Button>
@@ -72,6 +133,7 @@ function DeleteWorkspaceForm({
label={t`Delete workspace`}
variant="filled"
color="danger"
+ disabled={isLoading}
/>
</Group>
</Stack>
diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.unit.spec.tsx
index e75be30cadb..a2ebadf8ce2 100644
--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.unit.spec.tsx
@@ -4,61 +4,145 @@ import fetchMock from "fetch-mock";
import {
setupDeleteWorkspaceEndpoint,
setupDeleteWorkspaceEndpointError,
+ setupGetWorkspaceEndpoint,
} from "__support__/server-mocks";
import { renderWithProviders, screen, waitFor } from "__support__/ui";
-import { createMockWorkspace } from "metabase-types/api/mocks";
+import { UndoListing } from "metabase/common/components/UndoListing";
+import type { WorkspaceDatabase } from "metabase-types/api";
+import {
+ createMockDatabase,
+ createMockWorkspace,
+ createMockWorkspaceDatabase,
+} from "metabase-types/api/mocks";
import { DeleteWorkspaceModal } from "./DeleteWorkspaceModal";
-const WORKSPACE = createMockWorkspace({ id: 1, name: "My workspace" });
+const ORPHAN_MESSAGE =
+ 'Workspace 1 was deleted, but warehouse cleanup failed for 1 database(s). These resources were left in place and may need to be removed manually:\n - database 2 (postgres): schema "mb_iso_1", user "mb_iso_1" — not removed because: Connection refused';
+
+function setup({
+ withError = false,
+ withOrphans = false,
+ databases = [],
+}: {
+ withError?: boolean;
+ withOrphans?: boolean;
+ databases?: WorkspaceDatabase[];
+} = {}) {
+ const workspace = createMockWorkspace({
+ id: 1,
+ name: "My workspace",
+ databases,
+ });
+ setupGetWorkspaceEndpoint(workspace);
-function setup({ withError = false }: { withError?: boolean } = {}) {
if (withError) {
- setupDeleteWorkspaceEndpointError(WORKSPACE.id);
+ setupDeleteWorkspaceEndpointError(workspace.id);
+ } else if (withOrphans) {
+ setupDeleteWorkspaceEndpoint(workspace.id, {
+ message: ORPHAN_MESSAGE,
+ orphaned_resources: [
+ {
+ workspace_database_id: 1,
+ database_id: 2,
+ driver: "postgres",
+ schema: "mb_iso_1",
+ user: "mb_iso_1",
+ reason: "Connection refused",
+ },
+ ],
+ });
} else {
- setupDeleteWorkspaceEndpoint(WORKSPACE.id);
+ setupDeleteWorkspaceEndpoint(workspace.id);
}
const onDelete = jest.fn();
const onClose = jest.fn();
renderWithProviders(
- <DeleteWorkspaceModal
- workspace={WORKSPACE}
- opened
- onDelete={onDelete}
- onClose={onClose}
- />,
+ <>
+ <DeleteWorkspaceModal
+ workspace={workspace}
+ opened
+ onDelete={onDelete}
+ onClose={onClose}
+ />
+ <UndoListing />
+ </>,
);
- return { onDelete, onClose };
+ return { workspace, onDelete, onClose };
+}
+
+async function clickDelete() {
+ const button = await screen.findByRole("button", {
+ name: "Delete workspace",
+ });
+ await waitFor(() => expect(button).toBeEnabled());
+ await userEvent.click(button);
}
describe("DeleteWorkspaceModal", () => {
it("should delete the workspace and call the callback when the confirm button is clicked", async () => {
const { onDelete } = setup();
- await userEvent.click(
- screen.getByRole("button", { name: "Delete workspace" }),
- );
+ await clickDelete();
await waitFor(() => {
expect(
- fetchMock.callHistory.called(
- `path:/api/ee/workspace-manager/${WORKSPACE.id}`,
- { method: "DELETE" },
- ),
+ fetchMock.callHistory.called("path:/api/ee/workspace-manager/1", {
+ method: "DELETE",
+ }),
).toBe(true);
});
await waitFor(() => expect(onDelete).toHaveBeenCalled());
});
+ it("should list pending databases and delete with ignore-pending when confirmed", async () => {
+ const { onDelete } = setup({
+ databases: [
+ createMockWorkspaceDatabase({
+ database_id: 7,
+ status: "provisioning",
+ database: createMockDatabase({ id: 7, name: "Pending DB" }),
+ }),
+ ],
+ });
+
+ expect(
+ await screen.findByText(/still being set up or torn down/i),
+ ).toBeInTheDocument();
+ expect(screen.getByText("Pending DB")).toBeInTheDocument();
+
+ await clickDelete();
+
+ await waitFor(() => {
+ expect(
+ fetchMock.callHistory.called("path:/api/ee/workspace-manager/1", {
+ method: "DELETE",
+ query: { "ignore-pending": "true" },
+ }),
+ ).toBe(true);
+ });
+ await waitFor(() => expect(onDelete).toHaveBeenCalled());
+ });
+
+ it("should warn about orphaned warehouse resources but still delete when teardown partly fails", async () => {
+ const { onDelete } = setup({ withOrphans: true });
+
+ await clickDelete();
+
+ expect(
+ await screen.findByText(/warehouse cleanup failed/i),
+ ).toBeInTheDocument();
+ expect(screen.getByText(/Connection refused/i)).toBeInTheDocument();
+ await waitFor(() => expect(onDelete).toHaveBeenCalled());
+ });
+
it("should show an error message and not call the callback when the request fails", async () => {
const { onDelete } = setup({ withError: true });
- await userEvent.click(
- screen.getByRole("button", { name: "Delete workspace" }),
- );
+ await clickDelete();
expect(await screen.findByRole("alert")).toHaveTextContent(
"An error occurred",
diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.unit.spec.tsx
index 1637e777b92..e1a04b8955f 100644
--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.unit.spec.tsx
@@ -1,5 +1,6 @@
import userEvent from "@testing-library/user-event";
+import { setupGetWorkspaceEndpoint } from "__support__/server-mocks";
import { renderWithProviders, screen } from "__support__/ui";
import {
createMockDatabase,
@@ -13,6 +14,8 @@ import { WorkspaceItem } from "./WorkspaceItem";
const WORKSPACE = createMockWorkspace({ id: 1, name: "My workspace" });
function setup({ workspace = WORKSPACE } = {}) {
+ // The delete modal fetches the hydrated workspace when opened.
+ setupGetWorkspaceEndpoint(workspace);
renderWithProviders(<WorkspaceItem workspace={workspace} />);
}
diff --git a/frontend/src/metabase-types/api/workspace-manager.ts b/frontend/src/metabase-types/api/workspace-manager.ts
index 6e7f9d6bd2d..ae0a890246a 100644
--- a/frontend/src/metabase-types/api/workspace-manager.ts
+++ b/frontend/src/metabase-types/api/workspace-manager.ts
@@ -36,3 +36,29 @@ export type UpdateWorkspaceRequest = {
id: WorkspaceId;
name?: string;
};
+
+export type OrphanedWorkspaceResource = {
+ workspace_database_id: number;
+ database_id: DatabaseId;
+ driver: string;
+ schema: string;
+ user: string;
+ reason?: string | null;
+};
+
+export type DeleteWorkspaceRequest = {
+ id: WorkspaceId;
+ // When the workspace has databases still provisioning/deprovisioning, the
+ // backend refuses unless this is set — then it leaves those databases'
+ // warehouse resources in place and removes only the app-DB rows.
+ ignorePending?: boolean;
+};
+
+export type DeleteWorkspaceResponse = {
+ id: WorkspaceId;
+ deleted: boolean;
+ // Present only when the warehouse was unreachable during teardown: the workspace
+ // is still deleted, but these inert schema/user objects were left behind.
+ message?: string;
+ orphaned_resources?: OrphanedWorkspaceResource[];
+};
diff --git a/frontend/test/__support__/server-mocks/workspace-manager.ts b/frontend/test/__support__/server-mocks/workspace-manager.ts
index f1b4bdc631f..ca4629e907b 100644
--- a/frontend/test/__support__/server-mocks/workspace-manager.ts
+++ b/frontend/test/__support__/server-mocks/workspace-manager.ts
@@ -1,6 +1,10 @@
import fetchMock from "fetch-mock";
-import type { Workspace, WorkspaceId } from "metabase-types/api";
+import type {
+ DeleteWorkspaceResponse,
+ Workspace,
+ WorkspaceId,
+} from "metabase-types/api";
const BASE_URL = "path:/api/ee/workspace-manager";
@@ -8,6 +12,10 @@ export function setupListWorkspacesEndpoint(workspaces: Workspace[]) {
fetchMock.get(BASE_URL, workspaces);
}
+export function setupGetWorkspaceEndpoint(workspace: Workspace) {
+ fetchMock.get(`${BASE_URL}/${workspace.id}`, workspace);
+}
+
export function setupCreateWorkspaceEndpoint(workspace: Workspace) {
fetchMock.post(BASE_URL, workspace);
}
@@ -16,10 +24,14 @@ export function setupUpdateWorkspaceEndpoint(workspace: Workspace) {
fetchMock.put(`${BASE_URL}/${workspace.id}`, workspace);
}
-export function setupDeleteWorkspaceEndpoint(workspaceId: WorkspaceId) {
+export function setupDeleteWorkspaceEndpoint(
+ workspaceId: WorkspaceId,
+ response?: Partial<DeleteWorkspaceResponse>,
+) {
fetchMock.delete(`${BASE_URL}/${workspaceId}`, {
id: workspaceId,
deleted: true,
+ ...response,
});
}