The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Workspaces V2 (#73236)
Aleksandr Lesnenko0bf62315d346
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.ednindex 817d56836bf..96101c53801 100644--- a/.clj-kondo/config.edn+++ b/.clj-kondo/config.edn@@ -192,6 +192,10 @@metabase.lib-metric.ast.plan/validate-arithmetic-ast!metabase-enterprise.workspaces.isolation/ensure-database-isolation!metabase-enterprise.workspaces.test-util/create-ready-ws!+ metabase-enterprise.workspaces.core/clear-instance-workspace!+ metabase-enterprise.workspaces.core/set-instance-workspace!+ metabase.workspaces.core/clear-instance-workspace!+ metabase.workspaces.core/set-instance-workspace!;; Safe in tests, where we typically use a private, temporary index per thread.metabase.search.core/reindex!metabase.util/run-count!diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 4fe0cf58537..d1a37f28fdc 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -109,7 +109,8 @@searchsettingsutil- warehouses}+ warehouses+ workspaces}:model-exports #{:model/Action}:model-imports #{:model/Card:model/Collection@@ -849,7 +850,10 @@tracingutilwarehouse-schema- warehouses}+ warehouses+ ;; `metabase.driver.sync/include-schema?` skips workspace iso schemas+ ;; on parent (non-workspace-mode) instances. See GHY-3489.+ workspaces}:model-imports #{:model/Database :model/Field :model/Table}}driver-api@@ -1324,7 +1328,8 @@tasktask-historytracing- util}+ util+ workspaces}:model-exports #{:model/PersistedInfo}:model-imports #{:model/Card :model/Database}}@@ -1747,6 +1752,7 @@metabase.query-processor.interfacemetabase.query-processor.metadatametabase.query-processor.middleware.constraints+ metabase.query-processor.middleware.enterprisemetabase.query-processor.middleware.permissionsmetabase.query-processor.parameters.datesmetabase.query-processor.parameters.operators@@ -1792,7 +1798,8 @@systemtracingusers- util}+ util+ workspaces}:model-imports #{:model/Card:model/Dashboard:model/DashboardCard@@ -2148,7 +2155,8 @@tracingutilwarehouse-schema- warehouses}+ warehouses+ workspaces}:model-imports #{:model/Database:model/Field:model/FieldValues@@ -2207,7 +2215,8 @@permissionspremium-featuressearch- util}+ util+ workspaces}:model-imports #{:model/AiUsageLog:model/Card:model/Dashboard@@ -2315,9 +2324,11 @@eventsliblib-be+ premium-featuresquery-processorsync- util}+ util+ workspaces}:friends #{transformsenterprise/transforms-python}}@@ -2369,7 +2380,8 @@settingssyncutil- warehouse-schema}+ warehouse-schema+ workspaces}:model-imports #{:model/Card:model/Database:model/Field@@ -2603,6 +2615,13 @@:model/Field:model/Table}}+ workspaces+ {:team "Graphy"+ :api #{metabase.workspaces.core+ metabase.workspaces.table-remapping}+ :uses #{premium-features+ util}}+xrays{:team "Graphy":api #{metabase.xrays.api@@ -2673,7 +2692,8 @@setupsyncusers- util}+ util+ enterprise/workspaces}:model-imports #{:model/ApiKey :model/Database :model/User}}enterprise/advanced-permissions@@ -2709,7 +2729,7 @@enterprise/scimenterprise/semantic-searchenterprise/sso}- :model-imports #{:model/Database :model/Sandbox}}+ :model-imports #{:model/Database :model/Sandbox :model/Workspace}}enterprise/api{:team "UX West"@@ -2831,6 +2851,11 @@utilenterprise/harbormaster}}+ enterprise/connection-overlays+ {:team "Graphy"+ :api #{}+ :uses #{premium-features}}+enterprise/content-translation{:team "UX West":api #{metabase-enterprise.content-translation.routes}@@ -2945,7 +2970,8 @@settingsutilwarehouse-schema- warehouses}+ warehouses+ workspaces}:model-exports #{:model/DatabaseRouter}:model-imports #{:model/Database :model/Transform}}@@ -3311,6 +3337,7 @@metabase-enterprise.serialization.cmdmetabase-enterprise.serialization.coremetabase-enterprise.serialization.init+ metabase-enterprise.serialization.schemametabase-enterprise.serialization.v2.backfill-ids}:uses #{analyticsapi@@ -3473,14 +3500,28 @@:model-imports #{:model/Database :model/Table}}enterprise/workspaces- {:team "Metabot"- :api #{}- :uses #{premium-features}}-- enterprise/writable-connection{:team "Graphy"- :api #{}- :uses #{premium-features}}}+ :api #{metabase-enterprise.workspaces.api+ metabase-enterprise.workspaces.core+ metabase-enterprise.workspaces.init}+ :uses #{api+ app-db+ cache+ driver+ lib+ models+ permissions+ premium-features+ query-processor+ server+ settings+ sql-tools+ util+ workspaces+ enterprise/serialization}+ :model-exports #{:model/Workspace}+ :model-imports #{:model/Database+ :model/User}}};; namespaces matching these patterns (with `re-find`) are excluded from the module linter. Since regex literals;; aren't allowed in EDN just used the `(str <regex>)` version i.e. two slashes instead of one.diff --git a/.clj-kondo/src/hooks/clojure/test.clj b/.clj-kondo/src/hooks/clojure/test.cljindex 00e79592df2..7ee67b2e501 100644--- a/.clj-kondo/src/hooks/clojure/test.clj+++ b/.clj-kondo/src/hooks/clojure/test.clj@@ -21,15 +21,19 @@(error! form (format "destructive functions like %s are not allowed inside a ^:parallel test or test fixture. If this should be allowed, add it to the whitelist in the Kondo config file [:metabase/validate-deftest]"qualified-symbol)))))(walk [form]- (f form)- (doseq [child (:children form)]- (walk child)))]+ (when-not (contains? (hooks.common/ignored-linters form) :metabase/validate-deftest)+ (f form)+ (doseq [child (:children form)]+ (walk child))))](walk form)))(defn- deftest-check-parallel- "1. Check if test is marked ^:parallel / ^:synchronized correctly- 2. Make sure disallowed forms are not used in ^:parallel tests"- [{[_ test-name & body] :children, :as _node} config]+ "1. Check if test is marked ^:parallel / ^:synchronized correctly.+ 2. Make sure disallowed forms are not used in ^:parallel tests.+ 3. Disallow ^:parallel tests in namespaces marked ^:synchronous -- the+ ns marker is the author's blanket promise that nothing inside runs+ in parallel, so an individual ^:parallel deftest contradicts it."+ [{[_ test-name & body] :children, :as _node} ns-sym config](let [test-metadata (:meta test-name)metadata-sexprs (map hooks/sexpr test-metadata)combined-metadata (transduce@@ -41,11 +45,20 @@{}metadata-sexprs)parallel? (:parallel combined-metadata)- synchronized? (:synchronized combined-metadata)]+ synchronized? (:synchronized combined-metadata)+ ns-synchronous? (boolean (:synchronous (meta ns-sym)))](when (and parallel? synchronized?)(hooks/reg-finding! (assoc (meta test-name):message "Test should not be marked both ^:parallel and ^:synchronized":type :metabase/validate-deftest)))+ (when (and parallel? ns-synchronous?)+ (hooks/reg-finding!+ (assoc (meta test-name)+ :message (str "Test should not be marked ^:parallel in a namespace marked ^:synchronous -- "+ "the ns marker promises no parallel execution. "+ "Drop ^:parallel from the deftest, or drop ^:synchronous from the ns. "+ "[:metabase/validate-deftest]")+ :type :metabase/validate-deftest)));; only when the custom `:metabase/deftest-not-marked-parallel-or-synchronized` is enabled: complain if tests are;; not explicitly marked `^:parallel` or `^:synchronized`. This is mostly to encourage people to mark everything;; `^:parallel` in places like `metabase.lib` tests unless there is a really good reason not to.@@ -135,12 +148,12 @@:message (format "All tests must live in a known module; %s is not a known module" (pr-str current-module)):type :metabase/tests-must-live-in-known-modules)))))))-(defn deftest [{:keys [node cljc lang config], :as input}]+(defn deftest [{:keys [node cljc lang config], ns-sym :ns, :as input}];; run [[deftest-check-parallel]] only once... if this is a `.cljc` file only run it for the `:clj` analysis, no point;; in running it twice.(when (or (not cljc)(= lang :clj))- (deftest-check-parallel node (get-in config [:linters :metabase/validate-deftest])))+ (deftest-check-parallel node ns-sym (get-in config [:linters :metabase/validate-deftest])))(deftest-check-not-horrifically-long node (get-in config [:linters :metabase/i-like-making-cams-eyes-bleed-with-horrifically-long-tests]))(deftest-check-no-driver-keywords node (get-in config [:linters :metabase/disallow-hardcoded-driver-names-in-tests]))(deftest-check-in-valid-module input)@@ -185,6 +198,21 @@(warn-about-missing-test-expr-requires-in-cljs node)){:node node})-(defn use-fixtures [{:keys [node config]}]- (warn-about-disallowed-parallel-forms node config)+(defn use-fixtures+ "Flag `:parallel/unsafe` forms inside fixtures. Skipped when the surrounding+ namespace is marked `^:synchronous` -- the ns marker is the author's+ explicit opt-in to a single-threaded test ns where destructive setup is+ safe. Without that opt-in, kondo can't know whether sibling deftests will+ be `^:parallel`, so the conservative default is to flag.++ Sister to [[deftest-check-parallel]]: the deftest hook rejects `^:parallel`+ tests inside a `^:synchronous` ns, so the two checks together form a+ coherent opt-in: either the whole ns is synchronous (fixtures can be+ anything; no parallel tests allowed) or it isn't (fixtures must be+ parallel-safe)."+ [{:keys [node config], ns-sym :ns}]+ (when-not (:synchronous (meta ns-sym))+ (let [linter-config (get-in config [:linters :metabase/validate-deftest])]+ (doseq [form (rest (:children node))]+ (warn-about-disallowed-parallel-forms form linter-config)))){:node node})diff --git a/docs/developers-guide/driver-changelog.md b/docs/developers-guide/driver-changelog.mdindex f6617444881..06ecc7194e7 100644--- a/docs/developers-guide/driver-changelog.md+++ b/docs/developers-guide/driver-changelog.md@@ -29,6 +29,29 @@ title: Driver interface changelog`metabase.driver-api.core/simplify-compound-filter`, deprecated in 0.57.0, have been removed; use the`metabase.lib.core` versions instead. The new versions operate on MBQL 5 instead of MBQL 4.+- Added `metabase.driver.sql/table-qualification-style` multimethod. Returns one of+ `:table-qualification-style/{table,schema-table,db-table,db-schema-table}` describing the per-driver+ SQL identifier shape. Used by workspace table remapping+ (`metabase-enterprise.workspaces.core/engine-namespace-positions`) to decide tuple shape when+ storing `:model/TableRemapping` rows and matching AST positions during query rewriting. Defaults+ to `:table-qualification-style/schema-table` -- the common case, so Postgres/Redshift/H2/ClickHouse+ need no override. Drivers that emit `db.table` (MySQL) override to+ `:table-qualification-style/db-table`; drivers that emit `db.schema.table` (SQL Server, BigQuery)+ override to `:table-qualification-style/db-schema-table`. Drivers that emit a bare `table` use+ `:table-qualification-style/table`.++- Added `metabase.driver.sql/db-slot-value` multimethod. Returns the `:db` AST slot string (catalog,+ project id, etc.) for a `Database` row. Required for `:table-qualification-style/db-table` and+ `:table-qualification-style/db-schema-table` drivers; the default returns `nil`. Overridden by+ MySQL and SQL Server (`(:db (:details db))`) and BigQuery (`(:project-id (:details db))`).++- Added `metabase.driver/qualified-name-components` multimethod. Returns the ordered subset of+ `#{:db :schema}` identifier positions a driver populates when referencing a table in compiled+ SQL. Defaults to `[:schema]`. Drivers that emit bare table names (Mongo) override to `[]`;+ MySQL overrides to `[:db]` (its "database" rides on the connection but participates as the+ `:db` AST slot for cross-DB consumers); drivers that emit a 3-part `catalog.schema.table`+ identifier (SQL Server, BigQuery) override to `[:db :schema]`.+## Metabase 0.61.0- Added the following driver multimethods to support MBQL5 compilation migration:diff --git a/e2e/support/helpers/api/index.ts b/e2e/support/helpers/api/index.tsindex 522efdcb689..cfbf4bd73c6 100644--- a/e2e/support/helpers/api/index.ts+++ b/e2e/support/helpers/api/index.ts@@ -54,3 +54,7 @@ export {updateSetting,} from "./updateSetting";export { updateSnippet } from "./updateSnippet";+export {+ clearWorkspaceInstanceConfig,+ setWorkspaceInstanceConfig,+} from "./setWorkspaceInstanceConfig";diff --git a/e2e/support/helpers/api/setWorkspaceInstanceConfig.ts b/e2e/support/helpers/api/setWorkspaceInstanceConfig.tsnew file mode 100644index 00000000000..3d9945d2f64--- /dev/null+++ b/e2e/support/helpers/api/setWorkspaceInstanceConfig.ts@@ -0,0 +1,9 @@+import type { WorkspaceInstance } from "metabase-types/api";++export function setWorkspaceInstanceConfig(config: WorkspaceInstance) {+ return cy.request("POST", "/api/testing/workspace-instance", config);+}++export function clearWorkspaceInstanceConfig() {+ return cy.request("DELETE", "/api/testing/workspace-instance");+}diff --git a/e2e/support/helpers/e2e-data-studio-helpers.ts b/e2e/support/helpers/e2e-data-studio-helpers.tsindex 7d6cb28ad48..4607d1be04a 100644--- a/e2e/support/helpers/e2e-data-studio-helpers.ts+++ b/e2e/support/helpers/e2e-data-studio-helpers.ts@@ -42,11 +42,15 @@ export const DataStudio = {cy.visit("/data-studio/transforms");DataStudio.Transforms.list().should("be.visible");},+ visitTransform: (transformId: TransformId) => {+ cy.visit(`/data-studio/transforms/${transformId}`);+ },visitInspect: (transformId: TransformId) => {cy.visit(`/data-studio/transforms/${transformId}/inspect`);},visitSettingsTab: (transformId: TransformId) =>cy.visit(`/data-studio/transforms/${transformId}/settings`),+ runButton: () => cy.findAllByTestId("run-button").eq(0),pythonResults: () => cy.findByTestId("python-results"),enableTransformPage: () => cy.findByTestId("enable-transform-page"),},diff --git a/e2e/support/helpers/e2e-workspace-helpers.ts b/e2e/support/helpers/e2e-workspace-helpers.tsnew file mode 100644index 00000000000..c4c676b80dd--- /dev/null+++ b/e2e/support/helpers/e2e-workspace-helpers.ts@@ -0,0 +1,127 @@+import type { WorkspaceId } from "metabase-types/api";++import { modal } from "./e2e-ui-elements-helpers";++export const WorkspaceListPage = {+ get: () => cy.findByTestId("workspace-list-page"),+ visit: () => {+ cy.visit("/data-studio/workspaces");+ WorkspaceListPage.get().should("be.visible");+ },+ newButton: ({ first = true }: { first?: boolean } = {}) =>+ WorkspaceListPage.get().findByRole("button", {+ name: first ? "Create a workspace" : "New",+ }),+ workspaceList: () => WorkspaceListPage.get().findByTestId("workspace-list"),+ workspace: (name: string) =>+ WorkspaceListPage.get().findByRole("region", { name }),+};++export const NewWorkspaceModal = {+ get: () => modal(),+ nameInput: () => NewWorkspaceModal.get().findByLabelText("Name"),+ createButton: () =>+ NewWorkspaceModal.get().findByRole("button", { name: "Create" }),+ cancelButton: () =>+ NewWorkspaceModal.get().findByRole("button", { name: "Cancel" }),+};++export const WorkspacePage = {+ get: () => cy.findByTestId("workspace-page"),+ visit: (id: WorkspaceId) => {+ cy.visit(`/data-studio/workspaces/${id}`);+ WorkspacePage.get().should("be.visible");+ },+ nameInput: () => WorkspacePage.get().findByTestId("workspace-name-input"),+ breadcrumbs: () =>+ WorkspacePage.get().findByTestId("data-studio-breadcrumbs"),+ menuButton: () =>+ WorkspacePage.get().findByRole("button", { name: "Workspace actions" }),+ deleteMenuItem: () => cy.findByRole("menuitem", { name: "Delete" }),+};++export const DeleteWorkspaceModal = {+ get: () => modal(),+ confirmButton: () =>+ DeleteWorkspaceModal.get().findByRole("button", {+ name: "Delete workspace",+ }),+ cancelButton: () =>+ DeleteWorkspaceModal.get().findByRole("button", { name: "Cancel" }),+};++export const WorkspaceDatabaseSection = {+ get: () => cy.findByTestId("workspace-database-section"),+ addDatabaseButton: ({ first = true }: { first?: boolean } = {}) =>+ WorkspaceDatabaseSection.get().findByRole("button", {+ name: first ? "Add database" : "Add another database",+ }),+ databaseList: () =>+ WorkspaceDatabaseSection.get().findAllByTestId("workspace-database-item"),+ database: (name: string) =>+ WorkspaceDatabaseSection.get().findByRole("region", { name }),+ databaseMenuButton: (name: string) =>+ WorkspaceDatabaseSection.database(name).findByRole("button", {+ name: "Database actions",+ }),+ editMenuItem: () => cy.findByRole("menuitem", { name: "Edit" }),+ removeMenuItem: () => cy.findByRole("menuitem", { name: "Remove" }),+};++export const NewWorkspaceDatabaseModal = {+ get: () => modal(),+ databaseRadio: (name: string) =>+ NewWorkspaceDatabaseModal.get().findByRole("radio", { name }),+ schemasInput: () =>+ NewWorkspaceDatabaseModal.get().findByLabelText("Schemas to include"),+ submitButton: () =>+ NewWorkspaceDatabaseModal.get().findByRole("button", {+ name: "Add database",+ }),+ cancelButton: () =>+ NewWorkspaceDatabaseModal.get().findByRole("button", { name: "Cancel" }),+};++export const UpdateWorkspaceDatabaseModal = {+ get: () => modal(),+ schemasInput: () =>+ UpdateWorkspaceDatabaseModal.get().findByLabelText("Schemas to include"),+ saveButton: () =>+ UpdateWorkspaceDatabaseModal.get().findByRole("button", {+ name: "Save changes",+ }),+ cancelButton: () =>+ UpdateWorkspaceDatabaseModal.get().findByRole("button", { name: "Cancel" }),+};++export const RemoveWorkspaceDatabaseModal = {+ get: () => modal(),+ confirmButton: () =>+ RemoveWorkspaceDatabaseModal.get().findByRole("button", { name: "Remove" }),+ cancelButton: () =>+ RemoveWorkspaceDatabaseModal.get().findByRole("button", { name: "Cancel" }),+};++export const WorkspaceSetupSection = {+ get: () => cy.findByTestId("workspace-setup-section"),+ downloadConfigButton: () =>+ WorkspaceSetupSection.get().findByRole("link", {+ name: /Download config\.yml/,+ }),+};++export const WorkspaceInstancePage = {+ get: () => cy.findByTestId("workspace-instance-page"),+ visit: () => {+ cy.visit("/data-studio/workspaces/instance");+ WorkspaceInstancePage.get().should("be.visible");+ },+ database: (name: string) =>+ WorkspaceInstancePage.get().findByRole("region", { name }),+ emptyState: () =>+ WorkspaceInstancePage.get().findByText(+ /Tables will be remapped here the first time a transform runs/,+ ),+ remappingRow: (canonicalName: string) =>+ WorkspaceInstancePage.get().findByText(canonicalName),+};diff --git a/e2e/support/helpers/index.ts b/e2e/support/helpers/index.tsindex 59210d611c9..f0ec0ffb3f1 100644--- a/e2e/support/helpers/index.ts+++ b/e2e/support/helpers/index.ts@@ -64,4 +64,5 @@ export * from "./e2e-upload-helpers";export * from "./e2e-users-helpers";export * from "./e2e-visual-tests-helpers";export * from "./e2e-viz-settings-helpers";+export * from "./e2e-workspace-helpers";export * from "./embedding-sdk-helpers/constants";diff --git a/e2e/test/scenarios/workspaces/workspace-instance.cy.spec.ts b/e2e/test/scenarios/workspaces/workspace-instance.cy.spec.tsnew file mode 100644index 00000000000..fdce85a41b5--- /dev/null+++ b/e2e/test/scenarios/workspaces/workspace-instance.cy.spec.ts@@ -0,0 +1,199 @@+import { WRITABLE_DB_ID } from "e2e/support/cypress_data";++const { H } = cy;++const WORKSPACE_NAME = "E2E workspace instance";+const ROW_COUNT = 3;++describe("scenarios > workspaces > workspace instance", () => {+ describe("postgres", () => {+ const databaseName = "Writable Postgres12";+ const inputSchema = "Domestic";+ const outputSchema = "mb__isolation";+ const sourceTable = "Animals";+ const targetTable = "transform_table";+ const targetTableDisplayName = "Transform Table";++ beforeEach(() => {+ H.restore("postgres-writable");+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ H.resetTestTable({ type: "postgres", table: "multi_schema" });+ H.queryWritableDB(+ `CREATE SCHEMA IF NOT EXISTS ${outputSchema}`,+ "postgres",+ );+ H.resyncDatabase({ dbId: WRITABLE_DB_ID });+ H.setWorkspaceInstanceConfig({+ name: WORKSPACE_NAME,+ databases: {+ [WRITABLE_DB_ID]: {+ input_schemas: [inputSchema],+ output: { schema: outputSchema },+ },+ },+ });+ });++ afterEach(() => {+ H.clearWorkspaceInstanceConfig();+ H.queryWritableDB(+ `DROP SCHEMA IF EXISTS ${outputSchema} CASCADE`,+ "postgres",+ );+ });++ it("runs a transform, surfaces the remapping on the instance page, and queries the remapped table", () => {+ cy.log("create and run a transform via the API");+ createAndRunTransform({+ sourceTable,+ sourceSchema: inputSchema,+ targetTable,+ targetSchema: inputSchema,+ });++ cy.log("instance page shows the remapping for the transform target");+ H.WorkspaceInstancePage.visit();+ H.WorkspaceInstancePage.database(databaseName)+ .should("contain.text", `${inputSchema}/${targetTable}`)+ .and("contain.text", `${outputSchema}/${inputSchema}__${targetTable}`);++ cy.log("native query is rewritten to workspace table");+ H.startNewNativeQuestion({+ database: WRITABLE_DB_ID,+ query: `SELECT * FROM "${inputSchema}"."${targetTable}"`,+ });+ H.runNativeQuery();+ H.assertQueryBuilderRowCount(ROW_COUNT);++ cy.log("mbql query is rewritten to workspace table");+ H.startNewQuestion();+ H.miniPicker().findByText(databaseName).click();+ H.miniPicker().findByText(inputSchema).click();+ H.miniPicker().findByText(targetTableDisplayName).click();+ H.visualize();+ H.assertQueryBuilderRowCount(ROW_COUNT);++ cy.log("the actual warehouse table lives under the workspace schema");+ H.queryWritableDB(+ `SELECT COUNT(*) AS count FROM "${outputSchema}"."${inputSchema}__${targetTable}"`,+ "postgres",+ ).then((result) => {+ expect(Number(result.rows[0].count)).to.eq(ROW_COUNT);+ });+ });+ });++ describe("mysql", () => {+ const databaseName = "Writable MySQL8";+ const outputDatabase = "mb__isolation";+ const sourceTable = "scoreboard_actions";+ const targetTable = "transform_table";+ const targetTableDisplayName = "Transform Table";++ beforeEach(() => {+ H.restore("mysql-writable");+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ H.resetTestTable({ type: "mysql", table: sourceTable });+ H.queryWritableDB(`DROP DATABASE IF EXISTS ${outputDatabase}`, "mysql");+ H.queryWritableDB(`CREATE DATABASE ${outputDatabase}`, "mysql");+ H.resyncDatabase({ dbId: WRITABLE_DB_ID });+ H.setWorkspaceInstanceConfig({+ name: WORKSPACE_NAME,+ databases: {+ [WRITABLE_DB_ID]: {+ input_schemas: [],+ output: { db: outputDatabase },+ },+ },+ });+ });++ afterEach(() => {+ H.clearWorkspaceInstanceConfig();+ H.queryWritableDB(`DROP DATABASE IF EXISTS ${outputDatabase}`, "mysql");+ });++ it("runs a transform, surfaces the remapping on the instance page, and queries the remapped table", () => {+ cy.log("create and run a transform via the API");+ createAndRunTransform({+ sourceTable,+ sourceSchema: null,+ targetTable,+ targetSchema: null,+ });++ cy.log("instance page shows the remapping for the transform target");+ H.WorkspaceInstancePage.visit();+ H.WorkspaceInstancePage.database(databaseName)+ .should("contain.text", targetTable)+ .and("contain.text", `${outputDatabase}/__${targetTable}`);++ cy.log("native query is rewritten to workspace table");+ H.startNewNativeQuestion({+ database: WRITABLE_DB_ID,+ query: `SELECT * FROM \`${targetTable}\``,+ });+ H.runNativeQuery();+ H.assertQueryBuilderRowCount(ROW_COUNT);++ cy.log("mbql query is rewritten to workspace table");+ H.startNewQuestion();+ H.miniPicker().findByText(databaseName).click();+ H.miniPicker().findByText(targetTableDisplayName).click();+ H.visualize();+ H.assertQueryBuilderRowCount(ROW_COUNT);++ cy.log("the actual warehouse table lives under the workspace database");+ H.queryWritableDB(+ `SELECT COUNT(*) AS count FROM \`${outputDatabase}\`.\`__${targetTable}\``,+ "mysql",+ ).then((result) => {+ expect(Number(result.rows[0].count)).to.eq(ROW_COUNT);+ });+ });+ });+});++function createAndRunTransform({+ sourceTable,+ sourceSchema,+ targetTable,+ targetSchema,+}: {+ sourceTable: string;+ sourceSchema: string | null;+ targetTable: string;+ targetSchema: string | null;+}) {+ cy.log("create and run a transform via the API");+ H.getTableId({+ databaseId: WRITABLE_DB_ID,+ name: sourceTable,+ schema: sourceSchema ?? undefined,+ }).then((sourceTableId) => {+ H.createTestQuery({+ database: WRITABLE_DB_ID,+ stages: [+ {+ source: { type: "table", id: sourceTableId },+ limit: ROW_COUNT,+ },+ ],+ }).then((query) => {+ H.createTransform({+ name: "Workspace transform",+ source: { type: "query", query },+ target: {+ type: "table",+ database: WRITABLE_DB_ID,+ name: targetTable,+ schema: targetSchema,+ },+ }).then(({ body: transform }) => {+ H.runTransformAndWaitForSuccess(transform.id);+ });+ });+ });+}diff --git a/e2e/test/scenarios/workspaces/workspace-manager.cy.spec.ts b/e2e/test/scenarios/workspaces/workspace-manager.cy.spec.tsnew file mode 100644index 00000000000..5432d4ce6c9--- /dev/null+++ b/e2e/test/scenarios/workspaces/workspace-manager.cy.spec.ts@@ -0,0 +1,215 @@+import path from "path";++import { WRITABLE_DB_ID } from "e2e/support/cypress_data";+import { NORMAL_USER_ID } from "e2e/support/cypress_sample_instance_data";++const { H } = cy;++const CONFIG_FILENAME = "config.yml";++const CREATE_QUERIES_PERMISSION_INDEX = 1;+const WORKSPACES_PERMISSION_INDEX = 6;++describe("scenarios > workspaces > workspace manager", () => {+ describe("navigation", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ it("renames a workspace and navigates between the workspace and the list", () => {+ const originalName = "My workspace";+ const renamedName = "Renamed workspace";++ cy.log("create the workspace from the empty state");+ H.WorkspaceListPage.visit();+ H.WorkspaceListPage.newButton().click();+ H.NewWorkspaceModal.nameInput().type(originalName);+ H.NewWorkspaceModal.createButton().click();+ H.WorkspacePage.get().should("be.visible");++ cy.log("rename the workspace in the header");+ H.WorkspacePage.nameInput()+ .should("have.value", originalName)+ .clear()+ .type(renamedName)+ .blur();+ H.WorkspacePage.nameInput().should("have.value", renamedName);++ cy.log("navigate back to the list via the breadcrumbs");+ H.WorkspacePage.breadcrumbs()+ .findByRole("link", { name: "Workspaces" })+ .click();+ H.WorkspaceListPage.get().should("be.visible");++ cy.log("open the renamed workspace from the list");+ H.WorkspaceListPage.workspace(renamedName).should("be.visible").click();+ H.WorkspacePage.get().should("be.visible");+ H.WorkspacePage.nameInput().should("have.value", renamedName);+ });+ });++ describe("postgres (with schemas)", () => {+ const POSTGRES_DB_NAME = "Writable Postgres12";+ const PG_SCHEMA_A = "Domestic";+ const PG_SCHEMA_B = "Wild";+ const workspaceName = "PG Workspace";++ beforeEach(() => {+ H.restore("postgres-writable");+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ H.resetTestTable({ type: "postgres", table: "multi_schema" });+ H.resyncDatabase({ dbId: WRITABLE_DB_ID });+ cy.deleteDownloadsFolder();+ });++ it("should create, edit, download config, and delete a workspace as admin", () => {+ runPostgresWorkflow();+ });++ it("should create, edit, download config, and delete a workspace as analyst after granting workspace permissions", () => {+ H.setUserAsAnalyst(NORMAL_USER_ID);+ grantWorkspacesPermissions();+ cy.signInAsNormalUser();+ runPostgresWorkflow();+ });++ function runPostgresWorkflow() {+ cy.log("create the workspace");+ H.WorkspaceListPage.visit();+ H.WorkspaceListPage.newButton().click();+ H.NewWorkspaceModal.nameInput().type(workspaceName);+ H.NewWorkspaceModal.createButton().click();+ H.WorkspacePage.get().should("be.visible");++ cy.log("add the writable Postgres database with one schema");+ H.WorkspaceDatabaseSection.addDatabaseButton().click();+ H.NewWorkspaceDatabaseModal.schemasInput().click();+ H.popover().findByRole("option", { name: PG_SCHEMA_A }).click();+ H.NewWorkspaceDatabaseModal.get().click();+ H.NewWorkspaceDatabaseModal.submitButton().click();++ H.WorkspaceDatabaseSection.database(POSTGRES_DB_NAME)+ .should("be.visible")+ .and("contain.text", PG_SCHEMA_A);++ cy.log("edit the database to include a second schema");+ H.WorkspaceDatabaseSection.databaseMenuButton(POSTGRES_DB_NAME).click();+ H.WorkspaceDatabaseSection.editMenuItem().click();+ H.UpdateWorkspaceDatabaseModal.schemasInput().click();+ H.popover().findByRole("option", { name: PG_SCHEMA_B }).click();+ H.UpdateWorkspaceDatabaseModal.get().click();+ H.UpdateWorkspaceDatabaseModal.saveButton().click();++ H.WorkspaceDatabaseSection.database(POSTGRES_DB_NAME)+ .should("contain.text", PG_SCHEMA_A)+ .and("contain.text", PG_SCHEMA_B);++ cy.log("download the workspace config");+ downloadConfig();+ readConfig().should((contents) => {+ expect(contents).to.contain(workspaceName);+ expect(contents).to.contain(POSTGRES_DB_NAME);+ expect(contents).to.contain("postgres");+ expect(contents).to.contain(PG_SCHEMA_A);+ expect(contents).to.contain(PG_SCHEMA_B);+ expect(contents).to.match(/schema-filters-patterns:.*Domestic.*Wild/);+ });++ cy.log("remove the database from the workspace");+ H.WorkspaceDatabaseSection.databaseMenuButton(POSTGRES_DB_NAME).click();+ H.WorkspaceDatabaseSection.removeMenuItem().click();+ H.RemoveWorkspaceDatabaseModal.confirmButton().click();+ H.WorkspaceDatabaseSection.databaseList().should("not.exist");++ cy.log("delete the workspace");+ H.WorkspacePage.menuButton().click();+ H.WorkspacePage.deleteMenuItem().click();+ H.DeleteWorkspaceModal.confirmButton().click();+ H.WorkspaceListPage.get().should("be.visible");+ H.WorkspaceListPage.workspace(workspaceName).should("not.exist");+ }+ });++ describe("mysql (no schemas)", () => {+ const MYSQL_DB_NAME = "Writable MySQL8";+ const workspaceName = "MySQL Workspace";++ beforeEach(() => {+ H.restore("mysql-writable");+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ cy.deleteDownloadsFolder();+ });++ it("creates, downloads config, and deletes a workspace as admin", () => {+ runMysqlWorkflow();+ });++ function runMysqlWorkflow() {+ cy.log("create the workspace");+ H.WorkspaceListPage.visit();+ H.WorkspaceListPage.newButton().click();+ H.NewWorkspaceModal.nameInput().type(workspaceName);+ H.NewWorkspaceModal.createButton().click();+ H.WorkspacePage.get().should("be.visible");++ cy.log("add the writable MySQL database (no schemas to pick)");+ H.WorkspaceDatabaseSection.addDatabaseButton().click();+ H.NewWorkspaceDatabaseModal.schemasInput().should("not.exist");+ H.NewWorkspaceDatabaseModal.submitButton().click();++ H.WorkspaceDatabaseSection.database(MYSQL_DB_NAME).should("be.visible");++ cy.log("download the workspace config");+ downloadConfig();+ readConfig().should((contents) => {+ expect(contents).to.contain(workspaceName);+ expect(contents).to.contain(MYSQL_DB_NAME);+ expect(contents).to.contain("mysql");+ expect(contents).not.to.contain("schema-filters-patterns");+ });++ cy.log("remove the database from the workspace");+ H.WorkspaceDatabaseSection.databaseMenuButton(MYSQL_DB_NAME).click();+ H.WorkspaceDatabaseSection.removeMenuItem().click();+ H.RemoveWorkspaceDatabaseModal.confirmButton().click();+ H.WorkspaceDatabaseSection.databaseList().should("not.exist");++ cy.log("delete the workspace");+ H.WorkspacePage.menuButton().click();+ H.WorkspacePage.deleteMenuItem().click();+ H.DeleteWorkspaceModal.confirmButton().click();+ H.WorkspaceListPage.get().should("be.visible");+ H.WorkspaceListPage.workspace(workspaceName).should("not.exist");+ }+ });+});++function grantWorkspacesPermissions() {+ cy.intercept("PUT", "/api/permissions/graph").as("savePermissions");+ cy.visit(`/admin/permissions/data/database/${WRITABLE_DB_ID}`);+ H.modifyPermission(+ "All Users",+ CREATE_QUERIES_PERMISSION_INDEX,+ "Query builder and native",+ );+ H.modifyPermission("All Users", WORKSPACES_PERMISSION_INDEX, "Yes");+ cy.button("Save changes").click();+ H.modal().within(() => {+ cy.button("Yes").click();+ });+ cy.wait("@savePermissions");+}++function downloadConfig() {+ H.WorkspaceSetupSection.downloadConfigButton().click();+ cy.verifyDownload(CONFIG_FILENAME, { timeout: 15000 });+}++function readConfig() {+ const downloadsFolder = Cypress.config("downloadsFolder");+ return cy.readFile(path.join(downloadsFolder, CONFIG_FILENAME));+}diff --git a/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/file.cljindex fa18a6d355e..98f9c82cdcd 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj@@ -101,6 +101,7 @@[metabase-enterprise.advanced-config.file.interface :as advanced-config.file.i][metabase-enterprise.advanced-config.file.settings][metabase-enterprise.advanced-config.file.users]+ [metabase-enterprise.advanced-config.file.workspace :as advanced-config.file.workspace][metabase.lib.core :as lib][metabase.premium-features.core :as premium-features][metabase.util :as u]@@ -118,7 +119,9 @@;; for `users:` section codemetabase-enterprise.advanced-config.file.users/keep-me;; for `api-keys:` section code- metabase-enterprise.advanced-config.file.api-keys/keep-me)+ metabase-enterprise.advanced-config.file.api-keys/keep-me+ ;; for `workspace:` section code+ advanced-config.file.workspace/keep-me)(set! *warn-on-reflection* true)@@ -248,21 +251,49 @@config-sections)](concat settings-sections other-sections)))+(defn- workspace-bring-up?+ "True iff the config file declares a *structurally-valid* `:workspace` section.+ Workspace bring-up is a fundamentally different mode of operation from normal+ `config-text-file` usage: the instance is being bootstrapped as a child of a parent+ Metabase, and workspace mode (table remapping, transform redirection, sync rewiring)+ is destructive enough to a normal Metabase that an instance carrying a `:workspace`+ config is committing to that mode regardless of token state. A non-workspace OSS+ instance reading this same config would not function correctly anyway, so the file's+ presence is itself the operator's declaration of intent.++ The validity check (delegated to `advanced-config.file.workspace/valid-workspace-section?`)+ is deliberate: we don't want a typo'd or empty `workspace:` key — `workspace: {}`,+ `workspace: foo` — to silently bypass the premium gate. Only a section that would+ actually drive workspace bring-up (has `:name` plus a non-empty `:databases` map of+ the correct shape) opens the gate."+ [m]+ (when-let [section (get-in m [:config :workspace])]+ (advanced-config.file.workspace/valid-workspace-section? section)))+(defn ^{:added "0.45.0"} initialize!"Initialize Metabase according to the directives in the config file, if it exists."[];; TODO -- this should only do anything if we have an appropriate token (we should get a token for testing this before;; enabling that check tho)(when-let [m (config)]- (doseq [[section-name section-config] (sort-by-initialization-order (:config m))]- ;; you can only use the config-from-file stuff with an EE/Pro token with the `:config-text-file` feature. Since you- ;; might have to use the `:settings` section to set the token, skip the check for Settings. But check it for the- ;; other sections.- (when-not (= section-name :settings)- (when-not (premium-features/enable-config-text-file?)- (throw (ex-info (tru "Metabase config files require a Premium token with the :config-text-file feature.")- {}))))- (log/info (u/format-color :magenta "Initializing %s from config file..." section-name) (u/emoji "🗄️"))- (advanced-config.file.i/initialize-section! section-name section-config))+ (let [bring-up? (workspace-bring-up? m)]+ (doseq [[section-name section-config] (sort-by-initialization-order (:config m))]+ ;; You can only use the config-from-file stuff with an EE/Pro token with the `:config-text-file` feature. Two+ ;; carve-outs:+ ;;+ ;; 1. The `:settings` section is always allowed — you may need it to *install* the token.+ ;;+ ;; 2. If the file contains a `:workspace` section, the entire file is treated as a workspace bring-up+ ;; manifest and runs without the premium token. Workspace child instances bootstrap from a `config.yml`+ ;; *before* their token is installed; the file is the delivery mechanism for the token (and the+ ;; workspace itself). The `defenterprise` gates on workspace mode (`workspace-mode?`,+ ;; `db-workspace-namespace`) still keep non-EE builds from acting on the rows.+ (when-not (or (= section-name :settings)+ bring-up?)+ (when-not (premium-features/enable-config-text-file?)+ (throw (ex-info (tru "Metabase config files require a Premium token with the :config-text-file feature.")+ {}))))+ (log/info (u/format-color :magenta "Initializing %s from config file..." section-name) (u/emoji "🗄️"))+ (advanced-config.file.i/initialize-section! section-name section-config)))(log/info (u/colorize :magenta "Done initializing from file.") (u/emoji "🗄️"))):ok)diff --git a/enterprise/backend/src/metabase_enterprise/advanced_config/file/workspace.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/file/workspace.cljnew file mode 100644index 00000000000..3ce6348ff89--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/file/workspace.clj@@ -0,0 +1,140 @@+(ns metabase-enterprise.advanced-config.file.workspace+ "Loader for the `:workspace` section of `config.yml`.++ On boot, parses the section and stores it in the in-process atom:+ [[metabase-enterprise.workspaces.core/workspace-instance-config]].+ That atom is the instance-side source of truth for `db-workspace-namespace`.++ The atom is fresh per process, every boot re-reads `config.yml` and replaces the+ prior value. There is no durable storage of instance-side workspace state, by+ design: the file IS the source of truth, and a different file at boot means a+ different workspace, no questions asked."+ (:require+ [clojure.spec.alpha :as s]+ [clojure.string :as str]+ [clojure.walk :as walk]+ [metabase-enterprise.advanced-config.file.interface :as advanced-config.file.i]+ [metabase-enterprise.workspaces.core :as ws]+ [metabase.driver :as driver]+ [metabase.util.log :as log]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++;;; ---------------------------------- Spec ----------------------------------+;;;+;;; Wire format: every workspace-database carries `:input_schemas` (vec of plain+;;; schema-name strings) and `:output_namespace` (driver-opaque string —+;;; schema name on Postgres-shaped drivers, database name on MySQL). The+;;; warehouse catalog (SQL Server/BigQuery) is derived from the+;;; canonical `Database.details` at boot, not duplicated on each row.++(s/def ::non-blank-string+ (s/and string? (complement str/blank?)))++(s/def ::input_schemas+ (s/coll-of ::non-blank-string :min-count 1))++(s/def ::output_namespace ::non-blank-string)++(s/def ::workspace-database-config+ (s/keys :req-un [::input_schemas ::output_namespace]))++(s/def ::databases+ ;; map of database-name -> per-database workspace config. Keys may be keywords or+ ;; strings depending on YAML parser flags; both are accepted.+ (s/and (s/map-of (s/or :kw keyword? :str string?) ::workspace-database-config)+ seq))++(s/def ::name ::non-blank-string)++(s/def ::config-file-spec+ (s/keys :req-un [::name ::databases]))++(defn valid-workspace-section?+ "Predicate used by the file-level loader to decide whether `(:workspace m)` is a+ structurally-valid bring-up manifest. Only a valid section opens the+ config-text-file gate for OSS instances — a typo or empty section must not."+ [section-config]+ (s/valid? ::config-file-spec section-config))++(defmethod advanced-config.file.i/section-spec :workspace+ [_section-name]+ (s/spec ::config-file-spec))++;;; -------------------------------- Loader ---------------------------------++(defn- ordered->plain+ "snakeyaml's parsed maps are ordered/lazy; re-walk into plain Clojure maps."+ [x]+ (walk/postwalk+ (fn [form]+ (if (map? form)+ (into {} form)+ form))+ x))++(defn- resolve-db [db-name]+ (or (t2/select-one :model/Database :name db-name)+ (throw (ex-info (str "Workspace config references unknown database: " (pr-str db-name))+ {:database-name db-name}))))++(defn- expand-output+ "Expand a driver-opaque `output_namespace` string into the `{:db ?, :schema ?}`+ atom shape that QP middleware, transform_hooks, and table_remapping consume.++ The atom contract is map-shaped on purpose: doing this expansion once at+ boot lets the per-query hot path read both slots without re-running the+ per-engine case. For 3-slot drivers (SQL Server, BigQuery) the+ `:db` slot is filled from `Database.details`. For 2-slot drivers the+ `output_namespace` string lands in the schema slot.++ `output_namespace` blank means the workspace database isn't provisioned yet —+ the atom carries `{:db ? :schema nil}` and QP/transform consumers treat the+ workspace as having no output mapping."+ [db output-namespace]+ (let [components (set (driver/qualified-name-components (:engine db)))+ positions (ws/engine-namespace-positions db)+ schema (when-not (str/blank? output-namespace) output-namespace)]+ (cond-> {}+ (:db components) (assoc :db (:db positions))+ (:schema components) (assoc :schema schema)+ ;; No-schema drivers (MySQL): the namespace name lands in the db slot.+ (and (:db components) (not (:schema components)))+ (assoc :db schema))))++(defn apply-workspace-section!+ "Boot-time materialization of the parsed `:workspace` section.++ Shape (post-`ordered->plain`):++ {:name \"<workspace-name>\"+ :databases {\"<db-name>\" {:input_schemas [<schema-name>, ...]+ :output_namespace <ns-string>}+ ...}}++ Resolves each database name to a `:model/Database` (rows are populated by the+ `:databases` section, which runs earlier — see [[metabase-enterprise.advanced-config.file/initialize!]])+ and stores the result in [[metabase-enterprise.workspaces.core/workspace-instance-config]] keyed by+ db-id. The atom carries `{:input_schemas [String], :output {:db ?, :schema ?}}`.+ `:output` is expanded once at boot via [[expand-output]] so the QP hot path+ doesn't re-run the per-engine case per query."+ [section-config]+ (let [{:keys [name databases]} (ordered->plain section-config)+ resolved-databases (into {}+ (map (fn [[db-name-kw wsd-config]]+ (let [db-name (clojure.core/name db-name-kw)+ db (resolve-db db-name)]+ [(:id db) {:input_schemas (vec (:input_schemas wsd-config))+ :output (expand-output db (:output_namespace wsd-config))}])))+ databases)]+ (ws/set-instance-workspace! {:name name+ :databases resolved-databases})+ (log/infof "Loaded workspace %s from config.yml with %d database(s)"+ (pr-str name) (count resolved-databases))+ {:workspace-name name+ :database-count (count resolved-databases)}))++(defmethod advanced-config.file.i/initialize-section! :workspace+ [_section-name section-config]+ (apply-workspace-section! section-config))diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.cljindex 0174a6d6a4c..0bfb453d743 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj@@ -78,6 +78,9 @@:can_access_transforms (or api/*is-superuser?* (and api/*is-data-analyst?*(perms/user-has-any-perms-of-type? api/*current-user-id* :perms/view-data:exclude-db-ids [audit/audit-db-id])))+ :can_access_workspaces (or api/*is-superuser?* (and api/*is-data-analyst?*+ (perms/user-has-any-perms-of-type? api/*current-user-id* :perms/view-data+ :exclude-db-ids [audit/audit-db-id]))):is_data_analyst api/*is-data-analyst?*:is_group_manager api/*is-group-manager?*)))diff --git a/enterprise/backend/src/metabase_enterprise/analytics/stats.clj b/enterprise/backend/src/metabase_enterprise/analytics/stats.cljindex dd6e2b1b123..e05430b7c03 100644--- a/enterprise/backend/src/metabase_enterprise/analytics/stats.clj+++ b/enterprise/backend/src/metabase_enterprise/analytics/stats.clj@@ -31,4 +31,7 @@:enabled (some? (advanced-config.settings/subscription-allowed-domains))}{:name :semantic-search:available (premium-features/enable-semantic-search?)- :enabled (semantic-search/supported?)}])+ :enabled (semantic-search/supported?)}+ {:name :workspaces+ :available (premium-features/enable-workspaces?)+ :enabled (t2/exists? :model/Workspace)}])diff --git a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj b/enterprise/backend/src/metabase_enterprise/api_routes/routes.cljindex 35e0a0f2d17..e3e3d705374 100644--- a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj+++ b/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj@@ -42,6 +42,7 @@[metabase-enterprise.transforms-python.api][metabase-enterprise.transforms.api][metabase-enterprise.upload-management.api]+ [metabase-enterprise.workspaces.api][metabase.api.macros :as api.macros][metabase.api.util.handlers :as handlers][metabase.util.i18n :refer [deferred-tru]]))@@ -73,7 +74,8 @@:metabot-v3 (deferred-tru "Metabot"):cloud-custom-smtp (deferred-tru "Custom SMTP"):support-users (deferred-tru "Support Users")- :transforms-python (deferred-tru "Transforms Python")})+ :transforms-python (deferred-tru "Transforms Python")+ :workspaces (deferred-tru "Workspaces")})(defn- premium-handler [handler required-feature](let [handler (cond-> handler@@ -136,7 +138,9 @@"/stale" (premium-handler metabase-enterprise.stale.api/routes :collection-cleanup)"/support-access-grant" (premium-handler metabase-enterprise.support-access-grants.api/routes :support-users)"/tenant" (premium-handler metabase-enterprise.tenants.api/routes :tenants)- "/upload-management" (premium-handler metabase-enterprise.upload-management.api/routes :upload-management)})+ "/upload-management" (premium-handler metabase-enterprise.upload-management.api/routes :upload-management)+ "/workspace-instance" (premium-handler metabase-enterprise.workspaces.api/instance-routes :workspaces)+ "/workspace-manager" (premium-handler metabase-enterprise.workspaces.api/manager-routes :workspaces)});;; ↑↑↑ KEEP THIS SORTED OR ELSE ↑↑↑(def ^:private routes-mapdiff --git a/enterprise/backend/src/metabase_enterprise/connection_overlays/core.clj b/enterprise/backend/src/metabase_enterprise/connection_overlays/core.cljnew file mode 100644index 00000000000..05da47b7d4c--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/connection_overlays/core.clj@@ -0,0 +1,21 @@+(ns metabase-enterprise.connection-overlays.core+ "EE implementations for non-default connection-detail overlays. Each overlay is gated by its own+ premium feature; the OSS counterparts in [[metabase.driver.connection]] return `nil`."+ (:require+ [metabase.premium-features.core :refer [defenterprise]]))++(defenterprise database-write-data-details+ "Returns the `:write-data-details` for a database when the `:writable-connection` feature is available.++ `database` is a [[lib/metadata]] database instance. To pass a Toucan2 database instance, use [[driver.u/ensure-lib-database]]."+ :feature :writable-connection+ [database]+ (:write-data-details database))++(defenterprise database-admin-details+ "Returns the `:admin-details` for a database when the `:workspaces` feature is available.++ `database` is a [[lib/metadata]] database instance. To pass a Toucan2 database instance, use [[driver.u/ensure-lib-database]]."+ :feature :workspaces+ [database]+ (:admin-details database))diff --git a/enterprise/backend/src/metabase_enterprise/core/init.clj b/enterprise/backend/src/metabase_enterprise/core/init.cljindex 8abb55639c3..c441cf49ea9 100644--- a/enterprise/backend/src/metabase_enterprise/core/init.clj+++ b/enterprise/backend/src/metabase_enterprise/core/init.clj@@ -24,4 +24,5 @@[metabase-enterprise.sso.init][metabase-enterprise.stale.init][metabase-enterprise.support-access-grants.init]- [metabase-enterprise.transforms-python.init]))+ [metabase-enterprise.transforms-python.init]+ [metabase-enterprise.workspaces.init]))diff --git a/enterprise/backend/src/metabase_enterprise/database_routing/api.clj b/enterprise/backend/src/metabase_enterprise/database_routing/api.cljindex 4a61f1c0374..7a9c605cc49 100644--- a/enterprise/backend/src/metabase_enterprise/database_routing/api.clj+++ b/enterprise/backend/src/metabase_enterprise/database_routing/api.clj@@ -10,6 +10,7 @@[metabase.util :as u][metabase.util.malli.schema :as ms][metabase.warehouses.core :as warehouses]+ [metabase.workspaces.core :as workspaces][toucan2.core :as t2]));; TODO (Cam 10/28/25) -- fix this endpoint so it uses kebab-case for query parameters for consistency with the rest@@ -36,6 +37,7 @@[:map[:name ms/NonBlankString][:details ms/Map]]]]]]+ (workspaces/check-not-in-workspace-mode! "Database routing")(api/check-400 (t2/exists? :model/DatabaseRouter :database_id router_database_id))(api/check-400 (not (t2/exists? :model/Database :router_database_id router_database_id :name [:in (map :name destinations)]))"A destination database with that name already exists.")@@ -109,11 +111,13 @@[{:keys [id]} :- [:map [:id ms/PositiveInt]]_query-params{:keys [user_attribute]} :- [:map [:user_attribute {:optional true} [:maybe ms/NonBlankString]]]]+ (workspaces/check-not-in-workspace-mode! "Database routing")(let [db (t2/select-one :model/Database :id id)](api/check-404 db)(api/check-400 (not (:router_database_id db)) "Cannot make a destination database a router database")(api/check-400 (not (:uploads_enabled db)) "Cannot enable database routing for a database with uploads enabled")(api/check-400 (not (:write_data_details db)) "Cannot enable database routing for a database with a write connection configured")+ (api/check-400 (not (:admin_details db)) "Cannot enable database routing for a database with an admin connection configured")(api/check-400 (not (t2/exists? :model/Transform :source_database_id id)) "Cannot enable database routing for a database with transforms")(setting/with-database db(api/check-400 (not (setting/get :persist-models-enabled)) "Cannot enable database routing for a database with model persistence enabled")diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.cljindex b3fc907415f..2a415cb35a1 100644--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj@@ -135,6 +135,7 @@"SemanticSearchTokenTracking""Session""SupportAccessGrantLog"+ "TableRemapping""TaskHistory""TaskRun""Tenant"@@ -145,4 +146,6 @@"User""UserKeyValue""UserParameterValue"- "ViewLog"])+ "ViewLog"+ "Workspace"+ "WorkspaceDatabase"])diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/api.clj b/enterprise/backend/src/metabase_enterprise/workspaces/api.cljnew file mode 100644index 00000000000..832a42f0041--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/api.clj@@ -0,0 +1,17 @@+(ns metabase-enterprise.workspaces.api+ (:require+ [metabase-enterprise.workspaces.api.workspace-instance]+ [metabase-enterprise.workspaces.api.workspace-manager]+ [metabase.api.macros :as api.macros]+ [metabase.api.routes.common :refer [+auth]]))++(comment metabase-enterprise.workspaces.api.workspace-instance/keep-me+ metabase-enterprise.workspaces.api.workspace-manager/keep-me)++(def ^{:arglists '([request respond raise])} manager-routes+ "`/api/ee/workspace-manager` routes. Authenticated."+ (+auth (api.macros/ns-handler 'metabase-enterprise.workspaces.api.workspace-manager)))++(def ^{:arglists '([request respond raise])} instance-routes+ "`/api/ee/workspace-instance` routes. Authenticated."+ (+auth (api.macros/ns-handler 'metabase-enterprise.workspaces.api.workspace-instance)))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_instance.clj b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_instance.cljnew file mode 100644index 00000000000..9326963a5e9--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_instance.clj@@ -0,0 +1,73 @@+(ns metabase-enterprise.workspaces.api.workspace-instance+ "EE API endpoints for the workspace loaded on this (child) instance, served under+ `/api/ee/workspace-instance`. Read-only — see [[metabase-enterprise.workspaces.api.workspace-manager]]+ for admin operations."+ (:require+ [metabase-enterprise.workspaces.core :as ws]+ [metabase.api.common :as api]+ [metabase.api.macros :as api.macros]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.util.malli.schema :as ms]))++;;; ----------------------------------------------- Schemas ----------------------------------------------------++(def ^:private TableNamespace+ [:map+ [:db {:optional true} [:maybe :string]]+ [:schema {:optional true} [:maybe :string]]])++(def ^:private WorkspaceInstanceDatabase+ [:map+ [:input_schemas [:sequential :string]]+ [:output TableNamespace]])++(def ^:private WorkspaceInstance+ [:map+ [:name ms/NonBlankString]+ [:databases [:map-of ::lib.schema.id/database WorkspaceInstanceDatabase]]])++(def ^:private TableRemapping+ [:map+ [:id ms/PositiveInt]+ [:database_id ::lib.schema.id/database]+ [:from_db [:maybe :string]]+ [:from_schema [:maybe :string]]+ [:from_table_name ms/NonBlankString]+ [:to_db [:maybe :string]]+ [:to_schema [:maybe :string]]+ [:to_table_name ms/NonBlankString]+ [:created_at :any]])++;;; -------------------------------------------- Presentation --------------------------------------------------++(defn- present-remapping [row]+ (select-keys row [:id :database_id+ :from_db :from_schema :from_table_name+ :to_db :to_schema :to_table_name+ :created_at]))++(defn- present-workspace-instance-database [wsd]+ (select-keys wsd [:input_schemas :output]))++(defn- present-workspace-instance [workspace]+ {:name (:name workspace)+ :databases (update-vals (:databases workspace) present-workspace-instance-database)})++;;; ---------------------------------------------- Endpoints ---------------------------------------------------++(api.macros/defendpoint :get "/current" :- [:maybe WorkspaceInstance]+ "Read-only summary of the workspace loaded on this instance.++ Reads from the in-process atom populated at boot by the `:workspace` section of+ `config.yml`. Returns `nil` when no workspace was loaded — i.e. this is a manager-only+ instance, or no `config.yml` was present at boot."+ []+ (api/check-superuser)+ (when-let [workspace (ws/instance-workspace)]+ (present-workspace-instance workspace)))++(api.macros/defendpoint :get "/table-remappings" :- [:sequential TableRemapping]+ "Return all table remappings, ordered by id."+ []+ (api/check-superuser)+ (mapv present-remapping (ws/list-remappings)))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.cljnew file mode 100644index 00000000000..c9d3870dc77--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj@@ -0,0 +1,219 @@+(ns metabase-enterprise.workspaces.api.workspace-manager+ "EE API endpoints for managing workspaces, served under `/api/ee/workspace-manager`.+ Validation and presentation only — domain logic lives in+ [[metabase-enterprise.workspaces.core]] and permission predicates live on+ `:model/Workspace` and `:model/WorkspaceDatabase` (see `mi/can-read?`/`can-write?`/`can-create?`)."+ (:require+ [medley.core :as m]+ [metabase-enterprise.serialization.core :as serialization]+ [metabase-enterprise.serialization.schema :as serialization.schema]+ [metabase-enterprise.workspaces.config :as ws.config]+ [metabase-enterprise.workspaces.core :as ws]+ [metabase.api.common :as api]+ [metabase.api.macros :as api.macros]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.models.interface :as mi]+ [metabase.server.streaming-response :as sr]+ [metabase.util.malli.schema :as ms]+ [toucan2.core :as t2]))++(comment serialization.schema/keep-me)++;;; ----------------------------------------------- Schemas ----------------------------------------------------++(def ^:private WorkspaceStatus+ [:enum {:decode/api keyword}+ :unprovisioned :provisioning :provisioned :deprovisioning])++(def ^:private AddDatabaseParams+ [:map {:closed true}+ [:database_id ::lib.schema.id/database]+ [:input_schemas [:sequential ms/NonBlankString]]])++(def ^:private UpdateDatabaseParams+ [:map {:closed true}+ [:input_schemas [:sequential ms/NonBlankString]]])++(def ^:private CreateWorkspaceParams+ [:map {:closed true}+ [:name ms/NonBlankString]])++(def ^:private UpdateWorkspaceParams+ [:map {:closed true}+ [:name {:optional true} ms/NonBlankString]])++(def ^:private WorkspaceDatabaseResponse+ [:map {:closed true}+ [:database_id ::lib.schema.id/database]+ [:input_schemas [:sequential ms/NonBlankString]]+ [:output_namespace :string]+ [:status WorkspaceStatus]])++(def ^:private CreatorResponse+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:first_name [:maybe :string]]+ [:last_name [:maybe :string]]+ [:email ms/NonBlankString]+ [:common_name {:optional true} [:maybe :string]]])++(def ^:private DateTimeWithTimeZone+ "An instant with explicit time-zone info. `Timestamp` would be ambiguous --+ `TIMESTAMP` in most SQL databases is short for `TIMESTAMP WITHOUT TIME ZONE`."+ [:or+ (ms/InstanceOfClass java.time.OffsetDateTime)+ (ms/InstanceOfClass java.time.ZonedDateTime)])++(def ^:private WorkspaceResponse+ [:map {:closed true}+ [:id ms/PositiveInt]+ [:name ms/NonBlankString]+ [:creator [:maybe CreatorResponse]]+ [:created_at DateTimeWithTimeZone]+ [:updated_at DateTimeWithTimeZone]+ ;; `:databases` is only included when hydrated (i.e. the GET /:id endpoint).+ ;; The list endpoint omits it — clients should treat a missing array as `[]`.+ [:databases {:optional true} [:sequential WorkspaceDatabaseResponse]]])++;;; -------------------------------------------- Presentation --------------------------------------------------++(defn- present-workspace-database [wsd]+ (select-keys wsd [:database_id :input_schemas :output_namespace :status]))++(defn- present-creator [creator]+ (when creator+ (select-keys creator [:id :first_name :last_name :email :common_name])))++(defn- present-workspace [workspace]+ (some-> workspace+ (select-keys [:id :name :creator :created_at :updated_at :databases])+ (update :creator present-creator)+ (m/update-existing :databases #(mapv present-workspace-database %))))++;;; ---------------------------------------------- Endpoints ---------------------------------------------------++(api.macros/defendpoint :get "/" :- [:sequential WorkspaceResponse]+ "List all Workspaces."+ []+ ;; Top-level gate: only Data Analysts (and admins) may list. We then apply `mi/can-read?` per row+ ;; for defense in depth — if `can-read?` ever grows tighter rules, the listing will narrow with it+ ;; instead of leaking rows that the per-row check would refuse.+ (api/check-data-analyst)+ (into [] (comp (filter mi/can-read?)+ (map present-workspace))+ (ws/list-workspaces)))++(api.macros/defendpoint :get "/:id" :- WorkspaceResponse+ "Get a single Workspace by id."+ [{:keys [id]} :- [:map [:id ms/PositiveInt]]]+ (api/read-check :model/Workspace id)+ (present-workspace (api/check-404 (ws/get-workspace id))))++(api.macros/defendpoint :post "/" :- WorkspaceResponse+ "Create a new Workspace (name only, no databases)."+ [_route-params _query-params params :- CreateWorkspaceParams]+ (api/create-check :model/Workspace params)+ (present-workspace+ (ws/create-workspace!+ (assoc params :creator_id api/*current-user-id*))))++(api.macros/defendpoint :put "/:id" :- WorkspaceResponse+ "Update a workspace's name."+ [{:keys [id]} :- [:map [:id ms/PositiveInt]]+ _query-params+ params :- UpdateWorkspaceParams]+ (api/write-check :model/Workspace id)+ (when (:name params)+ (t2/update! :model/Workspace :id id {:name (:name params)}))+ (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]]]+ (api/write-check :model/Workspace id)+ (ws/delete-workspace! id)+ {:id id :deleted true})++;;; ---------------------------------------- Database sub-endpoints --------------------------------------------++(api.macros/defendpoint :post "/:id/database" :- WorkspaceResponse+ "Add a database to a workspace and provision it immediately (blocking)."+ [{:keys [id]} :- [:map [:id ms/PositiveInt]]+ _query-params+ params :- AddDatabaseParams]+ (api/create-check :model/WorkspaceDatabase params)+ (present-workspace+ (ws/add-database! id (:database_id params) (:input_schemas params))))++(api.macros/defendpoint :put "/:id/database/:db-id" :- WorkspaceResponse+ "Update a database's input namespaces. Deprovisions the old config and reprovisions+ with the new one (blocking)."+ [{:keys [id db-id]} :- [:map [:id ms/PositiveInt] [:db-id ms/PositiveInt]]+ _query-params+ params :- UpdateDatabaseParams]+ (api/write-check (api/check-404 (t2/select-one :model/WorkspaceDatabase+ :workspace_id id+ :database_id db-id)))+ (present-workspace+ (ws/update-database! id db-id (:input_schemas params))))++(api.macros/defendpoint :delete "/:id/database/:db-id"+ :- WorkspaceResponse+ "Deprovision and remove a database from a workspace (blocking)."+ [{:keys [id db-id]} :- [:map [:id ms/PositiveInt] [:db-id ms/PositiveInt]]]+ (api/write-check (api/check-404 (t2/select-one :model/WorkspaceDatabase+ :workspace_id id+ :database_id db-id)))+ (present-workspace+ (ws/remove-database! id db-id)))++;;; ------------------------------------------- Config download --------------------------------------------------++(api.macros/defendpoint :get "/:id/config"+ :- [:map+ [:status [:= 200]]+ [:headers [:map-of :string :string]]+ [:body :string]]+ "Download the workspace's developer-instance config as a YAML file. 409 if any+ of the workspace's databases is not `:provisioned`."+ [{:keys [id]} :- [:map [:id ms/PositiveInt]]]+ (api/write-check :model/Workspace id)+ (let [config (api/check-404 (ws.config/build-workspace-config id))]+ {:status 200+ :headers {"Content-Type" "application/x-yaml"+ "Content-Disposition" "attachment; filename=\"config.yml\""}+ :body (ws.config/config->yaml config)}))++;;; ----------------------------------------- Metadata export --------------------------------------------------++(defn- workspace-metadata-filters+ "Derive the `:database-ids` and `:schema-ids` filter values from a hydrated workspace.+ `:schema-ids` is a `{db-id [\"schema\" ...]}` map matching the metadata export schema."+ [{:keys [databases]}]+ {:database-ids (mapv :database_id databases)+ :schema-ids (into {}+ (map (fn [{:keys [database_id input_schemas]}]+ [database_id (vec input_schemas)]))+ databases)})++(api.macros/defendpoint :get "/:id/metadata/export"+ :- (sr/streaming-response-schema ::serialization.schema/export-metadata-response)+ "Stream the warehouse metadata (databases, tables, fields) for the workspace's databases,+ scoped to each database's `:input` namespaces. Same flag semantics as+ `/api/ee/serialization/metadata/export` — sections must be opted into via the+ `with-databases` / `with-tables` / `with-fields` query parameters."+ [{:keys [id]} :- [:map [:id ms/PositiveInt]]+ query-params+ :- [:map+ [:with-databases {:default false} [:maybe :boolean]]+ [:with-tables {:default false} [:maybe :boolean]]+ [:with-fields {:default false} [:maybe :boolean]]]]+ (api/read-check :model/Workspace id)+ (let [workspace (api/check-404 (ws/get-workspace id))+ opts (merge query-params+ (workspace-metadata-filters workspace)+ {:user-info {:user-id api/*current-user-id*+ :is-superuser? api/*is-superuser?*}})]+ (sr/streaming-response {:content-type "application/json; charset=utf-8"} [os _]+ (serialization/export-metadata! os opts))))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/config.clj b/enterprise/backend/src/metabase_enterprise/workspaces/config.cljnew file mode 100644index 00000000000..c75fc6093dc--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/config.clj@@ -0,0 +1,104 @@+(ns metabase-enterprise.workspaces.config+ (:require+ [clojure.string :as str]+ [metabase-enterprise.workspaces.models.workspace :as workspace]+ [metabase-enterprise.workspaces.models.workspace-database]+ [metabase.driver.util :as driver.u]+ [metabase.util.yaml :as yaml]+ [toucan2.core :as t2]))++(comment metabase-enterprise.workspaces.models.workspace-database/keep-me)++(defn- schema-filter-entries+ "Build sync-filter keys for the workspace's input scope. Per-engine:++ - schema-having JDBC drivers (Postgres, Redshift, SQL Server)+ emit `:schema-filters-*` — `metabase.driver.sync` reads these to scope+ describe-database to the named schemas.+ - ClickHouse emits `:db-filters-*` — CH calls its top-level namespaces+ 'databases' (= schemas in MB's vocabulary) and its describe-database+ impl reads `:db-filters-*`, not `:schema-filters-*`. The canonical+ CH details already carry `:db-filters-patterns <bound-db>`, which would+ otherwise leak through and restrict sync to the canonical DB the+ workspace user has no GRANTs on.+ - BigQuery emits `:dataset-filters-*` — its `list-datasets` reads these+ instead (BQ doesn't go through `metabase.driver.sync`).+ - No-schema engines (MySQL) emit `{}` — JDBC reports `TABLE_SCHEM` as null+ and `schema-patterns->filter-fn*` documents inclusion patterns NEVER+ match a `nil` schema. Scoping on those is enforced by the connection's+ bound database (`:details.db`)."+ [{:keys [engine] :as db} wsd]+ (let [patterns (str/join "," (:input_schemas wsd))]+ (cond+ (= engine :bigquery-cloud-sdk)+ {:dataset-filters-type "inclusion"+ :dataset-filters-patterns patterns}++ (= engine :clickhouse)+ {:db-filters-type "inclusion"+ :db-filters-patterns patterns}++ (driver.u/supports? engine :schemas db)+ {:schema-filters-type "inclusion"+ :schema-filters-patterns patterns}++ :else+ {})))++(defn- database-entry [wsd db]+ {:name (:name db)+ :engine (:engine db)+ :details (merge (:details db)+ (:database_details wsd)+ (schema-filter-entries db wsd))})++(defn- workspace-database-entry [wsd db]+ ;; Emit the wire shape: `:output_namespace` is a driver-opaque string. The+ ;; loader (`advanced-config.file.workspace/expand-output`) expands it into the+ ;; `{:db ?, :schema ?}` runtime map at boot. Emitting the already-expanded+ ;; shape here makes the round-trip `build -> yaml -> initialize!` fail the+ ;; `::workspace-database-config` spec assertion.+ [(:name db) {:input_schemas (vec (:input_schemas wsd))+ :output_namespace (:output_namespace wsd)}])++(defn build-workspace-config+ "Return a downloadable config.yml-shaped map for `workspace-id`:++ {:version 1+ :config {:databases [...]+ :workspace {:name <ws-name>+ :databases {<db-name> {:input_schemas [<schema-name> ...]+ :output_namespace <ns-string>}}}}}++ Each database entry merges the underlying `metabase_database.details` with the+ WorkspaceDatabase's override credentials and adds `schema-filters-*` keys+ derived from `:input_schemas`. Per-database workspace entries carry plain+ schema-name strings for `:input_schemas` (the 3-slot driver catalog is read+ from `Database.details` at use time, not duplicated on each row), and a+ driver-opaque string for `:output_namespace` (the loader expands it into the+ `{:db ?, :schema ?}` runtime map at boot). Returns nil when the workspace+ does not exist. Throws a 409 `ex-info` if any of the workspace's databases+ is not `:provisioned`."+ [workspace-id]+ (when-let [ws (workspace/get-workspace workspace-id)]+ (let [wsds (:databases ws)]+ (when (some #(not= :provisioned (:status %)) wsds)+ (throw (ex-info "Cannot build config while workspace has databases that are not :provisioned"+ {:status-code 409+ :workspace_id workspace-id})))+ (let [dbs-by-id (if-let [ids (seq (map :database_id wsds))]+ (into {} (map (juxt :id identity))+ (t2/select :model/Database :id [:in ids]))+ {})+ pairs (for [wsd wsds+ :let [db (get dbs-by-id (:database_id wsd))]]+ [wsd db])]+ {:version 1+ :config {:databases (mapv (fn [[wsd db]] (database-entry wsd db)) pairs)+ :workspace {:name (:name ws)+ :databases (into {} (map (fn [[wsd db]] (workspace-database-entry wsd db))) pairs)}}}))))++(defn config->yaml+ "Render a workspace config map as a pretty-printed YAML string."+ [config]+ (yaml/generate-string config :dumper-options {:flow-style :block}))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/core.clj b/enterprise/backend/src/metabase_enterprise/workspaces/core.cljnew file mode 100644index 00000000000..d9514b86645--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/core.clj@@ -0,0 +1,242 @@+(ns metabase-enterprise.workspaces.core+ "Programmatic API for workspaces.++ Two operational surfaces share this namespace:++ - **Manager-side state.** Workspaces and their per-database configs live in the+ `:model/Workspace` and `:model/WorkspaceDatabase` rows. Mutations go through+ [[create-workspace!]], [[add-database!]], [[update-database!]],+ [[remove-database!]], [[delete-workspace!]]. Provisioning operations are+ synchronous — the caller waits until the warehouse work completes.++ - **Instance-side state.** When a Metabase boots in workspace mode, the+ `:workspace` section loader (`metabase-enterprise.advanced-config.file.workspace`)+ parses `config.yml` and stores the resulting workspace map in+ [[workspace-instance-config]]. Workspace-aware code (transform target+ rewriting, table-remapping QP middleware) reads from that atom via+ [[workspace-mode?]] / [[db-workspace-namespace]]. The atom is fresh per process+ — every boot re-reads `config.yml` and replaces the prior value.++ Per-database lifecycle for the manager-side rows:++ provision+ unprovisioned ────────────► provisioning ──────► provisioned+ ▲ │ │+ │ failure │ │ deprovision+ │ ▼ ▼+ │ unprovisioned ◄──── deprovisioning+ │ │+ │ failure │+ │ ▼+ └──────────────────────────────────────── provisioned"+ (:require+ [metabase-enterprise.workspaces.models.workspace :as workspace]+ [metabase-enterprise.workspaces.provisioning :as provisioning]+ [metabase.driver.sql :as driver.sql]+ [metabase.driver.util :as driver.u]+ [metabase.premium-features.core :refer [defenterprise defenterprise-schema]]+ [metabase.workspaces.core :as ws]+ [toucan2.core :as t2]))++(defn engine-namespace-positions+ "Return `{:db ?, :schema ?}` — the values that should populate the `:db` and+ `:schema` AST slots for a Table row in `database`. `table` is optional; pass+ it when you want the schema position derived from the Table's `:schema`+ column (the normal `spec-for-table` case). Pass nil for `table` when you only+ need the `:db` slot (workspace `output_namespace` expansion, GRANT emission).++ `nil` for either slot means \"this driver doesn't emit this AST level.\"+ Empty-string sentinel coercion happens at the storage boundary, not here.++ Driven by [[metabase.driver.sql/table-qualification-style]] ++ [[metabase.driver.sql/db-slot-value]] -- third-party drivers participate by+ implementing those rather than getting a new case branch here."+ ([database] (engine-namespace-positions database nil))+ ([database table]+ (case (driver.sql/table-qualification-style (:engine database))+ :table-qualification-style/table+ {:db nil+ :schema nil}++ :table-qualification-style/schema-table+ {:db nil+ :schema (:schema table)}++ :table-qualification-style/db-table+ {:db (driver.sql/db-slot-value (:engine database) database)+ :schema nil}++ :table-qualification-style/db-schema-table+ {:db (driver.sql/db-slot-value (:engine database) database)+ :schema (:schema table)})))++(defonce ^{:dynamic true+ :doc "The single workspace loaded into this instance from `config.yml`, or nil+ when no workspace was loaded.++ Shape:+ {:name <workspace-name>+ :databases {<database-id> {:input_schemas [<schema-name>, ...]+ :output {:db ?, :schema ?}}+ ...}}++ `:input_schemas` is a vector of driver-opaque schema strings. For 3-slot+ drivers (SQL Server, BigQuery) the warehouse catalog/project is+ derived from the canonical `Database.details` at use time, not duplicated+ on each row.++ `:output` is a `::table-namespace` map — the loader expands the stored+ `output_namespace` string into `{:db (db-position-value db) :schema ns}`+ at boot so QP middleware can read both slots without a per-query case."}+ *workspace-instance-config*+ (atom nil))++(defenterprise-schema set-instance-workspace! :- :any+ "EE impl: install the in-process workspace config atom. Replaces any prior+ value. The shape is validated against `::ws/workspace-instance-config`."+ :feature :none+ [config :- ::ws/workspace-instance-config]+ (reset! *workspace-instance-config* config)+ nil)++(defenterprise clear-instance-workspace!+ "EE impl: clear the in-process workspace config atom."+ :feature :none+ []+ (reset! *workspace-instance-config* nil)+ nil)++(defn instance-workspace+ "Return the workspace loaded on this instance, or nil if none."+ []+ @*workspace-instance-config*)++(defenterprise workspace-mode?+ "EE impl: true iff this instance is running in workspace mode (a `:workspace`+ section was loaded from `config.yml` at boot). Single source of truth for+ gating features that conflict with workspace remapping (DB routing,+ impersonation, writeback, CSV upload, model persistence). Use+ [[db-workspace-namespace]] when you need per-database scoping.++ Deliberately ungated on premium features: a workspace child instance bootstraps+ from `config.yml` *before* its token is installed; if the workspace map is+ loaded, we refuse incompatible features regardless of token state."+ :feature :none+ []+ (some? @*workspace-instance-config*))++(defn db-workspace-namespace+ "Return the workspace-isolated output namespace map for `db-id` on this+ instance, or `nil` when this instance is not running a workspace or the+ workspace has no entry for `db-id`. The namespace map is+ `{:db ?, :schema ?}` - either or both keys may be absent depending on+ the driver's `qualified-name-components`. Reads from the in-process atom+ populated by `config.yml`."+ [db-id]+ (get-in @*workspace-instance-config* [:databases db-id :output]))++(defn list-remappings+ "Return all TableRemapping rows, ordered by id."+ []+ (t2/select :model/TableRemapping {:order-by [[:id :asc]]}))++;;; ------------------------------------- Manager-side helpers ------------------------------------------------++(defn- assert-workspace-exists [workspace-id]+ (or (workspace/get-workspace workspace-id)+ (throw (ex-info "Workspace not found"+ {:status-code 404 :workspace_id workspace-id}))))++(defn- assert-input-schemas-when-supported!+ "Throw 400 if the database supports the `:schemas` feature and `input-schemas` is empty.+ Databases without schemas (e.g. MySQL) accept an empty `input-schemas`."+ [database-id input-schemas]+ (when-let [db (t2/select-one :model/Database :id database-id)]+ (when (and (driver.u/supports? (:engine db) :schemas db)+ (empty? input-schemas))+ (throw (ex-info "input_schemas is required: at least one source schema must be specified"+ {:status-code 400 :database_id database-id})))))++(defn- find-wsd+ "Find the WorkspaceDatabase for a given workspace + database, or throw 404."+ [ws database-id]+ (or (some #(when (= database-id (:database_id %)) %) (:databases ws))+ (throw (ex-info "Database not in workspace"+ {:status-code 404 :workspace_id (:id ws) :database_id database-id}))))++;;; ------------------------------------- Manager-side reads --------------------------------------------------++(defn get-workspace+ "Return the Workspace with the given `id`, hydrated with `:databases` and `:creator`.+ Returns nil if not found."+ [id]+ (workspace/get-workspace id))++(defn list-workspaces+ "Return all Workspaces, each hydrated with `:databases` and `:creator`."+ []+ (workspace/list-workspaces))++;;; ------------------------------------- Manager-side writes -------------------------------------------------++(defn create-workspace!+ "Create a new Workspace (name only, no databases). Returns the created workspace, hydrated."+ [params]+ (workspace/create-workspace! (select-keys params [:name :creator_id])))++(defn add-database!+ "Add a database to a workspace and provision it immediately (blocking).+ `input-schemas` is a vector of driver-opaque schema name strings.+ Returns the updated workspace, hydrated."+ [workspace-id database-id input-schemas]+ (let [ws (assert-workspace-exists workspace-id)]+ (assert-input-schemas-when-supported! database-id input-schemas)+ (when (some #(= database-id (:database_id %)) (:databases ws))+ (throw (ex-info "Database already in workspace"+ {:status-code 409 :workspace_id workspace-id :database_id database-id})))+ (let [wsd-id (t2/insert-returning-pk! :model/WorkspaceDatabase+ {:workspace_id workspace-id+ :database_id database-id+ :input_schemas input-schemas+ :database_details {}+ :output_namespace ""})]+ (try+ (provisioning/provision-single! wsd-id)+ (catch Throwable t+ (t2/delete! :model/WorkspaceDatabase :id wsd-id)+ (throw t)))+ (workspace/get-workspace workspace-id))))++(defn update-database!+ "Update a database's config in a workspace: deprovision the existing one (if provisioned),+ update `input-schemas`, then reprovision (blocking). `input-schemas` is a vector of+ driver-opaque schema name strings. Returns the updated workspace, hydrated."+ [workspace-id database-id input-schemas]+ (let [ws (assert-workspace-exists workspace-id)+ wsd (find-wsd ws database-id)]+ (assert-input-schemas-when-supported! database-id input-schemas)+ (when (= :provisioned (:status wsd))+ (provisioning/deprovision-single! (:id wsd)))+ (t2/update! :model/WorkspaceDatabase {:id (:id wsd)}+ {:input_schemas input-schemas})+ (provisioning/provision-single! (:id wsd))+ (workspace/get-workspace workspace-id)))++(defn remove-database!+ "Deprovision a database (if provisioned) and remove it from the workspace (blocking).+ Returns the updated workspace, hydrated."+ [workspace-id database-id]+ (let [ws (assert-workspace-exists workspace-id)+ wsd (find-wsd ws database-id)]+ (when (= :provisioned (:status wsd))+ (provisioning/deprovision-single! (:id wsd)))+ (t2/delete! :model/WorkspaceDatabase :id (:id wsd))+ (workspace/get-workspace workspace-id)))++(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)))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/init.clj b/enterprise/backend/src/metabase_enterprise/workspaces/init.cljnew file mode 100644index 00000000000..db1fbe6533e--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/init.clj@@ -0,0 +1,18 @@+(ns metabase-enterprise.workspaces.init+ "Loader for the workspaces EE module. Requires namespaces that register+ `defenterprise` overrides so the EE behavior is wired up before sync,+ QP middleware, or model lifecycle hooks run.++ Without this eager load, the OSS `defenterprise` declarations in+ `metabase.workspaces.table-remapping` resolve to identity fallbacks on+ cold-boot sync (the lazy `classloader/require` inside `defenterprise`+ races the registry consult), and isolation-schema tables leak into+ app-db. See `metabase-enterprise.core.init` docstring for the broader+ pattern."+ (:require+ [metabase-enterprise.workspaces.models.workspace]+ [metabase-enterprise.workspaces.models.workspace-database]+ [metabase-enterprise.workspaces.query-processor.middleware]+ [metabase-enterprise.workspaces.settings]+ [metabase-enterprise.workspaces.table-remapping]+ [metabase-enterprise.workspaces.transform-hooks]))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/table_remapping.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/table_remapping.cljnew file mode 100644index 00000000000..10da75bec0f--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/table_remapping.clj@@ -0,0 +1,52 @@+(ns metabase-enterprise.workspaces.models.table-remapping+ "Toucan 2 model for the `table_remapping` table.++ `from_db` / `from_schema` / `to_db` / `to_schema` use `\"\"` as a storage+ sentinel for an absent AST level — Postgres/MySQL/H2 treat NULL as+ not-equal-to-NULL in unique indexes, so the constraint+ `(database_id, from_db, from_schema, from_table_name)` needs non-null+ values. The model coerces nil (and, on writes, missing keys) to `\"\"` so+ app code can pass nil or omit the column. Reads return rows verbatim+ (`\"\"` shows up); callers wanting nil semantics translate locally.++ Inserts and deletes invalidate the QP cache for the affected database —+ Phase 2's SQL rewriter doesn't run on cache hits, so a stale canonical-+ identifier result post-remap would silently breach workspace isolation."+ (:require+ [metabase.cache.core :as cache]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(methodical/defmethod t2/table-name :model/TableRemapping [_model] :table_remapping)++(doto :model/TableRemapping+ (derive :metabase/model)+ (derive :hook/created-at-timestamped?))++(def ^:private sentinel-columns+ "Columns that use `\"\"` as the empty-slot sentinel in storage."+ #{:from_db :from_schema :to_db :to_schema})++(defn- coerce-sentinels+ "Replace nil with `\"\"` on every sentinel column present in `m`. When `fill?`,+ also fill in missing sentinel columns with `\"\"` (used on writes — the unique+ constraint requires non-null values; absent ≠ valid). On selects we leave+ absent columns alone so broad lookups like `:database_id 7` don't gain+ spurious `from_db = \"\"` predicates."+ [fill? m]+ (reduce (fn [acc k]+ (cond+ (and (contains? acc k) (nil? (get acc k))) (assoc acc k "")+ (and fill? (not (contains? acc k))) (assoc acc k "")+ :else acc))+ m+ sentinel-columns))++(t2/define-before-insert :model/TableRemapping [row] (coerce-sentinels true row))+(t2/define-before-update :model/TableRemapping [row] (coerce-sentinels true row))+(t2/define-before-select :model/TableRemapping [query] (update query :kv-args #(coerce-sentinels false %)))++(defn- invalidate! [row] (cache/invalidate-config! {:databases [(:database_id row)]}))++(t2/define-after-insert :model/TableRemapping [row] (invalidate! row) row)+(t2/define-before-delete :model/TableRemapping [row] (invalidate! row))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace.cljnew file mode 100644index 00000000000..b1f56ba54b4--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace.clj@@ -0,0 +1,163 @@+(ns metabase-enterprise.workspaces.models.workspace+ (:require+ [metabase-enterprise.workspaces.models.workspace-database]+ [metabase.api.common :as api]+ [metabase.models.interface :as mi]+ [metabase.permissions.core :as perms]+ [metabase.util :as u]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(comment metabase-enterprise.workspaces.models.workspace-database/keep-me)++(methodical/defmethod t2/table-name :model/Workspace [_model] :workspace)++(doto :model/Workspace+ (derive :metabase/model)+ (derive :hook/timestamped?))++;;; --------------------------------------- Permission predicates ---------------------------------------+;;;+;;; - read: Data Analyst + `mi/can-read?` for every attached WorkspaceDatabase.+;;; - write: Data Analyst + `:perms/workspaces :yes` on any database+;;; + `mi/can-write?` for every attached WorkspaceDatabase.+;;; - create: Data Analyst + `:perms/workspaces :yes` on any database.++(defmethod mi/can-read? :model/Workspace+ ([instance]+ (and (api/is-data-analyst?)+ (every? mi/can-read? (:databases (t2/hydrate instance :databases)))))+ ([_model pk]+ (when-let [ws (t2/select-one :model/Workspace :id pk)]+ (mi/can-read? ws))))++(defmethod mi/can-create? :model/Workspace+ [_model _instance]+ (and (api/is-data-analyst?)+ (perms/has-any-workspaces-permission? api/*current-user-id*)))++(defmethod mi/can-write? :model/Workspace+ ([instance]+ (and (api/is-data-analyst?)+ (perms/has-any-workspaces-permission? api/*current-user-id*)+ (every? mi/can-write? (:databases (t2/hydrate instance :databases)))))+ ([_model pk]+ (when-let [ws (t2/select-one :model/Workspace :id pk)]+ (mi/can-write? ws))))++(methodical/defmethod t2/batched-hydrate [:model/Workspace :databases]+ [_model k workspaces]+ (mi/instances-with-hydrated-data+ workspaces k+ (fn []+ (when-let [ids (seq (map :id workspaces))]+ (group-by :workspace_id+ (t2/select :model/WorkspaceDatabase :workspace_id [:in ids]))))+ :id+ {:default []}))++(methodical/defmethod t2/batched-hydrate [:model/Workspace :creator]+ [_model k workspaces]+ (mi/instances-with-hydrated-data+ workspaces k+ (fn []+ (when-let [ids (seq (distinct (keep :creator_id workspaces)))]+ (u/index-by :id+ (t2/select [:model/User :id :first_name :last_name :email :common_name]+ :id [:in ids]))))+ :creator_id))++(defn list-workspaces+ "Return every Workspace with its `:databases` and `:creator` hydrated."+ []+ (t2/hydrate (t2/select :model/Workspace {:order-by [[:id :asc]]}) :creator :databases))++(defn get-workspace+ "Return the Workspace with the given id and its `:databases` + `:creator` hydrated,+ or nil if none exists."+ [id]+ (when-let [workspace (t2/select-one :model/Workspace :id id)]+ (t2/hydrate workspace :creator :databases)))++(defn get-workspace-by-name+ "Return the Workspace with the given name and its `:databases` + `:creator` hydrated,+ or nil if none exists. Workspace names are not unique at the schema level, so this+ returns the lowest-id match."+ [workspace-name]+ (when-let [workspace (t2/select-one :model/Workspace :name workspace-name {:order-by [[:id :asc]]})]+ (t2/hydrate workspace :creator :databases)))++(defn- with-workspace-database-defaults+ "Fill server-managed columns that are NOT NULL in the DB with their defaults when callers omit them."+ [wsd workspace-id]+ (merge {:database_details {}+ :output_namespace ""}+ wsd+ {:workspace_id workspace-id}))++(defn create-workspace!+ "Create a Workspace and its nested WorkspaceDatabase rows in a single transaction.+ The param map must supply `:creator_id`. Returns the created Workspace with+ `:databases` and `:creator` hydrated."+ [{:keys [name creator_id databases]}]+ (t2/with-transaction [_conn]+ (let [workspace-id (t2/insert-returning-pk! :model/Workspace+ {:name name+ :creator_id creator_id})]+ (when (seq databases)+ (t2/insert! :model/WorkspaceDatabase+ (map #(with-workspace-database-defaults % workspace-id) databases)))+ (get-workspace workspace-id))))++(defn- reject-active-modification!+ "Throw a 409 if any row in `existing-active` (everything other than `:unprovisioned`)+ is missing from the incoming `:databases` list or would have its `:input_schemas` changed.+ Only `:unprovisioned` rows are freely mutable; `:provisioning`, `:provisioned`,+ and `:deprovisioning` rows must be preserved verbatim."+ [existing-active incoming-by-db-id]+ (doseq [{:keys [database_id input_schemas]} existing-active]+ (let [incoming (get incoming-by-db-id database_id)]+ (when (or (nil? incoming)+ (not= (vec input_schemas) (vec (:input_schemas incoming))))+ (throw (ex-info "Cannot modify a workspace_database that is not :unprovisioned"+ {:status-code 409+ :database_id database_id}))))))++(defn delete-workspace!+ "Delete a Workspace. Refuses with a 409 if any of its WorkspaceDatabase rows is in+ a non-`:unprovisioned` state (`:provisioning`, `:provisioned`, or `:deprovisioning`)+ — those point at live warehouse resources and must be unprovisioned explicitly+ first. Cascade-deletes `:unprovisioned` children via the FK."+ [id]+ (when (t2/exists? :model/WorkspaceDatabase+ :workspace_id id+ :status [:not= :unprovisioned])+ (throw (ex-info "Cannot delete a workspace with databases that are not :unprovisioned; deprovision them first"+ {:status-code 409+ :workspace_id id})))+ (t2/delete! :model/Workspace :id id))++(defn update-workspace!+ "Update a Workspace and reconcile its `WorkspaceDatabase` rows with the provided+ list. Only `:unprovisioned` rows are freely mutable. Rows in any other state+ (`:provisioning`, `:provisioned`, `:deprovisioning`) must be preserved by+ `database_id` with matching `:input`, or a 409 is raised. Fields not+ present in `params` are left untouched."+ [id params]+ (t2/with-transaction [_conn]+ (when (contains? params :name)+ (t2/update! :model/Workspace :id id {:name (:name params)}))+ (when (contains? params :databases)+ (let [databases (:databases params)+ existing (t2/select :model/WorkspaceDatabase :workspace_id id)+ active (remove #(= :unprovisioned (:status %)) existing)+ unprovisioned (filter #(= :unprovisioned (:status %)) existing)+ incoming-by-db-id (into {} (map (juxt :database_id identity)) databases)+ preserved-db-ids (set (map :database_id active))]+ (reject-active-modification! active incoming-by-db-id)+ (when-let [to-delete-ids (seq (map :id unprovisioned))]+ (t2/delete! :model/WorkspaceDatabase :id [:in to-delete-ids]))+ (when-let [to-insert (seq (remove #(contains? preserved-db-ids (:database_id %)) databases))]+ (t2/insert! :model/WorkspaceDatabase+ (map #(with-workspace-database-defaults % id) to-insert)))))+ (get-workspace id)))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_database.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_database.cljnew file mode 100644index 00000000000..60fe4fee58e--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_database.clj@@ -0,0 +1,63 @@+(ns metabase-enterprise.workspaces.models.workspace-database+ (:require+ [metabase.api.common :as api]+ [metabase.models.interface :as mi]+ [metabase.permissions.core :as perms]+ [metabase.premium-features.core :refer [defenterprise]]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(methodical/defmethod t2/table-name :model/WorkspaceDatabase [_model] :workspace_database)++(doto :model/WorkspaceDatabase+ (derive :metabase/model)+ (derive :hook/timestamped?))++(t2/deftransforms :model/WorkspaceDatabase+ {:database_details mi/transform-encrypted-json+ :input_schemas mi/transform-json+ :status mi/transform-keyword})++;;; --------------------------------------- Permission predicates ---------------------------------------+;;;+;;; - read: Data Analyst.+;;; - write/create: Data Analyst + `:perms/workspaces :yes` for `:database_id`.++(defmethod mi/can-read? :model/WorkspaceDatabase+ ([_instance]+ (api/is-data-analyst?))+ ([_model _pk]+ (api/is-data-analyst?)))++(defmethod mi/can-write? :model/WorkspaceDatabase+ ([{:keys [database_id]}]+ (and (api/is-data-analyst?)+ (perms/has-db-workspaces-permission? api/*current-user-id* database_id)))+ ([_model pk]+ (when-let [wsd (t2/select-one :model/WorkspaceDatabase :id pk)]+ (mi/can-write? wsd))))++(defmethod mi/can-create? :model/WorkspaceDatabase+ [_model {:keys [database_id]}]+ (and (api/is-data-analyst?)+ (perms/has-db-workspaces-permission? api/*current-user-id* database_id)))++(defenterprise reconcile-workspace-database-refs-before-delete!+ "Enterprise impl of the `:model/Database` before-delete hook. Refuses (409) when any+ non-`:unprovisioned` workspace_database row references `db-id` — anything in a+ `:provisioning`, `:provisioned`, or `:deprovisioning` state points at (or is in+ flight against) live warehouse schemas/users that must be unprovisioned explicitly+ first. Deletes any `:unprovisioned` rows so the `workspace_database.database_id`+ FK RESTRICT doesn't trip on the subsequent Database delete. Deliberately NOT gated+ on a premium feature: a token expiry shouldn't leave workspace_database rows+ un-cleanable, and an active row must refuse the delete regardless of current+ feature state."+ :feature :none+ [db-id]+ (when (t2/exists? :model/WorkspaceDatabase+ :database_id db-id+ :status [:not= :unprovisioned])+ (throw (ex-info "Cannot delete a Database with active workspace_database rows; deprovision them first"+ {:status-code 409+ :database_id db-id})))+ (t2/delete! :model/WorkspaceDatabase :database_id db-id :status :unprovisioned))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.cljnew file mode 100644index 00000000000..dca7d1274f5--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj@@ -0,0 +1,204 @@+(ns metabase-enterprise.workspaces.provisioning+ (:require+ [metabase-enterprise.workspaces.models.workspace-database]+ [metabase-enterprise.workspaces.remapping-cleanup :as ws.remapping-cleanup]+ [metabase.app-db.cluster-lock :as cluster-lock]+ [metabase.driver :as driver]+ [metabase.driver.util :as driver.u]+ [metabase.util.log :as log]+ [potemkin.types :as p]+ [toucan2.connection :as t2.connection]+ [toucan2.core :as t2]))++(comment metabase-enterprise.workspaces.models.workspace-database/keep-me)++;;; ------------------------------------------------- Protocol -------------------------------------------------------++(p/defprotocol+ Provisioner+ "Wrapper around driver workspace-isolation multimethods for testability.+ The default [[dispatching-provisioner]] delegates to the real driver multimethods.+ Tests can reify custom implementations that fail on demand, count calls, etc."+ (init! [this driver database workspace]+ "Create isolated schema + user. Returns {:schema ... :database_details ...}.")+ (grant! [this driver database workspace schemas]+ "Grant read access on `schemas` to the workspace user/role. `schemas` is a+ vector of driver-opaque schema-name strings. 3-slot drivers (SQL Server,+ BigQuery) derive the catalog from `database.details`.")+ (destroy! [this driver database workspace]+ "Tear down isolated schema + user. Should be idempotent."))++(def dispatching-provisioner+ "Default Provisioner that dispatches to the driver multimethods."+ (reify Provisioner+ (init! [_ driver database workspace]+ (driver/init-workspace-isolation! driver database workspace))+ (grant! [_ driver database workspace schemas]+ (driver/grant-workspace-read-access! driver database workspace schemas))+ (destroy! [_ driver database workspace]+ (driver/destroy-workspace-isolation! driver database workspace))))++;;; ---------------------------------------------- Implementation ----------------------------------------------------++(def ^:private ^:const provisioning-lock-timeout-seconds+ "Max wait for another node/thread to finish provisioning the same row."+ 60)++(defn- wsd-lock-key+ "Per-row cluster-lock keyword."+ [workspace-database-id]+ (keyword "metabase-enterprise.workspaces.provisioning"+ (str "wsd-" workspace-database-id)))++;;; ---------------------------------------- Single-database operations -----------------------------------------------++(defn provision-workspace-database!+ "Provision an isolated output schema and user for a `:provisioning`+ WorkspaceDatabase row. State transitions: `:provisioning` -> `:provisioned`+ on success, or back to `:unprovisioned` on failure."+ [workspace-database-id provisioner]+ (try+ (cluster-lock/with-cluster-lock {:lock (wsd-lock-key workspace-database-id)+ :timeout-seconds provisioning-lock-timeout-seconds}+ (let [wsd (t2/select-one :model/WorkspaceDatabase :id workspace-database-id)]+ (when-not wsd+ (throw (ex-info "WorkspaceDatabase not found" {:id workspace-database-id})))+ (when-not (= :provisioning (:status wsd))+ (throw (ex-info "WorkspaceDatabase must be :provisioning to provision"+ {:id workspace-database-id :status (:status wsd)})))+ (let [db (t2/select-one :model/Database :id (:database_id wsd))+ driver (driver.u/database->driver db)+ workspace {:id workspace-database-id :name (str "wsd-" workspace-database-id)}+ init-result (init! provisioner driver db workspace)+ ws-details (merge workspace init-result)+ schemas (vec (:input_schemas wsd))]+ (try+ (grant! provisioner driver db ws-details schemas)+ (catch Throwable t+ (destroy! provisioner driver db ws-details)+ (throw t)))+ (t2/update! :model/WorkspaceDatabase+ {:id workspace-database-id}+ {:output_namespace (:schema init-result)+ :database_details (:database_details init-result)+ :status :provisioned}))))+ (catch Throwable t+ (t2/update! :model/WorkspaceDatabase+ {:id workspace-database-id}+ {:status :unprovisioned})+ (throw t)))+ (t2/select-one :model/WorkspaceDatabase :id workspace-database-id))++(defn deprovision-workspace-database!+ "Reverse provisioning for a `:deprovisioning` WorkspaceDatabase.+ State transitions: `:deprovisioning` -> `:unprovisioned` on success,+ or back to `:provisioned` on failure."+ [workspace-database-id provisioner]+ (try+ (cluster-lock/with-cluster-lock {:lock (wsd-lock-key workspace-database-id)+ :timeout-seconds provisioning-lock-timeout-seconds}+ (let [wsd (t2/select-one :model/WorkspaceDatabase :id workspace-database-id)]+ (when-not wsd+ (throw (ex-info "WorkspaceDatabase not found" {:id workspace-database-id})))+ (when-not (= :deprovisioning (:status wsd))+ (throw (ex-info "WorkspaceDatabase must be :deprovisioning to deprovision"+ {:id workspace-database-id :status (:status wsd)})))+ (let [db (t2/select-one :model/Database :id (:database_id wsd))+ driver (driver.u/database->driver db)+ workspace {:id workspace-database-id+ :name (str "wsd-" workspace-database-id)+ :schema (:output_namespace wsd)+ :database_details (:database_details wsd)}]+ (try+ (destroy! provisioner driver db workspace)+ (finally+ ;; Clear `TableRemapping` rows whose `to_*` matches this workspace's+ ;; iso namespace. Runs in `finally` so a partial warehouse teardown+ ;; (e.g. BQ dataset deleted, SA delete throws) still leaves app-DB+ ;; in a "workspace no longer routes queries" state. Without this,+ ;; future queries against canonical tables would rewrite to an iso+ ;; namespace that no longer exists and 500 in the QP. The unique+ ;; constraint on `(database_id, from_*)` prevents two workspaces on+ ;; the same DB from remapping the same canonical table, so scoping+ ;; by iso namespace alone is correct.+ ;;+ ;; Rebind `*current-connectable*` to nil so the DELETE runs on a+ ;; fresh autocommit connection outside the `with-cluster-lock` tx.+ ;; Otherwise, when `destroy!` throws, the surrounding tx rolls back+ ;; and undoes the DELETE.+ (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 workspace-database-id}+ {:output_namespace ""+ :database_details {}+ :status :unprovisioned}))))+ (catch Throwable t+ (t2/update! :model/WorkspaceDatabase+ {:id workspace-database-id}+ {:status :provisioned})+ (throw t)))+ (t2/select-one :model/WorkspaceDatabase :id workspace-database-id))++;;; ----------------------------------- High-level entry points (blocking) --------------------------------------------++(defn provision-single!+ "Flip a single WorkspaceDatabase to `:provisioning` and provision it synchronously.+ The row must be `:unprovisioned`. Returns the updated WorkspaceDatabase row."+ ([wsd-id]+ (provision-single! wsd-id dispatching-provisioner))+ ([wsd-id provisioner]+ (t2/update! :model/WorkspaceDatabase {:id wsd-id :status :unprovisioned}+ {:status :provisioning})+ (provision-workspace-database! wsd-id provisioner)))++(defn deprovision-single!+ "Flip a single WorkspaceDatabase to `:deprovisioning` and deprovision it synchronously.+ The row must be `:provisioned`. Returns the updated WorkspaceDatabase row."+ ([wsd-id]+ (deprovision-single! wsd-id dispatching-provisioner))+ ([wsd-id provisioner]+ (t2/update! :model/WorkspaceDatabase {:id wsd-id :status :provisioned}+ {:status :deprovisioning})+ (deprovision-workspace-database! wsd-id provisioner)))++(defn provision-workspace!+ "Flip every `:unprovisioned` WorkspaceDatabase under `workspace-id` to+ `:provisioning`, then provision each one synchronously (blocking).+ Returns the number of databases provisioned."+ ([workspace-id]+ (provision-workspace! workspace-id dispatching-provisioner))+ ([workspace-id provisioner]+ (let [triggered (t2/update! :model/WorkspaceDatabase+ {:workspace_id workspace-id :status :unprovisioned}+ {:status :provisioning})]+ (when (pos? triggered)+ (doseq [{:keys [id]} (t2/select [:model/WorkspaceDatabase :id]+ :workspace_id workspace-id+ :status :provisioning)]+ (try+ (provision-workspace-database! id provisioner)+ (catch Throwable t+ (log/warnf t "Failed to provision WorkspaceDatabase %s" id)))))+ triggered)))++(defn deprovision-workspace!+ "Flip every `:provisioned` WorkspaceDatabase under `workspace-id` to+ `:deprovisioning`, then deprovision each one synchronously (blocking).+ Returns the number of databases deprovisioned."+ ([workspace-id]+ (deprovision-workspace! workspace-id dispatching-provisioner))+ ([workspace-id provisioner]+ (let [triggered (t2/update! :model/WorkspaceDatabase+ {:workspace_id workspace-id :status :provisioned}+ {:status :deprovisioning})]+ (when (pos? triggered)+ (doseq [{:keys [id]} (t2/select [:model/WorkspaceDatabase :id]+ :workspace_id workspace-id+ :status :deprovisioning)]+ (try+ (deprovision-workspace-database! id provisioner)+ (catch Throwable t+ (log/warnf t "Failed to deprovision WorkspaceDatabase %s" id)))))+ triggered)))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/query_processor/middleware.clj b/enterprise/backend/src/metabase_enterprise/workspaces/query_processor/middleware.cljindex 6d68ccea4f4..01cf5673166 100644--- a/enterprise/backend/src/metabase_enterprise/workspaces/query_processor/middleware.clj+++ b/enterprise/backend/src/metabase_enterprise/workspaces/query_processor/middleware.clj@@ -1,9 +1,311 @@(ns metabase-enterprise.workspaces.query-processor.middleware+ "QP middleware for workspace table remapping.++ The job: when a query references a canonical production table (e.g. `public.orders`) on a+ workspace child instance, redirect it to the workspace-isolated copy+ (e.g. `ws_alice.public__orders`). This is the security boundary that makes workspace+ isolation real — a silent miss here would let a workspace child read or write production.++ The work is split across **two phases** because there is no single point in the QP pipeline+ where both structured query data AND fully-resolved SQL are available simultaneously:++ ## Phase 1 — [[apply-workspace-remapping]] (preprocess; MBQL metadata mutation)++ Position: preprocessing pipeline (`metabase.query-processor.preprocess`).++ What it does: walks the cached metadata provider, finds each `:metadata/table` whose+ `(:schema, :name)` matches a remapping `from` pair, and mutates `:schema`/`:name` in place+ to the `to` pair. Downstream HoneySQL compilation reads those overridden values and emits+ workspace identifiers directly.++ Why it exists (Phase 2 alone is sufficient for the security guarantee):+ 1. **Pipeline coherence.** Other middleware between preprocess and execute may read+ `:schema`/`:name` off table metadata (sandboxing, permissions checks, cache key+ generation, audit logging). Phase 1 ensures they all see the same identifiers Phase 2+ will emit, preventing subtle bugs where intermediate decisions are made against+ canonical names while the final SQL targets workspace names.+ 2. **Cost is essentially zero.** A few `store-metadata!` calls; no string parsing.++ Phase 1 deliberately **does not touch native SQL**. At this stage native queries may still+ contain unresolved template tags (`{{snippet:foo}}`, `{{#42}}`, `{{date_filter}}`) that+ make the SQL un-parseable by SQLGlot. Any preprocess-time SQL rewrite would be fragile.++ ## Phase 2 — [[apply-workspace-sql-remapping]] (execute; authoritative SQL rewrite)++ Position: execution middleware chain (`metabase.query-processor.execute`), at position 0.+ Runs after every other preprocess step — snippets expanded, card references resolved,+ parameters substituted, MBQL compiled to SQL — so the query is reduced to one canonical+ SQL string with no unresolved bits.++ What it does: parses the SQL via `sql-tools/replace-names` (SQLGlot via GraalPy), walks the+ AST, rewrites every table reference to its workspace counterpart, re-emits.++ This is the **authoritative** rewriter — it covers both MBQL-origin and native-origin+ queries, and it is the only place native SQL is rewritten.++ ## Failure contract: fail closed++ On parse failure Phase 2 throws `ex-info` with `:type qp.error-type/qp`. The query never+ reaches the warehouse. There is no fallback to the original SQL — that would silently+ breach workspace isolation. **Better a loud error than a silent leak to production.**++ ## Short-circuits (escape hatches)++ - `enabled-for-db?` is false (no remappings exist for this DB) — both phases pass through.+ - `ws.remapping/*skip-remapping?*` is bound true — both phases pass through. For+ display-oriented paths that want the user to see the SQL they authored, not the+ rewritten form. Never bound on an execution path.++ ## Cost++ - Phase 1: microseconds (a few in-memory map mutations).+ - Phase 2: ~150ms per query when remappings exist (SQLGlot via GraalPy roundtrip).+ Amortized against query execution time, which is usually >150ms anyway."(:require- [metabase.premium-features.core :refer [defenterprise]]))+ [metabase-enterprise.workspaces.remapping.core :as ws.remapping]+ [metabase-enterprise.workspaces.table-remapping :as ws.table-remapping]+ [metabase.api.common :as api]+ [metabase.driver :as driver]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.permissions.core :as perms]+ [metabase.premium-features.core :refer [defenterprise]]+ [metabase.query-processor.error-type :as qp.error-type]+ [metabase.query-processor.middleware.enterprise :as qp.middleware.enterprise]+ ^{:clj-kondo/ignore [:discouraged-namespace :deprecated-namespace]}+ [metabase.query-processor.store :as qp.store]))++(set! *warn-on-reflection* true)++(defn- table-remapper+ "Build a function that remaps table metadata according to `remappings`.+ The returned fn merges `:db`, `:schema`, and `:name` overrides onto any table whose+ `(:schema, :name)` matches a `from` spec."+ [remappings]+ (let [schema-table-index (into {}+ (map (fn [[from-spec to-spec]]+ (let [{from-schema :schema from-name :table} from-spec+ {to-db :db to-schema :schema to-name :table} to-spec]+ [[(ws.table-remapping/denormalize-level from-schema)+ (ws.table-remapping/denormalize-level from-name)]+ {:db (ws.table-remapping/denormalize-level to-db)+ :schema (ws.table-remapping/denormalize-level to-schema)+ :name to-name}])))+ remappings)]+ (fn [table-metadata]+ (merge table-metadata+ (get schema-table-index [(ws.table-remapping/denormalize-level (:schema table-metadata))+ (ws.table-remapping/denormalize-level (:name table-metadata))])))))++(defn- table-transform+ "Wrap a per-table function `f` into a transform suitable for [[lib.metadata/transforming-metadata-provider]].+ Only applies `f` when the metadata spec's `:lib/type` is `:metadata/table`; all other types pass through."+ [f]+ (fn [{metadata-type :lib/type} results]+ (if (= metadata-type :metadata/table)+ (into [] (map f) results)+ results)))++;;; ------------------------------------------------- Helpers --------------------------------------------------+;;;+;;; SQL-rewrite primitives (`rewrite-sql`, `build-table-replacements`,+;;; `table-spec->sqlglot-key`) live in [[metabase-enterprise.workspaces.table-remapping]]+;;; alongside the `::table-spec` shape they consume. The native-transform exec hook+;;; needs the same primitives outside this QP middleware path.++(defn- install-remapped-metadata-provider!+ "Override `:db`, `:schema`, and `:name` on table metadata in the CachedMetadataProvider+ for each remapping. Downstream HoneySQL compilation will read the overridden values.++ Match key is `(:schema, :name)` — sync doesn't populate `:db` on `:metadata/table`,+ so the canonical from-side never carries it. The `to-spec`'s `:db` IS written when+ populated; the `[:sql :metadata/table]` ->honeysql handler reads it and emits a+ `db.schema.table` (or `db.table`) qualifier. That makes cross-DB workspaces+ (currently MySQL — iso namespace lives in `:db`) routable through Phase 1 without+ needing Phase 2's SQLGlot rewriter to insert a missing qualifier.++ `denormalize-level` collapses storage's `\"\"` sentinel to `nil` on both sides of+ the schema comparison, so a remapping row with `from_schema = \"\"` matches a+ schema-less driver's `:metadata/table.:schema = nil` (and a Postgres remapping+ row with `from_schema = \"public\"` matches the literal value)."+ [mp remappings]+ (let [remapping-mp (lib.metadata/transforming-metadata-provider+ (table-transform (table-remapper remappings))+ mp)]+ (binding [qp.store/*DANGER-allow-replacing-metadata-provider* true]+ ;; this has no body so it looks like this is a no-op. But the with-metadata-provider sets the metadata provider and then+ ;; doesn't pop it. We could use the private function that this uses: qp.store/set-metadata-provider!, or we could use the+ ;; public function `(qp.store/store-miscellaneous-value! ::qp.store/metadata-provider remapping-mp)`. But the former is+ ;; private and the latter is too general. We want this usage to really stand out.+ (qp.store/with-metadata-provider remapping-mp))))++;;; --------------------------------------- Phase 1: Preprocessing (MBQL only) ------------------------------------(defenterprise apply-workspace-remapping- "Dummy implementation — the workspaces feature has been removed."- :feature :workspaces- [query]- query)+ "**Phase 1 — preprocess.** Mutate cached table metadata so every QP middleware between here+ and execute sees workspace identifiers, not canonical ones.++ For each remapping, walks the cached metadata provider, finds the `:metadata/table` that+ matches the `from` (schema, name) pair, and stores it back with `:schema`/`:name` set to+ the `to` pair. Downstream HoneySQL compilation reads the mutated values and emits workspace+ identifiers in the compiled SQL.++ Phase 1 is **not** the security boundary — Phase 2 is. Phase 1 exists for *pipeline+ coherence*: middleware like sandboxing, permission checks, and cache-key generation may+ read `:schema`/`:name` off table metadata and make decisions on them. Without Phase 1+ those decisions would be made against canonical names, which is invisible bug-bait. With+ Phase 1, the whole pipeline sees the same identifiers Phase 2 will emit.++ Native queries are intentionally untouched here — see the namespace docstring for why.++ ## Why `:feature :none` and not `:feature :workspaces`++ Workspace child instances bootstrap from `config.yml` *before* their token is installed+ (see `metabase-enterprise.advanced-config.file/initialize!`). A child whose remap rows+ exist but whose `:workspaces` token isn't yet active must still rewrite reads — otherwise+ the child silently leaks production data. The same rationale documented on+ [[metabase-enterprise.workspaces.transform-hooks/resolve-transform-target]] applies here:+ if remap rows exist, isolation must engage regardless of token state. The internal+ `(ws.remapping/enabled-for-db? db-id)` check is the actual gate."+ :feature :none+ [{db-id :database, mp :lib/metadata, :as query}]+ (if-not (ws.remapping/enabled-for-db? db-id)+ query+ (let [remappings (ws.remapping/remappings-for-db db-id)]+ (if (empty? remappings)+ query+ (do+ (install-remapped-metadata-provider! mp remappings)+ query)))))++;;; ----------------------------- Phase 2: Post-Compilation SQL Rewrite (authoritative) ----------------------------++(defn- rewrite-compiled-map+ "Rewrite table references in a compiled query map ({:query sql, :params [...]}). Parses the+ complete SQL, replaces production schema/table refs with workspace equivalents, re-emits."+ [driver compiled-map remappings]+ (let [sql (:query compiled-map)+ rewritten (ws.table-remapping/rewrite-sql driver sql remappings)]+ (assoc compiled-map :query rewritten)))++(defn- rewrite-stages+ "Recursively walk an MBQL 5 query's `:stages` and rewrite the `:native` SQL on every native+ stage, descending into each join's own `:stages` as well.++ The stage's `:native` is the **source of truth** for native-origin SQL.+ `[[metabase.query-processor.execute/run]]` calls `[[lib/->legacy-MBQL]]` immediately+ before driver dispatch; that conversion rebuilds the legacy top-level `:native` from+ `(get-in query [:stages -1 :native])`. So patching legacy `:native` directly is futile+ -- it gets overwritten. Patch the stage and `lib/->legacy-MBQL` propagates the rewrite+ to the legacy form naturally."+ [driver stages remappings]+ (mapv (fn [stage]+ (cond-> stage+ (and (= :mbql.stage/native (:lib/type stage))+ (string? (:native stage)))+ (update :native #(ws.table-remapping/rewrite-sql driver % remappings))++ (seq (:joins stage))+ (update :joins+ (fn [joins]+ (mapv (fn [join]+ (cond-> join+ (seq (:stages join))+ (update :stages #(rewrite-stages driver % remappings))))+ joins)))))+ stages))++(defenterprise apply-workspace-sql-remapping+ "**Phase 2 — execute (post-compilation).** The authoritative SQL rewriter and the security+ boundary for workspace isolation.++ Runs in the execution middleware chain after every other preprocess step — snippets+ expanded, card refs resolved, parameters substituted, MBQL compiled to SQL. By this point+ the query is reduced to one canonical SQL string with no unresolved template syntax.++ Parses that SQL via `sql-tools/replace-names` (SQLGlot via GraalPy), walks the AST, and+ rewrites every `from` schema/table reference to its `to` counterpart. Re-emits.++ ## SQL carrier hierarchy (read this before changing what gets patched)++ Native SQL has three carriers in the query map and only one is the source of truth:++ - `(get-in query [:stages -1 :native])` — **source of truth**. Set by preprocess+ (`substitute-native-parameters*`); read by `compile*` and by `lib/->legacy-MBQL`.+ - `(:qp/compiled query :query)` — compile-time snapshot taken from the stage by+ `compile*`. Not re-derived at execute time. Read by+ `add-native-form-to-result-metadata` to produce the user-facing `:native_form`,+ and by the `(not (:native query)) (assoc :native (:qp/compiled query))` branch+ in `qp.execute/run` for MBQL-origin queries (where the stage isn't native).+ - `(:native query :query)` — legacy top-level. **Always re-derived** by+ `lib/->legacy-MBQL` in `qp.execute/run` from `(get-in query [:stages -1 :native])`.+ JDBC reads from this carrier. Patching it directly is futile: the rebuild from+ the stage clobbers any patch.++ Phase 2 patches **stage `:native` and `:qp/compiled`**, in that order. The stage patch+ is the security boundary (flows through `lib/->legacy-MBQL` to legacy `:native` to+ JDBC). The `:qp/compiled` patch keeps the user-visible `:native_form` consistent with+ what hits the warehouse and covers the MBQL-origin path through `qp.execute/run`'s+ rename branch.++ Runs unconditionally for queries against remapped databases — MBQL-origin and native-origin+ alike. This is the only place native SQL is rewritten.++ **Failure contract: fail closed.** On parse failure (via `rewrite-sql`) throws `ex-info`+ with `:type qp.error-type/qp`. The query does not execute. There is deliberately no+ fallback to the original SQL — a silent pass-through would breach workspace isolation.++ Short-circuits when `enabled-for-db?` is false (no remappings) or+ `ws.remapping/*skip-remapping?*` is bound true.++ ## Why `:feature :none` and not `:feature :workspaces`++ See the rationale on [[apply-workspace-remapping]]. Same answer: workspace children+ must rewrite reads even when the `:workspaces` token isn't active (boot sequence,+ token expiry, etc.). The internal `(enabled-for-db? db-id)` is the gate."+ :feature :none+ [qp]+ (fn [{:keys [database] :as query} rff]+ (cond+ (not (ws.remapping/enabled-for-db? database))+ (qp query rff)++ ;; Tier B fail-closed: workspace remap is incompatible with DB routing+ ;; (which swaps the destination connection — see GHY-3484 audit conflict+ ;; notes) and connection impersonation (which binds a warehouse role that+ ;; almost certainly lacks GRANT on workspace schemas).+ (qp.middleware.enterprise/currently-db-routed?)+ (throw (ex-info "Database routing is incompatible with workspace table remapping"+ {:type qp.error-type/qp :database-id database}))++ ;; impersonation-enforced-for-db? throws when no current user is bound+ ;; (sync/transform contexts). Skip the check there; impersonation can't+ ;; engage without a user.+ (and api/*current-user-id*+ (perms/impersonation-enforced-for-db? {:id database}))+ (throw (ex-info "Connection impersonation is incompatible with workspace table remapping"+ {:type qp.error-type/qp :database-id database}))++ :else+ (let [remappings (ws.remapping/remappings-for-db database)]+ (if (empty? remappings)+ (qp query rff)+ (let [driver driver/*driver*+ rewrite #(rewrite-compiled-map driver % remappings)+ query (cond-> query+ ;; Stage `:native` is the source of truth for native-origin+ ;; SQL. `lib/->legacy-MBQL` (in `qp.execute/run`) rebuilds the+ ;; legacy top-level `:native` from `[:stages -1 :native]`, so+ ;; this is the patch that actually reaches the warehouse.+ (:lib/type query)+ (update :stages #(rewrite-stages driver % remappings))++ ;; `:qp/compiled` is a compile-time snapshot, not re-derived+ ;; at execute time. Read by `add-native-form-to-result-metadata`+ ;; for the user-facing `:native_form`, and by the rename branch+ ;; in `qp.execute/run` when the stage isn't native (MBQL-origin).+ (:qp/compiled query)+ (update :qp/compiled rewrite)++ (:qp/compiled-inline query)+ (update :qp/compiled-inline rewrite))]+ (qp query rff)))))))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/remapping/core.clj b/enterprise/backend/src/metabase_enterprise/workspaces/remapping/core.cljnew file mode 100644index 00000000000..e308716d8bd--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/remapping/core.clj@@ -0,0 +1,216 @@+(ns metabase-enterprise.workspaces.remapping.core+ "Data-access layer for workspace table remappings.++ Access methods are defined as a protocol ([[RemappingStore]]). The default implementation+ reads from the `table_remapping` app-DB table via Toucan2. Tests can bind+ [[*remapping-store*]] with a simple map-backed implementation to avoid DB access."+ (:require+ [metabase-enterprise.workspaces.models.table-remapping]+ [metabase.app-db.core :as app-db]+ [toucan2.core :as t2]))++(comment metabase-enterprise.workspaces.models.table-remapping/keep-me)++(set! *warn-on-reflection* true)++;;; ------------------------------------------------- Protocol --------------------------------------------------++(defprotocol RemappingStore+ "Protocol for accessing workspace table remappings.++ Reads are pure functions of `db-id`. Writes take pre-normalized `::table-spec` maps+ (`{:db, :schema, :table}`) — callers should normalize `nil`/missing slots to the+ empty-string sentinel before invoking the store. The convenience writers in+ [[metabase-enterprise.workspaces.table-remapping]] do this normalization.++ Writes are idempotent on the `(database_id, from_db, from_schema, from_table_name)`+ unique key: duplicate inserts no-op, missing removes return 0."+ ;; reads+ (enabled-for-db?* [store db-id]+ "Returns true if `db-id` has any active workspace remappings.")+ (remappings-for-db* [store db-id]+ "Returns remappings for `db-id` as `{from-spec to-spec, ...}` where each value is a+ `::table-spec` map (`{:db, :schema, :table}`).++ Empty-string `\"\"` in `:db` / `:schema` is the sentinel for \"this driver doesn't+ emit this level\" — pruned before being handed to SQLGlot.")+ (get-mapping* [store db-id from-spec]+ "Targeted lookup: return the to-side `::table-spec` for `from-spec`, or nil if no+ matching remap exists. AppDB impl uses an indexed select-one (matches the unique+ constraint on `(database_id, from_db, from_schema, from_table_name)`); MapStore+ does a hash-map get. Hot path -- called per-table during sync, so stays O(1) per call.")+ ;; writes+ (insert-mapping!* [store db-id from-spec to-spec]+ "Idempotently ensure a remapping exists. `from-spec` and `to-spec` are pre-normalized+ `::table-spec` maps. Returns truthy when the remapping is in place after the call+ (whether this call inserted it or it already existed). Handles the concurrent-insert+ race portably -- callers don't see SQL state codes. Spec slots must be all-string;+ use `\"\"` as the sentinel for slots a driver doesn't emit (nil slots are a contract+ violation).")+ (remove-mapping!* [store db-id from-spec]+ "Remove a single remapping by source `from-spec`. Returns the number of rows removed+ (0 or 1) as a long.")+ (clear-for-db!* [store db-id]+ "Remove all remappings for `db-id`. Returns the number of rows removed as a long."))++;;; -------------------------------------------- App-DB Implementation ---------------------------------------------++(defn- row->spec-pair+ "Project a `:model/TableRemapping` row into a `[from-spec to-spec]` pair."+ [row]+ [{:db (:from_db row) :schema (:from_schema row) :table (:from_table_name row)}+ {:db (:to_db row) :schema (:to_schema row) :table (:to_table_name row)}])++(defrecord AppDBRemappingStore []+ RemappingStore+ (enabled-for-db?* [_ db-id]+ (t2/exists? :model/TableRemapping :database_id db-id))+ (remappings-for-db* [_ db-id]+ (into {} (map row->spec-pair) (t2/select :model/TableRemapping :database_id db-id)))+ (get-mapping* [_ db-id {:keys [db schema table]}]+ (when-let [row (t2/select-one :model/TableRemapping+ :database_id db-id+ :from_db db+ :from_schema schema+ :from_table_name table)]+ {:db (:to_db row) :schema (:to_schema row) :table (:to_table_name row)}))+ (insert-mapping!* [_ db-id from-spec to-spec]+ ;; Idempotent on the unique constraint `(database_id, from_db, from_schema, from_table_name)`.+ ;; `app-db/update-or-insert!` handles the concurrent-insert race portably -- if a duplicate+ ;; insert loses the race, it retries the lookup and returns the existing row's id. The+ ;; update-fn returns nil on existing (no-op).+ (app-db/update-or-insert!+ :model/TableRemapping+ {:database_id db-id+ :from_db (:db from-spec)+ :from_schema (:schema from-spec)+ :from_table_name (:table from-spec)}+ (fn [existing]+ (when-not existing+ {:to_db (:db to-spec)+ :to_schema (:schema to-spec)+ :to_table_name (:table to-spec)}))))+ (remove-mapping!* [_ db-id {:keys [db schema table]}]+ (long+ (t2/delete! :model/TableRemapping+ :database_id db-id+ :from_db db+ :from_schema schema+ :from_table_name table)))+ (clear-for-db!* [_ db-id]+ (long (t2/delete! :model/TableRemapping :database_id db-id))))++(def app-db-store+ "Default remapping store backed by the `table_remapping` app-DB table."+ (->AppDBRemappingStore))++;;; ----------------------------------------- Map-backed Implementation --------------------------------------------++(defrecord MapRemappingStore [!mappings]+ ;; !mappings is an atom holding {db-id {from-spec to-spec, ...}}.+ ;; Atom-backed so test code can write through `insert-mapping!*` / `remove-mapping!*`+ ;; without going through the app DB.+ ;;+ ;; Specs MUST be `{:db, :schema, :table}` maps. The empty-string sentinel `""` is the+ ;; only allowed value for slots a driver doesn't emit -- the canonical shape across the+ ;; whole codebase. This impl does no normalization; tests get the same shape contract+ ;; as production.+ RemappingStore+ (enabled-for-db?* [_ db-id]+ (boolean (seq (get @!mappings db-id))))+ (remappings-for-db* [_ db-id]+ (get @!mappings db-id {}))+ (get-mapping* [_ db-id from-spec]+ (get-in @!mappings [db-id from-spec]))+ (insert-mapping!* [_ db-id from-spec to-spec]+ ;; Idempotent: if `from-spec` already maps to anything, leave it alone (matches+ ;; `app-db/update-or-insert!`'s "no update" semantics). Either way return the pair+ ;; that's now in place so the caller sees a truthy result.+ (when-not (contains? (get @!mappings db-id {}) from-spec)+ (swap! !mappings update db-id (fnil assoc {}) from-spec to-spec))+ [from-spec (get-in @!mappings [db-id from-spec])])+ (remove-mapping!* [_ db-id from-spec]+ (if (contains? (get @!mappings db-id {}) from-spec)+ (do (swap! !mappings update db-id dissoc from-spec)+ 1)+ 0))+ (clear-for-db!* [_ db-id]+ (let [n (count (get @!mappings db-id {}))]+ (swap! !mappings dissoc db-id)+ n)))++(defn map-store+ "Create a mutable map-backed remapping store. For testing -- supports both reads and writes+ without app-DB access. **Single-threaded; not safe for concurrent writers** (the store+ reads-then-swaps to compute return values; multiple writers could race). Tests are+ single-threaded so this is fine; do not reach for this in production.++ `initial-mappings` is the seed value, consumed-into-an-atom (not held by reference).+ Specs MUST be `::table-spec` maps `{:db :schema :table}` with `\"\"` as the sentinel+ for slots a driver doesn't emit. This is the canonical shape used everywhere.++ Examples:+ (map-store {})+ (map-store {1 {{:db \"\" :schema \"public\" :table \"orders\"}+ {:db \"\" :schema \"mb_iso\" :table \"orders\"}}})+ (map-store {1 {{:db \"proj\" :schema \"ds\" :table \"orders\"}+ {:db \"proj\" :schema \"ws_ds\" :table \"orders\"}}}) ;; BigQuery"+ [initial-mappings]+ (->MapRemappingStore (atom initial-mappings)))++;;; ------------------------------------------------ Dynamic vars -------------------------------------------------++(def ^:dynamic *remapping-store*+ "The active remapping store. Defaults to [[app-db-store]]. Bind in tests to avoid DB access."+ app-db-store)++(def ^:dynamic *skip-remapping?*+ "When true, [[enabled-for-db?]] always returns false. Used by workspace execution contexts+ that need to query the isolation schema directly without remapping."+ false)++;;; ------------------------------------------------ Public API ---------------------------------------------------++(defn enabled-for-db?+ "Returns true if database `db-id` has any active workspace remappings.+ Returns false when [[*skip-remapping?*]] is true."+ [db-id]+ (and (not *skip-remapping?*)+ (enabled-for-db?* *remapping-store* db-id)))++(defn remappings-for-db+ "Returns a map of remappings for `db-id`:++ {from-spec to-spec, ...}++ where each spec is a `::table-spec` map `{:db :schema :table}`. Empty-string `\"\"`+ in `:db`/`:schema` is the sentinel for \"this driver doesn't emit this level.\"++ Returns empty map if no remappings exist."+ [db-id]+ (remappings-for-db* *remapping-store* db-id))++(defn get-mapping+ "Targeted lookup: return the to-side `::table-spec` for `from-spec`, or nil if no+ matching remap exists. Hot path during sync -- O(1) per call."+ [db-id from-spec]+ (get-mapping* *remapping-store* db-id from-spec))++(defn insert-mapping!+ "Idempotently ensure a remapping exists in the active store. Specs MUST be+ pre-normalized `::table-spec` maps — `\"\"` (not nil) for slots a driver doesn't+ emit. The user-facing wrappers in `table-remapping` handle that normalization.++ Returns truthy when the remapping is in place after the call (insert or already-existed)."+ [db-id from-spec to-spec]+ (insert-mapping!* *remapping-store* db-id from-spec to-spec))++(defn remove-mapping!+ "Remove a single remapping via the active store. Returns the number of rows removed."+ [db-id from-spec]+ (remove-mapping!* *remapping-store* db-id from-spec))++(defn clear-for-db!+ "Remove all remappings for `db-id` via the active store. Returns the number of rows removed."+ [db-id]+ (clear-for-db!* *remapping-store* db-id))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/remapping_cleanup.clj b/enterprise/backend/src/metabase_enterprise/workspaces/remapping_cleanup.cljnew file mode 100644index 00000000000..f6521f56240--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/remapping_cleanup.clj@@ -0,0 +1,45 @@+(ns metabase-enterprise.workspaces.remapping-cleanup+ "Lifecycle hook: clear `TableRemapping` rows when a `WorkspaceDatabase` is+ deprovisioned.++ Lives as a leaf to break a `provisioning` -> `table-remapping` -> `core` ->+ `provisioning` require cycle: the deprovision path needs to delete remapping+ rows scoped to the workspace's iso namespace, but `table-remapping` reaches+ back through `workspaces.core` which loads `provisioning`. The `:db`-slot+ derivation goes through [[metabase.driver.sql/db-slot-value]] so this ns+ has no workspace deps beyond the model registration."+ (:require+ [metabase-enterprise.workspaces.models.table-remapping]+ [metabase.driver.sql :as driver.sql]+ [toucan2.core :as t2]))++(comment metabase-enterprise.workspaces.models.table-remapping/keep-me)++(defn- iso-db-slot+ "Value of the `:db` AST slot a `TableRemapping.to_db` carries for `database`'s+ engine. Empty string for drivers that don't populate the `:db` slot+ (Postgres, Redshift, H2, ClickHouse) — the storage sentinel. For drivers+ that do (MySQL, SQL Server, BigQuery), consults+ [[metabase.driver.sql/db-slot-value]]."+ [database]+ (or (driver.sql/db-slot-value (:engine database) database) ""))++(defn clear-mappings-for-iso!+ "Delete every `TableRemapping` row on `database`'s id whose `to_*` slots match+ the iso namespace `(iso-db, iso-schema)` for this workspace_database row.++ Idempotent — 0 rows deleted is a valid outcome when nothing was registered+ (e.g. a workspace_database that was provisioned but never had a transform run).++ Scope rationale: the unique constraint on `(database_id, from_db, from_schema,+ from_table_name)` prevents two workspaces on the same metabase_database from+ remapping the same canonical table. So deleting by iso namespace is enough+ to avoid clobbering another workspace's rows on the same database_id.++ Returns the count of rows deleted."+ [database database-id output-namespace]+ (long+ (t2/delete! :model/TableRemapping+ :database_id database-id+ :to_db (iso-db-slot database)+ :to_schema (or output-namespace ""))))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/settings.clj b/enterprise/backend/src/metabase_enterprise/workspaces/settings.cljnew file mode 100644index 00000000000..0bb4c9320bb--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/settings.clj@@ -0,0 +1,19 @@+(ns metabase-enterprise.workspaces.settings+ (:require+ [metabase-enterprise.workspaces.core :as ws]+ [metabase.settings.core :refer [defsetting]]+ [metabase.util.i18n :refer [deferred-tru]]))++(def keep-me+ "Marker so callers can `(comment ...keep-me)` to retain the require that registers the settings in this ns."+ nil)++(defsetting has-active-workspace+ (deferred-tru "True if a workspace is loaded on this instance from a config.yml :workspace section. Read-only projection of the in-process atom populated by the boot-time loader; false on parent and unconfigured instances.")+ :type :boolean+ :visibility :admin+ :export? false+ :setter :none+ :getter (fn [] (some? (ws/instance-workspace)))+ :audit :never+ :doc false)diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/table_remapping.clj b/enterprise/backend/src/metabase_enterprise/workspaces/table_remapping.cljnew file mode 100644index 00000000000..77f746a546b--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/table_remapping.clj@@ -0,0 +1,655 @@+(ns metabase-enterprise.workspaces.table-remapping+ "Internal API for table-to-table remapping. Used by workspace isolation to redirect+ queries from production tables to workspace tables.++ A remapping is a single row in the app-db `table_remapping` table, consulted by the+ query processor middleware at query time.++ ## Public writers++ - [[add-mapping!]] — the writer. Takes `db-id` and two `::table-spec` maps+ (`{:db, :schema, :table}`). Idempotent on the unique constraint.+ - [[add-transform-target-mapping!]] — transform-hook integration for rewriting a+ transform target to a workspace schema. Resolves the workspace output schema from+ the database's provisioned `WorkspaceDatabase` row and delegates to [[add-mapping!]].++ Callers with `:model/Database` and `:model/Table` rows can produce a `::table-spec`+ via [[spec-for-table]] and hand it to [[add-mapping!]] directly.++ ## Three-level identifiers++ `:model/TableRemapping` rows have three identifier columns per side: `db`, `schema`,+ `table_name`. Slot names are SQLGlot **AST positions**, not warehouse vocabulary:+ `:db` ↔ `Table.catalog`, `:schema` ↔ `Table.db`, `:table` ↔ `Table.name`. A driver+ populates a slot iff its [[metabase.driver/qualified-name-components]] includes it.++ | Driver | db | schema | table_name |+ |-------------------------------------|-------------|--------------|------------|+ | Postgres / Redshift / H2 | \"\" | schema | table |+ | MySQL | \"\" | \"\" | table |+ | ClickHouse | \"\" | db-name | table |+ | SQL Server | database | schema | table |+ | BigQuery | project | dataset | table |+ | Mongo | \"\" | \"\" | collection |++ ClickHouse calls its top level a \"database\" but it lives at AST `:schema` because+ that's where it appears in compiled SQL (`db.table`). The slot name follows the AST+ position, not what the warehouse vendor calls the level.++ Empty string (`\"\"`) is the sentinel for \"this driver does not emit this level.\"+ Empty string is preferred over `nil` because Postgres/MySQL/H2 treat NULL as+ not-equal-to-NULL in unique indexes — using `\"\"` keeps the+ `(database_id, db, schema, table_name)` unique constraint enforceable across DBs.+ The QP rewriter filters `\"\"` out before handing keys to SQLGlot.++ [[spec-for-table]] uses [[metabase.driver/qualified-name-components]] to fill the+ right slots from a `:model/Database` and `:model/Table` row."+ (:require+ [clojure.set :as set]+ [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.models.table-remapping]+ [metabase-enterprise.workspaces.remapping.core :as ws.remapping]+ [metabase.driver :as driver]+ [metabase.driver.connection :as driver.conn]+ [metabase.premium-features.core :refer [defenterprise]]+ [metabase.query-processor.error-type :as qp.error-type]+ [metabase.sql-tools.core :as sql-tools]+ [metabase.util :as u]+ [metabase.util.malli :as mu]+ [metabase.util.malli.registry :as mr]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++;;; ----------------------------------------- Schemas + sentinel -----------------------------------------++(def ^:private no-level+ "Sentinel string stored in `from_db` / `from_schema` / `to_db` / `to_schema` when the+ driver does not emit that identifier level. See namespace docstring."+ "")++(defn- normalize-level+ "Coerce a possibly-nil identifier-level value to the empty-string sentinel."+ [v]+ (or v no-level))++(mr/def ::table-spec+ "A driver-resolved table identifier. `:table` is required; `:schema` and `:db` default+ to the empty-string sentinel when absent. Produced by [[spec-for-table]] and consumed+ by [[add-mapping!]]."+ [:map+ [:table :string]+ [:schema {:optional true} [:maybe :string]]+ [:db {:optional true} [:maybe :string]]])++;;; ---------------------------------------- Driver-aware resolution ----------------------------------------++(defn db-position-value+ "Value to put in the `:db` slot for `database`. Convenience accessor over+ [[ws/engine-namespace-positions]]. Only meaningful for drivers whose+ `qualified-name-components` includes `:db`."+ [database]+ (:db (ws/engine-namespace-positions database)))++(mu/defn spec-for-table :- ::table-spec+ "Return `{:db, :schema, :table}` for `table` in `database`, populating only the+ identifier positions the driver emits per [[metabase.driver/qualified-name-components]].+ Absent positions are normalized to the empty-string sentinel.++ `database` is a `:model/Database` row (must have `:engine`, `:name`, `:details`).+ `table` is a `:model/Table` row (must have `:name` and `:schema`).++ (spec-for-table {:engine :postgres, :name \"db\"} {:name \"orders\", :schema \"public\"})+ ;; => {:db \"\", :schema \"public\", :table \"orders\"}++ (spec-for-table {:engine :clickhouse, :name \"analytics\"} {:name \"orders\", :schema nil})+ ;; => {:db \"\", :schema \"analytics\", :table \"orders\"}++ (spec-for-table {:engine :bigquery-cloud-sdk, :details {:project-id \"p\"}}+ {:name \"orders\", :schema \"ds\"})+ ;; => {:db \"p\", :schema \"ds\", :table \"orders\"}"+ [database :- [:map [:engine :keyword]]+ table :- [:map [:name :string]]]+ (let [components (set (driver/qualified-name-components (:engine database)))+ positions (ws/engine-namespace-positions database table)]+ {:db (normalize-level (when (:db components) (:db positions)))+ :schema (normalize-level (when (:schema components) (:schema positions)))+ :table (:name table)}))++(defn- short-hash+ "Short, stable, identifier-safe hash of `s`. Used as a disambiguating suffix+ when [[remapped-table-name]] must truncate to fit a driver's identifier limit.++ Uses base36 (`0-9a-z`) over the leading bytes of SHA-1: ~5.17 bits per character,+ versus hex's 4. Eight base36 chars carry ~41 bits of entropy; 2^41 inputs are+ needed for a ~50% birthday collision, which is well past any realistic per-database+ table count. The alphabet is unquoted-identifier-safe on every supported warehouse+ (base64 / base64-url include `+`, `/`, or `-` which are not)."+ [^String s]+ (let [md (java.security.MessageDigest/getInstance "SHA-1")+ bytes (.digest md (.getBytes s "UTF-8"))+ ;; First 8 bytes -> non-negative BigInteger (sign byte = 1).+ bi (java.math.BigInteger. 1 ^bytes (java.util.Arrays/copyOfRange bytes 0 8))+ b36 (.toString bi 36)+ padded (str "00000000" b36)]+ ;; Take the rightmost 8 chars so leading-zero values still produce 8 chars.+ (subs padded (- (count padded) 8))))++(defn remapped-table-name-with-limit+ "Implementation of [[remapped-table-name]] taking an explicit `max-bytes`. Exposed so+ tests can exercise the truncation path with a small limit without driver dispatch.+ Production callers should use [[remapped-table-name]] which derives `max-bytes` from+ the driver. Counts and truncates by UTF-8 *bytes*, not characters -- Postgres et al.+ measure their identifier limits in bytes, and a multi-byte schema name (emoji,+ kanji) would overflow under naive char-based truncation. Truncation is codepoint-safe:+ we never split a multi-byte char in the middle."+ [from-spec max-bytes]+ (let [{:keys [schema table]} from-spec+ raw (str (or schema "") "__" table)+ new-table (if (<= (u/string-byte-count raw) max-bytes)+ raw+ (let [suffix (str "_" (short-hash raw))+ head-room (max 0 (- max-bytes (u/string-byte-count suffix)))]+ (str (u/truncate-string-to-byte-count raw head-room) suffix)))]+ (assoc from-spec :table new-table)))++(mu/defn remapped-table-name :- ::table-spec+ "Rewrite `:table` of `from-spec` into a deterministic, collision-resistant+ name suitable for the workspace schema on `driver`. Concatenates+ `:schema` + '__' + `:table` so two source tables with the same `:table`+ under different `:schema` values land at distinct identifiers in the+ single workspace schema (e.g. `schemaA.orders` and `schemaB.orders`+ become `schemaA__orders` and `schemaB__orders`).++ Honors `(driver/table-name-length-limit driver)` -- when the+ concatenation exceeds the limit (measured in UTF-8 bytes, since that is+ what every supported warehouse counts), truncates and appends an 8-char+ base36 SHA-1 suffix of the full original concatenation so distinct+ inputs stay distinct after truncation. Truncation is codepoint-safe.+ Leftward slots (`:db`, `:schema`) are not modified here; callers+ `assoc` the workspace schema themselves.++ Note: MySQL has no schema dimension (`qualified-name-components` is `[]`),+ so cross-schema collisions cannot occur there. The function still runs and+ produces `\"__<table>\"` (empty prefix) for consistency across drivers."+ [driver :- :keyword+ from-spec :- ::table-spec]+ (remapped-table-name-with-limit+ from-spec+ (or (driver/table-name-length-limit driver) Integer/MAX_VALUE)))++;;; ---------------------------------- canonical / isolated translators ----------------------------------++(mu/defn canonical->isolated :- [:maybe ::table-spec]+ "Look up the isolated `::table-spec` for canonical `table` in `remappings`. Returns+ nil when no remap exists.++ Driver-aware projection: only the slots `(qualified-name-components driver)`+ plus `:table` participate in the match. Slots the driver doesn't emit (e.g. `:db`+ on Postgres) are ignored even if present on `table` or on the remapping keys, so+ the empty-string sentinel can't cause a false miss."+ [driver :- :keyword+ remappings :- [:map-of ::table-spec ::table-spec]+ table :- ::table-spec]+ (let [target-keys (into [:table] (driver/qualified-name-components driver))+ driver-remappings (update-keys remappings #(select-keys % target-keys))]+ (get driver-remappings (select-keys table target-keys))))++(mu/defn isolated->canonical :- [:maybe ::table-spec]+ "Inverse of [[canonical->isolated]]: look up the canonical `::table-spec` for an+ isolated `table` in `remappings`."+ [driver :- :keyword+ remappings :- [:map-of ::table-spec ::table-spec]+ table :- ::table-spec]+ (canonical->isolated driver (set/map-invert remappings) table))++;;; -------------------------------------------- Read API --------------------------------------------+;;;+;;; All reads route through the active store ([[ws.remapping/*remapping-store*]]) so tests+;;; can swap in a [[ws.remapping/map-store]] without app-DB access. The store returns+;;; `{from-spec to-spec}` maps; consumer-facing helpers below project them into the+;;; `:model/Table`-row vocabulary (`{:db :schema :name}` with nil sentinels) where needed.++(defn denormalize-level+ "Inverse of `normalize-level`: `\"\"` (the storage sentinel meaning \"this driver+ doesn't emit this level\") becomes `nil` (what `:model/Table` rows actually carry+ for the same level). Anything else passes through unchanged.++ Public so consumers outside the storage layer (the QP middleware) can apply the+ same `\"\"`/`nil` equivalence without redefining it."+ [v]+ (when-not (or (nil? v) (= no-level v)) v))++(defn prune-no-level+ "Remove map keys whose values are the storage `\"\"` sentinel. Used by callers+ that need to drop absent slots before passing the map to a downstream tool+ (e.g. SQLGlot's `replace-names` matcher, which treats absent keys as wildcards+ but would match an empty string literally)."+ [m]+ (into {} (remove (fn [[_ v]] (= no-level v))) m))++;;; ----------------------------------------- SQL rewriting -----------------------------------------+;;;+;;; Pure SQL rewrite primitives keyed off `::table-spec`. Pure relative to a `{from-spec to-spec}`+;;; remappings map -- fetched from the active store and passed in. Shared between the QP+;;; middleware Phase 2 rewriter and the native-transform exec hook.++(defn table-spec->sqlglot-key+ "Translate our `::table-spec` (`{:db :schema :table}` with our AST-position vocab,+ where `:db` ↔ `Table.catalog` and `:schema` ↔ `Table.db`) into the slot vocabulary+ `metabase.sql-parsing/replace-names` (SQLGlot) expects.++ SQLGlot's `replace-names` matcher uses `:schema` for the db-position qualifier+ (the leftmost in `db.tbl`) and `:catalog` for the catalog-position (the leftmost+ in `catalog.db.tbl`). Empirically:++ {:db \"x\" :table \"t\"} -> doesn't match `x.t` on MySQL+ {:schema \"x\" :table \"t\"} -> matches `x.t` (and `db.x.t`)+ {:catalog \"x\" :db \"y\" :table \"t\"} -> matches `x.y.t`++ The translation:+ - Our `:schema` -> SQLGlot's `:schema` (same name, same slot — unchanged).+ - Our `:db` -> SQLGlot's `:catalog` *if* `:schema` is also populated+ (i.e. 3-part SQL Server/BigQuery shape),+ otherwise SQLGlot's `:schema` (the 2-part MySQL case+ where our `:db` IS the leftmost qualifier in the SQL).++ `\"\"` sentinels are pruned (`prune-no-level`) so SQLGlot treats absent slots+ as wildcards rather than matching the literal empty string."+ [{:keys [db schema table] :as spec}]+ (let [pruned (prune-no-level spec)+ db? (contains? pruned :db)+ sch? (contains? pruned :schema)]+ (cond-> {:table table}+ (and db? sch?) (assoc :catalog db :schema schema)+ (and db? (not sch?)) (assoc :schema db)+ (and (not db?) sch?) (assoc :schema schema))))++(defn build-table-replacements+ "Convert a `{from-spec to-spec}` remappings map into the format expected by+ `sql-tools/replace-names`. SQLGlot handles quoting internally based on the+ dialect, so we pass raw identifiers."+ [remappings]+ (into {}+ (map (fn [[from-spec to-spec]]+ [(table-spec->sqlglot-key from-spec)+ (table-spec->sqlglot-key to-spec)]))+ remappings))++(defn rewrite-sql+ "Parse `sql` and rewrite every table reference whose `(catalog, schema, table)` matches a+ `from-spec` key in `remappings` to its `to-spec` counterpart. Returns the rewritten SQL.++ `remappings` is a `{from-spec to-spec}` map of `::table-spec`s. Caller is responsible for+ fetching it (typically `ws.remapping/remappings-for-db`).++ Fail-closed: throws `ex-info` with `:type qp.error-type/qp` on parse failure. A workspace+ child must not silently pass canonical refs through to the warehouse."+ [driver sql remappings]+ (try+ (let [replacements {:tables (build-table-replacements remappings)}]+ (sql-tools/replace-names driver sql replacements {:allow-unused? true}))+ (catch Exception e+ (throw (ex-info "Workspace table remapping failed: cannot parse SQL"+ {:type qp.error-type/qp+ :sql sql+ :driver driver}+ e)))))++; TODO Is this the same as prune-no-level?+(defn- spec->consumer-shape+ "Project a storage `::table-spec` (`{:db :schema :table}` with `\"\"` sentinels) into+ the consumer-shaped `{:db :schema :name}` map (with `nil` for absent slots). The+ storage sentinel stays below this line; consumers (`:model/Table` predicates,+ transform targets, sync's describe-fields lookups) work with `nil`."+ [{:keys [db schema table]}]+ {:db (denormalize-level db)+ :schema (denormalize-level schema)+ :name table})++(defn- consumer-shape->spec+ "Project a `{:db :schema :name}` consumer-shape map into a storage `::table-spec`,+ normalizing nil slots to the empty-string sentinel."+ [{:keys [db schema name]}]+ {:db (normalize-level db)+ :schema (normalize-level schema)+ :table name})++(defn- enrich-from-spec+ "Fill identifier slots the driver emits but the caller omitted from `from-spec`.++ Callers in `metabase.sync.fetch-metadata` only carry the `:schema` and `:name`+ they read off the `:model/Table` row — they don't know how to compute `:db`+ for drivers like MySQL (whose canonical namespace is the connection's bound+ database, not the Table's `:schema` column). Use `engine-namespace-positions`+ to derive any missing slot from the `database`, so the driver-aware match in+ `canonical->isolated` doesn't false-miss on a populated remap row."+ [database from-spec]+ (let [positions (ws/engine-namespace-positions database {:name (:name from-spec)+ :schema (:schema from-spec)})]+ (cond-> from-spec+ (nil? (:db from-spec)) (assoc :db (:db positions))+ (nil? (:schema from-spec)) (assoc :schema (:schema positions)))))++(defn remap-table+ "Returns `{:db :schema :name}` for the workspace destination of canonical+ `from-spec`, or nil if no remapping exists.++ Called per-table during sync via the `workspace-remap-schema+name` defenterprise hook.+ Uses driver-aware key projection so engines like MySQL (whose canonical/isolated+ tables differ at the `:db` AST position rather than `:schema`) match correctly.++ `from-spec` is `{:db :schema :name}`. Output uses nil-instead-of-empty-string for+ slots the driver doesn't emit."+ [database-id from-spec]+ (when-let [database (t2/select-one :model/Database :id database-id)]+ (let [driver (keyword (:engine database))+ enriched (enrich-from-spec database from-spec)]+ (some-> (canonical->isolated driver+ (ws.remapping/remappings-for-db database-id)+ (consumer-shape->spec enriched))+ spec->consumer-shape))))++(defenterprise workspace-remap-schema+name+ "Enterprise impl of the sync hook. Returns `{:db :schema :name}` for the+ isolated warehouse table when a `TableRemapping` row exists — sync asks the+ driver there, while app-db rows keep their logical identity. Deliberately+ ungated on premium features: if rows exist they must be respected, regardless+ of current token state."+ :feature :none+ [db-id from-spec]+ (remap-table db-id from-spec))++(defn all-mappings-for-db+ "Return all remappings for a given database as a map of `from-spec` to `to-spec`,+ each a `::table-spec` (`{:db :schema :table}`).++ Empty-string sentinels in `:db` / `:schema` indicate \"this driver does not emit+ this level\" and should be dropped before being handed to SQLGlot — see+ [[metabase-enterprise.workspaces.query-processor.middleware]]."+ [database-id]+ (ws.remapping/remappings-for-db database-id))++(defenterprise filter-workspace-side-tables+ "Enterprise impl of the table-list filter. Drops tuples whose `(schema, name)`+ matches the to-side of any active `TableRemapping` row for `db-id`. Workspace+ isolation tables show up in `describe-database` because the connection has+ GRANT on the isolation schema, but they must not become `:model/Table` rows —+ canonical Tables back them via remap. Like the read-side hook, deliberately+ ungated on premium features: if rows exist, the filter must apply. See DEV-1898."+ :feature :none+ [tuples db-id]+ ;; Storage rows carry the `""` sentinel for absent slots; sync tuples carry+ ;; `nil` (e.g. MySQL `:schema` is always nil). Denormalize both sides so the+ ;; comparison matches across the boundary.+ (let [to-pairs (into #{}+ (map (fn [to-spec] [(denormalize-level (:schema to-spec)) (:table to-spec)]))+ (vals (all-mappings-for-db db-id)))]+ (if (empty? to-pairs)+ tuples+ (into #{} (remove (fn [t] (contains? to-pairs [(denormalize-level (:schema t)) (:name t)]))) tuples))))++(defenterprise expand-schema-names-with-workspace+ "Enterprise impl: augment a `:schema-names` list with `to_schema` values for+ any active remap row whose `from_schema` matches one of the input schemas.+ Lets sync's FK fetch reach the workspace-isolated warehouse tables that+ physically back canonical Tables on a workspace child."+ :feature :none+ [schema-names db-id]+ (if (empty? schema-names)+ schema-names+ (let [from->to (into {}+ (map (fn [[from-spec to-spec]]+ [(:schema from-spec) (:schema to-spec)]))+ (all-mappings-for-db db-id))+ extras (into #{} (keep from->to) schema-names)]+ (vec (distinct (concat schema-names extras))))))++(defn- iso-db-swap-maps-for-fk-probe+ "Return a seq of swap-maps to apply per `describe-fks` invocation. Each entry is+ either `{:db <iso-db>}` (run describe-fks with the JDBC connection pointed at+ that iso DB) or nil (run with no swap, against the canonical connection).++ When the database has any active remap rows whose `to_db` differs from the+ canonical bound database (today: MySQL workspaces with cross-DB swaps), return+ only the iso swap-maps -- on a workspace child the canonical DB holds no real+ tables, so probing it would waste a round-trip and the rows it returns wouldn't+ correspond to any `:model/Table`. Otherwise return a single nil entry so the+ probe runs once against the canonical connection, preserving non-workspace+ behavior.++ 3-slot engines (BigQuery, SQL Server) populate `:db` on both sides of the+ remap row but the canonical `:db` already routes through the existing+ connection, so the iso `:db` matches canonical and this collapses to the+ single-canonical-run path."+ [db-id]+ (let [database (t2/select-one :model/Database :id db-id)+ canonical-db (-> database :details :db)+ iso-dbs (into #{}+ (comp (map (comp denormalize-level :db second))+ (remove nil?)+ (remove #(= % canonical-db)))+ (all-mappings-for-db db-id))]+ (if (seq iso-dbs)+ (map (fn [iso-db] {:db iso-db}) iso-dbs)+ [nil])))++(defenterprise call-with-fk-probe-iso-dbs+ "Enterprise impl: invoke `f` once for the canonical bound DB, then once for each+ distinct iso `:db` differing from the canonical, each iteration inside a+ `with-swapped-connection-details` scope. Returns a vector of `f`'s results so+ the caller can concatenate row batches before back-translation.++ Cross-DB MySQL workspaces store the iso table in a different bound database+ than the canonical one. Without a per-iso-DB describe-fks call, the JDBC+ connection only ever talks to the canonical DB and the iso DB's FK rows go+ undiscovered."+ :feature :none+ [db-id f]+ (mapv (fn [swap-map]+ (if swap-map+ (driver.conn/with-swapped-connection-details db-id swap-map+ (f))+ (f)))+ (iso-db-swap-maps-for-fk-probe db-id)))++(defenterprise inject-workspace-canonical-tuples+ "Enterprise impl: augment a `describe-database` result with synthetic+ canonical-side `(from_schema, from_table_name)` tuples for every active+ remap row. Without this, `sync-tables-and-database!` would diff app-db's+ canonical Table rows against a `describe-database` result that excludes+ them (the canonical name only exists virtually, backed by the workspace+ warehouse table). The diff would then retire those rows on every sync.++ Adds one synthetic tuple per remap row, whether or not the canonical+ tuple is already present (`into` deduplicates; tuples are equal by value).+ Tuples carry only `:schema` and `:name` -- the only fields the+ `sync-tables-and-database!` diff keys on."+ :feature :none+ [tuples db-id]+ ;; Storage rows carry the `""` sentinel for absent slots; the sync diff+ ;; matches against `nil`-schema tuples on schema-less drivers (MySQL). Use+ ;; `denormalize-level` so synthetic tuples match the diff key shape.+ (let [mappings (all-mappings-for-db db-id)]+ (if (empty? mappings)+ tuples+ (let [synthetic (into #{}+ (map (fn [[from-spec _]]+ {:schema (denormalize-level (:schema from-spec))+ :name (:table from-spec)}))+ mappings)]+ (into tuples synthetic)))))++(defenterprise rewrite-fk-result-canonical+ "Enterprise impl: walk an FK-result collection, rewriting workspace-side+ `(schema, name)` pairs back to canonical `(from_schema, from_table_name)`+ on both fk-side and pk-side. Rows whose pairs don't match any active+ `to_*` tuple pass through unchanged — used for FKs to non-remapped tables+ and for the legacy `describe-table-fks` fallback whose fk-side is already+ canonical."+ :feature :none+ [rows db-id]+ ;; Storage `:schema` is the `""` sentinel for absent slots; FK-result rows+ ;; carry `nil` on schema-less drivers (MySQL). Denormalize on both sides of+ ;; the lookup so the rewrite matches across the boundary. Output `:schema`+ ;; values come from the canonical from-spec — also denormalized.+ (let [to->from (into {}+ (map (fn [[from-spec to-spec]]+ [[(denormalize-level (:schema to-spec)) (:table to-spec)]+ [(denormalize-level (:schema from-spec)) (:table from-spec)]]))+ (all-mappings-for-db db-id))]+ (if (empty? to->from)+ rows+ (mapv (fn [row]+ (let [fk-key [(denormalize-level (:fk-table-schema row)) (:fk-table-name row)]+ pk-key [(denormalize-level (:pk-table-schema row)) (:pk-table-name row)]]+ (cond-> row+ (contains? to->from fk-key)+ (assoc :fk-table-schema (first (to->from fk-key))+ :fk-table-name (second (to->from fk-key)))+ (contains? to->from pk-key)+ (assoc :pk-table-schema (first (to->from pk-key))+ :pk-table-name (second (to->from pk-key))))))+ rows))))++(defenterprise canonical-schema+name+ "Enterprise impl: invert an active workspace remapping. Given a workspace-side+ `to-spec` (`{:db :schema :name}`), return a `{:db :schema :name}` map for the+ canonical table if a TableRemapping row records that pair as the destination+ of a canonical table; nil otherwise. Mirror of `workspace-remap-schema+name`+ for write-side paths that already have the rewritten target on hand and need+ the canonical slot before touching `:model/Table` rows.++ Driver-aware: matches against the slots the driver actually emits, so an+ engine like MySQL (whose canonical and isolated tables differ at `:db` rather+ than `:schema`) inverts correctly. Output `:db` / `:schema` slots are nil+ when the driver doesn't populate them, so the empty-string sentinel never+ leaks above this boundary."+ :feature :none+ [db-id to-spec]+ (when-let [driver (some-> (t2/select-one [:model/Database :engine] :id db-id) :engine keyword)]+ (some-> (isolated->canonical driver+ (all-mappings-for-db db-id)+ (consumer-shape->spec to-spec))+ spec->consumer-shape)))++(defenterprise call-with-display-context+ "Enterprise impl: bind `ws.remapping/*skip-remapping?*` true around `thunk` so Phase 1+ (metadata override) and Phase 2 (SQLGlot rewrite) both short-circuit. For display+ paths that want users to see canonical SQL instead of the isolation schema.+ Deliberately ungated on premium features."+ :feature :none+ [thunk]+ (binding [ws.remapping/*skip-remapping?* true]+ (thunk)))++;;; -------------------------------------------- Write API --------------------------------------------+;;;+;;; The convenience layer over [[metabase-enterprise.workspaces.remapping.core]]. Accepts+;;; `::table-spec` maps, normalizes `nil`/missing slots to the `""` sentinel, hands them+;;; to the active store. The store handles persistence (Toucan2 against+;;; `:model/TableRemapping` in production, atom in tests).+;;;+;;; Public callers should reach for these wrappers, NOT the raw store -- the wrappers+;;; enforce the `::table-spec` contract and the sentinel normalization at the boundary.++(defn- normalize-spec+ "Coerce nil `:db` / `:schema` slots to the empty-string sentinel so storage rows+ carry the canonical shape the unique constraint expects."+ [spec]+ (-> spec+ (update :db normalize-level)+ (update :schema normalize-level)))++(mu/defn add-mapping!+ "Idempotently ensure a remapping row exists, via the active store.++ Idempotent on the unique key `(database_id, from_db, from_schema, from_table_name)`:+ a duplicate is a no-op (the existing row is left untouched). Concurrent-insert races+ are handled portably by [[metabase.app-db.core/update-or-insert!]]. Returns truthy when+ the remapping is in place after the call.++ `from-spec` and `to-spec` are `::table-spec` maps. Nil/missing identifier levels+ are normalized to the empty-string sentinel before insert.++ (add-mapping! 6+ {:schema \"my-schema\" :table \"my-table\"}+ {:schema \"new-schema\" :table \"new-table-name\"})++ ;; BigQuery (3-level): include :db+ (add-mapping! 6+ {:db \"proj\" :schema \"ds\" :table \"orders\"}+ {:db \"proj\" :schema \"ws_ds\" :table \"orders\"})"+ [database-id :- :int+ from-spec :- ::table-spec+ to-spec :- ::table-spec]+ (ws.remapping/insert-mapping! database-id (normalize-spec from-spec) (normalize-spec to-spec)))++(mu/defn remove-mapping!+ "Remove a remapping row by source `from-spec`. Returns the number of rows removed (0 or 1)."+ [database-id :- :int+ from-spec :- ::table-spec]+ (ws.remapping/remove-mapping! database-id (normalize-spec from-spec)))++(defn clear-mappings-for-db!+ "Remove all remappings for a given database. Returns the number of rows removed."+ [database-id]+ (ws.remapping/clear-for-db! database-id))++(defn add-transform-target-mapping!+ "Register a remapping for a transform target on a workspaced database. The from-side spec+ is normalized via [[spec-for-table]] so it carries the right slots for the driver+ (e.g., 3-slot for SQL Server/BigQuery, 2-slot for Postgres/ClickHouse,+ 1-slot for MySQL). Delegates to [[add-mapping!]] (idempotent on the unique constraint).++ Inputs:+ - `db-id` -- the canonical `:model/Database` id.+ - `target` -- the transform's target map, with `:schema` and `:name` for the canonical+ identifier the transform was configured to write to. Same shape `resolve-transform-target`+ already passes around.++ Resolution:+ - From-side: a synthetic `:model/Database` + `:model/Table` shape is fed through+ `spec-for-table` so `:db` and `:schema` slots are filled per the driver's+ `qualified-name-components`.+ - To-side: workspace output namespace comes from `ws/db-workspace-namespace`,+ a `{:db ?, :schema ?}` map sourced from `config.yml`. The `:db` and+ `:schema` slots both flow into the `TableRemapping` row's `to_db` /+ `to_schema` columns - so cross-DB SQL Server / BigQuery workspaces are+ now expressible end-to-end.++ Returns the to-side as a denormalized `{:db :schema :name}` map (consumer+ shape: `nil` for slots the driver doesn't fill, string otherwise). The+ storage layer's `\"\"` sentinel never leaks above this boundary, so the value+ can be `assoc`ed onto a transform target without a per-call denormalization shim.++ Throws when the database is not workspaced -- reaching this with a non-workspaced+ db is a programming error; gate on [[ws/db-workspace-namespace]] first."+ [db-id target]+ (let [workspace-ns (ws/db-workspace-namespace db-id)]+ (when-not workspace-ns+ (throw (ex-info "Cannot record transform-target remapping: database is not workspaced"+ {:db-id db-id :target target})))+ (let [database (or (t2/select-one :model/Database :id db-id)+ (throw (ex-info "Database not found"+ {:db-id db-id :target target})))+ {table-name :name from-schema :schema} target+ ;; Synthesize a :model/Table shape so spec-for-table can populate the canonical+ ;; from-side slots per the driver's qualified-name-components.+ from-table {:name table-name :schema from-schema}+ from-spec (spec-for-table database from-table)+ ;; To-side: rename via [[remapped-table-name]] so two source tables sharing a+ ;; name across different schemas (schemaA.orders vs schemaB.orders) land at+ ;; distinct warehouse identifiers in the single workspace namespace. Both :db+ ;; and :schema slots come from `db-workspace-namespace` so cross-DB workspaces+ ;; (SQL Server / BigQuery) get full namespace remapping. Slots not+ ;; populated by the workspace config are normalized to "" via `normalize-level`+ ;; so the storage row's unique constraint stays enforceable.+ to-spec (-> (remapped-table-name (:engine database) from-spec)+ (assoc :db (normalize-level (:db workspace-ns))+ :schema (normalize-level (:schema workspace-ns))))]+ (add-mapping! db-id from-spec to-spec)+ ;; Hand callers the denormalized shape so storage's `""` sentinel stays+ ;; below the line. Symmetric with the read hooks (`workspace-remap-schema+name`,+ ;; `canonical-schema+name`) which also denormalize at the boundary.+ (spec->consumer-shape to-spec))))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/transform_hooks.clj b/enterprise/backend/src/metabase_enterprise/workspaces/transform_hooks.cljnew file mode 100644index 00000000000..4e4b6dfa905--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/transform_hooks.clj@@ -0,0 +1,70 @@+(ns metabase-enterprise.workspaces.transform-hooks+ "Enterprise hooks that integrate the transform execution path with workspace isolation."+ (:require+ [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.remapping.core :as ws.remapping]+ [metabase-enterprise.workspaces.table-remapping :as ws.table-remapping]+ [metabase.premium-features.core :refer [defenterprise]]))++(set! *warn-on-reflection* true)++(defenterprise resolve-transform-target+ "Enterprise impl of the transform-target rewrite hook.++ When a `WorkspaceDatabase` is provisioned for `db-id`, the transform should write to+ the workspace's output schema rather than the canonical target schema. This:++ 1. Records a `TableRemapping` from the canonical `(schema, name)` to the workspace's+ `(workspace-schema, remapped-name)` via [[ws.table-remapping/add-transform-target-mapping!]].+ The remap is normalized per-driver (3-slot for SQL Server/BigQuery,+ 2-slot for Postgres/ClickHouse, 1-slot for MySQL). The remapped table name is+ derived deterministically from the canonical `(schema, name)` so that two source+ tables sharing a name across different schemas land at distinct warehouse+ identifiers in the single workspace schema.+ 2. Returns the target with `:schema` and `:name` rewritten to the recorded warehouse+ identifier, so the transform executor writes to the same place the row points.++ Subsequent reads of the canonical `(schema, name)` pair will resolve to the workspace+ copy via the QP middleware (see `metabase-enterprise.workspaces.query-processor.middleware`).++ When no workspace is active for `db-id`, the canonical target passes through unchanged.++ Deliberately ungated on premium features: a token expiry shouldn't silently cause+ workspace transforms to overwrite production tables. If a `WorkspaceDatabase` is+ provisioned, the rewrite must apply."+ :feature :none+ [db-id target]+ (if (some? (ws/db-workspace-namespace db-id))+ (let [{to-db :db to-schema :schema to-name :name}+ (ws.table-remapping/add-transform-target-mapping! db-id target)]+ ;; **Replace** `:db` and `:schema` on the target with the to-spec's values+ ;; — not just merge in the populated ones. The canonical target carries the+ ;; input namespace; after rewriting, the target points at the iso namespace+ ;; which on MySQL lives at `:db`, not `:schema`. Failing to clear the+ ;; canonical `:schema` leaves the SQL compiler with two competing+ ;; qualifiers and the output lands in the wrong place.+ ;;+ ;; `add-transform-target-mapping!` returns the to-side denormalized (`""`+ ;; sentinels already converted to nil), so we can `assoc` directly without+ ;; a per-call shim.+ (assoc target :db to-db :schema to-schema :name to-name))+ target))++(defenterprise rewrite-native-sql-for-workspace+ "Workspace-isolation SQL rewrite for the native-transform exec path. Reuses the same+ `rewrite-sql` primitive as the QP's Phase 2 middleware, so canonical refs in a native+ transform's SQL resolve to the same isolation-schema names the QP would emit.++ Short-circuits when no remappings are active for `db-id`.++ Deliberately ungated on premium features. See [[resolve-transform-target]] for the+ rationale -- if a workspace child has `TableRemapping` rows, isolation must engage+ regardless of token state."+ :feature :none+ [driver db-id sql]+ (if-not (ws.remapping/enabled-for-db? db-id)+ sql+ (let [remappings (ws.remapping/remappings-for-db db-id)]+ (if (empty? remappings)+ sql+ (ws.table-remapping/rewrite-sql driver sql remappings)))))diff --git a/enterprise/backend/src/metabase_enterprise/writable_connection/core.clj b/enterprise/backend/src/metabase_enterprise/writable_connection/core.cljdeleted file mode 100644index 6996d8cce0f..00000000000--- a/enterprise/backend/src/metabase_enterprise/writable_connection/core.clj+++ /dev/null@@ -1,11 +0,0 @@-(ns metabase-enterprise.writable-connection.core- (:require- [metabase.premium-features.core :refer [defenterprise]]))--(defenterprise database-write-data-details- "Returns the `:write-data-details` for a database when the `:writable-connection` feature is available.-- `database` is a [[lib/metadata]] database instance. To pass a Toucan2 database instance, use [[driver.u/ensure-lib-database]]."- :feature :writable-connection- [database]- (:write-data-details database))diff --git a/enterprise/backend/test/metabase_enterprise/advanced_config/file/api_keys_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_config/file/api_keys_test.cljindex 013815d5abc..c8e91b6a6b7 100644--- a/enterprise/backend/test/metabase_enterprise/advanced_config/file/api_keys_test.clj+++ b/enterprise/backend/test/metabase_enterprise/advanced_config/file/api_keys_test.clj@@ -1,4 +1,4 @@-(ns metabase-enterprise.advanced-config.file.api-keys-test+(ns ^:synchronous metabase-enterprise.advanced-config.file.api-keys-test(:require[clojure.test :refer :all][metabase-enterprise.advanced-config.file :as config.file]diff --git a/enterprise/backend/test/metabase_enterprise/advanced_config/file/workspace_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_config/file/workspace_test.cljnew file mode 100644index 00000000000..4fd55bc9602--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/advanced_config/file/workspace_test.clj@@ -0,0 +1,372 @@+(ns metabase-enterprise.advanced-config.file.workspace-test+ (:require+ [clojure.java.io :as io]+ [clojure.spec.alpha :as s]+ [clojure.test :refer [deftest is testing use-fixtures]]+ [metabase-enterprise.advanced-config.file :as advanced-config.file]+ [metabase-enterprise.advanced-config.file.workspace :as advanced-config.file.workspace]+ [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.test-util :as workspaces.tu]+ [metabase.test :as mt]+ [metabase.util.yaml :as yaml])+ (:import+ (clojure.lang ExceptionInfo)))++(def default-workspace-instance-config nil)++(use-fixtures :each+ (fn [thunk]+ (binding [advanced-config.file/*supported-versions* {:min 1, :max 1}+ ws/*workspace-instance-config* (atom default-workspace-instance-config)]+ (thunk))))++(defn- load-fixture-by-driver [driver]+ (-> (str "metabase_enterprise/workspaces/resources/workspace_config_" (name driver) ".yml")+ io/resource slurp yaml/parse-string))++(defn- load-fixture+ "Postgres-shaped fixture used by `fixture-parses-test` and the loader+ round-trip tests. Sibling fixtures for other drivers live in the same+ resources dir (see `per-driver-fixtures-test` below)."+ []+ (load-fixture-by-driver :postgres))++(defn- workspace-section+ "Pull just the `:workspace` section out of the fixture, with the database name+ rewritten to point at the supplied `db-name` (so tests can use whatever DB+ `mt/with-temp` creates rather than depending on the fixture's literal name)."+ [db-name]+ (let [section (-> (load-fixture) :config :workspace)]+ (-> section+ (assoc :databases+ (into {} (map (fn [[_db-name-kw wsd-config]]+ [(keyword db-name) wsd-config])+ (:databases section)))))))++(deftest fixture-parses-test+ (testing "the checked-in fixture parses and has the expected top-level shape"+ (let [parsed (load-fixture)]+ (is (= 1 (:version parsed)))+ (is (map? (:config parsed)))+ (is (= "New workspace" (get-in parsed [:config :workspace :name])))+ (testing "workspace.databases is keyed by database name"+ (let [dbs (get-in parsed [:config :workspace :databases])]+ (is (= 1 (count dbs)))+ (let [[_db-name wsd] (first dbs)]+ (is (= ["public"] (:input_schemas wsd)))+ (is (= "mb__isolation_44490_1933" (:output_namespace wsd)))))))))++(deftest non-blank-string-spec-test+ (testing "::non-blank-string rejects nil, non-strings, and whitespace-only strings,+ and accepts any string with at least one non-whitespace character."+ (let [spec :metabase-enterprise.advanced-config.file.workspace/non-blank-string]+ (doseq [v ["" " " " " "\t" "\n" "\t \n" nil 42 :foo]]+ (is (not (s/valid? spec v))+ (str "must reject " (pr-str v))))+ (doseq [v ["a" "abc" " a" "a " " a " "non-blank with spaces"]]+ (is (s/valid? spec v)+ (str "must accept " (pr-str v)))))))++(deftest apply-workspace-section-populates-atom-test+ (testing "applying the :workspace section stores parsed config in the in-process atom keyed by db-id"+ (mt/with-empty-h2-app-db!+ (mt/with-temp [:model/Database {db-id :id} {:name "ws-test-db" :engine :postgres}]+ (let [section (workspace-section "ws-test-db")+ {:keys [workspace-name database-count]} (advanced-config.file.workspace/apply-workspace-section! section)]+ (is (= "New workspace" workspace-name))+ (is (= 1 database-count))+ (testing "atom holds the parsed config"+ (let [stored (ws/instance-workspace)]+ (is (= "New workspace" (:name stored)))+ (is (= ["public"]+ (get-in stored [:databases db-id :input_schemas])))+ (is (= {:schema "mb__isolation_44490_1933"}+ (get-in stored [:databases db-id :output]))))))))))++(deftest re-apply-replaces-atom-test+ (testing "re-applying with a different config replaces the atom — no mismatch detection, file is the truth"+ (mt/with-empty-h2-app-db!+ (mt/with-temp [:model/Database _ {:name "ws-test-db" :engine :postgres}+ :model/Database _ {:name "ws-test-db-2" :engine :postgres}]+ (let [section-1 (workspace-section "ws-test-db")+ section-2 (assoc section-1+ :name "Renamed Workspace"+ :databases {:ws-test-db-2 {:input_schemas ["public"]+ :output_namespace "different_schema"}})]+ (advanced-config.file.workspace/apply-workspace-section! section-1)+ (is (= "New workspace" (:name (ws/instance-workspace))))+ (advanced-config.file.workspace/apply-workspace-section! section-2)+ (is (= "Renamed Workspace" (:name (ws/instance-workspace))))+ (is (= 1 (count (:databases (ws/instance-workspace)))))+ (is (= {:schema "different_schema"}+ (->> (ws/instance-workspace) :databases vals first :output))+ "atom reflects the new config, not the old one"))))))++(deftest unknown-database-name-throws-test+ (testing "referencing a database that doesn't exist in app-db throws ex-info"+ (mt/with-empty-h2-app-db!+ (is (thrown-with-msg?+ ExceptionInfo+ #"Workspace config references unknown database"+ (advanced-config.file.workspace/apply-workspace-section!+ (workspace-section "nonexistent-db")))))))++(deftest db-workspace-namespace-resolves-after-loading-test+ (testing "after loading, db-workspace-namespace returns the configured output namespace"+ (mt/with-empty-h2-app-db!+ (mt/with-temp [:model/Database {db-id :id} {:name "ws-test-db" :engine :postgres}]+ (advanced-config.file.workspace/apply-workspace-section!+ (workspace-section "ws-test-db"))+ (is (= {:schema "mb__isolation_44490_1933"} (ws/db-workspace-namespace db-id)))))))++(deftest db-workspace-namespace-returns-nil-when-no-load-test+ (testing "without a config.yml load, db-workspace-namespace returns nil — manager-side rows are not consulted"+ ;; The atom is cleared in the use-fixtures :each tear-down, so this confirms+ ;; the read truly comes from the atom and not from any leftover rows.+ (mt/with-empty-h2-app-db!+ (mt/with-temp [:model/Database {db-id :id} {:name "ws-test-db" :engine :postgres}]+ (is (nil? (ws/db-workspace-namespace db-id)))))))++(deftest oss-readable-no-premium-token-test+ (testing "the :workspace section loads on instances without the :config-text-file premium feature"+ (mt/with-empty-h2-app-db!+ (mt/with-premium-features #{}+ (mt/with-temp [:model/Database {db-id :id} {:name "ws-test-db" :engine :postgres}]+ (binding [advanced-config.file/*config*+ {:version 1+ :config {:workspace (workspace-section "ws-test-db")}}]+ (is (= :ok (advanced-config.file/initialize!)))+ (is (some? (ws/instance-workspace))+ "atom is populated even without :config-text-file token")+ (is (= {:schema "mb__isolation_44490_1933"} (ws/db-workspace-namespace db-id)))))))))++(deftest oss-rejects-config-without-workspace-section-test+ (testing "OSS instance still gets the premium-token error when :workspace is absent"+ (mt/with-empty-h2-app-db!+ (mt/with-premium-features #{}+ (binding [advanced-config.file/*config*+ {:version 1+ :config {:users [{:first_name "X" :last_name "Y"+ :email "[email protected]" :password "pw"}]}}]+ (is (thrown-with-msg?+ ExceptionInfo+ #"Premium token with the :config-text-file feature"+ (advanced-config.file/initialize!))+ "without :workspace, OSS still requires the premium token"))))))++(deftest workspace-bring-up-opens-gate-for-other-sections-test+ (testing "presence of :workspace lets sibling sections (:databases, :users, :api-keys) load on OSS"+ (mt/with-empty-h2-app-db!+ (mt/with-premium-features #{}+ (mt/with-temp [:model/Database _ {:name "sibling-db"}]+ (binding [advanced-config.file/*config*+ {:version 1+ :config {:users [{:first_name "Bring"+ :last_name "Up"+ :email "[email protected]"+ :password "password1"}]+ :workspace (workspace-section "sibling-db")}}]+ (is (= :ok (advanced-config.file/initialize!)))+ (is (some? (ws/instance-workspace))+ ":workspace section ran")))))))++(defn- try-initialize!+ "Run `initialize!` and report whether it returned `:ok` cleanly. Catches every+ Throwable so the OSS-bypass attack tests below can ask 'did anything block+ this?' without caring which layer (spec validation or the premium-token gate)+ did the blocking — both count as defense."+ []+ (try+ (= :ok (advanced-config.file/initialize!))+ (catch Throwable _+ false)))++(deftest invalid-workspace-section-does-not-open-gate-test+ (testing "an empty or malformed :workspace section does NOT let the rest of the file load on OSS"+ (mt/with-empty-h2-app-db!+ (mt/with-premium-features #{}+ (testing "empty workspace map"+ (binding [advanced-config.file/*config*+ {:version 1+ :config {:users [{:first_name "X" :last_name "Y"+ :email "[email protected]" :password "pw"}]+ :workspace {}}}]+ (is (false? (try-initialize!))+ "empty :workspace doesn't unlock the gate")))+ (testing "workspace missing :databases"+ (binding [advanced-config.file/*config*+ {:version 1+ :config {:users [{:first_name "X" :last_name "Y"+ :email "[email protected]" :password "pw"}]+ :workspace {:name "Just a name"}}}]+ (is (false? (try-initialize!)))))+ (testing "workspace with empty :databases map"+ (binding [advanced-config.file/*config*+ {:version 1+ :config {:users [{:first_name "X" :last_name "Y"+ :email "[email protected]" :password "pw"}]+ :workspace {:name "n" :databases {}}}}]+ (is (false? (try-initialize!)))))))))++(deftest valid-workspace-section?-test+ (testing "valid-workspace-section? matches the spec used by the gate"+ (testing "structurally-valid sections"+ (is (true? (advanced-config.file.workspace/valid-workspace-section?+ {:name "ws" :databases {:db1 {:input_schemas ["s1"]+ :output_namespace "out"}}}))+ "minimal: single schema string on input, output namespace is a single string")+ (is (true? (advanced-config.file.workspace/valid-workspace-section?+ {:name "ws" :databases {:db1 {:input_schemas ["PUBLIC" "ANALYTICS"]+ :output_namespace "WS_ALICE"}}}))+ "multiple input schemas allowed"))+ (testing "structural rejection"+ (is (false? (advanced-config.file.workspace/valid-workspace-section? {}))+ "empty map is invalid")+ (is (false? (advanced-config.file.workspace/valid-workspace-section? {:name "ws"}))+ ":databases is required")+ (is (false? (advanced-config.file.workspace/valid-workspace-section?+ {:name "ws" :databases {}}))+ "empty :databases map is invalid")+ (is (false? (advanced-config.file.workspace/valid-workspace-section?+ {:name "ws"+ :databases {:db1 {:input_schemas []+ :output_namespace "out"}}}))+ "empty :input_schemas is invalid - need at least one input schema"))+ (testing "schema-string rejection"+ (is (false? (advanced-config.file.workspace/valid-workspace-section?+ {:name "ws"+ :databases {:db1 {:input_schemas [""]+ :output_namespace "out"}}}))+ "empty-string in :input_schemas is invalid")+ (is (false? (advanced-config.file.workspace/valid-workspace-section?+ {:name "ws"+ :databases {:db1 {:input_schemas ["s1"]+ :output_namespace ""}}}))+ "empty-string :output_namespace is invalid"))))++;;; ----------------------------------------- per-driver shapes -----------------------------------------+;;;+;;; Drivers with different cardinalities should round-trip through the wire format+;;; without modification.++(deftest mysql-cardinality-upgrade-section-test+ (testing "MySQL workspace: only `:db` slot is populated in the atom (no schema layer)"+ (let [section {:name "ws"+ :databases {:mysql-prod {:input_schemas ["prod_db"]+ :output_namespace "ws_alice"}}}]+ (is (true? (advanced-config.file.workspace/valid-workspace-section? section))+ "MySQL workspace passes validation"))+ (testing "loader stores MySQL output namespace in the :db slot"+ (mt/with-empty-h2-app-db!+ (mt/with-temp [:model/Database {db-id :id} {:name "mysql-prod" :engine :mysql :details {:db "prod_db"}}]+ (advanced-config.file.workspace/apply-workspace-section!+ {:name "ws"+ :databases {:mysql-prod {:input_schemas ["prod_db"]+ :output_namespace "ws_alice"}}})+ (is (= {:db "ws_alice"} (ws/db-workspace-namespace db-id))))))))++(deftest bigquery-3-slot-section-test+ (testing "BigQuery workspace: project from details, dataset from output_namespace"+ ;; `apply-workspace-section!` -> `expand-output` -> `engine-namespace-positions`+ ;; -> `driver/qualified-name-components` triggers a driver-load. On the default+ ;; Enterprise classpath BigQuery isn't loaded; skip the round-trip half of this+ ;; test when the driver isn't on the test classpath.+ (when (workspaces.tu/driver-loadable? :bigquery-cloud-sdk)+ (mt/with-empty-h2-app-db!+ (mt/with-temp [:model/Database {db-id :id} {:name "bq-prod" :engine :bigquery-cloud-sdk :details {:project-id "metabase-prod"}}]+ (advanced-config.file.workspace/apply-workspace-section!+ {:name "ws"+ :databases {:bq-prod {:input_schemas ["core"]+ :output_namespace "ws_alice"}}})+ (is (= {:db "metabase-prod" :schema "ws_alice"} (ws/db-workspace-namespace db-id))))))))++;;; ----------------------------------------- per-driver YAML fixtures -----------------------------------------+;;;+;;; The `resources/workspace_config_<driver>.yml` files are reference fixtures+;;; - one per driver shape. They double as documentation (each is a complete+;;; valid `config.yml` showing the per-driver wire format) and as test data.+;;; This test parametrizes over all of them, asserting:+;;;+;;; - the YAML parses+;;; - the `:workspace` section validates against the spec+;;; - the loader round-trips it into the atom with the per-driver expected+;;; output namespace+;;;+;;; If a driver's wire format drifts, the corresponding fixture and this test+;;; both have to be updated together - keeps doc and code aligned.++(def ^:private per-driver-fixture-expectations+ "Per-driver fixture metadata: db-name + engine + connection details from the fixture,+ the wire-shape `:input_schemas`, the wire-shape `:output_namespace`, and the+ expanded `:output` map the atom should hold after loading. The expanded `:output`+ reflects each engine's catalog source — `:db` comes from `Database.details` for+ 3-slot drivers, `nil` for 2-slot drivers; the schema slot holds the+ `output_namespace` string (or lands in `:db` for MySQL)."+ {:postgres {:db-name "test-data (postgres)"+ :engine :postgres+ :details {}+ :input_schemas ["public"]+ :output_namespace "mb__isolation_44490_1933"+ :expanded-output {:schema "mb__isolation_44490_1933"}}+ :redshift {:db-name "test-data (redshift)"+ :engine :redshift+ :details {}+ :input_schemas ["public"]+ :output_namespace "mb__isolation_44490_1933"+ :expanded-output {:schema "mb__isolation_44490_1933"}}+ :mysql {:db-name "test-data (mysql)"+ :engine :mysql+ :details {:db "prod_db"}+ :input_schemas ["prod_db"]+ :output_namespace "ws_alice"+ :expanded-output {:db "ws_alice"}}+ :clickhouse {:db-name "test-data (clickhouse)"+ :engine :clickhouse+ :details {}+ :input_schemas ["prod_events"]+ :output_namespace "ws_alice"+ :expanded-output {:schema "ws_alice"}}+ :sqlserver {:db-name "test-data (sqlserver)"+ :engine :sqlserver+ :details {:db "AnalyticsDB"}+ :input_schemas ["dbo"]+ :output_namespace "ws_alice"+ :expanded-output {:db "AnalyticsDB" :schema "ws_alice"}}+ :bigquery {:db-name "test-data (bigquery)"+ :engine :bigquery-cloud-sdk+ :details {:project-id "metabase-prod"}+ :input_schemas ["core"]+ :output_namespace "ws_alice"+ :expanded-output {:db "metabase-prod" :schema "ws_alice"}}})++(deftest per-driver-fixtures-parse-and-validate-test+ (testing "Each per-driver fixture YAML parses, has the expected workspace section, and round-trips through the loader"+ (doseq [[driver expectations] per-driver-fixture-expectations]+ (testing (str driver " fixture")+ (let [parsed (load-fixture-by-driver driver)+ section (get-in parsed [:config :workspace])+ wsd (-> section :databases vals first)]+ (testing "parses to the expected wire shape"+ (is (= 1 (:version parsed)))+ (is (= "New workspace" (:name section)))+ (is (= (:input_schemas expectations) (:input_schemas wsd))+ (str driver " input_schemas matches expectation"))+ (is (= (:output_namespace expectations) (:output_namespace wsd))+ (str driver " output_namespace matches expectation")))+ (testing "validates against the workspace spec"+ (is (true? (advanced-config.file.workspace/valid-workspace-section? section))+ (str driver " fixture must satisfy the section spec")))+ (testing "round-trips through apply-workspace-section! into the atom"+ ;; The round-trip step requires the driver to be loadable because+ ;; `apply-workspace-section!` -> `expand-output` -> `engine-namespace-positions`+ ;; -> `driver/qualified-name-components` triggers driver-load. Skip drivers+ ;; absent from the current classpath (e.g. BigQuery on the+ ;; default Enterprise OSS run).+ (when (workspaces.tu/driver-loadable? (:engine expectations))+ (mt/with-empty-h2-app-db!+ (mt/with-temp [:model/Database {db-id :id} {:name (:db-name expectations)+ :engine (:engine expectations)+ :details (:details expectations)}]+ (advanced-config.file.workspace/apply-workspace-section! section)+ (is (= (:expanded-output expectations) (ws/db-workspace-namespace db-id))+ (str driver " atom output matches fixture")))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/api/session_test.clj b/enterprise/backend/test/metabase_enterprise/api/session_test.cljindex 937eafb371b..d5fd6b04b73 100644--- a/enterprise/backend/test/metabase_enterprise/api/session_test.clj+++ b/enterprise/backend/test/metabase_enterprise/api/session_test.clj@@ -121,6 +121,7 @@:etl_connections false:etl_connections_pg false:dependencies false+ :workspaces false:writable_connection true}(mt/with-temporary-setting-values [custom-viz-enabled true](:token-features (mt/user-http-request :crowberto :get 200 "session/properties")))))))diff --git a/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj b/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.cljindex 6a67228c235..b9ef32b2424 100644--- a/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj+++ b/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj@@ -81,7 +81,8 @@:perms/manage-table-metadata :no:perms/view-data :unrestricted:perms/create-queries :no- :perms/transforms :no}+ :perms/transforms :no+ :perms/workspaces :no}(-> (data-perms.graph/data-permissions-graph :db-id audit/audit-db-id :audit? true)(get-in [(u/the-id (perms-group/all-users)) audit/audit-db-id])))))diff --git a/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/api_test.clj b/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/api_test.cljindex ece5fc715a9..738566a90e4 100644--- a/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/api_test.clj@@ -1,4 +1,4 @@-(ns metabase-enterprise.custom-viz-plugin.api-test+(ns ^:synchronous metabase-enterprise.custom-viz-plugin.api-test(:require[clojure.string :as str][clojure.test :refer :all]diff --git a/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/cache_test.clj b/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/cache_test.cljindex 75c52914473..ad67a065c8a 100644--- a/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/cache_test.clj+++ b/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/cache_test.clj@@ -1,4 +1,4 @@-(ns metabase-enterprise.custom-viz-plugin.cache-test+(ns ^:synchronous metabase-enterprise.custom-viz-plugin.cache-test(:require[clojure.test :refer :all][metabase-enterprise.custom-viz-plugin.cache :as cache]diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/collection_api_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/collection_api_test.cljindex 96602804d76..bfc02204f61 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/collection_api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/collection_api_test.clj@@ -1,4 +1,4 @@-(ns metabase-enterprise.remote-sync.collection-api-test+(ns ^:synchronous metabase-enterprise.remote-sync.collection-api-test{:clj-kondo/config '{:linters {:deprecated-var {:exclude {metabase.test.data/mbql-query {:namespaces [metabase-enterprise.remote-sync.collection-api-test]}}}}}}(:require[clojure.string :as str]diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/dashboard_api_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/dashboard_api_test.cljindex 025d9856e42..b5a8b5a5c9d 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/dashboard_api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/dashboard_api_test.clj@@ -1,4 +1,4 @@-(ns metabase-enterprise.remote-sync.dashboard-api-test+(ns ^:synchronous metabase-enterprise.remote-sync.dashboard-api-test{:clj-kondo/config '{:linters {:deprecated-var {:exclude {metabase.test.data/mbql-query {:namespaces [metabase-enterprise.remote-sync.dashboard-api-test]}}}}}}(:require[clojure.string :as str]diff --git a/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.cljindex b9daf746f73..99ae9f98970 100644--- a/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj+++ b/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj@@ -104,6 +104,10 @@:model/Revision:model/SemanticSearchTokenTracking:model/SearchIndexMetadata+ ;; Workspace remappings are runtime-only; they redirect QP queries against canonical+ ;; tables to workspace-isolated copies. They aren't portable across instances and+ ;; aren't included in serdes export/import.+ :model/TableRemapping:model/Secret:model/Session:model/SupportAccessGrantLog@@ -127,7 +131,11 @@:model/SecurityAdvisory:model/CloudMigration:model/Comment- :model/CommentReaction})+ :model/CommentReaction+ ;; Workspace and WorkspaceDatabase are runtime-only -- per-instance workspace+ ;; provisioning state, not portable content. Same rationale as TableRemapping above.+ :model/Workspace+ :model/WorkspaceDatabase})(deftest ^:parallel comprehensive-entity-id-test(let [entity-id-models (->> (v2.entity-ids/toucan-models)diff --git a/enterprise/backend/test/metabase_enterprise/support_access_grants/session_integration_test.clj b/enterprise/backend/test/metabase_enterprise/support_access_grants/session_integration_test.cljindex e1fc4f7e045..a3be5e64732 100644--- a/enterprise/backend/test/metabase_enterprise/support_access_grants/session_integration_test.clj+++ b/enterprise/backend/test/metabase_enterprise/support_access_grants/session_integration_test.clj@@ -1,4 +1,4 @@-(ns metabase-enterprise.support-access-grants.session-integration-test+(ns ^:synchronous metabase-enterprise.support-access-grants.session-integration-test"Tests for session API integration with support access grants.Tests the fallback mechanism in /api/session/reset_password and /api/session/password_reset_token_validthat tries support-access-grant provider first, then falls back to emailed-secret-password-reset."@@ -220,7 +220,7 @@:ip_address "127.0.0.1"}})](is (:success? login-result) "Should be able to login with new password")))))))))))))-(deftest ^:parallel with-fallback-preserves-error-info-test+(deftest with-fallback-preserves-error-info-test(testing "with-fallback swallows exceptions from the first provider, losing error info"(let [result (auth-identity/with-fallback(fn [provider _request]diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_instance_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_instance_test.cljnew file mode 100644index 00000000000..7a145fa3d38--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_instance_test.clj@@ -0,0 +1,60 @@+(ns metabase-enterprise.workspaces.api.workspace-instance-test+ "Smoke tests for the workspace-instance HTTP API.+ Behavioral logic is tested elsewhere — these just verify routing, auth, and+ request/response shape through the HTTP layer."+ (:require+ [clojure.test :refer [deftest is testing use-fixtures]]+ [metabase-enterprise.workspaces.core :as ws]+ [metabase.test :as mt]+ [metabase.test.fixtures :as fixtures]+ [toucan2.core :as t2]))++(use-fixtures :once (fixtures/initialize :db))++(defn- with-premium-feature [f]+ (mt/with-premium-features #{:workspaces}+ (f)))++(use-fixtures :each with-premium-feature)++(deftest current-superuser-only-test+ (testing "GET /ee/workspace-instance/current requires superuser"+ (is (= "You don't have permissions to do that."+ (mt/user-http-request :rasta :get 403 "ee/workspace-instance/current")))))++(deftest current-empty-test+ (testing "GET /ee/workspace-instance/current returns nil body when no workspace is loaded"+ (with-redefs [ws/instance-workspace (constantly nil)]+ (is (nil? (mt/user-http-request :crowberto :get 204 "ee/workspace-instance/current"))))))++(deftest current-with-workspace-test+ (testing "GET /ee/workspace-instance/current returns the atom shape — name + databases map keyed by id"+ (with-redefs [ws/instance-workspace (constantly {:name "Current Test"+ :databases {(mt/id) {:input_schemas ["PUBLIC"]+ :output {:schema "ws_alice"}}}})]+ (let [result (mt/user-http-request :crowberto :get 200 "ee/workspace-instance/current")]+ (is (= "Current Test" (:name result)))+ (testing "databases is a map keyed by database id with input_schemas + output"+ (is (= 1 (count (:databases result))))+ (let [databases (:databases result)+ db (or (get databases (mt/id))+ (get databases (keyword (str (mt/id))))+ (get databases (str (mt/id))))]+ (is (= ["PUBLIC"] (:input_schemas db)))+ (is (= {:schema "ws_alice"} (:output db)))))))))++(deftest table-remappings-superuser-only-test+ (testing "GET /ee/workspace-instance/table-remappings requires superuser"+ (is (= "You don't have permissions to do that."+ (mt/user-http-request :rasta :get 403 "ee/workspace-instance/table-remappings")))))++(deftest table-remappings-smoke-test+ (testing "GET /ee/workspace-instance/table-remappings returns all rows"+ (mt/with-model-cleanup [:model/TableRemapping]+ (t2/insert! :model/TableRemapping {:database_id (mt/id)+ :from_schema "public"+ :from_table_name "orders"+ :to_schema "mb_iso"+ :to_table_name "orders"})+ (let [result (mt/user-http-request :crowberto :get 200 "ee/workspace-instance/table-remappings")]+ (is (some #(= "orders" (:from_table_name %)) result))))))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.cljnew file mode 100644index 00000000000..bd47eda727a--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_manager_test.clj@@ -0,0 +1,202 @@+(ns metabase-enterprise.workspaces.api.workspace-manager-test+ "HTTP smoke tests for the workspace-manager API. Permission rules are exercised at the+ model level — see workspace_test.clj and workspace_database_test.clj. These tests just+ verify routing, request/response shape, and that the model-level permission predicates+ are wired into the endpoints (one 403 spot-check is enough)."+ (:require+ [clojure.test :refer [deftest is testing use-fixtures]]+ [metabase-enterprise.workspaces.provisioning :as provisioning]+ [metabase.permissions.core :as perms]+ [metabase.permissions.test-util :as perms.test-util]+ [metabase.test :as mt]+ [metabase.test.fixtures :as fixtures]))++(use-fixtures :once (fixtures/initialize :db))++(defn- with-premium-feature [f]+ (mt/with-premium-features #{:workspaces}+ (f)))++(use-fixtures :each with-premium-feature)++(defn- stub-provisioner []+ (reify provisioning/Provisioner+ (init! [_ _ _ _]+ {:schema "mb_iso_stub" :database_details {:user "stub_user" :password "stub_pass"}})+ (grant! [_ _ _ _ _] nil)+ (destroy! [_ _ _ _] nil)))++(deftest crud-smoke-test+ (testing "create, get, list, delete round-trip"+ (mt/with-model-cleanup [:model/Workspace]+ (let [{ws-id :id :as ws} (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"+ {:name "Smoke Test"})]+ (is (=? {:id pos-int? :name "Smoke Test" :databases [] :creator some?}+ ws))++ (testing "get"+ (is (=? {:id ws-id :name "Smoke Test"}+ (mt/user-http-request :crowberto :get 200 (str "ee/workspace-manager/" ws-id)))))++ (testing "list"+ (is (=? [{:id ws-id}]+ (mt/user-http-request :crowberto :get 200 "ee/workspace-manager/"))))++ (testing "delete"+ (is (=? {:id ws-id :deleted true}+ (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 database-endpoints-smoke-test+ (testing "add, update, remove database via HTTP"+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]+ (mt/with-model-cleanup [:model/Workspace]+ (let [{ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"+ {:name "DB Test"})]+ (testing "POST /:id/database adds and provisions"+ (is (=? {:databases [{:database_id (mt/id) :status "provisioned"}]}+ (mt/user-http-request :crowberto :post 200+ (str "ee/workspace-manager/" ws-id "/database")+ {:database_id (mt/id) :input_schemas ["PUBLIC"]}))))++ (testing "PUT /:id/database/:db-id updates input"+ (is (=? {:databases [{:database_id (mt/id)+ :input_schemas ["PUBLIC" "ANALYTICS"]}]}+ (mt/user-http-request :crowberto :put 200+ (str "ee/workspace-manager/" ws-id "/database/" (mt/id))+ {:input_schemas ["PUBLIC" "ANALYTICS"]}))))++ (testing "DELETE /:id/database/:db-id deprovisions and removes"+ (is (=? {:databases empty?}+ (mt/user-http-request :crowberto :delete 200+ (str "ee/workspace-manager/" ws-id "/database/" (mt/id)))))))))))++(deftest add-database-without-schemas-feature-test+ (testing "POST /:id/database accepts an empty :input_schemas list for drivers that support workspaces but not the `:schemas` feature (e.g. MySQL)"+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]+ (mt/with-temp [:model/Database {db-id :id} {:engine :mysql :details {}}]+ (mt/with-model-cleanup [:model/Workspace]+ (let [{ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"+ {:name "Schemaless"})]+ (try+ (is (=? {:databases [{:database_id db-id+ :input_schemas empty?+ :status "provisioned"}]}+ (mt/user-http-request :crowberto :post 200+ (str "ee/workspace-manager/" ws-id "/database")+ {:database_id db-id :input_schemas []})))+ (finally+ ;; Drop the workspace-database row before the `with-temp` rollback+ ;; tries to delete the underlying Database -- the pre-delete hook+ ;; refuses to delete a Database with active workspace_database rows.+ (mt/user-http-request :crowberto :delete 200+ (str "ee/workspace-manager/" ws-id "/database/" db-id))))))))))++(deftest metadata-export-test+ (testing "GET /:id/metadata/export streams metadata scoped to the workspace's databases + input"+ (mt/with-temp [:model/Database {db-id :id db-name :name} {:engine :postgres :details {}}+ :model/Table {t1-id :id} {:db_id db-id :schema "schema-1" :name "table-1" :active true}+ :model/Table {t2-id :id} {:db_id db-id :schema "schema-2" :name "table-2" :active true}+ :model/Field {f1-id :id} {:table_id t1-id :name "field-1" :active true+ :base_type :type/Integer :database_type "BIGINT"}+ :model/Field _ {:table_id t2-id :name "field-2" :active true+ :base_type :type/Text :database_type "TEXT"}+ :model/Workspace {ws-id :id} {:name "Export"+ :creator_id (mt/user->id :crowberto)}+ :model/WorkspaceDatabase _ {:workspace_id ws-id+ :database_id db-id+ :database_details {}+ :output_namespace ""+ ;; schema-2 is deliberately excluded+ :input_schemas ["schema-1"]+ :status :provisioned}]+ ;; Only schema-1's table + field are kept; schema-2's are excluded entirely.+ ;; Length mismatch in any section would fail `=?` — that's how we assert the+ ;; schema filter is doing its job.+ (is (=? {:databases [{:id db-id :name db-name :engine "postgres"}]+ :tables [{:id t1-id :db_id db-id :name "table-1" :schema "schema-1"}]+ :fields [{:id f1-id :table_id t1-id :name "field-1" :base_type "type/Integer"}]}+ (mt/user-http-request :crowberto :get 202+ (str "ee/workspace-manager/" ws-id "/metadata/export")+ :with-databases true+ :with-tables true+ :with-fields true))))))++(defmacro ^:private with-data-analyst [& body]+ `(perms.test-util/with-data-analyst-role! (mt/user->id :rasta) ~@body))++(defmacro ^:private with-workspaces-perm [db-id & body]+ `(perms.test-util/with-db-perm-for-group! (perms/all-users-group) ~db-id+ :perms/workspaces :yes+ ~@body))++(deftest get-endpoints-use-workspace-can-read?-test+ (testing "GET /, GET /:id, and GET /:id/metadata/export dispatch through `Workspace.can-read?` — Data Analyst alone passes"+ (mt/with-temp [:model/Workspace {ws-id :id} {}]+ (with-data-analyst+ (mt/user-http-request :rasta :get 200 "ee/workspace-manager/")+ (mt/user-http-request :rasta :get 200 (str "ee/workspace-manager/" ws-id))+ (mt/user-http-request :rasta :get 202+ (str "ee/workspace-manager/" ws-id "/metadata/export"))))))++(deftest post-workspace-uses-workspace-can-create?-test+ (testing "POST / dispatches through `Workspace.can-create?` — Data Analyst alone is rejected; granting `:perms/workspaces` on any DB lets the call through"+ (mt/with-model-cleanup [:model/Workspace]+ (with-data-analyst+ (mt/user-http-request :rasta :post 403 "ee/workspace-manager/" {:name "Nope"})+ (with-workspaces-perm (mt/id)+ (is (=? {:name "OK"}+ (mt/user-http-request :rasta :post 200 "ee/workspace-manager/" {:name "OK"}))))))))++(deftest workspace-write-endpoints-use-workspace-can-write?-test+ (testing "PUT /:id, DELETE /:id, and GET /:id/config dispatch through `Workspace.can-write?` — Data Analyst alone is rejected (rules out `can-read?`); granting `:perms/workspaces` on any DB lets the call through"+ (mt/with-temp [:model/Workspace {ws-id :id} {}]+ (with-data-analyst+ (mt/user-http-request :rasta :put 403 (str "ee/workspace-manager/" ws-id) {:name "Nope"})+ (mt/user-http-request :rasta :get 403 (str "ee/workspace-manager/" ws-id "/config"))+ (mt/user-http-request :rasta :delete 403 (str "ee/workspace-manager/" ws-id))+ (with-workspaces-perm (mt/id)+ (mt/user-http-request :rasta :put 200 (str "ee/workspace-manager/" ws-id) {:name "OK"})+ (mt/user-http-request :rasta :get 200 (str "ee/workspace-manager/" ws-id "/config"))+ (mt/user-http-request :rasta :delete 200 (str "ee/workspace-manager/" ws-id)))))))++(deftest database-sub-endpoints-use-workspace-database-can-create?-and-can-write?-test+ (testing "POST /:id/database dispatches through `WorkspaceDatabase.can-create?`; PUT/DELETE /:id/database/:db-id dispatch through `WorkspaceDatabase.can-write?` — both keyed to the *specific* `:database_id`, not just any DB the caller has perm on (rules out `Workspace.can-write?`)"+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]+ (mt/with-temp [:model/Database {target-db-id :id} {}+ :model/Database {other-db-id :id} {}+ :model/Workspace {ws-id :id} {}]+ (with-data-analyst+ (with-workspaces-perm target-db-id+ (testing "POST /:id/database — 403 against the no-perm DB"+ (mt/user-http-request :rasta :post 403+ (str "ee/workspace-manager/" ws-id "/database")+ {:database_id other-db-id :input_schemas ["PUBLIC"]}))++ (testing "POST /:id/database — 200 against the perm-holding DB"+ (mt/user-http-request :rasta :post 200+ (str "ee/workspace-manager/" ws-id "/database")+ {:database_id target-db-id :input_schemas ["PUBLIC"]}))++ ;; Attach `other-db-id` so we can target it on PUT / DELETE too.+ (mt/with-temp [:model/WorkspaceDatabase _ {:workspace_id ws-id+ :database_id other-db-id+ :input_schemas ["PUBLIC"]+ :status :provisioned}]+ (testing "PUT /:id/database/:db-id — 403 against the no-perm DB"+ (mt/user-http-request :rasta :put 403+ (str "ee/workspace-manager/" ws-id "/database/" other-db-id)+ {:input_schemas ["ANALYTICS"]}))++ (testing "PUT /:id/database/:db-id — 200 against the perm-holding DB"+ (mt/user-http-request :rasta :put 200+ (str "ee/workspace-manager/" ws-id "/database/" target-db-id)+ {:input_schemas ["PUBLIC"]}))++ (testing "DELETE /:id/database/:db-id — 403 against the no-perm DB"+ (mt/user-http-request :rasta :delete 403+ (str "ee/workspace-manager/" ws-id "/database/" other-db-id)))++ (testing "DELETE /:id/database/:db-id — 200 against the perm-holding DB"+ (mt/user-http-request :rasta :delete 200+ (str "ee/workspace-manager/" ws-id "/database/" target-db-id))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/config_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/config_test.cljnew file mode 100644index 00000000000..7e6a94fb87a--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/config_test.clj@@ -0,0 +1,150 @@+(ns metabase-enterprise.workspaces.config-test+ (:require+ [clojure.test :refer [deftest is testing use-fixtures]]+ [metabase-enterprise.workspaces.config :as config]+ [metabase-enterprise.workspaces.test-util :as workspaces.tu]+ [metabase.test :as mt]+ [metabase.test.fixtures :as fixtures]))++(use-fixtures :once (fixtures/initialize :db))++(deftest build-workspace-config-happy-path-test+ (testing "build-workspace-config returns a {:version 1 :config {...}} structure matching config.yml"+ (mt/with-temp [:model/Database {db-id :id}+ {:name "Analytics Data Warehouse"+ :engine :postgres+ :details {:host "mbdata.metabase.com"+ :port 5432+ :user "admin"+ :dbname "stitchdata_incoming"}}+ :model/Workspace {ws-id :id} {:name "github"+ :creator_id (mt/user->id :crowberto)}+ :model/WorkspaceDatabase _+ {:workspace_id ws-id+ :database_id db-id+ :database_details {:user "mb_isolation_github" :password "secret"}+ :output_namespace "mb_isolation_github"+ :input_schemas ["raw_github"]+ :status :provisioned}]+ (let [cfg (config/build-workspace-config ws-id)]+ (testing "outer shape matches config.yml (version + config block)"+ (is (= 1 (:version cfg)))+ (is (= #{:databases :workspace} (set (keys (:config cfg))))))+ (testing "databases entry"+ (is (= 1 (count (-> cfg :config :databases))))+ (let [db (first (-> cfg :config :databases))]+ (is (= "Analytics Data Warehouse" (:name db)))+ (is (= :postgres (:engine db)))+ (testing "original details are preserved, workspace overrides win, schema-filters appended"+ (is (= {:host "mbdata.metabase.com"+ :port 5432+ :user "mb_isolation_github"+ :password "secret"+ :dbname "stitchdata_incoming"+ :schema-filters-type "inclusion"+ :schema-filters-patterns "raw_github"}+ (:details db))))))+ (testing "workspace entry uses flat input_schemas + opaque output_namespace string"+ (is (= "github" (-> cfg :config :workspace :name)))+ (is (= {"Analytics Data Warehouse"+ {:input_schemas ["raw_github"]+ :output_namespace "mb_isolation_github"}}+ (-> cfg :config :workspace :databases))))))))++(deftest build-workspace-config-three-slot-engine-test+ (when (workspaces.tu/driver-loadable? :sqlserver)+ (testing "SQL Server (3-slot, qualified-name-components=[:db :schema]) gets the :db slot populated from connection details"+ (mt/with-temp [:model/Database {db-id :id}+ {:name "MSSQL DW"+ :engine :sqlserver+ :details {:db "AnalyticsDB" :user "u" :password "p"}}+ :model/Workspace {ws-id :id} {:name "mssql"+ :creator_id (mt/user->id :crowberto)}+ :model/WorkspaceDatabase _+ {:workspace_id ws-id+ :database_id db-id+ :database_details {}+ :output_namespace "ws_alice"+ :input_schemas ["dbo"]+ :status :provisioned}]+ (let [cfg (config/build-workspace-config ws-id)]+ (is (= {"MSSQL DW"+ {:input_schemas ["dbo"]+ :output_namespace "ws_alice"}}+ (-> cfg :config :workspace :databases))))))))++(deftest build-workspace-config-joins-multiple-input-schemas-test+ (testing "Multiple input schemas are comma-joined in schema-filters-patterns"+ (mt/with-temp [:model/Database {db-id :id}+ {:name "DW" :engine :postgres :details {:host "h" :port 5432}}+ :model/Workspace {ws-id :id} {:name "multi"+ :creator_id (mt/user->id :crowberto)}+ :model/WorkspaceDatabase _+ {:workspace_id ws-id+ :database_id db-id+ :database_details {:user "u" :password "p"}+ :output_namespace "out"+ :input_schemas ["schema_a" "schema_b" "schema_c"]+ :status :provisioned}]+ (let [cfg (config/build-workspace-config ws-id)]+ (is (= "schema_a,schema_b,schema_c"+ (-> cfg :config :databases first :details :schema-filters-patterns)))))))++(deftest build-workspace-config-bigquery-emits-dataset-filters-test+ (when (workspaces.tu/driver-loadable? :bigquery-cloud-sdk)+ (testing "BigQuery emits :dataset-filters-* (not :schema-filters-*); BQ's list-datasets reads dataset-filters"+ ;; Stub `:service-account-json` so the resulting config :details has the same+ ;; required shape a production BQ Database row would carry. The BQ driver's+ ;; `database-details->client` (and therefore any downstream `can-connect?`)+ ;; reads `:service-account-json` to mint credentials -- omitting it here+ ;; would make this test pass while the generated config :details would fail+ ;; the real wire-format consumer.+ (mt/with-temp [:model/Database {db-id :id}+ {:name "BQ" :engine :bigquery-cloud-sdk+ :details {:project-id "metabase-prod"+ :service-account-json "{\"type\":\"service_account\",\"project_id\":\"metabase-prod\"}"}}+ :model/Workspace {ws-id :id} {:name "bq-ws"+ :creator_id (mt/user->id :crowberto)}+ :model/WorkspaceDatabase _+ {:workspace_id ws-id+ :database_id db-id+ :database_details {}+ :output_namespace "ws_alice"+ :input_schemas ["core" "warehouse"]+ :status :provisioned}]+ (let [details (-> (config/build-workspace-config ws-id) :config :databases first :details)]+ (is (= "inclusion" (:dataset-filters-type details)))+ (is (= "core,warehouse" (:dataset-filters-patterns details)))+ (is (some? (:service-account-json details))+ "BigQuery config :details must carry :service-account-json -- the BQ driver requires it to mint credentials")+ (is (nil? (:schema-filters-type details))+ "BigQuery must NOT emit :schema-filters-* — those wire into describe-database which BQ doesn't use")+ (is (nil? (:schema-filters-patterns details))))))))++(deftest build-workspace-config-rejects-non-provisioned-test+ (testing "Any non-:provisioned WorkspaceDatabase causes a 409"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "Mixed"+ :creator_id (mt/user->id :crowberto)}+ :model/WorkspaceDatabase _+ {:workspace_id ws-id :database_id (mt/id)+ :database_details {} :output_namespace "" :input_schemas ["public"]+ :status :unprovisioned}]+ (is (thrown-with-msg?+ Exception+ #"not :provisioned"+ (config/build-workspace-config ws-id))))))++(deftest build-workspace-config-empty-workspace-test+ (testing "A workspace with no databases still produces the version+config outer shape"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "Empty"+ :creator_id (mt/user->id :crowberto)}]+ (let [cfg (config/build-workspace-config ws-id)]+ (is (= 1 (:version cfg)))+ (is (= "Empty" (-> cfg :config :workspace :name)))+ (is (= [] (-> cfg :config :databases)))+ (is (= {} (-> cfg :config :workspace :databases)))))))++(deftest build-workspace-config-missing-workspace-returns-nil-test+ (testing "A missing workspace returns nil"+ (mt/with-model-cleanup [:model/Workspace]+ (is (nil? (config/build-workspace-config Integer/MAX_VALUE))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/core_test.cljnew file mode 100644index 00000000000..9368cb4fa5b--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj@@ -0,0 +1,163 @@+(ns metabase-enterprise.workspaces.core-test+ "Tests for the workspace programmatic API and lifecycle rules."+ (:require+ [clojure.test :refer [deftest is testing use-fixtures]]+ [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.provisioning :as provisioning]+ [metabase-enterprise.workspaces.test-util :as workspaces.tu]+ [metabase.test :as mt]+ [metabase.test.fixtures :as fixtures]+ [toucan2.core :as t2]))++(use-fixtures :once (fixtures/initialize :db))++(defn- with-premium-feature [f]+ (mt/with-premium-features #{:workspaces}+ (f)))++(use-fixtures :each with-premium-feature)++;;; Stub provisioner: records calls but doesn't hit real drivers.+(defn- stub-provisioner []+ (reify provisioning/Provisioner+ (init! [_ _ _ _]+ {:schema "mb_iso_stub" :database_details {:user "stub_user" :password "stub_pass"}})+ (grant! [_ _ _ _ _] nil)+ (destroy! [_ _ _ _] nil)))++;;; ----------------------------------------------- CRUD -------------------------------------------------------++(deftest create-workspace-test+ (testing "create with name only (no databases)"+ (mt/with-model-cleanup [:model/Workspace]+ (let [ws (ws/create-workspace! {:name "Test WS"+ :creator_id (mt/user->id :crowberto)})]+ (is (= "Test WS" (:name ws)))+ (is (empty? (:databases ws)))))))++(deftest get-and-list-test+ (testing "get returns nil for non-existent"+ (is (nil? (ws/get-workspace 999999))))++ (testing "get returns hydrated workspace"+ (mt/with-model-cleanup [:model/Workspace]+ (let [ws (ws/create-workspace! {:name "Get Test"+ :creator_id (mt/user->id :crowberto)})+ fetched (ws/get-workspace (:id ws))]+ (is (some? (:creator fetched)))+ (is (= [] (:databases fetched))))))++ (testing "list returns all"+ (mt/with-model-cleanup [:model/Workspace]+ (ws/create-workspace! {:name "A" :creator_id (mt/user->id :crowberto)})+ (ws/create-workspace! {:name "B" :creator_id (mt/user->id :crowberto)})+ (is (>= (count (ws/list-workspaces)) 2)))))++;;; ----------------------------------------- Add/Remove Database ----------------------------------------------++(deftest add-database-test+ (testing "add database provisions immediately"+ (mt/with-model-cleanup [:model/Workspace]+ (let [ws (ws/create-workspace! {:name "Add DB" :creator_id (mt/user->id :crowberto)})+ ws' (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]+ (ws/add-database! (:id ws) (mt/id) ["PUBLIC" "ANALYTICS"]))]+ (is (= 1 (count (:databases ws'))))+ (is (= ["PUBLIC" "ANALYTICS"]+ (:input_schemas (first (:databases ws')))))+ (is (= :provisioned (:status (first (:databases ws'))))))))++ (testing "input required on add"+ (mt/with-model-cleanup [:model/Workspace]+ (let [ws (ws/create-workspace! {:name "No Schema Add" :creator_id (mt/user->id :crowberto)})]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"input_schemas is required"+ (ws/add-database! (:id ws) (mt/id) []))))))++ (testing "input not required when database does not support :schemas (e.g. MySQL)"+ (when (workspaces.tu/driver-loadable? :mysql)+ (mt/with-temp [:model/Database {db-id :id} {:engine :mysql}]+ (mt/with-model-cleanup [:model/Workspace]+ (let [ws (ws/create-workspace! {:name "MySQL Add" :creator_id (mt/user->id :crowberto)})+ ws' (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]+ (ws/add-database! (:id ws) db-id []))]+ (is (= 1 (count (:databases ws'))))+ (is (= [] (:input_schemas (first (:databases ws'))))))))))++ (testing "duplicate database throws 409"+ (mt/with-model-cleanup [:model/Workspace]+ (let [ws (ws/create-workspace! {:name "Dup DB" :creator_id (mt/user->id :crowberto)})]+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]+ (ws/add-database! (:id ws) (mt/id) ["PUBLIC"]))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Database already in workspace"+ (ws/add-database! (:id ws) (mt/id) ["PUBLIC"]))))))++ (testing "provisioning failure rolls the row back so the caller can retry cleanly"+ (mt/with-model-cleanup [:model/Workspace]+ (let [ws (ws/create-workspace! {:name "Roll Back" :creator_id (mt/user->id :crowberto)})+ failing-provisioner (reify provisioning/Provisioner+ (init! [_ _ _ _] (throw (ex-info "boom" {})))+ (grant! [_ _ _ _ _] nil)+ (destroy! [_ _ _ _] nil))]+ (with-redefs [provisioning/dispatching-provisioner failing-provisioner]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"boom"+ (ws/add-database! (:id ws) (mt/id) ["PUBLIC"]))))+ (is (empty? (:databases (ws/get-workspace (:id ws))))+ "the failed add must not leave a workspace_database row behind")))))++(deftest remove-database-test+ (testing "remove deprovisions and deletes"+ (mt/with-model-cleanup [:model/Workspace]+ (let [ws (ws/create-workspace! {:name "Remove DB" :creator_id (mt/user->id :crowberto)})]+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]+ (ws/add-database! (:id ws) (mt/id) ["PUBLIC"])+ (let [ws' (ws/remove-database! (:id ws) (mt/id))]+ (is (empty? (:databases ws'))))))))++ (testing "remove from non-existent workspace throws 404"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Workspace not found"+ (ws/remove-database! 999999 (mt/id))))))++;;; ----------------------------------------- Update Database --------------------------------------------------++(deftest update-database-test+ (testing "update deprovisions old config and reprovisions with new input"+ (mt/with-model-cleanup [:model/Workspace]+ (let [ws (ws/create-workspace! {:name "Update DB" :creator_id (mt/user->id :crowberto)})]+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]+ (ws/add-database! (:id ws) (mt/id) ["PUBLIC"])+ (let [ws' (ws/update-database! (:id ws) (mt/id) ["PUBLIC" "ANALYTICS"])]+ (is (= ["PUBLIC" "ANALYTICS"]+ (:input_schemas (first (:databases ws')))))+ (is (= :provisioned (:status (first (:databases ws')))))))))))++;;; ----------------------------------------- Delete Workspace ------------------------------------------------++(deftest delete-workspace-test+ (testing "delete deprovisions all databases first"+ (mt/with-model-cleanup [:model/Workspace]+ (let [ws (ws/create-workspace! {:name "Delete WS" :creator_id (mt/user->id :crowberto)})]+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]+ (ws/add-database! (:id ws) (mt/id) ["PUBLIC"]))+ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]+ (ws/delete-workspace! (:id ws)))+ (is (nil? (ws/get-workspace (:id ws)))))))++ (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 (nil? (ws/get-workspace (:id ws))))))))++;;; -------------------------------------------- Remappings ----------------------------------------------------++(deftest list-remappings-test+ (mt/with-model-cleanup [:model/TableRemapping]+ (t2/insert! :model/TableRemapping {:database_id (mt/id)+ :from_schema "public"+ :from_table_name "orders"+ :to_schema "mb_iso"+ :to_table_name "orders"})+ (let [remappings (ws/list-remappings)]+ (is (some #(= "orders" (:from_table_name %)) remappings)))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/driver_isolation_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/driver_isolation_test.cljnew file mode 100644index 00000000000..342374008e3--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/driver_isolation_test.clj@@ -0,0 +1,960 @@+(ns ^:mb/driver-tests metabase-enterprise.workspaces.driver-isolation-test+ "Driver-agnostic tests for workspace database isolation. For any driver that+ supports the `:workspace` feature, exercises the full provisioning lifecycle+ (`init-workspace-isolation!` → `grant-workspace-read-access!` →+ `destroy-workspace-isolation!`) against a real warehouse and asserts that the+ workspace user has *exactly* the privileges the design promises:++ - Can SELECT from input tables it has been granted access to.+ - Cannot INSERT/UPDATE/DELETE or run DDL against the input schema.+ - Can SELECT, INSERT, UPDATE, DELETE, and run DDL against its own output+ schema.++ Setup model: the test runs against the existing test database `(mt/db)` and+ creates a per-run uniquely-named source table inside its default input+ schema. Rationale: cloud DWs (Redshift) don't support drop+create of+ databases at will, but creating a per-run table inside a shared DB works+ the same way everywhere.++ Cross-driver counterpart to the postgres-only+ `workspace-user-cannot-write-to-input-schema-test`. The BigQuery sibling test+ lives at `metabase.driver.bigquery-cloud-sdk.workspace-isolation-test` because+ it needs the BigQuery driver module on the classpath."+ (:require+ [clojure.java.jdbc :as jdbc]+ [clojure.test :refer :all]+ [metabase.driver :as driver]+ [metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]+ [metabase.driver.sql.util :as sql.u]+ [metabase.driver.util :as driver.u]+ [metabase.test :as mt]+ [metabase.util :as u]+ [metabase.util.log :as log]))++(set! *warn-on-reflection* true)++(defn- find-sql-exception+ "Find a `java.sql.SQLException` reachable from `t`, or nil. Walks the cause+ chain and also peeks into each level's `ex-data` for Throwable values:+ `clojure.java.jdbc/db-transaction*` (which `execute!` uses by default)+ wraps a caught Throwable in an `ex-info` with the original stashed under+ `:handling` when the rollback path also fails - observed on Redshift,+ where permission-denied inside an implicit transaction surfaces this way+ instead of as a bare SQLException with the SQLException as `.getCause`."+ [^Throwable t]+ (letfn [(walk [^Throwable t seen]+ (cond+ (nil? t) nil+ (contains? seen t) nil+ (instance? java.sql.SQLException t) t+ :else+ (let [seen' (conj seen t)]+ (or (some (fn [v]+ (when (instance? Throwable v)+ (walk v seen')))+ (vals (or (ex-data t) {})))+ (walk (.getCause t) seen')))))]+ (walk t #{})))++(defn- reuse-bound-db-as-input?+ "Drivers where the workspace input namespace IS the bound database, i.e. the+ `:details.db` of the canonical `Database` row. MySQL has no schema-within-DB,+ so the only shipping workspace config (see `workspace_config_mysql.yml`) lists+ the bound DB as the input namespace. The driver-isolation tests mirror that+ shape: source tables live in the bound DB; `grant-workspace-read-access!`+ grants `SELECT ON <bound-db>.*`; the JDBC handshake's DB-access check finds+ the resulting `mysql.db` row and lets the iso user connect with the bound+ DB as the default.++ For these drivers we skip [[create-input-namespace-sql]] (the bound DB already+ exists) and [[drop-input-namespace-sqls]] (we don't own the bound DB, only the+ per-run tables inside it -- caller cleans those up explicitly)."+ [driver]+ (= driver :mysql))++(defn- input-namespace-name+ "Resolve the input-namespace name for a per-run test.+ - On [[reuse-bound-db-as-input?]] drivers (MySQL): return the bound DB.+ `fresh-name` is ignored; source tables go in the bound DB with random+ per-run suffixes so they don't collide.+ - On schema-having drivers: return `fresh-name` -- a freshly-created schema+ inside the bound DB, which the caller will create + drop. Fresh schemas+ are required on Redshift specifically because `public` has `CREATE`+ granted to `PUBLIC` by default, breaking the input-deny contract."+ [driver database fresh-name]+ (if (reuse-bound-db-as-input? driver)+ (-> database :details :db)+ fresh-name))++(defn- create-input-namespace-sql+ "DDL to create a fresh per-run input namespace (a schema for schema'd drivers,+ a database for schema-less ones like MySQL/ClickHouse)."+ [driver namespace-name]+ (case driver+ (:postgres :redshift) (str "CREATE SCHEMA \"" namespace-name "\"")+ :sqlserver (str "CREATE SCHEMA [" namespace-name "]")+ (:mysql :clickhouse) (str "CREATE DATABASE `" namespace-name "`")))++(defn- maybe-create-input-namespace!+ "Create the per-run input namespace UNLESS the caller is using the bound DB as+ the input (the [[reuse-bound-db-as-input?]] path on MySQL) -- in which case the+ namespace already exists and is owned by the test DB, not by us."+ [admin-spec driver database namespace-name]+ (when-not (and (reuse-bound-db-as-input? driver)+ (= namespace-name (-> database :details :db)))+ (jdbc/execute! admin-spec [(create-input-namespace-sql driver namespace-name)])))++(defn- drop-input-namespace-sqls+ "DDL to drop the per-run input namespace and any tables left in it. Schema'd+ drivers with CASCADE (postgres/redshift) and database-as-namespace+ drivers (mysql/clickhouse) take a single statement; SQL Server has no DROP+ SCHEMA CASCADE so each source table has to be dropped explicitly first.++ `table-names` may be a single name or a sequence -- the cross-workspace test+ creates more than one source table in the same input namespace."+ [driver namespace-name table-names]+ (let [tables (if (sequential? table-names) table-names [table-names])]+ (case driver+ (:postgres :redshift) [(str "DROP SCHEMA \"" namespace-name "\" CASCADE")]+ :sqlserver (concat+ (for [t tables]+ (str "DROP TABLE [" namespace-name "].[" t "]"))+ [(str "DROP SCHEMA [" namespace-name "]")])+ (:mysql :clickhouse) [(str "DROP DATABASE `" namespace-name "`")])))++(defn- maybe-drop-input-namespace!+ "Issue [[drop-input-namespace-sqls]] UNLESS the caller is using the bound DB+ as the input (the [[reuse-bound-db-as-input?]] path on MySQL) -- in which+ case we don't own the bound DB and only need to drop the per-run tables+ inside it. Errors are swallowed per-statement so we keep cleaning up after+ a partial-failure run."+ [admin-spec driver database namespace-name table-names]+ (let [tables (if (sequential? table-names) table-names [table-names])]+ (if (and (reuse-bound-db-as-input? driver)+ (= namespace-name (-> database :details :db)))+ (doseq [t tables]+ (try (jdbc/execute! admin-spec [(str "DROP TABLE IF EXISTS `" namespace-name "`.`" t "`")])+ (catch Throwable _ nil)))+ (doseq [sql (drop-input-namespace-sqls driver namespace-name table-names)]+ (try (jdbc/execute! admin-spec [sql]) (catch Throwable _ nil))))))++(defn- list-namespaces-sql+ "SQL that enumerates the namespaces visible to the connecting user. JDBC+ `information_schema.schemata` and ClickHouse's `system.databases` both+ row-level-filter their output by the caller's privileges, so a workspace+ user with no grants on another workspace's namespace should not see it+ appear. Used by the cross-workspace test to check catalog-level isolation+ in addition to data-level isolation."+ [driver]+ (case driver+ (:postgres :redshift :sqlserver :mysql)+ "SELECT schema_name AS ns FROM information_schema.schemata"++ :clickhouse+ "SELECT name AS ns FROM system.databases"))++(defn- list-tables-sql+ "SQL that enumerates `(namespace, table)` pairs visible to the connecting+ user. Stronger than [[list-namespaces-sql]] — even if the namespace itself+ is hidden, an over-broad grant on the tables table would let a workspace+ discover other workspaces' table names."+ [driver]+ (case driver+ (:postgres :redshift :sqlserver :mysql)+ "SELECT table_schema AS ns, table_name AS tbl FROM information_schema.tables"++ :clickhouse+ "SELECT database AS ns, name AS tbl FROM system.tables"))++(defn- visible-namespaces+ "Lowercased set of namespace names visible to `user-spec` through the+ driver's catalog views."+ [driver user-spec]+ (->> (jdbc/query user-spec [(list-namespaces-sql driver)])+ (map (comp u/lower-case-en str :ns))+ set))++(defn- visible-tables+ "Lowercased set of `[namespace table]` pairs visible to `user-spec` through+ the driver's catalog views."+ [driver user-spec]+ (->> (jdbc/query user-spec [(list-tables-sql driver)])+ (map (juxt (comp u/lower-case-en str :ns)+ (comp u/lower-case-en str :tbl)))+ set))++(defn- qualify+ "Per-driver identifier-quoted `schema.table` (or `database.table` for schema-less+ drivers — see [[input-schema]]). Uses `sql.u/quote-name` so identifiers with+ hyphens or other special chars (e.g. MySQL's `test-data` db-name) survive."+ [driver schema table]+ (sql.u/quote-name driver :table schema table))++(defn- create-table-tail+ "Trailing clause appended to `CREATE TABLE ... (cols)` per driver. ClickHouse+ requires every table to declare a storage engine and (for MergeTree-family)+ an ORDER BY key; SQL drivers don't."+ [driver]+ (case driver+ :clickhouse " ENGINE = MergeTree() ORDER BY id"+ ""))++(defn- supports-update-delete-as-perm-test?+ "True for drivers whose UPDATE/DELETE failure on the input schema, and+ UPDATE/DELETE success on the output schema, are meaningful signals about+ workspace permissions. False for column-oriented engines where standard+ UPDATE/DELETE aren't supported regardless of perms (ClickHouse MergeTree+ needs `ALTER TABLE … UPDATE/DELETE` mutation syntax instead)."+ [driver]+ (case driver+ :clickhouse false+ true))++(defn- expect-sql-denied!+ "Assert that executing `sql` against `user-spec` raises a `SQLException`."+ [user-spec sql label]+ (testing (format "%s is denied" label)+ (try+ (jdbc/execute! user-spec [sql])+ (is false (format "%s unexpectedly succeeded" label))+ (catch Throwable t+ (let [sqle (find-sql-exception t)]+ (is (some? sqle)+ (format "expected SQLException for %s; got %s" label (class t))))))))++(defn- verify-jdbc-destroy!+ "Assert post-destroy state for a JDBC workspace: the workspace user can no+ longer open a fresh connection, and the output namespace is gone from the+ admin's catalog. Called right after `destroy-workspace-isolation!` to+ confirm cleanup actually happened.++ `user-spec` is an unpooled JDBC spec — each `jdbc/query` opens a new+ connection, so after destroy the auth handshake must fail (user/role+ dropped on most drivers). Any exception is acceptable; what specifically+ fails is driver-defined."+ [driver admin-spec user-spec out-schema]+ (testing "workspace user cannot open a fresh connection"+ (try+ (jdbc/query user-spec ["SELECT 1"])+ (is false "workspace user unexpectedly succeeded querying after destroy")+ (catch Throwable t+ (is (some? t)+ (format "workspace user denied after destroy: %s" (ex-message t))))))+ (testing "output namespace was dropped"+ (let [visible-after (visible-namespaces driver admin-spec)]+ (is (not (contains? visible-after (u/lower-case-en out-schema)))+ (format "output namespace %s should not appear in catalog after destroy. visible=%s"+ out-schema visible-after)))))++(defn- escalation-attempt-sqls+ "Per-driver list of `[label sql]` pairs that probe the privilege-escalation+ surface — account-/server-/role-level operations the workspace user should+ never be allowed to execute. Each SQL is shaped so denial surfaces as a+ permission error (the user lacks CREATEROLE / CREATE USER / IMPERSONATE /+ ACCOUNTADMIN / etc.) rather than a parse error, so a successful execution+ would indicate a real privilege-escalation bug rather than slip past as a+ benign syntax denial. Names embed `hacker-name` (the test's run-id-derived+ suffix) so the operation name is fresh per run."+ [driver hacker-name]+ (case driver+ :postgres [[:create-role (str "CREATE ROLE \"" hacker-name "\"")]+ [:create-user (str "CREATE USER \"" hacker-name "_u\" PASSWORD 'Pass1234X'")]]+ :redshift [[:create-user (str "CREATE USER \"" hacker-name "\" PASSWORD 'Pass1234X'")]+ [:create-group (str "CREATE GROUP \"" hacker-name "_grp\"")]]+ :mysql [[:create-user (str "CREATE USER '" hacker-name "'@'%' IDENTIFIED BY 'Pass1234X'")]+ [:create-role (str "CREATE ROLE '" hacker-name "_r'")]+ ;; Driver-specific quirk #11: workspace user must not have+ ;; GRANT OPTION on anything. Granting *any* privilege to *any*+ ;; principal requires the granter to hold the priv WITH+ ;; GRANT OPTION, which the workspace user does not.+ [:grant-other "GRANT SELECT ON mysql.* TO 'metabase'@'%'"]]+ :sqlserver [[:create-user (str "CREATE USER [" hacker-name "] WITHOUT LOGIN")]+ [:create-role (str "CREATE ROLE [" hacker-name "_r]")]+ [:execute-as "EXECUTE AS USER = 'dbo'"]]+ :clickhouse [[:create-user (str "CREATE USER `" hacker-name "` IDENTIFIED WITH plaintext_password BY 'Pass1234X'")]+ [:create-role (str "CREATE ROLE `" hacker-name "_r`")]]))++(defn- rename-input-table-sql+ "Per-driver SQL the workspace user attempts when renaming a granted input+ table — RENAME requires ALTER on the table, which the workspace user only+ has SELECT on. Engines spell rename three different ways: `ALTER TABLE …+ RENAME TO …` (postgres / mysql / redshift), `RENAME TABLE …+ TO …` (clickhouse), and `EXEC sp_rename '…', '…'` (sql server)."+ [driver schema src-name new-name]+ (case driver+ :clickhouse (str "RENAME TABLE `" schema "`.`" src-name "` TO `" schema "`.`" new-name "`")+ :sqlserver (str "EXEC sp_rename '[" schema "].[" src-name "]', '" new-name "'")+ (str "ALTER TABLE " (sql.u/quote-name driver :table schema src-name)+ " RENAME TO " (sql.u/quote-name driver :field new-name))))++(defn- storage-escape-sqls+ "Per-driver list of `[label sql]` pairs probing the warehouse's bridges to+ external storage (S3, internal stages, etc.) — the high-value bypass paths+ that would let a workspace user exfiltrate granted-input or output data+ outside the warehouse sandbox. Each SQL is shaped to fail at permission+ check, not parse: if denial flips to success, the workspace user has+ storage perms it shouldn't.++ Drivers without a meaningful storage-bridge primitive (postgres / mysql /+ sqlserver / clickhouse — all of which keep data in-engine without an+ external-storage SQL surface) return an empty list."+ [driver hacker-name]+ (case driver+ :redshift [;; COPY from S3 — needs S3 read perms via IAM_ROLE; workspace user+ ;; has no IAM role assumption privilege.+ [:copy-from-s3 (str "COPY \"" hacker-name "_tmp\" FROM 's3://nonexistent-mb-iso-bucket/data.csv' "+ "IAM_ROLE 'arn:aws:iam::000000000000:role/nonexistent-role'")]+ ;; UNLOAD to S3 — write side of the same path; equally denied.+ [:unload-to-s3 (str "UNLOAD ('SELECT 1') TO 's3://nonexistent-mb-iso-bucket/' "+ "IAM_ROLE 'arn:aws:iam::000000000000:role/nonexistent-role'")]]+ (:postgres :mysql :sqlserver :clickhouse) []))++(defn- random-suffix+ "Eight hex chars from a random UUID. Per-run unique enough to avoid collisions+ from leftover state in the shared test DB."+ []+ (subs (str (random-uuid)) 0 8))++(deftest ^:synchronized workspace-isolation-perms-test+ ;; BigQuery isn't a JDBC driver (its workspace isolation goes through GCP IAM+ ;; rather than SQL ACLs), so it's covered by `workspace-isolation-perms-bigquery-test`+ ;; below. Everything else fans out through this single JDBC-shaped test.+ (mt/test-drivers (filter #(isa? driver/hierarchy % :sql-jdbc)+ (mt/normal-drivers-with-feature :workspace))+ (testing "workspace user gets read-only access to input schema, full access to output schema"+ (let [driver driver/*driver*+ database (mt/db)+ details (:details database)+ admin-spec (sql-jdbc.conn/connection-details->spec driver details)+ ;; Per-run identifiers — the test runs against a shared warehouse DB+ ;; (`(mt/db)`), so anything we create has to be uniquely named or+ ;; it'll collide with leftover state from a prior failed run.+ run-id (random-suffix)+ in-schema (input-namespace-name driver database (str "mb_iso_in_" run-id))+ src-name (str "ws_iso_src_" run-id)+ sneaky-name (str "ws_iso_sneaky_" run-id)+ out-name (str "ws_iso_out_" run-id)+ src (qualify driver in-schema src-name)+ workspace {:id (Long/parseLong run-id 16)+ :name (str "wsd-permstest-" run-id)}+ ;; Pre-init synthetic ws-details for cleanup. Every driver's destroy impl+ ;; derives its actual identifiers from `workspace :id` (via the `driver.u`+ ;; namespace-/user-name helpers), so this skeleton is enough to drive an+ ;; idempotent destroy even if init never ran or only partially succeeded.+ ;; We swap in the real init-result once we have it, but the atom always+ ;; holds *something* destroy can be called with.+ ws-state (atom (merge workspace+ {:schema (driver.u/workspace-isolation-namespace-name workspace)+ :database_details {:user (driver.u/workspace-isolation-user-name workspace)}}))]+ (try+ (maybe-create-input-namespace! admin-spec driver database in-schema)+ (jdbc/execute! admin-spec [(str "CREATE TABLE " src " (id INT, v VARCHAR(8))" (create-table-tail driver))])+ (jdbc/execute! admin-spec [(str "INSERT INTO " src " VALUES (1, 'a')")])+ (let [init-result (driver/init-workspace-isolation! driver database workspace)+ ws-with-details (merge workspace init-result)+ _ (reset! ws-state ws-with-details)+ user-details (merge details (:database_details ws-with-details))+ user-spec (sql-jdbc.conn/connection-details->spec driver user-details)+ out-schema (:schema ws-with-details)+ out (qualify driver out-schema out-name)]+ (driver/grant-workspace-read-access! driver database ws-with-details+ [in-schema])+ (testing "workspace user can SELECT from a granted input table"+ (is (= [{:id 1 :v "a"}]+ (jdbc/query user-spec [(str "SELECT id, v FROM " src " ORDER BY id")]))))+ (testing "workspace user cannot write to or DDL against the input schema"+ ;; Beyond INSERT/UPDATE/DELETE we also exercise ALTER TABLE, TRUNCATE,+ ;; and RENAME — they route through different privilege checks per+ ;; engine (e.g. ALTER ADD COLUMN needs a separate ALTER privilege on+ ;; most warehouses; TRUNCATE on SQL Server requires ALTER TABLE+ ;; rather than DELETE; RENAME spelling differs entirely between+ ;; ClickHouse, SQL Server, and the rest), so a too-broad revoke+ ;; that missed one of these would slip through INSERT-only coverage.+ (let [base-ops [[:insert (str "INSERT INTO " src " VALUES (2, 'b')")]+ [:create-table (str "CREATE TABLE "+ (qualify driver in-schema sneaky-name)+ " (id INT)" (create-table-tail driver))]+ [:drop-table (str "DROP TABLE " src)]+ [:alter-add-col (str "ALTER TABLE " src " ADD COLUMN extra INT")]+ [:truncate (str "TRUNCATE TABLE " src)]+ [:rename (rename-input-table-sql driver in-schema src-name+ (str "ws_iso_renamed_" run-id))]]+ ops (cond-> base-ops+ (supports-update-delete-as-perm-test? driver)+ (into [[:update (str "UPDATE " src " SET v = 'x'")]+ [:delete (str "DELETE FROM " src)]]))]+ (doseq [[label sql] ops]+ (expect-sql-denied! user-spec sql label))))+ (testing "workspace user cannot read from an ungranted namespace"+ (let [ungranted-ns (str "mb_iso_nogrant_" run-id)+ ungranted-tbl (str "ws_iso_secret_" run-id)+ ungranted-fq (qualify driver ungranted-ns ungranted-tbl)]+ (try+ (maybe-create-input-namespace! admin-spec driver database ungranted-ns)+ (jdbc/execute! admin-spec [(str "CREATE TABLE " ungranted-fq " (id INT, secret VARCHAR(8))"+ (create-table-tail driver))])+ (jdbc/execute! admin-spec [(str "INSERT INTO " ungranted-fq " VALUES (1, 'hidden')")])+ (expect-sql-denied! user-spec+ (str "SELECT * FROM " ungranted-fq)+ :select-ungranted-namespace)+ (finally+ (maybe-drop-input-namespace! admin-spec driver database ungranted-ns ungranted-tbl)))))+ (testing "workspace user has full read+write access to its own output schema"+ (jdbc/execute! user-spec [(str "CREATE TABLE " out " (id INT, v VARCHAR(8))" (create-table-tail driver))])+ (jdbc/execute! user-spec [(str "INSERT INTO " out " VALUES (1, 'a')")])+ (is (= [{:id 1 :v "a"}]+ (jdbc/query user-spec [(str "SELECT id, v FROM " out)])))+ (when (supports-update-delete-as-perm-test? driver)+ (jdbc/execute! user-spec [(str "UPDATE " out " SET v = 'b'")])+ (is (= [{:id 1 :v "b"}]+ (jdbc/query user-spec [(str "SELECT id, v FROM " out)])))+ (jdbc/execute! user-spec [(str "DELETE FROM " out)])+ (is (empty? (jdbc/query user-spec [(str "SELECT id, v FROM " out)]))))+ (jdbc/execute! user-spec [(str "DROP TABLE " out)]))+ (testing "re-granting the same input table is idempotent"+ ;; A second grant call with an identical table list should not throw+ ;; (driver impls must handle "GRANT … already exists" type cases) and+ ;; must not change the workspace user's perms — they still SELECT and+ ;; still cannot INSERT. Catches both noisy re-grant failures and silent+ ;; privilege escalation in re-grant code paths.+ (driver/grant-workspace-read-access! driver database ws-with-details+ [in-schema])+ (is (= [{:id 1 :v "a"}]+ (jdbc/query user-spec [(str "SELECT id, v FROM " src " ORDER BY id")])))+ (expect-sql-denied! user-spec+ (str "INSERT INTO " src " VALUES (3, 'c')")+ :insert-after-regrant))+ (testing "workspace user cannot escalate privileges via account-level operations"+ ;; Probes the privilege-escalation surface that a workspace user could+ ;; otherwise use to break out of its sandbox: CREATE USER/ROLE,+ ;; impersonation primitives (`USE ROLE ACCOUNTADMIN`, `EXECUTE AS`),+ ;; and account-/server-level grants. If any of these unexpectedly+ ;; succeeds, the workspace user has more than the read-only-input ++ ;; full-output contract intends.+ (let [hacker-name (str "ws_iso_hacker_" run-id)]+ (doseq [[label sql] (escalation-attempt-sqls driver hacker-name)]+ (expect-sql-denied! user-spec sql label))))+ (testing "workspace user cannot exfiltrate via storage/external escapes"+ ;; Redshift COPY/UNLOAD is the SQL-level bridge to external object+ ;; storage — high-value bypass path because it lets a sandboxed user+ ;; pull data in (COPY) or push data out (UNLOAD) without going through+ ;; grant-mediated tables. The other JDBC drivers (postgres / mysql /+ ;; sqlserver / clickhouse) have no equivalent SQL surface and are+ ;; no-ops here.+ (let [hacker-name (str "ws_iso_storage_" run-id)]+ (doseq [[label sql] (storage-escape-sqls driver hacker-name)]+ (expect-sql-denied! user-spec sql label))))+ (testing "after destroy-workspace-isolation!, the workspace's footprint is gone"+ ;; Explicit destroy here (instead of relying on the `finally`) lets us+ ;; assert that the cleanup actually happened. Drivers' destroy impls are+ ;; idempotent (`IF EXISTS` everywhere), so the `finally` calling destroy+ ;; a second time is a no-op.+ (driver/destroy-workspace-isolation! driver database ws-with-details)+ (verify-jdbc-destroy! driver admin-spec user-spec out-schema)))+ (finally+ ;; Always attempt destroy first — drivers' impls are idempotent (`IF EXISTS`+ ;; everywhere) so this is safe whether init succeeded fully, partially, or+ ;; not at all. Catch+log so a destroy failure doesn't shadow the real+ ;; test failure.+ (try (driver/destroy-workspace-isolation! driver database @ws-state)+ (catch Throwable t+ (log/warnf t "destroy-workspace-isolation! failed for %s during test cleanup"+ driver)))+ ;; Then drop the input namespace.+ (maybe-drop-input-namespace! admin-spec driver database in-schema src-name)))))))++(deftest ^:synchronized cross-workspace-isolation-perms-test+ ;; Exercises *mutual* isolation between two workspaces on the same database — the+ ;; single-workspace happy path lives in `workspace-isolation-perms-test` above. We+ ;; provision A and B together, give each a grant on a different source table, then+ ;; assert that A cannot reach B's data, B's grants, or B's namespace via either+ ;; direct SQL or catalog enumeration.+ ;;+ ;; BigQuery isolation works through GCP IAM rather than SQL grants — see+ ;; `cross-workspace-isolation-perms-bigquery-test` for its sibling.+ ;;+ ;; MySQL/MariaDB excluded: the test premise requires two distinct workspace input+ ;; namespaces on the same Database row, but MySQL has no schema layer -- the only+ ;; shipping config (`workspace_config_mysql.yml`) lists the bound DB as the input,+ ;; so two workspaces on the same Database row would necessarily share input+ ;; namespace. Cross-Database isolation across two separate MySQL servers is a+ ;; different scenario from what this test asserts.+ (mt/test-drivers (->> (mt/normal-drivers-with-feature :workspace)+ (filter #(isa? driver/hierarchy % :sql-jdbc))+ (remove #{:mysql}))+ (testing "two workspaces on the same database are mutually isolated"+ (let [driver driver/*driver*+ database (mt/db)+ details (:details database)+ admin-spec (sql-jdbc.conn/connection-details->spec driver details)+ ;; Three independent random ids: one for the shared input namespace and+ ;; one per workspace, so reruns from leftover state can't collide.+ test-id (random-suffix)+ ws-a-id (random-suffix)+ ws-b-id (random-suffix)+ ;; Each workspace gets its own input namespace. Some drivers+ ;; (notably MySQL — see `grant-workspace-read-access-sqls` in+ ;; `metabase.driver.mysql`) intentionally grant SELECT at the+ ;; database/namespace level rather than per-table, so co-locating+ ;; A's and B's source tables in a single namespace would let+ ;; either workspace SELECT both tables and break the+ ;; `:select-other-grant` assertion below as a *correctly-scoped*+ ;; grant rather than a leak. Splitting per workspace makes the+ ;; assertion driver-portable.+ in-schema-a (str "mb_iso_in_a_" test-id)+ in-schema-b (str "mb_iso_in_b_" test-id)+ src-a-name (str "ws_iso_src_a_" test-id)+ src-b-name (str "ws_iso_src_b_" test-id)+ sneaky-name (str "ws_iso_sneaky_" test-id)+ b-secret (str "ws_iso_secret_" ws-b-id)+ ws-a {:id (Long/parseLong ws-a-id 16)+ :name (str "wsd-A-" ws-a-id)}+ ws-b {:id (Long/parseLong ws-b-id 16)+ :name (str "wsd-B-" ws-b-id)}+ ;; Pre-init synthetic ws-details for cleanup — same idempotent-destroy+ ;; rationale as in the single-workspace test.+ ws-a-state (atom (merge ws-a+ {:schema (driver.u/workspace-isolation-namespace-name ws-a)+ :database_details {:user (driver.u/workspace-isolation-user-name ws-a)}}))+ ws-b-state (atom (merge ws-b+ {:schema (driver.u/workspace-isolation-namespace-name ws-b)+ :database_details {:user (driver.u/workspace-isolation-user-name ws-b)}}))]+ (try+ (maybe-create-input-namespace! admin-spec driver database in-schema-a)+ (maybe-create-input-namespace! admin-spec driver database in-schema-b)+ (jdbc/execute! admin-spec [(str "CREATE TABLE " (qualify driver in-schema-a src-a-name)+ " (id INT, v VARCHAR(8))" (create-table-tail driver))])+ (jdbc/execute! admin-spec [(str "INSERT INTO " (qualify driver in-schema-a src-a-name)+ " VALUES (1, 'a')")])+ (jdbc/execute! admin-spec [(str "CREATE TABLE " (qualify driver in-schema-b src-b-name)+ " (id INT, v VARCHAR(8))" (create-table-tail driver))])+ (jdbc/execute! admin-spec [(str "INSERT INTO " (qualify driver in-schema-b src-b-name)+ " VALUES (1, 'b')")])+ (let [init-a (driver/init-workspace-isolation! driver database ws-a)+ init-b (driver/init-workspace-isolation! driver database ws-b)+ ws-a-full (merge ws-a init-a)+ ws-b-full (merge ws-b init-b)+ _ (reset! ws-a-state ws-a-full)+ _ (reset! ws-b-state ws-b-full)+ user-a-spec (sql-jdbc.conn/connection-details->spec driver (merge details (:database_details ws-a-full)))+ user-b-spec (sql-jdbc.conn/connection-details->spec driver (merge details (:database_details ws-b-full)))+ out-a-schema (:schema ws-a-full)+ out-b-schema (:schema ws-b-full)+ b-secret-fq (qualify driver out-b-schema b-secret)+ sneaky-fq (qualify driver out-b-schema sneaky-name)]+ ;; Each workspace is granted access only to its own input namespace —+ ;; A's grant must not let A read src-b in B's namespace, and vice-versa.+ (driver/grant-workspace-read-access! driver database ws-a-full+ [in-schema-a])+ (driver/grant-workspace-read-access! driver database ws-b-full+ [in-schema-b])+ ;; B populates its own output schema with a table A should never reach.+ (jdbc/execute! user-b-spec [(str "CREATE TABLE " b-secret-fq+ " (id INT, v VARCHAR(8))" (create-table-tail driver))])+ (jdbc/execute! user-b-spec [(str "INSERT INTO " b-secret-fq " VALUES (1, 'b-only')")])+ (testing "workspace A cannot SELECT from workspace B's output table"+ (expect-sql-denied! user-a-spec+ (str "SELECT id, v FROM " b-secret-fq)+ :select-other-output))+ (testing "workspace A cannot write to or DDL against workspace B's output schema"+ (let [base-ops [[:insert (str "INSERT INTO " b-secret-fq " VALUES (2, 'x')")]+ [:create-table (str "CREATE TABLE " sneaky-fq+ " (id INT)" (create-table-tail driver))]+ [:drop-table (str "DROP TABLE " b-secret-fq)]+ [:alter-add-col (str "ALTER TABLE " b-secret-fq " ADD COLUMN extra INT")]+ [:truncate (str "TRUNCATE TABLE " b-secret-fq)]]+ ops (cond-> base-ops+ (supports-update-delete-as-perm-test? driver)+ (into [[:update (str "UPDATE " b-secret-fq " SET v = 'x'")]+ [:delete (str "DELETE FROM " b-secret-fq)]]))]+ (doseq [[label sql] ops]+ (expect-sql-denied! user-a-spec sql label))))+ (testing "workspace A cannot SELECT a source table that was only granted to workspace B"+ (expect-sql-denied! user-a-spec+ (str "SELECT id, v FROM " (qualify driver in-schema-b src-b-name))+ :select-other-grant))+ (testing "workspace A's namespace catalog enumerates A's own namespace but not B's"+ ;; Per-driver quirks in how `information_schema.schemata` (or the+ ;; equivalent system table) is gated by user perms make this check+ ;; only meaningful on a subset of drivers:+ ;; - SQL Server: returns *every* schema in the database regardless+ ;; of user perms, so the isolation half of this assertion would+ ;; spuriously fail (B is visible, but only as a name).+ ;; - Redshift: returns *empty* for non-superusers (catalog grants+ ;; not propagated to information_schema), so the sanity-check+ ;; half (A should see its own schema) would spuriously fail.+ ;; Both are documented driver-side metadata quirks, not isolation+ ;; breaches — the data and table-name assertions (table catalog+ ;; below + cross-grant SELECT above) still cover the real guarantee.+ (when-not (#{:sqlserver :redshift} driver)+ (let [visible (visible-namespaces driver user-a-spec)]+ (is (contains? visible (u/lower-case-en out-a-schema))+ (format "sanity: A should see its own output namespace %s in catalog. visible=%s"+ out-a-schema visible))+ (is (not (contains? visible (u/lower-case-en out-b-schema)))+ (format "A unexpectedly enumerates B's output namespace %s in catalog. visible=%s"+ out-b-schema visible)))))+ (testing "workspace A's table catalog does not enumerate workspace B's output table"+ (let [visible (visible-tables driver user-a-spec)+ pair [(u/lower-case-en out-b-schema)+ (u/lower-case-en b-secret)]]+ (is (not (contains? visible pair))+ (format "A unexpectedly enumerates B's table %s.%s in catalog. visible=%s"+ out-b-schema b-secret visible)))))+ (finally+ (try (driver/destroy-workspace-isolation! driver database @ws-a-state)+ (catch Throwable t+ (log/warnf t "destroy-workspace-isolation! failed for ws-A on %s during cross-workspace test cleanup"+ driver)))+ (try (driver/destroy-workspace-isolation! driver database @ws-b-state)+ (catch Throwable t+ (log/warnf t "destroy-workspace-isolation! failed for ws-B on %s during cross-workspace test cleanup"+ driver)))+ (maybe-drop-input-namespace! admin-spec driver database in-schema-a src-a-name)+ (maybe-drop-input-namespace! admin-spec driver database in-schema-b src-b-name)))))))++(deftest ^:synchronized init-handles-pre-existing-namespace-test+ ;; The output-namespace name init creates is *deterministic* from `workspace.id`+ ;; (see `driver.u/workspace-isolation-namespace-name`), not random — so init can+ ;; land on an already-existing namespace (e.g., from a partial prior init that+ ;; was never cleaned up, or from another process). All current driver impls use+ ;; `CREATE SCHEMA IF NOT EXISTS` (or equivalent), so init silently succeeds when+ ;; the namespace exists. This test pins that behavior: init must not crash on+ ;; collision and the standard read-only-input + full-output contract must still+ ;; hold afterward.+ ;;+ ;; KNOWN LIMITATION: pre-existing data in the colliding namespace is *not*+ ;; cleared by init. If an attacker who can predict the namespace name (the+ ;; transformation in `workspace-isolation-namespace-name` is public) seeds it+ ;; with data before init runs, the workspace user inherits read access to that+ ;; data. Whether to refuse-on-collision or clear-on-collision is a design+ ;; decision tracked separately; this test does not assert one way or the other.+ (mt/test-drivers (filter #(isa? driver/hierarchy % :sql-jdbc) (mt/normal-drivers-with-feature :workspace))+ (testing "init-workspace-isolation! is robust when its target output namespace already exists"+ (let [driver driver/*driver*+ database (mt/db)+ details (:details database)+ admin-spec (sql-jdbc.conn/connection-details->spec driver details)+ run-id (random-suffix)+ in-schema (input-namespace-name driver database (str "mb_iso_in_" run-id))+ src-name (str "ws_iso_src_" run-id)+ out-name (str "ws_iso_out_" run-id)+ src (qualify driver in-schema src-name)+ workspace {:id (Long/parseLong run-id 16)+ :name (str "wsd-collision-" run-id)}+ out-schema (driver.u/workspace-isolation-namespace-name workspace)+ ws-state (atom (merge workspace+ {:schema out-schema+ :database_details {:user (driver.u/workspace-isolation-user-name workspace)}}))]+ (try+ (maybe-create-input-namespace! admin-spec driver database in-schema)+ (jdbc/execute! admin-spec [(str "CREATE TABLE " src " (id INT, v VARCHAR(8))" (create-table-tail driver))])+ (jdbc/execute! admin-spec [(str "INSERT INTO " src " VALUES (1, 'a')")])+ ;; Pre-create the output namespace at exactly the name init will target,+ ;; before init runs.+ (maybe-create-input-namespace! admin-spec driver database out-schema)+ (let [init-result (driver/init-workspace-isolation! driver database workspace)+ ws-with-details (merge workspace init-result)+ _ (reset! ws-state ws-with-details)+ user-spec (sql-jdbc.conn/connection-details->spec driver (merge details (:database_details ws-with-details)))+ out (qualify driver out-schema out-name)]+ (driver/grant-workspace-read-access! driver database ws-with-details+ [in-schema])+ (testing "init succeeded against the pre-existing namespace"+ (is (some? init-result)))+ (testing "workspace user has full read+write access to its output namespace post-collision"+ (jdbc/execute! user-spec [(str "CREATE TABLE " out " (id INT, v VARCHAR(8))" (create-table-tail driver))])+ (jdbc/execute! user-spec [(str "INSERT INTO " out " VALUES (1, 'a')")])+ (is (= [{:id 1 :v "a"}]+ (jdbc/query user-spec [(str "SELECT id, v FROM " out)])))+ (jdbc/execute! user-spec [(str "DROP TABLE " out)]))+ (testing "workspace user retains read-only access to input namespace post-collision"+ (is (= [{:id 1 :v "a"}]+ (jdbc/query user-spec [(str "SELECT id, v FROM " src " ORDER BY id")])))+ (expect-sql-denied! user-spec+ (str "INSERT INTO " src " VALUES (2, 'b')")+ :insert-input-after-collision)))+ (finally+ (try (driver/destroy-workspace-isolation! driver database @ws-state)+ (catch Throwable t+ (log/warnf t "destroy-workspace-isolation! failed for %s during collision test cleanup"+ driver)))+ (maybe-drop-input-namespace! admin-spec driver database in-schema src-name)+ ;; Belt-and-suspenders: if destroy didn't clean up the (originally pre-+ ;; created, then taken-over) output namespace, drop it directly.+ (maybe-drop-input-namespace! admin-spec driver database out-schema [])))))))++(deftest ^:synchronized grant-accumulation-test+ ;; Pins the *additive* contract of `grant-workspace-read-access!`: each call+ ;; adds tables to the workspace user's read-set without revoking previously-+ ;; granted ones. So `grant(ws, [A])` followed by `grant(ws, [B])` leaves the+ ;; workspace user able to SELECT both A and B. (The opposite contract —+ ;; replacing — would revoke A on the second call. We chose additive+ ;; deliberately; this test prevents an accidental flip.)+ ;;+ ;; All current driver impls are naturally additive because they emit+ ;; `GRANT SELECT ON …` statements (or table-level IAM bindings on BigQuery)+ ;; without revoking prior grants. If a driver ever does explicit revoke-then-+ ;; grant, this test will catch the regression.+ (mt/test-drivers (filter #(isa? driver/hierarchy % :sql-jdbc) (mt/normal-drivers-with-feature :workspace))+ (testing "grant-workspace-read-access! is additive across multiple calls"+ (let [driver driver/*driver*+ database (mt/db)+ details (:details database)+ admin-spec (sql-jdbc.conn/connection-details->spec driver details)+ run-id (random-suffix)+ in-schema (input-namespace-name driver database (str "mb_iso_in_" run-id))+ src-a-name (str "ws_iso_src_a_" run-id)+ src-b-name (str "ws_iso_src_b_" run-id)+ src-a (qualify driver in-schema src-a-name)+ src-b (qualify driver in-schema src-b-name)+ workspace {:id (Long/parseLong run-id 16)+ :name (str "wsd-grantaccum-" run-id)}+ ws-state (atom (merge workspace+ {:schema (driver.u/workspace-isolation-namespace-name workspace)+ :database_details {:user (driver.u/workspace-isolation-user-name workspace)}}))]+ (try+ (maybe-create-input-namespace! admin-spec driver database in-schema)+ (jdbc/execute! admin-spec [(str "CREATE TABLE " src-a " (id INT, v VARCHAR(8))" (create-table-tail driver))])+ (jdbc/execute! admin-spec [(str "INSERT INTO " src-a " VALUES (1, 'a')")])+ (jdbc/execute! admin-spec [(str "CREATE TABLE " src-b " (id INT, v VARCHAR(8))" (create-table-tail driver))])+ (jdbc/execute! admin-spec [(str "INSERT INTO " src-b " VALUES (1, 'b')")])+ (let [init-result (driver/init-workspace-isolation! driver database workspace)+ ws-with-details (merge workspace init-result)+ _ (reset! ws-state ws-with-details)+ user-spec (sql-jdbc.conn/connection-details->spec driver (merge details (:database_details ws-with-details)))]+ ;; First grant: only A.+ (driver/grant-workspace-read-access! driver database ws-with-details+ [in-schema])+ (testing "after first grant, A is readable and B is not"+ (is (= [{:id 1 :v "a"}]+ (jdbc/query user-spec [(str "SELECT id, v FROM " src-a)])))+ ;; MySQL and ClickHouse grant at db.* granularity (database-as-namespace+ ;; drivers), so a single grant on the input db happens to allow B too.+ ;; Skip the negative assertion there: this isn't an additivity question,+ ;; it's a grant-granularity question (covered separately in the+ ;; cross-workspace test by using *separate* input namespaces per workspace).+ (when-not (contains? #{:mysql :clickhouse} driver)+ (expect-sql-denied! user-spec+ (str "SELECT id, v FROM " src-b)+ :select-b-before-grant)))+ ;; Second grant: only B. The additive contract means A's grant must+ ;; still be in effect afterward.+ (driver/grant-workspace-read-access! driver database ws-with-details+ [in-schema])+ (testing "after second grant, both A and B are readable (A's grant accumulated)"+ (is (= [{:id 1 :v "a"}]+ (jdbc/query user-spec [(str "SELECT id, v FROM " src-a)])))+ (is (= [{:id 1 :v "b"}]+ (jdbc/query user-spec [(str "SELECT id, v FROM " src-b)])))))+ (finally+ (try (driver/destroy-workspace-isolation! driver database @ws-state)+ (catch Throwable t+ (log/warnf t "destroy-workspace-isolation! failed for %s during grant-accumulation test cleanup"+ driver)))+ (maybe-drop-input-namespace! admin-spec driver database in-schema [src-a-name src-b-name])))))))++;; --- cross-database isolation -----------------------------------------------+;; Workspace tests above all live within a single `(mt/db)` database. SQL Server+;; and BigQuery host *many* databases/projects on one account/server, and a+;; workspace user/SA could in principle reach another database it was never+;; granted on. The drivers without a meaningful "outside-the-current-db" surface+;; (postgres / mysql / clickhouse / redshift — they treat each database as a+;; separate connection and the existing ungranted-namespace assertion in+;; `workspace-isolation-perms-test` already exercises the equivalent isolation)+;; are excluded from this fanout. BigQuery's "different project" requires+;; second-project billing setup we don't have in test infra, so it's deferred+;; to a follow-up.++(defn- supports-cross-database-isolation-test?+ "True for drivers where a second database can be created in the test admin+ connection and the workspace user (created in the first DB) is naturally+ excluded from it. SQL Server (separate user mappings per DB) qualifies; the+ rest don't fit the shape."+ [driver]+ (contains? #{:sqlserver} driver))++(defn- create-second-db-sql+ "DDL the admin connection runs to create a second database used by the+ cross-database test."+ [driver db-name]+ (case driver+ :sqlserver (str "CREATE DATABASE [" db-name "]")))++(defn- drop-second-db-sql+ [driver db-name]+ (case driver+ :sqlserver (str "DROP DATABASE IF EXISTS [" db-name "]")))++(defn- create-table-in-second-db-sqls+ "DDL to create a `secret` table inside the second database. SQL Server's+ default schema after CREATE DATABASE is `dbo`."+ [driver db-name table-name]+ (case driver+ :sqlserver [(str "CREATE TABLE [" db-name "].dbo.[" table-name "] (id INT, secret VARCHAR(64))")+ (str "INSERT INTO [" db-name "].dbo.[" table-name "] VALUES (1, 'cross-db-secret')")]))++(defn- second-db-qualified+ "Driver-specific fully-qualified `db.schema.table` reference used for the+ workspace user's denied SELECT."+ [driver db-name table-name]+ (case driver+ :sqlserver (str "[" db-name "].dbo.[" table-name "]")))++(deftest ^:synchronized cross-database-isolation-test+ ;; Verifies that a workspace user provisioned against `(mt/db)` cannot reach+ ;; data in *another* database created on the same warehouse account/server.+ ;; The threat model: warehouses where one account/server hosts many isolated+ ;; databases. If the workspace user can traverse `db_a.schema.table` →+ ;; `db_b.schema.table` the isolation contract is broken, since the design+ ;; assumes per-database scoping.+ (mt/test-drivers (filter supports-cross-database-isolation-test?+ (mt/normal-drivers-with-feature :workspace))+ (testing "workspace user cannot read tables in a different database on the same account/server"+ (let [driver driver/*driver*+ database (mt/db)+ details (:details database)+ admin-spec (sql-jdbc.conn/connection-details->spec driver details)+ run-id (random-suffix)+ in-schema (input-namespace-name driver database (str "mb_iso_in_" run-id))+ src-name (str "ws_iso_src_" run-id)+ other-db (str "mb_iso_otherdb_" run-id)+ secret-tbl (str "secret_" run-id)+ workspace {:id (Long/parseLong run-id 16)+ :name (str "wsd-crossdb-" run-id)}+ ws-state (atom (merge workspace+ {:schema (driver.u/workspace-isolation-namespace-name workspace)+ :database_details {:user (driver.u/workspace-isolation-user-name workspace)}}))+ second-db-created? (atom false)]+ (try+ (maybe-create-input-namespace! admin-spec driver database in-schema)+ (jdbc/execute! admin-spec [(str "CREATE TABLE " (qualify driver in-schema src-name)+ " (id INT, v VARCHAR(8))" (create-table-tail driver))])+ ;; Provision a second database, create a `secret` table in it, populate.+ (jdbc/execute! admin-spec [(create-second-db-sql driver other-db)])+ (reset! second-db-created? true)+ (doseq [sql (create-table-in-second-db-sqls driver other-db secret-tbl)]+ (jdbc/execute! admin-spec [sql]))+ (let [init-result (driver/init-workspace-isolation! driver database workspace)+ ws-with-details (merge workspace init-result)+ _ (reset! ws-state ws-with-details)+ user-spec (sql-jdbc.conn/connection-details->spec driver (merge details (:database_details ws-with-details)))]+ (driver/grant-workspace-read-access! driver database ws-with-details+ [in-schema])+ (testing "workspace user denied SELECT against a fully-qualified table in another database"+ (expect-sql-denied! user-spec+ (str "SELECT id, secret FROM " (second-db-qualified driver other-db secret-tbl))+ :select-other-database)))+ (finally+ (try (driver/destroy-workspace-isolation! driver database @ws-state)+ (catch Throwable t+ (log/warnf t "destroy-workspace-isolation! failed for %s during cross-database test cleanup"+ driver)))+ (maybe-drop-input-namespace! admin-spec driver database in-schema src-name)+ (when @second-db-created?+ (try (jdbc/execute! admin-spec [(drop-second-db-sql driver other-db)])+ (catch Throwable t+ (log/warnf t "DROP DATABASE %s failed for %s during cross-database test cleanup"+ other-db driver))))))))))++;; --- PUBLIC / anonymous default-grant probes --------------------------------+;; Defense-in-depth tests for the original review's #10 concern: assert that+;; init+grant did not accidentally land any grant on a "default" principal+;; (MySQL's anonymous `''@'%'` user). These are principals that all users+;; implicitly inherit; a stray grant on them would break workspace isolation+;; for every other connecting user.+;;+;; Postgres / Redshift's `public` schema CREATE-grant case is already covered+;; in production by `assert-no-public-create-grant!`. ClickHouse / SQL Server+;; don't have an analogous default-principal threat that's tractable here.++(defn- supports-public-default-grant-test?+ "True for drivers with a meaningful default principal (anonymous user) whose+ grants on workspace resources we want to verify are absent."+ [driver]+ (contains? #{:mysql} driver))++(defn- public-grant-probe-sql+ "Per-driver SQL that, run as admin, returns rows iff the anonymous user holds+ *any* grant whose target name contains `ws-fragment` (the workspace's+ deterministic id-derived suffix). The workspace's role, schema/database, and+ user names all embed `ws-fragment`, so a single LIKE match catches every+ flavor.++ MySQL: `mysql.tables_priv` and `mysql.db` are the system tables that store+ per-table and per-database grants; filtering by `User = ''` finds anonymous-+ user grants. The anonymous user is often disabled on modern installs (no+ row exists), so the query returns 0 rows in the happy path."+ [driver ws-fragment]+ (case driver+ :mysql (str "SELECT CONCAT(Db, '.', COALESCE(Table_name, '*')) AS resource "+ "FROM mysql.tables_priv WHERE User = '' AND Db LIKE '%" ws-fragment "%' "+ "UNION ALL "+ "SELECT Db AS resource FROM mysql.db WHERE User = '' AND Db LIKE '%" ws-fragment "%'")))++(defn- public-grant-pre-query-sql+ "Some drivers need an introspection query run *before* the probe. Returns nil+ for drivers that don't need a setup step."+ [_driver]+ nil)++(deftest ^:synchronized no-public-default-grant-test+ ;; Verifies that after `init-workspace-isolation!` and+ ;; `grant-workspace-read-access!`, no grant has landed on a default principal+ ;; (MySQL's anonymous `''@'%'` user). A stray grant on that principal would+ ;; silently leak the workspace's resources to every other+ ;; user/role on the warehouse.+ ;;+ ;; The probe queries the warehouse's own grants catalog as admin, filtering+ ;; for any row whose target name contains the workspace's deterministic id-+ ;; derived suffix. Workspace role, schema/db, and user names all embed that+ ;; suffix, so a single substring match catches every flavor of accidental+ ;; grant. Empty result = test passes (no leakage); any row = regression.+ (mt/test-drivers (filter supports-public-default-grant-test?+ (mt/normal-drivers-with-feature :workspace))+ (testing "init+grant did not place any grant on PUBLIC / anonymous principal"+ (let [driver driver/*driver*+ database (mt/db)+ details (:details database)+ admin-spec (sql-jdbc.conn/connection-details->spec driver details)+ run-id (random-suffix)+ in-schema (input-namespace-name driver database (str "mb_iso_in_" run-id))+ src-name (str "ws_iso_src_" run-id)+ src (qualify driver in-schema src-name)+ workspace {:id (Long/parseLong run-id 16)+ :name (str "wsd-public-" run-id)}+ ws-fragment (driver.u/workspace-isolation-namespace-name workspace)+ ws-state (atom (merge workspace+ {:schema ws-fragment+ :database_details {:user (driver.u/workspace-isolation-user-name workspace)}}))]+ (try+ (maybe-create-input-namespace! admin-spec driver database in-schema)+ (jdbc/execute! admin-spec [(str "CREATE TABLE " src " (id INT, v VARCHAR(8))" (create-table-tail driver))])+ (let [init-result (driver/init-workspace-isolation! driver database workspace)+ ws-with-details (merge workspace init-result)]+ (reset! ws-state ws-with-details)+ (driver/grant-workspace-read-access! driver database ws-with-details+ [in-schema])+ (testing "no leakage to default principal after init+grant"+ ;; Search both the workspace-namespace name and the workspace-user+ ;; name fragment, since per-driver grants can land on either.+ (let [user-fragment (driver.u/workspace-isolation-user-name workspace)+ leaks (atom #{})]+ (doseq [fragment [ws-fragment user-fragment]]+ (when-let [pre-sql (public-grant-pre-query-sql driver)]+ (try (jdbc/query admin-spec [pre-sql]) (catch Throwable _ nil)))+ (let [rows (try (jdbc/query admin-spec [(public-grant-probe-sql driver fragment)])+ (catch Throwable t+ (log/warnf t "public-grant probe failed on %s; skipping" driver)+ nil))]+ (when (seq rows)+ (swap! leaks into (map :resource rows)))))+ (is (empty? @leaks)+ (format "PUBLIC / anonymous principal unexpectedly has grants on workspace resources: %s"+ @leaks)))))+ (finally+ (try (driver/destroy-workspace-isolation! driver database @ws-state)+ (catch Throwable t+ (log/warnf t "destroy-workspace-isolation! failed for %s during public-grant test cleanup"+ driver)))+ (maybe-drop-input-namespace! admin-spec driver database in-schema src-name)))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.cljnew file mode 100644index 00000000000..edceff0cad5--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj@@ -0,0 +1,819 @@+(ns ^:mb/driver-tests metabase-enterprise.workspaces.e2e-test+ "End-to-end workspace test against the real warehouse for each+ workspace-supported driver.++ Stands up the full pipeline the way `config.yml` does in production:+ provisions an isolation schema + workspace user via+ `workspaces.provisioning/*`, builds the canonical `config.yml` map via+ `workspaces.config/build-workspace-config`, round-trips it through YAML,+ binds it to `advanced-config.file/*config*`, and calls `initialize!` —+ which runs the same `:databases` and `:workspace` section loaders the+ child instance uses at boot. Then it runs a transform whose target gets+ rewritten to the isolation schema, and verifies that visibility from+ Metabase's perspective is confined to the input schema — neither the+ app-db `Table` rows nor `describe-database` should ever surface the+ isolation schema.++ Driver branches: JDBC drivers (Postgres/MySQL/SQL Server/Redshift/+ ClickHouse) seed tables via `jdbc/execute!` and probe via `jdbc/query` against+ an `admin-spec` JDBC db-spec. BigQuery uses the BigQuery Java API via+ `metabase.driver.bigquery-cloud-sdk.workspace-test-util`, loaded at call time+ through `requiring-resolve` -- that test ns lives under+ `modules/drivers/bigquery-cloud-sdk/test/` and isn't on every test-runner+ classpath, so a static `:require` would fail at compile time on non-BQ runs.+ The deftest body stays driver-agnostic via the `case admin-driver` dispatch+ inside each helper below."+ (:require+ [clojure.java.jdbc :as jdbc]+ [clojure.string :as str]+ [clojure.test :refer [deftest is testing]]+ [metabase-enterprise.advanced-config.file :as advanced-config.file]+ [metabase-enterprise.workspaces.config :as ws.config]+ [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.table-remapping :as ws.table-remapping]+ [metabase.driver :as driver]+ [metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]+ [metabase.driver.sql.util :as sql.u]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.sync.core :as sync]+ [metabase.test :as mt]+ [metabase.test.data.sql :as sql.tx]+ [metabase.transforms.execute :as transforms.execute]+ [metabase.util.log :as log]+ [metabase.util.yaml :as yaml]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(defn- random-suffix+ "Eight hex chars from a random UUID — uniquifies the per-run schema/table/user+ identifiers so we don't collide with leftover state in the shared test DB."+ []+ (subs (str (random-uuid)) 0 8))++(def workspaces-supported-drivers+ "Drivers covered by the full e2e. Mirror the set of drivers that declare+ `:workspace true` in their `database-supports?` map -- update both when+ adding a workspace-supported driver.++ JDBC drivers go through `jdbc/execute!`+`jdbc/query`; `:bigquery-cloud-sdk`+ goes through the BQ Java API via+ `metabase.driver.bigquery-cloud-sdk.workspace-test-util` (loaded lazily+ via `requiring-resolve`). Each helper below dispatches on driver.++ The `workspace-full-e2e-test` exercises this whole set. The smaller+ `native-transform-references-prior-canonical-output-test` deliberately+ runs on `#{:postgres :mysql}` only -- not because Redshift/etc.+ aren't workspace-supported, but because that test depends on+ workspace-user `describe-database` returning the source table, and+ on Redshift the workspace user's `describe-database` returns+ `{:tables #{}}` even when USAGE/SELECT grants are intact (root cause+ pending investigation; see that test's docstring). Don't add drivers+ to the smaller test without first fixing the underlying sync visibility."+ #{:postgres :sqlserver :clickhouse :mysql :redshift :bigquery-cloud-sdk})++(defn- three-slot-driver?+ "True when the driver emits `db.schema.table` (SQL Server / BigQuery).+ Drives whether the workspace input/output namespace map populates `:db`."+ [driver]+ (boolean (some #{:db} (driver/qualified-name-components driver))))++(defn- workspace-input-schema+ "Canonical input schema string for `add-database!`. For 2-slot drivers (and+ ClickHouse) it's the schema name. For MySQL — which has no schema layer —+ the same string is interpreted as the database name. For 3-slot drivers+ (SQL Server, BigQuery) the catalog is read from `Database.details`+ inside the driver's `grant!` impl; only the schema is supplied here."+ [_driver _admin-details main-schema]+ main-schema)++;;; -------------------- driver-branched helpers --------------------+;;;+;;; `admin-warehouse` is polymorphic: a JDBC db-spec map for JDBC drivers, a+;;; `BigQuery` Java client for `:bigquery-cloud-sdk`. Helpers dispatch on+;;; `driver` and pick the right access path. The deftest body stays+;;; driver-agnostic.++(defn- admin-warehouse+ "Build whatever `admin-warehouse` value the rest of the helpers need for+ this driver."+ [driver admin-details]+ (case driver+ :bigquery-cloud-sdk ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/admin-client) admin-details)+ (sql-jdbc.conn/connection-details->spec driver admin-details)))++(defn- qualified-table-sql+ "Return the dialect-correct schema-qualified table reference for a native SQL+ string, e.g. `\"PUBLIC\".\"VENUES\"` on Postgres, `` `project.dataset.table` ``+ on BigQuery."+ [driver admin-details schema table]+ (case driver+ :bigquery-cloud-sdk (format "`%s.%s.%s`"+ ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/project-id) admin-details) schema table)+ (sql.u/quote-name driver :table schema table)))++(defn- create-table-tail+ "Driver-specific suffix appended to `CREATE TABLE ...` statements. ClickHouse+ requires every MergeTree-family table to declare a storage engine and (for+ MergeTree-family) an ORDER BY key; SQL drivers don't. Mirror+ `driver-isolation-test/create-table-tail`."+ [driver]+ (case driver+ :clickhouse " ENGINE = MergeTree() ORDER BY id"+ ""))++(defn- create-source-table!+ "Driver-aware CREATE TABLE + INSERT for the e2e source table."+ [driver admin-warehouse admin-details schema table]+ (let [qual (qualified-table-sql driver admin-details schema table)]+ (case driver+ :bigquery-cloud-sdk+ (do+ ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/execute!) admin-warehouse (format "CREATE TABLE %s (id INT64, v STRING)" qual))+ ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/execute!) admin-warehouse (format "INSERT INTO %s (id, v) VALUES (1, 'a'), (2, 'b'), (3, 'c')" qual)))++ (let [int-type (sql.tx/field-base-type->sql-type driver :type/Integer)+ text-type (sql.tx/field-base-type->sql-type driver :type/Text)+ tail (create-table-tail driver)]+ (jdbc/execute! admin-warehouse [(format "CREATE TABLE %s (id %s, v %s)%s" qual int-type text-type tail)])+ (jdbc/execute! admin-warehouse [(format "INSERT INTO %s (id, v) VALUES (1, 'a'), (2, 'b'), (3, 'c')" qual)])))))++(defn- create-output-table!+ "Pre-create the canonical *output* target with `rows` (each `[id v]`). Used+ by the canonical-table-protection test to seed `main_schema.tgt-name` with+ distinct rows before the workspace transform writes to that same name."+ [driver admin-warehouse admin-details schema table rows]+ (let [values (str/join ", " (map (fn [[id v]] (format "(%d, '%s')" id v)) rows))+ qual (qualified-table-sql driver admin-details schema table)]+ (case driver+ :bigquery-cloud-sdk+ (do+ ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/execute!) admin-warehouse (format "CREATE TABLE %s (id INT64, v STRING)" qual))+ ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/execute!) admin-warehouse (format "INSERT INTO %s (id, v) VALUES %s" qual values)))++ (let [int-type (sql.tx/field-base-type->sql-type driver :type/Integer)+ text-type (sql.tx/field-base-type->sql-type driver :type/Text)+ tail (create-table-tail driver)]+ (jdbc/execute! admin-warehouse [(format "CREATE TABLE %s (id %s, v %s)%s" qual int-type text-type tail)])+ (jdbc/execute! admin-warehouse [(format "INSERT INTO %s (id, v) VALUES %s" qual values)])))))++(defn- canonical-schema-name+ "Pick the warehouse \"schema\" (in this test's vocabulary) the canonical input+ tables will live in for this driver.++ Schema-having JDBC drivers + BigQuery get a freshly-created per-run schema /+ dataset. MySQL has no schema-within-database — reuse the bound database;+ concurrency-isolation falls to the per-run table-name suffix."+ [driver run-id admin-details]+ (case driver+ :mysql (:db admin-details)+ (str "canonical_schema_" run-id)))++(defn- table-row-schema-value+ "Translate the warehouse \"schema\" name to the value Metabase stores in+ `:model/Table.schema` for this driver. MySQL reports no schema at all so+ synced Table rows have `:schema nil`. Other drivers store the schema name+ verbatim."+ [driver schema]+ (if (= :mysql driver) nil schema))++(defn- create-canonical-schema!+ "Create the canonical schema/dataset, except on MySQL (the bound database+ already exists)."+ [driver admin-warehouse admin-details schema]+ (case driver+ :mysql nil+ :bigquery-cloud-sdk ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/create-dataset!)+ admin-warehouse+ ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/project-id) admin-details)+ schema)+ ;; ClickHouse calls its top level a "database" — `CREATE SCHEMA` is a+ ;; syntax error there. Same drop counterpart in `drop-canonical-schema!`.+ :clickhouse (jdbc/execute!+ admin-warehouse+ [(format "CREATE DATABASE %s" (sql.u/quote-name driver :schema schema))])+ (jdbc/execute! admin-warehouse+ [(format "CREATE SCHEMA %s" (sql.u/quote-name driver :schema schema))])))++(defn- drop-canonical-schema!+ "Driver-aware schema/dataset teardown for the e2e test's `finally` cleanup."+ [driver admin-warehouse admin-details schema src-name tgt-name]+ (case driver+ :mysql (doseq [t [src-name tgt-name]]+ (try (jdbc/execute! admin-warehouse+ [(format "DROP TABLE IF EXISTS %s"+ (qualified-table-sql driver admin-details schema t))])+ (catch Throwable _ nil)))+ :clickhouse (jdbc/execute! admin-warehouse+ [(format "DROP DATABASE IF EXISTS %s"+ (sql.u/quote-name driver :schema schema))])+ :bigquery-cloud-sdk (try ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/drop-dataset!) admin-warehouse+ ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/project-id) admin-details)+ schema)+ (catch Throwable _ nil))+ (jdbc/execute! admin-warehouse+ [(format "DROP SCHEMA IF EXISTS %s CASCADE"+ (sql.u/quote-name driver :schema schema))])))++(defn- read-canonical-rows+ "Read all rows from `(schema.table)` on the warehouse via admin perms,+ returning a set of `[id v]` vectors. Used by the canonical-table-protection+ assertion."+ [driver admin-warehouse admin-details schema table]+ (case driver+ :bigquery-cloud-sdk+ (->> ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/query) admin-warehouse+ (format "SELECT id, v FROM %s ORDER BY id"+ (qualified-table-sql driver admin-details schema table)))+ (map (fn [{:keys [id v]}] [(Long/parseLong id) v]))+ set)++ (->> (jdbc/query admin-warehouse+ [(format "SELECT id, v FROM %s ORDER BY id"+ (qualified-table-sql driver admin-details schema table))])+ (map (fn [row]+ (let [vs (vals row)]+ (assert (= 2 (count vs))+ "expected 2 columns from canonical select")+ (vec vs))))+ set)))++;; `^:synchronized` because `ws/workspace-instance-config` is a process-wide atom;+;; running concurrently with other workspace-mode tests would cross-pollute.+#_{:clj-kondo/ignore [:metabase/i-like-making-cams-eyes-bleed-with-horrifically-long-tests]}+(defn- with-redshift-describe-filter-disabled+ "Redshift test infra normally filters describe-database to only return tables+ prefixed by dataset name (`<dataset>_<name>`). Workspace e2e creates tables+ with non-conforming names (`x_input_table_<run-id>`), so disable the filter+ for the duration of `thunk`. No-op for non-Redshift drivers."+ [thunk]+ (if (= :redshift driver/*driver*)+ (with-bindings* {(requiring-resolve 'metabase.test.data.redshift/*override-describe-database-to-filter-by-db-name?*)+ false}+ thunk)+ (thunk)))++#_{:clj-kondo/ignore [:metabase/i-like-making-cams-eyes-bleed-with-horrifically-long-tests]}+(deftest ^:synchronized workspace-full-e2e-test+ (mt/test-drivers workspaces-supported-drivers+ (mt/with-premium-features #{:workspaces}+ ;; The default test-time `db-connection-timeout-ms` is 5s. BigQuery's+ ;; first call through a freshly-built impersonated client (cold gRPC+ ;; handshake + impersonation token mint + `listDatasets`) regularly+ ;; needs >5s to return -- `init-from-config-file!`'s+ ;; `can-connect-with-details?` then trips its `do-with-timeout` and+ ;; throws `Timed out after 5.0 s` before the workspace-mode read path+ ;; can warm up. Bumping the timeout matches the production default and+ ;; gives the cold call room to complete.+ (mt/with-temporary-setting-values [db-connection-timeout-ms 30000]+ (with-redshift-describe-filter-disabled+ (fn []+ (testing "transform run on a workspaced DB → app db + describe-database stay in the input schema"+ (let [admin-driver driver/*driver*+ admin-db (mt/db)+ admin-details (:details admin-db)+ admin-spec (admin-warehouse admin-driver admin-details)+ run-id (random-suffix)+ ;; All identifiers carry [[run-id]] — we share a single test DB across+ ;; runs, so any leftover state from a failed run has to be+ ;; distinguishable from this one.+ main-schema (canonical-schema-name admin-driver run-id admin-details)+ ;; Value Metabase stores in `:model/Table.schema` for synced tables.+ ;; Equals [[main-schema]] for schema-having drivers; nil for MySQL.+ tbl-schema (table-row-schema-value admin-driver main-schema)+ src-name (str "x_input_table_" run-id)+ output-table-name (str "x_output_table_" run-id)]+ (try+ ;; --- Setup: DWH main schema + source table ---------------------------+ ;; Schema-creation: most workspace-supported drivers accept `CREATE SCHEMA "<name>"`,+ ;; but MySQL "schemas" are databases. See [[canonical-schema-name]] for why we+ ;; reuse the bound database there instead of creating a fresh one.+ (create-canonical-schema! admin-driver admin-spec admin-details main-schema)+ (create-source-table! admin-driver admin-spec admin-details main-schema src-name)+ ;; Diagnostic: confirm the source table exists in the warehouse before we+ ;; lean on Metabase sync to surface it. If this fails, the problem is in+ ;; the test-side DDL, not workspace code.+ (let [warehouse-tables (case admin-driver+ :bigquery-cloud-sdk ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/list-tables) admin-spec+ ((requiring-resolve 'metabase.driver.bigquery-cloud-sdk.workspace-test-util/project-id) admin-details)+ main-schema)+ ;; No LIMIT/TOP — the source table is seeded with 3+ ;; rows above ([[create-source-table!]]); the assertion+ ;; below only needs >= 1 row, and avoiding `LIMIT`+ ;; keeps this portable across SQL Server (`TOP`) and+ ;; the LIMIT-aware drivers.+ (jdbc/query admin-spec+ [(format "SELECT 1 FROM %s"+ (qualified-table-sql admin-driver admin-details main-schema src-name))]))]+ (is (>= (count warehouse-tables) 1)+ (str "warehouse source table " main-schema "." src-name " is not queryable")))++ ;; TODO: copy this test, but instead of pre-seeding the canonical output table, leave it uncreated and+ ;; verify the workspace transform can create it from scratch. That would be a more direct test of the+ ;; transform's ability to write to the warehouse, without the wrinkle of pre-existing rows. The+ ;; canonical-table-protection test covers the "pre-existing rows stay intact" angle, so we have that+ ;; covered either way.+ ;; --- Setup: pre-existing canonical OUTPUT table with distinct rows ---------+ ;; The workspace transform writes to canonical {schema main-schema, name output-table-name}.+ ;; We seed that table with rows BEFORE the workspace is provisioned so we can+ ;; later verify (a) the workspace's view of the canonical name returns the+ ;; transform's output (via remap), and (b) the canonical warehouse table itself+ ;; was never mutated by the transform - the workspace transform only wrote to+ ;; iso.<derived>, leaving the canonical contents intact.+ (create-output-table! admin-driver admin-spec admin-details main-schema output-table-name+ [[99 "pre-existing"]+ [98 "still-pre-existing"]])++ ;; --- Setup: a Metabase Database row attached to the warehouse with admin+ ;; creds. The config-loader path (below) will rewrite its `:details` with+ ;; workspace user creds + schema-filters when `initialize!` runs the+ ;; `:databases` section, mirroring what a child instance does at boot.+ (mt/with-temp [:model/Database ws-db {:engine admin-driver+ :details admin-details+ :name (str "ws-e2e-" run-id)}]+ (let [{ws-id :id} (ws/create-workspace! {:name (str "ws-e2e-" run-id)+ :creator_id (mt/user->id :crowberto)})]+ (try++ ;; --- Stage 1: provision via the workspace provisioning entrypoint.+ ;; Drives the same `init-workspace-isolation!` + `grant-workspace-read-access!`+ ;; multimethods, but through `provisioning/provision-single!`, which writes+ ;; the resulting `:database_details` and `:output_namespace` back to the+ ;; `WorkspaceDatabase` row — the inputs `build-workspace-config` reads.+ (ws/add-database! ws-id (:id ws-db)+ [(workspace-input-schema admin-driver admin-details main-schema)])+ ;; Sanity check: provisioning must have populated :output_namespace and flipped+ ;; status to :provisioned. If empty, the workspace driver impl is broken.+ (let [wsd (-> (ws/get-workspace ws-id) :databases first)]+ (is (= :provisioned (:status wsd))+ "WorkspaceDatabase provisioning did not reach :provisioned status")+ (is (and (string? (:output_namespace wsd))+ (seq (:output_namespace wsd)))+ (str "provisioning wrote an empty :output_namespace (got "+ (pr-str (:output_namespace wsd)) ")")))++ ;; --- Stage 2: build the canonical config.yml-shaped map and round-trip+ ;; through YAML, the same way a child instance receives the file from disk.+ ;; The round-trip is load-bearing: [[build-workspace-config]] returns+ ;; `:engine :postgres` (keyword), but the `:databases` section spec requires+ ;; a string. [[yaml/parse-string]] or [[yaml/generate-string]] collapses keyword+ ;; values to strings, matching the on-disk wire format.+ (let [yaml-str (-> ws-id+ ws.config/build-workspace-config+ ws.config/config->yaml)+ ;; Read the provisioned isolation schema name back from the WSD row;+ ;; `provision-single!` derives it from the WSD id, not the workspace id.+ wsd (-> (ws/get-workspace ws-id) :databases first)+ isolation-schema (:output_namespace wsd)]++ ;;+---------------------------++ ;;| Parent above |+ ;;+---------------------------+++ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;++ ;;+---------------------------++ ;;| Child below |+ ;;+---------------------------+++ ;; --- Stage 3: bind `*config*` and run the file loader. This invokes+ ;; `init-from-config-file!` for the `:databases` section (updates the+ ;; existing Database row with merged workspace creds + schema-filters)+ ;; and `apply-workspace-section!` for the `:workspace` section (resolves+ ;; db names → ids and populates `ws/workspace-instance-config`).+ (binding [advanced-config.file/*config* (yaml/parse-string yaml-str)]+ (advanced-config.file/initialize!))+ ;; Diagnostic: the loader should have populated the in-process workspace+ ;; atom and rewritten the Database row's :details to workspace-user creds.+ (is (ws/workspace-mode?)+ "loader did not put the instance into workspace-mode (atom not populated)")+ ;; The Database row's `:details` was just rewritten by the loader. Re-read+ ;; it so `mt/with-db` and the connection pool see the workspace-user creds.+ (let [ws-db (t2/select-one :model/Database :id (:id ws-db))+ input-ns-db (when (three-slot-driver? admin-driver) (:db admin-details))]+ (is (not= (:details admin-db) (:details ws-db))+ "loader did not rewrite Database :details with workspace-user creds")+ ;; Regression guard: a colleague caught a pre-fix bug visually by spotting+ ;; the iso DB name in the YAML's :db slot. Pin it down with an assertion so+ ;; any future driver that puts :db into :database_details fails here loudly,+ ;; not 100 lines downstream as "sync produced no rows".+ (is (= (:db admin-details) (:db (:details ws-db)))+ (str "loader must preserve canonical :db on the workspace Database. "+ "If you see the isolation DB here, a driver's "+ "init-workspace-isolation! is putting :db into :database_details, "+ "which the workspace config-loader merges over canonical :details "+ "and breaks the connection's bound database for sync."))+ (mt/with-db ws-db+ (sync/sync-database! ws-db {:scan :schema})+ ;; Diagnostic: enumerate what sync produced so a missing src-table+ ;; gives us a useful error instead of `(some? nil)`.+ (let [synced (->> (t2/select :model/Table :db_id (:id ws-db) :active true)+ (map #(select-keys % [:schema :name])))]+ (is (seq synced)+ "sync produced no Table rows at all — connection or schema-filter is wrong")+ (is (some #(= [tbl-schema src-name] [(:schema %) (:name %)]) synced)+ (str "sync did not surface " main-schema "." src-name+ " (Table.schema expected " (pr-str tbl-schema) ")"+ ". Synced tables: " (pr-str synced))))++ ;; --- Action: define + run a transform ------------------+ (let [src-table (t2/select-one :model/Table+ :db_id (:id ws-db)+ :schema tbl-schema+ :name src-name)+ _ (is (some? src-table)+ "input-schema source table is synced before the transform runs")+ mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (:id src-table)))+ ;; the workspace transform-hook rewrites `:schema` to `isolation-schema`+ canonical-target-table {:type :table+ :schema main-schema+ :name output-table-name}]+ (mt/with-temp [:model/Transform transform+ {:name (str "transform-" run-id)+ :source {:type :query :query query}+ :target canonical-target-table}]+ (transforms.execute/execute! transform {:run-method :manual})+ ;; --- Assertion: app db tables stay in main schema ----+ (testing "app db Table entities are confined to the input schema"+ (let [tables (t2/select :model/Table :db_id (:id ws-db) :active true)+ iso-tbl-schema (table-row-schema-value admin-driver isolation-schema)]+ (is (some #(and (= tbl-schema (:schema %))+ (= src-name (:name %)))+ tables)+ "the input-schema source table appears in the app db")+ (is (some #(and (= tbl-schema (:schema %))+ (= output-table-name (:name %)))+ tables)+ "the input-schema output table appears in the app db")+ (if (= :mysql admin-driver)+ ;; MySQL stores `:schema nil` for every Table row regardless of+ ;; which database it lives in, so we can't distinguish input-DB+ ;; from isolation-DB rows via `:schema`. Instead probe the+ ;; warehouse directly: any tables actually in the isolation DB+ ;; should not show up as app-db Table rows under this Database.+ (let [iso-warehouse-tables+ (->> (jdbc/query admin-spec+ [(str/join ["SELECT table_name "+ "FROM information_schema.tables "+ "WHERE table_schema = ?"])+ isolation-schema])+ (map :table_name)+ set)+ leaked (filter #(contains? iso-warehouse-tables (:name %)) tables)]+ (is (= [] leaked)+ (str "no app-db Table row should point at a table living in the isolation DB " isolation-schema+ " (leaked: " (pr-str (map :name leaked)) ")")))+ (is (= [] (filter #(= iso-tbl-schema (:schema %)) (map #(select-keys % [:schema :name]) tables)))+ "no app-db Table row points at the isolation schema"))))++ (testing "A table remapping record exists"+ ;; `:from_db` is the empty-string sentinel for 2-slot drivers (Postgres,+ ;; Redshift, ClickHouse) and the connection's bound DB for drivers whose+ ;; `qualified-name-components` populates `:db` (SQL Server, BigQuery,+ ;; MySQL).+ ;;+ ;; `:to_db` equals `:from_db` for drivers whose iso namespace lives in+ ;; the SAME DB as canonical (Postgres-family schema-based, SQL Server+ ;; schema-based). On MySQL the iso namespace is a different+ ;; DATABASE, so `:to_db` is the iso DB name (= isolation-schema).+ ;;+ ;; `:from_schema` is the canonical schema name on schema-having drivers,+ ;; and the empty-string sentinel on MySQL (no schema layer).+ (let [from-schema-stored (if (= :mysql admin-driver) "" main-schema)+ to-db-stored (if (= :mysql admin-driver)+ isolation-schema+ (or input-ns-db ""))+ to-schema-stored (if (= :mysql admin-driver) "" isolation-schema)]+ (is (= [{:to_schema to-schema-stored+ :from_schema from-schema-stored+ :from_table_name output-table-name+ :from_db (or input-ns-db "")+ :to_db to-db-stored+ :database_id (:id ws-db)}]+ (for [r (t2/select :model/TableRemapping :database_id (:id ws-db))]+ (select-keys r [:to_schema :from_schema :from_table_name :from_db :to_db :database_id]))))))++ ;; --- Assertion: describe-database stays in main ------+ ;; describe-database reads JDBC's TABLE_SCHEM into `:schema`. For MySQL+ ;; that's always null, same as `:model/Table.schema`. Use `tbl-schema`+ ;; (the per-driver translation we already do for synced rows).+ (testing "describe-database returns only input-schema tables"+ (let [{described :tables} (driver/describe-database admin-driver ws-db)+ iso-tbl-schema (table-row-schema-value admin-driver isolation-schema)]+ (is (some #(and (= tbl-schema (:schema %))+ (= src-name (:name %)))+ described)+ "the input-schema source table is described")+ (if (= :mysql admin-driver)+ ;; On MySQL describe-database only enumerates the connection's+ ;; bound DB (`test-data`), so iso-DB tables can't appear here+ ;; at all. The assertion is trivially true; we still want SOMETHING+ ;; to confirm describe-database isn't returning iso-DB rows+ ;; (e.g., via a cross-DB enumeration bug).+ (let [iso-warehouse-tables (->> (jdbc/query admin-spec+ ["SELECT table_name FROM information_schema.tables WHERE table_schema = ?"+ isolation-schema])+ (map :table_name)+ set)]+ (is (not-any? #(contains? iso-warehouse-tables (:name %)) described)+ "no described table should physically live in the isolation DB"))+ (is (not-any? #(= iso-tbl-schema (:schema %)) described)+ "no isolation-schema table is described"))))++ ;; --- Assertion: a Card querying the canonical output table ----+ ;; reads the remapped (isolation-schema) data, not the canonical+ ;; main-schema table.+ (testing "card query returns the remapped isolation-schema table, not the canonical main-schema table"+ (let [out-table (t2/select-one :model/Table+ :db_id (:id ws-db)+ :schema tbl-schema+ :name output-table-name)+ {:keys [to_table_name]} (t2/select-one :model/TableRemapping+ :database_id (:id ws-db)+ :from_table_name output-table-name)]+ (is (some? out-table)+ "canonical-named output table exists to represent the table that will exist as a result of the new transform running in production")+ ;; Diagnostic: the Card MBQL query downstream needs Fields on+ ;; out-table. If sync-table! after the transform run didn't+ ;; populate them, the QP throws "Table X has no Fields"+ ;; 100 lines into preprocess. Flag it here instead.+ (let [field-count (t2/count :model/Field :table_id (:id out-table) :active true)]+ (is (pos? field-count)+ (str "out-table Table id=" (:id out-table)+ " (schema=" (pr-str (:schema out-table)) " name=" (pr-str (:name out-table)) ")"+ " has no Field rows; transform-time sync did not populate columns")))+ (mt/with-temp [:model/Card card+ {:name (str "ws-e2e-card-" run-id)+ :database_id (:id ws-db)+ :dataset_query {:database (:id ws-db)+ :type :query+ :query {:source-table (:id out-table)}}}]+ (let [rows (set (mt/rows (mt/process-query (:dataset_query card))))]+ (testing "card query returns the transform output"+ (is (= #{[1 "a"] [2 "b"] [3 "c"]} rows)+ "card returns the rows the transform wrote to the isolation schema"))))+ (mt/with-temp [:model/Card card+ {:name (str "ws-e2e-card-native-" run-id)+ :database_id (:id ws-db)+ :dataset_query {:database (:id ws-db)+ :type :native+ :native {:query (format "SELECT * FROM %s"+ (qualified-table-sql admin-driver+ admin-details+ isolation-schema+ to_table_name))}}}]+ (let [rows (set (mt/rows (mt/process-query (:dataset_query card))))]+ (testing "querying the isolation table directly works like querying any other table"+ (is (= #{[1 "a"] [2 "b"] [3 "c"]} rows)))))+ ;; INTENTIONALLY DISABLED (GHY-3580): bare-table-name SQL.+ ;;+ ;; The two `mt/with-temp` blocks immediately above and below this one cover+ ;; the qualified-name flows:+ ;; - above: `SELECT * FROM iso_schema.table` (workspace-side identifier)+ ;; - below: `SELECT * FROM main_schema.table` (canonical identifier;+ ;; Phase 2 rewrites it to iso)+ ;; The skipped variant covers `SELECT * FROM table` -- bare, unqualified.+ ;; That shape isn't supported yet because the Phase 2 SQLGlot rewriter keys+ ;; on `(schema, table)` pairs, and an unqualified ref carries no schema slot+ ;; for the lookup to match. Re-enabling needs the rewriter to attribute+ ;; bare refs to the connection's default schema before lookup. See gist+ ;; https://gist.github.com/escherize/721764240c300e995c54add2d71ff356 for the+ ;; repro and proposed approach.+ ;;+ ;; Re-enable when GHY-3580 lands: delete the `#_`, run the test.+ #_(mt/with-temp [:model/Card card+ {:name (str "ws-e2e-card-native-" run-id)+ :database_id (:id ws-db)+ :dataset_query {:database (:id ws-db)+ :type :native+ :native {:query (format "SELECT * FROM %s"+ (sql.u/quote-name admin-driver :table output-table-name))}}}]+ (let [rows (try (set (mt/rows (mt/process-query (:dataset_query card))))+ (catch Exception e [::exception-thrown e]))]+ (testing "native card query returns the transform output"+ (is (= #{[1 "a"] [2 "b"] [3 "c"]} rows)+ "native card returns the rows the transform wrote to the isolation schema"))))+ (mt/with-temp [:model/Card card+ {:name (str "ws-e2e-card-native-" run-id)+ :database_id (:id ws-db)+ :dataset_query {:database (:id ws-db)+ :type :native+ :native {:query (format "SELECT * FROM %s"+ (qualified-table-sql admin-driver+ admin-details+ main-schema+ output-table-name))}}}]+ (let [rows (set (mt/rows (mt/process-query (:dataset_query card))))]+ (testing "native card query returns the transform output"+ (is (= #{[1 "a"] [2 "b"] [3 "c"]} rows)+ "native card returns the rows the transform wrote to the isolation schema"))))))++ ;; --- Assertion: canonical-table-protection invariant (GHY-3513 item 4) ----+ ;; Pre-seeded canonical `main_schema.output-table-name` with rows A *before* workspace+ ;; provisioning (see `create-output-table!` call at the top of the test). The+ ;; transform writes its output (rows B = src's [1,a],[2,b],[3,c]) to the+ ;; canonical target name, which the transform-hook redirects to iso.<derived>.+ ;; The card reads above already verified workspace queries see rows B (remap+ ;; engaged). What's load-bearing for *this* assertion: the workspace transform+ ;; must NOT have mutated the canonical warehouse table. Probe it directly via+ ;; `admin-spec` (bypasses the QP and workspace mode entirely).+ (testing "the workspace transform does not mutate the canonical warehouse table"+ ;; Probe directly via admin perms (bypasses QP + workspace mode).+ (let [canonical-rows (read-canonical-rows admin-driver admin-spec admin-details+ main-schema output-table-name)]+ (is (= #{[99 "pre-existing"] [98 "still-pre-existing"]}+ canonical-rows)+ "canonical main_schema.output-table-name still has its pre-seeded rows; transform output went to iso.<derived> instead")))++ (testing "app db Table rows stay confined to the input schema after card run"+ (let [tables (t2/select :model/Table :db_id (:id ws-db) :active true)+ iso-tbl-schema (table-row-schema-value admin-driver isolation-schema)]+ (is (some #(and (= tbl-schema (:schema %))+ (= src-name (:name %)))+ tables)+ "the input-schema source table appears in the app db")+ (if (= :mysql admin-driver)+ (let [iso-warehouse-tables (->> (jdbc/query admin-spec+ ["SELECT table_name FROM information_schema.tables WHERE table_schema = ?"+ isolation-schema])+ (map :table_name)+ set)+ leaked (filter #(contains? iso-warehouse-tables (:name %)) tables)]+ (is (empty? leaked)+ (str "no app-db Table row should point at a table in the isolation DB " isolation-schema+ " (leaked: " (pr-str (map :name leaked)) ")")))+ (is (= [] (filter #(= iso-tbl-schema (:schema %)) (map #(select-keys % [:schema :name]) tables)))+ "no app-db Table row points at the isolation schema")))))))))+ (finally+ ;; Clear the in-process workspace atom (populated by `apply-workspace-section!`+ ;; via `initialize!` above) and tear down the WorkspaceDatabase. The+ ;; `:databases` initializer rewrote the `Database.details` to the workspace+ ;; user's creds — but `destroy-workspace-isolation!` needs admin privileges+ ;; (DROP DATABASE / DROP USER on MySQL, etc), so restore admin details first.+ ;; `delete-workspace!` then deprovisions any `:provisioned` databases (calls+ ;; `destroy-workspace-isolation!`) before deleting the row, safe whether+ ;; provision succeeded fully or partially.+ (ws/clear-instance-workspace!)+ (t2/update! :model/Database (:id ws-db) {:details admin-details})+ ;; Cleanup tries [[ws/delete-workspace!]] first. If it throws (e.g. on+ ;; Redshift, [[destroy-workspace-isolation!]] rolls WSD status back to+ ;; `:provisioned` if any cleanup statement fails, and [[delete-workspace!]]+ ;; refuses with 409), fall back to force-clearing the WSD status so+ ;; `mt/with-temp Database` cleanup can complete. Real fix for the+ ;; Redshift destroy fragility is tracked separately.+ (let [delete-result (try (ws/delete-workspace! ws-id) ::deleted-ok+ (catch Throwable t+ (log/warn t "delete-workspace! failed; force-clearing WSD")+ ::delete-failed))]+ (when (= ::delete-failed delete-result)+ (doseq [wsd (t2/select :model/WorkspaceDatabase :workspace_id ws-id)]+ (t2/update! :model/WorkspaceDatabase :id (:id wsd) {:status :unprovisioned}))+ (t2/delete! :model/Workspace :id ws-id)))))))+ (finally+ (try (drop-canonical-schema! admin-driver admin-spec admin-details main-schema src-name output-table-name)+ (catch Throwable _ nil))))))))))))++;; -----------------------------------------------------------------------------+;; Native transform that references a prior MBQL transform's canonical output table.+;;+;; Repro for the user-reported bug: on a workspace-mode child instance, given a+;; Transform A whose target is a canonical `(main_schema, t_a)` (which the+;; transform-hook redirects to iso.<derived>), a *native* Transform B whose SQL+;; references `main_schema.t_a` historically failed at the warehouse with+;; `relation does not exist`. Native transforms bypass the QP middleware+;; pipeline, so Phase 2's `apply-workspace-sql-remapping` never saw their SQL,+;; and the canonical name -- which exists only virtually, backed by the iso+;; copy -- reached the warehouse unrewritten.+;;+;; The fix wires `transforms-base.workspace-hooks/rewrite-native-sql-for-workspace`+;; into `transforms-base.query/run-query-transform!` so native transforms get+;; the same SQL rewrite the QP middleware applies to cards. This test asserts+;; that Transform B succeeds and reads the rows Transform A wrote to the iso+;; copy.+(deftest ^:synchronized native-transform-references-prior-canonical-output-test+ ;; Redshift omitted: `workspace-full-e2e-test` already fails on Redshift because+ ;; `describe-database` returns `{:tables #{}}` for the workspace user even though+ ;; the user has USAGE on the schema and SELECT on the tables (verified directly via+ ;; the same connection pool). The native-transform repro depends on sync seeing the+ ;; source table, so it inherits that failure mode. Investigation pending; unrelated+ ;; to the native-SQL-rewrite hook this test was written to cover.+ (mt/test-drivers #{:postgres :mysql}+ (mt/with-premium-features #{:workspaces}+ (testing "a native transform whose SQL references a prior MBQL transform's canonical target succeeds via the workspace SQL rewriter"+ (let [admin-driver driver/*driver*+ admin-db (mt/db)+ admin-details (:details admin-db)+ admin-spec (admin-warehouse admin-driver admin-details)+ run-id (random-suffix)+ main-schema (canonical-schema-name admin-driver run-id admin-details)+ tbl-schema (table-row-schema-value admin-driver main-schema)+ src-name (str "x_input_" run-id)+ tgt-a-name (str "t_a_" run-id)+ tgt-b-name (str "t_b_" run-id)]+ (try+ (create-canonical-schema! admin-driver admin-spec admin-details main-schema)+ (create-source-table! admin-driver admin-spec admin-details main-schema src-name)+ (mt/with-temp [:model/Database ws-db {:engine admin-driver+ :details admin-details+ :name (str "ws-native-repro-" run-id)}]+ (let [{ws-id :id} (ws/create-workspace! {:name (str "ws-native-repro-" run-id)+ :creator_id (mt/user->id :crowberto)})]+ (try+ (ws/add-database! ws-id (:id ws-db)+ [(workspace-input-schema admin-driver admin-details main-schema)])+ (let [cfg-map (ws.config/build-workspace-config ws-id)+ yaml-str (ws.config/config->yaml cfg-map)+ reparsed (yaml/parse-string yaml-str)]+ (binding [advanced-config.file/*config* reparsed]+ (advanced-config.file/initialize!))+ (let [ws-db (t2/select-one :model/Database :id (:id ws-db))]+ (mt/with-db ws-db+ (sync/sync-database! ws-db {:scan :schema})+ (let [src-table (t2/select-one :model/Table+ :db_id (:id ws-db)+ :schema tbl-schema+ :name src-name)+ _ (is (some? src-table) "input source table is synced")+ mp (mt/metadata-provider)+ mbql-q (lib/query mp (lib.metadata/table mp (:id src-table)))]+ ;; Transform A: MBQL -> writes to canonical (main_schema, t_a).+ ;; Hook redirects target to iso.<derived> and records a TableRemapping+ ;; for the canonical (main_schema, t_a) pair.+ (mt/with-temp [:model/Transform transform-a+ {:name (str "transform-a-" run-id)+ :source {:type :query :query mbql-q}+ :target {:type :table+ :schema main-schema+ :name tgt-a-name}}]+ (transforms.execute/execute! transform-a {:run-method :manual})+ (testing "transform A produced a remap row for its canonical target"+ (let [{from-db :db from-schema :schema from-table :table}+ (ws.table-remapping/spec-for-table+ ws-db {:name tgt-a-name :schema main-schema})]+ (is (some? (t2/select-one :model/TableRemapping+ :database_id (:id ws-db)+ :from_db from-db+ :from_schema from-schema+ :from_table_name from-table))+ "MBQL transform A's canonical target became a remap row")))+ ;; Transform B: native SQL referencing the canonical name of A's+ ;; output. Pre-fix, this would fail at the warehouse with+ ;; `relation does not exist`. Post-fix, `rewrite-native-sql-for-workspace`+ ;; substitutes the iso table name before driver/run-transform!.+ (let [native-sql (format "SELECT count(*) AS n FROM %s"+ (qualified-table-sql admin-driver admin-details main-schema tgt-a-name))]+ (mt/with-temp [:model/Transform transform-b+ {:name (str "transform-b-" run-id)+ :source {:type :query+ :query {:database (:id ws-db)+ :type :native+ :native {:query native-sql}}}+ :target {:type :table+ :schema main-schema+ :name tgt-b-name}}]+ (testing "native transform B referencing A's canonical output runs without warehouse error"+ (let [outcome (try (transforms.execute/execute! transform-b {:run-method :manual})+ :ok+ (catch Throwable t+ [::failed (ex-message t)]))]+ (is (= :ok outcome)+ (str "native transform B failed; outcome=" (pr-str outcome)))))+ (testing "B's iso output table contains the count of A's source rows"+ ;; Iso namespace lives at `:to_schema` (schema-having drivers) or+ ;; `:to_db` (MySQL); pruning empty-string sentinels picks the right one.+ (let [{from-db :db from-schema :schema from-table :table}+ (ws.table-remapping/spec-for-table+ ws-db {:name tgt-b-name :schema main-schema})+ b-row (t2/select-one :model/TableRemapping+ :database_id (:id ws-db)+ :from_db from-db+ :from_schema from-schema+ :from_table_name from-table)+ b-to-table (:to_table_name b-row)+ b-iso-spec (ws.table-remapping/prune-no-level+ {:db (:to_db b-row)+ :schema (:to_schema b-row)})+ iso-namespace (or (:schema b-iso-spec) (:db b-iso-spec))+ rows (jdbc/query admin-spec+ [(format "SELECT n FROM %s"+ (qualified-table-sql admin-driver admin-details iso-namespace b-to-table))])]+ (is (= [{:n 3}] (vec rows))+ "B's iso output reflects rewriter routing B's SELECT to A's iso table"))))))))))+ (finally+ (ws/clear-instance-workspace!)+ (t2/update! :model/Database (:id ws-db) {:details admin-details})+ (try (ws/delete-workspace! ws-id)+ (catch Throwable t+ (log/warn t "delete-workspace! failed during native-transform repro cleanup")))))))+ (finally+ (try (drop-canonical-schema! admin-driver admin-spec admin-details main-schema src-name tgt-a-name)+ (catch Throwable _ nil))+ (try (jdbc/execute! admin-spec+ [(format "DROP TABLE IF EXISTS %s"+ (qualified-table-sql admin-driver admin-details main-schema tgt-b-name))])+ (catch Throwable _ nil)))))))))++(comment+ (workspace-full-e2e-test))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_database_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_database_test.cljnew file mode 100644index 00000000000..aae8dba08d6--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_database_test.clj@@ -0,0 +1,186 @@+(ns metabase-enterprise.workspaces.models.workspace-database-test+ "Tests for the `:model/Database` pre-delete hook that reconciles workspace_database+ rows before deletion.++ Background: `workspace_database.database_id -> metabase_database.id` used to be+ ON DELETE CASCADE, which silently removed the row when the parent Database was+ deleted — orphaning any `:provisioned` warehouse schemas/users because the 409+ guard in `delete-workspace!` was never consulted. We switched the FK to+ RESTRICT, which requires the app-side to reconcile rows explicitly: refuse the+ delete if any row is `:provisioned`, otherwise delete the `:unprovisioned`+ rows so the FK is satisfied."+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.workspaces.models.workspace]+ [metabase-enterprise.workspaces.models.workspace-database]+ [metabase.models.interface :as mi]+ [metabase.permissions.core :as perms]+ [metabase.permissions.test-util :as perms.test-util]+ [metabase.test :as mt]+ [metabase.test.fixtures :as fixtures]+ [toucan2.core :as t2]))++(use-fixtures :once (fixtures/initialize :db))++(defn- with-tracked-ids [f]+ (let [ids (atom {:dbs #{} :workspaces #{} :workspace-dbs #{}})]+ (try+ (f (fn track-db! [id] (swap! ids update :dbs conj id))+ (fn track-ws! [id] (swap! ids update :workspaces conj id))+ (fn track-wsd! [id] (swap! ids update :workspace-dbs conj id)))+ (finally+ ;; Manual cleanup in reverse-FK order: wsd rows first, then workspaces, then dbs.+ (when-let [wsd-ids (seq (:workspace-dbs @ids))]+ (try (t2/delete! :workspace_database :id [:in wsd-ids]) (catch Throwable _ nil)))+ (when-let [ws-ids (seq (:workspaces @ids))]+ (try (t2/delete! :model/Workspace :id [:in ws-ids]) (catch Throwable _ nil)))+ (when-let [db-ids (seq (:dbs @ids))]+ ;; Use raw table to bypass our pre-delete hook during teardown, in case+ ;; the test left behind state the hook would refuse.+ (try (t2/delete! :workspace_database :database_id [:in db-ids]) (catch Throwable _ nil))+ (try (t2/delete! :metabase_database :id [:in db-ids]) (catch Throwable _ nil)))))))++(deftest pre-delete-refuses-database-with-initialized-workspace-databases-test+ (testing "deleting a Database with an :provisioned WorkspaceDatabase child raises 409 and leaves state untouched"+ (mt/with-premium-features #{:workspaces}+ (with-tracked-ids+ (fn [track-db! track-ws! track-wsd!]+ (let [db-id (t2/insert-returning-pk! :model/Database+ {:name "pre-delete-refuse-initialized-db"+ :engine :h2 :details {}+ :created_at :%now :updated_at :%now})+ _ (track-db! db-id)+ ws-id (t2/insert-returning-pk! :model/Workspace+ {:name "pre-delete-refuse-initialized-ws"})+ _ (track-ws! ws-id)+ wsd-id (t2/insert-returning-pk! :model/WorkspaceDatabase+ {:workspace_id ws-id+ :database_id db-id+ :database_details {}+ :output_namespace ""+ :input_schemas []+ :status :provisioned})]+ (track-wsd! wsd-id)+ (let [thrown (try (t2/delete! :model/Database :id db-id) nil+ (catch Throwable t t))]+ (is (some? thrown)+ "deletion of a Database with initialized workspace_database children must throw")+ (is (= 409 (:status-code (ex-data thrown)))+ "the exception must surface as a 409 so the API layer returns Conflict, not 500")+ (is (true? (t2/exists? :model/Database :id db-id))+ "Database row must still exist after the refused delete")+ (is (true? (t2/exists? :model/WorkspaceDatabase :id wsd-id))+ "WorkspaceDatabase row must still exist after the refused delete"))))))))++(deftest pre-delete-cleans-up-uninitialized-workspace-databases-test+ (testing "deleting a Database whose only workspace_database children are :unprovisioned succeeds"+ ;; The FK is RESTRICT; the pre-delete hook must remove those rows so the delete+ ;; can proceed. Semantically this preserves the old CASCADE-like ergonomics for the+ ;; safe case (nothing provisioned on the warehouse side).+ (mt/with-premium-features #{:workspaces}+ (with-tracked-ids+ (fn [track-db! track-ws! track-wsd!]+ (let [db-id (t2/insert-returning-pk! :model/Database+ {:name "pre-delete-cleanup-uninitialized-db"+ :engine :h2 :details {}+ :created_at :%now :updated_at :%now})+ _ (track-db! db-id)+ ws-id (t2/insert-returning-pk! :model/Workspace+ {:name "pre-delete-cleanup-uninitialized-ws"})+ _ (track-ws! ws-id)+ wsd-id (t2/insert-returning-pk! :model/WorkspaceDatabase+ {:workspace_id ws-id+ :database_id db-id+ :database_details {}+ :output_namespace ""+ :input_schemas []+ :status :unprovisioned})]+ (track-wsd! wsd-id)+ (t2/delete! :model/Database :id db-id)+ (is (false? (t2/exists? :model/Database :id db-id))+ "Database row is gone after successful delete")+ (is (false? (t2/exists? :model/WorkspaceDatabase :id wsd-id))+ "pre-delete hook must have removed the uninitialized workspace_database row first")))))))++(deftest fk-restrict-is-defense-in-depth-test+ (testing "FK constraint blocks cascade-delete of an :provisioned workspace_database even if the app-level hook is bypassed"+ ;; `(t2/delete! :metabase_database ...)` against the raw table name bypasses the+ ;; `before-delete` hook (per toucan2 docs). The FK must still refuse the delete so+ ;; the warehouse schema isn't silently orphaned by a path that doesn't know about+ ;; the workspace invariants.+ (mt/with-premium-features #{:workspaces}+ (with-tracked-ids+ (fn [track-db! track-ws! track-wsd!]+ (let [db-id (t2/insert-returning-pk! :model/Database+ {:name "fk-restrict-defense-db"+ :engine :h2 :details {}+ :created_at :%now :updated_at :%now})+ _ (track-db! db-id)+ ws-id (t2/insert-returning-pk! :model/Workspace+ {:name "fk-restrict-defense-ws"})+ _ (track-ws! ws-id)+ wsd-id (t2/insert-returning-pk! :model/WorkspaceDatabase+ {:workspace_id ws-id+ :database_id db-id+ :database_details {}+ :output_namespace ""+ :input_schemas []+ :status :provisioned})]+ (track-wsd! wsd-id)+ (let [thrown (try (t2/delete! :metabase_database :id db-id) nil+ (catch Throwable t t))]+ (is (some? thrown)+ "raw-table delete must still throw — the FK enforces the invariant at the DB layer")+ (is (true? (t2/exists? :model/WorkspaceDatabase :id wsd-id))+ "WorkspaceDatabase row must survive the refused raw delete"))))))))++;;; ========================================= Permission predicates =========================================+;;;+;;; - read: Data Analyst.+;;; - write/create: Data Analyst + `:perms/workspaces :yes` for `:database_id`.++(defmacro ^:private with-normal-user [& body]+ `(mt/with-test-user :rasta ~@body))++(defmacro ^:private with-data-analyst [& body]+ `(perms.test-util/with-data-analyst-role! (mt/user->id :rasta)+ (mt/with-test-user :rasta ~@body)))++(defmacro ^:private with-workspaces-perm [db-id & body]+ `(perms.test-util/with-db-perm-for-group! (perms/all-users-group) ~db-id+ :perms/workspaces :yes+ ~@body))++(deftest can-read?-test+ (testing "WorkspaceDatabase `mi/can-read?`"+ (mt/with-temp [:model/Workspace {ws-id :id} {}+ :model/WorkspaceDatabase wsd {:workspace_id ws-id}]+ (testing "non-Data-Analyst — false"+ (with-normal-user (is (false? (mi/can-read? wsd)))))+ (testing "Data Analyst — true (no per-database check)"+ (with-data-analyst (is (true? (mi/can-read? wsd))))))))++(deftest can-create?-test+ (testing "WorkspaceDatabase `mi/can-create?`"+ (testing "non-Data-Analyst — false"+ (with-normal-user (is (false? (mi/can-create? :model/WorkspaceDatabase {:database_id (mt/id)})))))+ (testing "Data Analyst without `:perms/workspaces` for the target database — false"+ (with-data-analyst+ (is (false? (mi/can-create? :model/WorkspaceDatabase {:database_id (mt/id)})))))+ (testing "Data Analyst with `:perms/workspaces :yes` for the target database — true"+ (with-data-analyst+ (with-workspaces-perm (mt/id)+ (is (true? (mi/can-create? :model/WorkspaceDatabase {:database_id (mt/id)}))))))))++(deftest can-write?-test+ (testing "WorkspaceDatabase `mi/can-write?`"+ (mt/with-temp [:model/Workspace {ws-id :id} {}+ :model/WorkspaceDatabase wsd {:workspace_id ws-id}]+ (testing "non-Data-Analyst — false"+ (with-normal-user (is (false? (mi/can-write? wsd)))))+ (testing "Data Analyst without `:perms/workspaces` for the row's database — false"+ (with-data-analyst (is (false? (mi/can-write? wsd)))))+ (testing "Data Analyst with `:perms/workspaces :yes` for the row's database — true"+ (with-data-analyst+ (with-workspaces-perm (mt/id)+ (is (true? (mi/can-write? wsd)))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_test.cljnew file mode 100644index 00000000000..fa242bf1084--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_test.clj@@ -0,0 +1,300 @@+(ns metabase-enterprise.workspaces.models.workspace-test+ (:require+ [clojure.test :refer [deftest testing is]]+ [metabase-enterprise.workspaces.models.workspace :as workspace]+ [metabase.models.interface :as mi]+ [metabase.permissions.core :as perms]+ [metabase.permissions.test-util :as perms.test-util]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(defn- ws-db-attrs+ ([] (ws-db-attrs {}))+ ([overrides]+ (merge {:database_id (mt/id)+ :database_details {:user "alice" :password "s3cr3t"}+ :output_namespace "ws_out"+ :input_schemas ["public" "analytics"]}+ overrides)))++(defn- create-ws!+ "Test helper: supply a default :creator_id so call sites stay terse."+ [params]+ (workspace/create-workspace! (merge {:creator_id (mt/user->id :crowberto)} params)))++(deftest create-workspace-minimal-test+ (testing "create-workspace! creates a Workspace with no databases"+ (mt/with-model-cleanup [:model/Workspace]+ (let [created (create-ws! {:name "Solo" :databases []})]+ (is (some? (:id created)))+ (is (= "Solo" (:name created)))+ (is (= [] (:databases created)))+ (is (t2/exists? :model/Workspace :id (:id created)))))))++(deftest create-workspace-with-databases-test+ (testing "create-workspace! stores nested workspace_database rows"+ (mt/with-model-cleanup [:model/Workspace]+ (mt/with-temp [:model/Database {db2-id :id} {:engine :h2 :details {}}]+ (let [created (create-ws!+ {:name "With DBs"+ :databases [(ws-db-attrs)+ (ws-db-attrs {:database_id db2-id+ :output_namespace "other_out"+ :input_schemas ["raw"]})]})+ dbs (:databases created)]+ (is (= 2 (count dbs)))+ (is (= #{"ws_out" "other_out"} (into #{} (map :output_namespace) dbs)))+ (testing "JSON columns round-trip with keywordized keys"+ (let [first-db (first (filter #(= "ws_out" (:output_namespace %)) dbs))]+ (is (= {:user "alice" :password "s3cr3t"} (:database_details first-db)))+ (is (= ["public" "analytics"]+ (:input_schemas first-db))))))))))++(deftest get-workspace-test+ (testing "get-workspace returns a hydrated workspace"+ (mt/with-model-cleanup [:model/Workspace]+ (let [{id :id} (create-ws!+ {:name "Fetch Me" :databases [(ws-db-attrs)]})+ fetched (workspace/get-workspace id)]+ (is (= "Fetch Me" (:name fetched)))+ (is (= 1 (count (:databases fetched))))+ (is (= (mt/id) (:database_id (first (:databases fetched))))))))++ (testing "get-workspace returns nil for a missing id"+ (is (nil? (workspace/get-workspace Integer/MAX_VALUE)))))++(deftest list-workspaces-test+ (testing "list-workspaces returns all workspaces with their databases hydrated"+ (mt/with-model-cleanup [:model/Workspace]+ (let [{id-a :id} (create-ws!+ {:name "A" :databases [(ws-db-attrs)]})+ {id-b :id} (create-ws!+ {:name "B" :databases []})+ results (workspace/list-workspaces)+ by-id (into {} (map (juxt :id identity)) results)]+ (is (contains? by-id id-a))+ (is (contains? by-id id-b))+ (is (= 1 (count (:databases (get by-id id-a)))))+ (is (= [] (:databases (get by-id id-b))))))))++(deftest update-workspace-replaces-databases-test+ (testing "update-workspace! replaces the set of workspace_databases"+ (mt/with-model-cleanup [:model/Workspace]+ (mt/with-temp [:model/Database {db2-id :id} {:engine :h2 :details {}}]+ (let [{id :id} (create-ws!+ {:name "Before"+ :databases [(ws-db-attrs {:output_namespace "keep_out"+ :input_schemas ["keep"]})+ (ws-db-attrs {:database_id db2-id+ :output_namespace "drop_out"+ :input_schemas ["drop"]})]})+ updated (workspace/update-workspace!+ id+ {:name "After"+ :databases [(ws-db-attrs {:output_namespace "keep_out"+ :input_schemas ["keep"]})+ (ws-db-attrs {:database_id db2-id+ :output_namespace "new_out"+ :input_schemas ["new"]})]})]+ (is (= "After" (:name updated)))+ (is (= #{"keep_out" "new_out"}+ (into #{} (map :output_namespace) (:databases updated))))+ (testing "the removed workspace_database is gone from the database"+ (is (not (t2/exists? :model/WorkspaceDatabase+ :workspace_id id+ :output_namespace "drop_out")))))))))++(deftest update-workspace-can-clear-databases-test+ (testing "update-workspace! with empty :databases removes all children"+ (mt/with-model-cleanup [:model/Workspace]+ (let [{id :id} (create-ws!+ {:name "To Clear" :databases [(ws-db-attrs)]})+ updated (workspace/update-workspace! id {:name "Cleared" :databases []})]+ (is (= [] (:databases updated)))+ (is (not (t2/exists? :model/WorkspaceDatabase :workspace_id id)))))))++(deftest cascade-delete-workspace-test+ (testing "Deleting a Workspace cascades to its workspace_database rows"+ (mt/with-model-cleanup [:model/Workspace]+ (mt/with-temp [:model/Database {db2-id :id} {:engine :h2 :details {}}]+ (let [{id :id} (create-ws!+ {:name "Doomed"+ :databases [(ws-db-attrs)+ (ws-db-attrs {:database_id db2-id})]})]+ (is (= 2 (t2/count :model/WorkspaceDatabase :workspace_id id)))+ (t2/delete! :model/Workspace :id id)+ (is (zero? (t2/count :model/WorkspaceDatabase :workspace_id id))))))))++(deftest status-defaults-to-uninitialized-test+ (testing "When :status is omitted, workspace_database defaults to :unprovisioned"+ (mt/with-model-cleanup [:model/Workspace]+ (let [created (create-ws!+ {:name "Status Default" :databases [(ws-db-attrs)]})]+ (is (= :unprovisioned (:status (first (:databases created)))))))))++(deftest status-round-trip-test+ (testing "Caller-supplied :status round-trips as a keyword"+ (mt/with-model-cleanup [:model/Workspace]+ (let [created (create-ws!+ {:name "Status Set"+ :databases [(ws-db-attrs {:status :provisioned})]})]+ (is (= :provisioned (:status (first (:databases created)))))+ (testing "and the column persists as a string in the database"+ (is (= "provisioned"+ (:status (t2/query-one {:select [:status]+ :from [:workspace_database]+ :where [:= :workspace_id (:id created)]})))))))))++(deftest status-can-be-updated-test+ (testing "update-workspace! can change the :status of a workspace_database"+ (mt/with-model-cleanup [:model/Workspace]+ (let [{id :id} (create-ws!+ {:name "Evolving"+ :databases [(ws-db-attrs {:status :unprovisioned})]})+ updated (workspace/update-workspace!+ id+ {:name "Evolving"+ :databases [(ws-db-attrs {:status :provisioned})]})]+ (is (= :provisioned (:status (first (:databases updated)))))))))++(deftest update-preserves-initialized-rows-test+ (testing "update-workspace! leaves :provisioned rows untouched when the PUT keeps their database_id + input"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase {init-id :id}+ {:workspace_id ws-id+ :database_id (mt/id)+ :database_details {:user "keep-me" :password "keep-pw"}+ :output_namespace "keep_schema"+ :input_schemas ["public"]+ :status :provisioned}]+ (let [updated (workspace/update-workspace!+ ws-id+ {:name "Renamed"+ :databases [{:database_id (mt/id) :input_schemas ["public"]}]})+ row (t2/select-one :model/WorkspaceDatabase :id init-id)]+ (testing "name update takes effect"+ (is (= "Renamed" (:name updated))))+ (testing "the initialized row was not deleted + reinserted (its id is the same)"+ (is (some? row))+ (is (= :provisioned (:status row))))+ (testing "credentials and output_namespace are preserved verbatim"+ (is (= {:user "keep-me" :password "keep-pw"} (:database_details row)))+ (is (= "keep_schema" (:output_namespace row))))))))++(deftest update-rejects-dropping-initialized-test+ (testing "update-workspace! refuses to drop an :provisioned row"+ (mt/with-temp [:model/Database {db2-id :id} {:engine :h2 :details {}}+ :model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase _+ {:workspace_id ws-id+ :database_id (mt/id)+ :database_details {:user "u"}+ :output_namespace "sch"+ :input_schemas ["public"]+ :status :provisioned}]+ (is (thrown-with-msg?+ Exception+ #"provisioned"+ (workspace/update-workspace!+ ws-id+ {:name "WS" :databases [{:database_id db2-id :input_schemas ["public"]}]}))))))++(deftest update-rejects-changing-initialized-input-test+ (testing "update-workspace! refuses to change :input_schemasof an :provisioned row"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase _+ {:workspace_id ws-id+ :database_id (mt/id)+ :database_details {:user "u"}+ :output_namespace "sch"+ :input_schemas ["public"]+ :status :provisioned}]+ (is (thrown-with-msg?+ Exception+ #"provisioned"+ (workspace/update-workspace!+ ws-id+ {:name "WS"+ :databases [{:database_id (mt/id) :input_schemas ["analytics"]}]}))))))++(deftest cascade-delete-database-test+ (testing "Deleting an underlying Database cascades to workspace_database rows"+ (mt/with-model-cleanup [:model/Workspace]+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2 :details {}}]+ (let [{ws-id :id} (create-ws!+ {:name "Linked"+ :databases [(ws-db-attrs {:database_id db-id})]})]+ (is (t2/exists? :model/WorkspaceDatabase :workspace_id ws-id))+ (t2/delete! :model/Database :id db-id)+ (is (not (t2/exists? :model/WorkspaceDatabase :workspace_id ws-id))))))))++;;; ========================================= Permission predicates =========================================+;;;+;;; - read: Data Analyst + can-read? for every attached WorkspaceDatabase.+;;; - create: Data Analyst + `:perms/workspaces :yes` on any database.+;;; - write: Data Analyst + `:perms/workspaces :yes` on any database+;;; + can-write? for every attached WorkspaceDatabase.+;;; Admins always pass via the underlying superuser bypass.++(defmacro ^:private with-normal-user [& body]+ `(mt/with-test-user :rasta ~@body))++(defmacro ^:private with-data-analyst [& body]+ `(perms.test-util/with-data-analyst-role! (mt/user->id :rasta)+ (mt/with-test-user :rasta ~@body)))++(defmacro ^:private with-workspaces-perm [db-id & body]+ `(perms.test-util/with-db-perm-for-group! (perms/all-users-group) ~db-id+ :perms/workspaces :yes+ ~@body))++(deftest can-read?-test+ (testing "Workspace `mi/can-read?`"+ (mt/with-temp [:model/Workspace ws {}]+ (testing "non-Data-Analyst — false"+ (with-normal-user (is (false? (mi/can-read? ws)))))+ (testing "Data Analyst, empty workspace — true"+ (with-data-analyst (is (true? (mi/can-read? ws))))))++ (mt/with-temp [:model/Workspace {ws-id :id :as ws} {}+ :model/WorkspaceDatabase _ {:workspace_id ws-id}]+ (testing "Data Analyst with attached database — every WSD `can-read?` is satisfied (analyst-level)"+ (with-data-analyst (is (true? (mi/can-read? ws))))))))++(deftest can-create?-test+ (testing "Workspace `mi/can-create?`"+ (testing "non-Data-Analyst — false"+ (with-normal-user (is (false? (mi/can-create? :model/Workspace {})))))+ (testing "Data Analyst alone (no `:perms/workspaces` anywhere) — false"+ (with-data-analyst (is (false? (mi/can-create? :model/Workspace {})))))+ (testing "Data Analyst + `:perms/workspaces :yes` on any database — true"+ (with-data-analyst+ (with-workspaces-perm (mt/id)+ (is (true? (mi/can-create? :model/Workspace {}))))))))++(deftest can-write?-test+ (testing "Workspace `mi/can-write?`"+ (mt/with-temp [:model/Workspace ws {}]+ (testing "non-Data-Analyst — false"+ (with-normal-user (is (false? (mi/can-write? ws)))))+ (testing "Data Analyst alone, even on an empty workspace — false (need workspaces perm somewhere)"+ (with-data-analyst (is (false? (mi/can-write? ws)))))+ (testing "Data Analyst + `:perms/workspaces :yes` on any database, empty workspace — true"+ (with-data-analyst+ (with-workspaces-perm (mt/id)+ (is (true? (mi/can-write? ws)))))))++ (mt/with-temp [:model/Database {db1-id :id} {}+ :model/Database {db2-id :id} {}+ :model/Workspace {ws-id :id :as ws} {}+ :model/WorkspaceDatabase _ {:workspace_id ws-id, :database_id db1-id}+ :model/WorkspaceDatabase _ {:workspace_id ws-id, :database_id db2-id}]+ (testing "Data Analyst + perm on ONE of two attached databases — false"+ (with-data-analyst+ (with-workspaces-perm db1-id+ (is (false? (mi/can-write? ws))))))+ (testing "Data Analyst + perm on BOTH attached databases — true"+ (with-data-analyst+ (with-workspaces-perm db1-id+ (with-workspaces-perm db2-id+ (is (true? (mi/can-write? ws))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/corpus_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/corpus_test.cljnew file mode 100644index 00000000000..a21f85b2e7c--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/corpus_test.clj@@ -0,0 +1,357 @@+(ns metabase-enterprise.workspaces.query-processor.corpus-test+ "Test runner for the workspace remapping SQL corpus.++ Reads test cases from `test_resources/remapping_corpus/*.sql`, applies remappings+ via `sql-tools/replace-names`, and verifies the output contains no production refs.++ Corpus file format (see postgres.sql for examples):+ -- ;;;; <- separator+ -- remap: from_schema.table to_schema.table+ -- tags: simple, join, cte <- for filtering+ -- expect: ok | parse-error <- expected outcome+ <SQL query>"+ (:require+ [clojure.java.io :as io]+ [clojure.string :as str]+ [clojure.test :refer :all]+ [metabase-enterprise.advanced-config.file.workspace :as advanced-config.file.workspace]+ [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.table-remapping :as ws.table-remapping]+ [metabase-enterprise.workspaces.test-util :as workspaces.tu]+ [metabase.driver :as driver]+ [metabase.sql-tools.core :as sql-tools]+ [metabase.test :as mt]+ [metabase.util.yaml :as yaml]))++(set! *warn-on-reflection* true)++;;; ------------------------------------------------ Parser ----------------------------------------------------++(defn- parse-remap-directive+ "Parse '-- remap: from_schema.table to_schema.table' into [[from-schema from-table] [to-schema to-table]]."+ [line]+ (let [[_ from to] (re-matches #"--\s*remap:\s+(\S+)\s+(\S+)" line)]+ (when (and from to)+ (let [[fs ft] (str/split from #"\." 2)+ [ts tt] (str/split to #"\." 2)]+ [[fs ft] [ts tt]]))))++(defn- parse-tags-directive+ "Parse '-- tags: foo, bar, baz' into a set of keyword tags."+ [line]+ (let [[_ tags-str] (re-matches #"--\s*tags:\s+(.*)" line)]+ (when tags-str+ (into #{} (map (comp keyword str/trim)) (str/split tags-str #",")))))++(defn- parse-expect-directive+ "Parse '-- expect: ok | parse-error' into a keyword."+ [line]+ (let [[_ expect] (re-matches #"--\s*expect:\s+(\S+)" line)]+ (when expect+ (keyword expect))))++(defn- parse-corpus-entry+ "Parse a single corpus entry (lines between separators) into a test case map."+ [lines]+ (let [remappings (atom {})+ tags (atom #{})+ expect (atom :ok)+ sql-lines (atom [])]+ (doseq [line lines]+ (if-let [remap (parse-remap-directive line)]+ (swap! remappings conj remap)+ (if-let [t (parse-tags-directive line)]+ (reset! tags t)+ (if-let [e (parse-expect-directive line)]+ (reset! expect e)+ ;; Skip blank comment-only lines, collect SQL+ (when-not (re-matches #"--.*" line)+ (swap! sql-lines conj line))))))+ (let [sql (str/trim (str/join "\n" @sql-lines))]+ (when (seq sql)+ {:remappings @remappings+ :tags @tags+ :expect @expect+ :sql sql}))))++(defn- parse-corpus-file+ "Parse a corpus .sql file into a seq of test case maps."+ [resource-path]+ (let [content (slurp (io/resource resource-path))+ ;; Split on "-- ;;;;" separator, drop leading empty chunk+ chunks (rest (str/split content #"(?m)^--\s*;;;;+\s*$"))]+ (keep (fn [chunk]+ (let [lines (str/split-lines (str/trim chunk))]+ (parse-corpus-entry lines)))+ chunks)))++;;; ---------------------------------------------- Test runner -------------------------------------------------++(defn- build-replacements+ "Build the replacements map for sql-tools/replace-names from a remappings map."+ [remappings]+ {:tables (into {}+ (map (fn [[[from-schema from-table] [to-schema to-table]]]+ [{:schema from-schema :table from-table}+ {:schema to-schema :table to-table}]))+ remappings)})++(defn- from-ref-pattern+ "Build a regex that matches any 'from' schema.table reference in SQL output."+ [remappings]+ (let [patterns (map (fn [[[from-schema from-table] _]]+ (str "(?i)"+ (java.util.regex.Pattern/quote from-schema)+ "\\s*\\.\\s*"+ (java.util.regex.Pattern/quote from-table)))+ remappings)]+ (when (seq patterns)+ (re-pattern (str/join "|" patterns)))))++(defn- run-corpus-entry+ "Run a single corpus test case. Returns nil on success, error string on failure."+ [driver {:keys [remappings tags expect sql]}]+ (try+ (let [replacements (build-replacements remappings)+ rewritten (sql-tools/replace-names driver sql replacements {:allow-unused? true})]+ (if (= expect :parse-error)+ (format "Expected parse error but got success. Tags: %s\nSQL: %s\nRewritten: %s"+ tags sql rewritten)+ ;; Verify no "from" references remain in the rewritten SQL+ (let [pattern (from-ref-pattern remappings)]+ (when (and pattern (re-find pattern rewritten))+ (format "Rewritten SQL still contains production refs. Tags: %s\nOriginal: %s\nRewritten: %s"+ tags sql rewritten)))))+ (catch Exception e+ (if (= expect :parse-error)+ nil ;; Expected+ (format "Unexpected parse error. Tags: %s\nSQL: %s\nError: %s"+ tags sql (ex-message e))))))++;;; ---------------------------------------------- Tests -------------------------------------------------------++(deftest postgres-corpus-test+ (let [entries (parse-corpus-file "remapping_corpus/postgres.sql")]+ (is (pos? (count entries))+ "Corpus file should contain at least one test case")+ (doseq [{:keys [tags sql] :as entry} entries]+ (testing (format "tags=%s sql=%.60s..." tags (str/replace sql #"\n" " "))+ (let [error (run-corpus-entry :postgres entry)]+ (is (nil? error) error))))))++;;; ----------------------------- Per-driver SQL emission snapshots ------------------------------+;;;+;;; Ground truth: real Metabase-emitted SQL captured from production-ish instances. Sources:+;;; - MySQL: Magento (test) / Admin Assert (screenshot 2026-04-28 #proj-table-remappings)+;;; - ClickHouse: Metabase Cloud Storage / Pa Events (screenshot 2026-04-28 #proj-table-remappings)+;;; - BigQuery: BigQuery Census (test) / Population By Zip 2010+;;;+;;; Each test:+;;; 1. takes a canonical SQL string emitted by Metabase+;;; 2. runs it through `sql-tools/replace-names` with a workspace remapping+;;; 3. parses the rewritten SQL with `sql-tools/referenced-tables-raw`+;;; 4. asserts the *parsed table references* match expected slot values per driver+;;;+;;; Asserting on parsed-AST table refs (not regex on the SQL string) means we sidestep+;;; quoting/case differences in driver-specific re-emission and verify the semantic+;;; rewrite directly.++(defn- rewrite-and-parse+ "Run a remapping through the rewriter, then parse the result. Returns the set of+ `{:db?, :schema?, :table}` references in the rewritten SQL."+ [driver canonical-sql remappings]+ (let [rewritten (sql-tools/replace-names driver canonical-sql {:tables remappings} {:allow-unused? true})]+ {:rewritten rewritten+ :tables (set (sql-tools/referenced-tables-raw driver rewritten))}))++(deftest mysql-rewriter-emission-snapshot-test+ (testing "MySQL: bare-table input gets the workspace schema added at AST :schema"+ (let [{:keys [tables rewritten]}+ (rewrite-and-parse+ :mysql+ "SELECT `admin_assert`.`assert_id` FROM `admin_assert`"+ {(ws.table-remapping/prune-no-level {:db "" :schema "" :table "admin_assert"})+ (ws.table-remapping/prune-no-level {:db "" :schema "ws_alice" :table "admin_assert"})})]+ (is (contains? tables {:schema "ws_alice" :table "admin_assert"})+ (str "expected workspace-qualified ref in parsed tables; got: " tables+ "\n rewritten SQL: " rewritten))+ (is (not-any? #(= % {:table "admin_assert"}) tables)+ (str "expected bare `admin_assert` to be gone from parsed tables; got: " tables)))))++(deftest clickhouse-rewriter-emission-snapshot-test+ (testing "ClickHouse: db-name lives at AST :schema; rewrite swaps it"+ (let [{:keys [tables rewritten]}+ (rewrite-and-parse+ :clickhouse+ "SELECT `db_c6633c128ed24e74`.`pa_events`.`tag` FROM `db_c6633c128ed24e74`.`pa_events`"+ {(ws.table-remapping/prune-no-level {:db "" :schema "db_c6633c128ed24e74" :table "pa_events"})+ (ws.table-remapping/prune-no-level {:db "" :schema "ws_alice" :table "pa_events"})})]+ (is (contains? tables {:schema "ws_alice" :table "pa_events"})+ (str "expected workspace db-as-schema ref in parsed tables; got: " tables+ "\n rewritten SQL: " rewritten))+ (is (not-any? #(= (:schema %) "db_c6633c128ed24e74") tables)+ (str "expected canonical db-name gone from parsed tables; got: " tables)))))++;;; For 3-slot drivers (SQL Server, BigQuery), `referenced-tables-raw` returns 2-slot+;;; specs (`{:schema, :table}`) -- it doesn't populate the `:db` slot even when the+;;; SQL has 3 levels. The `:db` rewrite still happens inside `replace-names` (the+;;; rewritten SQL string contains the right catalog name), the parser is just lossy.+;;; For these drivers we assert on parsed `:schema`/`:table` AND verify the `:db`+;;; substitution by string presence/absence in the rewritten SQL.++(deftest bigquery-rewriter-emission-snapshot-test+ (testing "BigQuery: project.dataset.table input gets all three slots rewritten"+ (let [{:keys [tables rewritten]}+ (rewrite-and-parse+ :bigquery-cloud-sdk+ "SELECT `bigquery-public-data.census_bureau_usa.population_by_zip_2010`.`geo_id` FROM `bigquery-public-data.census_bureau_usa.population_by_zip_2010`"+ {(ws.table-remapping/prune-no-level {:db "bigquery-public-data" :schema "census_bureau_usa" :table "population_by_zip_2010"})+ (ws.table-remapping/prune-no-level {:db "ws-project" :schema "ws_alice" :table "population_by_zip_2010"})})]+ ;; parsed table refs (no :db slot from the parser) -- confirms dataset+table swapped:+ (is (contains? tables {:schema "ws_alice" :table "population_by_zip_2010"})+ (str "expected workspace dataset+table in parsed refs; got: " tables+ "\n rewritten SQL: " rewritten))+ (is (not-any? #(= (:schema %) "census_bureau_usa") tables)+ (str "expected canonical dataset gone from parsed refs; got: " tables))+ ;; :db slot lives in the rewritten string only (parser doesn't return it).+ ;; Asserting on FROM clause specifically -- SELECT-clause column refs intentionally+ ;; keep canonical names because we only declared :tables replacements, not :columns.+ (let [from-text (re-find #"(?i)\bFROM\b.*$" rewritten)]+ (is (re-find #"ws-project" from-text)+ (str "expected workspace project in FROM; got: " from-text))+ (is (not (re-find #"bigquery-public-data" from-text))+ (str "expected canonical project gone from FROM; got: " from-text))))))++;;; ----------------- Wire-format -> rewriter end-to-end -----------------+;;;+;;; This block closes the loop: it loads each per-driver fixture YAML through the real+;;; loader (`apply-workspace-section!`), reads the workspace namespace via the production+;;; reader (`db-workspace-namespace`), assembles a remapping the way `add-transform-target-mapping!` would, and runs+;;; the rewriter against canonical SQL. If any link in the chain breaks+;;; (loader/atom/reader/spec drift), this test catches it.+;;;+;;; Each fixture-row pairs the per-driver YAML with: a representative+;;; canonical SQL (Metabase-shaped emission for the input table), the+;;; from-side `::table-spec` matching what `spec-for-table` would produce+;;; for the source, and per-driver assertions on the rewritten SQL.++(defn- load-fixture-section [driver-kw]+ (-> (str "metabase_enterprise/workspaces/resources/workspace_config_" (name driver-kw) ".yml")+ io/resource slurp yaml/parse-string :config :workspace))++(defn- fixture-wsd+ "Pull the single workspace-database entry from a fixture: returns+ `[<db-name-string>, <wsd-config-map>]` where wsd-config has `:input_schemas`+ and `:output_namespace`."+ [section]+ (let [[db-name-kw wsd] (-> section :databases first)]+ [(name db-name-kw) wsd]))++(def ^:private fixture-rewriter-test-cases+ "Per-driver chain test cases. The fixture YAML carries `:input_schemas` and+ `:output_namespace`; the catalog (SQL Server/BigQuery) is pulled from+ `Database.details`. Each test case adds the engine + details needed to drive+ the loader's expansion path, plus the SQL the QP would emit for a+ representative source table."+ [{:fixture-driver :postgres+ :driver :postgres+ :engine :postgres+ :details {}+ :source-table "orders"+ :canonical-sql "SELECT \"public\".\"orders\".\"id\" FROM \"public\".\"orders\""}++ {:fixture-driver :mysql+ :driver :mysql+ :engine :mysql+ :details {:db "prod_db"}+ :source-table "orders"+ :canonical-sql "SELECT `orders`.`id` FROM `orders`"}++ {:fixture-driver :clickhouse+ :driver :clickhouse+ :engine :clickhouse+ :details {}+ :source-table "events"+ :canonical-sql "SELECT `prod_events`.`events`.`tag` FROM `prod_events`.`events`"}++ {:fixture-driver :sqlserver+ :driver :sqlserver+ :engine :sqlserver+ :details {:db "AnalyticsDB"}+ :source-table "orders"+ :canonical-sql "SELECT [AnalyticsDB].[dbo].[orders].[id] FROM [AnalyticsDB].[dbo].[orders]"}++ {:fixture-driver :bigquery+ :driver :bigquery-cloud-sdk+ :engine :bigquery-cloud-sdk+ :details {:project-id "metabase-prod"}+ :source-table "orders"+ :canonical-sql "SELECT `metabase-prod.core.orders`.`id` FROM `metabase-prod.core.orders`"}])++(deftest per-driver-fixture-rewriter-chain-test+ (testing "Each per-driver fixture YAML loads, sets up a TableRemapping, and the rewriter swaps slots correctly"+ (doseq [{:keys [fixture-driver driver engine details source-table canonical-sql]} fixture-rewriter-test-cases+ :when (workspaces.tu/driver-loadable? driver)]+ (testing (str fixture-driver " fixture -> rewriter chain")+ (let [section (load-fixture-section fixture-driver)+ [db-name wsd] (fixture-wsd section)+ first-input (first (:input_schemas wsd))+ ;; Build expected `:output` map the same way the loader does — so the+ ;; assertions below test loader fidelity end-to-end.+ fake-db {:engine engine :details details}+ expected-output (cond-> {}+ (some #{:schema} (driver/qualified-name-components driver))+ (assoc :schema (:output_namespace wsd))+ (and (some #{:db} (driver/qualified-name-components driver))+ (some #{:schema} (driver/qualified-name-components driver)))+ (assoc :db (:db (ws/engine-namespace-positions fake-db)))+ (and (some #{:db} (driver/qualified-name-components driver))+ (not (some #{:schema} (driver/qualified-name-components driver))))+ (assoc :db (:output_namespace wsd)))+ ;; The from-spec handed to the rewriter must match what the driver+ ;; actually *emits* in SQL, which is governed by qualified-name-components.+ emitted-slots (set (driver/qualified-name-components driver))+ input-positions (ws/engine-namespace-positions fake-db {:schema first-input})+ from-spec {:db (if (:db emitted-slots) (or (:db input-positions) "") "")+ :schema (if (:schema emitted-slots) (or (:schema input-positions) "") "")+ :table source-table}]+ (mt/with-empty-h2-app-db!+ (mt/with-temp [:model/Database {db-id :id} {:name db-name :engine engine :details details}]+ (try+ ;; 1. Load through the production loader. Populates the atom.+ (advanced-config.file.workspace/apply-workspace-section! section)+ ;; 2. Read the namespace via the production reader.+ (let [ws-ns (ws/db-workspace-namespace db-id)+ to-spec (merge from-spec ws-ns)+ {:keys [tables rewritten]} (rewrite-and-parse+ driver canonical-sql+ {(ws.table-remapping/prune-no-level from-spec)+ (ws.table-remapping/prune-no-level to-spec)})]+ (testing "atom output matches the loader's expansion of fixture's output_namespace"+ (is (= expected-output ws-ns)+ "db-workspace-namespace must return the loader's expanded :output map"))+ ;; MySQL is special-cased: it has no schema layer, so Phase 1 (table+ ;; metadata mutation) adds the iso `:db` qualifier — Phase 2's SQLGlot+ ;; rewriter never sees a qualifier to swap. Skip the rewriter SQL+ ;; assertions for MySQL; the atom-output assertion above is what matters.+ (when (not= driver :mysql)+ (testing "rewritten SQL parses to the workspace :schema slot"+ (let [expected-schema (or (:schema expected-output) (:db expected-output))]+ (is (contains? tables {:schema expected-schema :table source-table})+ (str "expected {:schema " expected-schema ", :table " source-table+ "} in parsed tables; got: " tables+ "\n rewritten SQL: " rewritten))))+ (testing "rewritten SQL no longer references the canonical schema"+ (when (:schema input-positions)+ (is (not-any? #(= (:schema %) (:schema input-positions)) tables)+ (str "expected canonical schema " (pr-str (:schema input-positions))+ " gone from parsed refs; got: " tables))))+ ;; 3-slot drivers (where workspace output has :db): parser is lossy on :db,+ ;; so verify via the rewritten string.+ (when-let [output-db (:db expected-output)]+ (let [from-text (str (re-find #"(?i)\bFROM\b.*$" rewritten))]+ (testing "rewritten FROM clause contains workspace :db slot"+ (is (re-find (re-pattern (java.util.regex.Pattern/quote output-db)) from-text)+ (str "expected " (pr-str output-db) " in FROM; got: " from-text)))))))+ (finally+ (ws/clear-instance-workspace!))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/cross_driver_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/cross_driver_test.cljnew file mode 100644index 00000000000..36eed0a982f--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/cross_driver_test.clj@@ -0,0 +1,353 @@+(ns ^:mb/driver-tests metabase-enterprise.workspaces.query-processor.cross-driver-test+ "End-to-end read-path coverage for workspace table remapping against real+ warehouses.++ Each test creates a fresh workspace schema in the warehouse, populates it with+ a table whose rows differ from the canonical-side table, registers a+ `:model/TableRemapping` pointing canonical -> workspace, and runs an MBQL+ query against the canonical table id. Phase 1 mutates table metadata so the+ compiled SQL targets the workspace schema; the warehouse executes; the+ assertion compares row content (workspace-side, not canonical-side).++ The H2-only string-SQL tests in `middleware_test.clj` prove the rewriter+ emits the right strings. The SQLGlot corpus runner proves the grammar layer+ across 7 dialects. This namespace fills the third layer — does the warehouse+ actually accept and execute the rewritten SQL, AND do we get the right rows?++ Runs locally against H2 because we provision the workspace schema with raw+ DDL (no dependency on `:workspace` driver feature). CI fans out across every+ driver in the case statements below."+ (:require+ [clojure.java.jdbc :as jdbc]+ [clojure.string :as str]+ [clojure.test :refer [deftest is testing]]+ [metabase.driver :as driver]+ [metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]+ [metabase.driver.sql.util :as sql.u]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++;;; ----------------------------- Per-driver DDL ---------------------------------++(defn- quoted-namespace+ "Driver-correct quoted reference to a schema (or database, for schema-less+ drivers like MySQL). Used in CREATE/DROP statements where the helper builds+ the SQL by string concatenation."+ [driver namespace-name]+ (case driver+ (:postgres :redshift :h2) (str \" namespace-name \")+ :sqlserver (str \[ namespace-name \])+ (:mysql :clickhouse) (str \` namespace-name \`)))++(defn- create-namespace-sql+ "DDL to create a fresh per-run schema (or database, for schema-less drivers+ like MySQL/ClickHouse)."+ [driver namespace-name]+ (case driver+ (:postgres :redshift :h2) (str "CREATE SCHEMA " (quoted-namespace driver namespace-name))+ :sqlserver (str "CREATE SCHEMA " (quoted-namespace driver namespace-name))+ (:mysql :clickhouse) (str "CREATE DATABASE " (quoted-namespace driver namespace-name))))++(defn- drop-namespace-sqls+ "DDL to drop the per-run namespace and any tables left in it. Schema'd+ drivers with CASCADE (postgres/redshift/h2) and database-as-namespace+ drivers (mysql/clickhouse) take a single statement; SQL Server has no DROP+ SCHEMA CASCADE so the source table has to be dropped explicitly first."+ [driver namespace-name table-name]+ (case driver+ (:postgres :redshift :h2) [(str "DROP SCHEMA " (quoted-namespace driver namespace-name) " CASCADE")]+ :sqlserver [(str "DROP TABLE " (quoted-namespace driver namespace-name) "." (quoted-namespace driver table-name))+ (str "DROP SCHEMA " (quoted-namespace driver namespace-name))]+ (:mysql :clickhouse) [(str "DROP DATABASE " (quoted-namespace driver namespace-name))]))++(defn- qualify+ "Per-driver quoted `schema.table` (or `database.table` for schema-less drivers)."+ [driver schema table]+ (sql.u/quote-name driver :table schema table))++(defn- create-table-tail+ "Trailing clause appended to `CREATE TABLE ... (cols)` per driver. ClickHouse+ requires every table to declare a storage engine and an ORDER BY key; SQL+ drivers don't."+ [driver]+ (case driver+ :clickhouse " ENGINE = MergeTree() ORDER BY id"+ ""))++(defn- random-suffix []+ (subs (str (random-uuid)) 0 8))++;;; ----------------------------- Helpers --------------------------------------++(def ^:private cross-driver-test-drivers+ "Drivers we exercise with these end-to-end remap tests. Restricted to a small+ set on purpose: the [[with-workspace-tables!]] fixture sends literal DDL+ (`INT`, `DOUBLE PRECISION`, etc.) to whatever driver runs, and the test queries assume+ case-insensitive identifier handling. That holds on H2 / Postgres but breaks+ on ClickHouse (case-sensitive, no `LEFT JOIN`) and SQL Server (different type names).+ Other drivers' workspace remap+ behavior is covered by [[metabase-enterprise.workspaces.table-remapping-test]]+ unit tests and the SQLGlot corpus tests in+ [[metabase-enterprise.workspaces.query-processor.driver-corpus-test]]."+ #{:h2 :postgres})++(defn- canonical-schema+ "Schema of the orders table on the current driver."+ []+ (t2/select-one-fn :schema :model/Table :id (mt/id :orders)))++(defn- canonical-table-name+ "The :name of the orders :model/Table, as the driver stores it (e.g. \"ORDERS\"+ on H2, \"orders\" on Postgres). Phase 1's metadata override matches by name,+ so the remapping's from-name has to be exactly this string."+ []+ (t2/select-one-fn :name :model/Table :id (mt/id :orders)))++(defn- admin-spec+ "JDBC connection-spec usable for raw DDL and inserts against the current+ driver's test database."+ [driver]+ (sql-jdbc.conn/connection-details->spec driver (:details (mt/db))))++(defn- with-workspace-tables!+ "Provision a workspace schema and one or more tables, each shaped to match a+ canonical-table's MBQL field references for the test query.++ `tables` is a vector of `{:canonical-kw <kw>, :columns \"col1 type, col2 type\",+ :insert-rows [[id, val] ...]}` maps. Each table is created in the workspace+ schema with the given DDL, named the same as its canonical counterpart+ (case-matched), and seeded with the rows. Row values are interpolated raw+ into the INSERT — wrap strings in literal quotes (`\"'foo'\"`).++ Runs `body-fn` with the schema name; tears everything down afterward."+ [tables body-fn]+ (let [driver driver/*driver*+ suffix (random-suffix)+ ws-schema (str "mb_xdr_" suffix)+ spec (admin-spec driver)+ ws-table-names (mapv #(t2/select-one-fn :name :model/Table :id (mt/id (:canonical-kw %)))+ tables)]+ (try+ (jdbc/execute! spec [(create-namespace-sql driver ws-schema)])+ (doseq [{:keys [canonical-kw columns insert-rows]} tables+ :let [ws-table (t2/select-one-fn :name :model/Table :id (mt/id canonical-kw))]]+ (jdbc/execute! spec [(str "CREATE TABLE " (qualify driver ws-schema ws-table)+ " (" columns ")"+ (create-table-tail driver))])+ (doseq [row insert-rows]+ (jdbc/execute! spec [(str "INSERT INTO " (qualify driver ws-schema ws-table)+ " VALUES (" (str/join ", " row) ")")])))+ (body-fn ws-schema)+ (finally+ (doseq [ws-table ws-table-names+ sql (drop-namespace-sqls driver ws-schema ws-table)]+ (try (jdbc/execute! spec [sql])+ (catch Throwable _)))))))++(defn- with-workspace-table!+ "Single-table convenience wrapper. Creates a workspace `orders` table with+ `(id INT, v VARCHAR(32))` columns and the given `[[id v] ...]` rows."+ [orders-rows body-fn]+ (with-workspace-tables!+ [{:canonical-kw :orders+ :columns "id INT, v VARCHAR(32)"+ :insert-rows (mapv (fn [[id v]] [id (str \' v \')]) orders-rows)}]+ body-fn))++(defn- with-remapping!+ "Insert a TableRemapping row pointing canonical -> workspace, run `body-fn`,+ delete the row on the way out.++ Nil schema (MySQL/ClickHouse) is normalized to the empty-string sentinel so the+ row can be persisted regardless of warehouse. The QP rewriter prunes the sentinel+ before handing the key to SQLGlot."+ [from-schema from-name to-schema to-name body-fn]+ (let [{remap-id :id} (t2/insert-returning-instance!+ :model/TableRemapping+ {:database_id (mt/id)+ :from_schema (or from-schema "")+ :from_table_name from-name+ :to_schema to-schema+ :to_table_name to-name})]+ (try+ (body-fn)+ (finally+ (t2/delete! :model/TableRemapping :id remap-id)))))++(defn- canonical-row-count+ "Row count of the canonical orders table — used as the disambiguator. The+ workspace table we create has a tiny number of rows so the read-side+ assertion can compare counts and tell whether we read from the workspace+ schema or the canonical one."+ []+ (-> (mt/process-query+ {:database (mt/id)+ :type :query+ :query {:source-table (mt/id :orders)+ :aggregation [[:count]]}})+ mt/rows+ ffirst))++(defn- count-via-qp+ "Run `SELECT COUNT(*) FROM orders` through the QP via the canonical orders+ table id. If a TableRemapping is in effect, the rewriter should redirect+ this to the workspace schema."+ []+ (-> (mt/process-query+ {:database (mt/id)+ :type :query+ :query {:source-table (mt/id :orders)+ :aggregation [[:count]]}})+ mt/rows+ ffirst))++;;; --------------------------------- Tests -------------------------------------++(deftest single-remapped-table-cross-driver-test+ (testing "QP read against canonical name resolves to the workspace copy on the warehouse"+ (mt/test-drivers (filter cross-driver-test-drivers+ (mt/normal-drivers-with-feature :workspace))+ (mt/with-premium-features #{:workspaces}+ ;; Workspace table has 2 rows; canonical orders has many thousands.+ ;; Asserting the count comes back as 2 proves the read hit the workspace+ ;; copy, not canonical.+ (let [workspace-rows [[1 "ws-foo"] [2 "ws-bar"]]+ canonical-count (canonical-row-count)]+ (assert (> canonical-count 2)+ "test assumes canonical orders has >2 rows so count alone disambiguates")+ (with-workspace-table! workspace-rows+ (fn [ws-schema]+ (with-remapping! (canonical-schema) (canonical-table-name)+ ws-schema (canonical-table-name)+ (fn []+ (is (= 2 (count-via-qp))+ (str "count came from workspace (2 rows), not canonical (" canonical-count " rows)")))))))))))++(deftest non-remapped-passthrough-cross-driver-test+ (testing "0/1: a query against a non-remapped table still hits canonical even when an unrelated remap exists"+ (mt/test-drivers (filter cross-driver-test-drivers+ (mt/normal-drivers-with-feature :workspace))+ (mt/with-premium-features #{:workspaces}+ ;; Setup: a workspace `orders` table with 2 rows + a remap orders -> workspace.+ ;; Query: `:people` (NOT remapped). Must return canonical people count.+ (let [workspace-rows [[1 "ws-foo"] [2 "ws-bar"]]+ canonical-count (-> (mt/process-query+ {:database (mt/id)+ :type :query+ :query {:source-table (mt/id :people)+ :aggregation [[:count]]}})+ mt/rows ffirst)]+ (with-workspace-table! workspace-rows+ (fn [ws-schema]+ (with-remapping! (canonical-schema) (canonical-table-name)+ ws-schema (canonical-table-name)+ (fn []+ (let [people-count (-> (mt/process-query+ {:database (mt/id)+ :type :query+ :query {:source-table (mt/id :people)+ :aggregation [[:count]]}})+ mt/rows ffirst)]+ (is (= canonical-count people-count)+ "people read passes through canonical when only orders is remapped")))))))))))++(deftest join-one-side-remapped-cross-driver-test+ (testing "1/2: an MBQL join with only the orders side remapped reads workspace orders + canonical people"+ (mt/test-drivers (filter cross-driver-test-drivers+ (mt/normal-drivers-with-feature :workspace))+ (mt/with-premium-features #{:workspaces}+ ;; Workspace orders has 2 rows; both have user_id values that exist in canonical people.+ ;; The join count should be 2 (one row per workspace order, joined to its canonical people row).+ ;; If the remap silently failed and the query hit canonical orders, the count would be ~thousands.+ (with-workspace-tables!+ [{:canonical-kw :orders+ :columns "id INT, user_id INT"+ :insert-rows [[1 1] [2 2]]}]+ (fn [ws-schema]+ (with-remapping! (canonical-schema) (canonical-table-name)+ ws-schema (canonical-table-name)+ (fn []+ (let [orders-id (mt/id :orders)+ orders-user-id (mt/id :orders :user_id)+ people-id (mt/id :people :id)+ people-tbl-id (mt/id :people)+ result-count (-> (mt/process-query+ {:database (mt/id)+ :type :query+ :query {:source-table orders-id+ :joins [{:source-table people-tbl-id+ :alias "p"+ :fields :none+ :condition [:= [:field orders-user-id nil]+ [:field people-id {:join-alias "p"}]]}]+ :aggregation [[:count]]}})+ mt/rows ffirst)]+ (is (= 2 result-count)+ "join count = workspace orders rows that match canonical people"))))))))))++(deftest join-both-sides-remapped-cross-driver-test+ (testing "2/2: an MBQL join with both sides remapped reads workspace orders + workspace people"+ (mt/test-drivers (filter cross-driver-test-drivers+ (mt/normal-drivers-with-feature :workspace))+ (mt/with-premium-features #{:workspaces}+ ;; Workspace orders has user_id 999; workspace people has id 999.+ ;; Canonical people doesn't have id 999, so the 999/999 match exists+ ;; only when both sides are remapped. Workspace tables mirror+ ;; canonical column shape because the QP's implicit join compiles to+ ;; SELECT (every canonical field) — `:fields :none` doesn't suppress+ ;; this when the join target is itself remapped.+ (with-workspace-tables!+ [{:canonical-kw :orders+ :columns (str/join " " ["ID BIGINT,"+ "USER_ID INT,"+ "PRODUCT_ID INT,"+ "SUBTOTAL DOUBLE PRECISION,"+ "TAX DOUBLE PRECISION,"+ "TOTAL DOUBLE PRECISION,"+ "DISCOUNT DOUBLE PRECISION,"+ "CREATED_AT TIMESTAMP,"+ "QUANTITY INT"])+ :insert-rows [[1 999 "NULL" "NULL" "NULL" "NULL" "NULL" "NULL" "NULL"]]}+ {:canonical-kw :people+ :columns (str/join " " ["ID BIGINT,"+ "ADDRESS VARCHAR(256),"+ "EMAIL VARCHAR(256),"+ "PASSWORD VARCHAR(256),"+ "NAME VARCHAR(256),"+ "CITY VARCHAR(256),"+ "LONGITUDE DOUBLE PRECISION,"+ "STATE VARCHAR(256),"+ "SOURCE VARCHAR(256),"+ "BIRTH_DATE DATE,"+ "ZIP VARCHAR(256),"+ "LATITUDE DOUBLE PRECISION,"+ "CREATED_AT TIMESTAMP"])+ :insert-rows [[999 "NULL" "NULL" "NULL" "'ws-only-person'"+ "NULL" "NULL" "NULL" "NULL" "NULL"+ "NULL" "NULL" "NULL"]]}]+ (fn [ws-schema]+ (with-remapping! (canonical-schema) (t2/select-one-fn :name :model/Table :id (mt/id :orders))+ ws-schema (t2/select-one-fn :name :model/Table :id (mt/id :orders))+ (fn []+ (with-remapping! (canonical-schema) (t2/select-one-fn :name :model/Table :id (mt/id :people))+ ws-schema (t2/select-one-fn :name :model/Table :id (mt/id :people))+ (fn []+ (let [orders-id (mt/id :orders)+ orders-user-id (mt/id :orders :user_id)+ people-id (mt/id :people :id)+ people-tbl-id (mt/id :people)+ result-count (-> (mt/process-query+ {:database (mt/id)+ :type :query+ :query {:source-table orders-id+ :joins [{:source-table people-tbl-id+ :alias "p"+ :fields :none+ :condition [:= [:field orders-user-id nil]+ [:field people-id {:join-alias "p"}]]}]+ :aggregation [[:count]]}})+ mt/rows ffirst)]+ (is (= 1 result-count)+ "join count = 1, the 999/999 match that only exists in the workspace tables"))))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/driver_corpus_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/driver_corpus_test.cljnew file mode 100644index 00000000000..21b8512c577--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/driver_corpus_test.clj@@ -0,0 +1,185 @@+(ns metabase-enterprise.workspaces.query-processor.driver-corpus-test+ "Round-trip remapping tests against a real query corpus.++ Reads SQL queries from markdown files in the query_corpus repo+ (https://github.com/metabase/query_corpus), extracts table references,+ generates synthetic remappings, applies them, and verifies no original+ table references survive in the output.++ This namespace provides [[run-corpus-for-dialect]] for REPL use (see comment block+ at the bottom). No deftest — the corpus is too large and too slow for CI."+ (:require+ [clojure.java.io :as io]+ [clojure.string :as str]+ [clojure.test :refer :all]+ [metabase.sql-tools.core :as sql-tools]+ [metabase.util.log :as log]))++(set! *warn-on-reflection* true)++(def ^:private dialect-for-file+ {"postgres.md" :postgres+ "mysql.md" :mysql+ "redshift.md" :redshift+ "sqlserver.md" :sqlserver+ "clickhouse.md" :clickhouse+ "bigquery.md" :bigquery-cloud-sdk})++;;; ------------------------------------------------ Parser ----------------------------------------------------++(defn- extract-sql-blocks+ "Extract SQL from markdown fenced code blocks (```sql ... ```)."+ [markdown-content]+ (let [;; Match ```sql ... ``` blocks, capturing the content+ pattern #"(?ms)```sql\s*\n(.*?)```"]+ (->> (re-seq pattern markdown-content)+ (map second)+ (map str/trim)+ ;; Strip trailing JSON metadata comments+ (map (fn [sql]+ (str/replace sql #"(?m)\n--\s*\{\"pulseId\".*$" "")))+ (map str/trim)+ (filter seq))))++(defn- synthetic-remap+ "Generate a synthetic remap for a table reference: schema.table -> ws_test_remap.table.+ If no schema, uses nil -> ws_test_remap."+ [{:keys [schema table]}]+ (when table+ [{:schema schema :table table}+ {:schema (or schema "ws_test_remap") :table (str "remapped_" table)}]))++;;; -------------------------------------------- Round-trip test ------------------------------------------------++(defn- referenced-tables-or-error+ "Return either the parsed table refs (a possibly-empty seq of `{:schema ..., :table ...}`)+ or `[::parse-failed ex-message]` if SQLGlot couldn't parse the SQL."+ [driver sql]+ (try+ (sql-tools/referenced-tables-raw driver sql)+ (catch Exception e+ [::parse-failed (ex-message e)])))++(defn- run-roundtrip+ "Run a single round-trip remapping test. Returns one of:++ {:outcome :passed} — round-trip succeeded+ {:outcome :no-tables} — original SQL has no table refs to rewrite+ {:outcome :source-parse-failed, :message ..., :sql ...} — original SQL didn't parse at all+ {:outcome :ref-survived, :original ..., :refs ...} — rewriter missed the ref+ {:outcome :rewrite-failed, :message ..., :sql ...} — replace-names threw++ No silent skips — every input maps to exactly one outcome category."+ [driver sql]+ (let [refs (referenced-tables-or-error driver sql)]+ (cond+ (and (vector? refs) (= ::parse-failed (first refs)))+ {:outcome :source-parse-failed+ :message (second refs)+ :sql (subs sql 0 (min 200 (count sql)))}++ (empty? refs)+ {:outcome :no-tables}++ :else+ (let [remap-target (first (filter :table refs))]+ (if-not remap-target+ ;; Refs were extracted but none had a :table — treat the same as :no-tables;+ ;; the rewriter has nothing to do.+ {:outcome :no-tables}+ (let [[from-spec to-spec] (synthetic-remap remap-target)+ replacements {:tables {from-spec to-spec}}]+ (try+ (let [rewritten (sql-tools/replace-names driver sql replacements {:allow-unused? true})+ new-refs (referenced-tables-or-error driver rewritten)+ original [(:schema from-spec) (:table from-spec)]]+ (cond+ ;; If we can't re-parse the rewritten SQL, that's a different failure mode —+ ;; the rewriter produced unparseable output. Surface it loudly rather than+ ;; silently passing.+ (and (vector? new-refs) (= ::parse-failed (first new-refs)))+ {:outcome :rewrite-emitted-unparseable+ :message (second new-refs)+ :sql (subs sql 0 (min 200 (count sql)))}++ (contains? (into #{}+ (map (fn [{:keys [schema table]}] [schema table]))+ new-refs)+ original)+ {:outcome :ref-survived+ :original original+ :sql (subs sql 0 (min 200 (count sql)))}++ :else+ {:outcome :passed}))+ (catch Exception e+ {:outcome :rewrite-failed+ :message (ex-message e)+ :sql (subs sql 0 (min 200 (count sql)))}))))))))++;;; ---------------------------------------------- Tests -------------------------------------------------------++(defn run-corpus-for-dialect+ "Run round-trip tests for a single dialect file. Returns+ `{:total N :passed N :no-tables N :source-parse-failed N :errors [...]}`.++ `:passed` is the count of queries that genuinely round-tripped — the rewriter saw the+ picked ref and produced output where it no longer appears.++ `:no-tables` and `:source-parse-failed` are queries the test could not meaningfully+ exercise (the rewriter wouldn't be invoked for them, or would be invoked on something+ it can't parse — the production fail-closed path catches the latter at runtime).++ `:errors` contains genuine failures: `:ref-survived`, `:rewrite-failed`, or+ `:rewrite-emitted-unparseable`."+ [dir filename driver]+ (let [file (io/file dir filename)+ content (slurp file)+ queries (extract-sql-blocks content)+ results (atom {:total 0+ :passed 0+ :no-tables 0+ :source-parse-failed 0+ :errors []})]+ (doseq [sql queries]+ (swap! results update :total inc)+ (let [{:keys [outcome] :as result} (run-roundtrip driver sql)]+ (case outcome+ :passed (swap! results update :passed inc)+ :no-tables (swap! results update :no-tables inc)+ :source-parse-failed (swap! results update :source-parse-failed inc)+ (do+ (swap! results update :errors conj result)+ (log/debugf "%s for %s: %s" outcome filename (:message result))))))+ @results))++(comment+ ;; Query corpus: https://github.com/metabase/query_corpus+ ;; Clone it and point `dir` at the drivers/ directory.++ (def dir "/path/to/query_corpus/drivers")++ ;; Run a single dialect:+ (run-corpus-for-dialect dir "postgres.md" :postgres)+ ;; => {:total 1384, :passed 1257, :no-tables 124, :source-parse-failed 3, :errors []}++ ;; Run all dialects:+ (into []+ (map (fn [[file driver]] [file (run-corpus-for-dialect dir file driver)]))+ dialect-for-file)++ ;; Results as of 2026-04-28 (with the categorising harness):+ ;; dialect passed/eligible no-tables source-parse-failed errors+ ;; postgres 1257/1260 124 3 0+ ;; bigquery 1517/1520 38 3 0+ ;; redshift 303/305 12 2 0+ ;; sqlserver 1243/1262 23 19 0+ ;; mysql 3362/3364 78 2 0+ ;; clickhouse 2894/2896 86 2 0+ ;; total 10576/10607 361 31 0+ ;;+ ;; "passed/eligible" excludes :no-tables (nothing to rewrite) and+ ;; :source-parse-failed (rewriter never invoked; production fail-closed catches these+ ;; at runtime). Across 12077 SQL strings that actually exercised the rewriter, zero+ ;; produced surviving original refs and zero raised :rewrite-failed.+ )diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/middleware_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/middleware_test.cljnew file mode 100644index 00000000000..efe697bf6fc--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/middleware_test.clj@@ -0,0 +1,852 @@+(ns metabase-enterprise.workspaces.query-processor.middleware-test+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.workspaces.query-processor.middleware :as ws.middleware]+ [metabase-enterprise.workspaces.remapping.core :as ws.remapping]+ [metabase-enterprise.workspaces.table-remapping :as ws.table-remapping]+ [metabase.api.common :as api]+ [metabase.driver :as driver]+ [metabase.lib.convert :as lib.convert]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.metadata.protocols :as lib.metadata.protocols]+ [metabase.lib.test-util.metadata-providers.mock :as mock]+ [metabase.permissions.core :as perms]+ [metabase.query-processor.compile :as qp.compile]+ [metabase.query-processor.error-type :as qp.error-type]+ [metabase.query-processor.middleware.enterprise :as qp.middleware.enterprise]+ [metabase.query-processor.preprocess :as qp.preprocess]+ ^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store]+ [metabase.sql-tools.core :as sql-tools]+ [metabase.test :as mt]+ [metabase.test.fixtures :as fixtures]+ [toucan2.core :as t2]))++(use-fixtures :once (fixtures/initialize :db))++(defn- ->spec+ "Project a `[schema table]` 2-tuple, `[db schema table]` 3-tuple, or `::table-spec`+ map into the canonical `::table-spec` shape. Test convenience: callers pass the+ shape that's most readable for their case."+ [x]+ (cond+ (map? x) x+ (= 2 (count x)) {:db "" :schema (first x) :table (second x)}+ :else {:db (nth x 0) :schema (nth x 1) :table (nth x 2)}))++(defn- widen-2-tuples+ "Project a map of `{from-key to-key}` (where keys may be 2-tuples, 3-tuples, or+ spec maps) to the canonical `{::table-spec ::table-spec}` shape the store+ requires."+ [m]+ (into {} (map (fn [[k v]] [(->spec k) (->spec v)])) m))++(defmacro ^:private with-remappings [db-id remappings & body]+ `(binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {~db-id (widen-2-tuples ~remappings)})]+ ~@body))++;;; -------------------------------------- Phase 1: Preprocessing (MBQL only) --------------------------------------++(deftest phase-1-no-remappings-passthrough-test+ (testing "Phase 1 passes through when no remappings exist"+ (mt/with-premium-features #{:workspaces}+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store {})]+ (qp.store/with-metadata-provider (mt/id)+ (let [venues-table (lib.metadata/table (qp.store/metadata-provider) (mt/id :venues))+ query (-> (lib/query (qp.store/metadata-provider) venues-table)+ (assoc :database (mt/id)))]+ (is (= query (#'ws.middleware/apply-workspace-remapping query)))))))))++(deftest phase-1-skip-dynamic-var-test+ (testing "Phase 1 passes through when *skip-remapping?* is true"+ (mt/with-premium-features #{:workspaces}+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["mb_iso_abc" "VENUES"]}+ (binding [ws.remapping/*skip-remapping?* true]+ (qp.store/with-metadata-provider (mt/id)+ (let [venues-table (lib.metadata/table (qp.store/metadata-provider) (mt/id :venues))+ query (-> (lib/query (qp.store/metadata-provider) venues-table)+ (assoc :database (mt/id)))]+ (is (= query (#'ws.middleware/apply-workspace-remapping query))))))))))++(deftest phase-1-mbql-table-metadata-swap-test+ (testing "Phase 1 swaps table metadata for MBQL queries"+ (mt/with-premium-features #{:workspaces}+ (qp.store/with-metadata-provider (mt/id)+ (let [venues-table (lib.metadata/table (qp.store/metadata-provider) (mt/id :venues))+ original-schema (:schema venues-table)+ original-name (:name venues-table)]+ (with-remappings (mt/id) {[original-schema original-name] ["mb_iso_workspace" "remapped_venues"]}+ (let [query (-> (lib/query (qp.store/metadata-provider) venues-table)+ (assoc :database (mt/id)))]+ (#'ws.middleware/apply-workspace-remapping query)+ (let [table-after (lib.metadata/table (qp.store/metadata-provider) (mt/id :venues))]+ (is (= "mb_iso_workspace" (:schema table-after)))+ (is (= "remapped_venues" (:name table-after)))))))))))++(deftest phase-1-does-not-touch-native-queries-test+ (testing "Phase 1 passes native queries through unchanged — Phase 2 handles SQL rewriting"+ (mt/with-premium-features #{:workspaces}+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["mb_iso_abc" "VENUES"]}+ (qp.store/with-metadata-provider (mt/id)+ (let [query {:lib/type :mbql/query+ :database (mt/id)+ :lib/metadata (qp.store/metadata-provider)+ :stages [{:lib/type :mbql.stage/native+ :native "SELECT * FROM PUBLIC.VENUES"}]}+ result (#'ws.middleware/apply-workspace-remapping query)]+ ;; SQL should be unchanged — Phase 1 doesn't rewrite native SQL+ (is (= "SELECT * FROM PUBLIC.VENUES"+ (get-in result [:stages 0 :native])))))))))++;;; --------------------------------- Phase 2: Post-Compilation SQL Rewrite ----------------------------------------++(deftest phase-2-no-remappings-passthrough-test+ (testing "Phase 2 passes through when no remappings exist — the next middleware sees the SQL verbatim"+ (mt/with-premium-features #{:workspaces}+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store {})]+ (let [original-sql "SELECT * FROM foo"+ captured (atom nil)+ mock-qp (fn [query _rff] (reset! captured query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:database (mt/id)+ :qp/compiled {:query original-sql}}]+ (wrapped query identity)+ (is (= original-sql (get-in @captured [:qp/compiled :query]))+ "SQL flows through unchanged when no remappings exist"))))))++(deftest phase-2-skip-dynamic-var-test+ (testing "Phase 2 passes through when *skip-remapping?* is true"+ (mt/with-premium-features #{:workspaces}+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["mb_iso_abc" "VENUES"]}+ (binding [ws.remapping/*skip-remapping?* true]+ (let [original-sql "SELECT * FROM PUBLIC.VENUES"+ called-with (atom nil)+ mock-qp (fn [query _rff] (reset! called-with query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:database (mt/id)+ :qp/compiled {:query original-sql}}]+ (wrapped query identity)+ (is (= original-sql (get-in @called-with [:qp/compiled :query])))))))))++(deftest phase-2-rewrites-compiled-sql-test+ (testing "Phase 2 rewrites table references in compiled SQL"+ (mt/with-premium-features #{:workspaces}+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["mb_iso_abc" "VENUES"]}+ (binding [driver/*driver* :h2]+ (let [called-with (atom nil)+ mock-qp (fn [query _rff] (reset! called-with query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:database (mt/id)+ :qp/compiled {:query "SELECT * FROM PUBLIC.VENUES"}}]+ (wrapped query identity)+ (let [rewritten (get-in @called-with [:qp/compiled :query])]+ (is (some? rewritten))+ (is (re-find #"(?i)mb_iso_abc" rewritten)))))))))++(deftest phase-2-rewrites-compiled-inline-too-test+ (testing "Phase 2 also rewrites :qp/compiled-inline"+ (mt/with-premium-features #{:workspaces}+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["mb_iso_abc" "VENUES"]}+ (binding [driver/*driver* :h2]+ (let [called-with (atom nil)+ mock-qp (fn [query _rff] (reset! called-with query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:database (mt/id)+ :qp/compiled {:query "SELECT * FROM PUBLIC.VENUES"}+ :qp/compiled-inline {:query "SELECT * FROM PUBLIC.VENUES"}}]+ (wrapped query identity)+ (is (re-find #"(?i)mb_iso_abc" (get-in @called-with [:qp/compiled-inline :query])))))))))++(deftest phase-2-preserves-params-test+ (testing "Phase 2 preserves query params when rewriting SQL"+ (mt/with-premium-features #{:workspaces}+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["mb_iso_abc" "VENUES"]}+ (binding [driver/*driver* :h2]+ (let [called-with (atom nil)+ mock-qp (fn [query _rff] (reset! called-with query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:database (mt/id)+ :qp/compiled {:query "SELECT * FROM PUBLIC.VENUES WHERE id = ?"+ :params [42]}}]+ (wrapped query identity)+ (let [rewritten-sql (get-in @called-with [:qp/compiled :query])+ params (get-in @called-with [:qp/compiled :params])]+ (is (re-find #"(?i)mb_iso_abc" rewritten-sql))+ (is (= [42] params)))))))))++(deftest phase-2-noop-when-already-remapped-test+ (testing "Phase 2 is a no-op when refs are already in the workspace schema — SQL identical at next middleware"+ (mt/with-premium-features #{:workspaces}+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["mb_iso_abc" "VENUES"]}+ (binding [driver/*driver* :h2]+ (let [original-sql "SELECT * FROM mb_iso_abc.VENUES"+ captured (atom nil)+ mock-qp (fn [query _rff] (reset! captured query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:database (mt/id)+ :qp/compiled {:query original-sql}}]+ (wrapped query identity)+ ;; The semantic claim is "already-remapped SQL doesn't change." SQLGlot's+ ;; re-emission can normalize whitespace/casing on a round-trip, so equality+ ;; via `referenced-tables-raw` is the right comparison: same AST tables in,+ ;; same AST tables out.+ (let [rewritten (get-in @captured [:qp/compiled :query])]+ (is (= (set (sql-tools/referenced-tables-raw :h2 original-sql))+ (set (sql-tools/referenced-tables-raw :h2 rewritten)))+ "the same set of table references appears in input and output"))))))))++;;; -------------------------------------------- Fail-closed contracts -------------------------------------------+;;;+;;; Phase 2 is the workspace-isolation security boundary. Three branches must fail closed:+;;;+;;; 1. SQLGlot parse failure on the compiled SQL -> throw, never reach the driver.+;;; 2. Database routing engaged -> throw, the rewriter can't reason about+;;; a destination connection different from+;;; the one the remap rows reference.+;;; 3. Connection impersonation engaged -> throw, the impersonated role almost+;;; certainly lacks GRANT on the iso schema,+;;; and silently running canonical SQL would+;;; leak through the isolation boundary.+;;;+;;; Each test stubs the relevant predicate and asserts the throw before the next-fn runs.++(deftest phase-2-fail-closed-on-unparseable-sql-test+ (testing "Phase 2 throws (does not pass through) when the compiled SQL can't be parsed"+ (mt/with-premium-features #{:workspaces}+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["mb_iso_abc" "VENUES"]}+ (binding [driver/*driver* :h2]+ (let [next-called? (atom false)+ mock-qp (fn [_ _] (reset! next-called? true) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ ;; SQLGlot rejects this -- not a SELECT, not valid SQL at all.+ garbage "this is not sql, it's a love letter"+ query {:database (mt/id)+ :qp/compiled {:query garbage}}]+ (is (thrown? Exception (wrapped query identity))+ "unparseable SQL must throw")+ (is (false? @next-called?)+ "next middleware must NOT be called -- failing closed means no query reaches the warehouse")))))))++(deftest phase-2-fail-closed-on-db-routing-test+ (testing "Phase 2 throws when the request is db-routed -- workspace remap is incompatible"+ (mt/with-premium-features #{:workspaces}+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["mb_iso_abc" "VENUES"]}+ (with-redefs [qp.middleware.enterprise/currently-db-routed? (constantly true)]+ (let [next-called? (atom false)+ mock-qp (fn [_ _] (reset! next-called? true) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:database (mt/id)+ :qp/compiled {:query "SELECT * FROM PUBLIC.VENUES"}}]+ (try+ (wrapped query identity)+ (is false "Phase 2 must throw on db-routed request")+ (catch clojure.lang.ExceptionInfo e+ (is (= qp.error-type/qp (:type (ex-data e)))+ "the error must carry the QP error type so the wrapper can render it")+ (is (re-find #"(?i)database routing" (ex-message e))+ "the message must name db-routing as the reason")))+ (is (false? @next-called?)+ "next middleware must NOT be called when db-routing is incompatible")))))))++(deftest phase-2-fail-closed-on-impersonation-test+ (testing "Phase 2 throws when the user has connection-impersonation enforced -- iso role mismatch"+ (mt/with-premium-features #{:workspaces}+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["mb_iso_abc" "VENUES"]}+ (with-redefs [qp.middleware.enterprise/currently-db-routed? (constantly false)+ perms/impersonation-enforced-for-db? (constantly true)]+ (binding [api/*current-user-id* 1]+ (let [next-called? (atom false)+ mock-qp (fn [_ _] (reset! next-called? true) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:database (mt/id)+ :qp/compiled {:query "SELECT * FROM PUBLIC.VENUES"}}]+ (try+ (wrapped query identity)+ (is false "Phase 2 must throw on impersonation-enforced request")+ (catch clojure.lang.ExceptionInfo e+ (is (= qp.error-type/qp (:type (ex-data e)))+ "the error must carry the QP error type")+ (is (re-find #"(?i)impersonation" (ex-message e))+ "the message must name impersonation as the reason")))+ (is (false? @next-called?)+ "next middleware must NOT be called when impersonation engages"))))))))++(deftest phase-2-impersonation-skipped-without-current-user-test+ (testing "impersonation-enforced-for-db? is NOT consulted when no current user is bound (sync/transform paths)"+ (mt/with-premium-features #{:workspaces}+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["mb_iso_abc" "VENUES"]}+ (let [impersonation-checked? (atom false)]+ (with-redefs [qp.middleware.enterprise/currently-db-routed? (constantly false)+ perms/impersonation-enforced-for-db?+ (fn [_db]+ (reset! impersonation-checked? true)+ (throw (ex-info "must not be called without a current user" {})))]+ (binding [api/*current-user-id* nil+ driver/*driver* :h2]+ (let [captured (atom nil)+ mock-qp (fn [query _] (reset! captured query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:database (mt/id)+ :qp/compiled {:query "SELECT * FROM PUBLIC.VENUES"}}]+ (wrapped query identity)+ (is (false? @impersonation-checked?)+ "the impersonation predicate must be short-circuited when no user is bound")+ (is (some? @captured)+ "the rewrite proceeds normally and the next middleware is called")))))))))++;;; -------------------------------------------- Helper tests -----------------------------------------------------++(deftest build-table-replacements-test+ (testing "builds replacement map with raw identifiers (SQLGlot handles quoting per dialect)"+ (let [remappings {{:db "" :schema "public" :table "orders"} {:db "" :schema "mb_iso" :table "orders"}+ {:db "" :schema "public" :table "users"} {:db "" :schema "mb_iso" :table "users"}}+ result (#'ws.table-remapping/build-table-replacements remappings)]+ (is (= 2 (count result)))+ ;; Empty-string sentinels are pruned before being handed to SQLGlot.+ (is (= {:schema "mb_iso" :table "orders"}+ (get result {:schema "public" :table "orders"})))+ (is (= {:schema "mb_iso" :table "users"}+ (get result {:schema "public" :table "users"})))))++ (testing "3-level remappings (BigQuery-style) preserve :db"+ ;; 3-slot specs translate to SQLGlot's `:catalog`+`:schema`+`:table` shape.+ ;; Our `:db` -> SQLGlot's `:catalog`; our `:schema` stays as `:schema`.+ (let [remappings {{:db "proj" :schema "ds" :table "orders"} {:db "proj" :schema "ws_ds" :table "orders"}}+ result (#'ws.table-remapping/build-table-replacements remappings)]+ (is (= {:catalog "proj" :schema "ws_ds" :table "orders"}+ (get result {:catalog "proj" :schema "ds" :table "orders"})))))++ (testing "schema-less drivers (MySQL-style) prune both :db and :schema"+ (let [remappings {{:db "" :schema "" :table "orders"} {:db "" :schema "ws_db" :table "orders"}}+ result (#'ws.table-remapping/build-table-replacements remappings)]+ (is (= {:schema "ws_db" :table "orders"}+ (get result {:table "orders"}))))))++;;; -------------------------- Pre-sync ordering: to-side has no :model/Table yet ----------------------------------+;;;+;;; When a TableRemapping row points to a (to_schema, to_table_name) that has not yet been synced into the app DB,+;;; both phases must still operate correctly. They consume schema/table strings from the remapping store; nothing+;;; should require a hydrated `:model/Table` on the to-side.++(deftest phase-2-handles-to-side-without-model-table-test+ (testing "Phase 2 rewrites SQL using to-side strings even when no :model/Table exists for the to-side"+ (mt/with-premium-features #{:workspaces}+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["ws_unsynced" "venues_unsynced"]}+ (binding [driver/*driver* :h2]+ (let [called-with (atom nil)+ mock-qp (fn [query _rff] (reset! called-with query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:database (mt/id)+ :qp/compiled {:query "SELECT * FROM PUBLIC.VENUES"}}]+ (wrapped query identity)+ (let [rewritten (get-in @called-with [:qp/compiled :query])]+ (is (re-find #"(?i)ws_unsynced" rewritten)+ "Phase 2 emits the to-side schema in the rewritten SQL")+ (is (re-find #"(?i)venues_unsynced" rewritten)+ "Phase 2 emits the to-side table name in the rewritten SQL"))))))))++;;; ========================================= Read-path tests ===================================================+;;;+;;; Progressive coverage of every read-path query shape we ship. Each test adds one bit of complexity, all+;;; asserting the same invariant: remapped tables resolve to the workspace schema; non-remapped tables stay+;;; canonical.+;;;+;;; This subsumes the call-site survey's bypass-path concern from a different angle — instead of enumerating+;;; call sites where remapping might miss, enumerate query shapes and prove each one rewrites correctly.+;;;+;;; All single-table-through-subquery tests run through Dan's `MapRemappingStore` (no DB temps) and assert on+;;; the rewritten SQL produced by Phase 2. The MBQL Phase 1, Card, and Dashboard tests add the MBQL preprocess+;;; pipeline, Card execution, and Dashboard execution respectively.++(defn- rewrite-via-phase-2+ "Run `sql` through Phase 2 with the given remappings and return the rewritten SQL.+ Convenience for h2 + real `(mt/id)`-based tests; widens 2-tuple remapping keys for+ ergonomics. For non-h2 drivers or synthetic db-ids, use [[rewrite-via-phase-2-with-driver]]."+ [db-id remappings sql]+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {db-id (widen-2-tuples remappings)})+ driver/*driver* :h2]+ (let [captured (atom nil)+ mock-qp (fn [query _rff] (reset! captured query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)]+ (wrapped {:database db-id :qp/compiled {:query sql}} identity)+ (get-in @captured [:qp/compiled :query]))))++(deftest single-remapped-table-rewrite-test+ (testing "a single remapped table is rewritten to the workspace schema"+ (mt/with-premium-features #{:workspaces}+ (let [rewritten (rewrite-via-phase-2 (mt/id)+ {["PUBLIC" "ORDERS"] ["ws_alice" "orders_workspace"]}+ "SELECT * FROM PUBLIC.ORDERS")]+ (is (re-find #"(?i)ws_alice" rewritten))+ (is (re-find #"(?i)orders_workspace" rewritten))+ (is (not (re-find #"(?i)PUBLIC\.ORDERS\b" rewritten))+ "no canonical reference survives the rewrite")))))++(deftest non-remapped-table-passthrough-test+ (testing "a non-remapped table passes through unchanged (canonical pass-through)"+ (mt/with-premium-features #{:workspaces}+ (let [sql "SELECT * FROM PUBLIC.VENUES"+ rewritten (rewrite-via-phase-2 (mt/id)+ ;; remapping exists for ORDERS, but the query targets VENUES+ {["PUBLIC" "ORDERS"] ["ws_alice" "orders_workspace"]}+ sql)]+ (is (re-find #"(?i)PUBLIC\.VENUES" rewritten)+ "VENUES has no remapping — the canonical reference must survive")+ (is (not (re-find #"(?i)ws_alice" rewritten))+ "the unrelated workspace schema must not appear")))))++(deftest join-one-side-remapped-test+ (testing "in a join, only the remapped side is rewritten"+ (mt/with-premium-features #{:workspaces}+ (let [rewritten (rewrite-via-phase-2 (mt/id)+ {["PUBLIC" "ORDERS"] ["ws_alice" "orders_workspace"]}+ (str "SELECT o.id, u.name "+ "FROM PUBLIC.ORDERS o "+ "JOIN PUBLIC.USERS u ON o.user_id = u.id"))]+ (is (re-find #"(?i)ws_alice\.orders_workspace" rewritten)+ "ORDERS is remapped to the workspace schema")+ (is (re-find #"(?i)PUBLIC\.USERS" rewritten)+ "USERS has no remapping — its canonical reference survives")))))++(deftest join-both-sides-remapped-test+ (testing "in a join, both remapped sides are rewritten"+ (mt/with-premium-features #{:workspaces}+ (let [rewritten (rewrite-via-phase-2 (mt/id)+ {["PUBLIC" "ORDERS"] ["ws_alice" "orders_workspace"]+ ["PUBLIC" "USERS"] ["ws_alice" "users_workspace"]}+ (str "SELECT o.id, u.name "+ "FROM PUBLIC.ORDERS o "+ "JOIN PUBLIC.USERS u ON o.user_id = u.id"))]+ (is (re-find #"(?i)ws_alice\.orders_workspace" rewritten))+ (is (re-find #"(?i)ws_alice\.users_workspace" rewritten))+ (is (not (re-find #"(?i)PUBLIC\.ORDERS\b" rewritten)))+ (is (not (re-find #"(?i)PUBLIC\.USERS\b" rewritten)))))))++(deftest three-tables-mixed-remapping-test+ (testing "three tables, mixed remapping — only the configured ones are rewritten"+ (mt/with-premium-features #{:workspaces}+ (let [rewritten (rewrite-via-phase-2 (mt/id)+ {["PUBLIC" "ORDERS"] ["ws_alice" "orders_workspace"]+ ["PUBLIC" "PRODUCTS"] ["ws_alice" "products_workspace"]}+ (str "SELECT o.id, p.name, u.email "+ "FROM PUBLIC.ORDERS o "+ "JOIN PUBLIC.PRODUCTS p ON o.product_id = p.id "+ "JOIN PUBLIC.USERS u ON o.user_id = u.id"))]+ (is (re-find #"(?i)ws_alice\.orders_workspace" rewritten))+ (is (re-find #"(?i)ws_alice\.products_workspace" rewritten))+ (is (re-find #"(?i)PUBLIC\.USERS" rewritten)+ "USERS is the only un-remapped table; its canonical ref survives")))))++(deftest cte-mixed-remapping-test+ (testing "a CTE referencing both remapped and non-remapped tables is rewritten correctly"+ (mt/with-premium-features #{:workspaces}+ (let [rewritten (rewrite-via-phase-2 (mt/id)+ {["PUBLIC" "ORDERS"] ["ws_alice" "orders_workspace"]}+ (str "WITH recent_orders AS ("+ " SELECT * FROM PUBLIC.ORDERS WHERE created_at > '2026-01-01'"+ ") "+ "SELECT o.id, u.email "+ "FROM recent_orders o "+ "JOIN PUBLIC.USERS u ON o.user_id = u.id"))]+ (is (re-find #"(?i)ws_alice\.orders_workspace" rewritten)+ "ORDERS reference inside the CTE body is rewritten")+ (is (re-find #"(?i)PUBLIC\.USERS" rewritten)+ "USERS outside the CTE keeps its canonical ref")))))++(deftest subquery-mixed-remapping-test+ (testing "a nested subquery referencing both kinds of tables is rewritten correctly"+ (mt/with-premium-features #{:workspaces}+ (let [rewritten (rewrite-via-phase-2 (mt/id)+ {["PUBLIC" "ORDERS"] ["ws_alice" "orders_workspace"]}+ (str "SELECT u.id, u.name, "+ " (SELECT COUNT(*) FROM PUBLIC.ORDERS o WHERE o.user_id = u.id) AS n "+ "FROM PUBLIC.USERS u"))]+ (is (re-find #"(?i)ws_alice\.orders_workspace" rewritten)+ "ORDERS inside the correlated subquery is rewritten")+ (is (re-find #"(?i)PUBLIC\.USERS" rewritten)+ "USERS in the outer query keeps its canonical ref")))))++(deftest card-execution-path-test+ (testing "a Card whose dataset_query targets a remapped table compiles to workspace SQL"+ (mt/with-premium-features #{:workspaces}+ (qp.store/with-metadata-provider (mt/id)+ (let [venues (lib.metadata/table (qp.store/metadata-provider) (mt/id :venues))+ original-schema (:schema venues)+ original-name (:name venues)+ venues-query (lib.convert/->legacy-MBQL+ (lib/query (qp.store/metadata-provider) venues))]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query venues-query+ :database_id (mt/id)}]+ (with-remappings (mt/id) {[original-schema original-name] ["ws_alice" "venues_workspace"]}+ (let [card (t2/select-one :model/Card :id card-id)+ mbql-query (-> (:dataset_query card)+ (assoc :lib/metadata (qp.store/metadata-provider)))+ ;; Phase 1 runs during preprocess, mutating the metadata provider in place.+ preprocessed (qp.preprocess/preprocess mbql-query)+ compiled (qp.compile/compile preprocessed)+ ;; Phase 2 runs during execute. Apply it to the compiled SQL.+ rewritten (binding [driver/*driver* :h2]+ (let [captured (atom nil)+ mock-qp (fn [q _rff] (reset! captured q) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)]+ (wrapped (assoc preprocessed+ :database (mt/id)+ :qp/compiled compiled)+ identity)+ (get-in @captured [:qp/compiled :query])))]+ (is (re-find #"(?i)ws_alice|venues_workspace" rewritten)+ "Card MBQL → preprocess → compile → Phase 2 produces workspace identifiers")))))))))++(deftest dashboard-multiple-cards-mixed-remapping-test+ (testing "a Dashboard with multiple cards rewrites each card's SQL according to the remapping config"+ (mt/with-premium-features #{:workspaces}+ (qp.store/with-metadata-provider (mt/id)+ ;; One card targets a remapped table (VENUES), the other a non-remapped table (CHECKINS).+ (let [venues-query (lib.convert/->legacy-MBQL+ (lib/query (qp.store/metadata-provider)+ (lib.metadata/table (qp.store/metadata-provider) (mt/id :venues))))+ checkins-query (lib.convert/->legacy-MBQL+ (lib/query (qp.store/metadata-provider)+ (lib.metadata/table (qp.store/metadata-provider) (mt/id :checkins))))]+ (mt/with-temp [:model/Dashboard {dash-id :id} {}+ :model/Card {venues-card-id :id} {:dataset_query venues-query+ :database_id (mt/id)}+ :model/Card {checkins-card-id :id} {:dataset_query checkins-query+ :database_id (mt/id)}+ :model/DashboardCard _ {:dashboard_id dash-id :card_id venues-card-id}+ :model/DashboardCard _ {:dashboard_id dash-id :card_id checkins-card-id}]+ (with-remappings (mt/id) {["PUBLIC" "VENUES"] ["ws_alice" "venues_workspace"]}+ (let [compile-card-sql+ (fn [card-id]+ (let [card (t2/select-one :model/Card :id card-id)+ query (-> (:dataset_query card)+ (assoc :lib/metadata (qp.store/metadata-provider)))+ pre (qp.preprocess/preprocess query)+ compiled (qp.compile/compile pre)]+ (binding [driver/*driver* :h2]+ (let [captured (atom nil)+ mock-qp (fn [q _rff] (reset! captured q) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)]+ (wrapped (assoc pre :database (mt/id) :qp/compiled compiled) identity)+ (get-in @captured [:qp/compiled :query])))))]+ (testing "the remapped card is rewritten"+ (let [sql (compile-card-sql venues-card-id)]+ (is (re-find #"(?i)ws_alice|venues_workspace" sql)+ "venues card targets ws_alice schema after Phase 2")))+ (testing "the non-remapped card passes through canonical"+ (let [sql (compile-card-sql checkins-card-id)]+ (is (re-find #"(?i)CHECKINS" sql)+ "checkins card keeps its canonical reference")+ (is (not (re-find #"(?i)ws_alice" sql))+ "the workspace schema does not appear in the non-remapped card")))))))))))++;;; ====================================== Cross-cardinality SQL rewrites =========================================+;;;+;;; Drivers vary in how many identifier levels they emit in compiled SQL:+;;; - cardinality 1 (MySQL-style): `SELECT * FROM orders`+;;; - cardinality 2 (Postgres-style): `SELECT * FROM public.orders`+;;; - cardinality 3 (BigQuery-style): `SELECT * FROM proj.ds.orders`+;;; Phase 2 has to handle each shape. These tests exercise the rewriter directly, sidestepping+;;; the warehouse — they verify SQLGlot accepts our `{:db, :schema, :table}` keys and emits+;;; correct output for every cardinality.++;;; These tests use a synthetic db-id and bind `*driver*` directly so we exercise the+;;; rewriter against arbitrary dialects without triggering test-data fixtures for+;;; warehouses we don't intend to provision (e.g. binding `*driver* :mysql` while using+;;; `(mt/id)` would coerce mt to spin up a MySQL container).+(def ^:private synthetic-db-id 99999)++(defn- rewrite-via-phase-2-with-driver+ "Phase 2 rewrite for an arbitrary driver and remappings, against a synthetic db-id.+ Remappings must be `::table-spec`-keyed (no widening). For h2 + real `(mt/id)`, use+ [[rewrite-via-phase-2]]."+ [driver remappings sql]+ (mt/with-premium-features #{:workspaces}+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store {synthetic-db-id remappings})+ driver/*driver* driver]+ (let [called-with (atom nil)+ mock-qp (fn [query _rff] (reset! called-with query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)]+ (wrapped {:database synthetic-db-id :qp/compiled {:query sql}} identity)+ (get-in @called-with [:qp/compiled :query])))))++(deftest phase-2-cardinality-1-mysql-bare-table-test+ (testing "MySQL-style: bare-table SQL with empty-string sentinels in the remapping rewrites correctly"+ (let [rewritten (rewrite-via-phase-2-with-driver+ :mysql+ {{:db "" :schema "" :table "orders"} {:db "" :schema "ws_db" :table "orders"}}+ "SELECT * FROM orders")]+ (is (re-find #"(?i)ws_db" rewritten)+ "the to-side schema (db-as-namespace) appears in the rewritten SQL")+ (is (re-find #"(?i)orders" rewritten)))))++(deftest phase-2-cardinality-2-postgres-schema-table-test+ (testing "Postgres-style: schema.table SQL with the from_db sentinel pruned"+ (let [rewritten (rewrite-via-phase-2-with-driver+ :postgres+ {{:db "" :schema "public" :table "orders"} {:db "" :schema "ws_alice" :table "orders"}}+ "SELECT * FROM public.orders")]+ (is (re-find #"(?i)ws_alice" rewritten))+ (is (not (re-find #"(?i)public\.orders" rewritten))+ "the canonical schema-qualified reference is gone"))))++(deftest phase-2-cardinality-3-bigquery-project-dataset-table-test+ (testing "BigQuery-style: project.dataset.table SQL preserves the project, swaps the dataset"+ (let [rewritten (rewrite-via-phase-2-with-driver+ :bigquery-cloud-sdk+ {{:db "proj" :schema "ds" :table "orders"} {:db "proj" :schema "ws_ds" :table "orders"}}+ "SELECT * FROM `proj`.`ds`.`orders`")]+ (is (re-find #"(?i)ws_ds" rewritten)+ "the workspace dataset appears in the rewritten SQL")+ (is (re-find #"(?i)proj" rewritten)+ "the project (catalog) is preserved")+ (is (not (re-find #"`ds`\.`orders`" rewritten))+ "the canonical dataset.table is gone"))))++;;; =========== T0.1 — native-origin queries: stage `:native` must be rewritten ============+;;;+;;; The stage's `:native` is the source of truth for native-origin SQL. `lib/->legacy-MBQL`+;;; (called inside `metabase.query-processor.execute/run` immediately before driver dispatch)+;;; rebuilds the legacy top-level `:native` from `(get-in query [:stages -1 :native])`. So+;;; patching legacy `:native` directly is futile — the rebuild from the stage clobbers it.+;;; Patch the stage; the rebuild propagates the rewrite to legacy `:native`.++(deftest phase-2-rewrites-native-stage-test+ (testing "Phase 2 rewrites stage's :native for native-origin queries (security boundary)"+ (mt/with-premium-features #{:workspaces}+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {synthetic-db-id+ {{:db "" :schema "PUBLIC" :table "VENUES"} {:db "" :schema "ws_alice" :table "venues"}}})+ driver/*driver* :h2]+ (let [called-with (atom nil)+ mock-qp (fn [query _rff] (reset! called-with query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:lib/type :mbql/query+ :database synthetic-db-id+ :stages [{:lib/type :mbql.stage/native+ :native "SELECT * FROM PUBLIC.VENUES"}]+ :qp/compiled {:query "SELECT * FROM PUBLIC.VENUES"}}]+ (wrapped query identity)+ (testing ":qp/compiled is rewritten (covers FE :native_form display)"+ (is (re-find #"(?i)ws_alice" (get-in @called-with [:qp/compiled :query]))))+ (testing "stage's :native is rewritten -- this is what reaches the warehouse via lib/->legacy-MBQL"+ (is (re-find #"(?i)ws_alice" (get-in @called-with [:stages 0 :native])))+ (is (not (re-find #"(?i)PUBLIC\.VENUES" (get-in @called-with [:stages 0 :native])))+ "no canonical reference survives in the stage")))))))++(deftest phase-2-walks-multi-stage-native-test+ (testing "Phase 2 rewrites :native on every native stage, not just the last"+ (mt/with-premium-features #{:workspaces}+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {synthetic-db-id+ {{:db "" :schema "PUBLIC" :table "VENUES"} {:db "" :schema "ws_alice" :table "venues"}}})+ driver/*driver* :h2]+ (let [called-with (atom nil)+ mock-qp (fn [query _rff] (reset! called-with query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ ;; native stage at index 0 (a source query) with an MBQL stage on top.+ query {:lib/type :mbql/query+ :database synthetic-db-id+ :stages [{:lib/type :mbql.stage/native+ :native "SELECT * FROM PUBLIC.VENUES"}+ {:lib/type :mbql.stage/mbql}]}]+ (wrapped query identity)+ (is (re-find #"(?i)ws_alice" (get-in @called-with [:stages 0 :native]))+ "the leading native stage (not the last stage) is rewritten"))))))++(deftest phase-2-walks-native-in-join-test+ (testing "Phase 2 recurses into joins' :stages and rewrites their native SQL"+ (mt/with-premium-features #{:workspaces}+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {synthetic-db-id+ {{:db "" :schema "PUBLIC" :table "VENUES"} {:db "" :schema "ws_alice" :table "venues"}}})+ driver/*driver* :h2]+ (let [called-with (atom nil)+ mock-qp (fn [query _rff] (reset! called-with query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:lib/type :mbql/query+ :database synthetic-db-id+ :stages [{:lib/type :mbql.stage/mbql+ :joins [{:stages [{:lib/type :mbql.stage/native+ :native "SELECT * FROM PUBLIC.VENUES"}]}]}]}]+ (wrapped query identity)+ (is (re-find #"(?i)ws_alice"+ (get-in @called-with [:stages 0 :joins 0 :stages 0 :native]))+ "native SQL nested inside a join is rewritten"))))))++(deftest phase-2-idempotent-test+ (testing "Phase 2 running twice is a no-op on the second pass"+ (mt/with-premium-features #{:workspaces}+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {synthetic-db-id+ {{:db "" :schema "PUBLIC" :table "VENUES"} {:db "" :schema "ws_alice" :table "venues"}}})+ driver/*driver* :h2]+ (let [run-once (fn [query]+ (let [captured (atom nil)+ mock-qp (fn [q _rff] (reset! captured q) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)]+ (wrapped query identity)+ @captured))+ query {:lib/type :mbql/query+ :database synthetic-db-id+ :stages [{:lib/type :mbql.stage/native+ :native "SELECT * FROM PUBLIC.VENUES"}]+ :qp/compiled {:query "SELECT * FROM PUBLIC.VENUES"}}+ first-pass (run-once query)+ second-pass (run-once first-pass)]+ (is (= (get-in first-pass [:stages 0 :native])+ (get-in second-pass [:stages 0 :native]))+ "second pass leaves the already-rewritten stage SQL unchanged")+ (is (= (get-in first-pass [:qp/compiled :query])+ (get-in second-pass [:qp/compiled :query]))+ "second pass leaves the already-rewritten :qp/compiled unchanged"))))))++;;; =========== T0.2 — workspace remapping engages without `:workspaces` token ============+;;;+;;; Workspace child instances bootstrap from config.yml *before* their token is installed+;;; (see metabase-enterprise.advanced-config.file/initialize!). A child whose remap rows+;;; exist but whose `:workspaces` token isn't yet active must still rewrite reads —+;;; otherwise the child silently leaks production data.++(deftest phase-1-engages-without-workspaces-token-test+ (testing "Phase 1 mutates table metadata even when :workspaces token is absent (workspace child boot)"+ (mt/with-premium-features #{}+ (qp.store/with-metadata-provider (mt/id)+ (let [venues-table (lib.metadata/table (qp.store/metadata-provider) (mt/id :venues))+ original-schema (:schema venues-table)+ original-name (:name venues-table)]+ (with-remappings (mt/id) {[original-schema original-name] ["mb_iso_workspace" "remapped_venues"]}+ (let [query (-> (lib/query (qp.store/metadata-provider) venues-table)+ (assoc :database (mt/id)))]+ (#'ws.middleware/apply-workspace-remapping query)+ (let [table-after (lib.metadata/table (qp.store/metadata-provider) (mt/id :venues))]+ (is (= "mb_iso_workspace" (:schema table-after))+ "Phase 1 fires without the :workspaces token")+ (is (= "remapped_venues" (:name table-after)))))))))))++(deftest phase-2-engages-without-workspaces-token-test+ (testing "Phase 2 rewrites SQL even when :workspaces token is absent (workspace child boot)"+ (mt/with-premium-features #{}+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {synthetic-db-id+ {{:db "" :schema "PUBLIC" :table "VENUES"} {:db "" :schema "ws_alice" :table "venues"}}})+ driver/*driver* :h2]+ (let [called-with (atom nil)+ mock-qp (fn [query _rff] (reset! called-with query) :ok)+ wrapped (#'ws.middleware/apply-workspace-sql-remapping mock-qp)+ query {:database synthetic-db-id+ :qp/compiled {:query "SELECT * FROM PUBLIC.VENUES"}}]+ (wrapped query identity)+ (is (re-find #"(?i)ws_alice" (get-in @called-with [:qp/compiled :query]))+ "Phase 2 must engage on a workspace child even before the :workspaces token is installed"))))))++;;; -------------------------------- Unit tests: TableRemappingMetadataProvider --------------------------------+;;;+;;; These tests exercise the remapping layer in isolation using mock metadata+;;; providers — no QP store, no DB fixtures, no premium-feature gating.++(def ^:private test-db {:id 1 :name "test-db" :engine :h2})++(defn- make-table+ ([id table-name] (make-table id table-name nil))+ ([id table-name schema] {:id id :name table-name :schema schema :db-id 1}))++(defn- make-field [id field-name table-id]+ {:id id :name field-name :table-id table-id :base-type :type/Integer})++(defn- remap-provider+ "Wrap `mp` with a remapping layer. Each spec-pair is `[from to]` where from/to+ are `[schema table]` 2-tuples or `[db schema table]` 3-tuples."+ [mp & spec-pairs]+ (let [remappings (into {} (map (fn [[from to]] [(->spec from) (->spec to)])) spec-pairs)+ transform (#'ws.middleware/table-transform (#'ws.middleware/table-remapper remappings))]+ (lib.metadata/transforming-metadata-provider transform mp)))++(defn- tables-by-id+ "Return `{id {:schema s :name n}}` for all tables visible through `mp`."+ [mp]+ (into {} (map (juxt :id #(select-keys % [:schema :name]))) (lib.metadata/tables mp)))++(deftest ^:parallel remapping-provider-test+ (let [mp (mock/mock-metadata-provider+ {:database test-db+ :tables [(make-table 1 "VENUES" "PUBLIC")+ (make-table 2 "ORDERS" "PUBLIC")+ (make-table 3 "USERS" "PUBLIC")]})+ rmp (remap-provider mp+ [["PUBLIC" "VENUES"] ["workspace_schema" "workspace_venues"]]+ [["PUBLIC" "ORDERS"] ["workspace_schema" "workspace_orders"]])]+ (testing "matched tables are remapped, unmatched pass through"+ (is (= {1 {:schema "workspace_schema" :name "workspace_venues"}+ 2 {:schema "workspace_schema" :name "workspace_orders"}+ 3 {:schema "PUBLIC" :name "USERS"}}+ (tables-by-id rmp))))+ (testing "merge preserves keys the remapping doesn't touch"+ (is (=? {:id 1 :db-id 1}+ (lib.metadata/table rmp 1))))))++(deftest ^:parallel remapping-provider-nil-schema-test+ (testing "schema-less driver: nil in metadata matches the \"\" storage sentinel"+ (let [mp (mock/mock-metadata-provider+ {:database test-db+ :tables [(make-table 1 "VENUES")]})+ rmp (remap-provider mp [["" "VENUES"] ["workspace_schema" "workspace_venues"]])]+ (is (=? {:schema "workspace_schema" :name "workspace_venues"}+ (lib.metadata/table rmp 1))))))++(deftest ^:parallel remapping-provider-db-override-test+ (testing "3-tuple to-spec writes :db onto the remapped table (cross-DB workspaces)"+ (let [mp (mock/mock-metadata-provider+ {:database test-db+ :tables [(make-table 1 "VENUES" "PUBLIC")]})+ rmp (remap-provider mp [["PUBLIC" "VENUES"] ["other_database" "workspace_schema" "workspace_venues"]])]+ (is (=? {:db "other_database" :schema "workspace_schema" :name "workspace_venues"}+ (lib.metadata/table rmp 1))))))++(deftest ^:parallel remapping-provider-non-table-metadata-test+ (testing "fields pass through the remapping layer unchanged"+ (let [mp (mock/mock-metadata-provider+ {:database test-db+ :tables [(make-table 1 "VENUES" "PUBLIC")]+ :fields [(make-field 10 "PRICE" 1)]})+ rmp (remap-provider mp [["PUBLIC" "VENUES"] ["workspace_schema" "workspace_venues"]])]+ (is (=? {:name "PRICE" :table-id 1}+ (lib.metadata/field rmp 10)))))+ (testing "database and settings delegate to parent"+ (let [mp (mock/mock-metadata-provider+ {:database test-db+ :settings {:site-name "My Instance"}})+ rmp (remap-provider mp [["PUBLIC" "VENUES"] ["workspace_schema" "workspace_venues"]])]+ (is (= "test-db" (:name (lib.metadata.protocols/database rmp))))+ (is (= "My Instance" (lib.metadata.protocols/setting rmp :site-name))))))++(deftest ^:parallel remapping-provider-case-sensitive-test+ (testing "match is case-sensitive: \"public\" does not match \"PUBLIC\""+ (let [mp (mock/mock-metadata-provider+ {:database test-db+ :tables [(make-table 1 "VENUES" "PUBLIC")]})+ rmp (remap-provider mp [["public" "VENUES"] ["workspace_schema" "workspace_venues"]])]+ (is (= "PUBLIC" (:schema (lib.metadata/table rmp 1)))))))++(deftest ^:parallel remapping-provider-stacks-test+ (testing "two remapping layers compose — each rewrites independently"+ (let [mp (mock/mock-metadata-provider+ {:database test-db+ :tables [(make-table 1 "VENUES" "PUBLIC")+ (make-table 2 "ORDERS" "PUBLIC")]})+ rmp1 (remap-provider mp [["PUBLIC" "VENUES"] ["alice_schema" "alice_venues"]])+ rmp2 (remap-provider rmp1 [["PUBLIC" "ORDERS"] ["bob_schema" "bob_orders"]])]+ (is (= {1 {:schema "alice_schema" :name "alice_venues"}+ 2 {:schema "bob_schema" :name "bob_orders"}}+ (tables-by-id rmp2))))))++(deftest ^:parallel remapping-provider-no-remappings-identity-test+ (testing "empty remappings = identity"+ (let [mp (mock/mock-metadata-provider+ {:database test-db+ :tables [(make-table 1 "VENUES" "PUBLIC")]})+ rmp (remap-provider mp)]+ (is (=? {:schema "PUBLIC" :name "VENUES"}+ (lib.metadata/table rmp 1))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/remapping/core_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/remapping/core_test.cljnew file mode 100644index 00000000000..8ce358eea63--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/remapping/core_test.clj@@ -0,0 +1,142 @@+(ns metabase-enterprise.workspaces.remapping.core-test+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.workspaces.remapping.core :as ws.remapping]))++(deftest map-store-enabled-for-db?-test+ (testing "returns false for DB without remappings"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store {})]+ (is (false? (ws.remapping/enabled-for-db? 999)))))++ (testing "returns true for DB with remappings"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {1 {{:db "" :schema "public" :table "orders"}+ {:db "" :schema "mb_iso" :table "orders"}}})]+ (is (true? (ws.remapping/enabled-for-db? 1)))+ (is (false? (ws.remapping/enabled-for-db? 2))))))++(deftest map-store-remappings-for-db-test+ (testing "returns empty map for DB without remappings"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store {})]+ (is (= {} (ws.remapping/remappings-for-db 999)))))++ (testing "returns correct mappings (canonical ::table-spec shape)"+ (let [mappings {{:db "" :schema "github_analytics" :table "commits"}+ {:db "" :schema "mb_iso_abc" :table "commits"}+ {:db "" :schema "github_analytics" :table "prs"}+ {:db "" :schema "mb_iso_abc" :table "prs"}}]+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store {1 mappings})]+ (is (= mappings (ws.remapping/remappings-for-db 1))))))++ (testing "different DBs have independent remappings"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {1 {{:db "" :schema "s1" :table "t1"} {:db "" :schema "s2" :table "t1"}}+ 2 {{:db "" :schema "s3" :table "t3"} {:db "" :schema "s4" :table "t3"}}})]+ (is (= {{:db "" :schema "s1" :table "t1"} {:db "" :schema "s2" :table "t1"}}+ (ws.remapping/remappings-for-db 1)))+ (is (= {{:db "" :schema "s3" :table "t3"} {:db "" :schema "s4" :table "t3"}}+ (ws.remapping/remappings-for-db 2)))))++ (testing "BigQuery 3-slot specs (catalog populated)"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {1 {{:db "mb-prod" :schema "core" :table "orders"}+ {:db "mb-prod" :schema "ws_alice" :table "orders"}}})]+ (is (= {{:db "mb-prod" :schema "core" :table "orders"}+ {:db "mb-prod" :schema "ws_alice" :table "orders"}}+ (ws.remapping/remappings-for-db 1))))))++(deftest skip-remapping-test+ (testing "*skip-remapping?* makes enabled-for-db? return false"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {1 {{:db "" :schema "public" :table "orders"}+ {:db "" :schema "mb_iso" :table "orders"}}})+ ws.remapping/*skip-remapping?* true]+ (is (false? (ws.remapping/enabled-for-db? 1))))))++(deftest map-store-get-mapping-test+ (testing "targeted lookup by source spec"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {1 {{:db "" :schema "public" :table "orders"} {:db "" :schema "ws" :table "orders"}+ {:db "mb-prod" :schema "core" :table "users"} {:db "mb-prod" :schema "ws_alice" :table "users"}}})]+ (testing "found: returns the to-side spec"+ (is (= {:db "" :schema "ws" :table "orders"}+ (ws.remapping/get-mapping 1 {:db "" :schema "public" :table "orders"})))+ (is (= {:db "mb-prod" :schema "ws_alice" :table "users"}+ (ws.remapping/get-mapping 1 {:db "mb-prod" :schema "core" :table "users"}))))+ (testing "miss returns nil"+ (is (nil? (ws.remapping/get-mapping 1 {:db "" :schema "public" :table "nope"})))+ (is (nil? (ws.remapping/get-mapping 999 {:db "" :schema "public" :table "orders"}))+ "wrong db-id returns nil")))))++;;; -------------------------- write methods on the map store --------------------------++(deftest map-store-insert-mapping-test+ (testing "insert into an empty store"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store {})]+ (is (some? (ws.remapping/insert-mapping! 1+ {:db "" :schema "public" :table "orders"}+ {:db "" :schema "ws" :table "orders"})))+ (is (= {{:db "" :schema "public" :table "orders"}+ {:db "" :schema "ws" :table "orders"}}+ (ws.remapping/remappings-for-db 1)))))++ (testing "insert is idempotent on the (db, from) key"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store {})]+ (is (some? (ws.remapping/insert-mapping! 1+ {:db "" :schema "public" :table "orders"}+ {:db "" :schema "ws" :table "orders"})))+ (is (some? (ws.remapping/insert-mapping! 1+ {:db "" :schema "public" :table "orders"}+ {:db "" :schema "ws" :table "orders"}))+ "duplicate insert resolves cleanly (returns truthy, no exception)")+ (is (= 1 (count (ws.remapping/remappings-for-db 1)))+ "only one row persists after duplicate insert")))++ (testing "BigQuery 3-slot insert"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store {})]+ (ws.remapping/insert-mapping! 1+ {:db "mb-prod" :schema "core" :table "orders"}+ {:db "mb-prod" :schema "ws_alice" :table "orders"})+ (is (= {{:db "mb-prod" :schema "core" :table "orders"}+ {:db "mb-prod" :schema "ws_alice" :table "orders"}}+ (ws.remapping/remappings-for-db 1))))))++(deftest map-store-remove-mapping-test+ (testing "remove an existing mapping returns 1"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {1 {{:db "" :schema "public" :table "orders"}+ {:db "" :schema "ws" :table "orders"}}})]+ (is (= 1 (ws.remapping/remove-mapping! 1 {:db "" :schema "public" :table "orders"})))+ (is (= {} (ws.remapping/remappings-for-db 1)))))++ (testing "remove a non-existent mapping returns 0"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store {})]+ (is (= 0 (ws.remapping/remove-mapping! 1 {:db "" :schema "public" :table "orders"}))))))++(deftest map-store-clear-for-db-test+ (testing "clear-for-db! removes only the target db's mappings"+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {1 {{:db "" :schema "s1" :table "t1"} {:db "" :schema "ws" :table "t1"}+ {:db "" :schema "s2" :table "t2"} {:db "" :schema "ws" :table "t2"}}+ 2 {{:db "" :schema "other" :table "x"} {:db "" :schema "ws" :table "x"}}})]+ (is (= 2 (ws.remapping/clear-for-db! 1)))+ (is (= {} (ws.remapping/remappings-for-db 1)))+ (is (= {{:db "" :schema "other" :table "x"} {:db "" :schema "ws" :table "x"}}+ (ws.remapping/remappings-for-db 2))+ "other db's remappings untouched"))))++(deftest display-context-suppresses-remapping-test+ (testing "with-display-context binds *skip-remapping?* true so enabled-for-db? returns false+ even when remap rows exist; restores the binding on exit"+ (require 'metabase.workspaces.table-remapping)+ (binding [ws.remapping/*remapping-store* (ws.remapping/map-store+ {1 {{:db "" :schema "public" :table "orders"}+ {:db "" :schema "mb_iso" :table "orders"}}})]+ (is (true? (ws.remapping/enabled-for-db? 1))+ "baseline: remapping is enabled outside the display context")+ ((requiring-resolve 'metabase.workspaces.table-remapping/call-with-display-context)+ (fn []+ (is (false? (ws.remapping/enabled-for-db? 1))+ "inside the display context, remapping is suppressed")))+ (is (true? (ws.remapping/enabled-for-db? 1))+ "binding restored after the display context exits"))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/remapping_cleanup_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/remapping_cleanup_test.cljnew file mode 100644index 00000000000..7a4ff05d196--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/remapping_cleanup_test.clj@@ -0,0 +1,151 @@+(ns metabase-enterprise.workspaces.remapping-cleanup-test+ "Tests for [[metabase-enterprise.workspaces.remapping-cleanup]] — the iso-scoped+ `TableRemapping` deletion path invoked from `provisioning/deprovision-workspace-database!`."+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.workspaces.provisioning :as provisioning]+ [metabase-enterprise.workspaces.remapping-cleanup :as ws.remapping-cleanup]+ [metabase-enterprise.workspaces.table-remapping :as ws.table-remapping]+ [metabase.driver :as driver]+ [metabase.driver.sql :as driver.sql]+ [metabase.test :as mt]+ [metabase.test.util.thread-local :as tu.thread-local]+ [toucan2.core :as t2]))++;;; Register a fake `db-schema-table`-shape engine so this test can exercise the+;;; 3-slot iso-db-slot path without depending on the sqlserver driver module+;;; being on the test classpath.+(driver/register! ::fake-3-slot, :abstract? true)++(defmethod driver.sql/table-qualification-style ::fake-3-slot [_]+ :table-qualification-style/db-schema-table)++(defmethod driver.sql/db-slot-value ::fake-3-slot [_ database]+ (:db (:details database)))++(defn- clean-db-fixture!+ "Run `f` with mappings cleared before and after so tests don't leak state."+ [db-id f]+ (ws.table-remapping/clear-mappings-for-db! db-id)+ (try (f)+ (finally (ws.table-remapping/clear-mappings-for-db! db-id))))++(deftest deletes-only-iso-scoped-rows-test+ (testing "clear-mappings-for-iso! deletes only rows whose to_* match the given iso namespace"+ (clean-db-fixture!+ (mt/id)+ (fn []+ ;; Workspace A's mappings, iso = ws_a.+ (ws.table-remapping/add-mapping!+ (mt/id) {:schema "public" :table "orders"} {:schema "ws_a" :table "orders_copy"})+ (ws.table-remapping/add-mapping!+ (mt/id) {:schema "public" :table "products"} {:schema "ws_a" :table "products_copy"})+ ;; Workspace B's mapping (different canonical table, same DB, different iso).+ (ws.table-remapping/add-mapping!+ (mt/id) {:schema "public" :table "customers"} {:schema "ws_b" :table "customers_copy"})++ (is (= 3 (count (ws.table-remapping/all-mappings-for-db (mt/id))))+ "all three mappings registered")++ ;; Deprovisioning workspace A: pass a fake Database row that won't trigger the+ ;; 3-slot :db branch — Postgres-shaped, no :details :db. iso :db slot stays "".+ (let [n (ws.remapping-cleanup/clear-mappings-for-iso!+ {:engine :postgres :details {}}+ (mt/id)+ "ws_a")]+ (is (= 2 n) "two ws_a rows deleted"))++ (let [remaining (ws.table-remapping/all-mappings-for-db (mt/id))]+ (is (= 1 (count remaining)) "ws_b's row survives")+ (is (= {:db "" :schema "ws_b" :table "customers_copy"}+ (-> remaining vals first))+ "the surviving row is ws_b's"))))))++(deftest noop-when-no-rows-test+ (testing "clear-mappings-for-iso! is idempotent — returns 0 when nothing matches"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (is (= 0+ (ws.remapping-cleanup/clear-mappings-for-iso!+ {:engine :postgres :details {}}+ (mt/id)+ "never_provisioned"))+ "no rows, returns 0")+ (is (= 0+ (ws.remapping-cleanup/clear-mappings-for-iso!+ {:engine :postgres :details {}}+ (mt/id)+ ""))+ "empty output_namespace, returns 0")))))++(deftest three-slot-engine-test+ (testing "clear-mappings-for-iso! matches the iso :db slot for 3-slot engines (SQL Server)"+ (clean-db-fixture!+ (mt/id)+ (fn []+ ;; Two workspaces on the same Metabase Database, different iso :db.+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:db "AnalyticsDB" :schema "dbo" :table "orders"}+ {:db "AnalyticsDB" :schema "ws_a" :table "orders_copy"})+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:db "AnalyticsDB" :schema "dbo" :table "products"}+ {:db "AnalyticsDB" :schema "ws_b" :table "products_copy"})++ (let [n (ws.remapping-cleanup/clear-mappings-for-iso!+ {:engine ::fake-3-slot :details {:db "AnalyticsDB"}}+ (mt/id)+ "ws_a")]+ (is (= 1 n) "only ws_a's row deleted"))++ (is (= [{:to_schema "ws_b"}]+ (->> (t2/select :model/TableRemapping :database_id (mt/id))+ (map #(select-keys % [:to_schema]))))+ "ws_b's row survives")))))++(deftest deprovision-clears-rows-even-when-destroy-throws-test+ (testing "When the driver's `destroy!` step throws (e.g. BQ dataset gone but SA delete fails),+ `deprovision-workspace-database!` must still clear the workspace's `TableRemapping`+ rows. Without that, future queries against canonical tables on this DB rewrite to+ an iso namespace that no longer exists on the warehouse and 500 in the QP. The+ warehouse-side leak (orphan SA / dataset) is acceptable -- app-DB state must match+ the deprovision intent so the workspace stops routing queries."+ ;; Disable the rollback-only tx wrap that `metabase.test.redefs` adds around `with-temp`.+ ;; `deprovision-workspace-database!` rebinds `*current-connectable*` to nil for the+ ;; `TableRemapping` cleanup so the DELETE survives a `with-cluster-lock` rollback when+ ;; `destroy!` throws. Under the rollback-only wrap, that fresh connection can't see+ ;; the fixture rows -- they live only inside the wrap's open transaction. Real+ ;; production callers persist `TableRemapping` rows before deprovision runs.+ (binding [tu.thread-local/*thread-local* false]+ (mt/with-temp [:model/Database {db-id :id} {:engine :postgres :details {}}+ :model/Workspace {ws-id :id} {:name "tear-down"+ :creator_id (mt/user->id :crowberto)}+ :model/WorkspaceDatabase {wsd-id :id} {:workspace_id ws-id+ :database_id db-id+ :input_schemas ["public"]+ :database_details {}+ :output_namespace "ws_alice"+ :status :deprovisioning}]+ (ws.table-remapping/add-mapping!+ db-id {:schema "public" :table "orders"} {:schema "ws_alice" :table "orders_copy"})+ (ws.table-remapping/add-mapping!+ db-id {:schema "public" :table "products"} {:schema "ws_alice" :table "products_copy"})+ (is (= 2 (count (ws.table-remapping/all-mappings-for-db db-id)))+ "fixture: two remap rows registered before deprovision")+ ;; Provisioner that fails on destroy! to simulate partial warehouse teardown+ ;; (e.g. BQ dataset deleted, SA delete throws).+ (let [failing-provisioner (reify provisioning/Provisioner+ (init! [_ _ _ _] (throw (ex-info "not used" {})))+ (grant! [_ _ _ _ _] (throw (ex-info "not used" {})))+ (destroy! [_ _ _ _] (throw (ex-info "warehouse teardown blew up" {}))))]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"warehouse teardown blew up"+ (provisioning/deprovision-workspace-database! wsd-id failing-provisioner))+ "deprovision rethrows the destroy failure so the caller knows"))+ (is (zero? (count (ws.table-remapping/all-mappings-for-db db-id)))+ "remap rows must be cleared even when destroy! threw -- otherwise canonical-table queries 500")+ (is (= :provisioned (:status (t2/select-one :model/WorkspaceDatabase :id wsd-id)))+ "current rollback semantic: failed deprovision flips status back to :provisioned")))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_bigquery.yml b/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_bigquery.ymlnew file mode 100644index 00000000000..a8ee721266b--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_bigquery.yml@@ -0,0 +1,28 @@+# BigQuery workspace - 3-slot identifiers.+#+# BigQuery emits `project.dataset.table` natively (qualified-name-components+# is [:db :schema]). The :db slot carries the GCP project ID (the AST+# catalog), the :schema slot carries the dataset name. Authentication is+# via service-account credentials in :details, not in the wire format.+version: 1+config:+ databases:+ - name: test-data (bigquery)+ engine: bigquery-cloud-sdk+ details:+ project-id: metabase-prod+ service-account-json: '{"private_key": "not-a-real-key"}'+ dataset-filters-type: inclusion+ dataset-filters-patterns: core+ users:+ - first_name: Workspace+ last_name: Admin+ email: [email protected]+ password: password1+ workspace:+ name: New workspace+ databases:+ test-data (bigquery):+ input_schemas:+ - core+ output_namespace: ws_alicediff --git a/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_clickhouse.yml b/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_clickhouse.ymlnew file mode 100644index 00000000000..fe415e943d4--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_clickhouse.yml@@ -0,0 +1,30 @@+# ClickHouse workspace.+#+# ClickHouse emits 2-part `db.table` natively (qualified-name-components is+# [:schema]). The warehouse calls its top level a "database" but the slot+# in our addressing scheme is named :schema - slot names follow AST+# positions, not warehouse vocabulary. ClickHouse's "database" lives at+# the same AST position as Postgres's "schema."+version: 1+config:+ databases:+ - name: test-data (clickhouse)+ engine: clickhouse+ details:+ host: localhost+ port: 8123+ dbname: prod_events+ user: mb__isolation_44490_1933+ password: not-a-real-password+ users:+ - first_name: Workspace+ last_name: Admin+ email: [email protected]+ password: password1+ workspace:+ name: New workspace+ databases:+ test-data (clickhouse):+ input_schemas:+ - prod_events+ output_namespace: ws_alicediff --git a/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_mysql.yml b/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_mysql.ymlnew file mode 100644index 00000000000..eb8fbc6b163--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_mysql.yml@@ -0,0 +1,31 @@+# MySQL workspace - cardinality upgrade case.+#+# MySQL emits bare table identifiers natively (qualified-name-components is+# []), so the source side has only :schema (used as the input filter on the+# warehouse "database" - MySQL synonym for schema). The output side replaces+# it with the workspace database name. Same wire shape as Postgres - the+# slot named :schema carries what MySQL calls a "database" because that's+# the AST position the qualifier lands at.+version: 1+config:+ databases:+ - name: test-data (mysql)+ engine: mysql+ details:+ host: localhost+ port: 3306+ db: prod_db+ user: mb__isolation_44490_1933+ password: not-a-real-password+ users:+ - first_name: Workspace+ last_name: Admin+ email: [email protected]+ password: password1+ workspace:+ name: New workspace+ databases:+ test-data (mysql):+ input_schemas:+ - prod_db+ output_namespace: ws_alicediff --git a/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_postgres.yml b/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_postgres.ymlnew file mode 100644index 00000000000..f777284eb80--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_postgres.yml@@ -0,0 +1,31 @@+version: 1+config:+ databases:+ - name: test-data (postgres)+ engine: postgres+ details:+ host: localhost+ port: 5432+ timezone: America/Los_Angeles+ db: test-data+ user: mb__isolation_44490_1933+ password: $52-8Aqjd%4$=-5H#h!uS#66Pge=+B4u9SqYJ9+fr2ED*&XTh2tPtC2U9+ schema-filters-type: inclusion+ schema-filters-patterns: public+ users:+ - first_name: Workspace+ last_name: Admin+ email: [email protected]+ password: password1+ settings:+ remote-sync-url: https://github.com/metabase/stats-remote-sync+ remote-sync-type: read-write+ remote-sync-branch: main+ remote-sync-token: not-real+ workspace:+ name: New workspace+ databases:+ test-data (postgres):+ input_schemas:+ - public+ output_namespace: mb__isolation_44490_1933diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_redshift.yml b/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_redshift.ymlnew file mode 100644index 00000000000..cb8d0fc261f--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_redshift.yml@@ -0,0 +1,35 @@+# Redshift workspace - 2-slot identifiers (Postgres-family).+#+# Redshift emits `schema.table` (qualified-name-components is [:schema], inherited+# from :postgres). Same wire shape as Postgres; the SSL-enabled JDBC connection+# and port 5439 are the only details that differ from a vanilla Postgres workspace.+version: 1+config:+ databases:+ - name: test-data (redshift)+ engine: redshift+ details:+ host: redshift-cluster.example.us-east-1.redshift.amazonaws.com+ port: 5439+ db: test-data+ user: mb__isolation_44490_1933+ password: $52-8Aqjd%4$=-5H#h!uS#66Pge=+B4u9SqYJ9+fr2ED*&XTh2tPtC2U9+ schema-filters-type: inclusion+ schema-filters-patterns: public+ users:+ - first_name: Workspace+ last_name: Admin+ email: [email protected]+ password: password1+ settings:+ remote-sync-url: https://github.com/metabase/stats-remote-sync+ remote-sync-type: read-write+ remote-sync-branch: main+ remote-sync-token: not-real+ workspace:+ name: New workspace+ databases:+ test-data (redshift):+ input_schemas:+ - public+ output_namespace: mb__isolation_44490_1933diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_sqlserver.yml b/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_sqlserver.ymlnew file mode 100644index 00000000000..cdfd757c89a--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_sqlserver.yml@@ -0,0 +1,29 @@+# SQL Server workspace - 3-slot identifiers.+#+# SQL Server emits `database.schema.table` when crossing databases+# (qualified-name-components is [:db :schema]). Same wire shape as+# BigQuery; vocabulary differs (SQL Server calls them+# "database" and "schema" - matches our slot names directly).+version: 1+config:+ databases:+ - name: test-data (sqlserver)+ engine: sqlserver+ details:+ host: localhost+ port: 1433+ db: AnalyticsDB+ user: mb__isolation_44490_1933+ password: not-a-real-password+ users:+ - first_name: Workspace+ last_name: Admin+ email: [email protected]+ password: password1+ workspace:+ name: New workspace+ databases:+ test-data (sqlserver):+ input_schemas:+ - dbo+ output_namespace: ws_alicediff --git a/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.cljnew file mode 100644index 00000000000..69153c10530--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.clj@@ -0,0 +1,30 @@+(ns metabase-enterprise.workspaces.settings-test+ (:require+ [clojure.test :refer [deftest is testing use-fixtures]]+ [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.settings :as ws.settings]))++(use-fixtures :each (fn [thunk]+ (try+ (thunk)+ (finally+ (ws/clear-instance-workspace!)))))++(deftest has-active-workspace-false-when-atom-empty-test+ (testing "has-active-workspace is false when no :workspace section has been loaded"+ (ws/clear-instance-workspace!)+ (is (false? (ws.settings/has-active-workspace)))))++(deftest has-active-workspace-true-when-atom-populated-test+ (testing "has-active-workspace flips to true once the loader populates the in-process atom"+ (ws/clear-instance-workspace!)+ (is (false? (ws.settings/has-active-workspace)))+ (ws/set-instance-workspace! {:name "Acme" :databases {}})+ (is (true? (ws.settings/has-active-workspace)))))++(deftest has-active-workspace-flips-back-to-false-on-clear-test+ (testing "has-active-workspace returns to false after clear-instance-workspace!"+ (ws/set-instance-workspace! {:name "Acme" :databases {}})+ (is (true? (ws.settings/has-active-workspace)))+ (ws/clear-instance-workspace!)+ (is (false? (ws.settings/has-active-workspace)))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/table_remapping_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/table_remapping_test.cljnew file mode 100644index 00000000000..1a221451a3c--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/table_remapping_test.clj@@ -0,0 +1,1079 @@+(ns metabase-enterprise.workspaces.table-remapping-test+ "Tests for the public writer API in `metabase-enterprise.workspaces.table-remapping`.+ Exercises the round-trip between `add-mapping!`, `remap-table`, `remove-mapping!`,+ `all-mappings-for-db`, `clear-mappings-for-db!`, and `add-transform-target-mapping!`."+ (:require+ [clojure.test :refer :all]+ [java-time.api :as t]+ [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.table-remapping :as ws.table-remapping]+ [metabase-enterprise.workspaces.test-util :as workspaces.tu]+ [metabase.driver :as driver]+ [metabase.driver.connection.workspaces :as driver.conn.w]+ [metabase.sync.fetch-metadata :as fetch-metadata]+ [metabase.sync.sync-metadata.tables :as sync-tables]+ [metabase.test :as mt]+ [metabase.util :as u]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(defn- clean-db-fixture!+ "Run `f` with mappings cleared before and after so tests don't leak state."+ [db-id f]+ (ws.table-remapping/clear-mappings-for-db! db-id)+ (try (f)+ (finally (ws.table-remapping/clear-mappings-for-db! db-id)+ (sync-tables/sync-tables-and-database!+ (t2/select-one :model/Database db-id)))))++(defn- with-provisioned-workspace-db!+ "Set the in-process workspace atom so `db-workspace-namespace` returns+ `{:schema output-schema}` for `db-id`, run `f`, clear the atom on the way out."+ [db-id output-schema f]+ (try+ (ws/set-instance-workspace! {:name "table-remapping-test-ws"+ :databases {db-id {:input_schemas ["_"]+ :output {:schema output-schema}}}})+ (f)+ (finally+ (ws/clear-instance-workspace!))))++(deftest remap-table-returns-nil-when-no-mapping-test+ (clean-db-fixture!+ (mt/id)+ (fn []+ (is (nil? (ws.table-remapping/remap-table (mt/id) {:schema "nope_schema" :name "nope_table"}))))))++(deftest add-then-remap-table-test+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "PUBLIC" :table "ORDERS"}+ {:schema "ws_schema" :table "orders_copy"})+ (is (= {:db nil :schema "ws_schema" :name "orders_copy"}+ (ws.table-remapping/remap-table (mt/id) {:schema "PUBLIC" :name "ORDERS"}))))))++(deftest all-mappings-for-db-test+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id) {:schema "PUBLIC" :table "ORDERS"} {:schema "ws_schema" :table "orders_copy"})+ (ws.table-remapping/add-mapping!+ (mt/id) {:schema "PUBLIC" :table "PRODUCTS"} {:schema "ws_schema" :table "products_copy"})+ (is (= {{:db "" :schema "PUBLIC" :table "ORDERS"} {:db "" :schema "ws_schema" :table "orders_copy"}+ {:db "" :schema "PUBLIC" :table "PRODUCTS"} {:db "" :schema "ws_schema" :table "products_copy"}}+ (ws.table-remapping/all-mappings-for-db (mt/id)))))))++(deftest remove-mapping!-test+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id) {:schema "PUBLIC" :table "ORDERS"} {:schema "ws_schema" :table "orders_copy"})+ (ws.table-remapping/remove-mapping! (mt/id) {:schema "PUBLIC" :table "ORDERS"})+ (is (nil? (ws.table-remapping/remap-table (mt/id) {:schema "PUBLIC" :name "ORDERS"}))))))++(deftest clear-mappings-for-db!-test+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id) {:schema "PUBLIC" :table "ORDERS"} {:schema "ws_schema" :table "orders_copy"})+ (ws.table-remapping/add-mapping!+ (mt/id) {:schema "PUBLIC" :table "PRODUCTS"} {:schema "ws_schema" :table "products_copy"})+ (ws.table-remapping/clear-mappings-for-db! (mt/id))+ (is (= {} (ws.table-remapping/all-mappings-for-db (mt/id)))))))++(deftest add-mapping!-is-idempotent-test+ (testing "duplicate inserts no-op via app-db/update-or-insert! (no exception, no extra row)"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id) {:schema "PUBLIC" :table "ORDERS"} {:schema "ws_schema" :table "orders_copy"})+ (is (some? (ws.table-remapping/add-mapping!+ (mt/id) {:schema "PUBLIC" :table "ORDERS"} {:schema "ws_schema" :table "orders_copy"}))+ "second identical insert resolves cleanly without throwing")+ (is (= {{:db "" :schema "PUBLIC" :table "ORDERS"} {:db "" :schema "ws_schema" :table "orders_copy"}}+ (ws.table-remapping/all-mappings-for-db (mt/id)))+ "only one row persists")))))++;; ------------------------------------------------- add-transform-target-mapping! -------------------------------------------------++(deftest add-transform-target-mapping!-writes-app-db-test+ (testing "add-transform-target-mapping! writes the app-db cache using the workspace's output schema as the to-schema and a collision-resistant to-table"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (with-provisioned-workspace-db!+ (mt/id) "ws_fresh"+ (fn []+ (ws.table-remapping/add-transform-target-mapping!+ (mt/id) {:schema "PUBLIC" :name "ORDERS" :type :table})+ (is (= {:db nil :schema "ws_fresh" :name "PUBLIC__ORDERS"}+ (ws.table-remapping/remap-table (mt/id) {:schema "PUBLIC" :name "ORDERS"}))+ "to-side is rewritten to <from-schema>__<from-name> so cross-schema collisions are avoided")))))))++(deftest add-transform-target-mapping!-is-idempotent-test+ (testing "calling add-transform-target-mapping! twice leaves the app-db with a single row (no duplicate-key explosion)"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (with-provisioned-workspace-db!+ (mt/id) "ws_idem"+ (fn []+ (let [target {:schema "PUBLIC" :name "ORDERS" :type :table}]+ (ws.table-remapping/add-transform-target-mapping! (mt/id) target)+ (ws.table-remapping/add-transform-target-mapping! (mt/id) target)+ (is (= {{:db "" :schema "PUBLIC" :table "ORDERS"} {:db "" :schema "ws_idem" :table "PUBLIC__ORDERS"}}+ (ws.table-remapping/all-mappings-for-db (mt/id)))))))))))++(defn- with-provisioned-workspace-db-namespace!+ "Variant of `with-provisioned-workspace-db!` that takes a full+ `::table-namespace` map (`{:db ?, :schema ?}`) instead of just a schema+ string. Used to exercise cross-DB workspaces (SQL Server / BigQuery) where+ the to-side `:db` slot must flow through to the `TableRemapping` row."+ [db-id output-namespace f]+ (try+ (ws/set-instance-workspace! {:name "ws-3-slot"+ :databases {db-id {:input_schemas ["_"]+ :output output-namespace}}})+ (f)+ (finally+ (ws/clear-instance-workspace!))))++(deftest add-transform-target-mapping!-flows-both-slots-from-namespace-test+ (testing "When the workspace output namespace populates :db, both slots flow into the TableRemapping row"+ ;; Requires the :sqlserver driver class on the test classpath - same skip pattern as the+ ;; per-driver spec-for-table tests above. Run with:+ ;; ./bin/test-agent --drivers=sqlserver,h2 :only '[...this test...]'+ (when (workspaces.tu/driver-loadable? :sqlserver)+ ;; Synthesize the canonical Database row's :engine after :model/Database is created+ ;; so spec-for-table dispatches as SQL Server, populating :db on the from-side.+ (clean-db-fixture!+ (mt/id)+ (fn []+ (with-redefs [t2/select-one (let [orig t2/select-one]+ (fn [model & args]+ (let [row (apply orig model args)]+ (if (and row+ (= model :model/Database)+ (= (:id row) (mt/id)))+ (-> row+ (assoc :engine :sqlserver+ :details {:db "AnalyticsDB"}))+ row))))]+ (with-provisioned-workspace-db-namespace!+ (mt/id) {:db "WS_DB" :schema "ws_alice"}+ (fn []+ (let [to-spec (ws.table-remapping/add-transform-target-mapping!+ (mt/id) {:schema "dbo" :name "orders" :type :table})]+ (testing "to-side spec carries both :db and :schema from the workspace namespace"+ (is (= "WS_DB" (:db to-spec)))+ (is (= "ws_alice" (:schema to-spec)))+ (is (= "dbo__orders" (:name to-spec))+ "table is renamed via remapped-table-name to avoid cross-schema collisions"))+ (testing "TableRemapping row stores both slots, source-side too"+ (is (= {{:db "AnalyticsDB" :schema "dbo" :table "orders"}+ {:db "WS_DB" :schema "ws_alice" :table "dbo__orders"}}+ (ws.table-remapping/all-mappings-for-db (mt/id)))+ "from_db = AnalyticsDB (from spec-for-table on :sqlserver), to_db = WS_DB (from workspace namespace)")))))))))))++(deftest workspace-remap-schema+name-redirects-sync-fetch-test+ (testing "sync's fetch-metadata hook returns [to-schema to-table-name] when a TableRemapping exists"+ (let [db-id (mt/id)]+ (clean-db-fixture!+ db-id+ (fn []+ (is (nil? (ws.table-remapping/workspace-remap-schema+name db-id {:schema "PUBLIC" :name "ORDERS"}))+ "without a remapping, the hook returns nil so sync queries the logical table")+ (ws.table-remapping/add-mapping! db-id+ {:schema "PUBLIC" :table "ORDERS"}+ {:schema "mb_iso_ws" :table "orders_copy"})+ (is (= {:db nil :schema "mb_iso_ws" :name "orders_copy"}+ (ws.table-remapping/workspace-remap-schema+name db-id {:schema "PUBLIC" :name "ORDERS"}))+ "with a remapping, the hook returns the isolated warehouse location so sync asks the driver there"))))))++(deftest fk-metadata-mysql-swaps-connection-to-iso-db-test+ (testing "fk-metadata on a MySQL workspace with a :db swap must run describe-fks under the iso DB.+ Bug GHY-3599: fk-metadata didn't wrap its describe-fks call in with-swapped-connection-details,+ so the JDBC connection talked to the canonical bound DB instead of the iso DB, missing every FK+ that lives in the iso DB."+ (mt/with-model-cleanup [:model/Database]+ (let [db-id (:id (t2/insert-returning-instance!+ :model/Database+ {:engine :mysql+ :name "mysql-fk-fixture"+ :details {:db "appdata"}}))+ ;; Capture both the swap-state and what schema-names the driver call received.+ describe-calls (atom [])]+ (try+ (ws.table-remapping/clear-mappings-for-db! db-id)+ (ws.table-remapping/add-mapping! db-id+ {:db "appdata" :schema nil :table "orders"}+ {:db "appdata_ws" :schema nil :table "orders_copy"})+ (with-redefs [driver/database-supports? (constantly true)+ driver/describe-fks (fn [_driver _db & {:keys [schema-names]}]+ (swap! describe-calls conj+ {:swap-active? (driver.conn.w/has-connection-swap? db-id)+ :schema-names schema-names})+ [])]+ (let [db (t2/select-one :model/Database :id db-id)]+ ;; Sync FK probe runs with the canonical input-schema patterns. On MySQL `:schema` is+ ;; never populated, so the iso DB swap is the only way the probe reaches `appdata_ws`.+ (doall (fetch-metadata/fk-metadata db :schema-names ["appdata"]))))+ (is (seq @describe-calls)+ "describe-fks should be called at least once during fk-metadata")+ (is (every? :swap-active? @describe-calls)+ (str "MySQL fk-metadata must wrap describe-fks in with-swapped-connection-details "+ "so JDBC connections target the iso DB. Captured calls: "+ (pr-str @describe-calls)))+ (finally+ (ws.table-remapping/clear-mappings-for-db! db-id)))))))++(deftest workspace-remap-schema+name-mysql-fills-db-slot-test+ (testing "MySQL: hook receives `{:schema nil :name X}` from sync but must still match a remap row+ whose canonical from-spec carries `:db <bound-db>`. The hook enriches the from-spec from+ the database row's `:details :db` before matching, so the lookup succeeds."+ ;; Insert raw via t2 so `mt/with-temp`'s post-create sync hook does not try+ ;; to connect to MySQL on the test host.+ (mt/with-model-cleanup [:model/Database]+ (let [db-id (:id (t2/insert-returning-instance!+ :model/Database+ {:engine :mysql+ :name "mysql-fixture"+ :details {:db "appdata"}}))]+ (try+ (ws.table-remapping/clear-mappings-for-db! db-id)+ (ws.table-remapping/add-mapping! db-id+ {:db "appdata" :schema nil :table "orders"}+ {:db "appdata_ws" :schema nil :table "orders_copy"})+ (testing "from-spec without :db (the sync caller's shape) still resolves"+ (is (= {:db "appdata_ws" :schema nil :name "orders_copy"}+ (ws.table-remapping/workspace-remap-schema+name db-id {:schema nil :name "orders"}))))+ (finally+ (ws.table-remapping/clear-mappings-for-db! db-id)))))))++(deftest table-fields-metadata-honors-workspace-remapping-test+ (testing "sync/fetch-metadata/table-fields-metadata asks the driver about the remapped warehouse table"+ (let [db-id (mt/id)+ describe-calls (atom [])]+ (clean-db-fixture!+ db-id+ (fn []+ (ws.table-remapping/add-mapping! db-id+ {:schema "PUBLIC" :table "ORDERS"}+ {:schema "mb_iso_ws" :table "orders_copy"})+ (with-redefs [driver/describe-fields+ (fn [_driver _db & {:keys [table-names schema-names]}]+ (swap! describe-calls conj {:path :describe-fields+ :table-names table-names+ :schema-names schema-names})+ #{})+ driver/describe-table+ (fn [_driver _db table]+ (swap! describe-calls conj {:path :describe-table+ :schema (:schema table)+ :name (:name table)})+ {:fields #{}})]+ (let [logical-table (t2/instance :model/Table+ {:id 999 :name "ORDERS" :schema "PUBLIC" :db_id db-id})]+ (fetch-metadata/table-fields-metadata+ (t2/select-one :model/Database :id db-id)+ logical-table))+ (is (= 1 (count @describe-calls)))+ (let [call (first @describe-calls)]+ (testing "driver is asked about the remapped (to_schema, to_table_name), not the logical source"+ (case (:path call)+ :describe-fields+ (do (is (= ["orders_copy"] (:table-names call)))+ (is (= ["mb_iso_ws"] (:schema-names call))))+ :describe-table+ (do (is (= "orders_copy" (:name call)))+ (is (= "mb_iso_ws" (:schema call))))+ (is false (str "unexpected path " (:path call))))))))))))++(deftest add-transform-target-mapping!-requires-workspaced-db-test+ (testing "throws with a clear error when db is not workspaced (db-workspace-namespace returns nil)"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (let [ex (try+ (ws.table-remapping/add-transform-target-mapping!+ (mt/id) {:schema "PUBLIC" :name "ORDERS" :type :table})+ nil+ (catch clojure.lang.ExceptionInfo e e))]+ (is (some? ex) "add-transform-target-mapping! must throw when the db is not workspaced")+ (is (re-find #"not workspaced" (ex-message ex)))+ (is (= (mt/id) (:db-id (ex-data ex)))))+ (testing "no app-db row was written"+ (is (nil? (ws.table-remapping/remap-table (mt/id) {:schema "PUBLIC" :name "ORDERS"}))))))))++;;; ------------------------------------------------- remapped-table-name -------------------------------------------------+;;;+;;; Unit tests for the rename helper. Two source tables sharing a `:table` slot under different+;;; `:schema` slots must produce distinct `:table` slots after the rewrite, even when the driver's+;;; identifier limit forces truncation.++(deftest ^:parallel remapped-table-name-short-passthrough-test+ (testing "inputs under the driver's limit pass through as schema__table"+ (is (= {:db "" :schema "public" :table "public__orders"}+ (ws.table-remapping/remapped-table-name+ :postgres {:db "" :schema "public" :table "orders"})))))++(deftest ^:parallel remapped-table-name-determinism-test+ (testing "same input -> same output"+ (let [in {:db "" :schema "schema_a" :table "orders"}]+ (is (= (ws.table-remapping/remapped-table-name :postgres in)+ (ws.table-remapping/remapped-table-name :postgres in))))))++(deftest ^:parallel remapped-table-name-distinct-schemas-test+ (testing "two source tables with same :table under different :schema produce distinct :table slots"+ (let [a (ws.table-remapping/remapped-table-name :postgres {:db "" :schema "schemaA" :table "orders"})+ b (ws.table-remapping/remapped-table-name :postgres {:db "" :schema "schemaB" :table "orders"})]+ (is (= "schemaA__orders" (:table a)))+ (is (= "schemaB__orders" (:table b)))+ (is (not= (:table a) (:table b))))))++(deftest ^:parallel remapped-table-name-honors-driver-limit-test+ (testing "Postgres (63) hashes long names; H2 (256) leaves them readable"+ (let [long-schema (apply str (repeat 40 "a"))+ long-table (apply str (repeat 40 "b"))+ in {:db "" :schema long-schema :table long-table}+ pg (ws.table-remapping/remapped-table-name :postgres in)+ h2 (ws.table-remapping/remapped-table-name :h2 in)]+ (is (<= (count (:table pg)) 63))+ (is (re-find #"_[0-9a-z]{8}$" (:table pg))+ "Postgres output ends in an 8-char base36 disambiguating suffix")+ (is (= (str long-schema "__" long-table) (:table h2))+ "H2's 256-char headroom leaves the readable concatenation alone"))))++(deftest ^:parallel remapped-table-name-collision-resistance-under-truncation-test+ (testing "different long inputs that share a prefix produce distinct outputs after truncation"+ (let [base (apply str (repeat 60 "x"))+ a (ws.table-remapping/remapped-table-name :postgres {:db "" :schema (str base "_a") :table "tbl"})+ b (ws.table-remapping/remapped-table-name :postgres {:db "" :schema (str base "_b") :table "tbl"})]+ (is (<= (count (:table a)) 63))+ (is (<= (count (:table b)) 63))+ (is (not= (:table a) (:table b))+ "the hash suffix discriminates inputs that would collide after naive truncation"))))++(deftest ^:parallel remapped-table-name-truncation-shape-test+ (testing "with max-len = 20"+ ;; Layout, position by position:+ ;;+ ;; positions: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20+ ;;+ ;; verbatim: s c h e m a A _ _ t a b l e _ f i t s (19, verbatim)+ ;; at limit: s c h e m a A _ _ t a b l e _ f i t s ! (20, verbatim)+ ;; over limit: s c h e m a A _ _ t a _ <-------- 8-char hash -------->+ ;; [-- first (max-len - 9) chars of raw -][- _ + 8 base36 chars (9 total) -]+ ;;+ ;; When raw exceeds max-len, the function preserves the first (max-len - 9) chars of raw+ ;; and appends an underscore + 8-char base36 hash of the FULL pre-truncation raw. So two+ ;; inputs sharing those first chars still get distinct outputs via the hash.+ (testing "verbatim cases (raw <= max-len)"+ (doseq [[case-name {:keys [table-spec expected-output]}]+ {"raw is 18 chars, under limit"+ {:table-spec {:db "" :schema "schemaA" :table "table_fits"}+ :expected-output "schemaA__table_fits"}++ "raw is 20 chars, exactly at limit"+ {:table-spec {:db "" :schema "schemaA" :table "table_fits!"}+ :expected-output "schemaA__table_fits!"}}]+ (testing case-name+ (let [out (:table (ws.table-remapping/remapped-table-name-with-limit table-spec 20))]+ (is (= expected-output out))+ (is (>= 20 (count out)) "output is at or under the limit")))))+ (testing "overflow cases (raw > max-len): output is <first 11 chars of raw>_<8-char base36 hash of full raw>"+ ;; The hash is deterministic, so we pin the entire 20-char output literally.+ ;; All three raws share the prefix "schemaA__ta" -- the hash discriminates them.+ (doseq [[case-name {:keys [table-spec expected-output]}]+ {"raw is 21 chars: 1 over the limit"+ {:table-spec {:db "" :schema "schemaA" :table "table_no_fit"}+ :expected-output "schemaA__ta_lnvvf3pc"}++ "raw is 27 chars: shares the first 11 chars with the 21-char case above"+ {:table-spec {:db "" :schema "schemaA" :table "table_alpha_long"}+ :expected-output "schemaA__ta_s6fpa2r3"}++ "raw is 28 chars: also shares the first 11 chars"+ {:table-spec {:db "" :schema "schemaA" :table "table_beta_longer"}+ :expected-output "schemaA__ta_rmulb7o0"}}]+ (testing case-name+ (let [out (:table (ws.table-remapping/remapped-table-name-with-limit table-spec 20))]+ (is (= expected-output out))+ (is (= 20 (count out)) "output is exactly the limit")))))))++(deftest ^:parallel remapped-table-name-byte-aware-truncation-test+ (testing "limit is enforced in UTF-8 bytes, not Java chars, and truncation never splits a multi-byte codepoint"+ ;; Each Japanese char is 1 Java char and 3 UTF-8 bytes. With six of them in the schema,+ ;; raw = "ああああああ__orders" -> 14 chars but 26 bytes. With max-bytes = 20:+ ;; - char-based logic would see 14 <= 20 and skip truncation (BUG: 26 bytes overflows Postgres)+ ;; - byte-aware logic truncates because string-byte-count returns 26+ ;;+ ;; Truncation is codepoint-safe: head-room = 20 - 9 = 11 bytes; we fit only 3 full+ ;; Japanese chars (9 bytes) plus the 9-byte ASCII suffix = 18 bytes total, which is+ ;; at-or-under the limit without ever splitting a 3-byte codepoint into 1.5.+ (let [out (:table (ws.table-remapping/remapped-table-name-with-limit+ {:db "" :schema "ああああああ" :table "orders"}+ 20))]+ (is (= "あああ_pgiv8lx2" out))+ (is (= 12 (count out)) "12 java chars: 3 Japanese + underscore + 8 ASCII hash chars")+ (is (= 18 (u/string-byte-count out))+ "18 UTF-8 bytes: 9 from the 3 Japanese + 9 from the ASCII suffix; <=20 bytes")+ (is (>= 20 (u/string-byte-count out)) "output respects the byte limit"))))++(deftest ^:parallel remapped-table-name-postgres-real-limit-test+ (testing "with the real :postgres limit of 63: a long schema__table concatenation truncates to 54 chars + '_' + 8-char hash"+ ;; raw = "marketing_analytics_warehouse_production__monthly_customer_lifetime_value_summary" (81 chars)+ ;; head-len = 63 - 9 = 54, head = first 54 chars of raw.+ (let [out (:table (ws.table-remapping/remapped-table-name+ :postgres+ {:db "" :schema "marketing_analytics_warehouse_production"+ :table "monthly_customer_lifetime_value_summary"}))]+ (is (= "marketing_analytics_warehouse_production__monthly_cust_x5niligw" out))+ (is (= 63 (count out)) "output is exactly the Postgres identifier limit"))))++(deftest ^:parallel remapped-table-name-mysql-empty-schema-test+ (testing "MySQL has no schema dimension (qualified-name-components is []) -- empty :schema produces __table without throwing"+ (is (= "__orders"+ (:table (ws.table-remapping/remapped-table-name+ :mysql {:db "" :schema "" :table "orders"}))))))++;;; ------------------------------------------------- cross-schema collision ------------------------------------------------++(deftest add-transform-target-mapping!-cross-schema-collision-test+ (testing "two source tables sharing :name across different :schema land at distinct warehouse identifiers"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (with-provisioned-workspace-db!+ (mt/id) "ws_collide"+ (fn []+ (let [a-spec (ws.table-remapping/add-transform-target-mapping!+ (mt/id) {:schema "schemaA" :name "ORDERS" :type :table})+ b-spec (ws.table-remapping/add-transform-target-mapping!+ (mt/id) {:schema "schemaB" :name "ORDERS" :type :table})]+ (testing "the two writers return distinct to-side specs"+ (is (= "schemaA__ORDERS" (:name a-spec)))+ (is (= "schemaB__ORDERS" (:name b-spec))))+ (testing "both rows are present and resolve to distinct warehouse names"+ (is (= {:db nil :schema "ws_collide" :name "schemaA__ORDERS"}+ (ws.table-remapping/remap-table (mt/id) {:schema "schemaA" :name "ORDERS"}))+ "schemaA.ORDERS resolves to a unique warehouse identifier")+ (is (= {:db nil :schema "ws_collide" :name "schemaB__ORDERS"}+ (ws.table-remapping/remap-table (mt/id) {:schema "schemaB" :name "ORDERS"}))+ "schemaB.ORDERS resolves to a different warehouse identifier")))))))))++;;; --------------------------- Pre-sync ordering: to-side has no :model/Table yet -------------------------------+;;;+;;; A `TableRemapping` row can be inserted before sync has run on the workspace's destination schema, so the+;;; `(to_schema, to_table_name)` pair has no corresponding `:model/Table` row in the app DB yet. Every code path+;;; that consumes the remapping must operate on the schema/table strings alone — none should require a hydrated+;;; `:model/Table` for the to-side. These tests pin that contract so a future change that adds `:model/Table`+;;; lookup on the to-side surfaces immediately.++(deftest remap-table-works-without-to-side-model-table-test+ (testing "remap-table returns the [to-schema to-table-name] pair even when no :model/Table exists for it"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (let [to-schema "ws_unsynced"+ to-table "orders_workspace_copy"]+ (testing "precondition: no :model/Table row exists for the to-side"+ (is (nil? (t2/select-one :model/Table :db_id (mt/id) :schema to-schema :name to-table))))+ (ws.table-remapping/add-mapping! (mt/id)+ {:schema "PUBLIC" :table "ORDERS"}+ {:schema to-schema :table to-table})+ (is (= {:db nil :schema to-schema :name to-table}+ (ws.table-remapping/remap-table (mt/id) {:schema "PUBLIC" :name "ORDERS"}))+ "remap-table operates on strings only — no :model/Table lookup on the to-side"))))))++(deftest workspace-remap-schema+name-works-without-to-side-model-table-test+ (testing "the sync hook returns [to-schema to-table-name] even when no :model/Table exists for it"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (let [to-schema "ws_unsynced"+ to-table "orders_workspace_copy"]+ (is (nil? (t2/select-one :model/Table :db_id (mt/id) :schema to-schema :name to-table))+ "precondition: no :model/Table row exists for the to-side")+ (ws.table-remapping/add-mapping! (mt/id)+ {:schema "PUBLIC" :table "ORDERS"}+ {:schema to-schema :table to-table})+ (is (= {:db nil :schema to-schema :name to-table}+ (ws.table-remapping/workspace-remap-schema+name (mt/id) {:schema "PUBLIC" :name "ORDERS"}))+ "sync hook returns the remap pair regardless of to-side sync state"))))))++(deftest all-mappings-for-db-works-without-to-side-model-tables-test+ (testing "all-mappings-for-db returns rows whose to-side has no :model/Table yet"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping! (mt/id) {:schema "PUBLIC" :table "ORDERS"} {:schema "ws_unsynced" :table "orders_copy"})+ (ws.table-remapping/add-mapping! (mt/id) {:schema "PUBLIC" :table "PEOPLE"} {:schema "ws_unsynced" :table "people_copy"})+ (ws.table-remapping/add-mapping! (mt/id) {:schema "PUBLIC" :table "PRODUCTS"} {:schema "ws_unsynced" :table "products_copy"})+ (let [mappings (ws.table-remapping/all-mappings-for-db (mt/id))]+ (is (= {{:db "" :schema "PUBLIC" :table "ORDERS"} {:db "" :schema "ws_unsynced" :table "orders_copy"}+ {:db "" :schema "PUBLIC" :table "PEOPLE"} {:db "" :schema "ws_unsynced" :table "people_copy"}+ {:db "" :schema "PUBLIC" :table "PRODUCTS"} {:db "" :schema "ws_unsynced" :table "products_copy"}}+ mappings)+ "all-mappings-for-db is purely (db, schema, name) string-based — no :model/Table required"))))))++;; ----------------------------- spec-for-table -----------------------------+;;+;; Per-driver hierarchy resolution. Verifies the {db, schema, table} shape we'd+;; persist in `:model/TableRemapping` rows for a given (database, table) pair.++(deftest ^:parallel spec-for-table-h2-test+ (testing "H2 (default test driver) populates :schema only"+ (let [database {:engine :h2 :name "test-data (h2)"}+ table-row {:name "ORDERS" :schema "PUBLIC"}+ spec (ws.table-remapping/spec-for-table database table-row)]+ (is (= "" (:db spec)) "db slot is the empty-string sentinel for non-catalog drivers")+ (is (= "PUBLIC" (:schema spec)))+ (is (= "ORDERS" (:table spec))))))++(deftest ^:parallel spec-for-table-mysql-engine-test+ (testing "MySQL populates neither :db nor :schema (bare table)"+ (let [database {:engine :mysql :name "test-data"}+ table {:name "orders" :schema nil}+ {:keys [db schema]} (ws.table-remapping/spec-for-table database table)]+ (is (= "" db))+ (is (= "" schema)))))++;; The clickhouse / bigquery `spec-for-table` tests below exercise the per-driver branches+;; of `engine-namespace-positions`. They require the driver to be loaded —+;; not for warehouse interaction, but because `(driver/qualified-name-components engine)`+;; triggers driver lazy-load via `dispatch-on-initialized-driver`. Skipped when the driver+;; isn't on the test classpath.++(deftest spec-for-table-clickhouse-engine-test+ (when (workspaces.tu/driver-loadable? :clickhouse)+ (testing "ClickHouse fills :schema from the Table row's :schema column (CH sync stores its database name there)"+ (let [database (assoc (t2/select-one :model/Database :id (mt/id)) :engine :clickhouse)+ table (t2/select-one :model/Table :id (mt/id :orders))+ {:keys [db schema]} (ws.table-remapping/spec-for-table database table)]+ (is (= "" db) "no catalog level on ClickHouse")+ (is (= (:schema table) schema)+ "schema-position reads off Table.:schema (= CH warehouse database name as recorded by sync)")))))++(deftest spec-for-table-bigquery-engine-test+ (when (workspaces.tu/driver-loadable? :bigquery-cloud-sdk)+ (testing "BigQuery fills :db from connection details :project-id"+ (let [database {:engine :bigquery-cloud-sdk+ :name "ignored"+ :details {:project-id "my-proj"}}+ table {:name "orders" :schema "ds"}+ {:keys [db schema table]} (ws.table-remapping/spec-for-table database table)]+ (is (= "my-proj" db))+ (is (= "ds" schema))+ (is (= "orders" table))))))++(deftest spec-for-table-bigquery-no-project-id-test+ (when (workspaces.tu/driver-loadable? :bigquery-cloud-sdk)+ (testing "BigQuery without explicit :project-id leaves :db empty (does not leak credentials blob)"+ (let [database {:engine :bigquery-cloud-sdk+ :name "ignored"+ :details {:service-account-json "{\"private_key\": \"secret\"}"}}+ table {:name "orders" :schema "ds"}+ {:keys [db]} (ws.table-remapping/spec-for-table database table)]+ (is (= "" db) "service-account-json must NOT be used as a project id")))))++;; ----------------------------- Per-driver addressing truth table -----------------------------+;;+;; Mirrors the per-driver table in+;; ai-reports/2026-05-01-canonical-table-addressing-scheme.md+;; Update both together. This is the executable spec for the addressing contract:+;;+;; 1. `qualified-name-components` returns the documented value per driver.+;; 2. `spec-for-table` populates exactly the slots `qualified-name-components`+;; lists, with empty-string sentinels everywhere else.+;;+;; Pure: no warehouse connection. Each row needs the driver class on the test+;; classpath — `workspaces.tu/driver-loadable?` skip pattern is reused.++(def ^:private addressing-truth-table+ "Per-driver addressing contract. Each row:+ [driver expected-components db-name-or-details table-row expected-spec]++ `db-name-or-details` is the canonical Database row data the test uses. For+ drivers where the rewriter only cares about `:name` (Postgres/Redshift/H2/+ MySQL/ClickHouse) we pass a `:name`. For BigQuery we pass `:details+ {:project-id ...}` — that's the only detail key `spec-for-table` reads, and+ making it visible here documents the load-bearing contract."+ ;; driver components database-fields table-row expected-spec+ [[:postgres [:schema] {:name "analytics"} {:schema "public" :name "orders"} {:db "" :schema "public" :table "orders"}]+ [:redshift [:schema] {:name "analytics"} {:schema "public" :name "orders"} {:db "" :schema "public" :table "orders"}]+ [:h2 [:schema] {:name "mem:test"} {:schema "PUBLIC" :name "ORDERS"} {:db "" :schema "PUBLIC" :table "ORDERS"}]+ [:mysql [:db] {:name "ignored" :details {:db "analytics"}} {:schema nil :name "orders"} {:db "analytics" :schema "" :table "orders"}]+ [:clickhouse [:schema] {:name "ignored"} {:schema "analytics" :name "events"} {:db "" :schema "analytics" :table "events"}]+ [:sqlserver [:db :schema] {:name "ignored" :details {:db "AnalyticsDB"}} {:schema "dbo" :name "orders"} {:db "AnalyticsDB" :schema "dbo" :table "orders"}]+ [:bigquery-cloud-sdk [:db :schema] {:name "ignored" :details {:project-id "metabase-prod"}} {:schema "core" :name "orders"} {:db "metabase-prod" :schema "core" :table "orders"}]])++(deftest ^:parallel addressing-truth-table-qualified-name-components-test+ (testing "qualified-name-components returns the documented AST positions per driver"+ (doseq [[driver expected-components & _] addressing-truth-table]+ (when (workspaces.tu/driver-loadable? driver)+ (testing (str driver)+ (is (= expected-components+ (driver/qualified-name-components driver))+ "Update the canonical-table-addressing scheme doc and this row together if this changes."))))))++(deftest ^:parallel addressing-truth-table-spec-for-table-test+ (testing "spec-for-table produces the documented {:db, :schema, :table} shape per driver"+ (doseq [[driver _components db-fields table-row expected-spec] addressing-truth-table]+ (when (workspaces.tu/driver-loadable? driver)+ (testing (str driver)+ (let [database (assoc db-fields :engine driver)]+ (is (= expected-spec+ (ws.table-remapping/spec-for-table database table-row)))))))))++(deftest ^:parallel addressing-truth-table-slot-population-invariant-test+ (testing "Invariant: slot is non-empty iff qualified-name-components includes it"+ (doseq [[driver components db-fields table-row _expected-spec] addressing-truth-table]+ (when (workspaces.tu/driver-loadable? driver)+ (testing (str driver)+ (let [database (assoc db-fields :engine driver)+ spec (ws.table-remapping/spec-for-table database table-row)+ comp-set (set components)]+ (is (= (contains? comp-set :db) (not= "" (:db spec)))+ (str ":db slot population must match qualified-name-components — got "+ (pr-str spec)))+ (is (= (contains? comp-set :schema) (not= "" (:schema spec)))+ (str ":schema slot population must match qualified-name-components — got "+ (pr-str spec)))))))))++;; ----------------------------- Cache invalidation hooks -----------------------------+;;+;; Inserting or deleting a TableRemapping must invalidate the QP results cache for the+;; affected database. Otherwise a query cached *before* a remap was registered would+;; silently return canonical-table results forever — Phase 2's SQL rewriter never runs+;; on cache hits, so a stale entry leaks production data into the workspace.++(defn- cache-config-invalidated-at [db-id]+ (t2/select-one-fn :invalidated_at :model/CacheConfig+ :model "database"+ :model_id db-id))++(defn- with-database-cache-config!+ "Ensure a `:database` CacheConfig row exists for `db-id` with a known `invalidated_at`.+ Runs `f` with the row's actual stored `invalidated_at` (read back to avoid+ `OffsetDateTime` vs `ZonedDateTime` typing mismatches across drivers) and cleans+ up the row on the way out."+ [db-id f]+ (try+ (t2/insert! :model/CacheConfig+ {:model "database"+ :model_id db-id+ :strategy :ttl+ :config {:multiplier 10 :min_duration_ms 1}+ :invalidated_at (t/offset-date-time 2020 1 1)})+ (f (cache-config-invalidated-at db-id))+ (finally+ (t2/delete! :model/CacheConfig :model "database" :model_id db-id))))++(deftest cache-invalidation-on-insert-test+ (testing "inserting a TableRemapping bumps cache_config.invalidated_at for the database"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (with-database-cache-config!+ (mt/id)+ (fn [initial-invalidated-at]+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "PUBLIC" :table "ORDERS"}+ {:schema "ws_schema" :table "orders_copy"})+ (let [after (cache-config-invalidated-at (mt/id))]+ (is (some? after) "cache config still exists")+ (is (t/after? after initial-invalidated-at)+ "invalidated_at was bumped past its previous value"))))))))++(deftest cache-invalidation-on-delete-test+ (testing "deleting a TableRemapping bumps cache_config.invalidated_at for the database"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (with-database-cache-config!+ (mt/id)+ (fn [_]+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "PUBLIC" :table "ORDERS"}+ {:schema "ws_schema" :table "orders_copy"})+ (let [post-insert (cache-config-invalidated-at (mt/id))]+ (Thread/sleep (long 10)) ; ensure timestamp clock advances+ (ws.table-remapping/remove-mapping! (mt/id) {:schema "PUBLIC" :table "ORDERS"})+ (let [post-delete (cache-config-invalidated-at (mt/id))]+ (is (t/after? post-delete post-insert)+ "delete bumped invalidated_at past its post-insert value")))))))))++(deftest cache-invalidation-only-affects-target-database-test+ (testing "inserting a remap on db A doesn't invalidate db B's cache config"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (let [other-db-id 999999]+ (with-database-cache-config!+ (mt/id)+ (fn [_]+ (with-database-cache-config!+ other-db-id+ (fn [other-initial]+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "PUBLIC" :table "ORDERS"}+ {:schema "ws_schema" :table "orders_copy"})+ (let [other-after (cache-config-invalidated-at other-db-id)]+ (is (= (t/instant other-initial) (t/instant other-after))+ "the other database's invalidated_at is untouched")))))))))))++;;; -------------------------- DEV-1898: filter-workspace-side-tables --------------------------++(deftest filter-workspace-side-tables-no-rows-test+ (testing "without any remap rows, the filter is identity"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (let [tuples #{{:schema "public" :name "orders"}+ {:schema "public" :name "users"}}]+ (is (= tuples (ws.table-remapping/filter-workspace-side-tables tuples (mt/id)))))))))++(deftest filter-workspace-side-tables-drops-only-to-side-test+ (testing "with a remap row, only the to-side (schema, name) tuple is dropped"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "public" :table "orders"}+ {:schema "ws_alice" :table "orders"})+ (let [tuples #{{:schema "public" :name "orders"}+ {:schema "public" :name "users"}+ {:schema "ws_alice" :name "orders"}}+ filtered (ws.table-remapping/filter-workspace-side-tables tuples (mt/id))]+ (is (= #{{:schema "public" :name "orders"}+ {:schema "public" :name "users"}}+ filtered)+ "ws_alice.orders is removed; canonical tuples pass through"))))))++(deftest filter-workspace-side-tables-scoped-by-database-test+ (testing "remappings on db A do not affect filtering on db B"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "public" :table "orders"}+ {:schema "ws_alice" :table "orders"})+ (let [other-db-id 999999+ tuples #{{:schema "ws_alice" :name "orders"}}]+ (is (= tuples (ws.table-remapping/filter-workspace-side-tables tuples other-db-id))+ "another database's table-list is untouched"))))))++(deftest sync-tables-and-database-skips-workspace-side-tuples-test+ (testing "DEV-1898: sync-tables-and-database! does not create :model/Table rows for workspace-side tuples"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (mt/with-temp [:model/Table _ {:db_id (mt/id)+ :schema "public"+ :name "dev_1898_orders"}]+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "public" :table "dev_1898_orders"}+ {:schema "ws_dev_1898" :table "dev_1898_orders"})+ ;; Simulate a driver whose describe-database surfaces both the canonical+ ;; and workspace-isolation schemas.+ (let [fake-metadata {:tables #{{:schema "public" :name "dev_1898_orders"}+ {:schema "ws_dev_1898" :name "dev_1898_orders"}}}]+ (with-redefs [fetch-metadata/db-metadata (constantly fake-metadata)]+ (sync-tables/sync-tables-and-database! (t2/select-one :model/Database (mt/id)))))+ (let [created-pairs (set (t2/select-fn-set (juxt :schema :name)+ :model/Table+ :db_id (mt/id)+ :name "dev_1898_orders"))]+ (is (contains? created-pairs ["public" "dev_1898_orders"])+ "canonical Table row exists")+ (is (not (contains? created-pairs ["ws_dev_1898" "dev_1898_orders"]))+ "workspace-side Table row is filtered out and never persisted")))))))++;;; -------------------------- expand-schema-names-with-workspace --------------------------++(deftest expand-schema-names-no-rows-test+ (testing "without remap rows, schema-names pass through unchanged"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (is (= ["public" "analytics"]+ (ws.table-remapping/expand-schema-names-with-workspace+ ["public" "analytics"] (mt/id))))))))++(deftest expand-schema-names-adds-workspace-schemas-test+ (testing "input schemas with active remappings get their to-side schema added"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "public" :table "orders"}+ {:schema "ws_alice" :table "orders"})+ (let [expanded (ws.table-remapping/expand-schema-names-with-workspace+ ["public" "analytics"] (mt/id))]+ (is (= #{"public" "analytics" "ws_alice"} (set expanded))+ "ws_alice is appended; analytics (no remap) untouched"))))))++(deftest expand-schema-names-no-duplicates-test+ (testing "expansion does not introduce duplicates if to-schema is already in input"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "public" :table "orders"}+ {:schema "ws_alice" :table "orders"})+ (let [expanded (ws.table-remapping/expand-schema-names-with-workspace+ ["public" "ws_alice"] (mt/id))]+ (is (= 2 (count expanded)) "no duplicate ws_alice")+ (is (= #{"public" "ws_alice"} (set expanded))))))))++;;; -------------------------- rewrite-fk-result-canonical --------------------------++(deftest rewrite-fk-no-remappings-test+ (testing "without remap rows, rows pass through unchanged"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (let [rows [{:fk-table-schema "public" :fk-table-name "orders" :fk-column-name "user_id"+ :pk-table-schema "public" :pk-table-name "users" :pk-column-name "id"}]]+ (is (= rows (ws.table-remapping/rewrite-fk-result-canonical rows (mt/id)))))))))++(deftest rewrite-fk-translates-both-sides-test+ (testing "FK row referencing workspace identifiers on both ends gets translated"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "public" :table "orders"}+ {:schema "ws_alice" :table "orders"})+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "public" :table "users"}+ {:schema "ws_alice" :table "users"})+ (let [workspace-row {:fk-table-schema "ws_alice" :fk-table-name "orders" :fk-column-name "user_id"+ :pk-table-schema "ws_alice" :pk-table-name "users" :pk-column-name "id"}+ [out] (ws.table-remapping/rewrite-fk-result-canonical [workspace-row] (mt/id))]+ (is (= "public" (:fk-table-schema out)))+ (is (= "orders" (:fk-table-name out)))+ (is (= "public" (:pk-table-schema out)))+ (is (= "users" (:pk-table-name out))))))))++(deftest rewrite-fk-leaves-non-remapped-targets-alone-test+ (testing "FK to a canonical-only table (no remap) passes through; remapped end is rewritten"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "public" :table "orders"}+ {:schema "ws_alice" :table "orders"})+ (let [row {:fk-table-schema "ws_alice" :fk-table-name "orders" :fk-column-name "audit_id"+ :pk-table-schema "audit" :pk-table-name "events" :pk-column-name "id"}+ [out] (ws.table-remapping/rewrite-fk-result-canonical [row] (mt/id))]+ (is (= "public" (:fk-table-schema out))+ "remapped fk-side is back-translated")+ (is (= "orders" (:fk-table-name out)))+ (is (= "audit" (:pk-table-schema out))+ "non-remapped pk-side passes through unchanged")+ (is (= "events" (:pk-table-name out))))))))++(deftest rewrite-fk-self-referential-test+ (testing "self-referential FK within a remapped table is translated on both sides"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "public" :table "employees"}+ {:schema "ws_alice" :table "employees"})+ (let [row {:fk-table-schema "ws_alice" :fk-table-name "employees" :fk-column-name "manager_id"+ :pk-table-schema "ws_alice" :pk-table-name "employees" :pk-column-name "id"}+ [out] (ws.table-remapping/rewrite-fk-result-canonical [row] (mt/id))]+ (is (= "public" (:fk-table-schema out)))+ (is (= "public" (:pk-table-schema out)))+ (is (= "employees" (:fk-table-name out)))+ (is (= "employees" (:pk-table-name out))))))))++;;; -------------------------- inject-workspace-canonical-tuples --------------------------++(deftest inject-canonical-tuples-no-rows-test+ (testing "without remap rows, tuples pass through unchanged"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (let [tuples #{{:schema "public" :name "orders"}}]+ (is (= tuples (ws.table-remapping/inject-workspace-canonical-tuples tuples (mt/id)))))))))++(deftest inject-canonical-tuples-adds-from-side-test+ (testing "for each remap row, the from-side tuple is added so the diff doesn't retire the canonical Table"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "public" :table "my_transform_output"}+ {:schema "ws_alice" :table "my_transform_output"})+ ;; Simulating describe-database AFTER filter-workspace-side-tables ran:+ ;; only canonical input schema is present, no workspace-side tuples.+ (let [tuples #{{:schema "public" :name "src"}}+ injected (ws.table-remapping/inject-workspace-canonical-tuples tuples (mt/id))]+ (is (contains? injected {:schema "public" :name "src"})+ "non-remapped canonical tuple still present")+ (is (contains? injected {:schema "public" :name "my_transform_output"})+ "synthetic canonical tuple injected for the remapped table"))))))++(deftest inject-canonical-tuples-deduplicates-test+ (testing "if the canonical tuple is already present (e.g. before the workspace transform was wired up), no duplication"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "public" :table "orders"}+ {:schema "ws_alice" :table "orders"})+ (let [tuples #{{:schema "public" :name "orders"}}+ injected (ws.table-remapping/inject-workspace-canonical-tuples tuples (mt/id))]+ (is (= 1 (count injected)) "set semantics dedupe by value"))))))++(deftest sync-does-not-retire-canonical-tables-with-active-remappings-test+ (testing "Canonical Table rows whose physical backing is in the isolation schema must not be retired+ on a sync where describe-database doesn't surface the canonical name."+ (clean-db-fixture!+ (mt/id)+ (fn []+ (mt/with-temp [:model/Table _ {:db_id (mt/id)+ :schema "public"+ :name "ws_canonical_keep"+ :active true}]+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:schema "public" :table "ws_canonical_keep"}+ {:schema "ws_alice" :table "ws_canonical_keep"})+ ;; describe-database returns ONLY the workspace-side tuple (which DEV-1898+ ;; will filter out) and an unrelated canonical table. The canonical+ ;; ws_canonical_keep does not physically exist on the warehouse.+ (let [fake-metadata {:tables #{{:schema "public" :name "unrelated_table"}+ {:schema "ws_alice" :name "ws_canonical_keep"}}}]+ (with-redefs [fetch-metadata/db-metadata (constantly fake-metadata)]+ (sync-tables/sync-tables-and-database! (t2/select-one :model/Database (mt/id)))))+ (let [{:keys [active]} (t2/select-one [:model/Table :active] :db_id (mt/id) :name "ws_canonical_keep")]+ (is (true? active)+ "canonical Table row with an active remap stays active across syncs")))))))++;;; -------------------------- engine-namespace-positions: unknown-engine handling --------------------------+;;;+;;; Third-party drivers participate via `metabase.driver.sql/table-qualification-style`+;;; (+ `db-slot-value` for the `:db-table` and `:db-schema-table` shapes).+;;; The default for drivers that don't override is `:schema-table`, so unknown+;;; engines silently get `{:db nil :schema (:schema table)}` -- the most common+;;; SQL identifier shape, and a safe degrade.++(driver/register! ::ws-test-unknown-driver, :abstract? true)++(deftest engine-namespace-positions-unknown-driver-defaults-to-schema-table-test+ (testing "Unknown engine gets the default :schema-table shape -- no throw, no surprise"+ (is (= {:db nil :schema "public"}+ (ws/engine-namespace-positions {:engine ::ws-test-unknown-driver :name "x"}+ {:schema "public" :name "orders"})))))++;;; -------------------------- GHY-3553: MySQL-shape sentinel-leak regressions --------------------------+;;;+;;; MySQL stores `:schema` as `""` (the no-level sentinel) at the storage layer but+;;; sync tuples / FK-result rows / describe-database tuples carry `:schema nil`. Pre-fix,+;;; the three workspace-aware filter/inject/rewrite fns built comparison keys from raw+;;; storage values, so the comparison never matched on schema-less drivers. Result:+;;;+;;; - `filter-workspace-side-tables` leaked iso-DB tuples through to sync.+;;; - `inject-workspace-canonical-tuples` failed to inject, so canonical rows were retired.+;;; - `rewrite-fk-result-canonical` silently no-op'd; iso-DB FK rows leaked.+;;;+;;; The fix (commit db39a79608a) routes both sides of every comparison through+;;; `denormalize-level`. These tests lock that fix in by simulating the MySQL shape:+;;; `add-mapping!` with `:schema nil`, then call the fn with sync tuples carrying+;;; `:schema nil`.++(deftest filter-workspace-side-tables-mysql-shape-test+ (testing "MySQL: storage `\"\"` schema sentinel matches sync `:schema nil` tuple"+ (clean-db-fixture!+ (mt/id)+ (fn []+ ;; MySQL-style mapping: no schema layer; only :db and :table.+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:db "test_data" :schema nil :table "orders"}+ {:db "ws_iso_alice" :schema nil :table "orders"})+ ;; Sync surfaces both the canonical and iso-DB tuples with :schema nil.+ (let [tuples #{{:schema nil :name "orders"}+ {:schema nil :name "users"}}+ filtered (ws.table-remapping/filter-workspace-side-tables tuples (mt/id))]+ ;; Pre-fix: filtered == tuples (the iso row leaks). Post-fix: orders drops out.+ ;; NOTE: `filter-workspace-side-tables` keys only on `(schema, name)` so it can't+ ;; distinguish canonical-DB orders from iso-DB orders on MySQL. The filter is run+ ;; per-DB at a higher layer; the regression here is that without denormalization+ ;; the row never drops at all (`"" != nil`). See namespace docstring.+ (is (= #{{:schema nil :name "users"}} filtered)+ "iso-DB orders tuple is dropped despite storage `\"\"` vs sync `nil`"))))))++(deftest inject-workspace-canonical-tuples-mysql-shape-test+ (testing "MySQL: synthetic canonical tuple carries `:schema nil` to match sync diff key shape"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:db "test_data" :schema nil :table "my_output"}+ {:db "ws_iso_alice" :schema nil :table "my_output"})+ ;; Sync diff input: describe-database surfaced only the unrelated table after+ ;; filter-workspace-side-tables ran. The canonical `:schema nil` tuple is absent.+ (let [tuples #{{:schema nil :name "unrelated"}}+ injected (ws.table-remapping/inject-workspace-canonical-tuples tuples (mt/id))]+ (is (contains? injected {:schema nil :name "my_output"})+ "synthetic canonical tuple uses `:schema nil`, not `\"\"`; sync diff keys won't retire it")+ (is (contains? injected {:schema nil :name "unrelated"})+ "non-remapped tuple passes through"))))))++(deftest rewrite-fk-result-canonical-mysql-shape-test+ (testing "MySQL: FK rows with `:fk-table-schema nil` translate via storage `\"\"` sentinel"+ (clean-db-fixture!+ (mt/id)+ (fn []+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:db "test_data" :schema nil :table "orders"}+ {:db "ws_iso_alice" :schema nil :table "orders"})+ (ws.table-remapping/add-mapping!+ (mt/id)+ {:db "test_data" :schema nil :table "users"}+ {:db "ws_iso_alice" :schema nil :table "users"})+ ;; JDBC FK enumeration in the iso DB returns rows whose schema is nil (MySQL+ ;; has no schema layer). Pre-fix, the storage `""` schema sentinel built a+ ;; lookup key `["" "orders"]` that never matched the row's `[nil "orders"]`.+ (let [row {:fk-table-schema nil :fk-table-name "orders" :fk-column-name "user_id"+ :pk-table-schema nil :pk-table-name "users" :pk-column-name "id"}+ [out] (ws.table-remapping/rewrite-fk-result-canonical [row] (mt/id))]+ ;; Output schemas are denormalized too: storage `""` -> output `nil`.+ (is (nil? (:fk-table-schema out))+ "fk-side schema rewrites to canonical (denormalized nil)")+ (is (= "orders" (:fk-table-name out))+ "fk-side name rewrites to canonical")+ (is (nil? (:pk-table-schema out))+ "pk-side schema rewrites to canonical (denormalized nil)")+ (is (= "users" (:pk-table-name out))+ "pk-side name rewrites to canonical"))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj b/enterprise/backend/test/metabase_enterprise/workspaces/test_util.cljnew file mode 100644index 00000000000..e6a5fcaad5d--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj@@ -0,0 +1,15 @@+(ns metabase-enterprise.workspaces.test-util+ "Shared test helpers for the workspaces module."+ (:require+ [metabase.driver :as driver]))++(defn driver-loadable?+ "True iff `engine`'s driver class is on the test classpath. Wraps+ `driver/the-initialized-driver` so tests that walk all workspace-capable+ drivers can `(when (driver-loadable? :sqlserver) ...)` to silently skip+ rows for drivers absent from the current `DRIVERS` env. Used by per-driver+ corpus / remapping tests where most drivers are off-classpath on the+ default H2-only run."+ [engine]+ (try (driver/the-initialized-driver engine) true+ (catch Throwable _ false)))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/transform_hooks_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/transform_hooks_test.cljnew file mode 100644index 00000000000..c7204a2f8ed--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/transform_hooks_test.clj@@ -0,0 +1,153 @@+(ns metabase-enterprise.workspaces.transform-hooks-test+ (:require+ [clojure.test :refer [deftest is testing use-fixtures]]+ [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.table-remapping :as ws.table-remapping]+ [metabase-enterprise.workspaces.transform-hooks :as ws.transform-hooks]+ [metabase.test :as mt]+ [metabase.transforms.execute :as transforms.execute]+ [metabase.transforms.interface :as transforms.i]+ [toucan2.core :as t2]))++(use-fixtures :each (fn [f] (mt/with-premium-features #{:workspaces} (f))))++(deftest resolve-transform-target-no-workspace-passthrough-test+ (testing "without a provisioned WorkspaceDatabase, target passes through unchanged"+ (with-redefs [ws/db-workspace-namespace (constantly nil)+ ws.table-remapping/add-transform-target-mapping!+ (fn [& _] (throw (ex-info "add-transform-target-mapping! must not be called when no workspace is active" {})))]+ (let [target {:schema "public" :name "orders" :type :table}]+ (is (= target (ws.transform-hooks/resolve-transform-target 42 target)))))))++(deftest resolve-transform-target-rewrites-schema-test+ (testing "with a provisioned WorkspaceDatabase, target's :schema and :name come from the recorded to-spec"+ (let [recorded (atom nil)]+ (with-redefs [ws/db-workspace-namespace (constantly {:schema "ws_alice"})+ ws.table-remapping/add-transform-target-mapping!+ (fn [db-id target]+ (reset! recorded {:db-id db-id :target target})+ ;; Mimic real return: collision-resistant to-spec.+ {:db nil :schema "ws_alice" :name "public__orders"})]+ (let [target {:schema "public" :name "orders" :type :table}+ result (ws.transform-hooks/resolve-transform-target 42 target)]+ (is (= "ws_alice" (:schema result))+ "schema comes from the recorded to-spec (workspace output schema)")+ (is (= "public__orders" (:name result))+ "name comes from the recorded to-spec (collision-resistant rename)")+ (is (= :table (:type result))+ "other target keys are preserved"))))))++(deftest resolve-transform-target-records-remapping-test+ (testing "with a workspace active, the canonical->workspace remapping is recorded"+ (let [recorded (atom nil)]+ (with-redefs [ws/db-workspace-namespace (constantly {:schema "ws_alice"})+ ws.table-remapping/add-transform-target-mapping!+ (fn [db-id target]+ (reset! recorded {:db-id db-id :target target})+ {:db nil :schema "ws_alice" :name "public__orders"})]+ (ws.transform-hooks/resolve-transform-target 42 {:schema "public" :name "orders" :type :table})+ (is (= {:db-id 42+ :target {:schema "public" :name "orders" :type :table}}+ @recorded)+ "add-transform-target-mapping! receives the db-id and the canonical target map verbatim")))))++(defn- with-instance-workspace-for-db!+ "Set the in-process workspace atom so that `db-workspace-namespace` returns+ `{:schema output-schema}` for the test database, run `body-fn`, clear it on+ the way out."+ [output-schema body-fn]+ (try+ (ws/set-instance-workspace! {:name "test-ws"+ :databases {(mt/id) {:input_schemas ["_"]+ :output {:schema output-schema}}}})+ (body-fn)+ (finally+ (ws/clear-instance-workspace!))))++(deftest resolve-transform-target-end-to-end-test+ (testing "end-to-end against the test app DB: a remapping row appears after the hook fires"+ (with-instance-workspace-for-db! "ws_test_schema"+ (fn []+ (try+ (ws.table-remapping/clear-mappings-for-db! (mt/id))+ (let [target {:schema "PUBLIC" :name "ORDERS" :type :table}+ result (ws.transform-hooks/resolve-transform-target (mt/id) target)]+ (is (= {:db nil :schema "ws_test_schema" :name "PUBLIC__ORDERS" :type :table}+ result)+ "the executor target gets the workspace schema and the collision-resistant name")+ (testing "TableRemapping row was inserted with the canonical (schema, name) on the from-side"+ (let [row (t2/select-one :model/TableRemapping+ :database_id (mt/id)+ :from_schema "PUBLIC"+ :from_table_name "ORDERS")]+ (is (some? row))+ (is (= "ws_test_schema" (:to_schema row)))+ (is (= "PUBLIC__ORDERS" (:to_table_name row))+ "to-side is rewritten via remapped-table-name; executor and row agree byte-for-byte"))))+ (finally+ (ws.table-remapping/clear-mappings-for-db! (mt/id))))))))++(defn- with-stubbed-execute-multimethod!+ "Replace `transforms.i/execute!` with a stub that captures the transform map it receives.+ Returns the captured value via the supplied atom. The hook still fires before dispatch+ (wired in `transforms.execute/execute!`), so the captured target reflects the post-rewrite+ state — the question this helper answers is 'what does the per-type method actually see?'."+ [captured-atom body-fn]+ #_{:clj-kondo/ignore [:discouraged-var]}+ (with-redefs [transforms.i/execute! (fn [transform _opts]+ (reset! captured-atom transform)+ {:status :succeeded})]+ (body-fn)))++(deftest python-transforms-inherit-target-rewrite-test+ (testing "Python transforms see the rewritten target via the wrapper-level gate"+ (with-instance-workspace-for-db! "ws_test_schema"+ (fn []+ (try+ (ws.table-remapping/clear-mappings-for-db! (mt/id))+ (let [captured (atom nil)+ python-transform {:id 999+ :source {:type :python}+ :target {:database (mt/id)+ :schema "PUBLIC"+ :name "FOO"+ :type :table}}]+ (with-stubbed-execute-multimethod!+ captured+ (fn []+ (transforms.execute/execute! python-transform)))+ (is (= "ws_test_schema" (get-in @captured [:target :schema]))+ "Python transform's :target.schema is rewritten before dispatch")+ (is (= "PUBLIC__FOO" (get-in @captured [:target :name]))+ ":name is rewritten to the collision-resistant warehouse identifier")+ (testing "TableRemapping row was recorded for the Python path too"+ (let [row (t2/select-one :model/TableRemapping+ :database_id (mt/id)+ :from_schema "PUBLIC"+ :from_table_name "FOO")]+ (is (some? row) "Python target rewrite records a TableRemapping row")+ (is (= "ws_test_schema" (:to_schema row)))+ (is (= "PUBLIC__FOO" (:to_table_name row))))))+ (finally+ (ws.table-remapping/clear-mappings-for-db! (mt/id))))))))++(deftest non-workspaced-db-target-passthrough-via-execute-test+ (testing "When no WorkspaceDatabase is provisioned, the wrapper passes target through unchanged"+ (let [captured (atom nil)+ python-transform {:id 998+ :source {:type :python}+ :target {:database (mt/id)+ :schema "PUBLIC"+ :name "BAR"+ :type :table}}]+ (with-stubbed-execute-multimethod!+ captured+ (fn []+ (transforms.execute/execute! python-transform)))+ (is (= "PUBLIC" (get-in @captured [:target :schema]))+ ":schema is unchanged when DB is not workspaced")+ (is (nil? (t2/select-one :model/TableRemapping+ :database_id (mt/id)+ :from_schema "PUBLIC"+ :from_table_name "BAR"))+ "no TableRemapping row recorded for a non-workspaced DB"))))diff --git a/enterprise/frontend/src/metabase-enterprise/api/index.ts b/enterprise/frontend/src/metabase-enterprise/api/index.tsindex d0402e7daec..25fe2795414 100644--- a/enterprise/frontend/src/metabase-enterprise/api/index.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/index.ts@@ -20,3 +20,5 @@ export * from "./table";export * from "./tags";export * from "./tenants";export * from "./upload-management";+export * from "./workspace";+export * from "./workspace-instance";diff --git a/enterprise/frontend/src/metabase-enterprise/api/tags.ts b/enterprise/frontend/src/metabase-enterprise/api/tags.tsindex fc67efbc78a..1537165377c 100644--- a/enterprise/frontend/src/metabase-enterprise/api/tags.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/tags.ts@@ -25,6 +25,7 @@ import {type SupportAccessGrant,type TableDependencyNode,type TransformDependencyNode,+ type Workspace,} from "metabase-types/api";export const ENTERPRISE_TAG_TYPES = [@@ -50,6 +51,7 @@ export const ENTERPRISE_TAG_TYPES = ["ai-controls-usage-group-limits","ai-controls-usage-tenant-limits","data-complexity-scores",+ "workspace",] as const;export type EnterpriseTagType = TagType | (typeof ENTERPRISE_TAG_TYPES)[number];@@ -252,3 +254,15 @@ export function provideSourceReplacementRunListTags(...runs.flatMap(provideSourceReplacementRunTags),];}++export function provideWorkspaceTags(+ workspace: Workspace,+): TagDescription<EnterpriseTagType>[] {+ return [idTag("workspace", workspace.id)];+}++export function provideWorkspaceListTags(+ workspaces: Workspace[],+): TagDescription<EnterpriseTagType>[] {+ return [listTag("workspace"), ...workspaces.flatMap(provideWorkspaceTags)];+}diff --git a/enterprise/frontend/src/metabase-enterprise/api/workspace-instance.ts b/enterprise/frontend/src/metabase-enterprise/api/workspace-instance.tsnew file mode 100644index 00000000000..a7236bf0540--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/api/workspace-instance.ts@@ -0,0 +1,28 @@+import { provideTableRemappingListTags } from "metabase/api/tags";+import type { TableRemapping, WorkspaceInstance } from "metabase-types/api";++import { EnterpriseApi } from "./api";+import { tag } from "./tags";++export const workspaceInstanceApi = EnterpriseApi.injectEndpoints({+ endpoints: (builder) => ({+ getCurrentWorkspace: builder.query<WorkspaceInstance | null, void>({+ query: () => ({+ method: "GET",+ url: "/api/ee/workspace-instance/current",+ }),+ providesTags: [tag("workspace")],+ }),+ listTableRemappings: builder.query<TableRemapping[], void>({+ query: () => ({+ method: "GET",+ url: "/api/ee/workspace-instance/table-remappings",+ }),+ providesTags: (remappings = []) =>+ provideTableRemappingListTags(remappings),+ }),+ }),+});++export const { useGetCurrentWorkspaceQuery, useListTableRemappingsQuery } =+ workspaceInstanceApi;diff --git a/enterprise/frontend/src/metabase-enterprise/api/workspace.ts b/enterprise/frontend/src/metabase-enterprise/api/workspace.tsnew file mode 100644index 00000000000..6c20639213b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/api/workspace.ts@@ -0,0 +1,110 @@+import type {+ CreateWorkspaceDatabaseRequest,+ CreateWorkspaceRequest,+ DeleteWorkspaceDatabaseRequest,+ UpdateWorkspaceDatabaseRequest,+ UpdateWorkspaceRequest,+ Workspace,+ WorkspaceId,+} from "metabase-types/api";++import { EnterpriseApi } from "./api";+import {+ idTag,+ invalidateTags,+ listTag,+ provideWorkspaceListTags,+ provideWorkspaceTags,+} from "./tags";++export const workspaceApi = EnterpriseApi.injectEndpoints({+ endpoints: (builder) => ({+ listWorkspaces: builder.query<Workspace[], void>({+ query: () => ({+ method: "GET",+ url: "/api/ee/workspace-manager",+ }),+ providesTags: (workspaces = []) => provideWorkspaceListTags(workspaces),+ }),+ getWorkspace: builder.query<Workspace, WorkspaceId>({+ query: (id) => ({+ method: "GET",+ url: `/api/ee/workspace-manager/${id}`,+ }),+ providesTags: (workspace) =>+ workspace ? provideWorkspaceTags(workspace) : [],+ }),+ createWorkspace: builder.mutation<Workspace, CreateWorkspaceRequest>({+ query: (body) => ({+ method: "POST",+ url: "/api/ee/workspace-manager",+ body,+ }),+ invalidatesTags: (_, error) =>+ invalidateTags(error, [listTag("workspace")]),+ }),+ updateWorkspace: builder.mutation<Workspace, UpdateWorkspaceRequest>({+ query: ({ id, ...body }) => ({+ method: "PUT",+ url: `/api/ee/workspace-manager/${id}`,+ body,+ }),+ invalidatesTags: (_, error, { id }) =>+ invalidateTags(error, [listTag("workspace"), idTag("workspace", id)]),+ }),+ deleteWorkspace: builder.mutation<void, WorkspaceId>({+ query: (id) => ({+ method: "DELETE",+ url: `/api/ee/workspace-manager/${id}`,+ }),+ invalidatesTags: (_, error, id) =>+ invalidateTags(error, [listTag("workspace"), idTag("workspace", id)]),+ }),+ createWorkspaceDatabase: builder.mutation<+ Workspace,+ CreateWorkspaceDatabaseRequest+ >({+ query: ({ id, ...body }) => ({+ method: "POST",+ url: `/api/ee/workspace-manager/${id}/database`,+ body,+ }),+ invalidatesTags: (_, error, { id }) =>+ invalidateTags(error, [listTag("workspace"), idTag("workspace", id)]),+ }),+ updateWorkspaceDatabase: builder.mutation<+ Workspace,+ UpdateWorkspaceDatabaseRequest+ >({+ query: ({ id, database_id, ...body }) => ({+ method: "PUT",+ url: `/api/ee/workspace-manager/${id}/database/${database_id}`,+ body,+ }),+ invalidatesTags: (_, error, { id }) =>+ invalidateTags(error, [listTag("workspace"), idTag("workspace", id)]),+ }),+ deleteWorkspaceDatabase: builder.mutation<+ Workspace,+ DeleteWorkspaceDatabaseRequest+ >({+ query: ({ id, database_id }) => ({+ method: "DELETE",+ url: `/api/ee/workspace-manager/${id}/database/${database_id}`,+ }),+ invalidatesTags: (_, error, { id }) =>+ invalidateTags(error, [listTag("workspace"), idTag("workspace", id)]),+ }),+ }),+});++export const {+ useListWorkspacesQuery,+ useGetWorkspaceQuery,+ useCreateWorkspaceMutation,+ useUpdateWorkspaceMutation,+ useDeleteWorkspaceMutation,+ useCreateWorkspaceDatabaseMutation,+ useUpdateWorkspaceDatabaseMutation,+ useDeleteWorkspaceDatabaseMutation,+} = workspaceApi;diff --git a/enterprise/frontend/src/metabase-enterprise/feature_level_permissions/permission-management/index.ts b/enterprise/frontend/src/metabase-enterprise/feature_level_permissions/permission-management/index.tsindex 2ef3bc571ff..954811cf4c8 100644--- a/enterprise/frontend/src/metabase-enterprise/feature_level_permissions/permission-management/index.ts+++ b/enterprise/frontend/src/metabase-enterprise/feature_level_permissions/permission-management/index.ts@@ -15,6 +15,7 @@ import { buildDataModelPermission } from "./data-model-permission";import { buildDetailsPermission } from "./details-permission";import { buildDownloadPermission } from "./download-permission";import { buildTransformsPermission } from "./transforms-permission";+import { buildWorkspacesPermission } from "./workspaces-permission";export const getFeatureLevelDataPermissions = ({entityId,@@ -26,6 +27,7 @@ export const getFeatureLevelDataPermissions = ({permissionSubject,permissionView,showTransformPermissions = false,+ showWorkspacesPermissions = false,}: {entityId: EntityId;groupId: number;@@ -36,6 +38,7 @@ export const getFeatureLevelDataPermissions = ({permissionSubject: PermissionSubject;permissionView?: "group" | "database";showTransformPermissions?: boolean;+ showWorkspacesPermissions?: boolean;}): PermissionSectionConfig[] => {const isAdmin = groupType === "admin";const isExternal = groupType === "external";@@ -85,10 +88,22 @@ export const getFeatureLevelDataPermissions = ({): null;+ const workspacesPermission = showWorkspacesPermissions+ ? buildWorkspacesPermission(+ entityId,+ groupId,+ isAdmin,+ permissions,+ defaultGroup,+ permissionSubject,+ )+ : null;+return [downloadPermission,dataModelPermission,detailsPermission,transformsPermission,+ workspacesPermission,].filter(isNotNull);};diff --git a/enterprise/frontend/src/metabase-enterprise/feature_level_permissions/permission-management/workspaces-permission.ts b/enterprise/frontend/src/metabase-enterprise/feature_level_permissions/permission-management/workspaces-permission.tsnew file mode 100644index 00000000000..3b28e0c7de4--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/feature_level_permissions/permission-management/workspaces-permission.ts@@ -0,0 +1,160 @@+import { getIn } from "icepick";+import { t } from "ttag";++import { Messages } from "metabase/admin/permissions/constants/messages";+import {+ getPermissionWarning,+ getPermissionWarningModal,+} from "metabase/admin/permissions/selectors/confirmations";+import {+ DataPermissionType,+ type EntityId,+ type PermissionOption,+ type PermissionSectionConfig,+ type PermissionSubject,+} from "metabase/admin/permissions/types";+import { getSchemasPermission } from "metabase/admin/permissions/utils/graph";+import {+ DataPermission,+ DataPermissionValue,+ type Group,+ type GroupsPermissions,+} from "metabase-types/api";++export const WORKSPACES_PERMISSION_OPTIONS: Record<string, PermissionOption> = {+ no: {+ get label() {+ return t`No`;+ },+ value: DataPermissionValue.NO,+ icon: "close",+ iconColor: "danger",+ },+ yes: {+ get label() {+ return t`Yes`;+ },+ value: DataPermissionValue.YES,+ icon: "check",+ iconColor: "success",+ },+};++const WORKSPACES_PERMISSIONS_DESC = [+ WORKSPACES_PERMISSION_OPTIONS.yes.value,+ WORKSPACES_PERMISSION_OPTIONS.no.value,+];++const getWorkspacesPermission = (+ permissions: GroupsPermissions,+ groupId: number,+ databaseId: number,+) =>+ getIn(permissions, [groupId, databaseId, DataPermission.WORKSPACES]) ??+ WORKSPACES_PERMISSION_OPTIONS.no.value;++const hasFullViewDataAccess = (viewDataValue: DataPermissionValue): boolean => {+ return viewDataValue === DataPermissionValue.UNRESTRICTED;+};++const hasFullCreateQueriesAccess = (+ createQueriesValue: DataPermissionValue,+): boolean => {+ return createQueriesValue === DataPermissionValue.QUERY_BUILDER_AND_NATIVE;+};++const getWorkspacesDisabledTooltip = (+ isAdmin: boolean,+ hasRequiredPermissions: boolean,+): string | null => {+ if (isAdmin) {+ return Messages.UNABLE_TO_CHANGE_ADMIN_PERMISSIONS;+ }++ if (!hasRequiredPermissions) {+ return t`Workspaces require "Can view" data access and "Query builder and native" for all tables in this database`;+ }++ return null;+};++export const buildWorkspacesPermission = (+ entityId: EntityId,+ groupId: number,+ isAdmin: boolean,+ permissions: GroupsPermissions,+ defaultGroup: Group,+ permissionSubject: PermissionSubject,+): PermissionSectionConfig | null => {+ if (permissionSubject !== "schemas") {+ return null;+ }++ const viewDataValue = getSchemasPermission(+ permissions,+ groupId,+ entityId,+ DataPermission.VIEW_DATA,+ );++ const createQueriesValue = getSchemasPermission(+ permissions,+ groupId,+ entityId,+ DataPermission.CREATE_QUERIES,+ );++ const hasRequiredPermissions =+ hasFullViewDataAccess(viewDataValue) &&+ hasFullCreateQueriesAccess(createQueriesValue);++ const isDisabled = isAdmin || !hasRequiredPermissions;++ const value = hasRequiredPermissions+ ? getWorkspacesPermission(permissions, groupId, entityId.databaseId)+ : WORKSPACES_PERMISSION_OPTIONS.no.value;++ const defaultGroupValue = getWorkspacesPermission(+ permissions,+ defaultGroup.id,+ entityId.databaseId,+ );++ const warning = getPermissionWarning(+ value,+ defaultGroupValue,+ permissionSubject,+ defaultGroup,+ groupId,+ WORKSPACES_PERMISSIONS_DESC,+ );++ const confirmations = (newValue: DataPermissionValue) => [+ getPermissionWarningModal(+ newValue,+ defaultGroupValue,+ permissionSubject,+ defaultGroup,+ groupId,+ WORKSPACES_PERMISSIONS_DESC,+ ),+ ];++ return {+ permission: DataPermission.WORKSPACES,+ type: DataPermissionType.WORKSPACES,+ value,+ isDisabled,+ isHighlighted: isAdmin,+ warning,+ confirmations,+ disabledTooltip: getWorkspacesDisabledTooltip(+ isAdmin,+ hasRequiredPermissions,+ ),+ options: [+ WORKSPACES_PERMISSION_OPTIONS.no,+ WORKSPACES_PERMISSION_OPTIONS.yes,+ ],+ };+};diff --git a/enterprise/frontend/src/metabase-enterprise/feature_level_permissions/utils.tsx b/enterprise/frontend/src/metabase-enterprise/feature_level_permissions/utils.tsxindex e9c7dcbe2c9..071b7e1b1ae 100644--- a/enterprise/frontend/src/metabase-enterprise/feature_level_permissions/utils.tsx+++ b/enterprise/frontend/src/metabase-enterprise/feature_level_permissions/utils.tsx@@ -38,11 +38,13 @@ export const getDataColumns = ({groupType,isExternal,showTransformPermissions = false,+ showWorkspacesPermissions = false,}: {subject: PermissionSubject;groupType?: SpecialGroupType;isExternal?: boolean;showTransformPermissions?: boolean;+ showWorkspacesPermissions?: boolean;}) => {const allSubjectsColumns: { name: string; hint?: ReactNode }[] = [{@@ -77,6 +79,22 @@ export const getDataColumns = ({)} to use transforms.`,});}++ if (showWorkspacesPermissions) {+ allSubjectsColumns.push({+ name: t`Workspaces`,+ hint:+ groupType === "analyst" || groupType === "admin"+ ? null+ : jt`Users must also be a member of the ${(+ <Link+ key="link"+ to="/admin/people"+ style={{ textDecoration: "underline" }}+ >{t`Data Analysts group`}</Link>+ )} to use workspaces.`,+ });+ }}return allSubjectsColumns;diff --git a/enterprise/frontend/src/metabase-enterprise/plugins.ts b/enterprise/frontend/src/metabase-enterprise/plugins.tsindex 55f77830446..c81a6b0ca3f 100644--- a/enterprise/frontend/src/metabase-enterprise/plugins.ts+++ b/enterprise/frontend/src/metabase-enterprise/plugins.ts@@ -52,6 +52,7 @@ import { initializePlugin as initializeTransformsPython } from "./transforms-pytimport { initializePlugin as initializeUploadManagement } from "./upload_management";import { initializePlugin as initializeUserProvisioning } from "./user_provisioning";import { initializePlugin as initializeWhitelabel } from "./whitelabel";+import { initializePlugin as initializeWorkspaces } from "./workspaces";import { initializePlugin as initializeWritableConnection } from "./writable_connection";/**@@ -102,5 +103,6 @@ export function initializePlugins() {initializeSupport();initializeTenants?.();initializeWritableConnection();+ initializeWorkspaces();initializeAiControls();}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/components/AdminConnectionInfoSection/AdminConnectionInfoSection.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/components/AdminConnectionInfoSection/AdminConnectionInfoSection.tsxnew file mode 100644index 00000000000..74926a8815b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/components/AdminConnectionInfoSection/AdminConnectionInfoSection.tsx@@ -0,0 +1,97 @@+import { Link } from "react-router";+import { t } from "ttag";++import { DatabaseConnectionHealthInfo } from "metabase/admin/databases/components/DatabaseConnectionHealthInfo";+import {+ DatabaseInfoSection,+ DatabaseInfoSectionDivider,+} from "metabase/admin/databases/components/DatabaseInfoSection";+import { useUpdateDatabaseMutation } from "metabase/api";+import { useConfirmation } from "metabase/common/hooks/use-confirmation";+import {+ hasDbRoutingEnabled,+ hasFeature,+ isDbModifiable,+} from "metabase/common/utils/database";+import type { AdminConnectionInfoSectionProps } from "metabase/plugins/oss/workspaces";+import { Alert, Button, FixedSizeIcon, Group } from "metabase/ui";+import * as Urls from "metabase/urls";++export function AdminConnectionInfoSection({+ database,+}: AdminConnectionInfoSectionProps) {+ const hasAdminConnection = database.admin_details != null;+ const [updateDatabase] = useUpdateDatabaseMutation();+ const { modalContent, show: showConfirmation } = useConfirmation();+ const isDbRoutingEnabled = hasDbRoutingEnabled(database);++ if (!isDbModifiable(database) || !hasFeature(database, "workspace")) {+ return null;+ }++ const handleRemove = () => {+ showConfirmation({+ title: t`Remove admin connection?`,+ message: t`This will remove the admin connection for this database. Any features that depend on it will stop working.`,+ confirmButtonText: t`Remove`,+ onConfirm: async () => {+ await updateDatabase({+ id: database.id,+ admin_details: null,+ }).unwrap();+ },+ });+ };++ return (+ <DatabaseInfoSection+ name={t`Admin connection`}+ description={t`Admin connection is used to create a temporary user and an isolation schema for a workspace.`}+ data-testid="admin-connection-info-section"+ >+ <Group justify="space-between" gap="lg">+ {hasAdminConnection && (+ <DatabaseConnectionHealthInfo+ databaseId={database.id}+ connectionType="admin"+ />+ )}+ <Button+ component={isDbRoutingEnabled ? undefined : Link}+ to={Urls.editDatabaseAdminConnection(database.id)}+ disabled={isDbRoutingEnabled}+ >+ {hasAdminConnection+ ? t`Edit connection details`+ : t`Add admin connection`}+ </Button>+ </Group>++ {isDbRoutingEnabled && (+ <>+ <DatabaseInfoSectionDivider />+ <Alert+ variant="light"+ color="info"+ icon={<FixedSizeIcon name="info" />}+ mb="md"+ >+ {t`Admin connection can't be enabled when Database Routing is enabled.`}+ </Alert>+ </>+ )}++ {hasAdminConnection && (+ <>+ <DatabaseInfoSectionDivider condensed />+ <Group>+ <Button variant="filled" color="error" onClick={handleRemove}>+ {t`Remove admin connection`}+ </Button>+ </Group>+ </>+ )}+ {modalContent}+ </DatabaseInfoSection>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/components/AdminConnectionInfoSection/AdminConnectionInfoSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/components/AdminConnectionInfoSection/AdminConnectionInfoSection.unit.spec.tsxnew file mode 100644index 00000000000..fe7410233bd--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/components/AdminConnectionInfoSection/AdminConnectionInfoSection.unit.spec.tsx@@ -0,0 +1,83 @@+import { setupDatabaseEndpoints } from "__support__/server-mocks/database";+import { renderWithProviders, screen } from "__support__/ui";+import type { Database } from "metabase-types/api";+import { createMockDatabase } from "metabase-types/api/mocks";++import { AdminConnectionInfoSection } from "./AdminConnectionInfoSection";++interface SetupOpts {+ database?: Database;+}++function setup({+ database = createMockDatabase({ features: ["workspace"] }),+}: SetupOpts = {}) {+ setupDatabaseEndpoints(database);+ renderWithProviders(<AdminConnectionInfoSection database={database} />);+}++describe("AdminConnectionInfoSection", () => {+ it("should not render for a non-modifiable database (attached DWH)", () => {+ setup({+ database: createMockDatabase({+ is_attached_dwh: true,+ features: ["workspace"],+ }),+ });++ expect(+ screen.queryByTestId("admin-connection-info-section"),+ ).not.toBeInTheDocument();+ });++ it("should not render when the database does not support the workspace feature", () => {+ setup({ database: createMockDatabase({ features: [] }) });++ expect(+ screen.queryByTestId("admin-connection-info-section"),+ ).not.toBeInTheDocument();+ });++ it("should render for a modifiable database that supports the workspace feature", () => {+ setup({+ database: createMockDatabase({+ is_attached_dwh: false,+ features: ["workspace"],+ }),+ });++ expect(+ screen.getByTestId("admin-connection-info-section"),+ ).toBeInTheDocument();+ });++ it("should show a warning when database routing is enabled", () => {+ setup({+ database: createMockDatabase({+ router_user_attribute: "some_attribute",+ features: ["workspace"],+ }),+ });++ expect(+ screen.getByText(+ "Admin connection can't be enabled when Database Routing is enabled.",+ ),+ ).toBeInTheDocument();+ });++ it("should not show a warning when database routing is not enabled", () => {+ setup({+ database: createMockDatabase({+ router_user_attribute: null,+ features: ["workspace"],+ }),+ });++ expect(+ screen.queryByText(+ "Admin connection can't be enabled when Database Routing is enabled.",+ ),+ ).not.toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/components/AdminConnectionInfoSection/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/components/AdminConnectionInfoSection/index.tsnew file mode 100644index 00000000000..37530b80625--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/components/AdminConnectionInfoSection/index.ts@@ -0,0 +1 @@+export * from "./AdminConnectionInfoSection";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/components/DatabaseInfo/DatabaseInfo.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/components/DatabaseInfo/DatabaseInfo.tsxnew file mode 100644index 00000000000..2ee1628ef50--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/components/DatabaseInfo/DatabaseInfo.tsx@@ -0,0 +1,23 @@+import { Group, Pill, Text } from "metabase/ui";+import type { Database, WorkspaceDatabase } from "metabase-types/api";++export type DatabaseInfoProps = {+ workspaceDatabase: WorkspaceDatabase;+ database: Database | undefined;+};++export function DatabaseInfo({+ workspaceDatabase,+ database,+}: DatabaseInfoProps) {+ return (+ <Group gap="sm">+ <Text fw="bold">{database?.name}</Text>+ <Group gap="sm">+ {workspaceDatabase.input_schemas.map((schema) => (+ <Pill key={schema}>{schema}</Pill>+ ))}+ </Group>+ </Group>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/components/DatabaseInfo/DatabaseInfo.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/components/DatabaseInfo/DatabaseInfo.unit.spec.tsxnew file mode 100644index 00000000000..12ce8d8c28b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/components/DatabaseInfo/DatabaseInfo.unit.spec.tsx@@ -0,0 +1,27 @@+import { renderWithProviders, screen } from "__support__/ui";+import {+ createMockDatabase,+ createMockWorkspaceDatabase,+} from "metabase-types/api/mocks";++import { DatabaseInfo } from "./DatabaseInfo";++const POSTGRES = createMockDatabase({ id: 10, name: "Postgres" });++describe("DatabaseInfo", () => {+ it("renders the database name and schema pills when the database is found", () => {+ renderWithProviders(+ <DatabaseInfo+ workspaceDatabase={createMockWorkspaceDatabase({+ database_id: POSTGRES.id,+ input_schemas: ["public", "analytics"],+ })}+ database={POSTGRES}+ />,+ );++ expect(screen.getByText("Postgres")).toBeInTheDocument();+ expect(screen.getByText("public")).toBeInTheDocument();+ expect(screen.getByText("analytics")).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/components/DatabaseInfo/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/components/DatabaseInfo/index.tsnew file mode 100644index 00000000000..21e437881fe--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/components/DatabaseInfo/index.ts@@ -0,0 +1 @@+export * from "./DatabaseInfo";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/components/HelpMenu/HelpMenu.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/components/HelpMenu/HelpMenu.tsxnew file mode 100644index 00000000000..46fa7128c50--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/components/HelpMenu/HelpMenu.tsx@@ -0,0 +1,50 @@+import { Link } from "react-router";+import { t } from "ttag";++import { useDocsUrl } from "metabase/common/hooks";+import { Button, FixedSizeIcon, Menu } from "metabase/ui";++export function HelpMenu() {+ const { url: fileBasedDevDocsUrl, showMetabaseLinks: showFileBasedDevLink } =+ useDocsUrl("ai/file-based-development");+ const { url: remoteSyncDocsUrl, showMetabaseLinks: showRemoteSyncLink } =+ useDocsUrl("installation-and-operation/remote-sync");++ if (!showFileBasedDevLink && !showRemoteSyncLink) {+ return null;+ }++ return (+ <Menu>+ <Menu.Target>+ <Button+ rightSection={<FixedSizeIcon name="chevrondown" aria-hidden />}+ >{t`Help`}</Button>+ </Menu.Target>+ <Menu.Dropdown>+ {showFileBasedDevLink && (+ <Menu.Item+ component={Link}+ to={fileBasedDevDocsUrl}+ target="_blank"+ rel="noreferrer"+ leftSection={<FixedSizeIcon name="reference" aria-hidden />}+ >+ {t`File-based development`}+ </Menu.Item>+ )}+ {showRemoteSyncLink && (+ <Menu.Item+ component={Link}+ to={remoteSyncDocsUrl}+ target="_blank"+ rel="noreferrer"+ leftSection={<FixedSizeIcon name="reference" aria-hidden />}+ >+ {t`Using remote sync`}+ </Menu.Item>+ )}+ </Menu.Dropdown>+ </Menu>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/components/HelpMenu/HelpMenu.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/components/HelpMenu/HelpMenu.unit.spec.tsxnew file mode 100644index 00000000000..b0236ecdcf9--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/components/HelpMenu/HelpMenu.unit.spec.tsx@@ -0,0 +1,34 @@+import userEvent from "@testing-library/user-event";+import { Route } from "react-router";++import { renderWithProviders, screen } from "__support__/ui";+import { createMockSettingsState } from "metabase/redux/store/mocks";+import { createMockSettings } from "metabase-types/api/mocks";++import { HelpMenu } from "./HelpMenu";++function setup() {+ renderWithProviders(<Route path="*" component={HelpMenu} />, {+ withRouter: true,+ storeInitialState: {+ settings: createMockSettingsState(+ createMockSettings({ "show-metabase-links": true }),+ ),+ },+ });+}++describe("HelpMenu", () => {+ it("renders two docs links in the menu", async () => {+ setup();++ await userEvent.click(screen.getByRole("button", { name: /Help/ }));++ expect(+ await screen.findByRole("menuitem", { name: /File-based development/ }),+ ).toBeInTheDocument();+ expect(+ screen.getByRole("menuitem", { name: /Using remote sync/ }),+ ).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/components/HelpMenu/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/components/HelpMenu/index.tsnew file mode 100644index 00000000000..1223cd71e70--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/components/HelpMenu/index.ts@@ -0,0 +1 @@+export * from "./HelpMenu";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/hooks/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/hooks/index.tsnew file mode 100644index 00000000000..997c4528a9e--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/hooks/index.ts@@ -0,0 +1,2 @@+export * from "./use-delete-database";+export * from "./use-delete-workspace";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/hooks/use-delete-database.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/hooks/use-delete-database.tsxnew file mode 100644index 00000000000..e24afa6e7be--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/hooks/use-delete-database.tsx@@ -0,0 +1,43 @@+import { t } from "ttag";++import { useConfirmation } from "metabase/common/hooks/use-confirmation";+import { useDeleteWorkspaceDatabaseMutation } from "metabase-enterprise/api";+import type {+ Database,+ Workspace,+ WorkspaceDatabase,+} from "metabase-types/api";++export type DeleteDatabaseProps = {+ database: Database | undefined;+ onSuccess?: () => void;+};++export function useDeleteDatabase({+ database,+ onSuccess,+}: DeleteDatabaseProps) {+ const { modalContent, show } = useConfirmation();+ const [deleteWorkspaceDatabase] = useDeleteWorkspaceDatabaseMutation();++ const handleDelete = (+ workspace: Workspace,+ workspaceDatabase: WorkspaceDatabase,+ ) => {+ show({+ title: t`Remove ${database?.name} from this workspace?`,+ message: t`This will delete the temporary user and schema from this database.`,+ confirmButtonText: t`Remove`,+ confirmButtonProps: { color: "danger" },+ onConfirm: async () => {+ await deleteWorkspaceDatabase({+ id: workspace.id,+ database_id: workspaceDatabase.database_id,+ }).unwrap();+ onSuccess?.();+ },+ });+ };++ return { handleDelete, modalContent };+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/hooks/use-delete-workspace.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/hooks/use-delete-workspace.tsxnew file mode 100644index 00000000000..56a8ecc0659--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/hooks/use-delete-workspace.tsx@@ -0,0 +1,29 @@+import { t } from "ttag";++import { useConfirmation } from "metabase/common/hooks/use-confirmation";+import { useDeleteWorkspaceMutation } from "metabase-enterprise/api";+import type { Workspace } from "metabase-types/api";++export type DeleteWorkspaceProps = {+ onSuccess?: () => void;+};++export function useDeleteWorkspace({ onSuccess }: DeleteWorkspaceProps = {}) {+ const { modalContent, show } = useConfirmation();+ const [deleteWorkspace] = useDeleteWorkspaceMutation();++ const handleDelete = (workspace: Workspace) => {+ show({+ title: t`Delete this workspace?`,+ message: 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.`,+ confirmButtonText: t`Delete workspace`,+ confirmButtonProps: { color: "danger" },+ onConfirm: async () => {+ await deleteWorkspace(workspace.id).unwrap();+ onSuccess?.();+ },+ });+ };++ return { handleDelete, modalContent };+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/index.tsnew file mode 100644index 00000000000..830f2323b0b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/index.ts@@ -0,0 +1,17 @@+import { PLUGIN_WORKSPACES } from "metabase/plugins";+import { hasPremiumFeature } from "metabase-enterprise/settings";++import { AdminConnectionInfoSection } from "./components/AdminConnectionInfoSection";+import { getAdminConnectionInfoRoutes, getDataStudioRoutes } from "./routes";+import { canManageWorkspaces, hasActiveWorkspace } from "./selectors";++export function initializePlugin() {+ if (hasPremiumFeature("workspaces")) {+ PLUGIN_WORKSPACES.canManageWorkspaces = canManageWorkspaces;+ PLUGIN_WORKSPACES.hasActiveWorkspace = hasActiveWorkspace;+ PLUGIN_WORKSPACES.getDataStudioRoutes = getDataStudioRoutes;+ PLUGIN_WORKSPACES.getAdminConnectionInfoRoutes =+ getAdminConnectionInfoRoutes;+ PLUGIN_WORKSPACES.AdminConnectionInfoSection = AdminConnectionInfoSection;+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/AdminConnectionInfoPage.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/AdminConnectionInfoPage.tsxnew file mode 100644index 00000000000..26025757cc8--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/AdminConnectionInfoPage.tsx@@ -0,0 +1,138 @@+import { useMemo, useState } from "react";+import type { Route } from "react-router";+import { push } from "react-router-redux";+import { t } from "ttag";++import { SettingsSection } from "metabase/admin/components/SettingsSection";+import {+ skipToken,+ useGetDatabaseQuery,+ useUpdateDatabaseMutation,+} from "metabase/api";+import { LeaveRouteConfirmModal } from "metabase/common/components/LeaveConfirmModal";+import { DelayedLoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper/DelayedLoadingAndErrorWrapper";+import { DatabaseForm } from "metabase/databases/components/DatabaseForm";+import type { DatabaseFormConfig } from "metabase/databases/types";+import { useDispatch } from "metabase/redux";+import { Box, Flex, ScrollArea, Title } from "metabase/ui";+import * as Urls from "metabase/urls";+import type { Database, DatabaseData } from "metabase-types/api";++const FORM_CONFIG: DatabaseFormConfig = {+ engine: {+ fieldState: "disabled",+ },+ name: {+ fieldState: "hidden",+ },+ isAdvanced: true,+};++type AdminConnectionInfoPageParams = {+ databaseId: string;+};++type AdminConnectionInfoPageProps = {+ params: AdminConnectionInfoPageParams;+ route: Route;+};++export function AdminConnectionInfoPage({+ params,+ route,+}: AdminConnectionInfoPageProps) {+ const databaseId = Urls.extractEntityId(params.databaseId);+ const {+ data: database,+ isLoading,+ error,+ } = useGetDatabaseQuery(databaseId != null ? { id: databaseId } : skipToken);++ if (isLoading || error != null || database == null) {+ return <DelayedLoadingAndErrorWrapper loading={isLoading} error={error} />;+ }++ return <AdminConnectionInfoPageBody database={database} route={route} />;+}++type AdminConnectionInfoPageBodyProps = {+ database: Database;+ route: Route;+};++function AdminConnectionInfoPageBody({+ database,+ route,+}: AdminConnectionInfoPageBodyProps) {+ const title = getTitle(database);+ const initialValues = useMemo(() => getInitialValues(database), [database]);+ const [isDirty, setIsDirty] = useState(false);+ const [updateDatabase, { isLoading: isSaving }] = useUpdateDatabaseMutation();+ const dispatch = useDispatch();++ const handleSubmit = async (newValues: DatabaseData) => {+ await updateDatabase({+ id: database.id,+ admin_details: getSubmitDetails(newValues),+ }).unwrap();+ dispatch(push(Urls.viewDatabase(database.id)));+ };++ const handleCancel = () => {+ dispatch(push(Urls.viewDatabase(database.id)));+ };++ return (+ <Flex+ direction="row"+ h="100%"+ bg="background-secondary"+ data-testid="admin-connection-info-page"+ >+ <Box h="100%" w="100%" component={ScrollArea}>+ <Box w="100%" maw="54rem" mx="auto" p={{ base: "md", sm: "xl" }}>+ <Flex mb="lg" align="center">+ <Title order={1} fz="h2">+ {title}+ </Title>+ </Flex>+ <SettingsSection>+ <DatabaseForm+ initialValues={initialValues}+ config={FORM_CONFIG}+ location="full-page"+ onSubmit={handleSubmit}+ onCancel={handleCancel}+ onDirtyStateChange={setIsDirty}+ />+ </SettingsSection>+ </Box>+ </Box>+ <LeaveRouteConfirmModal isEnabled={isDirty && !isSaving} route={route} />+ </Flex>+ );+}++function getTitle(database: Database): string {+ return database.admin_details == null+ ? t`Add admin connection`+ : t`Edit admin connection details`;+}++function getInitialValues(database: Database): DatabaseData {+ const { password: _password, ...details } = database.details ?? {};++ return {+ ...database,+ id: database.admin_details ? database.id : undefined,+ details: {+ ...details,+ ...database.admin_details,+ "admin-connection": true,+ },+ };+}++function getSubmitDetails(values: DatabaseData) {+ return values.details ?? {};+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/AdminConnectionInfoPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/AdminConnectionInfoPage.unit.spec.tsxnew file mode 100644index 00000000000..f1dfabb3f1b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/AdminConnectionInfoPage.unit.spec.tsx@@ -0,0 +1,62 @@+import { Route } from "react-router";++import { setupDatabaseEndpoints } from "__support__/server-mocks/database";+import { renderWithProviders, screen } from "__support__/ui";+import { createMockSettingsState } from "metabase/redux/store/mocks";+import type { Database } from "metabase-types/api";+import {+ createMockDatabase,+ createMockSettings,+} from "metabase-types/api/mocks";++import { AdminConnectionInfoPage } from "./AdminConnectionInfoPage";++function setup({ database }: { database: Database }) {+ setupDatabaseEndpoints(database);++ renderWithProviders(+ <Route+ path="*"+ component={() => (+ <AdminConnectionInfoPage+ params={{ databaseId: String(database.id) }}+ route={{ path: "/" } as unknown as Route}+ />+ )}+ />,+ {+ withRouter: true,+ storeInitialState: {+ settings: createMockSettingsState(createMockSettings()),+ },+ },+ );+}++describe("AdminConnectionInfoPage", () => {+ it("renders the 'Add' title when the database has no admin connection yet", async () => {+ setup({+ database: createMockDatabase({ id: 1, name: "PG", admin_details: null }),+ });++ expect(+ await screen.findByRole("heading", { name: "Add admin connection" }),+ ).toBeInTheDocument();+ });++ it("renders the 'Edit' title when the database already has admin connection details", async () => {+ setup({+ database: createMockDatabase({+ id: 1,+ name: "PG",+ admin_details: { user: "admin" },+ }),+ });++ expect(+ await screen.findByRole("heading", {+ name: "Edit admin connection details",+ }),+ ).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/index.tsnew file mode 100644index 00000000000..a416af52aad--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/index.ts@@ -0,0 +1 @@+export * from "./AdminConnectionInfoPage";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingSection.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingSection.tsxnew file mode 100644index 00000000000..c4fe41b9b8c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingSection.tsx@@ -0,0 +1,30 @@+import { TitleSection } from "metabase/data-studio/common/components/TitleSection";+import { Box, FixedSizeIcon, Group } from "metabase/ui";+import type { Database, TableRemapping } from "metabase-types/api";++import { TableRemappingTable } from "./TableRemappingTable";++type TableRemappingSectionProps = {+ database: Database;+ remappings: TableRemapping[];+};++export function TableRemappingSection({+ database,+ remappings,+}: TableRemappingSectionProps) {+ return (+ <Box role="region" aria-label={database.name}>+ <TitleSection+ label={+ <Group gap="sm" wrap="nowrap">+ <FixedSizeIcon name="database" aria-hidden />+ {database.name}+ </Group>+ }+ >+ <TableRemappingTable remappings={remappings} />+ </TitleSection>+ </Box>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingSection.unit.spec.tsxnew file mode 100644index 00000000000..7d48048a7ed--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingSection.unit.spec.tsx@@ -0,0 +1,32 @@+import { renderWithProviders, screen } from "__support__/ui";+import {+ createMockDatabase,+ createMockTableRemapping,+} from "metabase-types/api/mocks";++import { TableRemappingSection } from "./TableRemappingSection";++const POSTGRES = createMockDatabase({ id: 10, name: "Postgres" });++describe("TableRemappingSection", () => {+ it("renders the database name and a remapping row", () => {+ renderWithProviders(+ <TableRemappingSection+ database={POSTGRES}+ remappings={[+ createMockTableRemapping({+ database_id: POSTGRES.id,+ from_schema: "public",+ from_table_name: "orders",+ to_schema: "ws_dev",+ to_table_name: "orders",+ }),+ ]}+ />,+ );++ expect(screen.getByText("Postgres")).toBeInTheDocument();+ expect(screen.getByText("public/orders")).toBeInTheDocument();+ expect(screen.getByText("ws_dev/orders")).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/TableRemappingTable.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/TableRemappingTable.tsxnew file mode 100644index 00000000000..d232496b726--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/TableRemappingTable.tsx@@ -0,0 +1,32 @@+import { useMemo } from "react";+import { t } from "ttag";++import { Text, TreeTable, useTreeTableInstance } from "metabase/ui";+import type { TableRemapping } from "metabase-types/api";++import { getColumns } from "./utils";++type TableRemappingTableProps = {+ remappings: TableRemapping[];+};++export function TableRemappingTable({ remappings }: TableRemappingTableProps) {+ const columns = useMemo(() => getColumns(), []);++ const treeTableInstance = useTreeTableInstance<TableRemapping>({+ data: remappings,+ columns,+ getNodeId: (remapping) => String(remapping.id),+ });++ return (+ <TreeTable+ instance={treeTableInstance}+ emptyState={+ <Text c="text-secondary">+ {t`Tables will be remapped here the first time a transform runs in this workspace for this database.`}+ </Text>+ }+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/TableRemappingTable.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/TableRemappingTable.unit.spec.tsxnew file mode 100644index 00000000000..1c062242542--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/TableRemappingTable.unit.spec.tsx@@ -0,0 +1,25 @@+import { renderWithProviders, screen } from "__support__/ui";+import { createMockTableRemapping } from "metabase-types/api/mocks";++import { TableRemappingTable } from "./TableRemappingTable";++describe("TableRemappingTable", () => {+ it("renders one row per remapping with joined source and target names", () => {+ renderWithProviders(+ <TableRemappingTable+ remappings={[+ createMockTableRemapping({+ id: 1,+ from_schema: "public",+ from_table_name: "orders",+ to_schema: "ws_dev",+ to_table_name: "orders",+ }),+ ]}+ />,+ );++ expect(screen.getByText("public/orders")).toBeInTheDocument();+ expect(screen.getByText("ws_dev/orders")).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/index.tsnew file mode 100644index 00000000000..a2899531de9--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/index.ts@@ -0,0 +1 @@+export * from "./TableRemappingTable";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/utils.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/utils.tsxnew file mode 100644index 00000000000..d2cce1b64f0--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/utils.tsx@@ -0,0 +1,59 @@+import { t } from "ttag";++import { DateTime } from "metabase/common/components/DateTime";+import { Ellipsified, type TreeTableColumnDef } from "metabase/ui";+import type { TableRemapping } from "metabase-types/api";++export function getColumns(): TreeTableColumnDef<TableRemapping>[] {+ return [getFromTableColumn(), getToTableColumn(), getCreatedAtColumn()];+}++function getFromTableColumn(): TreeTableColumnDef<TableRemapping> {+ return {+ id: "from-table",+ header: t`Table`,+ width: "auto",+ maxAutoWidth: 520,+ accessorFn: (remapping) =>+ getQualifiedName(+ remapping.from_db,+ remapping.from_schema,+ remapping.from_table_name,+ ),+ cell: ({ getValue }) => <Ellipsified>{String(getValue())}</Ellipsified>,+ };+}++function getToTableColumn(): TreeTableColumnDef<TableRemapping> {+ return {+ id: "to-table",+ header: t`Mapped table`,+ width: "auto",+ maxAutoWidth: 520,+ accessorFn: (remapping) =>+ getQualifiedName(+ remapping.to_db,+ remapping.to_schema,+ remapping.to_table_name,+ ),+ cell: ({ getValue }) => <Ellipsified>{String(getValue())}</Ellipsified>,+ };+}++function getCreatedAtColumn(): TreeTableColumnDef<TableRemapping> {+ return {+ id: "created-at",+ header: t`Mapping created at`,+ width: "auto",+ accessorFn: (remapping) => remapping.created_at,+ cell: ({ getValue }) => (+ <DateTime value={String(getValue())} unit="minute" />+ ),+ };+}++export function getQualifiedName(...parts: (string | null)[]): string {+ return parts+ .filter((part): part is string => part != null && part !== "")+ .join("/");+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/utils.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/utils.unit.spec.tsxnew file mode 100644index 00000000000..949d662cba7--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/TableRemappingTable/utils.unit.spec.tsx@@ -0,0 +1,16 @@+import { getQualifiedName } from "./utils";++describe("getQualifiedName", () => {+ it.each<{ parts: (string | null)[]; expected: string }>([+ { parts: ["public", "orders"], expected: "public/orders" },+ { parts: [null, "public", "orders"], expected: "public/orders" },+ { parts: ["", "public", "orders"], expected: "public/orders" },+ { parts: ["acme", "public", "orders"], expected: "acme/public/orders" },+ { parts: [null, null, "orders"], expected: "orders" },+ { parts: ["", "", "orders"], expected: "orders" },+ { parts: ["acme", "", "orders"], expected: "acme/orders" },+ { parts: ["acme", null, "orders"], expected: "acme/orders" },+ ])("joins $parts → '$expected'", ({ parts, expected }) => {+ expect(getQualifiedName(...parts)).toBe(expected);+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/index.tsnew file mode 100644index 00000000000..046576d6818--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/TableRemappingSection/index.ts@@ -0,0 +1 @@+export * from "./TableRemappingSection";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.tsxnew file mode 100644index 00000000000..f3c6dfc64e5--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.tsx@@ -0,0 +1,98 @@+import { t } from "ttag";++import { useListDatabasesQuery } from "metabase/api";+import { DelayedLoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper/DelayedLoadingAndErrorWrapper";+import { DataStudioBreadcrumbs } from "metabase/data-studio/common/components/DataStudioBreadcrumbs";+import { PageContainer } from "metabase/data-studio/common/components/PageContainer";+import { PaneHeader } from "metabase/data-studio/common/components/PaneHeader";+import { Stack, Title } from "metabase/ui";+import {+ useGetCurrentWorkspaceQuery,+ useListTableRemappingsQuery,+} from "metabase-enterprise/api";+import type {+ Database,+ TableRemapping,+ WorkspaceInstance,+} from "metabase-types/api";++import { HelpMenu } from "../../components/HelpMenu";++import { TableRemappingSection } from "./TableRemappingSection";+import { getDatabasesInfo } from "./utils";++export function WorkspaceInstancePage() {+ const {+ data: workspace,+ isLoading: isLoadingWorkspace,+ error: workspaceError,+ } = useGetCurrentWorkspaceQuery();+ const {+ data: remappings,+ isLoading: isLoadingRemappings,+ error: remappingsError,+ } = useListTableRemappingsQuery();+ const {+ data: databasesResponse,+ isLoading: isLoadingDatabases,+ error: databasesError,+ } = useListDatabasesQuery();++ const isLoading =+ isLoadingWorkspace || isLoadingRemappings || isLoadingDatabases;+ const error = workspaceError ?? remappingsError ?? databasesError;++ if (+ isLoading ||+ error != null ||+ workspace == null ||+ remappings == null ||+ databasesResponse == null+ ) {+ return <DelayedLoadingAndErrorWrapper loading={isLoading} error={error} />;+ }++ return (+ <WorkspaceInstancePageBody+ workspace={workspace}+ remappings={remappings}+ databases={databasesResponse.data}+ />+ );+}++type WorkspaceInstancePageBodyProps = {+ workspace: WorkspaceInstance;+ remappings: TableRemapping[];+ databases: Database[];+};++function WorkspaceInstancePageBody({+ workspace,+ remappings,+ databases,+}: WorkspaceInstancePageBodyProps) {+ const databasesInfo = getDatabasesInfo(workspace, databases, remappings);++ return (+ <PageContainer data-testid="workspace-instance-page">+ <PaneHeader+ title={<Title order={3}>{workspace.name}</Title>}+ breadcrumbs={+ <DataStudioBreadcrumbs>{t`Workspace`}</DataStudioBreadcrumbs>+ }+ actions={<HelpMenu />}+ py={0}+ />+ <Stack gap="3.5rem">+ {databasesInfo.map(({ database, remappings }) => (+ <TableRemappingSection+ key={database.id}+ database={database}+ remappings={remappings}+ />+ ))}+ </Stack>+ </PageContainer>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.unit.spec.tsxnew file mode 100644index 00000000000..5ad61f0b235--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/WorkspaceInstancePage.unit.spec.tsx@@ -0,0 +1,69 @@+import { Route } from "react-router";++import {+ setupDatabasesEndpoints,+ setupGetCurrentWorkspaceEndpoint,+ setupListTableRemappingsEndpoint,+} from "__support__/server-mocks";+import { renderWithProviders, screen } from "__support__/ui";+import type { TableRemapping } from "metabase-types/api";+import {+ createMockDatabase,+ createMockTableRemapping,+ createMockWorkspaceInstance,+ createMockWorkspaceInstanceDatabase,+} from "metabase-types/api/mocks";++import { WorkspaceInstancePage } from "./WorkspaceInstancePage";++const POSTGRES = createMockDatabase({ id: 10, name: "Postgres" });++function setup({ remappings = [] as TableRemapping[] } = {}) {+ setupDatabasesEndpoints([POSTGRES]);+ setupGetCurrentWorkspaceEndpoint(+ createMockWorkspaceInstance({+ name: "Dev workspace",+ databases: {+ [POSTGRES.id]: createMockWorkspaceInstanceDatabase({+ input_schemas: ["public"],+ output: { schema: "ws_dev" },+ }),+ },+ }),+ );+ setupListTableRemappingsEndpoint(remappings);++ renderWithProviders(<Route path="*" component={WorkspaceInstancePage} />, {+ withRouter: true,+ });+}++describe("WorkspaceInstancePage", () => {+ it("renders the empty state when there are no remappings", async () => {+ setup();++ expect(await screen.findByText("Dev workspace")).toBeInTheDocument();+ expect(+ await screen.findByText(/Tables will be remapped here/i),+ ).toBeInTheDocument();+ });++ it("renders the remapping rows when there is content", async () => {+ setup({+ remappings: [+ createMockTableRemapping({+ database_id: POSTGRES.id,+ from_schema: "public",+ from_table_name: "orders",+ to_schema: "ws_dev",+ to_table_name: "orders",+ }),+ ],+ });++ expect(await screen.findByText("Dev workspace")).toBeInTheDocument();+ expect(+ await screen.findByRole("region", { name: "Postgres" }),+ ).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/index.tsnew file mode 100644index 00000000000..667104e6c3b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/index.ts@@ -0,0 +1 @@+export * from "./WorkspaceInstancePage";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/utils.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/utils.tsnew file mode 100644index 00000000000..57699630467--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceInstancePage/utils.ts@@ -0,0 +1,44 @@+import type {+ Database,+ DatabaseId,+ TableRemapping,+ WorkspaceInstance,+} from "metabase-types/api";++export type DatabaseInfo = {+ database: Database;+ remappings: TableRemapping[];+};++export function getDatabasesInfo(+ workspace: WorkspaceInstance,+ databases: Database[],+ remappings: TableRemapping[],+): DatabaseInfo[] {+ const databaseById = new Map<DatabaseId, Database>(+ databases.map((database) => [database.id, database]),+ );++ const remappingsByDatabaseId = new Map<DatabaseId, TableRemapping[]>();+ for (const remapping of remappings) {+ const list = remappingsByDatabaseId.get(remapping.database_id) ?? [];+ list.push(remapping);+ remappingsByDatabaseId.set(remapping.database_id, list);+ }++ return Object.keys(workspace.databases).reduce<DatabaseInfo[]>(+ (result, key) => {+ const databaseId = Number(key);+ const database = databaseById.get(databaseId);+ if (database == null) {+ return result;+ }+ result.push({+ database,+ remappings: remappingsByDatabaseId.get(databaseId) ?? [],+ });+ return result;+ },+ [],+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/NewWorkspaceButton.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/NewWorkspaceButton.tsxnew file mode 100644index 00000000000..abbfc44a23c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/NewWorkspaceButton.tsx@@ -0,0 +1,36 @@+import { useDisclosure } from "@mantine/hooks";+import { push } from "react-router-redux";+import { t } from "ttag";++import { useDispatch } from "metabase/redux";+import { Button, FixedSizeIcon } from "metabase/ui";+import * as Urls from "metabase/urls";+import type { Workspace } from "metabase-types/api";++import { NewWorkspaceModal } from "../NewWorkspaceModal";++export function NewWorkspaceButton() {+ const [opened, { open, close }] = useDisclosure(false);+ const dispatch = useDispatch();++ const handleCreate = (workspace: Workspace) => {+ close();+ dispatch(push(Urls.workspace(workspace.id)));+ };++ return (+ <>+ <Button+ leftSection={<FixedSizeIcon name="add" aria-hidden />}+ onClick={open}+ >+ {t`New`}+ </Button>+ <NewWorkspaceModal+ opened={opened}+ onCreate={handleCreate}+ onClose={close}+ />+ </>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/NewWorkspaceButton.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/NewWorkspaceButton.unit.spec.tsxnew file mode 100644index 00000000000..91e051c732d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/NewWorkspaceButton.unit.spec.tsx@@ -0,0 +1,24 @@+import userEvent from "@testing-library/user-event";+import { Route } from "react-router";++import { renderWithProviders, screen } from "__support__/ui";++import { NewWorkspaceButton } from "./NewWorkspaceButton";++function setup() {+ renderWithProviders(<Route path="*" component={NewWorkspaceButton} />, {+ withRouter: true,+ });+}++describe("NewWorkspaceButton", () => {+ it("opens the New workspace modal when clicked", async () => {+ setup();++ await userEvent.click(screen.getByRole("button", { name: /New/ }));++ expect(+ await screen.findByRole("heading", { name: "Create a workspace" }),+ ).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/index.tsnew file mode 100644index 00000000000..7204326d4e0--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/index.ts@@ -0,0 +1 @@+export * from "./NewWorkspaceButton";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.tsxnew file mode 100644index 00000000000..d673f947120--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.tsx@@ -0,0 +1,87 @@+import { t } from "ttag";+import * as Yup from "yup";++import {+ Form,+ FormErrorMessage,+ FormProvider,+ FormSubmitButton,+ FormTextInput,+} from "metabase/forms";+import { Button, Group, Modal, Stack } from "metabase/ui";+import * as Errors from "metabase/utils/errors";+import { useCreateWorkspaceMutation } from "metabase-enterprise/api";+import type { Workspace } from "metabase-types/api";++type NewWorkspaceModalProps = {+ opened: boolean;+ onCreate: (workspace: Workspace) => void;+ onClose: () => void;+};++export function NewWorkspaceModal({+ opened,+ onCreate,+ onClose,+}: NewWorkspaceModalProps) {+ return (+ <Modal+ title={t`Create a workspace`}+ opened={opened}+ padding="xl"+ onClose={onClose}+ >+ <NewWorkspaceForm onCreate={onCreate} onClose={onClose} />+ </Modal>+ );+}++type NewWorkspaceFormValues = {+ name: string;+};++const NEW_WORKSPACE_SCHEMA = Yup.object({+ name: Yup.string().required(Errors.required),+});++const INITIAL_VALUES: NewWorkspaceFormValues = {+ name: "",+};++type NewWorkspaceFormProps = {+ onCreate: (workspace: Workspace) => void;+ onClose: () => void;+};++function NewWorkspaceForm({ onCreate, onClose }: NewWorkspaceFormProps) {+ const [createWorkspace] = useCreateWorkspaceMutation();++ const handleSubmit = async ({ name }: NewWorkspaceFormValues) => {+ const workspace = await createWorkspace({ name }).unwrap();+ onCreate(workspace);+ };++ return (+ <FormProvider+ initialValues={INITIAL_VALUES}+ validationSchema={NEW_WORKSPACE_SCHEMA}+ onSubmit={handleSubmit}+ >+ <Form>+ <Stack gap="lg">+ <FormTextInput+ name="name"+ label={t`Name`}+ placeholder={t`My workspace`}+ data-autofocus+ />+ <FormErrorMessage />+ <Group justify="flex-end">+ <Button onClick={onClose}>{t`Cancel`}</Button>+ <FormSubmitButton label={t`Create`} variant="filled" />+ </Group>+ </Stack>+ </Form>+ </FormProvider>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.unit.spec.tsxnew file mode 100644index 00000000000..b7d4dedaae6--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.unit.spec.tsx@@ -0,0 +1,40 @@+import userEvent from "@testing-library/user-event";++import { setupCreateWorkspaceEndpoint } from "__support__/server-mocks";+import { renderWithProviders, screen, waitFor } from "__support__/ui";+import type { Workspace } from "metabase-types/api";+import { createMockWorkspace } from "metabase-types/api/mocks";++import { NewWorkspaceModal } from "./NewWorkspaceModal";++type SetupOpts = {+ createdWorkspace?: Workspace;+};++function setup({+ createdWorkspace = createMockWorkspace({ name: "Brand new workspace" }),+}: SetupOpts = {}) {+ const onCreate = jest.fn();+ const onClose = jest.fn();++ setupCreateWorkspaceEndpoint(createdWorkspace);++ renderWithProviders(+ <NewWorkspaceModal opened onCreate={onCreate} onClose={onClose} />,+ );++ return { onCreate, onClose, createdWorkspace };+}++describe("NewWorkspaceModal", () => {+ it("can create a workspace", async () => {+ const { onCreate, createdWorkspace } = setup();++ await userEvent.type(screen.getByLabelText("Name"), "Brand new workspace");+ await userEvent.click(screen.getByRole("button", { name: "Create" }));++ await waitFor(() =>+ expect(onCreate).toHaveBeenCalledWith(createdWorkspace),+ );+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/index.tsnew file mode 100644index 00000000000..81a7209216c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/index.ts@@ -0,0 +1 @@+export * from "./NewWorkspaceModal";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.module.css b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.module.cssnew file mode 100644index 00000000000..d30bbd50a26--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.module.css@@ -0,0 +1,9 @@+.docsLink {+ color: inherit;+ text-decoration: none;+ background-color: var(--mb-color-background-secondary);+}++.docsLink:hover {+ background-color: var(--mb-color-background-hover);+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.tsxnew file mode 100644index 00000000000..e3ad1b49c05--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.tsx@@ -0,0 +1,118 @@+import { useDisclosure } from "@mantine/hooks";+import { Link } from "react-router";+import { push } from "react-router-redux";+import { t } from "ttag";++import { useDocsUrl } from "metabase/common/hooks";+import { useDispatch } from "metabase/redux";+import {+ Box,+ Button,+ Card,+ FixedSizeIcon,+ Group,+ Stack,+ Text,+ Title,+} from "metabase/ui";+import * as Urls from "metabase/urls";+import type { Workspace } from "metabase-types/api";++import { NewWorkspaceModal } from "../NewWorkspaceModal";++import S from "./WorkspaceEmptyState.module.css";++export function WorkspaceEmptyState() {+ const [opened, { open, close }] = useDisclosure(false);+ const dispatch = useDispatch();+ const { url: fileBasedDevDocsUrl, showMetabaseLinks: showFileBasedDevLink } =+ useDocsUrl("ai/file-based-development");+ const { url: remoteSyncDocsUrl, showMetabaseLinks: showRemoteSyncLink } =+ useDocsUrl("installation-and-operation/remote-sync");++ const handleCreate = (workspace: Workspace) => {+ close();+ dispatch(push(Urls.workspace(workspace.id)));+ };++ return (+ <Card p="xl" maw="40rem" mx="auto" shadow="none" withBorder>+ <Box p="md">+ <Title+ order={3}+ mb="sm"+ >{t`Isolated spaces for agents and developers`}</Title>+ <Text c="text-secondary" mb="sm">+ {t`Develop and run transforms, and build your semantic layer without touching your production tables.`}+ </Text>+ <Text c="text-secondary" mb="md">+ {+ // eslint-disable-next-line metabase/no-literal-metabase-strings -- referring to the product name is intentional+ t`Create a workspace here, or with Metabase’s CLI. That will set up an isolated sandbox with a dedicated schema and database user in the data warehouse(s) you choose.`+ }+ </Text>+ <Box pb="xl">+ <Button variant="filled" onClick={open}>+ {t`Create a workspace`}+ </Button>+ </Box>+ {(showFileBasedDevLink || showRemoteSyncLink) && (+ <Group pt="md" gap="sm" align="stretch">+ {showFileBasedDevLink && (+ <DocsLink+ title={t`File-based development`}+ description={t`How to use the CLI to develop content locally.`}+ link={fileBasedDevDocsUrl}+ />+ )}+ {showRemoteSyncLink && (+ <DocsLink+ title={t`Using remote sync`}+ description={+ // eslint-disable-next-line metabase/no-literal-metabase-strings -- referring to the product name is intentional+ t`How to sync and review Metabase content with git.`+ }+ link={remoteSyncDocsUrl}+ />+ )}+ </Group>+ )}+ </Box>+ <NewWorkspaceModal+ opened={opened}+ onCreate={handleCreate}+ onClose={close}+ />+ </Card>+ );+}++type DocsLinkProps = {+ title: string;+ description: string;+ link: string;+};++function DocsLink({ title, description, link }: DocsLinkProps) {+ return (+ <Box+ className={S.docsLink}+ component={Link}+ to={link}+ target="_blank"+ rel="noreferrer"+ p="md"+ bdrs="md"+ flex="1"+ miw="16rem"+ >+ <Group gap="sm" wrap="nowrap" align="flex-start">+ <FixedSizeIcon c="brand" name="reference" />+ <Stack gap="xs">+ <Title order={5}>{title}</Title>+ <Box c="text-secondary">{description}</Box>+ </Stack>+ </Group>+ </Box>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.unit.spec.tsxnew file mode 100644index 00000000000..e0d83928766--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.unit.spec.tsx@@ -0,0 +1,31 @@+import { Route } from "react-router";++import { renderWithProviders, screen } from "__support__/ui";+import { createMockSettingsState } from "metabase/redux/store/mocks";+import { createMockSettings } from "metabase-types/api/mocks";++import { WorkspaceEmptyState } from "./WorkspaceEmptyState";++function setup() {+ renderWithProviders(<Route path="*" component={WorkspaceEmptyState} />, {+ withRouter: true,+ storeInitialState: {+ settings: createMockSettingsState(+ createMockSettings({ "show-metabase-links": true }),+ ),+ },+ });+}++describe("WorkspaceEmptyState", () => {+ it("renders docs link buttons", () => {+ setup();++ expect(+ screen.getByRole("link", { name: /File-based development/ }),+ ).toBeInTheDocument();+ expect(+ screen.getByRole("link", { name: /Using remote sync/ }),+ ).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/index.tsnew file mode 100644index 00000000000..ec1aff8d377--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/index.ts@@ -0,0 +1 @@+export * from "./WorkspaceEmptyState";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.module.css b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.module.cssnew file mode 100644index 00000000000..619f6e0c0c3--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.module.css@@ -0,0 +1,8 @@+.workspaceItem {+ color: inherit;+ text-decoration: none;+}++.workspaceItem:hover {+ background-color: var(--mb-color-background-hover);+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.tsxnew file mode 100644index 00000000000..3ff17981cec--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.tsx@@ -0,0 +1,77 @@+import { Link } from "react-router";+import { t } from "ttag";++import { Card, Divider, Stack, Text, Title } from "metabase/ui";+import * as Urls from "metabase/urls";+import { getRelativeTime } from "metabase/utils/time-dayjs";+import type { Database, Workspace } from "metabase-types/api";++import { DatabaseInfo } from "../../../components/DatabaseInfo";+import { getDatabasesById } from "../../../utils";++import S from "./WorkspaceItem.module.css";++export type WorkspaceItemProps = {+ workspace: Workspace;+ availableDatabases: Database[];+};++export function WorkspaceItem({+ workspace,+ availableDatabases,+}: WorkspaceItemProps) {+ const databaseById = getDatabasesById(availableDatabases);+ const hasDatabases = workspace.databases.length > 0;++ return (+ <Card+ role="region"+ aria-label={workspace.name}+ data-testid="workspace-item"+ className={S.workspaceItem}+ component={Link}+ to={Urls.workspace(workspace.id)}+ p="lg"+ shadow="none"+ withBorder+ >+ <Stack gap="md">+ <Stack>+ <Title order={4}>{workspace.name}</Title>+ <CreatorInfo workspace={workspace} />+ </Stack>+ {hasDatabases && (+ <>+ <Divider />+ <Stack gap="sm">+ {workspace.databases.map((workspaceDatabase) => (+ <DatabaseInfo+ key={workspaceDatabase.database_id}+ workspaceDatabase={workspaceDatabase}+ database={databaseById.get(workspaceDatabase.database_id)}+ />+ ))}+ </Stack>+ </>+ )}+ </Stack>+ </Card>+ );+}++type CreatorInfoProps = {+ workspace: Workspace;+};++function CreatorInfo({ workspace }: CreatorInfoProps) {+ const timeAgo = getRelativeTime(workspace.created_at);+ const creatorName = workspace.creator?.common_name;++ return (+ <Text c="text-secondary" size="sm">+ {creatorName != null+ ? t`Created by ${creatorName} ${timeAgo}`+ : t`Created ${timeAgo}`}+ </Text>+ );+}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.tsxnew file mode 100644index 00000000000..8c98c74ef0f--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.unit.spec.tsx@@ -0,0 +1,34 @@+import { Route } from "react-router";++import { renderWithProviders, screen } from "__support__/ui";+import * as Urls from "metabase/urls";+import {+ createMockDatabase,+ createMockWorkspace,+} from "metabase-types/api/mocks";++import { WorkspaceItem } from "./WorkspaceItem";++const POSTGRES = createMockDatabase({ id: 10, name: "Postgres" });+const WORKSPACE = createMockWorkspace({ id: 1, name: "My workspace" });++function setup() {+ renderWithProviders(+ <Route+ path="*"+ component={() => (+ <WorkspaceItem workspace={WORKSPACE} availableDatabases={[POSTGRES]} />+ )}+ />,+ { withRouter: true },+ );+}++describe("WorkspaceItem", () => {+ it("renders as a link to the workspace page", () => {+ setup();+ expect(+ screen.getByRole("region", { name: "My workspace" }),+ ).toHaveAttribute("href", Urls.workspace(WORKSPACE.id));+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/index.tsnew file mode 100644index 00000000000..36844fc8a1b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/index.ts@@ -0,0 +1 @@+export * from "./WorkspaceItem";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceListPage.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceListPage.tsxnew file mode 100644index 00000000000..fe6df262e28--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceListPage.tsx@@ -0,0 +1,92 @@+import { t } from "ttag";++import { useListDatabasesQuery } from "metabase/api";+import { DelayedLoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper/DelayedLoadingAndErrorWrapper";+import { DataStudioBreadcrumbs } from "metabase/data-studio/common/components/DataStudioBreadcrumbs";+import { PageContainer } from "metabase/data-studio/common/components/PageContainer";+import { PaneHeader } from "metabase/data-studio/common/components/PaneHeader";+import { Group, Stack } from "metabase/ui";+import { useListWorkspacesQuery } from "metabase-enterprise/api";+import type { Database, Workspace } from "metabase-types/api";++import { HelpMenu } from "../../components/HelpMenu";++import { NewWorkspaceButton } from "./NewWorkspaceButton";+import { WorkspaceEmptyState } from "./WorkspaceEmptyState";+import { WorkspaceItem } from "./WorkspaceItem";++export function WorkspaceListPage() {+ const {+ data: workspaces,+ isLoading: isLoadingWorkspaces,+ error: workspacesError,+ } = useListWorkspacesQuery();+ const {+ data: databasesResponse,+ isLoading: isLoadingDatabases,+ error: databasesError,+ } = useListDatabasesQuery();++ const isLoading = isLoadingWorkspaces || isLoadingDatabases;+ const error = workspacesError ?? databasesError;++ if (+ isLoading ||+ error != null ||+ workspaces == null ||+ databasesResponse == null+ ) {+ return <DelayedLoadingAndErrorWrapper loading={isLoading} error={error} />;+ }++ return (+ <WorkspaceListPageBody+ workspaces={workspaces}+ availableDatabases={databasesResponse.data}+ />+ );+}++type WorkspaceListPageBodyProps = {+ workspaces: Workspace[];+ availableDatabases: Database[];+};++function WorkspaceListPageBody({+ workspaces,+ availableDatabases,+}: WorkspaceListPageBodyProps) {+ const hasWorkspaces = workspaces.length > 0;++ return (+ <PageContainer data-testid="workspace-list-page">+ <PaneHeader+ breadcrumbs={+ <DataStudioBreadcrumbs>{t`Workspaces`}</DataStudioBreadcrumbs>+ }+ actions={+ hasWorkspaces && (+ <Group gap="sm">+ <NewWorkspaceButton />+ <HelpMenu />+ </Group>+ )+ }+ py={0}+ />+ {hasWorkspaces ? (+ <Stack data-testid="workspace-list" gap="lg">+ {workspaces.map((workspace) => (+ <WorkspaceItem+ key={workspace.id}+ workspace={workspace}+ availableDatabases={availableDatabases}+ />+ ))}+ </Stack>+ ) : (+ <WorkspaceEmptyState />+ )}+ </PageContainer>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceListPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceListPage.unit.spec.tsxnew file mode 100644index 00000000000..bbe62f2be7d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceListPage.unit.spec.tsx@@ -0,0 +1,58 @@+import userEvent from "@testing-library/user-event";+import { Route } from "react-router";++import {+ setupCreateWorkspaceEndpoint,+ setupDatabasesEndpoints,+ setupListWorkspacesEndpoint,+} from "__support__/server-mocks";+import { renderWithProviders, screen } from "__support__/ui";+import * as Urls from "metabase/urls";+import type { Workspace } from "metabase-types/api";+import {+ createMockDatabase,+ createMockWorkspace,+} from "metabase-types/api/mocks";++import { WorkspaceListPage } from "./WorkspaceListPage";++const POSTGRES = createMockDatabase({+ id: 10,+ name: "Postgres",+ features: ["workspace"],+});++function setup({ workspaces = [] as Workspace[] } = {}) {+ setupDatabasesEndpoints([POSTGRES]);+ setupListWorkspacesEndpoint(workspaces);+ setupCreateWorkspaceEndpoint(createMockWorkspace({ name: "Brand new" }));++ renderWithProviders(<Route path="*" component={WorkspaceListPage} />, {+ withRouter: true,+ });+}++describe("WorkspaceListPage", () => {+ it("from the empty state, opens the Add workspace modal", async () => {+ setup();++ expect(await screen.findByText(/Isolated spaces/i)).toBeInTheDocument();++ await userEvent.click(+ screen.getByRole("button", { name: /Create a workspace/ }),+ );++ expect(+ await screen.findAllByRole("heading", { name: "Create a workspace" }),+ ).not.toHaveLength(0);+ });++ it("when not empty, each workspace card links to its workspace page", async () => {+ const workspace = createMockWorkspace({ id: 42, name: "Existing" });+ setup({ workspaces: [workspace] });++ expect(+ await screen.findByRole("region", { name: "Existing" }),+ ).toHaveAttribute("href", Urls.workspace(workspace.id));+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/index.tsnew file mode 100644index 00000000000..16f93531518--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/index.ts@@ -0,0 +1 @@+export * from "./WorkspaceListPage";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/AddDatabaseButton/AddDatabaseButton.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/AddDatabaseButton/AddDatabaseButton.tsxnew file mode 100644index 00000000000..3cdda6509c3--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/AddDatabaseButton/AddDatabaseButton.tsx@@ -0,0 +1,66 @@+import { useDisclosure } from "@mantine/hooks";+import { t } from "ttag";++import { Button, FixedSizeIcon, Tooltip } from "metabase/ui";+import type { Database, Workspace } from "metabase-types/api";++import { supportsWorkspaces } from "../../../../utils";+import { NewDatabaseModal } from "../NewDatabaseModal";++export type AddDatabaseButtonProps = {+ workspace: Workspace;+ availableDatabases: Database[];+};++export function AddDatabaseButton({+ workspace,+ availableDatabases,+}: AddDatabaseButtonProps) {+ const [opened, { open, close }] = useDisclosure(false);++ const selectedIds = new Set(workspace.databases.map((db) => db.database_id));+ const selectableDatabases = availableDatabases.filter(+ (database) => !selectedIds.has(database.id),+ );+ const errorMessage = getErrorMessage(availableDatabases, selectableDatabases);+ const isDisabled = errorMessage != null;+ const isEmpty = workspace.databases.length === 0;++ return (+ <>+ <Tooltip label={errorMessage} disabled={!isDisabled}>+ <Button+ variant={isEmpty ? "filled" : "default"}+ disabled={isDisabled}+ leftSection={<FixedSizeIcon name="add" aria-hidden />}+ onClick={open}+ >+ {isEmpty ? t`Add database` : t`Add another database`}+ </Button>+ </Tooltip>+ <NewDatabaseModal+ workspace={workspace}+ availableDatabases={selectableDatabases}+ opened={opened}+ onCreate={close}+ onClose={close}+ />+ </>+ );+}++function getErrorMessage(+ availableDatabases: Database[],+ selectableDatabases: Database[],+): string | undefined {+ if (availableDatabases.length === 0) {+ return t`There are no databases available.`;+ }+ if (selectableDatabases.length === 0) {+ return t`There are no more databases available.`;+ }+ if (!selectableDatabases.some(supportsWorkspaces)) {+ return t`None of the remaining databases support workspaces.`;+ }+ return undefined;+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/AddDatabaseButton/AddDatabaseButton.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/AddDatabaseButton/AddDatabaseButton.unit.spec.tsxnew file mode 100644index 00000000000..37ba4dd09f9--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/AddDatabaseButton/AddDatabaseButton.unit.spec.tsx@@ -0,0 +1,110 @@+import userEvent from "@testing-library/user-event";++import {+ setupCreateWorkspaceDatabaseEndpoint,+ setupDatabasesEndpoints,+} from "__support__/server-mocks";+import { renderWithProviders, screen } from "__support__/ui";+import type { Database, Workspace } from "metabase-types/api";+import {+ createMockDatabase,+ createMockWorkspace,+ createMockWorkspaceDatabase,+} from "metabase-types/api/mocks";++import { AddDatabaseButton } from "./AddDatabaseButton";++const POSTGRES_DATABASE = createMockDatabase({+ id: 10,+ name: "Postgres",+ features: ["schemas", "workspace"],+});++const MYSQL_DATABASE = createMockDatabase({+ id: 20,+ name: "MySQL",+ features: ["workspace"],+});++const UNSUPPORTED_DATABASE = createMockDatabase({+ id: 30,+ name: "Unsupported",+ features: ["schemas"],+});++type SetupOpts = {+ workspace?: Workspace;+ availableDatabases?: Database[];+};++function setup({+ workspace = createMockWorkspace(),+ availableDatabases = [POSTGRES_DATABASE],+}: SetupOpts = {}) {+ setupDatabasesEndpoints(availableDatabases);+ setupCreateWorkspaceDatabaseEndpoint(createMockWorkspace());++ renderWithProviders(+ <AddDatabaseButton+ workspace={workspace}+ availableDatabases={availableDatabases}+ />,+ );+}++describe("AddDatabaseButton", () => {+ it("disables the button when all available databases are already used", async () => {+ const workspace = createMockWorkspace({+ databases: [+ createMockWorkspaceDatabase({ database_id: POSTGRES_DATABASE.id }),+ createMockWorkspaceDatabase({ database_id: MYSQL_DATABASE.id }),+ ],+ });+ setup({+ workspace,+ availableDatabases: [POSTGRES_DATABASE, MYSQL_DATABASE],+ });++ const button = screen.getByRole("button", { name: /Add another database/ });+ expect(button).toBeDisabled();++ await userEvent.hover(button);+ expect(+ await screen.findByText("There are no more databases available."),+ ).toBeInTheDocument();+ });++ it("disables the button when all unused databases do not support workspaces", async () => {+ const workspace = createMockWorkspace({+ databases: [+ createMockWorkspaceDatabase({ database_id: POSTGRES_DATABASE.id }),+ ],+ });+ setup({+ workspace,+ availableDatabases: [POSTGRES_DATABASE, UNSUPPORTED_DATABASE],+ });++ const button = screen.getByRole("button", { name: /Add another database/ });+ expect(button).toBeDisabled();++ await userEvent.hover(button);+ expect(+ await screen.findByText(+ "None of the remaining databases support workspaces.",+ ),+ ).toBeInTheDocument();+ });++ it("disables the button when there are no databases at all", async () => {+ setup({ availableDatabases: [] });++ const button = screen.getByRole("button", { name: /Add database/ });+ expect(button).toBeDisabled();++ await userEvent.hover(button);+ expect(+ await screen.findByText("There are no databases available."),+ ).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/AddDatabaseButton/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/AddDatabaseButton/index.tsnew file mode 100644index 00000000000..fb168cc371a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/AddDatabaseButton/index.ts@@ -0,0 +1 @@+export * from "./AddDatabaseButton";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseEmptyState/DatabaseEmptyState.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseEmptyState/DatabaseEmptyState.tsxnew file mode 100644index 00000000000..70e40a58129--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseEmptyState/DatabaseEmptyState.tsx@@ -0,0 +1,28 @@+import { t } from "ttag";++import { Card, Group, Text } from "metabase/ui";+import type { Database, Workspace } from "metabase-types/api";++import { AddDatabaseButton } from "../AddDatabaseButton";++export type DatabaseEmptyStateProps = {+ workspace: Workspace;+ availableDatabases: Database[];+};++export function DatabaseEmptyState({+ workspace,+ availableDatabases,+}: DatabaseEmptyStateProps) {+ return (+ <Card p="lg" shadow="none" withBorder>+ <Group justify="space-between" align="center" wrap="nowrap">+ <Text>{t`No databases have been added to this workspace yet.`}</Text>+ <AddDatabaseButton+ workspace={workspace}+ availableDatabases={availableDatabases}+ />+ </Group>+ </Card>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseEmptyState/DatabaseEmptyState.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseEmptyState/DatabaseEmptyState.unit.spec.tsxnew file mode 100644index 00000000000..9d648ee32b3--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseEmptyState/DatabaseEmptyState.unit.spec.tsx@@ -0,0 +1,86 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import {+ setupCreateWorkspaceDatabaseEndpoint,+ setupDatabasesEndpoints,+} from "__support__/server-mocks";+import { renderWithProviders, screen, waitFor, within } from "__support__/ui";+import type { Workspace } from "metabase-types/api";+import {+ createMockDatabase,+ createMockTable,+ createMockWorkspace,+ createMockWorkspaceDatabase,+} from "metabase-types/api/mocks";++import { DatabaseEmptyState } from "./DatabaseEmptyState";++const TEST_DATABASE = createMockDatabase({+ id: 10,+ name: "Postgres",+ features: ["schemas", "workspace"],+ tables: [+ createMockTable({ id: 100, db_id: 10, schema: "public", name: "orders" }),+ ],+});++function setup({+ workspace = createMockWorkspace(),+ createdWorkspace = createMockWorkspace({+ databases: [+ createMockWorkspaceDatabase({+ database_id: TEST_DATABASE.id,+ input_schemas: ["public"],+ }),+ ],+ }),+}: {+ workspace?: Workspace;+ createdWorkspace?: Workspace;+} = {}) {+ setupDatabasesEndpoints([TEST_DATABASE]);+ setupCreateWorkspaceDatabaseEndpoint(createdWorkspace);++ renderWithProviders(+ <DatabaseEmptyState+ workspace={workspace}+ availableDatabases={[TEST_DATABASE]}+ />,+ );++ return { workspace };+}++describe("DatabaseEmptyState", () => {+ it("can add a database from the empty state", async () => {+ const { workspace } = setup();++ await userEvent.click(screen.getByRole("button", { name: /Add database/ }));++ const dialog = await screen.findByRole("dialog");+ await userEvent.click(+ await within(dialog).findByLabelText("Schemas to include"),+ );+ await userEvent.click(+ await screen.findByRole("option", { name: "public" }),+ );+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Add database" }),+ );++ await waitFor(() => {+ const request = fetchMock.callHistory.lastCall(+ `path:/api/ee/workspace-manager/${workspace.id}/database`,+ )?.request;+ expect(request).toBeDefined();+ });+ const request = fetchMock.callHistory.lastCall(+ `path:/api/ee/workspace-manager/${workspace.id}/database`,+ )?.request;+ expect(await request?.json()).toEqual({+ database_id: TEST_DATABASE.id,+ input_schemas: ["public"],+ });+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseEmptyState/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseEmptyState/index.tsnew file mode 100644index 00000000000..541af63b882--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseEmptyState/index.ts@@ -0,0 +1 @@+export * from "./DatabaseEmptyState";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseItem/DatabaseItem.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseItem/DatabaseItem.tsxnew file mode 100644index 00000000000..0b8404efc09--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseItem/DatabaseItem.tsx@@ -0,0 +1,78 @@+import { useDisclosure } from "@mantine/hooks";+import { t } from "ttag";++import { hasFeature } from "metabase/common/utils/database";+import { ActionIcon, Card, FixedSizeIcon, Group, Menu } from "metabase/ui";+import type {+ Database,+ Workspace,+ WorkspaceDatabase,+} from "metabase-types/api";++import { DatabaseInfo } from "../../../../components/DatabaseInfo";+import { useDeleteDatabase } from "../../../../hooks";+import { UpdateDatabaseModal } from "../UpdateDatabaseModal";++export type DatabaseItemProps = {+ workspace: Workspace;+ workspaceDatabase: WorkspaceDatabase;+ database: Database | undefined;+};++export function DatabaseItem({+ workspace,+ workspaceDatabase,+ database,+}: DatabaseItemProps) {+ const [opened, { open, close }] = useDisclosure(false);+ const { handleDelete, modalContent } = useDeleteDatabase({ database });+ const supportsSchemas = database != null && hasFeature(database, "schemas");++ return (+ <Card+ role="region"+ aria-label={database?.name}+ data-testid="workspace-database-item"+ p="lg"+ shadow="none"+ withBorder+ >+ <Group justify="space-between" align="center" wrap="nowrap">+ <DatabaseInfo+ workspaceDatabase={workspaceDatabase}+ database={database}+ />+ <Menu>+ <Menu.Target>+ <ActionIcon size="sm" aria-label={t`Database actions`}>+ <FixedSizeIcon name="ellipsis" aria-hidden />+ </ActionIcon>+ </Menu.Target>+ <Menu.Dropdown>+ {supportsSchemas && (+ <Menu.Item+ leftSection={<FixedSizeIcon name="pencil" aria-hidden />}+ onClick={open}+ >{t`Edit`}</Menu.Item>+ )}+ <Menu.Item+ leftSection={<FixedSizeIcon name="trash" aria-hidden />}+ onClick={() => handleDelete(workspace, workspaceDatabase)}+ >{t`Remove`}</Menu.Item>+ </Menu.Dropdown>+ </Menu>+ </Group>+ {database != null && (+ <UpdateDatabaseModal+ workspace={workspace}+ workspaceDatabase={workspaceDatabase}+ database={database}+ opened={opened}+ onUpdate={close}+ onClose={close}+ />+ )}+ {modalContent}+ </Card>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseItem/DatabaseItem.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseItem/DatabaseItem.unit.spec.tsxnew file mode 100644index 00000000000..3419d1c160e--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseItem/DatabaseItem.unit.spec.tsx@@ -0,0 +1,92 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import {+ setupDatabasesEndpoints,+ setupDeleteWorkspaceDatabaseEndpoint,+ setupUpdateWorkspaceDatabaseEndpoint,+} from "__support__/server-mocks";+import { renderWithProviders, screen, waitFor } from "__support__/ui";+import {+ createMockDatabase,+ createMockTable,+ createMockWorkspace,+ createMockWorkspaceDatabase,+} from "metabase-types/api/mocks";++import { DatabaseItem } from "./DatabaseItem";++const TEST_DATABASE = createMockDatabase({+ id: 10,+ name: "Postgres",+ features: ["schemas"],+ tables: [+ createMockTable({ id: 100, db_id: 10, schema: "public", name: "orders" }),+ ],+});++const WORKSPACE = createMockWorkspace({+ id: 1,+ databases: [+ createMockWorkspaceDatabase({+ database_id: TEST_DATABASE.id,+ input_schemas: ["public"],+ }),+ ],+});++function setup() {+ setupDatabasesEndpoints([TEST_DATABASE]);+ setupUpdateWorkspaceDatabaseEndpoint(WORKSPACE, TEST_DATABASE.id);+ setupDeleteWorkspaceDatabaseEndpoint(WORKSPACE, TEST_DATABASE.id);++ renderWithProviders(+ <DatabaseItem+ workspace={WORKSPACE}+ workspaceDatabase={WORKSPACE.databases[0]}+ database={TEST_DATABASE}+ />,+ );+}++describe("DatabaseItem", () => {+ it("opens the edit modal from the menu when Edit is clicked", async () => {+ setup();++ await userEvent.click(+ screen.getByRole("button", { name: "Database actions" }),+ );+ await userEvent.click(+ await screen.findByRole("menuitem", { name: /Edit/ }),+ );++ expect(+ await screen.findByRole("heading", {+ name: `Edit settings for ${TEST_DATABASE.name}`,+ }),+ ).toBeInTheDocument();+ });++ it("calls DELETE on the database after confirming Remove", async () => {+ setup();++ await userEvent.click(+ screen.getByRole("button", { name: "Database actions" }),+ );+ await userEvent.click(+ await screen.findByRole("menuitem", { name: /Remove/ }),+ );+ await userEvent.click(+ await screen.findByRole("button", { name: "Remove" }),+ );++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called(+ `path:/api/ee/workspace-manager/${WORKSPACE.id}/database/${TEST_DATABASE.id}`,+ { method: "DELETE" },+ ),+ ).toBe(true);+ });+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseItem/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseItem/index.tsnew file mode 100644index 00000000000..d4813704f4e--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseItem/index.ts@@ -0,0 +1 @@+export * from "./DatabaseItem";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseSection.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseSection.tsxnew file mode 100644index 00000000000..e7f8d9a5b28--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseSection.tsx@@ -0,0 +1,59 @@+import { t } from "ttag";++import { Box, Stack, Text, Title } from "metabase/ui";+import type { Database, Workspace } from "metabase-types/api";++import { getDatabasesById } from "../../../utils";++import { AddDatabaseButton } from "./AddDatabaseButton";+import { DatabaseEmptyState } from "./DatabaseEmptyState";+import { DatabaseItem } from "./DatabaseItem";++export type DatabaseSectionProps = {+ workspace: Workspace;+ availableDatabases: Database[];+};++export function DatabaseSection({+ workspace,+ availableDatabases,+}: DatabaseSectionProps) {+ const databaseById = getDatabasesById(availableDatabases);+ const isEmpty = workspace.databases.length === 0;++ return (+ <Stack data-testid="workspace-database-section">+ <Stack gap="sm">+ <Title order={4}>{t`Databases to include`}</Title>+ <Text c="text-secondary">+ {t`The databases and schemas that should be accessible from this workspace`}+ </Text>+ </Stack>+ {isEmpty ? (+ <DatabaseEmptyState+ workspace={workspace}+ availableDatabases={availableDatabases}+ />+ ) : (+ <>+ <Stack gap="md">+ {workspace.databases.map((workspaceDatabase) => (+ <DatabaseItem+ key={workspaceDatabase.database_id}+ workspace={workspace}+ workspaceDatabase={workspaceDatabase}+ database={databaseById.get(workspaceDatabase.database_id)}+ />+ ))}+ </Stack>+ <Box>+ <AddDatabaseButton+ workspace={workspace}+ availableDatabases={availableDatabases}+ />+ </Box>+ </>+ )}+ </Stack>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseSection.unit.spec.tsxnew file mode 100644index 00000000000..e6e4ab346dc--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/DatabaseSection.unit.spec.tsx@@ -0,0 +1,83 @@+import userEvent from "@testing-library/user-event";++import {+ setupCreateWorkspaceDatabaseEndpoint,+ setupDatabasesEndpoints,+} from "__support__/server-mocks";+import { renderWithProviders, screen, within } from "__support__/ui";+import {+ createMockDatabase,+ createMockTable,+ createMockWorkspace,+ createMockWorkspaceDatabase,+} from "metabase-types/api/mocks";++import { DatabaseSection } from "./DatabaseSection";++const POSTGRES = createMockDatabase({+ id: 10,+ name: "Postgres",+ features: ["schemas", "workspace"],+ tables: [+ createMockTable({ id: 100, db_id: 10, schema: "public", name: "orders" }),+ ],+});++const SNOWFLAKE = createMockDatabase({+ id: 11,+ name: "Snowflake",+ features: ["schemas", "workspace"],+ tables: [+ createMockTable({ id: 200, db_id: 11, schema: "raw", name: "events" }),+ ],+});++function setup({+ workspace = createMockWorkspace(),+ availableDatabases = [POSTGRES, SNOWFLAKE],+} = {}) {+ setupDatabasesEndpoints(availableDatabases);+ setupCreateWorkspaceDatabaseEndpoint(workspace);++ renderWithProviders(+ <DatabaseSection+ workspace={workspace}+ availableDatabases={availableDatabases}+ />,+ );+}++describe("DatabaseSection", () => {+ it("can open the add modal and shows both databases as radio options", async () => {+ setup();++ await userEvent.click(screen.getByRole("button", { name: /Add database/ }));++ expect(+ await screen.findByRole("radio", { name: "Postgres" }),+ ).toBeInTheDocument();+ expect(+ screen.getByRole("radio", { name: "Snowflake" }),+ ).toBeInTheDocument();+ });++ it("excludes already-selected databases from the picker", async () => {+ const workspace = createMockWorkspace({+ databases: [+ createMockWorkspaceDatabase({+ database_id: POSTGRES.id,+ input_schemas: ["public"],+ }),+ ],+ });+ setup({ workspace });++ await userEvent.click(+ screen.getByRole("button", { name: /Add another database/ }),+ );++ const dialog = await screen.findByRole("dialog");+ expect(within(dialog).getByText("Snowflake")).toBeInTheDocument();+ expect(within(dialog).queryByText("Postgres")).not.toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/NewDatabaseModal/NewDatabaseModal.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/NewDatabaseModal/NewDatabaseModal.tsxnew file mode 100644index 00000000000..e21936d6b02--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/NewDatabaseModal/NewDatabaseModal.tsx@@ -0,0 +1,229 @@+import { useMemo } from "react";+import { t } from "ttag";+import * as Yup from "yup";++import { hasFeature } from "metabase/common/utils/database";+import {+ Form,+ FormErrorMessage,+ FormProvider,+ FormSubmitButton,+} from "metabase/forms";+import {+ Button,+ FixedSizeIcon,+ FocusTrap,+ Group,+ Input,+ Modal,+ Radio,+ Stack,+ Text,+ Tooltip,+} from "metabase/ui";+import * as Errors from "metabase/utils/errors";+import { useCreateWorkspaceDatabaseMutation } from "metabase-enterprise/api";+import type { Database, DatabaseId, Workspace } from "metabase-types/api";++import { supportsWorkspaces } from "../../../../utils";+import { SchemaMultiSelect } from "../SchemaMultiSelect";++export type NewDatabaseModalProps = {+ workspace: Workspace;+ availableDatabases: Database[];+ opened: boolean;+ onCreate: (workspace: Workspace) => void;+ onClose: () => void;+};++export function NewDatabaseModal({+ workspace,+ availableDatabases,+ opened,+ onCreate,+ onClose,+}: NewDatabaseModalProps) {+ return (+ <Modal+ title={t`Add a database to this workspace`}+ opened={opened}+ padding="xl"+ onClose={onClose}+ >+ <FocusTrap.InitialFocus />+ <NewDatabaseForm+ workspace={workspace}+ availableDatabases={availableDatabases}+ onCreate={onCreate}+ onClose={onClose}+ />+ </Modal>+ );+}++type NewDatabaseFormValues = {+ database_id: DatabaseId | null;+ input_schemas: string[];+};++function getValidationSchema(availableDatabases: Database[]) {+ return Yup.object({+ database_id: Yup.number().nullable().required(Errors.required),+ input_schemas: Yup.array()+ .of(Yup.string().required())+ .when("database_id", {+ is: (databaseId: DatabaseId | null | undefined) => {+ if (databaseId == null) {+ return false;+ }+ const database = availableDatabases.find(+ (db) => db.id === databaseId,+ );+ return database != null && hasFeature(database, "schemas");+ },+ then: (schema) => schema.min(1, Errors.required).required(),+ otherwise: (schema) => schema.required(),+ }),+ });+}++type NewDatabaseFormProps = {+ workspace: Workspace;+ availableDatabases: Database[];+ onCreate: (workspace: Workspace) => void;+ onClose: () => void;+};++function NewDatabaseForm({+ workspace,+ availableDatabases,+ onCreate,+ onClose,+}: NewDatabaseFormProps) {+ const [createWorkspaceDatabase] = useCreateWorkspaceDatabaseMutation();++ const initialValues: NewDatabaseFormValues = {+ database_id: availableDatabases.find(supportsWorkspaces)?.id ?? null,+ input_schemas: [],+ };++ const validationSchema = useMemo(+ () => getValidationSchema(availableDatabases),+ [availableDatabases],+ );++ const handleSubmit = async (values: NewDatabaseFormValues) => {+ if (values.database_id == null) {+ return;+ }+ const updated = await createWorkspaceDatabase({+ id: workspace.id,+ database_id: values.database_id,+ input_schemas: values.input_schemas,+ }).unwrap();+ onCreate(updated);+ };++ return (+ <FormProvider+ initialValues={initialValues}+ validationSchema={validationSchema}+ onSubmit={handleSubmit}+ >+ {({ values, setFieldValue }) => {+ const selectedDatabase = availableDatabases.find(+ (database) => database.id === values.database_id,+ );+ const supportsSchemas =+ selectedDatabase != null && hasFeature(selectedDatabase, "schemas");++ return (+ <Form>+ <Stack gap="lg">+ <Text>+ {t`This will create a temporary schema and user in this database, grant that user read access to the schemas you select, and write access to the temporary schema.`}+ </Text>+ <DatabaseInput+ databases={availableDatabases}+ value={values.database_id}+ onChange={(databaseId) => {+ setFieldValue("database_id", databaseId);+ setFieldValue("input_schemas", []);+ }}+ />+ {values.database_id != null && supportsSchemas && (+ <SchemaMultiSelect+ databaseId={values.database_id}+ value={values.input_schemas}+ onChange={(schemas) =>+ setFieldValue("input_schemas", schemas)+ }+ />+ )}+ <FormErrorMessage />+ <Group justify="flex-end">+ <Button onClick={onClose}>{t`Cancel`}</Button>+ <FormSubmitButton label={t`Add database`} variant="filled" />+ </Group>+ </Stack>+ </Form>+ );+ }}+ </FormProvider>+ );+}++type DatabaseInputProps = {+ databases: Database[];+ value: DatabaseId | null;+ onChange: (databaseId: DatabaseId | null) => void;+};++function DatabaseInput({ databases, value, onChange }: DatabaseInputProps) {+ if (databases.length === 1) {+ return (+ <Input.Wrapper label={t`Database`}>+ <Text>{databases[0].name}</Text>+ </Input.Wrapper>+ );+ }++ return (+ <Radio.Group+ label={t`Database`}+ value={value != null ? String(value) : null}+ onChange={(newValue) =>+ onChange(newValue != null ? Number(newValue) : null)+ }+ >+ <Stack gap="sm">+ {databases.map((database) => (+ <Radio+ key={database.id}+ value={String(database.id)}+ label={<DatabaseLabel database={database} />}+ disabled={!supportsWorkspaces(database)}+ />+ ))}+ </Stack>+ </Radio.Group>+ );+}++type DatabaseLabelProps = {+ database: Database;+};++function DatabaseLabel({ database }: DatabaseLabelProps) {+ if (supportsWorkspaces(database)) {+ return <>{database.name}</>;+ }+ return (+ <Group gap="xs" wrap="nowrap" component="span">+ <span>{database.name}</span>+ <Tooltip label={t`This database does not support workspaces.`}>+ <FixedSizeIcon name="info" c="text-secondary" aria-hidden />+ </Tooltip>+ </Group>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/NewDatabaseModal/NewDatabaseModal.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/NewDatabaseModal/NewDatabaseModal.unit.spec.tsxnew file mode 100644index 00000000000..79c69d180f2--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/NewDatabaseModal/NewDatabaseModal.unit.spec.tsx@@ -0,0 +1,144 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import {+ setupCreateWorkspaceDatabaseEndpoint,+ setupDatabasesEndpoints,+} from "__support__/server-mocks";+import { renderWithProviders, screen, waitFor } from "__support__/ui";+import type { Database, Workspace } from "metabase-types/api";+import {+ createMockDatabase,+ createMockTable,+ createMockWorkspace,+ createMockWorkspaceDatabase,+} from "metabase-types/api/mocks";++import { NewDatabaseModal } from "./NewDatabaseModal";++const POSTGRES_DATABASE = createMockDatabase({+ id: 10,+ name: "Postgres",+ features: ["schemas", "workspace"],+ tables: [+ createMockTable({ id: 100, db_id: 10, schema: "public", name: "orders" }),+ ],+});++const MYSQL_DATABASE = createMockDatabase({+ id: 20,+ name: "MySQL",+ features: ["workspace"],+ tables: [createMockTable({ id: 200, db_id: 20, schema: "", name: "people" })],+});++const UNSUPPORTED_DATABASE = createMockDatabase({+ id: 30,+ name: "Unsupported",+ features: ["schemas"],+});++type SetupOpts = {+ workspace?: Workspace;+ availableDatabases?: Database[];+ createdWorkspace?: Workspace;+};++function setup({+ workspace = createMockWorkspace(),+ availableDatabases = [POSTGRES_DATABASE],+ createdWorkspace = createMockWorkspace({+ databases: [+ createMockWorkspaceDatabase({+ database_id: POSTGRES_DATABASE.id,+ input_schemas: ["public"],+ }),+ ],+ }),+}: SetupOpts = {}) {+ const onCreate = jest.fn();+ const onClose = jest.fn();++ setupDatabasesEndpoints(availableDatabases);+ setupCreateWorkspaceDatabaseEndpoint(createdWorkspace);++ renderWithProviders(+ <NewDatabaseModal+ workspace={workspace}+ availableDatabases={availableDatabases}+ opened+ onCreate={onCreate}+ onClose={onClose}+ />,+ );++ return { onCreate, onClose, workspace, createdWorkspace };+}++describe("NewDatabaseModal", () => {+ it("can create a workspace database", async () => {+ const { onCreate, createdWorkspace } = setup();++ await userEvent.click(screen.getByLabelText("Schemas to include"));+ await userEvent.click(+ await screen.findByRole("option", { name: "public" }),+ );+ await userEvent.click(screen.getByRole("button", { name: "Add database" }));++ await waitFor(() =>+ expect(onCreate).toHaveBeenCalledWith(createdWorkspace),+ );+ });++ it("renders a single database as text without a radio group", () => {+ setup({ availableDatabases: [POSTGRES_DATABASE] });++ expect(screen.getByText("Postgres")).toBeInTheDocument();+ expect(screen.queryByRole("radio")).not.toBeInTheDocument();+ });++ it("disables radios for databases that do not support workspaces", () => {+ setup({ availableDatabases: [POSTGRES_DATABASE, UNSUPPORTED_DATABASE] });++ expect(screen.getByRole("radio", { name: "Postgres" })).toBeEnabled();+ expect(screen.getByRole("radio", { name: /Unsupported/ })).toBeDisabled();+ });++ it("requires at least one schema when the database supports schemas", async () => {+ const { onCreate } = setup({ availableDatabases: [POSTGRES_DATABASE] });++ await userEvent.click(screen.getByRole("button", { name: "Add database" }));++ expect(onCreate).not.toHaveBeenCalled();+ expect(+ fetchMock.callHistory.lastCall(+ "path:/api/ee/workspace-manager/1/database",+ ),+ ).toBeUndefined();+ });++ it("allows submitting without schemas when the database does not support schemas", async () => {+ const createdWorkspace = createMockWorkspace({+ databases: [+ createMockWorkspaceDatabase({+ database_id: MYSQL_DATABASE.id,+ input_schemas: [],+ }),+ ],+ });+ const { onCreate } = setup({+ availableDatabases: [MYSQL_DATABASE],+ createdWorkspace,+ });++ expect(+ screen.queryByLabelText("Schemas to include"),+ ).not.toBeInTheDocument();++ await userEvent.click(screen.getByRole("button", { name: "Add database" }));++ await waitFor(() =>+ expect(onCreate).toHaveBeenCalledWith(createdWorkspace),+ );+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/NewDatabaseModal/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/NewDatabaseModal/index.tsnew file mode 100644index 00000000000..3c53504ceb5--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/NewDatabaseModal/index.ts@@ -0,0 +1 @@+export * from "./NewDatabaseModal";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/SchemaMultiSelect/SchemaMultiSelect.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/SchemaMultiSelect/SchemaMultiSelect.tsxnew file mode 100644index 00000000000..59d4a03aa61--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/SchemaMultiSelect/SchemaMultiSelect.tsx@@ -0,0 +1,32 @@+import { t } from "ttag";++import { useListDatabaseSchemasQuery } from "metabase/api";+import { MultiSelect } from "metabase/ui";+import type { DatabaseId } from "metabase-types/api";++export type SchemaMultiSelectProps = {+ databaseId: DatabaseId;+ value: string[];+ onChange: (schemas: string[]) => void;+};++export function SchemaMultiSelect({+ databaseId,+ value,+ onChange,+}: SchemaMultiSelectProps) {+ const { data: schemas = [] } = useListDatabaseSchemasQuery({+ id: databaseId,+ });+ const isAllSelected = schemas.length > 0 && value.length === schemas.length;++ return (+ <MultiSelect+ label={t`Schemas to include`}+ placeholder={isAllSelected ? t`All schemas selected` : t`Pick schemas`}+ data={schemas}+ value={value}+ onChange={onChange}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/SchemaMultiSelect/SchemaMultiSelect.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/SchemaMultiSelect/SchemaMultiSelect.unit.spec.tsxnew file mode 100644index 00000000000..ca6c4f438f3--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/SchemaMultiSelect/SchemaMultiSelect.unit.spec.tsx@@ -0,0 +1,59 @@+import userEvent from "@testing-library/user-event";++import { setupDatabasesEndpoints } from "__support__/server-mocks";+import { renderWithProviders, screen } from "__support__/ui";+import { createMockDatabase, createMockTable } from "metabase-types/api/mocks";++import { SchemaMultiSelect } from "./SchemaMultiSelect";++const TEST_DATABASE = createMockDatabase({+ id: 10,+ name: "Postgres",+ features: ["schemas"],+ tables: [+ createMockTable({ id: 100, db_id: 10, schema: "public", name: "orders" }),+ createMockTable({+ id: 101,+ db_id: 10,+ schema: "analytics",+ name: "events",+ }),+ ],+});++function setup({ value = [] as string[] } = {}) {+ const onChange = jest.fn();+ setupDatabasesEndpoints([TEST_DATABASE]);+ renderWithProviders(+ <SchemaMultiSelect+ databaseId={TEST_DATABASE.id}+ value={value}+ onChange={onChange}+ />,+ );+ return { onChange };+}++describe("SchemaMultiSelect", () => {+ it("can select the first schema", async () => {+ const { onChange } = setup();++ await userEvent.click(screen.getByLabelText("Schemas to include"));+ await userEvent.click(+ await screen.findByRole("option", { name: "public" }),+ );++ expect(onChange).toHaveBeenCalledWith(["public"]);+ });++ it("can select the second schema", async () => {+ const { onChange } = setup({ value: ["public"] });++ await userEvent.click(screen.getByLabelText("Schemas to include"));+ await userEvent.click(+ await screen.findByRole("option", { name: "analytics" }),+ );++ expect(onChange).toHaveBeenCalledWith(["public", "analytics"]);+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/SchemaMultiSelect/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/SchemaMultiSelect/index.tsnew file mode 100644index 00000000000..166d9e78787--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/SchemaMultiSelect/index.ts@@ -0,0 +1 @@+export * from "./SchemaMultiSelect";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/UpdateDatabaseModal/UpdateDatabaseModal.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/UpdateDatabaseModal/UpdateDatabaseModal.tsxnew file mode 100644index 00000000000..e10ac20c1d8--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/UpdateDatabaseModal/UpdateDatabaseModal.tsx@@ -0,0 +1,122 @@+import { t } from "ttag";+import * as Yup from "yup";++import {+ Form,+ FormErrorMessage,+ FormProvider,+ FormSubmitButton,+} from "metabase/forms";+import { Button, FocusTrap, Group, Modal, Stack, Text } from "metabase/ui";+import { useUpdateWorkspaceDatabaseMutation } from "metabase-enterprise/api";+import type {+ Database,+ Workspace,+ WorkspaceDatabase,+} from "metabase-types/api";++import { SchemaMultiSelect } from "../SchemaMultiSelect";++export type UpdateDatabaseModalProps = {+ workspace: Workspace;+ workspaceDatabase: WorkspaceDatabase;+ database: Database;+ opened: boolean;+ onUpdate: (workspace: Workspace) => void;+ onClose: () => void;+};++export function UpdateDatabaseModal({+ workspace,+ workspaceDatabase,+ database,+ opened,+ onUpdate,+ onClose,+}: UpdateDatabaseModalProps) {+ return (+ <Modal+ title={t`Edit settings for ${database.name}`}+ opened={opened}+ padding="xl"+ onClose={onClose}+ >+ <FocusTrap.InitialFocus />+ <UpdateDatabaseForm+ workspace={workspace}+ workspaceDatabase={workspaceDatabase}+ onUpdate={onUpdate}+ onClose={onClose}+ />+ </Modal>+ );+}++type UpdateDatabaseFormValues = {+ input_schemas: string[];+};++const UPDATE_DATABASE_SCHEMA = Yup.object({+ input_schemas: Yup.array().of(Yup.string().required()).required(),+});++type UpdateDatabaseFormProps = {+ workspace: Workspace;+ workspaceDatabase: WorkspaceDatabase;+ onUpdate: (workspace: Workspace) => void;+ onClose: () => void;+};++function UpdateDatabaseForm({+ workspace,+ workspaceDatabase,+ onUpdate,+ onClose,+}: UpdateDatabaseFormProps) {+ const [updateWorkspaceDatabase] = useUpdateWorkspaceDatabaseMutation();++ const initialValues: UpdateDatabaseFormValues = {+ input_schemas: workspaceDatabase.input_schemas,+ };++ const handleSubmit = async (values: UpdateDatabaseFormValues) => {+ const updated = await updateWorkspaceDatabase({+ id: workspace.id,+ database_id: workspaceDatabase.database_id,+ input_schemas: values.input_schemas,+ }).unwrap();+ onUpdate(updated);+ };++ return (+ <FormProvider+ initialValues={initialValues}+ validationSchema={UPDATE_DATABASE_SCHEMA}+ onSubmit={handleSubmit}+ >+ {({ values, setFieldValue, dirty }) => (+ <Form>+ <Stack gap="lg">+ <Text>+ {t`Changing these settings will first delete the temporary user and schema, then recreate them with your new settings.`}+ </Text>+ <SchemaMultiSelect+ databaseId={workspaceDatabase.database_id}+ value={values.input_schemas}+ onChange={(schemas) => setFieldValue("input_schemas", schemas)}+ />+ <FormErrorMessage />+ <Group justify="flex-end">+ <Button onClick={onClose}>{t`Cancel`}</Button>+ <FormSubmitButton+ label={t`Save changes`}+ variant="filled"+ disabled={!dirty}+ />+ </Group>+ </Stack>+ </Form>+ )}+ </FormProvider>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/UpdateDatabaseModal/UpdateDatabaseModal.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/UpdateDatabaseModal/UpdateDatabaseModal.unit.spec.tsxnew file mode 100644index 00000000000..52f48dbe389--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/UpdateDatabaseModal/UpdateDatabaseModal.unit.spec.tsx@@ -0,0 +1,94 @@+import userEvent from "@testing-library/user-event";++import {+ setupDatabasesEndpoints,+ setupUpdateWorkspaceDatabaseEndpoint,+} from "__support__/server-mocks";+import { renderWithProviders, screen, waitFor } from "__support__/ui";+import type {+ Database,+ Workspace,+ WorkspaceDatabase,+} from "metabase-types/api";+import {+ createMockDatabase,+ createMockTable,+ createMockWorkspace,+ createMockWorkspaceDatabase,+} from "metabase-types/api/mocks";++import { UpdateDatabaseModal } from "./UpdateDatabaseModal";++const TEST_DATABASE = createMockDatabase({+ id: 10,+ name: "Postgres",+ features: ["schemas"],+ tables: [+ createMockTable({ id: 100, db_id: 10, schema: "public", name: "orders" }),+ createMockTable({+ id: 101,+ db_id: 10,+ schema: "analytics",+ name: "events",+ }),+ ],+});++type SetupOpts = {+ workspace?: Workspace;+ workspaceDatabase?: WorkspaceDatabase;+ database?: Database;+ updatedWorkspace?: Workspace;+};++function setup({+ workspace = createMockWorkspace(),+ workspaceDatabase = createMockWorkspaceDatabase({+ database_id: TEST_DATABASE.id,+ input_schemas: ["public"],+ }),+ database = TEST_DATABASE,+ updatedWorkspace = createMockWorkspace({+ databases: [+ createMockWorkspaceDatabase({+ database_id: TEST_DATABASE.id,+ input_schemas: ["public", "analytics"],+ }),+ ],+ }),+}: SetupOpts = {}) {+ const onUpdate = jest.fn();+ const onClose = jest.fn();++ setupDatabasesEndpoints([TEST_DATABASE]);+ setupUpdateWorkspaceDatabaseEndpoint(updatedWorkspace, TEST_DATABASE.id);++ renderWithProviders(+ <UpdateDatabaseModal+ workspace={workspace}+ workspaceDatabase={workspaceDatabase}+ database={database}+ opened+ onUpdate={onUpdate}+ onClose={onClose}+ />,+ );++ return { onUpdate, onClose, updatedWorkspace };+}++describe("UpdateDatabaseModal", () => {+ it("can update a workspace database", async () => {+ const { onUpdate, updatedWorkspace } = setup();++ await userEvent.click(screen.getByLabelText("Schemas to include"));+ await userEvent.click(+ await screen.findByRole("option", { name: "analytics" }),+ );+ await userEvent.click(screen.getByRole("button", { name: "Save changes" }));++ await waitFor(() =>+ expect(onUpdate).toHaveBeenCalledWith(updatedWorkspace),+ );+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/UpdateDatabaseModal/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/UpdateDatabaseModal/index.tsnew file mode 100644index 00000000000..350dc2200f4--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/UpdateDatabaseModal/index.ts@@ -0,0 +1 @@+export * from "./UpdateDatabaseModal";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/index.tsnew file mode 100644index 00000000000..a7f46d3eb67--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/DatabaseSection/index.ts@@ -0,0 +1 @@+export * from "./DatabaseSection";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/SetupSection/SetupSection.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/SetupSection/SetupSection.tsxnew file mode 100644index 00000000000..0a8e81077c1--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/SetupSection/SetupSection.tsx@@ -0,0 +1,35 @@+import { jt, t } from "ttag";++import { TitleSection } from "metabase/data-studio/common/components/TitleSection";+import { Button, Code, FixedSizeIcon, Group, Text } from "metabase/ui";+import type { Workspace } from "metabase-types/api";++const CONFIG_FILENAME = "config.yml";++export type SetupSectionProps = {+ workspace: Workspace;+};++export function SetupSection({ workspace }: SetupSectionProps) {+ // eslint-disable-next-line metabase/no-literal-metabase-strings -- referring to the product name is intentional+ const description = jt`Run a local Metabase instance backed by this workspace's data so you can make changes safely. Pass this ${<Code key="config">{CONFIG_FILENAME}</Code>} file, containing the database credentials, when starting the instance.`;++ return (+ <TitleSection+ data-testid="workspace-setup-section"+ label={t`How to set up a development instance`}+ >+ <Group p="lg" justify="space-between" align="center">+ <Text maw="40rem">{description}</Text>+ <Button+ component="a"+ href={`/api/ee/workspace-manager/${workspace.id}/config`}+ download={CONFIG_FILENAME}+ leftSection={<FixedSizeIcon name="download" aria-hidden />}+ >+ {t`Download ${CONFIG_FILENAME}`}+ </Button>+ </Group>+ </TitleSection>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/SetupSection/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/SetupSection/index.tsnew file mode 100644index 00000000000..aa90fcb4034--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/SetupSection/index.ts@@ -0,0 +1 @@+export * from "./SetupSection";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspaceHeader/WorkspaceHeader.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspaceHeader/WorkspaceHeader.tsxnew file mode 100644index 00000000000..37710d8f589--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspaceHeader/WorkspaceHeader.tsx@@ -0,0 +1,95 @@+import type { ReactNode } from "react";+import { Link } from "react-router";+import { push } from "react-router-redux";+import { t } from "ttag";++import { DataStudioBreadcrumbs } from "metabase/data-studio/common/components/DataStudioBreadcrumbs";+import {+ PaneHeader,+ PaneHeaderInput,+} from "metabase/data-studio/common/components/PaneHeader";+import { useMetadataToasts } from "metabase/metadata/hooks";+import { useDispatch } from "metabase/redux";+import { ActionIcon, FixedSizeIcon, Menu } from "metabase/ui";+import * as Urls from "metabase/urls";+import { useUpdateWorkspaceMutation } from "metabase-enterprise/api";+import type { Workspace } from "metabase-types/api";++import { useDeleteWorkspace } from "../../../hooks";++export type WorkspaceHeaderProps = {+ workspace: Workspace;+ actions?: ReactNode;+};++export function WorkspaceHeader({ workspace, actions }: WorkspaceHeaderProps) {+ const [updateWorkspace] = useUpdateWorkspaceMutation();+ const { sendSuccessToast, sendErrorToast } = useMetadataToasts();++ const handleNameChange = async (newName: string) => {+ const { error } = await updateWorkspace({+ id: workspace.id,+ name: newName,+ });+ if (error) {+ sendErrorToast(t`Failed to update workspace name`);+ } else {+ sendSuccessToast(t`Workspace name updated`);+ }+ };++ return (+ <PaneHeader+ title={+ <PaneHeaderInput+ data-testid="workspace-name-input"+ initialValue={workspace.name}+ onChange={handleNameChange}+ />+ }+ breadcrumbs={+ <DataStudioBreadcrumbs>+ <Link key="workspace-list" to={Urls.workspaceList()}>+ {t`Workspaces`}+ </Link>+ {workspace.name}+ </DataStudioBreadcrumbs>+ }+ menu={<WorkspaceHeaderMenu workspace={workspace} />}+ actions={actions}+ py={0}+ />+ );+}++type WorkspaceHeaderMenuProps = {+ workspace: Workspace;+};++function WorkspaceHeaderMenu({ workspace }: WorkspaceHeaderMenuProps) {+ const dispatch = useDispatch();+ const { handleDelete, modalContent } = useDeleteWorkspace({+ onSuccess: () => dispatch(push(Urls.workspaceList())),+ });++ return (+ <>+ <Menu>+ <Menu.Target>+ <ActionIcon size="sm" aria-label={t`Workspace actions`}>+ <FixedSizeIcon name="ellipsis" aria-hidden />+ </ActionIcon>+ </Menu.Target>+ <Menu.Dropdown>+ <Menu.Item+ leftSection={<FixedSizeIcon name="trash" aria-hidden />}+ onClick={() => handleDelete(workspace)}+ >+ {t`Delete`}+ </Menu.Item>+ </Menu.Dropdown>+ </Menu>+ {modalContent}+ </>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspaceHeader/WorkspaceHeader.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspaceHeader/WorkspaceHeader.unit.spec.tsxnew file mode 100644index 00000000000..ac6e1af9e7e--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspaceHeader/WorkspaceHeader.unit.spec.tsx@@ -0,0 +1,78 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";+import { Route } from "react-router";++import {+ setupDeleteWorkspaceEndpoint,+ setupUpdateWorkspaceEndpoint,+} from "__support__/server-mocks";+import { renderWithProviders, screen, waitFor } from "__support__/ui";+import { createMockWorkspace } from "metabase-types/api/mocks";++import { WorkspaceHeader } from "./WorkspaceHeader";++const WORKSPACE = createMockWorkspace({ id: 1, name: "My workspace" });++function setup() {+ setupUpdateWorkspaceEndpoint({ ...WORKSPACE, name: "Renamed" });+ setupDeleteWorkspaceEndpoint(WORKSPACE.id);++ renderWithProviders(+ <Route+ path="*"+ component={() => <WorkspaceHeader workspace={WORKSPACE} />}+ />,+ {+ withRouter: true,+ },+ );+}++describe("WorkspaceHeader", () => {+ it("renames the workspace via PUT when the title input changes", async () => {+ setup();++ await userEvent.click(screen.getByTestId("workspace-name-input"));+ const input = screen.getByRole("textbox");+ await userEvent.clear(input);+ await userEvent.type(input, "Renamed");+ await userEvent.tab();++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called(+ `path:/api/ee/workspace-manager/${WORKSPACE.id}`,+ { method: "PUT" },+ ),+ ).toBe(true);+ });+ const request = fetchMock.callHistory.lastCall(+ `path:/api/ee/workspace-manager/${WORKSPACE.id}`,+ { method: "PUT" },+ )?.request;+ expect(await request?.json()).toEqual({ name: "Renamed" });+ });++ it("deletes the workspace via DELETE after confirming from the menu", async () => {+ setup();++ await userEvent.click(+ screen.getByRole("button", { name: "Workspace actions" }),+ );+ await userEvent.click(+ await screen.findByRole("menuitem", { name: /Delete/ }),+ );+ await userEvent.click(+ await screen.findByRole("button", { name: "Delete workspace" }),+ );++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called(+ `path:/api/ee/workspace-manager/${WORKSPACE.id}`,+ { method: "DELETE" },+ ),+ ).toBe(true);+ });+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspaceHeader/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspaceHeader/index.tsnew file mode 100644index 00000000000..7f076e6913e--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspaceHeader/index.ts@@ -0,0 +1 @@+export * from "./WorkspaceHeader";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspacePage.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspacePage.tsxnew file mode 100644index 00000000000..ccb57e2ac2f--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspacePage.tsx@@ -0,0 +1,81 @@+import { useListDatabasesQuery } from "metabase/api";+import { DelayedLoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper/DelayedLoadingAndErrorWrapper";+import { PageContainer } from "metabase/data-studio/common/components/PageContainer";+import { Stack } from "metabase/ui";+import * as Urls from "metabase/urls";+import { useGetWorkspaceQuery } from "metabase-enterprise/api";+import type { Database, Workspace, WorkspaceId } from "metabase-types/api";++import { HelpMenu } from "../../components/HelpMenu";++import { DatabaseSection } from "./DatabaseSection";+import { SetupSection } from "./SetupSection";+import { WorkspaceHeader } from "./WorkspaceHeader";++type WorkspacePageParams = {+ workspaceId: string;+};++type WorkspacePageProps = {+ params: WorkspacePageParams;+};++export function WorkspacePage({ params }: WorkspacePageProps) {+ const workspaceId = Urls.extractEntityId(params.workspaceId);++ const {+ data: workspace,+ isLoading: isLoadingWorkspace,+ error: workspaceError,+ } = useGetWorkspaceQuery(workspaceId as WorkspaceId, {+ skip: workspaceId == null,+ });++ const {+ data: databasesResponse,+ isLoading: isLoadingDatabases,+ error: databasesError,+ } = useListDatabasesQuery();++ const isLoading = isLoadingWorkspace || isLoadingDatabases;+ const error = workspaceError ?? databasesError;++ if (+ isLoading ||+ error != null ||+ workspace == null ||+ databasesResponse == null+ ) {+ return <DelayedLoadingAndErrorWrapper loading={isLoading} error={error} />;+ }++ return (+ <WorkspacePageBody+ workspace={workspace}+ availableDatabases={databasesResponse.data}+ />+ );+}++type WorkspacePageBodyProps = {+ workspace: Workspace;+ availableDatabases: Database[];+};++function WorkspacePageBody({+ workspace,+ availableDatabases,+}: WorkspacePageBodyProps) {+ return (+ <PageContainer data-testid="workspace-page">+ <WorkspaceHeader workspace={workspace} actions={<HelpMenu />} />+ <Stack gap="3.5rem">+ <DatabaseSection+ workspace={workspace}+ availableDatabases={availableDatabases}+ />+ <SetupSection workspace={workspace} />+ </Stack>+ </PageContainer>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspacePage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspacePage.unit.spec.tsxnew file mode 100644index 00000000000..09c7d3b9bdb--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/WorkspacePage.unit.spec.tsx@@ -0,0 +1,87 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";+import { Route } from "react-router";++import {+ setupCreateWorkspaceDatabaseEndpoint,+ setupDatabasesEndpoints,+ setupGetWorkspaceEndpoint,+} from "__support__/server-mocks";+import { renderWithProviders, screen, waitFor, within } from "__support__/ui";+import {+ createMockDatabase,+ createMockTable,+ createMockWorkspace,+ createMockWorkspaceDatabase,+} from "metabase-types/api/mocks";++import { WorkspacePage } from "./WorkspacePage";++const POSTGRES = createMockDatabase({+ id: 10,+ name: "Postgres",+ features: ["schemas", "workspace"],+ tables: [+ createMockTable({ id: 100, db_id: 10, schema: "public", name: "orders" }),+ ],+});++const WORKSPACE = createMockWorkspace({ id: 1, name: "My workspace" });++function setup() {+ setupGetWorkspaceEndpoint(WORKSPACE);+ setupDatabasesEndpoints([POSTGRES]);+ setupCreateWorkspaceDatabaseEndpoint(+ createMockWorkspace({+ ...WORKSPACE,+ databases: [+ createMockWorkspaceDatabase({+ database_id: POSTGRES.id,+ input_schemas: ["public"],+ }),+ ],+ }),+ );++ renderWithProviders(+ <Route+ path="*"+ component={() => <WorkspacePage params={{ workspaceId: "1" }} />}+ />,+ {+ withRouter: true,+ },+ );+}++describe("WorkspacePage", () => {+ it("loads the workspace and lets you add a database", async () => {+ setup();++ expect(await screen.findByTestId("workspace-name-input")).toHaveValue(+ "My workspace",+ );++ await userEvent.click(screen.getByRole("button", { name: /Add database/ }));++ const dialog = await screen.findByRole("dialog");+ await userEvent.click(+ await within(dialog).findByLabelText("Schemas to include"),+ );+ await userEvent.click(+ await screen.findByRole("option", { name: "public" }),+ );+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Add database" }),+ );++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called(+ `path:/api/ee/workspace-manager/${WORKSPACE.id}/database`,+ { method: "POST" },+ ),+ ).toBe(true);+ });+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/index.tsnew file mode 100644index 00000000000..d625d77d99a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspacePage/index.ts@@ -0,0 +1 @@+export * from "./WorkspacePage";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/routes.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/routes.tsxnew file mode 100644index 00000000000..a99376268d6--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/routes.tsx@@ -0,0 +1,24 @@+import { IndexRoute, Route, type RouteComponent } from "react-router";++import { AdminConnectionInfoPage } from "./pages/AdminConnectionInfoPage";+import { WorkspaceInstancePage } from "./pages/WorkspaceInstancePage";+import { WorkspaceListPage } from "./pages/WorkspaceListPage";+import { WorkspacePage } from "./pages/WorkspacePage";++export function getDataStudioRoutes() {+ return (+ <Route path="workspaces">+ <IndexRoute component={WorkspaceListPage} />+ <Route path="instance" component={WorkspaceInstancePage} />+ <Route path=":workspaceId" component={WorkspacePage} />+ </Route>+ );+}++export function getAdminConnectionInfoRoutes(IsAdmin: RouteComponent) {+ return (+ <Route component={IsAdmin}>+ <Route path=":databaseId/admin" component={AdminConnectionInfoPage} />+ </Route>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/selectors.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/selectors.tsnew file mode 100644index 00000000000..41b37775acd--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/selectors.ts@@ -0,0 +1,14 @@+import type { State } from "metabase/redux/store";+import { getSetting } from "metabase/selectors/settings";+import { getUser, getUserIsAdmin } from "metabase/selectors/user";++export const canManageWorkspaces = (state: State): boolean => {+ if (getUserIsAdmin(state)) {+ return true;+ }+ const user = getUser(state);+ return user?.permissions?.can_manage_workspaces ?? false;+};++export const hasActiveWorkspace = (state: State): boolean =>+ getSetting(state, "has-active-workspace") ?? false;diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/utils.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/utils.tsnew file mode 100644index 00000000000..21e8268dc3d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/utils.ts@@ -0,0 +1,16 @@+import { hasFeature } from "metabase/common/utils/database";+import type { Database, DatabaseId } from "metabase-types/api";++export function getDatabasesById(+ databases: Database[],+): Map<DatabaseId, Database> {+ return new Map(databases.map((database) => [database.id, database]));+}++export function supportsWorkspaces(database: Database): boolean {+ return (+ hasFeature(database, "workspace") &&+ !database.is_sample &&+ !database.is_audit+ );+}diff --git a/frontend/src/metabase-types/api/database.ts b/frontend/src/metabase-types/api/database.tsindex fa94f61f35c..fe8431204da 100644--- a/frontend/src/metabase-types/api/database.ts+++ b/frontend/src/metabase-types/api/database.ts@@ -57,7 +57,8 @@ export type DatabaseFeature =| "split-part"| "collate"| "transforms/python"- | "transforms/table";+ | "transforms/table"+ | "workspace";export interface Database extends DatabaseData {id: DatabaseId;@@ -96,6 +97,7 @@ export interface DatabaseData {// [[metabase.models.interface/to-json]] for `:model/Database`:details?: Record<string, unknown>;write_data_details?: Record<string, unknown> | null;+ admin_details?: Record<string, unknown> | null;schedules: DatabaseSchedules;auto_run_queries: boolean | null;refingerprint: boolean | null;@@ -138,7 +140,7 @@ export type DatabaseLocalSettingAvailability =| { enabled: true }| { enabled: false; reasons: DatabaseLocalSettingDisableReason[] };-export type DatabaseConnectionType = "default" | "write-data";+export type DatabaseConnectionType = "default" | "write-data" | "admin";export type GetDatabaseHealthRequest = {id: DatabaseId;@@ -221,6 +223,7 @@ export interface UpdateDatabaseRequest {refingerprint?: boolean | null;details?: Record<string, unknown>;write_data_details?: Record<string, unknown> | null;+ admin_details?: Record<string, unknown> | null;schedules?: DatabaseSchedules;description?: string;caveats?: string;diff --git a/frontend/src/metabase-types/api/index.ts b/frontend/src/metabase-types/api/index.tsindex cfcd51237b3..928e0b319f5 100644--- a/frontend/src/metabase-types/api/index.ts+++ b/frontend/src/metabase-types/api/index.ts@@ -71,6 +71,8 @@ export * from "./util";export * from "./visualization";export * from "./visualization-settings";export * from "./visualizer";+export * from "./workspace";+export * from "./workspace-instance";// ISO8601 timestampexport type ISO8601Time = string;diff --git a/frontend/src/metabase-types/api/mocks/index.ts b/frontend/src/metabase-types/api/mocks/index.tsindex 5a43d0a81bd..701095a5f1f 100644--- a/frontend/src/metabase-types/api/mocks/index.ts+++ b/frontend/src/metabase-types/api/mocks/index.ts@@ -46,3 +46,5 @@ export * from "./timeline";export * from "./transform";export * from "./user";export * from "./util";+export * from "./workspace";+export * from "./workspace-instance";diff --git a/frontend/src/metabase-types/api/mocks/settings.ts b/frontend/src/metabase-types/api/mocks/settings.tsindex 0a54c56573f..00d721a0be6 100644--- a/frontend/src/metabase-types/api/mocks/settings.ts+++ b/frontend/src/metabase-types/api/mocks/settings.ts@@ -150,6 +150,7 @@ export const createMockTokenFeatures = (writable_connection: false,admin_security_center: false,ai_controls: false,+ workspaces: false,...opts,});@@ -275,6 +276,7 @@ export const createMockSettings = (* You probably don't want to set this directly in tests because it can get out* of sync with the database state (which should be a source of truth).*/+ "has-active-workspace": false,"has-sample-database?": undefined,"has-user-setup": true,"hide-embed-branding?": true,diff --git a/frontend/src/metabase-types/api/mocks/workspace-instance.ts b/frontend/src/metabase-types/api/mocks/workspace-instance.tsnew file mode 100644index 00000000000..1de45f92fa8--- /dev/null+++ b/frontend/src/metabase-types/api/mocks/workspace-instance.ts@@ -0,0 +1,42 @@+import type {+ TableRemapping,+ WorkspaceInstance,+ WorkspaceInstanceDatabase,+} from "../workspace-instance";++export function createMockWorkspaceInstanceDatabase(+ opts?: Partial<WorkspaceInstanceDatabase>,+): WorkspaceInstanceDatabase {+ return {+ input_schemas: [],+ output: { schema: null, db: null },+ ...opts,+ };+}++export function createMockWorkspaceInstance(+ opts?: Partial<WorkspaceInstance>,+): WorkspaceInstance {+ return {+ name: "Test workspace",+ databases: {},+ ...opts,+ };+}++export function createMockTableRemapping(+ opts?: Partial<TableRemapping>,+): TableRemapping {+ return {+ id: 1,+ database_id: 1,+ from_db: null,+ from_schema: null,+ from_table_name: "orders",+ to_db: null,+ to_schema: null,+ to_table_name: "orders",+ created_at: "2026-01-01T00:00:00Z",+ ...opts,+ };+}diff --git a/frontend/src/metabase-types/api/mocks/workspace.ts b/frontend/src/metabase-types/api/mocks/workspace.tsnew file mode 100644index 00000000000..fe5a407525b--- /dev/null+++ b/frontend/src/metabase-types/api/mocks/workspace.ts@@ -0,0 +1,23 @@+import type { Workspace, WorkspaceDatabase } from "../workspace";++export function createMockWorkspaceDatabase(+ opts?: Partial<WorkspaceDatabase>,+): WorkspaceDatabase {+ return {+ database_id: 1,+ input_schemas: [],+ status: "provisioned",+ ...opts,+ };+}++export function createMockWorkspace(opts?: Partial<Workspace>): Workspace {+ return {+ id: 1,+ name: "Test workspace",+ databases: [],+ created_at: "2026-01-01T00:00:00Z",+ creator_id: 1,+ ...opts,+ };+}diff --git a/frontend/src/metabase-types/api/permissions.ts b/frontend/src/metabase-types/api/permissions.tsindex df6313fb93e..0323a3bbe0b 100644--- a/frontend/src/metabase-types/api/permissions.ts+++ b/frontend/src/metabase-types/api/permissions.ts@@ -15,6 +15,7 @@ export enum DataPermission {DATA_MODEL = "data-model",DETAILS = "details",TRANSFORMS = "transforms",+ WORKSPACES = "workspaces",COLLECTIONS = "collections",}@@ -85,6 +86,10 @@ export type TransformsPermission =| DataPermissionValue.NO| DataPermissionValue.YES;+export type WorkspacesPermission =+ | DataPermissionValue.NO+ | DataPermissionValue.YES;+export type DatabasePermissions = {[DataPermission.VIEW_DATA]: SchemasPermissions;[DataPermission.CREATE_QUERIES]?: NativePermissions;@@ -92,6 +97,7 @@ export type DatabasePermissions = {[DataPermission.DOWNLOAD]?: DownloadAccessPermission;[DataPermission.DETAILS]?: DetailsPermissions;[DataPermission.TRANSFORMS]?: TransformsPermission;+ [DataPermission.WORKSPACES]?: WorkspacesPermission;};export type DataModelPermissions = {diff --git a/frontend/src/metabase-types/api/settings.ts b/frontend/src/metabase-types/api/settings.tsindex 631726d2a8e..74afea6c26c 100644--- a/frontend/src/metabase-types/api/settings.ts+++ b/frontend/src/metabase-types/api/settings.ts@@ -379,6 +379,7 @@ export const tokenFeatures = ["writable_connection","admin_security_center","ai_controls",+ "workspaces",] as const;export type TokenFeature = (typeof tokenFeatures)[number];@@ -738,6 +739,7 @@ export interface EnterpriseSettings extends Settings {"remote-sync-type"?: RemoteSyncType | null;"remote-sync-auto-import"?: boolean | null;"remote-sync-transforms"?: boolean | null;+ "has-active-workspace": boolean;"login-page-illustration"?: IllustrationSettingValue;"login-page-illustration-custom"?: string;"landing-page-illustration"?: IllustrationSettingValue;diff --git a/frontend/src/metabase-types/api/user.ts b/frontend/src/metabase-types/api/user.tsindex c9c47478f4a..78a6ba062e2 100644--- a/frontend/src/metabase-types/api/user.ts+++ b/frontend/src/metabase-types/api/user.ts@@ -58,6 +58,8 @@ export interface UserPermissions {can_access_subscription?: boolean;can_access_data_studio?: boolean;can_access_transforms?: boolean;+ can_access_workspaces?: boolean;+ can_manage_workspaces?: boolean;}export interface User extends BaseUser {diff --git a/frontend/src/metabase-types/api/workspace-instance.ts b/frontend/src/metabase-types/api/workspace-instance.tsnew file mode 100644index 00000000000..3a9ad694001--- /dev/null+++ b/frontend/src/metabase-types/api/workspace-instance.ts@@ -0,0 +1,30 @@+import type { DatabaseId } from "./database";++export type TableNamespace = {+ db?: string | null;+ schema?: string | null;+};++export type WorkspaceInstanceDatabase = {+ input_schemas: string[];+ output: TableNamespace;+};++export type WorkspaceInstance = {+ name: string;+ databases: Record<DatabaseId, WorkspaceInstanceDatabase>;+};++export type TableRemappingId = number;++export type TableRemapping = {+ id: TableRemappingId;+ database_id: DatabaseId;+ from_db: string | null;+ from_schema: string | null;+ from_table_name: string;+ to_db: string | null;+ to_schema: string | null;+ to_table_name: string;+ created_at: string;+};diff --git a/frontend/src/metabase-types/api/workspace.ts b/frontend/src/metabase-types/api/workspace.tsnew file mode 100644index 00000000000..c1f449e3b59--- /dev/null+++ b/frontend/src/metabase-types/api/workspace.ts@@ -0,0 +1,52 @@+import type { DatabaseId } from "./database";+import type { UserId, UserInfo } from "./user";++export type WorkspaceId = number;++export type WorkspaceDatabaseStatus =+ | "unprovisioned"+ | "provisioning"+ | "provisioned"+ | "deprovisioning";++export type WorkspaceDatabase = {+ database_id: DatabaseId;+ input_schemas: string[];+ status: WorkspaceDatabaseStatus;+};++export type Workspace = {+ id: WorkspaceId;+ name: string;+ databases: WorkspaceDatabase[];+ created_at: string;+ creator_id: UserId;++ creator?: UserInfo;+};++export type CreateWorkspaceRequest = {+ name: string;+};++export type UpdateWorkspaceRequest = {+ id: WorkspaceId;+ name?: string;+};++export type CreateWorkspaceDatabaseRequest = {+ id: WorkspaceId;+ database_id: DatabaseId;+ input_schemas: string[];+};++export type UpdateWorkspaceDatabaseRequest = {+ id: WorkspaceId;+ database_id: DatabaseId;+ input_schemas: string[];+};++export type DeleteWorkspaceDatabaseRequest = {+ id: WorkspaceId;+ database_id: DatabaseId;+};diff --git a/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx b/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsxindex 59172e569bb..74b4d284077 100644--- a/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx+++ b/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx@@ -20,6 +20,7 @@ import {PLUGIN_DATABASE_REPLICATION,PLUGIN_DB_ROUTING,PLUGIN_TABLE_EDITING,+ PLUGIN_WORKSPACES,PLUGIN_WRITABLE_CONNECTION,} from "metabase/plugins";import { connect, useSelector } from "metabase/redux";@@ -120,6 +121,10 @@ function DatabaseEditAppInner({database={database}/>+ <PLUGIN_WORKSPACES.AdminConnectionInfoSection+ database={database}+ />+<DatabaseModelFeaturesSectiondatabase={database}isModelPersistenceEnabled={isModelPersistenceEnabled}diff --git a/frontend/src/metabase/admin/permissions/components/DataPermissionsHelp/DataPermissionsHelp.tsx b/frontend/src/metabase/admin/permissions/components/DataPermissionsHelp/DataPermissionsHelp.tsxindex 7cf6a171e08..10ee749e6e8 100644--- a/frontend/src/metabase/admin/permissions/components/DataPermissionsHelp/DataPermissionsHelp.tsx+++ b/frontend/src/metabase/admin/permissions/components/DataPermissionsHelp/DataPermissionsHelp.tsx@@ -26,6 +26,10 @@ export const DataPermissionsHelp = () => {(state) => getSetting(state, "token-features").advanced_permissions,);+ const isWorkspacesFeatureEnabled = useSelector(+ (state) => getSetting(state, "token-features").workspaces,+ );+const shouldShowLegacyNoSelfServiceInfo = useSelector((state) =>hasPermissionValueInGraph(state.admin.permissions.originalDataPermissions,@@ -207,6 +211,13 @@ export const DataPermissionsHelp = () => {)} The group can create, edit, and run Transforms for a given database.`}</Text>)}+ {isWorkspacesFeatureEnabled && (+ <Text>+ {jt`${(+ <strong key="permission">{t`Workspaces:`}</strong>+ )} The group can add, edit, and remove the database from Workspaces.`}+ </Text>+ )}</Stack></Accordion.Panel></Accordion.Item>diff --git a/frontend/src/metabase/admin/permissions/permissions.ts b/frontend/src/metabase/admin/permissions/permissions.tsindex 1eff2bad6c9..d1fd8e25cbd 100644--- a/frontend/src/metabase/admin/permissions/permissions.ts+++ b/frontend/src/metabase/admin/permissions/permissions.ts@@ -12,6 +12,7 @@ import {inferAndUpdateEntityPermissions,restrictCreateQueriesPermissionsIfNeeded,revokeTransformsPermissionIfNeeded,+ revokeWorkspacesPermissionIfNeeded,updateFieldsPermission,updatePermission,updateSchemasPermission,@@ -631,6 +632,17 @@ const dataPermissions = createReducer<GroupsPermissions | null>();}+ if (permissionInfo.type === DataPermissionType.WORKSPACES) {+ return updatePermission(+ state,+ groupId,+ entityId.databaseId,+ DataPermission.WORKSPACES,+ [],+ value,+ );+ }+if (permissionInfo.type === DataPermissionType.NATIVE &&PLUGIN_DATA_PERMISSIONS.upgradeViewPermissionsIfNeeded@@ -662,6 +674,14 @@ const dataPermissions = createReducer<GroupsPermissions | null>(value,);+ state = revokeWorkspacesPermissionIfNeeded(+ state,+ groupId,+ entityId,+ permissionInfo.permission,+ value,+ );+if (isTableEntityId(entityId)) {const updatedPermissions = updateFieldsPermission(state,diff --git a/frontend/src/metabase/admin/permissions/selectors/data-permissions/fields.ts b/frontend/src/metabase/admin/permissions/selectors/data-permissions/fields.tsindex 9d6640449f1..7b6e7ea315f 100644--- a/frontend/src/metabase/admin/permissions/selectors/data-permissions/fields.ts+++ b/frontend/src/metabase/admin/permissions/selectors/data-permissions/fields.ts@@ -200,6 +200,7 @@ export const buildFieldsPermissions = ({defaultGroup,database,showTransformPermissions,+ showWorkspacesPermissions,}: {entityId: TableEntityId;groupId: number;@@ -209,6 +210,7 @@ export const buildFieldsPermissions = ({defaultGroup: Group;database: Database;showTransformPermissions: boolean;+ showWorkspacesPermissions: boolean;}): PermissionSectionConfig[] => {const isAdmin = groupType === "admin";@@ -246,6 +248,7 @@ export const buildFieldsPermissions = ({defaultGroup,permissionSubject: "fields",showTransformPermissions,+ showWorkspacesPermissions,}),]);};diff --git a/frontend/src/metabase/admin/permissions/selectors/data-permissions/permission-editor.tsx b/frontend/src/metabase/admin/permissions/selectors/data-permissions/permission-editor.tsxindex f592711d4e1..4e47fe767a0 100644--- a/frontend/src/metabase/admin/permissions/selectors/data-permissions/permission-editor.tsx+++ b/frontend/src/metabase/admin/permissions/selectors/data-permissions/permission-editor.tsx@@ -199,6 +199,17 @@ export const getShouldShowTransformPermissions = createSelector(},);+export const getShouldShowWorkspacesPermissions = createSelector(+ (state: State) => getPlan(getSetting(state, "token-features")),+ (state: State) => getTokenFeature(state, "workspaces"),+ (plan, workspacesFeatureEnabled) => {+ if (plan === "oss") {+ return false;+ }+ return Boolean(workspacesFeatureEnabled);+ },+);+export const getDatabasesPermissionEditor = createSelector(getMetadataWithHiddenTables,getGroupRouteParams,@@ -208,6 +219,7 @@ export const getDatabasesPermissionEditor = createSelector(selectGroupList,getIsLoadingDatabaseTables,getShouldShowTransformPermissions,+ getShouldShowWorkspacesPermissions,(metadata,params,@@ -217,6 +229,7 @@ export const getDatabasesPermissionEditor = createSelector(groups: Group[],isLoading,showTransformPermissions,+ showWorkspacesPermissions,): PermissionEditorType | null => {const { groupId, databaseId, schemaName } = params;@@ -272,6 +285,7 @@ export const getDatabasesPermissionEditor = createSelector(defaultGroup: isExternal ? externalUsersGroup : defaultGroup,database,showTransformPermissions,+ showWorkspacesPermissions,}),};});@@ -296,6 +310,7 @@ export const getDatabasesPermissionEditor = createSelector(defaultGroup: isExternal ? externalUsersGroup : defaultGroup,database,showTransformPermissions,+ showWorkspacesPermissions,}),};});@@ -329,6 +344,7 @@ export const getDatabasesPermissionEditor = createSelector(database,permissionView: "group",showTransformPermissions,+ showWorkspacesPermissions,}),};});@@ -346,6 +362,7 @@ export const getDatabasesPermissionEditor = createSelector(groupType,isExternal,showTransformPermissions,+ showWorkspacesPermissions,}): []),]);@@ -399,6 +416,7 @@ export const getGroupsDataPermissionEditor: GetGroupsDataPermissionEditorSelectogetOriginalDataPermissions,getOrderedGroups,getShouldShowTransformPermissions,+ getShouldShowWorkspacesPermissions,(metadata,params,@@ -406,6 +424,7 @@ export const getGroupsDataPermissionEditor: GetGroupsDataPermissionEditorSelectooriginalPermissions,groups,showTransformPermissions,+ showWorkspacesPermissions,) => {const { databaseId, schemaName, tableId } = params;const database = metadata?.database(databaseId);@@ -457,6 +476,7 @@ export const getGroupsDataPermissionEditor: GetGroupsDataPermissionEditorSelecto: defaultGroup,database,showTransformPermissions,+ showWorkspacesPermissions,});} else if (schemaName != null) {groupPermissions = buildTablesPermissions({@@ -473,6 +493,7 @@ export const getGroupsDataPermissionEditor: GetGroupsDataPermissionEditorSelecto: defaultGroup,database,showTransformPermissions,+ showWorkspacesPermissions,});} else if (databaseId != null) {groupPermissions = buildSchemasPermissions({@@ -489,6 +510,7 @@ export const getGroupsDataPermissionEditor: GetGroupsDataPermissionEditorSelectodatabase,permissionView: "database",showTransformPermissions,+ showWorkspacesPermissions,});}@@ -517,6 +539,7 @@ export const getGroupsDataPermissionEditor: GetGroupsDataPermissionEditorSelecto...PLUGIN_FEATURE_LEVEL_PERMISSIONS.getDataColumns({subject: permissionSubject,showTransformPermissions,+ showWorkspacesPermissions,}),]);diff --git a/frontend/src/metabase/admin/permissions/selectors/data-permissions/schemas.ts b/frontend/src/metabase/admin/permissions/selectors/data-permissions/schemas.tsindex 2dc3a5a9ef7..2cea46a0ba4 100644--- a/frontend/src/metabase/admin/permissions/selectors/data-permissions/schemas.ts+++ b/frontend/src/metabase/admin/permissions/selectors/data-permissions/schemas.ts@@ -202,6 +202,7 @@ export const buildSchemasPermissions = ({database,permissionView,showTransformPermissions,+ showWorkspacesPermissions,}: {entityId: DatabaseEntityId;groupId: number;@@ -212,6 +213,7 @@ export const buildSchemasPermissions = ({database: Database;permissionView: "group" | "database";showTransformPermissions: boolean;+ showWorkspacesPermissions: boolean;}): PermissionSectionConfig[] => {const isAdmin = groupType === "admin";@@ -252,6 +254,7 @@ export const buildSchemasPermissions = ({permissionSubject: "schemas",permissionView,showTransformPermissions,+ showWorkspacesPermissions,}),]);};diff --git a/frontend/src/metabase/admin/permissions/selectors/data-permissions/tables.ts b/frontend/src/metabase/admin/permissions/selectors/data-permissions/tables.tsindex 4df59a7f113..6ee6e63a373 100644--- a/frontend/src/metabase/admin/permissions/selectors/data-permissions/tables.ts+++ b/frontend/src/metabase/admin/permissions/selectors/data-permissions/tables.ts@@ -193,6 +193,7 @@ export const buildTablesPermissions = ({defaultGroup,database,showTransformPermissions,+ showWorkspacesPermissions,}: {entityId: SchemaEntityId;groupId: number;@@ -202,6 +203,7 @@ export const buildTablesPermissions = ({defaultGroup: Group;database: Database;showTransformPermissions: boolean;+ showWorkspacesPermissions: boolean;}): PermissionSectionConfig[] => {const isAdmin = groupType === "admin";@@ -240,6 +242,7 @@ export const buildTablesPermissions = ({defaultGroup,permissionSubject: "tables",showTransformPermissions,+ showWorkspacesPermissions,}),]);};diff --git a/frontend/src/metabase/admin/permissions/types.ts b/frontend/src/metabase/admin/permissions/types.tsindex ea845cf88c0..86cd7d4d4ce 100644--- a/frontend/src/metabase/admin/permissions/types.ts+++ b/frontend/src/metabase/admin/permissions/types.ts@@ -77,6 +77,7 @@ export enum DataPermissionType {DOWNLOAD = "download",DATA_MODEL = "data-model",TRANSFORMS = "transforms",+ WORKSPACES = "workspaces",COLLECTIONS = "collections",}diff --git a/frontend/src/metabase/admin/permissions/utils/graph/data-permissions/get.ts b/frontend/src/metabase/admin/permissions/utils/graph/data-permissions/get.tsindex 256d9f314e5..0d030b717ce 100644--- a/frontend/src/metabase/admin/permissions/utils/graph/data-permissions/get.ts+++ b/frontend/src/metabase/admin/permissions/utils/graph/data-permissions/get.ts@@ -19,6 +19,7 @@ const flatPermissions = new Set([DataPermission.VIEW_DATA,DataPermission.CREATE_QUERIES,DataPermission.TRANSFORMS,+ DataPermission.WORKSPACES,]);// util to ease migration of perms attributes into a flatter structure@@ -44,6 +45,7 @@ const omittedDefaultValues: Record<DataPermission, DataPermissionValue> = {[DataPermission.DATA_MODEL]: DataPermissionValue.NONE,[DataPermission.DETAILS]: DataPermissionValue.NO,[DataPermission.TRANSFORMS]: DataPermissionValue.NO,+ [DataPermission.WORKSPACES]: DataPermissionValue.NO,[DataPermission.COLLECTIONS]: DataPermissionValue.NONE,};diff --git a/frontend/src/metabase/admin/permissions/utils/graph/data-permissions/update.ts b/frontend/src/metabase/admin/permissions/utils/graph/data-permissions/update.tsindex ef73d3ef547..ee004f31105 100644--- a/frontend/src/metabase/admin/permissions/utils/graph/data-permissions/update.ts+++ b/frontend/src/metabase/admin/permissions/utils/graph/data-permissions/update.ts@@ -331,6 +331,60 @@ export function revokeTransformsPermissionIfNeeded(return permissions;}+export function revokeWorkspacesPermissionIfNeeded(+ permissions: GroupsPermissions,+ groupId: number,+ entityId: EntityId,+ permission: DataPermission,+ value: DataPermissionValue,+): GroupsPermissions {+ if (+ permission !== DataPermission.VIEW_DATA &&+ permission !== DataPermission.CREATE_QUERIES+ ) {+ return permissions;+ }++ const { databaseId } = entityId;++ const viewDataValue =+ permission === DataPermission.VIEW_DATA+ ? value+ : getSchemasPermission(+ permissions,+ groupId,+ { databaseId },+ DataPermission.VIEW_DATA,+ );++ const createQueriesValue =+ permission === DataPermission.CREATE_QUERIES+ ? value+ : getSchemasPermission(+ permissions,+ groupId,+ { databaseId },+ DataPermission.CREATE_QUERIES,+ );++ const hasRequiredPermissions =+ hasFullViewDataAccess(viewDataValue) &&+ hasFullCreateQueriesAccess(createQueriesValue);++ if (!hasRequiredPermissions) {+ return updatePermission(+ permissions,+ groupId,+ databaseId,+ DataPermission.WORKSPACES,+ [],+ DataPermissionValue.NO,+ );+ }++ return permissions;+}+function inferEntityPermissionValueFromChildTables(permissions: GroupsPermissions,groupId: number,diff --git a/frontend/src/metabase/admin/routes.tsx b/frontend/src/metabase/admin/routes.tsxindex ecac8be9bb1..d82f9cb2185 100644--- a/frontend/src/metabase/admin/routes.tsx+++ b/frontend/src/metabase/admin/routes.tsx@@ -61,6 +61,7 @@ import {PLUGIN_SECURITY_CENTER,PLUGIN_SUPPORT,PLUGIN_TENANTS,+ PLUGIN_WORKSPACES,PLUGIN_WRITABLE_CONNECTION,} from "metabase/plugins";import type { State } from "metabase/redux/store";@@ -103,6 +104,7 @@ export const getRoutes = (</Route><Route path=":databaseId/edit" component={DatabasePage} />{PLUGIN_WRITABLE_CONNECTION.getWritableConnectionInfoRoutes(IsAdmin)}+ {PLUGIN_WORKSPACES.getAdminConnectionInfoRoutes(IsAdmin)}<Route path=":databaseId" component={DatabaseEditApp}>{PLUGIN_DB_ROUTING.getDestinationDatabaseRoutes(IsAdmin)}</Route>diff --git a/frontend/src/metabase/api/tags/constants.ts b/frontend/src/metabase/api/tags/constants.tsindex f80a114cc2d..5bdca66c3db 100644--- a/frontend/src/metabase/api/tags/constants.ts+++ b/frontend/src/metabase/api/tags/constants.ts@@ -45,6 +45,7 @@ export const TAG_TYPES = ["subscription","subscription-channel","table",+ "table-remapping","task","task-run","timeline",diff --git a/frontend/src/metabase/api/tags/utils.ts b/frontend/src/metabase/api/tags/utils.tsindex 034333ade45..acb86a8b2d1 100644--- a/frontend/src/metabase/api/tags/utils.ts+++ b/frontend/src/metabase/api/tags/utils.ts@@ -45,6 +45,7 @@ import type {SearchResult,Segment,Table,+ TableRemapping,Task,TaskRun,Timeline,@@ -648,6 +649,21 @@ export function provideTableTags(table: Table): TagDescription<TagType>[] {];}+export function provideTableRemappingTags(+ remapping: TableRemapping,+): TagDescription<TagType>[] {+ return [idTag("table-remapping", remapping.id)];+}++export function provideTableRemappingListTags(+ remappings: TableRemapping[],+): TagDescription<TagType>[] {+ return [+ listTag("table-remapping"),+ ...remappings.flatMap(provideTableRemappingTags),+ ];+}+export function provideTaskListTags(tasks: Task[]): TagDescription<TagType>[] {return [listTag("task"), ...tasks.flatMap(provideTaskTags)];}diff --git a/frontend/src/metabase/api/transform-job.ts b/frontend/src/metabase/api/transform-job.tsindex 0554420f7e2..0172e67c707 100644--- a/frontend/src/metabase/api/transform-job.ts+++ b/frontend/src/metabase/api/transform-job.ts@@ -55,6 +55,7 @@ export const transformJobApi = Api.injectEndpoints({idTag("transform-job", id),tag("transform"),tag("table"),+ listTag("table-remapping"),]),onQueryStarted: async (id, { dispatch, queryFulfilled }) => {const patchResult = dispatch(diff --git a/frontend/src/metabase/api/transform.ts b/frontend/src/metabase/api/transform.tsindex c603fa57b75..82c2ffda4cd 100644--- a/frontend/src/metabase/api/transform.ts+++ b/frontend/src/metabase/api/transform.ts@@ -66,6 +66,7 @@ export const transformApi = Api.injectEndpoints({invalidateTags(error, [idTag("transform", id),tag("table"),+ listTag("table-remapping"),listTag("transform-run"),]),onQueryStarted: async (id, { dispatch, queryFulfilled }) => {diff --git a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/DataStudioLayout.tsx b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/DataStudioLayout.tsxindex 10f69f4b642..25d49ab68cd 100644--- a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/DataStudioLayout.tsx+++ b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/DataStudioLayout.tsx@@ -11,6 +11,7 @@ import { useRegisterShortcut } from "metabase/palette/hooks/useRegisterShortcut"import {PLUGIN_FEATURE_LEVEL_PERMISSIONS,PLUGIN_REMOTE_SYNC,+ PLUGIN_WORKSPACES,} from "metabase/plugins";import { useSelector } from "metabase/redux";import { getLocation } from "metabase/selectors/routing";@@ -90,6 +91,10 @@ function DataStudioNav({ isNavbarOpened, onNavbarToggle }: DataStudioNavProps) {PLUGIN_FEATURE_LEVEL_PERMISSIONS.canAccessDataModel,);const canAccessTransforms = useSelector(canAccessTransformsSelector);+ const canManageWorkspaces = useSelector(+ PLUGIN_WORKSPACES.canManageWorkspaces,+ );+ const hasActiveWorkspace = useSelector(PLUGIN_WORKSPACES.hasActiveWorkspace);const hasDirtyChanges = PLUGIN_REMOTE_SYNC.useHasLibraryDirtyChanges();const hasTransformDirtyChanges =PLUGIN_REMOTE_SYNC.useHasTransformDirtyChanges();@@ -196,6 +201,24 @@ function DataStudioNav({ isNavbarOpened, onNavbarToggle }: DataStudioNavProps) {isGated/>)}+ {hasActiveWorkspace && (+ <DataStudioTab+ label={t`Workspace`}+ icon="folder"+ to={Urls.workspaceInstance()}+ isSelected={currentTab === "workspaces"}+ showLabel={isNavbarOpened}+ />+ )}+ {!hasActiveWorkspace && canManageWorkspaces && (+ <DataStudioTab+ label={t`Workspaces`}+ icon="folder"+ to={Urls.workspaceList()}+ isSelected={currentTab === "workspaces"}+ showLabel={isNavbarOpened}+ />+ )}{canAccessTransforms && (<DataStudioTablabel={t`Jobs`}diff --git a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/utils.ts b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/utils.tsindex 7326726d621..365725c955b 100644--- a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/utils.ts+++ b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/utils.ts@@ -9,7 +9,8 @@ type TabName =| "dependencies"| "dependency-diagnostics"| "glossary"- | "git-sync";+ | "git-sync"+ | "workspaces";export const getCurrentTab = (pathname: string): TabName => {switch (true) {@@ -17,6 +18,8 @@ export const getCurrentTab = (pathname: string): TabName => {return "glossary";case pathname.startsWith(Urls.dataStudioGitSync()):return "git-sync";+ case pathname.startsWith(Urls.workspaceList()):+ return "workspaces";case pathname.startsWith(Urls.transformJobList()):return "jobs";case pathname.startsWith(Urls.dependencyGraph()):diff --git a/frontend/src/metabase/data-studio/app/pages/WorkspacesSectionLayout/WorkspacesSectionLayout.tsx b/frontend/src/metabase/data-studio/app/pages/WorkspacesSectionLayout/WorkspacesSectionLayout.tsxnew file mode 100644index 00000000000..a86a11b62a7--- /dev/null+++ b/frontend/src/metabase/data-studio/app/pages/WorkspacesSectionLayout/WorkspacesSectionLayout.tsx@@ -0,0 +1,18 @@+import type { ReactNode } from "react";+import { t } from "ttag";++import { usePageTitle } from "metabase/hooks/use-page-title";++import { SectionLayout } from "../../components/SectionLayout";++type WorkspacesSectionLayoutProps = {+ children?: ReactNode;+};++export function WorkspacesSectionLayout({+ children,+}: WorkspacesSectionLayoutProps) {+ usePageTitle(t`Workspaces`, { titleIndex: 1 });++ return <SectionLayout>{children}</SectionLayout>;+}diff --git a/frontend/src/metabase/data-studio/app/pages/WorkspacesSectionLayout/index.ts b/frontend/src/metabase/data-studio/app/pages/WorkspacesSectionLayout/index.tsnew file mode 100644index 00000000000..85d3fb9745d--- /dev/null+++ b/frontend/src/metabase/data-studio/app/pages/WorkspacesSectionLayout/index.ts@@ -0,0 +1 @@+export * from "./WorkspacesSectionLayout";diff --git a/frontend/src/metabase/transforms/components/TitleSection/TitleSection.tsx b/frontend/src/metabase/data-studio/common/components/TitleSection/TitleSection.tsxsimilarity index 72%rename from frontend/src/metabase/transforms/components/TitleSection/TitleSection.tsxrename to frontend/src/metabase/data-studio/common/components/TitleSection/TitleSection.tsxindex e245178be82..64be65c373b 100644--- a/frontend/src/metabase/transforms/components/TitleSection/TitleSection.tsx+++ b/frontend/src/metabase/data-studio/common/components/TitleSection/TitleSection.tsx@@ -3,22 +3,24 @@ import type { ReactNode } from "react";import { Card, Group, Stack, Text, Title } from "metabase/ui";type TitleSectionProps = {- label: string;+ label: ReactNode;description?: ReactNode;children?: ReactNode;+ "data-testid"?: string;};export function TitleSection({label,description,children,+ "data-testid": dataTestId,}: TitleSectionProps) {return (- <Stack>+ <Stack data-testid={dataTestId}><Group><Stack flex={1} gap="sm"><Title order={4}>{label}</Title>- <Text c="text-secondary">{description}</Text>+ {description != null && <Text c="text-secondary">{description}</Text>}</Stack></Group><Card p={0} shadow="none" withBorder>diff --git a/frontend/src/metabase/transforms/components/TitleSection/TitleSection.unit.spec.tsx b/frontend/src/metabase/data-studio/common/components/TitleSection/TitleSection.unit.spec.tsxsimilarity index 91%rename from frontend/src/metabase/transforms/components/TitleSection/TitleSection.unit.spec.tsxrename to frontend/src/metabase/data-studio/common/components/TitleSection/TitleSection.unit.spec.tsxindex c9565c71193..4e1001d57ce 100644--- a/frontend/src/metabase/transforms/components/TitleSection/TitleSection.unit.spec.tsx+++ b/frontend/src/metabase/data-studio/common/components/TitleSection/TitleSection.unit.spec.tsx@@ -14,7 +14,7 @@ function setup({ label = "Default Title", children }: SetupOpts = {}) {}describe("TitleSection", () => {- it("should render label, rightSection, and children", () => {+ it("should render label and children", () => {const label = "Custom Title Label";const children = <div>Title section content</div>;diff --git a/frontend/src/metabase/transforms/components/TitleSection/index.ts b/frontend/src/metabase/data-studio/common/components/TitleSection/index.tssimilarity index 100%rename from frontend/src/metabase/transforms/components/TitleSection/index.tsrename to frontend/src/metabase/data-studio/common/components/TitleSection/index.tsdiff --git a/frontend/src/metabase/data-studio/routes.tsx b/frontend/src/metabase/data-studio/routes.tsxindex bc43389d9e3..218783d8258 100644--- a/frontend/src/metabase/data-studio/routes.tsx+++ b/frontend/src/metabase/data-studio/routes.tsx@@ -6,6 +6,7 @@ import {PLUGIN_DEPENDENCIES,PLUGIN_FEATURE_LEVEL_PERMISSIONS,PLUGIN_LIBRARY,+ PLUGIN_WORKSPACES,} from "metabase/plugins";import type { State } from "metabase/redux/store";import { getDataStudioTransformRoutes } from "metabase/transforms/routes";@@ -18,6 +19,7 @@ import { DependenciesSectionLayout } from "./app/pages/DependenciesSectionLayoutimport { DependencyDiagnosticsSectionLayout } from "./app/pages/DependencyDiagnosticsSectionLayout";import { GitSyncSectionLayout } from "./app/pages/GitSyncSectionLayout";import { TransformsSectionLayout } from "./app/pages/TransformsSectionLayout";+import { WorkspacesSectionLayout } from "./app/pages/WorkspacesSectionLayout";import { getDataStudioMetadataRoutes } from "./data-model/routes";import { getDataStudioGlossaryRoutes } from "./glossary/routes";import {@@ -49,6 +51,9 @@ export function getDataStudioRoutes(<Route path="transforms" component={TransformsSectionLayout}>{getDataStudioTransformRoutes()}</Route>+ <Route component={WorkspacesSectionLayout}>+ {PLUGIN_WORKSPACES.getDataStudioRoutes()}+ </Route>{getDataStudioGlossaryRoutes()}{PLUGIN_LIBRARY.isEnabled ? (PLUGIN_LIBRARY.getDataStudioLibraryRoutes(IsAdmin)diff --git a/frontend/src/metabase/forms/components/FormMultiSelect/FormMultiSelect.tsx b/frontend/src/metabase/forms/components/FormMultiSelect/FormMultiSelect.tsxnew file mode 100644index 00000000000..d62bcf90ed0--- /dev/null+++ b/frontend/src/metabase/forms/components/FormMultiSelect/FormMultiSelect.tsx@@ -0,0 +1,49 @@+import { useField } from "formik";+import type { FocusEvent, Ref } from "react";+import { forwardRef, useCallback } from "react";++import type { MultiSelectProps } from "metabase/ui";+import { MultiSelect } from "metabase/ui";++export interface FormMultiSelectProps extends Omit<+ MultiSelectProps,+ "value" | "error"+> {+ name: string;+}++export const FormMultiSelect = forwardRef(function FormMultiSelect(+ { name, onChange, onBlur, ...props }: FormMultiSelectProps,+ ref: Ref<HTMLInputElement>,+) {+ const [{ value }, { error, touched }, { setValue, setTouched }] =+ useField(name);++ const handleChange = useCallback(+ (newValue: string[]) => {+ setValue(newValue);+ onChange?.(newValue);+ },+ [setValue, onChange],+ );++ const handleBlur = useCallback(+ (event: FocusEvent<HTMLInputElement>) => {+ setTouched(true);+ onBlur?.(event);+ },+ [setTouched, onBlur],+ );++ return (+ <MultiSelect+ {...props}+ ref={ref}+ name={name}+ value={value ?? []}+ error={touched ? error : null}+ onChange={handleChange}+ onBlur={handleBlur}+ />+ );+});diff --git a/frontend/src/metabase/forms/components/FormMultiSelect/index.ts b/frontend/src/metabase/forms/components/FormMultiSelect/index.tsnew file mode 100644index 00000000000..19f145f28c6--- /dev/null+++ b/frontend/src/metabase/forms/components/FormMultiSelect/index.ts@@ -0,0 +1 @@+export * from "./FormMultiSelect";diff --git a/frontend/src/metabase/forms/components/index.ts b/frontend/src/metabase/forms/components/index.tsindex ec6be4f174b..cc1a4e83498 100644--- a/frontend/src/metabase/forms/components/index.ts+++ b/frontend/src/metabase/forms/components/index.ts@@ -8,6 +8,7 @@ export * from "./FormField";export * from "./FormGroupWidget";export * from "./FormInlineUpdater";export * from "./FormMessage";+export * from "./FormMultiSelect";export * from "./FormNumberInput";export * from "./FormObserver";export * from "./FormProvider";diff --git a/frontend/src/metabase/plugins/index.ts b/frontend/src/metabase/plugins/index.tsindex b930f813374..52f5023845f 100644--- a/frontend/src/metabase/plugins/index.ts+++ b/frontend/src/metabase/plugins/index.ts@@ -147,6 +147,10 @@ export {PLUGIN_WRITABLE_CONNECTION,type WritableConnectionInfoSectionProps,} from "./oss/writable-connection";+export {+ PLUGIN_WORKSPACES,+ type AdminConnectionInfoSectionProps,+} from "./oss/workspaces";export { PLUGIN_SECURITY_CENTER } from "./oss/security-center";export { PLUGIN_AI_CONTROLS, type AiControlsPlugin } from "./oss/ai-controls";export { PLUGIN_SUPPORT } from "./oss/support";@@ -198,6 +202,7 @@ import { reinitialize as reinitializeTenants } from "./oss/tenants";import { reinitialize as reinitializeTransforms } from "./oss/transforms";import { reinitialize as reinitializeUploadManagement } from "./oss/upload-management";import { reinitialize as reinitializeWhitelabel } from "./oss/whitelabel";+import { reinitialize as reinitializeWorkspaces } from "./oss/workspaces";import { reinitialize as reinitializeWritableConnection } from "./oss/writable-connection";/*** Mostly for test purposes, reinitialize all plugins.@@ -242,5 +247,6 @@ export function reinitialize() {reinitializeTransforms();reinitializeUploadManagement();reinitializeWhitelabel();+ reinitializeWorkspaces();reinitializeWritableConnection();}diff --git a/frontend/src/metabase/plugins/oss/permissions.ts b/frontend/src/metabase/plugins/oss/permissions.tsindex 07df81e5e51..50ba8f61969 100644--- a/frontend/src/metabase/plugins/oss/permissions.ts+++ b/frontend/src/metabase/plugins/oss/permissions.ts@@ -142,6 +142,7 @@ const getDefaultFeatureLevelPermissions = () => ({permissionSubject: _permissionSubject,permissionView: _permissionView,showTransformPermissions: _showTransformPermissions,+ showWorkspacesPermissions: _showWorkspacesPermissions,}: {entityId: DatabaseEntityId;groupId: number;@@ -152,6 +153,7 @@ const getDefaultFeatureLevelPermissions = () => ({permissionSubject: PermissionSubject;permissionView?: "group" | "database";showTransformPermissions?: boolean;+ showWorkspacesPermissions?: boolean;}) => {return [] as any;},@@ -160,11 +162,13 @@ const getDefaultFeatureLevelPermissions = () => ({groupType: _groupType,isExternal: _isExternal,showTransformPermissions: _showTransformPermissions,+ showWorkspacesPermissions: _showWorkspacesPermissions,}: {subject: PermissionSubject;groupType?: SpecialGroupType;isExternal?: boolean;showTransformPermissions?: boolean;+ showWorkspacesPermissions?: boolean;}) => [] as any,getDownloadWidgetMessageOverride: (_result: Dataset): string | null => null,canDownloadResults: (_result: Dataset): boolean => true,diff --git a/frontend/src/metabase/plugins/oss/workspaces.ts b/frontend/src/metabase/plugins/oss/workspaces.tsnew file mode 100644index 00000000000..98e2b4457d4--- /dev/null+++ b/frontend/src/metabase/plugins/oss/workspaces.ts@@ -0,0 +1,29 @@+import type { ReactNode } from "react";+import type { RouteComponent } from "react-router";++import type { State } from "metabase/redux/store";+import type { Database } from "metabase-types/api";++import { PluginPlaceholder } from "../components/PluginPlaceholder";++export type AdminConnectionInfoSectionProps = {+ database: Database;+};++const getDefaultWorkspaces = () => ({+ canManageWorkspaces: (_state: State): boolean => false,+ hasActiveWorkspace: (_state: State): boolean => false,+ getDataStudioRoutes: (): ReactNode => null,+ getAdminConnectionInfoRoutes: (_IsAdmin: RouteComponent): ReactNode => null,+ AdminConnectionInfoSection:+ PluginPlaceholder<AdminConnectionInfoSectionProps>,+});++export const PLUGIN_WORKSPACES = getDefaultWorkspaces();++/**+ * @internal Do not call directly. Use the main reinitialize function from metabase/plugins instead.+ */+export function reinitialize() {+ Object.assign(PLUGIN_WORKSPACES, getDefaultWorkspaces());+}diff --git a/frontend/src/metabase/transforms/components/IncrementalTransform/IncrementalTransformSettings.tsx b/frontend/src/metabase/transforms/components/IncrementalTransform/IncrementalTransformSettings.tsxindex 47ba8a7a8d5..bcbfb89d030 100644--- a/frontend/src/metabase/transforms/components/IncrementalTransform/IncrementalTransformSettings.tsx+++ b/frontend/src/metabase/transforms/components/IncrementalTransform/IncrementalTransformSettings.tsx@@ -2,11 +2,11 @@ import { useFormikContext } from "formik";import { t } from "ttag";import { useDocsUrl } from "metabase/common/hooks";+import { TitleSection } from "metabase/data-studio/common/components/TitleSection";import { FormSelect } from "metabase/forms";import { PLUGIN_REMOTE_SYNC } from "metabase/plugins";import { useSelector } from "metabase/redux";import { getMetadata } from "metabase/selectors/metadata";-import { TitleSection } from "metabase/transforms/components/TitleSection";import {SOURCE_STRATEGY_OPTIONS,TARGET_STRATEGY_OPTIONS,diff --git a/frontend/src/metabase/transforms/components/JobEditor/ScheduleSection/ScheduleSection.tsx b/frontend/src/metabase/transforms/components/JobEditor/ScheduleSection/ScheduleSection.tsxindex 2d6ac6dabeb..9c63056588c 100644--- a/frontend/src/metabase/transforms/components/JobEditor/ScheduleSection/ScheduleSection.tsx+++ b/frontend/src/metabase/transforms/components/JobEditor/ScheduleSection/ScheduleSection.tsx@@ -3,6 +3,7 @@ import { c, t } from "ttag";import { useRunTransformJobMutation } from "metabase/api";import { Schedule } from "metabase/common/components/Schedule";import { useSetting } from "metabase/common/hooks";+import { TitleSection } from "metabase/data-studio/common/components/TitleSection";import { useMetadataToasts } from "metabase/metadata/hooks";import { Box, Divider, Group, Tooltip } from "metabase/ui";import { getScheduleExplanation } from "metabase/utils/cron";@@ -15,7 +16,6 @@ import type {import { trackTransformJobTriggerManualRun } from "../../../analytics";import { RunButton } from "../../RunButton";import { RunStatus } from "../../RunStatus";-import { TitleSection } from "../../TitleSection";import type { TransformJobInfo } from "../types";type ScheduleSectionProps = {diff --git a/frontend/src/metabase/transforms/components/JobEditor/TagSection/TagSection.tsx b/frontend/src/metabase/transforms/components/JobEditor/TagSection/TagSection.tsxindex 347622de99f..c9e07141d16 100644--- a/frontend/src/metabase/transforms/components/JobEditor/TagSection/TagSection.tsx+++ b/frontend/src/metabase/transforms/components/JobEditor/TagSection/TagSection.tsx@@ -1,10 +1,10 @@import { t } from "ttag";+import { TitleSection } from "metabase/data-studio/common/components/TitleSection";import { Box } from "metabase/ui";import type { TransformTagId } from "metabase-types/api";import { TagMultiSelect } from "../../TagMultiSelect";-import { TitleSection } from "../../TitleSection";import type { TransformJobInfo } from "../types";type TagSectionProps = {diff --git a/frontend/src/metabase/transforms/components/JobEditor/TransformsSection/TransformsSection.tsx b/frontend/src/metabase/transforms/components/JobEditor/TransformsSection/TransformsSection.tsxindex 610df5338fb..0820b5ddacf 100644--- a/frontend/src/metabase/transforms/components/JobEditor/TransformsSection/TransformsSection.tsx+++ b/frontend/src/metabase/transforms/components/JobEditor/TransformsSection/TransformsSection.tsx@@ -7,13 +7,12 @@ import { useListTransformJobTransformsQuery } from "metabase/api";import { ListEmptyState } from "metabase/common/components/ListEmptyState";import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";import CS from "metabase/css/core/index.css";+import { TitleSection } from "metabase/data-studio/common/components/TitleSection";import { useDispatch } from "metabase/redux";import { Card, TreeTable, useTreeTableInstance } from "metabase/ui";import * as Urls from "metabase/urls";import type { Transform, TransformJobId } from "metabase-types/api";-import { TitleSection } from "../../TitleSection";-import { getColumns } from "./utils";type TransformsSectionProps = {diff --git a/frontend/src/metabase/transforms/pages/TransformRunPage/RunSection/RunSection.tsx b/frontend/src/metabase/transforms/pages/TransformRunPage/RunSection/RunSection.tsxindex bd03e2d0f39..09960f4ba5d 100644--- a/frontend/src/metabase/transforms/pages/TransformRunPage/RunSection/RunSection.tsx+++ b/frontend/src/metabase/transforms/pages/TransformRunPage/RunSection/RunSection.tsx@@ -9,6 +9,7 @@ import {useUpdateTransformMutation,} from "metabase/api";import { ConfirmModal } from "metabase/common/components/ConfirmModal";+import { TitleSection } from "metabase/data-studio/common/components/TitleSection";import { useMetadataToasts } from "metabase/metadata/hooks";import { PLUGIN_REMOTE_SYNC } from "metabase/plugins";import { useSelector } from "metabase/redux";@@ -24,7 +25,6 @@ import {import { RunButton } from "../../../components/RunButton";import { RunStatus } from "../../../components/RunStatus";import { TagMultiSelect } from "../../../components/TagMultiSelect";-import { TitleSection } from "../../../components/TitleSection";import { LogOutput } from "./LogOutput";diff --git a/frontend/src/metabase/transforms/pages/TransformSettingsPage/TransformSettingsSection/TransformSettingsSection.tsx b/frontend/src/metabase/transforms/pages/TransformSettingsPage/TransformSettingsSection/TransformSettingsSection.tsxindex 6ce68c57177..e3357bda234 100644--- a/frontend/src/metabase/transforms/pages/TransformSettingsPage/TransformSettingsSection/TransformSettingsSection.tsx+++ b/frontend/src/metabase/transforms/pages/TransformSettingsPage/TransformSettingsSection/TransformSettingsSection.tsx@@ -9,6 +9,7 @@ import {} from "metabase/api";import { Link } from "metabase/common/components/Link";import CS from "metabase/css/core/index.css";+import { TitleSection } from "metabase/data-studio/common/components/TitleSection";import { UserInput } from "metabase/metadata/components";import { useMetadataToasts } from "metabase/metadata/hooks";import { PLUGIN_REMOTE_SYNC } from "metabase/plugins";@@ -18,7 +19,6 @@ import { Button, Divider, Group, Icon, Loader, Stack, Text } from "metabase/ui";import * as Urls from "metabase/urls";import type { IconName, Transform, UserId } from "metabase-types/api";-import { TitleSection } from "../../../components/TitleSection";import { isTransformRunning, sourceDatabaseId } from "../../../utils";import { UpdateIncrementalSettings } from "./UpdateIncrementalSettings";diff --git a/frontend/src/metabase/urls/admin.ts b/frontend/src/metabase/urls/admin.tsindex a11e090e2c0..87017236de1 100644--- a/frontend/src/metabase/urls/admin.ts+++ b/frontend/src/metabase/urls/admin.ts@@ -70,6 +70,10 @@ export function editDatabaseWritableConnection(databaseId: DatabaseId) {return `/admin/databases/${databaseId}/write-data`;}+export function editDatabaseAdminConnection(databaseId: DatabaseId) {+ return `/admin/databases/${databaseId}/admin`;+}+type DataModelParams = {databaseId?: DatabaseId;schemaName?: SchemaName | null;diff --git a/frontend/src/metabase/urls/index.ts b/frontend/src/metabase/urls/index.tsindex 0a1ef3b8a19..b2f04bcc573 100644--- a/frontend/src/metabase/urls/index.ts+++ b/frontend/src/metabase/urls/index.ts@@ -20,3 +20,4 @@ export * from "./questions";export * from "./timelines";export * from "./transforms";export * from "./utils";+export * from "./workspaces";diff --git a/frontend/src/metabase/urls/workspaces.ts b/frontend/src/metabase/urls/workspaces.tsnew file mode 100644index 00000000000..55cb252d26d--- /dev/null+++ b/frontend/src/metabase/urls/workspaces.ts@@ -0,0 +1,15 @@+import type { WorkspaceId } from "metabase-types/api";++const WORKSPACES_ROOT_URL = `/data-studio/workspaces`;++export function workspaceList() {+ return WORKSPACES_ROOT_URL;+}++export function workspace(id: WorkspaceId) {+ return `${WORKSPACES_ROOT_URL}/${id}`;+}++export function workspaceInstance() {+ return `${WORKSPACES_ROOT_URL}/instance`;+}diff --git a/frontend/test/__support__/server-mocks/index.ts b/frontend/test/__support__/server-mocks/index.tsindex d886cee5354..974a538cd10 100644--- a/frontend/test/__support__/server-mocks/index.ts+++ b/frontend/test/__support__/server-mocks/index.ts@@ -57,4 +57,6 @@ export * from "./timeline";export * from "./transform";export * from "./user";export * from "./user-key-value";+export * from "./workspace-instance";+export * from "./workspace-manager";export * from "./util";diff --git a/frontend/test/__support__/server-mocks/workspace-instance.ts b/frontend/test/__support__/server-mocks/workspace-instance.tsnew file mode 100644index 00000000000..a84a20722e0--- /dev/null+++ b/frontend/test/__support__/server-mocks/workspace-instance.ts@@ -0,0 +1,18 @@+import fetchMock from "fetch-mock";++import type { TableRemapping, WorkspaceInstance } from "metabase-types/api";++const BASE_URL = "path:/api/ee/workspace-instance";++export function setupGetCurrentWorkspaceEndpoint(+ workspace: WorkspaceInstance | null,+) {+ fetchMock.get(+ `${BASE_URL}/current`,+ workspace ?? { status: 204, body: null },+ );+}++export function setupListTableRemappingsEndpoint(remappings: TableRemapping[]) {+ fetchMock.get(`${BASE_URL}/table-remappings`, remappings);+}diff --git a/frontend/test/__support__/server-mocks/workspace-manager.ts b/frontend/test/__support__/server-mocks/workspace-manager.tsnew file mode 100644index 00000000000..19bb62701fa--- /dev/null+++ b/frontend/test/__support__/server-mocks/workspace-manager.ts@@ -0,0 +1,52 @@+import fetchMock from "fetch-mock";++import type { DatabaseId, Workspace, WorkspaceId } from "metabase-types/api";++const BASE_URL = "path:/api/ee/workspace-manager";++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);+}++export function setupUpdateWorkspaceEndpoint(workspace: Workspace) {+ fetchMock.put(`${BASE_URL}/${workspace.id}`, workspace);+}++export function setupDeleteWorkspaceEndpoint(workspaceId: WorkspaceId) {+ fetchMock.delete(`${BASE_URL}/${workspaceId}`, {+ id: workspaceId,+ deleted: true,+ });+}++export function setupCreateWorkspaceDatabaseEndpoint(workspace: Workspace) {+ fetchMock.post(`${BASE_URL}/${workspace.id}/database`, workspace);+}++export function setupUpdateWorkspaceDatabaseEndpoint(+ workspace: Workspace,+ databaseId: DatabaseId,+) {+ fetchMock.put(+ `${BASE_URL}/${workspace.id}/database/${databaseId}`,+ workspace,+ );+}++export function setupDeleteWorkspaceDatabaseEndpoint(+ workspace: Workspace,+ databaseId: DatabaseId,+) {+ fetchMock.delete(+ `${BASE_URL}/${workspace.id}/database/${databaseId}`,+ workspace,+ );+}diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.cljindex 7dde2fdded2..18905917d66 100644--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj@@ -10,6 +10,10 @@[metabase.driver.bigquery-cloud-sdk.common :as bigquery.common][metabase.driver.bigquery-cloud-sdk.params :as bigquery.params][metabase.driver.bigquery-cloud-sdk.query-processor :as bigquery.qp]+ ;; Side-effects: registers BigQuery driver multimethods for workspace+ ;; isolation (`init-workspace-isolation!`, `grant-workspace-read-access!`,+ ;; `check-isolation-permissions`, `destroy-workspace-isolation!`).+ [metabase.driver.bigquery-cloud-sdk.workspaces][metabase.driver.common.table-rows-sample :as table-rows-sample][metabase.driver.connection :as driver.conn][metabase.driver.settings :as driver.settings]@@ -20,7 +24,6 @@[metabase.driver.sql.query-processor.like-escape-char-built-in :as-alias like-escape-char-built-in][metabase.driver.sql.util :as sql.u][metabase.driver.sync :as driver.s]- [metabase.driver.util :as driver.u][metabase.util :as u][metabase.util.date-2 :as u.date][metabase.util.i18n :refer [tru]]@@ -32,23 +35,19 @@(:import(clojure.lang PersistentList)(com.google.api.gax.rpc FixedHeaderProvider)- (com.google.auth.oauth2 ImpersonatedCredentials ServiceAccountCredentials)- (com.google.cloud Identity Role)+ (com.google.auth.oauth2 ImpersonatedCredentials)(com.google.cloud.bigquery- Acl Acl$Role Acl$UserBigQuery- BigQuery$DatasetDeleteOptionBigQuery$DatasetListOptionBigQuery$DatasetOption- BigQuery$IAMOptionBigQuery$JobOptionBigQuery$QueryResultsOptionBigQuery$TableDataListOptionBigQuery$TableOptionBigQueryException- BigQueryOptions BigQueryOptions$Builder+ BigQueryOptionsDataset- DatasetId DatasetInfo+ DatasetIdFieldField$ModeFieldValue@@ -61,13 +60,8 @@TableIdTableResult)(com.google.cloud.http HttpTransportOptions)- (com.google.cloud.iam.admin.v1 IAMClient IAMSettings)- (com.google.cloud.resourcemanager.v3 ProjectsClient ProjectsSettings)(com.google.common.collect ImmutableMap)(com.google.gson JsonParser)- (com.google.iam.admin.v1 CreateServiceAccountRequest DeleteServiceAccountRequest ServiceAccount)- (com.google.iam.v1 Binding Policy SetIamPolicyRequest GetIamPolicyRequest)- (java.io ByteArrayInputStream)(java.util Iterator)))(set! *warn-on-reflection* true)@@ -95,8 +89,9 @@;; ImpersonatedCredentials automatically refreshes tokens before expiration,;; so the 1-hour lifetime is just the initial token validity period.;; Each query creates a fresh client, and the credentials handle refresh internally.- final-creds (if-let [target-sa (:impersonate-service-account details)]- (do+ impersonating? (some? (:impersonate-service-account details))+ final-creds (if impersonating?+ (let [target-sa (:impersonate-service-account details)](log/debugf "Creating impersonated credentials for service account: %s" target-sa)(ImpersonatedCredentials/create(.createScoped base-creds bigquery-scopes)@@ -118,6 +113,16 @@(.setCredentials final-creds)(.setHeaderProvider header-provider)(.setTransportOptions transport-options))]+ ;; `ImpersonatedCredentials` doesn't carry a project id (it derives identity+ ;; from the impersonation target SA, not from a key file), so the Google SDK+ ;; would throw "A project ID is required for this service but could not be+ ;; determined from the builder or the environment" when building the client.+ ;; Fall back to the base SA's project id, which is what every non-impersonated+ ;; call site is implicitly relying on through `getOptions.getProjectId`.+ (when impersonating?+ (when-let [pid (or (:project-id details)+ (.getProjectId base-creds))]+ (.setProjectId bq-bldr ^String pid)))(when-let [host (not-empty (:host details))](.setHost bq-bldr host))(.. bq-bldr build getService)))@@ -136,15 +141,11 @@;;; | Sync |;;; +----------------------------------------------------------------------------------------------------------------+-(defn- get-project-id- [{:keys [project-id] :as details}]- (or project-id (bigquery.common/database-details->credential-project-id details)))-(defn- list-datasets"Fetch all datasets given database `details`, applying dataset filters if specified."[{:keys [dataset-filters-type dataset-filters-patterns] :as details} & {:keys [logging-schema-exclusions?]}](let [client (database-details->client details)- project-id (get-project-id details)+ project-id (bigquery.common/get-project-id details)datasets (.listDatasets client project-id (u/varargs BigQuery$DatasetListOption))inclusion-patterns (when (= "inclusion" dataset-filters-type) dataset-filters-patterns)exclusion-patterns (when (= "exclusion" dataset-filters-type) dataset-filters-patterns)]@@ -201,7 +202,7 @@(when-not (or (str/blank? dataset-id) (str/blank? table-id))(let [details (driver.conn/effective-details database)client (database-details->client details)- project-id (get-project-id details)]+ project-id (bigquery.common/get-project-id details)](boolean(get-table* client project-id dataset-id table-id)))))@@ -229,7 +230,7 @@(defn- describe-database-tables[driver database](let [details (driver.conn/effective-details database)- project-id (get-project-id details)+ project-id (bigquery.common/get-project-id details)query-dataset (fn [dataset-id](query-honeysqldriver@@ -480,7 +481,7 @@(defmethod driver/describe-fields :bigquery-cloud-sdk[driver database & {:keys [schema-names table-names]}](let [details (driver.conn/effective-details database)- project-id (get-project-id details)+ project-id (bigquery.common/get-project-id details)dataset-ids (or schema-names (list-datasets details))];; The contract of [[driver/describe-fields]] requires results ordered by:@@ -845,9 +846,24 @@:transforms/python true:transforms/table true;; Workspace isolation using service account impersonation- :workspace false}]+ :workspace true}](defmethod driver/database-supports? [:bigquery-cloud-sdk feature] [_driver _feature _db] supported?))+(defmethod driver/qualified-name-components :bigquery-cloud-sdk+ [_driver]+ ;; BigQuery emits three-part identifiers in compiled SQL: `project.dataset.table`.+ ;; Project is connection-level identity but it appears in the AST as `Table.catalog`,+ ;; so we model it as `:db`. Dataset sits at SQLGlot's `Table.db` position, our `:schema`.+ [:db :schema])++(defmethod driver.sql/table-qualification-style :bigquery-cloud-sdk+ [_driver]+ :table-qualification-style/db-schema-table)++(defmethod driver.sql/db-slot-value :bigquery-cloud-sdk+ [_driver database]+ (:project-id (:details database)))+;; BigQuery is always in UTC(defmethod driver/db-default-timezone :bigquery-cloud-sdk [_ _]"UTC")@@ -894,10 +910,10 @@;; TODO: THIS METHOD SHOULD NOT BE UPDATING THE APP-DB (which it does in [convert-dataset-id-to-filters!]);; Issue: https://github.com/metabase/metabase/issues/39392-;; NOTE: This normalize is a legacy migration (OAuth -> service-account, dataset-id -> filters).-;; write-data-details is a new feature and will never contain these legacy fields, so we only-;; normalize the primary details here. The convert-dataset-id-to-filters! function has DB write-;; side effects that are specific to the primary details.+;; This normalize is a legacy migration (OAuth -> service-account, dataset-id -> filters); it+;; runs only against `:details` because the overlay maps (`:write-data-details`, `:admin-details`)+;; never carry these legacy fields, and `convert-dataset-id-to-filters!` writes side effects+;; specific to the primary details.(defmethod driver/normalize-db-details :bigquery-cloud-sdk[_driver database](let [details (driver.conn/default-details database)]@@ -1075,7 +1091,7 @@;; This is important for workspace isolation where the impersonated SA has;; access to an existing isolated dataset but cannot create new datasets.(let [client (database-details->client conn-spec) ;; for bigquery, connection spec *is* the details- project-id (get-project-id conn-spec)+ project-id (bigquery.common/get-project-id conn-spec)dataset-id (DatasetId/of project-id schema)](when-not (.getDataset client dataset-id (u/varargs BigQuery$DatasetOption));; Dataset doesn't exist, try to create it@@ -1106,407 +1122,5 @@;;; - Roles: `roles/bigquery.admin`, `roles/iam.serviceAccountAdmin`, `roles/resourcemanager.projectIamAdmin`;;; - APIs: `bigquery.googleapis.com`, `iam.googleapis.com`, `cloudresourcemanager.googleapis.com`-(defn- ws-service-account-credentials- "Parse ServiceAccountCredentials from database details."- ^ServiceAccountCredentials [{:keys [service-account-json]}]- (ServiceAccountCredentials/fromStream- (ByteArrayInputStream. (.getBytes ^String service-account-json))))--(defn- ws-database-details->client- "Create a BigQuery client from database details for workspace isolation."- ^BigQuery [details]- (let [creds (.createScoped (ws-service-account-credentials details)- (doto (java.util.ArrayList.)- (.add "https://www.googleapis.com/auth/bigquery")))]- (-> (BigQueryOptions/newBuilder)- ^BigQueryOptions$Builder (.setCredentials creds)- ^BigQueryOptions (.build)- (.getService))))--(defn- ws-database-details->iam-client- "Create an IAM Admin client from database details."- ^IAMClient [details]- (let [creds (.createScoped (ws-service-account-credentials details)- (doto (java.util.ArrayList.)- (.add "https://www.googleapis.com/auth/cloud-platform")))]- (IAMClient/create- (-> (IAMSettings/newBuilder)- (.setCredentialsProvider (reify com.google.api.gax.core.CredentialsProvider- (getCredentials [_] creds)))- ^IAMSettings (.build)))))--(defn- ws-service-account-id- "Generate the service account ID for a workspace (max 30 chars, lowercase, alphanumeric + hyphens)."- [workspace]- ;; Format: mb-ws-{workspace-id} truncated to 30 chars- (let [ws-id (str (:id workspace))- sa-id (str "mb-ws-" ws-id)]- (-> sa-id- (subs 0 (min 30 (count sa-id)))- u/lower-case-en)))--(defn- ws-service-account-exists?- "Check if a service account exists."- [^IAMClient iam-client ^String project-id ^String sa-email]- (try- (.getServiceAccount iam-client (format "projects/%s/serviceAccounts/%s" project-id sa-email))- true- (catch com.google.api.gax.rpc.NotFoundException _- false)))--(defn- ws-create-service-account!- "Create a service account for a workspace if it doesn't exist.- Returns the service account email."- [^IAMClient iam-client ^String project-id workspace]- (let [sa-id (ws-service-account-id workspace)- sa-email (format "%s@%s.iam.gserviceaccount.com" sa-id project-id)- project-name (format "projects/%s" project-id)]- ;; Check if already exists first- (if (ws-service-account-exists? iam-client project-id sa-email)- (log/debugf "Service account already exists: %s" sa-email)- (do- (log/infof "Creating service account %s in project %s" sa-id project-id)- (let [request (-> (CreateServiceAccountRequest/newBuilder)- (.setName project-name)- (.setAccountId sa-id)- (.setServiceAccount- (-> (ServiceAccount/newBuilder)- (.setDisplayName (format "Metabase Workspace %s" (:id workspace)))- (.setDescription "Auto-created by Metabase for workspace isolation")- (.build)))- (.build))]- (.createServiceAccount iam-client request)- (log/infof "Created service account: %s" sa-email))))- sa-email))--(defn- ws-delete-service-account!- "Delete a service account for a workspace. Idempotent - does nothing if SA doesn't exist."- [^IAMClient iam-client ^String project-id workspace]- (let [sa-id (ws-service-account-id workspace)- sa-email (format "%s@%s.iam.gserviceaccount.com" sa-id project-id)- sa-name (format "projects/%s/serviceAccounts/%s" project-id sa-email)]- (when (ws-service-account-exists? iam-client project-id sa-email)- (log/infof "Deleting service account %s" sa-email)- (let [request (-> (DeleteServiceAccountRequest/newBuilder)- (.setName sa-name)- (.build))]- (.deleteServiceAccount iam-client request)- (log/infof "Deleted service account: %s" sa-email)))))--(defn- ws-has-role-binding?- "Check if a policy already has a binding for the given role and member."- [^Policy policy ^String role ^String member]- (some (fn [^Binding binding]- (and (= (.getRole binding) role)- (some #(= % member) (.getMembersList binding))))- (.getBindingsList policy)))--(defn- ws-grant-impersonation-permission!- "Grant the main service account permission to impersonate the workspace service account."- [^IAMClient iam-client ^String project-id ^String main-sa-email ^String workspace-sa-email]- (let [resource (format "projects/%s/serviceAccounts/%s" project-id workspace-sa-email)- role "roles/iam.serviceAccountTokenCreator"- member (format "serviceAccount:%s" main-sa-email)]- (try- ;; Get current policy- (let [current-policy (.getIamPolicy iam-client resource)]- ;; Only add if not already granted- (when-not (ws-has-role-binding? current-policy role member)- (let [new-binding (-> (Binding/newBuilder)- (.setRole role)- (.addMembers member)- (.build))- updated-policy (-> (Policy/newBuilder current-policy)- (.addBindings new-binding)- (.build))- request (-> (SetIamPolicyRequest/newBuilder)- (.setResource resource)- (.setPolicy updated-policy)- (.build))]- (.setIamPolicy iam-client request)- (log/infof "Granted impersonation permission on %s to %s" workspace-sa-email main-sa-email))))- (catch Exception e- (log/warn e "Failed to grant impersonation permission (may already exist)")))))--(defn- ws-grant-project-role!- "Grant a project-level IAM role to a service account.- This is needed for roles like bigquery.jobUser that must be granted at project level."- [details ^String project-id ^String service-account-email ^String role]- (log/infof "Granting project role %s to %s" role service-account-email)- (let [creds (.createScoped (ws-service-account-credentials details)- (doto (java.util.ArrayList.)- (.add "https://www.googleapis.com/auth/cloud-platform")))- settings (-> (ProjectsSettings/newBuilder)- (.setCredentialsProvider (reify com.google.api.gax.core.CredentialsProvider- (getCredentials [_] creds)))- ^ProjectsSettings (.build))- projects-client (ProjectsClient/create settings)- resource (format "projects/%s" project-id)- member (format "serviceAccount:%s" service-account-email)]- (try- ;; Get current policy- (let [get-request (-> (GetIamPolicyRequest/newBuilder)- (.setResource resource)- (.build))- current-policy (.getIamPolicy projects-client get-request)]- ;; Only add if not already granted- (when-not (ws-has-role-binding? current-policy role member)- (let [new-binding (-> (Binding/newBuilder)- (.setRole role)- (.addMembers member)- (.build))- updated-policy (-> (Policy/newBuilder current-policy)- (.addBindings new-binding)- (.build))- set-request (-> (SetIamPolicyRequest/newBuilder)- (.setResource resource)- (.setPolicy updated-policy)- (.build))]- (.setIamPolicy projects-client set-request)- (log/infof "Granted %s on project %s to %s" role project-id service-account-email))))- (finally- (.close projects-client)))))--(defn- ws-wait-for-impersonation-ready!- "Poll until impersonation is working. GCP IAM changes can take up to 60 seconds to propagate.- Tests by actually creating impersonated credentials and making a simple API call."- [details ^String target-sa-email & {:keys [max-attempts interval-ms]- :or {max-attempts 120- interval-ms 1000}}]- (log/info "Waiting for IAM impersonation to be ready...")- (let [base-creds (.createScoped (ws-service-account-credentials details)- (doto (java.util.ArrayList.)- (.add "https://www.googleapis.com/auth/bigquery")))- project-id (get-project-id details)]- (loop [attempt 1]- (log/debugf "Checking impersonation readiness (attempt %d/%d)" attempt max-attempts)- (let [result (try- ;; Try to create impersonated credentials and use them- (let [impersonated (ImpersonatedCredentials/create- base-creds- target-sa-email- nil ;; delegates- (doto (java.util.ArrayList.)- (.add "https://www.googleapis.com/auth/bigquery"))- 3600)- client (-> (BigQueryOptions/newBuilder)- ^BigQueryOptions$Builder (.setCredentials impersonated)- ^BigQueryOptions (.build)- ^BigQuery (.getService))]- ;; Try a simple operation - list datasets (limited to 1)- (.listDatasets client project-id (into-array BigQuery$DatasetListOption []))- :ready)- (catch Exception e- (if (or (str/includes? (str (ex-message e)) "permission")- (str/includes? (str (ex-message e)) "403")- (str/includes? (str (ex-message e)) "Access Denied"))- :not-ready- (do- (log/debugf "Unexpected error checking impersonation: %s" (ex-message e))- :not-ready))))]- (cond- (= result :ready)- (log/info "IAM impersonation is ready")-- (>= attempt max-attempts)- (throw (ex-info "Timeout waiting for IAM impersonation to propagate"- {:target-sa target-sa-email- :attempts attempt}))-- :else- (do- (Thread/sleep ^long interval-ms)- (recur (inc attempt))))))))--(defn- ws-role-name->acl-role- "Convert a BigQuery IAM role name to an Acl$Role."- ^Acl$Role [^String role-name]- (case role-name- "roles/bigquery.dataEditor" Acl$Role/WRITER- "roles/bigquery.dataViewer" Acl$Role/READER- "roles/bigquery.dataOwner" Acl$Role/OWNER- (throw (ex-info (str "Unknown role: " role-name) {:role role-name}))))--(defn- ws-has-acl-entry?- "Check if an ACL list already has an entry for the given entity and role."- [acl-list ^Acl$User entity ^Acl$Role role]- (some (fn [^Acl acl]- (and (= (.getEntity acl) entity)- (= (.getRole acl) role)))- acl-list))--(defn- ws-grant-dataset-acl!- "Grant an ACL role on a dataset to a service account."- [^BigQuery client ^DatasetId dataset-id ^String service-account-email ^String role-name]- (log/debugf "Granting %s on dataset %s to %s" role-name dataset-id service-account-email)- (let [dataset ^Dataset (.getDataset client dataset-id- ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"- (into-array BigQuery$DatasetOption []))- current-acl (into [] (.getAcl dataset))- acl-role (ws-role-name->acl-role role-name)- acl-user (Acl$User. service-account-email)]- ;; Only add if not already granted- (when-not (ws-has-acl-entry? current-acl acl-user acl-role)- (let [new-acl-entry (Acl/of acl-user acl-role)- updated-acl (conj current-acl new-acl-entry)- updated-dataset (-> (.toBuilder dataset)- (.setAcl ^"clojure.lang.PersistentVector" updated-acl)- (.build))]- (.update client updated-dataset- ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"- (into-array BigQuery$DatasetOption []))))))--(defn- ws-has-table-iam-binding?- "Check if a table IAM policy already has a binding for the given role and identity."- [^com.google.cloud.Policy policy ^Role role ^Identity identity]- (let [bindings (.getBindings policy)- identity-set (clojure.core/get bindings role)]- (and identity-set (.contains ^java.util.Set identity-set identity))))--(defn- ws-grant-table-read-access!- "Grant read access on a specific table to a service account using table-level IAM."- [^BigQuery client ^TableId table-id ^String service-account-email]- (log/debugf "Granting read access on table %s to %s" table-id service-account-email)- (let [current-policy (.getIamPolicy client table-id (into-array BigQuery$IAMOption []))- role (Role/of "roles/bigquery.dataViewer")- sa-identity (Identity/serviceAccount service-account-email)]- ;; Only add if not already granted- (when-not (ws-has-table-iam-binding? current-policy role sa-identity)- (let [updated-policy (-> current-policy- (.toBuilder)- (.addIdentity role sa-identity (into-array Identity []))- (.build))]- (.setIamPolicy client table-id updated-policy (into-array BigQuery$IAMOption []))))))--(defmethod driver/init-workspace-isolation! :bigquery-cloud-sdk- [_driver database workspace]- (let [details (driver.conn/effective-details database)- client (ws-database-details->client details)- iam-client (ws-database-details->iam-client details)- project-id (get-project-id details)- main-sa-email (.getClientEmail (ws-service-account-credentials details))- dataset-name (driver.u/workspace-isolation-namespace-name workspace)]-- (try- ;; Create the workspace service account (or get existing)- (let [ws-sa-email (ws-create-service-account! iam-client project-id workspace)- dataset-id (DatasetId/of project-id dataset-name)]-- (log/infof "Initializing BigQuery workspace isolation: dataset=%s, service-account=%s"- dataset-name ws-sa-email)-- ;; Grant main SA permission to impersonate workspace SA- (ws-grant-impersonation-permission! iam-client project-id main-sa-email ws-sa-email)-- ;; Grant the workspace SA permission to run BigQuery jobs (queries) at project level- ;; Note: We intentionally do NOT grant project-level dataEditor as that would give- ;; access to all datasets. The workspace SA only gets dataEditor on its isolated dataset.- (ws-grant-project-role! details project-id ws-sa-email "roles/bigquery.jobUser")-- ;; Wait for IAM permissions to propagate by polling until impersonation works- (ws-wait-for-impersonation-ready! details ws-sa-email)-- ;; Create the isolated dataset if it doesn't exist (using main SA credentials, not impersonated)- (when-not (.getDataset client dataset-id- ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"- (into-array BigQuery$DatasetOption []))- (let [dataset-info (-> (DatasetInfo/newBuilder dataset-id)- (.setDescription (format "Metabase workspace isolation for workspace %s" (:id workspace)))- (.build))]- (.create client dataset-info- ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"- (into-array BigQuery$DatasetOption []))))-- ;; Grant the workspace service account dataEditor role on the isolated dataset- ;; dataEditor allows: create/update/delete tables, insert/update/delete data- (ws-grant-dataset-acl! client dataset-id ws-sa-email "roles/bigquery.dataEditor")-- ;; Return workspace connection details for impersonation- ;; :user is used by grant-read-access-to-tables! to know which SA to grant access to- ;; :impersonate-service-account is used by the connection swap to use impersonated credentials- {:schema dataset-name- :database_details {:user ws-sa-email- :impersonate-service-account ws-sa-email}})- (finally- (.close iam-client)))))--(defmethod driver/grant-workspace-read-access! :bigquery-cloud-sdk- [_driver database workspace tables]- ;; For BigQuery, the workspace contains the service account email in database_details- (let [ws-sa-email (-> workspace :database_details :impersonate-service-account)- details (driver.conn/effective-details database)- client (ws-database-details->client details)- project-id (get-project-id details)]-- (log/debugf "Granting read access to %d tables for %s" (count tables) ws-sa-email)-- ;; Grant dataViewer at table level for each table - proper isolation- (doseq [{:keys [schema name]} tables]- (let [table-id (TableId/of project-id schema name)]- (ws-grant-table-read-access! client table-id ws-sa-email)))))--(def ^:private perm-check-workspace-id "00000000-0000-0000-0000-000000000000")--(defmethod driver/check-isolation-permissions :bigquery-cloud-sdk- [driver database test-table]- ;; BigQuery uses GCP IAM APIs instead of SQL, so we can't use transaction rollback.- ;; We run the actual init/grant/destroy operations and clean up immediately.- (let [test-workspace {:id perm-check-workspace-id- :name "_mb_perm_check_"}]- (try- (let [init-result (try- (driver/init-workspace-isolation! driver database test-workspace)- (catch Exception e- (throw (ex-info (tru "Failed to initialize workspace isolation: {0}" (ex-message e))- {:step :init} e))))- workspace-with-details (merge test-workspace init-result)]- (when test-table- (try- (driver/grant-workspace-read-access! driver database workspace-with-details [test-table])- (catch Exception e- (throw (ex-info (tru "Failed to grant read access to table {0}.{1}: {2}"- (:schema test-table) (:name test-table) (ex-message e))- {:step :grant :table test-table} e)))))- (try- (driver/destroy-workspace-isolation! driver database workspace-with-details)- (catch Exception e- (throw (ex-info (tru "Failed to destroy workspace isolation: {0}" (ex-message e))- {:step :destroy} e)))))- nil- (catch Exception e- (ex-message e))- (finally- (try- (driver/destroy-workspace-isolation! driver database test-workspace)- (catch Exception _ nil))))))--(defmethod driver/destroy-workspace-isolation! :bigquery-cloud-sdk- [_driver database workspace]- (let [details (driver.conn/effective-details database)- client (ws-database-details->client details)- iam-client (ws-database-details->iam-client details)- project-id (get-project-id details)- dataset-name (driver.u/workspace-isolation-namespace-name workspace)- dataset-id (DatasetId/of project-id dataset-name)]- (try- (log/infof "Destroying BigQuery workspace isolation: dataset=%s" dataset-name)-- ;; Delete the dataset if it exists (deleteContents=true removes all tables)- (when (.getDataset client dataset-id- ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"- (into-array BigQuery$DatasetOption []))- (log/infof "Deleting dataset %s" dataset-name)- (.delete client dataset-id- ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetDeleteOption;"- (into-array BigQuery$DatasetDeleteOption [(BigQuery$DatasetDeleteOption/deleteContents)]))- (log/infof "Deleted dataset %s" dataset-name))-- ;; Delete the service account (this also removes its IAM bindings)- (ws-delete-service-account! iam-client project-id workspace)-- {:success true}- (finally- (.close iam-client)))))-(defmethod driver/llm-sql-dialect-resource :bigquery-cloud-sdk [_]- "metabot/prompts/dialects/bigquery.md")+ "metabot/prompts/dialects/bigquery.md")\ No newline at end of filediff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/common.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/common.cljindex 748a3968c1e..863cdd44f50 100644--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/common.clj+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/common.clj@@ -46,6 +46,12 @@(-> (database-details->service-account-credential details).getProjectId))+(defn get-project-id+ "Project-id for `details`. Prefers the user-supplied `:project-id`, falls back to the+ one embedded in the service-account credentials."+ [{:keys [project-id] :as details}]+ (or project-id (database-details->credential-project-id details)))+(mu/defn populate-project-id-from-credentials!"Update the given `database` details blob to include the credentials' project-id as a separate entry (under a`project-id-from-credentials` key). This is basically an inferred/calculated key (not something the user will everdiff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/workspaces.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/workspaces.cljnew file mode 100644index 00000000000..bc7917f5cd6--- /dev/null+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/workspaces.clj@@ -0,0 +1,691 @@+(ns metabase.driver.bigquery-cloud-sdk.workspaces+ "BigQuery workspace isolation: per-workspace service accounts, IAM bindings,+ and dataset ACLs that gate read/write access to isolated datasets. Loaded+ for side-effects by `metabase.driver.bigquery-cloud-sdk` to register the+ `driver/{init,grant,check,destroy}-workspace-isolation*` multimethod+ implementations."+ (:refer-clojure :exclude [some not-empty])+ (:require+ [clojure.string :as str]+ [metabase.driver :as driver]+ [metabase.driver.bigquery-cloud-sdk.common :as bigquery.common]+ [metabase.driver.connection :as driver.conn]+ [metabase.driver.util :as driver.u]+ [metabase.util :as u]+ [metabase.util.i18n :refer [tru]]+ [metabase.util.log :as log]+ [metabase.util.performance :refer [some not-empty]])+ (:import+ (com.google.auth.oauth2 ImpersonatedCredentials ServiceAccountCredentials)+ (com.google.cloud.bigquery+ Acl Acl$Role Acl$User+ BigQuery+ BigQuery$DatasetDeleteOption+ BigQuery$DatasetListOption+ BigQuery$DatasetOption+ BigQueryOptions BigQueryOptions$Builder+ Dataset+ DatasetId DatasetInfo)+ (com.google.cloud.iam.admin.v1 IAMClient IAMSettings)+ (com.google.cloud.resourcemanager.v3 ProjectsClient ProjectsSettings)+ (com.google.iam.admin.v1 CreateServiceAccountRequest DeleteServiceAccountRequest ServiceAccount)+ (com.google.iam.v1 Binding Policy SetIamPolicyRequest GetIamPolicyRequest)+ (java.io ByteArrayInputStream)))++(set! *warn-on-reflection* true)++(defn create-dataset!+ "Create `dataset-name` in `project-id` via `client`. Idempotent — no-op when+ the dataset already exists. `:description` (in `opts`) is optional metadata.++ Used by [[init-workspace-isolation!]] to provision the workspace's output+ dataset, and by tests to set up per-run source datasets."+ [^BigQuery client ^String project-id ^String dataset-name & [{:keys [^String description]}]]+ (let [dataset-id (DatasetId/of project-id dataset-name)]+ (when-not (.getDataset client dataset-id+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"+ (into-array BigQuery$DatasetOption []))+ (let [builder (DatasetInfo/newBuilder dataset-id)+ _ (when description (.setDescription builder description))+ info (.build builder)]+ (.create client info+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"+ (into-array BigQuery$DatasetOption []))))))++(defn drop-dataset!+ "Delete `dataset-name` in `project-id` via `client`, including all its tables.+ Idempotent — no-op when the dataset doesn't exist.++ Used by [[destroy-workspace-isolation!]] to tear down the workspace's output+ dataset, and by tests to clean up per-run source datasets."+ [^BigQuery client ^String project-id ^String dataset-name]+ (let [dataset-id (DatasetId/of project-id dataset-name)]+ (when (.getDataset client dataset-id+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"+ (into-array BigQuery$DatasetOption []))+ (.delete client dataset-id+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetDeleteOption;"+ (into-array BigQuery$DatasetDeleteOption+ [(BigQuery$DatasetDeleteOption/deleteContents)])))))++(def ^:private perm-check-workspace-id "00000000-0000-0000-0000-000000000000")++(defn- sa-propagation-error?+ "True for the GCP race where an API call references a service account that was+ just created but hasn't yet propagated to the API receiving the call. GCP+ surfaces this as `INVALID_ARGUMENT: Service account <email> does not exist.`+ even though `iam.googleapis.com` already created it. Retrying the same call+ eventually succeeds once propagation completes (usually <30s)."+ [^Throwable t]+ (when-let [msg (ex-message t)]+ (and (instance? com.google.api.gax.rpc.InvalidArgumentException t)+ (str/includes? msg "Service account")+ (str/includes? msg "does not exist"))))++(defn- ws-has-acl-entry?+ "Check if an ACL list already has an entry for the given entity and role."+ [acl-list ^Acl$User entity ^Acl$Role role]+ (some (fn [^Acl acl]+ (and (= (.getEntity acl) entity)+ (= (.getRole acl) role)))+ acl-list))++(defn- ws-has-role-binding?+ "Check if a policy already has a binding for the given role and member."+ [^Policy policy ^String role ^String member]+ (some (fn [^Binding binding]+ (and (= (.getRole binding) role)+ (some #(= % member) (.getMembersList binding))))+ (.getBindingsList policy)))++(defn- ws-role-name->acl-role+ "Convert a BigQuery IAM role name to an Acl$Role."+ ^Acl$Role [^String role-name]+ (case role-name+ "roles/bigquery.dataEditor" Acl$Role/WRITER+ "roles/bigquery.dataViewer" Acl$Role/READER+ "roles/bigquery.dataOwner" Acl$Role/OWNER+ (throw (ex-info (str "Unknown role: " role-name) {:role role-name}))))++(defn ws-sa-description->created-at+ "Parse the `created-at:<iso-instant>` token out of a workspace iso SA+ `description` string. Returns the parsed `java.time.Instant`, or nil+ when the marker is absent / malformed.++ Round-trip contract: `(= i (ws-sa-description->created-at (ws-sa-description i)))`."+ ^java.time.Instant [description]+ (when description+ (when-let [match (re-find #"created-at:(\S+)" description)]+ (try+ (java.time.Instant/parse (second match))+ (catch Throwable _ nil)))))++(def ^:private ws-sa-description-prefix+ "Auto-created by Metabase for workspace isolation; created-at:")++(defn- ws-service-account-credentials+ "Parse ServiceAccountCredentials from database details."+ ^ServiceAccountCredentials [{:keys [service-account-json]}]+ (ServiceAccountCredentials/fromStream+ (ByteArrayInputStream. (.getBytes ^String service-account-json))))++(defn- ws-service-account-exists?+ "Check if a service account exists."+ [^IAMClient iam-client ^String project-id ^String sa-email]+ (try+ (.getServiceAccount iam-client (format "projects/%s/serviceAccounts/%s" project-id sa-email))+ true+ (catch com.google.api.gax.rpc.NotFoundException _+ false)))++(defn- ws-service-account-id+ "Generate the service account ID for a workspace (max 30 chars, lowercase, alphanumeric + hyphens)."+ [workspace]+ ;; Format: mb-ws-{workspace-id} truncated to 30 chars+ (let [ws-id (str (:id workspace))+ sa-id (str "mb-ws-" ws-id)]+ (-> sa-id+ (subs 0 (min 30 (count sa-id)))+ u/lower-case-en)))++(defn- with-sa-propagation-retry!+ "Run `f` (a no-arg fn), retrying when GCP rejects it because a freshly-created+ workspace SA isn't yet visible to the API being called. Used for IAM bindings+ that reference the SA as a member — these can race with SA creation across+ GCP services (IAM ↔ Cloud Resource Manager) even after the create returned."+ [{:keys [max-attempts interval-ms]+ :or {max-attempts 60+ interval-ms 1000}}+ f]+ (loop [attempt 1]+ (let [result (try+ [::ok (f)]+ (catch Throwable t+ (if (and (sa-propagation-error? t)+ (< attempt max-attempts))+ [::retry t]+ (throw t))))]+ (case (first result)+ ::ok (second result)+ ::retry (do+ (log/debugf "SA propagation race on attempt %d/%d, retrying in %dms"+ attempt max-attempts interval-ms)+ (Thread/sleep ^long interval-ms)+ (recur (inc attempt)))))))++(defn- ws-grant-impersonation-permission!+ "Grant the main service account permission to impersonate the workspace service account.++ The pre-check `ws-has-role-binding?` makes the only no-op-of-concern (the binding+ already exists) a non-write. Any exception raised by `getIamPolicy` or `setIamPolicy`+ reflects a real failure -- `PERMISSION_DENIED`, `RESOURCE_EXHAUSTED`, `INVALID_ARGUMENT`,+ network/`UNAVAILABLE`, or an etag race between concurrent provisioners -- and must+ propagate so the caller's `init-workspace-isolation!` flow fails loudly instead of+ timing out 120s downstream in `ws-wait-for-impersonation-ready!` with no signal."+ [^IAMClient iam-client ^String project-id ^String main-sa-email ^String workspace-sa-email]+ (let [resource (format "projects/%s/serviceAccounts/%s" project-id workspace-sa-email)+ role "roles/iam.serviceAccountTokenCreator"+ member (format "serviceAccount:%s" main-sa-email)+ ;; Get current policy+ current-policy (.getIamPolicy iam-client resource)]+ ;; Only add if not already granted+ (when-not (ws-has-role-binding? current-policy role member)+ (let [new-binding (-> (Binding/newBuilder)+ (.setRole role)+ (.addMembers member)+ (.build))+ updated-policy (-> (Policy/newBuilder current-policy)+ (.addBindings new-binding)+ (.build))+ request (-> (SetIamPolicyRequest/newBuilder)+ (.setResource resource)+ (.setPolicy updated-policy)+ (.build))]+ (.setIamPolicy iam-client request)+ (log/infof "Granted impersonation permission on %s to %s" workspace-sa-email main-sa-email)))))++(defn- ws-grant-dataset-acl!+ "Grant an ACL role on a dataset to a service account."+ [^BigQuery client ^DatasetId dataset-id ^String service-account-email ^String role-name]+ (log/debugf "Granting %s on dataset %s to %s" role-name dataset-id service-account-email)+ (let [dataset ^Dataset (.getDataset client dataset-id+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"+ (into-array BigQuery$DatasetOption []))+ current-acl (into [] (.getAcl dataset))+ acl-role (ws-role-name->acl-role role-name)+ acl-user (Acl$User. service-account-email)]+ ;; Only add if not already granted+ (when-not (ws-has-acl-entry? current-acl acl-user acl-role)+ (let [new-acl-entry (Acl/of acl-user acl-role)+ updated-acl (conj current-acl new-acl-entry)+ updated-dataset (-> (.toBuilder dataset)+ (.setAcl ^"clojure.lang.PersistentVector" updated-acl)+ (.build))]+ (.update client updated-dataset+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"+ (into-array BigQuery$DatasetOption []))))))++(defn ws-sa-description+ "Format an IAM service account `description` field for a workspace iso SA.+ The string carries `created-at:<ISO-8601 instant>` so that CI cleanup can+ age-gate orphan SAs without needing IAM audit-log access.++ This is the single source of truth for the description format. The cleanup+ side ([[ws-sa-description->created-at]]) reads from it -- keep them in+ lockstep by going through these two fns rather than building/parsing+ the string inline anywhere else."+ ^String [^java.time.Instant instant]+ (str ws-sa-description-prefix instant))++(defn- ws-database-details->client+ "Create a BigQuery client from database details for workspace isolation."+ ^BigQuery [details]+ (let [creds (.createScoped (ws-service-account-credentials details)+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/bigquery")))]+ (-> (BigQueryOptions/newBuilder)+ ^BigQueryOptions$Builder (.setCredentials creds)+ ^BigQueryOptions (.build)+ (.getService))))++(defn- ws-database-details->iam-client+ "Create an IAM Admin client from database details."+ ^IAMClient [details]+ (let [creds (.createScoped (ws-service-account-credentials details)+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/cloud-platform")))]+ (IAMClient/create+ (-> (IAMSettings/newBuilder)+ (.setCredentialsProvider (reify com.google.api.gax.core.CredentialsProvider+ (getCredentials [_] creds)))+ ^IAMSettings (.build)))))++(defn- ws-wait-for-dataset-visible-via-impersonation!+ "Poll until the impersonated workspace SA can see `target-dataset` from+ BOTH `getDataset` AND `listDatasets`. BigQuery dataset ACL updates are+ eventually consistent and the two endpoints sync independently:+ `getDataset` typically picks up a dataViewer grant within ~1s, but+ `listDatasets` (which `can-connect-with-details?` uses) can lag tens+ of seconds behind. Returning from the wait when only `getDataset` agrees+ leaves the next `can-connect-with-details?` call hitting `listDatasets`+ with the still-stale cache, which throws `Looks like we cannot find any+ matching datasets` and surfaces as `Failed to connect to Database` in+ `init-from-config-file!`.++ Gate on the `listDatasets` outcome so the wait returns only once the+ exact endpoint can-connect uses agrees. Each iteration builds a fresh+ impersonated client to avoid carrying token / negative-cache state."+ [details ^String target-sa-email ^String project-id ^String target-dataset+ & {:keys [max-attempts interval-ms]+ :or {max-attempts 90+ interval-ms 1000}}]+ (log/infof "Waiting for dataset %s to be visible to %s via listDatasets..."+ target-dataset target-sa-email)+ (loop [attempt 1]+ (let [outcome (try+ (let [base-creds (.createScoped (ws-service-account-credentials details)+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/bigquery")))+ impersonated (ImpersonatedCredentials/create+ base-creds+ target-sa-email+ nil+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/bigquery"))+ 3600)+ client (-> (BigQueryOptions/newBuilder)+ ^BigQueryOptions$Builder (.setCredentials impersonated)+ ^BigQueryOptions$Builder (.setProjectId project-id)+ ^BigQueryOptions (.build)+ ^BigQuery (.getService))+ ;; Fast-path probe: if `getDataset` doesn't even see the dataset,+ ;; `listDatasets` certainly won't, and we can skip the more expensive+ ;; pagination this iteration.+ single-ok? (some? (try (.getDataset client+ (DatasetId/of project-id target-dataset)+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"+ (into-array BigQuery$DatasetOption []))+ (catch Exception _ nil)))+ datasets (when single-ok?+ (.listDatasets client project-id+ (into-array BigQuery$DatasetListOption [])))+ listed-ok? (boolean+ (when datasets+ (some (fn [^Dataset d] (= target-dataset (.. d getDatasetId getDataset)))+ (.iterateAll datasets))))]+ (if listed-ok? :ready :not-yet))+ (catch Exception e+ (log/debugf "Unexpected error checking dataset visibility: %s" (ex-message e))+ :not-yet))]+ (cond+ (= outcome :ready)+ (log/infof "Dataset %s is visible to %s via listDatasets after %d attempt(s)"+ target-dataset target-sa-email attempt)++ (>= attempt max-attempts)+ (throw (ex-info "Timeout waiting for dataset ACL grant to propagate to listDatasets"+ {:target-sa target-sa-email+ :target-dataset target-dataset+ :attempts attempt}))++ :else+ (do+ (Thread/sleep ^long interval-ms)+ (recur (inc attempt)))))))++(defn- ws-wait-for-impersonation-ready!+ "Poll until impersonation is working. GCP IAM changes can take up to 60 seconds to propagate.+ Tests by actually creating impersonated credentials and making a simple API call.++ Each iteration builds a fresh `ServiceAccountCredentials` source (and the+ derived `ImpersonatedCredentials` + `BigQuery` client). Reusing one across+ iterations would let GCP's auth-library token-cache and retry/backoff state+ from an early failure persist for the duration of the loop, so even after+ the underlying IAM grant propagates, every subsequent check inherits the+ negative-cached state and the loop times out. Per-iteration creation+ isolates each attempt.++ On timeout, throws an `ex-info` with a verbose diagnostic so CI logs+ immediately show why we gave up: total wait time, ceiling vs Google's+ documented worst case (7 min for direct IAM policy edits), last GCP error+ message + class, and a histogram of error classes seen across attempts.+ See https://docs.cloud.google.com/iam/docs/access-change-propagation."+ [details ^String target-sa-email & {:keys [max-attempts interval-ms]+ :or {max-attempts 120+ interval-ms 1000}}]+ (log/info "Waiting for IAM impersonation to be ready...")+ (let [project-id (bigquery.common/get-project-id details)+ started-ns (System/nanoTime)]+ (loop [attempt 1+ last-error nil+ error-classes {}]+ (log/debugf "Checking impersonation readiness (attempt %d/%d)" attempt max-attempts)+ (let [{:keys [status error err-class]}+ (try+ ;; Build fresh credentials each iteration to avoid cached+ ;; negative results from earlier attempts.+ (let [base-creds (.createScoped (ws-service-account-credentials details)+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/bigquery")))+ impersonated (ImpersonatedCredentials/create+ base-creds+ target-sa-email+ nil ;; delegates+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/bigquery"))+ 3600)+ client (-> (BigQueryOptions/newBuilder)+ ^BigQueryOptions$Builder (.setCredentials impersonated)+ ^BigQueryOptions$Builder (.setProjectId project-id)+ ^BigQueryOptions (.build)+ ^BigQuery (.getService))]+ ;; Try a simple operation - list datasets (limited to 1)+ (.listDatasets client project-id (into-array BigQuery$DatasetListOption []))+ {:status :ready})+ (catch Exception e+ (let [msg (str (ex-message e))+ cls (cond+ (or (str/includes? msg "permission")+ (str/includes? msg "403")+ (str/includes? msg "Access Denied")) :access-denied+ (or (str/includes? msg "DEADLINE_EXCEEDED")+ (str/includes? msg "deadline")+ (str/includes? msg "timeout")) :deadline+ (str/includes? msg "UNAVAILABLE") :unavailable+ (or (str/includes? msg "UNAUTHENTICATED")+ (str/includes? msg "401")) :unauthenticated+ :else :other)]+ (when (= cls :other)+ (log/debugf "Unexpected error checking impersonation: %s" msg))+ {:status :not-ready :error e :err-class cls})))]+ (cond+ (= status :ready)+ (log/infof "IAM impersonation ready after %d attempt(s) (%.1fs)"+ attempt (/ (- (System/nanoTime) started-ns) 1e9))++ (>= attempt max-attempts)+ (let [elapsed-s (/ (- (System/nanoTime) started-ns) 1e9)+ last-msg (some-> last-error ex-message)+ last-cls (some-> last-error class .getName)+ histogram (->> error-classes+ (sort-by (comp - val))+ (map (fn [[k v]] (format "%s=%d" (name k) v)))+ (str/join ", "))]+ (throw (ex-info+ (format (str "Timeout waiting for IAM impersonation to propagate "+ "after %d attempts / %.1fs (ceiling %ds). "+ "Google's documented worst case for direct IAM policy edits is ~7min "+ "(see https://docs.cloud.google.com/iam/docs/access-change-propagation). "+ "Errors seen: {%s}. Last error (%s): %s")+ attempt elapsed-s (long (/ (* max-attempts interval-ms) 1000))+ (or (not-empty histogram) "<none>")+ (or last-cls "n/a")+ (or last-msg "n/a"))+ {:target-sa target-sa-email+ :project-id project-id+ :attempts attempt+ :elapsed-seconds elapsed-s+ :ceiling-seconds (long (/ (* max-attempts interval-ms) 1000))+ :error-class-counts error-classes+ :last-error-message last-msg+ :last-error-class last-cls+ :iam-propagation-doc "https://docs.cloud.google.com/iam/docs/access-change-propagation"}+ last-error)))++ :else+ (do+ (Thread/sleep ^long interval-ms)+ (recur (inc attempt)+ error+ (update error-classes err-class (fnil inc 0)))))))))++(defn- ws-wait-for-service-account!+ "Poll IAM until the SA created by `ws-create-service-account!` is visible.++ GCP IAM is eventually consistent: `.createServiceAccount` returns once the+ create request is accepted by the control plane, but the SA record then+ propagates to read endpoints (`getServiceAccount`) and policy enforcement+ (`setIamPolicy`) over the next few seconds. Subsequent grants in+ `init-workspace-isolation!` reference this SA and fail with+ `INVALID_ARGUMENT: ... does not exist` if they fire before the SA is+ visible to those endpoints -- the failure mode that left workspace e2e+ red on BigQuery before this wait existed.++ `ws-service-account-exists?` already swallows `NotFoundException` and+ returns false, so the loop just polls its boolean return."+ [^IAMClient iam-client ^String project-id ^String sa-email+ & {:keys [max-attempts interval-ms]+ :or {max-attempts 60+ interval-ms 1000}}]+ (log/infof "Waiting for service account %s to be visible to IAM..." sa-email)+ (loop [attempt 1]+ (cond+ (ws-service-account-exists? iam-client project-id sa-email)+ (log/infof "Service account %s is visible to IAM after %d attempt(s)" sa-email attempt)++ (>= attempt max-attempts)+ (throw (ex-info "Timeout waiting for service account to propagate"+ {:sa-email sa-email+ :attempts attempt}))++ :else+ (do+ (Thread/sleep ^long interval-ms)+ (recur (inc attempt))))))++(defn- ws-delete-service-account!+ "Delete a service account for a workspace. Idempotent - does nothing if SA doesn't exist."+ [^IAMClient iam-client ^String project-id workspace]+ (let [sa-id (ws-service-account-id workspace)+ sa-email (format "%s@%s.iam.gserviceaccount.com" sa-id project-id)+ sa-name (format "projects/%s/serviceAccounts/%s" project-id sa-email)]+ (when (ws-service-account-exists? iam-client project-id sa-email)+ (log/infof "Deleting service account %s" sa-email)+ (let [request (-> (DeleteServiceAccountRequest/newBuilder)+ (.setName sa-name)+ (.build))]+ (.deleteServiceAccount iam-client request)+ (log/infof "Deleted service account: %s" sa-email)))))++(defn- ws-grant-project-role!+ "Grant a project-level IAM role to a service account.+ This is needed for roles like bigquery.jobUser that must be granted at project level."+ [details ^String project-id ^String service-account-email ^String role]+ (log/infof "Granting project role %s to %s" role service-account-email)+ (let [creds (.createScoped (ws-service-account-credentials details)+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/cloud-platform")))+ settings (-> (ProjectsSettings/newBuilder)+ (.setCredentialsProvider (reify com.google.api.gax.core.CredentialsProvider+ (getCredentials [_] creds)))+ ^ProjectsSettings (.build))+ projects-client (ProjectsClient/create settings)+ resource (format "projects/%s" project-id)+ member (format "serviceAccount:%s" service-account-email)]+ (try+ (with-sa-propagation-retry! {}+ (fn []+ ;; Get current policy+ (let [get-request (-> (GetIamPolicyRequest/newBuilder)+ (.setResource resource)+ (.build))+ current-policy (.getIamPolicy projects-client get-request)]+ ;; Only add if not already granted+ (when-not (ws-has-role-binding? current-policy role member)+ (let [new-binding (-> (Binding/newBuilder)+ (.setRole role)+ (.addMembers member)+ (.build))+ updated-policy (-> (Policy/newBuilder current-policy)+ (.addBindings new-binding)+ (.build))+ set-request (-> (SetIamPolicyRequest/newBuilder)+ (.setResource resource)+ (.setPolicy updated-policy)+ (.build))]+ (.setIamPolicy projects-client set-request)+ (log/infof "Granted %s on project %s to %s" role project-id service-account-email))))))+ (finally+ (.close projects-client)))))++(defn- ws-create-service-account!+ "Create a service account for a workspace if it doesn't exist.+ Returns the service account email.++ The SA `description` field is built via [[ws-sa-description]] so CI cleanup+ can age-gate orphan SAs. See [[ws-sa-description->created-at]] for the parse+ side, and `metabase.test.data.bigquery-cloud-sdk` for the cleanup loop."+ [^IAMClient iam-client ^String project-id workspace]+ (let [sa-id (ws-service-account-id workspace)+ sa-email (format "%s@%s.iam.gserviceaccount.com" sa-id project-id)+ project-name (format "projects/%s" project-id)+ description (ws-sa-description (java.time.Instant/now))]+ ;; Check if already exists first+ (if (ws-service-account-exists? iam-client project-id sa-email)+ (log/debugf "Service account already exists: %s" sa-email)+ (do+ (log/infof "Creating service account %s in project %s" sa-id project-id)+ (let [request (-> (CreateServiceAccountRequest/newBuilder)+ (.setName project-name)+ (.setAccountId sa-id)+ (.setServiceAccount+ (-> (ServiceAccount/newBuilder)+ (.setDisplayName (format "Metabase Workspace %s" (:id workspace)))+ (.setDescription description)+ (.build)))+ (.build))]+ (.createServiceAccount iam-client request)+ (log/infof "Created service account: %s" sa-email))))+ sa-email))++(defmethod driver/grant-workspace-read-access! :bigquery-cloud-sdk+ [_driver database workspace schemas]+ ;; `schemas` is a vector of BigQuery dataset names. The project comes from the+ ;; connection's effective details — a Metabase `Database` row binds to one+ ;; project. Grant dataViewer at the dataset level so future tables also gain+ ;; access — matches the schema-wide semantics of the SQL drivers.+ (let [ws-sa-email (-> workspace :database_details :impersonate-service-account)+ details (driver.conn/effective-details database)+ client (ws-database-details->client details)+ project-id (bigquery.common/get-project-id details)]+ (log/infof "Granting dataset-level read access on %d dataset(s) %s to %s"+ (count schemas) (pr-str (vec schemas)) ws-sa-email)+ (doseq [dataset schemas+ :let [dataset-id (DatasetId/of project-id dataset)]]+ (log/infof "Granting dataViewer on %s/%s to %s" project-id dataset ws-sa-email)+ (ws-grant-dataset-acl! client dataset-id ws-sa-email "roles/bigquery.dataViewer")+ ;; Wait for the grant to propagate to the impersonated read path. Without+ ;; this, the next `can-connect-with-details?` running under the workspace+ ;; SA filters `listDatasets` by this dataset name, sees an empty list,+ ;; and throws `Looks like we cannot find any matching datasets`.+ (ws-wait-for-dataset-visible-via-impersonation! details ws-sa-email project-id dataset))))++(defmethod driver/destroy-workspace-isolation! :bigquery-cloud-sdk+ [_driver database workspace]+ (let [details (driver.conn/effective-details database)+ client (ws-database-details->client details)+ iam-client (ws-database-details->iam-client details)+ project-id (bigquery.common/get-project-id details)+ dataset-name (driver.u/workspace-isolation-namespace-name workspace)]+ (try+ (log/infof "Destroying BigQuery workspace isolation: dataset=%s" dataset-name)++ ;; Delete the dataset if it exists (deleteContents=true removes all tables)+ (drop-dataset! client project-id dataset-name)++ ;; Delete the service account (this also removes its IAM bindings)+ (ws-delete-service-account! iam-client project-id workspace)++ {:success true}+ (finally+ (.close iam-client)))))++(defmethod driver/init-workspace-isolation! :bigquery-cloud-sdk+ [_driver database workspace]+ (let [details (driver.conn/effective-details database)+ client (ws-database-details->client details)+ iam-client (ws-database-details->iam-client details)+ project-id (bigquery.common/get-project-id details)+ main-sa-email (.getClientEmail (ws-service-account-credentials details))+ dataset-name (driver.u/workspace-isolation-namespace-name workspace)]++ (try+ ;; Create the workspace service account (or get existing)+ (let [ws-sa-email (ws-create-service-account! iam-client project-id workspace)+ dataset-id (DatasetId/of project-id dataset-name)]++ (log/infof "Initializing BigQuery workspace isolation: dataset=%s, service-account=%s"+ dataset-name ws-sa-email)++ ;; Wait until the new SA is visible to IAM read + policy endpoints+ ;; before we hand its email to any grant call -- without this the+ ;; immediate `setIamPolicy` calls below race with GCP IAM's+ ;; eventual-consistency propagation and throw+ ;; `INVALID_ARGUMENT: ... does not exist`.+ (ws-wait-for-service-account! iam-client project-id ws-sa-email)++ ;; Grant main SA permission to impersonate workspace SA+ (ws-grant-impersonation-permission! iam-client project-id main-sa-email ws-sa-email)++ ;; Grant the workspace SA permission to run BigQuery jobs (queries) at project level+ ;; Note: We intentionally do NOT grant project-level dataEditor as that would give+ ;; access to all datasets. The workspace SA only gets dataEditor on its isolated dataset.+ (ws-grant-project-role! details project-id ws-sa-email "roles/bigquery.jobUser")++ ;; Wait for IAM permissions to propagate by polling until impersonation works+ (ws-wait-for-impersonation-ready! details ws-sa-email)++ ;; Create the isolated dataset if it doesn't exist (using main SA credentials, not impersonated)+ (create-dataset! client project-id dataset-name+ {:description (format "Metabase workspace isolation for workspace %s" (:id workspace))})++ ;; Grant the workspace service account dataEditor role on the isolated dataset+ ;; dataEditor allows: create/update/delete tables, insert/update/delete data+ (ws-grant-dataset-acl! client dataset-id ws-sa-email "roles/bigquery.dataEditor")++ ;; Return workspace connection details for impersonation+ ;; :user is used by grant-read-access-to-tables! to know which SA to grant access to+ ;; :impersonate-service-account is used by the connection swap to use impersonated credentials+ {:schema dataset-name+ :database_details {:user ws-sa-email+ :impersonate-service-account ws-sa-email}})+ (finally+ (.close iam-client)))))++(defmethod driver/check-isolation-permissions :bigquery-cloud-sdk+ [driver database test-table]+ ;; BigQuery uses GCP IAM APIs instead of SQL, so we can't use transaction rollback.+ ;; We run the actual init/grant/destroy operations and clean up immediately.+ (let [test-workspace {:id perm-check-workspace-id+ :name "_mb_perm_check_"}]+ (driver.conn/with-admin-connection+ (try+ (let [init-result (try+ (driver/init-workspace-isolation! driver database test-workspace)+ (catch Exception e+ (throw (ex-info (tru "Failed to initialize workspace isolation: {0}" (ex-message e))+ {:step :init} e))))+ workspace-with-details (merge test-workspace init-result)]+ (when test-table+ (try+ ;; `grant-workspace-read-access!` takes a vector of schema-name strings.+ ;; Pass the test-table's dataset.+ (driver/grant-workspace-read-access! driver database workspace-with-details+ [(:schema test-table)])+ (catch Exception e+ (throw (ex-info (tru "Failed to grant read access to dataset {0}: {1}"+ (:schema test-table) (ex-message e))+ {:step :grant :table test-table} e)))))+ (try+ (driver/destroy-workspace-isolation! driver database workspace-with-details)+ (catch Exception e+ (throw (ex-info (tru "Failed to destroy workspace isolation: {0}" (ex-message e))+ {:step :destroy} e)))))+ nil+ (catch Exception e+ (ex-message e))+ (finally+ (try+ (driver/destroy-workspace-isolation! driver database test-workspace)+ (catch Exception _ nil)))))))+diff --git a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/workspace_isolation_test.clj b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/workspace_isolation_test.cljnew file mode 100644index 00000000000..c3a0678cba1--- /dev/null+++ b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/workspace_isolation_test.clj@@ -0,0 +1,459 @@+(ns ^:mb/driver-tests metabase.driver.bigquery-cloud-sdk.workspace-isolation-test+ "BigQuery-specific tests for workspace database isolation. BigQuery isn't a JDBC+ driver — its workspace isolation goes through GCP IAM (a per-workspace service+ account that the admin SA impersonates) rather than SQL user/role grants. So+ this test mirrors the JDBC sibling's *shape* (see+ `metabase-enterprise.workspaces.driver-isolation-test`) but drives BigQuery+ through the GCP SDK directly: the admin BigQuery client seeds a per-run input+ dataset/table, then `init-workspace-isolation! :bigquery-cloud-sdk` creates the+ workspace SA and grants impersonation, after which an `ImpersonatedCredentials`+ client runs queries *as* that SA so we can probe the actual perms.++ IAM permissions required on the admin service account+ (`MB_BIGQUERY_CLOUD_SDK_TEST_SERVICE_ACCOUNT_JSON`):++ - `iam.serviceAccounts.create` / `.delete` / `.get` (project-level: Service Account Admin)+ - `iam.serviceAccounts.getIamPolicy` / `.setIamPolicy` on the workspace SAs+ it creates (granted automatically when create succeeds, but the role+ `roles/iam.serviceAccountAdmin` covers it)+ - `iam.serviceAccounts.getAccessToken` on the workspace SA (granted by+ `ws-grant-impersonation-permission!` at init time — needs `setIamPolicy`)+ - `resourcemanager.projects.setIamPolicy` (to grant the workspace SA+ `roles/bigquery.jobUser` at the project level — needs Project IAM Admin+ or a custom role with `resourcemanager.projects.setIamPolicy`)+ - `bigquery.datasets.create` / `.delete` and `bigquery.tables.*` on the+ project (`roles/bigquery.dataEditor` covers everything we need)++ If any are missing, `init-workspace-isolation!` fails fast with a 403 — the+ test surfaces that as a setup error and the `finally` block still runs cleanup."+ (:require+ [clojure.test :refer :all]+ [metabase.driver :as driver]+ [metabase.driver.bigquery-cloud-sdk.workspace-test-util :as bq.util]+ [metabase.driver.bigquery-cloud-sdk.workspaces :as bigquery.ws]+ [metabase.driver.util :as driver.u]+ [metabase.test :as mt]+ [metabase.util :as u]+ [metabase.util.log :as log])+ (:import+ (com.google.auth.oauth2 ServiceAccountCredentials)+ (com.google.cloud.bigquery+ BigQuery+ BigQuery$DatasetListOption+ BigQuery$JobOption+ Dataset+ FieldValueList+ QueryJobConfiguration+ TableResult)+ (com.google.cloud.iam.admin.v1 IAMClient)))++(set! *warn-on-reflection* true)++(defn- random-suffix []+ (subs (str (random-uuid)) 0 8))++;; BQ helpers (admin client, IAM client, impersonated client, dataset CRUD,+;; assertion helpers) live in `metabase.driver.bigquery-cloud-sdk.workspace-test-util`+;; and are aliased as `bq.util` so the workspaces full-e2e test+;; (`metabase-enterprise.workspaces.e2e-test`) can share them.++(deftest ^:synchronized workspace-isolation-perms-bigquery-test+ (mt/test-driver :bigquery-cloud-sdk+ (testing "workspace SA gets read-only access to input dataset, full access to output dataset"+ (let [database (mt/db)+ details (:details database)+ ;; The BQ test extension only populates `:project-id` in `details` when+ ;; `MB_BIGQUERY_CLOUD_SDK_TEST_PROJECT_ID` is set (see test/data/+ ;; bigquery_cloud_sdk.clj:69-70). When it isn't, fall back to the+ ;; project embedded in the admin service-account JSON — every+ ;; Google-issued SA key carries the project it was created in.+ project-id (or (:project-id details)+ (.getProjectId (bq.util/admin-credentials details)))+ admin-creds (bq.util/admin-credentials details)+ admin-client (bq.util/admin-client details)+ iam-client (bq.util/iam-client details)+ run-id (random-suffix)+ in-dataset (str "mb_iso_in_" run-id)+ src-name (str "ws_iso_src_" run-id)+ sneaky-name (str "ws_iso_sneaky_" run-id)+ out-name (str "ws_iso_out_" run-id)+ workspace {:id (Long/parseLong run-id 16)+ :name (str "wsd-permstest-" run-id)}+ ws-state (atom (merge workspace+ {:schema (driver.u/workspace-isolation-namespace-name workspace)}))+ qual (fn [ds tbl] (format "`%s.%s.%s`" project-id ds tbl))+ run-sql (fn [^BigQuery c sql]+ (.query c (QueryJobConfiguration/of sql)+ (into-array BigQuery$JobOption [])))]+ (try+ (bq.util/create-dataset! admin-client project-id in-dataset)+ (run-sql admin-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual in-dataset src-name)))+ (run-sql admin-client (format "INSERT INTO %s (id, v) VALUES (1, 'a')" (qual in-dataset src-name)))+ (let [init-result (driver/init-workspace-isolation! :bigquery-cloud-sdk database workspace)+ ws-with-details (merge workspace init-result)+ _ (reset! ws-state ws-with-details)+ ws-sa-email (-> ws-with-details :database_details :impersonate-service-account)+ user-client (bq.util/impersonated-client admin-creds ws-sa-email project-id)+ out-dataset (:schema ws-with-details)]+ (driver/grant-workspace-read-access! :bigquery-cloud-sdk database ws-with-details+ [in-dataset])+ (testing "workspace SA can SELECT from a granted input table"+ (let [result (run-sql user-client+ (format "SELECT id, v FROM %s ORDER BY id" (qual in-dataset src-name)))+ rows (mapv (fn [^FieldValueList row]+ {:id (.getLongValue (.get row "id"))+ :v (.getStringValue (.get row "v"))})+ (.iterateAll ^TableResult result))]+ (is (= [{:id 1 :v "a"}] rows))))+ (testing "workspace SA cannot write to or DDL against the input dataset"+ ;; Beyond INSERT/UPDATE/DELETE we exercise ALTER TABLE and TRUNCATE+ ;; — BigQuery routes these through different IAM-permission checks+ ;; than DML, so a missed permission would slip through INSERT-only+ ;; coverage. Every assertion expects a 403 from the workspace SA.+ (doseq [[label sql] [[:insert (format "INSERT INTO %s (id, v) VALUES (2, 'b')" (qual in-dataset src-name))]+ [:update (format "UPDATE %s SET v = 'x' WHERE id = 1" (qual in-dataset src-name))]+ [:delete (format "DELETE FROM %s WHERE id = 1" (qual in-dataset src-name))]+ [:create-table (format "CREATE TABLE %s (id INT64)" (qual in-dataset sneaky-name))]+ [:drop-table (format "DROP TABLE %s" (qual in-dataset src-name))]+ [:alter-add-col (format "ALTER TABLE %s ADD COLUMN extra INT64" (qual in-dataset src-name))]+ [:truncate (format "TRUNCATE TABLE %s" (qual in-dataset src-name))]]]+ (bq.util/expect-write-denied! user-client sql label)))+ (testing "workspace SA has full read+write access to its own output dataset"+ (run-sql user-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual out-dataset out-name)))+ (run-sql user-client (format "INSERT INTO %s (id, v) VALUES (1, 'a')" (qual out-dataset out-name)))+ (run-sql user-client (format "UPDATE %s SET v = 'b' WHERE id = 1" (qual out-dataset out-name)))+ (run-sql user-client (format "DELETE FROM %s WHERE id = 1" (qual out-dataset out-name)))+ (run-sql user-client (format "DROP TABLE %s" (qual out-dataset out-name))))+ (testing "re-granting the same input table is idempotent"+ ;; Second grant call with an identical table list must not throw and+ ;; must not change the workspace SA's perms — still SELECT, still no+ ;; INSERT. Catches both noisy re-grant failures and silent IAM-binding+ ;; escalation.+ (driver/grant-workspace-read-access! :bigquery-cloud-sdk database ws-with-details+ [in-dataset])+ (let [result (run-sql user-client (format "SELECT id FROM %s" (qual in-dataset src-name)))+ rows (mapv (fn [^FieldValueList row] {:id (.getLongValue (.get row "id"))})+ (.iterateAll ^TableResult result))]+ (is (= [{:id 1}] rows)))+ (bq.util/expect-write-denied! user-client+ (format "INSERT INTO %s (id, v) VALUES (3, 'c')" (qual in-dataset src-name))+ :insert-after-regrant))+ (testing "workspace SA cannot create a new dataset outside its own workspace"+ ;; The workspace SA holds `roles/bigquery.jobUser` at project level+ ;; plus `dataEditor` on its own output dataset. Creating a *new*+ ;; dataset needs project-level `bigquery.datasets.create`, which+ ;; only project-IAM admins / BigQuery admins have. If this+ ;; succeeds, the workspace SA can grow its footprint indefinitely+ ;; — an escalation hole. We clean up only if the assertion+ ;; spuriously fails so a real bug doesn't leave a stray dataset.+ (let [hacker-ds (str "ws_iso_hacker_" run-id)+ succeeded (atom false)]+ (try+ (bigquery.ws/create-dataset! user-client project-id hacker-ds)+ (reset! succeeded true)+ (is false (format "workspace SA unexpectedly created dataset %s" hacker-ds))+ (catch Throwable t+ (is (some? t)+ (format "workspace SA correctly denied dataset creation: %s" (ex-message t)))))+ (when @succeeded+ (try (bigquery.ws/drop-dataset! admin-client project-id hacker-ds)+ (catch Throwable _ nil)))))+ (testing "workspace SA cannot exfiltrate data via storage/external escapes"+ ;; The two BigQuery primitives that bridge to GCS (and thus would+ ;; let a workspace SA exfiltrate or inject data outside the BQ+ ;; sandbox) are CREATE EXTERNAL TABLE (read GCS object as a+ ;; queryable table) and EXPORT DATA (write query result to GCS).+ ;; Both require GCS-side `storage.objects.get` / `.create` on the+ ;; bucket, which the workspace SA has on no bucket — its IAM+ ;; bindings are scoped to BigQuery resources only. We point at a+ ;; freshly-named bucket so the response can be either 403+ ;; (perm-denied) or 404 (bucket-not-found); both confirm the SA+ ;; cannot reach GCS. Accept any 4xx via expect-bq-denied!.+ (let [hacker-uri (format "gs://nonexistent-mb-iso-%s/data.csv" run-id)+ ext-tbl (qual out-dataset (str "ws_iso_ext_" run-id))]+ (bq.util/expect-denied! user-client+ (format "CREATE EXTERNAL TABLE %s OPTIONS (uris = ['%s'], format = 'CSV')"+ ext-tbl hacker-uri)+ :create-external-table)+ (bq.util/expect-denied! user-client+ (format "EXPORT DATA OPTIONS (uri = '%s', format = 'CSV') AS SELECT 1 AS x"+ hacker-uri)+ :export-data)))+ (testing "after destroy-workspace-isolation!, the workspace's footprint is gone"+ ;; Explicit destroy here (instead of relying on the `finally`) lets us+ ;; assert the cleanup actually happened. destroy is idempotent so+ ;; finally re-calling it is a no-op.+ (driver/destroy-workspace-isolation! :bigquery-cloud-sdk database ws-with-details)+ (bq.util/verify-destroy! project-id admin-client out-dataset)))+ (finally+ (try (driver/destroy-workspace-isolation! :bigquery-cloud-sdk database @ws-state)+ (catch Throwable t+ (log/warn t "destroy-workspace-isolation! failed for :bigquery-cloud-sdk during test cleanup")))+ ;; Belt-and-suspenders: directly delete the input dataset and workspace SA,+ ;; regardless of whether destroy succeeded. Both are idempotent.+ (try (bq.util/drop-dataset! admin-client project-id in-dataset) (catch Throwable _ nil))+ (try (bq.util/delete-sa-direct! iam-client project-id workspace) (catch Throwable _ nil))+ (u/ignore-exceptions (.close iam-client))))))))++(deftest ^:synchronized cross-workspace-isolation-perms-bigquery-test+ ;; BigQuery sibling of `cross-workspace-isolation-perms-test`. Provisions two+ ;; workspaces, each with its own service account and dataset, and asserts that+ ;; A's impersonated SA cannot reach B's dataset, B's grants, or B's dataset+ ;; through `listDatasets` enumeration.+ (mt/test-driver :bigquery-cloud-sdk+ (testing "two workspaces on the same project are mutually isolated"+ (let [database (mt/db)+ details (:details database)+ admin-creds (#'bigquery.ws/ws-service-account-credentials details)+ project-id (or (:project-id details)+ (.getProjectId ^ServiceAccountCredentials admin-creds))+ admin-client (#'bigquery.ws/ws-database-details->client details)+ iam-client (#'bigquery.ws/ws-database-details->iam-client details)+ test-id (random-suffix)+ ws-a-id (random-suffix)+ ws-b-id (random-suffix)+ ;; Each workspace gets its own input dataset. The BQ grant impl+ ;; grants `dataViewer` at the *dataset* level (matching the+ ;; schema-wide semantics of the SQL drivers), so co-locating+ ;; A's and B's source tables in a single dataset would let+ ;; either workspace SELECT both tables as a correctly-scoped+ ;; grant rather than a leak. Splitting per workspace makes the+ ;; `:select-other-grant` assertion meaningful.+ in-dataset-a (str "mb_iso_in_a_" test-id)+ in-dataset-b (str "mb_iso_in_b_" test-id)+ src-a-name (str "ws_iso_src_a_" test-id)+ src-b-name (str "ws_iso_src_b_" test-id)+ sneaky-name (str "ws_iso_sneaky_" test-id)+ b-secret (str "ws_iso_secret_" ws-b-id)+ ws-a {:id (Long/parseLong ws-a-id 16)+ :name (str "wsd-A-" ws-a-id)}+ ws-b {:id (Long/parseLong ws-b-id 16)+ :name (str "wsd-B-" ws-b-id)}+ ws-a-state (atom (merge ws-a+ {:schema (driver.u/workspace-isolation-namespace-name ws-a)}))+ ws-b-state (atom (merge ws-b+ {:schema (driver.u/workspace-isolation-namespace-name ws-b)}))+ qual (fn [ds tbl] (format "`%s.%s.%s`" project-id ds tbl))+ run-sql (fn [^BigQuery c sql]+ (.query c (QueryJobConfiguration/of sql)+ (into-array BigQuery$JobOption [])))]+ (try+ (bigquery.ws/create-dataset! admin-client project-id in-dataset-a)+ (bigquery.ws/create-dataset! admin-client project-id in-dataset-b)+ (run-sql admin-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual in-dataset-a src-a-name)))+ (run-sql admin-client (format "INSERT INTO %s (id, v) VALUES (1, 'a')" (qual in-dataset-a src-a-name)))+ (run-sql admin-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual in-dataset-b src-b-name)))+ (run-sql admin-client (format "INSERT INTO %s (id, v) VALUES (1, 'b')" (qual in-dataset-b src-b-name)))+ (let [init-a (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-a)+ init-b (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-b)+ ws-a-full (merge ws-a init-a)+ ws-b-full (merge ws-b init-b)+ _ (reset! ws-a-state ws-a-full)+ _ (reset! ws-b-state ws-b-full)+ a-sa-email (-> ws-a-full :database_details :impersonate-service-account)+ b-sa-email (-> ws-b-full :database_details :impersonate-service-account)+ a-client (bq.util/impersonated-client admin-creds a-sa-email project-id)+ b-client (bq.util/impersonated-client admin-creds b-sa-email project-id)+ out-b-ds (:schema ws-b-full)]+ ;; Each workspace is granted access only to its own input dataset —+ ;; A's grant must not let A read src-b in B's dataset, and vice-versa.+ (driver/grant-workspace-read-access! :bigquery-cloud-sdk database ws-a-full+ [in-dataset-a])+ (driver/grant-workspace-read-access! :bigquery-cloud-sdk database ws-b-full+ [in-dataset-b])+ ;; B populates its own output dataset with a table A should never reach.+ (run-sql b-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual out-b-ds b-secret)))+ (run-sql b-client (format "INSERT INTO %s (id, v) VALUES (1, 'b-only')" (qual out-b-ds b-secret)))+ (testing "workspace A's SA cannot SELECT from workspace B's output table"+ (bq.util/expect-denied! a-client+ (format "SELECT id, v FROM %s" (qual out-b-ds b-secret))+ :select-other-output))+ (testing "workspace A's SA cannot write to or DDL against workspace B's output dataset"+ (doseq [[label sql] [[:insert (format "INSERT INTO %s (id, v) VALUES (2, 'x')" (qual out-b-ds b-secret))]+ [:update (format "UPDATE %s SET v = 'x' WHERE id = 1" (qual out-b-ds b-secret))]+ [:delete (format "DELETE FROM %s WHERE id = 1" (qual out-b-ds b-secret))]+ [:create-table (format "CREATE TABLE %s (id INT64)" (qual out-b-ds sneaky-name))]+ [:drop-table (format "DROP TABLE %s" (qual out-b-ds b-secret))]+ [:alter-add-col (format "ALTER TABLE %s ADD COLUMN extra INT64" (qual out-b-ds b-secret))]+ [:truncate (format "TRUNCATE TABLE %s" (qual out-b-ds b-secret))]]]+ (bq.util/expect-denied! a-client sql label)))+ (testing "workspace A's SA cannot SELECT a source table that was only granted to workspace B"+ (bq.util/expect-denied! a-client+ (format "SELECT id, v FROM %s" (qual in-dataset-b src-b-name))+ :select-other-grant))+ (testing "workspace A's listDatasets does not enumerate workspace B's output dataset"+ ;; A's SA has only `roles/bigquery.jobUser` at the project plus `dataEditor`+ ;; on its own dataset, so listDatasets either returns A's own dataset+ ;; (filtered by visibility) or throws permission-denied. Either is fine —+ ;; what matters is that B's dataset never appears.+ (let [^com.google.api.gax.paging.Page+ page (try (.listDatasets a-client (into-array BigQuery$DatasetListOption []))+ (catch Throwable t+ (log/infof "listDatasets denied for ws-A SA (acceptable): %s" (ex-message t))+ nil))+ names (when page+ (->> (.iterateAll page)+ (map (fn [^Dataset d] (.getDataset (.getDatasetId d))))+ set))]+ (is (not (contains? (or names #{}) out-b-ds))+ (format "A unexpectedly enumerates B's dataset %s. visible=%s"+ out-b-ds (or names #{}))))))+ (finally+ (doseq [w [@ws-a-state @ws-b-state]]+ (try (driver/destroy-workspace-isolation! :bigquery-cloud-sdk database w)+ (catch Throwable t+ (log/warn t "destroy-workspace-isolation! failed for :bigquery-cloud-sdk during cross-workspace test cleanup"))))+ ;; Belt-and-suspenders: drop input datasets + delete each workspace SA directly.+ (doseq [ds [in-dataset-a in-dataset-b]]+ (try (bigquery.ws/drop-dataset! admin-client project-id ds) (catch Throwable _ nil)))+ (doseq [w [ws-a ws-b]]+ (try (bq.util/delete-sa-direct! iam-client project-id w) (catch Throwable _ nil)))+ (u/ignore-exceptions (.close ^IAMClient iam-client))))))))++(deftest ^:synchronized grant-accumulation-bigquery-test+ ;; BigQuery sibling of `grant-accumulation-test`. Pins the *additive* contract+ ;; of `grant-workspace-read-access!` for BigQuery: each call adds tables to+ ;; the workspace SA's read-set without revoking previously-granted ones.+ ;; BQ's grant impl adds a `dataViewer` IAM binding per table on each call,+ ;; which is naturally additive — this test prevents a future flip to+ ;; revoke-then-grant from passing unnoticed.+ (mt/test-driver :bigquery-cloud-sdk+ (testing "grant-workspace-read-access! is additive across multiple calls"+ (let [database (mt/db)+ details (:details database)+ project-id (or (:project-id details)+ (.getProjectId (bq.util/admin-credentials details)))+ admin-creds (bq.util/admin-credentials details)+ admin-client (bq.util/admin-client details)+ iam-client (bq.util/iam-client details)+ run-id (random-suffix)+ ;; Two SEPARATE input datasets: BQ's `dataViewer` is dataset-scoped,+ ;; so a per-table read-denied assertion requires the tables live in+ ;; different datasets.+ in-a-dataset (str "mb_iso_in_a_" run-id)+ in-b-dataset (str "mb_iso_in_b_" run-id)+ src-a-name (str "ws_iso_src_a_" run-id)+ src-b-name (str "ws_iso_src_b_" run-id)+ workspace {:id (Long/parseLong run-id 16)+ :name (str "wsd-grantaccum-" run-id)}+ ws-state (atom (merge workspace {:schema (driver.u/workspace-isolation-namespace-name workspace)}))+ qual (fn [ds tbl] (format "`%s.%s.%s`" project-id ds tbl))+ run-sql (fn [^BigQuery c sql]+ (.query c (QueryJobConfiguration/of sql)+ (into-array BigQuery$JobOption [])))+ select-id (fn [^BigQuery c sql]+ (mapv (fn [^FieldValueList row] {:id (.getLongValue (.get row "id"))})+ (.iterateAll ^TableResult (run-sql c sql))))]+ (try+ (bigquery.ws/create-dataset! admin-client project-id in-a-dataset)+ (bigquery.ws/create-dataset! admin-client project-id in-b-dataset)+ (run-sql admin-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual in-a-dataset src-a-name)))+ (run-sql admin-client (format "INSERT INTO %s (id, v) VALUES (1, 'a')" (qual in-a-dataset src-a-name)))+ (run-sql admin-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual in-b-dataset src-b-name)))+ (run-sql admin-client (format "INSERT INTO %s (id, v) VALUES (1, 'b')" (qual in-b-dataset src-b-name)))+ (let [init-result (driver/init-workspace-isolation! :bigquery-cloud-sdk database workspace)+ ws-with-details (merge workspace init-result)+ _ (reset! ws-state ws-with-details)+ ws-sa-email (-> ws-with-details :database_details :impersonate-service-account)+ user-client (bq.util/impersonated-client admin-creds ws-sa-email project-id)]+ ;; First grant: only dataset A.+ (driver/grant-workspace-read-access! :bigquery-cloud-sdk database ws-with-details+ [in-a-dataset])+ (testing "after first grant, A is readable and B is not"+ (is (= [{:id 1}] (select-id user-client (format "SELECT id FROM %s" (qual in-a-dataset src-a-name)))))+ (bq.util/expect-denied! user-client+ (format "SELECT id FROM %s" (qual in-b-dataset src-b-name))+ :select-b-before-grant))+ ;; Second grant: only dataset B. The additive contract means A's+ ;; grant must still be in effect afterward.+ (driver/grant-workspace-read-access! :bigquery-cloud-sdk database ws-with-details+ [in-b-dataset])+ (testing "after second grant, both A and B are readable (A's binding accumulated)"+ (is (= [{:id 1}] (select-id user-client (format "SELECT id FROM %s" (qual in-a-dataset src-a-name)))))+ (is (= [{:id 1}] (select-id user-client (format "SELECT id FROM %s" (qual in-b-dataset src-b-name)))))))+ (finally+ (try (driver/destroy-workspace-isolation! :bigquery-cloud-sdk database @ws-state)+ (catch Throwable t+ (log/warn t "destroy-workspace-isolation! failed for :bigquery-cloud-sdk during grant-accumulation test cleanup")))+ (try (bigquery.ws/drop-dataset! admin-client project-id in-a-dataset) (catch Throwable _ nil))+ (try (bigquery.ws/drop-dataset! admin-client project-id in-b-dataset) (catch Throwable _ nil))+ (try (bq.util/delete-sa-direct! iam-client project-id workspace) (catch Throwable _ nil))+ (u/ignore-exceptions (.close ^IAMClient iam-client))))))))++(deftest ^:synchronized init-handles-pre-existing-dataset-bigquery-test+ ;; BigQuery sibling of `init-handles-pre-existing-namespace-test`. The+ ;; output-dataset name is deterministic from `workspace.id` (see+ ;; `driver.u/workspace-isolation-namespace-name`), so init can land on an+ ;; existing dataset (partial prior init, another process, etc.). The driver's+ ;; `bigquery.ws/create-dataset!` is documented as idempotent ("no-op when the+ ;; dataset already exists"), so init silently succeeds. This test pins that+ ;; behavior — init must not crash on collision and the standard contract must+ ;; still hold afterward. Same KNOWN-LIMITATION caveat about pre-existing data+ ;; in the colliding dataset as the JDBC counterpart.+ (mt/test-driver :bigquery-cloud-sdk+ (testing "init-workspace-isolation! is robust when its target output dataset already exists"+ (let [database (mt/db)+ details (:details database)+ project-id (or (:project-id details)+ (.getProjectId (bq.util/admin-credentials details)))+ admin-creds (bq.util/admin-credentials details)+ admin-client (bq.util/admin-client details)+ iam-client (bq.util/iam-client details)+ run-id (random-suffix)+ in-dataset (str "mb_iso_in_" run-id)+ src-name (str "ws_iso_src_" run-id)+ out-name (str "ws_iso_out_" run-id)+ workspace {:id (Long/parseLong run-id 16)+ :name (str "wsd-collision-" run-id)}+ out-dataset (driver.u/workspace-isolation-namespace-name workspace)+ ws-state (atom (merge workspace {:schema out-dataset}))+ qual (fn [ds tbl] (format "`%s.%s.%s`" project-id ds tbl))+ run-sql (fn [^BigQuery c sql]+ (.query c (QueryJobConfiguration/of sql)+ (into-array BigQuery$JobOption [])))]+ (try+ (bigquery.ws/create-dataset! admin-client project-id in-dataset)+ (run-sql admin-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual in-dataset src-name)))+ (run-sql admin-client (format "INSERT INTO %s (id, v) VALUES (1, 'a')" (qual in-dataset src-name)))+ ;; Pre-create the output dataset at exactly the name init will target,+ ;; before init runs.+ (bigquery.ws/create-dataset! admin-client project-id out-dataset)+ (let [init-result (driver/init-workspace-isolation! :bigquery-cloud-sdk database workspace)+ ws-with-details (merge workspace init-result)+ _ (reset! ws-state ws-with-details)+ ws-sa-email (-> ws-with-details :database_details :impersonate-service-account)+ user-client (bq.util/impersonated-client admin-creds ws-sa-email project-id)]+ (driver/grant-workspace-read-access! :bigquery-cloud-sdk database ws-with-details+ [in-dataset])+ (testing "init succeeded against the pre-existing dataset"+ (is (some? init-result)))+ (testing "workspace SA has full read+write access to its output dataset post-collision"+ (run-sql user-client (format "CREATE TABLE %s (id INT64, v STRING)" (qual out-dataset out-name)))+ (run-sql user-client (format "INSERT INTO %s (id, v) VALUES (1, 'a')" (qual out-dataset out-name)))+ (let [result (run-sql user-client (format "SELECT id, v FROM %s" (qual out-dataset out-name)))+ rows (mapv (fn [^FieldValueList row]+ {:id (.getLongValue (.get row "id"))+ :v (.getStringValue (.get row "v"))})+ (.iterateAll ^TableResult result))]+ (is (= [{:id 1 :v "a"}] rows)))+ (run-sql user-client (format "DROP TABLE %s" (qual out-dataset out-name))))+ (testing "workspace SA retains read-only access to input dataset post-collision"+ (let [result (run-sql user-client (format "SELECT id FROM %s" (qual in-dataset src-name)))+ rows (mapv (fn [^FieldValueList row] {:id (.getLongValue (.get row "id"))})+ (.iterateAll ^TableResult result))]+ (is (= [{:id 1}] rows)))+ (bq.util/expect-write-denied! user-client+ (format "INSERT INTO %s (id, v) VALUES (2, 'b')" (qual in-dataset src-name))+ :insert-input-after-collision)))+ (finally+ (try (driver/destroy-workspace-isolation! :bigquery-cloud-sdk database @ws-state)+ (catch Throwable t+ (log/warn t "destroy-workspace-isolation! failed for :bigquery-cloud-sdk during collision test cleanup")))+ (try (bigquery.ws/drop-dataset! admin-client project-id in-dataset) (catch Throwable _ nil))+ ;; Belt-and-suspenders for the colliding output dataset: destroy+ ;; should have dropped it, but if init never reached the+ ;; create-dataset step (e.g., earlier failure) destroy might+ ;; not know to drop it. Idempotent.+ (try (bigquery.ws/drop-dataset! admin-client project-id out-dataset) (catch Throwable _ nil))+ (try (bq.util/delete-sa-direct! iam-client project-id workspace) (catch Throwable _ nil))+ (u/ignore-exceptions (.close ^IAMClient iam-client))))))))diff --git a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/workspace_test_util.clj b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/workspace_test_util.cljnew file mode 100644index 00000000000..7dbade38384--- /dev/null+++ b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/workspace_test_util.clj@@ -0,0 +1,226 @@+(ns metabase.driver.bigquery-cloud-sdk.workspace-test-util+ "Shared BigQuery test helpers for workspace-isolation tests. Two consumers:++ - `metabase.driver.bigquery-cloud-sdk.workspace-isolation-test` — the BQ-only+ contract test for `init`/`grant`/`destroy`/cross-workspace.+ - `metabase-enterprise.workspaces.e2e-test` — the cross-driver full e2e that+ needs BQ branches in its JDBC-shaped helpers.++ The BQ driver source provides admin-client + project-id helpers as private+ fns; this ns re-exports them via `#'` so both consumers share one path+ instead of each duplicating `BigQueryOptions/newBuilder` plumbing."+ (:require+ [clojure.test :refer [is testing]]+ [metabase.driver.bigquery-cloud-sdk :as bigquery]+ [metabase.driver.bigquery-cloud-sdk.common :as bigquery.common]+ [metabase.driver.bigquery-cloud-sdk.workspaces :as bigquery.ws]+ [metabase.util :as u])+ (:import+ (com.google.api.gax.core CredentialsProvider)+ (com.google.auth.oauth2 ImpersonatedCredentials ServiceAccountCredentials)+ (com.google.cloud.bigquery+ BigQuery+ BigQuery$DatasetDeleteOption+ BigQuery$DatasetOption+ BigQuery$JobOption+ BigQuery$TableListOption+ BigQueryException+ BigQueryOptions+ DatasetId+ DatasetInfo+ FieldValueList+ Table+ TableResult+ QueryJobConfiguration)+ (com.google.cloud.iam.admin.v1 IAMClient IAMSettings)+ (java.io ByteArrayInputStream)))++(set! *warn-on-reflection* true)++;;; -------------------- Credentials + clients --------------------++(defn admin-credentials+ "Parse `ServiceAccountCredentials` from the admin SA JSON in `details`. Used+ both to build the admin BigQuery client and as the source for+ `ImpersonatedCredentials` against the workspace SA."+ ^ServiceAccountCredentials [details]+ (ServiceAccountCredentials/fromStream+ (ByteArrayInputStream. (.getBytes ^String (:service-account-json details)))))++(defn admin-client+ "Build an admin `BigQuery` client for `details`. Thin re-export of the BQ+ driver's private `database-details->client` so workspace tests don't need to+ reach into the driver themselves."+ ^BigQuery [details]+ (#'bigquery/database-details->client details))++(defn iam-client+ "Build an IAM admin client scoped to `cloud-platform` for `details`. Used to+ create / delete / grant impersonation on workspace service accounts."+ ^IAMClient [details]+ (let [creds (.createScoped (admin-credentials details)+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/cloud-platform")))]+ (IAMClient/create+ (-> (doto (IAMSettings/newBuilder)+ (.setCredentialsProvider (reify CredentialsProvider+ (getCredentials [_] creds))))+ .build))))++(defn impersonated-client+ "BigQuery client that authenticates as `ws-sa-email` via service-account+ impersonation. Requires the admin creds to hold+ `iam.serviceAccounts.getAccessToken` on `ws-sa-email`, which the driver's+ `init-workspace-isolation!` grants during provisioning."+ ^BigQuery [^ServiceAccountCredentials admin-creds ws-sa-email project-id]+ (let [imp-creds (ImpersonatedCredentials/create+ admin-creds+ ws-sa-email+ nil+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/bigquery"))+ 3600)]+ (-> (doto (BigQueryOptions/newBuilder)+ (.setCredentials imp-creds)+ (.setProjectId project-id))+ .build+ .getService)))++(defn project-id+ "Project ID for `details`: explicit `:project-id` field if set, else parsed+ out of the admin SA JSON. Thin re-export of the BQ driver's+ `bigquery-cloud-sdk.common/get-project-id`."+ [details]+ (bigquery.common/get-project-id details))++;;; -------------------- Dataset CRUD --------------------++(defn create-dataset!+ "Create a BQ dataset. Idempotent: no-op if it already exists."+ [^BigQuery client project-id dataset-name]+ (.create client+ (.build (DatasetInfo/newBuilder (DatasetId/of project-id dataset-name)))+ (u/varargs BigQuery$DatasetOption [])))++(defn drop-dataset!+ "Drop a BQ dataset and its contents. Idempotent: no-op if not present.+ Distinct from `metabase.test.data.bigquery-cloud-sdk/destroy-dataset!`,+ which also writes to the test-tracking table — workspace tests don't want+ that side-effect."+ [^BigQuery client project-id dataset-name]+ (let [ds-id (DatasetId/of project-id dataset-name)]+ (when (.getDataset client ds-id (u/varargs BigQuery$DatasetOption []))+ (.delete client ds-id+ (u/varargs BigQuery$DatasetDeleteOption+ [(BigQuery$DatasetDeleteOption/deleteContents)])))))++(defn list-tables+ "Return a vector of `{:schema dataset :table table-id}` maps for every table+ in `dataset-name`. Empty vector when the dataset doesn't exist."+ [^BigQuery client project-id dataset-name]+ (let [ds-id (DatasetId/of project-id dataset-name)]+ (if-let [_ds (.getDataset client ds-id (u/varargs BigQuery$DatasetOption []))]+ (vec (for [^Table t (.iterateAll (.listTables client ds-id (u/varargs BigQuery$TableListOption [])))]+ {:schema dataset-name :table (.. t getTableId getTable)}))+ [])))++;;; -------------------- SQL execution --------------------++(defn execute!+ "Run a BigQuery SQL string on `client`, returning nil. Mirrors `jdbc/execute!`+ semantics — for DDL/DML the result is uninteresting. Distinct from+ `metabase.test.data.bigquery-cloud-sdk/execute!` which runs as the test admin+ SA only; this one runs on whatever client you pass (admin or impersonated)."+ [^BigQuery client sql]+ (.query client (QueryJobConfiguration/of sql) (into-array BigQuery$JobOption []))+ nil)++(defn query+ "Run a SELECT and eagerly materialize results to `[{:col val ...}]`. Mirrors+ `jdbc/query` semantics. Column values are coerced via `.getValue` (returns a+ String — BQ's lossy-but-uniform coercion). Callers needing typed values+ should `Long/parseLong` etc. at the use site.++ Materializes synchronously; not suitable for huge result sets."+ [^BigQuery client sql]+ (let [^TableResult result (.query client (QueryJobConfiguration/of sql) (into-array BigQuery$JobOption []))+ col-names (mapv #(.getName ^com.google.cloud.bigquery.Field %)+ (.getFields (.getSchema result)))]+ (vec (for [^FieldValueList row (.iterateAll result)]+ (into {} (for [name col-names]+ [(keyword name) (.getValue (.get row ^String name))]))))))++;;; -------------------- Assertion helpers --------------------++(defn find-bq-exception+ "Walk the cause chain until we find a `BigQueryException`, or nil."+ [^Throwable t]+ (loop [t t]+ (cond+ (nil? t) nil+ (instance? BigQueryException t) t+ :else (recur (.getCause t)))))++(defn expect-write-denied!+ "Assert that running `sql` on `client` is denied with a 403. Use for the+ strict case where only forbidden is acceptable (input-dataset writes)."+ [^BigQuery client sql label]+ (testing (format "%s on input dataset is denied" label)+ (try+ (.query client (QueryJobConfiguration/of sql) (into-array BigQuery$JobOption []))+ (is false (format "%s unexpectedly succeeded" label))+ (catch Throwable t+ (let [bq-ex (find-bq-exception t)]+ (is (some? bq-ex)+ (format "expected BigQueryException for %s; got %s" label (class t)))+ (when bq-ex+ (is (= 403 (.getCode ^BigQueryException bq-ex))+ (format "expected 403 for %s; got %d: %s"+ label (.getCode ^BigQueryException bq-ex) (.getMessage ^BigQueryException bq-ex)))))))))++(defn expect-denied!+ "Like `expect-write-denied!` but accepts any 4xx — cross-workspace reads can+ return 403 (forbidden) or 404 (resource not found from caller's POV), and+ storage/external-table escapes can surface as either. Both mean the+ operation was correctly denied."+ [^BigQuery client sql label]+ (testing (format "%s is denied" label)+ (try+ (.query client (QueryJobConfiguration/of sql) (into-array BigQuery$JobOption []))+ (is false (format "%s unexpectedly succeeded" label))+ (catch Throwable t+ (let [bq-ex (find-bq-exception t)]+ (is (some? bq-ex)+ (format "expected BigQueryException for %s; got %s" label (class t)))+ (when bq-ex+ (let [code (.getCode ^BigQueryException bq-ex)]+ (is (and (>= code 400) (< code 500))+ (format "expected 4xx for %s; got %d: %s"+ label code (.getMessage ^BigQueryException bq-ex))))))))))++(defn verify-destroy!+ "Assert post-destroy state for BigQuery workspace isolation: the workspace's+ output dataset is gone. Called right after `destroy-workspace-isolation!`.++ Note: the natural companion check — \"workspace SA can no longer issue access+ tokens\" — is *not* asserted because GCP IAM propagation of+ `deleteServiceAccount` is documented as taking up to 24 hours: deleted+ service accounts can keep issuing tokens during that window. Dataset+ deletion is reliable; SA deletion is not (immediately)."+ [project-id ^BigQuery admin-client out-dataset]+ (testing "workspace output dataset is dropped"+ (let [ds-id (DatasetId/of project-id out-dataset)+ ds (.getDataset admin-client ds-id (u/varargs BigQuery$DatasetOption []))]+ (is (nil? ds)+ (format "output dataset %s should be removed after destroy" out-dataset)))))++(defn delete-sa-direct!+ "Belt-and-suspenders SA deletion that bypasses `destroy-workspace-isolation!` —+ call from `finally` so we always attempt to clean up the workspace SA even+ when destroy itself failed mid-run."+ [^IAMClient iam-client project-id workspace]+ (let [sa-id (#'bigquery.ws/ws-service-account-id workspace)+ sa-email (format "%s@%s.iam.gserviceaccount.com" sa-id project-id)+ sa-name (format "projects/%s/serviceAccounts/%s" project-id sa-email)]+ (try (.deleteServiceAccount iam-client sa-name)+ (catch Throwable _ nil))))diff --git a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.cljindex 5c93826392b..c24355af0a6 100644--- a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj+++ b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj@@ -11,6 +11,7 @@[metabase.driver :as driver][metabase.driver.bigquery-cloud-sdk :as bigquery][metabase.driver.bigquery-cloud-sdk.common :as bigquery.common]+ [metabase.driver.bigquery-cloud-sdk.workspaces :as bigquery.ws][metabase.driver.common.table-rows-sample :as table-rows-sample][metabase.driver.settings :as driver.settings][metabase.lib.core :as lib]@@ -1384,3 +1385,25 @@(is (= ["INSERT INTO `PRODUCTS_COPY` SELECT * FROM products" nil](driver/compile-insert :bigquery-cloud-sdk {:query {:query "SELECT * FROM products"}:output-table :PRODUCTS_COPY}))))))++(deftest ^:parallel ws-sa-description-roundtrip-test+ (testing "ws-sa-description and ws-sa-description->created-at are exact inverses"+ ;; This is a contract test. The SA description is the only place the+ ;; created-at marker is stored, so the writer in+ ;; `ws-create-service-account!` and the reader used by CI cleanup+ ;; (`metabase.test.data.bigquery-cloud-sdk/delete-old-isolation-service-accounts!`)+ ;; must agree on format. If this test fails, orphan SA cleanup will+ ;; silently break -- expired SAs will accumulate because their created-at+ ;; can no longer be parsed.+ (doseq [instant [(java.time.Instant/parse "2026-01-15T10:30:45.123456789Z")+ (java.time.Instant/parse "2026-12-31T23:59:59Z")+ (java.time.Instant/parse "2020-06-15T00:00:00Z")+ (java.time.Instant/now)]]+ (is (= instant+ (bigquery.ws/ws-sa-description->created-at (bigquery.ws/ws-sa-description instant)))+ (str "round-trip failed for " instant))))+ (testing "ws-sa-description->created-at returns nil for non-conforming inputs"+ (is (nil? (bigquery.ws/ws-sa-description->created-at nil)))+ (is (nil? (bigquery.ws/ws-sa-description->created-at "")))+ (is (nil? (bigquery.ws/ws-sa-description->created-at "some other description")))+ (is (nil? (bigquery.ws/ws-sa-description->created-at "created-at:not-an-instant")))))diff --git a/modules/drivers/bigquery-cloud-sdk/test/metabase/test/data/bigquery_cloud_sdk.clj b/modules/drivers/bigquery-cloud-sdk/test/metabase/test/data/bigquery_cloud_sdk.cljindex f9b9d11e992..48634e4c678 100644--- a/modules/drivers/bigquery-cloud-sdk/test/metabase/test/data/bigquery_cloud_sdk.clj+++ b/modules/drivers/bigquery-cloud-sdk/test/metabase/test/data/bigquery_cloud_sdk.clj@@ -5,6 +5,7 @@[medley.core :as m][metabase.driver :as driver][metabase.driver.bigquery-cloud-sdk :as bigquery]+ [metabase.driver.bigquery-cloud-sdk.workspaces :as bigquery.ws][metabase.driver.ddl.interface :as ddl.i][metabase.lib.schema.common :as lib.schema.common][metabase.test.data.impl :as data.impl]@@ -354,14 +355,113 @@(log/info (u/format-color 'blue "Deleting temporary dataset more than two days old: %s`." outdated))(destroy-dataset! outdated))))+;;; ---------------------- Workspace isolation orphan cleanup ----------------------+;;;+;;; Split into pure enumerators (`orphan-isolation-*`) and destructive drops+;;; (`drop-orphan-isolation-*!`). REPL-preview without the destructive side:+;;;+;;; (#'bq-tx/orphan-isolation-datasets)+;;; ;; => ("mb__isolation_..." ...)+;;; (#'bq-tx/orphan-isolation-service-accounts)+;;; ;; => ("mb__isolation_..." ...)+;;;+;;; The whole-orchestration entry point is the existing+;;; [[delete-old-datasets-if-needed!]].++(defn- orphan-isolation-datasets+ "Return iso dataset names (`mb__isolation_*`) older than 3 hours.+ Pure: queries `INFORMATION_SCHEMA.SCHEMATA`, returns a seq of strings."+ []+ (->> (execute!+ (str "SELECT schema_name FROM `%s`.INFORMATION_SCHEMA.SCHEMATA "+ ;; Prefix must match driver.u/workspace-isolated-prefix+ "WHERE schema_name LIKE 'mb__isolation_%%' "+ "AND creation_time < TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL -3 hour)")+ (project-id))+ (map first)))++(defn- orphan-isolation-service-accounts+ "Return iso SA email addresses (`mb__isolation_*@...`) older than 3 hours.++ Age comes from the `created-at:<iso-instant>` marker encoded in the SA+ `description` field by+ [[metabase.driver.bigquery-cloud-sdk/ws-sa-description->created-at]].+ SAs without that marker are NOT returned (we don't know their age).++ Pure: lists SAs via the IAM client and filters in-memory."+ []+ (let [details (test-db-details)+ iam-client (#'bigquery.ws/ws-database-details->iam-client details)+ proj-name (format "projects/%s" (project-id))+ threshold (.minus (java.time.Instant/now) 3 java.time.temporal.ChronoUnit/HOURS)]+ (try+ (let [request (-> (com.google.iam.admin.v1.ListServiceAccountsRequest/newBuilder)+ (.setName proj-name)+ (.build))+ response (.listServiceAccounts ^com.google.cloud.iam.admin.v1.IAMClient iam-client request)]+ (->> (.iterateAll response)+ (keep (fn [^com.google.iam.admin.v1.ServiceAccount sa]+ (let [sa-email (.getEmail sa)+ created-at (bigquery.ws/ws-sa-description->created-at (.getDescription sa))]+ (when (and (str/starts-with? sa-email "mb__isolation_")+ created-at+ (.isBefore ^java.time.Instant created-at threshold))+ sa-email))))+ ;; Realize before the iam-client is closed in `finally`.+ vec))+ (finally+ (.close ^com.google.cloud.iam.admin.v1.IAMClient iam-client)))))++(defn- drop-orphan-isolation-datasets!+ "Destroy iso datasets returned by [[orphan-isolation-datasets]].+ Per-entry try/catch: one failure won't block the others."+ []+ (doseq [dataset-name (orphan-isolation-datasets)]+ (log/info (u/format-color 'blue "Deleting orphan workspace isolation dataset older than 3h: %s" dataset-name))+ (try+ (destroy-dataset! dataset-name)+ (catch Throwable e+ (log/warnf "Failed to drop orphan iso dataset %s: %s" dataset-name (ex-message e))))))++(defn- drop-orphan-isolation-service-accounts!+ "Delete iso service accounts returned by [[orphan-isolation-service-accounts]].+ Per-entry try/catch: one failure won't block the others. Opens its own IAM+ client (separate from the enumerator's) so the lifecycle is local."+ []+ (let [details (test-db-details)+ iam-client (#'bigquery.ws/ws-database-details->iam-client details)]+ (try+ (doseq [sa-email (orphan-isolation-service-accounts)]+ (log/info (u/format-color 'blue "Deleting orphan workspace isolation SA older than 3h: %s" sa-email))+ (try+ (let [del-req (-> (com.google.iam.admin.v1.DeleteServiceAccountRequest/newBuilder)+ (.setName (format "projects/%s/serviceAccounts/%s" (project-id) sa-email))+ (.build))]+ (.deleteServiceAccount ^com.google.cloud.iam.admin.v1.IAMClient iam-client del-req))+ (catch Throwable e+ (log/warnf "Failed to delete orphan iso SA %s: %s" sa-email (ex-message e)))))+ (finally+ (.close ^com.google.cloud.iam.admin.v1.IAMClient iam-client)))))+(defonce ^:private deleted-old-datasets?(atom false))(defn- delete-old-datasets-if-needed!- "Call [[delete-old-datasets!]], only if we haven't done so already."+ "Call [[delete-old-datasets!]] (+ workspace-iso sweep), only if we haven't+ done so already."[](when (compare-and-set! deleted-old-datasets? false true)- (delete-old-datasets!)))+ (delete-old-datasets!)+ ;; Sweep orphan workspace-isolation resources. Per-entry try/catch already+ ;; inside each `drop-orphan-*!`; the outer try/catch here guards against+ ;; an enumerator-side failure (e.g. transient IAM 5xx) blocking test-data+ ;; setup.+ (try (drop-orphan-isolation-datasets!)+ (catch Throwable e+ (log/warnf "drop-orphan-isolation-datasets! failed: %s" (ex-message e))))+ (try (drop-orphan-isolation-service-accounts!)+ (catch Throwable e+ (log/warnf "drop-orphan-isolation-service-accounts! failed: %s" (ex-message e))))))(defn- setup-tracking-dataset!"Idempotently create test tracking database"diff --git a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.cljindex 33e6d5f4383..a07a2b606cd 100644--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj@@ -9,6 +9,7 @@[metabase.driver.clickhouse-nippy][metabase.driver.clickhouse-qp][metabase.driver.clickhouse-version :as clickhouse-version]+ [metabase.driver.connection :as driver.conn][metabase.driver.ddl.interface :as ddl.i][metabase.driver.sql :as driver.sql][metabase.driver.sql-jdbc :as sql-jdbc]@@ -71,6 +72,14 @@:window-functions/offset false}](defmethod driver/database-supports? [:clickhouse feature] [_driver _feature _db] supported?))+(defmethod driver/qualified-name-components :clickhouse+ [_driver]+ ;; ClickHouse emits 2-part `db.table`. Its "database" sits at the schema AST position+ ;; (one level above the table) — same position SQLGlot stores in `Table.db`. Per+ ;; [[driver/qualified-name-components]] this is `:schema`, NOT `:db` (which is reserved+ ;; for catalog-level / 3-part identifiers like BigQuery's project).+ [:schema])+(def ^:private default-connection-details{:user "default" :password "" :dbname "default" :host "localhost" :port 8123})@@ -398,37 +407,46 @@(defmethod driver/init-workspace-isolation! :clickhouse[_driver database workspace]- (let [db-name (driver.u/workspace-isolation-namespace-name workspace)- read-user {:user (driver.u/workspace-isolation-user-name workspace)- :password (driver.u/random-workspace-password)}]+ (let [db-name (driver.u/workspace-isolation-namespace-name workspace)+ canonical-db (:db (driver.conn/effective-details database))+ read-user {:user (driver.u/workspace-isolation-user-name workspace)+ :password (driver.u/random-workspace-password)}](jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))](with-open [stmt (.createStatement ^Connection (:connection t-conn))]- (doseq [sql [(format "CREATE DATABASE IF NOT EXISTS `%s`" db-name)- (format "CREATE USER IF NOT EXISTS `%s` IDENTIFIED BY '%s'"- (:user read-user) (:password read-user))- (format "GRANT ALL ON `%s`.* TO `%s`" db-name (:user read-user))]]+ (doseq [sql (cond-> [(format "CREATE DATABASE IF NOT EXISTS `%s`" db-name)+ (format "CREATE USER IF NOT EXISTS `%s` IDENTIFIED BY '%s'"+ (:user read-user) (:password read-user))+ (format "GRANT ALL ON `%s`.* TO `%s`" db-name (:user read-user))]+ (not (str/blank? canonical-db))+ (conj (format "GRANT SHOW DATABASES ON `%s`.* TO `%s`"+ canonical-db (:user read-user))))](.addBatch ^Statement stmt ^String sql))(.executeBatch ^Statement stmt))){:schema db-name:database_details read-user}))(defmethod driver/grant-workspace-read-access! :clickhouse- [_driver database workspace tables]+ [_driver database workspace schemas](let [read-user-name (-> workspace :database_details :user)- qu (sql.u/quote-name :clickhouse :field read-user-name)- sqls (for [table tables]- (format "GRANT SELECT ON %s.%s TO %s"- (sql.u/quote-name :clickhouse :schema (:schema table))- (sql.u/quote-name :clickhouse :table (:name table))- qu))]+ qu (sql.u/quote-name :clickhouse :field read-user-name)](when-not read-user-name(throw (ex-info (tru "Workspace isolation is not properly initialized - missing read user name"){:workspace-id (:id workspace) :step :grant})))- (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]- (with-open [stmt (.createStatement ^Connection (:connection t-conn))]- (doseq [sql sqls]- (.addBatch ^Statement stmt ^String sql))- (.executeBatch ^Statement stmt)))))+ ;; ClickHouse `qualified-name-components` is `[:schema]` — each entry in+ ;; `schemas` is a database-as-schema. Grant `*` covers all tables in the+ ;; database; ClickHouse re-resolves `*` so future tables get coverage too.+ (let [sqls (for [schema schemas+ :let [_ (when (str/blank? schema)+ (throw (ex-info (tru "ClickHouse workspace input schema is blank")+ {:database-id (:id database) :step :grant})))]]+ (format "GRANT SELECT ON %s.* TO %s"+ (sql.u/quote-name :clickhouse :schema schema)+ qu))]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (with-open [stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql sqls]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt))))))(defmethod driver/destroy-workspace-isolation! :clickhouse[_driver database workspace]diff --git a/modules/drivers/oracle/test/metabase/driver/oracle_test.clj b/modules/drivers/oracle/test/metabase/driver/oracle_test.cljindex 1bd13699fd6..dfe855977ee 100644--- a/modules/drivers/oracle/test/metabase/driver/oracle_test.clj+++ b/modules/drivers/oracle/test/metabase/driver/oracle_test.clj@@ -179,6 +179,7 @@{:name "advanced-options"}{:name "destination-database"}{:name "write-data-connection"}+ {:name "admin-connection"}{:name "auto_run_queries"}{:name "let-user-control-scheduling"}{:name "schedules.metadata_sync"}diff --git a/modules/drivers/redshift/src/metabase/driver/redshift.clj b/modules/drivers/redshift/src/metabase/driver/redshift.cljindex b71349dc151..38db8af687e 100644--- a/modules/drivers/redshift/src/metabase/driver/redshift.clj+++ b/modules/drivers/redshift/src/metabase/driver/redshift.clj@@ -17,7 +17,9 @@[metabase.driver.sql-jdbc.sync :as sql-jdbc.sync][metabase.driver.sql-jdbc.sync.describe-database :as sql-jdbc.describe-database][metabase.driver.sql.query-processor :as sql.qp]+ [metabase.driver.sql.util :as sql.u][metabase.driver.sync :as driver.s]+ [metabase.driver.util :as driver.u][metabase.util :as u][metabase.util.date-2 :as u.date][metabase.util.honey-sql-2 :as h2x]@@ -59,9 +61,15 @@:transforms/table true:transforms/index-ddl false:uuid-type false- :workspace false}]+ :workspace true}](defmethod driver/database-supports? [:redshift feature] [_driver _feat _db] supported?))+(defmethod driver/qualified-name-components :redshift+ [_driver]+ ;; Redshift emits `schema.table` (Postgres-style 2-part) in compiled SQL.+ ;; Cross-database queries use external schemas, not `db.schema.table`.+ [:schema])+;;; +----------------------------------------------------------------------------------------------------------------+;;; | metabase.driver impls |;;; +----------------------------------------------------------------------------------------------------------------+@@ -704,14 +712,100 @@;;; | Workspace Isolation |;;; +----------------------------------------------------------------------------------------------------------------+-;; Redshift inherits init-workspace-isolation! and grant-workspace-read-access! from Postgres.-;; Only destroy needs to be overridden because Redshift doesn't support DROP OWNED BY.+;; All three workspace-isolation multimethods are overridden for Redshift:+;;+;; - `init` drops the postgres impl's trailing `GRANT "<user>" TO CURRENT_USER`+;; (PG-specific role-membership syntax Redshift rejects).+;;+;; - `grant-workspace-read-access!` adds `REVOKE CREATE ON SCHEMA … FROM <user>`+;; and `REVOKE INSERT, UPDATE, DELETE, … ON ALL TABLES IN SCHEMA … FROM <user>`+;; per source schema, plus a pre-flight probe for the public-CREATE-grant+;; limit (see below).+;;+;; - `destroy` replaces the postgres impl's `DROP OWNED BY` (which behaves+;; differently in Redshift) with explicit per-schema REVOKEs.+;;+;; Isolation limit, checked at grant time via [[assert-no-public-create-grant!]]:+;; Redshift's permission model (inherited from PostgreSQL) lets a user receive+;; privileges either directly or through PUBLIC. REVOKE-ing CREATE from a+;; specific user only removes their direct grant — it can't override a PUBLIC+;; grant. Redshift's `public` schema has CREATE granted to PUBLIC by default,+;; so on a default-config cluster the workspace user inherits CREATE on `public`+;; transitively: it can create tables there (privilege-escalation hole), becomes+;; their owner, and `DROP USER` then fails at deprovisioning. The probe at grant+;; time fails fast with a 412 so the cluster admin can run+;; `REVOKE CREATE ON SCHEMA <name> FROM PUBLIC` before retrying. Custom input+;; schemas created without a PUBLIC grant pass the probe and don't need any+;; admin action.(defn- user-exists?"Check if a Redshift user exists."[conn username](seq (jdbc/query conn ["SELECT 1 FROM pg_user WHERE usename = ?" username])))+(defn- public-create-grant?+ "Redshift-flavored check for `public-create-grant?`. Postgres reads+ `pg_namespace.nspacl::text`, but Redshift refuses to cast `aclitem` to+ character varying — so we use Redshift's own `SVV_SCHEMA_PRIVILEGES`+ system view instead, which exposes the equivalent grant info as plain+ columns."+ [conn schema-name]+ (boolean+ (seq (jdbc/query conn+ ["SELECT 1 FROM svv_schema_privileges+ WHERE namespace_name = ?+ AND identity_type = 'public'+ AND privilege_type = 'CREATE'"+ schema-name]))))++(defn- assert-no-public-create-grant!+ [conn schema-name]+ (when (public-create-grant? conn schema-name)+ (driver.postgres/raise-public-create-grant! schema-name)))++(defmethod driver/init-workspace-isolation! :redshift+ [_driver database workspace]+ (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)+ read-user {:user (driver.u/workspace-isolation-user-name workspace)+ :password (driver.u/random-workspace-password)}]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (let [user-sql (if (user-exists? t-conn (:user read-user))+ (format "ALTER USER \"%s\" WITH PASSWORD '%s'" (:user read-user) (:password read-user))+ (format "CREATE USER \"%s\" WITH PASSWORD '%s'" (:user read-user) (:password read-user)))]+ (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql [(format "CREATE SCHEMA IF NOT EXISTS \"%s\"" schema-name)+ user-sql+ (format "GRANT ALL PRIVILEGES ON SCHEMA \"%s\" TO \"%s\"" schema-name (:user read-user))+ (format "ALTER DEFAULT PRIVILEGES IN SCHEMA \"%s\" GRANT ALL ON TABLES TO \"%s\""+ schema-name (:user read-user))]]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt))))+ {:schema schema-name+ :database_details read-user}))++(defmethod driver/grant-workspace-read-access! :redshift+ [_driver database workspace schemas]+ (let [username (-> workspace :database_details :user)+ qu (sql.u/quote-name :postgres :field username)+ source-schemas (set schemas)+ spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ ;; Pre-flight check (read-only) can run in its own transaction.+ (jdbc/with-db-transaction [t-conn spec]+ (doseq [s source-schemas]+ (assert-no-public-create-grant! t-conn s)))+ ;; Grants run as auto-commit per statement so privileges are immediately+ ;; observable to a subsequent describe-database from a different connection.+ (doseq [s source-schemas+ :let [sq (sql.u/quote-name :postgres :schema s)]+ sql [(format "GRANT USAGE ON SCHEMA %s TO %s" sq qu)+ (format "REVOKE CREATE ON SCHEMA %s FROM %s" sq qu)+ (format "REVOKE INSERT, UPDATE, DELETE, REFERENCES ON ALL TABLES IN SCHEMA %s FROM %s" sq qu)+ ;; Schema-wide SELECT — workspace-scoped users receive whole-schema access.+ ;; Per-table granularity intentionally discarded (API contract is namespace-grained).+ (format "GRANT SELECT ON ALL TABLES IN SCHEMA %s TO %s" sq qu)+ (format "ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT SELECT ON TABLES TO %s" sq qu)]]+ (jdbc/execute! spec [sql]))))+(defn- schema-exists?"Check if a schema exists in Redshift."[conn schema-name]@@ -744,6 +838,15 @@schema username))(.addBatch ^Statement stmt^String (format "REVOKE ALL PRIVILEGES ON SCHEMA \"%s\" FROM \"%s\""+ schema username))+ ;; `grant-workspace-read-access!` issues `ALTER DEFAULT PRIVILEGES IN SCHEMA <s>+ ;; GRANT SELECT ON TABLES TO <iso-user>` on every input schema. Those default-priv+ ;; entries are owned by the granting role (metabase_ci) but reference the iso-user;+ ;; without an explicit REVOKE here, `DROP USER` fails with "cannot be dropped+ ;; because some objects depend on it / privileges for default privileges on new+ ;; relations belonging to user <grantor> in schema <input-schema>".+ (.addBatch ^Statement stmt+ ^String (format "ALTER DEFAULT PRIVILEGES IN SCHEMA \"%s\" REVOKE ALL ON TABLES FROM \"%s\""schema username)));; Only revoke default privileges if both user and schema exist(when schema-existsdiff --git a/modules/drivers/redshift/test/metabase/test/data/redshift.clj b/modules/drivers/redshift/test/metabase/test/data/redshift.cljindex 37f63a7b0a4..dd9970793ad 100644--- a/modules/drivers/redshift/test/metabase/test/data/redshift.clj+++ b/modules/drivers/redshift/test/metabase/test/data/redshift.clj@@ -220,14 +220,26 @@:expired))schemas))))-(defn- delete-old-schemas!- "Remove unneeded schemas from redshift. Local databases are thrown away after a test run. Shared cloud instances do- not have this luxury. Test runs can create schemas where models are persisted and nothing cleans these up, leading- to redshift clusters hitting the max number of tables allowed.-- Also cleans up workspace isolation schemas (mb__isolation_*) and their associated users that may have been- left behind by workspace tests. Only deletes isolation schemas older than [[hours-before-expired-threshold]]- to avoid interfering with parallel test runs."+;;; --------------------------------- Enumeration ----------------------------------+;;;+;;; Pure (read-only) classifiers. Call from REPL to preview what cleanup WOULD do:+;;;+;;; (with-open [c (.. (sql-jdbc.conn/connection-details->spec :redshift @db-connection-details)+;;; jdbc/get-connection)]+;;; (rs-tx/orphan-schemas c))+;;; ;; => {:old [...] :expired-cache [...] :expired-isolation [...]}++(defn- orphan-schemas+ "Classify every schema in the connected Redshift DB into orphan buckets.+ Returns a map with possibly-empty vectors under each key:+ :old -- pre-current-convention test data schemas+ :expired-cache -- model-persistence cache schemas past TTL+ :lacking-created-at -- cache schemas with no `cache_info.created-at`+ :old-style-cache -- cache schemas without a `cache_info` table at all+ :expired-isolation -- workspace-isolation schemas past TTL++ Pure: makes 1-2 catalog queries but does NOT drop anything. Use the+ `drop-orphan-*!` fns to act on the result."[^java.sql.Connection conn](let [{old-convention :oldcaches-with-info :cache@@ -241,23 +253,87 @@:else acc)){:old [] :cache [] :isolation []}(fetch-schemas conn))- {:keys [expired- old-style-cache- lacking-created-at]} (classify-cache-schemas conn caches-with-info)- {expired-isolation :expired} (classify-isolation-schemas conn isolation)- drop-sql (fn [schema-name] (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE;" schema-name))]+ {expired-cache :expired+ old-style-cache :old-style-cache+ lacking-created-at :lacking-created-at} (classify-cache-schemas conn caches-with-info)+ {expired-isolation :expired} (classify-isolation-schemas conn isolation)]+ {:old (vec old-convention)+ :expired-cache (vec expired-cache)+ :old-style-cache (vec old-style-cache)+ :lacking-created-at (vec lacking-created-at)+ :expired-isolation (vec expired-isolation)}))++#_{:clj-kondo/ignore [:unused-private-var]}+(defn- orphan-isolation-users+ "Iso users (`mb__isolation_*`) older than [[hours-before-expired-threshold]].++ Derived from [[orphan-schemas]] -- iso schema names and iso user names are+ identical strings by construction (`workspace-isolation-namespace-name` ==+ `workspace-isolation-user-name`). Schema is always created BEFORE user in+ `init-workspace-isolation! :redshift`, so an orphan user without a paired+ schema is structurally impossible; we don't need a separate query.++ REPL-only convenience -- production path consumes the schemas result map+ directly via [[delete-old-schemas!]]. Kept here so devs can preview without+ knowing the orphan-map shape."+ [^java.sql.Connection conn]+ (:expired-isolation (orphan-schemas conn)))++;;; --------------------------------- Destruction ----------------------------------++(defn- drop-orphan-schemas!+ "Drop every schema classified by [[orphan-schemas]] as expired/old. Per-entry+ try/catch: never let one orphan block the rest.++ Takes the orphan-map directly so callers can preview-then-drop without+ re-querying. Caller owns the Statement."+ [^java.sql.Statement stmt orphans]+ (let [drop-sql (fn [schema-name] (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE;" schema-name))]+ (doseq [[k fmt-str] [[:old "Dropping old data schema: %s"]+ [:expired-cache "Dropping expired cache schema: %s"]+ [:lacking-created-at "Dropping cache without created-at info: %s"]+ [:old-style-cache "Dropping old cache schema without `cache_info` table: %s"]+ [:expired-isolation "Dropping expired workspace isolation schema: %s"]]+ schema (get orphans k)]+ (log/infof fmt-str schema)+ (try+ (.execute stmt (drop-sql schema))+ (catch Throwable e+ (log/infof "Failed to drop %s, skipping: %s" schema (ex-message e)))))))++(defn- drop-orphan-isolation-users!+ "Drop iso users paired with expired iso schemas. The schema was dropped first+ (see [[drop-orphan-schemas!]]), so its default-priv dependencies on the user+ are already gone -- DROP USER should succeed without REVOKE chasing.++ Per-entry try/catch: a lingering grant from a NON-iso schema would block+ DROP USER, but `grant-workspace-read-access!` issues those against+ user-named input schemas which the production `destroy-workspace-isolation!`+ revokes. If production ran, no orphan grants. If it didn't (the case this+ cleanup catches), the user is dead-state anyway and a logged DROP failure+ is fine."+ [^java.sql.Statement stmt iso-usernames]+ (doseq [iso-username iso-usernames]+ (log/infof "Dropping expired workspace isolation user: %s" iso-username)+ (try+ (.execute stmt (format "DROP USER IF EXISTS \"%s\";" iso-username))+ (catch Throwable e+ (log/infof "Failed to drop user %s, skipping: %s" iso-username (ex-message e))))))++(defn- delete-old-schemas!+ "Remove unneeded schemas + users from redshift. Local databases are thrown+ away after a test run; shared cloud instances are not. Test runs can leak+ schemas (persisted models, workspace iso namespaces) and users (workspace+ iso accounts), leading to clusters hitting the max-tables / max-users limits.++ Glue: thin wrapper that calls the enumerator + droppers in order. To preview+ from a REPL, call [[orphan-schemas]] / [[orphan-isolation-users]] directly."+ [^java.sql.Connection conn]+ (let [orphans (orphan-schemas conn)+ iso-usernames (:expired-isolation orphans)](with-open [stmt (.createStatement conn)]- (doseq [[collection fmt-str] [[old-convention "Dropping old data schema: %s"]- [expired "Dropping expired cache schema: %s"]- [lacking-created-at "Dropping cache without created-at info: %s"]- [old-style-cache "Dropping old cache schema without `cache_info` table: %s"]- [expired-isolation "Dropping expired workspace isolation schema: %s"]]- schema collection]- (log/infof fmt-str schema)- (try- (.execute stmt (drop-sql schema))- (catch Throwable e- (log/infof "Failed to drop %s, skipping: %s" schema (ex-message e))))))))+ (drop-orphan-schemas! stmt orphans)+ (drop-orphan-isolation-users! stmt iso-usernames))))(defn- create-session-schema! [^java.sql.Connection conn](with-open [stmt (.createStatement conn)]diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.cljindex dbb2af621ea..664574d9f22 100644--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj@@ -73,7 +73,7 @@:regex/lookaheads-and-lookbehinds false:transforms/python true:transforms/table true- :workspace true}]+ :workspace false}](defmethod driver/database-supports? [:snowflake feature] [_driver _feature _db] supported?))(defmethod driver/humanize-connection-error-message :snowflake@@ -927,7 +927,8 @@[_ database](-> database(m/update-existing :details normalize-details)- (m/update-existing :write_data_details normalize-details)))+ (m/update-existing :write_data_details normalize-details)+ (m/update-existing :admin_details normalize-details)));;; If you try to read a Snowflake `timestamptz` as a String with `.getString` it always comes back in;;; `America/Los_Angeles` for some reason I cannot figure out. Let's just read them out as UTC, which is what they're@@ -1113,29 +1114,31 @@(jdbc/execute! conn-spec [sql]))))(defmethod driver/grant-workspace-read-access! :snowflake- [_driver database workspace tables]+ [_driver database workspace schemas](let [conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))db-name (:db (driver.conn/effective-details database))role-name (-> workspace :database_details :role)]- (when-not db-name- (throw (ex-info (tru "Snowflake database configuration is missing required ''db'' (database name) setting")- {:database-id (:id database) :step :grant})))(when-not role-name(throw (ex-info (tru "Workspace isolation is not properly initialized - missing role name"){:workspace-id (:id workspace) :step :grant})))- (let [qdb (sql.u/quote-name :snowflake :schema db-name)- qr (sql.u/quote-name :snowflake :field role-name)]- ;; Grant USAGE on each unique schema first (required to access tables within)- (doseq [schema (distinct (map :schema tables))]- (jdbc/execute! conn-spec [(format "GRANT USAGE ON SCHEMA %s.%s TO ROLE %s"- qdb (sql.u/quote-name :snowflake :schema schema) qr)]))- ;; Grant SELECT on each specific table- (doseq [table tables]- (jdbc/execute! conn-spec [(format "GRANT SELECT ON TABLE %s.%s.%s TO ROLE %s"- qdb- (sql.u/quote-name :snowflake :schema (:schema table))- (sql.u/quote-name :snowflake :table (:name table))- qr)])))))+ (when (str/blank? db-name)+ (throw (ex-info (tru "Snowflake workspaces require an explicit database in connection details")+ {:database-id (:id database) :step :grant})))+ ;; Each entry in `schemas` is a Snowflake schema in the bound database. The+ ;; catalog (`:db`) comes from connection details rather than per-input — a+ ;; Metabase `Database` row binds to one Snowflake database.+ (let [qr (sql.u/quote-name :snowflake :field role-name)+ qdb (sql.u/quote-name :snowflake :schema db-name)]+ (doseq [schema schemas]+ (when (str/blank? schema)+ (throw (ex-info (tru "Snowflake workspace input schema is blank")+ {:database-id (:id database) :step :grant})))+ (let [qs (sql.u/quote-name :snowflake :schema schema)]+ (doseq [sql [(format "GRANT USAGE ON DATABASE %s TO ROLE %s" qdb qr)+ (format "GRANT USAGE ON SCHEMA %s.%s TO ROLE %s" qdb qs qr)+ (format "GRANT SELECT ON ALL TABLES IN SCHEMA %s.%s TO ROLE %s" qdb qs qr)+ (format "GRANT SELECT ON FUTURE TABLES IN SCHEMA %s.%s TO ROLE %s" qdb qs qr)]]+ (jdbc/execute! conn-spec [sql])))))))(defmethod driver/llm-sql-dialect-resource :snowflake [_]"metabot/prompts/dialects/snowflake.md")diff --git a/modules/drivers/snowflake/test/metabase/test/data/snowflake.clj b/modules/drivers/snowflake/test/metabase/test/data/snowflake.cljindex b9cd6d0ddfe..54674dc6a23 100644--- a/modules/drivers/snowflake/test/metabase/test/data/snowflake.clj+++ b/modules/drivers/snowflake/test/metabase/test/data/snowflake.clj@@ -111,7 +111,7 @@and created < dateadd(day, ?, current_timestamp()))"](into [] (map :name) (jdbc/reducible-query (no-db-connection-spec) [query days-ago days-ago]))))-(defn- old-isolation-schema-names+(defn- orphan-isolation-schemas"Return a collection of schema names with mb__isolation_ prefix that are more than 3 hours old,along with their database names."[]@@ -136,49 +136,157 @@(recur results)))results)))))))+(defn- old-snowflake-objects+ "Return a vector of names matching `like-pattern` (a `SHOW <kind> LIKE ...` argument) that are+ more than 3 hours old. `kind` is the SQL noun -- one of `USERS`, `ROLES`. Snowflake's `SHOW`+ result sets expose `name` and `created_on` columns for both.++ Filters to the iso-prefixed set up front so the result set stays small even on a long-lived+ account. We only return *expired* entries -- entries within the 3h threshold may belong to a+ parallel-running test job."+ [^String kind ^String like-pattern]+ (sql-jdbc.execute/do-with-connection-with-options+ :snowflake+ (no-db-connection-spec)+ {:write? false}+ (fn [^java.sql.Connection conn]+ (with-open [stmt (.createStatement conn)+ rs (.executeQuery stmt (format "SHOW %s LIKE '%s'" kind like-pattern))]+ (let [threshold (-> (Instant/now) (.minus 3 ChronoUnit/HOURS) java.util.Date/from)]+ (loop [results []]+ (if (.next rs)+ (let [name (.getString rs "name")+ created-on (.getTimestamp rs "created_on")]+ (if (and created-on (.before created-on threshold))+ (recur (conj results name))+ (recur results)))+ results)))))))++(defn- orphan-isolation-users+ "Return iso usernames (`mb__isolation_*`) older than 3 hours."+ []+ ;; Prefix must match driver.u/workspace-isolated-prefix.+ (old-snowflake-objects "USERS" "mb__isolation_%"))++(defn- orphan-isolation-roles+ "Return iso role names older than 3 hours. NOTE: roles use a different prefix+ (`MB_ISOLATION_ROLE_`) than schemas/users -- see `isolation-role-name` in+ `metabase.driver.snowflake`."+ []+ (old-snowflake-objects "ROLES" "MB_ISOLATION_ROLE_%"))++;;; --------------------------------- Destruction ----------------------------------+;;;+;;; The fns below are split into pure enumerators (above: `orphan-isolation-*`)+;;; and destructive drops. This lets you preview from a REPL:+;;;+;;; (#'sf-tx/orphan-isolation-users)+;;; ;; => ["mb__isolation_..." ...]+;;;+;;; without firing the destructive side. The whole-orchestration entry point+;;; ([[delete-old-test-data!]]) is the glue and gets called from `tx/create-db!`.++(defn- with-write-stmt!+ "Open a write-capable Snowflake connection + Statement, call `f` with the stmt,+ close everything. Centralizes the boilerplate so the per-resource drop fns+ don't repeat it."+ [f]+ (sql-jdbc.execute/do-with-connection-with-options+ :snowflake+ (no-db-connection-spec)+ {:write? true}+ (fn [^java.sql.Connection conn]+ (with-open [stmt (.createStatement conn)]+ (f stmt)))))++(defn- drop-old-datasets!+ "Drop test datasets (databases) prefixed by `sha_` that are >2 days old."+ []+ (when-let [old-datasets (not-empty (old-dataset-names))]+ (with-write-stmt!+ (fn [^java.sql.Statement stmt]+ (doseq [dataset-name old-datasets]+ #_{:clj-kondo/ignore [:discouraged-var]}+ (println "[Snowflake] Deleting old dataset:" dataset-name)+ (try+ (.execute stmt (format "DROP DATABASE IF EXISTS \"%s\";" dataset-name))+ (.execute stmt (format "delete from metabase_test_tracking.PUBLIC.datasets where name = '%s';"+ dataset-name))+ ;; if this fails for some reason it's probably just because some other job tried to delete the dataset at the+ ;; same time. No big deal. Just log this and carry on trying to delete the other datasets. If we don't end up+ ;; deleting anything it's not the end of the world because it won't affect our ability to run our tests+ (catch Throwable e+ #_{:clj-kondo/ignore [:discouraged-var]}+ (println "[Snowflake] Error deleting old dataset:" (ex-message e)))))))))++(defn- drop-orphan-isolation-schemas!+ "Drop iso schemas (`mb__isolation_*`) older than 3 hours."+ []+ (when-let [old-schemas (not-empty (orphan-isolation-schemas))]+ (with-write-stmt!+ (fn [^java.sql.Statement stmt]+ (doseq [{:keys [schema-name database-name]} old-schemas]+ #_{:clj-kondo/ignore [:discouraged-var]}+ (println "[Snowflake] Deleting old isolation schema:" database-name "." schema-name)+ (try+ (.execute stmt (format "DROP SCHEMA IF EXISTS \"%s\".\"%s\";" database-name schema-name))+ (catch Throwable e+ #_{:clj-kondo/ignore [:discouraged-var]}+ (println "[Snowflake] Error deleting old isolation schema:" (ex-message e)))))))))++(defn- drop-orphan-isolation-users!+ "Drop iso users (`mb__isolation_*`) older than 3 hours. Per-entry try/catch:+ never let one orphan block the others."+ []+ (when-let [old-users (not-empty (orphan-isolation-users))]+ (with-write-stmt!+ (fn [^java.sql.Statement stmt]+ (doseq [username old-users]+ #_{:clj-kondo/ignore [:discouraged-var]}+ (println "[Snowflake] Deleting old isolation user:" username)+ (try+ (.execute stmt (format "DROP USER IF EXISTS \"%s\";" username))+ (catch Throwable e+ #_{:clj-kondo/ignore [:discouraged-var]}+ (println "[Snowflake] Error deleting old isolation user:" (ex-message e)))))))))++(defn- drop-orphan-isolation-roles!+ "Drop iso roles (`MB_ISOLATION_ROLE_*`) older than 3 hours. Note the+ different prefix vs schemas/users -- see `isolation-role-name` in+ `metabase.driver.snowflake`."+ []+ (when-let [old-roles (not-empty (orphan-isolation-roles))]+ (with-write-stmt!+ (fn [^java.sql.Statement stmt]+ (doseq [role-name old-roles]+ #_{:clj-kondo/ignore [:discouraged-var]}+ (println "[Snowflake] Deleting old isolation role:" role-name)+ (try+ (.execute stmt (format "DROP ROLE IF EXISTS \"%s\";" role-name))+ (catch Throwable e+ #_{:clj-kondo/ignore [:discouraged-var]}+ (println "[Snowflake] Error deleting old isolation role:" (ex-message e)))))))))+(defn- delete-old-test-data!"Delete old test data:- Datasets (databases) prefixed by sha_ that are two days ago or older- - Isolation schemas prefixed by mb__isolation_ that are more than 3 hours old"+ - Isolation schemas prefixed by mb__isolation_ that are more than 3 hours old+ - Isolation users prefixed by mb__isolation_ that are more than 3 hours old+ - Isolation roles prefixed by MB_ISOLATION_ROLE_ that are more than 3 hours old++ Glue. Each drop step is independent; a failure in one doesn't block the+ others. To preview from a REPL, call the corresponding `orphan-isolation-*`+ enumerator instead."[]- ;; the printlns below are on purpose because we want them to show up when running tests, even on CI, to make sure this+ ;; the printlns are on purpose because we want them to show up when running tests, even on CI, to make sure this;; stuff is working correctly. We can change it to `log` in the future when we're satisfied everything is working as;; intended -- Cam#_{:clj-kondo/ignore [:discouraged-var]}(println "[Snowflake] deleting old test data...")- (sql-jdbc.execute/do-with-connection-with-options- :snowflake- (no-db-connection-spec)- {:write? true}- (fn [^java.sql.Connection conn]- (with-open [stmt (.createStatement conn)]- ;; Delete old datasets- (when-let [old-datasets (not-empty (old-dataset-names))]- (doseq [dataset-name old-datasets]- #_{:clj-kondo/ignore [:discouraged-var]}- (println "[Snowflake] Deleting old dataset:" dataset-name)- (try- (.execute stmt (format "DROP DATABASE IF EXISTS \"%s\";"- dataset-name))- (.execute stmt (format "delete from metabase_test_tracking.PUBLIC.datasets where name = '%s';"- dataset-name))- ;; if this fails for some reason it's probably just because some other job tried to delete the dataset at the- ;; same time. No big deal. Just log this and carry on trying to delete the other datasets. If we don't end up- ;; deleting anything it's not the end of the world because it won't affect our ability to run our tests- (catch Throwable e- #_{:clj-kondo/ignore [:discouraged-var]}- (println "[Snowflake] Error deleting old dataset:" (ex-message e))))))- ;; Delete old isolation schemas- (when-let [old-schemas (not-empty (old-isolation-schema-names))]- (doseq [{:keys [schema-name database-name]} old-schemas]- #_{:clj-kondo/ignore [:discouraged-var]}- (println "[Snowflake] Deleting old isolation schema:" database-name "." schema-name)- (try- (.execute stmt (format "DROP SCHEMA IF EXISTS \"%s\".\"%s\";"- database-name schema-name))- (catch Throwable e- #_{:clj-kondo/ignore [:discouraged-var]}- (println "[Snowflake] Error deleting old isolation schema:" (ex-message e))))))))))+ (drop-old-datasets!)+ (drop-orphan-isolation-schemas!)+ (drop-orphan-isolation-users!)+ (drop-orphan-isolation-roles!))(defonce ^:private deleted-old-test-data?(atom false))diff --git a/modules/drivers/sqlserver/resources/metabase/sqlserver/metabase-plugin.yaml b/modules/drivers/sqlserver/resources/metabase/sqlserver/metabase-plugin.yamlindex 7678e26ab7b..209ca269e07 100644--- a/modules/drivers/sqlserver/resources/metabase/sqlserver/metabase-plugin.yaml+++ b/modules/drivers/sqlserver/resources/metabase/sqlserver/metabase-plugin.yaml@@ -29,6 +29,9 @@ driver:helper-text: User used to run system queries, required if the connection's user cannot be impersonated, such as the 'sa' user.placeholder: user- cloud-ip-address-info+ - name: schema-filters+ type: schema-filters+ display-name: Schemas- group:container-style: ["component", "backdrop"]fields:diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.cljindex d358ecb66fd..b6211d17234 100644--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj@@ -71,6 +71,21 @@:table-privileges true}](defmethod driver/database-supports? [:sqlserver feature] [_driver _feature _db] supported?))+(defmethod driver/qualified-name-components :sqlserver+ [_driver]+ ;; SQL Server emits `db.schema.table` (3-part) when crossing databases. Single-DB+ ;; queries are typically `schema.table`, but workspace remap rows must be keyed+ ;; on the more general 3-part shape.+ [:db :schema])++(defmethod driver.sql/table-qualification-style :sqlserver+ [_driver]+ :table-qualification-style/db-schema-table)++(defmethod driver.sql/db-slot-value :sqlserver+ [_driver database]+ (:db (:details database)))+(mu/defmethod driver/database-supports? [:sqlserver :percentile-aggregations][_ _ db :- ::lib.schema.metadata/database](let [major-version (get-in db [:dbms-version :semantic-version 0] 0)]@@ -1178,19 +1193,27 @@(jdbc/execute! conn-spec [sql]))))(defmethod driver/grant-workspace-read-access! :sqlserver- [_driver database workspace tables]+ [_driver database workspace schemas](let [conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))- username (-> workspace :database_details :user)]+ username (-> workspace :database_details :user)+ db-name (:db (:details database))](when-not username(throw (ex-info (tru "Workspace isolation is not properly initialized - missing read user name"){:workspace-id (:id workspace) :step :grant})))- ;; Grant SELECT on each specific table only - no schema-level grants+ (when (str/blank? db-name)+ (throw (ex-info (tru "SQL Server workspaces require an explicit database in connection details")+ {:database-id (:id database) :step :grant})))+ ;; SQL Server connection is bound to one DB (`:db` in details). Per-schema+ ;; grant: SELECT on the schema covers existing + future objects within it.(let [qu (sql.u/quote-name :sqlserver :field username)]- (doseq [table tables]- (jdbc/execute! conn-spec [(format "GRANT SELECT ON %s.%s TO %s"- (sql.u/quote-name :sqlserver :schema (:schema table))- (sql.u/quote-name :sqlserver :table (:name table))- qu)])))))+ (doseq [schema schemas]+ (when (str/blank? schema)+ (throw (ex-info (tru "SQL Server workspace input schema is blank")+ {:database-id (:id database) :step :grant})))+ (jdbc/execute! conn-spec+ [(format "GRANT SELECT ON SCHEMA::%s TO %s"+ (sql.u/quote-name :sqlserver :schema schema)+ qu)])))))(defmethod driver/llm-sql-dialect-resource :sqlserver [_]"metabot/prompts/dialects/sqlserver.md")diff --git a/resources/migrations/062/20260421_workspaces.yaml b/resources/migrations/062/20260421_workspaces.yamlnew file mode 100644index 00000000000..7ce68490fe8--- /dev/null+++ b/resources/migrations/062/20260421_workspaces.yaml@@ -0,0 +1,288 @@+## Workspaces: workspace + workspace_database tables, plus the table_remapping table+## used by the QP middleware to redirect production-table reads into a workspace's+## isolation schema.++databaseChangeLog:+ - logicalFilePath: migrations/062_update_migrations.yaml+ - changeSet:+ id: v62.2026-04-21T12:00:00+ author: bcm+ comment: Create table_remapping table for workspace table-to-table remapping+ changes:+ - createTable:+ tableName: table_remapping+ remarks: Maps production tables to workspace tables for query remapping+ columns:+ - column:+ name: id+ type: int+ remarks: Unique ID+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: database_id+ type: int+ remarks: Database this remapping applies to+ constraints:+ nullable: false+ - column:+ name: from_db+ type: varchar(254)+ remarks: Source database/project (top-level identifier; e.g. BigQuery project, ClickHouse db). Empty string when driver does not emit this level.+ defaultValue: ""+ constraints:+ nullable: false+ - column:+ name: from_schema+ type: varchar(254)+ remarks: Source schema name. Empty string for schema-less drivers.+ defaultValue: ""+ constraints:+ nullable: false+ - column:+ name: from_table_name+ type: varchar(254)+ remarks: Source table name+ constraints:+ nullable: false+ - column:+ name: to_db+ type: varchar(254)+ remarks: Target database/project (top-level identifier). Empty string when driver does not emit this level.+ defaultValue: ""+ constraints:+ nullable: false+ - column:+ name: to_schema+ type: varchar(254)+ remarks: Target schema name. Empty string for schema-less drivers.+ defaultValue: ""+ constraints:+ nullable: false+ - column:+ name: to_table_name+ type: varchar(254)+ remarks: Target table name+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: When this remapping was created+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false++ - changeSet:+ id: v62.2026-04-21T12:00:01+ author: bcm+ comment: Add unique constraint on (database_id, from_db, from_schema, from_table_name)+ ## This constraint's leading column is `database_id`, so MySQL reuses its index+ ## to satisfy the FK added below — no separate `idx_..._database_id` needed.+ changes:+ - addUniqueConstraint:+ tableName: table_remapping+ constraintName: idx_unique_table_remapping_source+ columnNames: database_id, from_db, from_schema, from_table_name++ - changeSet:+ id: v62.2026-04-21T12:00:02+ author: bcm+ comment: Add unique constraint on (database_id, to_db, to_schema, to_table_name) to prevent two source tables mapping to the same target+ changes:+ - addUniqueConstraint:+ tableName: table_remapping+ constraintName: idx_unique_table_remapping_target+ columnNames: database_id, to_db, to_schema, to_table_name++ - changeSet:+ id: v62.2026-04-21T12:00:03+ author: bcm+ comment: Add foreign key on table_remapping(database_id)+ ## Added after the unique constraints so MySQL can satisfy the FK index+ ## requirement using the source-side unique index (whose leading column is+ ## `database_id`) instead of auto-creating a duplicate.+ changes:+ - addForeignKeyConstraint:+ baseTableName: table_remapping+ baseColumnNames: database_id+ referencedTableName: metabase_database+ referencedColumnNames: id+ constraintName: fk_table_remapping_database_id+ deleteCascade: true++ - changeSet:+ id: v62.2026-04-22T12:00:00+ author: eric+ comment: Create workspace table+ changes:+ - createTable:+ tableName: workspace+ remarks: Groups one or more databases with override credentials and per-database schema config+ columns:+ - column:+ name: id+ type: int+ remarks: Unique ID+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: name+ type: varchar(254)+ remarks: Display name of the workspace+ constraints:+ nullable: false+ - column:+ name: creator_id+ type: int+ remarks: User who created this workspace+ constraints:+ nullable: true+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: When this workspace was created+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false+ - column:+ name: updated_at+ type: ${timestamp_type}+ remarks: When this workspace was last updated+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false++ - changeSet:+ id: v62.2026-04-22T12:00:01+ author: eric+ comment: Create workspace_database table+ changes:+ - createTable:+ tableName: workspace_database+ remarks: Associates a workspace with a database, with override credentials and schema config+ columns:+ - column:+ name: id+ type: int+ remarks: Unique ID+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: workspace_id+ type: int+ remarks: The workspace this row belongs to+ constraints:+ nullable: false+ - column:+ name: database_id+ type: int+ remarks: The underlying database this row configures+ constraints:+ nullable: false+ - column:+ name: database_details+ type: ${text.type}+ remarks: JSON-encoded override connection details+ constraints:+ nullable: false+ - column:+ name: output_namespace+ type: varchar(254)+ remarks: Driver-opaque namespace string where workspace output tables are written (schema on Postgres-shaped drivers, database name on MySQL)+ constraints:+ nullable: false+ - column:+ name: input_schemas+ type: ${text.type}+ remarks: JSON array of input schema names+ constraints:+ nullable: false+ - column:+ name: status+ type: varchar(20)+ defaultValue: unprovisioned+ remarks: Provisioning state+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: When this row was created+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false+ - column:+ name: updated_at+ type: ${timestamp_type}+ remarks: When this row was last updated+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false++ - changeSet:+ id: v62.2026-04-22T12:00:02+ author: eric+ comment: FK workspace.creator_id -> core_user.id (set null on delete so deleting a user doesn't cascade)+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace+ baseColumnNames: creator_id+ referencedTableName: core_user+ referencedColumnNames: id+ constraintName: fk_workspace_creator_id+ onDelete: SET NULL++ - changeSet:+ id: v62.2026-04-22T12:00:03+ author: eric+ comment: Unique constraint on workspace_database (workspace_id, database_id)+ changes:+ - addUniqueConstraint:+ tableName: workspace_database+ constraintName: idx_unique_workspace_database+ columnNames: workspace_id, database_id++ - changeSet:+ id: v62.2026-04-22T12:00:04+ author: eric+ comment: FK workspace_database.workspace_id -> workspace.id (cascade on delete)+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_database+ baseColumnNames: workspace_id+ referencedTableName: workspace+ referencedColumnNames: id+ constraintName: fk_workspace_database_workspace_id+ deleteCascade: true++ - changeSet:+ id: v62.2026-04-22T12:00:05+ author: eric+ comment: FK workspace_database.database_id -> metabase_database.id (restrict on delete)+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_database+ baseColumnNames: database_id+ referencedTableName: metabase_database+ referencedColumnNames: id+ constraintName: fk_workspace_database_database_id+ onDelete: RESTRICT++ - changeSet:+ id: v62.2026-04-22T12:00:06+ author: eric+ comment: Index workspace_database.workspace_id for hydration lookups+ changes:+ - createIndex:+ tableName: workspace_database+ indexName: idx_workspace_database_workspace_id+ columns:+ - column:+ name: workspace_iddiff --git a/resources/migrations/062/20260504_admin_details.yaml b/resources/migrations/062/20260504_admin_details.yamlnew file mode 100644index 00000000000..77e4a468cf6--- /dev/null+++ b/resources/migrations/062/20260504_admin_details.yaml@@ -0,0 +1,28 @@+## Add admin_details column to metabase_database for the :admin-connection feature++databaseChangeLog:++ - changeSet:+ id: v62.2026-05-04T00:00:00+ author: galdre+ comment: Add admin_details column to metabase_database for separate admin (highest-privilege) connection credentials+ preConditions:+ - onFail: MARK_RAN+ - not:+ - columnExists:+ tableName: metabase_database+ columnName: admin_details+ changes:+ - addColumn:+ tableName: metabase_database+ columns:+ - column:+ name: admin_details+ type: ${text.type}+ remarks: Encrypted JSON containing connection details for admin (highest-privilege) operations such as DDL and ownership changes+ constraints:+ nullable: true+ rollback:+ - dropColumn:+ tableName: metabase_database+ columnName: admin_detailsdiff --git a/resources/python-sources/sql_tools.py b/resources/python-sources/sql_tools.pyindex faf2e4cd778..5beaf2dfb33 100644--- a/resources/python-sources/sql_tools.py+++ b/resources/python-sources/sql_tools.py@@ -506,13 +506,15 @@ def replace_names(sql: str, replacements_json: str, dialect: str = None) -> str:columns = replacements.get("columns") or [] # List of [key, value] pairs# Convert list-of-pairs to lookup dicts for O(1) matching- # Tables: (schema, table) -> new_name+ # Tables: (db, schema, table) -> new_name. `db` is the catalog (BigQuery project,+ # ClickHouse database, etc). Keys with no :db default to None for back-compat.table_map = {}for item in tables:key, new_name = item+ db = key.get("db") # may be Noneschema = key.get("schema") # may be Nonetable = key["table"]- table_map[(schema, table)] = new_name+ table_map[(db, schema, table)] = new_name# Columns: (schema, table, column) -> new_namecolumn_map = {}@@ -528,12 +530,20 @@ def replace_names(sql: str, replacements_json: str, dialect: str = None) -> str:def rename_fn(node):# Schema rename (appears in Table.db)if isinstance(node, exp.Table):- # Capture original values BEFORE any modifications (important for lookup)+ # Capture original values BEFORE any modifications (important for lookup).+ # SQLGlot stores 3-part identifiers as catalog.db.this -> catalog=BigQuery+ # project / ClickHouse db, db=schema, this=table.+ original_db = node.catalog or Noneoriginal_schema = node.dboriginal_table = node.name- # Preserve original quoting status for renamed identifiers- original_schema_quoted = node.args.get("db").quoted if node.args.get("db") else False- original_table_quoted = node.this.quoted if node.this else False+ # Preserve original quoting status for renamed identifiers.+ # Some Table nodes have non-Identifier children (e.g., ExplodingGenerateSeries,+ # Anonymous) that lack a `quoted` attribute — default to False for those.+ db_node = node.args.get("db")+ catalog_node = node.args.get("catalog")+ original_schema_quoted = db_node.quoted if isinstance(db_node, exp.Identifier) else False+ original_db_quoted = catalog_node.quoted if isinstance(catalog_node, exp.Identifier) else False+ original_table_quoted = node.this.quoted if isinstance(node.this, exp.Identifier) else False# Rename schema if presentif original_schema and original_schema in schemas:@@ -541,12 +551,24 @@ def replace_names(sql: str, replacements_json: str, dialect: str = None) -> str:schema_quoted = original_schema_quoted or was_quoted or needs_quoting(raw_schema, dialect)node.set("db", exp.Identifier(this=raw_schema, quoted=schema_quoted))- # Rename table - try exact match first (with original schema), then without schema- new_table = (table_map.get((original_schema, original_table)) or- table_map.get((None, original_table)))+ # Rename table - try most-specific match first, falling back to less-qualified keys.+ # Order: (db, schema, table), (None, schema, table), (None, None, table).+ # This lets a remapping omit :db when it doesn't matter (Postgres-style),+ # and still match a 3-part SQL reference, while a remapping that does include :db+ # only matches references that have the matching catalog.+ new_table = (table_map.get((original_db, original_schema, original_table)) or+ table_map.get((None, original_schema, original_table)) or+ table_map.get((None, None, original_table)))if new_table:if isinstance(new_table, dict):- # New format: {schema: x, table: y}+ # New format: {db?, schema?, table?}+ if new_table.get("db"):+ raw_db, was_quoted = unquote_identifier(new_table["db"], dialect)+ db_quoted = original_db_quoted or was_quoted or needs_quoting(raw_db, dialect)+ node.set("catalog", exp.Identifier(this=raw_db, quoted=db_quoted))+ elif "db" in new_table and new_table["db"] is None:+ # Explicitly clear the catalog (database/project) qualifier.+ node.set("catalog", None)if new_table.get("schema"):# When injecting a new schema, quote if it contains special charactersraw_schema, was_quoted = unquote_identifier(new_table["schema"], dialect)diff --git a/src/metabase/actions/execution.clj b/src/metabase/actions/execution.cljindex cc786467fa7..c621819d7a0 100644--- a/src/metabase/actions/execution.clj+++ b/src/metabase/actions/execution.clj@@ -24,6 +24,7 @@[metabase.util.i18n :refer [tru]][metabase.util.log :as log][metabase.util.malli :as mu]+ [metabase.workspaces.core :as workspaces][toucan2.core :as t2]))(mu/defn- execute-query-action!@@ -190,6 +191,7 @@(mu/defn execute-action!"Execute the given action with the given parameters of shape `{<parameter-id> <value>}."[action request-parameters]+ (workspaces/check-not-in-workspace-mode! "Actions")(let [;; if a value is supplied for a hidden parameter, it should raise an errorfield-settings (get-in action [:visualization_settings :fields])hidden-param-ids (->> (vals field-settings)diff --git a/src/metabase/analytics/prometheus.clj b/src/metabase/analytics/prometheus.cljindex dd914385e1a..bec9452d237 100644--- a/src/metabase/analytics/prometheus.clj+++ b/src/metabase/analytics/prometheus.clj@@ -549,12 +549,12 @@(prometheus/counter :metabase-token-check/attempt{:description "Total number of token checks. Includes a status label.":labels [:status]})- ;; Write-connection telemetry+ ;; Connection-type telemetry (default / write-data / admin)(prometheus/counter :metabase-db-connection/write-op- {:description "JDBC connection pool acquisitions by connection type (default or write-data)."+ {:description "JDBC connection pool acquisitions by connection type (default, write-data, or admin).":labels [:connection-type]})(prometheus/counter :metabase-db-connection/type-resolved- {:description "Write-data details resolved by effective-details (driver-agnostic). Only incremented when write-data-details are genuinely used, not on fallback or workspace swap."+ {:description "Non-default connection details resolved by effective-details (driver-agnostic). Only incremented when an overlay (write-data or admin details) is genuinely used, not on fallback or workspace swap.":labels [:connection-type]});; SQL parsing metrics(prometheus/counter :metabase-sql-parsing/context-timeoutsdiff --git a/src/metabase/analytics/stats.clj b/src/metabase/analytics/stats.cljindex 4ce345ef824..8ed4caed580 100644--- a/src/metabase/analytics/stats.clj+++ b/src/metabase/analytics/stats.clj@@ -795,7 +795,7 @@(defn- ee-snowplow-features-data'[]- (let [features [:sso-jwt :sso-saml :scim :sandboxes :email-allow-list :semantic-search]]+ (let [features [:sso-jwt :sso-saml :scim :sandboxes :email-allow-list :semantic-search :workspaces]](map(fn [feature]{:name featurediff --git a/src/metabase/cmd/copy.clj b/src/metabase/cmd/copy.cljindex 85422c7f3bf..1135bc75afc 100644--- a/src/metabase/cmd/copy.clj+++ b/src/metabase/cmd/copy.clj@@ -144,7 +144,10 @@:model/Sandbox:model/Tenant:model/ConnectionImpersonation- :model/CustomVizPlugin])))+ :model/CustomVizPlugin+ :model/Workspace+ :model/WorkspaceDatabase+ :model/TableRemapping])))(defn- objects->columns+values"Given a sequence of objects/rows fetched from the H2 DB, return a the `columns` that should be used in the `INSERT`diff --git a/src/metabase/driver.clj b/src/metabase/driver.cljindex a2d92496786..cf2853130e7 100644--- a/src/metabase/driver.clj+++ b/src/metabase/driver.clj@@ -376,6 +376,38 @@(defmethod escape-entity-name-for-metadata :default [_driver table-name] table-name)+(defmulti qualified-name-components+ "Which **AST identifier positions** does this driver populate when referencing a table?+ Returns an ordered subset of `#{:db :schema}`. The `:table` position is always implicit.++ These positions correspond to SQLGlot's `Table.catalog` (`:db`) and `Table.db`+ (`:schema`) AST fields — *not* warehouse vocabulary. A warehouse calling its top+ level \"database\" (e.g. ClickHouse) does **not** use the `:db` position here unless+ it emits a 3-part `catalog.schema.table` identifier; it uses `:schema` because that's+ the AST position holding the level above the table.++ Concrete mapping:+ - 1-level (`SELECT * FROM t`): `[]` — Mongo+ - 1-level over a catalog (`SELECT * FROM t`, but workspace remap and other+ consumers need a catalog name to route across DBs): `[:db]` — MySQL.+ The compiled SQL is still bare; this slot tells consumers MySQL has a+ meaningful database identifier (= JDBC `TABLE_CAT`) above the table.+ - 2-level (`SELECT * FROM s.t`): `[:schema]` — Postgres, Redshift, ClickHouse, H2, Oracle+ - 3-level (`SELECT * FROM c.s.t`): `[:db :schema]` — Snowflake, SQL Server, BigQuery+ (Snowflake `db.schema.table`; SQL Server `db.schema.table`; BigQuery+ `project.dataset.table`).++ Used by workspace table remapping to decide:+ - which columns to populate when storing a `:model/TableRemapping` row+ - which AST positions to match against during query rewriting++ Defaults to `[:schema]` (Postgres-style)."+ {:added "0.62.0" :arglists '([driver])}+ dispatch-on-initialized-driver+ :hierarchy #'hierarchy)++(defmethod qualified-name-components ::driver [_driver] [:schema])+(defmulti describe-table-fks"Return information about the foreign keys in a `table`. Required for drivers that support :metadata/key-constraintsbut not :describe-fks. Results should match the [[metabase.sync.interface/FKMetadata]] schema."@@ -1171,10 +1203,11 @@update the value for `:details`. The default implementation is essentially `identity` (i.e returns `database`unchanged). This multimethod will only be called if `:details` is actually present in the `database` map.- Implementations should normalize both `:details` and `:write-data-details` (if present), since- `:write-data-details` is merged on top of `:details` by [[metabase.driver.connection/effective-details]].- Un-normalized fields in `:write-data-details` can leak through to the merged result. See- [[metabase.driver.connection]] for more information."+ Implementations should normalize `:details`, `:write-data-details`, and `:admin-details` (each if present),+ since `:write-data-details` and `:admin-details` are merged on top of `:details` by+ [[metabase.driver.connection/effective-details]] under their respective contexts. Un-normalized fields in+ the overlay maps can leak through to the merged result. See [[metabase.driver.connection]] for more+ information."{:added "0.41.0" :arglists '([driver database])}dispatch-on-initialized-driver:hierarchy #'hierarchy)@@ -1812,12 +1845,20 @@:hierarchy #'hierarchy)(defmulti grant-workspace-read-access!- "Grant read access on specified tables to a workspace's isolated user.- This allows the workspace to read from source tables that it needs as inputs.-- `tables` is a sequence of maps with :schema and :name keys identifying- the tables to grant access to."- {:added "0.59.0" :arglists '([driver database workspace tables])}+ "Grant read access on the workspace's input namespaces to the workspace's isolated+ user/role. `input` is a sequence of `::table-namespace` maps `[{:db ?, :schema ?}]`+ identifying the namespaces to grant access to. Granularity is per-namespace+ (schema-wide / database-wide); per-table grants are not supported.++ Slot semantics by driver:++ - 2-slot schema-having (Postgres, Redshift, ClickHouse): read `:schema`,+ ignore `:db` (connection-bound).+ - 3-slot (Snowflake, SQL Server, BigQuery): read `:db` + `:schema`. `:db`+ falls back to the connection's bound db when absent.+ - schema-less (MySQL): read `:db`; the `qualified-name-components` is `[]`+ and the database name lives in the `:db` slot."+ {:added "0.59.0" :arglists '([driver database workspace input])}dispatch-on-initialized-driver:hierarchy #'hierarchy)diff --git a/src/metabase/driver/common.clj b/src/metabase/driver/common.cljindex 9e6d66b10d8..44fe14854cb 100644--- a/src/metabase/driver/common.clj+++ b/src/metabase/driver/common.clj@@ -141,6 +141,12 @@:type :hidden:default false})+(def admin-connection-option+ "Map representing the 'is this an admin connection' option"+ {:name "admin-connection"+ :type :hidden+ :default false})+(def advanced-options-start"Map representing the start of the advanced option section in a DB connection form. Fields in this section shouldhave their visibility controlled using the `visible-if` property."@@ -159,7 +165,8 @@(str "We execute the underlying query when you explore data using Summarize or Filter. ""This is on by default but you can turn it off if performance is slow.")):visible-if {"advanced-options" true- "write-data-connection" false}})+ "write-data-connection" false+ "admin-connection" false}})(def let-user-control-scheduling"Map representing the `let-user-control-scheduling` option in a DB connection form."@@ -168,7 +175,8 @@:display-name (deferred-tru "Choose when syncs and scans happen"):description (deferred-tru "By default, Metabase does a lightweight hourly sync and an intensive daily scan of field values. If you have a large database, turn this on to make changes."):visible-if {"advanced-options" true- "write-data-connection" false}})+ "write-data-connection" false+ "admin-connection" false}})(def metadata-sync-schedule"Map representing the `schedules.metadata_sync` option in a DB connection form, which should be only visible if@@ -198,7 +206,8 @@:display-name (deferred-tru "Allow unfolding of JSON columns"):type :boolean:visible-if {"advanced-options" true- "write-data-connection" false}+ "write-data-connection" false+ "admin-connection" false}:description (deferred-tru(str "This enables unfolding JSON columns into their component fields. ""Disable unfolding if performance is slow. If enabled, you can still disable unfolding for "@@ -214,7 +223,8 @@(str "This enables Metabase to scan for additional field values during syncs allowing smarter ""behavior, like improved auto-binning on your bar charts.")):visible-if {"advanced-options" true- "write-data-connection" false}})+ "write-data-connection" false+ "admin-connection" false}})(def multi-level-schema"Map representing the `multi-level-schema` option for databases. Stores schemas with multiple levels of hierarchy."@@ -226,6 +236,7 @@"Vector containing the most common options present in the advanced option section of the DB connection form."[destination-database-optionwrite-data-connection-option+ admin-connection-optionauto-run-querieslet-user-control-schedulingmetadata-sync-schedulediff --git a/src/metabase/driver/connection.clj b/src/metabase/driver/connection.cljindex f6a2fb7f8b9..82c1f8b49db 100644--- a/src/metabase/driver/connection.clj+++ b/src/metabase/driver/connection.clj@@ -15,7 +15,8 @@[metabase.premium-features.core :refer [defenterprise]][metabase.util :as u][metabase.util.log :as log]- [metabase.util.malli.registry :as mr]))+ [metabase.util.malli.registry :as mr]+ [metabase.util.performance :as perf]));; Database `:details` is a single encrypted JSON column that stores everything a driver;; needs to know about a connection. The problem is that "everything a driver needs to know"@@ -60,16 +61,21 @@;; - (let [{:keys [details]} database]);; - (let [{{:keys [user]} :details} database])+(def connection-types+ "All valid values for [[*connection-type*]]."+ [:default :write-data :admin])+(mr/def ::connection-type- [:enum :default :write-data])+ (into [:enum] connection-types))(def ^:dynamic *connection-type*"Which connection details [[effective-details]] should resolve.- `:default` — primary `:details`- `:write-data` — `:write-data-details` merged over `:details` (if configured)+ - `:admin` — `:admin-details` merged over `:details` (if configured)- Bind via [[with-write-connection]] or [[with-default-connection]], not directly."+ Bind via [[with-write-connection]], [[with-default-connection]], or [[with-admin-connection]], not directly.":default)(defmacro with-write-connection@@ -81,6 +87,21 @@`(binding [*connection-type* :write-data]~@body))+(defmacro with-admin-connection+ "Establishes an admin-connection context for body.++ [[effective-details]] calls within this scope resolve to take `:admin-details`+ into account (if configured) instead of only primary `:details`. The admin+ connection carries the highest-privilege credentials for a database (DDL,+ ownership, schema management). Code that needs admin access must opt in+ explicitly."+ [& body]+ `(let [prior# *connection-type*]+ (when (not= prior# :admin)+ (log/infof "Entering :admin connection scope (from %s)" prior#))+ (binding [*connection-type* :admin]+ ~@body)))+(defmacro with-default-connection"Establishes a default-connection context for body.@@ -100,69 +121,102 @@`(binding [*suppress-resolution-telemetry* true]~@body))+(defmacro with-swapped-connection-details+ "Re-export of [[metabase.driver.connection.workspaces/with-swapped-connection-details]]+ on the driver module's public API surface, so consumers outside the module can apply+ per-database connection-detail overrides through `metabase.driver.connection`."+ {:style/indent 2}+ [database-id swap-map & body]+ `(driver.w/do-with-swapped-connection-details ~database-id ~swap-map (fn [] ~@body)))+(defenterprise database-write-data-details"Returns the `:write-data-details` for a database, or `nil` if the writable-connection feature is not available.OSS implementation always returns `nil`."- metabase-enterprise.writable-connection.core+ metabase-enterprise.connection-overlays.core+ [_database]+ nil)++(defenterprise database-admin-details+ "Returns the `:admin-details` for a database, or `nil` if the workspaces feature is not available.+ OSS implementation always returns `nil`."+ metabase-enterprise.connection-overlays.core[_database]nil)+(defn- overlay-details-for-type+ "Returns the connection-type-specific details overlay for `database`, or nil if the+ requested type has no configured overlay (or is `:default`)."+ [database connection-type]+ (case connection-type+ :default nil+ :write-data (database-write-data-details database)+ :admin (database-admin-details database)))+(defn effective-details"Returns the connection details map appropriate for the current context.Accepts a database (Toucan2 instance or lib/metadata). Returns nil for nil input.- By default, returns the primary `:details`. Within a [[with-write-connection]] scope,- takes `:write-data-details` into account (if configured). Within a+ By default, returns the primary `:details`. Within a [[with-write-connection]] or+ [[with-admin-connection]] scope, takes the corresponding `:write-data-details` /+ `:admin-details` into account (if configured). Within a[[driver.w/with-swapped-connection-details]] scope, applies workspace isolationoverrides on top."[database](when-let [database (some-> database driver.u/ensure-lib-database)]- (let [write? (= *connection-type* :write-data)- write-details (when write? (database-write-data-details database))- base (merge (:details database) write-details)]- ;; Track when write-data-details are genuinely used (not fallback, not workspace-swapped).+ (let [overlay (perf/not-empty (overlay-details-for-type database *connection-type*))+ base (merge (:details database) overlay)+ eff-type (if overlay *connection-type* :default)]+ ;; Track when an overlay is genuinely used (not fallback, not workspace-swapped).;; Default resolutions are not tracked here — see :metabase-db-connection/write-op for;; pool-level connection acquisition metrics.- (when (and write-details+ (when (and overlay(not *suppress-resolution-telemetry*)(not (driver.w/has-connection-swap? (:id database))))- (try (analytics/inc! :metabase-db-connection/type-resolved {:connection-type "write-data"})+ (try (analytics/inc! :metabase-db-connection/type-resolved+ {:connection-type (name *connection-type*)})(catch Exception _ nil)))(-> (driver.w/maybe-swap-details (:id database) base)- (assoc ::effective-connection-type (if write-details :write-data :default))+ (assoc ::effective-connection-type eff-type)(assoc ::database-id (u/id database))))))(defn details-for-exact-type"Returns the details map for exactly the given connection-type, with no fallback or merging.- Unlike [[effective-details]], `:write-data` returns only `:write-data-details` (possibly nil),- not a merge with `:details`. Use when you need to inspect or update a specific details map- without resorting to raw key access."+ Unlike [[effective-details]], `:write-data` and `:admin` return only their respective+ overlay maps (possibly nil), not a merge with `:details`. Use when you need to inspect+ or update a specific details map without resorting to raw key access."[database connection-type](let [database (driver.u/ensure-lib-database database)](case connection-type:default (:details database)- :write-data (database-write-data-details database))))+ :write-data (database-write-data-details database)+ :admin (database-admin-details database))))(defn write-connection-requested?"True if currently executing within a [[with-write-connection]] scope."[](= *connection-type* :write-data))+(defn admin-connection-requested?+ "True if currently executing within a [[with-admin-connection]] scope."+ []+ (= *connection-type* :admin))+(defn effective-connection-type"Returns the connection type actually in effect for the given database. Return valuematches malli schema [[::connection-type]].- Returns `:write-data` only when a write connection is both *requested*- ([[with-write-connection]]) and *configured* (the database has `:write-data-details`).- Otherwise returns `:default`, avoiding duplicate resource allocation (e.g. connection- pools) when the requested type would resolve identically to `:default`."+ Returns the requested non-default type only when both *requested* (via the+ corresponding `with-*-connection`) and *configured* (the database has the relevant+ overlay details). Otherwise returns `:default`, avoiding duplicate resource allocation+ (e.g. connection pools) when the requested type would resolve identically to `:default`."[database]- (if (and (= *connection-type* :write-data)- (some? (database-write-data-details (driver.u/ensure-lib-database database))))- :write-data- :default))+ (let [database (driver.u/ensure-lib-database database)]+ (if (and (not= *connection-type* :default)+ (perf/not-empty (overlay-details-for-type database *connection-type*)))+ *connection-type*+ :default)))(defn track-connection-acquisition!"Increments a Prometheus counter tracking connection acquisitions by connection typediff --git a/src/metabase/driver/h2.clj b/src/metabase/driver/h2.cljindex 98e78b37f00..90a0882434f 100644--- a/src/metabase/driver/h2.clj+++ b/src/metabase/driver/h2.clj@@ -760,23 +760,19 @@(.executeBatch ^Statement stmt)))))(defmethod driver/grant-workspace-read-access! :h2- [_driver database workspace tables]+ [_driver database workspace schemas](let [username (-> workspace :database_details :db get-user-from-connection-string)qu (sql.u/quote-name :h2 :field username)- schemas (distinct (map :schema tables))]- ;; H2 uses GRANT SELECT ON SCHEMA schemaName TO userName+ schemas (distinct schemas)]+ ;; H2 uses GRANT SELECT ON SCHEMA schemaName TO userName.+ ;; Schema-wide grant covers existing + future tables. Per-table grants+ ;; are not emitted: workspace input shape is per-namespace, not per-table.(jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))](with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))](doseq [schema schemas](.addBatch ^Statement stmt^String (format "GRANT SELECT ON SCHEMA %s TO %s"(sql.u/quote-name :h2 :schema schema) qu)))- ;; Also grant on individual tables for more fine-grained access- (doseq [table tables]- (.addBatch ^Statement stmt- ^String (format "GRANT SELECT ON %s.%s TO %s"- (sql.u/quote-name :h2 :schema (:schema table))- (sql.u/quote-name :h2 :table (:name table)) qu)))(.executeBatch ^Statement stmt)))))(defmethod driver/llm-sql-dialect-resource :h2 [_]diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.cljindex c21f7d38f7b..15eb4f4419a 100644--- a/src/metabase/driver/mysql.clj+++ b/src/metabase/driver/mysql.clj@@ -100,6 +100,28 @@:workspace true}](defmethod driver/database-supports? [:mysql feature] [_driver _feature _db] supported?))+(defmethod driver/qualified-name-components :mysql+ [_driver]+ ;; MySQL's "schema" doesn't exist; its "database" is what other engines call a schema and+ ;; what JDBC calls a catalog. We populate the `:db` AST slot (= SQLGlot `Table.catalog`)+ ;; with the connection's bound DB so workspace remap can record both canonical and iso+ ;; DB names on `TableRemapping` rows and the QP can rewrite identifiers across DBs.+ ;; Production SELECTs continue to emit unqualified `t` because the SQL compiler reads+ ;; the per-driver `quote-name` rules, not this multimethod.+ [:db])++;;; MySQL has no schema layer. Workspace remap stores `:db.table` -- the `:db`+;;; slot carries the connection's bound DB so cross-DB routing works. Production+;;; SELECTs emit unqualified `t` because the SQL compiler reads `quote-name`+;;; rules, not this multimethod.+(defmethod driver.sql/table-qualification-style :mysql+ [_driver]+ :table-qualification-style/db-table)++(defmethod driver.sql/db-slot-value :mysql+ [_driver database]+ (:db (:details database)))+;; This is a bit of a lie since the JSON type was introduced for MySQL since 5.7.8.;; And MariaDB doesn't have the JSON type at all, though `JSON` was introduced as an alias for LONGTEXT in 10.2.7.;; But since JSON unfolding will only apply columns with JSON types, this won't cause any problems during sync.@@ -743,8 +765,18 @@(sql-jdbc.common/handle-additional-options details))))))(defmethod sql-jdbc.sync/active-tables :mysql- [& args]- (apply sql-jdbc.sync/post-filtered-active-tables args))+ [driver ^java.sql.Connection conn schema-inclusion-filters schema-exclusion-filters]+ ;; Scope describe-database to the connection's current catalog (= bound DB).+ ;; By default `post-filtered-active-tables` passes `nil` for the catalog filter,+ ;; which makes JDBC enumerate every DB the user has privileges on. That's wrong+ ;; for workspace MySQL users — they own the iso DB and have GRANT SELECT on the+ ;; canonical DB, so the iso DB's tables would leak into sync. Constraining to+ ;; `conn.getCatalog()` means only the canonical (bound) DB's tables show up,+ ;; regardless of what else the user can read. Also strictly more correct for+ ;; plain MySQL: a user with GRANT on multiple DBs only configured one as the+ ;; Metabase Database; sync should never have surfaced the others.+ (sql-jdbc.sync/post-filtered-active-tables+ driver conn (.getCatalog conn) schema-inclusion-filters schema-exclusion-filters))(defmethod sql-jdbc.sync/excluded-schemas :mysql[_]@@ -1296,7 +1328,14 @@(.addBatch ^Statement stmt ^String sql))(.executeBatch ^Statement stmt)))){:schema db-name- :database_details {:user user, :password password :db db-name}}))+ ;; Intentionally omit `:db` from `:database_details`: when the workspace+ ;; loader merges these over the canonical Database's `:details`, we must+ ;; not overwrite the connection's bound database. MySQL workspace users+ ;; need to read from the canonical input DB (granted via `GRANT SELECT+ ;; ON <input-db>.*`) while output writes go to `db-name` via fully+ ;; qualified `INSERT INTO <db-name>.<table>`. The output DB lives in+ ;; the WSD row's `:output_namespace`, not in connection details.+ :database_details {:user user, :password password}}))(defmethod driver/destroy-workspace-isolation! :mysql[_driver database workspace]@@ -1310,19 +1349,28 @@(.addBatch ^Statement stmt ^String sql))(.executeBatch ^Statement stmt)))))+(defn- grant-workspace-read-access-sqls+ "Build SQL statements that grant `username` SELECT on all tables in each database+ named in `schemas`. MySQL has no schema layer — `qualified-name-components`+ is `[]` — so each entry in `schemas` is interpreted as a database name.+ Workspace-scoped users receive database-wide SELECT via `GRANT SELECT ON db.*`."+ [username schemas]+ (let [qu (sql.u/quote-name :mysql :field username)+ source-databases (set schemas)]+ (perf/mapv (fn [db]+ (format "GRANT SELECT ON %s.* TO %s@'%%'"+ (sql.u/quote-name :mysql :schema db) qu))+ source-databases)))+(defmethod driver/grant-workspace-read-access! :mysql- [_driver database workspace tables]+ [_driver database workspace schemas]+ ;; Each entry in `schemas` is interpreted as a MySQL database name. The+ ;; workspace SA gets database-wide SELECT via `GRANT SELECT ON db.*`. The+ ;; Metabase `Database` row's `:details.db` is the bound database, but MySQL+ ;; itself allows `GRANT SELECT ON other_db.*` as long as the admin has it,+ ;; so we don't reject inputs that don't equal `:details.db`.(let [username (-> workspace :database_details :user)- qu (sql.u/quote-name :mysql :field username)- ;; In MySQL, tables don't have separate schemas within a database,- ;; but the :schema field contains the source database name- sqls (for [{db :schema, t :name} tables]- (if (str/blank? db)- (format "GRANT SELECT ON %s TO %s@'%%'"- (sql.u/quote-name :mysql :table t) qu)- (format "GRANT SELECT ON %s.%s TO %s@'%%'"- (sql.u/quote-name :mysql :schema db)- (sql.u/quote-name :mysql :table t) qu)))]+ sqls (grant-workspace-read-access-sqls username schemas)](jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))](with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))](doseq [sql sqls]@@ -1337,44 +1385,50 @@[driver database test-table](let [test-workspace {:id perm-check-workspace-id:name "_mb_perm_check_"}]- (sql-jdbc.execute/do-with-connection-with-options- driver- database- {:write? true}- (fn [^Connection _conn]- (let [result (try- (let [init-result (try- (driver/init-workspace-isolation! driver database test-workspace)- (catch Exception e- (throw (ex-info (tru "Failed to initialize workspace isolation (CREATE DATABASE/USER): {0}"- (ex-message e))- {:step :init} e))))- workspace-with-details (merge test-workspace init-result)]- (when test-table+ (driver.conn/with-admin-connection+ (sql-jdbc.execute/do-with-connection-with-options+ driver+ database+ {:write? true}+ (fn [^Connection _conn]+ (let [result (try+ (let [init-result (try+ (driver/init-workspace-isolation! driver database test-workspace)+ (catch Exception e+ (throw (ex-info (tru "Failed to initialize workspace isolation (CREATE DATABASE/USER): {0}"+ (ex-message e))+ {:step :init} e))))+ workspace-with-details (merge test-workspace init-result)]+ (when test-table+ (try+ ;; `grant-workspace-read-access!` takes a vector of+ ;; schema-name strings. MySQL has no schema layer; each+ ;; entry is interpreted as a database name. `test-table`'s+ ;; `:schema` from the caller IS the MySQL database name.+ (driver/grant-workspace-read-access! driver database workspace-with-details+ [(:schema test-table)])+ (catch Exception e+ (throw (ex-info (tru "Failed to grant read access to database {0}: {1}"+ (:schema test-table) (ex-message e))+ {:step :grant :table test-table} e)))))(try- (driver/grant-workspace-read-access! driver database workspace-with-details [test-table])+ (driver/destroy-workspace-isolation! driver database workspace-with-details)(catch Exception e- (throw (ex-info (tru "Failed to grant read access to table {0}.{1}: {2}"- (:schema test-table) (:name test-table) (ex-message e))- {:step :grant :table test-table} e)))))- (try- (driver/destroy-workspace-isolation! driver database workspace-with-details)- (catch Exception e- (throw (ex-info (tru "Failed to destroy workspace isolation (DROP DATABASE/USER): {0}"- (ex-message e))- {:step :destroy} e))))- nil)- (catch Exception e- ;; On failure, attempt cleanup- (try- (driver/destroy-workspace-isolation! driver database- (merge test-workspace- {:schema (driver.u/workspace-isolation-namespace-name test-workspace)- :database_details {:user (driver.u/workspace-isolation-user-name test-workspace)}}))- (catch Exception _cleanup-error- nil))- (ex-message e)))]- result)))))+ (throw (ex-info (tru "Failed to destroy workspace isolation (DROP DATABASE/USER): {0}"+ (ex-message e))+ {:step :destroy} e))))+ nil)+ (catch Exception e+ ;; On failure, attempt cleanup+ (try+ (driver/destroy-workspace-isolation! driver database+ (merge test-workspace+ {:schema (driver.u/workspace-isolation-namespace-name test-workspace)+ :database_details {:user (driver.u/workspace-isolation-user-name test-workspace)}}))+ (catch Exception _cleanup-error+ nil))+ (ex-message e)))]+ result))))))(defmethod driver/llm-sql-dialect-resource :mysql [_]"metabot/prompts/dialects/mysql.md")diff --git a/src/metabase/driver/postgres.clj b/src/metabase/driver/postgres.cljindex 922c72d686a..71ab0334480 100644--- a/src/metabase/driver/postgres.clj+++ b/src/metabase/driver/postgres.clj@@ -1438,6 +1438,63 @@[conn username](seq (jdbc/query conn ["SELECT 1 FROM pg_user WHERE usename = ?" username])))+(defn- public-create-grant?+ "True if `schema-name` grants CREATE to the PUBLIC pseudo-role on PostgreSQL.++ When this is the case, REVOKE-ing CREATE from the workspace user is a no-op:+ the user still inherits CREATE through PUBLIC, can create tables in input+ schemas, and the resulting orphaned objects make `DROP USER` fail at+ deprovisioning time. Workspace isolation can't enforce read-only on input+ schemas in this state.++ Historical default on PostgreSQL ≤14; PostgreSQL 15 changed the default to+ drop CREATE for PUBLIC on `public`. Redshift uses a different check (see+ the redshift driver) because it can't cast `aclitem[]` to text."+ [conn schema-name]+ (let [{:keys [acl]} (first (jdbc/query conn ["SELECT nspacl::text AS acl FROM pg_namespace WHERE nspname = ?" schema-name]))]+ (boolean+ (when (and acl (> (count acl) 1))+ ;; nspacl text form: `{<grantee>=<perms>/<grantor>,...}`. PUBLIC entries+ ;; have an empty grantee, so they appear as `=<perms>/<grantor>` after+ ;; the opening `{` or after a comma.+ (re-find #"(?:^\{|,)=[^/]*C" acl)))))++(defn raise-public-create-grant!+ "Throw the standard ex-info for the public-CREATE-grant pre-condition. Shared+ between drivers (Postgres uses [[public-create-grant?]]; Redshift uses its+ own check via `SVV_SCHEMA_PRIVILEGES`)."+ [schema-name]+ (throw (ex-info (format (str "Schema \"%s\" grants CREATE to PUBLIC; workspace isolation cannot "+ "enforce input-schema read-only until this is revoked. Run:\n\n"+ " REVOKE CREATE ON SCHEMA %s FROM PUBLIC;\n\n"+ "then retry workspace provisioning.")+ schema-name schema-name)+ {:status-code 412+ :schema schema-name})))++(defn assert-no-public-create-grant!+ "Throws when `schema-name` has CREATE granted to PUBLIC on PostgreSQL. Called+ from `grant-workspace-read-access!` for each input schema being granted —+ we want this check per-schema (only the schemas actually used as inputs+ matter), not a blanket check on `public` at init time."+ [conn schema-name]+ (when (public-create-grant? conn schema-name)+ (raise-public-create-grant! schema-name)))++;;; Isolation limit, checked at grant time in [[grant-workspace-read-access!]] via+;;; [[assert-no-public-create-grant!]]: PostgreSQL's permission model lets a user+;;; receive privileges either directly or through PUBLIC (the implicit pseudo-role+;;; every login is a member of). REVOKE-ing CREATE from a specific user only+;;; removes their *direct* grant — it can't override a PUBLIC grant. So if any+;;; input schema (most commonly `public` itself, on PostgreSQL ≤14 where that+;;; was the default) grants CREATE to PUBLIC, the workspace user inherits it+;;; transitively and our isolation contract leaks: the user can create tables+;;; in input schemas, becoming their owner, which then makes `DROP USER` fail+;;; at deprovisioning. The probe at grant time fails fast with a 412 so the+;;; cluster admin can run `REVOKE CREATE ON SCHEMA <name> FROM PUBLIC` before+;;; retrying. PostgreSQL 15+ removed the permissive default on `public`, so+;;; fresh PG15 clusters typically pass; PG14 and earlier need the manual revoke.+(defmethod driver/init-workspace-isolation! :postgres[_driver database workspace](let [schema-name (driver.u/workspace-isolation-namespace-name workspace)@@ -1478,27 +1535,35 @@(.addBatch ^Statement stmt ^String sql))(.executeBatch ^Statement stmt)))))+(defn- grant-workspace-read-access-sqls+ "Build the sequence of SQL statements that grant `username` read access to every+ table in each schema named in `schemas`. Per source schema we emit three+ statements: USAGE on the schema, SELECT on all existing tables in the schema,+ and an ALTER DEFAULT PRIVILEGES covering future tables created by the granting+ role."+ [username schemas]+ (let [qu (sql.u/quote-name :postgres :field username)+ source-schemas (set schemas)]+ (mapcat (fn [s]+ (let [qs (sql.u/quote-name :postgres :schema s)]+ [(format "GRANT USAGE ON SCHEMA %s TO %s" qs qu)+ (format "GRANT SELECT ON ALL TABLES IN SCHEMA %s TO %s" qs qu)+ (format "ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT SELECT ON TABLES TO %s" qs qu)]))+ source-schemas)))+(defmethod driver/grant-workspace-read-access! :postgres- [_driver database workspace tables]+ [_driver database workspace schemas](let [username (-> workspace :database_details :user)- qu (sql.u/quote-name :postgres :field username)- ;; Collect all unique source schemas that contain the tables we need to grant access to- source-schemas (into #{} (keep :schema) tables)- ;; Grant USAGE on source schemas, then SELECT on each table- ;; Note: workspace schema already has ALL PRIVILEGES from init, so no need to grant USAGE there- sqls (concat- ;; USAGE on each source schema containing tables we're granting access to- (for [s source-schemas]- (format "GRANT USAGE ON SCHEMA %s TO %s"- (sql.u/quote-name :postgres :schema s) qu))- ;; SELECT on each table- (for [{s :schema, t :name} tables]- (if (str/blank? s)- (format "GRANT SELECT ON TABLE %s TO %s"- (sql.u/quote-name :postgres :table t) qu)- (format "GRANT SELECT ON TABLE %s.%s TO %s"- (sql.u/quote-name :postgres :schema s)- (sql.u/quote-name :postgres :table t) qu))))]+ source-schemas (set schemas)+ ;; Pre-flight check: each input schema must not grant CREATE to PUBLIC. See+ ;; the comment block above [[init-workspace-isolation! :postgres]] for the+ ;; isolation hole this catches. We probe per-schema so only the schemas+ ;; actually used as inputs need to be locked down — schemas the workspace+ ;; never touches can keep their default ACLs.+ _ (jdbc/with-db-transaction [check-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (doseq [s source-schemas]+ (assert-no-public-create-grant! check-conn s)))+ sqls (grant-workspace-read-access-sqls username schemas)](jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))](with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))](doseq [sql sqls]diff --git a/src/metabase/driver/sql.clj b/src/metabase/driver/sql.cljindex aa175bff62a..e04650cc4fa 100644--- a/src/metabase/driver/sql.clj+++ b/src/metabase/driver/sql.clj@@ -123,6 +123,38 @@[_ _database]nil)+;;; +----------------------------------------------------------------------------------------------------------------++;;; | Table identifier qualification |+;;; +----------------------------------------------------------------------------------------------------------------+++(defmulti table-qualification-style+ "Returns the shape of identifiers this driver emits for tables in compiled SQL. One of:++ - `:table-qualification-style/table` — bare `table` (no current driver uses this)+ - `:table-qualification-style/schema-table` — `schema.table` (e.g. Postgres, Redshift, H2, ClickHouse) — default+ - `:table-qualification-style/db-table` — `db.table` (e.g. MySQL, which calls its table-containers+ \"database\"; the default db name is fixed by the JDBC connection URL)+ - `:table-qualification-style/db-schema-table` — `db.schema.table` (e.g. SQL Server, BigQuery)"+ {:added "0.62.0" :arglists '([driver])}+ driver/dispatch-on-initialized-driver+ :hierarchy #'driver/hierarchy)++(defmethod table-qualification-style :default+ [_]+ :table-qualification-style/schema-table)++(defmulti db-slot-value+ "Returns the project-id / catalog string a driver places in the `db` segment of a fully-qualified+ table reference. Meaningful only for drivers whose [[table-qualification-style]] includes a `db`+ segment; returns `nil` otherwise."+ {:added "0.62.0" :arglists '([driver database])}+ (fn [driver _] driver)+ :hierarchy #'driver/hierarchy)++(defmethod db-slot-value :default+ [_ _database]+ nil)+;;; +----------------------------------------------------------------------------------------------------------------+;;; | Transforms |;;; +----------------------------------------------------------------------------------------------------------------+diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.cljindex 9d115afcc8d..e4a4c477f03 100644--- a/src/metabase/driver/sql/query_processor.clj+++ b/src/metabase/driver/sql/query_processor.clj@@ -2049,8 +2049,13 @@(defmethod ->honeysql [:sql :metadata/table][driver table]- (let [{table-name :name, schema :schema} table]- (->honeysql driver (h2x/identifier :table schema table-name))))+ ;; `:db` is normally absent on `:metadata/table` — sync doesn't populate it.+ ;; Workspace remap (and any future cross-DB rewriter) can fill it to route the+ ;; query at a database different from the connection's bound one. The+ ;; identifier helper drops nil components, so absent `:db` produces the same+ ;; `schema.table` shape as before.+ (let [{table-name :name, schema :schema, db :db} table]+ (->honeysql driver (h2x/identifier :table db schema table-name))))(defmethod apply-top-level-clause [:sql :source-table][driver _top-level-clause honeysql-form {source-table-id :source-table}]@@ -2308,10 +2313,26 @@;;;; Transforms(defmethod driver/compile-transform :sql- [driver {:keys [query output-table]}]- (let [{sql-query :query sql-params :params} query]+ [driver {:keys [query output-db output-table]}]+ (let [{sql-query :query sql-params :params} query+ ;; If the workspace remap populated `:output-db`, qualify the output+ ;; table with that DB so the CTAS lands in the iso database. Used by+ ;; MySQL workspace transforms today — the iso namespace lives at the+ ;; AST `:db` position and the canonical `output-table` is bare.+ ;;+ ;; HoneySQL renders `(keyword ns name)` as ``ns`.`name`` on MySQL — we+ ;; lean on that here. 3-part `:db.:schema.:name` writes (Snowflake / SQL+ ;; Server / BigQuery cross-DB) aren't expressible through this single-+ ;; keyword shape; when those workspaces land they'll either need+ ;; `output-table` to carry both qualifiers or a different HoneySQL form.+ target-id (cond+ (and (not (str/blank? output-db))+ (str/blank? (namespace (keyword output-table))))+ (keyword output-db (clojure.core/name (keyword output-table)))+ :else+ (keyword output-table))][(first (format-honeysql driver- {:create-table-as [(keyword output-table)]+ {:create-table-as [target-id]:raw sql-query}))sql-params]))diff --git a/src/metabase/driver/sql_jdbc.clj b/src/metabase/driver/sql_jdbc.cljindex 6fe05c585e3..784c576ebaf 100644--- a/src/metabase/driver/sql_jdbc.clj+++ b/src/metabase/driver/sql_jdbc.clj@@ -8,6 +8,7 @@[medley.core :as m][metabase.driver :as driver][metabase.driver-api.core :as driver-api]+ [metabase.driver.connection :as driver.conn][metabase.driver.sql :as driver.sql][metabase.driver.sql-jdbc.actions :as sql-jdbc.actions][metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]@@ -413,35 +414,39 @@[driver database test-table](let [test-workspace {:id perm-check-workspace-id:name "_mb_perm_check_"}]- (sql-jdbc.execute/do-with-connection-with-options- driver- database- {:write? true}- (fn [^Connection conn]- (.setAutoCommit conn false)- (try- (let [init-result (try- (driver/init-workspace-isolation! driver database test-workspace)- (catch Exception e- (throw (ex-info (tru "Failed to initialize workspace isolation (CREATE SCHEMA/USER): {0}"- (ex-message e))- {:step :init} e))))- workspace-with-details (merge test-workspace init-result)]- (when test-table+ (driver.conn/with-admin-connection+ (sql-jdbc.execute/do-with-connection-with-options+ driver+ database+ {:write? true}+ (fn [^Connection conn]+ (.setAutoCommit conn false)+ (try+ (let [init-result (try+ (driver/init-workspace-isolation! driver database test-workspace)+ (catch Exception e+ (throw (ex-info (tru "Failed to initialize workspace isolation (CREATE SCHEMA/USER): {0}"+ (ex-message e))+ {:step :init} e))))+ workspace-with-details (merge test-workspace init-result)]+ (when test-table+ (try+ ;; `grant-workspace-read-access!` takes a vector of schema-name+ ;; strings; per-table grants no longer exist.+ (driver/grant-workspace-read-access! driver database workspace-with-details+ [(:schema test-table)])+ (catch Exception e+ (throw (ex-info (tru "Failed to grant read access to schema {0}: {1}"+ (:schema test-table) (ex-message e))+ {:step :grant :table test-table} e)))))(try- (driver/grant-workspace-read-access! driver database workspace-with-details [test-table])+ (driver/destroy-workspace-isolation! driver database workspace-with-details)(catch Exception e- (throw (ex-info (tru "Failed to grant read access to table {0}.{1}: {2}"- (:schema test-table) (:name test-table) (ex-message e))- {:step :grant :table test-table} e)))))- (try- (driver/destroy-workspace-isolation! driver database workspace-with-details)- (catch Exception e- (throw (ex-info (tru "Failed to destroy workspace isolation (DROP SCHEMA/USER): {0}"- (ex-message e))- {:step :destroy} e)))))- nil- (catch Exception e- (ex-message e))- (finally- (.rollback conn)))))))+ (throw (ex-info (tru "Failed to destroy workspace isolation (DROP SCHEMA/USER): {0}"+ (ex-message e))+ {:step :destroy} e)))))+ nil+ (catch Exception e+ (ex-message e))+ (finally+ (.rollback conn))))))))diff --git a/src/metabase/driver/sql_jdbc/connection.clj b/src/metabase/driver/sql_jdbc/connection.cljindex 60b9bf887ca..1223ec278d3 100644--- a/src/metabase/driver/sql_jdbc/connection.clj+++ b/src/metabase/driver/sql_jdbc/connection.clj@@ -1,7 +1,7 @@(ns metabase.driver.sql-jdbc.connection"Logic for creating and managing connection pools for SQL JDBC drivers. Implementations for connection-related drivermultimethods for SQL JDBC drivers."- (:refer-clojure :exclude [get-in select-keys])+ (:refer-clojure :exclude [get-in mapv select-keys])(:require[clojure.java.jdbc :as jdbc][metabase.driver :as driver]@@ -15,7 +15,7 @@[metabase.util.i18n :refer [tru]][metabase.util.log :as log][metabase.util.malli :as mu]- [metabase.util.performance :refer [get-in select-keys]]+ [metabase.util.performance :refer [get-in mapv select-keys]][potemkin :as p]^{:clj-kondo/ignore [:discouraged-namespace]}[toucan2.core :as t2])@@ -290,7 +290,8 @@them and removing from cache."[database](let [db-id (u/the-id database)- canonical-keys [[db-id :default] [db-id :write-data]]+ canonical-keys (mapv (fn [conn-type] [db-id conn-type])+ driver.conn/connection-types)pool-map @pool-cache-key->connection-poolcanonical-count (count (filter pool-map canonical-keys));; Find all swapped pool keys for this database (keys are [db-id, details-hash] tuples)@@ -412,7 +413,7 @@;; the hash didn't match, but it's possible that a stale instance of `DatabaseInstance`;; was passed in (ex: from a long-running sync operation); fetch the latest one from;; our app DB, and see if it STILL doesn't match- (not= curr-hash (-> (t2/select-one [:model/Database :id :engine :details :write_data_details] :id database-id)+ (not= curr-hash (-> (t2/select-one [:model/Database :id :engine :details :write_data_details :admin_details] :id database-id)jdbc-spec-hash)))))(defn- get-canonical-pooldiff --git a/src/metabase/driver/sql_jdbc/sync/describe_table.clj b/src/metabase/driver/sql_jdbc/sync/describe_table.cljindex 12b4dd89f2a..bf44df1e76a 100644--- a/src/metabase/driver/sql_jdbc/sync/describe_table.clj+++ b/src/metabase/driver/sql_jdbc/sync/describe_table.clj@@ -117,10 +117,8 @@(range 1 (inc (.getColumnCount metadata))))))(catch Exception e(if (driver/table-known-to-not-exist? driver e)- ;; if the table does not exist, we just warn and ignore it, rather than failing with an exception- (do- (log/warnf e "Cannot sync Table %s: does not exist" table-name)- init)+ ;; if the table does not exist, we just ignore it, rather than failing with an exception+ init(throw e))))))))(defn- jdbc-fields-metadatadiff --git a/src/metabase/driver/sync.clj b/src/metabase/driver/sync.cljindex d2ef36e2386..020968f4d7e 100644--- a/src/metabase/driver/sync.clj+++ b/src/metabase/driver/sync.clj@@ -3,7 +3,8 @@(:require[clojure.string :as str][metabase.driver.connection :as driver.conn]- [metabase.driver.util :as driver.u])+ [metabase.driver.util :as driver.u]+ [metabase.workspaces.core :as workspaces])(:import(java.util.regex Pattern)))@@ -74,11 +75,20 @@(defn include-schema?"Returns true if the given `schema-name` should be included/synced, considering the given `inclusion-patterns` and`exclusion-patterns` (either provided explicitly or taken from the driver's connection properties). Patterns are- comma-separated, and can contain wildcard characters (`*`)."+ comma-separated, and can contain wildcard characters (`*`).++ Workspace isolation schemas (`mb__isolation_*`) are excluded on **parent (stats) instances** -- they are+ local-side state created by workspace provisioning and must not bleed into stats's view of the warehouse.+ On **child (workspace-mode) instances** they are NOT excluded -- those are the schemas the local instance+ needs to sync (with table remapping) to surface its workspace's data. The gate is+ `(metabase.workspaces.core/workspace-mode?)`: false on parent (OSS fallback) or any EE instance that+ didn't load a `:workspace` section, true on child instances that did. See GHY-3489 for context."{:added "0.42.0"}([database schema-name](let [[inclusion-patterns exclusion-patterns] (db-details->schema-filter-patterns database)](include-schema? inclusion-patterns exclusion-patterns schema-name)))([inclusion-patterns exclusion-patterns schema-name]- (let [filter-fn (schema-patterns->filter-fn inclusion-patterns exclusion-patterns)]- (filter-fn schema-name))))+ (and (or (workspaces/workspace-mode?)+ (not (driver.u/workspace-isolated-schema? schema-name)))+ (let [filter-fn (schema-patterns->filter-fn inclusion-patterns exclusion-patterns)]+ (filter-fn schema-name)))))diff --git a/src/metabase/driver/util.clj b/src/metabase/driver/util.cljindex 1221efa86bb..00e656e4b58 100644--- a/src/metabase/driver/util.clj+++ b/src/metabase/driver/util.clj@@ -730,21 +730,34 @@(into default-sensitive-fields (map (comp keyword :name) password-fields)))default-sensitive-fields))-(defn fields-hidden-for-write-data-connection- "Returns the set of field names (strings) that should NOT appear in `write_data_details` for the given `driver`.- These are fields whose resolved `visible-if` includes `\"write-data-connection\" false`, meaning they are hidden- when the write-data-connection form marker is true."- [driver]+(defn- fields-hidden-by-form-marker+ "Returns the set of field names (strings) whose resolved `visible-if` includes `marker false`,+ meaning they are hidden when the named form marker is set on the connection-edit form."+ [driver marker](when-some [conn-prop-fn (get-method driver/connection-properties driver)](let [all-props (conn-prop-fn driver)resolved (connection-props-server->client driver all-props)props-by-name (collect-all-props-by-name resolved)](into #{}(keep (fn [[field-name {:keys [visible-if]}]]- (when (false? (get visible-if "write-data-connection"))+ (when (false? (get visible-if marker))field-name)))props-by-name))))+(defn fields-hidden-for-write-data-connection+ "Returns the set of field names (strings) that should NOT appear in `write_data_details` for the given `driver`.+ These are fields whose resolved `visible-if` includes `\"write-data-connection\" false`, meaning they are hidden+ when the write-data-connection form marker is true."+ [driver]+ (fields-hidden-by-form-marker driver "write-data-connection"))++(defn fields-hidden-for-admin-connection+ "Returns the set of field names (strings) that should NOT appear in `admin_details` for the given `driver`.+ These are fields whose resolved `visible-if` includes `\"admin-connection\" false`, meaning they are hidden+ when the admin-connection form marker is true."+ [driver]+ (fields-hidden-by-form-marker driver "admin-connection"))+(defn fetch-and-incorporate-auth-provider-details"Incorporates auth-provider responses with db-details.diff --git a/src/metabase/lib/metadata.cljc b/src/metabase/lib/metadata.cljcindex c8e7117cd0d..0a31ea61368 100644--- a/src/metabase/lib/metadata.cljc+++ b/src/metabase/lib/metadata.cljc@@ -4,6 +4,7 @@[medley.core :as m][metabase.lib.metadata.cache :as lib.metadata.cache][metabase.lib.metadata.protocols :as lib.metadata.protocols]+ [metabase.lib.metadata.transforming-provider :as lib.metadata.transforming-provider][metabase.lib.metadata.util :as lib.metadata.util][metabase.lib.schema :as lib.schema][metabase.lib.schema.id :as lib.schema.id]@@ -28,7 +29,9 @@(shared.ns/import-fns[lib.metadata.util- ->metadata-provider])+ ->metadata-provider]+ [lib.metadata.transforming-provider+ transforming-metadata-provider])(mu/defn database :- ::lib.schema.metadata/database"Get metadata about the Database we're querying."diff --git a/src/metabase/lib/metadata/transforming_provider.cljc b/src/metabase/lib/metadata/transforming_provider.cljcnew file mode 100644index 00000000000..3542ed14d56--- /dev/null+++ b/src/metabase/lib/metadata/transforming_provider.cljc@@ -0,0 +1,83 @@+(ns metabase.lib.metadata.transforming-provider+ "A metadata provider that transforms results from a parent provider by applying `f` to every+ `metadatas` and `cached-metadatas` call.++ What it is+ ----------+ A read-side wrapper. Delegates the whole `CachedMetadataProvider` surface to the parent,+ intercepting only the read paths to apply `f` on the way out. The parent's atom is never+ mutated by the wrapper; the same parent can be wrapped, unwrapped, and read directly with+ no risk of pollution.++ What it solves+ --------------+ The wrapped parent may already hold canonical metadata from prior reads. Without+ interception every read through the wrapper would return canonical, defeating the+ purpose of the transform. Intercepting `metadatas` AND `cached-metadatas` ensures every+ public read path (`lib.metadata/table`, `field`, `tables`, `lib.metadata/cached-metadata`,+ `cached-metadatas`) returns the transformed shape.++ Why it works+ ------------+ - `metadatas` applies `f` on the way out. Parent serves canonical from its cache;+ wrapper transforms; caller sees transformed.+ - `cached-metadatas` does the same. Without this, callers reaching the parent's cache+ directly via the `CachedMetadataProvider` API would bypass `f`.+ - `store-metadata!`, `cache-value!`, `cached-value`, `clear-cache!`, `has-cache?` all+ delegate to the parent. The wrapper has no opinion about non-read operations; whatever+ the parent does is what happens.++ `f` must be pure."+ (:require+ #?@(:clj ([potemkin :as p]+ [pretty.core :as pretty]))+ [metabase.lib.metadata.protocols :as lib.metadata.protocols]))++(#?(:clj p/deftype+ :cljs deftype) TransformingMetadataProvider [f parent-metadata-provider]+ lib.metadata.protocols/MetadataProvider+ (database [_this]+ (lib.metadata.protocols/database parent-metadata-provider))+ (metadatas [_this metadata-spec]+ (f metadata-spec (lib.metadata.protocols/metadatas parent-metadata-provider metadata-spec)))+ (setting [_this setting-key]+ (lib.metadata.protocols/setting parent-metadata-provider setting-key))++ lib.metadata.protocols/CachedMetadataProvider+ (cached-metadatas [_this metadata-type metadata-ids]+ (f {:lib/type metadata-type}+ (lib.metadata.protocols/cached-metadatas parent-metadata-provider metadata-type metadata-ids)))+ (store-metadata! [_this metadata]+ (lib.metadata.protocols/store-metadata! parent-metadata-provider metadata))+ (cached-value [_this k not-found]+ (lib.metadata.protocols/cached-value parent-metadata-provider k not-found))+ (cache-value! [_this k v]+ (lib.metadata.protocols/cache-value! parent-metadata-provider k v))+ (has-cache? [_this]+ (lib.metadata.protocols/has-cache? parent-metadata-provider))+ (clear-cache! [_this]+ (lib.metadata.protocols/clear-cache! parent-metadata-provider))++ lib.metadata.protocols/InvocationTracker+ (invoked-ids [_this metadata-type]+ (when (satisfies? lib.metadata.protocols/InvocationTracker parent-metadata-provider)+ (lib.metadata.protocols/invoked-ids parent-metadata-provider metadata-type)))++ #?(:clj Object :cljs IEquiv)+ (#?(:clj equals :cljs -equiv) [_this another]+ (and (instance? TransformingMetadataProvider another)+ (= f (.-f ^TransformingMetadataProvider another))+ (= parent-metadata-provider (.-parent-metadata-provider ^TransformingMetadataProvider another))))++ #?@(:clj+ [pretty/PrettyPrintable+ (pretty [_this]+ (list `transforming-metadata-provider f parent-metadata-provider))]))++(defn transforming-metadata-provider+ "Wrap `parent-metadata-provider` with a transform function `f`. `f` receives+ `(f metadata-spec results)` for every `metadatas` and `cached-metadatas` call on the+ wrapper, and returns the (possibly transformed) results.++ `f` must be pure."+ [f parent-metadata-provider]+ (->TransformingMetadataProvider f parent-metadata-provider))diff --git a/src/metabase/lib/schema/metadata.cljc b/src/metabase/lib/schema/metadata.cljcindex 7769eb4da76..ad99f98ce5e 100644--- a/src/metabase/lib/schema/metadata.cljc+++ b/src/metabase/lib/schema/metadata.cljc@@ -766,7 +766,12 @@[:id ::lib.schema.id/table][:name ::lib.schema.common/non-blank-string][:display-name {:optional true} [:maybe ::lib.schema.common/non-blank-string]]- [:schema {:optional true} [:maybe ::lib.schema.common/non-blank-string]]])+ [:schema {:optional true} [:maybe ::lib.schema.common/non-blank-string]]+ ;; Optional `:db` AST slot for cross-DB references (BigQuery `project.dataset.table`,+ ;; SQL Server / Snowflake `db.schema.table`, MySQL workspace-remapped tables routing+ ;; to a different database). Sync doesn't populate it on standard reads — only+ ;; workspace remap and other cross-DB rewriters fill it.+ [:db {:optional true} [:maybe ::lib.schema.common/non-blank-string]]])(mr/def ::database"Malli schema for the DatabaseMetadata as returned by `GET /api/database/:id/metadata` -- what should be available todiff --git a/src/metabase/lib_be/metadata/jvm.clj b/src/metabase/lib_be/metadata/jvm.cljindex 018d365533c..cb908d86137 100644--- a/src/metabase/lib_be/metadata/jvm.clj+++ b/src/metabase/lib_be/metadata/jvm.clj@@ -80,7 +80,7 @@#_resolved-query clojure.lang.IPersistentMap][query-type model parsed-args honeysql](merge (next-method query-type model parsed-args honeysql)- {:select [:id :engine :name :dbms_version :settings :is_audit :details :write_data_details :timezone :router_database_id]}))+ {:select [:id :engine :name :dbms_version :settings :is_audit :details :write_data_details :admin_details :timezone :router_database_id]}))(t2/define-after-select :metadata/database[database]diff --git a/src/metabase/model_persistence/api.clj b/src/metabase/model_persistence/api.cljindex 9e17f4cae29..aeb17978c44 100644--- a/src/metabase/model_persistence/api.clj+++ b/src/metabase/model_persistence/api.clj@@ -22,6 +22,7 @@[metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.schema :as ms]+ [metabase.workspaces.core :as workspaces][toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -150,6 +151,7 @@(api.macros/defendpoint :post "/enable""Enable global setting to allow databases to persist models."[]+ (workspaces/check-not-in-workspace-mode! "Model persistence")(perms/check-has-application-permission :setting)(log/info "Enabling model persistence")(model-persistence.settings/persisted-models-enabled! true)diff --git a/src/metabase/models/resolution.clj b/src/metabase/models/resolution.cljindex 2a0b3f69846..ad5d4d1f162 100644--- a/src/metabase/models/resolution.clj+++ b/src/metabase/models/resolution.clj@@ -114,6 +114,7 @@:model/Setting metabase.settings.models.setting:model/SupportAccessGrantLog metabase-enterprise.support-access-grants.models.support-access-grant-log:model/Table metabase.warehouse-schema.models.table+ :model/TableRemapping metabase-enterprise.workspaces.models.table-remapping:model/TaskHistory metabase.task-history.models.task-history:model/TaskRun metabase.task-history.models.task-run:model/Tenant metabase-enterprise.tenants.models@@ -131,7 +132,9 @@:model/User metabase.users.models.user:model/UserKeyValue metabase.user-key-value.models.user-key-value:model/UserParameterValue metabase.users.models.user-parameter-value- :model/ViewLog metabase.view-log.models.view-log})+ :model/ViewLog metabase.view-log.models.view-log+ :model/Workspace metabase-enterprise.workspaces.models.workspace+ :model/WorkspaceDatabase metabase-enterprise.workspaces.models.workspace-database});;; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!;;; !! !!diff --git a/src/metabase/permissions/core.clj b/src/metabase/permissions/core.cljindex 1b770465944..ed98bc2314f 100644--- a/src/metabase/permissions/core.clj+++ b/src/metabase/permissions/core.clj@@ -67,6 +67,8 @@with-relevant-permissions-for-userhas-any-transforms-permission?has-db-transforms-permission?+ has-any-workspaces-permission?+ has-db-workspaces-permission?set-default-group-permissions!set-default-database-permissions!set-default-table-permissions!diff --git a/src/metabase/permissions/models/data_permissions.clj b/src/metabase/permissions/models/data_permissions.cljindex f1ef476939b..15fb951d1b9 100644--- a/src/metabase/permissions/models/data_permissions.clj+++ b/src/metabase/permissions/models/data_permissions.clj@@ -711,10 +711,12 @@(build-database-permission perms group-or-id db-or-id :perms/download-results :no)])(and (= perm-type :perms/view-data) (not= value :unrestricted))- (conj (build-database-permission perms group-or-id db-or-id :perms/transforms :no))+ (into [(build-database-permission perms group-or-id db-or-id :perms/transforms :no)+ (build-database-permission perms group-or-id db-or-id :perms/workspaces :no)])(and (= perm-type :perms/create-queries) (not= value :query-builder-and-native))- (conj (build-database-permission perms group-or-id db-or-id :perms/transforms :no)))]+ (into [(build-database-permission perms group-or-id db-or-id :perms/transforms :no)+ (build-database-permission perms group-or-id db-or-id :perms/workspaces :no)]))](apply merge-with concat{:to-delete existing-perms:to-insert [new-perm]}@@ -1040,7 +1042,8 @@:perms/manage-database :no}(or (not= view-data-level :unrestricted)(not= cq-level :query-builder-and-native))- (assoc :perms/transforms :no))]+ (assoc :perms/transforms :no+ :perms/workspaces :no))][perm-type perm-value] perm-map]{:perm_type perm-type:group_id group-id@@ -1091,7 +1094,8 @@:perms/download-results :one-million-rows:perms/manage-table-metadata :no:perms/manage-database :no- :perms/transforms :no}+ :perms/transforms :no+ :perms/workspaces :no};; Normal: compute based on group's lowest existing perm level:else@@ -1114,7 +1118,8 @@:perms/manage-database :no}(or (not= view-data-level :unrestricted)(not= cq-level :query-builder-and-native))- (assoc :perms/transforms :no))))]+ (assoc :perms/transforms :no+ :perms/workspaces :no))))](for [[perm-type perm-value] perm-map]{:perm_type perm-type:group_id group-id@@ -1249,3 +1254,17 @@"Returns true if the current user has the transforms permission for _any_ source db."[user-id](user-has-any-perms-of-type? user-id :perms/transforms))++(defn has-db-workspaces-permission?+ "Returns true if the given user has the workspaces permission for the given database."+ [user-id database-id]+ (and (not= database-id audit/audit-db-id)+ (user-has-permission-for-database? user-id+ :perms/workspaces+ :yes+ database-id)))++(defn has-any-workspaces-permission?+ "Returns true if the given user has the workspaces permission for _any_ source db."+ [user-id]+ (user-has-any-perms-of-type? user-id :perms/workspaces))diff --git a/src/metabase/permissions/schema.clj b/src/metabase/permissions/schema.cljindex 4cf7280d201..0afb7685e26 100644--- a/src/metabase/permissions/schema.clj+++ b/src/metabase/permissions/schema.clj@@ -21,7 +21,8 @@:perms/download-results {:model :model/Table, :values [:one-million-rows :ten-thousand-rows :no]}:perms/manage-table-metadata {:model :model/Table, :values [:yes :no]}:perms/manage-database {:model :model/Database, :values [:yes :no]}- :perms/transforms {:model :model/Database, :values [:yes :no]}})+ :perms/transforms {:model :model/Database, :values [:yes :no]}+ :perms/workspaces {:model :model/Database, :values [:yes :no]}})(mr/def ::data-permission-type"Malli spec for valid permission types."diff --git a/src/metabase/permissions_rest/data_permissions/graph.clj b/src/metabase/permissions_rest/data_permissions/graph.cljindex 46b6108af41..462b4244b68 100644--- a/src/metabase/permissions_rest/data_permissions/graph.clj+++ b/src/metabase/permissions_rest/data_permissions/graph.clj@@ -27,7 +27,8 @@:perms/download-results :download:perms/manage-table-metadata :data-model:perms/manage-database :details- :perms/transforms :transforms})+ :perms/transforms :transforms+ :perms/workspaces :workspaces})(def ^:private ->api-vals{:perms/view-data {:unrestricted :unrestricted@@ -41,7 +42,8 @@:no nil}:perms/manage-table-metadata {:yes :all :no nil}:perms/manage-database {:yes :yes :no :no}- :perms/transforms {:yes :yes :no :no}})+ :perms/transforms {:yes :yes :no :no}+ :perms/workspaces {:yes :yes :no :no}})(defenterprise add-impersonations-to-permissions-graph"Augment the permissions graph with active connection impersonation policies. OSS implementation returns graph as-is."@@ -121,7 +123,8 @@:download {:schemas :full}:data-model {:schemas :all}:details :yes- :transforms :yes})+ :transforms :yes+ :workspaces :yes})(def ^:private data-analyst-perms"Data Analysts have implicit manage-table-metadata permission for all databases."@@ -312,7 +315,8 @@:data-model {:all :yes:none :no}:details {:yes :yes :no :no}- :transforms {:yes :yes :no :no}})+ :transforms {:yes :yes :no :no}+ :workspaces {:yes :yes :no :no}})(def ^:private api-key->perm-type{:view-data :perms/view-data@@ -320,7 +324,8 @@:download :perms/download-results:data-model :perms/manage-table-metadata:details :perms/manage-database- :transforms :perms/transforms})+ :transforms :perms/transforms+ :workspaces :perms/workspaces})(defn- resolve-api-value"Translates an API permission value for a single [group-id db-id api-key] into a map of@@ -361,12 +366,16 @@#(merge % {nil {:perm_value :no :schema_name nil}})))(and (= perm-type :perms/view-data) (not= db-value :unrestricted))- (update [group-id db-id :perms/transforms]- #(merge % {nil {:perm_value :no :schema_name nil}}))+ (-> (update [group-id db-id :perms/transforms]+ #(merge % {nil {:perm_value :no :schema_name nil}}))+ (update [group-id db-id :perms/workspaces]+ #(merge % {nil {:perm_value :no :schema_name nil}})))(and (= perm-type :perms/create-queries) (not= db-value :query-builder-and-native))- (update [group-id db-id :perms/transforms]- #(merge % {nil {:perm_value :no :schema_name nil}}))))+ (-> (update [group-id db-id :perms/transforms]+ #(merge % {nil {:perm_value :no :schema_name nil}}))+ (update [group-id db-id :perms/workspaces]+ #(merge % {nil {:perm_value :no :schema_name nil}})))))(defn- add-implications:table-level[desired group-id db-id perm-type table-entries]@@ -422,7 +431,7 @@{}(for [[group-id group-changes] graph[db-id db-changes] group-changes- api-key [:details :data-model :download :transforms :create-queries :view-data]+ api-key [:details :data-model :download :transforms :workspaces :create-queries :view-data]:let [api-value (get db-changes api-key)]:when api-value][group-id db-id api-key api-value])))diff --git a/src/metabase/permissions_rest/schema.clj b/src/metabase/permissions_rest/schema.cljindex 6c826d2af3b..40c3180b6b5 100644--- a/src/metabase/permissions_rest/schema.clj+++ b/src/metabase/permissions_rest/schema.clj@@ -97,7 +97,8 @@[:download {:optional true} [:ref ::strict-data-perms]][:data-model {:optional true} [:ref ::strict-data-perms]][:details {:optional true} [:enum :yes :no]]- [:transforms {:optional true} [:enum :yes :no]]]+ [:transforms {:optional true} [:enum :yes :no]]+ [:workspaces {:optional true} [:enum :yes :no]]][:fn {:error/fn (fn [_ _] (trs "Invalid DB permissions: If you have write access for native queries, you must have data access to all schemas."))}(fn [db-entry](let [{:keys [create-queries view-data]} db-entry]diff --git a/src/metabase/premium_features/core.clj b/src/metabase/premium_features/core.cljindex 90cc3cd00fa..d35fb150419 100644--- a/src/metabase/premium_features/core.clj+++ b/src/metabase/premium_features/core.clj@@ -87,6 +87,7 @@enable-basic-transforms?enable-python-transforms?enable-upload-management?+ enable-workspaces?enable-whitelabeling?enable-writable-connection?has-attached-dwh?diff --git a/src/metabase/premium_features/settings.clj b/src/metabase/premium_features/settings.cljindex a3ce53288f9..b697c8a8b1a 100644--- a/src/metabase/premium_features/settings.clj+++ b/src/metabase/premium_features/settings.clj@@ -363,6 +363,10 @@"Should we enable AI controls (metabot permissions, scope management)?":ai-controls)+(define-premium-feature enable-workspaces?+ "Should we allow users to manage workspaces?"+ :workspaces)+(defn- -token-features []{:admin_security_center (security-center-enabled?):advanced_permissions (enable-advanced-permissions?)@@ -416,6 +420,7 @@:transforms-basic (enable-basic-transforms?):transforms-python (enable-python-transforms?):upload_management (enable-upload-management?)+ :workspaces (enable-workspaces?):whitelabel (enable-whitelabeling?):writable_connection (enable-writable-connection?):ai_controls (enable-ai-controls?)})diff --git a/src/metabase/query_processor/api.clj b/src/metabase/query_processor/api.cljindex 52106e9faa5..5c035ad5425 100644--- a/src/metabase/query_processor/api.clj+++ b/src/metabase/query_processor/api.clj@@ -34,6 +34,7 @@[metabase.util.malli :as mu]^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.util.malli.schema :as ms][metabase.util.performance :refer [not-empty get-in]]+ [metabase.workspaces.table-remapping :as ws.table-remapping][steffan-westcott.clj-otel.api.trace.span :as span]^{:clj-kondo/ignore [:discouraged-namespace]} [toucan2.core :as t2]))@@ -192,19 +193,26 @@;;#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}(api.macros/defendpoint :post "/native"- "Fetch a native version of an MBQL query."+ "Fetch a native version of an MBQL query.++ Display path: workspace remapping is suppressed via+ [[ws.table-remapping/with-display-context]] so the user sees canonical-schema SQL+ in the 'Show me the SQL' panel. The query still executes against the workspace+ isolation schema at warehouse time (separate code path); this endpoint only+ affects what the user reads."[_route-params_query-params{:keys [database pretty] :as query} :- [:map[:database ms/PositiveInt][:pretty {:default true} [:maybe :boolean]]]]- (model-persistence/with-persisted-substituion-disabled- (qp.perms/check-current-user-has-adhoc-native-query-perms query)- (let [driver (driver.u/database->driver database)- prettify (partial driver/prettify-native-form driver)- compiled (qp.compile/compile-with-inline-parameters query)]- (cond-> compiled- pretty (update :query prettify)))))+ (ws.table-remapping/with-display-context+ (model-persistence/with-persisted-substituion-disabled+ (qp.perms/check-current-user-has-adhoc-native-query-perms query)+ (let [driver (driver.u/database->driver database)+ prettify (partial driver/prettify-native-form driver)+ compiled (qp.compile/compile-with-inline-parameters query)]+ (cond-> compiled+ pretty (update :query prettify))))))(api.macros/defendpoint :post "/pivot":- (server/streaming-response-schema ::qp.schema/query-result)diff --git a/src/metabase/query_processor/execute.clj b/src/metabase/query_processor/execute.cljindex 08ccc9b7caa..817152a4870 100644--- a/src/metabase/query_processor/execute.clj+++ b/src/metabase/query_processor/execute.clj@@ -53,6 +53,7 @@;; to execute INSIDE the `binding` blocks established by the EE postprocessing middlewares below. See;; [[metabase.query-processor.middleware.process-userland-query/capture-execution-context-middleware]] for why.[#'qp.process-userland-query/capture-execution-context-middleware+ #'qp.middleware.enterprise/apply-workspace-sql-remapping-middleware#'qp.middleware.enterprise/swap-destination-db-middleware#'qp.middleware.enterprise/apply-impersonation-postprocessing-middleware#'update-used-cards/update-used-cards!diff --git a/src/metabase/query_processor/middleware/enterprise.clj b/src/metabase/query_processor/middleware/enterprise.cljindex f2cd7617b5c..1fe69e8820b 100644--- a/src/metabase/query_processor/middleware/enterprise.clj+++ b/src/metabase/query_processor/middleware/enterprise.clj@@ -78,13 +78,40 @@query)(defenterprise apply-workspace-remapping- "Pre-processing middleware to rewrite 'global' table references with 'isolated' tables in the workspace schema."+ "**Workspace remapping, Phase 1 — preprocess.** Mutates cached MBQL table metadata so+ downstream middleware and HoneySQL compilation see workspace identifiers.++ Phase 1 is for pipeline coherence; Phase 2 ([[apply-workspace-sql-remapping]]) is the+ authoritative security boundary. Native SQL is intentionally untouched here. See the EE+ impl namespace docstring for the full design rationale."metabase-enterprise.workspaces.query-processor.middleware[query]query);;;; Execution middleware+(defenterprise apply-workspace-sql-remapping+ "**Workspace remapping, Phase 2 — execute (post-compilation).** Authoritative SQL rewriter+ and the security boundary for workspace isolation.++ Runs after all preprocess work — snippets, card refs, params, and MBQL compilation are+ complete — so the query is reduced to one canonical SQL string. Parses it, rewrites every+ `from` table ref to its `to` counterpart, re-emits.++ **Fails closed:** on parse failure throws `ex-info` with `:type qp.error-type/qp`. There+ is no fallback to the original SQL — a silent pass-through would breach workspace+ isolation."+ metabase-enterprise.workspaces.query-processor.middleware+ [qp]+ qp)++(defn apply-workspace-sql-remapping-middleware+ "Helper middleware wrapper for [[apply-workspace-sql-remapping]] to make sure we do [[defenterprise]] dispatch+ correctly on each QP run rather than just once when we combine all of the QP middleware."+ [qp]+ (fn [query rff]+ ((apply-workspace-sql-remapping qp) query rff)))+;;; (f qp) => qp(defenterprise swap-destination-dbdiff --git a/src/metabase/secrets/models/secret.clj b/src/metabase/secrets/models/secret.cljindex 790d11c6e66..81f582578ed 100644--- a/src/metabase/secrets/models/secret.clj+++ b/src/metabase/secrets/models/secret.clj@@ -277,18 +277,19 @@(defn delete-orphaned-secrets!"Delete Secret instances from the app DB, that will become orphaned when `database` is deleted. For now, this will- simply delete any Secret whose ID appears in the details blobs (both `:details` and `:write_data_details`), since- every Secret instance that is currently created is exclusively associated with a single Database.+ simply delete any Secret whose ID appears in the details blobs (`:details`, `:write_data_details`, and+ `:admin_details`), since every Secret instance that is currently created is exclusively associated with a single+ Database.In the future, if/when we allow arbitrary association of secret instances to database instances, this will need tochange and become more complicated (likely by consulting a many-to-many join table)."- [{:keys [id details write_data_details] :as database}]+ [{:keys [id details write_data_details admin_details] :as database}](when-let [possible-secret-prop-names (seq (keys (secret-conn-props-by-name (driver.u/database->driver database))))](doseq [secret-id (reduce (fn [acc prop-name](let [id-kw (->id-kw prop-name)](into acc(keep #(get % id-kw))- [details write_data_details])))+ [details write_data_details admin_details])))#{}possible-secret-prop-names)](log/infof "Deleting secret ID %s from app DB because the owning database (%s) is being deleted" secret-id id)@@ -327,7 +328,7 @@This is a transformation on `:model/Database` `to-json`Fetches the stored secret and fills in `-path` `-options` `-value` for each secret property.- Operates on both `:details` and `:write_data_details`."+ Operates on `:details`, `:write_data_details`, and `:admin_details`."[database](let [driver (driver.u/database->driver database)hydrate (fn [details]@@ -335,7 +336,8 @@;; Very low-level operation here, so not using driver.conn/* utils:(-> database(m/update-existing :details hydrate)- (m/update-existing :write_data_details hydrate))))+ (m/update-existing :write_data_details hydrate)+ (m/update-existing :admin_details hydrate))))(defn clean-secret-properties-from-details"Ensures that all possible secret property values are removed from `:details`.@@ -350,14 +352,15 @@(apply dissoc db-details (vals (->possible-secret-property-names conn-prop-nm))))))(defn clean-secret-properties-from-database- "Ensures that all possible secret property values are removed from `:details` and `:write_data_details`.- This is a transformation on `:model/Database` `results-transform`."+ "Ensures that all possible secret property values are removed from `:details`, `:write_data_details`, and+ `:admin_details`. This is a transformation on `:model/Database` `results-transform`."[database](let [clean #(clean-secret-properties-from-details % (driver.u/database->driver database))];; Very low-level operation here, so not using driver.conn/* utils:(-> database(m/update-existing :details clean)- (m/update-existing :write_data_details clean))))+ (m/update-existing :write_data_details clean)+ (m/update-existing :admin_details clean))))(defn- handle-secrets-for-details-key"Process secret-type connection properties in `details-key` of `database`, converting raw secret@@ -392,7 +395,7 @@database))(defn handle-incoming-client-secrets!- "Converts incoming secret values in `:details` and `:write_data_details` into Secrets.+ "Converts incoming secret values in `:details`, `:write_data_details`, and `:admin_details` into Secrets.This is a transformation on `:model/Database` `before-insert` and `before-update`.Only the Secret id should be stored in the details maps. All other secret props should be cleared.@@ -412,4 +415,5 @@[database](-> database(handle-secrets-for-details-key :details)- (handle-secrets-for-details-key :write_data_details)))+ (handle-secrets-for-details-key :write_data_details)+ (handle-secrets-for-details-key :admin_details)))diff --git a/src/metabase/sql_tools/core.clj b/src/metabase/sql_tools/core.cljindex 25d4173a7b2..642e6a7c0b6 100644--- a/src/metabase/sql_tools/core.clj+++ b/src/metabase/sql_tools/core.clj@@ -20,25 +20,29 @@;;; -------------------------------------------------- Schemas --------------------------------------------------(mr/def ::table-spec- "Schema for table identifier used in replacements."+ "Schema for table identifier used in replacements. `:db` is the top-level qualifier for+ drivers that emit one (BigQuery project, ClickHouse database)."[:map[:table :string]- [:schema {:optional true} [:maybe :string]]])+ [:schema {:optional true} [:maybe :string]]+ [:db {:optional true} [:maybe :string]]])(mr/def ::column-spec"Schema for column identifier used in replacements."[:map[:column :string][:table {:optional true} [:maybe :string]]- [:schema {:optional true} [:maybe :string]]])+ [:schema {:optional true} [:maybe :string]]+ [:db {:optional true} [:maybe :string]]])(mr/def ::table-replacement-value- "Schema for table replacement target - can be a string or a map with schema/table."+ "Schema for table replacement target - can be a string or a map with schema/table/db."[:or:string[:map[:schema {:optional true} [:maybe :string]]- [:table {:optional true} [:maybe :string]]]])+ [:table {:optional true} [:maybe :string]]+ [:db {:optional true} [:maybe :string]]]])(mr/def ::replacements"Schema for replace-names replacements map."diff --git a/src/metabase/sync/fetch_metadata.clj b/src/metabase/sync/fetch_metadata.cljindex 09f7940087a..4cca3d55b4d 100644--- a/src/metabase/sync/fetch_metadata.clj+++ b/src/metabase/sync/fetch_metadata.clj@@ -5,14 +5,44 @@(:require[clojure.set :as set][metabase.driver :as driver]+ [metabase.driver.connection :as driver.conn][metabase.driver.util :as driver.u][metabase.sync.interface :as i][metabase.sync.util :as sync-util][metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.fn :as mu.fn]+ [metabase.workspaces.table-remapping :as ws.table-remapping][toucan2.core :as t2]))+(defn- effective-table-spec+ "Resolves the workspace remap for `(schema, table-name)` on `database-id`. Returns+ `{:db :schema :name}`: the iso warehouse coordinates when a remap exists, otherwise+ the canonical input. Lets workspace mode redirect to the iso table while the app-db+ row keeps its logical identity."+ [database-id schema table-name]+ (let [from-spec {:schema schema :name table-name}]+ (or (ws.table-remapping/workspace-remap-schema+name database-id from-spec)+ {:db nil :schema schema :name table-name})))++(defn- do-with-effective-table+ "Resolve the workspace remap for `table` on `database`, then call `f` with an+ `{:db :schema :name}` map of effective coordinates.++ When the resolved `:db` differs from the canonical bound DB (true for engines like+ MySQL whose iso namespace is a *different* database, not just a different schema),+ installs a `:db` swap via [[driver.conn/with-swapped-connection-details]] so the+ driver-side `describe-fields`/`describe-table`/`describe-indexes` SQL filters and+ connects to the iso DB. Schema-having drivers (Postgres etc.) keep `:db` at the+ canonical value and skip the swap."+ [database table f]+ (let [{effective-db :db :as spec} (effective-table-spec (:id database) (:schema table) (:name table))+ canonical-db (-> database :details :db)]+ (if (and effective-db (not= effective-db canonical-db))+ (driver.conn/with-swapped-connection-details (:id database) {:db effective-db}+ (f spec))+ (f spec))))+(defmacro log-if-error"Logs an error message if an exception is thrown while executing the body."{:style/indent 1}@@ -34,9 +64,13 @@"Add nested-field-columns for table to set of fields."[fields database table](let [driver (driver.u/database->driver database)]- (cond-> fields- (driver.u/supports? driver :nested-field-columns database)- (set/union ((requiring-resolve 'metabase.driver.sql-jdbc.sync/describe-nested-field-columns) driver database table)))))+ (do-with-effective-table+ database table+ (fn [{effective-schema :schema effective-name :name}]+ (let [effective-table (assoc table :schema effective-schema :name effective-name)]+ (cond-> fields+ (driver.u/supports? driver :nested-field-columns database)+ (set/union ((requiring-resolve 'metabase.driver.sql-jdbc.sync/describe-nested-field-columns) driver database effective-table))))))))(mu/defn table-fields-metadata :- [:set i/TableMetadataField]"Fetch metadata about Fields belonging to a given `table` directly from an external database by calling its driver's@@ -45,14 +79,17 @@[database :- i/DatabaseInstancetable :- i/TableInstance](log-if-error "table-fields-metadata"- (let [driver (driver.u/database->driver database)- result (if (driver.u/supports? driver :describe-fields database)- (set (driver/describe-fields driver- database- :table-names [(:name table)]- :schema-names [(:schema table)]))- (:fields (driver/describe-table driver database table)))]- result)))+ (let [driver (driver.u/database->driver database)]+ (do-with-effective-table+ database table+ (fn [{effective-schema :schema effective-name :name}]+ (let [effective-table (assoc table :schema effective-schema :name effective-name)]+ (if (driver.u/supports? driver :describe-fields database)+ (set (driver/describe-fields driver+ database+ :table-names [effective-name]+ :schema-names [effective-schema]))+ (:fields (driver/describe-table driver database effective-table)))))))))(defn- describe-fields-using-describe-table"Replaces [[metabase.driver/describe-fields]] for drivers that haven't implemented it. Uses [[driver/describe-table]]@@ -113,24 +150,50 @@(mu/defn fk-metadata"Effectively a wrapper for [[metabase.driver/describe-fks]] that also validates the output against the schema.If the driver doesn't support [[metabase.driver/describe-fks]] it uses [[driver/describe-table-fks]] instead.- This will be deprecated in "+ This will be deprecated in++ In workspace mode, expands `:schema-names` to include workspace-isolation schemas so+ the warehouse driver finds FKs on the physical tables that back canonical Tables, then+ back-translates workspace-side identifiers in the result so the rows match canonical+ Table rows in app-db."[database :- i/DatabaseInstance & {:as args}](log-if-error "fk-metadata"- (let [driver (driver.u/database->driver database)]+ (let [driver (driver.u/database->driver database)+ db-id (:id database)+ expanded-args (cond-> args+ (:schema-names args)+ (update :schema-names ws.table-remapping/expand-schema-names-with-workspace db-id))](when (driver.u/supports? driver :metadata/key-constraints database)(let [describe-fks-fn (if (driver.u/supports? driver :describe-fks database)driver/describe-fks;; In version 52 we'll remove [[driver/describe-table-fks]];; and we'll just use [[driver/describe-fks]] here- describe-fks-using-describe-table-fks)]- (cond->> (describe-fks-fn driver database args)+ describe-fks-using-describe-table-fks)+ ;; Workspace cross-DB swaps (today: MySQL whose iso table lives in a+ ;; *different* bound database than the canonical) need describe-fks to+ ;; run with the JDBC connection pointed at the iso DB. Loop per iso-DB+ ;; via the workspace hook and concatenate row results. OSS fallback+ ;; calls the thunk once with no swap, preserving today's behavior.+ row-batches (ws.table-remapping/call-with-fk-probe-iso-dbs+ db-id+ (fn []+ (vec (describe-fks-fn driver database expanded-args))))+ raw-rows (vec (mapcat identity row-batches))+ rewritten-rows (ws.table-remapping/rewrite-fk-result-canonical raw-rows db-id)]+ (cond->> rewritten-rows;; This is a workaround for the fact that [[mu/defn]] can't check reducible collections yet(mu.fn/instrument-ns? *ns*)(eduction (map #(mu.fn/validate-output {} i/FKMetadataEntry %)))))))))(mu/defn index-metadata :- [:maybe i/TableIndexMetadata]- "Get information about the indexes belonging to `table`."+ "Get information about the indexes belonging to `table`. In workspace mode,+ redirects to the isolated warehouse table so describe-table-indexes asks the+ warehouse about the physical table that backs this canonical Table row."[database :- i/DatabaseInstancetable :- i/TableInstance](log-if-error "index-metadata"- (driver/describe-table-indexes (driver.u/database->driver database) database table)))+ (do-with-effective-table+ database table+ (fn [{effective-schema :schema effective-name :name}]+ (let [effective-table (assoc table :schema effective-schema :name effective-name)]+ (driver/describe-table-indexes (driver.u/database->driver database) database effective-table))))))diff --git a/src/metabase/sync/sync_metadata/tables.clj b/src/metabase/sync/sync_metadata/tables.cljindex 7749993cd43..e0f8859669d 100644--- a/src/metabase/sync/sync_metadata/tables.clj+++ b/src/metabase/sync/sync_metadata/tables.clj@@ -20,6 +20,7 @@[metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.schema :as ms]+ [metabase.workspaces.table-remapping :as ws.table-remapping][toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -364,6 +365,13 @@;; determine what's changed between what info we have and what's in the DB(let [driver (driver.u/database->driver database)db-table-metadatas (table-set db-metadata)+ ;; DEV-1898: workspace-isolated tables (`ws_*`) live on the warehouse but must not+ ;; become :model/Table rows. They back canonical Tables via remap, not their own identity.+ db-table-metadatas (ws.table-remapping/filter-workspace-side-tables db-table-metadatas (:id database))+ ;; Inject synthetic canonical-side tuples for any remap whose to-side is materialized.+ ;; Without this, the diff retires canonical Table rows whose physical backing only+ ;; exists at the workspace location (not at the canonical name on the warehouse).+ db-table-metadatas (ws.table-remapping/inject-workspace-canonical-tuples db-table-metadatas (:id database))name+schema #(select-keys % [:name :schema])name+schema->db-table (m/index-by name+schema db-table-metadatas)our-metadata (db->our-metadata database)diff --git a/src/metabase/testing_api/api.clj b/src/metabase/testing_api/api.cljindex 86ddae83b48..f31f6f38d7c 100644--- a/src/metabase/testing_api/api.clj+++ b/src/metabase/testing_api/api.clj@@ -25,6 +25,7 @@[metabase.util.json :as json][metabase.util.log :as log][metabase.util.malli.schema :as ms]+ [metabase.workspaces.core :as ws][toucan2.core :as t2])(:import(com.mchange.v2.c3p0 PoolBackedDataSource)@@ -253,6 +254,25 @@[](refresh-cache-configs!))+#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}+(api.macros/defendpoint :post "/workspace-instance"+ "Install a workspace config atom on this instance for E2E testing. The body+ is passed straight through to `metabase.workspaces.core/set-instance-workspace!`+ — same shape the `:workspace` section of `config.yml` resolves to at boot.+ Validation happens inside that fn against the `::ws/workspace-instance-config`+ schema. No-op on OSS."+ [_route-params _query-params body :- ::ws/workspace-instance-config]+ (ws/set-instance-workspace! body)+ {:ok true})++#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}+(api.macros/defendpoint :delete "/workspace-instance"+ "Clear the in-process workspace config atom installed by POST `/workspace-instance`.+ No-op on OSS."+ []+ (ws/clear-instance-workspace!)+ {:ok true})+(api.macros/defendpoint :post "/query" :- ::lib.schema/query"Creates a query from a test query spec."[_route-paramsdiff --git a/src/metabase/transforms/execute.clj b/src/metabase/transforms/execute.cljindex 990a3a0ab51..af31023932f 100644--- a/src/metabase/transforms/execute.clj+++ b/src/metabase/transforms/execute.clj@@ -1,9 +1,31 @@(ns metabase.transforms.execute(:require- [metabase.transforms.interface :as transforms.i]))+ [metabase.transforms-base.interface :as transforms-base.i]+ [metabase.transforms.interface :as transforms.i]+ [metabase.transforms.query-impl :as transforms.query-impl]))(set! *warn-on-reflection* true)+(defn- resolve-target+ "Apply the workspace target-rewrite hook before dispatch. On a workspaced child+ instance for `(:database target)`, the hook rewrites `:schema` to the workspace+ output schema and records a TableRemapping for the canonical (schema, name).+ Off-workspace and OSS: no-op pass-through.++ Lives at the dispatch wrapper so every transform type (`:query`, `:python`, and+ any future kind registered via `transforms.i/execute!`) inherits the gate. The+ hook must run before per-type execution begins, since callees consume+ `(:target transform)` to decide where to write.++ When `target-db-id` returns nil (no per-type impl, or transform shape doesn't+ identify a target DB), the hook is skipped — workspace rewriting requires a+ known DB id to look up `db-workspace-namespace`."+ [transform]+ (if-let [db-id (transforms-base.i/target-db-id transform)]+ (assoc transform :target+ (transforms.query-impl/resolve-transform-target db-id (:target transform)))+ transform))+(defn execute!"Run `transform` and sync its target table.@@ -14,4 +36,4 @@(execute! transform nil))([transform opts]#_{:clj-kondo/ignore [:discouraged-var]}- (transforms.i/execute! transform opts)))+ (transforms.i/execute! (resolve-target transform) opts)))diff --git a/src/metabase/transforms/query_impl.clj b/src/metabase/transforms/query_impl.cljindex 25bb10b103a..dd0c3f41a46 100644--- a/src/metabase/transforms/query_impl.clj+++ b/src/metabase/transforms/query_impl.clj@@ -3,6 +3,7 @@[clojure.core.async :as a][metabase.driver :as driver][metabase.driver.connection :as driver.conn]+ [metabase.premium-features.core :refer [defenterprise]][metabase.tracing.core :as tracing][metabase.transforms-base.interface :as transforms-base.i][metabase.transforms-base.util :as transforms-base.u]@@ -14,10 +15,26 @@(set! *warn-on-reflection* true)+(defenterprise resolve-transform-target+ "Hook for workspace isolation: given a database id and a transform's canonical target+ `{:schema ..., :name ...}`, returns the target the transform should actually write to.++ When workspace isolation is active for `db-id`, the EE impl rewrites the target's+ `:schema` to the workspace's output schema and records a `TableRemapping` so that+ subsequent queries against the canonical `(schema, name)` pair resolve to the+ workspace copy via the QP middleware.++ OSS / no-workspace fallback: returns the target unchanged."+ metabase-enterprise.workspaces.transform-hooks+ [_db-id target]+ target)+(defn- run-mbql-transform!([transform] (run-mbql-transform! transform nil))([{:keys [id source target owner_user_id creator_id] :as transform}{:keys [run-method start-promise user-id]}]+ ;; `:target` is already workspace-rewritten — `resolve-transform-target` runs in+ ;; `metabase.transforms.execute/execute!` before dispatch.(try(let [db (t2/select-one :model/Database (get-in source [:query :database]))driver (:engine db)diff --git a/src/metabase/transforms_base/query.clj b/src/metabase/transforms_base/query.cljindex 6483f6592d0..acc7835eff5 100644--- a/src/metabase/transforms_base/query.clj+++ b/src/metabase/transforms_base/query.clj@@ -11,6 +11,7 @@[metabase.transforms-base.interface :as transforms-base.i][metabase.transforms-base.schema :as transforms-base.schema][metabase.transforms-base.util :as transforms-base.u]+ [metabase.transforms-base.workspace-hooks :as transforms-base.workspace-hooks][metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.registry :as mr]@@ -93,13 +94,31 @@effective-transform-type (if (transforms-base.u/full-incremental-run? transform):table(keyword (:type target)))+ ;; Workspace SQL rewrite. Transforms route through `driver/run-transform!`+ ;; with pre-compiled SQL -- they don't go through `qp.execute/run`, so the+ ;; Phase 2 rewriter (which lives in the execute middleware chain) NEVER+ ;; fires for transforms. Both MBQL and native sources need their compiled+ ;; SQL rewritten here so canonical refs resolve to workspace-isolation+ ;; tables. No-op when no workspace is active. See+ ;; `metabase.transforms-base.workspace-hooks`.+ compiled-query (-> (transforms-base.u/compile-source transform source-range-params)+ (update :query+ #(transforms-base.workspace-hooks/rewrite-native-sql-for-workspace+ driver db %)))transform-details {:db-id db:database database:transform-id id:transform-type effective-transform-type:conn-spec (driver/connection-spec driver database)- :query (transforms-base.u/compile-source transform source-range-params)+ :query compiled-query:output-schema (:schema target)+ ;; Cross-DB write target qualifier. Populated when the driver's+ ;; `qualified-name-components` includes `:db` (MySQL/Snowflake/+ ;; SQL Server/BigQuery) AND the workspace remap put a different+ ;; DB into the target's `:db` slot. Compilation prepends this to+ ;; the CTAS table name. See+ ;; `metabase.driver.sql.query-processor/compile-transform :sql`.+ :output-db (:db target):output-table (transforms-base.u/qualified-table-name driver target)}opts (transform-opts transform-details)features (transforms-base.u/required-database-features transform)]diff --git a/src/metabase/transforms_base/util.clj b/src/metabase/transforms_base/util.cljindex 38e683b9cd6..383a486ccca 100644--- a/src/metabase/transforms_base/util.clj+++ b/src/metabase/transforms_base/util.clj@@ -30,6 +30,7 @@[metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.registry :as mr]+ [metabase.workspaces.table-remapping :as ws.table-remapping][toucan2.core :as t2])(:import(java.time Instant LocalDate LocalDateTime LocalTime OffsetDateTime OffsetTime ZonedDateTime)@@ -122,11 +123,20 @@default-schema)))(defn qualified-table-name- "Return the name of the target table of a transform as a possibly qualified symbol."+ "Return the transform target as a `:schema/name` (or bare `:name`) HoneySQL+ identifier. Consumers downstream rely on `name`/`namespace` to extract the+ parts (e.g. `metabase.driver.sql/run-transform! [:sql :table]`), so this stays+ 2-segment.++ The `:db` slot — populated for engines whose `qualified-name-components`+ includes `:db` (Snowflake / SQL Server / BigQuery / MySQL) — is **not** encoded+ here. It travels separately on `transform-details` as `:output-db` and is+ prepended at the SQL emission site. See+ [[metabase.driver.sql.query-processor/compile-transform :sql]]."[_driver {:keys [schema name]}]- (if schema- (keyword schema name)- (keyword name)))+ (if (str/blank? schema)+ (keyword name)+ (keyword schema name)))(defn temp-table-name"Generate a temporary table name with current timestamp in milliseconds.@@ -386,23 +396,43 @@{driver :engine :as database} (t2/select-one :model/Database db-id)](driver/table-exists? driver database target)))+(defn- canonicalize-target+ "If `target` is workspace-rewritten (i.e. its `(schema, name)` matches the+ to-side of an active TableRemapping for `db-id`), return `target` with+ `:schema` and `:name` swapped back to canonical. Else return `target`+ unchanged. The transform pipeline mutates `:target.schema` to the workspace+ output schema in `metabase.transforms.execute/resolve-target` so writes+ land in isolation; `:model/Table` rows must stay at the canonical schema,+ so we invert here.++ `canonical-schema+name` returns a `{:db :schema :name}` map with slot+ values already normalized to `:model/Table` row vocabulary (nil for+ engines that don't emit a slot, string otherwise) — directly usable as a+ Table-row predicate without further translation."+ [db-id target]+ (let [lookup-spec {:db (:db target) :schema (:schema target) :name (:name target)}]+ (if-let [{:keys [db schema name]} (ws.table-remapping/canonical-schema+name db-id lookup-spec)]+ (assoc target :db db :schema schema :name name)+ target)))+(defn- sync-table!([database target] (sync-table! database target nil))([database target {:keys [create?]}]- (when-let [table (or (target-table (:id database) target)- (when create?- (sync/create-table! database (select-keys target [:schema :name :data_source :data_authority :is_writable]))))]- ;; If the table has nil schema, check if the physical table actually lives under- ;; the driver's default schema. If so, fix the Table record before syncing.- (let [table (if (nil? (:schema table))- (if-let [actual-schema (resolve-nil-schema (:engine database) database table)]- (do (t2/update! :model/Table (:id table) {:schema actual-schema})- (-> (t2/select-one :model/Table (:id table))- (t2/hydrate :db)))- table)- table)]- (sync/sync-table! table)- table))))+ (let [target (canonicalize-target (:id database) target)]+ (when-let [table (or (target-table (:id database) target)+ (when create?+ (sync/create-table! database (select-keys target [:schema :name :data_source :data_authority :is_writable]))))]+ ;; If the table has nil schema, check if the physical table actually lives under+ ;; the driver's default schema. If so, fix the Table record before syncing.+ (let [table (if (nil? (:schema table))+ (if-let [actual-schema (resolve-nil-schema (:engine database) database table)]+ (do (t2/update! :model/Table (:id table) {:schema actual-schema})+ (-> (t2/select-one :model/Table (:id table))+ (t2/hydrate :db)))+ table)+ table)]+ (sync/sync-table! table)+ table)))))(defn activate-table-and-mark-computed!"Activate table for `target` in `database` in the app db."diff --git a/src/metabase/transforms_base/workspace_hooks.clj b/src/metabase/transforms_base/workspace_hooks.cljnew file mode 100644index 00000000000..ffeca8aa89d--- /dev/null+++ b/src/metabase/transforms_base/workspace_hooks.clj@@ -0,0 +1,18 @@+(ns metabase.transforms-base.workspace-hooks+ "Workspace-isolation hooks for transform execution paths that bypass the QP middleware+ pipeline. EE implementations live in `metabase-enterprise.workspaces.transform-hooks`."+ (:require+ [metabase.premium-features.core :refer [defenterprise]]))++(set! *warn-on-reflection* true)++(defenterprise rewrite-native-sql-for-workspace+ "Rewrite canonical table references in `sql` to their workspace-isolation counterparts.++ Hook for native-transform exec paths that hand compiled SQL directly to+ `driver/run-transform!` and bypass the QP's Phase 2 SQL rewriter.++ OSS / no-workspace fallback: returns the SQL unchanged."+ metabase-enterprise.workspaces.transform-hooks+ [_driver _db-id sql]+ sql)diff --git a/src/metabase/upload/impl.clj b/src/metabase/upload/impl.cljindex ffb5ced8a69..3a7f2b51734 100644--- a/src/metabase/upload/impl.clj+++ b/src/metabase/upload/impl.clj@@ -32,6 +32,7 @@[metabase.util.malli :as mu][metabase.util.malli.schema :as ms][metabase.warehouse-schema.models.table :as table]+ [metabase.workspaces.core :as workspaces][toucan2.core :as t2])(:import(com.ibm.icu.text Transliterator)@@ -561,6 +562,9 @@:file-extension extension:mime-type mime-type}))))))+(defn- check-workspace-mode! []+ (workspaces/check-not-in-workspace-mode! "CSV upload"))+(mu/defn create-csv-upload!"Main entry point for CSV uploading.@@ -591,6 +595,7 @@[:db-id ms/PositiveInt][:schema-name {:optional true} [:maybe :string]][:table-prefix {:optional true} [:maybe :string]]]]+ (check-workspace-mode!)(let [database (or (t2/select-one :model/Database :id db-id)(throw (ex-info (tru "The uploads database does not exist."){:status-code 422})))]@@ -967,6 +972,7 @@[:filename :string][:file (ms/InstanceOfClass File)][:action update-action-schema]]]+ (check-workspace-mode!)(let [table (api/check-404 (t2/select-one :model/Table :id table-id))database (table/database table)replace? (= :metabase.upload/replace action)]diff --git a/src/metabase/warehouses/models/database.clj b/src/metabase/warehouses/models/database.cljindex 0f81de35354..a9afc538bfa 100644--- a/src/metabase/warehouses/models/database.clj+++ b/src/metabase/warehouses/models/database.clj@@ -52,6 +52,7 @@(t2/deftransforms :model/Database{:details mi/transform-encrypted-json:write_data_details mi/transform-encrypted-json+ :admin_details mi/transform-encrypted-json:engine mi/transform-keyword:metadata_sync_schedule mi/transform-cron-string:cache_field_values_schedule mi/transform-cron-string@@ -153,6 +154,16 @@[_db-id](mi/superuser?))+(defenterprise reconcile-workspace-database-refs-before-delete!+ "Hook called from the `:model/Database` before-delete. In workspaces mode this refuses+ the delete (409) if any non-`:unprovisioned` `workspace_database` rows reference `db-id`,+ and explicitly removes any `:unprovisioned` rows so the FK RESTRICT is satisfied. OSS+ implementation is a no-op — fresh OSS installs have no workspace_database table, and+ feature-off EE instances have nothing to reconcile."+ metabase-enterprise.workspaces.models.workspace-database+ [_db-id]+ nil)+(defn- can-write?[db-id](or (some-> db-id db-id->router-db-id can-write?)@@ -283,6 +294,7 @@"Checks database health off-thread.- checks connectivity for the default connection- checks connectivity for the write connection (if configured)+ - checks connectivity for the admin connection (if configured)- cleans-up ambiguous legacy db-details"[{:keys [engine] :as database}](when-not (or (:is_audit database) (:is_sample database))@@ -293,7 +305,8 @@database (assoc database :details details)engine-str (name engine)driver (keyword engine-str)- details-map (assoc details :engine engine-str)]+ details-map (assoc details :engine engine-str)+ lib-db (driver.u/ensure-lib-database database)](when (check-connection! database driver engine-str details-map "default")(let [provider (provider-detection/detect-provider-from-database database)](when (not= provider (:provider_name database))@@ -304,11 +317,16 @@(t2/update! :model/Database (:id database) {:provider_name provider})(catch Throwable provider-e(log/warnf provider-e "Error during provider detection for database {:id %d}" (:id database)))))))- (when (driver.conn/database-write-data-details (driver.u/ensure-lib-database database))+ (when (driver.conn/database-write-data-details lib-db)(let [write-details (driver.conn/without-resolution-telemetry(driver.conn/with-write-connection(driver.conn/effective-details database)))]- (check-connection! database driver engine-str (assoc write-details :engine engine-str) "write-data"))))))))+ (check-connection! database driver engine-str (assoc write-details :engine engine-str) "write-data")))+ (when (driver.conn/database-admin-details lib-db)+ (let [admin-details (driver.conn/without-resolution-telemetry+ (driver.conn/with-admin-connection+ (driver.conn/effective-details database)))]+ (check-connection! database driver engine-str (assoc admin-details :engine engine-str) "admin"))))))))(defn check-health!"Health checks databases connected to metabase asynchronously using a thread pool."@@ -362,7 +380,8 @@driver(-> db(m/update-existing-in [:details :auth-provider] keyword)- (m/update-existing-in [:write_data_details :auth-provider] keyword)))))]+ (m/update-existing-in [:write_data_details :auth-provider] keyword)+ (m/update-existing-in [:admin_details :auth-provider] keyword)))))](cond-> database;; TODO - this is only really needed for API responses. This should be a `hydrate` thing instead!(and driver@@ -403,6 +422,11 @@(t2/define-before-delete :model/Database[{id :id, driver :engine, :as database}]+ ;; Reconcile workspace_database rows first: the FK is RESTRICT, so :unprovisioned+ ;; rows must be cleaned up explicitly, and non-:unprovisioned rows must refuse the delete.+ ;; Runs before any other cleanup so we don't partially unwind sync tasks / secrets+ ;; / fields for a Database whose deletion is about to be refused.+ (reconcile-workspace-database-refs-before-delete! id)(unschedule-tasks! database)(secret/delete-orphaned-secrets! database)(delete-database-fields! id)@@ -468,7 +492,8 @@infer-db-schedules(or (some? (:details changes))- (some? (:write_data_details changes)))+ (some? (:write_data_details changes))+ (some? (:admin_details changes)))secret/handle-incoming-client-secrets!(:uploads_enabled changes)@@ -563,9 +588,9 @@driver.u/default-sensitive-fields))(methodical/defmethod mi/to-json :model/Database- "When encoding a Database as JSON remove the `details` and `write_data_details` for any User without write perms- for the DB. Users with write perms can see the details but remove anything resembling a password. No one gets to- see this in an API response!+ "When encoding a Database as JSON remove the `details`, `write_data_details`, and `admin_details` for any User+ without write perms for the DB. Users with write perms can see the details but remove anything resembling a+ password. No one gets to see this in an API response!Also remove settings that the User doesn't have read perms for."[db json-generator]@@ -577,12 +602,13 @@(next-method(let [db (if (not (mi/can-write? db))(do (log/debug "Fully redacting database details during json encoding.")- (dissoc db :details :write_data_details))+ (dissoc db :details :write_data_details :admin_details))(do (log/debug "Redacting sensitive fields within database details during json encoding.")(-> db(secret/to-json-hydrate-redacted-secrets)(update :details redact-sensitive-fields)- (m/update-existing :write_data_details redact-sensitive-fields))))]+ (m/update-existing :write_data_details redact-sensitive-fields)+ (m/update-existing :admin_details redact-sensitive-fields))))](update db :settings(fn [settings](when (map? settings)@@ -609,38 +635,34 @@;;; ------------------------------------------------ Serialization ----------------------------------------------------(defmethod serdes/make-spec "Database"[_model-name {:keys [include-database-secrets]}]- {:copy [:auto_run_queries :cache_field_values_schedule :caveats :dbms_version- :description :engine :is_audit :is_attached_dwh :is_full_sync :is_on_demand :is_sample- :metadata_sync_schedule :name :points_of_interest :provider_name :refingerprint :settings :timezone :uploads_enabled- :uploads_schema_name :uploads_table_prefix]- :skip [;; deprecated field- :cache_ttl]- :transform {:created_at (serdes/date)- ;; details should be imported if available regardless of options- :details {:export-with-context- (fn [current _ details]- (if (and include-database-secrets- (not (:is_attached_dwh current)))- details- ::serdes/skip))- :import identity}- :write_data_details {:export-with-context- (fn [current _ details]- (if (and include-database-secrets- (not (:is_attached_dwh current)))- details- ::serdes/skip))- :import identity}- :creator_id (serdes/fk :model/User)- :router_database_id (serdes/fk :model/Database)- :initial_sync_status {:export identity :import (constantly "complete")}}- :defaults {:auto_run_queries true- :is_attached_dwh false- :is_audit false- :is_full_sync true- :is_on_demand false- :is_sample false- :uploads_enabled false}})+ ;; Export only when secrets are explicitly included AND the database isn't an attached DWH.+ ;; Import is unconditional.+ (let [details-transform {:export-with-context (fn [current _ details]+ (if (and include-database-secrets+ (not (:is_attached_dwh current)))+ details+ ::serdes/skip))+ :import identity}]+ {:copy [:auto_run_queries :cache_field_values_schedule :caveats :dbms_version+ :description :engine :is_audit :is_attached_dwh :is_full_sync :is_on_demand :is_sample+ :metadata_sync_schedule :name :points_of_interest :provider_name :refingerprint :settings :timezone :uploads_enabled+ :uploads_schema_name :uploads_table_prefix]+ :skip [;; deprecated field+ :cache_ttl]+ :transform {:created_at (serdes/date)+ :details details-transform+ :write_data_details details-transform+ :admin_details details-transform+ :creator_id (serdes/fk :model/User)+ :router_database_id (serdes/fk :model/Database)+ :initial_sync_status {:export identity :import (constantly "complete")}}+ :defaults {:auto_run_queries true+ :is_attached_dwh false+ :is_audit false+ :is_full_sync true+ :is_on_demand false+ :is_sample false+ :uploads_enabled false}}))(def ^:dynamic *include-h2-in-extract?*"When false (the default), [[serdes/extract-query]] skips H2 databases because they are rejected at import time@@ -685,7 +707,8 @@(assert-not-h2! ingested)(serdes/default-load-one! (cond-> ingested(:details ingested) (update :details driver/sanitize-db-details)- (:write_data_details ingested) (update :write_data_details driver/sanitize-db-details))+ (:write_data_details ingested) (update :write_data_details driver/sanitize-db-details)+ (:admin_details ingested) (update :admin_details driver/sanitize-db-details))maybe-local))(def ^:private metadata-export-permsdiff --git a/src/metabase/warehouses_rest/api.clj b/src/metabase/warehouses_rest/api.cljindex 4344884c988..160d8d43cc1 100644--- a/src/metabase/warehouses_rest/api.clj+++ b/src/metabase/warehouses_rest/api.clj@@ -947,29 +947,56 @@details(database/sensitive-fields-for-db database))))))-(defn- validate-write-data-details!- "Validates write_data_details guardrails. Throws 400 on violation."- [existing-database write-data-details]- (api/check-400 (not (:router_database_id existing-database))- (tru "Cannot configure a write connection on a destination database"))- (api/check-400 (not (t2/exists? :model/Database :router_database_id (:id existing-database)))- (tru "Cannot configure a write connection on a router database"))- (when-not (:write-data-connection write-data-details)- (throw (ex-info (tru "write-data-connection must be set in write_data_details")- {:status-code 400})))- (when (:destination-database write-data-details)- (throw (ex-info (tru "destination-database must be false in write_data_details")- {:status-code 400})))- (let [engine (keyword (:engine existing-database))]- (when-let [hidden-fields (not-empty (driver.u/fields-hidden-for-write-data-connection engine))]+(def ^:private connection-marker-key->details-column+ {:write-data-connection "write_data_details"+ :admin-connection "admin_details"})++(defn- validate-overlay-details!+ "Common guardrails for overlay-details (write_data_details, admin_details).+ Throws 400 on violation."+ [existing-database overlay-details+ {:keys [marker-key article+noun hidden-fields-fn]}]+ (let [[article noun] article+noun+ marker-name (name marker-key)+ column-name (connection-marker-key->details-column marker-key)+ article-noun (str article " " noun)]+ (api/check-400 (not (:router_database_id existing-database))+ (tru "Cannot configure {0} connection on a destination database" article-noun))+ (api/check-400 (not (t2/exists? :model/Database :router_database_id (:id existing-database)))+ (tru "Cannot configure {0} connection on a router database" article-noun))+ (when-not (get overlay-details marker-key)+ (throw (ex-info (tru "{0} must be set in {1}" marker-name column-name)+ {:status-code 400})))+ (when (:destination-database overlay-details)+ (throw (ex-info (tru "destination-database must be false in {0}" column-name)+ {:status-code 400})))+ (when-let [hidden-fields (not-empty (hidden-fields-fn (keyword (:engine existing-database))))](let [hidden-kws (into #{} (map keyword) hidden-fields)- disallowed-keys (filterv #(contains? hidden-kws %) (keys write-data-details))]+ disallowed-keys (filterv #(contains? hidden-kws %) (keys overlay-details))](when (seq disallowed-keys)- (throw (ex-info (tru "write_data_details must not contain fields hidden for write connections: {0}"- (str/join ", " (sort (map name disallowed-keys))))+ (throw (ex-info (tru "{0} must not contain fields hidden for {1} connections: {2}"+ column-name noun (str/join ", " (sort (map name disallowed-keys)))){:status-code 400:disallowed-keys disallowed-keys})))))))+(defn- validate-write-data-details!+ "Validates write_data_details guardrails. Throws 400 on violation."+ [existing-database write-data-details]+ (validate-overlay-details!+ existing-database write-data-details+ {:marker-key :write-data-connection+ :article+noun ["a" "write"]+ :hidden-fields-fn driver.u/fields-hidden-for-write-data-connection}))++(defn- validate-admin-details!+ "Validates admin_details guardrails. Throws 400 on violation."+ [existing-database admin-details]+ (validate-overlay-details!+ existing-database admin-details+ {:marker-key :admin-connection+ :article+noun ["an" "admin"]+ :hidden-fields-fn driver.u/fields-hidden-for-admin-connection}))+;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to;; use our API + we will need it when we make auto-TypeScript-signature generation happen;;@@ -979,14 +1006,15 @@[{:keys [id]} :- [:map[:id ms/PositiveInt]]_query-params- {:keys [name engine details write_data_details is_full_sync is_on_demand description caveats points_of_interest- schedules auto_run_queries refingerprint cache_ttl settings provider_name]}+ {:keys [name engine details write_data_details admin_details is_full_sync is_on_demand description caveats+ points_of_interest schedules auto_run_queries refingerprint cache_ttl settings provider_name]}:- [:map[:name {:optional true} [:maybe ms/NonBlankString]][:engine {:optional true} [:maybe DBEngineString]][:refingerprint {:optional true} [:maybe :boolean]][:details {:optional true} [:maybe ms/Map]][:write_data_details {:optional true} [:maybe ms/Map]]+ [:admin_details {:optional true} [:maybe ms/Map]][:schedules {:optional true} [:maybe sync.schedules/ExpandedSchedulesMap]][:description {:optional true} [:maybe :string]][:caveats {:optional true} [:maybe :string]]@@ -998,21 +1026,32 @@;; TODO - ensure that custom schedules and let-user-control-scheduling go in lockstep(when (some? write_data_details)(premium-features/assert-has-feature :writable-connection (tru "Writable Connection")))+ (when (some? admin_details)+ (premium-features/assert-has-feature :workspaces (tru "Admin Connection")))(when (:write-data-connection details)(throw (ex-info (tru "write-data-connection must not be set in details"){:status-code 400})))+ (when (:admin-connection details)+ (throw (ex-info (tru "admin-connection must not be set in details")+ {:status-code 400})))(let [existing-database (api/write-check (t2/select-one :model/Database :id id))_ (when write_data_details(validate-write-data-details! existing-database write_data_details))+ _ (when admin_details+ (validate-admin-details! existing-database admin_details))incoming-details detailsincoming-write-data-details write_data_details+ incoming-admin-details admin_detailsdetails-with-secrets (some->> details(upsert-sensitive-fields existing-database))write-data-details-with-secrets (when write_data_details(upsert-sensitive-fields existing-database write_data_details :write_data_details))+ admin-details-with-secrets (when admin_details+ (upsert-sensitive-fields existing-database admin_details :admin_details));; verify that we can connect to the database if details OR `:engine` have changed.details-changed? (some-> details-with-secrets (not= (:details existing-database)))write-details-changed? (some-> write-data-details-with-secrets (not= (:write_data_details existing-database)))+ admin-details-changed? (some-> admin-details-with-secrets (not= (:admin_details existing-database)))engine-changed? (some-> engine keyword (not= (:engine existing-database)))main-conn-error (when (or details-changed? engine-changed?)(warehouses/test-database-connection (or engine (:engine existing-database))@@ -1024,13 +1063,19 @@(driver.conn/with-write-connection(warehouses/test-database-connection (or engine (:engine would-be-database))(driver.conn/effective-details would-be-database)))))+ admin-conn-error (when (or admin-details-changed? engine-changed?)+ (let [would-be-database (cond-> existing-database+ details-with-secrets (assoc :details details-with-secrets)+ admin-details-with-secrets (assoc :admin_details admin-details-with-secrets))]+ (driver.conn/with-admin-connection+ (warehouses/test-database-connection (or engine (:engine would-be-database))+ (driver.conn/effective-details would-be-database)))))full-sync? (some-> is_full_sync boolean)on-demand? (boolean is_on_demand)]- (if (or main-conn-error write-conn-error)+ (if-let [conn-error (or main-conn-error write-conn-error admin-conn-error)];; failed to connect, return error{:status 400- ;; Write details are a merge over the top of default connection:- :body (or main-conn-error write-conn-error)}+ :body conn-error};; no error, proceed with update(let [existing-settings (:settings existing-database)pending-settings (into {}@@ -1046,6 +1091,7 @@:engine engine:details details-with-secrets:write_data_details write-data-details-with-secrets+ :admin_details admin-details-with-secrets:refingerprint refingerprint:is_full_sync full-sync?:is_on_demand on-demand?@@ -1057,7 +1103,7 @@:provider_name provider_name}:non-nil #{:name :engine :details :refingerprint :is_full_sync :is_on_demand:description :caveats :points_of_interest :auto_run_queries :settings}- :present #{:provider_name :write_data_details})+ :present #{:provider_name :write_data_details :admin_details});; cache_field_values_schedule can be nil(when schedules(sync.schedules/schedule-map->cron-strings schedules)))@@ -1098,7 +1144,8 @@add-expanded-schedules;; return the DB with the passed in details in place(m/update-existing :details #(merge incoming-details %))- (m/update-existing :write_data_details #(merge incoming-write-data-details %))))))))+ (m/update-existing :write_data_details #(merge incoming-write-data-details %))+ (m/update-existing :admin_details #(merge incoming-admin-details %))))))));;; -------------------------------------------- DELETE /api/database/:id --------------------------------------------diff --git a/src/metabase/workspaces/core.clj b/src/metabase/workspaces/core.cljnew file mode 100644index 00000000000..0c8febc2c3a--- /dev/null+++ b/src/metabase/workspaces/core.clj@@ -0,0 +1,122 @@+(ns metabase.workspaces.core+ "OSS-side public API for workspace mode.++ ## Three related-but-distinct concepts++ 1. **Workspace mode** — an *instance-level boot state*. True iff the instance+ loaded a `:workspace` section from `config.yml` at boot. Set once, doesn't+ change. The `workspace-mode?` predicate.++ 2. **Workspace-managed database** — a *database-scoped state*. True iff this+ instance is in workspace mode AND the workspace's `:databases` map includes+ this database. The `(ws/db-workspace-namespace db-id)` predicate (EE only).++ 3. **Table remapping engaged** — a *query-time data state*. True iff+ `:model/TableRemapping` rows exist for the database. The+ `(ws.remapping/enabled-for-db? db-id)` predicate. Today this implies+ workspace-managed (transforms create the rows when the database is in+ the workspace map), but the rewriting machinery responds purely to row+ existence — it doesn't check workspace mode.++ ## What this namespace gates++ `check-not-in-workspace-mode!` uses concept (1) — the *instance-level*+ check. Features blocked here (DB routing, impersonation, writeback, CSV+ upload, model persistence enable) are refused on a child instance entirely,+ not per-database. Rationale: workspace children boot with empty everything+ except content; per Dan in proj-workspaces 2026-04-30, those features can't+ already be configured. The instance-level gate is simpler and defensive —+ no escape hatch.++ If a future use case requires running a feature against a non-workspace-+ managed database on a child, refine the call sites to per-database checks+ using the EE `db-workspace-namespace` predicate.++ ## OSS vs EE++ The OSS jar has no workspace mode (`workspace-mode?` returns false), so+ the gates are no-ops. The EE impl in `metabase-enterprise.workspaces.core`+ reads the in-process workspace atom."+ (:require+ [metabase.premium-features.core :refer [defenterprise defenterprise-schema]]+ [metabase.util.malli.registry :as mr]))++;;; ----------------------------- Workspace config schemas ----------------------------------++(mr/def ::table-namespace+ "A `{:db ?, :schema ?}` namespace map. Either or both keys may be present+ depending on the driver's `qualified-name-components`; at least one must+ populate. Empty-string `\"\"` is reserved for the storage layer; the atom+ carries `nil`/missing for absent slots."+ [:and+ [:map+ [:db {:optional true} [:maybe :string]]+ [:schema {:optional true} [:maybe :string]]]+ [:fn {:error/message "table namespace must populate at least one of :db or :schema"}+ (fn [m] (or (some? (:db m)) (some? (:schema m))))]])++(mr/def ::workspace-database-config+ "Per-database workspace config: `:input_schemas` is a vector of driver-opaque+ schema names (the source schemas the workspace reads from) — may be empty+ on drivers with no schema layer (e.g. MySQL), where the bound DB itself acts+ as the implicit input namespace; `:output` is a single namespace map (the+ workspace's isolation schema, expanded with the warehouse catalog at boot)."+ [:map+ [:input_schemas [:vector :string]]+ [:output ::table-namespace]])++(mr/def ::workspace-instance-config+ "The shape stored in the EE workspace atom after the `:workspace` config.yml+ loader has resolved database names to ids. Database keys are integer ids+ (post-resolution); the wire format with name keys lives in+ `metabase-enterprise.advanced-config.file.workspace`."+ [:map+ [:name [:string {:min 1}]]+ [:databases [:map-of+ {:decode/json #(update-keys % (fn [k] (cond-> k (keyword? k) (-> name parse-long))))}+ :int ::workspace-database-config]]])++(defenterprise workspace-mode?+ "True if this instance is running in workspace mode — a `:workspace` section+ was loaded from `config.yml` at boot. Instance-level state set once at boot.++ For the per-database check, use the EE function+ `metabase-enterprise.workspaces.core/db-workspace-namespace` (returns the+ workspace-isolated output namespace map if this instance is in workspace mode+ AND the workspace's `:databases` map includes this database).++ The OSS fallback returns false."+ metabase-enterprise.workspaces.core+ []+ false)++(defenterprise-schema set-instance-workspace! :- :any+ "Install the in-process workspace config atom on this instance. The shape is+ validated against `::workspace-instance-config` at the OSS boundary so a+ malformed config never reaches the EE atom or the QP / transform hooks that+ read from it.++ No-op on OSS — workspace mode is EE-only."+ metabase-enterprise.workspaces.core+ [_config :- ::workspace-instance-config]+ nil)++(defenterprise clear-instance-workspace!+ "Clear the in-process workspace config atom. No-op on OSS."+ metabase-enterprise.workspaces.core+ []+ nil)++(defn check-not-in-workspace-mode!+ "Throws an HTTP 400 `ex-info` if this instance is running in workspace mode.++ Use at API entry points for features that must be refused on a workspace+ child instance (DB routing, connection impersonation, writeback, CSV upload,+ model persistence enable). The check is *instance-level*, not per-database+ — see namespace docstring for the rationale."+ [feature-name]+ (when (workspace-mode?)+ (throw (ex-info (str feature-name " is not supported on a workspace instance.")+ {:status-code 400+ :feature feature-name+ :workspace-mode true}))))diff --git a/src/metabase/workspaces/table_remapping.clj b/src/metabase/workspaces/table_remapping.cljnew file mode 100644index 00000000000..edd5dbd7692--- /dev/null+++ b/src/metabase/workspaces/table_remapping.clj@@ -0,0 +1,131 @@+(ns metabase.workspaces.table-remapping+ "The OSS namespace for workspace table remapping."+ (:require+ [metabase.premium-features.core :refer [defenterprise]]))++(defenterprise workspace-remap-schema+name+ "In workspace mode, a Table at the canonical identity `from-spec` may be backed+ by a physically-different warehouse table recorded in `table_remapping`. Returns+ a `{:db :schema :name}` map for the workspace destination when a remapping+ exists so sync asks the driver about the isolated warehouse location; returns+ nil otherwise (OSS fallback) so the driver is queried at the logical identity.++ Both input and output are the same `{:db :schema :name}` shape — symmetric so+ call sites don't have to translate between vector tuples and maps. Slot values+ are normalized to the form `:model/Table` rows actually carry: a string for+ engines that emit that AST position; `nil` for engines that don't. For MySQL,+ both `:db` and `:schema` are `nil` on a Table row (the connection's bound DB+ serves as the implicit catalog; MySQL has no schemas)."+ metabase-enterprise.workspaces.table-remapping+ [_db-id _from-spec]+ nil)++(defenterprise filter-workspace-side-tables+ "Drop tuples from a `describe-database` result whose `(schema, name)` matches+ the to-side of any active TableRemapping for `db-id`. The workspace's physical+ isolation tables are surfaced by the warehouse driver but must not become+ `:model/Table` rows in app-db -- they back canonical Tables via remap, not+ their own identity. OSS fallback is identity (no filtering). See DEV-1898."+ metabase-enterprise.workspaces.table-remapping+ [tuples _db-id]+ tuples)++(defenterprise expand-schema-names-with-workspace+ "Augment a list of `:schema-names` with workspace-isolation schemas+ (`to_schema` values) for any remap whose `from_schema` appears in the input.+ Lets sync's FK fetch reach the warehouse tables that physically back canonical+ Tables on a workspace child. OSS fallback is identity."+ metabase-enterprise.workspaces.table-remapping+ [schema-names _db-id]+ schema-names)++(defenterprise inject-workspace-canonical-tuples+ "Augment a `describe-database` result with synthetic canonical-side tuples for+ any `from_*` remap row whose to-side is materialized in the warehouse. The+ canonical name doesn't physically exist on a workspace child (only the+ isolation-schema copy does), so without this it'd be diffed against app-db's+ Table rows and silently retired. OSS fallback is identity."+ metabase-enterprise.workspaces.table-remapping+ [tuples _db-id]+ tuples)++(defenterprise rewrite-fk-result-canonical+ "Translate workspace-side identifiers in a `describe-fks` result back to canonical+ names. When sync redirects FK lookups to the workspace warehouse table, the+ returned rows reference workspace-side `(schema, name)` on both sides; app-db's+ view needs them in canonical terms so subsequent FK resolution finds the+ matching `:model/Table`. OSS fallback is identity (no rewriting)."+ metabase-enterprise.workspaces.table-remapping+ [rows _db-id]+ rows)++(defenterprise call-with-display-context+ "Invoke `thunk` with workspace remapping suppressed. Used by display-only paths+ (the QB's `POST /api/dataset/native` SQL preview, anywhere we surface compiled+ SQL to the user) so users see canonical-schema SQL instead of the workspace+ isolation schema. The query still executes against the isolation schema at+ warehouse time -- this only affects what the user *sees* in the SQL preview.++ OSS fallback: just calls the thunk. EE impl binds+ `ws.remapping/*skip-remapping?*` true around it, which short-circuits both+ Phase 1 (metadata override) and Phase 2 (SQLGlot rewrite).++ Use [[with-display-context]] for the macro form."+ metabase-enterprise.workspaces.table-remapping+ [thunk]+ (thunk))++(defmacro with-display-context+ "Suppress workspace remapping inside `body` so compiled SQL surfaces in canonical+ vocabulary instead of isolation-schema vocabulary. Wrapper around+ [[call-with-display-context]]."+ [& body]+ `(call-with-display-context (fn [] ~@body)))++(defenterprise call-with-fk-probe-iso-dbs+ "Invoke `f` once per iso-`:db` slot the FK probe needs to visit on a workspaced+ database. `f` is called with no arguments inside a `with-swapped-connection-details`+ scope (or no swap if iso-`:db` equals the canonical bound `:db`). Callers collect+ results across invocations and merge.++ On MySQL workspaces with a cross-DB swap, the iso table lives in a different+ bound database than the canonical one; describe-fks must run with the JDBC+ connection pointed at the iso DB to discover its FKs.++ OSS fallback: just calls `f` once with no swap -- the warehouse FK probe runs+ against the canonical connection only."+ metabase-enterprise.workspaces.table-remapping+ [_db-id f]+ [(f)])++(defenterprise canonical-schema+name+ "Inverse of `workspace-remap-schema+name`. Given an isolation-side+ `to-spec` (`{:db :schema :name}` for the workspace destination), return a+ `{:db :schema :name}` map for the canonical table if a `TableRemapping` row+ records that pair as the workspace destination of a canonical table; return+ nil otherwise.++ Use at write-side `:model/Table` lookups where the transform pipeline has+ already mutated `:target.schema` to the workspace output schema, but the+ app-db Table row lives at the canonical schema. OSS fallback is nil so+ callers fall through to the unchanged identity.++ Both input and output are the same `{:db :schema :name}` shape — symmetric+ with `workspace-remap-schema+name` and matches `:model/Table` row vocabulary.+ Slot values are driver-normalized: a string for engines that emit that AST+ position; `nil` for engines that don't. For MySQL, both `:db` and `:schema`+ are `nil` because that's what `:model/Table` rows on MySQL store, and a+ Table-row predicate on `:schema \"\"` will not match a row stored as+ `:schema nil`.++ Caveats (tracked separately):+ - Trusts the active `TableRemapping` row set. If a `WorkspaceDatabase`+ deprovision leaves stale rows behind, this hook will still translate+ against them. See the deprovision-clears-remappings cleanup in+ `workspaces-v2.md` worklog.+ - The H7 second half (cross-DB workspaces on BigQuery) needs+ callers to thread `:db` through the spec end-to-end. Both this hook and+ `add-transform-target-mapping!` now carry the slot."+ metabase-enterprise.workspaces.table-remapping+ [_db-id _to-spec]+ nil)diff --git a/test/metabase/app_db/general_schema_tests.clj b/test/metabase/app_db/general_schema_tests.cljindex 366d89a268d..daafc8b3d87 100644--- a/test/metabase/app_db/general_schema_tests.clj+++ b/test/metabase/app_db/general_schema_tests.clj@@ -18,7 +18,9 @@(deftest ^:parallel fks-are-indexed-test(when (= (mdb/db-type) :postgres)(let [excluded-fks #{{:table_name "pulse_channel"- :column_name "channel_id"}}+ :column_name "channel_id"}+ {:table_name "workspace"+ :column_name "creator_id"}}indexed-fks (t2/query"SELECTconrelid::regclass::text AS table_name,diff --git a/test/metabase/driver/connection_test.clj b/test/metabase/driver/connection_test.cljindex bc257e96549..2dad0da3ba6 100644--- a/test/metabase/driver/connection_test.clj+++ b/test/metabase/driver/connection_test.clj@@ -86,6 +86,159 @@(driver.conn/with-write-connection(is (true? (driver.conn/write-connection-requested?)))))))+(deftest effective-details-default-with-admin-details-test+ (testing "effective-details returns :details when *connection-type* is :default, even when :admin-details present"+ (let [details {:host "read-host" :port 5432}+ admin-details {:host "admin-host" :port 5432 :user "root"}+ database {:lib/type :metadata/database+ :details details+ :admin-details admin-details}]+ (is (=? details+ (driver.conn/effective-details database))))))++(deftest effective-details-admin-with-admin-details-test+ (mt/when-ee-evailable+ (mt/with-premium-features #{:workspaces}+ (let [details {:host "read-host" :port 5432}+ admin-details {:host "admin-host" :port 5432 :user "root"}]+ (testing "effective-details returns admin_details when :admin and details exist"+ (testing "with kebab-case key (Lib metadata style)"+ (let [database {:lib/type :metadata/database+ :details details+ :admin-details admin-details}]+ (driver.conn/with-admin-connection+ (is (=? admin-details+ (driver.conn/effective-details database))))))+ (testing "with snake_case key (Toucan2 instance style)"+ (let [database (t2/instance :model/Database+ {:id 1+ :details details+ :admin_details admin-details})]+ (driver.conn/with-admin-connection+ (is (=? admin-details+ (driver.conn/effective-details database)))))))))))++(deftest effective-details-admin-fallback-test+ (mt/with-premium-features #{:workspaces}+ (let [details {:host "read-host" :port 5432}]+ (testing "effective-details falls back to :details when :admin but no admin details"+ (let [database {:lib/type :metadata/database+ :details details+ :admin-details nil}]+ (driver.conn/with-admin-connection+ (is (=? details+ (driver.conn/effective-details database)))))+ (testing "also when :admin_details key is missing entirely"+ (let [database {:lib/type :metadata/database+ :details details}]+ (driver.conn/with-admin-connection+ (is (=? details+ (driver.conn/effective-details database))))))))))++(deftest effective-details-admin-without-feature-test+ (testing "without :workspaces feature, with-admin-connection falls back to main connection details"+ (mt/with-premium-features #{}+ (let [details {:host "read-host" :port 5432}+ admin-details {:host "admin-host" :port 5432 :user "root"}+ database {:lib/type :metadata/database+ :details details+ :admin-details admin-details}]+ (driver.conn/with-admin-connection+ (is (=? details+ (driver.conn/effective-details database))))))))++(deftest admin-connection-requested?-test+ (testing "admin-connection-requested? returns false by default"+ (is (false? (driver.conn/admin-connection-requested?))))+ (testing "admin-connection-requested? returns true inside with-admin-connection"+ (mt/with-premium-features #{:workspaces}+ (driver.conn/with-admin-connection+ (is (true? (driver.conn/admin-connection-requested?))))))+ (testing "admin-connection-requested? does not leak across to write-connection"+ (mt/with-premium-features #{:writable-connection}+ (driver.conn/with-write-connection+ (is (false? (driver.conn/admin-connection-requested?))))))+ (testing "write-connection-requested? does not leak across to admin-connection"+ (mt/with-premium-features #{:workspaces}+ (driver.conn/with-admin-connection+ (is (false? (driver.conn/write-connection-requested?)))))))++(deftest effective-connection-type-admin-test+ (mt/when-ee-evailable+ (mt/with-premium-features #{:workspaces}+ (let [details {:host "read-host"}+ admin-details {:host "admin-host"}+ configured {:lib/type :metadata/database :id 1+ :details details :admin-details admin-details}+ unconfigured {:lib/type :metadata/database :id 1 :details details}]+ (testing "returns :admin when both requested and configured"+ (driver.conn/with-admin-connection+ (is (= :admin (driver.conn/effective-connection-type configured)))))+ (testing "returns :default when requested but not configured (avoids duplicate pool)"+ (driver.conn/with-admin-connection+ (is (= :default (driver.conn/effective-connection-type unconfigured)))))+ (testing "returns :default when not requested even if configured"+ (is (= :default (driver.conn/effective-connection-type configured))))))))++(deftest effective-details-admin-with-workspace-swap-test+ (mt/when-ee-evailable+ (mt/with-premium-features #{:workspaces}+ (testing "effective-details applies workspace swap AFTER admin-details merge"+ (let [database {:lib/type :metadata/database+ :id 1+ :details {:host "host" :user "reader" :port 5432}+ :admin-details {:user "root" :password "admin-pass" :admin true}}]+ (driver.w/with-swapped-connection-details 1 {:user "ws-user" :password "ws-pass"}+ (driver.conn/with-admin-connection+ (is (=? {:host "host" :user "ws-user" :password "ws-pass" :port 5432 :admin true}+ (driver.conn/effective-details database))))))))))++(deftest type-resolved-metric-admin-test+ (mt/when-ee-evailable+ (mt/with-premium-features #{:workspaces}+ (testing "type-resolved counter increments when admin-details are genuinely used"+ (mt/with-prometheus-system! [_ system]+ (let [database {:lib/type :metadata/database+ :id 1+ :details {:host "read-host" :port 5432}+ :admin-details {:host "admin-host"}}]+ (driver.conn/with-admin-connection+ (driver.conn/effective-details database))+ (is (prometheus-test/approx= 1 (mt/metric-value system :metabase-db-connection/type-resolved+ {:connection-type "admin"}))))))+ (testing "type-resolved counter does NOT increment on fallback (no admin-details)"+ (mt/with-prometheus-system! [_ system]+ (let [database {:lib/type :metadata/database+ :id 1+ :details {:host "read-host" :port 5432}}]+ (driver.conn/with-admin-connection+ (driver.conn/effective-details database))+ (is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved+ {:connection-type "admin"}))))))+ (testing "type-resolved counter does NOT increment when workspace swap is active"+ (mt/with-prometheus-system! [_ system]+ (let [database {:lib/type :metadata/database+ :id 1+ :details {:host "read-host" :port 5432}+ :admin-details {:host "admin-host"}}]+ (driver.w/with-swapped-connection-details 1 {:user "ws-user"}+ (driver.conn/with-admin-connection+ (driver.conn/effective-details database)))+ (is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved+ {:connection-type "admin"}))))))+ (testing "type-resolved counter does NOT increment inside without-resolution-telemetry"+ (mt/with-prometheus-system! [_ system]+ (let [database {:lib/type :metadata/database+ :id 1+ :details {:host "read-host" :port 5432}+ :admin-details {:host "admin-host"}}]+ (driver.conn/without-resolution-telemetry+ (driver.conn/with-admin-connection+ (is (=? {:host "admin-host" :port 5432}+ (driver.conn/effective-details database)))))+ (is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved+ {:connection-type "admin"})))))))))+(deftest effective-details-with-workspace-swap-test(testing "effective-details applies workspace swap in :default connection type"(let [database {:lib/type :metadata/databasediff --git a/test/metabase/driver/h2_test.clj b/test/metabase/driver/h2_test.cljindex 06b2c50b0df..7bd0e6ee3d0 100644--- a/test/metabase/driver/h2_test.clj+++ b/test/metabase/driver/h2_test.clj@@ -47,6 +47,11 @@;; NB: because of test parallelism, this *will* affect other non-h2;; tests, but the check above in the test-driver function will;; prevent it from actually doing anything different in those tests.+ ;; The kondo ignore is the exemption: this `with-redefs` is+ ;; parallel-safe by construction (the redef target is a no-op+ ;; identity for h2, and the inner conditional in `test-driver`+ ;; prevents it from affecting non-h2 tests).+ #_{:clj-kondo/ignore [:metabase/validate-deftest]}(with-redefs [mtd/-test-driver test-driver](f))))diff --git a/test/metabase/driver/postgres_test.clj b/test/metabase/driver/postgres_test.cljindex 5b1cc017a74..3db924cdde3 100644--- a/test/metabase/driver/postgres_test.clj+++ b/test/metabase/driver/postgres_test.clj@@ -90,6 +90,16 @@;; NB: because of test parallelism, this *will* affect other non-pg;; tests, but the check above in the test-driver function will;; prevent it from actually doing anything different in those tests.+ ;;+ ;; The linter (`:metabase/validate-deftest`) flags `with-redefs` inside a+ ;; `use-fixtures` body because it isn't parallel-safe -- the root var is+ ;; mutated globally across threads. We accept the hazard here for the+ ;; duration of the `:postgres-mbql5` equivalence experiment: this fixture+ ;; reruns every `:postgres` test under the `:postgres-mbql5` dispatch so+ ;; we can confirm behavioral parity. Once the experiment concludes and+ ;; `:postgres-mbql5` becomes `:postgres`, this entire `with-redefs` goes+ ;; away (see Phil's note in #ee-querying-platform 2026-05-21).+ #_{:clj-kondo/ignore [:metabase/validate-deftest]}(with-redefs [mtd/-test-driver test-driver](mt/with-test-user :rasta (thunk))))))diff --git a/test/metabase/driver/sql_jdbc/connection_test.clj b/test/metabase/driver/sql_jdbc/connection_test.cljindex e88ff83dc8f..92c777e5187 100644--- a/test/metabase/driver/sql_jdbc/connection_test.clj+++ b/test/metabase/driver/sql_jdbc/connection_test.clj@@ -209,33 +209,39 @@;; Cleanup(sql-jdbc.conn/invalidate-pool-for-db! database)))))))))-(deftest invalidate-pool-clears-both-connection-types-test+(deftest invalidate-pool-clears-all-connection-types-test(mt/test-driver :h2(when config/ee-available?- (mt/with-premium-features #{:writable-connection}- (testing "invalidate-pool-for-db! clears both default and write pools"+ (mt/with-premium-features #{:writable-connection :workspaces}+ (testing "invalidate-pool-for-db! clears default, write, and admin pools"(let [read-details {:db "mem:invalidate_test"}- write-details {:db "mem:invalidate_write_test"}]- ;; Use snake_case for column name since deftransforms uses snake_case keys+ write-details {:db "mem:invalidate_write_test"}+ admin-details {:db "mem:invalidate_admin_test"}]+ ;; Use snake_case for column names since deftransforms uses snake_case keys(mt/with-temp [:model/Database database {:engine :h2:details read-details- :write_data_details write-details}]+ :write_data_details write-details+ :admin_details admin-details}](let [db-id (u/the-id database)default-cache-key [db-id :default]- write-cache-key [db-id :write-data]]- ;; Create both pools+ write-cache-key [db-id :write-data]+ admin-cache-key [db-id :admin]](sql-jdbc.conn/db->pooled-connection-spec database)(driver.conn/with-write-connection(sql-jdbc.conn/db->pooled-connection-spec database))+ (driver.conn/with-admin-connection+ (sql-jdbc.conn/db->pooled-connection-spec database))- (testing "both pools exist before invalidation"+ (testing "all pools exist before invalidation"(is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key))- (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key)))+ (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))+ (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool admin-cache-key)))- (testing "invalidate-pool-for-db! removes both pools"+ (testing "invalidate-pool-for-db! removes all pools"(sql-jdbc.conn/invalidate-pool-for-db! database)(is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key)))- (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))))))))))+ (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key)))+ (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool admin-cache-key))))))))))))(deftest ^:parallel c3p0-datasource-name-test(mt/test-drivers (mt/driver-select {:+parent :sql-jdbc})diff --git a/test/metabase/driver/sync_test.clj b/test/metabase/driver/sync_test.cljindex 69f7409fc0c..f19633d27d2 100644--- a/test/metabase/driver/sync_test.clj+++ b/test/metabase/driver/sync_test.clj@@ -2,7 +2,8 @@(:require[clojure.test :refer :all][metabase.driver.postgres]- [metabase.driver.sync :as driver.s])+ [metabase.driver.sync :as driver.s]+ [metabase.workspaces.core :as workspaces])(:import(clojure.lang ExceptionInfo)))@@ -45,3 +46,44 @@:schema-filters-patterns "x"}:engine :postgres}](is (driver.s/include-schema? db nil))))))++(deftest ^:parallel workspace-isolation-schemas-skipped-on-parent-instance-test+ (testing "On parent (non-workspace-mode) instances, include-schema? rejects mb__isolation_* (GHY-3489)"+ ;; `with-redefs` is unsafe in :parallel, but `workspace-mode?` is an EE+ ;; defenterprise whose OSS fallback returns false. Test ns is OSS, so the+ ;; default already simulates parent-instance behavior without redefs.+ (testing "with no user-configured filters (sync everything)"+ (is (not (driver.s/include-schema? nil nil "mb__isolation_abc_42"))+ "iso schema must be skipped on parent even when no filters are set")+ (is (driver.s/include-schema? nil nil "public")+ "non-iso schemas pass through unchanged"))+ (testing "with broad inclusion filters that WOULD match the iso prefix"+ (is (not (driver.s/include-schema? "mb*" nil "mb__isolation_abc_42"))+ "iso skip overrides an inclusion pattern that would otherwise match")+ (is (driver.s/include-schema? "mb*" nil "mb_user_schema")+ "user schemas starting with `mb_` (but NOT the iso prefix) still pass"))+ (testing "with exclusion filters covering OTHER schemas"+ (is (not (driver.s/include-schema? nil "public" "mb__isolation_abc_42"))+ "iso skip applies in addition to user-configured exclusions")+ (is (driver.s/include-schema? nil "other" "public")+ "non-iso schemas remain subject to user exclusion patterns only"))+ (testing "exact prefix match required"+ ;; The check uses `str/starts-with?` on the exact `mb__isolation_` prefix.+ ;; Customer schemas that happen to start with `mb_` (single underscore) or+ ;; `mb__` are safe -- only the specific iso prefix is skipped.+ (is (driver.s/include-schema? nil nil "mb_isolation_typo")+ "single-underscore variant is NOT the iso prefix; passes through")+ (is (driver.s/include-schema? nil nil "mb__customer_data")+ "double-underscore but different suffix is NOT the iso prefix; passes through"))))++(deftest ^:synchronized workspace-isolation-schemas-synced-on-child-instance-test+ ;; `with-redefs` to flip workspace-mode? to true -- not :parallel-safe.+ ;; Wrapping ns ^:synchronous would be cleaner but this single test stays local.+ (testing "On child (workspace-mode) instances, include-schema? DOES sync mb__isolation_* (GHY-3489)"+ (with-redefs [workspaces/workspace-mode? (constantly true)]+ (is (driver.s/include-schema? nil nil "mb__isolation_abc_42")+ "iso schemas must be synced on child -- table remapping needs them")+ (is (driver.s/include-schema? "mb*" nil "mb__isolation_abc_42")+ "inclusion patterns continue to apply normally on child")+ (is (not (driver.s/include-schema? nil "mb*" "mb__isolation_abc_42"))+ "exclusion patterns continue to apply normally on child -- user can still opt out"))))diff --git a/test/metabase/lib/metadata/transforming_provider_test.cljc b/test/metabase/lib/metadata/transforming_provider_test.cljcnew file mode 100644index 00000000000..e62c58dfbb9--- /dev/null+++ b/test/metabase/lib/metadata/transforming_provider_test.cljc@@ -0,0 +1,101 @@+(ns metabase.lib.metadata.transforming-provider-test+ (:require+ #?@(:cljs ([metabase.test-runner.assert-exprs.approximately-equal]))+ [clojure.test :refer [deftest is testing]]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.metadata.cached-provider :as lib.metadata.cached-provider]+ [metabase.lib.metadata.protocols :as lib.metadata.protocols]+ [metabase.lib.test-metadata :as meta]+ [metabase.lib.test-util.metadata-providers.mock :as mock]))++#?(:cljs+ (comment metabase.test-runner.assert-exprs.approximately-equal/keep-me))++(defn- tables-transform+ "A transform that applies `f` to each table, passes other metadata types through."+ [f]+ (fn [{metadata-type :lib/type} results]+ (if (= metadata-type :metadata/table)+ (into [] (map f) results)+ results)))++(deftest ^:parallel transforms-tables-test+ (testing "transform function is applied to table metadata"+ (let [mp (lib.metadata/transforming-metadata-provider+ (tables-transform #(assoc % :schema "transformed"))+ meta/metadata-provider)]+ (is (= "transformed" (:schema (lib.metadata/table mp (meta/id :venues))))))))++(deftest ^:parallel non-table-metadata-passes-through-test+ (testing "non-table metadata is passed through the transform unchanged"+ (let [mp (lib.metadata/transforming-metadata-provider+ (tables-transform #(assoc % :schema "transformed"))+ meta/metadata-provider)]+ (is (=? {:name "ID"}+ (lib.metadata/field mp (meta/id :venues :id)))))))++(deftest ^:parallel database-delegates-test+ (testing "database metadata delegates to parent"+ (let [mp (lib.metadata/transforming-metadata-provider+ (tables-transform identity)+ meta/metadata-provider)]+ (is (=? {:id (meta/id)}+ (lib.metadata/database mp))))))++(deftest ^:parallel setting-delegates-test+ (testing "settings delegate to parent"+ (let [mp (lib.metadata/transforming-metadata-provider+ (tables-transform identity)+ meta/metadata-provider)]+ ;; just verify it doesn't throw — test metadata may not have settings+ (is (nil? (lib.metadata.protocols/setting mp :nonexistent-setting))))))++(deftest ^:parallel identity-transform-test+ (testing "identity transform returns tables unchanged"+ (let [mp (lib.metadata/transforming-metadata-provider+ (fn [_metadata-spec results] results)+ meta/metadata-provider)]+ (is (=? {:name "VENUES"}+ (lib.metadata/table mp (meta/id :venues)))))))++(deftest ^:parallel equality-test+ (let [f (fn [_spec results] results)+ mp meta/metadata-provider]+ (testing "equal when f and parent are equal"+ (is (= (lib.metadata/transforming-metadata-provider f mp)+ (lib.metadata/transforming-metadata-provider f mp))))+ (testing "not equal when f differs"+ (is (not= (lib.metadata/transforming-metadata-provider f mp)+ (lib.metadata/transforming-metadata-provider (fn [_spec results] results) mp))))))++(deftest ^:parallel all-tables-transformed-test+ (testing "all tables from `tables` are transformed, not just single lookups"+ (let [mp (lib.metadata/transforming-metadata-provider+ (tables-transform #(assoc % :schema "ws"))+ meta/metadata-provider)]+ (is (every? #(= "ws" (:schema %)) (lib.metadata/tables mp))))))++(deftest ^:parallel cached-values-do-not-leak-through-test+ (testing "stale cached values from the parent don't bypass the transform"+ (let [base-mp (mock/mock-metadata-provider+ {:database {:id 1 :name "test-db" :engine :h2}+ :tables [{:id 1 :name "VENUES" :schema "PUBLIC" :db-id 1}]})+ cached-mp (lib.metadata.cached-provider/cached-metadata-provider base-mp)]+ ;; warm the parent cache+ (testing "parent cache has the original value"+ (is (= "PUBLIC" (:schema (lib.metadata/table cached-mp 1))))+ (is (= "VENUES" (:name (first (lib.metadata.protocols/cached-metadatas cached-mp :metadata/table [1]))))))+ ;; now wrap with a transform+ (let [transformed-mp (lib.metadata/transforming-metadata-provider+ (tables-transform #(assoc % :schema "workspace_schema" :name "workspace_venues"))+ cached-mp)]+ (testing "metadatas path returns transformed values"+ (let [t (lib.metadata/table transformed-mp 1)]+ (is (= "workspace_schema" (:schema t)))+ (is (= "workspace_venues" (:name t)))))+ (testing "cached-metadatas also returns transformed values (not stale parent cache)"+ ;; fetch once through metadatas to populate the transforming provider's own cache+ (lib.metadata/table transformed-mp 1)+ (let [cached (first (lib.metadata.protocols/cached-metadatas transformed-mp :metadata/table [1]))]+ (is (= "workspace_schema" (:schema cached)))+ (is (= "workspace_venues" (:name cached)))))))))diff --git a/test/metabase/permissions/models/data_permissions_test.clj b/test/metabase/permissions/models/data_permissions_test.cljindex ade07872999..312478d0b66 100644--- a/test/metabase/permissions/models/data_permissions_test.clj+++ b/test/metabase/permissions/models/data_permissions_test.clj@@ -409,6 +409,7 @@{table-id-3 :one-million-rows}}:perms/manage-database :yes:perms/transforms :no+ :perms/workspaces :no:perms/create-queries :no}}}(data-perms.graph/data-permissions-graph :group-id group-id-1)))diff --git a/test/metabase/permissions/models/permissions_group_test.clj b/test/metabase/permissions/models/permissions_group_test.cljindex 34996b74738..1eb06e2bcb4 100644--- a/test/metabase/permissions/models/permissions_group_test.clj+++ b/test/metabase/permissions/models/permissions_group_test.clj@@ -170,7 +170,8 @@:perms/download-results :no:perms/manage-table-metadata :no:perms/manage-database :no- :perms/transforms :no}}}+ :perms/transforms :no+ :perms/workspaces :no}}}(data-perms.graph/data-permissions-graph :group-id group-id :db-id db-id)))))))))(deftest hydrate-members-testsdiff --git a/test/metabase/permissions_rest/data_permissions/graph_test.clj b/test/metabase/permissions_rest/data_permissions/graph_test.cljindex bfc40261961..269a7a8f0dd 100644--- a/test/metabase/permissions_rest/data_permissions/graph_test.clj+++ b/test/metabase/permissions_rest/data_permissions/graph_test.clj@@ -52,6 +52,7 @@{:perms/view-data :blocked:perms/create-queries :no:perms/transforms :no+ :perms/workspaces :no:perms/download-results :no}}})))))(deftest update-db-level-create-queries-permissions!-test@@ -82,7 +83,8 @@{database-id-1{:perms/create-queries :query-builder:perms/view-data :unrestricted- :perms/transforms :no}}}+ :perms/transforms :no+ :perms/workspaces :no}}}{group-id-1{database-id-1@@ -90,7 +92,8 @@{group-id-1{database-id-1{:perms/create-queries :no- :perms/transforms :no}}}+ :perms/transforms :no+ :perms/workspaces :no}}}{group-id-1{database-id-1@@ -152,6 +155,7 @@{database-id-1{:perms/create-queries :no:perms/transforms :no+ :perms/workspaces :no:perms/view-data {"PUBLIC"{table-id-1 :unrestrictedtable-id-2 :legacy-no-self-service}@@ -159,6 +163,9 @@{table-id-3 :unrestricted}}}}};; Restoring full data access and native query permissions+ ;; Note: :perms/workspaces persists from the previous step (which forced it to :no when+ ;; create-queries was :no). It would be cleared explicitly by a graph entry setting+ ;; :workspaces :yes; we just exercise the cascade-clearance for transforms here.{group-id-1{database-id-1{:create-queries :query-builder-and-native@@ -168,7 +175,8 @@{database-id-1{:perms/create-queries :query-builder-and-native:perms/view-data :unrestricted- :perms/transforms :yes}}}+ :perms/transforms :yes+ :perms/workspaces :no}}};; Setting data access permissions at the schema-level{group-id-1@@ -180,6 +188,7 @@{database-id-1{:perms/create-queries :no:perms/transforms :no+ :perms/workspaces :no:perms/view-data {"PUBLIC"{table-id-1 :unrestrictedtable-id-2 :unrestricted}@@ -195,6 +204,7 @@{:perms/create-queries :no:perms/view-data :blocked:perms/transforms :no+ :perms/workspaces :no:perms/download-results :no}}})))))(deftest update-db-level-download-permissions!-testdiff --git a/test/metabase/test/util.clj b/test/metabase/test/util.cljindex 0d1b772ea75..cc241d70c7a 100644--- a/test/metabase/test/util.clj+++ b/test/metabase/test/util.clj@@ -393,7 +393,20 @@:email (u.random/random-email):password (u.random/random-name):date_joined (t/zoned-date-time)- :updated_at (t/zoned-date-time)})})+ :updated_at (t/zoned-date-time)})++ :model/Workspace+ (fn [_] (default-timestamped+ {:name (u.random/random-name)+ :creator_id (rasta-id)}))++ :model/WorkspaceDatabase+ (fn [_] (default-timestamped+ {:database_id (data/id)+ :database_details {}+ :input_schemas []+ :output_namespace ""+ :status :unprovisioned}))});; `with-temp` cleanup calls `t2/delete!` directly, which would hit our before-delete guard.;; Bind `*allow-direct-deletion*` so with-temp cleanup works.diff --git a/test/metabase/warehouse_schema_rest/api/table_test.clj b/test/metabase/warehouse_schema_rest/api/table_test.cljindex 82547a70546..bc0e4757f0d 100644--- a/test/metabase/warehouse_schema_rest/api/table_test.clj+++ b/test/metabase/warehouse_schema_rest/api/table_test.clj@@ -425,7 +425,7 @@(is (= (merge(-> (table-defaults)(dissoc :segments :field_values :metrics :measures :updated_at)- (update :db merge (select-keys (mt/db) [:details :write_data_details])))+ (update :db merge (select-keys (mt/db) [:details :write_data_details :admin_details])))(t2/hydrate (t2/select-one [:model/Table :id :schema :name :created_at :initial_sync_status] :id (u/the-id table)):pk_field :collection){:description "What a nice table!"diff --git a/test/metabase/warehouses_rest/api_test.clj b/test/metabase/warehouses_rest/api_test.cljindex 9662d7b4077..44101798a84 100644--- a/test/metabase/warehouses_rest/api_test.clj+++ b/test/metabase/warehouses_rest/api_test.clj@@ -160,7 +160,7 @@(testing "DB details visibility"(testing "Regular users should not see DB details"(is (= (-> (db-details)- (dissoc :details :write_data_details :schedules))+ (dissoc :details :write_data_details :admin_details :schedules))(-> (mt/user-http-request :rasta :get 200 (format "database/%d" (mt/id)))(dissoc :schedules :can_upload)))))(testing "Superusers should see DB details"@@ -752,7 +752,7 @@(deftest ^:parallel fetch-database-metadata-test(testing "GET /api/database/:id/metadata"- (is (= (merge (dissoc (db-details) :details :write_data_details :router_user_attribute)+ (is (= (merge (dissoc (db-details) :details :write_data_details :admin_details :router_user_attribute){:engine "h2":name "test-data (h2)":features (map u/qualified-name (driver.u/features :h2 (mt/db)))@@ -1002,7 +1002,7 @@(testing "Database details/settings *should not* come back for Rasta since she's not a superuser"(let [expected-keys (-> #{:features :native_permissions :can_upload :router_user_attribute :transforms_permissions}(into (keys (t2/select-one :model/Database :id (mt/id))))- (disj :details :write_data_details))]+ (disj :details :write_data_details :admin_details))](doseq [db (:data (mt/user-http-request :rasta :get 200 "database"))](testing (format "Database %s %d %s" (:engine db) (u/the-id db) (pr-str (:name db)))(is (= expected-keys@@ -2867,3 +2867,172 @@(mt/user-http-request :crowberto :put 400 (format "database/%d" router-id){:write_data_details {:host "write-host":write-data-connection true}}))))))))++;;; ----------------------------------------- admin_details tests -----------------------------------------++(deftest ^:parallel upsert-sensitive-fields-admin-details-test+ (testing "upsert-sensitive-fields works with :admin_details key"+ (is (= {:host "localhost"+ :port 5432+ :password "new-password"}+ (#'api.database/upsert-sensitive-fields+ {:engine :h2+ :id (mt/id)+ :details {:host "localhost" :port 5432 :password "main-pass"}+ :admin_details {:host "localhost" :port 5432 :password "admin-pass"}}+ {:host "localhost"+ :port 5432+ :password "new-password"}+ :admin_details)))+ (testing "protected passwords are replaced from original"+ (is (= {:host "localhost"+ :port 5432+ :password "admin-pass"}+ (#'api.database/upsert-sensitive-fields+ {:engine :h2+ :id (mt/id)+ :details {:host "localhost" :port 5432 :password "main-pass"}+ :admin_details {:host "localhost" :port 5432 :password "admin-pass"}}+ {:host "localhost"+ :port 5432+ :password secret/protected-password}+ :admin_details))))))++(deftest get-database-admin-details-test+ (testing "GET /api/database/:id"+ (testing "Superusers see admin_details with sensitive fields redacted"+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :details {:host "localhost"}+ :admin_details {:host "admin-host"+ :password "secret-admin-pass"}}]+ (let [response (mt/user-http-request :crowberto :get 200 (format "database/%d" db-id))]+ (is (= "admin-host" (get-in response [:admin_details :host])))+ (is (= secret/protected-password (get-in response [:admin_details :password]))))))+ (testing "Regular users do not see admin_details"+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :details {:host "localhost"}+ :admin_details {:host "admin-host"}}]+ (let [response (mt/user-http-request :rasta :get 200 (format "database/%d" db-id))]+ (is (not (contains? response :admin_details)))+ (is (not (contains? response :details))))))))++(deftest update-database-admin-details-test+ (testing "PUT /api/database/:id with admin_details"+ (testing "Superusers can set admin_details"+ (mt/with-premium-features #{:workspaces}+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :details {:host "localhost"}}]+ (with-redefs [driver/can-connect? (constantly true)]+ (let [response (mt/user-http-request :crowberto :put 200 (format "database/%d" db-id)+ {:admin_details {:host "admin-host"+ :password "admin-pass"+ :admin-connection true}})]+ (is (= "admin-host" (get-in response [:admin_details :host])))+ (is (= secret/protected-password (get-in response [:admin_details :password])))+ (let [db (t2/select-one :model/Database :id db-id)]+ (is (= {:host "admin-host" :password "admin-pass" :admin-connection true}+ (:admin_details db)))))))))+ (testing "Superusers can clear admin_details by setting it to nil"+ (mt/with-premium-features #{:workspaces}+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :details {:host "localhost"}+ :admin_details {:host "admin-host"}}]+ (with-redefs [driver/can-connect? (constantly true)]+ (mt/user-http-request :crowberto :put 200 (format "database/%d" db-id)+ {:admin_details nil})+ (let [db (t2/select-one :model/Database :id db-id)]+ (is (nil? (:admin_details db))))))))+ (testing "Sensitive fields are preserved when protected-password is sent"+ (mt/with-premium-features #{:workspaces}+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :details {:host "localhost"}+ :admin_details {:host "admin-host"+ :password "original-pass"}}]+ (with-redefs [driver/can-connect? (constantly true)]+ (mt/user-http-request :crowberto :put 200 (format "database/%d" db-id)+ {:admin_details {:host "new-admin-host"+ :password secret/protected-password+ :admin-connection true}})+ (let [db (t2/select-one :model/Database :id db-id)]+ (is (= "new-admin-host" (get-in db [:admin_details :host])))+ (is (= "original-pass" (get-in db [:admin_details :password]))))))))+ (testing "Returns 402 without :workspaces feature"+ (with-redefs [premium-features/has-feature? (constantly false)]+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :details {:host "localhost"}}]+ (mt/user-http-request :crowberto :put 402 (format "database/%d" db-id)+ {:admin_details {:host "admin-host"}}))))))++(deftest put-validates-admin-details-connection-test+ (when config/ee-available?+ (testing "PUT /api/database/:id returns 400 when admin connection test fails"+ (mt/with-premium-features #{:workspaces}+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :details {:host "localhost"}}]+ (with-redefs [driver/can-connect? (fn [_engine details]+ (if (:admin-connection details)+ (throw (Exception. "Admin connection failed"))+ true))]+ (let [response (mt/user-http-request :crowberto :put 400 (format "database/%d" db-id)+ {:admin_details {:host "totally-bogus-host"+ :admin-connection true}})]+ (is (= "Admin connection failed" (:message response))))))))))++(deftest admin-details-guardrails-test+ (testing "PUT /api/database/:id admin_details guardrails"+ (testing "admin-connection must not be truthy in details"+ (mt/with-premium-features #{:workspaces}+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :details {:host "localhost"}}]+ (is (= "admin-connection must not be set in details"+ (mt/user-http-request :crowberto :put 400 (format "database/%d" db-id)+ {:details {:host "localhost"+ :admin-connection true}}))))))+ (testing "admin-connection must be truthy in admin_details"+ (mt/with-premium-features #{:workspaces}+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :details {:host "localhost"}}]+ (is (= "admin-connection must be set in admin_details"+ (mt/user-http-request :crowberto :put 400 (format "database/%d" db-id)+ {:admin_details {:host "admin-host"}}))))))+ (testing "Destination-database must be false in admin_details"+ (mt/with-premium-features #{:workspaces}+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :details {:host "localhost"}}]+ (is (= "destination-database must be false in admin_details"+ (mt/user-http-request :crowberto :put 400 (format "database/%d" db-id)+ {:admin_details {:host "admin-host"+ :admin-connection true+ :destination-database true}}))))))+ (testing "Fields hidden for admin connections must not be in admin_details"+ (mt/with-premium-features #{:workspaces}+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :details {:host "localhost"}}]+ (is (str/includes?+ (mt/user-http-request :crowberto :put 400 (format "database/%d" db-id)+ {:admin_details {:host "admin-host"+ :admin-connection true+ :auto_run_queries true}})+ "admin_details must not contain fields hidden for admin connections")))))+ (testing "Cannot set admin_details on a destination database"+ (mt/with-premium-features #{:workspaces}+ (mt/with-temp [:model/Database {router-id :id} {:engine :h2+ :details {:host "localhost"}}+ :model/Database {dest-id :id} {:engine :h2+ :details {:host "localhost"}+ :router_database_id router-id}]+ (is (= "Cannot configure an admin connection on a destination database"+ (mt/user-http-request :crowberto :put 400 (format "database/%d" dest-id)+ {:admin_details {:host "admin-host"+ :admin-connection true}}))))))+ (testing "Cannot set admin_details on a router database"+ (mt/with-premium-features #{:workspaces}+ (mt/with-temp [:model/Database {router-id :id} {:engine :h2+ :details {:host "localhost"}}+ :model/Database {_dest-id :id} {:engine :h2+ :details {:host "localhost"}+ :router_database_id router-id}]+ (is (= "Cannot configure an admin connection on a router database"+ (mt/user-http-request :crowberto :put 400 (format "database/%d" router-id)+ {:admin_details {:host "admin-host"+ :admin-connection true}}))))))))diff --git a/test_resources/remapping_corpus/postgres.sql b/test_resources/remapping_corpus/postgres.sqlnew file mode 100644index 00000000000..3d2274877c6--- /dev/null+++ b/test_resources/remapping_corpus/postgres.sql@@ -0,0 +1,216 @@+-- Workspace remapping test corpus: Postgres dialect+--+-- Format: SQL comment preamble followed by query. Separated by blank line + "-- ;;;;".+--+-- Preamble directives:+-- -- remap: <from_schema>.<from_table> <to_schema>.<to_table>+-- -- tags: <comma-separated tags for filtering>+-- -- expect: ok | parse-error+--+-- The test runner parses each entry, applies remappings via sql-tools/replace-names,+-- then verifies no "from" references remain in the output.++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: simple, select+-- expect: ok+SELECT * FROM public.orders++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: simple, select, where+-- expect: ok+SELECT id, total FROM public.orders WHERE created_at > '2024-01-01'++-- ;;;;+-- remap: public.orders temporary.ords+-- remap: public.customers temporary.custs+-- tags: join+-- expect: ok+SELECT o.id, c.name+FROM public.orders o+JOIN public.customers c ON c.id = o.customer_id++-- ;;;;+-- remap: public.orders temporary.ords+-- remap: public.customers temporary.custs+-- tags: join, left-join+-- expect: ok+SELECT o.id, c.name, c.email+FROM public.orders o+LEFT JOIN public.customers c ON c.id = o.customer_id+WHERE o.total > 100++-- ;;;;+-- remap: public.orders temporary.ords+-- remap: public.line_items temporary.li+-- tags: join, multiple-joins+-- expect: ok+SELECT o.id, li.product_id, li.quantity+FROM public.orders o+JOIN public.line_items li ON li.order_id = o.id+WHERE o.status = 'completed'++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: subquery+-- expect: ok+SELECT *+FROM public.orders+WHERE customer_id IN (SELECT id FROM public.orders WHERE total > 1000)++-- ;;;;+-- remap: public.orders temporary.ords+-- remap: public.customers temporary.custs+-- tags: subquery, correlated+-- expect: ok+SELECT c.name,+ (SELECT COUNT(*) FROM public.orders o WHERE o.customer_id = c.id) AS order_count+FROM public.customers c++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: cte+-- expect: ok+WITH recent_orders AS (+ SELECT * FROM public.orders WHERE created_at > '2024-01-01'+)+SELECT * FROM recent_orders WHERE total > 50++-- ;;;;+-- remap: public.orders temporary.ords+-- remap: public.customers temporary.custs+-- tags: cte, multiple-ctes+-- expect: ok+WITH+ big_orders AS (+ SELECT * FROM public.orders WHERE total > 500+ ),+ vip_customers AS (+ SELECT DISTINCT customer_id FROM big_orders+ )+SELECT c.name, c.email+FROM public.customers c+JOIN vip_customers v ON v.customer_id = c.id++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: cte, recursive+-- expect: ok+WITH RECURSIVE order_chain AS (+ SELECT id, parent_order_id, total+ FROM public.orders+ WHERE parent_order_id IS NULL+ UNION ALL+ SELECT o.id, o.parent_order_id, o.total+ FROM public.orders o+ JOIN order_chain oc ON oc.id = o.parent_order_id+)+SELECT * FROM order_chain++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: aggregate, group-by+-- expect: ok+SELECT customer_id, COUNT(*) AS cnt, SUM(total) AS revenue+FROM public.orders+GROUP BY customer_id+HAVING SUM(total) > 1000++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: window-function+-- expect: ok+SELECT id, total,+ ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY created_at DESC) AS rn,+ SUM(total) OVER (PARTITION BY customer_id) AS customer_total+FROM public.orders++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: union+-- expect: ok+SELECT id, total FROM public.orders WHERE status = 'completed'+UNION ALL+SELECT id, total FROM public.orders WHERE status = 'refunded'++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: alias, schema-qualified+-- expect: ok+SELECT o.id AS order_id, o.total AS order_total+FROM public.orders AS o+WHERE o.created_at >= CURRENT_DATE - INTERVAL '30 days'++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: exists+-- expect: ok+SELECT *+FROM public.orders o+WHERE EXISTS (+ SELECT 1 FROM public.orders o2+ WHERE o2.customer_id = o.customer_id+ AND o2.id != o.id+)++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: case-expression+-- expect: ok+SELECT id,+ CASE+ WHEN total > 1000 THEN 'large'+ WHEN total > 100 THEN 'medium'+ ELSE 'small'+ END AS size_category+FROM public.orders++-- ;;;;+-- remap: public.orders temporary.ords+-- remap: public.customers temporary.custs+-- remap: public.products temporary.prods+-- tags: complex, multiple-joins, subquery, aggregate+-- expect: ok+SELECT c.name,+ p.category,+ COUNT(DISTINCT o.id) AS order_count,+ SUM(o.total) AS total_revenue+FROM public.orders o+JOIN public.customers c ON c.id = o.customer_id+JOIN public.products p ON p.id = o.product_id+WHERE o.created_at >= '2024-01-01'+ AND o.status IN ('completed', 'shipped')+ AND c.id IN (SELECT customer_id FROM public.orders GROUP BY customer_id HAVING COUNT(*) > 5)+GROUP BY c.name, p.category+ORDER BY total_revenue DESC+LIMIT 100++-- ;;;;+-- remap: public.events temporary.evts+-- tags: lateral-join, postgres-specific+-- expect: ok+SELECT e.id, latest.*+FROM public.events e,+LATERAL (+ SELECT * FROM public.events e2+ WHERE e2.user_id = e.user_id+ ORDER BY e2.created_at DESC+ LIMIT 3+) latest++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: insert-select+-- expect: ok+INSERT INTO public.orders (customer_id, total, status)+SELECT customer_id, SUM(total), 'merged'+FROM public.orders+WHERE status = 'pending'+GROUP BY customer_id++-- ;;;;+-- remap: public.orders temporary.ords+-- tags: unqualified+-- expect: ok+SELECT * FROM orders WHERE total > 100