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
Remove the workspaces feature (#78617)
Alexander Polyankin1ec45cf46f66
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 531f560ef94..eeeec6b9388 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -55,9 +55,9 @@;; Set of `:model/X` keywords defined by this module that it allows others to reference. By default all are private.;; If you know what you're doing, we support an :any keyword, but let's prefer explicit sets for now.;;-;; enterprise/workspaces+;; transforms;; {...-;; :model-exports #{:model/Workspace :model/WorkspaceMergeTransform}}+;; :model-exports #{:model/Transform :model/TransformRun}};;;; ## `model-imports`:;;@@ -109,8 +109,7 @@searchsettingsutil- warehouses- workspaces}+ warehouses}:model-exports #{:model/Action}:model-imports #{:model/Card:model/Collection@@ -851,10 +850,8 @@metabase.driver.sql-jdbc.executemetabase.driver.sql-jdbc.execute.diagnosticmetabase.driver.sql-jdbc.sync- ;; TODO (lbrdnk 2025/12/17): We should revisit addition of the following line. I've added it to make- ;; tests pass as we are using it directly with ee/workspaces module. I believe later, when workspaces- ;; are in better state quality wise, it should become part of the driver api and that should be used- ;; instead of calling directly into the normalize module.+ ;; TODO (lbrdnk 2025/12/17): We should revisit addition of the following line. Ideally this would+ ;; become part of the driver api rather than callers reaching directly into the normalize module.metabase.driver.sql.normalizemetabase.driver.sql.query-processormetabase.driver.sql.util@@ -878,10 +875,7 @@tracingutilwarehouse-schema- warehouses- ;; `metabase.driver.sync/include-schema?` skips workspace iso schemas- ;; on parent (non-workspace-mode) instances. See GHY-3489.- workspaces}+ warehouses}:model-imports #{:model/Database :model/Field :model/Table}}driver-api@@ -1419,8 +1413,7 @@tasktask-historytracing- util- workspaces}+ util}:model-exports #{:model/PersistedInfo}:model-imports #{:model/Card :model/Database}}@@ -1880,7 +1873,6 @@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@@ -1929,8 +1921,7 @@systemtracingusers- util- workspaces}+ util}:model-imports #{:model/Card:model/DashboardCardSeries:model/Database@@ -2302,8 +2293,7 @@tracingutilwarehouse-schema- warehouses- workspaces}+ warehouses}:model-imports #{:model/Database:model/Field:model/FieldValues@@ -2486,8 +2476,7 @@premium-featuresquery-processorsync- util- workspaces}+ util}:friends #{transformsenterprise/transforms-python}}@@ -2563,8 +2552,7 @@settingssyncutil- warehouse-schema- workspaces}+ warehouse-schema}:model-imports #{:model/Card:model/Database:model/Field@@ -2817,13 +2805,6 @@: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@@ -2897,8 +2878,7 @@setupsyncusers- util- enterprise/workspaces}+ util}:model-imports #{:model/ApiKey :model/Database :model/User}}enterprise/advanced-permissions@@ -2935,7 +2915,7 @@enterprise/scimenterprise/semantic-searchenterprise/sso}- :model-imports #{:model/Database :model/Sandbox :model/Workspace}}+ :model-imports #{:model/Database :model/Sandbox}}enterprise/api{:team "UX West"@@ -3172,8 +3152,7 @@settingsutilwarehouse-schema- warehouses- workspaces}+ warehouses}:model-exports #{:model/DatabaseRouter}:model-imports #{:model/Database :model/Transform}}@@ -3775,31 +3754,7 @@premium-featuresuploadutil}- :model-imports #{:model/Database :model/Table}}-- enterprise/workspaces- {:team "Graphy"- :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- sample-data- settings- sql-tools- util- workspaces}- :model-exports #{:model/Workspace}- :model-imports #{:model/Database- :model/Table- :model/User}}}+ :model-imports #{:model/Database :model/Table}}};; 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/ratchets.edn b/.clj-kondo/ratchets.ednindex 7024ebb56cd..b20b8362c9e 100644--- a/.clj-kondo/ratchets.edn+++ b/.clj-kondo/ratchets.edn@@ -9,17 +9,17 @@:case-symbol-test 1:clojure-lsp/unused-public-var 9:def-fn 1- :deprecated-namespace 97+ :deprecated-namespace 95:deprecated-var 81:discouraged-java-method 3- :discouraged-namespace 81- :discouraged-var 106+ :discouraged-namespace 80+ :discouraged-var 100:equals-true 1:main-without-gen-class 1:metabase/defsetting-namespace 7:metabase/disallow-hardcoded-driver-names-in-tests 34:metabase/discourage-millis-duration 1- :metabase/i-like-making-cams-eyes-bleed-with-horrifically-long-tests 14+ :metabase/i-like-making-cams-eyes-bleed-with-horrifically-long-tests 13:metabase/modules 8:metabase/namespace-name 4:metabase/prefer-with-dynamic-fn-redefs 27diff --git a/e2e/support/helpers/api/index.ts b/e2e/support/helpers/api/index.tsindex 9a018c11418..522efdcb689 100644--- a/e2e/support/helpers/api/index.ts+++ b/e2e/support/helpers/api/index.ts@@ -54,4 +54,3 @@ export {updateSetting,} from "./updateSetting";export { updateSnippet } from "./updateSnippet";-export { clearWorkspaceInstanceConfig } from "./setWorkspaceInstanceConfig";diff --git a/e2e/support/helpers/api/setWorkspaceInstanceConfig.ts b/e2e/support/helpers/api/setWorkspaceInstanceConfig.tsdeleted file mode 100644index 4c9a66c5610..00000000000--- a/e2e/support/helpers/api/setWorkspaceInstanceConfig.ts+++ /dev/null@@ -1,3 +0,0 @@-export function clearWorkspaceInstanceConfig() {- return cy.request("DELETE", "/api/ee/workspace-instance/current");-}diff --git a/e2e/support/helpers/e2e-workspace-helpers.ts b/e2e/support/helpers/e2e-workspace-helpers.tsdeleted file mode 100644index cdc2e5ecc52..00000000000--- a/e2e/support/helpers/e2e-workspace-helpers.ts+++ /dev/null@@ -1,105 +0,0 @@-import yaml from "js-yaml";--import type { AdvancedConfig } 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: ({ primary = true }: { primary?: boolean } = {}) =>- WorkspaceListPage.get().findByRole("button", {- name: primary ? "Create a workspace" : "New",- }),- setupInstanceButton: () =>- WorkspaceListPage.get().findByRole("button", {- name: "Upload a workspace config",- }),- workspaceList: () => WorkspaceListPage.get().findByTestId("workspace-list"),- workspace: (name: string) =>- WorkspaceListPage.get().findByRole("region", { name }),- workspaceMenuButton: (name: string) =>- WorkspaceListPage.workspace(name).findByRole("button", {- name: "Workspace options",- }),- renameMenuItem: () => cy.findByRole("menuitem", { name: "Rename" }),- downloadConfigMenuItem: () =>- cy.findByRole("menuitem", { name: /Download config\.yml/ }),- deleteMenuItem: () => cy.findByRole("menuitem", { name: "Delete" }),-};--export const NewWorkspaceModal = {- get: () => modal(),- nameInput: () => NewWorkspaceModal.get().findByLabelText("Name"),- databaseCheckbox: (name: string) =>- NewWorkspaceModal.get().findByRole("checkbox", { name }),- createButton: () =>- NewWorkspaceModal.get().findByRole("button", { name: "Create workspace" }),- cancelButton: () =>- NewWorkspaceModal.get().findByRole("button", { name: "Cancel" }),-};--export const RenameWorkspaceModal = {- get: () => modal(),- nameInput: () => RenameWorkspaceModal.get().findByLabelText("Name"),- renameButton: () =>- RenameWorkspaceModal.get().findByRole("button", { name: "Rename" }),- cancelButton: () =>- RenameWorkspaceModal.get().findByRole("button", { name: "Cancel" }),-};--export const DeleteWorkspaceModal = {- get: () => modal(),- confirmButton: () =>- DeleteWorkspaceModal.get().findByRole("button", {- name: "Delete workspace",- }),- cancelButton: () =>- DeleteWorkspaceModal.get().findByRole("button", { name: "Cancel" }),-};--export const SetupWorkspaceModal = {- get: () => modal(),- configInput: () => SetupWorkspaceModal.get().get('input[type="file"]'),- setupButton: () =>- SetupWorkspaceModal.get().findByRole("button", { name: "Set up" }),- cancelButton: () =>- SetupWorkspaceModal.get().findByRole("button", { name: "Cancel" }),- uploadConfig: (config: AdvancedConfig) => {- SetupWorkspaceModal.configInput().selectFile(- {- contents: Cypress.Buffer.from(yaml.dump(config)),- fileName: "config.yml",- mimeType: "application/yaml",- },- { force: true },- );- },-};--export const CurrentWorkspacePage = {- get: () => cy.findByTestId("current-workspace-page"),- visit: () => {- cy.visit("/data-studio/workspaces");- CurrentWorkspacePage.get().should("be.visible");- },- database: (name: string) =>- CurrentWorkspacePage.get().findByRole("region", { name }),- remappingRow: (canonicalName: string) =>- CurrentWorkspacePage.get().findByText(canonicalName),- leaveButton: () =>- CurrentWorkspacePage.get().findByRole("button", {- name: "Leave workspace",- }),-};--export const LeaveWorkspaceModal = {- get: () => modal(),- confirmButton: () =>- LeaveWorkspaceModal.get().findByRole("button", { name: "Leave workspace" }),- cancelButton: () =>- LeaveWorkspaceModal.get().findByRole("button", { name: "Cancel" }),-};diff --git a/e2e/support/helpers/index.ts b/e2e/support/helpers/index.tsindex 1d4ffd84e7a..bf82f36ce08 100644--- a/e2e/support/helpers/index.ts+++ b/e2e/support/helpers/index.ts@@ -66,5 +66,4 @@ 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.tsdeleted file mode 100644index 42fb7f9845a..00000000000--- a/e2e/test/scenarios/workspaces/workspace-instance.cy.spec.ts+++ /dev/null@@ -1,286 +0,0 @@-import {- QA_DB_CREDENTIALS,- QA_MYSQL_PORT,- QA_POSTGRES_PORT,- WRITABLE_DB_ID,-} from "e2e/support/cypress_data";-import type { AdvancedConfig } from "metabase-types/api";--const { H } = cy;--const WORKSPACE_NAME = "E2E workspace instance";-const ROW_COUNT = 3;--const POSTGRES_DB_NAME = "Writable Postgres12";-const POSTGRES_INPUT_SCHEMA = "Domestic";-const POSTGRES_OUTPUT_SCHEMA = "mb__isolation";-const POSTGRES_SOURCE_TABLE = "Animals";-const POSTGRES_TARGET_TABLE = "transform_table";-const POSTGRES_TARGET_TABLE_DISPLAY_NAME = "Transform Table";--const POSTGRES_CONFIG: AdvancedConfig = {- version: 1,- config: {- databases: [- {- name: POSTGRES_DB_NAME,- engine: "postgres",- details: {- host: QA_DB_CREDENTIALS.host,- port: QA_POSTGRES_PORT,- dbname: "writable_db",- user: QA_DB_CREDENTIALS.user,- password: QA_DB_CREDENTIALS.password,- ssl: false,- },- },- ],- workspace: {- name: WORKSPACE_NAME,- databases: {- [POSTGRES_DB_NAME]: {- input_schemas: [POSTGRES_INPUT_SCHEMA],- output: { schema: POSTGRES_OUTPUT_SCHEMA },- },- },- },- },-};--const MYSQL_DB_NAME = "Writable MySQL8";-const MYSQL_OUTPUT_DATABASE = "mb__isolation";-const MYSQL_SOURCE_TABLE = "scoreboard_actions";-const MYSQL_TARGET_TABLE = "transform_table";-const MYSQL_TARGET_TABLE_DISPLAY_NAME = "Transform Table";--const MYSQL_CONFIG: AdvancedConfig = {- version: 1,- config: {- databases: [- {- name: MYSQL_DB_NAME,- engine: "mysql",- details: {- host: QA_DB_CREDENTIALS.host,- port: QA_MYSQL_PORT,- dbname: "writable_db",- user: "root",- password: QA_DB_CREDENTIALS.password,- ssl: false,- },- },- ],- workspace: {- name: WORKSPACE_NAME,- databases: {- [MYSQL_DB_NAME]: {- input_schemas: [],- output: { db: MYSQL_OUTPUT_DATABASE },- },- },- },- },-};--describe("scenarios > workspaces > workspace instance", () => {- describe("postgres", () => {- 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 ${POSTGRES_OUTPUT_SCHEMA}`,- "postgres",- );- H.resyncDatabase({ dbId: WRITABLE_DB_ID });- });-- afterEach(() => {- H.clearWorkspaceInstanceConfig();- H.queryWritableDB(- `DROP SCHEMA IF EXISTS ${POSTGRES_OUTPUT_SCHEMA} CASCADE`,- "postgres",- );- });-- it("runs a transform, surfaces the remapping on the instance page, and queries the remapped table", () => {- cy.log("set up the workspace");- H.WorkspaceListPage.visit();- H.WorkspaceListPage.setupInstanceButton().click();- H.SetupWorkspaceModal.uploadConfig(POSTGRES_CONFIG);- H.SetupWorkspaceModal.setupButton().click();- H.CurrentWorkspacePage.get().should("be.visible");-- cy.log("create and run a transform via the API");- createAndRunTransform({- sourceTable: POSTGRES_SOURCE_TABLE,- sourceSchema: POSTGRES_INPUT_SCHEMA,- targetTable: POSTGRES_TARGET_TABLE,- targetSchema: POSTGRES_INPUT_SCHEMA,- });-- cy.log("instance page shows the remapping for the transform target");- H.CurrentWorkspacePage.visit();- H.CurrentWorkspacePage.database(POSTGRES_DB_NAME)- .should(- "contain.text",- `${POSTGRES_INPUT_SCHEMA}/${POSTGRES_TARGET_TABLE}`,- )- .and(- "contain.text",- `${POSTGRES_OUTPUT_SCHEMA}/${POSTGRES_INPUT_SCHEMA}__${POSTGRES_TARGET_TABLE}`,- );-- cy.log("native query is rewritten to workspace table");- H.startNewNativeQuestion({- database: WRITABLE_DB_ID,- query: `SELECT * FROM "${POSTGRES_INPUT_SCHEMA}"."${POSTGRES_TARGET_TABLE}"`,- });- H.runNativeQuery();- H.assertQueryBuilderRowCount(ROW_COUNT);-- cy.log("mbql query is rewritten to workspace table");- H.startNewQuestion();- H.miniPicker().findByText(POSTGRES_DB_NAME).click();- H.miniPicker().findByText(POSTGRES_INPUT_SCHEMA).click();- H.miniPicker().findByText(POSTGRES_TARGET_TABLE_DISPLAY_NAME).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 "${POSTGRES_OUTPUT_SCHEMA}"."${POSTGRES_INPUT_SCHEMA}__${POSTGRES_TARGET_TABLE}"`,- "postgres",- ).then((result) => {- expect(Number(result.rows[0].count)).to.eq(ROW_COUNT);- });-- cy.log("leave the workspace through the UI");- H.CurrentWorkspacePage.visit();- H.CurrentWorkspacePage.leaveButton().click();- H.LeaveWorkspaceModal.confirmButton().click();- H.WorkspaceListPage.setupInstanceButton().should("be.visible");- });- });-- describe("mysql", () => {- beforeEach(() => {- H.restore("mysql-writable");- cy.signInAsAdmin();- H.activateToken("bleeding-edge");- H.resetTestTable({ type: "mysql", table: MYSQL_SOURCE_TABLE });- H.queryWritableDB(- `DROP DATABASE IF EXISTS ${MYSQL_OUTPUT_DATABASE}`,- "mysql",- );- H.queryWritableDB(`CREATE DATABASE ${MYSQL_OUTPUT_DATABASE}`, "mysql");- H.resyncDatabase({ dbId: WRITABLE_DB_ID });- });-- afterEach(() => {- H.clearWorkspaceInstanceConfig();- H.queryWritableDB(- `DROP DATABASE IF EXISTS ${MYSQL_OUTPUT_DATABASE}`,- "mysql",- );- });-- it("runs a transform, surfaces the remapping on the instance page, and queries the remapped table", () => {- cy.log("set up the workspace");- H.WorkspaceListPage.visit();- H.WorkspaceListPage.setupInstanceButton().click();- H.SetupWorkspaceModal.uploadConfig(MYSQL_CONFIG);- H.SetupWorkspaceModal.setupButton().click();- H.CurrentWorkspacePage.get().should("be.visible");-- cy.log("create and run a transform via the API");- createAndRunTransform({- sourceTable: MYSQL_SOURCE_TABLE,- sourceSchema: null,- targetTable: MYSQL_TARGET_TABLE,- targetSchema: null,- });-- cy.log("instance page shows the remapping for the transform target");- H.CurrentWorkspacePage.visit();- H.CurrentWorkspacePage.database(MYSQL_DB_NAME)- .should("contain.text", MYSQL_TARGET_TABLE)- .and(- "contain.text",- `${MYSQL_OUTPUT_DATABASE}/__${MYSQL_TARGET_TABLE}`,- );-- cy.log("native query is rewritten to workspace table");- H.startNewNativeQuestion({- database: WRITABLE_DB_ID,- query: `SELECT * FROM \`${MYSQL_TARGET_TABLE}\``,- });- H.runNativeQuery();- H.assertQueryBuilderRowCount(ROW_COUNT);-- cy.log("mbql query is rewritten to workspace table");- H.startNewQuestion();- H.miniPicker().findByText(MYSQL_DB_NAME).click();- H.miniPicker().findByText(MYSQL_TARGET_TABLE_DISPLAY_NAME).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 \`${MYSQL_OUTPUT_DATABASE}\`.\`__${MYSQL_TARGET_TABLE}\``,- "mysql",- ).then((result) => {- expect(Number(result.rows[0].count)).to.eq(ROW_COUNT);- });-- cy.log("leave the workspace through the UI");- H.CurrentWorkspacePage.visit();- H.CurrentWorkspacePage.leaveButton().click();- H.LeaveWorkspaceModal.confirmButton().click();- H.WorkspaceListPage.setupInstanceButton().should("be.visible");- });- });-});--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.tsdeleted file mode 100644index 407e3830b0d..00000000000--- a/e2e/test/scenarios/workspaces/workspace-manager.cy.spec.ts+++ /dev/null@@ -1,126 +0,0 @@-import path from "path";--import { WRITABLE_DB_ID } from "e2e/support/cypress_data";--const { H } = cy;--const CONFIG_FILENAME = "config.yml";--describe("scenarios > workspaces > workspace manager", () => {- describe("postgres (with schemas)", () => {- const POSTGRES_DB_NAME = "Writable Postgres12";- const PG_SCHEMA_A = "Domestic";- const PG_SCHEMA_B = "Wild";- const workspaceName = "PG Workspace";- const renamedName = "Renamed 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 });- enableWorkspaces(WRITABLE_DB_ID);- cy.deleteDownloadsFolder();- });-- it("creates, renames, downloads config, and deletes a workspace as admin", () => {- cy.log(- "create the workspace from the empty state, databases added at once",- );- H.WorkspaceListPage.visit();- H.WorkspaceListPage.newButton().click();- H.NewWorkspaceModal.nameInput().type(workspaceName);- H.NewWorkspaceModal.databaseCheckbox(POSTGRES_DB_NAME).click();- H.NewWorkspaceModal.createButton().click();-- cy.log("the database (with all of its schemas) is attached at once");- H.WorkspaceListPage.workspace(workspaceName)- .should("be.visible")- .and("contain.text", POSTGRES_DB_NAME);-- cy.log("rename the workspace via the menu");- H.WorkspaceListPage.workspaceMenuButton(workspaceName).click();- H.WorkspaceListPage.renameMenuItem().click();- H.RenameWorkspaceModal.nameInput().clear().type(renamedName);- H.RenameWorkspaceModal.renameButton().click();- H.WorkspaceListPage.workspace(renamedName).should("be.visible");-- cy.log("download the workspace config via the menu");- H.WorkspaceListPage.workspaceMenuButton(renamedName).click();- H.WorkspaceListPage.downloadConfigMenuItem().click();- cy.verifyDownload(CONFIG_FILENAME, { timeout: 15000 });- readConfig().should((contents) => {- expect(contents).to.contain(renamedName);- 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);- });-- cy.log("delete the workspace via the menu");- H.WorkspaceListPage.workspaceMenuButton(renamedName).click();- H.WorkspaceListPage.deleteMenuItem().click();- H.DeleteWorkspaceModal.confirmButton().click();- H.WorkspaceListPage.workspaceList().should("not.exist");- H.WorkspaceListPage.newButton().should("be.visible");- });- });-- 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");- enableWorkspaces(WRITABLE_DB_ID);- cy.deleteDownloadsFolder();- });-- it("creates, downloads config, and deletes a workspace as admin", () => {- cy.log("create the workspace with the schemaless database");- H.WorkspaceListPage.visit();- H.WorkspaceListPage.newButton().click();- H.NewWorkspaceModal.nameInput().type(workspaceName);- H.NewWorkspaceModal.databaseCheckbox(MYSQL_DB_NAME).click();- H.NewWorkspaceModal.createButton().click();-- H.WorkspaceListPage.workspace(workspaceName)- .should("be.visible")- .and("contain.text", MYSQL_DB_NAME);-- cy.log("download the workspace config");- H.WorkspaceListPage.workspaceMenuButton(workspaceName).click();- H.WorkspaceListPage.downloadConfigMenuItem().click();- cy.verifyDownload(CONFIG_FILENAME, { timeout: 15000 });- 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("delete the workspace");- H.WorkspaceListPage.workspaceMenuButton(workspaceName).click();- H.WorkspaceListPage.deleteMenuItem().click();- H.DeleteWorkspaceModal.confirmButton().click();- H.WorkspaceListPage.workspaceList().should("not.exist");- H.WorkspaceListPage.newButton().should("be.visible");- });- });-});--function enableWorkspaces(databaseId: number) {- cy.request("GET", `/api/database/${databaseId}`).then(({ body }) => {- cy.request("PUT", `/api/database/${databaseId}`, {- settings: { ...body.settings, "database-enable-workspaces": true },- });- });-}--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/api.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/api.cljindex c5abcdd0e9c..8fabff3653d 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_config/api.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/api.clj@@ -8,7 +8,6 @@[metabase-enterprise.advanced-config.file.databases][metabase-enterprise.advanced-config.file.settings][metabase-enterprise.advanced-config.file.users]- [metabase-enterprise.advanced-config.file.workspace][metabase.api.common :as api][metabase.api.macros :as api.macros][metabase.util.malli.schema :as ms]@@ -18,12 +17,11 @@metabase-enterprise.advanced-config.file.api-keys/keep-memetabase-enterprise.advanced-config.file.databases/keep-memetabase-enterprise.advanced-config.file.settings/keep-me- metabase-enterprise.advanced-config.file.users/keep-me- metabase-enterprise.advanced-config.file.workspace/keep-me)+ metabase-enterprise.advanced-config.file.users/keep-me)(api.macros/defendpoint :post "/" :- :nil"Apply an uploaded `config.yml` to this instance. Runs the same per-section- initializers (`settings`, `databases`, `users`, `api-keys`, `workspace`, ...)+ initializers (`settings`, `databases`, `users`, `api-keys`, ...)the boot-time loader runs. Superuser-only.Unlike the boot-time loader, `{{env VAR}}` templates are NOT expanded — thediff --git a/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/file.cljindex 2f30dcff2d1..ecc33c685c5 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj@@ -102,8 +102,6 @@[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-enterprise.workspaces.core :as ws][metabase.lib.core :as lib][metabase.premium-features.core :as premium-features][metabase.util :as u]@@ -121,9 +119,7 @@;; 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- ;; for `workspace:` section code- advanced-config.file.workspace/keep-me)+ metabase-enterprise.advanced-config.file.api-keys/keep-me)(set! *warn-on-reflection* true)@@ -288,8 +284,6 @@(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."){}))))- (when (= section-name :workspace)- (premium-features/assert-has-feature :workspaces (tru "Workspaces")))(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 "🗄️")))@@ -297,15 +291,6 @@(defn boot-initialize!"Boot-time entry point: read the config file from disk and run [[initialize!]]- with `{{env VAR}}` template expansion enabled. No-op when no file is present.- When the config contains a `:workspace` section, locks the workspace against- runtime mutation (see- [[metabase-enterprise.workspaces.core/workspace-locked-by-config?]])."+ with `{{env VAR}}` template expansion enabled. No-op when no file is present."[]- (let [parsed (config-from-disk)- result (initialize! parsed {:expand-templates? true})]- ;; Only the boot path locks. Runtime config uploads (POST /api/ee/advanced-config)- ;; call `initialize!` directly, bypassing this wrapper, so they never lock.- (when (get-in parsed [:config :workspace])- (ws/mark-locked-by-config!))- result))+ (initialize! (config-from-disk) {:expand-templates? true}))diff --git a/enterprise/backend/src/metabase_enterprise/advanced_config/file/workspace.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/file/workspace.cljdeleted file mode 100644index 31be057e107..00000000000--- a/enterprise/backend/src/metabase_enterprise/advanced_config/file/workspace.clj+++ /dev/null@@ -1,119 +0,0 @@-(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 `instance-workspace` setting- (see [[metabase-enterprise.workspaces.settings]]). That setting is the- instance-side source of truth for `db-workspace-namespace`.-- Every boot re-reads `config.yml` and overwrites the prior value. The setting- persists across restarts in the instance's app DB, so a running workspace- survives a process bounce even without `config.yml` — but if `config.yml` is- present at boot, it wins."- (: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.advanced-config.file.workspace.output :as-alias wkspc-output]- [metabase-enterprise.workspaces.core :as ws]- [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))--(s/def ::wkspc-output/db- (s/nilable string?))--(s/def ::wkspc-output/schema- (s/nilable string?))--(s/def ::output- (s/and (s/keys :opt-un [::wkspc-output/db- ::wkspc-output/schema])- (fn [m] (or (some? (:db m)) (some? (:schema m))))))--(s/def ::workspace-database-config- (s/keys :req-un [::input_schemas ::output]))--(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]))--(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 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 {:db ?, :schema ?}}- ...}}-- 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 the `instance-workspace` setting keyed by db-id.-- The per-database `:output` map is taken verbatim — `workspaces.config/build-workspace-config`- on the manager side emits it already-expanded, so we don't re-derive it here."- [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 (:output 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 80ea6c226cd..a90b5e78155 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj@@ -78,9 +78,6 @@:can_access_transforms (or api/*is-superuser?* (and api/*is-data-analyst?*(perms/user-has-any-perms-of-type? api/*current-user-id* :perms/transforms: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 2c801c2e49a..7d3b4e531f9 100644--- a/enterprise/backend/src/metabase_enterprise/analytics/stats.clj+++ b/enterprise/backend/src/metabase_enterprise/analytics/stats.clj@@ -38,7 +38,4 @@:enabled (some? (advanced-config.settings/subscription-allowed-domains))}{:name :semantic-search:available (premium-features/enable-semantic-search?)- :enabled (semantic-search/supported?)}- {:name :workspaces- :available (premium-features/enable-workspaces?)- :enabled (t2/exists? :model/Workspace)}])+ :enabled (semantic-search/supported?)}])diff --git a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj b/enterprise/backend/src/metabase_enterprise/api_routes/routes.cljindex f3cea35500d..608a7e8e7b4 100644--- a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj+++ b/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj@@ -46,7 +46,6 @@[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.request.core :as request]@@ -81,8 +80,7 @@:metabot-v3 (deferred-tru "Metabot"):cloud-custom-smtp (deferred-tru "Custom SMTP"):support-users (deferred-tru "Support Users")- :transforms-python (deferred-tru "Transforms Python")- :workspaces (deferred-tru "Workspaces")})+ :transforms-python (deferred-tru "Transforms Python")})(defn- premium-handler [handler required-feature](let [handler (cond-> handler@@ -156,9 +154,7 @@"/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)- "/workspace-instance" (premium-handler metabase-enterprise.workspaces.api/instance-routes :workspaces)- "/workspace-manager" (premium-handler metabase-enterprise.workspaces.api/manager-routes :workspaces)})+ "/upload-management" (premium-handler metabase-enterprise.upload-management.api/routes :upload-management)});;; ↑↑↑ 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.cljindex 05da47b7d4c..c9b97ce7759 100644--- a/enterprise/backend/src/metabase_enterprise/connection_overlays/core.clj+++ b/enterprise/backend/src/metabase_enterprise/connection_overlays/core.clj@@ -11,11 +11,3 @@: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 544f0820079..35387358e84 100644--- a/enterprise/backend/src/metabase_enterprise/core/init.clj+++ b/enterprise/backend/src/metabase_enterprise/core/init.clj@@ -29,5 +29,4 @@[metabase-enterprise.stale.init][metabase-enterprise.support-access-grants.init][metabase-enterprise.tenants.init]- [metabase-enterprise.transforms-python.init]- [metabase-enterprise.workspaces.init]))+ [metabase-enterprise.transforms-python.init]))diff --git a/enterprise/backend/src/metabase_enterprise/database_routing/api.clj b/enterprise/backend/src/metabase_enterprise/database_routing/api.cljindex 7a9c605cc49..fef4a8a77d5 100644--- a/enterprise/backend/src/metabase_enterprise/database_routing/api.clj+++ b/enterprise/backend/src/metabase_enterprise/database_routing/api.clj@@ -10,7 +10,6 @@[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@@ -37,7 +36,6 @@[: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.")@@ -111,7 +109,6 @@[{: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")diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.cljindex dc8f69a68ba..c9e51a236ff 100644--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj@@ -151,7 +151,6 @@"SourceSegmentDaily""SsoRelayState""SupportAccessGrantLog"- "TableRemapping""TaskHistory""TaskRun""Tenant"@@ -163,6 +162,4 @@"User""UserKeyValue""UserParameterValue"- "ViewLog"- "Workspace"- "WorkspaceDatabase"])+ "ViewLog"])diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/api.clj b/enterprise/backend/src/metabase_enterprise/workspaces/api.cljdeleted file mode 100644index 832a42f0041..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/api.clj+++ /dev/null@@ -1,17 +0,0 @@-(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.cljdeleted file mode 100644index 75b05213af0..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_instance.clj+++ /dev/null@@ -1,126 +0,0 @@-(ns metabase-enterprise.workspaces.api.workspace-instance- "EE API endpoints for the workspace loaded on this (child) instance, served under- `/api/ee/workspace-instance`. See [[metabase-enterprise.workspaces.api.workspace-manager]]- for the manager-side 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.i18n :refer [tru]]- [metabase.util.malli.schema :as ms]- [metabase.workspaces.core :as ws.oss]))--;;; ----------------------------------------------- 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]]- [:can_write :boolean]])--(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)- :can_write (not (ws/workspace-locked-by-config?))})--;;; ------------------------------------------------ Guards ------------------------------------------------------(defn- check-workspace-writable!- "Throw a 400 if this instance's workspace is locked by deployment config (a- `config.yml` `:workspace` section or the `MB_INSTANCE_WORKSPACE` env var).- Call after `check-superuser`, so a non-admin still gets a 403 rather than this."- []- (when (ws/workspace-locked-by-config?)- (throw (ex-info (tru "This instance''s workspace is set by deployment config (`config.yml` or the `MB_INSTANCE_WORKSPACE` env var) and can''t be changed at runtime. Remove that config and restart this instance to enable workspace edits.")- {:status-code 400- :workspace-locked-by-config true}))))--;;; ---------------------------------------------- Endpoints -----------------------------------------------------(api.macros/defendpoint :get "/current" :- [:map [:data [:maybe WorkspaceInstance]]]- "Read-only summary of the workspace loaded on this instance, wrapped in a- `{:data ...}` envelope.-- Reads from the `instance-workspace` setting populated at boot by the `:workspace`- section of `config.yml`, or at runtime by `POST /current`. `:data` is `null`- when no workspace was loaded — i.e. this is a manager-only instance, or no- `config.yml` was present at boot and `POST /current` hasn't been called.- The envelope avoids an empty JSON body for the `nil` case.-- The workspace's `can_write` flag is `false` when it was set by deployment- config (`config.yml` or `MB_INSTANCE_WORKSPACE`); `POST`/`DELETE` are rejected- in that case."- []- (api/check-superuser)- {:data (some-> (ws/instance-workspace) present-workspace-instance)})--(api.macros/defendpoint :post "/current" :- WorkspaceInstance- "Install a workspace config on this instance at runtime. Accepts the same shape- `GET /current` returns and persists it via the `instance-workspace` setting so- it survives restarts. Use this on a running instance to enter workspace mode- without restarting from `config.yml`.-- Returns 400 if the workspace was set by deployment config (`config.yml` or- `MB_INSTANCE_WORKSPACE`) and so is read-only."- [_route-params- _query-params- body :- ::ws.oss/workspace-instance-config]- (api/check-superuser)- (check-workspace-writable!)- (ws/set-instance-workspace! body)- (present-workspace-instance (ws/instance-workspace)))--(api.macros/defendpoint :delete "/current" :- :nil- "Clear the workspace config on this instance. After this returns, the instance- is no longer in workspace mode and `GET /current` returns `nil`. Also drops- every `TableRemapping` row, since stale mappings from the prior workspace- would otherwise keep rewriting queries on the now-unmanaged databases.-- Returns 400 if the workspace was set by deployment config (`config.yml` or- `MB_INSTANCE_WORKSPACE`) and so is read-only."- []- (api/check-superuser)- (check-workspace-writable!)- (ws/clear-instance-workspace!)- (ws/clear-all-remappings!)- nil)--(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.cljdeleted file mode 100644index 839be07a962..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj+++ /dev/null@@ -1,145 +0,0 @@-(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.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.util.malli.schema :as ms]- [toucan2.core :as t2]))--;;; ----------------------------------------------- Schemas ------------------------------------------------------(def ^:private WorkspaceStatus- [:enum {:decode/api keyword}- :unprovisioned :provisioning :provisioned :deprovisioning])--(def ^:private CreateWorkspaceParams- [:map {:closed true}- [:name ms/NonBlankString]- [:database_ids [:sequential {:min 1} ::lib.schema.id/database]]])--(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]- [:database {:optional true} [:maybe :map]]])--(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]- ;; Both the list and GET /:id endpoints hydrate `:databases`; it stays- ;; optional so clients treat a missing array as `[]`.- [:databases {:optional true} [:sequential WorkspaceDatabaseResponse]]])--;;; -------------------------------------------- Presentation ----------------------------------------------------(defn- present-workspace-database [wsd]- (-> wsd- (select-keys [:database_id :input_schemas :output_namespace :status :database])- ;; never expose connection credentials- (m/update-existing :database #(some-> % (dissoc :details)))))--(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."- []- (api/check-superuser)- (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 attached to the given databases (each must be eligible- for workspaces) and provision it (blocking)."- [_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" :- :nil- "Delete a Workspace. Tears down every database's warehouse isolation first- (blocking, any state). Each database is either fully torn down (its row is- deleted) or kept; when any teardown fails the workspace is kept too — with the- failed rows, so the delete can be retried — and the combined failure is- returned as the error. 204 on success, no response body."- [{:keys [id]} :- [:map [:id ms/PositiveInt]]]- (api/write-check :model/Workspace id)- (ws/delete-workspace! id)- nil)--;;; ------------------------------------------- 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)}))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/config.clj b/enterprise/backend/src/metabase_enterprise/workspaces/config.cljdeleted file mode 100644index 78cdffc6bf5..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/config.clj+++ /dev/null@@ -1,163 +0,0 @@-(ns metabase-enterprise.workspaces.config- (:require- [clojure.string :as str]- [metabase-enterprise.workspaces.core :as ws]- [metabase-enterprise.workspaces.models.workspace :as workspace]- [metabase-enterprise.workspaces.models.workspace-database]- [metabase.driver :as driver]- [metabase.driver.util :as driver.u]- [metabase.sample-data.core :as sample-data]- [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)- ;; Force :let-user-control-scheduling false. The YAML only carries :details — not the- ;; cache_field_values_schedule / metadata_sync_schedule columns — and infer-db-schedules- ;; asserts those columns are present whenever user-control scheduling is on. Without- ;; this override the round-trip blows up for any DB whose source details opted in.- :details (merge (:details db)- (:database_details wsd)- (schema-filter-entries db wsd)- {:let-user-control-scheduling false})})--(defn- expand-output- "Expand a driver-opaque `output_namespace` string into the `{:db ?, :schema ?}`- namespace map QP middleware, transform_hooks, and table_remapping consume.-- For 3-slot drivers (SQL Server, BigQuery) the `:db` slot is filled from- `Database.details`. For 2-slot drivers the namespace string lands in the- schema slot. For no-schema drivers (MySQL) it lands in the db slot.-- `output_namespace` blank → `:schema` is `nil`: the workspace database isn't- provisioned yet and QP/transform consumers treat it 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)- (and (:db components) (not (:schema components)))- (assoc :db schema))))--(defn- workspace-database-entry [wsd db]- [(:name db) {:input_schemas (vec (:input_schemas wsd))- :output (expand-output db (:output_namespace wsd))}])--(defn- stub-database-entry [db]- {:name (:name db)- :engine (:engine db)- :details {}- :is_stub true})--(defn- stub-databases- "Databases that exist in the instance but are not provisioned for this workspace.- Excludes the sample DB (emitted separately by `sample-database-entries`), the- audit DB, and routing-target databases (destinations with `:router_database_id` set)."- [workspace-db-ids]- (t2/select :model/Database- {:where [:and- [:= :is_sample false]- [:= :is_audit false]- [:= :router_database_id nil]- (if (seq workspace-db-ids)- [:not-in :id workspace-db-ids]- true)]}))--(defn- sample-database-entries- "If the instance has a Sample Database, emit a single placeholder entry the- import side will use to recreate it. Always uses the canonical name/engine —- the entry is a directive (\"there should be a sample DB here\"), not a snapshot."- []- (when (t2/exists? :model/Database :is_sample true)- [{:name sample-data/sample-database-name- :engine "h2"- :details {}- :is_sample true}]))--(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 {:db ? :schema ?}}}}}-- 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 the- expanded `{:db ?, :schema ?}` namespace map directly — the same shape the- `instance-workspace` setting stores. 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 [workspace-db-ids (mapv :database_id wsds)- dbs-by-id (if-let [ids (seq workspace-db-ids)]- (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])- ws-entries (mapv (fn [[wsd db]] (database-entry wsd db)) pairs)- stub-entries (mapv stub-database-entry (stub-databases workspace-db-ids))- sample-entries (sample-database-entries)]- {:version 1- :config {:databases (-> ws-entries- (into stub-entries)- (into sample-entries))- :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.cljdeleted file mode 100644index faecf90ce24..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/core.clj+++ /dev/null@@ -1,309 +0,0 @@-(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!]] and [[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 the- `instance-workspace` setting. Workspace-aware code (transform target- rewriting, table-remapping QP middleware) reads from the setting via- [[workspace-mode?]] / [[db-workspace-namespace]]. The setting lives in the- instance's own application database — child instances have their own app DB,- so storing it there persists the workspace across restarts without leaking- between parent and child.-- Per-database lifecycle for the manager-side rows:-- provision- unprovisioned ────────────► provisioning ──────► provisioned- ▲ │ │- │ failure │ │ deprovision- │ ▼ ▼- │ unprovisioned ◄──── deprovisioning- │ │- │ failure │- │ ▼- └──────────────────────────────────────── provisioned"- (:require- [clojure.string :as str]- [metabase-enterprise.workspaces.models.workspace :as workspace]- [metabase-enterprise.workspaces.models.workspace-database :as workspace-database]- [metabase-enterprise.workspaces.provisioning :as provisioning]- [metabase-enterprise.workspaces.settings :as ws.settings]- [metabase.driver.sql :as driver.sql]- [metabase.premium-features.core :refer [defenterprise]]- [metabase.settings.core :as setting]- [metabase.util.malli :as mu]- [metabase.workspaces.core :as ws]- [toucan2.core :as t2]))--(set! *warn-on-reflection* true)--(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)})))--(defn- coerce-database-id-key- "JSON round-trips through the `instance-workspace` setting return integer- `Database.id` keys as keywords (e.g. `:1`). Coerce them back to ints."- [k]- (cond- (int? k) k- (keyword? k) (parse-long (name k))- (string? k) (parse-long k)))--(defn- normalize-database-keys- "Coerce the `:databases` map keys to ints. See [[coerce-database-id-key]]."- [config]- (some-> config- (update :databases #(update-keys % coerce-database-id-key))))--(mu/defn set-instance-workspace! :- :any- "Store the workspace config in the `instance-workspace` setting. Replaces any- prior value. The shape is validated against `::ws/workspace-instance-config`."- [config :- ::ws/workspace-instance-config]- (ws.settings/instance-workspace! config)- nil)--(defn clear-instance-workspace!- "Clear the `instance-workspace` setting."- []- (ws.settings/instance-workspace! nil)- nil)--;;; ------------------------------- Instance-workspace lock ---------------------------------(defonce ^:private locked-by-config?* (atom false))--(defn workspace-locked-by-config?- "True iff this instance's workspace was set by out-of-band deployment config —- a `:workspace` section in `config.yml` at boot, or the `MB_INSTANCE_WORKSPACE`- env var — and so must not be mutated through the workspace-instance API. The- lock is per-boot and not persisted: removing the config and restarting clears it.-- Read at the HTTP boundary only. Do NOT consult it from `set-instance-workspace!`- or `clear-instance-workspace!` — internal callers and test fixtures rely on- those setters staying unconditional."- []- (or @locked-by-config?*- ;; Checked inline rather than captured at boot like the atom: nothing reads- ;; this env var at startup, and the lookup is cheap.- (some? (setting/env-var-value :instance-workspace))))--(defn mark-locked-by-config!- "Called by the boot loader only; runtime code paths never flip the lock."- []- (reset! locked-by-config?* true))--(defn clear-all-remappings!- "Delete every `:model/TableRemapping` row across all databases."- []- (t2/delete! :model/TableRemapping)- nil)--(defn instance-workspace- "Return the workspace loaded on this instance, or nil if none."- []- (normalize-database-keys (ws.settings/instance-workspace)))--(defenterprise workspace-mode?- "EE impl: true iff this instance is running in workspace mode (the- `instance-workspace` setting is populated — either from a `:workspace` section- of `config.yml` at boot or by a `POST /api/ee/advanced-config`). 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? (ws.settings/instance-workspace)))--(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 `workspace-instance`- setting."- [db-id]- (get-in (instance-workspace) [: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]- (when-not (t2/exists? :model/Workspace :id workspace-id)- (throw (ex-info "Workspace not found"- {:status-code 404 :workspace_id workspace-id}))))--(defn- assert-database-exists [database-id]- (or (t2/select-one :model/Database :id database-id)- (throw (ex-info "Database not found"- {:status-code 404 :database_id database-id}))))--(defn- assert-database-eligible-for-workspaces [database]- (when-not (workspace-database/database-eligible-for-workspaces? database)- (throw (ex-info "Workspaces are not enabled for this database"- {:status-code 400 :database_id (:id database)}))))--;;; ------------------------------------- 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- combined-exception- "One exception carrying every failure in `throwables`: the first is the cause,- the rest are attached as suppressed (the JDK mechanism for multiple failures- on one throwable). The message is the messages the databases returned, joined- into one line. The ex-data is shaped so the API error middleware returns just- that message — without it, an uncaught 500 dumps the full `Throwable->map`- cause chain into the response."- ^Throwable [throwables]- (let [message (str/join "; " (map #(or (ex-message %) (str (class %))) throwables))- ^Throwable ex (ex-info message- {:status-code 500- :message message- :errors {:_error message}}- (first throwables))]- (run! #(.addSuppressed ex ^Throwable %) (rest throwables))- ex))--(defn- provision-workspace-databases!- "Provision every WorkspaceDatabase of the hydrated workspace `ws` synchronously- (blocking), failing fast on the first error."- [ws]- (doseq [{wsd-id :id} (:databases ws)]- (provisioning/provision-single! wsd-id)))--(defn- teardown-workspace-databases!- "Tear down every WorkspaceDatabase of `workspace-id` — any state, blocking.- Mirrors [[provision-workspace-databases!]], but continues past failures so each- row gets its attempt: rows whose teardown succeeds are deleted immediately- (progress is persisted per row, so an instance crash midway loses nothing);- rows whose teardown fails are kept. Throws the combined failures when any- teardown failed."- [workspace-id]- (let [failures (into []- (keep (fn [wsd]- (try- (provisioning/teardown-workspace-database! wsd provisioning/dispatching-provisioner)- nil- (catch Throwable t t))))- (t2/select :model/WorkspaceDatabase :workspace_id workspace-id))]- (when (seq failures)- (throw (combined-exception failures)))))--(defn create-workspace!- "Create a new Workspace, attach the databases with ids `database_ids` — each must- exist (404) and be eligible for workspaces (400, see- [[workspace-database/database-eligible-for-workspaces?]]) — with all of their- known schemas as `input_schemas`, and provision each database (blocking).- Returns the created workspace, hydrated.-- The workspace and its WorkspaceDatabase rows are committed BEFORE provisioning- starts — deliberately NOT one big transaction. The rows are the durable record- of warehouse resources that may (partially) exist, so they must survive an- instance crash mid-provision; a rollback would erase them while the warehouse- objects live on. Cleanup after a provisioning failure is therefore explicit:- every database is torn down; when the cleanup removes everything, the Workspace- row is deleted and the provisioning error is rethrown; when the cleanup itself- fails, the workspace is kept — and returned like a successful create — with the- failed rows (`:unprovisioned`) so the leak stays visible and the teardown can- be retried via delete. Callers detect that case from the databases' statuses."- [{:keys [name creator_id database_ids]}]- (let [databases (mapv (fn [db-id]- (let [database (assert-database-exists db-id)]- (assert-database-eligible-for-workspaces database)- {:database_id db-id- :input_schemas (workspace-database/database-input-schemas database)}))- database_ids)- ws (workspace/create-workspace! {:name name- :creator_id creator_id- :databases databases})]- (try- (provision-workspace-databases! ws)- (catch Throwable t- (let [cleaned-up? (try- (teardown-workspace-databases! (:id ws))- true- ;; Cleanup failed: the workspace and the failed rows are- ;; kept — and returned — so the teardown can be retried- ;; via delete. Both errors are deliberately swallowed,- ;; not logged (raw warehouse errors are sensitive);- ;; callers see the failure in the databases' statuses,- ;; and a delete retry surfaces the teardown errors.- (catch Throwable _ false))]- (when cleaned-up?- (try- (workspace/delete-workspace! (:id ws))- (catch Throwable delete-t- (.addSuppressed t delete-t)))- (throw (combined-exception [t]))))))- (workspace/get-workspace (:id ws))))--(defn delete-workspace!- "Tear down every database's warehouse isolation (any state, blocking), then- delete the workspace. There is no partial deletion: each WorkspaceDatabase is- either fully torn down (warehouse footprint confirmed gone, row deleted) or- kept. Every database gets its teardown attempt even when earlier ones fail; if- any of them fail, the workspace is kept alongside the failed rows and one- exception combining all the failures is thrown, so the delete can be retried.- Progress is persisted per row, so an instance crash midway loses nothing.- App-DB `TableRemapping` rows are always cleared, so query routing is never- left dangling. Returns nil."- [id]- (assert-workspace-exists id)- (teardown-workspace-databases! id)- (workspace/delete-workspace! id)- nil)diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/init.clj b/enterprise/backend/src/metabase_enterprise/workspaces/init.cljdeleted file mode 100644index db1fbe6533e..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/init.clj+++ /dev/null@@ -1,18 +0,0 @@-(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.cljdeleted file mode 100644index 10da75bec0f..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/models/table_remapping.clj+++ /dev/null@@ -1,52 +0,0 @@-(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.cljdeleted file mode 100644index 1e139db39ee..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace.clj+++ /dev/null@@ -1,150 +0,0 @@-(ns metabase-enterprise.workspaces.models.workspace- (:require- [metabase-enterprise.workspaces.models.workspace-database]- [metabase.api.common :as api]- [metabase.models.interface :as mi]- [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 ----------------------------------------;;;-;;; Workspaces are superuser-only: read, write, and create all require admin.--(defmethod mi/can-read? :model/Workspace- ([_instance] api/*is-superuser?*)- ([_model _pk] api/*is-superuser?*))--(defmethod mi/can-create? :model/Workspace- [_model _instance]- api/*is-superuser?*)--(defmethod mi/can-write? :model/Workspace- ([_instance] api/*is-superuser?*)- ([_model _pk] api/*is-superuser?*))--(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` (each with its `:database`) and- `:creator` hydrated."- []- (t2/hydrate (t2/select :model/Workspace {:order-by [[:id :asc]]}) :creator [:databases :database]))--(defn get-workspace- "Return the Workspace with the given id and its `:databases` (each with its- `:database`) + `:creator` hydrated, or nil if none exists."- [id]- (when-let [workspace (t2/select-one :model/Workspace :id id)]- (t2/hydrate workspace :creator [:databases :database])))--(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.cljdeleted file mode 100644index 938c5bdf768..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_database.clj+++ /dev/null@@ -1,88 +0,0 @@-(ns metabase-enterprise.workspaces.models.workspace-database- (:require- [metabase-enterprise.workspaces.settings :as ws.settings]- [metabase.api.common :as api]- [metabase.driver.util :as driver.u]- [metabase.models.interface :as mi]- [metabase.premium-features.core :refer [defenterprise]]- [metabase.settings.core :as setting]- [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 ----------------------------------------;;;-;;; WorkspaceDatabases are superuser-only: read, write, and create all require admin.--(defmethod mi/can-read? :model/WorkspaceDatabase- ([_instance] api/*is-superuser?*)- ([_model _pk] api/*is-superuser?*))--(defmethod mi/can-write? :model/WorkspaceDatabase- ([_instance]- api/*is-superuser?*)- ([_model _pk]- api/*is-superuser?*))--(defmethod mi/can-create? :model/WorkspaceDatabase- [_model _instance]- api/*is-superuser?*)--(methodical/defmethod t2/batched-hydrate [:model/WorkspaceDatabase :database]- [_model k workspace-databases]- (mi/instances-with-hydrated-data- workspace-databases k- (fn []- (when-let [ids (seq (distinct (keep :database_id workspace-databases)))]- (t2/select-pk->fn identity :model/Database :id [:in ids])))- :database_id- {:default nil}))--;;; --------------------------------------- Database eligibility -----------------------------------------(defn database-eligible-for-workspaces?- "True iff `database`'s driver supports the `:workspace` feature and the- `database-enable-workspaces` database-local setting is enabled for it."- [database]- (and (driver.u/supports? (:engine database) :workspace database)- (boolean (setting/with-database database- (ws.settings/database-enable-workspaces)))))--(defn database-input-schemas- "The distinct non-blank schema names of `database`'s active tables, sorted.- Empty for schemaless drivers (e.g. MySQL)."- [database]- (->> (t2/select-fn-set :schema :model/Table :db_id (:id database) :active true)- (remove #(or (nil? %) (= % "")))- sort- vec))--(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.cljdeleted file mode 100644index ce56fcfc9e4..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj+++ /dev/null@@ -1,304 +0,0 @@-(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.connection :as driver.conn]- [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."- (details [this driver database workspace]- "Compute {:schema ... :database_details ...} for the workspace without touching- the warehouse. Called before init! so destroy! can clean up a partial init.")- (init! [this driver database workspace]- "Create isolated schema + user. `workspace` carries the precomputed `:schema`- and `:database_details` from [[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.-- Each call is wrapped in [[driver.conn/with-admin-connection]] so the underlying- driver impls acquire connections via the database's `:admin-details` overlay- (when configured). Workspace DDL — `CREATE USER`, `CREATE SCHEMA`, `GRANT` —- typically needs higher-privilege credentials than the regular query user, and- the admin overlay is how operators provide them. `details` computes no DDL but- still binds the overlay: some drivers derive connection strings/catalogs from- the effective details."- (reify Provisioner- (details [_ driver database workspace]- (driver.conn/with-admin-connection- (driver/workspace-isolation-details driver database workspace)))- (init! [_ driver database workspace]- (driver.conn/with-admin-connection- (driver/init-workspace-isolation! driver database workspace)))- (grant! [_ driver database workspace schemas]- (driver.conn/with-admin-connection- (driver/grant-workspace-read-access! driver database workspace schemas)))- (destroy! [_ driver database workspace]- (driver.conn/with-admin-connection- (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)))--(defn do-with-workspace-database-lock- "Run `thunk` while holding the per-row cluster lock for `workspace-database-id`.- [[provision-single!]]/[[deprovision-single!]] take this same lock internally, so- the acquisition here is reentrant — it lets a caller atomically combine an app-db- change with provisioning under a single lock, acquired *before* the row is- mutated (the lock-before-mutate order the rest of the system relies on)."- [workspace-database-id thunk]- (cluster-lock/with-cluster-lock {:lock (wsd-lock-key workspace-database-id)- :timeout-seconds provisioning-lock-timeout-seconds}- (thunk)))--(defmacro with-workspace-database-lock- "Sugar over [[do-with-workspace-database-lock]]."- [workspace-database-id & body]- `(do-with-workspace-database-lock ~workspace-database-id (fn [] ~@body)))--(defn- wsd-iso-workspace- "The synthetic workspace map from which all warehouse identifiers for a- WorkspaceDatabase row are derived. Every provision/deprovision/teardown path- must build the exact same map — teardown recomputes what provision computed."- [workspace-database-id]- {:id workspace-database-id :name (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- (with-workspace-database-lock workspace-database-id- (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 (wsd-iso-workspace workspace-database-id)- ws-details (merge workspace (details provisioner driver db workspace))- schemas (vec (:input_schemas wsd))]- (try- (init! provisioner driver db ws-details)- (grant! provisioner driver db ws-details schemas)- (catch Throwable t- (try- (destroy! provisioner driver db ws-details)- (catch Throwable destroy-t- (log/warnf destroy-t "Failed to clean up after provisioning failure for WorkspaceDatabase %s"- workspace-database-id)))- (throw t)))- (t2/update! :model/WorkspaceDatabase- {:id workspace-database-id}- {:output_namespace (:schema ws-details)- :database_details (:database_details ws-details)- :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- (with-workspace-database-lock workspace-database-id- (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 (assoc (wsd-iso-workspace 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 workspace-database lock's- ;; 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 teardown-workspace-database!- "Tear down one WorkspaceDatabase's warehouse isolation and, on success, delete- its row. Used by the delete path and by create-workspace cleanup. Works from ANY- state: the per-row cluster lock is taken first, so in-flight- provisioning/deprovisioning on another node completes before we touch anything- (or the lock times out and the teardown is reported as a failure), and the row- is re-read under the lock so we see whatever that work persisted. Identifiers- are the persisted ones, or- recomputed deterministically from the row id via the provisioner's `details`- ([[metabase.driver/workspace-isolation-details]]) for a crashed `:provisioning`- row that never recorded anything.-- There is no partial outcome: the teardown either fully succeeds or the row- stays. The row is the durable record that warehouse resources may exist — it- must survive instance crashes and disappear only once the warehouse footprint- is confirmed gone. On success the WorkspaceDatabase row is DELETED and the- app-DB `TableRemapping` rows for the iso namespace are cleared — stale- remappings would rewrite queries to a dropped schema and 500 the QP.-- Throws on failure. When the destroy itself fails, the row bookkeeping runs- first: the row is kept, forced `:unprovisioned`, and the remapping cleanup- still runs. Failures before the destroy — lock timeout, identifier- computation — throw without touching the row. Either way the teardown can be- retried. Returns nil."- [wsd provisioner]- (with-workspace-database-lock (:id wsd)- (when-let [wsd (t2/select-one :model/WorkspaceDatabase :id (:id wsd))]- (let [db (t2/select-one :model/Database :id (:database_id wsd))- driver (driver.u/database->driver db)- iso-ws (wsd-iso-workspace (:id wsd))- computed (delay (details provisioner driver db iso-ws))- schema (or (not-empty (:output_namespace wsd))- (:schema @computed))- db-details (if (seq (:database_details wsd))- (:database_details wsd)- (:database_details @computed))- workspace (assoc iso-ws :schema schema :database_details db-details)]- (try- (destroy! provisioner driver db workspace)- (catch Throwable t- ;; The rethrow rolls back the cluster lock's wrapping transaction on- ;; postgres/mysql app DBs, so the keep-row bookkeeping needs its own- ;; autocommit connection to survive it.- (binding [t2.connection/*current-connectable* nil]- (t2/update! :model/WorkspaceDatabase {:id (:id wsd)}- {:output_namespace "" :database_details {} :status :unprovisioned}))- (throw t))- (finally- ;; App-DB cleanup needs no warehouse connection, so it ALWAYS runs — stale- ;; remappings would otherwise rewrite queries to a dropped schema and 500 the- ;; QP. Fresh autocommit connection to survive any surrounding tx rollback- ;; (mirrors `deprovision-workspace-database!`).- (binding [t2.connection/*current-connectable* nil]- (ws.remapping-cleanup/clear-mappings-for-iso! db (:database_id wsd) schema))))- (t2/delete! :model/WorkspaceDatabase :id (:id wsd))- nil))))--(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.cljdeleted file mode 100644index 01cf5673166..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/query_processor/middleware.clj+++ /dev/null@@ -1,311 +0,0 @@-(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-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- "**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.cljdeleted file mode 100644index e308716d8bd..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/remapping/core.clj+++ /dev/null@@ -1,216 +0,0 @@-(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.cljdeleted file mode 100644index f6521f56240..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/remapping_cleanup.clj+++ /dev/null@@ -1,45 +0,0 @@-(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.cljdeleted file mode 100644index 7df12766529..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/settings.clj+++ /dev/null@@ -1,27 +0,0 @@-(ns metabase-enterprise.workspaces.settings- (:require- [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 database-enable-workspaces- (deferred-tru "Whether to enable Workspaces for a specific Database.")- :default false- :type :boolean- :feature :workspaces- :driver-feature :workspace- :visibility :public- :export? false- :database-local :only)--(defsetting instance-workspace- (deferred-tru "The workspace loaded on this instance. Populated at boot from a config.yml `:workspace` section or at runtime via `POST /api/ee/advanced-config`. `nil` on parent and unconfigured instances. Read by the QP, transform hooks, and the EE `workspace-mode?` predicate via `metabase-enterprise.workspaces.core/instance-workspace`.")- :type :json- :encryption :no- :visibility :internal- :export? false- :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.cljdeleted file mode 100644index 77f746a546b..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/table_remapping.clj+++ /dev/null@@ -1,655 +0,0 @@-(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.cljdeleted file mode 100644index 4e4b6dfa905..00000000000--- a/enterprise/backend/src/metabase_enterprise/workspaces/transform_hooks.clj+++ /dev/null@@ -1,70 +0,0 @@-(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/test/metabase_enterprise/advanced_config/api_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_config/api_test.cljindex 247b4997f14..fff908d4b6e 100644--- a/enterprise/backend/test/metabase_enterprise/advanced_config/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/advanced_config/api_test.clj@@ -1,7 +1,6 @@(ns metabase-enterprise.advanced-config.api-test(:require[clojure.test :refer [deftest is testing use-fixtures]]- [metabase-enterprise.workspaces.core :as ws][metabase.driver.util][metabase.test :as mt][metabase.test.fixtures :as fixtures]@@ -46,73 +45,3 @@(finally(when-let [db-id (t2/select-one-pk :model/Database :name db-name :engine "postgres")](t2/delete! :model/Database db-id)))))))--(deftest applies-workspace-section-test- (testing "POST /api/ee/advanced-config applies the :workspace section into the instance-workspace setting"- (let [db-name (str "advanced-config-ws-" (random-uuid))- payload {:version 1- :config {:databases [{:name db-name- :engine "postgres"- :details {}}]- :workspace {:name "Uploaded Workspace"- :databases {(keyword db-name) {:input_schemas ["public"]- :output {:schema "ws_uploaded"}}}}}}]- (try- (mt/with-premium-features #{:config-text-file :workspaces}- (mt/with-temporary-setting-values [config-from-file-sync-databases false]- (with-redefs [metabase.driver.util/can-connect-with-details? (constantly true)]- (mt/user-http-request :crowberto :post 204 "ee/advanced-config/"- (first (multipart (yaml-bytes payload)))- (second (multipart (yaml-bytes payload)))))))- (testing "Database row was created"- (let [db (t2/select-one :model/Database :name db-name :engine "postgres")]- (is (some? db))- (testing "Workspace instance setting was set"- (let [stored (ws/instance-workspace)]- (is (= "Uploaded Workspace" (:name stored)))- (is (= {:schema "ws_uploaded"} (get-in stored [:databases (:id db) :output])))))))- (finally- (ws/clear-instance-workspace!)- (when-let [db-id (t2/select-one-pk :model/Database :name db-name :engine "postgres")]- (t2/delete! :model/Database db-id)))))))--(deftest runtime-upload-via-api-does-not-set-the-lock-test- (testing "POST /api/ee/advanced-config with a :workspace section does NOT lock the workspace (only the boot loader does)"- (let [db-name (str "advanced-config-ws-" (random-uuid))- payload {:version 1- :config {:databases [{:name db-name- :engine "postgres"- :details {}}]- :workspace {:name "Uploaded Workspace"- :databases {(keyword db-name) {:input_schemas ["public"]- :output {:schema "ws_uploaded"}}}}}}- lock-atom @#'ws/locked-by-config?*- prior @lock-atom]- (try- (reset! lock-atom false)- (mt/with-premium-features #{:config-text-file :workspaces}- (mt/with-temporary-setting-values [config-from-file-sync-databases false]- (with-redefs [metabase.driver.util/can-connect-with-details? (constantly true)]- (mt/user-http-request :crowberto :post 204 "ee/advanced-config/"- (first (multipart (yaml-bytes payload)))- (second (multipart (yaml-bytes payload)))))))- (is (false? (ws/workspace-locked-by-config?))- "a runtime upload must not flip the boot lock")- (finally- (reset! lock-atom prior)- (ws/clear-instance-workspace!)- (when-let [db-id (t2/select-one-pk :model/Database :name db-name :engine "postgres")]- (t2/delete! :model/Database db-id)))))))--(deftest workspace-section-rejected-without-workspaces-feature-test- (testing "POST /api/ee/advanced-config returns 402 when :workspace section requires :workspaces"- (let [payload {:version 1- :config {:workspace {:name "Rejected Workspace"- :databases {:some-db {:input_schemas ["public"]- :output {:schema "ws_rejected"}}}}}}]- (mt/with-premium-features #{:config-text-file}- (mt/assert-has-premium-feature-error- "Workspaces"- (mt/user-http-request :crowberto :post 402 "ee/advanced-config/"- (first (multipart (yaml-bytes payload)))- (second (multipart (yaml-bytes payload)))))))))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.cljdeleted file mode 100644index 9e37f4ff24b..00000000000--- a/enterprise/backend/test/metabase_enterprise/advanced_config/file/workspace_test.clj+++ /dev/null@@ -1,351 +0,0 @@-(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.test :as mt]- [metabase.test.fixtures :as fixtures]- [metabase.util.yaml :as yaml])- (:import- (clojure.lang ExceptionInfo)))--(use-fixtures :once (fixtures/initialize :db))--(use-fixtures :each- (fn [thunk]- ;; Several tests drive a `:workspace` section through `initialize!`, which- ;; requires the `:workspaces` feature; default it on here. Tests that assert- ;; the feature's *absence* override with their own `mt/with-premium-features`.- ;; Also save/restore the boot-lock atom so the boot-lock tests below can't- ;; leak a flipped lock into the rest of the suite.- (mt/with-premium-features #{:workspaces}- (binding [advanced-config.file/*supported-versions* {:min 1, :max 1}]- (let [lock-atom @#'ws/locked-by-config?*- prior @lock-atom]- (try- (thunk)- (finally- (ws/clear-instance-workspace!)- (reset! lock-atom prior))))))))--(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 (= {:schema "mb__isolation_44490_1933"} (:output 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 instance-workspace setting 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 "setting 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 setting — 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 {:schema "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))- "setting 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 setting is cleared in the use-fixtures :each tear-down, so this confirms- ;; the read truly comes from the setting 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-rejects-workspace-section-without-premium-token-test- (testing "OSS instances need the :config-text-file token to load a :workspace section (no special bring-up carve-out)"- (mt/with-empty-h2-app-db!- (mt/with-premium-features #{}- (mt/with-temp [:model/Database _ {:name "ws-test-db" :engine :postgres}]- (is (thrown-with-msg?- ExceptionInfo- #"Premium token with the :config-text-file feature"- (advanced-config.file/initialize!- {:version 1- :config {:workspace (workspace-section "ws-test-db")}}))))))))--(deftest oss-rejects-non-settings-sections-without-premium-token-test- (testing "OSS instances always need the :config-text-file token for non-:settings sections"- (mt/with-empty-h2-app-db!- (mt/with-premium-features #{}- (is (thrown-with-msg?- ExceptionInfo- #"Premium token with the :config-text-file feature"- (advanced-config.file/initialize!- {:version 1- :config {:users [{:first_name "X" :last_name "Y"- :email "[email protected]" :password "pw"}]}})))))))--(deftest workspace-section-requires-workspaces-feature-test- (testing ":workspace section needs the :workspaces feature in addition to :config-text-file"- (mt/with-empty-h2-app-db!- (mt/with-temp [:model/Database _ {:name "ws-test-db" :engine :postgres}]- (mt/with-premium-features #{:config-text-file}- (is (thrown-with-msg?- ExceptionInfo- #"Workspaces is a paid feature"- (advanced-config.file/initialize!- {:version 1- :config {:workspace (workspace-section "ws-test-db")}}))))))))--(deftest workspace-section-loads-with-workspaces-feature-test- (testing ":workspace section loads cleanly when both :config-text-file and :workspaces are present"- (mt/with-empty-h2-app-db!- (mt/with-temp [:model/Database {db-id :id} {:name "ws-test-db" :engine :postgres}]- (mt/with-premium-features #{:config-text-file :workspaces}- (advanced-config.file/initialize!- {:version 1- :config {:workspace (workspace-section "ws-test-db")}})- (is (= "New workspace" (:name (ws/instance-workspace)))- "the workspace section should have been applied")- (is (= {:schema "mb__isolation_44490_1933"}- (ws/db-workspace-namespace db-id))))))))--(deftest workspaces-gate-is-scoped-to-workspace-section-test- (testing "without :workspaces, other non-:workspace sections still load (gate is :workspace-scoped)"- (mt/with-empty-h2-app-db!- (mt/with-premium-features #{:config-text-file}- (advanced-config.file/initialize!- {:version 1- :config {:users [{:first_name "X" :last_name "Y"- :email "[email protected]" :password "pw"}]}})- (is (nil? (ws/instance-workspace))- "no workspace section means no workspace state was set")))))--;;; ----------------------------------------- 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 setting (no schema layer)"- (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 {:db "ws_alice"}}}})- (is (= {:db "ws_alice"} (ws/db-workspace-namespace db-id))))))))--(deftest bigquery-3-slot-section-test- (testing "BigQuery workspace: project + dataset in the :output map"- (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 {:db "metabase-prod" :schema "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 setting 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`, and the `:output` map (already expanded in the- YAML since `workspaces.config/build-workspace-config` emits the runtime shape- directly)."- {:postgres {:db-name "test-data (postgres)"- :engine :postgres- :details {}- :input_schemas ["public"]- :output {:schema "mb__isolation_44490_1933"}}- :redshift {:db-name "test-data (redshift)"- :engine :redshift- :details {}- :input_schemas ["public"]- :output {:schema "mb__isolation_44490_1933"}}- :mysql {:db-name "test-data (mysql)"- :engine :mysql- :details {:db "prod_db"}- :input_schemas ["prod_db"]- :output {:db "ws_alice"}}- :clickhouse {:db-name "test-data (clickhouse)"- :engine :clickhouse- :details {}- :input_schemas ["prod_events"]- :output {:schema "ws_alice"}}- :sqlserver {:db-name "test-data (sqlserver)"- :engine :sqlserver- :details {:db "AnalyticsDB"}- :input_schemas ["dbo"]- :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 {: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 expectations) (:output wsd))- (str driver " :output matches expectation")))- (testing "validates against the workspace section spec"- (is (true? (s/valid? :metabase-enterprise.advanced-config.file.workspace/config-file-spec section))- (str driver " fixture must satisfy the section spec")))- (testing "round-trips through apply-workspace-section! into the setting"- (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 (= (:output expectations) (ws/db-workspace-namespace db-id))- (str driver " setting output matches fixture"))))))))))--;;; ----------------------------------------- boot lock wiring -----------------------------------------;;;-;;; `boot-initialize!` is the sole boot entry point. When it parses a-;;; `config.yml` containing a `:workspace` section, it flips the in-memory-;;; `workspace-locked-by-config?*` atom. Runtime callers of `initialize!` (the-;;; `POST /api/ee/advanced-config` path) and direct callers of-;;; `apply-workspace-section!` do NOT flip the lock — the boot wrapper is the-;;; only thing that does.--(defn- lock-atom [] @#'ws/locked-by-config?*)--(deftest boot-initialize!-sets-the-lock-when-config-has-workspace-test- (testing "boot-initialize!, given a parsed config.yml with a :workspace section, flips the lock"- (mt/with-empty-h2-app-db!- (mt/with-premium-features #{:config-text-file :workspaces}- (mt/with-temp [:model/Database _ {:name "ws-test-db" :engine :postgres}]- (reset! (lock-atom) false)- (with-redefs [advanced-config.file/config-from-disk- (constantly {:version 1- :config {:workspace (workspace-section "ws-test-db")}})]- (advanced-config.file/boot-initialize!))- (is (true? (ws/workspace-locked-by-config?))))))))--(deftest boot-initialize!-does-not-set-the-lock-when-config-has-no-workspace-test- (testing "boot-initialize! with no :workspace section in the config leaves the lock false"- (mt/with-empty-h2-app-db!- (reset! (lock-atom) false)- (with-redefs [advanced-config.file/config-from-disk- (constantly {:version 1- :config {:settings {}}})]- (advanced-config.file/boot-initialize!))- (is (false? (ws/workspace-locked-by-config?))))))--(deftest boot-initialize!-no-config-file-does-not-set-the-lock-test- (testing "boot-initialize! when no config.yml is present (config-from-disk returns nil) leaves the lock false"- (mt/with-empty-h2-app-db!- (reset! (lock-atom) false)- (with-redefs [advanced-config.file/config-from-disk (constantly nil)]- (advanced-config.file/boot-initialize!))- (is (false? (ws/workspace-locked-by-config?))))))--(deftest apply-workspace-section!-direct-call-does-not-set-the-lock-test- (testing "calling apply-workspace-section! directly (the runtime / test path) does NOT flip the lock"- (mt/with-empty-h2-app-db!- (mt/with-temp [:model/Database _ {:name "ws-test-db" :engine :postgres}]- (reset! (lock-atom) false)- (advanced-config.file.workspace/apply-workspace-section! (workspace-section "ws-test-db"))- (is (false? (ws/workspace-locked-by-config?)))))))diff --git a/enterprise/backend/test/metabase_enterprise/api/session_test.clj b/enterprise/backend/test/metabase_enterprise/api/session_test.cljindex a6ecf40f043..0434725bd11 100644--- a/enterprise/backend/test/metabase_enterprise/api/session_test.clj+++ b/enterprise/backend/test/metabase_enterprise/api/session_test.clj@@ -128,7 +128,6 @@:etl_connections_pg false:dependencies false:schema-viewer false- :workspaces false:writable_connection true}(mt/with-temporary-setting-values [csp-img-enabled truecustom-viz-enabled true]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 628831eba26..f66f944fbf7 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@@ -112,10 +112,6 @@: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/SourceDimensionDaily@@ -147,12 +143,7 @@:model/SecurityAdvisory:model/CloudMigration:model/Comment- :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- :model/WorkspaceInstance})+ :model/CommentReaction})(deftest ^:parallel comprehensive-entity-id-test(let [entity-id-models (->> (keys models.resolution/model->namespace)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.cljdeleted file mode 100644index c77466279a7..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_instance_test.clj+++ /dev/null@@ -1,172 +0,0 @@-(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-enterprise.workspaces.test-util :as ws.tu]- [metabase.test :as mt]- [metabase.test.fixtures :as fixtures]- [toucan2.core :as t2]))--(use-fixtures :once (fixtures/initialize :db))--(use-fixtures :each- (fn [thunk]- (mt/with-premium-features #{:workspaces}- (try- (thunk)- (finally- (ws/clear-instance-workspace!))))))--(deftest current-superuser-only-test- (testing "GET /ee/workspace-instance/current requires data analyst or 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 {:data nil} when no workspace is loaded"- (with-redefs [ws/instance-workspace (constantly nil)]- (is (= {:data nil}- (mt/user-http-request :crowberto :get 200 "ee/workspace-instance/current"))))))--(deftest current-with-workspace-test- (testing "GET /ee/workspace-instance/current returns the workspace wrapped in {:data ...}"- (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")- data (:data result)]- (is (= "Current Test" (:name data)))- (testing "can_write is true when the workspace is not locked by config"- (is (true? (:can_write data))))- (testing "databases is a map keyed by database id with input_schemas + output"- (is (= 1 (count (:databases data))))- (let [databases (:databases data)- 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 current-with-workspace-locked-returns-can-write-false-test- (testing "GET /ee/workspace-instance/current returns can_write false when locked by config"- (ws.tu/with-workspace-locked-by-config- (fn []- (with-redefs [ws/instance-workspace (constantly {:name "Locked Test" :databases {}})]- (let [data (:data (mt/user-http-request :crowberto :get 200 "ee/workspace-instance/current"))]- (is (= "Locked Test" (:name data)))- (is (false? (:can_write data)))))))))--(deftest current-with-workspace-locked-by-env-var-returns-can-write-false-test- (testing "GET /ee/workspace-instance/current returns can_write false when MB_INSTANCE_WORKSPACE is set"- (mt/with-temp-env-var-value! [:mb-instance-workspace "{\"name\":\"x\",\"databases\":{}}"]- (with-redefs [ws/instance-workspace (constantly {:name "Env Locked Test" :databases {}})]- (let [data (:data (mt/user-http-request :crowberto :get 200 "ee/workspace-instance/current"))]- (is (= "Env Locked Test" (:name data)))- (is (false? (:can_write data))))))))--(deftest post-current-superuser-only-test- (testing "POST /ee/workspace-instance/current requires data analyst or superuser"- (is (= "You don't have permissions to do that."- (mt/user-http-request :rasta :post 403 "ee/workspace-instance/current"- {:name "x" :databases {}})))))--(deftest post-current-round-trip-test- (testing "POST /ee/workspace-instance/current installs the workspace and is readable via GET"- (let [payload {:name "Installed via API"- :databases {(mt/id) {:input_schemas ["PUBLIC"]- :output {:schema "ws_api_install"}}}}- posted (mt/user-http-request :crowberto :post 200 "ee/workspace-instance/current" payload)]- (is (= "Installed via API" (:name posted)))- (testing "the POST response reports can_write true while unlocked"- (is (true? (:can_write posted))))- (testing "GET reflects the workspace just installed"- (let [fetched (:data (mt/user-http-request :crowberto :get 200 "ee/workspace-instance/current"))]- (is (= "Installed via API" (:name fetched)))- (is (= 1 (count (:databases fetched)))))))))--(deftest post-current-rejects-when-locked-test- (testing "POST /ee/workspace-instance/current returns 400 when locked by config"- (ws.tu/with-workspace-locked-by-config- (fn []- (let [resp (mt/user-http-request :crowberto :post 400 "ee/workspace-instance/current"- {:name "x" :databases {}})]- (is (re-find #"config\.yml" (:message resp))))))))--(deftest post-current-rejects-when-env-var-set-test- (testing "POST /ee/workspace-instance/current returns 400 when MB_INSTANCE_WORKSPACE is set"- (mt/with-temp-env-var-value! [:mb-instance-workspace "{\"name\":\"x\",\"databases\":{}}"]- (let [resp (mt/user-http-request :crowberto :post 400 "ee/workspace-instance/current"- {:name "x" :databases {}})]- (is (re-find #"config\.yml" (:message resp)))))))--(deftest delete-current-rejects-when-locked-test- (testing "DELETE /ee/workspace-instance/current returns 400 when locked by config"- (ws.tu/with-workspace-locked-by-config- (fn []- (let [resp (mt/user-http-request :crowberto :delete 400 "ee/workspace-instance/current")]- (is (re-find #"config\.yml" (:message resp))))))))--(deftest delete-current-rejects-when-env-var-set-test- (testing "DELETE /ee/workspace-instance/current returns 400 when MB_INSTANCE_WORKSPACE is set"- (mt/with-temp-env-var-value! [:mb-instance-workspace "{\"name\":\"x\",\"databases\":{}}"]- (let [resp (mt/user-http-request :crowberto :delete 400 "ee/workspace-instance/current")]- (is (re-find #"config\.yml" (:message resp)))))))--(deftest post-current-rejects-non-superuser-before-lock-test- (testing "POST /ee/workspace-instance/current returns 403 (not 400) for a non-superuser even when locked"- (ws.tu/with-workspace-locked-by-config- (fn []- (is (= "You don't have permissions to do that."- (mt/user-http-request :rasta :post 403 "ee/workspace-instance/current"- {:name "x" :databases {}})))))))--(deftest delete-current-rejects-non-superuser-before-lock-test- (testing "DELETE /ee/workspace-instance/current returns 403 (not 400) for a non-superuser even when locked"- (ws.tu/with-workspace-locked-by-config- (fn []- (is (= "You don't have permissions to do that."- (mt/user-http-request :rasta :delete 403 "ee/workspace-instance/current")))))))--(deftest delete-current-superuser-only-test- (testing "DELETE /ee/workspace-instance/current requires data analyst or superuser"- (is (= "You don't have permissions to do that."- (mt/user-http-request :rasta :delete 403 "ee/workspace-instance/current")))))--(deftest delete-current-clears-workspace-test- (testing "DELETE /ee/workspace-instance/current clears any installed workspace"- (ws/set-instance-workspace! {:name "to-clear" :databases {}})- (is (some? (ws/instance-workspace)))- (mt/user-http-request :crowberto :delete 204 "ee/workspace-instance/current")- (is (nil? (ws/instance-workspace)))))--(deftest delete-current-drops-table-remappings-test- (testing "DELETE /ee/workspace-instance/current drops any TableRemapping rows"- (mt/with-model-cleanup [:model/TableRemapping]- (ws/set-instance-workspace! {:name "to-clear" :databases {}})- (t2/insert! :model/TableRemapping {:database_id (mt/id)- :from_schema "public"- :from_table_name "orders"- :to_schema "mb_iso"- :to_table_name "orders"})- (is (t2/exists? :model/TableRemapping :database_id (mt/id)))- (mt/user-http-request :crowberto :delete 204 "ee/workspace-instance/current")- (is (not (t2/exists? :model/TableRemapping :database_id (mt/id)))))))--(deftest table-remappings-superuser-only-test- (testing "GET /ee/workspace-instance/table-remappings requires data analyst or 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.cljdeleted file mode 100644index 78fd98beac0..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_manager_test.clj+++ /dev/null@@ -1,157 +0,0 @@-(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.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-premium-feature [f]- (mt/with-premium-features #{:workspaces}- (f)))--(use-fixtures :each with-premium-feature)--(defn- stub-provisioner []- (reify provisioning/Provisioner- (details [_ _ _ _]- {:schema "mb_iso_stub" :database_details {:user "stub_user" :password "stub_pass"}})- (init! [_ _ _ _] nil)- (grant! [_ _ _ _ _] nil)- (destroy! [_ _ _ _] nil)))--(deftest crud-smoke-test- (testing "create, get, list, delete round-trip"- (mt/with-temp [:model/Database {db-id :id} {:engine :postgres- :details {}- :settings {:database-enable-workspaces true}}]- (mt/with-model-cleanup [:model/Workspace]- (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]- (let [{ws-id :id :as ws} (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"- {:name "Smoke Test" :database_ids [db-id]})]- (is (=? {:id pos-int?- :name "Smoke Test"- :databases [{:database_id db-id}]- :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"- (mt/user-http-request :crowberto :delete 204 (str "ee/workspace-manager/" ws-id))- (mt/user-http-request :crowberto :get 404 (str "ee/workspace-manager/" ws-id)))))))))--(deftest delete-workspace-pending-databases-test- (testing "DELETE tears down a pending database like any other state"- (mt/with-temp [:model/Database {db-id :id} {:engine :postgres- :details {}- :settings {:database-enable-workspaces true}}]- (mt/with-model-cleanup [:model/Workspace]- (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]- (let [{ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"- {:name "Pending" :database_ids [db-id]})- wsd-id (t2/select-one-pk :model/WorkspaceDatabase :workspace_id ws-id)]- (t2/update! :model/WorkspaceDatabase {:id wsd-id} {:status :provisioning})- (mt/user-http-request :crowberto :delete 204 (str "ee/workspace-manager/" ws-id))- (mt/user-http-request :crowberto :get 404 (str "ee/workspace-manager/" ws-id))))))))--(deftest delete-workspace-teardown-failure-test- (testing "DELETE returns a 500 with the database's error and keeps the workspace when a teardown fails"- (mt/with-temp [:model/Database {db-id :id} {:engine :postgres- :details {}- :settings {:database-enable-workspaces true}}]- (mt/with-model-cleanup [:model/Workspace]- (let [{ws-id :id} (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]- (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"- {:name "Unreachable" :database_ids [db-id]}))- boom (reify provisioning/Provisioner- (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {:user "stub_user"}})- (init! [_ _ _ _] nil)- (grant! [_ _ _ _ _] nil)- (destroy! [_ _ _ _] (throw (ex-info "Connection refused" {}))))]- (with-redefs [provisioning/dispatching-provisioner boom]- (is (=? {:message "Connection refused"}- (mt/user-http-request :crowberto :delete 500 (str "ee/workspace-manager/" ws-id)))))- (testing "the workspace survives and a retry deletes it"- (mt/user-http-request :crowberto :get 200 (str "ee/workspace-manager/" ws-id))- (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]- (mt/user-http-request :crowberto :delete 204 (str "ee/workspace-manager/" ws-id)))- (mt/user-http-request :crowberto :get 404 (str "ee/workspace-manager/" ws-id))))))))--(deftest create-workspace-with-database-ids-test- (testing "POST / attaches and provisions the given databases (each must be eligible) with their schemas"- (mt/with-temp [:model/Database {eligible-id :id} {:engine :postgres- :details {}- :settings {:database-enable-workspaces true}}- :model/Table _ {:db_id eligible-id :schema "public" :active true}- :model/Table _ {:db_id eligible-id :schema "analytics" :active true}- :model/Database {ineligible-id :id} {:engine :postgres :details {}}]- (mt/with-model-cleanup [:model/Workspace]- (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]- (is (=? {:databases [{:database_id eligible-id- :input_schemas ["analytics" "public"]- :status "provisioned"}]}- (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"- {:name "With dbs" :database_ids [eligible-id]})))- (testing "an ineligible database is rejected"- (mt/user-http-request :crowberto :post 400 "ee/workspace-manager/"- {:name "Nope" :database_ids [ineligible-id]}))- (testing "at least one database is required"- (mt/user-http-request :crowberto :post 400 "ee/workspace-manager/"- {:name "Nope" :database_ids []})))))))--(deftest rename-workspace-test- (testing "PUT /:id renames a workspace and returns the updated WorkspaceResponse"- (mt/with-temp [:model/Workspace {ws-id :id} {:name "Before"}]- (is (=? {:id ws-id :name "After"}- (mt/user-http-request :crowberto :put 200 (str "ee/workspace-manager/" ws-id) {:name "After"})))- (is (= "After" (t2/select-one-fn :name :model/Workspace :id ws-id)))- (testing "404 for a missing id"- (mt/user-http-request :crowberto :put 404 "ee/workspace-manager/13371337" {:name "X"})))))--(deftest download-config-endpoint-test- (testing "GET /:id/config"- (mt/with-temp [:model/Database {db-id :id} {:engine :postgres :details {}}- :model/Workspace {ws-id :id} {:name "Cfg WS"}- :model/WorkspaceDatabase _ {:workspace_id ws-id- :database_id db-id- :database_details {}- :output_namespace ""- :input_schemas ["public"]- :status :provisioned}]- (testing "returns application/x-yaml with an attachment Content-Disposition"- (is (=? {:status 200- :headers {"Content-Type" "application/x-yaml"- "Content-Disposition" "attachment; filename=\"config.yml\""}}- (mt/user-http-request-full-response- :crowberto :get 200 (str "ee/workspace-manager/" ws-id "/config")))))- (testing "409 when a database is not :provisioned"- (t2/update! :model/WorkspaceDatabase :workspace_id ws-id {:status :unprovisioned})- (mt/user-http-request :crowberto :get 409 (str "ee/workspace-manager/" ws-id "/config")))- (testing "404 for a missing workspace"- (mt/user-http-request :crowberto :get 404 "ee/workspace-manager/13371337/config")))))--(defmacro ^:private with-data-analyst [& body]- `(perms.test-util/with-data-analyst-role! (mt/user->id :rasta) ~@body))--(deftest endpoints-are-superuser-only-test- (testing "All workspace-manager endpoints reject non-superusers, even Data Analysts"- (mt/with-temp [:model/Workspace {ws-id :id} {}]- (with-data-analyst- (mt/user-http-request :rasta :get 403 "ee/workspace-manager/")- (mt/user-http-request :rasta :get 403 (str "ee/workspace-manager/" ws-id))- (mt/user-http-request :rasta :post 403 "ee/workspace-manager/"- {:name "Nope" :database_ids [(mt/id)]})- (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))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/config_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/config_test.cljdeleted file mode 100644index 885e650729a..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/config_test.clj+++ /dev/null@@ -1,269 +0,0 @@-(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]- [metabase.util :as u]))--(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"- (let [own-dbs (remove (some-fn :is_stub :is_sample) (-> cfg :config :databases))- db (first own-dbs)]- (is (= 1 (count own-dbs)))- (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"- :let-user-control-scheduling false}- (:details db))))))- (testing "workspace entry uses flat input_schemas + expanded :output map"- (is (= "github" (-> cfg :config :workspace :name)))- (is (= {"Analytics Data Warehouse"- {:input_schemas ["raw_github"]- :output {:schema "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 {:db "AnalyticsDB" :schema "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 (empty? (remove (some-fn :is_stub :is_sample) (-> cfg :config :databases)))- "no non-stub, non-sample databases since the workspace has none of its own")- (is (= {} (-> cfg :config :workspace :databases)))))))--(deftest build-workspace-config-injects-stubs-for-non-workspace-dbs-test- (testing "Databases that exist in the instance but are not provisioned for the workspace appear- in :config.databases as stub entries with :is_stub true and empty :details.- Sample, audit, and routing-target DBs are excluded."- (mt/with-temp [:model/Database router {:name "stub-test-router" :engine :postgres :details {}}- :model/Database _other {:name "stub-test-other" :engine :postgres :details {:host "x"}}- :model/Database _sample {:name "stub-test-sample" :engine :postgres :details {} :is_sample true}- :model/Database _audit {:name "stub-test-audit" :engine :postgres :details {} :is_audit true}- :model/Database _target {:name "stub-test-target" :engine :postgres :details {} :router_database_id (:id router)}- :model/Database ws-db {:name "stub-test-ws-db" :engine :postgres :details {:host "y"}}- :model/Workspace {ws-id :id} {:name "stub-test-ws"- :creator_id (mt/user->id :crowberto)}- :model/WorkspaceDatabase _ {:workspace_id ws-id- :database_id (:id ws-db)- :database_details {}- :output_namespace ""- :input_schemas ["public"]- :status :provisioned}]- (let [cfg-dbs (-> (config/build-workspace-config ws-id) :config :databases)- by-name (into {} (map (juxt :name identity)) cfg-dbs)]- (testing "workspace's own database is present and is NOT a stub"- (is (contains? by-name "stub-test-ws-db"))- (is (not (:is_stub (get by-name "stub-test-ws-db")))))- (testing "non-workspace plain DB appears as a stub with empty :details"- (is (= {:name "stub-test-other"- :engine :postgres- :details {}- :is_stub true}- (get by-name "stub-test-other"))))- (testing "router DB (not a routing target) also appears as a stub"- (is (= {:name "stub-test-router"- :engine :postgres- :details {}- :is_stub true}- (get by-name "stub-test-router"))))- (testing "sample, audit, and routing-target DBs are excluded"- (is (not (contains? by-name "stub-test-sample")))- (is (not (contains? by-name "stub-test-audit")))- (is (not (contains? by-name "stub-test-target"))))))))--(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))))))--(deftest build-workspace-config-forces-let-user-control-scheduling-false-test- (testing "Non-stub database entries emit :let-user-control-scheduling false even if the source- database had it true. The YAML carries :details only — not the schedule cron columns —- so on import infer-db-schedules must take the auto-schedule branch instead of asserting- on missing cache_field_values_schedule / metadata_sync_schedule."- (mt/with-temp [:model/Database {db-id :id}- {:name "User-Scheduled DW"- :engine :postgres- :details {:host "h"- :port 5432- :let-user-control-scheduling true}}- :model/Workspace {ws-id :id} {:name "sched"- :creator_id (mt/user->id :crowberto)}- :model/WorkspaceDatabase _- {:workspace_id ws-id- :database_id db-id- :database_details {:user "u" :password "p"}- :output_namespace "ws_alice"- :input_schemas ["public"]- :status :provisioned}]- (let [own-db (->> (config/build-workspace-config ws-id)- :config- :databases- (remove (some-fn :is_stub :is_sample))- first)]- (is (false? (get-in own-db [:details :let-user-control-scheduling])))))))--(deftest build-workspace-config-schemaless-engine-omits-filters-test- (testing "MySQL (no schemas) omits schema-filters-*/dataset-filters-* entirely (the :else {} branch)"- (mt/with-temp [:model/Database {db-id :id}- {:name "MySQL DW" :engine :mysql :details {:dbname "dw"}}- :model/Workspace {ws-id :id} {:name "mysql-ws"- :creator_id (mt/user->id :crowberto)}- :model/WorkspaceDatabase _- {:workspace_id ws-id- :database_id db-id- :database_details {:user "u" :password "p"}- :output_namespace "ws_alice"- :input_schemas ["public"]- :status :provisioned}]- ;; Select the MySQL entry explicitly rather than relying on `first` -- `:databases` also carries- ;; stub/sample entries (e.g. a pre-existing Sample Database), so its ordering isn't something this- ;; test should depend on.- (let [details (->> (config/build-workspace-config ws-id) :config :databases- (u/seek #(= :mysql (:engine %)))- :details)]- (is (nil? (:schema-filters-type details)))- (is (nil? (:schema-filters-patterns details)))- (is (nil? (:dataset-filters-type details)))- (is (nil? (:dataset-filters-patterns details)))))))--(deftest build-workspace-config-emits-sample-database-test- (testing "When a Sample Database exists in the instance, /config emits an entry with- standardized name/engine, empty :details, and :is_sample true. The entry is- distinct from stub entries and does not depend on the sample DB's actual- stored name/engine."- (mt/with-temp [:model/Database _sample {:name "Some Renamed Sample"- :engine :h2- :details {:db "real-sample-details"}- :is_sample true}- :model/Workspace {ws-id :id} {:name "sample-emit-ws"- :creator_id (mt/user->id :crowberto)}]- (let [cfg-dbs (-> (config/build-workspace-config ws-id) :config :databases)- samples (filter :is_sample cfg-dbs)]- (testing "exactly one sample entry is emitted"- (is (= 1 (count samples))))- (testing "sample entry uses standardized name/engine with empty :details"- (is (= {:name "Sample Database"- :engine "h2"- :details {}- :is_sample true}- (first samples))))- (testing "sample entry is NOT also marked as a stub"- (is (not (:is_stub (first samples)))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/core_test.cljdeleted file mode 100644index a67e18955d1..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/core_test.clj+++ /dev/null@@ -1,316 +0,0 @@-(ns metabase-enterprise.workspaces.core-test- "Tests for the workspace programmatic API and lifecycle rules."- (:require- [clojure.string :as str]- [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])- (:import- (clojure.lang ExceptionInfo)))--(set! *warn-on-reflection* true)--(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- (details [_ _ _ _]- {:schema "mb_iso_stub" :database_details {:user "stub_user" :password "stub_pass"}})- (init! [_ _ _ _] nil)- (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)))))--(deftest create-workspace-provisions-databases-test- (testing "create-workspace! attaches and provisions the given databases"- (mt/with-temp [:model/Database {db-id :id} {:engine :postgres- :details {}- :settings {:database-enable-workspaces true}}- :model/Table _ {:db_id db-id :schema "public" :active true}- :model/Database {ineligible-id :id} {:engine :postgres :details {}}]- (mt/with-model-cleanup [:model/Workspace]- (testing "an ineligible database is rejected"- (is (thrown-with-msg? ExceptionInfo #"Workspaces are not enabled for this database"- (ws/create-workspace! {:name "Nope"- :creator_id (mt/user->id :crowberto)- :database_ids [ineligible-id]}))))- (testing "a missing database is rejected"- (is (thrown-with-msg? ExceptionInfo #"Database not found"- (ws/create-workspace! {:name "Nope"- :creator_id (mt/user->id :crowberto)- :database_ids [Integer/MAX_VALUE]}))))- (testing "provisioning failure cleans up: databases torn down, workspace and rows deleted"- (let [failing-provisioner (reify provisioning/Provisioner- (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {:user "stub_user"}})- (init! [_ _ _ _] (throw (ex-info "boom" {})))- (grant! [_ _ _ _ _] nil)- (destroy! [_ _ _ _] nil))]- (with-redefs [provisioning/dispatching-provisioner failing-provisioner]- (is (thrown-with-msg? ExceptionInfo #"boom"- (ws/create-workspace! {:name "Boom"- :creator_id (mt/user->id :crowberto)- :database_ids [db-id]}))))- (is (not (t2/exists? :model/Workspace :name "Boom"))- "the failed create must not leave a Workspace row behind")- (is (not (t2/exists? :model/WorkspaceDatabase :database_id db-id))- "the failed create must not leave WorkspaceDatabase rows behind")))- (testing "provisioning failure whose cleanup also fails keeps the workspace and returns it for retry"- (let [wedged (reify provisioning/Provisioner- (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {:user "stub_user"}})- (init! [_ _ _ _] (throw (ex-info "boom" {})))- (grant! [_ _ _ _ _] nil)- (destroy! [_ _ _ _] (throw (ex-info "warehouse down" {}))))]- (with-redefs [provisioning/dispatching-provisioner wedged]- (is (=? {:name "Wedged"- :databases [{:status :unprovisioned}]}- (ws/create-workspace! {:name "Wedged"- :creator_id (mt/user->id :crowberto)- :database_ids [db-id]}))- "the created workspace is returned even though provisioning failed and cleanup could not finish"))- (let [ws (t2/select-one :model/Workspace :name "Wedged")]- (is (some? ws)- "the workspace must be kept while any database row remains")- (is (=? [{:status :unprovisioned}]- (t2/select :model/WorkspaceDatabase :workspace_id (:id ws)))- "the row whose teardown failed is kept, forced :unprovisioned")- ;; retry the teardown via delete once the warehouse is back- (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]- (is (nil? (ws/delete-workspace! (:id ws)))))- (is (nil? (ws/get-workspace (:id ws)))))))- (testing "success: the attached database comes back :provisioned"- (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]- (let [ws (ws/create-workspace! {:name "Provisioned"- :creator_id (mt/user->id :crowberto)- :database_ids [db-id]})]- (is (=? [{:database_id db-id- :input_schemas ["public"]- :status :provisioned}]- (:databases ws))))))))))--(defn- add-database!- "Test helper: insert a WorkspaceDatabase row for `workspace-id` and provision it- (blocking). Replaces the removed production add-database! for test setup."- [workspace-id database-id input-schemas]- (t2/with-transaction [_conn]- (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 ""})]- (provisioning/provision-single! wsd-id))))--;;; ----------------------------------------- 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)]- (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)})]- (is (nil? (ws/delete-workspace! (:id ws))))- (is (nil? (ws/get-workspace (:id ws))))))))--(deftest delete-workspace-with-pending-status-test- (testing "a workspace database still :provisioning/:deprovisioning is torn down like any other state"- (doseq [pending-status [:provisioning :deprovisioning]]- (testing (str "status " pending-status)- (mt/with-model-cleanup [:model/Workspace]- (let [ws (ws/create-workspace! {:name (str "Pending " (name pending-status))- :creator_id (mt/user->id :crowberto)})- _ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]- (add-database! (:id ws) (mt/id) ["PUBLIC"]))- wsd-id (t2/select-one-pk :model/WorkspaceDatabase :workspace_id (:id ws))]- ;; force the row into the pending state. :provisioning rows that crashed- ;; never stored their iso details, so clear them too.- (t2/update! :model/WorkspaceDatabase {:id wsd-id}- (cond-> {:status pending-status}- (= pending-status :provisioning)- (assoc :output_namespace "" :database_details {})))- (let [destroyed? (atom false)- recording (reify provisioning/Provisioner- (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {:user "stub_user"}})- (init! [_ _ _ _] nil)- (grant! [_ _ _ _ _] nil)- (destroy! [_ _ _ _] (reset! destroyed? true) nil))]- (with-redefs [provisioning/dispatching-provisioner recording]- (is (nil? (ws/delete-workspace! (:id ws)))))- (is (true? @destroyed?)- "pending rows get a real warehouse teardown, not an app-DB-only removal")- (is (nil? (ws/get-workspace (:id ws))))- (is (not (t2/exists? :model/WorkspaceDatabase :id wsd-id))))))))))--(deftest delete-workspace-teardown-failure-test- (testing "when teardown fails, nothing is deleted: the row and the workspace are kept for retry"- (mt/with-model-cleanup [:model/Workspace]- (let [ws (ws/create-workspace! {:name "Unreachable WS" :creator_id (mt/user->id :crowberto)})- _ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]- (add-database! (:id ws) (mt/id) ["PUBLIC"]))- wsd-id (t2/select-one-pk :model/WorkspaceDatabase :workspace_id (:id ws))- boom (reify provisioning/Provisioner- (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {:user "stub_user"}})- (init! [_ _ _ _] nil)- (grant! [_ _ _ _ _] nil)- (destroy! [_ _ _ _] (throw (ex-info "Connection refused" {}))))]- (with-redefs [provisioning/dispatching-provisioner boom]- (is (thrown-with-msg? ExceptionInfo #"Connection refused"- (ws/delete-workspace! (:id ws)))- "the combined teardown failure is thrown, carrying what the database returned"))- (is (some? (ws/get-workspace (:id ws)))- "the workspace must be kept when any teardown fails")- (is (=? {:status :unprovisioned}- (t2/select-one :model/WorkspaceDatabase :id wsd-id))- "the failed row is kept, forced :unprovisioned")- (testing "retrying the delete once the warehouse is reachable finishes the job"- (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]- (is (nil? (ws/delete-workspace! (:id ws)))))- (is (nil? (ws/get-workspace (:id ws))))- (is (not (t2/exists? :model/WorkspaceDatabase :id wsd-id))))))))--(deftest delete-workspace-combines-teardown-failures-test- (testing "every database gets its teardown attempt and all failures come back as one combined exception"- (mt/with-temp [:model/Database {db2-id :id} {:engine :postgres- :details {}- :settings {:database-enable-workspaces true}}]- (mt/with-model-cleanup [:model/Workspace]- (let [ws (ws/create-workspace! {:name "Multi fail" :creator_id (mt/user->id :crowberto)})- _ (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]- (add-database! (:id ws) (mt/id) ["PUBLIC"])- (add-database! (:id ws) db2-id ["public"]))- boom (reify provisioning/Provisioner- (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {:user "stub_user"}})- (init! [_ _ _ _] nil)- (grant! [_ _ _ _ _] nil)- (destroy! [_ _ db _] (throw (ex-info (str "down: " (:id db)) {}))))- e (with-redefs [provisioning/dispatching-provisioner boom]- (try- (ws/delete-workspace! (:id ws))- nil- (catch Throwable t t)))]- (is (some? e))- (is (= #{(str "down: " (mt/id)) (str "down: " db2-id)}- (set (str/split (ex-message e) #"; ")))- "the message joins what each database returned")- (is (some? (ex-cause e))- "the first failure is the cause")- (is (= 1 (count (.getSuppressed ^Throwable e)))- "the remaining failures are suppressed")- (is (= 2 (t2/count :model/WorkspaceDatabase :workspace_id (:id ws)))- "both rows are kept for retry")- (with-redefs [provisioning/dispatching-provisioner (stub-provisioner)]- (is (nil? (ws/delete-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)))))--;;; ----------------------------------------- workspace-instance lock --------------------------------------------(defn- lock-atom- "Reach through the private var to the lock atom."- []- @#'ws/locked-by-config?*)--(deftest workspace-locked-by-config?-baseline-test- (testing "default state (no atom flip, no env-var) is unlocked"- (let [a (lock-atom)- prior @a]- (try- (reset! a false)- (is (false? (ws/workspace-locked-by-config?)))- (finally (reset! a prior))))))--(deftest workspace-locked-by-config?-atom-flipped-test- (testing "after mark-locked-by-config!, the predicate returns true"- (workspaces.tu/with-workspace-locked-by-config- (fn [] (is (true? (ws/workspace-locked-by-config?)))))))--(deftest mark-locked-by-config!-flips-the-atom-test- (testing "mark-locked-by-config! flips the atom to true"- (let [a (lock-atom)- prior @a]- (try- (reset! a false)- (is (false? (ws/workspace-locked-by-config?)))- (ws/mark-locked-by-config!)- (is (true? @a))- (is (true? (ws/workspace-locked-by-config?)))- (finally (reset! a prior))))))--(deftest env-var-presence-locks-test- (testing "MB_INSTANCE_WORKSPACE set => predicate true regardless of atom"- (let [a (lock-atom)- prior @a]- (try- (reset! a false)- (mt/with-temp-env-var-value! [mb-instance-workspace "{\"name\":\"x\",\"databases\":{}}"]- (is (true? (ws/workspace-locked-by-config?))))- (testing "outside the binding, atom alone determines the lock"- (is (false? (ws/workspace-locked-by-config?))))- (finally (reset! a prior))))))--(deftest env-var-empty-string-does-not-lock-test- (testing "MB_INSTANCE_WORKSPACE set to empty string does NOT lock (env-var-value treats blank as unset)"- (let [a (lock-atom)- prior @a]- (try- (reset! a false)- (mt/with-temp-env-var-value! [mb-instance-workspace ""]- (is (false? (ws/workspace-locked-by-config?))))- (finally (reset! a prior))))))--(deftest env-var-and-atom-both-lock-test- (testing "both sources set => still true; OR semantics"- (mt/with-temp-env-var-value! [mb-instance-workspace "{\"name\":\"x\",\"databases\":{}}"]- (workspaces.tu/with-workspace-locked-by-config- (fn [] (is (true? (ws/workspace-locked-by-config?))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/driver_isolation_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/driver_isolation_test.cljdeleted file mode 100644index 4afc97080c1..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/driver_isolation_test.clj+++ /dev/null@@ -1,1160 +0,0 @@-(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.string :as str]- [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))--(defn- with-isolation-details- "Merge [[driver/workspace-isolation-details]] into `workspace`. Details are pure- computation, so tests build the full workspace map before the `try` — the- `finally` destroy then always has real identifiers to clean up with, even when- init never ran or only partially succeeded."- [driver database workspace]- (merge workspace (driver/workspace-isolation-details driver database workspace)))--(defn- workspace-user-spec- "JDBC spec that connects as the workspace's isolated user."- [driver details workspace]- (sql-jdbc.conn/connection-details->spec driver (merge details (:database_details workspace))))--(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)}- workspace (with-isolation-details driver database 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')")])- (driver/init-workspace-isolation! driver database workspace)- (let [user-spec (workspace-user-spec driver details workspace)- out-schema (:schema workspace)- out (qualify driver out-schema out-name)]- (driver/grant-workspace-read-access! driver database workspace- [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 workspace- [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 workspace)- (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 workspace)- (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)}- ws-a (with-isolation-details driver database ws-a)- ws-b (with-isolation-details driver database 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')")])- (driver/init-workspace-isolation! driver database ws-a)- (driver/init-workspace-isolation! driver database ws-b)- (let [user-a-spec (workspace-user-spec driver details ws-a)- user-b-spec (workspace-user-spec driver details ws-b)- out-a-schema (:schema ws-a)- out-b-schema (:schema ws-b)- 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- [in-schema-a])- (driver/grant-workspace-read-access! driver database ws-b- [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)- (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)- (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)}- workspace (with-isolation-details driver database workspace)- out-schema (:schema 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)- (driver/init-workspace-isolation! driver database workspace)- (let [user-spec (workspace-user-spec driver details workspace)- out (qualify driver out-schema out-name)]- (driver/grant-workspace-read-access! driver database workspace- [in-schema])- (testing "init succeeded against the pre-existing namespace"- ;; init returns nil under the new contract; getting here without a- ;; throw is the success signal. Sanity-check the details it ran with.- (is (some? (:schema workspace))))- (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 workspace)- (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)}- workspace (with-isolation-details driver database 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')")])- (driver/init-workspace-isolation! driver database workspace)- (let [user-spec (workspace-user-spec driver details workspace)]- ;; First grant: only A.- (driver/grant-workspace-read-access! driver database workspace- [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 workspace- [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 workspace)- (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)}- workspace (with-isolation-details driver database 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]))- (driver/init-workspace-isolation! driver database workspace)- (let [user-spec (workspace-user-spec driver details workspace)]- (driver/grant-workspace-read-access! driver database workspace- [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 workspace)- (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)}- workspace (with-isolation-details driver database workspace)- ws-fragment (:schema 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))])- (driver/init-workspace-isolation! driver database workspace)- (driver/grant-workspace-read-access! driver database workspace- [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 (get-in workspace [:database_details :user])- 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 workspace)- (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)))))))--;; --- Password leak probes (GHY-3660) -------------------------------------------(defn- reachable-throwables- "Every Throwable reachable from `t`: the cause chain, suppressed exceptions,- Throwable-valued `ex-data` entries, and `SQLException.getNextException`- links (the path the GHY-3660 leak took — the JDBC batch exception's message- echoed the aborted CREATE USER statement, password included)."- [^Throwable t]- (loop [queue [t], seen #{}, out []]- (if (empty? queue)- out- (let [^Throwable cur (peek queue)- queue (pop queue)]- (if (or (nil? cur) (contains? seen cur))- (recur queue seen out)- (recur (into queue- (remove nil?)- (concat [(.getCause cur)]- (.getSuppressed cur)- (filter #(instance? Throwable %) (vals (or (ex-data cur) {})))- (when (instance? java.sql.SQLException cur)- [(.getNextException ^java.sql.SQLException cur)])))- (conj seen cur)- (conj out cur)))))))--(defn- throwable-log-text- "Everything a logger or debug print could plausibly render for `t`: the full- stack-trace printout, `Throwable->map`, and the message + `ex-data` of every- [[reachable-throwables]] node."- [^Throwable t]- (let [sw (java.io.StringWriter.)]- (.printStackTrace t (java.io.PrintWriter. sw))- (str/join "\n"- (into [(str sw) (pr-str (Throwable->map t))]- (map (fn [^Throwable e]- (str (ex-message e) " " (pr-str (ex-data e)))))- (reachable-throwables t)))))--(deftest ^:synchronized init-workspace-isolation-never-exposes-password-test- ;; GHY-3660: a failed CREATE USER logged the generated password verbatim — the- ;; postgres JDBC batch exception echoes the aborted statement, password and- ;; all, and provisioning logs that exception. Workspace passwords are- ;; generated once, so a logged password may be the live credential.- ;;- ;; Reproduce the failure for real: run `init-workspace-isolation!` for a- ;; second workspace over a connection authenticated as an existing workspace's- ;; low-privilege user, which by design cannot CREATE USER. Then assert the- ;; generated password appears nowhere in any text a logger could print from- ;; the thrown exception.- (mt/test-drivers (filter #(isa? driver/hierarchy % :sql-jdbc)- (mt/normal-drivers-with-feature :workspace))- (testing "failed init-workspace-isolation! never exposes the generated password (GHY-3660)"- (let [driver driver/*driver*- database (mt/db)- details (:details database)- admin-spec (sql-jdbc.conn/connection-details->spec driver details)- run-id (random-suffix)- ws-a {:id (Long/parseLong run-id 16)- :name (str "wsd-pwleak-a-" run-id)}- ws-b {:id (inc (Long/parseLong run-id 16))- :name (str "wsd-pwleak-b-" run-id)}- b-ns (driver.u/workspace-isolation-namespace-name ws-b)- generated (atom [])- gen-orig driver.u/random-workspace-password- ;; Details are pure computation, so build the full ws map before the- ;; `try` — the `finally` destroy always has real identifiers to work- ;; with even if init never ran or only partially succeeded.- ws-a (with-isolation-details driver database ws-a)]- (try- (driver/init-workspace-isolation! driver database ws-a)- ;; MySQL: the workspace user's JDBC handshake needs access to the- ;; bound DB (see [[reuse-bound-db-as-input?]]), which comes from a- ;; read grant.- (when (reuse-bound-db-as-input? driver)- (driver/grant-workspace-read-access! driver database ws-a- [(-> database :details :db)]))- ;; The ticket's failure shape is a caller that CAN create schemas but- ;; CANNOT create users: postgres/redshift check CREATE at the database- ;; level, so grant it to user A — execution then reaches the- ;; password-bearing CREATE USER, which fails with permission denied.- ;; Elsewhere, pre-create ws-b's namespace so the leading- ;; `CREATE ... IF NOT EXISTS` can skip instead of failing. That only- ;; helps engines that check existence before privileges (SQL Server's- ;; guarded EXEC); MySQL and ClickHouse check privileges regardless,- ;; so on those the batch dies on the namespace statement and never- ;; reaches CREATE USER — the leak assertion still covers whatever- ;; exception surfaces, it just can't prove the echo path there.- (when (#{:postgres :redshift} driver)- (jdbc/execute! admin-spec [(format "GRANT CREATE ON DATABASE \"%s\" TO \"%s\""- (or (:dbname details) (:db details))- (driver.u/workspace-isolation-user-name ws-a))]))- (jdbc/execute! admin-spec [(create-input-namespace-sql driver b-ns)])- (mt/with-temp [:model/Database low-priv-db {:engine driver- :details (merge details (:database_details ws-a))}]- ;; The password is generated by `workspace-isolation-details`, so- ;; compute ws-b's details inside the instrumented redef.- (let [thrown (with-redefs [driver.u/random-workspace-password- (fn []- (let [pw (gen-orig)]- (swap! generated conj pw)- pw))]- (try- (driver/init-workspace-isolation!- driver low-priv-db- (with-isolation-details driver low-priv-db ws-b))- nil- (catch Throwable t t)))]- (sql-jdbc.conn/invalidate-pool-for-db! low-priv-db)- (is (some? thrown)- "init-workspace-isolation! over a low-privilege connection should have thrown")- (is (seq @generated)- "the instrumented password generator should have been called")- (when thrown- (let [text (throwable-log-text thrown)]- ;; ws-a's password is also live here — it authenticates the- ;; low-priv pool, and connection failures can stash details- ;; maps in ex-data.- (doseq [pw (cons (get-in ws-a [:database_details :password]) @generated)- :when (some? pw)]- (is (not (str/includes? text pw))- (str "workspace password leaked into exception text:\n" text)))- ;; init unwraps the JDBC batch envelope (which echoed the- ;; aborted CREATE USER statement) and rethrows the scrubbed- ;; server error. Requiring the create-role permission error- ;; proves this test reached CREATE USER rather than failing- ;; earlier and passing vacuously. Only these two drivers get- ;; the database-level CREATE grant that lets execution reach- ;; CREATE USER.- (when (#{:postgres :redshift} driver)- (is (re-find #"(?i)permission denied|must be superuser|not authorized|privilege" text)- (str "expected the CREATE USER permission error in exception text:\n" text)))))))- (finally- ;; If init for ws-b unexpectedly succeeded (isolation regression),- ;; a user with a live password exists — tear it down too. Also- ;; drops ws-b's pre-created namespace; the loop below is backup.- (try (driver/destroy-workspace-isolation!- driver database- (merge ws-b {:schema b-ns- :database_details {:user (driver.u/workspace-isolation-user-name ws-b)}}))- (catch Throwable _ nil))- (doseq [sql (drop-input-namespace-sqls driver b-ns [])]- (try (jdbc/execute! admin-spec [sql]) (catch Throwable _ nil)))- ;; Redshift's destroy has no DROP OWNED BY equivalent, so the- ;; database-level CREATE granted above would block DROP USER there- ;; (and roll back destroy's whole cleanup batch). Revoke before- ;; destroying; postgres doesn't need it (DROP OWNED BY) but it's- ;; harmless.- (when (#{:postgres :redshift} driver)- (try (jdbc/execute! admin-spec [(format "REVOKE CREATE ON DATABASE \"%s\" FROM \"%s\""- (or (:dbname details) (:db details))- (driver.u/workspace-isolation-user-name ws-a))])- (catch Throwable _ nil)))- (try (driver/destroy-workspace-isolation! driver database ws-a)- (catch Throwable t- (log/warnf t "destroy-workspace-isolation! failed for %s during password-leak test cleanup"- driver)))))))))--(deftest ^:synchronized reprovision-resets-workspace-password-test- ;; A workspace user can survive a failed teardown (the row is kept and later- ;; re-provisioned). Identifiers are deterministic but the password is random- ;; per provisioning attempt, so init must reset the surviving user's password- ;; — engines whose `CREATE USER IF NOT EXISTS` silently no-ops need an- ;; explicit ALTER, or the persisted password stops matching the warehouse.- (mt/test-drivers (filter #(isa? driver/hierarchy % :sql-jdbc)- (mt/normal-drivers-with-feature :workspace))- (testing "re-running init for the same workspace id resets the user's password"- (let [driver driver/*driver*- database (mt/db)- details (:details database)- run-id (random-suffix)- workspace {:id (Long/parseLong run-id 16)- :name (str "wsd-repw-" run-id)}- ;; same deterministic identifiers, two different random passwords- first-ws (with-isolation-details driver database workspace)- second-ws (with-isolation-details driver database workspace)]- (try- (driver/init-workspace-isolation! driver database first-ws)- (driver/init-workspace-isolation! driver database second-ws)- ;; MySQL: the workspace user's JDBC handshake needs access to the- ;; bound DB (see [[reuse-bound-db-as-input?]]).- (when (reuse-bound-db-as-input? driver)- (driver/grant-workspace-read-access! driver database second-ws- [(-> database :details :db)]))- (testing "the credentials persisted by the second init authenticate"- (is (= [{:one 1}]- (jdbc/query (workspace-user-spec driver details second-ws)- ["SELECT 1 AS one"]))))- (finally- (try (driver/destroy-workspace-isolation! driver database second-ws)- (catch Throwable t- (log/warnf t "destroy-workspace-isolation! failed for %s during password-reset test cleanup"- driver)))))))))--(deftest ^:synchronized grant-failure-is-not-a-batch-envelope-test- ;; Every SQL-JDBC grant-workspace-read-access! impl batches its statements- ;; and rethrows the per-statement exception via `driver.u/batch-exception`,- ;; so a failed grant reads as the plain server error instead of "Batch entry- ;; N ... Call getNextException to see other errors in the batch."- (mt/test-drivers (filter #(isa? driver/hierarchy % :sql-jdbc) (mt/normal-drivers-with-feature :workspace))- (testing "a failed grant! surfaces the underlying server error, not a JDBC batch envelope"- (let [schema (case driver/*driver*- :postgres "public"- :h2 "PUBLIC"- "nonexistent_input_schema")- workspace {:id (rand-int Integer/MAX_VALUE)- :name "ws-grant-envelope"- :database_details {:user "mb_no_such_user_x1"- :role "mb_no_such_role_x1"}}- thrown (try- (driver/grant-workspace-read-access! driver/*driver* (mt/db) workspace [schema])- nil- (catch Throwable t t))]- (is (some? thrown)- "granting to a nonexistent user should have thrown")- (when thrown- (let [sql-ex (find-sql-exception thrown)]- (when sql-ex- (is (not (re-find #"Batch entry|getNextException" (str (ex-message sql-ex))))- (str "the JDBC batch envelope wording should not leak into the message:\n"- (ex-message sql-ex))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.cljdeleted file mode 100644index be3e048119c..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj+++ /dev/null@@ -1,831 +0,0 @@-(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,- and passes it to `advanced-config.file/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.provisioning :as provisioning]- [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- ;; currently this is very flaky in CI causing the entire bigquery driver- ;; to be quarantined. for now we disable just this one test so we can- ;; bring back the rest of it, but this still needs investigation.- #_: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)--(defn- add-database!- "Test helper: insert a WorkspaceDatabase row for `workspace-id` and provision it- (blocking). Replaces the removed production add-database! for test setup."- [workspace-id database-id input-schemas]- (t2/with-transaction [_conn]- (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 ""})]- (provisioning/provision-single! wsd-id))))--;;; -------------------- 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 the `instance-workspace` setting is process-wide state-;; backed by the shared test app DB; running concurrently with other workspace-mode-;; tests would cross-pollute.-(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 :config-text-file :transforms-basic :hosting}- ;; 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.- (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: feed the parsed YAML to 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 writes the `instance-workspace` setting).- (advanced-config.file/initialize! (yaml/parse-string yaml-str))- ;; Diagnostic: the loader should have written the workspace-instance- ;; setting 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 "- "workspace-isolation-details 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 (into [] (: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"))))- (testing "DELETE /api/transform/:id/table succeeds for the workspace user"- (let [outcome (try (mt/user-http-request :crowberto :delete 204- (format "transform/%d/table" (:id transform)))- ::ok- (catch Throwable t [::failed (ex-message t)]))]- (is (= ::ok outcome)- (str "deleting the transform output table failed; outcome=" (pr-str outcome))))))))))- (finally- ;; Clear the `instance-workspace` setting (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 tears down every database (any state, 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. A failed warehouse- ;; teardown throws and keeps the workspace + rows for retry. For test- ;; cleanup we don't retry: fall back to force-clearing the rows so- ;; `mt/with-temp Database` cleanup can complete. Real fix for the- ;; Redshift destroy fragility is tracked separately.- (let [deleted? (try (ws/delete-workspace! ws-id)- true- (catch Throwable t- (log/warnf t "delete-workspace! left workspace %d behind; force-clearing WSD"- ws-id)- false))]- (when-not deleted?- (t2/delete! :model/WorkspaceDatabase :workspace_id ws-id)- (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 :config-text-file}- (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- (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)]- (advanced-config.file/initialize! reparsed)- (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.cljdeleted file mode 100644index 0e868083cdc..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_database_test.clj+++ /dev/null@@ -1,181 +0,0 @@-(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.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 =========================================-;;;-;;; All WorkspaceDatabase permission predicates are superuser-only.--(defmacro ^:private with-normal-user [& body]- `(mt/with-test-user :rasta ~@body))--(defmacro ^:private with-admin [& body]- `(mt/with-test-user :crowberto ~@body))--(defmacro ^:private with-data-analyst [& body]- `(perms.test-util/with-data-analyst-role! (mt/user->id :rasta)- (mt/with-test-user :rasta ~@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-superuser — false"- (with-normal-user (is (false? (mi/can-read? wsd)))))- (testing "Data Analyst (non-superuser) — false"- (with-data-analyst (is (false? (mi/can-read? wsd)))))- (testing "superuser — true"- (with-admin (is (true? (mi/can-read? wsd))))))))--(deftest can-create?-test- (testing "WorkspaceDatabase `mi/can-create?`"- (testing "non-superuser — false"- (with-normal-user (is (false? (mi/can-create? :model/WorkspaceDatabase {:database_id (mt/id)})))))- (testing "Data Analyst (non-superuser) — false"- (with-data-analyst- (is (false? (mi/can-create? :model/WorkspaceDatabase {:database_id (mt/id)})))))- (testing "superuser — true"- (with-admin- (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-superuser — false"- (with-normal-user (is (false? (mi/can-write? wsd)))))- (testing "Data Analyst (non-superuser) — false"- (with-data-analyst (is (false? (mi/can-write? wsd)))))- (testing "superuser — true"- (with-admin (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.cljdeleted file mode 100644index aff091ab486..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_test.clj+++ /dev/null@@ -1,277 +0,0 @@-(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.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 =========================================-;;;-;;; All workspace permission predicates are superuser-only.--(defmacro ^:private with-normal-user [& body]- `(mt/with-test-user :rasta ~@body))--(defmacro ^:private with-admin [& body]- `(mt/with-test-user :crowberto ~@body))--(defmacro ^:private with-data-analyst [& body]- `(perms.test-util/with-data-analyst-role! (mt/user->id :rasta)- (mt/with-test-user :rasta ~@body)))--(deftest can-read?-test- (testing "Workspace `mi/can-read?`"- (mt/with-temp [:model/Workspace ws {}]- (testing "non-superuser — false"- (with-normal-user (is (false? (mi/can-read? ws)))))- (testing "Data Analyst (non-superuser) — false"- (with-data-analyst (is (false? (mi/can-read? ws)))))- (testing "superuser — true"- (with-admin (is (true? (mi/can-read? ws))))))))--(deftest can-create?-test- (testing "Workspace `mi/can-create?`"- (testing "non-superuser — false"- (with-normal-user (is (false? (mi/can-create? :model/Workspace {})))))- (testing "Data Analyst (non-superuser) — false"- (with-data-analyst (is (false? (mi/can-create? :model/Workspace {})))))- (testing "superuser — true"- (with-admin (is (true? (mi/can-create? :model/Workspace {})))))))--(deftest can-write?-test- (testing "Workspace `mi/can-write?`"- (mt/with-temp [:model/Workspace ws {}]- (testing "non-superuser — false"- (with-normal-user (is (false? (mi/can-write? ws)))))- (testing "Data Analyst (non-superuser) — false"- (with-data-analyst (is (false? (mi/can-write? ws)))))- (testing "superuser, empty workspace — true"- (with-admin (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 "superuser with attached databases — true"- (with-admin (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.cljdeleted file mode 100644index 6a6a132d8eb..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/corpus_test.clj+++ /dev/null@@ -1,347 +0,0 @@-(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` (the already-expanded `{:db ?, :schema ?}` map)."- [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 the- already-expanded `:output` map; the catalog (SQL Server/BigQuery) lives- inside that map. 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))- ;; The fixture's `:output` is already the expanded `{:db ?, :schema ?}`- ;; map (`workspaces.config/build-workspace-config` emits the runtime- ;; shape directly), so the loader stores it verbatim.- expected-output (:output wsd)- fake-db {:engine engine :details details}- ;; 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}]- (mt/with-premium-features #{:workspaces}- (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.cljdeleted file mode 100644index 36eed0a982f..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/cross_driver_test.clj+++ /dev/null@@ -1,353 +0,0 @@-(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.cljdeleted file mode 100644index 21b8512c577..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/driver_corpus_test.clj+++ /dev/null@@ -1,185 +0,0 @@-(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.cljdeleted file mode 100644index 13124fbf73b..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/middleware_test.clj+++ /dev/null@@ -1,850 +0,0 @@-(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.cljdeleted file mode 100644index 4f5fd1d5337..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/remapping/core_test.clj+++ /dev/null@@ -1,135 +0,0 @@-(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.cljdeleted file mode 100644index 7bde4dd283b..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/remapping_cleanup_test.clj+++ /dev/null@@ -1,152 +0,0 @@-(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- (details [_ _ _ _] (throw (ex-info "not used" {})))- (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.ymldeleted file mode 100644index 3fd283c4df4..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_bigquery.yml+++ /dev/null@@ -1,30 +0,0 @@-# 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:- db: metabase-prod- schema: 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.ymldeleted file mode 100644index 0f5f5e17cc3..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_clickhouse.yml+++ /dev/null@@ -1,31 +0,0 @@-# 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:- schema: 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.ymldeleted file mode 100644index 502415f7617..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_mysql.yml+++ /dev/null@@ -1,32 +0,0 @@-# 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:- db: 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.ymldeleted file mode 100644index eb0535c9262..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_postgres.yml+++ /dev/null@@ -1,32 +0,0 @@-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:- schema: 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.ymldeleted file mode 100644index 39df7054476..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_redshift.yml+++ /dev/null@@ -1,36 +0,0 @@-# 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:- schema: 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.ymldeleted file mode 100644index 8f72a8ccfe3..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/resources/workspace_config_sqlserver.yml+++ /dev/null@@ -1,31 +0,0 @@-# 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:- db: AnalyticsDB- schema: ws_alicediff --git a/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.cljdeleted file mode 100644index b126ab64c5f..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.clj+++ /dev/null@@ -1,32 +0,0 @@-(ns metabase-enterprise.workspaces.settings-test- (:require- [clojure.test :refer [deftest is testing use-fixtures]]- [metabase-enterprise.workspaces.core :as ws]- [metabase.test.fixtures :as fixtures]))--(use-fixtures :once (fixtures/initialize :db))--(use-fixtures :each (fn [thunk]- (try- (thunk)- (finally- (ws/clear-instance-workspace!)))))--(deftest workspace-mode?-false-when-setting-empty-test- (testing "workspace-mode? is false when no :workspace section has been loaded"- (ws/clear-instance-workspace!)- (is (false? (ws/workspace-mode?)))))--(deftest workspace-mode?-true-when-setting-populated-test- (testing "workspace-mode? flips to true once a workspace config has been set"- (ws/clear-instance-workspace!)- (is (false? (ws/workspace-mode?)))- (ws/set-instance-workspace! {:name "Acme" :databases {}})- (is (true? (ws/workspace-mode?)))))--(deftest workspace-mode?-flips-back-to-false-on-clear-test- (testing "workspace-mode? returns to false after clear-instance-workspace!"- (ws/set-instance-workspace! {:name "Acme" :databases {}})- (is (true? (ws/workspace-mode?)))- (ws/clear-instance-workspace!)- (is (false? (ws/workspace-mode?)))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/table_remapping_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/table_remapping_test.cljdeleted file mode 100644index 3435606d510..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/table_remapping_test.clj+++ /dev/null@@ -1,1083 +0,0 @@-(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 `instance-workspace` setting so `db-workspace-namespace` returns- `{:schema output-schema}` for `db-id`, run `f`, clear it on the way out.- Enables `:workspaces` for the duration."- [db-id output-schema f]- (mt/with-premium-features #{:workspaces}- (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.- Enables `:workspaces` for the duration."- [db-id output-namespace f]- (mt/with-premium-features #{:workspaces}- (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.cljdeleted file mode 100644index 7245f8d03f6..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj+++ /dev/null@@ -1,27 +0,0 @@-(ns metabase-enterprise.workspaces.test-util- "Shared test helpers for the workspaces module."- (:require- [metabase-enterprise.workspaces.core :as ws]- [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)))--(defn with-workspace-locked-by-config- "Flip the workspace-instance lock for the duration of `thunk`, restoring the- prior value on exit. Reaches into the private atom via `#'` so the ws/core- surface stays clean of test-only setters."- [thunk]- (let [a @#'ws/locked-by-config?*- prior @a]- (reset! a true)- (try (thunk)- (finally (reset! a prior)))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/transform_hooks_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/transform_hooks_test.cljdeleted file mode 100644index 52b422019b6..00000000000--- a/enterprise/backend/test/metabase_enterprise/workspaces/transform_hooks_test.clj+++ /dev/null@@ -1,152 +0,0 @@-(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 `instance-workspace` setting 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]- (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/advanced-config.ts b/enterprise/frontend/src/metabase-enterprise/api/advanced-config.tsindex fcf2e66bc7e..1bd79e06d6d 100644--- a/enterprise/frontend/src/metabase-enterprise/api/advanced-config.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/advanced-config.ts@@ -1,7 +1,7 @@import type { ApplyAdvancedConfigRequest } from "metabase-types/api";import { EnterpriseApi } from "./api";-import { invalidateTags, listTag, tag } from "./tags";+import { invalidateTags, listTag } from "./tags";export const advancedConfigApi = EnterpriseApi.injectEndpoints({endpoints: (builder) => ({@@ -16,11 +16,7 @@ export const advancedConfigApi = EnterpriseApi.injectEndpoints({};},invalidatesTags: (_, error) =>- invalidateTags(error, [- tag("workspace"),- listTag("workspace"),- listTag("database"),- ]),+ invalidateTags(error, [listTag("database")]),}),}),});diff --git a/enterprise/frontend/src/metabase-enterprise/api/index.ts b/enterprise/frontend/src/metabase-enterprise/api/index.tsindex 141a5c307fb..52d3150b8fe 100644--- a/enterprise/frontend/src/metabase-enterprise/api/index.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/index.ts@@ -26,5 +26,3 @@ export * from "./table";export * from "./tags";export * from "./tenants";export * from "./upload-management";-export * from "./workspace-manager";-export * from "./workspace-instance";diff --git a/enterprise/frontend/src/metabase-enterprise/api/tags.ts b/enterprise/frontend/src/metabase-enterprise/api/tags.tsindex 32687d4bece..d1024d47b6e 100644--- a/enterprise/frontend/src/metabase-enterprise/api/tags.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/tags.ts@@ -25,7 +25,6 @@ import {type SupportAccessGrant,type TableDependencyNode,type TransformDependencyNode,- type Workspace,} from "metabase-types/api";export const ENTERPRISE_TAG_TYPES = [@@ -51,7 +50,6 @@ 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];@@ -258,15 +256,3 @@ 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.tsdeleted file mode 100644index be0ecd73a20..00000000000--- a/enterprise/frontend/src/metabase-enterprise/api/workspace-instance.ts+++ /dev/null@@ -1,45 +0,0 @@-import { provideTableRemappingListTags } from "metabase/api/tags";-import type {- CurrentWorkspace,- GetCurrentWorkspaceResponse,- TableRemapping,-} from "metabase-types/api";--import { EnterpriseApi } from "./api";-import { invalidateTags, tag } from "./tags";--export const workspaceInstanceApi = EnterpriseApi.injectEndpoints({- endpoints: (builder) => ({- getCurrentWorkspace: builder.query<CurrentWorkspace | null, void>({- query: () => ({- method: "GET",- url: "/api/ee/workspace-instance/current",- }),- transformResponse: (response: GetCurrentWorkspaceResponse) =>- response.data,- providesTags: [tag("workspace")],- }),- deleteCurrentWorkspace: builder.mutation<void, void>({- query: () => ({- method: "DELETE",- url: "/api/ee/workspace-instance/current",- }),- invalidatesTags: (_, error) =>- invalidateTags(error, [tag("workspace"), tag("table-remapping")]),- }),- listTableRemappings: builder.query<TableRemapping[], void>({- query: () => ({- method: "GET",- url: "/api/ee/workspace-instance/table-remappings",- }),- providesTags: (remappings = []) =>- provideTableRemappingListTags(remappings),- }),- }),-});--export const {- useGetCurrentWorkspaceQuery,- useDeleteCurrentWorkspaceMutation,- useListTableRemappingsQuery,-} = workspaceInstanceApi;diff --git a/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts b/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.tsdeleted file mode 100644index 1d3b22c4653..00000000000--- a/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts+++ /dev/null@@ -1,69 +0,0 @@-import type {- CreateWorkspaceRequest,- UpdateWorkspaceRequest,- Workspace,- WorkspaceId,-} from "metabase-types/api";--import { EnterpriseApi } from "./api";-import {- idTag,- invalidateTags,- listTag,- provideWorkspaceListTags,-} from "./tags";--export const workspaceManagerApi = 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 ? [idTag("workspace", workspace.id)] : [],- }),- 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)]),- }),- }),-});--export const {- useListWorkspacesQuery,- useLazyListWorkspacesQuery,- useGetWorkspaceQuery,- useCreateWorkspaceMutation,- useUpdateWorkspaceMutation,- useDeleteWorkspaceMutation,-} = workspaceManagerApi;diff --git a/enterprise/frontend/src/metabase-enterprise/plugins.ts b/enterprise/frontend/src/metabase-enterprise/plugins.tsindex abfaae163d0..b3b3eb90ecd 100644--- a/enterprise/frontend/src/metabase-enterprise/plugins.ts+++ b/enterprise/frontend/src/metabase-enterprise/plugins.ts@@ -56,7 +56,6 @@ 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";/**@@ -112,6 +111,5 @@ export function initializePlugins() {initializeUploadManagement();initializeUserProvisioning();initializeWhitelabel();- initializeWorkspaces();initializeWritableConnection();}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/analytics.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/analytics.tsdeleted file mode 100644index 61d43d5672f..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/analytics.ts+++ /dev/null@@ -1,43 +0,0 @@-import { trackSimpleEvent } from "metabase/analytics";-import type { WorkspaceId } from "metabase-types/api";--export function trackWorkspaceCreated({- workspaceId,-}: {- workspaceId: WorkspaceId;-}) {- trackSimpleEvent({- event: "workspaces_new_created",- target_id: workspaceId,- });-}--export function trackWorkspaceConfigDownloaded({- workspaceId,-}: {- workspaceId: WorkspaceId;-}) {- trackSimpleEvent({- event: "workspaces_config_downloaded",- target_id: workspaceId,- });-}--export function trackWorkspaceSetupButtonClicked() {- trackSimpleEvent({- event: "workspaces_setup_button_clicked",- });-}--export function trackWorkspaceInstanceSetup() {- trackSimpleEvent({- event: "workspaces_instance_setup",- triggered_from: "upload",- });-}--export function trackWorkspaceInstanceLeave() {- trackSimpleEvent({- event: "workspaces_instance_leave",- });-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/components/WorkspaceDatabaseSection/WorkspaceDatabaseSection.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/components/WorkspaceDatabaseSection/WorkspaceDatabaseSection.tsxdeleted file mode 100644index ca1713f837f..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/components/WorkspaceDatabaseSection/WorkspaceDatabaseSection.tsx+++ /dev/null@@ -1,156 +0,0 @@-import { t } from "ttag";--import { DatabaseConnectionHealthInfo } from "metabase/admin/databases/components/DatabaseConnectionHealthInfo";-import { Label } from "metabase/admin/databases/components/DatabaseFeatureComponents";-import {- DatabaseInfoSection,- DatabaseInfoSectionDivider,-} from "metabase/admin/databases/components/DatabaseInfoSection";-import { useUpdateDatabaseMutation } from "metabase/api";-import { useConfirmation } from "metabase/common/hooks/use-confirmation";-import { useToast } from "metabase/common/hooks/use-toast";-import {- hasAdminConnectionDetails,- hasDbRoutingEnabled,- hasFeature,- hasWorkspacesEnabled,- isDbModifiable,-} from "metabase/common/utils/database";-import type { WorkspaceDatabaseSectionProps } from "metabase/plugins/oss/workspaces";-import { Link } from "metabase/router";-import {- Alert,- Box,- Button,- FixedSizeIcon,- Group,- Stack,- Switch,-} from "metabase/ui";-import * as Urls from "metabase/urls";--export function WorkspaceDatabaseSection({- database,-}: WorkspaceDatabaseSectionProps) {- const [updateDatabase] = useUpdateDatabaseMutation();- const { modalContent, show: showConfirmation } = useConfirmation();- const [sendToast] = useToast();-- const isEnabled = hasWorkspacesEnabled(database);- const hasAdminConnection = hasAdminConnectionDetails(database);- const isDbRoutingEnabled = hasDbRoutingEnabled(database);-- if (!isDbModifiable(database) || !hasFeature(database, "workspace")) {- return null;- }-- const handleToggle = async (value: boolean) => {- const { error } = await updateDatabase({- id: database.id,- settings: { "database-enable-workspaces": value },- });- if (error) {- sendToast({- message: t`Failed to enable Workspaces`,- toastColor: "feedback-negative",- icon: "warning",- });- }- };-- const handleRemove = () => {- showConfirmation({- title: t`Remove connection?`,- message: t`Any features that depend on it will stop working.`,- confirmButtonText: t`Remove`,- onConfirm: async () => {- const { error } = await updateDatabase({- id: database.id,- admin_details: null,- });- if (error) {- sendToast({- message: t`Failed to remove connection`,- toastColor: "feedback-negative",- });- }- },- });- };-- return (- <DatabaseInfoSection- name={t`Workspaces`}- description={t`Workspaces isolate transformed tables into a separate schema so you can build and test changes before syncing them back to production.`}- data-testid="workspace-database-section"- >- <Group justify="space-between">- <Box>- <Label htmlFor="workspace-enable-toggle">{t`Enable workspaces`}</Label>- </Box>- <Switch- id="workspace-enable-toggle"- checked={isEnabled}- disabled={isDbRoutingEnabled}- onChange={(event) => handleToggle(event.currentTarget.checked)}- />- </Group>-- {isDbRoutingEnabled && (- <>- <DatabaseInfoSectionDivider />- <Alert- size="compact"- variant="light"- icon={<FixedSizeIcon name="info" />}- mb="md"- >- {t`Workspaces can't be enabled when database routing is enabled.`}- </Alert>- </>- )}-- {isEnabled && (- <>- <DatabaseInfoSectionDivider />- <Stack gap={hasAdminConnection ? "md" : 0}>- <Box fw="bold" lh="lg">{t`Admin database connection`}</Box>- <Group justify="space-between">- {hasAdminConnection ? (- <DatabaseConnectionHealthInfo- databaseId={database.id}- connectionType="admin"- />- ) : (- <Box c="text-secondary">- {t`Used to create isolated schemas and temporary users for workspaces.`}- </Box>- )}- <Group>- <Button- component={isDbRoutingEnabled ? undefined : Link}- to={Urls.editDatabaseAdminConnection(database.id)}- disabled={isDbRoutingEnabled}- >- {hasAdminConnection- ? t`Edit connection details`- : t`Add admin connection`}- </Button>- {hasAdminConnection && (- <Button- variant="filled"- color="feedback-negative"- onClick={handleRemove}- >- {t`Remove admin connection`}- </Button>- )}- </Group>- </Group>- </Stack>- </>- )}- {modalContent}- </DatabaseInfoSection>- );-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/components/WorkspaceDatabaseSection/WorkspaceDatabaseSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/components/WorkspaceDatabaseSection/WorkspaceDatabaseSection.unit.spec.tsxdeleted file mode 100644index 353501578d2..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/components/WorkspaceDatabaseSection/WorkspaceDatabaseSection.unit.spec.tsx+++ /dev/null@@ -1,186 +0,0 @@-import userEvent from "@testing-library/user-event";-import fetchMock from "fetch-mock";--import {- setupDatabaseHealthcheckEndpoint,- setupUpdateDatabaseEndpoint,- setupUpdateDatabaseEndpointError,-} from "__support__/server-mocks/database";-import { renderWithProviders, screen, waitFor } from "__support__/ui";-import { UndoListing } from "metabase/common/components/UndoListing";-import type { Database } from "metabase-types/api";-import { createMockDatabase } from "metabase-types/api/mocks";--import { WorkspaceDatabaseSection } from "./WorkspaceDatabaseSection";--interface SetupOpts {- database?: Database;- withError?: boolean;-}--function setup({- database = createMockDatabase({ features: ["workspace"] }),- withError = false,-}: SetupOpts = {}) {- setupDatabaseHealthcheckEndpoint(database.id);- if (withError) {- setupUpdateDatabaseEndpointError(database.id);- } else {- setupUpdateDatabaseEndpoint(database);- }- renderWithProviders(- <>- <WorkspaceDatabaseSection database={database} />- <UndoListing />- </>,- );-}--function createWorkspaceDatabase(opts: Partial<Database> = {}) {- return createMockDatabase({- features: ["workspace"],- settings: { "database-enable-workspaces": true },- ...opts,- });-}--describe("WorkspaceDatabaseSection", () => {- it("should not render for a non-modifiable database (attached DWH)", () => {- setup({- database: createMockDatabase({- is_attached_dwh: true,- features: ["workspace"],- }),- });-- expect(- screen.queryByTestId("workspace-database-section"),- ).not.toBeInTheDocument();- });-- it("should not render when the database does not support the workspace feature", () => {- setup({ database: createMockDatabase({ features: [] }) });-- expect(- screen.queryByTestId("workspace-database-section"),- ).not.toBeInTheDocument();- });-- it("should render the enable toggle unchecked when Workspaces are disabled", () => {- setup({ database: createMockDatabase({ features: ["workspace"] }) });-- expect(- screen.getByTestId("workspace-database-section"),- ).toBeInTheDocument();- expect(- screen.getByRole("switch", { name: /Enable workspaces/i }),- ).not.toBeChecked();- });-- it("should render the enable toggle checked when Workspaces are enabled", () => {- setup({ database: createWorkspaceDatabase() });-- expect(- screen.getByRole("switch", { name: /Enable workspaces/i }),- ).toBeChecked();- });-- it("should not show the connection section while Workspaces are disabled", () => {- setup({ database: createMockDatabase({ features: ["workspace"] }) });-- expect(- screen.queryByText("Admin database connection"),- ).not.toBeInTheDocument();- });-- it("should persist the database-enable-workspaces setting when toggling on", async () => {- const database = createMockDatabase({ features: ["workspace"] });- setup({ database });-- await userEvent.click(- screen.getByRole("switch", { name: /Enable workspaces/i }),- );-- await waitFor(() => {- expect(- fetchMock.callHistory.calls(`path:/api/database/${database.id}`, {- method: "PUT",- }),- ).toHaveLength(1);- });- });-- it("should show the description and an Add connection link when enabled without a connection", () => {- setup({ database: createWorkspaceDatabase() });-- expect(screen.getByText("Admin database connection")).toBeInTheDocument();- expect(- screen.getByText(- "Used to create isolated schemas and temporary users for workspaces.",- ),- ).toBeInTheDocument();- expect(screen.getByText("Add admin connection")).toBeInTheDocument();- });-- it("should show the connection status with Edit and Remove buttons when a connection exists", async () => {- setup({- database: createWorkspaceDatabase({ admin_details: { user: "admin" } }),- });-- expect(- await screen.findByTestId("database-connection-health-info"),- ).toBeInTheDocument();- expect(screen.getByText("Edit connection details")).toBeInTheDocument();- expect(- screen.getByRole("button", { name: "Remove admin connection" }),- ).toBeInTheDocument();- });-- it("should disable the toggle and warn when database routing is enabled", () => {- setup({- database: createMockDatabase({- features: ["workspace"],- router_user_attribute: "some_attribute",- }),- });-- expect(- screen.getByRole("switch", { name: /Enable workspaces/i }),- ).toBeDisabled();- expect(- screen.getByText(- "Workspaces can't be enabled when database routing is enabled.",- ),- ).toBeInTheDocument();- });-- it("should disable the Add connection button when database routing is enabled", () => {- setup({- database: createWorkspaceDatabase({- router_user_attribute: "some_attribute",- }),- });-- expect(- screen.getByRole("button", { name: "Add admin connection" }),- ).toBeDisabled();- });-- it("should show an error toast when removing the connection fails", async () => {- const database = createWorkspaceDatabase({- admin_details: { user: "admin" },- });- setup({ database, withError: true });-- await userEvent.click(- screen.getByRole("button", { name: "Remove admin connection" }),- );- await userEvent.click(- await screen.findByRole("button", { name: "Remove" }),- );-- expect(- await screen.findByText("Failed to remove connection"),- ).toBeInTheDocument();- });-});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/components/WorkspaceDatabaseSection/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/components/WorkspaceDatabaseSection/index.tsdeleted file mode 100644index 2f13dd1fde4..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/components/WorkspaceDatabaseSection/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from "./WorkspaceDatabaseSection";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/index.tsdeleted file mode 100644index 4f10f6d4178..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/index.ts+++ /dev/null@@ -1,15 +0,0 @@-import { PLUGIN_WORKSPACES } from "metabase/plugins";-import { hasPremiumFeature } from "metabase-enterprise/settings";--import { WorkspaceDatabaseSection } from "./components/WorkspaceDatabaseSection";-import { getDataStudioRoutes, getWorkspaceDatabaseRoutes } from "./routes";-import { canManageWorkspaces } from "./selectors";--export function initializePlugin() {- if (hasPremiumFeature("workspaces")) {- PLUGIN_WORKSPACES.canManageWorkspaces = canManageWorkspaces;- PLUGIN_WORKSPACES.getDataStudioRoutes = getDataStudioRoutes;- PLUGIN_WORKSPACES.getWorkspaceDatabaseRoutes = getWorkspaceDatabaseRoutes;- PLUGIN_WORKSPACES.WorkspaceDatabaseSection = WorkspaceDatabaseSection;- }-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/AdminConnectionInfoPage.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/AdminConnectionInfoPage.tsxdeleted file mode 100644index 41cbae7bc5f..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/AdminConnectionInfoPage.tsx+++ /dev/null@@ -1,138 +0,0 @@-import { useMemo, useState } from "react";-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 type { Route } from "metabase/router";-import { push } from "metabase/router";-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_page-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.tsxdeleted file mode 100644index 56a71bfdb23..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/AdminConnectionInfoPage.unit.spec.tsx+++ /dev/null@@ -1,62 +0,0 @@-import { setupDatabaseEndpoints } from "__support__/server-mocks/database";-import { renderWithProviders, screen } from "__support__/ui";-import { createMockSettingsState } from "metabase/redux/store/mocks";-import { Route } from "metabase/router";-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="*"- element={- <AdminConnectionInfoPage- params={{ databaseId: String(database.id) }}- // Unjustified type cast. FIXME- 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.tsdeleted file mode 100644index a416af52aad..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/AdminConnectionInfoPage/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from "./AdminConnectionInfoPage";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/CurrentWorkspacePage.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/CurrentWorkspacePage.tsxdeleted file mode 100644index c58225ffb32..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/CurrentWorkspacePage.tsx+++ /dev/null@@ -1,97 +0,0 @@-import { t } from "ttag";--import { useListDatabasesQuery } from "metabase/api";-import { DelayedLoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper/DelayedLoadingAndErrorWrapper";-import { DataStudioBreadcrumbs } from "metabase/common/data-studio/components/DataStudioBreadcrumbs";-import { PageContainer } from "metabase/common/data-studio/components/PageContainer";-import { PaneHeader } from "metabase/common/data-studio/components/PaneHeader";-import { Stack, Title } from "metabase/ui";-import {- useGetCurrentWorkspaceQuery,- useListTableRemappingsQuery,-} from "metabase-enterprise/api";-import type {- CurrentWorkspace,- Database,- TableRemapping,-} from "metabase-types/api";--import { DeleteSection } from "./DeleteSection";-import { TableRemappingSection } from "./TableRemappingSection";-import { getDatabasesInfo } from "./utils";--export function CurrentWorkspacePage() {- 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 (- <CurrentWorkspacePageBody- workspace={workspace}- remappings={remappings}- databases={databasesResponse.data}- />- );-}--type CurrentWorkspacePageBodyProps = {- workspace: CurrentWorkspace;- remappings: TableRemapping[];- databases: Database[];-};--function CurrentWorkspacePageBody({- workspace,- remappings,- databases,-}: CurrentWorkspacePageBodyProps) {- const databasesInfo = getDatabasesInfo(workspace, databases, remappings);-- return (- <PageContainer data-testid="current-workspace-page">- <PaneHeader- title={<Title order={3}>{workspace.name}</Title>}- breadcrumbs={- <DataStudioBreadcrumbs>{t`Workspaces`}</DataStudioBreadcrumbs>- }- py={0}- />- <Stack gap="3.5rem">- {databasesInfo.map(({ database, remappings }) => (- <TableRemappingSection- key={database.id}- database={database}- remappings={remappings}- />- ))}- {workspace.can_write && <DeleteSection />}- </Stack>- </PageContainer>- );-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/CurrentWorkspacePage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/CurrentWorkspacePage.unit.spec.tsxdeleted file mode 100644index 4b766202680..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/CurrentWorkspacePage.unit.spec.tsx+++ /dev/null@@ -1,100 +0,0 @@-import {- setupDatabasesEndpoints,- setupGetCurrentWorkspaceEndpoint,- setupListTableRemappingsEndpoint,-} from "__support__/server-mocks";-import { renderWithProviders, screen } from "__support__/ui";-import { Route } from "metabase/router";-import type { TableRemapping } from "metabase-types/api";-import {- createMockCurrentWorkspace,- createMockCurrentWorkspaceDatabase,- createMockDatabase,- createMockTableRemapping,-} from "metabase-types/api/mocks";--import { CurrentWorkspacePage } from "./CurrentWorkspacePage";--const POSTGRES = createMockDatabase({ id: 10, name: "Postgres" });--function setup({- // Unjustified type cast. FIXME- remappings = [] as TableRemapping[],- workspace = createMockCurrentWorkspace({- name: "Dev workspace",- databases: {- [POSTGRES.id]: createMockCurrentWorkspaceDatabase({- input_schemas: ["public"],- output: { schema: "ws_dev" },- }),- },- can_write: true,- }),-} = {}) {- setupDatabasesEndpoints([POSTGRES]);- setupGetCurrentWorkspaceEndpoint(workspace);- setupListTableRemappingsEndpoint(remappings);-- renderWithProviders(<Route path="*" element={<CurrentWorkspacePage />} />, {- withRouter: true,- });-}--describe("CurrentWorkspacePage", () => {- 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();- });-- it("renders the delete section", async () => {- setup();-- expect(await screen.findByText("Dev workspace")).toBeInTheDocument();- expect(- await screen.findByTestId("workspace-instance-delete-section"),- ).toBeInTheDocument();- });-- it("hides the delete section when can_write is false", async () => {- setup({- workspace: createMockCurrentWorkspace({- name: "Dev workspace",- databases: {- [POSTGRES.id]: createMockCurrentWorkspaceDatabase({- input_schemas: ["public"],- output: { schema: "ws_dev" },- }),- },- can_write: false,- }),- });-- expect(await screen.findByText("Dev workspace")).toBeInTheDocument();- expect(- screen.queryByTestId("workspace-instance-delete-section"),- ).not.toBeInTheDocument();- });-});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/DeleteSection/DeleteSection.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/DeleteSection/DeleteSection.tsxdeleted file mode 100644index dec300a33d9..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/DeleteSection/DeleteSection.tsx+++ /dev/null@@ -1,53 +0,0 @@-import { jt, t } from "ttag";--import { TitleSection } from "metabase/common/data-studio/components/TitleSection";-import { useConfirmation } from "metabase/common/hooks/use-confirmation";-import { Button, Code, Group, Text } from "metabase/ui";-import { useDeleteCurrentWorkspaceMutation } from "metabase-enterprise/api";--import { trackWorkspaceInstanceLeave } from "../../../analytics";--const CONFIG_FILENAME = "config.yml";--export function DeleteSection() {- const { modalContent, show } = useConfirmation();- const [deleteInstance] = useDeleteCurrentWorkspaceMutation();-- const handleClick = () => {- show({- title: t`Leave workspace?`,- message: jt`This instance will stop remapping transform tables to the workspace's isolated schemas. The databases registered from the uploaded ${(- <Code key="config">{CONFIG_FILENAME}</Code>- )} will remain — you can re-enter the workspace at any time by uploading it again.`,- confirmButtonText: t`Leave workspace`,- confirmButtonProps: { color: "feedback-negative" },- onConfirm: async () => {- await deleteInstance().unwrap();- trackWorkspaceInstanceLeave();- },- });- };-- return (- <>- <TitleSection- data-testid="workspace-instance-delete-section"- label={t`Leave workspace`}- >- <Group p="lg" justify="space-between" align="center">- <Text maw="40rem">- {t`Stop remapping transform tables on this instance.`}- </Text>- <Button- color="feedback-negative"- variant="filled"- onClick={handleClick}- >- {t`Leave workspace`}- </Button>- </Group>- </TitleSection>- {modalContent}- </>- );-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/DeleteSection/DeleteSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/DeleteSection/DeleteSection.unit.spec.tsxdeleted file mode 100644index 30b061b1e87..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/DeleteSection/DeleteSection.unit.spec.tsx+++ /dev/null@@ -1,63 +0,0 @@-import userEvent from "@testing-library/user-event";-import fetchMock from "fetch-mock";--import { setupDeleteCurrentWorkspaceEndpoint } from "__support__/server-mocks";-import { renderWithProviders, screen, waitFor, within } from "__support__/ui";-import { Route } from "metabase/router";--import { DeleteSection } from "./DeleteSection";--const { trackSimpleEvent } = jest.requireMock("metabase/analytics");--function setup() {- setupDeleteCurrentWorkspaceEndpoint();-- renderWithProviders(<Route path="*" element={<DeleteSection />} />, {- withRouter: true,- });-}--describe("DeleteSection", () => {- beforeEach(() => {- trackSimpleEvent.mockClear();- });-- it("calls DELETE /api/ee/workspace-instance/current after confirming", async () => {- setup();-- await userEvent.click(- screen.getByRole("button", { name: "Leave workspace" }),- );- const dialog = await screen.findByRole("dialog");- await userEvent.click(- within(dialog).getByRole("button", { name: "Leave workspace" }),- );-- await waitFor(() => {- expect(- fetchMock.callHistory.called(- "path:/api/ee/workspace-instance/current",- { method: "DELETE" },- ),- ).toBe(true);- });- });-- it("tracks an analytics event when the instance is torn down", async () => {- setup();-- await userEvent.click(- screen.getByRole("button", { name: "Leave workspace" }),- );- const dialog = await screen.findByRole("dialog");- await userEvent.click(- within(dialog).getByRole("button", { name: "Leave workspace" }),- );-- await waitFor(() =>- expect(trackSimpleEvent).toHaveBeenCalledWith({- event: "workspaces_instance_leave",- }),- );- });-});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/DeleteSection/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/DeleteSection/index.tsdeleted file mode 100644index 50566513525..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/DeleteSection/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from "./DeleteSection";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingSection.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingSection.tsxdeleted file mode 100644index f2179f39b0f..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingSection.tsx+++ /dev/null@@ -1,38 +0,0 @@-import { t } from "ttag";--import { TitleSection } from "metabase/common/data-studio/components/TitleSection";-import { Box, FixedSizeIcon, Group, Text } 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>- }- >- {remappings.length === 0 ? (- <Text p="lg">- {t`Tables will be remapped here the first time a transform runs in this workspace for this database.`}- </Text>- ) : (- <TableRemappingTable remappings={remappings} />- )}- </TitleSection>- </Box>- );-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingSection.unit.spec.tsxdeleted file mode 100644index 7d48048a7ed..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingSection.unit.spec.tsx+++ /dev/null@@ -1,32 +0,0 @@-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/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/TableRemappingTable.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/TableRemappingTable.tsxdeleted file mode 100644index 69de768bdc7..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/TableRemappingTable.tsx+++ /dev/null@@ -1,22 +0,0 @@-import { useMemo } from "react";--import { 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} />;-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/TableRemappingTable.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/TableRemappingTable.unit.spec.tsxdeleted file mode 100644index 1c062242542..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/TableRemappingTable.unit.spec.tsx+++ /dev/null@@ -1,25 +0,0 @@-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/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/index.tsdeleted file mode 100644index a2899531de9..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from "./TableRemappingTable";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/utils.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/utils.tsxdeleted file mode 100644index d2cce1b64f0..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/utils.tsx+++ /dev/null@@ -1,59 +0,0 @@-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/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/utils.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/utils.unit.spec.tsxdeleted file mode 100644index 949d662cba7..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/TableRemappingTable/utils.unit.spec.tsx+++ /dev/null@@ -1,16 +0,0 @@-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/CurrentWorkspacePage/TableRemappingSection/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/index.tsdeleted file mode 100644index 046576d6818..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/TableRemappingSection/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from "./TableRemappingSection";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/index.tsdeleted file mode 100644index e423d287182..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from "./CurrentWorkspacePage";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/utils.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/utils.tsdeleted file mode 100644index fc615da1dfe..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/utils.ts+++ /dev/null@@ -1,44 +0,0 @@-import type {- CurrentWorkspace,- Database,- DatabaseId,- TableRemapping,-} from "metabase-types/api";--export type DatabaseInfo = {- database: Database;- remappings: TableRemapping[];-};--export function getDatabasesInfo(- workspace: CurrentWorkspace,- 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/WorkspaceIndexPage/WorkspaceIndexPage.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceIndexPage/WorkspaceIndexPage.tsxdeleted file mode 100644index 47315529059..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceIndexPage/WorkspaceIndexPage.tsx+++ /dev/null@@ -1,27 +0,0 @@-import { DelayedLoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper/DelayedLoadingAndErrorWrapper";-import { useSelector } from "metabase/redux";-import { useGetCurrentWorkspaceQuery } from "metabase-enterprise/api";--import { canManageWorkspaces } from "../../selectors";-import { CurrentWorkspacePage } from "../CurrentWorkspacePage";-import { WorkspaceListPage } from "../WorkspaceListPage";--export function WorkspaceIndexPage() {- const canManageInstance = useSelector(canManageWorkspaces);-- const {- data: workspace,- isLoading,- error,- } = useGetCurrentWorkspaceQuery(undefined, { skip: !canManageInstance });-- if (isLoading || error != null) {- return <DelayedLoadingAndErrorWrapper loading={isLoading} error={error} />;- }-- if (workspace != null && canManageInstance) {- return <CurrentWorkspacePage />;- }-- return <WorkspaceListPage />;-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceIndexPage/WorkspaceIndexPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceIndexPage/WorkspaceIndexPage.unit.spec.tsxdeleted file mode 100644index a8753b699b0..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceIndexPage/WorkspaceIndexPage.unit.spec.tsx+++ /dev/null@@ -1,100 +0,0 @@-import {- setupDatabasesEndpoints,- setupGetCurrentWorkspaceEndpoint,- setupListTableRemappingsEndpoint,- setupListWorkspacesEndpoint,-} from "__support__/server-mocks";-import { mockSettings } from "__support__/settings";-import { renderWithProviders, screen } from "__support__/ui";-import { createMockState } from "metabase/redux/store/mocks";-import { Route } from "metabase/router";-import type { CurrentWorkspace } from "metabase-types/api";-import {- createMockCurrentWorkspace,- createMockCurrentWorkspaceDatabase,- createMockDatabase,- createMockTokenFeatures,- createMockUser,-} from "metabase-types/api/mocks";--import { WorkspaceIndexPage } from "./WorkspaceIndexPage";--const POSTGRES = createMockDatabase({ id: 10, name: "Postgres" });--const WORKSPACE = createMockCurrentWorkspace({- name: "Dev workspace",- databases: {- [POSTGRES.id]: createMockCurrentWorkspaceDatabase({- input_schemas: ["public"],- output: { schema: "ws_dev" },- }),- },-});--type SetupOpts = {- isAdmin?: boolean;- workspace?: CurrentWorkspace | null;-};--function setup({ isAdmin = true, workspace = null }: SetupOpts = {}) {- setupDatabasesEndpoints([POSTGRES]);- setupGetCurrentWorkspaceEndpoint(workspace);- setupListTableRemappingsEndpoint([]);- setupListWorkspacesEndpoint([]);-- const state = createMockState({- currentUser: createMockUser({- is_superuser: isAdmin,- permissions: {- can_access_data_model: isAdmin,- can_access_db_details: false,- },- }),- settings: mockSettings({- "token-features": createMockTokenFeatures({- workspaces: true,- }),- }),- });-- renderWithProviders(<Route path="*" element={<WorkspaceIndexPage />} />, {- withRouter: true,- storeInitialState: state,- });-}--describe("WorkspaceIndexPage", () => {- it("admin with a workspace loaded sees the instance page", async () => {- setup({ isAdmin: true, workspace: WORKSPACE });-- expect(- await screen.findByTestId("current-workspace-page"),- ).toBeInTheDocument();- expect(screen.queryByTestId("workspace-list-page")).not.toBeInTheDocument();- });-- it("admin without a workspace sees the list page", async () => {- setup({ isAdmin: true, workspace: null });-- expect(- await screen.findByTestId("workspace-list-page"),- ).toBeInTheDocument();- expect(- screen.queryByTestId("current-workspace-page"),- ).not.toBeInTheDocument();- });-- it("non-admin skips the instance lookup and sees the list page", async () => {- setup({- isAdmin: false,- workspace: WORKSPACE,- });-- expect(- await screen.findByTestId("workspace-list-page"),- ).toBeInTheDocument();- expect(- screen.queryByTestId("current-workspace-page"),- ).not.toBeInTheDocument();- });-});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceIndexPage/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceIndexPage/index.tsdeleted file mode 100644index 38a9eebd632..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceIndexPage/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from "./WorkspaceIndexPage";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.tsxdeleted file mode 100644index 9ddb43b4fb1..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.tsx+++ /dev/null@@ -1,143 +0,0 @@-import { t } from "ttag";--import { DelayedLoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper/DelayedLoadingAndErrorWrapper";-import {- Form,- FormErrorMessage,- FormProvider,- FormSubmitButton,-} from "metabase/forms";-import {- Button,- FocusTrap,- Group,- List,- Modal,- Stack,- Text,-} from "metabase/ui";-import {- useDeleteWorkspaceMutation,- useGetWorkspaceQuery,-} from "metabase-enterprise/api";-import type { Workspace, WorkspaceId } from "metabase-types/api";--import { getWorkspaceDatabaseName, isPending } from "../../../utils";--export type DeleteWorkspaceModalProps = {- workspaceId: WorkspaceId;- onDelete: () => void;- onClose: () => void;-};--export function DeleteWorkspaceModal({- workspaceId,- onDelete,- onClose,-}: DeleteWorkspaceModalProps) {- return (- <Modal- title={t`Delete this workspace?`}- opened- padding="xl"- onClose={onClose}- >- <FocusTrap.InitialFocus />- <DeleteWorkspaceLoader- workspaceId={workspaceId}- onDelete={onDelete}- onClose={onClose}- />- </Modal>- );-}--type DeleteWorkspaceLoaderProps = {- workspaceId: WorkspaceId;- onDelete: () => void;- onClose: () => void;-};--function DeleteWorkspaceLoader({- workspaceId,- onDelete,- onClose,-}: DeleteWorkspaceLoaderProps) {- // Fetch the workspace on open so the pending-databases warning reflects- // fresh statuses, not the possibly stale list-page snapshot.- const {- data: workspace,- isLoading,- error,- } = useGetWorkspaceQuery(workspaceId);-- if (isLoading || error != null || workspace == null) {- return <DelayedLoadingAndErrorWrapper loading={isLoading} error={error} />;- }-- return (- <DeleteWorkspaceForm- workspace={workspace}- onDelete={onDelete}- onClose={onClose}- />- );-}--type DeleteWorkspaceFormProps = {- workspace: Workspace;- onDelete: () => void;- onClose: () => void;-};--function DeleteWorkspaceForm({- workspace,- onDelete,- onClose,-}: DeleteWorkspaceFormProps) {- const [deleteWorkspace] = useDeleteWorkspaceMutation();- const databases = workspace.databases ?? [];- const pendingDatabases = databases.filter(isPending);- const hasPendingDatabases = pendingDatabases.length > 0;-- const handleSubmit = async () => {- await deleteWorkspace(workspace.id).unwrap();- onDelete();- };-- return (- <FormProvider initialValues={{}} onSubmit={handleSubmit}>- <Form>- <Stack gap="lg">- {hasPendingDatabases ? (- <Stack gap="sm">- <Text>- {t`Some of this workspace's databases are still being set up or torn down. Deleting will wait for that work to finish, then remove the workspace along with its temporary database users and schemas:`}- </Text>- <List>- {pendingDatabases.map((workspaceDatabase) => (- <List.Item key={workspaceDatabase.database_id}>- {getWorkspaceDatabaseName(workspaceDatabase)}- </List.Item>- ))}- </List>- </Stack>- ) : (- <Text>- {t`This will delete the workspace as well as the temporary database users and schemas that were created for this workspace. This can't be undone.`}- </Text>- )}- <FormErrorMessage />- <Group justify="flex-end">- <Button onClick={onClose}>{t`Cancel`}</Button>- <FormSubmitButton- label={t`Delete workspace`}- variant="filled"- color="feedback-negative"- />- </Group>- </Stack>- </Form>- </FormProvider>- );-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.unit.spec.tsxdeleted file mode 100644index c75c618a4ed..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/DeleteWorkspaceModal.unit.spec.tsx+++ /dev/null@@ -1,119 +0,0 @@-import userEvent from "@testing-library/user-event";-import fetchMock from "fetch-mock";--import {- setupDeleteWorkspaceEndpoint,- setupDeleteWorkspaceEndpointError,- setupGetWorkspaceEndpoint,-} from "__support__/server-mocks";-import { renderWithProviders, screen, waitFor } from "__support__/ui";-import { UndoListing } from "metabase/common/components/UndoListing";-import type { WorkspaceDatabase } from "metabase-types/api";-import {- createMockDatabase,- createMockWorkspace,- createMockWorkspaceDatabase,-} from "metabase-types/api/mocks";--import { DeleteWorkspaceModal } from "./DeleteWorkspaceModal";--function setup({- withError = false,- databases = [],-}: {- withError?: boolean;- databases?: WorkspaceDatabase[];-} = {}) {- const workspace = createMockWorkspace({- id: 1,- name: "My workspace",- databases,- });- setupGetWorkspaceEndpoint(workspace);-- if (withError) {- setupDeleteWorkspaceEndpointError(workspace.id);- } else {- setupDeleteWorkspaceEndpoint(workspace.id);- }-- const onDelete = jest.fn();- const onClose = jest.fn();-- renderWithProviders(- <>- <DeleteWorkspaceModal- workspaceId={workspace.id}- onDelete={onDelete}- onClose={onClose}- />- <UndoListing />- </>,- );-- return { workspace, onDelete, onClose };-}--async function clickDelete() {- const button = await screen.findByRole("button", {- name: "Delete workspace",- });- await waitFor(() => expect(button).toBeEnabled());- await userEvent.click(button);-}--describe("DeleteWorkspaceModal", () => {- it("should delete the workspace and call the callback when the confirm button is clicked", async () => {- const { onDelete } = setup();-- await clickDelete();-- await waitFor(() => {- expect(- fetchMock.callHistory.called("path:/api/ee/workspace-manager/1", {- method: "DELETE",- }),- ).toBe(true);- });- await waitFor(() => expect(onDelete).toHaveBeenCalled());- });-- it("should list pending databases and delete when confirmed", async () => {- const { onDelete } = setup({- databases: [- createMockWorkspaceDatabase({- database_id: 7,- status: "provisioning",- database: createMockDatabase({ id: 7, name: "Pending DB" }),- }),- ],- });-- expect(- await screen.findByText(/still being set up or torn down/i),- ).toBeInTheDocument();- expect(screen.getByText("Pending DB")).toBeInTheDocument();-- await clickDelete();-- await waitFor(() => {- expect(- fetchMock.callHistory.called("path:/api/ee/workspace-manager/1", {- method: "DELETE",- }),- ).toBe(true);- });- await waitFor(() => expect(onDelete).toHaveBeenCalled());- });-- it("should show an error message and not call the callback when the request fails", async () => {- const { onDelete } = setup({ withError: true });-- await clickDelete();-- expect(await screen.findByRole("alert")).toHaveTextContent(- "An error occurred",- );- expect(onDelete).not.toHaveBeenCalled();- });-});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/index.tsdeleted file mode 100644index a3307e8c5c3..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/DeleteWorkspaceModal/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from "./DeleteWorkspaceModal";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.tsxdeleted file mode 100644index 09376acc35b..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/NewWorkspaceButton.tsx+++ /dev/null@@ -1,55 +0,0 @@-import { useDisclosure } from "@mantine/hooks";-import { t } from "ttag";--import {- hasFeature,- hasWorkspacesEnabled,-} from "metabase/common/utils/database";-import { Button, FixedSizeIcon, Tooltip } from "metabase/ui";-import type { Database } from "metabase-types/api";--import { NewWorkspaceModal } from "../NewWorkspaceModal";--export type NewWorkspaceButtonProps = {- databases: Database[];- primary?: boolean;-};--export function NewWorkspaceButton({- databases,- primary,-}: NewWorkspaceButtonProps) {- const [opened, { open, close }] = useDisclosure(false);-- const eligibleDatabases = databases.filter(- (database) =>- hasFeature(database, "workspace") && hasWorkspacesEnabled(database),- );- const hasEligibleDatabase = eligibleDatabases.length > 0;-- return (- <>- <Tooltip- label={t`You need to enable workspaces on at least one database.`}- disabled={hasEligibleDatabase}- >- <Button- variant={primary ? "filled" : "default"}- leftSection={- primary ? undefined : <FixedSizeIcon name="add" aria-hidden />- }- disabled={!hasEligibleDatabase}- onClick={open}- >- {primary ? t`Create a workspace` : t`New`}- </Button>- </Tooltip>- <NewWorkspaceModal- databases={eligibleDatabases}- opened={opened}- onCreate={close}- 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.tsxdeleted file mode 100644index 39eb7975856..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/NewWorkspaceButton.unit.spec.tsx+++ /dev/null@@ -1,40 +0,0 @@-import userEvent from "@testing-library/user-event";--import { renderWithProviders, screen } from "__support__/ui";-import type { Database } from "metabase-types/api";-import { createMockDatabase } from "metabase-types/api/mocks";--import { NewWorkspaceButton } from "./NewWorkspaceButton";--type SetupOpts = {- databases?: Database[];-};--function setup({ databases = [] }: SetupOpts = {}) {- renderWithProviders(<NewWorkspaceButton databases={databases} />);-}--describe("NewWorkspaceButton", () => {- it("opens the New workspace modal when clicked", async () => {- setup({- databases: [- createMockDatabase({- features: ["workspace"],- settings: { "database-enable-workspaces": true },- }),- ],- });-- await userEvent.click(screen.getByRole("button", { name: /New/ }));-- expect(- await screen.findByRole("heading", { name: "Create a workspace" }),- ).toBeInTheDocument();- });-- it("is disabled when no database has workspaces enabled", () => {- setup({ databases: [createMockDatabase({ features: ["workspace"] })] });-- expect(screen.getByRole("button", { name: /New/ })).toBeDisabled();- });-});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.tsdeleted file mode 100644index 7204326d4e0..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/index.ts+++ /dev/null@@ -1 +0,0 @@-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.tsxdeleted file mode 100644index 1659e893adb..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.tsx+++ /dev/null@@ -1,141 +0,0 @@-import { t } from "ttag";-import * as Yup from "yup";--import { useToast } from "metabase/common/hooks/use-toast";-import {- Form,- FormCheckboxGroup,- FormErrorMessage,- FormProvider,- FormSubmitButton,- FormTextInput,-} from "metabase/forms";-import { Button, Checkbox, Group, Modal, Stack } from "metabase/ui";-import * as Errors from "metabase/utils/errors";-import {- useCreateWorkspaceMutation,- useLazyListWorkspacesQuery,-} from "metabase-enterprise/api";-import type { Database, Workspace } from "metabase-types/api";--import { trackWorkspaceCreated } from "../../../analytics";-import { getProvisioningFailureMessage, isUnprovisioned } from "../../../utils";--type NewWorkspaceModalProps = {- databases: Database[];- opened: boolean;- onCreate: (workspace: Workspace) => void;- onClose: () => void;-};--export function NewWorkspaceModal({- databases,- opened,- onCreate,- onClose,-}: NewWorkspaceModalProps) {- return (- <Modal- title={t`Create a workspace`}- opened={opened}- padding="xl"- onClose={onClose}- >- <NewWorkspaceForm- databases={databases}- onCreate={onCreate}- onClose={onClose}- />- </Modal>- );-}--type NewWorkspaceFormValues = {- name: string;- database_ids: string[];-};--const NEW_WORKSPACE_SCHEMA = Yup.object({- name: Yup.string().required(Errors.required),- database_ids: Yup.array().of(Yup.string()).min(1, Errors.required),-});--const INITIAL_VALUES: NewWorkspaceFormValues = {- name: "",- database_ids: [],-};--type NewWorkspaceFormProps = {- databases: Database[];- onCreate: (workspace: Workspace) => void;- onClose: () => void;-};--function NewWorkspaceForm({- databases,- onCreate,- onClose,-}: NewWorkspaceFormProps) {- const [createWorkspace] = useCreateWorkspaceMutation();- const [fetchWorkspaces] = useLazyListWorkspacesQuery();- const [sendToast] = useToast();-- const handleSubmit = async ({- name,- database_ids,- }: NewWorkspaceFormValues) => {- const workspace = await createWorkspace({- name,- database_ids: database_ids.map(Number),- }).unwrap();- trackWorkspaceCreated({ workspaceId: workspace.id });-- const workspaceDatabases = workspace.databases ?? [];- if (workspaceDatabases.some(isUnprovisioned)) {- sendToast({- message: getProvisioningFailureMessage(),- icon: "warning",- toastColor: "feedback-negative",- timeout: null,- });- }-- await fetchWorkspaces();- 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- />- <FormCheckboxGroup name="database_ids" label={t`Databases`}>- <Stack gap="sm" mt="sm">- {databases.map((database) => (- <Checkbox- key={database.id}- value={String(database.id)}- label={database.name}- />- ))}- </Stack>- </FormCheckboxGroup>- <FormErrorMessage />- <Group justify="flex-end">- <Button onClick={onClose}>{t`Cancel`}</Button>- <FormSubmitButton label={t`Create workspace`} 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.tsxdeleted file mode 100644index be957354703..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.unit.spec.tsx+++ /dev/null@@ -1,122 +0,0 @@-import userEvent from "@testing-library/user-event";--import {- setupCreateWorkspaceEndpoint,- setupListWorkspacesEndpoint,-} from "__support__/server-mocks";-import { renderWithProviders, screen, waitFor } from "__support__/ui";-import { UndoListing } from "metabase/common/components/UndoListing";-import type { Workspace } from "metabase-types/api";-import {- createMockDatabase,- createMockWorkspace,- createMockWorkspaceDatabase,-} from "metabase-types/api/mocks";--import { NewWorkspaceModal } from "./NewWorkspaceModal";--const { trackSimpleEvent } = jest.requireMock("metabase/analytics");--type SetupOpts = {- createdWorkspace?: Workspace;-};--const DATABASE = createMockDatabase({ id: 10, name: "Postgres" });--function setup({- createdWorkspace = createMockWorkspace({ name: "Brand new workspace" }),-}: SetupOpts = {}) {- const onCreate = jest.fn();- const onClose = jest.fn();-- setupCreateWorkspaceEndpoint(createdWorkspace);- setupListWorkspacesEndpoint([createdWorkspace]);-- renderWithProviders(- <>- <NewWorkspaceModal- databases={[DATABASE]}- opened- onCreate={onCreate}- onClose={onClose}- />- <UndoListing />- </>,- );-- return { onCreate, onClose, createdWorkspace };-}--describe("NewWorkspaceModal", () => {- beforeEach(() => {- trackSimpleEvent.mockClear();- });-- it("can create a workspace", async () => {- const { onCreate, createdWorkspace } = setup();-- await userEvent.type(screen.getByLabelText("Name"), "Brand new workspace");- await userEvent.click(screen.getByRole("checkbox", { name: "Postgres" }));- await userEvent.click(- screen.getByRole("button", { name: "Create workspace" }),- );-- await waitFor(() =>- expect(onCreate).toHaveBeenCalledWith(createdWorkspace),- );- });-- it("warns when some databases could not be provisioned", async () => {- const { onCreate } = setup({- createdWorkspace: createMockWorkspace({- name: "Brand new workspace",- databases: [- createMockWorkspaceDatabase({- database_id: 10,- status: "unprovisioned",- database: createMockDatabase({ id: 10, name: "Postgres" }),- }),- ],- }),- });-- await userEvent.type(screen.getByLabelText("Name"), "Brand new workspace");- await userEvent.click(screen.getByRole("checkbox", { name: "Postgres" }));- await userEvent.click(- screen.getByRole("button", { name: "Create workspace" }),- );-- expect(- await screen.findByText("Failed to provision the workspace."),- ).toBeInTheDocument();- await waitFor(() => expect(onCreate).toHaveBeenCalled());- });-- it("requires at least one database", async () => {- const { onCreate } = setup();-- await userEvent.type(screen.getByLabelText("Name"), "Brand new workspace");- await userEvent.click(- screen.getByRole("button", { name: "Create workspace" }),- );-- expect(onCreate).not.toHaveBeenCalled();- });-- it("tracks an analytics event when a workspace is created", async () => {- const { createdWorkspace } = setup();-- await userEvent.type(screen.getByLabelText("Name"), "Brand new workspace");- await userEvent.click(screen.getByRole("checkbox", { name: "Postgres" }));- await userEvent.click(- screen.getByRole("button", { name: "Create workspace" }),- );-- await waitFor(() =>- expect(trackSimpleEvent).toHaveBeenCalledWith({- event: "workspaces_new_created",- target_id: createdWorkspace.id,- }),- );- });-});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.tsdeleted file mode 100644index 81a7209216c..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from "./NewWorkspaceModal";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/RenameWorkspaceModal/RenameWorkspaceModal.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/RenameWorkspaceModal/RenameWorkspaceModal.tsxdeleted file mode 100644index de3697ec4bc..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/RenameWorkspaceModal/RenameWorkspaceModal.tsx+++ /dev/null@@ -1,92 +0,0 @@-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 { useUpdateWorkspaceMutation } from "metabase-enterprise/api";-import type { Workspace } from "metabase-types/api";--export type RenameWorkspaceModalProps = {- workspace: Workspace;- opened: boolean;- onRename: (workspace: Workspace) => void;- onClose: () => void;-};--export function RenameWorkspaceModal({- workspace,- opened,- onRename,- onClose,-}: RenameWorkspaceModalProps) {- return (- <Modal- title={t`Rename this workspace?`}- opened={opened}- padding="xl"- onClose={onClose}- >- <RenameWorkspaceForm- workspace={workspace}- onRename={onRename}- onClose={onClose}- />- </Modal>- );-}--type RenameWorkspaceFormValues = {- name: string;-};--const RENAME_WORKSPACE_SCHEMA = Yup.object({- name: Yup.string().required(Errors.required),-});--type RenameWorkspaceFormProps = {- workspace: Workspace;- onRename: (workspace: Workspace) => void;- onClose: () => void;-};--function RenameWorkspaceForm({- workspace,- onRename,- onClose,-}: RenameWorkspaceFormProps) {- const [updateWorkspace] = useUpdateWorkspaceMutation();-- const handleSubmit = async ({ name }: RenameWorkspaceFormValues) => {- const updatedWorkspace = await updateWorkspace({- id: workspace.id,- name,- }).unwrap();- onRename(updatedWorkspace);- };-- return (- <FormProvider- initialValues={{ name: workspace.name }}- validationSchema={RENAME_WORKSPACE_SCHEMA}- onSubmit={handleSubmit}- >- <Form>- <Stack gap="lg">- <FormTextInput name="name" label={t`Name`} data-autofocus />- <FormErrorMessage />- <Group justify="flex-end">- <Button onClick={onClose}>{t`Cancel`}</Button>- <FormSubmitButton label={t`Rename`} variant="filled" />- </Group>- </Stack>- </Form>- </FormProvider>- );-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/RenameWorkspaceModal/RenameWorkspaceModal.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/RenameWorkspaceModal/RenameWorkspaceModal.unit.spec.tsxdeleted file mode 100644index 2c9132b5ac2..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/RenameWorkspaceModal/RenameWorkspaceModal.unit.spec.tsx+++ /dev/null@@ -1,58 +0,0 @@-import userEvent from "@testing-library/user-event";-import fetchMock from "fetch-mock";--import { setupUpdateWorkspaceEndpoint } from "__support__/server-mocks";-import { renderWithProviders, screen, waitFor } from "__support__/ui";-import { createMockWorkspace } from "metabase-types/api/mocks";--import { RenameWorkspaceModal } from "./RenameWorkspaceModal";--const WORKSPACE = createMockWorkspace({ id: 1, name: "Old name" });--function setup() {- setupUpdateWorkspaceEndpoint(WORKSPACE);-- const onRename = jest.fn();- const onClose = jest.fn();-- renderWithProviders(- <RenameWorkspaceModal- workspace={WORKSPACE}- opened- onRename={onRename}- onClose={onClose}- />,- );-- return { onRename, onClose };-}--describe("RenameWorkspaceModal", () => {- it("prefills the current name and submits the new one", async () => {- const { onRename } = setup();-- const input = screen.getByLabelText("Name");- expect(input).toHaveValue("Old name");-- await userEvent.clear(input);- await userEvent.type(input, "New name");- await userEvent.click(screen.getByRole("button", { name: "Rename" }));-- await waitFor(() => {- expect(onRename).toHaveBeenCalled();- });- const call = fetchMock.callHistory.lastCall(- `path:/api/ee/workspace-manager/1`,- );- expect(await call?.request?.json()).toEqual({ name: "New name" });- });-- it("closes without renaming on Cancel", async () => {- const { onRename, onClose } = setup();-- await userEvent.click(screen.getByRole("button", { name: "Cancel" }));-- expect(onClose).toHaveBeenCalled();- expect(onRename).not.toHaveBeenCalled();- });-});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/RenameWorkspaceModal/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/RenameWorkspaceModal/index.tsdeleted file mode 100644index e2b61b99527..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/RenameWorkspaceModal/index.ts+++ /dev/null@@ -1 +0,0 @@-export { RenameWorkspaceModal } from "./RenameWorkspaceModal";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/SetupWorkspaceModal/SetupWorkspaceModal.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/SetupWorkspaceModal/SetupWorkspaceModal.tsxdeleted file mode 100644index 377849a1ee0..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/SetupWorkspaceModal/SetupWorkspaceModal.tsx+++ /dev/null@@ -1,108 +0,0 @@-import { jt, t } from "ttag";--import {- Form,- FormErrorMessage,- FormFileInput,- FormProvider,- FormSubmitButton,-} from "metabase/forms";-import { useDispatch } from "metabase/redux";-import { push } from "metabase/router";-import {- Button,- Code,- FixedSizeIcon,- FocusTrap,- Group,- Modal,- Stack,- Text,-} from "metabase/ui";-import * as Urls from "metabase/urls";-import { useApplyAdvancedConfigMutation } from "metabase-enterprise/api";--import { trackWorkspaceInstanceSetup } from "../../../analytics";--const CONFIG_FILENAME = "config.yml";--type SetupWorkspaceModalProps = {- opened: boolean;- onClose: () => void;-};--export function SetupWorkspaceModal({- opened,- onClose,-}: SetupWorkspaceModalProps) {- return (- <Modal- title={t`Set up a workspace`}- opened={opened}- padding="xl"- onClose={onClose}- >- <FocusTrap.InitialFocus />- <SetupWorkspaceForm onClose={onClose} />- </Modal>- );-}--type SetupWorkspaceFormValues = {- config: File | null;-};--const INITIAL_VALUES: SetupWorkspaceFormValues = {- config: null,-};--type SetupWorkspaceFormProps = {- onClose: () => void;-};--function SetupWorkspaceForm({ onClose }: SetupWorkspaceFormProps) {- const dispatch = useDispatch();- const [applyConfig] = useApplyAdvancedConfigMutation();-- const handleSubmit = async ({ config }: SetupWorkspaceFormValues) => {- if (!config) {- return;- }- await applyConfig({ config }).unwrap();- trackWorkspaceInstanceSetup();- onClose();- dispatch(push(Urls.workspaces()));- };-- return (- <FormProvider initialValues={INITIAL_VALUES} onSubmit={handleSubmit}>- {({ values }) => (- <Form>- <Stack gap="lg">- <Text c="text-secondary">- {jt`Upload the ${(- <Code key="config">{CONFIG_FILENAME}</Code>- )} you downloaded from a workspace on your production instance.`}- </Text>- <FormFileInput- name="config"- label={t`Config file`}- placeholder={t`Choose a ${CONFIG_FILENAME} file`}- accept=".yml,.yaml,application/yaml,text/yaml"- leftSection={<FixedSizeIcon name="attachment" aria-hidden />}- />- <FormErrorMessage />- <Group justify="flex-end">- <Button onClick={onClose}>{t`Cancel`}</Button>- <FormSubmitButton- label={t`Set up`}- variant="filled"- disabled={values.config == null}- />- </Group>- </Stack>- </Form>- )}- </FormProvider>- );-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/SetupWorkspaceModal/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/SetupWorkspaceModal/index.tsdeleted file mode 100644index 66392812b9e..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/SetupWorkspaceModal/index.ts+++ /dev/null@@ -1 +0,0 @@-export { SetupWorkspaceModal } from "./SetupWorkspaceModal";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.cssdeleted file mode 100644index 3868f1258e1..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.module.css+++ /dev/null@@ -1,9 +0,0 @@-.docsLink {- color: inherit;- text-decoration: none;- background-color: var(--mb-color-background_page-secondary);-}--.docsLink:hover {- background-color: var(--mb-color-background_surface-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.tsxdeleted file mode 100644index 69df2534384..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.tsx+++ /dev/null@@ -1,123 +0,0 @@-import { useDisclosure } from "@mantine/hooks";-import { t } from "ttag";--import { useDocsUrl } from "metabase/common/hooks";-import { useSelector } from "metabase/redux";-import { Link } from "metabase/router";-import { getApplicationName } from "metabase/selectors/whitelabel";-import {- Box,- Button,- Card,- Divider,- FixedSizeIcon,- Group,- Stack,- Text,- Title,-} from "metabase/ui";-import type { Database } from "metabase-types/api";--import { trackWorkspaceSetupButtonClicked } from "../../../analytics";-import { NewWorkspaceButton } from "../NewWorkspaceButton";-import { SetupWorkspaceModal } from "../SetupWorkspaceModal";--import S from "./WorkspaceEmptyState.module.css";--export type WorkspaceEmptyStateProps = {- databases: Database[];-};--export function WorkspaceEmptyState({ databases }: WorkspaceEmptyStateProps) {- const [isSetupOpen, { open: openSetup, close: closeSetup }] =- useDisclosure(false);- const applicationName = useSelector(getApplicationName);-- const { url: fileBasedDevDocsUrl, showMetabaseLinks: showFileBasedDevLink } =- useDocsUrl("ai/file-based-development");- const { url: remoteSyncDocsUrl, showMetabaseLinks: showRemoteSyncLink } =- useDocsUrl("installation-and-operation/remote-sync");-- const handleSetupClick = () => {- trackWorkspaceSetupButtonClicked();- openSetup();- };-- return (- <>- <Card p="xl" maw="40rem" mx="auto" shadow="none" withBorder>- <Box p="md">- <Title- order={3}- mb="sm"- >{t`Use Workspaces to develop your semantic layer safely`}</Title>- <Text mb="md">- {t`While in a workspace, ${applicationName} will remap tables created by transforms to an isolated schema, letting you test and build on top of these tables. When you're ready, use remote sync to pull your changes into your production ${applicationName}.`}- </Text>- <Text mb="lg">- {t`If this is your production instance, create and download a workspace config here to use in a development instance.`}{" "}- {t`If you're using this ${applicationName} instance for development, you can upload a workspace config file to put this instance into that workspace.`}- </Text>- <Group gap="md">- <NewWorkspaceButton databases={databases} primary />- <Button variant="default" onClick={handleSetupClick}>- {t`Upload a workspace config`}- </Button>- </Group>- {(showFileBasedDevLink || showRemoteSyncLink) && (- <>- <Divider my="xl" />- <Group gap="sm" align="stretch">- {showFileBasedDevLink && (- <DocsLink- title={t`Agent-driven development`}- description={t`How to use the CLI to develop content locally.`}- link={fileBasedDevDocsUrl}- />- )}- {showRemoteSyncLink && (- <DocsLink- title={t`Using remote sync`}- description={t`How to sync and review ${applicationName} content with git.`}- link={remoteSyncDocsUrl}- />- )}- </Group>- </>- )}- </Box>- </Card>- <SetupWorkspaceModal opened={isSetupOpen} onClose={closeSetup} />- </>- );-}--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="core-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.tsxdeleted file mode 100644index faf8465ab41..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.unit.spec.tsx+++ /dev/null@@ -1,48 +0,0 @@-import { setupApplyAdvancedConfigEndpoint } from "__support__/server-mocks";-import { renderWithProviders, screen } from "__support__/ui";-import { createMockSettingsState } from "metabase/redux/store/mocks";-import { Route } from "metabase/router";--import { WorkspaceEmptyState } from "./WorkspaceEmptyState";--function setup() {- setupApplyAdvancedConfigEndpoint();- renderWithProviders(- <Route path="*" element={<WorkspaceEmptyState databases={[]} />} />,- {- withRouter: true,- storeInitialState: {- settings: createMockSettingsState({ "show-metabase-links": true }),- },- },- );-}--describe("WorkspaceEmptyState", () => {- it("renders the create workspace button", () => {- setup();-- expect(- screen.getByRole("button", { name: "Create a workspace" }),- ).toBeInTheDocument();- });-- it("renders docs link buttons", () => {- setup();-- expect(- screen.getByRole("link", { name: /Agent-driven development/ }),- ).toBeInTheDocument();- expect(- screen.getByRole("link", { name: /Using remote sync/ }),- ).toBeInTheDocument();- });-- it("shows the upload-config button", () => {- setup();-- expect(- screen.getByRole("button", { name: "Upload a workspace config" }),- ).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.tsdeleted file mode 100644index d35a8872ac8..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/index.ts+++ /dev/null@@ -1 +0,0 @@-export { WorkspaceEmptyState } from "./WorkspaceEmptyState";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.tsxdeleted file mode 100644index 4bb84881e54..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.tsx+++ /dev/null@@ -1,168 +0,0 @@-import { useDisclosure } from "@mantine/hooks";-import { t } from "ttag";--import {- ActionIcon,- Box,- Card,- FixedSizeIcon,- Group,- Menu,- Stack,-} from "metabase/ui";-import { getRelativeTime } from "metabase/utils/time-dayjs";-import { getUserName } from "metabase/utils/user";-import type { Workspace, WorkspaceDatabase } from "metabase-types/api";--import { trackWorkspaceConfigDownloaded } from "../../../analytics";-import {- getProvisioningFailureMessage,- getWorkspaceDatabaseName,- isUnprovisioned,-} from "../../../utils";-import { DeleteWorkspaceModal } from "../DeleteWorkspaceModal";-import { RenameWorkspaceModal } from "../RenameWorkspaceModal";--const CONFIG_FILENAME = "config.yml";--export type WorkspaceItemProps = {- workspace: Workspace;-};--export function WorkspaceItem({ workspace }: WorkspaceItemProps) {- const databases = workspace.databases ?? [];-- return (- <Card- role="region"- aria-label={workspace.name}- data-testid="workspace-item"- p="lg"- shadow="none"- withBorder- >- <Group justify="space-between" align="flex-start" wrap="nowrap">- <Stack gap="sm">- <Box fw="bold" fz="1rem" lh="1rem">- {workspace.name}- </Box>- <WorkspaceCreatorInfo workspace={workspace} />- {databases.some(isUnprovisioned) && <WorkspaceProvisioningWarning />}- {databases.map((workspaceDatabase) => (- <WorkspaceDatabaseItem- key={workspaceDatabase.database_id}- workspaceDatabase={workspaceDatabase}- />- ))}- </Stack>- <WorkspaceMenu workspace={workspace} />- </Group>- </Card>- );-}--type WorkspaceCreatorInfoProps = {- workspace: Workspace;-};--function WorkspaceCreatorInfo({ workspace }: WorkspaceCreatorInfoProps) {- const { creator } = workspace;- const timeAgo = getRelativeTime(workspace.created_at);-- return (- <Box c="text-secondary" lh="1rem">- {creator != null- ? t`Created by ${getUserName(creator)} ${timeAgo}`- : t`Created ${timeAgo}`}- </Box>- );-}--type WorkspaceDatabaseItemProps = {- workspaceDatabase: WorkspaceDatabase;-};--function WorkspaceProvisioningWarning() {- return (- <Box c="text-secondary" lh="1rem">- <Group gap="xs" wrap="nowrap">- <FixedSizeIcon name="warning" aria-hidden />- {getProvisioningFailureMessage()}- </Group>- </Box>- );-}--function WorkspaceDatabaseItem({- workspaceDatabase,-}: WorkspaceDatabaseItemProps) {- return (- <Box c="text-secondary" lh="1rem">- <Group gap="xs" wrap="nowrap">- <FixedSizeIcon name="database" aria-hidden />- {getWorkspaceDatabaseName(workspaceDatabase)}- </Group>- </Box>- );-}--type WorkspaceMenuProps = {- workspace: Workspace;-};--function WorkspaceMenu({ workspace }: WorkspaceMenuProps) {- const [isRenameOpen, { open: openRename, close: closeRename }] =- useDisclosure(false);- const [isDeleteOpen, { open: openDelete, close: closeDelete }] =- useDisclosure(false);-- return (- <>- <Menu position="bottom-end">- <Menu.Target>- <ActionIcon aria-label={t`Workspace options`}>- <FixedSizeIcon name="ellipsis" aria-hidden />- </ActionIcon>- </Menu.Target>- <Menu.Dropdown>- <Menu.Item- component="a"- href={`/api/ee/workspace-manager/${workspace.id}/config`}- download={CONFIG_FILENAME}- leftSection={<FixedSizeIcon name="download" aria-hidden />}- onClick={() =>- trackWorkspaceConfigDownloaded({ workspaceId: workspace.id })- }- >- {t`Download ${CONFIG_FILENAME}`}- </Menu.Item>- <Menu.Item- leftSection={<FixedSizeIcon name="pencil" aria-hidden />}- onClick={openRename}- >- {t`Rename`}- </Menu.Item>- <Menu.Item- leftSection={<FixedSizeIcon name="trash" aria-hidden />}- onClick={openDelete}- >- {t`Delete`}- </Menu.Item>- </Menu.Dropdown>- </Menu>- <RenameWorkspaceModal- workspace={workspace}- opened={isRenameOpen}- onRename={closeRename}- onClose={closeRename}- />- {isDeleteOpen && (- <DeleteWorkspaceModal- workspaceId={workspace.id}- onDelete={closeDelete}- onClose={closeDelete}- />- )}- </>- );-}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.tsxdeleted file mode 100644index 9aed518284b..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.unit.spec.tsx+++ /dev/null@@ -1,156 +0,0 @@-import userEvent from "@testing-library/user-event";--import { setupGetWorkspaceEndpoint } from "__support__/server-mocks";-import { renderWithProviders, screen } from "__support__/ui";-import {- createMockDatabase,- createMockUserInfo,- createMockWorkspace,- createMockWorkspaceDatabase,-} from "metabase-types/api/mocks";--import { WorkspaceItem } from "./WorkspaceItem";--const WORKSPACE = createMockWorkspace({ id: 1, name: "My workspace" });--function setup({ workspace = WORKSPACE } = {}) {- // The delete modal fetches the hydrated workspace when opened.- setupGetWorkspaceEndpoint(workspace);- renderWithProviders(<WorkspaceItem workspace={workspace} />);-}--describe("WorkspaceItem", () => {- it("renders the workspace name", () => {- setup();- expect(screen.getByText("My workspace")).toBeInTheDocument();- });-- it("renders the creator info when the creator is hydrated", () => {- setup({- workspace: createMockWorkspace({- name: "My workspace",- creator: createMockUserInfo({- first_name: "Ada",- last_name: "Lovelace",- }),- }),- });-- expect(screen.getByText(/Created by Ada Lovelace/)).toBeInTheDocument();- });-- it("renders plain creation info when the creator is missing", () => {- setup({- workspace: createMockWorkspace({ name: "My workspace", creator: null }),- });-- expect(screen.getByText(/^Created /)).toBeInTheDocument();- });-- it("renders databases, falling back to the id when not hydrated", () => {- setup({- workspace: createMockWorkspace({- id: 1,- name: "My workspace",- databases: [- createMockWorkspaceDatabase({- database_id: 10,- database: createMockDatabase({ id: 10, name: "Postgres" }),- }),- createMockWorkspaceDatabase({- database_id: 20,- database: null,- }),- ],- }),- });-- expect(screen.getByText("Postgres")).toBeInTheDocument();- expect(screen.getByText("Database 20")).toBeInTheDocument();- });-- it("warns when a database is not provisioned", () => {- setup({- workspace: createMockWorkspace({- id: 1,- name: "My workspace",- databases: [- createMockWorkspaceDatabase({- database_id: 10,- status: "unprovisioned",- database: createMockDatabase({ id: 10, name: "Postgres" }),- }),- ],- }),- });-- expect(- screen.getByText("Failed to provision the workspace."),- ).toBeInTheDocument();- expect(screen.getByText("Postgres")).toBeInTheDocument();- });-- it("does not warn when all databases are provisioned", () => {- setup({- workspace: createMockWorkspace({- id: 1,- name: "My workspace",- databases: [- createMockWorkspaceDatabase({- database_id: 10,- status: "provisioned",- database: createMockDatabase({ id: 10, name: "Postgres" }),- }),- ],- }),- });-- expect(- screen.queryByText("Failed to provision the workspace."),- ).not.toBeInTheDocument();- });-- it("offers a config download link in the menu", async () => {- setup();- await userEvent.click(- screen.getByRole("button", { name: "Workspace options" }),- );-- const downloadItem = await screen.findByRole("menuitem", {- name: /Download config\.yml/,- });- expect(downloadItem).toHaveAttribute(- "href",- `/api/ee/workspace-manager/${WORKSPACE.id}/config`,- );- expect(downloadItem).toHaveAttribute("download", "config.yml");- });-- it("opens the rename modal from the menu", async () => {- setup();- await userEvent.click(- screen.getByRole("button", { name: "Workspace options" }),- );- await userEvent.click(- await screen.findByRole("menuitem", { name: "Rename" }),- );-- expect(- await screen.findByRole("heading", { name: "Rename this workspace?" }),- ).toBeInTheDocument();- });-- it("opens the delete modal from the menu", async () => {- setup();- await userEvent.click(- screen.getByRole("button", { name: "Workspace options" }),- );- await userEvent.click(- await screen.findByRole("menuitem", { name: "Delete" }),- );-- expect(- await screen.findByRole("heading", { name: "Delete this workspace?" }),- ).toBeInTheDocument();- });-});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.tsdeleted file mode 100644index 36844fc8a1b..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/index.ts+++ /dev/null@@ -1 +0,0 @@-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.tsxdeleted file mode 100644index 8afb7eff734..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceListPage.tsx+++ /dev/null@@ -1,79 +0,0 @@-import { t } from "ttag";--import { useListDatabasesQuery } from "metabase/api";-import { DelayedLoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper/DelayedLoadingAndErrorWrapper";-import { DataStudioBreadcrumbs } from "metabase/common/data-studio/components/DataStudioBreadcrumbs";-import { PageContainer } from "metabase/common/data-studio/components/PageContainer";-import { PaneHeader } from "metabase/common/data-studio/components/PaneHeader";-import { Stack } from "metabase/ui";-import { useListWorkspacesQuery } from "metabase-enterprise/api";-import type { Database, Workspace } from "metabase-types/api";--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}- databases={databasesResponse.data}- />- );-}--type WorkspaceListPageBodyProps = {- workspaces: Workspace[];- databases: Database[];-};--function WorkspaceListPageBody({- workspaces,- databases,-}: WorkspaceListPageBodyProps) {- const hasWorkspaces = workspaces.length > 0;-- return (- <PageContainer data-testid="workspace-list-page">- <PaneHeader- breadcrumbs={- <DataStudioBreadcrumbs>{t`Workspaces`}</DataStudioBreadcrumbs>- }- actions={hasWorkspaces && <NewWorkspaceButton databases={databases} />}- py={0}- />- {hasWorkspaces ? (- <Stack data-testid="workspace-list">- {workspaces.map((workspace) => (- <WorkspaceItem key={workspace.id} workspace={workspace} />- ))}- </Stack>- ) : (- <WorkspaceEmptyState databases={databases} />- )}- </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.tsxdeleted file mode 100644index 038f5e1e17c..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceListPage.unit.spec.tsx+++ /dev/null@@ -1,61 +0,0 @@-import userEvent from "@testing-library/user-event";--import {- setupCreateWorkspaceEndpoint,- setupDatabasesEndpoints,- setupListWorkspacesEndpoint,-} from "__support__/server-mocks";-import { renderWithProviders, screen } from "__support__/ui";-import { Route } from "metabase/router";-import type { Workspace } from "metabase-types/api";-import {- createMockDatabase,- createMockWorkspace,-} from "metabase-types/api/mocks";--import { WorkspaceListPage } from "./WorkspaceListPage";--const ELIGIBLE_DATABASE = createMockDatabase({- features: ["workspace"],- settings: { "database-enable-workspaces": true },-});--// Unjustified type cast. FIXME-function setup({ workspaces = [] as Workspace[] } = {}) {- setupListWorkspacesEndpoint(workspaces);- setupDatabasesEndpoints([ELIGIBLE_DATABASE]);- setupCreateWorkspaceEndpoint(createMockWorkspace({ name: "Brand new" }));-- renderWithProviders(<Route path="*" element={<WorkspaceListPage />} />, {- withRouter: true,- });-}--describe("WorkspaceListPage", () => {- it("from the empty state, opens the Add workspace modal", async () => {- setup();-- expect(- await screen.findByTestId("workspace-list-page"),- ).toBeInTheDocument();-- await userEvent.click(- await screen.findByRole("button", { name: /Create a workspace/i }),- );-- expect(- await screen.findAllByRole("heading", { name: /Create a workspace/i }),- ).not.toHaveLength(0);- });-- it("when not empty, renders a card with a menu for each workspace", async () => {- const workspace = createMockWorkspace({ id: 42, name: "Existing" });- setup({ workspaces: [workspace] });-- const item = await screen.findByRole("region", { name: "Existing" });- expect(item).toBeInTheDocument();- expect(- screen.getByRole("button", { name: "Workspace options" }),- ).toBeInTheDocument();- });-});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/index.tsdeleted file mode 100644index 16f93531518..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from "./WorkspaceListPage";diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/routes.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/routes.tsxdeleted file mode 100644index 0a81036eeaa..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/routes.tsx+++ /dev/null@@ -1,25 +0,0 @@-import { Route, type RouteComponent, withRouteProps } from "metabase/router";--import { AdminConnectionInfoPage } from "./pages/AdminConnectionInfoPage";-import { WorkspaceIndexPage } from "./pages/WorkspaceIndexPage";--const RoutedAdminConnectionInfoPage = withRouteProps(AdminConnectionInfoPage);--export function getDataStudioRoutes() {- return (- <Route path="workspaces">- <Route index element={<WorkspaceIndexPage />} />- </Route>- );-}--export function getWorkspaceDatabaseRoutes(IsAdmin: RouteComponent) {- return (- <Route element={<IsAdmin />}>- <Route- path=":databaseId/admin"- element={<RoutedAdminConnectionInfoPage />}- />- </Route>- );-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/selectors.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/selectors.tsdeleted file mode 100644index 6efae4c4bd3..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/selectors.ts+++ /dev/null@@ -1,6 +0,0 @@-import type { State } from "metabase/redux/store";-import { getUserIsAdmin } from "metabase/selectors/user";--export function canManageWorkspaces(state: State): boolean {- return getUserIsAdmin(state);-}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/utils.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/utils.tsdeleted file mode 100644index b7a6c53d1c5..00000000000--- a/enterprise/frontend/src/metabase-enterprise/workspaces/utils.ts+++ /dev/null@@ -1,23 +0,0 @@-import { t } from "ttag";--import type { WorkspaceDatabase } from "metabase-types/api";--export function getWorkspaceDatabaseName(workspaceDatabase: WorkspaceDatabase) {- return (- workspaceDatabase.database?.name ??- t`Database ${workspaceDatabase.database_id}`- );-}--export function isUnprovisioned(workspaceDatabase: WorkspaceDatabase) {- return workspaceDatabase.status === "unprovisioned";-}--export function isPending(workspaceDatabase: WorkspaceDatabase) {- const { status } = workspaceDatabase;- return status === "provisioning" || status === "deprovisioning";-}--export function getProvisioningFailureMessage() {- return t`Failed to provision the workspace.`;-}diff --git a/frontend/src/metabase-types/api/advanced-config.ts b/frontend/src/metabase-types/api/advanced-config.tsindex ac4d17fa56c..74b48e006f0 100644--- a/frontend/src/metabase-types/api/advanced-config.ts+++ b/frontend/src/metabase-types/api/advanced-config.ts@@ -1,19 +1,11 @@-import type { CurrentWorkspaceDatabase } from "./workspace-instance";-export type AdvancedConfigDatabase = {name: string;engine: string;details: Record<string, unknown>;};-export type AdvancedConfigWorkspace = {- name: string;- databases: Record<string, CurrentWorkspaceDatabase>;-};-export type AdvancedConfigContent = {databases?: AdvancedConfigDatabase[];- workspace?: AdvancedConfigWorkspace;};export type AdvancedConfig = {diff --git a/frontend/src/metabase-types/api/database.ts b/frontend/src/metabase-types/api/database.tsindex 9cb37b8ad1a..4b7683dfc21 100644--- a/frontend/src/metabase-types/api/database.ts+++ b/frontend/src/metabase-types/api/database.ts@@ -10,7 +10,6 @@ export type InitialSyncStatus = LongTaskStatus;export type DatabaseSettings = {[key: string]: any;"database-enable-actions"?: boolean;- "database-enable-workspaces"?: boolean;};export type DatabaseFeature =@@ -58,8 +57,7 @@ export type DatabaseFeature =| "split-part"| "collate"| "transforms/python"- | "transforms/table"- | "workspace";+ | "transforms/table";export interface Database extends DatabaseData {id: DatabaseId;diff --git a/frontend/src/metabase-types/api/icon.ts b/frontend/src/metabase-types/api/icon.tsindex a23d4eba2af..4541e3a2b12 100644--- a/frontend/src/metabase-types/api/icon.ts+++ b/frontend/src/metabase-types/api/icon.ts@@ -296,7 +296,6 @@ export type IconName =| "warning_triangle_filled"| "waterfall"| "webhook"- | "workspace"| "10k"| "1m"| "zap"diff --git a/frontend/src/metabase-types/api/index.ts b/frontend/src/metabase-types/api/index.tsindex 35bfd36b158..17b94fd12f4 100644--- a/frontend/src/metabase-types/api/index.ts+++ b/frontend/src/metabase-types/api/index.ts@@ -77,8 +77,6 @@ export * from "./util";export * from "./visualization";export * from "./visualization-settings";export * from "./visualizer";-export * from "./workspace-manager";-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 36b1399e657..13acbb71140 100644--- a/frontend/src/metabase-types/api/mocks/index.ts+++ b/frontend/src/metabase-types/api/mocks/index.ts@@ -51,5 +51,3 @@ 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 20f0065d036..043a5159d6d 100644--- a/frontend/src/metabase-types/api/mocks/settings.ts+++ b/frontend/src/metabase-types/api/mocks/settings.ts@@ -154,7 +154,6 @@ export const createMockTokenFeatures = (writable_connection: false,admin_security_center: false,ai_controls: false,- workspaces: false,...opts,});diff --git a/frontend/src/metabase-types/api/mocks/workspace-instance.ts b/frontend/src/metabase-types/api/mocks/workspace-instance.tsdeleted file mode 100644index a900d36b0ab..00000000000--- a/frontend/src/metabase-types/api/mocks/workspace-instance.ts+++ /dev/null@@ -1,46 +0,0 @@-import type {- CurrentWorkspace,- CurrentWorkspaceDatabase,- TableRemapping,-} from "../workspace-instance";--export function createMockCurrentWorkspaceDatabase(- opts?: Partial<CurrentWorkspaceDatabase>,-): CurrentWorkspaceDatabase {- return {- input_schemas: [],- output: { schema: null, db: null },- ...opts,- };-}--export function createMockCurrentWorkspace(- opts?: Partial<CurrentWorkspace>,-): CurrentWorkspace {- return {- name: "Test workspace",- databases: {},- // Default to a non-writable workspace so tests that don't care about- // lock state behave like a deployment-managed instance. Opt into the- // editable case with createMockCurrentWorkspace({ can_write: true }).- can_write: false,- ...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.tsdeleted file mode 100644index 137e2a1480b..00000000000--- a/frontend/src/metabase-types/api/mocks/workspace.ts+++ /dev/null@@ -1,23 +0,0 @@-import type { Workspace, WorkspaceDatabase } from "../workspace-manager";--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/settings.ts b/frontend/src/metabase-types/api/settings.tsindex 64fc15435db..94f7073d6b7 100644--- a/frontend/src/metabase-types/api/settings.ts+++ b/frontend/src/metabase-types/api/settings.ts@@ -385,7 +385,6 @@ export const tokenFeatures = ["writable_connection","admin_security_center","ai_controls",- "workspaces",] as const;export type TokenFeature = (typeof tokenFeatures)[number];diff --git a/frontend/src/metabase-types/api/user.ts b/frontend/src/metabase-types/api/user.tsindex 7853159d5d9..ba13be4bc16 100644--- a/frontend/src/metabase-types/api/user.ts+++ b/frontend/src/metabase-types/api/user.ts@@ -60,7 +60,6 @@ export interface UserPermissions {can_access_subscription?: boolean;can_access_data_studio?: boolean;can_access_transforms?: boolean;- can_access_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.tsdeleted file mode 100644index da6714e279e..00000000000--- a/frontend/src/metabase-types/api/workspace-instance.ts+++ /dev/null@@ -1,35 +0,0 @@-import type { DatabaseId } from "./database";--export type TableNamespace = {- db?: string | null;- schema?: string | null;-};--export type CurrentWorkspaceDatabase = {- input_schemas: string[];- output: TableNamespace;-};--export type CurrentWorkspace = {- name: string;- databases: Record<DatabaseId, CurrentWorkspaceDatabase>;- can_write: boolean;-};--export type GetCurrentWorkspaceResponse = {- data: CurrentWorkspace | null;-};--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-manager.ts b/frontend/src/metabase-types/api/workspace-manager.tsdeleted file mode 100644index 6e7f9d6bd2d..00000000000--- a/frontend/src/metabase-types/api/workspace-manager.ts+++ /dev/null@@ -1,38 +0,0 @@-import type { Database, 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;-- database?: Database | null;-};--export type Workspace = {- id: WorkspaceId;- name: string;- created_at: string;- creator_id: UserId;-- creator?: UserInfo | null;- databases?: WorkspaceDatabase[];-};--export type CreateWorkspaceRequest = {- name: string;- database_ids: DatabaseId[];-};--export type UpdateWorkspaceRequest = {- id: WorkspaceId;- name?: string;-};diff --git a/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx b/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsxindex fe3d7a1d465..dd0bb615b71 100644--- a/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx+++ b/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx@@ -19,7 +19,6 @@ import {PLUGIN_DATABASE_REPLICATION,PLUGIN_DB_ROUTING,PLUGIN_TABLE_EDITING,- PLUGIN_WORKSPACES,PLUGIN_WRITABLE_CONNECTION,} from "metabase/plugins";import { connect, useSelector } from "metabase/redux";@@ -119,10 +118,6 @@ function DatabaseEditAppInner({database={database}/>- <PLUGIN_WORKSPACES.WorkspaceDatabaseSection- database={database}- />-<DatabaseModelFeaturesSectiondatabase={database}isModelPersistenceEnabled={isModelPersistenceEnabled}diff --git a/frontend/src/metabase/admin/routes.tsx b/frontend/src/metabase/admin/routes.tsxindex 7e3a0938888..aba827b2b63 100644--- a/frontend/src/metabase/admin/routes.tsx+++ b/frontend/src/metabase/admin/routes.tsx@@ -44,7 +44,6 @@ import {PLUGIN_SECURITY_CENTER,PLUGIN_SUPPORT,PLUGIN_TENANTS,- PLUGIN_WORKSPACES,PLUGIN_WRITABLE_CONNECTION,PerformanceTabId,} from "metabase/plugins";@@ -87,7 +86,6 @@ export const getRoutes = (</Route><Route path=":databaseId/edit" element={<DatabasePage />} />{PLUGIN_WRITABLE_CONNECTION.getWritableConnectionInfoRoutes(IsAdmin)}- {PLUGIN_WORKSPACES.getWorkspaceDatabaseRoutes(IsAdmin)}<Route path=":databaseId" element={<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 a5ba21c986b..bc9d26c401b 100644--- a/frontend/src/metabase/api/tags/constants.ts+++ b/frontend/src/metabase/api/tags/constants.ts@@ -51,7 +51,6 @@ 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 40f01c4081c..7ac1fb59d00 100644--- a/frontend/src/metabase/api/tags/utils.ts+++ b/frontend/src/metabase/api/tags/utils.ts@@ -49,7 +49,6 @@ import type {Table,TableIndexEntry,TableIndexRequest,- TableRemapping,Task,TaskRun,Timeline,@@ -712,21 +711,6 @@ 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 67cf0828e43..277fb62611e 100644--- a/frontend/src/metabase/api/transform-job.ts+++ b/frontend/src/metabase/api/transform-job.ts@@ -86,7 +86,6 @@ export const transformJobApi = Api.injectEndpoints({listTag("transform-run"),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 3690054d8d1..dbe5d9fa37d 100644--- a/frontend/src/metabase/api/transform.ts+++ b/frontend/src/metabase/api/transform.ts@@ -76,7 +76,6 @@ export const transformApi = Api.injectEndpoints({invalidateTags(error, [idTag("transform", id),tag("table"),- listTag("table-remapping"),listTag("transform-run"),listTag("table-index"),]),@@ -167,7 +166,6 @@ export const transformApi = Api.injectEndpoints({idTag("transform", id),tag("transform"),tag("table"),- listTag("table-remapping"),listTag("transform-run"),listTag("transform-dag-run"),]),diff --git a/frontend/src/metabase/common/utils/database.ts b/frontend/src/metabase/common/utils/database.tsindex a5544c31b46..ff7fb1dc084 100644--- a/frontend/src/metabase/common/utils/database.ts+++ b/frontend/src/metabase/common/utils/database.ts@@ -47,10 +47,6 @@ export const hasActionsEnabled = (database: Pick<Database, "settings">) => {return Boolean(database.settings?.["database-enable-actions"]);};-export const hasWorkspacesEnabled = (database: Pick<Database, "settings">) => {- return Boolean(database.settings?.["database-enable-workspaces"]);-};-export const hasWritableConnectionDetails = (database: Pick<Database, "write_data_details">,) => {diff --git a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/DataStudioLayout.tsx b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/DataStudioLayout.tsxindex d2745708aab..b3d1aaa5f00 100644--- a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/DataStudioLayout.tsx+++ b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/DataStudioLayout.tsx@@ -9,7 +9,6 @@ import { AreaLayout, AreaTab } from "metabase/nav/components/AreaLayout";import {PLUGIN_FEATURE_LEVEL_PERMISSIONS,PLUGIN_REMOTE_SYNC,- PLUGIN_WORKSPACES,} from "metabase/plugins";import { useSelector } from "metabase/redux";import { Outlet } from "metabase/router";@@ -35,9 +34,6 @@ export function DataStudioLayout() {PLUGIN_FEATURE_LEVEL_PERMISSIONS.canAccessDataModel,);const canAccessTransforms = useSelector(canAccessTransformsSelector);- const canManageWorkspaces = useSelector(- PLUGIN_WORKSPACES.canManageWorkspaces,- );const hasDirtyChanges = PLUGIN_REMOTE_SYNC.useHasLibraryDirtyChanges();const hasTransformDirtyChanges =PLUGIN_REMOTE_SYNC.useHasTransformDirtyChanges();@@ -147,15 +143,6 @@ export function DataStudioLayout() {isGated/>)}- {canManageWorkspaces && (- <AreaTab- label={t`Workspaces`}- icon="workspace"- to={Urls.workspaces()}- isSelected={currentTab === "workspaces"}- showLabel={isNavbarOpened}- />- )}{canUseTransforms && (<AreaTablabel={t`Jobs`}diff --git a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/tests/common.unit.spec.tsx b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/tests/common.unit.spec.tsxindex 1333cf3f69d..077981bcf46 100644--- a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/tests/common.unit.spec.tsx+++ b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/tests/common.unit.spec.tsx@@ -38,15 +38,6 @@ describe("DataStudioLayout", () => {});});- describe("workspaces tab", () => {- it("does not render the Workspaces tab on OSS", async () => {- setup({ remoteSyncBranch: "main" });-- expect(await screen.findByTestId("data-studio-nav")).toBeInTheDocument();- expect(screen.queryByLabelText("Workspaces")).not.toBeInTheDocument();- });- });-describe("transforms nav tabs", () => {it("shows Transforms but not Jobs or Runs when setup is incomplete", async () => {setup({ transformsSetupComplete: false, transformsEnabled: false });diff --git a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/tests/enterprise.unit.spec.tsx b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/tests/enterprise.unit.spec.tsxindex f8f5b365208..091504a43e8 100644--- a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/tests/enterprise.unit.spec.tsx+++ b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/tests/enterprise.unit.spec.tsx@@ -2,7 +2,6 @@ import userEvent from "@testing-library/user-event";import fetchMock from "fetch-mock";import { screen, waitFor, within } from "__support__/ui";-import * as Urls from "metabase/urls";import { DEFAULT_EE_SETTINGS, setup } from "./setup";@@ -256,23 +255,4 @@ describe("DataStudioLayout", () => {expect(screen.queryByLabelText("Transforms")).not.toBeInTheDocument();});});-- describe("workspaces tab", () => {- it("admin sees the tab and it links to the workspaces index", async () => {- setup({ ...DEFAULT_EE_SETTINGS, isAdmin: true });-- const tab = await screen.findByLabelText("Workspaces");- expect(tab).toHaveAttribute("href", Urls.workspaces());- });-- it("non-admin does not see the tab", async () => {- setup({- ...DEFAULT_EE_SETTINGS,- isAdmin: false,- });-- expect(await screen.findByTestId("data-studio-nav")).toBeInTheDocument();- expect(screen.queryByLabelText("Workspaces")).not.toBeInTheDocument();- });- });});diff --git a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/tests/setup.tsx b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/tests/setup.tsxindex 5a35064e41c..e5353d80722 100644--- a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/tests/setup.tsx+++ b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/tests/setup.tsx@@ -1,7 +1,6 @@import { setupEnterpriseOnlyPlugin } from "__support__/enterprise";import {setupCollectionsEndpoints,- setupGetCurrentWorkspaceEndpoint,setupLibraryEndpoints,setupPropertiesEndpoints,setupRemoteSyncEndpoints,@@ -14,7 +13,6 @@ import { createMockState } from "metabase/redux/store/mocks";import { Route } from "metabase/router";import type {Collection,- CurrentWorkspace,RemoteSyncEntity,TokenFeatures,} from "metabase-types/api";@@ -166,7 +164,6 @@ interface SetupOpts {remoteSyncBranch?: string | null;isAdmin?: boolean;canAccessTransforms?: boolean;- currentWorkspace?: CurrentWorkspace | null;hasDirtyChanges?: boolean;hasTransformDirtyChanges?: boolean;remoteSyncTransforms?: boolean;@@ -182,7 +179,6 @@ export const setup = ({remoteSyncBranch = null,isAdmin = true,canAccessTransforms = false,- currentWorkspace = null,hasDirtyChanges = false,hasTransformDirtyChanges = false,remoteSyncTransforms = false,@@ -226,7 +222,6 @@ export const setup = ({setupDirtyEndpoints({ dirty, collections });setupNavbarEndpoints(isNavbarOpened);setupLibraryEndpoints(false);- setupGetCurrentWorkspaceEndpoint(currentWorkspace);setupUserKeyValueEndpoints({namespace: "user_acknowledgement",key: "upsell-remote-sync-dev-instance",@@ -263,7 +258,6 @@ export const DEFAULT_EE_SETTINGS: Partial<SetupOpts> = {"remote_sync","dependencies","feature_level_permissions",- "workspaces",],tokenFeatures: {remote_sync: true,@@ -271,6 +265,5 @@ export const DEFAULT_EE_SETTINGS: Partial<SetupOpts> = {library: true,dependencies: true,"schema-viewer": true,- workspaces: true,},};diff --git a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/utils.ts b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/utils.tsindex e18c5bd9e64..2e09b37a4a6 100644--- a/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/utils.ts+++ b/frontend/src/metabase/data-studio/app/pages/DataStudioLayout/utils.ts@@ -10,7 +10,6 @@ type TabName =| "schema-viewer"| "glossary"| "git-sync"- | "workspaces"| "settings";export const getCurrentTab = (pathname: string): TabName => {@@ -19,8 +18,6 @@ export const getCurrentTab = (pathname: string): TabName => {return "glossary";case pathname.startsWith(Urls.dataStudioGitSync()):return "git-sync";- case pathname.startsWith(Urls.workspaces()):- 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.tsxdeleted file mode 100644index 3524115acff..00000000000--- a/frontend/src/metabase/data-studio/app/pages/WorkspacesSectionLayout/WorkspacesSectionLayout.tsx+++ /dev/null@@ -1,16 +0,0 @@-import { t } from "ttag";--import { usePageTitle } from "metabase/hooks/use-page-title";-import { Outlet } from "metabase/router";--import { SectionLayout } from "../../components/SectionLayout";--export function WorkspacesSectionLayout() {- usePageTitle(t`Workspaces`, { titleIndex: 1 });-- return (- <SectionLayout>- <Outlet />- </SectionLayout>- );-}diff --git a/frontend/src/metabase/data-studio/app/pages/WorkspacesSectionLayout/index.ts b/frontend/src/metabase/data-studio/app/pages/WorkspacesSectionLayout/index.tsdeleted file mode 100644index 85d3fb9745d..00000000000--- a/frontend/src/metabase/data-studio/app/pages/WorkspacesSectionLayout/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from "./WorkspacesSectionLayout";diff --git a/frontend/src/metabase/data-studio/routes.tsx b/frontend/src/metabase/data-studio/routes.tsxindex be8a086552c..1d47f33586b 100644--- a/frontend/src/metabase/data-studio/routes.tsx+++ b/frontend/src/metabase/data-studio/routes.tsx@@ -4,7 +4,6 @@ import {PLUGIN_FEATURE_LEVEL_PERMISSIONS,PLUGIN_LIBRARY,PLUGIN_SCHEMA_VIEWER,- PLUGIN_WORKSPACES,} from "metabase/plugins";import { useSelector } from "metabase/redux";import type { State } from "metabase/redux/store";@@ -23,7 +22,6 @@ import { DataStudioLayout } from "./app/pages/DataStudioLayout";import { DependenciesSectionLayout } from "./app/pages/DependenciesSectionLayout";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 { CanAccessDataModel, CanAccessDataStudio } from "./route-guards";@@ -56,9 +54,6 @@ export function getDataStudioRoutes(IsAdmin: RouteComponent) {<Route path="transforms" element={<TransformsSectionLayout />}>{getDataStudioTransformRoutes()}</Route>- <Route element={<WorkspacesSectionLayout />}>- {PLUGIN_WORKSPACES.getDataStudioRoutes()}- </Route>{getDataStudioGlossaryRoutes()}{getDataStudioSettingsRoutes()}{PLUGIN_LIBRARY.isEnabled ? (diff --git a/frontend/src/metabase/plugins/index.ts b/frontend/src/metabase/plugins/index.tsindex 26208fe26f3..b8eb096fbbc 100644--- a/frontend/src/metabase/plugins/index.ts+++ b/frontend/src/metabase/plugins/index.ts@@ -176,10 +176,6 @@ export {PLUGIN_WRITABLE_CONNECTION,type WritableConnectionInfoSectionProps,} from "./oss/writable-connection";-export {- PLUGIN_WORKSPACES,- type WorkspaceDatabaseSectionProps,-} 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";@@ -233,7 +229,6 @@ 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.@@ -281,6 +276,5 @@ export function reinitialize() {reinitializeTransforms();reinitializeUploadManagement();reinitializeWhitelabel();- reinitializeWorkspaces();reinitializeWritableConnection();}diff --git a/frontend/src/metabase/plugins/oss/workspaces.ts b/frontend/src/metabase/plugins/oss/workspaces.tsdeleted file mode 100644index d52fbaecc8b..00000000000--- a/frontend/src/metabase/plugins/oss/workspaces.ts+++ /dev/null@@ -1,27 +0,0 @@-import type { ReactNode } from "react";--import type { State } from "metabase/redux/store";-import type { RouteComponent } from "metabase/router";-import type { Database } from "metabase-types/api";--import { PluginPlaceholder } from "../components/PluginPlaceholder";--export type WorkspaceDatabaseSectionProps = {- database: Database;-};--const getDefaultWorkspaces = () => ({- canManageWorkspaces: (_state: State): boolean => false,- getDataStudioRoutes: (): ReactNode => null,- getWorkspaceDatabaseRoutes: (_IsAdmin: RouteComponent): ReactNode => null,- WorkspaceDatabaseSection: PluginPlaceholder<WorkspaceDatabaseSectionProps>,-});--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/ui/components/icons/Icon/icons/index.ts b/frontend/src/metabase/ui/components/icons/Icon/icons/index.tsindex 19fc22abc14..d273725d13f 100644--- a/frontend/src/metabase/ui/components/icons/Icon/icons/index.ts+++ b/frontend/src/metabase/ui/components/icons/Icon/icons/index.ts@@ -596,8 +596,6 @@ import waterfall_component from "./waterfall.svg?component";import waterfall_source from "./waterfall.svg?source";import webhook_component from "./webhook.svg?component";import webhook_source from "./webhook.svg?source";-import workspace_component from "./workspace.svg?component";-import workspace_source from "./workspace.svg?source";import zap_component from "./zap.svg?component";import zap_source from "./zap.svg?source";import zoom_in_component from "./zoom_in.svg?component";@@ -1785,10 +1783,6 @@ export const Icons: Record<IconName, { component: React.VFC; source: string }> =component: webhook_component,source: webhook_source,},- workspace: {- component: workspace_component,- source: workspace_source,- },"10k": {component: ten_thousand_component,source: ten_thousand_source,diff --git a/frontend/src/metabase/ui/components/icons/Icon/icons/workspace.svg b/frontend/src/metabase/ui/components/icons/Icon/icons/workspace.svgdeleted file mode 100644index 550c0030291..00000000000--- a/frontend/src/metabase/ui/components/icons/Icon/icons/workspace.svg+++ /dev/null@@ -1 +0,0 @@-<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none" viewBox="0 0 16 16"><g clip-path="url(#a)"><path fill="currentColor" d="M9.366.717c.275 0 .546.063.792.182l.105.055.021.013 4.2 2.6.05.034c.22.166.447.428.603.744q.02.026.04.054.069.122.085.253.07.23.071.482v4.333c0 .324-.121.642-.268.89-.149.252-.37.511-.656.682l-.007.004-6.867 4-.006.004a1.82 1.82 0 0 1-1.813-.013l-4.2-2.6-.05-.034a2.1 2.1 0 0 1-.552-.664c-.128-.235-.248-.548-.248-.869v-4.4q0-.245.073-.482a.7.7 0 0 1 .095-.27q.02-.03.042-.056c.169-.312.43-.572.748-.718L8.464.958 8.47.954c.274-.155.582-.237.896-.237M2 10.867a.3.3 0 0 0 .016.07 1 1 0 0 0 .069.16.9.9 0 0 0 .173.228L6 13.641V9.703L2 7.222zm5.334-1.15v3.9l1.332-.777V8.944zM10 8.17v3.892l1.333-.777V7.397zm2.667-1.547v3.886l1.063-.62a.7.7 0 0 0 .187-.212.8.8 0 0 0 .068-.147c.013-.04.015-.062.015-.064V5.852zm-3.3-4.573a.5.5 0 0 0-.238.062h-.001l-6.62 3.856 4.17 2.587 6.885-3.993L9.599 2.11a.5.5 0 0 0-.233-.06"/></g><defs><clipPath id="a"><path fill="#fff" d="M0 0h16v16H0z"/></clipPath></defs></svg>diff --git a/frontend/src/metabase/urls/index.ts b/frontend/src/metabase/urls/index.tsindex ce571185179..34903d5c842 100644--- a/frontend/src/metabase/urls/index.ts+++ b/frontend/src/metabase/urls/index.ts@@ -22,4 +22,3 @@ 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.tsdeleted file mode 100644index 42e2762ce9e..00000000000--- a/frontend/src/metabase/urls/workspaces.ts+++ /dev/null@@ -1,5 +0,0 @@-const WORKSPACES_ROOT_URL = `/data-studio/workspaces`;--export function workspaces() {- return WORKSPACES_ROOT_URL;-}diff --git a/frontend/test/__support__/enterprise-typed.ts b/frontend/test/__support__/enterprise-typed.tsindex a2c05a1384c..12bd09ffb55 100644--- a/frontend/test/__support__/enterprise-typed.ts+++ b/frontend/test/__support__/enterprise-typed.ts@@ -53,7 +53,6 @@ export const ENTERPRISE_PLUGIN_NAME_MAPPING = {content_verification: "metabase-enterprise/content_verification",moderation: "metabase-enterprise/moderation",tenants: "metabase-enterprise/tenants",- workspaces: "metabase-enterprise/workspaces",// Embedding SDK specific pluginssdk_notifications: "embedding-sdk-ee/notifications",diff --git a/frontend/test/__support__/server-mocks/index.ts b/frontend/test/__support__/server-mocks/index.tsindex 4877b0d5cc9..40b04d93c62 100644--- a/frontend/test/__support__/server-mocks/index.ts+++ b/frontend/test/__support__/server-mocks/index.ts@@ -62,6 +62,4 @@ 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.tsdeleted file mode 100644index 69a00dae6da..00000000000--- a/frontend/test/__support__/server-mocks/workspace-instance.ts+++ /dev/null@@ -1,19 +0,0 @@-import fetchMock from "fetch-mock";--import type { CurrentWorkspace, TableRemapping } from "metabase-types/api";--const BASE_URL = "path:/api/ee/workspace-instance";--export function setupGetCurrentWorkspaceEndpoint(- workspace: CurrentWorkspace | null,-) {- fetchMock.get(`${BASE_URL}/current`, { data: workspace });-}--export function setupDeleteCurrentWorkspaceEndpoint() {- fetchMock.delete(`${BASE_URL}/current`, 204);-}--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.tsdeleted file mode 100644index 86b3c713937..00000000000--- a/frontend/test/__support__/server-mocks/workspace-manager.ts+++ /dev/null@@ -1,29 +0,0 @@-import fetchMock from "fetch-mock";--import type { 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}`, { status: 204 });-}--export function setupDeleteWorkspaceEndpointError(workspaceId: WorkspaceId) {- fetchMock.delete(`${BASE_URL}/${workspaceId}`, { status: 500 });-}diff --git a/mage/src/mage/modules.clj b/mage/src/mage/modules.cljindex ce76fab3ab6..808460cc033 100644--- a/mage/src/mage/modules.clj+++ b/mage/src/mage/modules.clj@@ -20,7 +20,7 @@(def modules-triggering-cloud-drivers"Modules not only trigger driver tests, but run cloud drivers as well. Can be duplicative to driver triggers."'#{query-processor transforms- enterprise/transforms enterprise/transforms-python enterprise/workspaces})+ enterprise/transforms enterprise/transforms-python});;; TODO (Cam 2025-11-07) changes to test files should only cause us to run tests for that module as well, not;;; everything that depends on that module directly or indirectly in `src`@@ -158,8 +158,7 @@utilversionview-log- warehouse-schema- workspaces})+ warehouse-schema})(defn- affected-modules"Set of modules that are direct or indirect dependents of `modules`, and thus are affected by changes to them.diff --git a/mage/test/mage/modules_test.clj b/mage/test/mage/modules_test.cljindex 99ddf40e759..ecdc927402c 100644--- a/mage/test/mage/modules_test.clj+++ b/mage/test/mage/modules_test.clj@@ -253,7 +253,7 @@(deftest modules-can-trigger-cloud-drivers(doseq [module '#{query-processor transforms- enterprise/transforms enterprise/transforms-python enterprise/workspaces}+ enterprise/transforms enterprise/transforms-python}driver [:athena :bigquery :databricks :redshift :snowflake]](testing (format "Cloud driver runs when %s module is updated" module)(let [result (mage.modules/driver-decision driver@@ -378,8 +378,8 @@(testing "if you change a test in a module, that module is affected";; note in the future, this won't be all dependent modules see;; https://linear.app/metabase/issue/DEV-1487/treat-changed-test-namespaces-as-module-only-changes- (let [changed-file "enterprise/backend/test/metabase_enterprise/workspaces/api_test.clj"]- (is (= '#{enterprise/workspaces}+ (let [changed-file "enterprise/backend/test/metabase_enterprise/transforms_python/api_test.clj"]+ (is (= '#{enterprise/transforms-python}(mage.modules/updated-files->updated-modules [changed-file])))(is (-> [changed-file]mage.modules/updated-files->updated-modulesdiff --git a/modules/drivers/bigquery-cloud-sdk/deps.edn b/modules/drivers/bigquery-cloud-sdk/deps.ednindex 151d4e9827e..a03ffe5d871 100644--- a/modules/drivers/bigquery-cloud-sdk/deps.edn+++ b/modules/drivers/bigquery-cloud-sdk/deps.edn@@ -10,13 +10,6 @@;; netty is updated because of CVEio.netty/netty-commonio.netty/netty-buffer]}- ;; Required for service account impersonation in workspace isolation- ;; Version matches what google-cloud-bigquery depends on- com.google.auth/google-auth-library-oauth2-http {:mvn/version "1.40.0"}- ;; Required for granting project-level IAM roles to workspace service accounts- com.google.cloud/google-cloud-resourcemanager {:mvn/version "1.82.0"}- ;; Required for creating service accounts for workspace isolation- com.google.cloud/google-iam-admin {:mvn/version "3.54.0"};; pin arrow formats to 17 for cve about parquet filesorg.apache.arrow/arrow-format {:mvn/version "18.3.0"}org.apache.arrow/arrow-memory-core {:mvn/version "18.3.0"}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 c518638c84c..0e047269ef9 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,10 +10,6 @@[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!`,- ;; `destroy-workspace-isolation!`).- [metabase.driver.bigquery-cloud-sdk.workspaces][metabase.driver.common :as driver.common][metabase.driver.common.table-rows-sample :as table-rows-sample][metabase.driver.connection :as driver.conn]@@ -36,7 +32,6 @@(:import(clojure.lang PersistentList)(com.google.api.gax.rpc FixedHeaderProvider)- (com.google.auth.oauth2 ImpersonatedCredentials)(com.google.cloud.bigqueryBigQueryBigQuery$DatasetListOption@@ -89,21 +84,7 @@(mu/defn- database-details->client^BigQuery [details :- :map](let [base-creds (bigquery.common/database-details->service-account-credential details)- ;; Check if we should impersonate a different service account- ;; 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.- 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)- target-sa- nil ;; delegates (not needed)- (java.util.ArrayList. bigquery-scopes)- 3600)) ;; 1 hour token lifetime- (.createScoped base-creds bigquery-scopes))+ creds (.createScoped base-creds bigquery-scopes)mb-version (:tag driver-api/mb-version-info)run-mode (name driver-api/run-mode)user-agent (format "Metabase/%s (GPN:Metabase; %s)" mb-version run-mode)@@ -114,19 +95,9 @@(.setReadTimeout read-timeout-ms)(.build))bq-bldr (doto (BigQueryOptions/newBuilder)- (.setCredentials final-creds)+ (.setCredentials 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)))@@ -1029,10 +1000,7 @@;; statements by using a different driver-native API for affected-row counts.:transforms/accurate-rows-affected false:transforms/python true- :transforms/table true- ;; Workspace isolation using service account impersonation- ;; Tearing down workspaces is not working right currently- :workspace false}]+ :transforms/table true}](defmethod driver/database-supports? [:bigquery-cloud-sdk feature] [_driver _feature _db] supported?))(defmethod driver/qualified-name-components :bigquery-cloud-sdk@@ -1329,8 +1297,6 @@(defmethod driver/create-schema-if-needed! :bigquery-cloud-sdk[driver conn-spec schema];; Check if dataset exists using the BigQuery API before trying to create.- ;; 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 detailsproject-id (bigquery.common/get-project-id conn-spec)dataset-id (DatasetId/of project-id schema)]@@ -1353,15 +1319,5 @@;; https://cloud.google.com/bigquery/docs/tables1024)-;;; +----------------------------------------------------------------------------------------------------------------+-;;; | Workspace Isolation |-;;; +----------------------------------------------------------------------------------------------------------------+-;;; BigQuery workspace isolation uses service account impersonation instead of SQL GRANT statements.-;;; Each workspace gets its own service account (created automatically) with table-level read permissions.-;;;-;;; Required GCP setup for the main service account:-;;; - Roles: `roles/bigquery.admin`, `roles/iam.serviceAccountAdmin`, `roles/resourcemanager.projectIamAdmin`-;;; - APIs: `bigquery.googleapis.com`, `iam.googleapis.com`, `cloudresourcemanager.googleapis.com`-(defmethod driver/llm-sql-dialect-resource :bigquery-cloud-sdk [_]"metabot/prompts/dialects/bigquery.md")diff --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.cljdeleted file mode 100644index 8078d7d9cb0..00000000000--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk/workspaces.clj+++ /dev/null@@ -1,648 +0,0 @@-(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,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.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)])))))--(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- ws-service-account-email- "The email that identifies a workspace's service account. The single source of- the SA identity — create, delete, and the persisted `:database_details` must- all agree on it."- [workspace ^String project-id]- (format "%s@%s.iam.gserviceaccount.com" (ws-service-account-id workspace) project-id))--(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-email (ws-service-account-email workspace 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 (ws-service-account-email workspace 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 (:schema 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/workspace-isolation-details :bigquery-cloud-sdk- [_driver database workspace]- (let [details (driver.conn/effective-details database)- project-id (bigquery.common/get-project-id details)- sa-email (ws-service-account-email workspace project-id)]- {:schema (driver.u/workspace-isolation-namespace-name workspace)- :database_details {:user sa-email- :impersonate-service-account sa-email}}))--(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 (:schema 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")- nil)- (finally- (.close iam-client)))))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.cljdeleted file mode 100644index d58fc1abf6b..00000000000--- a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/workspace_isolation_test.clj+++ /dev/null@@ -1,463 +0,0 @@-(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.--(comment- ;; "the "1500 account limit" message is because of the workspace bug - when- ;; deprovisioning, we don't properly tear down the workspace for bigquery"- ;; need to fix the underlying deprovisioning problem for this-- (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-with-details (merge workspace- (driver/workspace-isolation-details :bigquery-cloud-sdk database 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)))- (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-with-details)- (let [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-with-details)- (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)}- ;; Details are pure computation, so both full workspace maps exist- ;; before init — the `finally` destroys always have them.- ws-a-full (merge ws-a (driver/workspace-isolation-details :bigquery-cloud-sdk database ws-a))- ws-b-full (merge ws-b (driver/workspace-isolation-details :bigquery-cloud-sdk database 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)))- (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-a-full)- (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-b-full)- (let [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-full ws-b-full]]- (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)}- ;; Details are pure computation, so the full workspace map exists- ;; before init — the `finally` destroy always has it.- ws-with-details (merge workspace- (driver/workspace-isolation-details :bigquery-cloud-sdk database 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)))- (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-with-details)- (let [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-with-details)- (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)- ;; Details are pure computation, so the full workspace map exists- ;; before init — the `finally` destroy always has it.- ws-with-details (merge workspace- (driver/workspace-isolation-details :bigquery-cloud-sdk database 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- (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)- (driver/init-workspace-isolation! :bigquery-cloud-sdk database ws-with-details)- (let [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 "isolation details target exactly the pre-existing dataset name"- ;; init returns nil, so reaching this point without a throw is the- ;; collision-tolerance signal; this pins that the computed details- ;; land on the deterministic (colliding) dataset name.- (is (= out-dataset (:schema ws-with-details))))- (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-with-details)- (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.cljdeleted file mode 100644index 7dbade38384..00000000000--- a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk/workspace_test_util.clj+++ /dev/null@@ -1,226 +0,0 @@-(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 c98403039b5..1792a7a48af 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,7 +11,6 @@[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]@@ -1524,28 +1523,6 @@(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")))))-(deftest ^:parallel bigquery-field-filter-alias-test(mt/test-driver :bigquery-cloud-sdk(let [mp (mt/metadata-provider)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 f1a0086fc56..d7bbeb12d21 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@@ -6,7 +6,6 @@[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]@@ -365,113 +364,14 @@(log/info (u/format-color 'blue "Deleting temporary dataset: %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-ws-..." ...)-;;;-;;; 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-ws-*@...`) 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-ws-")- 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!]] (+ workspace-iso sweep), only if we haven't- done so already."+ "Call [[delete-old-datasets!]], only if we haven't done so already."[](when (compare-and-set! deleted-old-datasets? false true)- (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))))))+ (delete-old-datasets!)))(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 caa8344085d..ee829ad0478 100644--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj@@ -10,7 +10,6 @@[metabase.driver.clickhouse-qp][metabase.driver.clickhouse-version :as clickhouse-version][metabase.driver.common :as driver.common]- [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]@@ -19,14 +18,13 @@[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute][metabase.driver.sql.query-processor :as sql.qp][metabase.driver.sql.util :as sql.u]- [metabase.driver.util :as driver.u][metabase.util :as u]- [metabase.util.i18n :refer [deferred-tru tru]]+ [metabase.util.i18n :refer [deferred-tru]][metabase.util.log :as log][metabase.util.performance :as perf])(:import(com.clickhouse.client.api.query QuerySettings)- (java.sql Connection SQLException Statement PreparedStatement)+ (java.sql Connection SQLException PreparedStatement)(java.time LocalDate)))(set! *warn-on-reflection* true)@@ -37,7 +35,6 @@(defmethod driver/display-name :clickhouse [_] "ClickHouse")(defn- quote-schema [s] (sql.u/quote-name :clickhouse :schema s))-(defn- quote-field [s] (sql.u/quote-name :clickhouse :field s))(defmethod driver/prettify-native-form :clickhouse[_ native-form]@@ -51,7 +48,6 @@;; JDBC driver always provides "NO" for the IS_GENERATEDCOLUMN JDBC metadata:describe-is-generated false:describe-is-nullable true- :workspace true:expression-literals true:expressions/date true:expressions/float true@@ -544,96 +540,5 @@[_ ^PreparedStatement prepared-statement i object](.setObject prepared-statement i object))-;;; ------------------------------------------ Workspace Isolation --------------------------------------------(defmethod driver/init-workspace-isolation! :clickhouse- [driver database workspace]- (let [db-name (:schema workspace)- canonical-db (:db (driver.conn/effective-details database))- read-user (:database_details workspace)- escaped-password (sql.u/escape-sql (:password read-user) :ansi)- quoted-db (quote-schema db-name)- quoted-user (quote-field (:user read-user))- quoted-canonical-db (when-not (str/blank? canonical-db)- (quote-schema canonical-db))]- ;; No transaction: ClickHouse has no transactional DDL — CREATE/GRANT apply- ;; immediately. Failure recovery is compensation via the idempotent destroy.- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [stmt (.createStatement conn)]- (doseq [sql (cond-> [(format "CREATE DATABASE IF NOT EXISTS %s" quoted-db)- (format "CREATE USER IF NOT EXISTS %s IDENTIFIED BY '%s'"- quoted-user escaped-password)- ;; the user may survive a failed teardown; without this it would keep- ;; its old password while the new one gets persisted- (format "ALTER USER %s IDENTIFIED BY '%s'"- quoted-user escaped-password)- ;; Least-privilege grant on the workspace's own DB (ClickHouse has no- ;; owner auto-privileges, so grant each verb explicitly):- ;; SELECT - read its own tables- ;; INSERT - CTAS populate + incremental insert- ;; CREATE TABLE - transform target- ;; DROP TABLE - swap/cleanup- ;; (these four also satisfy the atomic-swap RENAME TABLE.)- (format "GRANT SELECT, INSERT, CREATE TABLE, DROP TABLE ON %s.* TO %s"- quoted-db quoted-user)]- quoted-canonical-db- (conj (format "GRANT SHOW DATABASES ON %s.* TO %s"- quoted-canonical-db quoted-user)))]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/scrub-exceptions (driver.u/batch-exception t) [(:password read-user) escaped-password])))))))- nil))--(defmethod driver/grant-workspace-read-access! :clickhouse- [driver database workspace schemas]- (let [read-user-name (-> workspace :database_details :user)- quoted-user (quote-field read-user-name)]- (when-not read-user-name- (throw (ex-info (tru "Cannot grant workspace read access. Workspace details have no read user — initialization may have failed. Re-run workspace initialization and retry.")- {:workspace-id (:id workspace) :step :grant})))- ;; 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 "Cannot grant workspace read access. Input schema name is blank. Remove the blank entry from the workspace input schemas and retry.")- {:database-id (:id database) :step :grant})))]]- (format "GRANT SELECT ON %s.* TO %s"- (quote-schema schema)- quoted-user))]- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [stmt (.createStatement conn)]- (doseq [sql sqls]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t))))))))))--(defmethod driver/destroy-workspace-isolation! :clickhouse- [driver database workspace]- (let [db-name (:schema workspace)- username (-> workspace :database_details :user)- quoted-db (quote-schema db-name)- quoted-user (quote-field username)]- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [stmt (.createStatement conn)]- (doseq [sql [;; DROP DATABASE cascades to all tables within it- (format "DROP DATABASE IF EXISTS %s" quoted-db)- (format "DROP USER IF EXISTS %s" quoted-user)]]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t)))))))))-(defmethod driver/llm-sql-dialect-resource :clickhouse [_]"metabot/prompts/dialects/clickhouse.md")diff --git a/modules/drivers/mongo/src/metabase/driver/mongo.clj b/modules/drivers/mongo/src/metabase/driver/mongo.cljindex 99ecf158cfd..d3d3ae10131 100644--- a/modules/drivers/mongo/src/metabase/driver/mongo.clj+++ b/modules/drivers/mongo/src/metabase/driver/mongo.clj@@ -464,8 +464,7 @@:index-info false:python-transforms true:transforms/python true- :database-routing true- :workspace false}]+ :database-routing true}](defmethod driver/database-supports? [:mongo feature] [_driver _feature _db] supported?))(defmethod driver/database-supports? [:mongo :schemas] [_driver _feat _db] false)diff --git a/modules/drivers/redshift/src/metabase/driver/redshift.clj b/modules/drivers/redshift/src/metabase/driver/redshift.cljindex 4664ac1b465..5e507293515 100644--- a/modules/drivers/redshift/src/metabase/driver/redshift.clj+++ b/modules/drivers/redshift/src/metabase/driver/redshift.clj@@ -20,7 +20,6 @@[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]@@ -36,7 +35,6 @@PreparedStatementResultSetResultSetMetaData- StatementTypes)))(set! *warn-on-reflection* true)@@ -73,8 +71,7 @@:transforms/python true:transforms/table true:transforms/index-ddl false- :uuid-type false- :workspace true}]+ :uuid-type false}](defmethod driver/database-supports? [:redshift feature] [_driver _feat _db] supported?))(defmethod driver/qualified-name-components :redshift@@ -866,334 +863,5 @@;; 42P01: undefined_table, 3F000: invalid_schema_name(contains? #{"42P01" "3F000"} (sql-jdbc/get-sql-state e)))-;;; +----------------------------------------------------------------------------------------------------------------+-;;; | Workspace Isolation |-;;; +----------------------------------------------------------------------------------------------------------------+--;; 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- quote-schema [s] (sql.u/quote-name :redshift :schema s))-(defn- quote-field [s] (sql.u/quote-name :redshift :field s))--(defn- assert-no-public-create-grant!- [conn schema-name]- (when (public-create-grant? conn schema-name)- (driver.postgres/raise-public-create-grant! schema-name)))--(defn- current-user-usesuper?- "True when `current_user` has `usesuper`. Superusers can ALTER DEFAULT- PRIVILEGES FOR USER <anyone>, so they bypass the membership check below.-- Redshift's user-membership model differs from PostgreSQL's: there is no- `pg_has_role(name, name, text)` overload (the function doesn't exist on- Redshift at all on RA3 clusters), and `pg_group` is not generally populated.- The practical membership graph collapses to: you are `current_user`, or you- are a superuser. Anything else fails at REVOKE time."- [conn]- (boolean (:usesuper (first (jdbc/query conn ["SELECT usesuper FROM pg_user WHERE usename = current_user"])))))--(defn- relation-owners-in-schema- "Distinct relation owners in `schema-name`. Caller filters out the ones- `current_user` can impersonate.-- Why this matters: `ALTER DEFAULT PRIVILEGES IN SCHEMA <s>` (without- `FOR ROLE`) only affects future objects created by the connection user.- Tables created later by foreign owners skip the iso-user grant -- workspace- data goes stale silently.-- `relkind` set matches the scope of `ALTER DEFAULT PRIVILEGES ... ON TABLES`:- ordinary tables (`r`), views (`v`), materialized views (`m`), partitioned- tables (`p`), and foreign tables (`f`). Spectrum external tables live in- `svv_external_tables` and are not covered by `pg_default_acl`, so they need- no entry here."- [conn schema-name]- (->> (jdbc/query conn- [(str "SELECT DISTINCT pg_get_userbyid(c.relowner) AS owner "- "FROM pg_class c "- "JOIN pg_namespace n ON n.oid = c.relnamespace "- "WHERE n.nspname = ? "- " AND c.relkind IN ('r','v','m','p','f') "- "ORDER BY owner")- schema-name])- (keep :owner)))--(defn- default-acl-grantors-in-schema- "Distinct grantors of pre-existing `pg_default_acl` entries in `schema-name`.- Caller filters out the ones `current_user` can impersonate.-- Distinct from [[relation-owners-in-schema]]: that surfaces future-object- grantor risk; this surfaces pre-existing default-priv rows we will need to- REVOKE FOR USER <grantor> at destroy time. Both block the workspace- contract."- [conn schema-name]- (->> (jdbc/query conn- [(str "SELECT DISTINCT u.usename AS owner "- "FROM pg_catalog.pg_default_acl d "- "JOIN pg_catalog.pg_user u ON u.usesysid = d.defacluser "- "JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace "- "WHERE n.nspname = ? "- " AND d.defaclobjtype = 'r' "- "ORDER BY owner")- schema-name])- (keep :owner)))--(defn assert-can-alter-default-privileges!- "Throws when `schema-name` has relation-owners or pre-existing default-priv- grantors that `current_user` cannot impersonate via `FOR USER`. On Redshift- the impersonation graph collapses to: `owner == current_user`, or- `current_user` is a superuser.-- Without this guarantee we can neither extend defaults to future objects of- foreign owners (silent data drift) nor REVOKE pre-existing default-priv- entries at destroy time (DROP USER fails -> GHY-3709)."- [conn schema-name]- (when-not (current-user-usesuper? conn)- (let [me (:me (first (jdbc/query conn ["SELECT current_user AS me"])))- owners (->> (concat (relation-owners-in-schema conn schema-name)- (default-acl-grantors-in-schema conn schema-name))- distinct- (remove #(= % me))- (map (fn [o] {:owner o})))]- (when (seq owners)- (driver.postgres/raise-unmemberable-default-priv-owners! schema-name owners)))))--(defmethod driver/init-workspace-isolation! :redshift- [_driver database workspace]- (let [schema-name (:schema workspace)- read-user (:database_details workspace)- escaped-password (sql.u/escape-sql (:password read-user) :ansi)- quoted-schema (quote-schema schema-name)- quoted-user (quote-field (:user read-user))]- (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'" quoted-user escaped-password)- (format "CREATE USER %s WITH PASSWORD '%s'" quoted-user escaped-password))]- (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]- ;; Schema-level grant only (Redshift's two schema privileges):- ;; USAGE - access the schema- ;; CREATE - create tables in it- ;; Table DML comes from ownership (the user owns the tables it creates), so we skip- ;; `ALTER DEFAULT PRIVILEGES ... GRANT ALL ON TABLES` (that only covers tables created- ;; by the admin connection, which never happens on the transform path).- (doseq [sql [(format "CREATE SCHEMA IF NOT EXISTS %s" quoted-schema)- user-sql- (format "GRANT USAGE, CREATE ON SCHEMA %s TO %s" quoted-schema quoted-user)]]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/scrub-exceptions (driver.u/batch-exception t) [(:password read-user) escaped-password])))))))- nil))--(defmethod driver/grant-workspace-read-access! :redshift- [driver database workspace schemas]- (let [username (-> workspace :database_details :user)- quoted-user (quote-field username)- source-schemas (set schemas)- spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]- ;; Pre-flight check (read-only, one connection, no transaction). Redshift's- ;; GRANT statements error loudly when grant authority is missing, so PG's- ;; silent-skip USAGE/SELECT class doesn't reproduce here. But two ALTER- ;; DEFAULT PRIVILEGES failure modes do reproduce and need explicit checks:- ;;- ;; - Foreign relation-owners: tables created later by an owner the- ;; connection user can't impersonate skip our default-priv grant.- ;; - Foreign default-priv grantors: pre-existing `pg_default_acl` rows whose- ;; grantor we can't impersonate at destroy time -> `DROP USER` fails- ;; (GHY-3709).- (sql-jdbc.execute/do-with-connection-with-options- driver database nil- (fn [^Connection conn]- (let [check-conn {:connection conn}]- (doseq [s source-schemas]- (assert-no-public-create-grant! check-conn s)- (assert-can-alter-default-privileges! check-conn s)))))- ;; Grants run as one batched transaction; it commits before this fn- ;; returns, so a subsequent describe-database from a different connection- ;; still observes the privileges.- (jdbc/with-db-transaction [t-conn spec]- (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]- (doseq [s source-schemas- :let [quoted-schema (quote-schema s)]- sql [(format "GRANT USAGE ON SCHEMA %s TO %s" quoted-schema quoted-user)- (format "REVOKE CREATE ON SCHEMA %s FROM %s" quoted-schema quoted-user)- (format "REVOKE INSERT, UPDATE, DELETE, REFERENCES ON ALL TABLES IN SCHEMA %s FROM %s"- quoted-schema quoted-user)- ;; 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" quoted-schema quoted-user)- (format "ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT SELECT ON TABLES TO %s"- quoted-schema quoted-user)]]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t))))))))--(defn- schema-exists?- "Check if a schema exists in Redshift."- [conn schema-name]- (seq (jdbc/query conn ["SELECT 1 FROM pg_namespace WHERE nspname = ?" schema-name])))--(defn- schemas-with-user-grants- "Query Redshift to find schemas where the user has been granted schema-level- (USAGE/CREATE) or relation-level privileges. Both views are needed:- `svv_relation_privileges` only surfaces GRANTs on existing relations, so a- granted schema with no tables (or whose tables were dropped later) only- shows up in `svv_schema_privileges` — missing it leaves a USAGE grant behind- and `DROP USER` then fails with `user ... has a privilege on some object`.- Neither view lists ALTER DEFAULT PRIVILEGES entries; see- [[default-acl-grants-for-user]] for those."- [conn username]- (->> (jdbc/query conn- ["SELECT DISTINCT namespace_name FROM svv_relation_privileges- WHERE identity_name = ? AND identity_type = 'user'- UNION- SELECT DISTINCT namespace_name FROM svv_schema_privileges- WHERE identity_name = ? AND identity_type = 'user'"- username username])- (keep :namespace_name)))--(defn- escape-like-pattern- "Escape PostgreSQL/Redshift LIKE metacharacters (`%`, `_`) and our chosen- escape char (`|`) in `s` so it matches literally. Used with `ESCAPE '|'` on- the LIKE clause -- `|` chosen over `\\` because Redshift's SQL parser treats- a literal `'\\'` as an unterminated string. Without this escaping, iso-user- names containing `_` (which they always do -- the generator produces- `mb__isolation_<id>` form) would match siblings via wildcard, and the- destroy could revoke default-priv rows belonging to unrelated users."- [^String s]- (-> s- (str/replace "|" "||")- (str/replace "%" "|%")- (str/replace "_" "|_")))--(defn- default-acl-grants-for-user- "Enumerate `(grantor, schema)` pairs that have an ALTER DEFAULT PRIVILEGES entry referencing- `username` as a grantee. Returns a seq of `{:grantor :schema}` maps.-- Redshift exposes `pg_default_acl` but rejects `aclitem`->`varchar` casts and does not- provide `aclexplode`. We use `array_to_string(defaclacl, ',')` (allowed on aclitem[]).- Verified live: Redshift's `array_to_string` strips the enclosing braces of an `aclitem[]`,- so each entry is comma-separated text of the form `grantee=privs/grantor`. To make the- grantee match unambiguous, we prepend a `,` to the haystack and search for `,<name>=` --- that way a row with only one grantee (no preceding entries) still matches.-- LIKE metacharacters in `username` are escaped with `ESCAPE '|'` so `_` and `%` match- literally -- critical because the iso-user generator always produces `_`-containing- names. Pipe over backslash because Redshift's parser rejects a literal backslash escape- clause as an unterminated string.-- Schema-less entries (`defaclnamespace = 0`, applies to any schema) are excluded -- our- provisioning only ever issues `ALTER DEFAULT PRIVILEGES IN SCHEMA`, never the no-schema- form, so those entries do not originate here."- [conn username]- (let [esc (escape-like-pattern username)]- (jdbc/query- conn- [(str "SELECT u.usename AS grantor, "- " n.nspname AS schema "- "FROM pg_catalog.pg_default_acl d "- "JOIN pg_catalog.pg_user u ON u.usesysid = d.defacluser "- "JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace "- "WHERE d.defaclobjtype = 'r' "- " AND (',' || array_to_string(d.defaclacl, ',')) LIKE ? ESCAPE '|'")- (str "%," esc "=%")])))--(defmethod driver/destroy-workspace-isolation! :redshift- [_driver database workspace]- (let [schema-name (:schema workspace)- username (-> workspace :database_details :user)- quoted-user (quote-field username)- quoted-schema (quote-schema schema-name)- spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]- ;; One transactional batch: foreign-grantor default-priv revokes, relation- ;; revokes, iso-schema bare REVOKE, DROP SCHEMA, DROP USER. Redshift has no- ;; SAVEPOINT and aborts the whole transaction on the first error, so a- ;; stale row (e.g. a schema dropped between discovery and execution) fails- ;; the entire destroy — that's fine: the workspace layer records the- ;; failure and the deprovision is retried, re-discovering fresh state.- (jdbc/with-db-transaction [t-conn spec]- (let [user-exists (user-exists? t-conn username)- schema-exists (schema-exists? t-conn schema-name)- granted-schemas (when user-exists- (schemas-with-user-grants t-conn username))]- (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]- (when user-exists- ;; Foreign-grantor default-priv rows must be revoked FOR USER their- ;; grantor before DROP USER (GHY-3709); Redshift has no DROP OWNED BY.- (doseq [{:keys [grantor schema]} (default-acl-grants-for-user t-conn username)]- (.addBatch ^Statement stmt- ^String (format "ALTER DEFAULT PRIVILEGES FOR USER %s IN SCHEMA %s REVOKE ALL ON TABLES FROM %s"- (quote-field grantor) (quote-schema schema) quoted-user)))- (doseq [schema granted-schemas- :let [quoted-granted-schema (quote-schema schema)]]- (.addBatch ^Statement stmt- ^String (format "REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA %s FROM %s"- quoted-granted-schema quoted-user))- (.addBatch ^Statement stmt- ^String (format "REVOKE ALL PRIVILEGES ON SCHEMA %s FROM %s"- quoted-granted-schema quoted-user)))- ;; Iso-namespace default-priv was issued by the connection user at init time, so a- ;; no-FOR-USER REVOKE is correct here. Guarded on schema existence to avoid- ;; erroring on a schema that was dropped manually.- (when schema-exists- (.addBatch ^Statement stmt- ^String (format "ALTER DEFAULT PRIVILEGES IN SCHEMA %s REVOKE ALL ON TABLES FROM %s"- quoted-schema quoted-user))))- ;; These are safe with IF EXISTS- (.addBatch ^Statement stmt- ^String (format "DROP SCHEMA IF EXISTS %s CASCADE" quoted-schema))- (.addBatch ^Statement stmt- ^String (format "DROP USER IF EXISTS %s" quoted-user))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t)))))))))-(defmethod driver/llm-sql-dialect-resource :redshift [_]"metabot/prompts/dialects/redshift.md")diff --git a/modules/drivers/redshift/test/metabase/driver/redshift_test.clj b/modules/drivers/redshift/test/metabase/driver/redshift_test.cljindex a10f93a3429..5d18d77c9d2 100644--- a/modules/drivers/redshift/test/metabase/driver/redshift_test.clj+++ b/modules/drivers/redshift/test/metabase/driver/redshift_test.clj@@ -5,7 +5,6 @@[clojure.string :as str][clojure.test :refer :all][metabase.driver :as driver]- [metabase.driver.redshift :as redshift][metabase.driver.sql-jdbc :as driver.sql-jdbc][metabase.driver.sql-jdbc.connection :as sql-jdbc.conn][metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]@@ -724,228 +723,3 @@;; None (special role in Postgres to revert back to login role; should not be quoted)"none" "SET SESSION AUTHORIZATION none;""NONE" "SET SESSION AUTHORIZATION NONE;"))))))--;;; ---------------------------------------- Workspace provisioning ------------------------------------------------(defn- try-execute!- "Run `sql` against `spec`, logging exceptions at warn level. For cleanup- paths where the object may not exist *or* where a residual catalog row- (the very class of bug GHY-3709 covers) might prevent a successful drop.- Logging instead of swallowing surfaces orphan-role accumulation on CI."- [spec sql]- (try (jdbc/execute! spec [sql])- (catch Throwable t- (log/warnf t "Test cleanup failed: %s" sql))))--(deftest ^:synchronized workspace-precondition-alter-default-privileges-test- ;; Redshift mirror of `metabase.driver.postgres-test/workspace-precondition-alter-default-privileges-test`,- ;; covering GHY-3709. We simulate the prod failure mode by:- ;; - creating a non-superuser "tenant" role,- ;; - seeding a schema with a table whose owner is a different role,- ;; - connecting as the tenant role and running the pre-flight assert.- ;;- ;; The assert must throw with status 412, since the tenant role can neither- ;; impersonate the foreign owner nor flip to superuser at runtime. Once admin- ;; reassigns the table to the tenant role, the assert must pass.- ;;- ;; Note: Redshift's impersonation graph collapses to "current_user == owner- ;; OR current_user is superuser" -- there is no working `pg_has_role(...)`- ;; on RA3 clusters. The PG mirror grants role membership instead; here we- ;; ALTER OWNER to dodge the missing-overload problem.- (mt/test-driver :redshift- (let [admin-spec (sql-jdbc.conn/connection-details->spec :redshift (tx/dbdef->connection-details :redshift))- suffix (u/lower-case-en (mt/random-name))- tenant (str "ws_pre_adp_tenant_" suffix)- owner (str "ws_pre_adp_owner_" suffix)- schema (str "ws_pre_adp_schema_" suffix)- table (str "ws_pre_adp_t_" suffix)- password "Pwd_ws_pre_adp_1!"]- (try- (execute! (str- (format "CREATE USER \"%s\" WITH PASSWORD '%s';%n" tenant password)- (format "CREATE USER \"%s\" WITH PASSWORD '%s';%n" owner password)- (format "CREATE SCHEMA \"%s\";%n" schema)- (format "GRANT USAGE ON SCHEMA \"%s\" TO \"%s\";%n" schema tenant)- (format "CREATE TABLE \"%s\".\"%s\" (id INTEGER);%n" schema table)- ;; Reassign the table to a different role so tenant doesn't own it.- (format "ALTER TABLE \"%s\".\"%s\" OWNER TO \"%s\";%n" schema table owner)))- (sql-jdbc.conn/with-connection-spec-for-testing-connection- [tenant-spec [:redshift (assoc (tx/dbdef->connection-details :redshift)- :user tenant- :password password)]]- (testing "throws when a relation in the input schema is owned by an unmemberable role"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"not a member of \d+ role"- (redshift/assert-can-alter-default-privileges! tenant-spec schema))))- (testing "passes once the foreign-owned object is reassigned to the tenant"- (jdbc/execute! admin-spec- [(format "ALTER TABLE \"%s\".\"%s\" OWNER TO \"%s\""- schema table tenant)])- (is (nil? (redshift/assert-can-alter-default-privileges! tenant-spec schema)))))- (finally- (try-execute! admin-spec (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE" schema))- (try-execute! admin-spec (format "DROP OWNED BY \"%s\"" owner))- (try-execute! admin-spec (format "DROP USER IF EXISTS \"%s\"" tenant))- (try-execute! admin-spec (format "DROP USER IF EXISTS \"%s\"" owner)))))))--(deftest ^:synchronized workspace-precondition-foreign-default-acl-grantor-test- ;; The other half of GHY-3709: a `pg_default_acl` row owned by a role the- ;; tenant cannot impersonate must be caught at grant time. If we let- ;; provisioning through, destroy-time REVOKE has no way to drop that row- ;; and `DROP USER` fails -- which is exactly the production error reported- ;; on workspace 69.- (mt/test-driver :redshift- (let [admin-spec (sql-jdbc.conn/connection-details->spec :redshift (tx/dbdef->connection-details :redshift))- suffix (u/lower-case-en (mt/random-name))- tenant (str "ws_pre_facl_tenant_" suffix)- grantor (str "ws_pre_facl_grantor_" suffix)- schema (str "ws_pre_facl_schema_" suffix)- dummy (str "ws_pre_facl_dummy_" suffix)- password "Pwd_ws_pre_facl_1!"]- (try- (execute! (str- (format "CREATE USER \"%s\" WITH PASSWORD '%s';%n" tenant password)- (format "CREATE USER \"%s\" WITH PASSWORD '%s' CREATEUSER;%n" grantor password)- (format "CREATE USER \"%s\" WITH PASSWORD '%s';%n" dummy password)- (format "CREATE SCHEMA \"%s\" AUTHORIZATION \"%s\";%n" schema grantor)- (format "GRANT USAGE ON SCHEMA \"%s\" TO \"%s\";%n" schema tenant)))- ;; Connect as the foreign grantor to seed a pg_default_acl row owned by them.- (sql-jdbc.conn/with-connection-spec-for-testing-connection- [grantor-spec [:redshift (assoc (tx/dbdef->connection-details :redshift)- :user grantor- :password password)]]- (jdbc/execute! grantor-spec- [(format "ALTER DEFAULT PRIVILEGES IN SCHEMA \"%s\" GRANT SELECT ON TABLES TO \"%s\""- schema dummy)]))- (sql-jdbc.conn/with-connection-spec-for-testing-connection- [tenant-spec [:redshift (assoc (tx/dbdef->connection-details :redshift)- :user tenant- :password password)]]- (testing "throws when the schema carries a pre-existing default-priv row from a foreign grantor"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"not a member of \d+ role"- (redshift/assert-can-alter-default-privileges! tenant-spec schema)))))- (finally- ;; Revoke the seeded default-priv as the grantor (only they can).- (sql-jdbc.conn/with-connection-spec-for-testing-connection- [grantor-spec [:redshift (assoc (tx/dbdef->connection-details :redshift)- :user grantor- :password password)]]- (try-execute! grantor-spec- (format "ALTER DEFAULT PRIVILEGES IN SCHEMA \"%s\" REVOKE ALL ON TABLES FROM \"%s\""- schema dummy)))- (try-execute! admin-spec (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE" schema))- ;; Flush any residual catalog rows referencing these test roles -- the- ;; whole point of this test is the case where DROP USER would fail on- ;; lingering default-priv entries, so cleanup must be belt-and-- ;; suspenders to keep CI re-runs from accumulating orphan roles.- (try-execute! admin-spec (format "DROP OWNED BY \"%s\"" grantor))- (try-execute! admin-spec (format "DROP OWNED BY \"%s\"" dummy))- (try-execute! admin-spec (format "DROP USER IF EXISTS \"%s\"" tenant))- (try-execute! admin-spec (format "DROP USER IF EXISTS \"%s\"" grantor))- (try-execute! admin-spec (format "DROP USER IF EXISTS \"%s\"" dummy)))))))--(deftest ^:synchronized workspace-destroy-survives-foreign-grantor-default-priv-test- ;; GHY-3709 destroy-path integration test (Redshift counterpart to the PG test- ;; of the same name). PG sidesteps this via `DROP OWNED BY <iso-user>`, which- ;; removes default-priv ACL entries where the iso-user is grantee regardless- ;; of grantor. Redshift has no equivalent, so destroy has to enumerate- ;; `pg_default_acl` and issue `ALTER DEFAULT PRIVILEGES FOR USER <grantor>- ;; REVOKE` per discovered (grantor, schema) pair before `DROP USER`.- ;;- ;; Repro shape:- ;; 1. Provision an iso-user via `init-workspace-isolation!`.- ;; 2. Connect as a foreign role (not the current connection user) and- ;; `ALTER DEFAULT PRIVILEGES IN SCHEMA <s> GRANT SELECT ON TABLES TO- ;; <iso-user>` -- catalog row is owned by that foreign grantor.- ;; 3. Run `destroy-workspace-isolation!`. Without the fix, `DROP USER`- ;; fails with "user cannot be dropped because some objects depend on- ;; it / privileges for default privileges on new relations belonging- ;; to user <grantor> in schema <s>". With the fix, destroy enumerates- ;; the row, REVOKEs it FOR USER the grantor, and DROP USER succeeds.- (mt/test-driver :redshift- (let [admin-spec (sql-jdbc.conn/connection-details->spec :redshift (tx/dbdef->connection-details :redshift))- suffix (u/lower-case-en (mt/random-name))- grantor (str "ws_dest_grantor_" suffix)- schema (str "ws_dest_schema_" suffix)- password "Pwd_ws_dest_1!"- workspace {:id (rand-int Integer/MAX_VALUE)- :name (str "wsd-dest-foreign-" suffix)}]- (try- (execute! (str- (format "CREATE USER \"%s\" WITH PASSWORD '%s' CREATEUSER;%n" grantor password)- (format "CREATE SCHEMA \"%s\" AUTHORIZATION \"%s\";%n" schema grantor)))- (let [workspace+det (merge workspace (driver/workspace-isolation-details :redshift (mt/db) workspace))- _ (driver/init-workspace-isolation! :redshift (mt/db) workspace+det)- iso-user (-> workspace+det :database_details :user)]- (try- ;; Seed the foreign-grantor default-priv: connect as the foreign role- ;; and issue ALTER DEFAULT PRIVILEGES so the resulting pg_default_acl- ;; row's grantor (defacluser) is the foreign role, not current_user.- ;; This is the exact production shape that reproduced GHY-3709.- (sql-jdbc.conn/with-connection-spec-for-testing-connection- [grantor-spec [:redshift (assoc (tx/dbdef->connection-details :redshift)- :user grantor- :password password)]]- (jdbc/execute! grantor-spec- [(format "ALTER DEFAULT PRIVILEGES IN SCHEMA \"%s\" GRANT SELECT ON TABLES TO \"%s\""- schema iso-user)]))- (testing "destroy completes without error despite foreign-grantor default-priv row"- (is (some? (driver/destroy-workspace-isolation! :redshift (mt/db) workspace+det))))- (testing "iso-user is gone from pg_user after destroy"- (is (empty? (jdbc/query admin-spec ["SELECT 1 FROM pg_user WHERE usename = ?" iso-user]))))- (finally- ;; Belt-and-suspenders: if destroy raised, the iso-user may still- ;; exist with the foreign-grantor row still pinned to it. Revoke- ;; as the grantor (only they can drop their own default-priv) and- ;; force-drop the iso-user so CI re-runs don't accumulate orphans.- (try- (sql-jdbc.conn/with-connection-spec-for-testing-connection- [grantor-spec [:redshift (assoc (tx/dbdef->connection-details :redshift)- :user grantor- :password password)]]- (try-execute! grantor-spec- (format "ALTER DEFAULT PRIVILEGES IN SCHEMA \"%s\" REVOKE ALL ON TABLES FROM \"%s\""- schema iso-user)))- (catch Throwable t- (log/warn t "Test cleanup: connecting as foreign grantor failed")))- (try-execute! admin-spec (format "DROP OWNED BY \"%s\"" iso-user))- (try-execute! admin-spec (format "DROP USER IF EXISTS \"%s\"" iso-user)))))- (finally- (try-execute! admin-spec (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE" schema))- (try-execute! admin-spec (format "DROP OWNED BY \"%s\"" grantor))- (try-execute! admin-spec (format "DROP USER IF EXISTS \"%s\"" grantor)))))))--(deftest ^:synchronized workspace-destroy-revokes-schema-only-grants-test- ;; `DROP USER` fails with `user ... cannot be dropped because the user has a- ;; privilege on some object` when the iso-user holds a schema-level USAGE- ;; grant on a schema with no relations: destroy used to discover schemas to- ;; revoke via `svv_relation_privileges` only, which cannot surface- ;; schema-level grants when the schema has no tables. The production grant- ;; path (`grant-workspace-read-access!`) issues `GRANT USAGE` per input- ;; schema, so any input schema without tables (or whose tables were dropped- ;; after provisioning) reproduces this.- (mt/test-driver :redshift- (let [admin-spec (sql-jdbc.conn/connection-details->spec :redshift (tx/dbdef->connection-details :redshift))- suffix (u/lower-case-en (mt/random-name))- schema (str "ws_empty_schema_" suffix)- workspace {:id (rand-int Integer/MAX_VALUE)- :name (str "wsd-dest-empty-" suffix)}]- (try- (execute! (format "CREATE SCHEMA \"%s\"" schema))- (let [workspace (merge workspace (driver/workspace-isolation-details :redshift (mt/db) workspace))- _ (driver/init-workspace-isolation! :redshift (mt/db) workspace)- iso-user (-> workspace :database_details :user)]- (try- (jdbc/execute! admin-spec [(format "GRANT USAGE ON SCHEMA \"%s\" TO \"%s\"" schema iso-user)])- (testing "destroy completes despite a schema-level-only grant on an empty schema"- (is (some? (driver/destroy-workspace-isolation! :redshift (mt/db) workspace))))- (testing "iso-user is gone from pg_user after destroy"- (is (empty? (jdbc/query admin-spec ["SELECT 1 FROM pg_user WHERE usename = ?" iso-user]))))- (finally- (try-execute! admin-spec (format "DROP OWNED BY \"%s\"" iso-user))- (try-execute! admin-spec (format "DROP USER IF EXISTS \"%s\"" iso-user)))))- (finally- (try-execute! admin-spec (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE" schema)))))))diff --git a/modules/drivers/redshift/test/metabase/test/data/redshift.clj b/modules/drivers/redshift/test/metabase/test/data/redshift.cljindex 9dddf86d005..e28ac13b7f8 100644--- a/modules/drivers/redshift/test/metabase/test/data/redshift.clj+++ b/modules/drivers/redshift/test/metabase/test/data/redshift.clj@@ -20,7 +20,6 @@[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn][metabase.driver.sql-jdbc.execute :as sql-jdbc.execute][metabase.driver.sql.test-util.unique-prefix :as sql.tu.unique-prefix]- [metabase.driver.util :as driver.u][metabase.test :as mt][metabase.test.data.impl :as data.impl][metabase.test.data.interface :as tx]@@ -188,38 +187,6 @@:unknown-error)))](group-by classify schemas)))))-(defn- classify-isolation-schemas- "Classifies workspace isolation schemas by age using a single query. Returns a map:- {:expired schemas older than threshold (safe to delete)- :recent schemas created within threshold (might be from parallel test)}"- [^java.sql.Connection conn schemas]- (if (empty? schemas)- {}- (let [threshold (t/minus (t/instant) (t/hours hours-before-expired-threshold))- schema-list (str/join "," (map #(str "'" % "'") schemas))- ;; Use pg_class_info joined with pg_namespace to get oldest object creation time per schema- sql (str "SELECT TRIM(n.nspname) as schema_name, MIN(c.relcreationtime) as oldest "- "FROM pg_class_info c "- "JOIN pg_namespace n ON c.relnamespace = n.oid "- "WHERE TRIM(n.nspname) IN (" schema-list ") "- "GROUP BY n.nspname")- schema->time (with-open [stmt (.createStatement conn)- rset (.executeQuery stmt sql)]- (loop [result {}]- (if (.next rset)- (recur (assoc result- (.getString rset "schema_name")- (.getTimestamp rset "oldest")))- result)))]- (group-by (fn [schema-name]- (if-let [oldest (get schema->time schema-name)]- (if (t/before? (t/instant oldest) threshold)- :expired- :recent)- ;; Schema not in pg_class_info means no objects - treat as expired- :expired))- schemas))))-;;; --------------------------------- Enumeration ----------------------------------;;;;;; Pure (read-only) classifiers. Call from REPL to preview what cleanup WOULD do:@@ -227,7 +194,7 @@;;; (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 [...]}+;;; ;; => {:old [...] :expired-cache [...]}(defn- orphan-schemas"Classify every schema in the connected Redshift DB into orphan buckets.@@ -236,53 +203,26 @@: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 TTLPure: 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 :old- caches-with-info :cache- isolation :isolation} (reduce (fn [acc s]- (cond (sql.tu.unique-prefix/old-dataset-name? s)- (update acc :old conj s)- (str/starts-with? s "metabase_cache_")- (update acc :cache conj s)- (driver.u/workspace-isolated-schema? s)- (update acc :isolation conj s)- :else acc))- {:old [] :cache [] :isolation []}- (fetch-schemas conn))+ caches-with-info :cache} (reduce (fn [acc s]+ (cond (sql.tu.unique-prefix/old-dataset-name? s)+ (update acc :old conj s)+ (str/starts-with? s "metabase_cache_")+ (update acc :cache conj s)+ :else acc))+ {:old [] :cache []}+ (fetch-schemas conn)){expired-cache :expiredold-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)]+ lacking-created-at :lacking-created-at} (classify-cache-schemas conn caches-with-info)]{: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)}))--(defn- orphan-isolation-users- "Enumerate iso users (`mb__isolation_*`) from `pg_user` directly.-- Authoritative source: queries the cluster user catalog rather than deriving- from schema names. Catches users whose paired schema was already dropped in- a prior partial-cleanup run (DROP SCHEMA succeeded, DROP USER did not), which- the schema-derived enumeration misses forever.-- No age filter: `pg_user` does not expose user creation time on Redshift- (`usecreatedate` absent, `last_ddl_timestamp` is per-DDL-touch). Caller- filters by pairing against live schemas instead -- `init-workspace-isolation!`- creates schema BEFORE user, so any iso user without a live schema is a- cleanup leak, not a mid-creation race."- [^java.sql.Connection conn]- (with-open [stmt (.createStatement conn)- rs (.executeQuery stmt "SELECT usename FROM pg_user WHERE usename LIKE 'mb__isolation_%'")]- (loop [out []]- (if (.next rs)- (recur (conj out (.getString rs 1)))- out))))+ :lacking-created-at (vec lacking-created-at)}));;; --------------------------------- Destruction ----------------------------------@@ -297,8 +237,7 @@(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"]]+ [:old-style-cache "Dropping old cache schema without `cache_info` table: %s"]]schema (get orphans k)](log/infof fmt-str schema)(try@@ -306,48 +245,17 @@(catch Throwable e(log/infof "Failed to drop %s, skipping: %s" schema (ex-message e)))))))-(defn- drop-orphan-isolation-users!- "Drop iso users whose paired schema is no longer live. Callers ([[delete-old-schemas!]])- compute the username list AFTER [[drop-orphan-schemas!]] has run, so 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.-- Two-phase: drop schemas first, then re-snapshot live schemas and drop any iso- user whose schema is no longer live. Re-snapshotting AFTER the schema drop- means expired-iso users (whose schemas just dropped) are included, while- mid-creation parallel users (whose schemas exist) are excluded. Single- invariant: iso user without live schema = drop."+ "Remove unneeded schemas from redshift. Local databases are thrown away after+ a test run; shared cloud instances are not. Test runs can leak schemas+ (e.g. persisted models), leading to clusters hitting the max-tables limits.++ Glue: thin wrapper that calls the enumerator + dropper in order. To preview+ from a REPL, call [[orphan-schemas]] directly."[^java.sql.Connection conn]- (let [orphans (orphan-schemas conn)- iso-users (orphan-isolation-users conn)]+ (let [orphans (orphan-schemas conn)](with-open [stmt (.createStatement conn)]- (drop-orphan-schemas! stmt orphans)- (let [live-schemas (into #{} (fetch-schemas conn))- users-to-drop (remove live-schemas iso-users)]- (drop-orphan-isolation-users! stmt users-to-drop)))))+ (drop-orphan-schemas! stmt orphans))))(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 e00968d401a..b271cfdf044 100644--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj@@ -28,7 +28,6 @@[metabase.driver.sql.query-processor.util :as sql.qp.u][metabase.driver.sql.util :as sql.u][metabase.driver.sync :as driver.s]- [metabase.driver.util :as driver.u][metabase.lib.schema.common :as lib.schema.common][metabase.lib.schema.metadata :as lib.schema.metadata][metabase.util :as u]@@ -43,7 +42,7 @@(:import(java.io File)(java.net URI URLDecoder)- (java.sql Connection DatabaseMetaData ResultSet ResultSetMetaData Statement Types)+ (java.sql Connection DatabaseMetaData ResultSet ResultSetMetaData Types)(java.time LocalDate LocalDateTime LocalTime OffsetDateTime OffsetTime ZonedDateTime)(net.snowflake.client.api.exception SnowflakeSQLException)))@@ -80,12 +79,10 @@:regex/lookaheads-and-lookbehinds false:transforms/accurate-rows-affected false:transforms/python true- :transforms/table true- :workspace false}]+ :transforms/table true}](defmethod driver/database-supports? [:snowflake feature] [_driver _feature _db] supported?))(mu/defn- quote-schema ^String [s :- :string] (sql.u/quote-name :snowflake :schema s))-(mu/defn- quote-field ^String [s :- :string] (sql.u/quote-name :snowflake :field s))(defmethod driver/humanize-connection-error-message :snowflake[_ messages]@@ -1161,132 +1158,5 @@[driver [_ opts field arg]](string-filter driver :endswith field arg opts))-;;; +----------------------------------------------------------------------------------------------------------------+-;;; | Workspace Isolation |-;;; +----------------------------------------------------------------------------------------------------------------+--(defn- isolation-role-name- "Generate role name for workspace isolation."- [workspace]- (format "MB_ISOLATION_ROLE_%s" (:id workspace)))--(defmethod driver/workspace-isolation-details :snowflake- [driver database workspace]- (-> ((get-method driver/workspace-isolation-details :sql-jdbc) driver database workspace)- (update :database_details assoc- :role (isolation-role-name workspace)- :use-password true)))--(defmethod driver/init-workspace-isolation! :snowflake- [driver database workspace]- (let [details (driver.conn/effective-details database)- schema-name (:schema workspace)- db-name (:db details)- warehouse (:warehouse details)- role-name (-> workspace :database_details :role)- read-user (select-keys (:database_details workspace) [:user :password])- escaped-password (sql.u/escape-sql (:password read-user) :ansi)]- (when-not db-name- (throw (ex-info (tru "Cannot initialize workspace. Snowflake connection details must include a ''db'' (database name). Set it in the database connection and retry.")- {:database-id (:id database) :step :init})))- (when-not warehouse- (throw (ex-info (tru "Cannot initialize workspace. Snowflake connection details must include a ''warehouse''. Set it in the database connection and retry.")- {:database-id (:id database) :step :init})))- (let [quoted-db (quote-schema db-name)- quoted-warehouse (quote-field warehouse)- quoted-schema (quote-schema schema-name)- qualified-schema (str quoted-db "." quoted-schema)- quoted-role (quote-field role-name)- quoted-user (quote-field (:user read-user))]- ;; Snowflake RBAC: create schema -> create role -> grant privileges to role -> create user -> grant role to user- (try- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [^Statement stmt (.createStatement conn)]- (doseq [sql [(format "CREATE SCHEMA IF NOT EXISTS %s" qualified-schema)- (format "CREATE ROLE IF NOT EXISTS %s" quoted-role)- (format "GRANT USAGE ON DATABASE %s TO ROLE %s" quoted-db quoted-role)- (format "GRANT USAGE ON WAREHOUSE %s TO ROLE %s" quoted-warehouse quoted-role)- (format "GRANT USAGE ON SCHEMA %s TO ROLE %s" qualified-schema quoted-role)- (format "GRANT ALL PRIVILEGES ON SCHEMA %s TO ROLE %s" qualified-schema quoted-role)- (format "GRANT ALL ON FUTURE TABLES IN SCHEMA %s TO ROLE %s" qualified-schema quoted-role)- (format "CREATE USER IF NOT EXISTS %s PASSWORD = '%s' MUST_CHANGE_PASSWORD = FALSE DEFAULT_ROLE = %s"- quoted-user escaped-password quoted-role)- ;; the user may survive a failed teardown; without this it would keep- ;; its old password while the new one gets persisted- (format "ALTER USER %s SET PASSWORD = '%s'"- quoted-user escaped-password)- (format "GRANT ROLE %s TO USER %s" quoted-role quoted-user)]]- (.addBatch ^Statement stmt ^String sql))- (.executeBatch ^Statement stmt))))- (catch Throwable t- (throw (driver.u/scrub-exceptions (driver.u/batch-exception t) [(:password read-user) escaped-password])))))- nil))--(defmethod driver/destroy-workspace-isolation! :snowflake- [driver database workspace]- (let [details (driver.conn/effective-details database)- schema-name (:schema workspace)- db-name (:db details)- role-name (-> workspace :database_details :role)- username (-> workspace :database_details :user)]- (when-not db-name- (throw (ex-info (tru "Cannot destroy workspace. Snowflake connection details must include a ''db'' (database name). Set it in the database connection and retry.")- {:database-id (:id database) :step :destroy})))- (let [qualified-schema (str (quote-schema db-name) "." (quote-schema schema-name))- quoted-user (quote-field username)- quoted-role (quote-field role-name)]- ;; Drop in reverse order of creation: schema (CASCADE handles tables) -> user -> role- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [^Statement stmt (.createStatement conn)]- (doseq [sql [(format "DROP SCHEMA IF EXISTS %s CASCADE" qualified-schema)- (format "DROP USER IF EXISTS %s" quoted-user)- (format "DROP ROLE IF EXISTS %s" quoted-role)]]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t))))))))))--(defmethod driver/grant-workspace-read-access! :snowflake- [driver database workspace schemas]- (let [db-name (:db (driver.conn/effective-details database))- role-name (-> workspace :database_details :role)]- (when-not role-name- (throw (ex-info (tru "Cannot grant workspace read access. Workspace details have no role — initialization may have failed. Re-run workspace initialization and retry.")- {:workspace-id (:id workspace) :step :grant})))- (when (str/blank? db-name)- (throw (ex-info (tru "Cannot grant workspace read access. Snowflake connection details must include a ''db'' (database name). Set it in the database connection and retry.")- {: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 [quoted-role (quote-field role-name)- quoted-db (quote-schema db-name)]- (doseq [schema schemas]- (when (str/blank? schema)- (throw (ex-info (tru "Cannot grant workspace read access. Input schema name is blank. Remove the blank entry from the workspace input schemas and retry.")- {:database-id (:id database) :step :grant}))))- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [^Statement stmt (.createStatement conn)]- (doseq [schema schemas- :let [qualified-schema (str quoted-db "." (quote-schema schema))]- sql [(format "GRANT USAGE ON DATABASE %s TO ROLE %s" quoted-db quoted-role)- (format "GRANT USAGE ON SCHEMA %s TO ROLE %s" qualified-schema quoted-role)- (format "GRANT SELECT ON ALL TABLES IN SCHEMA %s TO ROLE %s"- qualified-schema quoted-role)- (format "GRANT SELECT ON FUTURE TABLES IN SCHEMA %s TO ROLE %s"- qualified-schema quoted-role)]]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t))))))))))-(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 f5110e282b9..467e9e67228 100644--- a/modules/drivers/snowflake/test/metabase/test/data/snowflake.clj+++ b/modules/drivers/snowflake/test/metabase/test/data/snowflake.clj@@ -16,9 +16,7 @@[metabase.util :as u][metabase.util.log :as log])(:import- (java.sql PreparedStatement ResultSet)- (java.time Instant)- (java.time.temporal ChronoUnit)))+ (java.sql PreparedStatement ResultSet)))(set! *warn-on-reflection* true)@@ -125,80 +123,10 @@(into [] (map :name) (jdbc/reducible-query (no-db-connection-spec)[query days-ago]))))-(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."- []- (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)- ;; Prefix must match driver.u/workspace-isolated-prefix- rs (.executeQuery stmt "SHOW SCHEMAS LIKE 'mb__isolation_%' IN ACCOUNT")]- (let [three-hours-ago (-> (Instant/now)- (.minus 3 ChronoUnit/HOURS)- java.util.Date/from)]- (loop [results []]- (if (.next rs)- (let [schema-name (.getString rs "name")- db-name (.getString rs "database_name")- created-on (.getTimestamp rs "created_on")]- (if (and created-on (.before created-on three-hours-ago))- (recur (conj results {:schema-name schema-name :database-name db-name}))- (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!`.+;;; 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,@@ -233,64 +161,9 @@#_{: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 haven't been accessed in a while- - 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."+ "Delete old test data: datasets (databases) prefixed by sha_ that haven't been+ accessed in a while."[];; 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@@ -305,10 +178,7 @@;; local testing shows that identifying old datasets works correctly, but;; sometimes randomly in CI it seems to decide that datasets are old and;; deletes them even tho they are not old.- ;; (drop-old-datasets!)- (drop-orphan-isolation-schemas!)- (drop-orphan-isolation-users!)- (drop-orphan-isolation-roles!))+ #_(drop-old-datasets!))(defonce ^:private deleted-old-test-data?(atom false))@@ -333,7 +203,7 @@[driver db-def & options];; qualify the DB name with the unique prefix(let [db-def (assoc db-def :database-name (qualified-db-name db-def))]- ;; clean up any old test data (datasets and isolation schemas)+ ;; clean up any old test data (datasets)(delete-old-test-data-if-needed!);; Snowflake by default uses America/Los_Angeles timezone. See https://docs.snowflake.com/en/sql-reference/parameters#timezone.;; We expect UTC in tests. Hence fixing [[metabase.query-processor.timezone/database-timezone-id]] (PR #36413)diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.cljindex 97b16c296ba..ec71e5431ae 100644--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj@@ -25,13 +25,12 @@[metabase.driver.sql.query-processor.like-escape-char-built-in :as like-escape-char-built-in][metabase.driver.sql.query-processor.util :as sql.qp.u][metabase.driver.sql.util :as sql.u]- [metabase.driver.util :as driver.u][metabase.lib.options :as lib.options][metabase.lib.schema.metadata :as lib.schema.metadata][metabase.sql-tools.core :as sql-tools][metabase.util :as u][metabase.util.honey-sql-2 :as h2x]- [metabase.util.i18n :refer [deferred-tru tru]]+ [metabase.util.i18n :refer [deferred-tru]][metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.match :as match]@@ -39,7 +38,7 @@[metabase.util.performance :as perf :refer [mapv get-in not-empty]][next.jdbc :as next.jdbc])(:import- (java.sql Connection DatabaseMetaData PreparedStatement ResultSet Statement Time)+ (java.sql Connection DatabaseMetaData PreparedStatement ResultSet Time)(java.time LocalDate LocalDateTime LocalTime OffsetDateTime OffsetTime ZonedDateTime)(java.time.format DateTimeFormatter)(java.util UUID)))@@ -71,14 +70,13 @@:describe-default-expr true:describe-is-nullable true:describe-is-generated true- :workspace true: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+ ;; queries are typically `schema.table`, but table remapping rows must be keyed;; on the more general 3-part shape.[:db :schema])@@ -1167,119 +1165,6 @@(str (quote-identifier (name schema)) "." (quote-identifier (name table-name)))(quote-identifier (name table-name))))))-;;; +----------------------------------------------------------------------------------------------------------------+-;;; | Workspace Isolation |-;;; +----------------------------------------------------------------------------------------------------------------+--(defmethod driver/init-workspace-isolation! :sqlserver- [driver database workspace]- (let [schema-name (:schema workspace)- {:keys [user password]} (:database_details workspace)- username user- escaped-password (sql.u/escape-sql password :ansi)- escaped-username (sql.u/escape-sql username :ansi)- escaped-schema (sql.u/escape-sql schema-name :ansi)- quoted-user (quote-field username)- quoted-schema (quote-schema schema-name)]- ;; SQL Server: create login (server level), then user (database level), then schema- (try- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [^Statement stmt (.createStatement conn)]- (doseq [sql [(format (str "IF NOT EXISTS (SELECT name FROM master.sys.server_principals WHERE name = '%s') "- "CREATE LOGIN %s WITH PASSWORD = N'%s'")- escaped-username quoted-user escaped-password)- ;; the login may survive a failed teardown; without this it would keep- ;; its old password while the new one gets persisted- (format "ALTER LOGIN %s WITH PASSWORD = N'%s'" quoted-user escaped-password)- (format "IF NOT EXISTS (SELECT name FROM sys.database_principals WHERE name = '%s') CREATE USER %s FOR LOGIN %s"- escaped-username quoted-user quoted-user)- (format "IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '%s') EXEC('CREATE SCHEMA %s')"- escaped-schema quoted-schema)- ;; Least-privilege grant on the workspace's own schema (vs. the old GRANT- ;; CONTROL, dropping EXECUTE, VIEW DEFINITION, REFERENCES, and re-grant rights):- ;; ALTER - create/drop/sp_rename objects in the schema- ;; SELECT, INSERT, UPDATE, DELETE - full DML (SQL Server, unlike Postgres,- ;; does not confer DML from ALTER/ownership, so grant it explicitly)- (format "GRANT ALTER, SELECT, INSERT, UPDATE, DELETE ON SCHEMA::%s TO %s"- quoted-schema quoted-user)- ;; db-level CREATE TABLE: SELECT INTO (transform materialization) needs it too- (format "GRANT CREATE TABLE TO %s" quoted-user)]]- (.addBatch ^Statement stmt ^String sql))- (.executeBatch ^Statement stmt))))- (catch Throwable t- (throw (driver.u/scrub-exceptions (driver.u/batch-exception t) [password escaped-password]))))- nil))--(defmethod driver/destroy-workspace-isolation! :sqlserver- [driver database workspace]- (let [schema-name (:schema workspace)- username (-> workspace :database_details :user)- escaped-schema (sql.u/escape-sql schema-name :ansi)- escaped-username (sql.u/escape-sql username :ansi)- quoted-schema (quote-schema schema-name)- quoted-user (quote-field username)]- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [^Statement stmt (.createStatement conn)]- ;; mssql-jdbc runs the whole batch in one T-SQL scope, so DECLAREd- ;; variable names must be unique across the batch entries.- (doseq [sql [(format (str "DECLARE @drop_tables_sql NVARCHAR(MAX) = ''; "- "SELECT @drop_tables_sql += 'DROP TABLE %s.[' + name + ']; ' "- "FROM sys.tables WHERE schema_id = SCHEMA_ID('%s'); "- "EXEC sp_executesql @drop_tables_sql")- quoted-schema escaped-schema)- (format "IF EXISTS (SELECT * FROM sys.schemas WHERE name = '%s') DROP SCHEMA %s"- escaped-schema quoted-schema)- (format "IF EXISTS (SELECT * FROM sys.database_principals WHERE name = '%s') DROP USER %s"- escaped-username quoted-user)- ;; Kill all sessions using this login before dropping it- (format (str "DECLARE @kill_sessions_sql NVARCHAR(MAX) = ''; "- "SELECT @kill_sessions_sql += 'KILL ' + CAST(session_id AS VARCHAR(10)) + '; ' "- "FROM sys.dm_exec_sessions WHERE login_name = '%s'; "- "EXEC sp_executesql @kill_sessions_sql")- escaped-username)- (format "IF EXISTS (SELECT * FROM master.sys.server_principals WHERE name = '%s') DROP LOGIN %s"- escaped-username quoted-user)]]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t)))))))))--(defmethod driver/grant-workspace-read-access! :sqlserver- [driver database workspace schemas]- (let [username (-> workspace :database_details :user)- db-name (:db (:details database))]- (when-not username- (throw (ex-info (tru "Cannot grant workspace read access. Workspace details have no read user — initialization may have failed. Re-run workspace initialization and retry.")- {:workspace-id (:id workspace) :step :grant})))- (when (str/blank? db-name)- (throw (ex-info (tru "Cannot grant workspace read access. SQL Server connection details must include a ''db'' (database name). Set it in the database connection and retry.")- {: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 [quoted-user (quote-field username)]- (doseq [schema schemas]- (when (str/blank? schema)- (throw (ex-info (tru "Cannot grant workspace read access. Input schema name is blank. Remove the blank entry from the workspace input schemas and retry.")- {:database-id (:id database) :step :grant}))))- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [^Statement stmt (.createStatement conn)]- (doseq [schema schemas]- (.addBatch ^Statement stmt- ^String (format "GRANT SELECT ON SCHEMA::%s TO %s"- (quote-schema schema)- quoted-user)))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t))))))))))-;;; +----------------------------------------------------------------------------------------------------------------+;;; | Indexes (Index Manager) |;;; +----------------------------------------------------------------------------------------------------------------+diff --git a/resources/migrations/064/20260724_drop_workspace_tables.yaml b/resources/migrations/064/20260724_drop_workspace_tables.yamlnew file mode 100644index 00000000000..529075e064d--- /dev/null+++ b/resources/migrations/064/20260724_drop_workspace_tables.yaml@@ -0,0 +1,236 @@+## Drop the remaining workspace tables (workspace, workspace_database, table_remapping).+## The workspaces feature (transform workspaces with per-database isolation schemas and+## table remapping) was removed; these tables are no longer used. Tables are dropped+## children-first to respect foreign-key dependencies. Each drop has a rollback that+## recreates the table in its pre-drop state (workspace_database.status keeps the+## widened varchar(254) from v64.2026-07-18T10:00:05) so the schema is reversible.++databaseChangeLog:++ - changeSet:+ id: v64.2026-07-24T10:00:00+ author: ranquild+ comment: Drop table_remapping table+ changes:+ - dropTable:+ tableName: table_remapping+ cascadeConstraints: true+ rollback:+ - createTable:+ tableName: table_remapping+ remarks: Maps production tables to workspace tables for query remapping+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: database_id+ type: int+ constraints:+ nullable: false+ - column:+ name: from_db+ type: varchar(254)+ defaultValue: ""+ constraints:+ nullable: false+ - column:+ name: from_schema+ type: varchar(254)+ defaultValue: ""+ constraints:+ nullable: false+ - column:+ name: from_table_name+ type: varchar(254)+ constraints:+ nullable: false+ - column:+ name: to_db+ type: varchar(254)+ defaultValue: ""+ constraints:+ nullable: false+ - column:+ name: to_schema+ type: varchar(254)+ defaultValue: ""+ constraints:+ nullable: false+ - column:+ name: to_table_name+ type: varchar(254)+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false+ - addUniqueConstraint:+ tableName: table_remapping+ constraintName: idx_unique_table_remapping_source+ columnNames: database_id, from_db, from_schema, from_table_name+ - addUniqueConstraint:+ tableName: table_remapping+ constraintName: idx_unique_table_remapping_target+ columnNames: database_id, to_db, to_schema, to_table_name+ - addForeignKeyConstraint:+ baseTableName: table_remapping+ baseColumnNames: database_id+ referencedTableName: metabase_database+ referencedColumnNames: id+ constraintName: fk_table_remapping_database_id+ deleteCascade: true++ - changeSet:+ id: v64.2026-07-24T10:00:01+ author: ranquild+ comment: Drop workspace_database table+ changes:+ - dropTable:+ tableName: workspace_database+ cascadeConstraints: true+ rollback:+ - createTable:+ tableName: workspace_database+ remarks: Associates a workspace with a database, with override credentials and schema config+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: workspace_id+ type: int+ constraints:+ nullable: false+ - column:+ name: database_id+ type: int+ constraints:+ nullable: false+ - column:+ name: database_details+ type: ${text.type}+ constraints:+ nullable: false+ - column:+ name: output_namespace+ type: varchar(254)+ constraints:+ nullable: false+ - column:+ name: input_schemas+ type: ${text.type}+ constraints:+ nullable: false+ - column:+ name: status+ type: varchar(254)+ defaultValue: unprovisioned+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false+ - column:+ name: updated_at+ type: ${timestamp_type}+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false+ - addUniqueConstraint:+ tableName: workspace_database+ constraintName: idx_unique_workspace_database+ columnNames: workspace_id, database_id+ - addForeignKeyConstraint:+ baseTableName: workspace_database+ baseColumnNames: workspace_id+ referencedTableName: workspace+ referencedColumnNames: id+ constraintName: fk_workspace_database_workspace_id+ deleteCascade: true+ - addForeignKeyConstraint:+ baseTableName: workspace_database+ baseColumnNames: database_id+ referencedTableName: metabase_database+ referencedColumnNames: id+ constraintName: fk_workspace_database_database_id+ onDelete: RESTRICT+ - createIndex:+ tableName: workspace_database+ indexName: idx_workspace_database_workspace_id+ columns:+ - column:+ name: workspace_id++ - changeSet:+ id: v64.2026-07-24T10:00:02+ author: ranquild+ comment: Drop workspace table+ changes:+ - dropTable:+ tableName: workspace+ cascadeConstraints: true+ rollback:+ - createTable:+ tableName: workspace+ remarks: Groups one or more databases with override credentials and per-database schema config+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: name+ type: varchar(254)+ constraints:+ nullable: false+ - column:+ name: creator_id+ type: int+ constraints:+ nullable: true+ - column:+ name: created_at+ type: ${timestamp_type}+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false+ - column:+ name: updated_at+ type: ${timestamp_type}+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false+ ## Recreate the explicit creator_id index (v63.2026-07-14T00:00:18) before the FK,+ ## mirroring the original order. Without it, rolling back further aborts when+ ## v63.2026-07-14T00:00:18's auto-rollback tries to DROP INDEX idx_workspace_creator_id.+ - createIndex:+ tableName: workspace+ indexName: idx_workspace_creator_id+ columns:+ - column:+ name: creator_id+ - addForeignKeyConstraint:+ baseTableName: workspace+ baseColumnNames: creator_id+ referencedTableName: core_user+ referencedColumnNames: id+ constraintName: fk_workspace_creator_id+ onDelete: SET NULLdiff --git a/src/metabase/actions/execution.clj b/src/metabase/actions/execution.cljindex c621819d7a0..cc786467fa7 100644--- a/src/metabase/actions/execution.clj+++ b/src/metabase/actions/execution.clj@@ -24,7 +24,6 @@[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!@@ -191,7 +190,6 @@(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 94f881f1c71..40ebcd305bd 100644--- a/src/metabase/analytics/prometheus.clj+++ b/src/metabase/analytics/prometheus.clj@@ -670,7 +670,7 @@{:description "JDBC connection pool acquisitions by connection type (default, write-data, or admin).":labels [:connection-type]})(prometheus/counter :metabase-db-connection/type-resolved- {: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."+ {: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.":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 c5b0d64ae30..0d80f5f4318 100644--- a/src/metabase/analytics/stats.clj+++ b/src/metabase/analytics/stats.clj@@ -794,7 +794,7 @@(defn- ee-snowplow-features-data'[]- (let [features [:sso-jwt :sso-saml :scim :multi-factor-auth :sandboxes :email-allow-list :semantic-search :workspaces]]+ (let [features [:sso-jwt :sso-saml :scim :multi-factor-auth :sandboxes :email-allow-list :semantic-search]](map(fn [feature]{:name featurediff --git a/src/metabase/api/macros.clj b/src/metabase/api/macros.cljindex 0ac2e79b814..053eca8a43f 100644--- a/src/metabase/api/macros.clj+++ b/src/metabase/api/macros.clj@@ -636,7 +636,7 @@{:multipart true} — wraps with multipart-params middleware{:multipart {:max-file-size N, ...}} — same, passing options to wrap-multipart-params- {:scope \"agent:workspaces\"} — wraps with scope enforcement middleware+ {:scope \"agent:query\"} — wraps with scope enforcement middleware{:scope :unchecked} — skips both enforce-scope and ensure-scopes-checkedEndpoints without `:scope` get [[metabase.api.macros.scope/ensure-scopes-checked]] to prevent scoped@@ -703,7 +703,7 @@(mr/def ::ns-endpoints [:map-of ::unique-key ::info])(mr/def ::route-metadata- "Metadata declared on a route via defendpoint, e.g. `{:access :workspace}`."+ "Metadata declared on a route via defendpoint, e.g. `{:scope \"agent:query\"}`.":map)(mr/def ::handler-mapdiff --git a/src/metabase/api/macros/scope.clj b/src/metabase/api/macros/scope.cljindex 43c14ba2578..720c8c629f1 100644--- a/src/metabase/api/macros/scope.clj+++ b/src/metabase/api/macros/scope.clj@@ -1,14 +1,14 @@(ns metabase.api.macros.scope"OAuth-style scope enforcement for API endpoints.- Endpoints declare their required scope via `defendpoint` metadata (e.g. `{:scope \"agent:workspaces\"}`).+ Endpoints declare their required scope via `defendpoint` metadata (e.g. `{:scope \"agent:query\"}`).A per-endpoint middleware rejects requests whose token doesn't carry the required scope.Endpoints that do NOT declare `:scope` automatically reject requests carrying `:token-scopes`,ensuring that scoped tokens can only reach endpoints that explicitly opt in.Scopes are space-delimited strings (mirroring OAuth's `scope` semantics) and support hierarchical- wildcards: `agent:*` covers `agent:workspaces`.+ wildcards: `agent:*` covers `agent:query`.The `::unrestricted` keyword is used as a sentinel in `:token-scopes` to indicate an unrestricted token(session auth or unscoped JWT). Unlike `\"*\"` which is a valid wildcard scope that could appear in a@@ -26,7 +26,7 @@(defn scope-satisfied?"Check if `token-scopes` (a set) satisfies `required-scope` (a string).- Supports hierarchical wildcards: `\"agent:*\"` covers `\"agent:workspaces\"`."+ Supports hierarchical wildcards: `\"agent:*\"` covers `\"agent:query\"`."[token-scopes required-scope](api-scope/scope-matches? token-scopes required-scope))diff --git a/src/metabase/app_db/encryption.clj b/src/metabase/app_db/encryption.cljindex 70efe53da28..d4469bb490e 100644--- a/src/metabase/app_db/encryption.clj+++ b/src/metabase/app_db/encryption.clj@@ -12,7 +12,7 @@;; All columns encrypted via `mi/transform-encrypted-json`. The on-disk format of such a column is;; `encrypt(json-string)`, so rotating the key only requires decrypting the raw value with the current key and;; re-encrypting the resulting string. We list raw table names (not models) so this also works for enterprise models-;; (e.g. WorkspaceDatabase) that aren't loaded in every edition.+;; that aren't loaded in every edition.(def ^:private encrypted-json-columns[[:metabase_database :details][:metabase_database :settings]@@ -20,7 +20,6 @@[:metabase_database :admin_details][:core_user :settings][:channel :details]- [:workspace_database :database_details][:auth_identity :credentials]]);; Older versions of dump-to-h2 and key rotation only processed `metabase_database.details` (plus settings anddiff --git a/src/metabase/cmd/copy.clj b/src/metabase/cmd/copy.cljindex a09eaadf5c5..a3e646a0433 100644--- a/src/metabase/cmd/copy.clj+++ b/src/metabase/cmd/copy.clj@@ -149,10 +149,7 @@:model/Sandbox:model/Tenant:model/ConnectionImpersonation- :model/CustomVizPlugin- :model/Workspace- :model/WorkspaceDatabase- :model/TableRemapping])))+ :model/CustomVizPlugin])))(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 cf7b82c2f6f..6e22f26749d 100644--- a/src/metabase/driver.clj+++ b/src/metabase/driver.clj@@ -391,8 +391,8 @@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.+ - 1-level over a catalog (`SELECT * FROM t`, but consumers need a catalog+ name to route across DBs): `[:db]` — MySQL.The compiled SQL is still bare; this slot tells consumers MySQL has ameaningful database identifier (= JDBC `TABLE_CAT`) above the table.- 2-level (`SELECT * FROM s.t`): `[:schema]` — Postgres, Redshift, ClickHouse, H2, Oracle@@ -400,7 +400,7 @@(Snowflake `db.schema.table`; SQL Server `db.schema.table`; BigQuery`project.dataset.table`).- Used by workspace table remapping to decide:+ Used by table remapping to decide:- which columns to populate when storing a `:model/TableRemapping` row- which AST positions to match against during query rewriting@@ -913,9 +913,6 @@;; Does this driver provide :database-is-generated on (describe-fields) or (describe-table):describe-is-generated;;- ;; Does this driver support the workspace feature- :workspace- ;;;; Does this driver support table references in native queries -- for example, "select * from {{table}}" where;; `{{table}}` gets replaced by a reference to a table.:parameters/table-reference@@ -970,8 +967,7 @@:test/uuids-in-create-table-statements true:test/use-fake-sync false:metadata/table-existence-check false- :metadata/table-writable-check false- :workspace false}]+ :metadata/table-writable-check false}](defmethod database-supports? [::driver feature] [_driver _feature _db] supported?));;; By default a driver supports `:native-parameter-card-reference` if it supports `:native-parameters` AND@@ -1444,7 +1440,7 @@Unlike [[native-query-deps]] which looks up tables in the database to return IDs,this method returns just the schema and table names as they appear in the query.- This is useful for workspace dependency tracking where referenced tables may not+ This is useful for dependency tracking where referenced tables may notexist yet.`query` is a Lib `:metabase.lib.schema/native-only-query`; you can use@@ -1999,103 +1995,6 @@false(throw e)))))-;;; +----------------------------------------------------------------------------------------------------------------+-;;; | Workspace Isolation |-;;; +----------------------------------------------------------------------------------------------------------------+--(defmulti workspace-isolation-details- "Compute the isolation resource descriptors for `workspace` without touching the- warehouse. Returns a map with:-- - :schema - The name of the isolated schema/database to create- - :database_details - Connection details (user, password, etc.) for the isolated user-- Names are deterministic (workspace id + instance slug), but generated credentials- are random per call — callers must compute this once, merge it into the workspace- map they pass to [[init-workspace-isolation!]]/[[grant-workspace-read-access!]]/- [[destroy-workspace-isolation!]], and persist `:database_details` themselves.- Computing details before init is what makes cleanup of a partially-failed init- possible: destroy can run with the same map even when init never returned.-- Implementations must be pure computation — no warehouse connections or mutations.- Reading the database's connection details (e.g. for a bound catalog) is fine;- callers bind [[metabase.driver.connection/with-admin-connection]] so those resolve- against the `:admin-details` overlay when configured."- {:added "0.64.0" :arglists '([driver database workspace])}- dispatch-on-initialized-driver- :hierarchy #'hierarchy)--(defmulti init-workspace-isolation!- "Initialize database isolation for a workspace. Creates an isolated schema/database,- user credentials, and grants appropriate permissions for the workspace to operate- within its own namespace.-- `workspace` must already carry `:schema` and `:database_details` — the output of- [[workspace-isolation-details]] merged over the workspace map. Implementations- create resources with exactly those identifiers/credentials and must not generate- names or passwords themselves.-- Implementations should:- - Create the `:schema` isolated schema or database for the workspace- - Create the `:database_details` user, with credentials that can only access that schema- - Grant appropriate permissions (CREATE, INSERT, SELECT, etc.) on the isolated schema-- The return value is unspecified; callers should persist the precomputed details,- not anything returned from here.-- This is an enterprise feature. Drivers must also return true for- (database-supports? driver :workspace database) to indicate support.-- Callers are expected to bind [[metabase.driver.connection/with-admin-connection]]- around this call so connections resolve against the database's `:admin-details`- overlay when configured. The default `dispatching-provisioner` in- `metabase-enterprise.workspaces.provisioning` does this; do not rely on- default connection details from inside impls."- {:added "0.59.0" :arglists '([driver database workspace])}- dispatch-on-initialized-driver- :hierarchy #'hierarchy)--(defmulti destroy-workspace-isolation!- "Destroy all database resources created for workspace isolation.- This includes dropping schemas/databases, users, roles, and any other- resources created by init-workspace-isolation!.-- `workspace` carries `:schema` and `:database_details` — either the persisted- values from provisioning, or freshly computed via [[workspace-isolation-details]]- when nothing was persisted (crashed init). Implementations read identifiers from- the map rather than recomputing them, and must not rely on `:password` matching- what's on the warehouse (a recomputed password never does).-- Should be called when deleting a workspace. Implementations should be- idempotent - calling on an already-destroyed workspace should not error.-- Same admin-connection-scope contract as [[init-workspace-isolation!]]: callers- bind [[metabase.driver.connection/with-admin-connection]] before invoking."- {:added "0.59.0" :arglists '([driver database workspace])}- dispatch-on-initialized-driver- :hierarchy #'hierarchy)--(defmulti grant-workspace-read-access!- "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.-- Same admin-connection-scope contract as [[init-workspace-isolation!]]: callers- bind [[metabase.driver.connection/with-admin-connection]] before invoking."- {:added "0.59.0" :arglists '([driver database workspace input])}- dispatch-on-initialized-driver- :hierarchy #'hierarchy)-(defmulti validate-impersonated-query"Validates a query for impersonation. Returns the query if it is valid and throws otherwise."{:added "0.60.0" :arglists '([driver query])}diff --git a/src/metabase/driver/connection.clj b/src/metabase/driver/connection.cljindex 2acbea475a6..a644b377b16 100644--- a/src/metabase/driver/connection.clj+++ b/src/metabase/driver/connection.clj@@ -3,14 +3,13 @@Direct access to `(:details database)` is an anti-pattern. It couples callers tothe raw data layout, which means any change to how details are resolved — connection- routing, write credentials, workspace isolation, security boundaries — requires+ routing, write credentials, security boundaries — requiresfinding and updating every call site. This namespace provides the indirection thatmakes those changes possible.Primary API: [[effective-details]], [[with-write-connection]], [[default-details]]."(:require[metabase.analytics-interface.core :as analytics]- [metabase.driver.connection.workspaces :as driver.w][metabase.driver.util :as driver.u][metabase.premium-features.core :refer [defenterprise]][metabase.util :as u]@@ -47,14 +46,13 @@;; there's no corresponding classification for "this field doesn't need to be encrypted at all.";;;; This namespace centralizes all reads of `:details` (and `:write-data-details`) behind-;; functions that can apply connection-type routing, workspace isolation, and — eventually —-;; a proper separation of credentials from configuration. The immediate goal is to make-;; direct `(:details database)` access a greppable code smell. The longer-term goal is to+;; functions that can apply connection-type routing and — eventually — a proper separation+;; of credentials from configuration. The immediate goal is to make direct+;; `(:details database)` access a greppable code smell. The longer-term goal is to;; make it possible to store non-sensitive configuration outside the encrypted column;; without a codebase-wide refactor. The immediate need for this namespace was to make it;; possible to access and *use* configuration in a controllable and auditable way as we-;; are going from one master-connection to a database to two + any number of dynamic-;; workspaces connections.+;; are going from one master-connection to a database to two.;;;; Please use/adapt/augment/improve this namespace and avoid all such patterns:;; - (:details database)@@ -145,14 +143,6 @@`(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`."@@ -161,7 +151,7 @@nil)(defenterprise database-admin-details- "Returns the `:admin-details` for a database, or `nil` if the workspaces feature is not available.+ "Returns the `:admin-details` for a database, or `nil` if the connection-overlays feature is not available.OSS implementation always returns `nil`."metabase-enterprise.connection-overlays.core[_database]@@ -203,24 +193,21 @@scope, takes `:write-data-details` into account (if configured) — transforms write, sothey get write-data credentials when set — but resolves to a *different* pool key(`:transform` vs `:write-data`) so the pool properties (e.g. `unreturnedConnectionTimeout`)- can differ. Within a- [[driver.w/with-swapped-connection-details]] scope, applies workspace isolation- overrides on top."+ can differ."[database](when-let [database (some-> database driver.u/ensure-lib-database)](let [overlay (perf/not-empty (overlay-details-for-type database *connection-type*))base (merge (:details database) overlay)eff-type (resolve-effective-type *connection-type* overlay)]- ;; Track when an overlay is genuinely used (not fallback, not workspace-swapped).+ ;; Track when an overlay is genuinely used (not fallback).;; Default resolutions are not tracked here — see :metabase-db-connection/write-op for;; pool-level connection acquisition metrics.(when (and overlay- (not *suppress-resolution-telemetry*)- (not (driver.w/has-connection-swap? (:id database))))+ (not *suppress-resolution-telemetry*))(try (analytics/inc! :metabase-db-connection/type-resolved{:connection-type (name *connection-type*)})(catch Exception _ nil)))- (-> (driver.w/maybe-swap-details (:id database) base)+ (-> base(assoc ::connection-pool-type eff-type)(assoc ::database-id (u/id database))))))diff --git a/src/metabase/driver/connection/workspaces.clj b/src/metabase/driver/connection/workspaces.cljdeleted file mode 100644index 8f397050a3c..00000000000--- a/src/metabase/driver/connection/workspaces.clj+++ /dev/null@@ -1,84 +0,0 @@-(ns metabase.driver.connection.workspaces- (:require- [metabase.util.log :as log]))--(def ^:private ^:dynamic *swapped-connection-details*- "A dynamic var that holds a map of database-id -> swapped-details-map for temporarily swapping connection details.- When a connection spec is created for a database, if its ID is present in this map, the swap map will be- merged into the connection `:details` before they are used to create a connection.-- This provides a mechanism for temporarily using different connection details (e.g., using alternative credentials- for workspaces) without mutating the database record.-- The swap map is merged into the database `:details` map. The swap is applied before any connection-specific- processing (like hash calculation for connection pooling), so different swaps will result in different- connection pools.-- Different drivers may apply this swap at different points in their connection lifecycle, but the semantics- are consistent: swapped details are used for the duration of the dynamic scope.-- See [[with-swapped-connection-details]] for usage."- nil)--(defn- apply-detail-swaps- "Merges the `swap-map` into `details`. Supports nested maps via deep merge."- [details swap-map]- (reduce-kv- (fn [acc k v]- (if (and (map? v) (map? (get acc k)))- (assoc acc k (apply-detail-swaps (get acc k) v))- (assoc acc k v)))- details- swap-map))--(defn has-connection-swap?- "Returns true if there is an active connection detail swap for `database-id`."- [database-id]- (contains? *swapped-connection-details* database-id))--(defn maybe-swap-details- "Returns the database details with any swaps applied from [[*swapped-connection-details*]].- If no swap exists for `database-id`, returns `details` unchanged.-- Drivers should call this function when creating connections to apply any active swaps.- For JDBC drivers, this is called in [[metabase.driver.sql-jdbc.connection/db->pooled-connection-spec]].- For other drivers (e.g., MongoDB), this should be called in their connection creation logic."- [database-id details]- (if-let [swap-map (get *swapped-connection-details* database-id)]- (do- (log/debugf "Applying swapped connection details for database %d, swap keys: %s"- database-id (keys swap-map))- (apply-detail-swaps details swap-map))- details))--(defn do-with-swapped-connection-details- "Implementation for [[with-swapped-connection-details]]."- [database-id swap-map thunk]- (when (contains? *swapped-connection-details* database-id)- (throw (ex-info "Nested connection detail swaps are not supported for the same database"- {:database-id database-id})))- (log/debugf "Entering swapped connection details scope for database %d, swap keys: %s"- database-id (keys swap-map))- (binding [*swapped-connection-details* (assoc *swapped-connection-details* database-id swap-map)]- (thunk)))--(defmacro with-swapped-connection-details- "Temporarily swap the connection details for a specific database within the dynamic scope of `body`.-- The `swap-map` is a map of detail keys to swap values. These will be merged into the database's- connection `:details` map. Nested maps are deep-merged.-- Any code that creates a connection for `database-id` within this scope will use the modified details.-- **Important:** Nested swaps for the same database are not supported and will throw an exception.- Different databases can have concurrent swaps.-- Example:-- ;; Swap connection to use alternate credentials- (driver/with-swapped-connection-details 1 {:user \"workspace-user\" :password \"workspace-pass\"}- ;; All connections created in this scope use the swapped credentials- (qp/process-query query))"- {:style/indent 2}- [database-id swap-map & body]- `(do-with-swapped-connection-details ~database-id ~swap-map (fn [] ~@body)))diff --git a/src/metabase/driver/h2.clj b/src/metabase/driver/h2.cljindex a514acd1a2a..bdbd67a6d27 100644--- a/src/metabase/driver/h2.clj+++ b/src/metabase/driver/h2.clj@@ -20,8 +20,6 @@[metabase.driver.sql.normalize :as sql.normalize][metabase.driver.sql.query-processor :as sql.qp][metabase.driver.sql.query-processor.like-escape-char-built-in :as like-escape-char-built-in]- [metabase.driver.sql.util :as sql.u]- [metabase.driver.util :as driver.u][metabase.lib.schema.id :as lib.schema.id][metabase.lib.schema.metadata :as lib.schema.metadata][metabase.util :as u]@@ -31,7 +29,7 @@[metabase.util.malli :as mu][metabase.util.performance :as perf :refer [every? some]])(:import- (java.sql Clob Connection ResultSet ResultSetMetaData SQLException Statement Types)+ (java.sql Clob ResultSet ResultSetMetaData SQLException Types)(java.time OffsetTime)(org.h2.command CommandInterface Parser)(org.h2.engine SessionLocal)@@ -78,9 +76,6 @@:test/jvm-timezone-setting false:uuid-type true:uploads true- ;; (Ngoc - 2026-01-27) we have the code to support workspace isolation but since workspace- ;; is useless with out transforms, so we disable it for now- :workspace false:database-routing true:describe-is-generated true:describe-is-nullable true@@ -180,7 +175,7 @@(when (= (keyword query-type) :native)(let [details (-> (driver-api/metadata-provider) driver-api/database driver.conn/effective-details)user (db-details->user details)]- (when (and config/is-prod? ;; we elevated permissions in workspace tests+ (when (and config/is-prod? ;; permissions are elevated in some tests(or (str/blank? user)(= user "sa"))) ; "sa" is the default USER(throw@@ -732,95 +727,5 @@[_]"PUBLIC")-;;; +----------------------------------------------------------------------------------------------------------------+-;;; | Workspace Isolation |-;;; +----------------------------------------------------------------------------------------------------------------+--(defn- replace-credentials- "Replace USER and PASSWORD in an H2 connection string."- [connection-string new-user new-password]- (let [[file options] (connection-string->file+options connection-string)]- (file+options->connection-string file (assoc options "USER" new-user "PASSWORD" new-password))))--(defn- get-user-from-connection-string- "Extract the USER from an H2 connection string."- [connection-string]- (let [[_file options] (connection-string->file+options connection-string)]- (get options "USER")))--(defmethod driver/workspace-isolation-details :h2- [_driver database workspace]- ;; H2 embeds credentials in the :db connection string, so build a new one off the- ;; connection's (admin-overlay-resolved) string rather than returning :user/:password.- (let [username (driver.u/workspace-isolation-user-name workspace)- password (driver.u/random-workspace-password)- original-db (:db (driver.conn/effective-details database))]- {:schema (driver.u/workspace-isolation-namespace-name workspace)- :database_details {:db (replace-credentials original-db username password)}}))--(defmethod driver/init-workspace-isolation! :h2- [driver database workspace]- (let [schema-name (:schema workspace)- [_file {username "USER" password "PASSWORD"}]- (connection-string->file+options (-> workspace :database_details :db))- escaped-password (sql.u/escape-sql password :ansi)]- ;; No transaction: H2 DDL (CREATE USER/SCHEMA, GRANT) commits any open- ;; transaction, so a wrapper would be decorative. Failure recovery is- ;; compensation via the idempotent destroy, not rollback.- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [^Statement stmt (.createStatement conn)]- (doseq [sql [(format "CREATE USER IF NOT EXISTS \"%s\" PASSWORD '%s'" username escaped-password)- ;; the user may survive a failed teardown; without this it would keep- ;; its old password while the new one gets persisted- (format "ALTER USER \"%s\" SET PASSWORD '%s'" username escaped-password)- (format "CREATE SCHEMA IF NOT EXISTS \"%s\" AUTHORIZATION \"%s\"" schema-name username)- (format "GRANT ALL ON SCHEMA \"%s\" TO \"%s\"" schema-name username)]]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/scrub-exceptions (driver.u/batch-exception t) [password escaped-password])))))))- nil))--(defmethod driver/destroy-workspace-isolation! :h2- [driver database workspace]- (let [schema-name (:schema workspace)- username (-> workspace :database_details :db get-user-from-connection-string)]- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [^Statement stmt (.createStatement conn)]- (doseq [sql [;; CASCADE drops all objects (tables, etc.) in the schema- (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE" schema-name)- (format "DROP USER IF EXISTS \"%s\"" username)]]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t)))))))))--(defmethod driver/grant-workspace-read-access! :h2- [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 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.- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [^Statement stmt (.createStatement conn)]- (doseq [schema schemas]- (.addBatch ^Statement stmt- ^String (format "GRANT SELECT ON SCHEMA %s TO %s"- (sql.u/quote-name :h2 :schema schema) qu)))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t)))))))))-(defmethod driver/llm-sql-dialect-resource :h2 [_]"metabot/prompts/dialects/h2.md")diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.cljindex 439ec2333cd..b3da54a40ce 100644--- a/src/metabase/driver/mysql.clj+++ b/src/metabase/driver/mysql.clj@@ -27,7 +27,6 @@[metabase.driver.sql.query-processor.like-escape-char-built-in :as like-escape-char-built-in][metabase.driver.sql.query-processor.util :as sql.qp.u][metabase.driver.sql.util :as sql.u]- [metabase.driver.util :as driver.u][metabase.lib.schema.common :as lib.schema.common][metabase.util :as u][metabase.util.date-2 :as u.date]@@ -40,7 +39,7 @@[next.jdbc :as next.jdbc])(:import(java.io File)- (java.sql Connection DatabaseMetaData ResultSet ResultSetMetaData SQLException Statement Types)+ (java.sql Connection DatabaseMetaData ResultSet ResultSetMetaData SQLException Types)(java.time LocalDateTime OffsetDateTime OffsetTime ZonedDateTime ZoneOffset)(java.time.format DateTimeFormatter)))@@ -97,21 +96,20 @@:transforms/index-ddl true:describe-default-expr true:describe-is-nullable true- :describe-is-generated true- :workspace true}]+ :describe-is-generated 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+ ;; with the connection's bound DB so table remapping can record both canonical and remapped;; 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`+;;; MySQL has no schema layer. Table remapping 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.@@ -1300,109 +1298,6 @@[_driver]nil)-;;; +----------------------------------------------------------------------------------------------------------------+-;;; | Workspace Isolation |-;;; +----------------------------------------------------------------------------------------------------------------+--(defn- quote-schema [s] (sql.u/quote-name :mysql :schema s))-(defn- quote-field [s] (sql.u/quote-name :mysql :field s))--(defn- mysql-user-exists?- "Check if a MySQL user exists."- [conn username]- (seq (jdbc/query conn ["SELECT 1 FROM mysql.user WHERE user = ?" username])))--(defmethod driver/init-workspace-isolation! :mysql- [driver database workspace]- ;; MySQL doesn't have schemas in the PostgreSQL sense - each database is its own namespace.- ;; We create a separate database for workspace isolation.- (let [db-name (:schema workspace)- {:keys [user password]} (:database_details workspace)- escaped-password (sql.u/escape-sql password :ansi)- quoted-db (quote-schema db-name)- quoted-user (quote-field user)]- ;; No transaction: MySQL DDL (CREATE DATABASE/USER, GRANT) implicitly commits- ;; per statement, so a transaction wrapper would be decorative. Failure- ;; recovery is compensation via the idempotent destroy, not rollback.- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (let [user-sql (if (mysql-user-exists? {:connection conn} user)- (format "ALTER USER %s@'%%' IDENTIFIED BY '%s'"- quoted-user escaped-password)- (format "CREATE USER %s@'%%' IDENTIFIED BY '%s'"- quoted-user escaped-password))]- (with-open [^Statement stmt (.createStatement conn)]- (doseq [sql [;; Create the isolated database- (format "CREATE DATABASE IF NOT EXISTS %s" quoted-db)- user-sql- ;; Least-privilege grant on the workspace's own DB (vs. ALL PRIVILEGES,- ;; dropping GRANT OPTION, CREATE VIEW/ROUTINE, TRIGGER, etc.):- ;; SELECT, INSERT, UPDATE, DELETE - full DML on its own tables- ;; CREATE - transform target / CTAS- ;; DROP - swap/cleanup- ;; ALTER - required (with DROP/CREATE) for RENAME TABLE swaps- (format "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER ON %s.* TO %s@'%%'"- quoted-db quoted-user)]]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/scrub-exceptions (driver.u/batch-exception t) [password escaped-password]))))))))- nil))--(defmethod driver/destroy-workspace-isolation! :mysql- [driver database workspace]- (let [db-name (:schema workspace)- username (-> workspace :database_details :user)- quoted-db (quote-schema db-name)- quoted-user (quote-field username)]- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [^Statement stmt (.createStatement conn)]- (doseq [sql (cond-> [(format "DROP DATABASE IF EXISTS %s" quoted-db)]- (mysql-user-exists? {:connection conn} username)- (conj (format "DROP USER IF EXISTS %s@'%%'" quoted-user)))]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t)))))))))--(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 [quoted-user (quote-field username)- source-databases (set schemas)]- (mapv (fn [db]- (format "GRANT SELECT ON %s.* TO %s@'%%'"- (quote-schema db) quoted-user))- source-databases)))--(defmethod driver/grant-workspace-read-access! :mysql- [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)- sqls (grant-workspace-read-access-sqls username schemas)]- (sql-jdbc.execute/do-with-connection-with-options- driver database {:write? true}- (fn [^Connection conn]- (with-open [^Statement stmt (.createStatement conn)]- (doseq [sql sqls]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t)))))))))-;;; +----------------------------------------------------------------------------------------------------------------+;;; | Indexes (Index Manager) |;;; +----------------------------------------------------------------------------------------------------------------+diff --git a/src/metabase/driver/postgres.clj b/src/metabase/driver/postgres.cljindex 6bc5e72f2fd..2ca81c92e4a 100644--- a/src/metabase/driver/postgres.clj+++ b/src/metabase/driver/postgres.clj@@ -28,7 +28,6 @@[metabase.driver.sql.query-processor.like-escape-char-built-in :as like-escape-char-built-in][metabase.driver.sql.query-processor.util :as sql.qp.u][metabase.driver.sql.util :as sql.u]- [metabase.driver.util :as driver.u][metabase.util :as u][metabase.util.date-2 :as u.date][metabase.util.honey-sql-2 :as h2x]@@ -41,7 +40,7 @@[taoensso.nippy :as nippy])(:import(java.io DataInput DataOutput StringReader)- (java.sql Array Connection ResultSet ResultSetMetaData Statement Types)+ (java.sql Array Connection ResultSet ResultSetMetaData Types)(java.time LocalDateTime OffsetDateTime OffsetTime)(org.apache.commons.codec.binary Hex)(org.postgresql.copy CopyManager)@@ -94,8 +93,7 @@:transforms/python true:transforms/table true:uploads true- :uuid-type true- :workspace true}]+ :uuid-type true}](defmethod driver/database-supports? [:postgres feature] [_driver _feature _db] supported?))(defmethod driver/database-supports? [:postgres :nested-field-columns]@@ -125,8 +123,6 @@;;; +----------------------------------------------------------------------------------------------------------------+(defn- quote-schema [s] (sql.u/quote-name :postgres :schema s))-(defn- quote-field [s] (sql.u/quote-name :postgres :field s))-(defn- quote-table [s] (sql.u/quote-name :postgres :table s))(defmethod driver/display-name :postgres [_] "PostgreSQL")@@ -1476,291 +1472,6 @@value (nippy/thaw-from-in! data-input)](doto (PGobject.) (.setType type) (.setValue value))))-;;; +----------------------------------------------------------------------------------------------------------------+-;;; | Workspace Isolation |-;;; +----------------------------------------------------------------------------------------------------------------+--(defn- user-exists?- "Check if a PostgreSQL user exists. Uses pg_user which also works in Redshift."- [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]- (let [quoted-schema (quote-schema schema-name)]- (throw (ex-info (format (str "Schema %s grants CREATE to PUBLIC. This breaks workspace "- "isolation. Run `REVOKE CREATE ON SCHEMA %s FROM PUBLIC` and retry.")- quoted-schema quoted-schema)- {: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)))--(defn schema-missing-usage-grant-option?- "True when the current PostgreSQL user does not hold `USAGE WITH GRANT OPTION`- on `schema-name`. Without it, our `GRANT USAGE ON SCHEMA … TO <user>` silently- no-ops and the workspace user can't resolve table names at all."- [conn schema-name]- (let [row (first (jdbc/query- conn- ["SELECT has_schema_privilege(current_user, ?, 'USAGE WITH GRANT OPTION') AS can_grant"- schema-name]))]- (not (:can_grant row))))--(defn raise-missing-usage-grant-option!- "Throw the standard ex-info for the schema-USAGE-grant-option pre-condition."- [schema-name]- (let [quoted-schema (quote-schema schema-name)]- (throw (ex-info (format (str "Current user lacks USAGE WITH GRANT OPTION on schema %s. "- "Cannot grant USAGE to the workspace user. Have the schema owner "- "run `GRANT USAGE ON SCHEMA %s TO CURRENT_USER WITH GRANT OPTION` "- "and retry.")- quoted-schema quoted-schema)- {:status-code 412- :schema schema-name}))))--(defn assert-has-usage-grant-option!- "Throws when the current PostgreSQL user lacks `USAGE WITH GRANT OPTION` on- `schema-name`. Called from `grant-workspace-read-access!` per input schema."- [conn schema-name]- (when (schema-missing-usage-grant-option? conn schema-name)- (raise-missing-usage-grant-option! schema-name)))--(defn tables-missing-grant-option- "Return objects in `schema-name` for which the current PostgreSQL user does- *not* hold `SELECT WITH GRANT OPTION`. Probes tables, partitioned tables,- views, materialized views, and foreign tables — the same `relkind`s touched- by `GRANT SELECT ON ALL TABLES IN SCHEMA`. `GRANT` silently no-ops on objects- the connection user can't re-grant, so we check up front to fail fast."- [conn schema-name]- (jdbc/query- conn- [(str "SELECT n.nspname AS schema, c.relname AS object "- "FROM pg_class c "- "JOIN pg_namespace n ON n.oid = c.relnamespace "- "WHERE n.nspname = ? "- " AND c.relkind IN ('r','p','v','m','f') "- " AND NOT has_table_privilege(current_user, c.oid, 'SELECT WITH GRANT OPTION') "- "ORDER BY c.relname")- schema-name]))--(defn raise-missing-grant-option!- "Throw the standard ex-info for the SELECT-WITH-GRANT-OPTION pre-condition,- naming the objects the current user cannot re-grant SELECT on."- [schema-name objects]- (let [qualified (map #(str (quote-schema (:schema %)) "." (quote-table (:object %)))- objects)- quoted-schema (quote-schema schema-name)]- (throw (ex-info (format (str "Current user lacks SELECT WITH GRANT OPTION on %d object(s) in "- "schema %s: %s. Cannot grant SELECT to the workspace user. "- "Have the object owner run `GRANT SELECT ON ALL TABLES IN SCHEMA "- "%s TO CURRENT_USER WITH GRANT OPTION` and retry.")- (count objects)- quoted-schema- (str/join ", " qualified)- quoted-schema)- {:status-code 412- :schema schema-name- :objects (vec qualified)}))))--(defn assert-has-grant-option!- "Throws when the current PostgreSQL user lacks `SELECT WITH GRANT OPTION` on- any table/view in `schema-name`. Called from `grant-workspace-read-access!`- per input schema alongside [[assert-no-public-create-grant!]] — without the- grant option, our `GRANT SELECT ON ALL TABLES IN SCHEMA` silently skips the- objects and the workspace user can't read them."- [conn schema-name]- (when-let [missing (seq (tables-missing-grant-option conn schema-name))]- (raise-missing-grant-option! schema-name missing)))--(defn unmemberable-owners-in-schema- "Return distinct roles owning objects in `schema-name` that the current- PostgreSQL user is *not* a member of. `ALTER DEFAULT PRIVILEGES` (without- `FOR ROLE`) only affects future objects created by the connection user, so- tables created by any of these owner roles won't receive the workspace- user's default SELECT — even if the statement runs without error.-- PostgreSQL-only: the query relies on `pg_has_role`'s implicit overload- resolution (`name, oid, text`), which Redshift's planner rejects."- [conn schema-name]- (jdbc/query- conn- [(str "SELECT DISTINCT pg_get_userbyid(c.relowner) AS owner "- "FROM pg_class c "- "JOIN pg_namespace n ON n.oid = c.relnamespace "- "WHERE n.nspname = ? "- " AND c.relkind IN ('r','p','v','m','f') "- " AND NOT pg_has_role(current_user, c.relowner, 'MEMBER') "- "ORDER BY owner")- schema-name]))--(defn raise-unmemberable-default-priv-owners!- "Throw the standard ex-info for the ALTER-DEFAULT-PRIVILEGES pre-condition,- naming the owner roles we cannot target with `FOR ROLE`."- [schema-name owners]- (let [owner-names (map :owner owners)- quoted-schema (quote-schema schema-name)- grants (str/join " "- (map #(format "GRANT %s TO CURRENT_USER;" (quote-field %))- owner-names))]- (throw (ex-info (format (str "Current user is not a member of %d role(s) owning objects in "- "schema %s: %s. The workspace user would lose access to "- "future tables created by these roles. Grant role membership "- "with `%s` and retry.")- (count owners) quoted-schema (str/join ", " owner-names) grants)- {:status-code 412- :schema schema-name- :owners (vec owner-names)}))))--(defn assert-can-alter-default-privileges!- "Throws when objects in `schema-name` are owned by roles the current user- isn't a member of, since our `ALTER DEFAULT PRIVILEGES IN SCHEMA …` (without- `FOR ROLE`) can't extend the workspace user's default SELECT to future- objects created by those roles."- [conn schema-name]- (when-let [missing (seq (unmemberable-owners-in-schema conn schema-name))]- (raise-unmemberable-default-priv-owners! schema-name missing)))--;;; 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 (:schema workspace)- read-user (:database_details workspace)- escaped-password (sql.u/escape-sql (:password read-user) :ansi)- quoted-schema (quote-schema schema-name)- quoted-user (quote-field (:user read-user))]- (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]- ;; Create user if not exists, otherwise update password- ;; PostgreSQL doesn't support CREATE USER IF NOT EXISTS, so we need to check first- (let [user-sql (if (user-exists? t-conn (:user read-user))- (format "ALTER USER %s WITH PASSWORD '%s'" quoted-user escaped-password)- (format "CREATE USER %s WITH PASSWORD '%s'" quoted-user escaped-password))]- (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]- (doseq [sql [;; PostgreSQL supports IF NOT EXISTS for schemas- (format "CREATE SCHEMA IF NOT EXISTS %s" quoted-schema)- user-sql- ;; Schema-level grant only (Postgres' two schema privileges):- ;; USAGE - access the schema- ;; CREATE - create tables in it- ;; Table DML comes from ownership (the user owns the tables it creates).- (format "GRANT USAGE, CREATE ON SCHEMA %s TO %s" quoted-schema quoted-user)- ;; role membership to admin so DROP OWNED BY works during cleanup- (format "GRANT %s TO CURRENT_USER" quoted-user)]]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/scrub-exceptions (driver.u/batch-exception t) [(:password read-user) escaped-password])))))))- nil))--(defmethod driver/destroy-workspace-isolation! :postgres- [_driver database workspace]- (let [schema-name (:schema workspace)- username (-> workspace :database_details :user)- quoted-schema (quote-schema schema-name)- quoted-user (quote-field username)]- (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 (cond-> [(format "DROP SCHEMA IF EXISTS %s CASCADE" quoted-schema)]- (user-exists? t-conn username)- (into [(format "DROP OWNED BY %s" quoted-user)- (format "DROP USER IF EXISTS %s" quoted-user)]))]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t))))))))--(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 [quoted-user (quote-field username)- source-schemas (set schemas)]- (mapcat (fn [s]- (let [quoted-schema (quote-schema s)]- [(format "GRANT USAGE ON SCHEMA %s TO %s" quoted-schema quoted-user)- (format "GRANT SELECT ON ALL TABLES IN SCHEMA %s TO %s" quoted-schema quoted-user)- (format "ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT SELECT ON TABLES TO %s"- quoted-schema quoted-user)]))- source-schemas)))--(defmethod driver/grant-workspace-read-access! :postgres- [driver database workspace schemas]- (let [username (-> workspace :database_details :user)- 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.- ;; read-only probes — one connection, no transaction needed- _ (sql-jdbc.execute/do-with-connection-with-options- driver database nil- (fn [^Connection conn]- (let [check-conn {:connection conn}]- (doseq [s source-schemas]- (assert-no-public-create-grant! check-conn s)- (assert-has-usage-grant-option! check-conn s)- (assert-has-grant-option! check-conn s)- (assert-can-alter-default-privileges! 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]- (.addBatch ^Statement stmt ^String sql))- (try- (.executeBatch ^Statement stmt)- (catch Throwable t- (throw (driver.u/batch-exception t))))))))-(defmethod driver/llm-sql-dialect-resource :postgres [_]"metabot/prompts/dialects/postgresql.md")diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.cljindex 94b0c7a9801..aaa6172b82e 100644--- a/src/metabase/driver/sql/query_processor.clj+++ b/src/metabase/driver/sql/query_processor.clj@@ -2086,7 +2086,7 @@(defmethod ->honeysql [:sql :metadata/table][driver table];; `: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+ ;; A 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.@@ -2346,16 +2346,15 @@(defmethod driver/compile-transform :sql[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+ ;; If `:output-db` is populated, qualify the output table with that DB+ ;; so the CTAS lands in that database. The target 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.+ ;; keyword shape; supporting those would 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))))diff --git a/src/metabase/driver/sql_jdbc.clj b/src/metabase/driver/sql_jdbc.cljindex fe038d1b330..c5c16128ea8 100644--- a/src/metabase/driver/sql_jdbc.clj+++ b/src/metabase/driver/sql_jdbc.clj@@ -19,7 +19,6 @@[metabase.driver.sql-jdbc.sync.describe-database :as sql-jdbc.describe-database][metabase.driver.sql.query-processor :as sql.qp][metabase.driver.sync :as driver.s]- [metabase.driver.util :as driver.u][metabase.lib.schema.metadata :as lib.schema.metadata][metabase.util.honey-sql-2 :as h2x][metabase.util.malli :as mu]@@ -405,9 +404,3 @@(->> (.getMetaData conn)sql-jdbc.describe-database/all-schemas(m/find-first #(= % schema))))))--(defmethod driver/workspace-isolation-details :sql-jdbc- [_driver _database workspace]- {:schema (driver.u/workspace-isolation-namespace-name workspace)- :database_details {:user (driver.u/workspace-isolation-user-name workspace)- :password (driver.u/random-workspace-password)}})diff --git a/src/metabase/driver/sql_jdbc/connection.clj b/src/metabase/driver/sql_jdbc/connection.cljindex 5bce228a9b5..866179aab3c 100644--- a/src/metabase/driver/sql_jdbc/connection.clj+++ b/src/metabase/driver/sql_jdbc/connection.clj@@ -7,7 +7,6 @@[metabase.driver :as driver][metabase.driver-api.core :as driver-api][metabase.driver.connection :as driver.conn]- [metabase.driver.connection.workspaces :as driver.w][metabase.driver.settings :as driver.settings][metabase.driver.sql-jdbc.connection.ssh-tunnel :as ssh][metabase.driver.util :as driver.u]@@ -20,9 +19,7 @@^{:clj-kondo/ignore [:discouraged-namespace]}[toucan2.core :as t2])(:import- (com.google.common.cache Cache CacheBuilder RemovalNotification)(com.mchange.v2.c3p0 DataSources)- (java.util.concurrent TimeUnit)(javax.sql DataSource)(org.apache.logging.log4j Level)))@@ -229,34 +226,12 @@(driver-api/destroy-connection-pool! pool-spec)(ssh/close-tunnel! pool-spec))-(def ^:private swapped-pool-ttl-minutes- "Time-to-live in minutes for swapped connection pools.- Pools are evicted after this duration of inactivity (no access)."- 15)--(defonce ^:private ^{:doc "A map of our currently open connection pools for canonical (non-swapped) connections,- keyed by pool cache key `[database-id, connection-type]`. Each database+connection-type pair has at most one canonical pool."}+(defonce ^:private ^{:doc "A map of our currently open connection pools,+ keyed by pool cache key `[database-id, connection-type]`. Each database+connection-type pair has at most one pool."}pool-cache-key->connection-pool(atom {}))-(defonce ^:private ^Cache ^{:doc "A Guava cache of swapped connection pools with TTL-based expiration,- keyed by [database-id, details-hash]. Pools are evicted after inactivity. Different swapped- details for the same database get separate pools, while identical details share a pool."}- swapped-connection-pools- (.. (CacheBuilder/newBuilder)- (expireAfterAccess swapped-pool-ttl-minutes TimeUnit/MINUTES)- (removalListener- (reify com.google.common.cache.RemovalListener- (^void onRemoval [_ ^RemovalNotification rn]- (let [[database-id _details-hash] (.getKey rn)- pool-spec (.getValue rn)]- (log/debugf "Evicting swapped pool for database %d (cause: %s)"- database-id (.getCause rn))- (destroy-pool! database-id pool-spec)- nil))))- (build)))--(defonce ^:private ^{:doc "A map of DB details hash values for the canonical (non-swapped) details,+(defonce ^:private ^{:doc "A map of DB details hash values for the connection details,keyed by pool cache key `[database-id, connection-type]`.This is used to detect when database details have been updated in the application database."}pool-cache-key->jdbc-spec-hash@@ -282,9 +257,7 @@"Atomically update the canonical connection pool for a database and connection type.Use this function instead of modifying connection pool atoms directly because it properly closes down old pools in a- thread-safe way, ensuring no more than one canonical pool is ever open for a specific database and connection type.-- This function is only for canonical (non-swapped) pools. Swapped pools are managed by the Guava cache."+ thread-safe way, ensuring no more than one canonical pool is ever open for a specific database and connection type."[cache-key details-hash pool-spec-or-nil]{:pre [(vector? cache-key) (some? details-hash)]}(let [[database-id _connection-type] cache-key@@ -300,30 +273,23 @@nil)(defn invalidate-pool-for-db!- "Invalidates all connection pools for the given database (canonical -- all connection types -- and swapped) by closing+ "Invalidates all connection pools for the given database (all connection types) by closingthem and removing from cache."[database](let [db-id (u/the-id database)canonical-keys (mapv (fn [conn-type] [db-id conn-type])driver.conn/connection-types)pool-map @pool-cache-key->connection-pool- canonical-count (count (filter pool-map canonical-keys))- ;; Find all swapped pool keys for this database (keys are [db-id, details-hash] tuples)- swapped-keys (filter (fn [[cached-db-id _details-hash]]- (= cached-db-id db-id))- (keys (.asMap ^Cache swapped-connection-pools)))]- (log/infof "Invalidating connection pools for database %d (canonical count: %d, swapped count: %d)"- db-id canonical-count (count swapped-keys))+ canonical-count (count (filter pool-map canonical-keys))]+ (log/infof "Invalidating connection pools for database %d (canonical count: %d)"+ db-id canonical-count);; Clear canonical pools for both connection types(doseq [cache-key canonical-keys:let [[old-map] (swap-vals! pool-cache-key->connection-pool dissoc cache-key)pool-spec (get old-map cache-key)]:when pool-spec](destroy-pool! db-id pool-spec)- (swap! pool-cache-key->jdbc-spec-hash dissoc cache-key))- ;; Clear all swapped pools for this DB (removal listener will call destroy-pool!)- (doseq [cache-key swapped-keys]- (.invalidate ^Cache swapped-connection-pools cache-key))))+ (swap! pool-cache-key->jdbc-spec-hash dissoc cache-key))))(defn- log-ssh-tunnel-reconnect-msg! [db-id](log/warn (u/format-color :red "ssh tunnel for database %s looks closed; marking pool invalid to reopen it" db-id))@@ -376,46 +342,6 @@nil)pool-spec))-(defn- swap-cache-key- "Generate cache key for swapped pools: [database-id, jdbc-spec-hash].- Different details (e.g., different user credentials) will produce different keys,- ensuring each unique swap gets its own connection pool.- Uses jdbc-spec-hash for stable hashing based on the actual connection spec."- [{:keys [id] :as db}]- [id (jdbc-spec-hash db)])--(defn- get-swapped-pool- "Get or create a swapped connection pool from the Guava cache.- Validates that existing pools are still valid (password not expired, tunnel open).- Pools are keyed by [database-id, details-hash] so different swapped details get separate pools.- The `db` should already have swapped details applied."- [{:keys [id] :as db}]- (let [cache-key (swap-cache-key db)- existing (.getIfPresent ^Cache swapped-connection-pools cache-key)]- (cond- ;; No existing pool - create a new one- (nil? existing)- (.get ^Cache swapped-connection-pools- cache-key- (reify java.util.concurrent.Callable- (call [_]- (log/debugf "Creating new swapped connection pool for database %d with spec hash %d"- id (second cache-key))- (create-pool! db))))-- ;; Existing pool is invalid (password expired, tunnel closed) - invalidate and recreate- (nil? (get-pool-if-valid existing id true))- (do- (log/debugf "Swapped connection pool for database %d is invalid, recreating" id)- (.invalidate ^Cache swapped-connection-pools cache-key)- (recur db))-- ;; Existing pool is valid - use it- :else- (do- (log/debugf "Using existing swapped connection pool for database %d" id)- existing))))-(defn- canonical-pool-hash-changed?"Check if the canonical pool's hash differs from the expected hash.Handles stale DatabaseInstance by re-fetching from app DB.@@ -454,11 +380,7 @@(defn db->pooled-connection-spec"Return a JDBC connection spec that includes a c3p0 `ComboPooledDataSource`. These connection pools are cached so wedon't create multiple ones for the same DB and connection type. The connection type follows the current- connection context — use [[driver.conn/with-write-connection]] to get a write connection pool.-- When [[metabase.driver/with-swapped-connection-details]] is active for a database, the database details are- modified before creating the connection pool. Swapped pools are stored in a separate Guava cache with TTL-based- eviction (see [[swapped-pool-ttl-minutes]]), while canonical pools are stored in the main atom cache."+ connection context — use [[driver.conn/with-write-connection]] to get a write connection pool."[db-or-id-or-spec](when-let [db-id (u/id db-or-id-or-spec)](driver-api/check-allowed-access! db-id))@@ -474,10 +396,7 @@(driver-api/with-metadata-provider database-id(driver-api/database (driver-api/metadata-provider))))cache-key (pool-cache-key db)- ;; Check for workspace detail swaps (for pool routing: canonical atom vs Guava TTL cache).- ;; The actual swap is applied inside effective-details, not here.- has-swap? (driver.w/has-connection-swap? database-id)- ;; Calculate hash from effective details (includes write-connection merge + workspace swap)+ ;; Calculate hash from effective details (includes write-connection merge)details-hash (jdbc-spec-hash db)](driver.conn/track-connection-acquisition! (driver.conn/effective-details db))(cond@@ -487,11 +406,6 @@(or (:is-audit db) (get-in db [:details :is-audit-dev])){:datasource (driver-api/data-source)}- ;; Swapped pool: use Guava cache with TTL- has-swap?- (get-swapped-pool db)-- ;; Canonical pool: use atom cache:else(or;; we have an existing valid pool for this database, so use itdiff --git a/src/metabase/driver/sync.clj b/src/metabase/driver/sync.cljindex 020968f4d7e..d2ef36e2386 100644--- a/src/metabase/driver/sync.clj+++ b/src/metabase/driver/sync.clj@@ -3,8 +3,7 @@(:require[clojure.string :as str][metabase.driver.connection :as driver.conn]- [metabase.driver.util :as driver.u]- [metabase.workspaces.core :as workspaces])+ [metabase.driver.util :as driver.u])(:import(java.util.regex Pattern)))@@ -75,20 +74,11 @@(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 (`*`).-- 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."+ comma-separated, and can contain wildcard characters (`*`)."{: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]- (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)))))+ (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 9970ff3c358..f7c6d714776 100644--- a/src/metabase/driver/util.clj+++ b/src/metabase/driver/util.clj@@ -17,7 +17,6 @@[metabase.premium-features.core :as premium-features][metabase.query-processor.error-type :as qp.error-type]^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store]- [metabase.system.core :as system][metabase.util :as u][metabase.util.i18n :refer [deferred-tru trs]][metabase.util.log :as log]@@ -780,118 +779,6 @@db-details))db-details)))-;;; +----------------------------------------------------------------------------------------------------------------+-;;; | Workspace Isolation Utilities |-;;; +----------------------------------------------------------------------------------------------------------------+--(defn- instance-uuid-slug- "Create a slug from the site UUID, taking the first character of each section."- [site-uuid-string]- (->> (str/split site-uuid-string #"-")- (map first)- (apply str)))--;; WARNING: Do NOT change this prefix. It is baked into existing workspace schemas in production databases.-;; Changing it would require extreme care for backwards compatibility. If you need to match against this-;; prefix, use [[workspace-isolated-schema?]] or [[workspace-isolated-schema-clause]] rather than hardcoding it.-(def ^:private workspace-isolated-prefix "mb__isolation_")-;; Escaped for SQL LIKE: underscores are wildcards, so we escape them with backslash.-;; The default escape character works across all supported app-database engines (H2, Postgres, MySQL, MariaDB),-;; so an explicit ESCAPE clause is not necessary.-(def ^:private workspace-isolated-like-pattern- (str (str/replace workspace-isolated-prefix "_" "\\_") "%"))--(defn workspace-isolated-schema?- "Returns true if the given schema name belongs to a workspace isolation namespace."- [schema-name]- (and (some? schema-name)- (str/starts-with? schema-name workspace-isolated-prefix)))--(defn workspace-isolated-schema-clause- "Returns a HoneySQL [:like column pattern] clause that matches workspace isolation schemas.- `column` is typically `:schema`."- [column]- [:like column workspace-isolated-like-pattern])--(defn workspace-isolation-namespace-name- "Generate namespace/database name for workspace isolation following mb__isolation_<slug>_<workspace-id> pattern.- Uses 'namespace' as the generic term that maps to 'schema' in Postgres, 'database' in ClickHouse, etc."- [workspace]- (assert (some? (:id workspace)) "Workspace must have an :id")- (let [instance-slug (instance-uuid-slug (str (system/site-uuid)))- clean-workspace-id (str/replace (str (:id workspace)) #"[^a-zA-Z0-9]" "_")]- (format "%s%s_%s" workspace-isolated-prefix instance-slug clean-workspace-id)))--(defn workspace-isolation-user-name- "Generate username for workspace isolation."- [workspace]- (let [instance-slug (instance-uuid-slug (str (system/site-uuid)))]- (format "%s%s_%s" workspace-isolated-prefix instance-slug (:id workspace))))--(def ^:private workspace-password-char-sets- "Character sets for password generation. Cycles through these to ensure representation from each."- ["ABCDEFGHJKLMNPQRSTUVWXYZ"- "abcdefghjkmnpqrstuvwxyz"- "123456789"- "!#$%&*+-="])--(defn random-workspace-password- "Generate a random password suitable for most database engines.- Ensures the password contains characters from all sets (uppercase, lowercase, digits, special)- by cycling through the character sets. Result is shuffled for randomness."- []- (->> (cycle workspace-password-char-sets)- (take (+ 32 (rand-int 32)))- (map rand-nth)- shuffle- (apply str)))--(defn- redact-msg- "Replace all `secrets` in `msg` with `****`."- [msg secrets]- (reduce (fn [s secret] (str/replace s secret "****")) (or msg "") secrets))--(defn batch-exception- "The per-statement exception hiding inside a `java.sql.BatchUpdateException`,- or `t` itself for any other throwable. Rethrow this instead of the raw batch- exception so failures read as the plain server error rather than `Batch- entry N ... was aborted: ... Call getNextException to see other errors in- the batch.`-- Connectors chain the underlying exception differently: pgjdbc (and its- Redshift fork) via `.getNextException`, the MariaDB connector via- `.getCause`. Connectors that chain nothing (e.g. mssql-jdbc) don't wrap the- message in the first place, so the batch exception is returned as is."- ^Throwable [^Throwable t]- (if (instance? java.sql.BatchUpdateException t)- (let [^java.sql.BatchUpdateException bue t]- (or (.getNextException bue)- (.getCause bue)- bue))- t))--(defn scrub-exceptions- "Scrub `secrets` from the exception message and cause chain of `t`. Returns a new- exception with every occurrence of each secret replaced by `****`. Use this to prevent- credentials embedded in DDL SQL from leaking into logs via exception messages."- [^Throwable t secrets]- (let [msg (redact-msg (ex-message t) secrets)- cause (some-> (.getCause t) (scrub-exceptions secrets))]- (cond- (instance? clojure.lang.ExceptionInfo t)- (ex-info msg (ex-data t) cause)-- (instance? java.sql.SQLException t)- (let [^java.sql.SQLException sql-ex t- next-ex (some-> (.getNextException sql-ex) (scrub-exceptions secrets))]- (doto (java.sql.SQLException. msg (.getSQLState sql-ex) (.getErrorCode sql-ex) cause)- (.setStackTrace (.getStackTrace t))- (cond-> next-ex (.setNextException next-ex))))-- :else- (doto (Exception. msg cause)- (.setStackTrace (.getStackTrace t))))))-;;; +----------------------------------------------------------------------------------------------------------------+;;; | Macaw parsing helpers |;;; +----------------------------------------------------------------------------------------------------------------+diff --git a/src/metabase/lib/schema/metadata.cljc b/src/metabase/lib/schema/metadata.cljcindex 947276eedc6..39943413ed9 100644--- a/src/metabase/lib/schema/metadata.cljc+++ b/src/metabase/lib/schema/metadata.cljc@@ -776,9 +776,8 @@[:display-name {: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.+ ;; SQL Server / Snowflake `db.schema.table`). Sync doesn't populate it on standard+ ;; reads — only cross-DB rewriters fill it.[:db {:optional true} [:maybe ::lib.schema.common/non-blank-string]]])(mr/def ::databasediff --git a/src/metabase/model_persistence/api.clj b/src/metabase/model_persistence/api.cljindex aeb17978c44..9e17f4cae29 100644--- a/src/metabase/model_persistence/api.clj+++ b/src/metabase/model_persistence/api.clj@@ -22,7 +22,6 @@[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)@@ -151,7 +150,6 @@(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 21cf56883ae..97db0e005e2 100644--- a/src/metabase/models/resolution.clj+++ b/src/metabase/models/resolution.clj@@ -123,7 +123,6 @@:model/SsoRelayState metabase-enterprise.sso.models.relay-state: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@@ -147,9 +146,7 @@: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/Workspace metabase-enterprise.workspaces.models.workspace- :model/WorkspaceDatabase metabase-enterprise.workspaces.models.workspace-database})+ :model/ViewLog metabase.view-log.models.view-log});;; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!;;; !! !!diff --git a/src/metabase/premium_features/core.clj b/src/metabase/premium_features/core.cljindex fce3a353eb0..47f95a6e0fd 100644--- a/src/metabase/premium_features/core.clj+++ b/src/metabase/premium_features/core.clj@@ -92,7 +92,6 @@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 8f6d0a6b370..f510e04565e 100644--- a/src/metabase/premium_features/settings.clj+++ b/src/metabase/premium_features/settings.clj@@ -380,10 +380,6 @@"Should we allow users to view database schemas as ER diagrams?":schema-viewer)-(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?)@@ -441,7 +437,6 @@: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 50a62a1b451..fe7dab5019d 100644--- a/src/metabase/query_processor/api.clj+++ b/src/metabase/query_processor/api.clj@@ -34,7 +34,6 @@[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]))@@ -199,26 +198,19 @@;;#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}(api.macros/defendpoint :post "/native"- "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."+ "Fetch a native version of an MBQL query."[_route-params_query-params{:keys [database pretty] :as query} :- [:map[:database ms/PositiveInt][:pretty {:default true} [:maybe :boolean]]]]- (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))))))+ (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 c55f2ddca23..60eca1e56ea 100644--- a/src/metabase/query_processor/execute.clj+++ b/src/metabase/query_processor/execute.clj@@ -40,7 +40,6 @@;; 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 1fe69e8820b..7470526d70b 100644--- a/src/metabase/query_processor/middleware/enterprise.clj+++ b/src/metabase/query_processor/middleware/enterprise.clj@@ -77,41 +77,8 @@[query]query)-(defenterprise apply-workspace-remapping- "**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/query_processor/preprocess.clj b/src/metabase/query_processor/preprocess.cljindex fa1e171c25a..97d81aa8d4c 100644--- a/src/metabase/query_processor/preprocess.clj+++ b/src/metabase/query_processor/preprocess.clj@@ -108,7 +108,6 @@#'optimize-temporal-clauses/optimize-temporal-clauses#'limit/add-default-limit#'qp.middleware.enterprise/apply-download-limit- #'qp.middleware.enterprise/apply-workspace-remapping#'check-features/check-features])(def ^:private ^Long slow-middleware-warning-threshold-msdiff --git a/src/metabase/sample_data/impl.clj b/src/metabase/sample_data/impl.cljindex 1f7a7bc0046..d09cb8c71ed 100644--- a/src/metabase/sample_data/impl.clj+++ b/src/metabase/sample_data/impl.clj@@ -18,7 +18,7 @@(def ^String sample-database-name"Canonical name of the bundled Sample Database. Shared with consumers that need to- identify or validate the sample DB (e.g. workspace-manager config import)."+ identify or validate the sample DB.""Sample Database")(def ^:private ^String sqlite-sample-database-filename "sample-database.sqlite")diff --git a/src/metabase/sql_parsing/core.clj b/src/metabase/sql_parsing/core.cljindex 7a1991848fd..896d7cef50b 100644--- a/src/metabase/sql_parsing/core.clj+++ b/src/metabase/sql_parsing/core.clj@@ -270,8 +270,6 @@(defn simple-query?"Check if SQL is a simple SELECT without LIMIT, OFFSET, or CTEs.- Used by Workspaces to determine if automatic checkpoints can be inserted.-Parameters:- dialect: SQLGlot dialect string (e.g., \"postgres\", \"mysql\"), or nil for default- sql: The SQL query string to checkdiff --git a/src/metabase/sql_tools/core.clj b/src/metabase/sql_tools/core.cljindex d1e681b0d74..ebe05b98722 100644--- a/src/metabase/sql_tools/core.clj+++ b/src/metabase/sql_tools/core.clj@@ -143,7 +143,7 @@SECURITY: Replacement values are injected into the SQL AST as identifier nameswithout sanitization. Callers MUST ensure replacement values are system-generated- (e.g., workspace isolation schema names, database metadata). Never pass+ (e.g., database metadata). Never passuser-controlled input as replacement values."([driver sql-string replacements](replace-names driver sql-string replacements {}))@@ -165,7 +165,6 @@(mu/defn simple-query? :- ::simple-query-result"Check if SQL string is a simple SELECT (no LIMIT, OFFSET, or CTEs).- Used by Workspaces to determine if automatic checkpoints can be inserted.Returns a map with:- `:is_simple` - boolean indicating if query is simplediff --git a/src/metabase/sync/fetch_metadata.clj b/src/metabase/sync/fetch_metadata.cljindex 883ef04e529..b2dec8f3e8b 100644--- a/src/metabase/sync/fetch_metadata.clj+++ b/src/metabase/sync/fetch_metadata.clj@@ -5,7 +5,6 @@(:require[clojure.set :as set][metabase.driver :as driver]- [metabase.driver.connection :as driver.conn][metabase.driver.util :as driver.u][metabase.lib-be.core :as lib-be][metabase.sync.interface :as i]@@ -13,37 +12,8 @@[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}@@ -65,13 +35,9 @@"Add nested-field-columns for table to set of fields."[fields database table](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)]- (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))))))))+ (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)))))(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@@ -81,16 +47,12 @@table :- i/TableInstance](log-if-error "table-fields-metadata"(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)))))))))+ (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))))))(defn- describe-fields-using-describe-table"Replaces [[metabase.driver/describe-fields]] for drivers that haven't implemented it. Uses [[driver/describe-table]]@@ -132,48 +94,22 @@(eduction (map #(mu.fn/validate-output {} i/FieldMetadataEntry %)))))))(mu/defn fk-metadata- "Effectively a wrapper for [[metabase.driver/describe-fks]] that also validates the output against the schema.-- 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."+ "Effectively a wrapper for [[metabase.driver/describe-fks]] that also validates the output against the schema."[database :- i/DatabaseInstance& {:as args} :- ::driver/describe-fks.options](log-if-error "fk-metadata"- (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))]+ (let [driver (driver.u/database->driver database)](when (driver.u/supports? driver :metadata/key-constraints database)- (let [ ;; 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 []- (into [] (driver/describe-fks driver- (lib-be/instance->metadata database :metadata/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 %)))))))))+ (cond->> (driver/describe-fks driver+ (lib-be/instance->metadata database :metadata/database)+ args)+ ;; 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`. 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."+ "Get information about the indexes belonging to `table`."[database :- i/DatabaseInstancetable :- i/TableInstance](log-if-error "index-metadata"- (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))))))+ (driver/describe-table-indexes (driver.u/database->driver database) database table)))diff --git a/src/metabase/sync/sync_metadata/tables.clj b/src/metabase/sync/sync_metadata/tables.cljindex 55155d02a65..2156ee97094 100644--- a/src/metabase/sync/sync_metadata/tables.clj+++ b/src/metabase/sync/sync_metadata/tables.clj@@ -21,7 +21,6 @@[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)@@ -427,8 +426,7 @@Captures any `_metabase_metadata` table(s) from the raw batch (for the downstream metabase-metadatastep), then narrows the batch -- dropping tables we never sync, collapsing duplicate `(name, schema)`,- dropping names too long for the app DB, and dropping workspace-isolated tables (they back canonical- Tables via remap, so they get no `:model/Table` of their own) -- and creates/reactivates new tables+ and dropping names too long for the app DB -- and creates/reactivates new tablesand updates metadata on existing ones. The reconcile is error-handled per batch: on failure we log,skip the batch, and mark the sync incomplete so retirement is skipped."[database :- i/DatabaseInstance@@ -438,8 +436,7 @@(letfn [(reconcile! [](let [tables (as-> batch <>(into #{} (comp (remove ignore-table?) (m/distinct-by table-name+schema)) <>)- (remove-tables-with-too-long-names database <>)- (ws.table-remapping/filter-workspace-side-tables <> (u/the-id database)))+ (remove-tables-with-too-long-names database <>))existing (existing-tables-by-name+schema database tables)existing-row #(get existing (table-name+schema %))already-active (into #{} (filter #(:active (existing-row %))) tables)@@ -479,10 +476,9 @@(mu/defn- retire-unseen-tables! :- :int"Retire active app-DB tables the warehouse no longer reports. `seen` holds the `:model/Table` id of- every table reconciled this sync (accumulated by [[sync-table-batch!]], including the injected- workspace-canonical tuples). Streams the active app-DB table ids and retires, a batch at a time, the- ones not in `seen` -- so we never hold either the full active set or the full retire set in memory.- Returns the number retired."+ every table reconciled this sync (accumulated by [[sync-table-batch!]]). Streams the active app-DB+ table ids and retires, a batch at a time, the ones not in `seen` -- so we never hold either the full+ active set or the full retire set in memory. Returns the number retired."[database :- i/DatabaseInstanceseen :- [:set ::lib.schema.id/table]](transduce@@ -523,7 +519,6 @@{:created 0, :updated 0, :complete? true:seen (transient #{}), :metabase-metadata []}(:tables db-metadata))- context (reconcile-batch context (ws.table-remapping/inject-workspace-canonical-tuples [] (u/the-id database))){:keys [created updated complete?]} context](when-let [holder (:metabase-metadata-tables db-metadata)](vreset! holder (:metabase-metadata context)))diff --git a/src/metabase/transforms/execute.clj b/src/metabase/transforms/execute.cljindex 6830d6cc8a6..b69128fe00d 100644--- a/src/metabase/transforms/execute.clj+++ b/src/metabase/transforms/execute.clj@@ -1,37 +1,20 @@(ns metabase.transforms.execute(:require[metabase.indexes.models.table-index :as table-index]- [metabase.transforms-base.interface :as transforms-base.i][metabase.transforms-base.util :as transforms-base.u][metabase.transforms.interface :as transforms.i]- [metabase.transforms.query-impl :as transforms.query-impl]))+ ;; load the `transforms.i/execute!` `:query` method+ [metabase.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`.-- Also hydrates the declared indexes and their request ids onto the target so the base reads them off+ "Hydrates the declared indexes and their request ids onto the target so the base reads them off`(:indexes target)` and `(:index-request-ids target)` at every table-creation seam, and stashes`:full-incremental-run?` so that (DB-backed) decision is made once and stays stable for the whole run."[transform](let [requests (table-index/select-applicable-for-transform (:id 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)+ (-> transform(assoc-in [:target :indexes] (mapv :structured requests))(assoc-in [:target :index-request-ids] (into [] (keep :id) requests))(assoc :full-incremental-run? (transforms-base.u/full-incremental-run? transform)))))diff --git a/src/metabase/transforms/query_impl.clj b/src/metabase/transforms/query_impl.cljindex 9efc6fbf2c6..90f1f4ac868 100644--- a/src/metabase/transforms/query_impl.clj+++ b/src/metabase/transforms/query_impl.clj@@ -3,7 +3,6 @@[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]@@ -15,26 +14,10 @@(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 on-start user-id parent-run]}]- ;; `: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/core.clj b/src/metabase/transforms_base/core.cljindex df3f8e654d7..ffa53d70df9 100644--- a/src/metabase/transforms_base/core.clj+++ b/src/metabase/transforms_base/core.clj@@ -18,7 +18,7 @@"Execute transform and return result map without writing transform_run rows.This is the main entry point for base transform execution. Use this when you- want to run a transform without database tracking (e.g., from workspaces).+ want to run a transform without database tracking.Performs the core execution via `execute-base!`, then runs post-processing(sync, events, secondary indexes) via `complete-execution!`.diff --git a/src/metabase/transforms_base/query.clj b/src/metabase/transforms_base/query.cljindex 69290cc4b01..02448752a0f 100644--- a/src/metabase/transforms_base/query.clj+++ b/src/metabase/transforms_base/query.clj@@ -12,7 +12,6 @@[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]@@ -106,17 +105,7 @@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 %)))+ compiled-query (transforms-base.u/compile-source transform source-range-params)transform-details {:db-id db:database database:transform-id id@@ -126,7 +115,7 @@: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+ ;; SQL Server/BigQuery) AND a cross-DB rewriter 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`.diff --git a/src/metabase/transforms_base/schema.clj b/src/metabase/transforms_base/schema.cljindex 2640d909971..b7a150fd5ca 100644--- a/src/metabase/transforms_base/schema.clj+++ b/src/metabase/transforms_base/schema.clj@@ -35,7 +35,6 @@(mr/def ::transform"A transform map as expected by execute-base! implementations."[:map- ;; The :id is only present for Global transforms, and missing for Workspace transforms.[:id {:optional true} pos-int?][:source [:map [:type [:or :string :keyword]]]][:target ::transform-target]diff --git a/src/metabase/transforms_base/util.clj b/src/metabase/transforms_base/util.cljindex de9b65636a5..79baec35366 100644--- a/src/metabase/transforms_base/util.clj+++ b/src/metabase/transforms_base/util.clj@@ -32,7 +32,6 @@[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)@@ -468,43 +467,23 @@{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?]}]- (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)))))+ (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."@@ -532,37 +511,14 @@;; TODO this should probably be a function in the sync module(t2/update! :model/Table (:id table) {:active false})))-(defn- isolated-drop-target- "Resolve `target` to the physical warehouse table to drop.-- On a workspace child the canonical target is backed by an isolation-namespace copy recorded- in `table_remapping`; the workspace user can only DROP inside its isolation namespace, so the- drop must hit that copy — not the canonical table. Looks the copy up via the general- canonical→isolated workspace hook (symmetric with [[canonicalize-target]], which uses the- inverse). The isolated namespace lives in `:schema` for schema-based drivers and in `:db` for- MySQL; collapse to the single `:schema` slot the driver drop path qualifies on. Off-workspace- the hook returns nil and `target` is dropped unchanged."- [db-id target]- (let [lookup-spec {:db (:db target) :schema (:schema target) :name (:name target)}]- (if-let [{:keys [db schema name]} (ws.table-remapping/workspace-remap-schema+name db-id lookup-spec)]- (assoc target :schema (or schema db) :name name)- target)))-(defn delete-target-table!- "Drop a transform's output table and deactivate its app-db Table row.-- In workspace-isolation mode the transform writes to an isolation-namespace copy, so the drop- resolves the canonical `:target` to that copy (see [[isolated-drop-target]]); dropping the- canonical name would fail (the workspace user is read-only there) or hit the wrong table. The- app-db Table row that gets deactivated is always the canonical `:target`, since that is what- sync surfaces. Off-workspace the two are identical."+ "Drop a transform's output table and deactivate its app-db Table row."[{:keys [id target], :as transform}](when target(let [database-id (transforms-base.i/target-db-id transform)](when database-id(if-let [{driver :engine :as database} (t2/select-one :model/Database database-id)]- (let [drop-target (->> (update target :type keyword)- (isolated-drop-target database-id))]+ (let [drop-target (update target :type keyword)](driver/drop-transform-target! driver database drop-target)(log/info "Deactivating target " (pr-str target) "for transform" id)(deactivate-table! database target))diff --git a/src/metabase/transforms_base/workspace_hooks.clj b/src/metabase/transforms_base/workspace_hooks.cljdeleted file mode 100644index ffeca8aa89d..00000000000--- a/src/metabase/transforms_base/workspace_hooks.clj+++ /dev/null@@ -1,18 +0,0 @@-(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 e0208be8b1c..e2a920ecaf8 100644--- a/src/metabase/upload/impl.clj+++ b/src/metabase/upload/impl.clj@@ -32,7 +32,6 @@[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)@@ -601,9 +600,6 @@: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.@@ -634,7 +630,6 @@[: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})))]@@ -992,7 +987,6 @@[: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 b28eb248f95..30ae4aa8b2e 100644--- a/src/metabase/warehouses/models/database.clj+++ b/src/metabase/warehouses/models/database.clj@@ -161,16 +161,6 @@[_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)-(defenterprise mark-transforms-stale-on-database-delete!"Hook called from the `:model/Database` before-delete. Transforms whose `source_database_id`is about to be SET NULL by the FK action survive the delete (so the analyst can read the@@ -434,11 +424,6 @@(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);; Mark transforms with this DB as source as stale so dependency-diagnostics re-analyzes;; them after the row's gone. Must run BEFORE the FK SET NULL fires (which happens when;; the database row is deleted below) so we can still resolve the affected transforms bydiff --git a/src/metabase/warehouses_rest/api.clj b/src/metabase/warehouses_rest/api.cljindex 9465034d683..b22929a5aef 100644--- a/src/metabase/warehouses_rest/api.clj+++ b/src/metabase/warehouses_rest/api.clj@@ -1020,11 +1020,10 @@(database/sensitive-fields-for-db database))))))(def ^:private connection-marker-key->details-column- {:write-data-connection "write_data_details"- :admin-connection "admin_details"})+ {:write-data-connection "write_data_details"})(defn- validate-overlay-details!- "Common guardrails for overlay-details (write_data_details, admin_details).+ "Common guardrails for overlay-details (write_data_details).Throws 400 on violation."[existing-database overlay-details{:keys [marker-key article+noun hidden-fields-fn]}]@@ -1060,15 +1059,6 @@: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;;@@ -1078,7 +1068,7 @@[{:keys [id]} :- [:map[:id ms/PositiveInt]]_query-params- {:keys [name engine details write_data_details admin_details is_full_sync is_on_demand description caveats+ {:keys [name engine details write_data_details is_full_sync is_on_demand description caveatspoints_of_interest schedules auto_run_queries refingerprint cache_ttl settings provider_name]:as body}:- [:map@@ -1087,7 +1077,6 @@[: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]]@@ -1102,14 +1091,9 @@;; 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));; e2e tests run against the H2 sample database and need to toggle its settings (actions,;; table editing), so the guard is lifted when test endpoints are enabled@@ -1119,21 +1103,15 @@{:status-code 400})))_ (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))@@ -1145,16 +1123,9 @@(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-let [conn-error (or main-conn-error write-conn-error admin-conn-error)]+ (if-let [conn-error (or main-conn-error write-conn-error)];; failed to connect, return error{:status 400:body conn-error}@@ -1190,8 +1161,6 @@{:provider_name provider_name})(when (contains? body :write_data_details){:write_data_details write-data-details-with-secrets})- (when (contains? body :admin_details)- {:admin_details admin-details-with-secrets})(when schedules(sync.schedules/schedule-map->cron-strings schedules)))pending-db (merge existing-database updates)]@@ -1230,8 +1199,7 @@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 :admin_details #(merge incoming-admin-details %))))))))+ (m/update-existing :write_data_details #(merge incoming-write-data-details %))))))));;; -------------------------------------------- DELETE /api/database/:id --------------------------------------------diff --git a/src/metabase/workspaces/core.clj b/src/metabase/workspaces/core.cljdeleted file mode 100644index ba30d97aee6..00000000000--- a/src/metabase/workspaces/core.clj+++ /dev/null@@ -1,105 +0,0 @@-(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 `instance-workspace` setting."- (:require- [metabase.premium-features.core :refer [defenterprise]]- [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 `instance-workspace` setting 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)--(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.cljdeleted file mode 100644index edd5dbd7692..00000000000--- a/src/metabase/workspaces/table_remapping.clj+++ /dev/null@@ -1,131 +0,0 @@-(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/api/macros/scope_test.clj b/test/metabase/api/macros/scope_test.cljindex ca58d0cbb08..c5f51230e79 100644--- a/test/metabase/api/macros/scope_test.clj+++ b/test/metabase/api/macros/scope_test.clj@@ -21,27 +21,27 @@(testing "blank string returns nil"(is (nil? (scope/parse-scopes " "))))(testing "single scope"- (is (= #{"agent:workspaces"}- (scope/parse-scopes "agent:workspaces"))))+ (is (= #{"agent:reports"}+ (scope/parse-scopes "agent:reports"))))(testing "multiple scopes"- (is (= #{"agent:workspaces" "agent:queries" "read:users"}- (scope/parse-scopes "agent:workspaces agent:queries read:users"))))+ (is (= #{"agent:reports" "agent:queries" "read:users"}+ (scope/parse-scopes "agent:reports agent:queries read:users"))))(testing "trims whitespace"- (is (= #{"agent:workspaces"}- (scope/parse-scopes " agent:workspaces "))))+ (is (= #{"agent:reports"}+ (scope/parse-scopes " agent:reports "))))(testing "wildcard scope"(is (= #{"*"}(scope/parse-scopes "*")))))(deftest ^:parallel scope-satisfied?-test(testing "exact match"- (is (scope/scope-satisfied? #{"agent:workspaces"} "agent:workspaces"))- (is (not (scope/scope-satisfied? #{"agent:queries"} "agent:workspaces"))))+ (is (scope/scope-satisfied? #{"agent:reports"} "agent:reports"))+ (is (not (scope/scope-satisfied? #{"agent:queries"} "agent:reports"))))(testing "global wildcard"- (is (scope/scope-satisfied? #{"*"} "agent:workspaces"))+ (is (scope/scope-satisfied? #{"*"} "agent:reports"))(is (scope/scope-satisfied? #{"*"} "anything")))(testing "hierarchical wildcard"- (is (scope/scope-satisfied? #{"agent:*"} "agent:workspaces"))+ (is (scope/scope-satisfied? #{"agent:*"} "agent:reports"))(is (scope/scope-satisfied? #{"agent:*"} "agent:queries"))(is (not (scope/scope-satisfied? #{"agent:*"} "read:users"))))(testing "deeper hierarchy"@@ -49,21 +49,21 @@(is (scope/scope-satisfied? #{"a:b:*"} "a:b:c"))(is (not (scope/scope-satisfied? #{"a:b:*"} "a:x:c"))))(testing "empty scopes set never satisfies"- (is (not (scope/scope-satisfied? #{} "agent:workspaces"))))+ (is (not (scope/scope-satisfied? #{} "agent:reports"))))(testing "multiple scopes in set"- (is (scope/scope-satisfied? #{"read:users" "agent:workspaces"} "agent:workspaces"))- (is (not (scope/scope-satisfied? #{"read:users" "agent:queries"} "agent:workspaces")))))+ (is (scope/scope-satisfied? #{"read:users" "agent:reports"} "agent:reports"))+ (is (not (scope/scope-satisfied? #{"read:users" "agent:queries"} "agent:reports")))))(deftest ^:parallel enforce-scope-test(let [ok-handler (fn [_request respond _raise](respond {:status 200 :body "ok"}))- middleware (scope/enforce-scope "agent:workspaces")]+ middleware (scope/enforce-scope "agent:reports")](testing "nil token-scopes (normal session auth) passes through"(is (= {:status 200 :body "ok"}(invoke-handler (middleware ok-handler) {}))))(testing "matching scope passes through"(is (= {:status 200 :body "ok"}- (invoke-handler (middleware ok-handler) {:token-scopes #{"agent:workspaces"}}))))+ (invoke-handler (middleware ok-handler) {:token-scopes #{"agent:reports"}}))))(testing "wildcard scope passes through"(is (= {:status 200 :body "ok"}(invoke-handler (middleware ok-handler) {:token-scopes #{"agent:*"}}))))@@ -78,8 +78,8 @@spy-handler (fn [request respond _raise](deliver seen-request request)(respond {:status 200}))- middleware (scope/enforce-scope "agent:workspaces")]- (invoke-handler (middleware spy-handler) {:token-scopes #{"agent:workspaces"}})+ middleware (scope/enforce-scope "agent:reports")]+ (invoke-handler (middleware spy-handler) {:token-scopes #{"agent:reports"}})(is (true? (:token-scopes-checked (deref seen-request 1000 ::timeout)))))))(deftest ^:parallel ensure-scopes-checked-test@@ -95,13 +95,13 @@(is (= 403(:status (invoke-handler (scope/ensure-scopes-checked ok-handler) {:token-scopes #{"*"}})))))(testing "scoped request without prior check is rejected with 403"- (let [response (invoke-handler (scope/ensure-scopes-checked ok-handler) {:token-scopes #{"agent:workspaces"}})]+ (let [response (invoke-handler (scope/ensure-scopes-checked ok-handler) {:token-scopes #{"agent:reports"}})](is (= 403 (:status response)))(is (= "scope_not_permitted" (get-in response [:body :error])))))(testing "scoped request with :token-scopes-checked passes through"(is (= {:status 200 :body "ok"}(invoke-handler (scope/ensure-scopes-checked ok-handler)- {:token-scopes #{"agent:workspaces"} :token-scopes-checked true}))))+ {:token-scopes #{"agent:reports"} :token-scopes-checked true}))))(testing "empty token-scopes set without prior check is rejected"(is (= 403(:status (invoke-handler (scope/ensure-scopes-checked ok-handler) {:token-scopes #{}})))))))diff --git a/test/metabase/app_db/schema_migrations_test.clj b/test/metabase/app_db/schema_migrations_test.cljindex 55db856ad20..f9f5c222dd0 100644--- a/test/metabase/app_db/schema_migrations_test.clj+++ b/test/metabase/app_db/schema_migrations_test.clj@@ -2791,68 +2791,6 @@{:first_name "JWT" :provider "jwt"}]results))))))-(deftest workspace-input-normalization-migration-test- (testing "Migrations v60.2026-02-09T12:00:00 through v60.2026-02-09T12:00:14:- Drop/recreate workspace_input with normalized schema and create workspace_input_transform"- (impl/test-migrations ["v60.2026-02-09T12:00:00" "v60.2026-02-09T12:00:14"] [migrate!]- ;; Create workspace- (let [ws-id (first (t2/insert-returning-pks! :workspace- {:name "Test Workspace"- :creator_id 1- :api_key_id 1- :execution_user 1- :database_id 1- :db_status "ready"- :base_status "active"- :graph_version 1- :created_at :%now- :updated_at :%now}))]- ;; Insert workspace_input rows with OLD schema (includes ref_id and transform_version).- (t2/insert-returning-pks! :workspace_input- {:workspace_id ws-id- :db_id 1- :schema "public"- :table "orders"- :ref_id (str (random-uuid))- :access_granted true- :transform_version 1- :created_at :%now- :updated_at :%now})- (migrate!)- ;; 1. Old workspace_input data is dropped (table recreated with new schema)- (testing "workspace_input table is empty after drop/recreate"- (is (= 0 (count (mdb.query/query {:select [:id] :from [:workspace_input]})))))- ;; 2. ref_id and transform_version columns no longer exist- (testing "ref_id column removed"- (is (thrown? Exception- (mdb.query/query {:select [:ref_id] :from [:workspace_input] :limit 1}))))- (testing "transform_version column removed"- (is (thrown? Exception- (mdb.query/query {:select [:transform_version] :from [:workspace_input] :limit 1}))))- ;; 3. workspace_input_transform table exists- (testing "workspace_input_transform table created"- (is (= 0 (count (mdb.query/query {:select [:id] :from [:workspace_input_transform]})))))- ;; 4. New unique constraint on (workspace_id, db_id, schema, table)- (testing "can insert into new schema"- (t2/insert-returning-pks! :workspace_input- {:workspace_id ws-id- :db_id 1- :schema "public"- :table "orders"- :access_granted false- :created_at :%now- :updated_at :%now}))- (testing "unique constraint prevents duplicate table entries"- (is (thrown? Exception- (t2/insert-returning-pks! :workspace_input- {:workspace_id ws-id- :db_id 1- :schema "public"- :table "orders"- :access_granted false- :created_at :%now- :updated_at :%now}))))))))-(deftest dependency-status-segment-handles-missing-column-migration-test(testing "The whole 20260402_dependency_status changeset run survives a missingsegment.dependency_analysis_version column (issue #74443). Every changeset thatdiff --git a/test/metabase/driver/connection/workspaces_test.clj b/test/metabase/driver/connection/workspaces_test.cljdeleted file mode 100644index d14d09f1262..00000000000--- a/test/metabase/driver/connection/workspaces_test.clj+++ /dev/null@@ -1,41 +0,0 @@-(ns metabase.driver.connection.workspaces-test- (:require- [clojure.test :refer :all]- [metabase.driver.connection.workspaces :as driver.w]))--(deftest ^:parallel maybe-swap-details-test- (testing "maybe-swap-details merges swap map into details"- (driver.w/with-swapped-connection-details 1 {:user "swap-user" :password "swap-pass"}- (is (= {:host "localhost" :user "swap-user" :password "swap-pass"}- (driver.w/maybe-swap-details 1 {:host "localhost" :user "original-user" :password "original-pass"})))))- (testing "maybe-swap-details returns details unchanged when no swap exists"- (driver.w/with-swapped-connection-details 1 {:user "swap-user"}- (is (= {:host "localhost" :user "original-user"}- (driver.w/maybe-swap-details 2 {:host "localhost" :user "original-user"})))))- (testing "maybe-swap-details supports deep merge for nested maps"- (driver.w/with-swapped-connection-details 1 {:ssl {:key-store-password "new-pass"}}- (is (= {:host "localhost" :ssl {:enabled true :key-store-password "new-pass"}}- (driver.w/maybe-swap-details 1 {:host "localhost" :ssl {:enabled true :key-store-password "old-pass"}})))))- (testing "deep merge adds new keys to nested maps"- (driver.w/with-swapped-connection-details 1 {:ssl {:new-key "new-value"}}- (is (= {:host "localhost" :ssl {:enabled true :new-key "new-value"}}- (driver.w/maybe-swap-details 1 {:host "localhost" :ssl {:enabled true}})))))- (testing "deep merge replaces nested map with non-map value"- (driver.w/with-swapped-connection-details 1 {:ssl "disabled"}- (is (= {:host "localhost" :ssl "disabled"}- (driver.w/maybe-swap-details 1 {:host "localhost" :ssl {:enabled true :key-store "path"}})))))- (testing "deep merge adds nested map where none existed"- (driver.w/with-swapped-connection-details 1 {:ssl {:enabled true}}- (is (= {:host "localhost" :ssl {:enabled true}}- (driver.w/maybe-swap-details 1 {:host "localhost"})))))- (testing "deep merge works with multiple levels of nesting"- (driver.w/with-swapped-connection-details 1 {:advanced {:ssl {:cert {:path "/new/path"}}}}- (is (= {:host "localhost" :advanced {:timeout 30 :ssl {:enabled true :cert {:path "/new/path"}}}}- (driver.w/maybe-swap-details 1 {:host "localhost" :advanced {:timeout 30 :ssl {:enabled true :cert {:path "/old/path"}}}})))))- (testing "nested swaps for the same database throw an exception"- (driver.w/with-swapped-connection-details 1 {:user "outer"}- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Nested connection detail swaps are not supported"- (driver.w/with-swapped-connection-details 1 {:user "inner"}- nil))))))diff --git a/test/metabase/driver/connection_test.clj b/test/metabase/driver/connection_test.cljindex 9d57ae8eeda..d4264ca3026 100644--- a/test/metabase/driver/connection_test.clj+++ b/test/metabase/driver/connection_test.clj@@ -4,7 +4,6 @@[mb.hawk.assert-exprs.approximately-equal :as =?][metabase.analytics.prometheus-test :as prometheus-test][metabase.driver.connection :as driver.conn]- [metabase.driver.connection.workspaces :as driver.w][metabase.test :as mt][toucan2.core :as t2]))@@ -98,47 +97,8 @@(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"+ (testing "without an admin-overlay 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"}@@ -149,112 +109,6 @@(is (=? details(driver.conn/effective-details database))))))))-(deftest connection-pool-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/connection-pool-type configured)))))- (testing "returns :default when requested but not configured (avoids duplicate pool)"- (driver.conn/with-admin-connection- (is (= :default (driver.conn/connection-pool-type unconfigured)))))- (testing "returns :default when not requested even if configured"- (is (= :default (driver.conn/connection-pool-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/database- :id 1- :details {:host "read-host" :user "admin" :port 5432}}]- (driver.w/with-swapped-connection-details 1 {:user "ws-user" :password "ws-pass"}- (is (=? {:host "read-host" :user "ws-user" :password "ws-pass" :port 5432}- (driver.conn/effective-details database))))))- (mt/when-ee-evailable- (mt/with-premium-features #{:writable-connection}- (testing "effective-details applies workspace swap AFTER write-data merge"- (let [database {:lib/type :metadata/database- :id 1- :details {:host "host" :user "admin" :port 5432}- :write-data-details {:user "writer" :password "write-pass" :write true}}]- (driver.w/with-swapped-connection-details 1 {:user "ws-user" :password "ws-pass"}- (driver.conn/with-write-connection- (is (=? {:host "host" :user "ws-user" :password "ws-pass" :port 5432 :write true}- (driver.conn/effective-details database)))))))- (testing "without workspace swap, effective-details is unchanged (regression)"- (let [database {:lib/type :metadata/database- :id 1- :details {:host "host" :user "admin"}- :write-data-details {:user "writer"}}]- (is (=? {:host "host" :user "admin"}- (driver.conn/effective-details database)))- (driver.conn/with-write-connection- (is (=? {:host "host" :user "writer"}- (driver.conn/effective-details database)))))))))-(deftest type-resolved-metric-test(mt/when-ee-evailable(mt/with-premium-features #{:writable-connection}@@ -286,17 +140,6 @@(driver.conn/effective-details database)(is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved{:connection-type "write-data"}))))))- (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}- :write-data-details {:host "write-host"}}]- (driver.w/with-swapped-connection-details 1 {:user "ws-user"}- (driver.conn/with-write-connection- (driver.conn/effective-details database)))- (is (prometheus-test/approx= 0 (mt/metric-value system :metabase-db-connection/type-resolved- {:connection-type "write-data"}))))))(testing "type-resolved counter does NOT increment inside without-resolution-telemetry"(mt/with-prometheus-system! [_ system](let [database {:lib/type :metadata/databasediff --git a/test/metabase/driver/postgres_test.clj b/test/metabase/driver/postgres_test.cljindex 052d4c3d5d9..fcfb1c9432f 100644--- a/test/metabase/driver/postgres_test.clj+++ b/test/metabase/driver/postgres_test.clj@@ -31,7 +31,6 @@[metabase.driver.sql-jdbc.sync.interface :as sql-jdbc.sync.interface][metabase.driver.sql.query-processor :as sql.qp][metabase.driver.sql.query-processor-test-util :as sql.qp-test-util]- [metabase.driver.sql.util :as sql.u][metabase.lib-be.metadata.jvm :as lib.metadata.jvm][metabase.lib.convert :as lib.convert][metabase.lib.core :as lib]@@ -2485,193 +2484,6 @@:name "xix"}(first (driver/validate-native-query-fields :postgres broken-query))))))))))-;;; ---------------------------------------- Workspace provisioning ------------------------------------------------(defmacro ^:private with-drop-schema!- "Run `body`, ensuring `schema` is dropped (CASCADE) on `admin-spec` afterward."- [admin-spec schema & body]- `(try- ~@body- (finally- (jdbc/execute! ~admin-spec- [(format "DROP SCHEMA IF EXISTS %s CASCADE"- (sql.u/quote-name :postgres :schema ~schema))]))))--(defmacro ^:private with-drop-role!- "Run `body`, ensuring `role` is dropped on `admin-spec` afterward."- [admin-spec role & body]- `(try- ~@body- (finally- (jdbc/execute! ~admin-spec- [(format "DROP ROLE IF EXISTS %s"- (sql.u/quote-name :postgres :field ~role))]))))--(deftest workspace-precondition-usage-grant-option-test- (mt/test-driver :postgres- (testing "assert-has-usage-grant-option! throws when current_user lacks USAGE WITH GRANT OPTION on the schema"- (mt/with-empty-db- (let [admin-spec (sql-jdbc.conn/db->pooled-connection-spec (mt/db))- role "ws_pre_usage_role"- schema "ws_pre_usage_schema"- password (str (random-uuid))]- (with-drop-role! admin-spec role- (with-drop-schema! admin-spec schema- (let [qrole (sql.u/quote-name :postgres :field role)- qschema (sql.u/quote-name :postgres :schema schema)]- (jdbc/execute! admin-spec- [(format (str "CREATE ROLE %s WITH LOGIN PASSWORD '%s'; "- "CREATE SCHEMA %s; "- ;; USAGE without WITH GRANT OPTION- "GRANT USAGE ON SCHEMA %s TO %s;")- qrole password qschema qschema qrole)])- (sql-jdbc.conn/with-connection-spec-for-testing-connection- [user-spec [:postgres (assoc (:details (mt/db)) :user role :password password)]]- (testing "fails when grant option is missing"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"USAGE WITH GRANT OPTION"- (postgres/assert-has-usage-grant-option! user-spec schema))))- (testing "passes once WITH GRANT OPTION is granted"- (jdbc/execute! admin-spec- [(format "GRANT USAGE ON SCHEMA %s TO %s WITH GRANT OPTION"- qschema qrole)])- (is (nil? (postgres/assert-has-usage-grant-option! user-spec schema)))))))))))))--(deftest workspace-precondition-table-grant-option-test- (mt/test-driver :postgres- (testing "assert-has-grant-option! throws when current_user lacks SELECT WITH GRANT OPTION on a schema table"- (mt/with-empty-db- (let [admin-spec (sql-jdbc.conn/db->pooled-connection-spec (mt/db))- role "ws_pre_table_role"- schema "ws_pre_table_schema"- table "ws_pre_table_t"- password (str (random-uuid))]- (with-drop-role! admin-spec role- (with-drop-schema! admin-spec schema- (let [qrole (sql.u/quote-name :postgres :field role)- qschema (sql.u/quote-name :postgres :schema schema)- qtable (sql.u/quote-name :postgres :table table)- qobject (str qschema "." qtable)]- (jdbc/execute! admin-spec- [(format (str "CREATE ROLE %s WITH LOGIN PASSWORD '%s'; "- "CREATE SCHEMA %s; "- "CREATE TABLE %s (id INT); "- ;; Pass the schema-USAGE precondition so we test the table check in isolation.- "GRANT USAGE ON SCHEMA %s TO %s WITH GRANT OPTION; "- ;; SELECT *without* WITH GRANT OPTION- "GRANT SELECT ON %s TO %s;")- qrole password qschema qobject qschema qrole qobject qrole)])- (sql-jdbc.conn/with-connection-spec-for-testing-connection- [user-spec [:postgres (assoc (:details (mt/db)) :user role :password password)]]- (testing "fails and names the offending table"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"SELECT WITH GRANT OPTION"- (postgres/assert-has-grant-option! user-spec schema))))- (testing "passes once SELECT WITH GRANT OPTION is granted"- (jdbc/execute! admin-spec- [(format "GRANT SELECT ON %s TO %s WITH GRANT OPTION"- qobject qrole)])- (is (nil? (postgres/assert-has-grant-option! user-spec schema)))))))))))))--(deftest workspace-precondition-alter-default-privileges-test- (mt/test-driver :postgres- (testing "assert-can-alter-default-privileges! throws when an object owner is not a role the current_user belongs to"- (mt/with-empty-db- (let [admin-spec (sql-jdbc.conn/db->pooled-connection-spec (mt/db))- role "ws_pre_adp_role"- owner "ws_pre_adp_other_owner"- schema "ws_pre_adp_schema"- table "ws_pre_adp_t"- password (str (random-uuid))]- (with-drop-role! admin-spec role- (with-drop-role! admin-spec owner- (with-drop-schema! admin-spec schema- (let [qrole (sql.u/quote-name :postgres :field role)- qowner (sql.u/quote-name :postgres :field owner)- qschema (sql.u/quote-name :postgres :schema schema)- qobject (str qschema "." (sql.u/quote-name :postgres :table table))]- (jdbc/execute! admin-spec- [(format (str "CREATE ROLE %s WITH LOGIN PASSWORD '%s'; "- "CREATE ROLE %s; "- "CREATE SCHEMA %s; "- "GRANT USAGE ON SCHEMA %s TO %s WITH GRANT OPTION; "- "CREATE TABLE %s (id INT); "- "ALTER TABLE %s OWNER TO %s;")- qrole password qowner qschema qschema qrole qobject qobject qowner)])- (sql-jdbc.conn/with-connection-spec-for-testing-connection- [user-spec [:postgres (assoc (:details (mt/db)) :user role :password password)]]- (testing "fails and names the unmemberable owner"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"not a member of \d+ role"- (postgres/assert-can-alter-default-privileges! user-spec schema))))- (testing "passes once the current_user becomes a member of the owner role"- (jdbc/execute! admin-spec- [(format "GRANT %s TO %s" qowner qrole)])- (is (nil? (postgres/assert-can-alter-default-privileges! user-spec schema))))))))))))))--(deftest ^:synchronized workspace-destroy-survives-foreign-grantor-default-priv-test- ;; PostgreSQL counterpart to Redshift's GHY-3709 destroy fix. The Redshift- ;; driver had to grow explicit `pg_default_acl` discovery + per-grantor- ;; REVOKEs because Redshift's `DROP OWNED BY` semantics differ from PG. PG's- ;; destroy issues `DROP OWNED BY <iso-user>` which removes default-priv ACL- ;; entries where the iso-user appears as a grantee, regardless of who granted- ;; them. This test pins that contract: seed a foreign-grantor default-priv- ;; row targeting the iso-user and confirm `destroy-workspace-isolation!`- ;; still cleans up without raising "user cannot be dropped because some- ;; objects depend on it".- (mt/test-driver :postgres- (testing "destroy succeeds when a non-current_user grantor seeded a default-priv targeting the iso-user"- (mt/with-empty-db- (let [admin-spec (sql-jdbc.conn/db->pooled-connection-spec (mt/db))- grantor "ws_destroy_foreign_grantor"- schema "ws_destroy_foreign_schema"- qgrantor (sql.u/quote-name :postgres :field grantor)- qschema (sql.u/quote-name :postgres :schema schema)- workspace {:id (rand-int Integer/MAX_VALUE)- :name "wsd-foreign-destroy"}]- (with-drop-role! admin-spec grantor- (with-drop-schema! admin-spec schema- (jdbc/execute! admin-spec- [(format (str "CREATE ROLE %s WITH LOGIN PASSWORD 'pwd'; "- "GRANT %s TO CURRENT_USER; "- "CREATE SCHEMA %s AUTHORIZATION %s;")- qgrantor qgrantor qschema qgrantor)])- (let [workspace+det (merge workspace (driver/workspace-isolation-details :postgres (mt/db) workspace))- _ (driver/init-workspace-isolation! :postgres (mt/db) workspace+det)- iso-user (-> workspace+det :database_details :user)- qiso (sql.u/quote-name :postgres :field iso-user)]- (try- ;; Seed the foreign-grantor default-priv: set the grantor role and- ;; issue ALTER DEFAULT PRIVILEGES so the resulting pg_default_acl- ;; row is owned by the grantor, not by current_user.- (jdbc/execute! admin-spec- [(format (str "SET ROLE %s; "- "ALTER DEFAULT PRIVILEGES IN SCHEMA %s "- "GRANT SELECT ON TABLES TO %s; "- "RESET ROLE;")- qgrantor qschema qiso)])- (testing "destroy completes without error"- (is (some? (driver/destroy-workspace-isolation! :postgres (mt/db) workspace+det))))- (testing "the iso-user has been dropped"- (is (empty? (jdbc/query admin-spec- ["SELECT 1 FROM pg_roles WHERE rolname = ?" iso-user]))))- (finally- ;; If destroy raised, the iso-user may still exist -- clean up so the- ;; with-drop-role!/with-drop-schema! frames don't fail on the schema.- ;; Log instead of swallowing so CI surfaces orphan-role accumulation- ;; rather than masking it behind a failed schema drop downstream.- (try (jdbc/execute! admin-spec- [(format "DROP OWNED BY %s CASCADE" qiso)])- (catch Throwable t- (log/warnf t "Test cleanup: DROP OWNED BY %s failed" qiso)))- (try (jdbc/execute! admin-spec- [(format "DROP USER IF EXISTS %s" qiso)])- (catch Throwable t- (log/warnf t "Test cleanup: DROP USER %s failed" qiso)))))))))))))-(deftest ^:synchronized reducible-query-streams-large-result-set-test(testing "reducible-query streams large result sets via a server-side cursor (autoCommit=false)"(mt/test-driver :postgresdiff --git a/test/metabase/driver/sql_jdbc/connection_test.clj b/test/metabase/driver/sql_jdbc/connection_test.cljindex 68a8a6af665..602fdf936bb 100644--- a/test/metabase/driver/sql_jdbc/connection_test.clj+++ b/test/metabase/driver/sql_jdbc/connection_test.clj@@ -15,7 +15,6 @@[metabase.core.core :as mbc][metabase.driver :as driver][metabase.driver.connection :as driver.conn]- [metabase.driver.connection.workspaces :as driver.w][metabase.driver.settings :as driver.settings][metabase.driver.sql-jdbc.connection :as sql-jdbc.conn][metabase.driver.sql-jdbc.connection.ssh-tunnel :as ssh]@@ -37,8 +36,6 @@[next.jdbc :as next.jdbc][toucan2.core :as t2])(:import- (com.google.common.cache Cache)- (com.mchange.v2.c3p0 DataSources)(org.h2.tools Server)))(set! *warn-on-reflection* true)@@ -203,34 +200,27 @@(deftest invalidate-pool-clears-all-connection-types-test(mt/test-driver :h2(when config/ee-available?- (mt/with-premium-features #{:writable-connection :workspaces}- (testing "invalidate-pool-for-db! clears default, write, and admin pools"+ (mt/with-premium-features #{:writable-connection}+ (testing "invalidate-pool-for-db! clears default and write pools"(let [read-details {:db "mem:invalidate_test"}- write-details {:db "mem:invalidate_write_test"}- admin-details {:db "mem:invalidate_admin_test"}]+ write-details {:db "mem:invalidate_write_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- :admin_details admin-details}]+ :write_data_details write-details}](let [db-id (u/the-id database)default-cache-key [db-id :default]- write-cache-key [db-id :write-data]- admin-cache-key [db-id :admin]]+ write-cache-key [db-id :write-data]](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 "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 admin-cache-key)))+ (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key)))(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 admin-cache-key))))))))))))+ (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))))))))))(deftest ^:parallel c3p0-datasource-name-test(mt/test-drivers (mt/driver-select {:+parent :sql-jdbc})@@ -799,206 +789,6 @@:ssl true})))))(log/info "Skipping test: MB_MYSQL_AWS_IAM_TEST not set")))-(defn- count-swapped-pools-for-db- "Count the number of swapped connection pools for a given database ID.- Since pools are keyed by [db-id, details-hash], we need to iterate through- all cache entries and count those matching the db-id."- [db-id]- (let [cache ^Cache @#'sql-jdbc.conn/swapped-connection-pools]- (count (filter (fn [[cached-db-id _details-hash]]- (= cached-db-id db-id))- (keys (.asMap cache))))))--(defn- swap-cache-key- "Helper to compute the cache key for a swapped pool, matching the implementation in connection.clj.- Takes a db map (with :id, :engine, :details) and returns [db-id, jdbc-spec-hash]."- [db]- [(:id db) (#'sql-jdbc.conn/jdbc-spec-hash db)])--(deftest with-swapped-connection-details-test- (testing "Swap connection details temporarily"- (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})- (let [db (mt/db)- db-id (u/the-id db)]- (sql-jdbc.conn/invalidate-pool-for-db! db)- (let [original-spec (sql-jdbc.conn/db->pooled-connection-spec db)]- (testing "Swap map is merged into details when creating connection"- (driver.w/with-swapped-connection-details db-id {:test-swap true}- (testing "spec is swapped"- (is (not= original-spec (sql-jdbc.conn/db->pooled-connection-spec db))))- (testing "Pool was created with swap in swapped pools cache"- (is (= 1 (count-swapped-pools-for-db db-id)))))))- (testing "Connection works normally outside swap scope"- (sql-jdbc.conn/invalidate-pool-for-db! db)- (let [spec (sql-jdbc.conn/db->pooled-connection-spec db)]- (is (some? spec))))))))--(deftest different-swap-details-get-separate-pools-test- (testing "Different swap details for the same database get separate pools, identical details share pools"- (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})- (let [db (mt/db)- db-id (u/the-id db)]- (sql-jdbc.conn/invalidate-pool-for-db! db)- (let [pool-a-1 (atom nil)- pool-b (atom nil)- pool-a-2 (atom nil)]- (testing "User A swaps with their credentials"- (driver.w/with-swapped-connection-details db-id {:user "user-a" :password "pass-a" :log-level 100}- (reset! pool-a-1 (sql-jdbc.conn/db->pooled-connection-spec db))- (is (= 1 (count-swapped-pools-for-db db-id)) "First swap creates one pool")))- (testing "User B swaps with different credentials"- (driver.w/with-swapped-connection-details db-id {:user "user-b" :password "pass-b" :log-level 99}- (reset! pool-b (sql-jdbc.conn/db->pooled-connection-spec db))- (is (= 2 (count-swapped-pools-for-db db-id)) "Different swap details create a second pool")- (is (not (identical? @pool-a-1 @pool-b)) "Different swap details return different pool instances")))- (testing "User A returns - should reuse their original pool (still in cache due to TTL)"- (driver.w/with-swapped-connection-details db-id {:user "user-a" :password "pass-a" :log-level 100}- (reset! pool-a-2 (sql-jdbc.conn/db->pooled-connection-spec db))- (is (= 2 (count-swapped-pools-for-db db-id)) "Identical swap details reuse existing pool")- (is (identical? @pool-a-1 @pool-a-2) "Identical swap details return the same pool instance"))))))))--(deftest with-swapped-connection-details-nested-test- (testing "Nested swaps for the same database throw an exception"- (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})- (let [db (mt/db)- db-id (u/the-id db)]- (sql-jdbc.conn/invalidate-pool-for-db! db)- (driver.w/with-swapped-connection-details db-id {:outer-swap true}- (sql-jdbc.conn/db->pooled-connection-spec db)- (testing "Attempting nested swap for same database throws"- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Nested connection detail swaps are not supported"- (driver.w/with-swapped-connection-details db-id {:inner-swap true}- (sql-jdbc.conn/db->pooled-connection-spec db)))))))))- (testing "Different databases can have concurrent swaps"- (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})- (let [db-1 (mt/db)- db-1-id (u/the-id db-1)]- ;; We can only test this with one db in most test setups, but the code path works- (driver.w/with-swapped-connection-details db-1-id {:swap-1 true}- ;; This would work for a different db-id- (is (some? (sql-jdbc.conn/db->pooled-connection-spec db-1))))))))--(deftest invalidate-pool-clears-both-canonical-and-swapped-test- (testing "invalidate-pool-for-db! clears canonical pools (default and write) and swapped pools"- (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})- (let [db (mt/db)- db-id (u/the-id db)- default-cache-key [db-id :default]- write-cache-key [db-id :write-data]]- (sql-jdbc.conn/invalidate-pool-for-db! db)- ;; Create default canonical pool- (sql-jdbc.conn/db->pooled-connection-spec db)- (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key)- "default canonical pool exists")- ;; Insert a fake write pool entry to verify it gets cleared.- ;; Needs :datasource so destroy-pool! can handle it.- (let [fake-ds (DataSources/pooledDataSource- (DataSources/unpooledDataSource "jdbc:h2:mem:fake_write_pool"))]- (swap! @#'sql-jdbc.conn/pool-cache-key->connection-pool- assoc write-cache-key {:datasource fake-ds}))- (is (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key)- "write canonical pool exists")- ;; Create swapped pool (for default connection type)- (driver.w/with-swapped-connection-details db-id {:test-swap true}- (sql-jdbc.conn/db->pooled-connection-spec db))- (is (= 1 (count-swapped-pools-for-db db-id))- "swapped pool exists")- ;; Now invalidate - should clear all pools- (sql-jdbc.conn/invalidate-pool-for-db! db)- (testing "Default canonical pool is cleared"- (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool default-cache-key))))- (testing "Write canonical pool is cleared"- (is (not (contains? @@#'sql-jdbc.conn/pool-cache-key->connection-pool write-cache-key))))- (testing "Swapped pool is cleared"- (is (= 0 (count-swapped-pools-for-db db-id))))))))--(deftest swapped-pool-recreated-when-expired-test- (testing "Swapped pools are recreated when password expires"- (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})- (let [db (mt/db)- db-id (u/the-id db)- swap-details {:test-swap true}- create-count (atom 0)]- (sql-jdbc.conn/invalidate-pool-for-db! db)- (with-redefs [sql-jdbc.conn/create-pool! (let [original @#'sql-jdbc.conn/create-pool!]- (fn [db]- (swap! create-count inc)- (original db)))]- (driver.w/with-swapped-connection-details db-id swap-details- ;; First call creates a pool- (let [pool-1 (sql-jdbc.conn/db->pooled-connection-spec db)]- (is (= 1 @create-count))- (is (some? pool-1))- ;; Simulate password expiration by modifying the cached pool- ;; Cache key is [db-id, jdbc-spec-hash-of-swapped-db]- (let [cache ^Cache @#'sql-jdbc.conn/swapped-connection-pools- swapped-db (update db :details merge swap-details)- cache-key (swap-cache-key swapped-db)- ;; Use a fixed past timestamp (year 2020) to simulate expired password- expired-timestamp 1577836800000]- (.put cache cache-key (assoc pool-1 :password-expiry-timestamp expired-timestamp)))- ;; Next call should detect invalid pool and recreate- (let [pool-2 (sql-jdbc.conn/db->pooled-connection-spec db)]- (is (= 2 @create-count) "Pool should have been recreated due to expired password")- (is (some? pool-2))- (is (not (identical? pool-1 pool-2)) "Should be a different pool instance")))))))))--(deftest swapped-pool-recreated-when-tunnel-closed-test- (testing "Swapped pools are recreated when SSH tunnel is closed"- (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})- (let [db (mt/db)- db-id (u/the-id db)- swap-details {:test-swap true}- create-count (atom 0)]- (sql-jdbc.conn/invalidate-pool-for-db! db)- (with-redefs [sql-jdbc.conn/create-pool! (let [original @#'sql-jdbc.conn/create-pool!]- (fn [db]- (swap! create-count inc)- (original db)))]- (driver.w/with-swapped-connection-details db-id swap-details- ;; First call creates a pool- (let [pool-1 (sql-jdbc.conn/db->pooled-connection-spec db)]- (is (= 1 @create-count))- (is (some? pool-1))- ;; Simulate closed tunnel by modifying the cached pool- ;; We add a tunnel-session that reports as closed- ;; Cache key is [db-id, jdbc-spec-hash-of-swapped-db]- (let [cache ^Cache @#'sql-jdbc.conn/swapped-connection-pools- swapped-db (update db :details merge swap-details)- cache-key (swap-cache-key swapped-db)]- (.put cache cache-key (assoc pool-1 :tunnel-session :mock-closed-session)))- ;; Mock ssh-tunnel-open? to return false for our mock session- (mt/with-dynamic-fn-redefs [ssh/ssh-tunnel-open? (fn [pool-spec]- (not= :mock-closed-session (:tunnel-session pool-spec)))]- ;; Next call should detect invalid pool and recreate- (let [pool-2 (sql-jdbc.conn/db->pooled-connection-spec db)]- (is (= 2 @create-count) "Pool should have been recreated due to closed tunnel")- (is (some? pool-2))- (is (not (identical? pool-1 pool-2)) "Should be a different pool instance"))))))))))--(deftest swapped-pool-reused-when-valid-test- (testing "Valid swapped pools are reused without recreation"- (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})- (let [db (mt/db)- db-id (u/the-id db)- create-count (atom 0)]- (sql-jdbc.conn/invalidate-pool-for-db! db)- (with-redefs [sql-jdbc.conn/create-pool! (let [original @#'sql-jdbc.conn/create-pool!]- (fn [db]- (swap! create-count inc)- (original db)))]- (driver.w/with-swapped-connection-details db-id {:test-swap true}- ;; First call creates a pool- (let [pool-1 (sql-jdbc.conn/db->pooled-connection-spec db)]- (is (= 1 @create-count))- (is (some? pool-1))- ;; Second call should reuse the same pool- (let [pool-2 (sql-jdbc.conn/db->pooled-connection-spec db)]- (is (= 1 @create-count) "Pool should be reused, not recreated")- (is (identical? pool-1 pool-2) "Should be the same pool instance")))))))))-(defmulti has-default-port?"Whether a driver has a default port"{:arglists '([driver])}diff --git a/test/metabase/driver/sync_test.clj b/test/metabase/driver/sync_test.cljindex f19633d27d2..69f7409fc0c 100644--- a/test/metabase/driver/sync_test.clj+++ b/test/metabase/driver/sync_test.clj@@ -2,8 +2,7 @@(:require[clojure.test :refer :all][metabase.driver.postgres]- [metabase.driver.sync :as driver.s]- [metabase.workspaces.core :as workspaces])+ [metabase.driver.sync :as driver.s])(:import(clojure.lang ExceptionInfo)))@@ -46,44 +45,3 @@: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/driver/util_test.clj b/test/metabase/driver/util_test.cljindex 052caa2f92e..71df47e1b0a 100644--- a/test/metabase/driver/util_test.clj+++ b/test/metabase/driver/util_test.clj@@ -14,7 +14,6 @@[metabase.test.fixtures :as fixtures][metabase.util :as u])(:import- (java.sql SQLException)(javax.net.ssl SSLSocketFactory)))(comment h2/keep-me)@@ -607,80 +606,3 @@(is (some? group-info))(is (= "Group info message" (:placeholder group-info)))(is (nil? (:getter group-info)) "Getter should be removed"))))))--;;; ---------------------------------------- scrub-exceptions ---------------------------------------------------(deftest ^:parallel scrub-exceptions-test- (testing "plain Exception: secret is redacted from message"- (let [e (driver.u/scrub-exceptions (Exception. "PASSWORD='s3cret'") ["s3cret"])]- (is (= "PASSWORD='****'" (ex-message e)))))- (testing "secret not present: message unchanged"- (let [e (driver.u/scrub-exceptions (Exception. "no secret here") ["s3cret"])]- (is (= "no secret here" (ex-message e)))))- (testing "cause chain is scrubbed"- (let [e (driver.u/scrub-exceptions- (Exception. "outer pw=s3cret" (Exception. "inner pw=s3cret"))- ["s3cret"])]- (is (= "outer pw=****" (ex-message e)))- (is (= "inner pw=****" (ex-message (.getCause ^Exception e))))))- (testing "ExceptionInfo: ex-data is preserved, message is scrubbed"- (let [e (driver.u/scrub-exceptions (ex-info "pw=s3cret" {:code 42}) ["s3cret"])]- (is (= "pw=****" (ex-message e)))- (is (= {:code 42} (ex-data e)))))- (testing "SQLException: SQLState and errorCode are preserved"- (let [e (driver.u/scrub-exceptions (SQLException. "pw=s3cret" "42501" 7) ["s3cret"])]- (is (= "pw=****" (ex-message e)))- (is (= "42501" (.getSQLState ^SQLException e)))- (is (= 7 (.getErrorCode ^SQLException e)))))- (testing "SQLException next-exception chain is scrubbed"- (let [next-ex (SQLException. "next pw=s3cret" "42501" 7)- main (doto (SQLException. "main pw=s3cret" "42000" 1)- (.setNextException next-ex))- e (driver.u/scrub-exceptions main ["s3cret"])]- (is (= "main pw=****" (ex-message e)))- (is (= "next pw=****" (ex-message (.getNextException ^SQLException e))))- (is (= "42501" (.getSQLState (.getNextException ^SQLException e))))))- (testing "multiple secrets are all redacted"- (let [e (driver.u/scrub-exceptions- (Exception. "user=admin password=s3cret escaped=s3cr\\et")- ["s3cret" "s3cr\\et"])]- (is (= "user=admin password=**** escaped=****" (ex-message e)))))- (testing "password with backslash sequences is treated literally, not as regex"- (let [pw "p\\nass\\r\\twor$d"- e (driver.u/scrub-exceptions (Exception. (str "CREATE USER x PASSWORD='" pw "'")) [pw])]- (is (= "CREATE USER x PASSWORD='****'" (ex-message e)))))- (testing "stack trace is preserved"- (let [original (Exception. "pw=s3cret")- trace (.getStackTrace original)- e (driver.u/scrub-exceptions original ["s3cret"])]- (is (= (seq trace) (seq (.getStackTrace ^Exception e)))))))--(deftest batch-exception-scrub-test- (testing "unwrapping the batch envelope composes with password scrubbing (the init-workspace-isolation! shape)"- (let [inner (java.sql.SQLException. "ERROR: syntax error in CREATE USER \"u\" PASSWORD 's3cret'")- batch (doto (java.sql.BatchUpdateException. "Batch entry 0 CREATE USER ... PASSWORD 's3cret' was aborted" (int-array 0))- (.setNextException inner))- scrubbed (driver.u/scrub-exceptions (driver.u/batch-exception batch) ["s3cret"])]- (is (not (instance? java.sql.BatchUpdateException scrubbed))- "the batch envelope is gone")- (is (not (str/includes? (ex-message scrubbed) "s3cret"))- "the password is gone from the message")- (is (str/includes? (ex-message scrubbed) "****")- "the password is redacted, not silently dropped"))))--(deftest batch-exception-test- (testing "unwraps the underlying per-statement exception of a BatchUpdateException"- (let [inner (java.sql.SQLException. "user cannot be dropped")- batch (doto (java.sql.BatchUpdateException. "Batch entry 18 ..." (int-array 0))- (.setNextException inner))]- (is (identical? inner (driver.u/batch-exception batch)))))- (testing "the MariaDB-connector shape chains the real exception via the cause"- (let [inner (java.sql.SQLException. "You are not allowed to create a user with GRANT")- batch (java.sql.BatchUpdateException. "envelope" nil 0 (int-array 0) inner)]- (is (identical? inner (driver.u/batch-exception batch)))))- (testing "the mssql-jdbc shape (no next exception, no cause) passes through — its message is already the plain server error"- (let [batch (java.sql.BatchUpdateException. "Cannot find the schema 'x'" "S0001" 208 (int-array 0))]- (is (identical? batch (driver.u/batch-exception batch)))))- (testing "non-batch throwables pass through"- (let [e (ex-info "boom" {})]- (is (identical? e (driver.u/batch-exception e))))))diff --git a/test/metabase/lib/metadata/transforming_provider_test.cljc b/test/metabase/lib/metadata/transforming_provider_test.cljcindex e62c58dfbb9..f559cf893c7 100644--- a/test/metabase/lib/metadata/transforming_provider_test.cljc+++ b/test/metabase/lib/metadata/transforming_provider_test.cljc@@ -87,15 +87,15 @@(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"))+ (tables-transform #(assoc % :schema "remapped_schema" :name "remapped_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)))))+ (is (= "remapped_schema" (:schema t)))+ (is (= "remapped_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)))))))))+ (is (= "remapped_schema" (:schema cached)))+ (is (= "remapped_venues" (:name cached)))))))))diff --git a/test/metabase/sql_tools/sqlglot/replace_names_test.clj b/test/metabase/sql_tools/sqlglot/replace_names_test.cljindex c61e3675c30..137a0794fda 100644--- a/test/metabase/sql_tools/sqlglot/replace_names_test.clj+++ b/test/metabase/sql_tools/sqlglot/replace_names_test.clj@@ -102,8 +102,8 @@{:tables {{:table "PEOPLE"} "users"}})))))(deftest ^:parallel table-rename-with-schema-map-value-test- (testing "Table rename using map value with schema and table (workspace isolation pattern)"- ;; This is the pattern used by workspaces to isolate tables into a new schema+ (testing "Table rename using map value with schema and table (schema relocation pattern)"+ ;; Relocating a table into a different schema as part of the rename(is (= "SELECT * FROM ws_isolated_123.public__orders"(replace-names :postgres"SELECT * FROM orders"@@ -124,7 +124,7 @@(deftest ^:parallel clickhouse-positional-placeholders-preserved-test(testing "ClickHouse: positional `?` placeholders survive the rewrite";; sqlglot renders ClickHouse placeholders in named-parameter syntax, which turns a- ;; positional `?` into the invalid `{?: }` — this broke workspace-isolated incremental+ ;; positional `?` into the invalid `{?: }` — this broke rewritten incremental;; transforms, whose compiled checkpoint filters carry prepared-statement params;; (GDGT-2847). Patched in sql_tools.py.(is (= "SELECT * FROM \"zz\".\"iso__src\" WHERE (\"ts\" > \"parseDateTimeBestEffort\"(?)) AND (\"ts\" <= \"parseDateTimeBestEffort\"(?))"diff --git a/test/metabase/test/data/h2.clj b/test/metabase/test/data/h2.cljindex 13ba2d3bfda..bba3f237159 100644--- a/test/metabase/test/data/h2.clj+++ b/test/metabase/test/data/h2.clj@@ -36,8 +36,8 @@(defmethod tx/dbdef->connection-details :h2[_driver _context dbdef]{:db (str "mem:" (tx/escaped-database-name dbdef)- ;; TODO (Ngoc 2025-12-05) -- we want admins user because workspaces tests need to be able to create- ;; user and grant privileges and stuffs. is it safe???+ ;; Tests connect as the default admin user so they can create users,+ ;; grant privileges, etc.#_(when (= context :db);; Return details with the GUEST user added so SQL queries are;; allowed.diff --git a/test/metabase/test/data/mysql.clj b/test/metabase/test/data/mysql.cljindex 57eeaeff1ec..9c459fd989b 100644--- a/test/metabase/test/data/mysql.clj+++ b/test/metabase/test/data/mysql.clj@@ -67,11 +67,7 @@(merge{:host (tx/db-test-env-var-or-throw :mysql :host "localhost"):port (tx/db-test-env-var-or-throw :mysql :port 3306)- :user (tx/db-test-env-var :mysql :user "root")- ;; MySQL 8+ uses caching_sha2_password by default, which requires RSA key exchange- ;; for password auth over non-SSL connections. Needed for workspace isolation tests- ;; which create password-authenticated users.- :additional-options "allowPublicKeyRetrieval=true"}+ :user (tx/db-test-env-var :mysql :user "root")}(when-let [password (tx/db-test-env-var :mysql :password)]{:password password})(when (= context :db)diff --git a/test/metabase/test/util.clj b/test/metabase/test/util.cljindex f7c8b92e004..d4736a4ce95 100644--- a/test/metabase/test/util.clj+++ b/test/metabase/test/util.clj@@ -413,20 +413,7 @@:entity_id (u/generate-nano-id):password (u.random/random-name):date_joined (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}))})+ :updated_at (t/zoned-date-time)})});; `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/warehouses_rest/api_test.clj b/test/metabase/warehouses_rest/api_test.cljindex 1d67ab52e15..7e0a45d3fa2 100644--- a/test/metabase/warehouses_rest/api_test.clj+++ b/test/metabase/warehouses_rest/api_test.clj@@ -3043,145 +3043,22 @@(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 update-database-preserves-overlay-details-test- (testing "PUT /api/database/:id without write_data_details/admin_details/provider_name keys"+ (testing "PUT /api/database/:id without write_data_details/provider_name keys"(testing "preserves nullable fields instead of nil-ing them out"- (mt/with-premium-features #{:writable-connection :workspaces}+ (mt/with-premium-features #{:writable-connection}(mt/with-temp [:model/Database {db-id :id} {:engine :h2:details {:host "localhost"}:write_data_details {:host "write-host"}- :admin_details {:host "admin-host"}:provider_name "AWS RDS"}](with-redefs [driver/can-connect? (constantly true)](mt/user-http-request :crowberto :put 200 (format "database/%d" db-id){:name "Renamed DB"})(is (=? {:name "Renamed DB":write_data_details {:host "write-host"}- :admin_details {:host "admin-host"}:provider_name "AWS RDS"}(t2/select-one :model/Database :id db-id)))))))))-(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}}))))))))-(deftest databases-list-can-upload-respects-view-data-test(testing "GET /api/database/:id can_upload reflects the user's view-data permission, not just uploads_enabled"(mt/with-temp [:model/Database {db-id :id} {:engine :postgres