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
Async workspace provisioning (#78091)
Alexander Polyankine1a1e6991153
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.ednindex 216b43e77e2..87a9bd52e22 100644--- a/.clj-kondo/config.edn+++ b/.clj-kondo/config.edn@@ -209,6 +209,8 @@metabase-enterprise.workspaces.test-util/create-ready-ws!metabase-enterprise.workspaces.core/clear-instance-workspace!metabase-enterprise.workspaces.core/set-instance-workspace!+ metabase-enterprise.workspaces.instance/clear-instance-workspace!+ metabase-enterprise.workspaces.instance/set-instance-workspace!metabase.workspaces.core/clear-instance-workspace!metabase.workspaces.core/set-instance-workspace!;; Safe in tests, where we typically use a private, temporary index per thread.diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 02e7083f8bc..74b26422b99 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -2870,8 +2870,8 @@enterprise/advanced-config{:team "UX West"- :api #{metabase-enterprise.advanced-config.api- metabase-enterprise.advanced-config.api.logs+ :api #{metabase-enterprise.advanced-config.api.logs+ metabase-enterprise.advanced-config.api.routesmetabase-enterprise.advanced-config.filemetabase-enterprise.advanced-config.initmetabase-enterprise.advanced-config.settings}@@ -2888,6 +2888,7 @@syncusersutil+ enterprise/remote-syncenterprise/workspaces}:model-imports #{:model/ApiKey :model/Database :model/User}}@@ -3620,7 +3621,6 @@metabase-enterprise.serialization.cmdmetabase-enterprise.serialization.coremetabase-enterprise.serialization.init- metabase-enterprise.serialization.schemametabase-enterprise.serialization.v2.backfill-ids}:friends #{enterprise/memoize-monitor}:uses #{analytics@@ -3800,12 +3800,11 @@premium-featuresquery-processorsample-data- serversettingssql-toolsutilworkspaces- enterprise/serialization}+ enterprise/remote-sync}:model-exports #{:model/Workspace}:model-imports #{:model/Database:model/Tablediff --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/support/timings.json b/e2e/support/timings.jsonindex 20a8b30a5d8..5ea5a6c248a 100644--- a/e2e/support/timings.json+++ b/e2e/support/timings.json@@ -1659,14 +1659,6 @@{"spec": "../test/scenarios/visualizations-tabular/visualizations-tabular-reproductions.cy.spec.js","duration": 113946- },- {- "spec": "../test/scenarios/workspaces/workspace-instance.cy.spec.ts",- "duration": 42522- },- {- "spec": "../test/scenarios/workspaces/workspace-manager.cy.spec.ts",- "duration": 9359}]-}\ No newline at end of file+}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..295a5b758d5 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_config/api.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/api.clj@@ -21,6 +21,25 @@metabase-enterprise.advanced-config.file.users/keep-memetabase-enterprise.advanced-config.file.workspace/keep-me)+(def ConfigUploadRequest+ "Schema for the multipart `config.yml` upload request, shared with the+ `/external` variant."+ [:map+ [:multipart-params+ [:map+ ["config" ms/File]]]])++(defn apply-config-upload!+ "Apply the uploaded `config.yml` multipart param `config` to this instance and+ delete its tempfile. Shared by the `/` and `/external` upload endpoints."+ [config]+ (let [tempfile (:tempfile config)]+ (try+ (advanced-config.file/initialize! (yaml/from-file tempfile))+ (finally+ (io/delete-file tempfile :silently))))+ nil)+(api.macros/defendpoint :post "/" :- :nil"Apply an uploaded `config.yml` to this instance. Runs the same per-sectioninitializers (`settings`, `databases`, `users`, `api-keys`, `workspace`, ...)@@ -33,17 +52,10 @@[_route-params_query-params_body- {{config "config"} :multipart-params, :as _request}- :- [:map- [:multipart-params- [:map- ["config" [:map- [:filename :string]- [:tempfile (ms/InstanceOfClass java.io.File)]]]]]]]+ {{config "config"} :multipart-params, :as _request} :- ConfigUploadRequest](api/check-superuser)- (let [tempfile (:tempfile config)]- (try- (advanced-config.file/initialize! (yaml/from-file tempfile))- (finally- (io/delete-file tempfile :silently))))- nil)+ (apply-config-upload! config))++(def ^{:arglists '([request respond raise])} routes+ "`/api/ee/advanced-config` routes."+ (api.macros/ns-handler *ns*))diff --git a/enterprise/backend/src/metabase_enterprise/advanced_config/api/external.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/api/external.cljnew file mode 100644index 00000000000..d061a4b4720--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/api/external.clj@@ -0,0 +1,26 @@+(ns metabase-enterprise.advanced-config.api.external+ "Static-API-key variant of the `config.yml` upload, served under+ `/api/ee/advanced-config/external`. The route is wrapped in+ `+static-apikey`, so requests authenticate with the static `MB_API_KEY`+ (`x-metabase-apikey` header) instead of a user session — for out-of-band+ automation, e.g. a workspace manager configuring a freshly provisioned+ child instance."+ (:require+ [metabase-enterprise.advanced-config.api :as advanced-config.api]+ [metabase.api.macros :as api.macros]))++(api.macros/defendpoint :post "/" :- :nil+ "Apply an uploaded `config.yml` to this instance. Same behavior as+ `POST /api/ee/advanced-config`, authenticated with the static `MB_API_KEY`+ header instead of a superuser session."+ {:multipart true}+ [_route-params+ _query-params+ _body+ {{config "config"} :multipart-params, :as _request}+ :- advanced-config.api/ConfigUploadRequest]+ (advanced-config.api/apply-config-upload! config))++(def ^{:arglists '([request respond raise])} routes+ "`/api/ee/advanced-config/external` routes."+ (api.macros/ns-handler *ns*))diff --git a/enterprise/backend/src/metabase_enterprise/advanced_config/api/routes.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/api/routes.cljnew file mode 100644index 00000000000..56c3988f871--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/api/routes.clj@@ -0,0 +1,13 @@+(ns metabase-enterprise.advanced-config.api.routes+ "Routes for the EE advanced-config API endpoints."+ (:require+ [metabase-enterprise.advanced-config.api :as advanced-config.api]+ [metabase-enterprise.advanced-config.api.external :as advanced-config.api.external]+ [metabase.api.routes.common :refer [+static-apikey]]+ [metabase.api.util.handlers :as handlers]))++(def ^{:arglists '([request respond raise])} routes+ "Ring routes for the EE advanced-config API endpoints."+ (handlers/route-map-handler+ {"/" advanced-config.api/routes+ "/external" (+static-apikey advanced-config.api.external/routes)}))diff --git a/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/file.cljindex fa04e6961a3..9f362a7bf78 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/file.clj@@ -102,7 +102,6 @@[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]@@ -294,15 +293,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/databases.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/file/databases.cljindex e1960ec0560..9991c73aa38 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_config/file/databases.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/file/databases.clj@@ -8,8 +8,8 @@[metabase.driver.util :as driver.u][metabase.sample-data.core :as sample-data][metabase.util :as u]+ [metabase.util.jvm :as u.jvm][metabase.util.log :as log]- [metabase.util.quick-task :as quick-task][toucan2.core :as t2]))(s/def :metabase-enterprise.advanced-config.file.databases.config-file-spec/name@@ -137,7 +137,7 @@(advanced-config.settings/config-from-file-sync-databases)(let [sync-database! (requiring-resolve 'metabase.sync.core/sync-database!)]- (quick-task/submit-task! (fn [] (sync-database! db))))+ (u.jvm/in-virtual-thread* (sync-database! db))):else(log/info "Sync on database creation when initializing from file is disabled. Skipping sync."))))))))diff --git a/enterprise/backend/src/metabase_enterprise/advanced_config/file/users.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/file/users.cljindex b64dd23a92d..6f2bec1a019 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_config/file/users.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/file/users.clj@@ -10,10 +10,10 @@[toucan2.core :as t2]))(s/def :metabase-enterprise.advanced-config.file.users.config-file-spec/first_name- string?)+ (s/nilable string?))(s/def :metabase-enterprise.advanced-config.file.users.config-file-spec/last_name- string?)+ (s/nilable string?))(s/def :metabase-enterprise.advanced-config.file.users.config-file-spec/passwordstring?)diff --git a/enterprise/backend/src/metabase_enterprise/advanced_config/file/workspace.clj b/enterprise/backend/src/metabase_enterprise/advanced_config/file/workspace.cljindex 31be057e107..29addbf616a 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_config/file/workspace.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_config/file/workspace.clj@@ -15,7 +15,9 @@[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.remote-sync.core :as remote-sync][metabase-enterprise.workspaces.core :as ws]+ [metabase.util.jvm :as u.jvm][metabase.util.log :as log][toucan2.core :as t2]))@@ -116,4 +118,7 @@(defmethod advanced-config.file.i/initialize-section! :workspace[_section-name section-config]- (apply-workspace-section! section-config))+ (apply-workspace-section! section-config)+ (when (remote-sync/remote-sync-enabled)+ (log/info "Workspace instance with remote sync configured; starting the initial import")+ (u.jvm/in-virtual-thread* (remote-sync/start-import!))))diff --git a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj b/enterprise/backend/src/metabase_enterprise/api_routes/routes.cljindex f3cea35500d..c7476deb667 100644--- a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj+++ b/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj@@ -6,8 +6,8 @@`enterprise/backend/README.md` for more details."(:require[metabase-enterprise.action-v2.api]- [metabase-enterprise.advanced-config.api][metabase-enterprise.advanced-config.api.logs]+ [metabase-enterprise.advanced-config.api.routes][metabase-enterprise.advanced-permissions.api.routes][metabase-enterprise.api.core :as ee.api][metabase-enterprise.audit-app.api.routes]@@ -109,7 +109,7 @@routes here and follow the convention.";; Postponing a granular flag for :actions until it's used more widely.{"/action-v2" (premium-handler metabase-enterprise.action-v2.api/routes :table-data-editing)- "/advanced-config" (api.macros/ns-handler 'metabase-enterprise.advanced-config.api)+ "/advanced-config" metabase-enterprise.advanced-config.api.routes/routes"/advanced-permissions" (premium-handler metabase-enterprise.advanced-permissions.api.routes/routes :advanced-permissions)"/ai-controls" (premium-handler metabase-enterprise.metabot.api.routes/routes :ai-controls)"/audit-app" (premium-handler metabase-enterprise.audit-app.api.routes/routes :audit-app)diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/core.cljindex aa84c79a93e..b98aadbe30c 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj@@ -1,6 +1,7 @@(ns metabase-enterprise.remote-sync.core(:require[metabase-enterprise.remote-sync.guards :as guards]+ [metabase-enterprise.remote-sync.impl :as impl][metabase-enterprise.remote-sync.settings :as settings][metabase-enterprise.remote-sync.source :as source][metabase-enterprise.remote-sync.source.protocol :as source.p]@@ -19,7 +20,49 @@(p/import-vars[source][source.p- ->ingestable])+ ->ingestable]+ [settings+ remote-sync-enabled+ remote-sync-url+ remote-sync-branch+ remote-sync-token])++(defn start-import!+ "Start an asynchronous import from the configured remote-sync source,+ regardless of `remote-sync-type`. Returns the started RemoteSyncTask, or nil+ when there is nothing new to import. Throws when remote sync is not+ configured, another sync task is already running, or the source is+ unreachable."+ []+ (let [branch (settings/remote-sync-branch)]+ (impl/async-import! branch true {}+ :on-success (fn [task-id _result]+ (impl/publish-sync-event! :event/remote-sync-import task-id+ {:branch branch :auto true} nil)))))++(defn branch-exists?+ "Whether `branch-name` exists on the configured remote. Throws when remote+ sync is not configured or the remote is unreachable."+ [branch-name]+ (contains? (set (source.p/branches (source/source-from-settings))) branch-name))++(defn create-branch!+ "Create `branch-name` on the configured remote, branching off the configured+ remote-sync branch (or the remote's default branch when none is+ configured). Throws when the branch already exists, remote sync is not+ configured, or the remote is unreachable."+ [branch-name]+ (let [source (source/source-from-settings)+ base (or (not-empty (settings/remote-sync-branch))+ (source.p/default-branch source))]+ (source.p/create-branch source branch-name base)))++(defn delete-branch!+ "Delete `branch-name` on the configured remote. A no-op when the branch does+ not exist. Throws when remote sync is not configured or the remote refuses+ the deletion."+ [branch-name]+ (source.p/delete-branch (source/source-from-settings) branch-name))(defenterprise collection-editable?"Determines if a remote-synced collection should be editable.diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.cljindex 05b151d0351..eeb89689010 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj@@ -494,6 +494,35 @@(push-branch! (assoc source :branch branch-name))branch-name))+(defn delete-branch+ "Deletes a branch from the remote repository, along with the local ref.+ A no-op when the branch does not exist on the remote.++ Takes a source map containing a :git Git instance and optional :token for+ authentication, and the branch-name string to delete.++ Returns nil. Throws ExceptionInfo if the remote refuses the deletion."+ [{:keys [^Git git] :as source} branch-name]+ (let [branch-ref (qualify-branch branch-name)+ push-cmd (-> (.push git)+ (.setRefSpecs (doto (java.util.ArrayList.)+ ;; empty source side = delete the remote ref+ (.add (RefSpec. (str ":" branch-ref))))))+ push-response (call-remote-command push-cmd source)+ statuses (->> push-response+ (mapcat #(.getRemoteUpdates ^PushResult %))+ (map #(.getStatus ^RemoteRefUpdate %)))]+ (when-let [failures (seq (remove #{RemoteRefUpdate$Status/OK+ RemoteRefUpdate$Status/UP_TO_DATE+ RemoteRefUpdate$Status/NON_EXISTING}+ statuses))]+ (throw (ex-info (str "Failed to delete branch " branch-name " on remote") {:failures failures})))+ (when (.resolve (.getRepository git) branch-ref)+ (call-command (-> (.branchDelete git)+ (.setBranchNames ^"[Ljava.lang.String;" (into-array String [branch-name]))+ (.setForce true))))+ nil))+(defrecord GitSnapshot [git remote-url branch version token managed-dirs]source.p/SourceSnapshot@@ -578,6 +607,9 @@(create-branch [source branch-name base-commit-ish](create-branch source branch-name base-commit-ish))+ (delete-branch [source branch-name]+ (delete-branch source branch-name))+(default-branch [this](default-branch this))diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/source/protocol.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/source/protocol.cljindex 342e3de2b43..74c0aa2c072 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/source/protocol.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/source/protocol.clj@@ -18,6 +18,13 @@Returns the name of the newly created branch.")+ (delete-branch [source branch-name]+ "Deletes a branch from the remote source. A no-op when the branch does not exist.++ Takes a source instance implementing this protocol and the branch-name to delete.++ Returns nil.")+(default-branch [source]"Gets the default branch name from the remote source.diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_instance.clj b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_instance.cljindex 75b05213af0..59ba690bb92 100644--- a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_instance.clj+++ b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_instance.clj@@ -3,13 +3,12 @@`/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-enterprise.workspaces.instance :as ws.instance][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]))+ [toucan2.core :as t2]));;; ----------------------------------------------- Schemas ----------------------------------------------------@@ -26,8 +25,7 @@(def ^:private WorkspaceInstance[:map[:name ms/NonBlankString]- [:databases [:map-of ::lib.schema.id/database WorkspaceInstanceDatabase]]- [:can_write :boolean]])+ [:databases [:map-of ::lib.schema.id/database WorkspaceInstanceDatabase]]])(def ^:private TableRemapping[:map@@ -54,20 +52,7 @@(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}))))+ :databases (update-vals (:databases workspace) present-workspace-instance-database)});;; ---------------------------------------------- Endpoints ---------------------------------------------------@@ -76,51 +61,15 @@`{: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."+ section of `config.yml`. `:data` is `null` when no workspace was loaded — i.e.+ this is a manager-only instance, or no `config.yml` was present at boot. The+ envelope avoids an empty JSON body for the `nil` case."[](api/check-superuser)- (check-workspace-writable!)- (ws/clear-instance-workspace!)- (ws/clear-all-remappings!)- nil)+ {:data (some-> (ws.instance/instance-workspace) present-workspace-instance)})(api.macros/defendpoint :get "/table-remappings" :- [:sequential TableRemapping]"Return all table remappings, ordered by id."[](api/check-superuser)- (mapv present-remapping (ws/list-remappings)))+ (mapv present-remapping (t2/select :model/TableRemapping {:order-by [[:id :asc]]})))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.cljindex 9d7778492b0..350d4349aa3 100644--- a/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj+++ b/enterprise/backend/src/metabase_enterprise/workspaces/api/workspace_manager.clj@@ -1,34 +1,35 @@(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?`)."+ Validation and presentation only — domain logic lives in the workspace models+ and [[metabase-enterprise.workspaces.provisioning]]; permission predicates live+ on `:model/Workspace` and `:model/WorkspaceDatabase` (see+ `mi/can-read?`/`can-write?`/`can-create?`).++ Creating a workspace only inserts `:unprovisioned` rows; provisioning is+ started explicitly via `POST /:id/provision` and runs in the background —+ clients poll `GET /:id` and follow the workspace's `:status`."(:require[medley.core :as m]- [metabase-enterprise.serialization.core :as serialization]- [metabase-enterprise.serialization.schema :as serialization.schema]- [metabase-enterprise.workspaces.config :as ws.config]- [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.models.workspace :as workspace]+ [metabase-enterprise.workspaces.provisioning :as ws.provisioning]+ [metabase-enterprise.workspaces.schema :as ws.schema][metabase.api.common :as api][metabase.api.macros :as api.macros][metabase.lib.schema.id :as lib.schema.id][metabase.models.interface :as mi]- [metabase.server.streaming-response :as sr]+ [metabase.util.i18n :refer [tru]]+ [metabase.util.jvm :as u.jvm]+ [metabase.util.log :as log][metabase.util.malli.schema :as ms][toucan2.core :as t2]))-(comment serialization.schema/keep-me)-;;; ----------------------------------------------- Schemas -----------------------------------------------------(def ^:private WorkspaceStatus- [:enum {:decode/api keyword}- :unprovisioned :provisioning :provisioned :deprovisioning])-(def ^:private CreateWorkspaceParams[:map {:closed true}- [:name ms/NonBlankString]- [:database_ids [:sequential {:min 1} ::lib.schema.id/database]]])+ [:name ms/NonBlankString]+ [:target_branch {:optional true} [:maybe ms/NonBlankString]]+ [:database_ids [:sequential {:min 1} ::lib.schema.id/database]]])(def ^:private UpdateWorkspaceParams[:map {:closed true}@@ -39,7 +40,8 @@[:database_id ::lib.schema.id/database][:input_schemas [:sequential ms/NonBlankString]][:output_namespace :string]- [:status WorkspaceStatus]+ [:status ::ws.schema/workspace-database-status]+ [:status_details [:maybe :string]][:database {:optional true} [:maybe :map]]])(def ^:private CreatorResponse@@ -59,20 +61,25 @@(def ^:private WorkspaceResponse[:map {:closed true}- [:id ms/PositiveInt]- [:name ms/NonBlankString]- [:creator [:maybe CreatorResponse]]- [:created_at DateTimeWithTimeZone]- [:updated_at DateTimeWithTimeZone]+ [:id ms/PositiveInt]+ [:name ms/NonBlankString]+ [:target_branch [:maybe :string]]+ [:status ::ws.schema/workspace-status]+ [:status_details [:maybe :string]]+ [:instance_id [:maybe :string]]+ [:instance_url [:maybe :string]]+ [: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]]])+ [:databases {:optional true} [:sequential WorkspaceDatabaseResponse]]]);;; -------------------------------------------- Presentation --------------------------------------------------(defn- present-workspace-database [wsd](-> wsd- (select-keys [:database_id :input_schemas :output_namespace :status :database])+ (select-keys [:database_id :input_schemas :output_namespace :status :status_details :database]);; never expose connection credentials(m/update-existing :database #(some-> % (dissoc :details)))))@@ -82,7 +89,7 @@(defn- present-workspace [workspace](some-> workspace- (select-keys [:id :name :creator :created_at :updated_at :databases])+ (select-keys [:id :name :target_branch :status :status_details :instance_id :instance_url :creator :created_at :updated_at :databases])(update :creator present-creator)(m/update-existing :databases #(mapv present-workspace-database %))))@@ -94,90 +101,92 @@(api/check-superuser)(into [] (comp (filter mi/can-read?)(map present-workspace))- (ws/list-workspaces)))+ (-> (t2/select :model/Workspace {:order-by [[:id :asc]]})+ (t2/hydrate :creator [:databases :database]))))(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))))+ (let [ws (api/read-check :model/Workspace id)]+ (-> ws+ (t2/hydrate :creator [:databases :database])+ present-workspace)))(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]+ for workspaces). The workspace and its databases start `:unprovisioned` —+ start provisioning explicitly via `POST /:id/provision`."+ [_route-params _query-params {:keys [target_branch database_ids] :as params} :- CreateWorkspaceParams](api/create-check :model/Workspace params)- (present-workspace- (ws/create-workspace!- (assoc params :creator_id api/*current-user-id*))))+ (when target_branch+ (api/check-400 (not (t2/exists? :model/Workspace :target_branch target_branch))+ (tru "A workspace with this target branch already exists")))+ (-> (workspace/create-workspace! {:name (:name params)+ :target_branch target_branch+ :creator_id api/*current-user-id*+ :databases (workspace/workspace-databases database_ids)})+ (t2/hydrate :creator [:databases :database])+ present-workspace))(api.macros/defendpoint :put "/:id" :- WorkspaceResponse"Update a workspace's name."[{:keys [id]} :- [:map [:id ms/PositiveInt]]_query-paramsparams :- 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))))+ (let [ws (api/write-check :model/Workspace id)+ ws (if-let [new-name (:name params)]+ (do (t2/update! :model/Workspace :id id {:name new-name})+ (assoc ws :name new-name))+ ws)]+ (-> ws+ (t2/hydrate :creator [:databases :database])+ present-workspace)))(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."+ "Delete a Workspace. Refuses with a 400 unless the workspace is strictly+ `:unprovisioned` — deprovision it first (`POST /:id/deprovision`). The status+ condition sits on the DELETE itself, closing the race with a concurrently+ started run. The workspace_database rows are cascade-deleted with the+ workspace. 204 on success, no response body."[{:keys [id]} :- [:map [:id ms/PositiveInt]]](api/write-check :model/Workspace id)- (ws/delete-workspace! id)+ (api/check-400 (pos? (t2/delete! :model/Workspace :id id :status :unprovisioned)))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`."+(api.macros/defendpoint :post "/:id/provision" :- WorkspaceResponse+ "Start provisioning the workspace in the background and return immediately.+ Retryable from any settled status except `:provisioned`; 400 when already+ provisioned or while a provision or deprovision run is in flight. Poll+ `GET /:id` to follow the workspace's `:status`; the response reflects the+ just-started run (`:database-provisioning`)."[{:keys [id]} :- [:map [:id ms/PositiveInt]]]- (api/write-check :model/Workspace id)- (let [config (api/check-404 (ws.config/build-workspace-config id))]- {:status 200- :headers {"Content-Type" "application/x-yaml"- "Content-Disposition" "attachment; filename=\"config.yml\""}- :body (ws.config/config->yaml config)}))--;;; ----------------------------------------- Metadata export ----------------------------------------------------(defn- workspace-metadata-filters- "Derive the `:database-ids` and `:schema-ids` filter values from a hydrated workspace.- `:schema-ids` is a `{db-id [\"schema\" ...]}` map matching the metadata export schema."- [{:keys [databases]}]- {:database-ids (mapv :database_id databases)- :schema-ids (into {}- (map (fn [{:keys [database_id input_schemas]}]- [database_id (vec input_schemas)]))- databases)})--(api.macros/defendpoint :get "/:id/metadata/export"- :- (sr/streaming-response-schema ::serialization.schema/export-metadata-response)- "Stream the warehouse metadata (databases, tables, fields) for the workspace's databases,- scoped to each database's `:input` namespaces. Same flag semantics as- `/api/ee/serialization/metadata/export` — sections must be opted into via the- `with-databases` / `with-tables` / `with-fields` query parameters."- [{:keys [id]} :- [:map [:id ms/PositiveInt]]- query-params- :- [:map- [:with-databases {:default false} [:maybe :boolean]]- [:with-tables {:default false} [:maybe :boolean]]- [:with-fields {:default false} [:maybe :boolean]]]]- (api/read-check :model/Workspace id)- (let [workspace (api/check-404 (ws/get-workspace id))- opts (merge query-params- (workspace-metadata-filters workspace)- {:user-info {:user-id api/*current-user-id*- :is-superuser? api/*is-superuser?*}})]- (sr/streaming-response {:content-type "application/json; charset=utf-8"} [os _]- (serialization/export-metadata! os opts))))+ (let [ws (api/write-check :model/Workspace id)+ ws (ws.provisioning/set-initial-workspace-provisioning-status! ws)+ response (-> ws+ (t2/hydrate :creator [:databases :database])+ present-workspace)]+ (u.jvm/in-virtual-thread*+ (try+ (ws.provisioning/provision-workspace! ws)+ (catch Throwable t+ (log/error t "Async workspace provisioning failed"))))+ response))++(api.macros/defendpoint :post "/:id/deprovision" :- WorkspaceResponse+ "Start deprovisioning the workspace in the background and return immediately.+ Retryable from any settled status except `:unprovisioned`; 400 when already+ unprovisioned or while a provision or deprovision run is in flight. Poll+ `GET /:id` to follow the workspace's `:status`; the response reflects the+ just-started run (`:instance-deprovisioning`)."+ [{:keys [id]} :- [:map [:id ms/PositiveInt]]]+ (let [ws (api/write-check :model/Workspace id)+ ws (ws.provisioning/set-initial-workspace-deprovisioning-status! ws)+ response (-> ws+ (t2/hydrate :creator [:databases :database])+ present-workspace)]+ (u.jvm/in-virtual-thread*+ (try+ (ws.provisioning/deprovision-workspace! ws)+ (catch Throwable t+ (log/error t "Async workspace deprovisioning failed"))))+ response))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/config.clj b/enterprise/backend/src/metabase_enterprise/workspaces/config.cljindex 78cdffc6bf5..9ce6836ed71 100644--- a/enterprise/backend/src/metabase_enterprise/workspaces/config.clj+++ b/enterprise/backend/src/metabase_enterprise/workspaces/config.clj@@ -1,9 +1,10 @@(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.remote-sync.core :as remote-sync][metabase-enterprise.workspaces.models.workspace-database]+ [metabase-enterprise.workspaces.provisioning.database :as provisioning.database]+ [metabase-enterprise.workspaces.settings :as ws.settings][metabase.driver :as driver][metabase.driver.util :as driver.u][metabase.sample-data.core :as sample-data]@@ -73,7 +74,7 @@mapping."[db output-namespace](let [components (set (driver/qualified-name-components (:engine db)))- positions (ws/engine-namespace-positions db)+ positions (provisioning.database/engine-namespace-positions db)schema (when-not (str/blank? output-namespace) output-namespace)](cond-> {}(:db components) (assoc :db (:db positions))@@ -105,6 +106,36 @@[:not-in :id workspace-db-ids]true)]}))+(defn- remote-sync-settings+ "Remote-sync settings for the child instance's `settings:` config section, or+ nil when remote sync is not enabled on this instance. The child instance+ always gets `:read-write` — its whole purpose is committing changes back.+ The child works on the workspace's `:target_branch` when set, falling back+ to this instance's configured remote-sync branch."+ [workspace]+ (when (remote-sync/remote-sync-enabled)+ (let [target-branch (:target_branch workspace)]+ {:remote-sync-url (remote-sync/remote-sync-url)+ :remote-sync-branch (if (str/blank? target-branch)+ (remote-sync/remote-sync-branch)+ target-branch)+ :remote-sync-token (remote-sync/remote-sync-token)+ :remote-sync-type :read-write})))++(defn- user-entries+ "config-file `users:` entries for the child instance: the workspace creator+ as a superuser, when both the creator and the configured+ `workspace-instance-user-password` exist."+ [workspace]+ (let [{:keys [creator]} (t2/hydrate workspace :creator)+ password (ws.settings/workspace-instance-user-password)]+ (when (and creator (not (str/blank? password)))+ [{:first_name (:first_name creator)+ :last_name (:last_name creator)+ :email (:email creator)+ :password password+ :is_superuser true}])))+(defn- sample-database-entries"If the instance has a Sample Database, emit a single placeholder entry theimport side will use to recreate it. Always uses the canonical name/engine —@@ -117,7 +148,7 @@:is_sample true}]))(defn build-workspace-config- "Return a downloadable config.yml-shaped map for `workspace-id`:+ "Return a downloadable config.yml-shaped map for `workspace`:{:version 1:config {:databases [...]@@ -129,33 +160,41 @@WorkspaceDatabase's override credentials and adds `schema-filters-*` keysderived from `:input_schemas`. Per-database workspace entries carry theexpanded `{: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)}}}))))+ `instance-workspace` setting stores. When remote sync is enabled on this+ instance, `:config` also carries a `:settings` section pointing the child+ instance at the same git repo in `:read-write` mode. When the+ `workspace-instance-user-password` setting is set and the workspace has a+ creator, `:config` carries a `:users` section creating that user as a+ superuser on the child. Takes a workspace with+ its `:databases` hydrated. Throws a 409 `ex-info` if any of the workspace's+ databases is not `:provisioned`."+ [workspace]+ (let [wsds (:databases workspace)]+ (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 (:id workspace)})))+ (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)+ settings (remote-sync-settings workspace)+ users (user-entries workspace)]+ {:version 1+ :config (cond-> {:databases (-> ws-entries+ (into stub-entries)+ (into sample-entries))+ :workspace {:name (:name workspace)+ :databases (into {} (map (fn [[wsd db]] (workspace-database-entry wsd db))) pairs)}}+ settings (assoc :settings settings)+ users (assoc :users users))})))(defn config->yaml"Render a workspace config map as a pretty-printed YAML string."diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/core.clj b/enterprise/backend/src/metabase_enterprise/workspaces/core.cljindex faecf90ce24..7935ccbef17 100644--- a/enterprise/backend/src/metabase_enterprise/workspaces/core.clj+++ b/enterprise/backend/src/metabase_enterprise/workspaces/core.clj@@ -1,309 +1,13 @@(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"+ "Facade for the workspaces module: the only namespace code outside the module+ may require. Everything here is a re-export; the implementations live in the+ module-internal namespaces (`instance`, `provisioning`, `models.*`, ...)."(: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)+ [metabase-enterprise.workspaces.instance]+ [metabase.util.namespaces :as shared.ns]))++(shared.ns/import-fns+ [metabase-enterprise.workspaces.instance+ clear-instance-workspace!+ instance-workspace+ set-instance-workspace!])diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/init.clj b/enterprise/backend/src/metabase_enterprise/workspaces/init.cljindex db1fbe6533e..1e4c887e0b9 100644--- a/enterprise/backend/src/metabase_enterprise/workspaces/init.clj+++ b/enterprise/backend/src/metabase_enterprise/workspaces/init.clj@@ -10,6 +10,7 @@app-db. See `metabase-enterprise.core.init` docstring for the broaderpattern."(:require+ [metabase-enterprise.workspaces.instance][metabase-enterprise.workspaces.models.workspace][metabase-enterprise.workspaces.models.workspace-database][metabase-enterprise.workspaces.query-processor.middleware]diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/instance.clj b/enterprise/backend/src/metabase_enterprise/workspaces/instance.cljnew file mode 100644index 00000000000..36a9acda6da--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/instance.clj@@ -0,0 +1,75 @@+(ns metabase-enterprise.workspaces.instance+ "Instance-side workspace 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."+ (:require+ [metabase-enterprise.workspaces.settings :as ws.settings]+ [metabase.premium-features.core :refer [defenterprise]]+ [metabase.util.malli :as mu]+ [metabase.workspaces.core :as ws]))++(set! *warn-on-reflection* true)++(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! :- :nil+ "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)++(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 `instance-workspace`+ setting."+ [db-id]+ (get-in (instance-workspace) [:databases db-id :output]))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace.cljindex 1e139db39ee..de0480310b7 100644--- a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace.clj+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace.clj@@ -1,20 +1,22 @@(ns metabase-enterprise.workspaces.models.workspace(:require- [metabase-enterprise.workspaces.models.workspace-database]+ [metabase-enterprise.workspaces.models.workspace-database :as 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?))+(t2/deftransforms :model/Workspace+ {:status mi/transform-keyword+ :status_details mi/transform-encrypted-text})+;;; --------------------------------------- Permission predicates ---------------------------------------;;;;;; Workspaces are superuser-only: read, write, and create all require admin.@@ -53,27 +55,6 @@: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]@@ -82,69 +63,39 @@wsd{:workspace_id workspace-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)}))))++(defn workspace-databases+ "Build the WorkspaceDatabase param maps for `database-ids` — each database must+ exist (404) and be eligible for workspaces (400) — with all of its known+ schemas as `input_schemas`. Fetches the databases and their schemas with one+ query each instead of per-id lookups."+ [database-ids]+ (let [dbs-by-id (when (seq database-ids)+ (t2/select-pk->fn identity :model/Database :id [:in database-ids]))+ schemas-by-id (workspace-database/database-input-schemas-by-id (keys dbs-by-id))]+ (mapv (fn [db-id]+ (let [database (or (get dbs-by-id db-id)+ (throw (ex-info "Database not found"+ {:status-code 404 :database_id db-id})))]+ (assert-database-eligible-for-workspaces database)+ {:database_id db-id+ :input_schemas (get schemas-by-id db-id [])}))+ database-ids)))+(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]}]+ The param map must supply `:creator_id`. Returns the created Workspace row."+ [{:keys [name target_branch creator_id databases]}](t2/with-transaction [_conn]- (let [workspace-id (t2/insert-returning-pk! :model/Workspace- {:name name- :creator_id creator_id})]+ (let [workspace (t2/insert-returning-instance! :model/Workspace+ {:name name+ :target_branch target_branch+ :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)))+ (map #(with-workspace-database-defaults % (:id workspace)) databases)))+ workspace)))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_database.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_database.cljindex 938c5bdf768..b095713ab6e 100644--- a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_database.clj+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_database.clj@@ -18,7 +18,8 @@(t2/deftransforms :model/WorkspaceDatabase{:database_details mi/transform-encrypted-json:input_schemas mi/transform-json- :status mi/transform-keyword})+ :status mi/transform-keyword+ :status_details mi/transform-encrypted-text});;; --------------------------------------- Permission predicates ---------------------------------------;;;@@ -58,14 +59,20 @@(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))+(defn database-input-schemas-by-id+ "Map of database id -> the distinct non-blank schema names of its active+ tables, sorted — one query for all `database-ids`. Schemaless drivers (e.g.+ MySQL) get no entry."+ [database-ids]+ (when (seq database-ids)+ (-> (group-by :db_id (t2/select [:model/Table :db_id :schema]+ :db_id [:in database-ids]+ :active true))+ (update-vals (fn [tables]+ (->> (into #{} (map :schema) tables)+ (remove #(or (nil? %) (= % "")))+ sort+ vec))))))(defenterprise reconcile-workspace-database-refs-before-delete!"Enterprise impl of the `:model/Database` before-delete hook. Refuses (409) when anydiff --git a/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.cljindex ce56fcfc9e4..64afa18119a 100644--- a/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj+++ b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning.clj@@ -1,304 +1,128 @@(ns metabase-enterprise.workspaces.provisioning+ "Top-level workspace provisioning orchestration: [[provision-workspace!]] and+ [[deprovision-workspace!]], recording progress in the workspace's+ `:status`/`:status_details` (see+ `:metabase-enterprise.workspaces.schema/workspace-status`). The API runs+ them in the background."(: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]+ [metabase-enterprise.workspaces.provisioning.database :as provisioning.database]+ [metabase-enterprise.workspaces.provisioning.instance :as provisioning.instance]+ [metabase-enterprise.workspaces.provisioning.remote-sync :as provisioning.remote-sync]+ [metabase-enterprise.workspaces.schema :as ws.schema]+ [metabase.util.malli :as mu][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)))+(set! *warn-on-reflection* true)++(mu/defn- set-initial-workspace-status! :- ::ws.schema/workspace+ "Atomically flip `workspace` to `status`, the first status of a run, unless+ its current status is in `disallowed-statuses`. The conditional UPDATE is+ the mutual-exclusion guard — two concurrent requests can never both start a+ run. Throws a 400 when the current status disallows the run. Returns the+ updated `workspace` copy."+ [workspace :- ::ws.schema/workspace+ status :- ::ws.schema/workspace-status+ disallowed-statuses :- [:set ::ws.schema/workspace-status]]+ (let [updated (t2/update! :model/Workspace+ :id (:id workspace)+ :status [:not-in disallowed-statuses]+ {:status status, :status_details nil})]+ (when (zero? updated)+ (throw (ex-info "The workspace status does not allow starting this run"+ {:status-code 400, :workspace-id (:id workspace)})))+ (assoc workspace :status status, :status_details nil)))++(mu/defn set-initial-workspace-provisioning-status! :- ::ws.schema/workspace+ "Atomically set `:database-provisioning`, the first status of the+ provisioning path; throws a 400 when a run is already in flight or the+ workspace is already `:provisioned`. Called before the background run+ starts so the run is immediately visible as in flight. Returns the updated+ `workspace` copy."+ [workspace :- ::ws.schema/workspace]+ (set-initial-workspace-status! workspace :database-provisioning+ (conj ws.schema/in-flight-statuses :provisioned)))++(mu/defn set-initial-workspace-deprovisioning-status! :- ::ws.schema/workspace+ "Atomically set `:instance-deprovisioning`, the first status of the+ deprovisioning path; throws a 400 when a run is already in flight or the+ workspace is already `:unprovisioned`. Called before the background run+ starts so the run is immediately visible as in flight. Returns the updated+ `workspace` copy."+ [workspace :- ::ws.schema/workspace]+ (set-initial-workspace-status! workspace :instance-deprovisioning+ (conj ws.schema/in-flight-statuses :unprovisioned)))++(defn- workspace-databases+ "The workspace's WorkspaceDatabase rows, in stable row order — the order the+ database phases provision and deprovision them in."+ [ws-id]+ (t2/select :model/WorkspaceDatabase :workspace_id ws-id {:order-by [[:id :asc]]}))++(mu/defn- set-workspace-status! :- ::ws.schema/workspace+ "Persist `status`/`status-details` on the row and return `workspace` with them+ assoc'ed, so callers can keep threading the updated copy."+ [workspace :- ::ws.schema/workspace+ status :- ::ws.schema/workspace-status+ status-details :- [:maybe :string]]+ (t2/update! :model/Workspace (:id workspace) {:status status, :status_details status-details})+ (assoc workspace :status status, :status_details status-details))++(mu/defn provision-workspace! :- ::ws.schema/workspace+ "Provision `workspace` (blocking): every database, then the git branch, then+ the child instance, ending `:provisioned`. Stops on the first failure — the+ phase's `*-failure` status and the error message land on the workspace —+ and rethrows. Retries skip work that already succeeded. Returns the updated+ workspace copy."+ [workspace :- ::ws.schema/workspace]+ (as-> workspace ws+ (set-workspace-status! ws :database-provisioning nil)+ (try+ (run! provisioning.database/provision-database! (workspace-databases (:id ws)))+ ws+ (catch Throwable t+ (set-workspace-status! ws :database-provisioning-failure (ex-message t))+ (throw t)))+ (set-workspace-status! ws :branch-provisioning nil)+ (try+ (provisioning.remote-sync/provision-branch! ws)+ (catch Throwable t+ (set-workspace-status! ws :branch-provisioning-failure (ex-message t))+ (throw t)))+ (set-workspace-status! ws :instance-provisioning nil)+ (try+ (-> ws+ provisioning.instance/deprovision-instance!+ provisioning.instance/provision-instance!)+ (catch Throwable t+ (set-workspace-status! ws :instance-provisioning-failure (ex-message t))+ (throw t)))+ (set-workspace-status! ws :provisioned nil)))++(mu/defn deprovision-workspace! :- ::ws.schema/workspace+ "Deprovision `workspace` (blocking): the child instance, then the git branch,+ then every database, ending `:unprovisioned`. Stops on the first failure —+ the phase's `*-failure` status and the error message land on the workspace —+ and rethrows. Retries skip work that already succeeded. Returns the updated+ workspace copy."+ [workspace :- ::ws.schema/workspace]+ (as-> workspace ws+ (set-workspace-status! ws :instance-deprovisioning nil)+ (try+ (provisioning.instance/deprovision-instance! ws)+ (catch Throwable t+ (set-workspace-status! ws :instance-deprovisioning-failure (ex-message t))+ (throw t)))+ (set-workspace-status! ws :branch-deprovisioning nil)+ (try+ (provisioning.remote-sync/deprovision-branch! ws)+ (catch Throwable t+ (set-workspace-status! ws :branch-deprovisioning-failure (ex-message t))+ (throw t)))+ (set-workspace-status! ws :database-deprovisioning nil)+ (try+ (run! provisioning.database/deprovision-database! (workspace-databases (:id ws)))+ ws+ (catch Throwable t+ (set-workspace-status! ws :database-deprovisioning-failure (ex-message t))+ (throw t)))+ (set-workspace-status! ws :unprovisioned nil)))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/provisioning/database.clj b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning/database.cljnew file mode 100644index 00000000000..2f19afb9140--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning/database.clj@@ -0,0 +1,225 @@+(ns metabase-enterprise.workspaces.provisioning.database+ "Warehouse-side provisioning for a single `:model/WorkspaceDatabase` row:+ creating/destroying the isolated schema + user via the driver+ workspace-isolation multimethods.++ Per-row lifecycle:++ provision+ unprovisioned ────────────► provisioning ──────► provisioned+ ▲ │ │+ │ failure ▼ │ deprovision+ │ provisioning-failure ▼+ │ deprovisioning+ │ │+ │ failure ▼+ └───────────────────────────◄──────── deprovisioning-failure (retry deprovision)++ Failures are recorded on the row (`:status_details` carries the error+ message); there are no rollbacks or retries — both operations may simply be+ invoked again from any status and no-op when there is nothing to do."+ (:require+ [metabase-enterprise.workspaces.models.table-remapping]+ [metabase-enterprise.workspaces.models.workspace-database]+ [metabase-enterprise.workspaces.schema :as ws.schema]+ [metabase.driver :as driver]+ [metabase.driver.connection :as driver.conn]+ [metabase.driver.sql :as driver.sql]+ [metabase.driver.util :as driver.u]+ [metabase.util.malli :as mu]+ [potemkin.types :as p]+ [toucan2.core :as t2]))++(comment metabase-enterprise.workspaces.models.table-remapping/keep-me+ metabase-enterprise.workspaces.models.workspace-database/keep-me)++;;; ------------------------------------------------- Protocol -------------------------------------------------------++(p/defprotocol+ DatabaseProvisioner+ "Wrapper around driver workspace-isolation multimethods for testability.+ The default [[database-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 database-provisioner+ "Default DatabaseProvisioner 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 DatabaseProvisioner+ (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)))))++;;; -------------------------------------------- Engine namespaces ---------------------------------------------------++(mu/defn engine-namespace-positions :- [:map [:db [:maybe :string]] [:schema [:maybe :string]]]+ "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 :- :map table :- [:maybe :map]]+ (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)})))++;;; ---------------------------------------------- Implementation ----------------------------------------------------++(defn- wsd-iso-workspace+ "The synthetic workspace map from which all warehouse identifiers for a+ WorkspaceDatabase row are derived. Every provision/deprovision path must build+ the exact same map — deprovision recomputes what provision computed."+ [wsd-id]+ {:id wsd-id :name (str "wsd-" wsd-id)})++(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 ""))))++(mu/defn provision-database! :- :nil+ "Provision an isolated output schema and user for one WorkspaceDatabase row+ (blocking). No-op when the row is already `:provisioned`, so retries are safe.+ State transitions: `:provisioning` -> `:provisioned` on success, or+ `:provisioning-failure` with the error message in `:status_details` on failure+ — no rollback, the error is rethrown and a later retry picks up from whatever+ state the warehouse is in."+ ([workspace-database]+ (provision-database! workspace-database database-provisioner))+ ([{wsd-id :id :as wsd} :- ::ws.schema/workspace-database+ provisioner]+ (when-not (= :provisioned (:status wsd))+ (t2/update! :model/WorkspaceDatabase wsd-id {:status :provisioning, :status_details nil})+ (try+ (let [db (t2/select-one :model/Database :id (:database_id wsd))+ driver (driver.u/database->driver db)+ workspace (wsd-iso-workspace wsd-id)+ ws-details (merge workspace (details provisioner driver db workspace))]+ (init! provisioner driver db ws-details)+ (grant! provisioner driver db ws-details (vec (:input_schemas wsd)))+ (t2/update! :model/WorkspaceDatabase wsd-id+ {:output_namespace (:schema ws-details)+ :database_details (:database_details ws-details)+ :status :provisioned}))+ (catch Throwable t+ (t2/update! :model/WorkspaceDatabase wsd-id+ {:status :provisioning-failure, :status_details (ex-message t)})+ (throw t))))+ nil))++(mu/defn deprovision-database! :- :nil+ "Tear down the warehouse isolation of one WorkspaceDatabase row (blocking).+ No-op when the row is already `:unprovisioned`, so retries are safe. Works+ from ANY other state: warehouse identifiers are the persisted ones, or+ recomputed deterministically from the row id via the provisioner's `details`+ for a crashed `:provisioning` row that never recorded anything.++ State transitions: `:deprovisioning` -> `:unprovisioned` on success (the+ persisted identifiers are cleared), or `:deprovisioning-failure` with the+ error message in `:status_details` on failure — the error is rethrown and the+ deprovision can be retried.++ App-DB `TableRemapping` rows for the row's iso namespace are ALWAYS cleared,+ even when the warehouse teardown fails partway — stale remappings would+ rewrite queries to a dropped schema and 500 the QP."+ ([workspace-database]+ (deprovision-database! workspace-database database-provisioner))+ ([{wsd-id :id :as wsd} :- ::ws.schema/workspace-database+ provisioner]+ (when-not (= :unprovisioned (:status wsd))+ (t2/update! :model/WorkspaceDatabase wsd-id {:status :deprovisioning, :status_details nil})+ (try+ (let [db (t2/select-one :model/Database :id (:database_id wsd))+ driver (driver.u/database->driver db)+ iso-ws (wsd-iso-workspace wsd-id)+ 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)+ (finally+ (clear-mappings-for-iso! db (:database_id wsd) schema)))+ (t2/update! :model/WorkspaceDatabase wsd-id+ {:output_namespace ""+ :database_details {}+ :status :unprovisioned}))+ (catch Throwable t+ (t2/update! :model/WorkspaceDatabase wsd-id+ {:status :deprovisioning-failure, :status_details (ex-message t)})+ (throw t))))+ nil))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/provisioning/instance.clj b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning/instance.cljnew file mode 100644index 00000000000..5177c33bee2--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning/instance.clj@@ -0,0 +1,128 @@+(ns metabase-enterprise.workspaces.provisioning.instance+ "Child-instance provisioning for a workspace: creating/destroying the+ developer Metabase instance a workspace's users work in, and persisting its+ identifiers (`instance_id`/`instance_url`) on the `:model/Workspace` row.++ The default [[instance-provisioner]] is a stub — the real (Harbormaster)+ implementation comes later."+ (:require+ [metabase-enterprise.workspaces.config :as ws.config]+ [metabase-enterprise.workspaces.schema :as ws.schema]+ [metabase.util.jvm :as u.jvm]+ [metabase.util.malli :as mu]+ [potemkin.types :as p]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(p/defprotocol+ InstanceProvisioner+ "Provisioning operations for a workspace's child Metabase instance.+ The default [[instance-provisioner]] is a stub; tests reify custom+ implementations that fail on demand, count calls, etc."+ (create! [this workspace config]+ "Start creating a child instance for `workspace`, booted from the+ config-file map `config` (see+ [[metabase-enterprise.workspaces.config/build-workspace-config]]).+ Creation is asynchronous: returns `::ws.schema/instance` whose `:status`+ may still be `:creating` — the instance is not usable until [[fetch]]+ reports `:active`.")+ (fetch [this instance-id]+ "Fetch a child instance by id. Returns `::ws.schema/instance`.")+ (delete! [this instance-id]+ "Delete the child instance with `instance-id`. Should be idempotent."))++(def instance-provisioner+ "Default InstanceProvisioner. Stub implementation for now: pretends a child+ instance was created by returning a random id and a placeholder url that+ are immediately `:active`."+ (reify InstanceProvisioner+ (create! [_ _workspace _config]+ {:id (str (random-uuid)), :url "https://example.com", :status :active})+ (fetch [_ instance-id]+ {:id instance-id, :url "https://example.com", :status :active})+ (delete! [_ _instance-id]+ nil)))++(def instance-poll-interval-ms+ "How often [[provision-instance!]] polls the provisioner while the child+ instance is still coming up."+ 5000)++(def instance-poll-timeout-ms+ "How long [[provision-instance!]] waits for the child instance to reach a+ terminal status before giving up."+ (* 10 60 1000))++(mu/defn- wait-for-instance :- ::ws.schema/instance+ "Poll `provisioner` until `instance-id` reaches a terminal status and return+ the instance. Throws when it lands anywhere but `:active`, or when+ [[instance-poll-timeout-ms]] elapses first."+ [provisioner instance-id :- :string]+ (let [{:keys [status] :as instance}+ (u.jvm/poll {:thunk #(fetch provisioner instance-id)+ :done? #(contains? #{:active :error} (:status %))+ :interval-ms instance-poll-interval-ms+ :timeout-ms instance-poll-timeout-ms})]+ (when-not instance+ (throw (ex-info "Timed out waiting for the workspace instance to start"+ {:instance_id instance-id, :timeout-ms instance-poll-timeout-ms})))+ (when-not (= :active status)+ (throw (ex-info "Workspace instance failed to start"+ {:instance_id instance-id, :status status})))+ instance))++(mu/defn- create-instance! :- ::ws.schema/workspace+ "Create a fresh child instance for `workspace` (blocking). Persists+ `:instance_id` (clearing any stale url) as soon as the provisioner accepts+ the creation, then polls until the instance is `:active` and persists the+ url the active instance reports. Throws when the instance lands in `:error`+ or the startup times out. Returns `workspace` with the new instance fields+ assoc'ed."+ [workspace :- ::ws.schema/workspace+ provisioner]+ (let [config (-> workspace+ (t2/hydrate :databases)+ ws.config/build-workspace-config)+ {:keys [id]} (create! provisioner workspace config)]+ (t2/update! :model/Workspace (:id workspace) {:instance_id id, :instance_url nil})+ (let [url (:url (wait-for-instance provisioner id))]+ (t2/update! :model/Workspace (:id workspace) {:instance_url url})+ (assoc workspace :instance_id id, :instance_url url))))++(mu/defn- delete-instance! :- ::ws.schema/workspace+ "Delete `workspace`'s child instance (blocking) and clear its+ `:instance_id`/`:instance_url`. Throws on failure — the caller records the+ failure on the workspace status. Returns `workspace` with the instance+ fields cleared."+ [workspace :- ::ws.schema/workspace+ provisioner]+ (delete! provisioner (:instance_id workspace))+ (t2/update! :model/Workspace (:id workspace) {:instance_id nil, :instance_url nil})+ (assoc workspace :instance_id nil, :instance_url nil))++(mu/defn provision-instance! :- ::ws.schema/workspace+ "Provision a fresh child instance for `workspace` (blocking) — callers+ deprovision any existing one first, so every run starts from a clean state.+ Throws on failure or startup timeout — the caller records the failure on+ the workspace status. Returns `workspace` with the new instance fields+ assoc'ed."+ ([workspace]+ (provision-instance! workspace instance-provisioner))+ ([workspace :- ::ws.schema/workspace+ provisioner]+ (create-instance! workspace provisioner)))++(mu/defn deprovision-instance! :- ::ws.schema/workspace+ "Delete `workspace`'s child instance (blocking) and clear its+ `:instance_id`/`:instance_url`. No-op when the workspace has no instance, so+ retries are safe. Throws on failure — the caller records the failure on the+ workspace status. Returns `workspace` with the instance fields cleared."+ ([workspace]+ (deprovision-instance! workspace instance-provisioner))+ ([workspace :- ::ws.schema/workspace+ provisioner]+ (if-not (:instance_id workspace)+ (do+ (t2/update! :model/Workspace (:id workspace) {:instance_url nil})+ (assoc workspace :instance_url nil))+ (delete-instance! workspace provisioner))))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/provisioning/remote_sync.clj b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning/remote_sync.cljnew file mode 100644index 00000000000..cc5713e8b5f--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/provisioning/remote_sync.clj@@ -0,0 +1,40 @@+(ns metabase-enterprise.workspaces.provisioning.remote-sync+ "Git-branch provisioning for workspaces. A workspace with a `:target_branch`+ owns a branch on the remote-sync repo: provisioning creates it (idempotent —+ an existing branch is fine), deprovisioning deletes it. `:target_branch`+ itself never changes. Both phases are no-ops when remote sync is disabled+ or the workspace has no branch."+ (:require+ [clojure.string :as str]+ [metabase-enterprise.remote-sync.core :as remote-sync]+ [metabase-enterprise.workspaces.schema :as ws.schema]+ [metabase.util.malli :as mu]))++(set! *warn-on-reflection* true)++(defn- workspace-branch [workspace]+ (let [branch (:target_branch workspace)]+ (when-not (str/blank? branch)+ branch)))++(mu/defn provision-branch! :- ::ws.schema/workspace+ "Create the workspace's `:target_branch` on the remote-sync repo. A no-op+ when remote sync is disabled, the workspace has no branch, or the branch+ already exists. Returns `workspace` unchanged."+ [workspace :- ::ws.schema/workspace]+ (when-let [branch (workspace-branch workspace)]+ (when (and (remote-sync/remote-sync-enabled)+ (not (remote-sync/branch-exists? branch)))+ (remote-sync/create-branch! branch)))+ workspace)++(mu/defn deprovision-branch! :- ::ws.schema/workspace+ "Delete the workspace's `:target_branch` from the remote-sync repo. A no-op+ when remote sync is disabled at this point or the workspace has no branch.+ `:target_branch` itself is kept as is — a later provision recreates the+ branch. Returns `workspace` unchanged."+ [workspace :- ::ws.schema/workspace]+ (when-let [branch (workspace-branch workspace)]+ (when (remote-sync/remote-sync-enabled)+ (remote-sync/delete-branch! branch)))+ workspace)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/schema.clj b/enterprise/backend/src/metabase_enterprise/workspaces/schema.cljnew file mode 100644index 00000000000..0830623ceea--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/schema.clj@@ -0,0 +1,93 @@+(ns metabase-enterprise.workspaces.schema+ "Malli schemas shared across the workspaces module."+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::workspace-status+ "Lifecycle status of a `:model/Workspace`. Deliberately kept separate from+ `::workspace-database-status` — the workspace lifecycle spans more phases+ than a single database's.++ Provisioning path (databases, then the git branch, then the child instance):++ unprovisioned ──► database-provisioning ──► branch-provisioning ──► instance-provisioning ──► provisioned+ │ │ │+ failure ▼ failure ▼ failure ▼+ database-provisioning-failure branch-provisioning-failure instance-provisioning-failure++ Deprovisioning path (the child instance, then the git branch, then databases):++ provisioned ──► instance-deprovisioning ──► branch-deprovisioning ──► database-deprovisioning ──► unprovisioned+ │ │ │+ failure ▼ failure ▼ failure ▼+ instance-deprovisioning-failure branch-deprovisioning-failure database-deprovisioning-failure++ `/provision` may be started from any settled status except `:provisioned`;+ `/deprovision` from any settled status except `:unprovisioned` (both 400+ otherwise, including while a run is in flight). The database phases skip+ rows that already reached the target state; the branch phases are no-ops+ when remote sync is disabled or the workspace has no `:target_branch`; the+ instance phase of a provision always deletes and recreates the child+ instance."+ [:enum+ :unprovisioned+ :database-provisioning+ :database-provisioning-failure+ :branch-provisioning+ :branch-provisioning-failure+ :instance-provisioning+ :instance-provisioning-failure+ :provisioned+ :instance-deprovisioning+ :instance-deprovisioning-failure+ :branch-deprovisioning+ :branch-deprovisioning-failure+ :database-deprovisioning+ :database-deprovisioning-failure])++(def in-flight-statuses+ "The workspace statuses that mark a provision or deprovision run as currently+ executing. All other statuses are settled — a new run may start from them."+ #{:database-provisioning :branch-provisioning :instance-provisioning+ :instance-deprovisioning :branch-deprovisioning :database-deprovisioning})++(mr/def ::instance-status+ "Status of a workspace's child instance: `:creating` — still coming up, keep+ polling; `:active` — up and usable; `:error` — terminal, stop waiting."+ [:enum :creating :active :error])++(mr/def ::instance+ "A workspace's child instance as reported by an InstanceProvisioner. The url+ is only set once the instance is `:active`."+ [:map+ [:id :string]+ [:url [:maybe :string]]+ [:status ::instance-status]])++(mr/def ::workspace-database-status+ "Lifecycle status of a `:model/WorkspaceDatabase`. The `*-failure` statuses are+ terminal until the next provision/deprovision retry."+ [:enum+ :unprovisioned+ :provisioning+ :provisioning-failure+ :provisioned+ :deprovisioning+ :deprovisioning-failure])++(mr/def ::workspace+ "A `:model/Workspace` row."+ [:map+ [:id ms/PositiveInt]+ [:name ms/NonBlankString]+ [:status {:optional true} ::workspace-status]+ [:status_details {:optional true} [:maybe :string]]+ [:instance_id {:optional true} [:maybe :string]]+ [:instance_url {:optional true} [:maybe :string]]])++(mr/def ::workspace-database+ "A `:model/WorkspaceDatabase` row."+ [:map+ [:id ms/PositiveInt]+ [:status {:optional true} ::workspace-database-status]])diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/settings.clj b/enterprise/backend/src/metabase_enterprise/workspaces/settings.cljindex 7df12766529..7ced87b5973 100644--- a/enterprise/backend/src/metabase_enterprise/workspaces/settings.clj+++ b/enterprise/backend/src/metabase_enterprise/workspaces/settings.clj@@ -3,10 +3,6 @@[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@@ -17,8 +13,18 @@:export? false:database-local :only)+(defsetting workspace-instance-user-password+ (deferred-tru "Default password for the workspace creator''s superuser account on workspace child instances.")+ :type :string+ :visibility :admin+ :encryption :when-encryption-key-set+ :sensitive? true+ :audit :never+ :export? false+ :doc false)+(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`.")+ (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.instance/instance-workspace`."):type :json:encryption :no:visibility :internaldiff --git a/enterprise/backend/src/metabase_enterprise/workspaces/table_remapping.clj b/enterprise/backend/src/metabase_enterprise/workspaces/table_remapping.cljindex 77f746a546b..300bb0a6063 100644--- a/enterprise/backend/src/metabase_enterprise/workspaces/table_remapping.clj+++ b/enterprise/backend/src/metabase_enterprise/workspaces/table_remapping.clj@@ -46,8 +46,9 @@right slots from a `:model/Database` and `:model/Table` row."(:require[clojure.set :as set]- [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.instance :as ws.instance][metabase-enterprise.workspaces.models.table-remapping]+ [metabase-enterprise.workspaces.provisioning.database :as provisioning.database][metabase-enterprise.workspaces.remapping.core :as ws.remapping][metabase.driver :as driver][metabase.driver.connection :as driver.conn]@@ -86,10 +87,10 @@(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`."+ [[provisioning.database/engine-namespace-positions]]. Only meaningful for+ drivers whose `qualified-name-components` includes `:db`."[database]- (:db (ws/engine-namespace-positions database)))+ (:db (provisioning.database/engine-namespace-positions database)))(mu/defn spec-for-table :- ::table-spec"Return `{:db, :schema, :table}` for `table` in `database`, populating only the@@ -111,7 +112,7 @@[database :- [:map [:engine :keyword]]table :- [:map [:name :string]]](let [components (set (driver/qualified-name-components (:engine database)))- positions (ws/engine-namespace-positions database table)]+ positions (provisioning.database/engine-namespace-positions database table)]{:db (normalize-level (when (:db components) (:db positions))):schema (normalize-level (when (:schema components) (:schema positions))):table (:name table)}))@@ -325,8 +326,8 @@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)})]+ (let [positions (provisioning.database/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)))))@@ -612,7 +613,7 @@- 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`,+ - To-side: workspace output namespace comes from `ws.instance/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@@ -624,9 +625,9 @@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 is a programming error; gate on [[ws.instance/db-workspace-namespace]] first."[db-id target]- (let [workspace-ns (ws/db-workspace-namespace db-id)]+ (let [workspace-ns (ws.instance/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})))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/transform_hooks.clj b/enterprise/backend/src/metabase_enterprise/workspaces/transform_hooks.cljindex 4e4b6dfa905..39d00267eb0 100644--- a/enterprise/backend/src/metabase_enterprise/workspaces/transform_hooks.clj+++ b/enterprise/backend/src/metabase_enterprise/workspaces/transform_hooks.clj@@ -1,7 +1,7 @@(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.instance :as ws.instance][metabase-enterprise.workspaces.remapping.core :as ws.remapping][metabase-enterprise.workspaces.table-remapping :as ws.table-remapping][metabase.premium-features.core :refer [defenterprise]]))@@ -34,7 +34,7 @@provisioned, the rewrite must apply.":feature :none[db-id target]- (if (some? (ws/db-workspace-namespace db-id))+ (if (some? (ws.instance/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 valuesdiff --git a/enterprise/backend/test/metabase_enterprise/advanced_config/api_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_config/api_test.cljindex 247b4997f14..0c388014554 100644--- a/enterprise/backend/test/metabase_enterprise/advanced_config/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/advanced_config/api_test.clj@@ -1,10 +1,13 @@(ns metabase-enterprise.advanced-config.api-test(:require[clojure.test :refer [deftest is testing use-fixtures]]+ [metabase-enterprise.advanced-config.file :as advanced-config.file][metabase-enterprise.workspaces.core :as ws][metabase.driver.util]+ [metabase.setup.core :as setup][metabase.test :as mt][metabase.test.fixtures :as fixtures]+ [metabase.util :as u][metabase.util.yaml :as yaml][toucan2.core :as t2]))@@ -15,9 +18,13 @@(defn- yaml-bytes ^bytes [body](.getBytes ^String (yaml/generate-string body) "UTF-8"))-(defn- multipart [bs]- [{:request-options {:headers {"content-type" "multipart/form-data"}}}- {:config bs}])+(defn- multipart+ ([bs]+ (multipart bs nil))+ ([bs api-key]+ [{:request-options {:headers (cond-> {"content-type" "multipart/form-data"}+ api-key (assoc "x-metabase-apikey" api-key))}}+ {:config bs}]))(deftest superuser-only-test(testing "POST /api/ee/advanced-config requires superuser"@@ -25,6 +32,25 @@(apply mt/user-http-request :rasta :post 403 "ee/advanced-config/"(multipart (yaml-bytes {:version 1 :config {}})))))))+(deftest external-requires-static-api-key-test+ (testing "POST /api/ee/advanced-config/external"+ (let [payload (yaml-bytes {:version 1 :config {}})]+ (testing "403 when MB_API_KEY is not set"+ (mt/with-temporary-setting-values [api-key nil]+ (apply mt/client :post 403 "ee/advanced-config/external"+ (multipart payload "anything"))))+ (mt/with-temporary-setting-values [api-key "test-api-key"]+ (testing "403 without the header"+ (apply mt/client :post 403 "ee/advanced-config/external"+ (multipart payload)))+ (testing "403 with a wrong key"+ (apply mt/client :post 403 "ee/advanced-config/external"+ (multipart payload "wrong-key")))+ (testing "applies the config with the correct key, no user session needed"+ (mt/with-premium-features #{:config-text-file}+ (apply mt/client :post 204 "ee/advanced-config/external"+ (multipart payload "test-api-key"))))))))+(deftest does-not-expand-templates-test(testing "POST /api/ee/advanced-config does NOT expand `{{env VAR}}` templates";; The literal template appears in the inserted database's name; if env@@ -76,34 +102,6 @@(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@@ -116,3 +114,51 @@(mt/user-http-request :crowberto :post 402 "ee/advanced-config/"(first (multipart (yaml-bytes payload)))(second (multipart (yaml-bytes payload)))))))))++(deftest config-upload-user-entry-test+ (testing "POST /api/ee/advanced-config with a users entry creates the user"+ (mt/with-premium-features #{:config-text-file}+ (mt/with-model-cleanup [:model/User]+ (let [email (u/lower-case-en (str (mt/random-name) "@config.test"))]+ (apply mt/user-http-request :crowberto :post 204 "ee/advanced-config/"+ (multipart (yaml-bytes {:version 1+ :config {:users [{:first_name "Work"+ :last_name "Space"+ :email email+ :password "sUp3r-s3cret1"+ :is_superuser true}]}})))+ (is (=? {:is_superuser true}+ (t2/select-one :model/User :email email))+ "the user from the config lands as a superuser"))))))++(deftest config-user-entry-completes-setup-test+ (testing "a users entry completes the initial setup on an empty instance (has-user-setup flips)"+ (mt/with-empty-h2-app-db!+ (mt/with-premium-features #{:config-text-file}+ (binding [advanced-config.file/*supported-versions* {:min 1 :max 1}]+ (is (false? (setup/has-user-setup)))+ (advanced-config.file/initialize!+ {:version 1+ :config {:users [{:first_name "First"+ :last_name "Last"+ :email "[email protected]"+ :password "sUp3r-s3cret1"+ :is_superuser true}]}})+ (is (true? (setup/has-user-setup)))+ (is (=? {:is_superuser true}+ (t2/select-one :model/User :email "[email protected]"))))))))++(deftest config-user-entry-allows-nil-names-test+ (testing "users entries without a first or last name are allowed"+ (mt/with-premium-features #{:config-text-file}+ (mt/with-model-cleanup [:model/User]+ (let [email (u/lower-case-en (str (mt/random-name) "@config.test"))]+ (binding [advanced-config.file/*supported-versions* {:min 1 :max 1}]+ (advanced-config.file/initialize!+ {:version 1+ :config {:users [{:first_name nil+ :last_name nil+ :email email+ :password "sUp3r-s3cret1"}]}}))+ (is (=? {:first_name nil, :last_name nil}+ (t2/select-one :model/User :email email))))))))diff --git a/enterprise/backend/test/metabase_enterprise/advanced_config/file/databases_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_config/file/databases_test.cljindex 01fb1020f6e..b8820227ac3 100644--- a/enterprise/backend/test/metabase_enterprise/advanced_config/file/databases_test.clj+++ b/enterprise/backend/test/metabase_enterprise/advanced_config/file/databases_test.clj@@ -9,7 +9,7 @@[metabase.sync.sync-metadata :as sync-metadata][metabase.test :as mt][metabase.util :as u]- [metabase.util.quick-task :as quick-task]+ [metabase.util.jvm :as u.jvm][toucan2.core :as t2])(:import(clojure.lang ExceptionInfo)))@@ -84,7 +84,7 @@(mt/with-temporary-setting-values [config-from-file-sync-databases true](try(let [submit-calls (atom 0)]- (with-redefs [quick-task/submit-task! (fn [_] (swap! submit-calls inc))]+ (with-redefs [u.jvm/run-in-virtual-thread (fn [_] (swap! submit-calls inc))](testing "config entry with :is_stub true and empty :details is accepted"(is (= :ok(advanced-config.file/initialize!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.cljindex 9e37f4ff24b..31481cfa5b6 100644--- a/enterprise/backend/test/metabase_enterprise/advanced_config/file/workspace_test.clj+++ b/enterprise/backend/test/metabase_enterprise/advanced_config/file/workspace_test.clj@@ -5,9 +5,11 @@[clojure.test :refer [deftest is testing use-fixtures]][metabase-enterprise.advanced-config.file :as advanced-config.file][metabase-enterprise.advanced-config.file.workspace :as advanced-config.file.workspace]- [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.remote-sync.core :as remote-sync]+ [metabase-enterprise.workspaces.instance :as ws.instance][metabase.test :as mt][metabase.test.fixtures :as fixtures]+ [metabase.util.jvm :as u.jvm][metabase.util.yaml :as yaml])(:import(clojure.lang ExceptionInfo)))@@ -19,17 +21,12 @@;; 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))))))))+ (try+ (thunk)+ (finally+ (ws.instance/clear-instance-workspace!)))))))(defn- load-fixture-by-driver [driver](-> (str "metabase_enterprise/workspaces/resources/workspace_config_" (name driver) ".yml")@@ -87,7 +84,7 @@(is (= "New workspace" workspace-name))(is (= 1 database-count))(testing "setting holds the parsed config"- (let [stored (ws/instance-workspace)]+ (let [stored (ws.instance/instance-workspace)](is (= "New workspace" (:name stored)))(is (= ["public"](get-in stored [:databases db-id :input_schemas])))@@ -105,12 +102,12 @@: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))))+ (is (= "New workspace" (:name (ws.instance/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 (= "Renamed Workspace" (:name (ws.instance/instance-workspace))))+ (is (= 1 (count (:databases (ws.instance/instance-workspace)))))(is (= {:schema "different_schema"}- (->> (ws/instance-workspace) :databases vals first :output))+ (->> (ws.instance/instance-workspace) :databases vals first :output))"setting reflects the new config, not the old one"))))))(deftest unknown-database-name-throws-test@@ -128,7 +125,7 @@(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)))))))+ (is (= {:schema "mb__isolation_44490_1933"} (ws.instance/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"@@ -136,7 +133,7 @@;; 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)))))))+ (is (nil? (ws.instance/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)"@@ -182,10 +179,10 @@(advanced-config.file/initialize!{:version 1:config {:workspace (workspace-section "ws-test-db")}})- (is (= "New workspace" (:name (ws/instance-workspace)))+ (is (= "New workspace" (:name (ws.instance/instance-workspace)))"the workspace section should have been applied")(is (= {:schema "mb__isolation_44490_1933"}- (ws/db-workspace-namespace db-id))))))))+ (ws.instance/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)"@@ -195,7 +192,7 @@{:version 1:config {:users [{:first_name "X" :last_name "Y":email "[email protected]" :password "pw"}]}})- (is (nil? (ws/instance-workspace))+ (is (nil? (ws.instance/instance-workspace))"no workspace section means no workspace state was set")))));;; ----------------------------------------- per-driver shapes -----------------------------------------@@ -212,7 +209,7 @@{:name "ws":databases {:mysql-prod {:input_schemas ["prod_db"]:output {:db "ws_alice"}}}})- (is (= {:db "ws_alice"} (ws/db-workspace-namespace db-id))))))))+ (is (= {:db "ws_alice"} (ws.instance/db-workspace-namespace db-id))))))))(deftest bigquery-3-slot-section-test(testing "BigQuery workspace: project + dataset in the :output map"@@ -222,7 +219,7 @@{: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)))))))+ (is (= {:db "metabase-prod" :schema "ws_alice"} (ws.instance/db-workspace-namespace db-id)))))));;; ----------------------------------------- per-driver YAML fixtures -----------------------------------------;;;@@ -298,54 +295,47 @@:engine (:engine expectations):details (:details expectations)}](advanced-config.file.workspace/apply-workspace-section! section)- (is (= (:output expectations) (ws/db-workspace-namespace db-id))+ (is (= (:output expectations) (ws.instance/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!+(deftest workspace-with-remote-settings-starts-import-test+ (testing "a config with both a :workspace section and remote-sync settings starts the initial import"+ (mt/with-premium-features #{:workspaces :config-text-file}(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?)))))))+ (mt/with-temporary-setting-values [remote-sync-url nil+ remote-sync-branch nil+ remote-sync-token nil]+ (let [started (atom 0)]+ (with-redefs [u.jvm/run-in-virtual-thread (fn [thunk] (thunk) nil)+ remote-sync/start-import! (fn [] (swap! started inc) nil)]+ (advanced-config.file/initialize!+ {:version 1+ :config {:settings {:remote-sync-url "https://git.example.com/test.git"+ :remote-sync-branch "main"+ :remote-sync-token "s3cr3t"}+ :workspace (workspace-section "ws-test-db")}})+ (is (= 1 @started)))))))+ (testing "but not without remote-sync settings"+ (mt/with-premium-features #{:workspaces :config-text-file}+ (mt/with-temp [:model/Database _ {:name "ws-test-db" :engine :postgres}]+ (let [started (atom 0)]+ (with-redefs [u.jvm/run-in-virtual-thread (fn [thunk] (thunk) nil)+ remote-sync/start-import! (fn [] (swap! started inc) nil)]+ (advanced-config.file/initialize!+ {:version 1+ :config {:workspace (workspace-section "ws-test-db")}})+ (is (zero? @started)))))))+ (testing "and not without a :workspace section"+ (mt/with-premium-features #{:workspaces :config-text-file}+ (mt/with-temporary-setting-values [remote-sync-url nil+ remote-sync-branch nil+ remote-sync-token nil]+ (let [started (atom 0)]+ (with-redefs [u.jvm/run-in-virtual-thread (fn [thunk] (thunk) nil)+ remote-sync/start-import! (fn [] (swap! started inc) nil)]+ (advanced-config.file/initialize!+ {:version 1+ :config {:settings {:remote-sync-url "https://git.example.com/test.git"+ :remote-sync-branch "main"+ :remote-sync-token "s3cr3t"}}})+ (is (zero? @started)))))))))diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.cljindex 27952cf7210..a9f24dac432 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj@@ -28,6 +28,8 @@(reify source.p/Source(create-branch [_ _branch _base]nil)+ (delete-branch [_ _branch]+ nil)(branches [_](if error-on-branches?(throw (Exception. "Repository not found"))@@ -1496,6 +1498,7 @@(reify source.p/Source(branches [_] ["main"])(create-branch [_ _ _] nil)+ (delete-branch [_ _] nil)(default-branch [_] "main")(snapshot [_](throw (ex-info (str "Invalid branch: " branch)@@ -1556,6 +1559,7 @@(let [failing-source (reify source.p/Source(branches [_] ["main"])(create-branch [_ _ _] nil)+ (delete-branch [_ _] nil)(default-branch [_] "main")(snapshot [_] (throw (RuntimeException. "boom")))(snapshot-at [_ _version] (throw (RuntimeException. "boom"))))]diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.cljindex d5cf43bbfed..7806b031cd9 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj@@ -1803,6 +1803,7 @@ serdes/meta:(reify source.p/Source(branches [_] ["main"])(create-branch [_ _ _] nil)+ (delete-branch [_ _] nil)(default-branch [_] "main")(snapshot [_] (export-test-snapshot "remote-R"))(snapshot-at [_ v] (export-test-snapshot v))))@@ -1887,6 +1888,7 @@ serdes/meta:no-resolve-source (reify source.p/Source(branches [_] ["main"])(create-branch [_ _ _] nil)+ (delete-branch [_ _] nil)(default-branch [_] "main")(snapshot [_] (export-test-snapshot "remote-R"))(snapshot-at [_ _] nil))]@@ -2004,6 +2006,7 @@ serdes/meta:(let [no-base-source (reify source.p/Source(branches [_] ["main"])(create-branch [_ _ _] nil)+ (delete-branch [_ _] nil)(default-branch [_] "main")(snapshot [_] (export-test-snapshot "remote-R"))(snapshot-at [_ _] nil))]diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/source/git_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/source/git_test.cljindex d346abb3cb2..2c20689bf38 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/source/git_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/source/git_test.clj@@ -160,6 +160,16 @@(git-working-create-branch! _remote "branch-3")(is (= ["branch-1" "branch-2" "branch-3" "master"] (source.p/branches source))))))+(deftest delete-branch-test+ (mt/with-temp-dir [remote-dir nil]+ (let [[source _remote] (init-source! "master" remote-dir :branches ["branch-1" "branch-2"])]+ (testing "deletes the branch on the remote"+ (source.p/delete-branch source "branch-1")+ (is (= ["branch-2" "master"] (source.p/branches source))))+ (testing "deleting a branch that does not exist is a no-op"+ (source.p/delete-branch source "no-such-branch")+ (is (= ["branch-2" "master"] (source.p/branches source)))))))+(deftest snapshot(mt/with-temp-dir [remote-dir nil](let [[master _remote] (init-source! "master" remote-dirdiff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/source/wrapping_source_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/source/wrapping_source_test.cljindex 31895f28fe7..609f4ce286e 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/source/wrapping_source_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/source/wrapping_source_test.clj@@ -23,6 +23,9 @@(create-branch [_ _branch _base]nil)+ (delete-branch [_ _branch]+ nil)+(branches [_]["main"])diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/test_helpers.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/test_helpers.cljindex 1b1e3325c39..52e95d24b7e 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/test_helpers.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/test_helpers.clj@@ -234,6 +234,10 @@ width: fixed(create-branch [_this branch _base](swap! branches-atom conj [branch (str branch "-ref")]))+ (delete-branch [_this branch]+ (swap! branches-atom (fn [branches] (vec (remove #(= (first %) branch) branches))))+ nil)+(branches [_this](case fail-mode:branches-error (throw (java.net.UnknownHostException. "Network error"))@@ -342,6 +346,7 @@ width: fixed(reify source.p/Source(branches [_] [branch])(create-branch [_ _ _] nil)+ (delete-branch [_ _] nil)(default-branch [_] branch)(snapshot [_] (mk-snapshot (:current @state)))(snapshot-at [_ v] (when (contains? (:trees @state) v) (mk-snapshot v))))))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.cljindex c77466279a7..d219bdca5b3 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_instance_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_instance_test.clj@@ -4,8 +4,7 @@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-enterprise.workspaces.instance :as ws.instance][metabase.test :as mt][metabase.test.fixtures :as fixtures][toucan2.core :as t2]))@@ -18,7 +17,7 @@(try(thunk)(finally- (ws/clear-instance-workspace!))))))+ (ws.instance/clear-instance-workspace!))))))(deftest current-superuser-only-test(testing "GET /ee/workspace-instance/current requires data analyst or superuser"@@ -27,20 +26,18 @@(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)]+ (with-redefs [ws.instance/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"}}}})]+ (with-redefs [ws.instance/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)@@ -50,111 +47,6 @@(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."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.cljindex fa00f51cc26..e53f349be01 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_manager_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/api/workspace_manager_test.clj@@ -1,144 +1,221 @@-(ns metabase-enterprise.workspaces.api.workspace-manager-test+(ns ^:synchronous metabase-enterprise.workspaces.api.workspace-manager-test"HTTP smoke tests for the workspace-manager API. Permission rules are exercised at themodel level — see workspace_test.clj and workspace_database_test.clj. These tests justverify routing, request/response shape, and that the model-level permission predicates- are wired into the endpoints (one 403 spot-check is enough)."+ are wired into the endpoints (one 403 spot-check is enough).++ A `use-fixtures`-installed `with-redefs` (hence `^:synchronous`) replaces the+ real DatabaseProvisioner and InstanceProvisioner with stubs and makes the+ `/provision`/`/deprovision` background execution synchronous, so responses+ already reflect the final status; individual tests override the provisioners+ with their own reify via an inner `with-redefs`."(:require[clojure.test :refer [deftest is testing use-fixtures]]- [metabase-enterprise.workspaces.provisioning :as provisioning]+ [metabase-enterprise.workspaces.provisioning.database :as provisioning.database]+ [metabase-enterprise.workspaces.provisioning.instance :as provisioning.instance][metabase.permissions.test-util :as perms.test-util][metabase.test :as mt][metabase.test.fixtures :as fixtures]+ [metabase.util.jvm :as u.jvm][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+(defn- stub-database-provisioner []+ (reify provisioning.database/DatabaseProvisioner(details [_ _ _ _]{:schema "mb_iso_stub" :database_details {:user "stub_user" :password "stub_pass"}})(init! [_ _ _ _] nil)(grant! [_ _ _ _ _] nil)(destroy! [_ _ _ _] nil)))+(defn- stub-instance-provisioner []+ (reify provisioning.instance/InstanceProvisioner+ (create! [_ _workspace _config]+ {:id (str (random-uuid)) :url "https://example.com" :status :active})+ (fetch [_ instance-id]+ {:id instance-id :url "https://example.com" :status :active})+ (delete! [_ _instance-id] nil)))++(use-fixtures :once+ (fixtures/initialize :db)+ (fn [thunk]+ (mt/with-premium-features #{:workspaces}+ (with-redefs [provisioning.database/database-provisioner (stub-database-provisioner)+ provisioning.instance/instance-provisioner (stub-instance-provisioner)+ u.jvm/run-in-virtual-thread (fn [work]+ (try (work) (catch Throwable _))+ nil)]+ (thunk)))))+(deftest crud-smoke-test- (testing "create, get, list, delete round-trip"+ (testing "create, get, list, delete round-trip — no provisioning involved"(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)))))))))+ (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"+ :status "unprovisioned"+ :status_details nil+ :databases [{:database_id db-id+ :status "unprovisioned"}]+ :creator some?}+ ws)+ "POST only creates rows; everything starts :unprovisioned")+ (testing "get"+ (is (=? {:id ws-id :name "Smoke Test" :status "unprovisioned"}+ (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"+(deftest create-workspace-validation-test+ (testing "POST / validates the attached databases without creating anything"+ (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]+ (is (=? {:status "unprovisioned"+ :databases [{:database_id eligible-id+ :input_schemas ["analytics" "public"]+ :status "unprovisioned"}]}+ (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"+ {:name "With dbs" :database_ids [eligible-id]}))+ "input_schemas are filled from the database's known schemas")+ (testing "an ineligible database is rejected"+ (mt/user-http-request :crowberto :post 400 "ee/workspace-manager/"+ {:name "Nope" :database_ids [ineligible-id]}))+ (testing "a missing database is rejected"+ (mt/user-http-request :crowberto :post 404 "ee/workspace-manager/"+ {:name "Nope" :database_ids [Integer/MAX_VALUE]}))+ (testing "at least one database is required"+ (mt/user-http-request :crowberto :post 400 "ee/workspace-manager/"+ {:name "Nope" :database_ids []}))))))++(deftest create-workspace-target-branch-test+ (testing "POST / accepts an optional target_branch and enforces its uniqueness"(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))))))))+ (is (=? {:target_branch "feature-x"}+ (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"+ {:name "First" :target_branch "feature-x" :database_ids [db-id]})))+ (testing "a duplicate target_branch is rejected"+ (mt/user-http-request :crowberto :post 400 "ee/workspace-manager/"+ {:name "Second" :target_branch "feature-x" :database_ids [db-id]}))+ (testing "target_branch is optional"+ (is (=? {:target_branch nil}+ (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"+ {:name "Second" :database_ids [db-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"+(deftest provision-endpoint-test+ (testing "POST /:id/provision provisions the databases and drives the workspace status"(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)+ (let [{ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"+ {:name "To provision" :database_ids [db-id]})]+ (is (=? {:status "database-provisioning"}+ (mt/user-http-request :crowberto :post 200+ (str "ee/workspace-manager/" ws-id "/provision")))+ "the response reflects the just-started run")+ (is (=? {:status "provisioned"+ :status_details nil+ :instance_url "https://example.com"+ :databases [{:status "provisioned"+ :output_namespace "mb_iso_stub"}]}+ (mt/user-http-request :crowberto :get 200 (str "ee/workspace-manager/" ws-id)))+ "the stub instance provisioner's url is exposed once the run finishes")+ (testing "404 for a missing workspace"+ (mt/user-http-request :crowberto :post 404 "ee/workspace-manager/13371337/provision")))))))++(deftest provision-endpoint-failure-and-retry-test+ (testing "a provisioning failure lands in the statuses and a retry finishes the job"+ (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} (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"+ {:name "Flaky" :database_ids [db-id]})+ boom (reify provisioning.database/DatabaseProvisioner+ (details [_ _ _ _] {:schema "mb_iso_stub" :database_details {}})+ (init! [_ _ _ _] (throw (ex-info "boom" {})))(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))))))))+ (destroy! [_ _ _ _] nil))]+ (with-redefs [provisioning.database/database-provisioner boom]+ (mt/user-http-request :crowberto :post 200 (str "ee/workspace-manager/" ws-id "/provision"))+ (is (=? {:status "database-provisioning-failure"+ :status_details "boom"+ :databases [{:status "provisioning-failure"+ :status_details "boom"}]}+ (mt/user-http-request :crowberto :get 200 (str "ee/workspace-manager/" ws-id)))))+ (testing "retrying with a healthy warehouse succeeds"+ (mt/user-http-request :crowberto :post 200 (str "ee/workspace-manager/" ws-id "/provision"))+ (is (=? {:status "provisioned"+ :status_details nil}+ (mt/user-http-request :crowberto :get 200 (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 {}}]+(deftest deprovision-endpoint-test+ (testing "POST /:id/deprovision tears the databases down and unblocks DELETE"+ (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)]- (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 []})))))))+ (let [{ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace-manager/"+ {:name "Round trip" :database_ids [db-id]})]+ (mt/user-http-request :crowberto :post 200 (str "ee/workspace-manager/" ws-id "/provision"))+ (testing "DELETE refuses while a database is :provisioned"+ (mt/user-http-request :crowberto :delete 400 (str "ee/workspace-manager/" ws-id))+ (is (t2/exists? :model/Workspace :id ws-id)))+ (is (=? {:status "instance-deprovisioning"}+ (mt/user-http-request :crowberto :post 200+ (str "ee/workspace-manager/" ws-id "/deprovision")))+ "the response reflects the just-started run")+ (is (=? {:status "unprovisioned"+ :status_details nil+ :instance_url nil+ :databases [{:status "unprovisioned"+ :output_namespace ""}]}+ (mt/user-http-request :crowberto :get 200 (str "ee/workspace-manager/" ws-id))))+ (testing "DELETE works once everything is :unprovisioned"+ (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 metadata-export-test- (testing "GET /:id/metadata/export streams metadata scoped to the workspace's databases + input"- (mt/with-temp [:model/Database {db-id :id db-name :name} {:engine :postgres :details {}}- :model/Table {t1-id :id} {:db_id db-id :schema "schema-1" :name "table-1" :active true}- :model/Table {t2-id :id} {:db_id db-id :schema "schema-2" :name "table-2" :active true}- :model/Field {f1-id :id} {:table_id t1-id :name "field-1" :active true- :base_type :type/Integer :database_type "BIGINT"}- :model/Field _ {:table_id t2-id :name "field-2" :active true- :base_type :type/Text :database_type "TEXT"}- :model/Workspace {ws-id :id} {:name "Export"- :creator_id (mt/user->id :crowberto)}- :model/WorkspaceDatabase _ {:workspace_id ws-id- :database_id db-id- :database_details {}- :output_namespace ""- ;; schema-2 is deliberately excluded- :input_schemas ["schema-1"]- :status :provisioned}]- ;; Only schema-1's table + field are kept; schema-2's are excluded entirely.- ;; Length mismatch in any section would fail `=?` — that's how we assert the- ;; schema filter is doing its job.- (is (=? {:databases [{:id db-id :name db-name :engine "postgres"}]- :tables [{:id t1-id :db_id db-id :name "table-1" :schema "schema-1"}]- :fields [{:id f1-id :table_id t1-id :name "field-1" :base_type "type/Integer"}]}- (mt/user-http-request :crowberto :get 202- (str "ee/workspace-manager/" ws-id "/metadata/export")- :with-databases true- :with-tables true- :with-fields true))))))+(deftest provision-refused-while-in-flight-test+ (testing "POST /:id/provision and /:id/deprovision 400 while a run is already in flight"+ (doseq [in-flight-status [:database-provisioning :instance-provisioning+ :instance-deprovisioning :database-deprovisioning]]+ (testing (str "workspace status " in-flight-status)+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "Busy" :status in-flight-status}]+ (mt/user-http-request :crowberto :post 400 (str "ee/workspace-manager/" ws-id "/provision"))+ (mt/user-http-request :crowberto :post 400 (str "ee/workspace-manager/" ws-id "/deprovision"))+ (mt/user-http-request :crowberto :delete 400 (str "ee/workspace-manager/" ws-id))+ (is (= in-flight-status (t2/select-one-fn :status :model/Workspace :id ws-id))+ "the in-flight status is untouched"))))+ (testing "provision 400s when already :provisioned, deprovision when already :unprovisioned"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "Done" :status :provisioned}]+ (mt/user-http-request :crowberto :post 400 (str "ee/workspace-manager/" ws-id "/provision"))+ (is (= :provisioned (t2/select-one-fn :status :model/Workspace :id ws-id))))+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "Empty" :status :unprovisioned}]+ (mt/user-http-request :crowberto :post 400 (str "ee/workspace-manager/" ws-id "/deprovision"))+ (is (= :unprovisioned (t2/select-one-fn :status :model/Workspace :id ws-id)))))+ (testing "the settled failure statuses stay retryable"+ (doseq [settled-status [:database-provisioning-failure :instance-provisioning-failure+ :instance-deprovisioning-failure :database-deprovisioning-failure]]+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "Settled" :status settled-status}]+ (mt/user-http-request :crowberto :post 200 (str "ee/workspace-manager/" ws-id "/provision"))+ (is (= :provisioned (t2/select-one-fn :status :model/Workspace :id ws-id)))+ (mt/user-http-request :crowberto :post 200 (str "ee/workspace-manager/" ws-id "/deprovision"))+ (is (= :unprovisioned (t2/select-one-fn :status :model/Workspace :id ws-id))))))))(deftest rename-workspace-test(testing "PUT /:id renames a workspace and returns the updated WorkspaceResponse"@@ -149,28 +226,6 @@(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))@@ -180,10 +235,9 @@(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 :get 403- (str "ee/workspace-manager/" ws-id "/metadata/export"))(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 :post 403 (str "ee/workspace-manager/" ws-id "/provision"))+ (mt/user-http-request :rasta :post 403 (str "ee/workspace-manager/" ws-id "/deprovision"))(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.cljindex 885e650729a..157c8588759 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/config_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/config_test.clj@@ -5,10 +5,18 @@[metabase-enterprise.workspaces.test-util :as workspaces.tu][metabase.test :as mt][metabase.test.fixtures :as fixtures]- [metabase.util :as u]))+ [metabase.util :as u]+ [toucan2.core :as t2]))(use-fixtures :once (fixtures/initialize :db))+(defn- build-config+ "Fetch + hydrate the workspace the way production callers do, then build."+ [ws-id]+ (-> (t2/select-one :model/Workspace :id ws-id)+ (t2/hydrate :databases)+ config/build-workspace-config))+(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}@@ -27,7 +35,7 @@:output_namespace "mb_isolation_github":input_schemas ["raw_github"]:status :provisioned}]- (let [cfg (config/build-workspace-config ws-id)]+ (let [cfg (build-config ws-id)](testing "outer shape matches config.yml (version + config block)"(is (= 1 (:version cfg)))(is (= #{:databases :workspace} (set (keys (:config cfg))))))@@ -70,7 +78,7 @@:output_namespace "ws_alice":input_schemas ["dbo"]:status :provisioned}]- (let [cfg (config/build-workspace-config ws-id)]+ (let [cfg (build-config ws-id)](is (= {"MSSQL DW"{:input_schemas ["dbo"]:output {:db "AnalyticsDB" :schema "ws_alice"}}}@@ -89,7 +97,7 @@:output_namespace "out":input_schemas ["schema_a" "schema_b" "schema_c"]:status :provisioned}]- (let [cfg (config/build-workspace-config ws-id)]+ (let [cfg (build-config ws-id)](is (= "schema_a,schema_b,schema_c"(-> cfg :config :databases first :details :schema-filters-patterns)))))))@@ -115,7 +123,7 @@:output_namespace "ws_alice":input_schemas ["core" "warehouse"]:status :provisioned}]- (let [details (-> (config/build-workspace-config ws-id) :config :databases first :details)]+ (let [details (-> (build-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))@@ -135,13 +143,13 @@(is (thrown-with-msg?Exception#"not :provisioned"- (config/build-workspace-config ws-id))))))+ (build-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)]+ (let [cfg (build-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)))@@ -166,7 +174,7 @@:output_namespace "":input_schemas ["public"]:status :provisioned}]- (let [cfg-dbs (-> (config/build-workspace-config ws-id) :config :databases)+ (let [cfg-dbs (-> (build-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"))@@ -188,11 +196,6 @@(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 sourcedatabase had it true. The YAML carries :details only — not the schedule cron columns —@@ -213,7 +216,7 @@:output_namespace "ws_alice":input_schemas ["public"]:status :provisioned}]- (let [own-db (->> (config/build-workspace-config ws-id)+ (let [own-db (->> (build-config ws-id):config:databases(remove (some-fn :is_stub :is_sample))@@ -236,7 +239,7 @@;; 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+ (let [details (->> (build-config ws-id) :config :databases(u/seek #(= :mysql (:engine %))):details)](is (nil? (:schema-filters-type details)))@@ -255,7 +258,7 @@: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)+ (let [cfg-dbs (-> (build-config ws-id) :config :databases)samples (filter :is_sample cfg-dbs)](testing "exactly one sample entry is emitted"(is (= 1 (count samples))))@@ -267,3 +270,43 @@(first samples))))(testing "sample entry is NOT also marked as a stub"(is (not (:is_stub (first samples)))))))))++(deftest build-workspace-config-remote-sync-settings-test+ (testing "when remote sync is enabled, :config carries read-write remote-sync settings for the child instance"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "github"+ :creator_id (mt/user->id :crowberto)}]+ (mt/with-temporary-setting-values [remote-sync-url "https://git.example.com/test.git"+ remote-sync-branch "main"+ remote-sync-token "s3cr3t-token"]+ (is (= {:remote-sync-url "https://git.example.com/test.git"+ :remote-sync-branch "main"+ :remote-sync-token "s3cr3t-token"+ :remote-sync-type :read-write}+ (-> (build-config ws-id) :config :settings))))+ (testing "and no :settings section when remote sync is disabled"+ (mt/with-temporary-setting-values [remote-sync-url nil]+ (is (not (contains? (-> (build-config ws-id) :config) :settings)))))))+ (testing "the workspace's target_branch overrides the instance's remote-sync branch"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "github"+ :creator_id (mt/user->id :crowberto)+ :target_branch "workspace-branch"}]+ (mt/with-temporary-setting-values [remote-sync-url "https://git.example.com/test.git"+ remote-sync-branch "main"+ remote-sync-token "s3cr3t-token"]+ (is (= "workspace-branch"+ (-> (build-config ws-id) :config :settings :remote-sync-branch)))))))++(deftest build-workspace-config-creator-superuser-test+ (testing "when workspace-instance-user-password is set, :config carries the creator as a superuser"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "github"+ :creator_id (mt/user->id :crowberto)}]+ (mt/with-temporary-setting-values [workspace-instance-user-password "sUp3r-s3cret"]+ (is (= [{:first_name "Crowberto"+ :last_name "Corv"+ :email "[email protected]"+ :password "sUp3r-s3cret"+ :is_superuser true}]+ (-> (build-config ws-id) :config :users))))+ (testing "and no :users section when the password is not set"+ (mt/with-temporary-setting-values [workspace-instance-user-password nil]+ (is (not (contains? (-> (build-config ws-id) :config) :users))))))))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/e2e_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.cljindex 4eff09c50d1..608c9ffbc62 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj@@ -29,8 +29,10 @@[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.instance :as ws.instance]+ [metabase-enterprise.workspaces.models.workspace :as workspace][metabase-enterprise.workspaces.provisioning :as provisioning]+ [metabase-enterprise.workspaces.provisioning.database :as provisioning.database][metabase-enterprise.workspaces.table-remapping :as ws.table-remapping][metabase.driver :as driver][metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]@@ -98,13 +100,28 @@(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))))+ (let [wsd (t2/insert-returning-instance! :model/WorkspaceDatabase+ {:workspace_id workspace-id+ :database_id database-id+ :input_schemas input-schemas+ :database_details {}+ :output_namespace ""})]+ (provisioning.database/provision-database! wsd))))++(defn- cleanup-workspace!+ "Best-effort test cleanup: deprovision every database (destroys the warehouse+ isolation; failures land in the statuses, no throw), then delete the+ workspace. When the deprovision didn't fully succeed the delete refuses, so+ fall back to force-clearing the rows — otherwise the `mt/with-temp Database`+ cleanup can't complete."+ [ws-id]+ (when-let [ws (t2/select-one :model/Workspace :id ws-id)]+ (try+ (provisioning/deprovision-workspace! ws)+ (catch Throwable t+ (log/warnf t "deprovision-workspace! failed during cleanup for workspace %d" ws-id)))+ (t2/delete! :model/WorkspaceDatabase :workspace_id ws-id)+ (t2/delete! :model/Workspace :id ws-id)));;; -------------------- driver-branched helpers --------------------;;;@@ -349,19 +366,19 @@(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)})]+ (let [{ws-id :id} (workspace/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+ ;; multimethods, but through `provisioning.database/provision-database!`, 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)]+ (let [wsd (t2/select-one :model/WorkspaceDatabase :workspace_id ws-id)](is (= :provisioned (:status wsd))"WorkspaceDatabase provisioning did not reach :provisioned status")(is (and (string? (:output_namespace wsd))@@ -374,12 +391,13 @@;; `: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+ (let [yaml-str (-> (t2/select-one :model/Workspace :id ws-id)+ (t2/hydrate :databases)ws.config/build-workspace-configws.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)+ ;; `provision-database!` derives it from the WSD id, not the workspace id.+ wsd (t2/select-one :model/WorkspaceDatabase :workspace_id ws-id)isolation-schema (:output_namespace wsd)];;+---------------------------+;;| Parent above |@@ -399,7 +417,7 @@(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?)+ (is (ws.instance/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.@@ -666,25 +684,15 @@;; `: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+ ;; `cleanup-workspace!` then deprovisions every database (any state, calls+ ;; `destroy-workspace-isolation!`) before deleting the workspace, safe whether;; provision succeeded fully or partially.- (ws/clear-instance-workspace!)+ (ws.instance/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+ ;; See [[cleanup-workspace!]]: deprovision (destroys the warehouse+ ;; isolation), delete, force-clear on failure. 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)))))))+ (cleanup-workspace! ws-id)))))(finally(try (drop-canonical-schema! admin-driver admin-spec admin-details main-schema src-name output-table-name)(catch Throwable _ nil))))))))))))@@ -732,12 +740,14 @@(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)})]+ (let [{ws-id :id} (workspace/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)+ (let [cfg-map (-> (t2/select-one :model/Workspace :id ws-id)+ (t2/hydrate :databases)+ ws.config/build-workspace-config)yaml-str (ws.config/config->yaml cfg-map)reparsed (yaml/parse-string yaml-str)](advanced-config.file/initialize! reparsed)@@ -815,11 +825,9 @@(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!)+ (ws.instance/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")))))))+ (cleanup-workspace! ws-id)))))(finally(try (drop-canonical-schema! admin-driver admin-spec admin-details main-schema src-name tgt-a-name)(catch Throwable _ nil))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.cljindex 0e868083cdc..b068157f3dd 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_database_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_database_test.clj@@ -4,8 +4,8 @@Background: `workspace_database.database_id -> metabase_database.id` used to beON 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+ deleted — orphaning any `:provisioned` warehouse schemas/users because the+ app-side delete guards were never consulted. We switched the FK toRESTRICT, which requires the app-side to reconcile rows explicitly: refuse thedelete if any row is `:provisioned`, otherwise delete the `:unprovisioned`rows so the FK is satisfied."diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_test.cljindex aff091ab486..0f6fbef01cc 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_test.clj@@ -17,9 +17,11 @@overrides)))(defn- create-ws!- "Test helper: supply a default :creator_id so call sites stay terse."+ "Test helper: supply a default :creator_id so call sites stay terse, and+ hydrate :databases the way the API does."[params]- (workspace/create-workspace! (merge {:creator_id (mt/user->id :crowberto)} params)))+ (-> (workspace/create-workspace! (merge {:creator_id (mt/user->id :crowberto)} params))+ (t2/hydrate :databases)))(deftest create-workspace-minimal-test(testing "create-workspace! creates a Workspace with no databases"@@ -49,68 +51,6 @@(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]@@ -143,77 +83,16 @@: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"+(deftest status-details-encrypted-round-trip-test+ (testing "workspace.status_details and workspace_database.status_details round-trip transparently"(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"]}]}))))))+ (let [{id :id} (create-ws! {:name "Statuses" :databases [(ws-db-attrs)]})]+ (t2/update! :model/Workspace id {:status :database-provisioning-failure, :status_details "boom"})+ (t2/update! :model/WorkspaceDatabase {:workspace_id id} {:status_details "db boom"})+ (is (=? {:status :database-provisioning-failure, :status_details "boom"}+ (t2/select-one :model/Workspace :id id)))+ (is (= "db boom"+ (t2/select-one-fn :status_details :model/WorkspaceDatabase :workspace_id id)))))))(deftest cascade-delete-database-test(testing "Deleting an underlying Database cascades to workspace_database rows"diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/remapping_cleanup_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/provisioning/database_test.cljsimilarity index 53%rename from enterprise/backend/test/metabase_enterprise/workspaces/remapping_cleanup_test.cljrename to enterprise/backend/test/metabase_enterprise/workspaces/provisioning/database_test.cljindex 7bde4dd283b..3bd12cce032 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/remapping_cleanup_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/provisioning/database_test.clj@@ -1,15 +1,14 @@-(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!`."+(ns metabase-enterprise.workspaces.provisioning.database-test+ "Tests for the iso-scoped `TableRemapping` deletion path+ ([[provisioning.database/clear-mappings-for-iso!]]) invoked from+ [[provisioning.database/deprovision-database!]]."(:require[clojure.test :refer :all]- [metabase-enterprise.workspaces.provisioning :as provisioning]- [metabase-enterprise.workspaces.remapping-cleanup :as ws.remapping-cleanup]+ [metabase-enterprise.workspaces.provisioning.database :as provisioning.database][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@@ -49,7 +48,7 @@;; 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!+ (let [n (provisioning.database/clear-mappings-for-iso!{:engine :postgres :details {}}(mt/id)"ws_a")]@@ -67,13 +66,13 @@(mt/id)(fn [](is (= 0- (ws.remapping-cleanup/clear-mappings-for-iso!+ (provisioning.database/clear-mappings-for-iso!{:engine :postgres :details {}}(mt/id)"never_provisioned"))"no rows, returns 0")(is (= 0- (ws.remapping-cleanup/clear-mappings-for-iso!+ (provisioning.database/clear-mappings-for-iso!{:engine :postgres :details {}}(mt/id)""))@@ -94,7 +93,7 @@{:db "AnalyticsDB" :schema "dbo" :table "products"}{:db "AnalyticsDB" :schema "ws_b" :table "products_copy"})- (let [n (ws.remapping-cleanup/clear-mappings-for-iso!+ (let [n (provisioning.database/clear-mappings-for-iso!{:engine ::fake-3-slot :details {:db "AnalyticsDB"}}(mt/id)"ws_a")]@@ -107,46 +106,41 @@(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`+ `deprovision-database!` must still clear the workspace's `TableRemapping`rows. Without that, future queries against canonical tables on this DB rewrite toan iso namespace that no longer exists on the warehouse and 500 in the QP. Thewarehouse-side leak (orphan SA / dataset) is acceptable -- app-DB state must matchthe 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")))))+ (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 :as wsd} {: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.database/DatabaseProvisioner+ (details [_ _ _ _] {:schema "ws_alice" :database_details {}})+ (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.database/deprovision-database! wsd 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 (=? {:status :deprovisioning-failure+ :status_details "warehouse teardown blew up"}+ (t2/select-one :model/WorkspaceDatabase :id wsd-id))+ "the failed deprovision is recorded on the row for retry"))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/provisioning_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/provisioning_test.cljnew file mode 100644index 00000000000..fddd54c61b8--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/provisioning_test.clj@@ -0,0 +1,443 @@+(ns ^:synchronous metabase-enterprise.workspaces.provisioning-test+ "Tests for the workspace provisioning lifecycle: [[provisioning/provision-workspace!]]+ and [[provisioning/deprovision-workspace!]] driving the workspace and+ workspace_database statuses. `use-fixtures`-installed stubs replace the real+ DatabaseProvisioner and InstanceProvisioner for every test (hence+ `^:synchronous`); individual tests override them with their own reify via an+ inner `with-redefs`."+ (:require+ [clojure.test :refer [deftest is testing use-fixtures]]+ [metabase-enterprise.remote-sync.core :as remote-sync]+ [metabase-enterprise.workspaces.provisioning :as provisioning]+ [metabase-enterprise.workspaces.provisioning.database :as provisioning.database]+ [metabase-enterprise.workspaces.provisioning.instance :as provisioning.instance]+ [metabase.test :as mt]+ [metabase.test.fixtures :as fixtures]+ [toucan2.core :as t2])+ (:import+ (clojure.lang ExceptionInfo)))++(set! *warn-on-reflection* true)++(def ^:private database-calls+ "init!/destroy! calls recorded by the default stub DatabaseProvisioner. Reset per test."+ (atom []))++(defn- stub-database-provisioner+ "DatabaseProvisioner that records init!/destroy! calls in [[database-calls]] and never+ touches a warehouse."+ []+ (reify provisioning.database/DatabaseProvisioner+ (details [_ _ _ workspace]+ {:schema (str "mb_iso_" (:id workspace)) :database_details {:user "stub_user"}})+ (init! [_ _ db _] (swap! database-calls conj [:init! (:id db)]) nil)+ (grant! [_ _ _ _ _] nil)+ (destroy! [_ _ db _] (swap! database-calls conj [:destroy! (:id db)]) nil)))++(defn- stub-instance-provisioner+ "InstanceProvisioner that never talks to a real service and starts instantly."+ []+ (reify provisioning.instance/InstanceProvisioner+ (create! [_ _workspace _config]+ {:id (str (random-uuid)) :url "https://example.com" :status :active})+ (fetch [_ instance-id]+ {:id instance-id :url "https://example.com" :status :active})+ (delete! [_ _instance-id] nil)))++(use-fixtures :once+ (fixtures/initialize :db)+ (fn [thunk]+ (with-redefs [provisioning.database/database-provisioner (stub-database-provisioner)+ provisioning.instance/instance-provisioner (stub-instance-provisioner)]+ (thunk))))++(use-fixtures :each+ (fn [thunk]+ (reset! database-calls [])+ (thunk)))++(defn- failing-on+ "Like [[stub-database-provisioner]] but `op` (:init! or :destroy!) throws for database+ `db-id`."+ [op db-id]+ (reify provisioning.database/DatabaseProvisioner+ (details [_ _ _ workspace]+ {:schema (str "mb_iso_" (:id workspace)) :database_details {:user "stub_user"}})+ (init! [_ _ db _]+ (when (and (= op :init!) (= (:id db) db-id))+ (throw (ex-info "boom" {}))))+ (grant! [_ _ _ _ _] nil)+ (destroy! [_ _ db _]+ (when (and (= op :destroy!) (= (:id db) db-id))+ (throw (ex-info "warehouse down" {}))))))++(defn- wsd-attrs [ws-id db-id]+ {:workspace_id ws-id+ :database_id db-id+ :database_details {}+ :output_namespace ""+ :input_schemas ["public"]})++(defn- workspace-row [ws-id]+ (t2/select-one :model/Workspace :id ws-id))++(deftest provision-workspace-success-test+ (testing "provision-workspace! provisions every database and lands the workspace :provisioned"+ (mt/with-temp [:model/Database {db2-id :id} {:engine :h2 :details {}}+ :model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase {wsd1-id :id} (wsd-attrs ws-id (mt/id))+ :model/WorkspaceDatabase {wsd2-id :id} (wsd-attrs ws-id db2-id)]+ (is (=? {:status :provisioned :status_details nil}+ (provisioning/provision-workspace! (workspace-row ws-id)))+ "the updated workspace copy is returned")+ (is (=? {:status :provisioned+ :status_details nil+ :instance_id string?+ :instance_url "https://example.com"}+ (workspace-row ws-id))+ "the stub instance provisioner persisted the instance identifiers")+ (is (=? {:status :provisioned+ :status_details nil+ :output_namespace (str "mb_iso_" wsd1-id)+ :database_details {:user "stub_user"}}+ (t2/select-one :model/WorkspaceDatabase :id wsd1-id)))+ (is (=? {:status :provisioned+ :output_namespace (str "mb_iso_" wsd2-id)}+ (t2/select-one :model/WorkspaceDatabase :id wsd2-id))))))++(deftest provision-workspace-branch-test+ (testing "the branch phase creates the workspace's target branch when remote sync is enabled"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS" :target_branch "ws-branch"}]+ (let [calls (atom [])]+ (with-redefs [remote-sync/remote-sync-enabled (constantly true)+ remote-sync/branch-exists? (fn [branch] (swap! calls conj [:exists? branch]) false)+ remote-sync/create-branch! (fn [branch] (swap! calls conj [:create! branch]) branch)]+ (is (=? {:status :provisioned}+ (provisioning/provision-workspace! (workspace-row ws-id))))+ (is (= [[:exists? "ws-branch"] [:create! "ws-branch"]] @calls))))))+ (testing "an already existing branch is fine"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS" :target_branch "ws-branch"}]+ (with-redefs [remote-sync/remote-sync-enabled (constantly true)+ remote-sync/branch-exists? (constantly true)+ remote-sync/create-branch! (fn [_] (throw (ex-info "should not be called" {})))]+ (is (=? {:status :provisioned}+ (provisioning/provision-workspace! (workspace-row ws-id)))))))+ (testing "the branch phase is a no-op when remote sync is disabled or there is no branch"+ (mt/with-temp [:model/Workspace {with-branch-id :id} {:name "WS" :target_branch "ws-branch"}+ :model/Workspace {no-branch-id :id} {:name "WS"}]+ (with-redefs [remote-sync/remote-sync-enabled (constantly false)+ remote-sync/branch-exists? (fn [_] (throw (ex-info "should not be called" {})))+ remote-sync/create-branch! (fn [_] (throw (ex-info "should not be called" {})))]+ (is (=? {:status :provisioned}+ (provisioning/provision-workspace! (workspace-row with-branch-id))))+ (is (=? {:status :provisioned}+ (provisioning/provision-workspace! (workspace-row no-branch-id)))))))+ (testing "a branch failure lands in :branch-provisioning-failure and stops the run"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS" :target_branch "ws-branch"}]+ (with-redefs [remote-sync/remote-sync-enabled (constantly true)+ remote-sync/branch-exists? (constantly false)+ remote-sync/create-branch! (fn [_] (throw (ex-info "git down" {})))]+ (is (thrown-with-msg? ExceptionInfo #"git down"+ (provisioning/provision-workspace! (workspace-row ws-id))))+ (is (=? {:status :branch-provisioning-failure+ :status_details "git down"}+ (workspace-row ws-id)))))))++(deftest deprovision-workspace-branch-test+ (testing "the branch phase deletes the branch and keeps :target_branch as is"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"+ :status :provisioned+ :target_branch "ws-branch"}]+ (let [calls (atom [])]+ (with-redefs [remote-sync/remote-sync-enabled (constantly true)+ remote-sync/delete-branch! (fn [branch] (swap! calls conj [:delete! branch]) nil)]+ (is (=? {:status :unprovisioned}+ (provisioning/deprovision-workspace! (workspace-row ws-id))))+ (is (= [[:delete! "ws-branch"]] @calls))+ (is (= "ws-branch" (:target_branch (workspace-row ws-id)))+ "the branch column survives deprovisioning")))))+ (testing "when remote sync is disabled there is nothing to delete and the column is kept"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"+ :status :provisioned+ :target_branch "ws-branch"}]+ (with-redefs [remote-sync/remote-sync-enabled (constantly false)+ remote-sync/delete-branch! (fn [_] (throw (ex-info "should not be called" {})))]+ (is (=? {:status :unprovisioned}+ (provisioning/deprovision-workspace! (workspace-row ws-id))))+ (is (= "ws-branch" (:target_branch (workspace-row ws-id)))))))+ (testing "a branch deletion failure lands in :branch-deprovisioning-failure and stops the run"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"+ :status :provisioned+ :target_branch "ws-branch"}]+ (with-redefs [remote-sync/remote-sync-enabled (constantly true)+ remote-sync/delete-branch! (fn [_] (throw (ex-info "git down" {})))]+ (is (thrown-with-msg? ExceptionInfo #"git down"+ (provisioning/deprovision-workspace! (workspace-row ws-id))))+ (is (=? {:status :branch-deprovisioning-failure+ :status_details "git down"}+ (workspace-row ws-id)))))))++(deftest provision-workspace-failure-test+ (testing "the first database failure stops the run and records the error on the row and the workspace"+ (mt/with-temp [:model/Database {db2-id :id} {:engine :h2 :details {}}+ :model/Database {db3-id :id} {:engine :h2 :details {}}+ :model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase {wsd1-id :id} (wsd-attrs ws-id (mt/id))+ :model/WorkspaceDatabase {wsd2-id :id} (wsd-attrs ws-id db2-id)+ :model/WorkspaceDatabase {wsd3-id :id} (wsd-attrs ws-id db3-id)]+ (with-redefs [provisioning.database/database-provisioner (failing-on :init! db2-id)]+ (is (thrown-with-msg? ExceptionInfo #"boom"+ (provisioning/provision-workspace! (workspace-row ws-id)))+ "the failure is rethrown"))+ (is (=? {:status :database-provisioning-failure+ :status_details "boom"+ :instance_id nil}+ (workspace-row ws-id))+ "the instance phase is never reached after a database failure")+ (is (=? {:status :provisioned}+ (t2/select-one :model/WorkspaceDatabase :id wsd1-id))+ "the database provisioned before the failure stays :provisioned")+ (is (=? {:status :provisioning-failure :status_details "boom"}+ (t2/select-one :model/WorkspaceDatabase :id wsd2-id))+ "the failed database records the failure — no rollback")+ (is (=? {:status :unprovisioned}+ (t2/select-one :model/WorkspaceDatabase :id wsd3-id))+ "databases after the failure are not attempted"))))++(deftest provision-workspace-retry-test+ (testing "a retry after a failure skips already-:provisioned databases and finishes the job"+ (mt/with-temp [:model/Database {db2-id :id} {:engine :h2 :details {}}+ :model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase {wsd1-id :id} (wsd-attrs ws-id (mt/id))+ :model/WorkspaceDatabase {wsd2-id :id} (wsd-attrs ws-id db2-id)]+ (with-redefs [provisioning.database/database-provisioner (failing-on :init! db2-id)]+ (is (thrown-with-msg? ExceptionInfo #"boom"+ (provisioning/provision-workspace! (workspace-row ws-id)))))+ (reset! database-calls [])+ (provisioning/provision-workspace! (workspace-row ws-id))+ (is (= [[:init! db2-id]] @database-calls)+ "only the previously-failed database is re-provisioned")+ (is (=? {:status :provisioned :status_details nil} (workspace-row ws-id)))+ (is (=? {:status :provisioned :status_details nil}+ (t2/select-one :model/WorkspaceDatabase :id wsd2-id))+ "the retried row ends :provisioned with its failure details cleared")+ (is (=? {:status :provisioned}+ (t2/select-one :model/WorkspaceDatabase :id wsd1-id))))))++(deftest provision-workspace-instance-failure-and-retry-test+ (testing "an instance-provisioning failure lands in :instance-provisioning-failure and a retry finishes"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase {wsd-id :id} (wsd-attrs ws-id (mt/id))]+ (with-redefs [provisioning.instance/instance-provisioner+ (reify provisioning.instance/InstanceProvisioner+ (create! [_ _ _] (throw (ex-info "no capacity" {})))+ (fetch [_ id] {:id id :url "https://example.com" :status :active})+ (delete! [_ _] nil))]+ (is (thrown-with-msg? ExceptionInfo #"no capacity"+ (provisioning/provision-workspace! (workspace-row ws-id)))))+ (is (=? {:status :instance-provisioning-failure+ :status_details "no capacity"+ :instance_id nil}+ (workspace-row ws-id)))+ (is (=? {:status :provisioned}+ (t2/select-one :model/WorkspaceDatabase :id wsd-id))+ "the databases were provisioned before the instance phase failed")+ (testing "retry skips the already-provisioned databases and finishes the instance"+ (reset! database-calls [])+ (provisioning/provision-workspace! (workspace-row ws-id))+ (is (= [] @database-calls))+ (is (=? {:status :provisioned :status_details nil :instance_id string?}+ (workspace-row ws-id)))))))++(deftest deprovision-workspace-instance-failure-test+ (testing "an instance-deprovisioning failure lands in :instance-deprovisioning-failure and stops"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase {wsd-id :id} (wsd-attrs ws-id (mt/id))]+ (provisioning/provision-workspace! (workspace-row ws-id))+ (with-redefs [provisioning.instance/instance-provisioner+ (reify provisioning.instance/InstanceProvisioner+ (create! [_ _ _] {:id "x" :url "https://example.com" :status :active})+ (fetch [_ id] {:id id :url "https://example.com" :status :active})+ (delete! [_ _] (throw (ex-info "instance stuck" {}))))]+ (is (thrown-with-msg? ExceptionInfo #"instance stuck"+ (provisioning/deprovision-workspace! (workspace-row ws-id)))))+ (is (=? {:status :instance-deprovisioning-failure+ :status_details "instance stuck"+ :instance_id string?}+ (workspace-row ws-id))+ "the instance identifiers are kept for the retry")+ (is (=? {:status :provisioned}+ (t2/select-one :model/WorkspaceDatabase :id wsd-id))+ "the database phase is never reached after an instance failure")+ (testing "retry once the instance is deletable"+ (provisioning/deprovision-workspace! (workspace-row ws-id))+ (is (=? {:status :unprovisioned :instance_id nil}+ (workspace-row ws-id)))))))++(deftest provision-instance-receives-workspace-config-test+ (testing "the instance provisioner receives the workspace's config-file map"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"+ :creator_id (mt/user->id :crowberto)}+ :model/WorkspaceDatabase _ (assoc (wsd-attrs ws-id (mt/id))+ :status :provisioned+ :output_namespace "mb_iso_x")]+ (let [received (atom nil)]+ (with-redefs [provisioning.instance/instance-provisioner+ (reify provisioning.instance/InstanceProvisioner+ (create! [_ _ config] (reset! received config) {:id "i" :url "https://example.com" :status :active})+ (fetch [_ id] {:id id :url "https://example.com" :status :active})+ (delete! [_ _] nil))]+ (provisioning/provision-workspace! (workspace-row ws-id)))+ (is (=? {:version 1+ :config {:workspace {:name "WS"}}}+ @received)+ "create! received the workspace's config map")))))++(deftest set-initial-workspace-status-guard-test+ (testing "set-initial-workspace-*-status! atomically refuse to start a run while one is in flight"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "Busy" :status :database-provisioning}]+ (doseq [start! [provisioning/set-initial-workspace-provisioning-status!+ provisioning/set-initial-workspace-deprovisioning-status!]]+ (is (= 400 (try (start! (workspace-row ws-id))+ (catch Exception e (:status-code (ex-data e))))))+ (is (= :database-provisioning (:status (workspace-row ws-id)))+ "the in-flight status is untouched"))))+ (testing "and start the run from any other settled status"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "Settled" :status :database-provisioning-failure}]+ (is (=? {:status :database-provisioning}+ (provisioning/set-initial-workspace-provisioning-status! (workspace-row ws-id))))+ (is (= :database-provisioning (:status (workspace-row ws-id))))))+ (testing "a provision cannot start when the workspace is already :provisioned"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "Done" :status :provisioned}]+ (is (= 400 (try (provisioning/set-initial-workspace-provisioning-status! (workspace-row ws-id))+ (catch Exception e (:status-code (ex-data e))))))+ (is (= :provisioned (:status (workspace-row ws-id))))))+ (testing "a deprovision cannot start when the workspace is already :unprovisioned"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "Empty" :status :unprovisioned}]+ (is (= 400 (try (provisioning/set-initial-workspace-deprovisioning-status! (workspace-row ws-id))+ (catch Exception e (:status-code (ex-data e))))))+ (is (= :unprovisioned (:status (workspace-row ws-id)))))))++(deftest provision-instance-polls-until-running-test+ (testing "provision-instance! records the ids up front, then polls until the instance is :active"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"}]+ (let [statuses (atom [:creating :creating :active])+ polls (atom 0)]+ (with-redefs [provisioning.instance/instance-poll-interval-ms 1+ provisioning.instance/instance-provisioner+ (reify provisioning.instance/InstanceProvisioner+ (create! [_ _ _] {:id "async-1" :url "https://example.com" :status :creating})+ (fetch [_ id]+ (swap! polls inc)+ {:id id :url "https://example.com" :status (ffirst (swap-vals! statuses rest))})+ (delete! [_ _] nil))]+ (provisioning/provision-workspace! (workspace-row ws-id)))+ (is (= 3 @polls) "polling continues through :creating until the terminal status")+ (is (=? {:status :provisioned :instance_id "async-1" :instance_url "https://example.com"}+ (workspace-row ws-id)))))))++(deftest provision-instance-error-status-test+ (testing "an instance that lands in :error fails the run but keeps the recorded ids"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"}]+ (with-redefs [provisioning.instance/instance-provisioner+ (reify provisioning.instance/InstanceProvisioner+ (create! [_ _ _] {:id "doomed-1" :url "https://example.com" :status :creating})+ (fetch [_ id] {:id id :url "https://example.com" :status :error})+ (delete! [_ _] nil))]+ (is (thrown-with-msg? ExceptionInfo #"failed to start"+ (provisioning/provision-workspace! (workspace-row ws-id)))))+ (is (=? {:status :instance-provisioning-failure+ :status_details "Workspace instance failed to start"+ :instance_id "doomed-1"}+ (workspace-row ws-id))+ "the instance identifiers stay recorded for the retry's cleanup"))))++(deftest provision-workspace-noop-when-provisioned-test+ (testing "provisioning a fully-provisioned workspace is a no-op"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase _ (assoc (wsd-attrs ws-id (mt/id))+ :status :provisioned+ :output_namespace "mb_iso_x")]+ (provisioning/provision-workspace! (workspace-row ws-id))+ (is (= [] @database-calls) "no provisioner database-calls are made")+ (is (=? {:status :provisioned} (workspace-row ws-id))))))++(deftest deprovision-workspace-success-test+ (testing "deprovision-workspace! tears down every database and lands the workspace :unprovisioned"+ (mt/with-temp [:model/Database {db2-id :id} {:engine :h2 :details {}}+ :model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase {wsd1-id :id} (wsd-attrs ws-id (mt/id))+ :model/WorkspaceDatabase {wsd2-id :id} (wsd-attrs ws-id db2-id)]+ (provisioning/provision-workspace! (workspace-row ws-id))+ (reset! database-calls [])+ (is (=? {:status :unprovisioned :status_details nil :instance_id nil}+ (provisioning/deprovision-workspace! (workspace-row ws-id)))+ "the updated workspace copy is returned")+ (is (= [[:destroy! (mt/id)] [:destroy! db2-id]] @database-calls)+ "every database gets a warehouse teardown, in row order")+ (is (=? {:status :unprovisioned+ :status_details nil+ :instance_id nil+ :instance_url nil}+ (workspace-row ws-id))+ "the instance identifiers are cleared")+ (doseq [wsd-id [wsd1-id wsd2-id]]+ (is (=? {:status :unprovisioned+ :status_details nil+ :output_namespace ""+ :database_details {}}+ (t2/select-one :model/WorkspaceDatabase :id wsd-id))+ "the persisted warehouse identifiers are cleared")))))++(deftest deprovision-workspace-failure-and-retry-test+ (testing "a teardown failure records the error and a later retry finishes the job"+ (mt/with-temp [:model/Database {db2-id :id} {:engine :h2 :details {}}+ :model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase {wsd1-id :id} (wsd-attrs ws-id (mt/id))+ :model/WorkspaceDatabase {wsd2-id :id} (wsd-attrs ws-id db2-id)]+ (provisioning/provision-workspace! (workspace-row ws-id))+ (with-redefs [provisioning.database/database-provisioner (failing-on :destroy! (mt/id))]+ (is (thrown-with-msg? ExceptionInfo #"warehouse down"+ (provisioning/deprovision-workspace! (workspace-row ws-id)))))+ (is (=? {:status :database-deprovisioning-failure :status_details "warehouse down"}+ (workspace-row ws-id)))+ (is (=? {:status :deprovisioning-failure :status_details "warehouse down"}+ (t2/select-one :model/WorkspaceDatabase :id wsd1-id)))+ (is (=? {:status :provisioned}+ (t2/select-one :model/WorkspaceDatabase :id wsd2-id))+ "databases after the failure are not attempted")+ (testing "retry once the warehouse is reachable"+ (provisioning/deprovision-workspace! (workspace-row ws-id))+ (is (=? {:status :unprovisioned :status_details nil} (workspace-row ws-id)))+ (doseq [wsd-id [wsd1-id wsd2-id]]+ (is (=? {:status :unprovisioned :status_details nil}+ (t2/select-one :model/WorkspaceDatabase :id wsd-id))))))))++(deftest deprovision-workspace-noop-when-unprovisioned-test+ (testing "deprovisioning an :unprovisioned workspace is a no-op"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase _ (wsd-attrs ws-id (mt/id))]+ (provisioning/deprovision-workspace! (workspace-row ws-id))+ (is (= [] @database-calls) "no provisioner database-calls are made")+ (is (=? {:status :unprovisioned} (workspace-row ws-id))))))++(deftest deprovision-recomputes-identifiers-for-crashed-provisioning-row-test+ (testing "a crashed :provisioning row that never persisted its identifiers is torn down+ with identifiers recomputed via the provisioner's `details`"+ (mt/with-temp [:model/Workspace {ws-id :id} {:name "WS"}+ :model/WorkspaceDatabase {wsd-id :id} (assoc (wsd-attrs ws-id (mt/id))+ :status :provisioning)]+ (let [destroyed (atom nil)+ recording (reify provisioning.database/DatabaseProvisioner+ (details [_ _ _ workspace]+ {:schema (str "mb_iso_" (:id workspace)) :database_details {:user "recomputed"}})+ (init! [_ _ _ _] nil)+ (grant! [_ _ _ _ _] nil)+ (destroy! [_ _ _ workspace] (reset! destroyed workspace) nil))]+ (with-redefs [provisioning.database/database-provisioner recording]+ (provisioning/deprovision-workspace! (workspace-row ws-id)))+ (is (=? {:status :unprovisioned} (workspace-row ws-id)))+ (is (=? {:schema (str "mb_iso_" wsd-id)+ :database_details {:user "recomputed"}}+ @destroyed)+ "destroy! received the deterministically recomputed identifiers")))))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.cljindex 6a6a132d8eb..016e759a9af 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/corpus_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/query_processor/corpus_test.clj@@ -15,7 +15,8 @@[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.instance :as ws.instance]+ [metabase-enterprise.workspaces.provisioning.database :as provisioning.database][metabase-enterprise.workspaces.table-remapping :as ws.table-remapping][metabase-enterprise.workspaces.test-util :as workspaces.tu][metabase.driver :as driver]@@ -300,7 +301,7 @@;; 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})+ input-positions (provisioning.database/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}]@@ -311,7 +312,7 @@;; 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)+ (let [ws-ns (ws.instance/db-workspace-namespace db-id)to-spec (merge from-spec ws-ns){:keys [tables rewritten]} (rewrite-and-parsedriver canonical-sql@@ -344,4 +345,4 @@(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!)))))))))))+ (ws.instance/clear-instance-workspace!)))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.cljindex b126ab64c5f..d375b6daef9 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/settings_test.clj@@ -1,7 +1,7 @@(ns metabase-enterprise.workspaces.settings-test(:require[clojure.test :refer [deftest is testing use-fixtures]]- [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.instance :as ws.instance][metabase.test.fixtures :as fixtures]))(use-fixtures :once (fixtures/initialize :db))@@ -10,23 +10,23 @@(try(thunk)(finally- (ws/clear-instance-workspace!)))))+ (ws.instance/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?)))))+ (ws.instance/clear-instance-workspace!)+ (is (false? (ws.instance/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?)))))+ (ws.instance/clear-instance-workspace!)+ (is (false? (ws.instance/workspace-mode?)))+ (ws.instance/set-instance-workspace! {:name "Acme" :databases {}})+ (is (true? (ws.instance/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?)))))+ (ws.instance/set-instance-workspace! {:name "Acme" :databases {}})+ (is (true? (ws.instance/workspace-mode?)))+ (ws.instance/clear-instance-workspace!)+ (is (false? (ws.instance/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.cljindex 3435606d510..607c726991d 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/table_remapping_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/table_remapping_test.clj@@ -5,7 +5,8 @@(:require[clojure.test :refer :all][java-time.api :as t]- [metabase-enterprise.workspaces.core :as ws]+ [metabase-enterprise.workspaces.instance :as ws.instance]+ [metabase-enterprise.workspaces.provisioning.database :as provisioning.database][metabase-enterprise.workspaces.table-remapping :as ws.table-remapping][metabase-enterprise.workspaces.test-util :as workspaces.tu][metabase.driver :as driver]@@ -34,12 +35,12 @@[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}}}})+ (ws.instance/set-instance-workspace! {:name "table-remapping-test-ws"+ :databases {db-id {:input_schemas ["_"]+ :output {:schema output-schema}}}})(f)(finally- (ws/clear-instance-workspace!)))))+ (ws.instance/clear-instance-workspace!)))))(deftest remap-table-returns-nil-when-no-mapping-test(clean-db-fixture!@@ -143,12 +144,12 @@[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}}})+ (ws.instance/set-instance-workspace! {:name "ws-3-slot"+ :databases {db-id {:input_schemas ["_"]+ :output output-namespace}}})(f)(finally- (ws/clear-instance-workspace!)))))+ (ws.instance/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"@@ -994,8 +995,8 @@(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"})))))+ (provisioning.database/engine-namespace-positions {:engine ::ws-test-unknown-driver :name "x"}+ {:schema "public" :name "orders"})))));;; -------------------------- GHY-3553: MySQL-shape sentinel-leak regressions --------------------------;;;diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj b/enterprise/backend/test/metabase_enterprise/workspaces/test_util.cljindex 7245f8d03f6..e6a5fcaad5d 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj@@ -1,7 +1,6 @@(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?@@ -14,14 +13,3 @@[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.cljindex 6e2e2494aa3..21c418d203b 100644--- a/enterprise/backend/test/metabase_enterprise/workspaces/transform_hooks_test.clj+++ b/enterprise/backend/test/metabase_enterprise/workspaces/transform_hooks_test.clj@@ -1,7 +1,7 @@(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.instance :as ws.instance][metabase-enterprise.workspaces.table-remapping :as ws.table-remapping][metabase-enterprise.workspaces.transform-hooks :as ws.transform-hooks][metabase.test :as mt]@@ -13,7 +13,7 @@(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)+ (with-redefs [ws.instance/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}]@@ -22,7 +22,7 @@(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"})+ (with-redefs [ws.instance/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})@@ -40,7 +40,7 @@(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"})+ (with-redefs [ws.instance/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})@@ -57,12 +57,12 @@the way out."[output-schema body-fn](try- (ws/set-instance-workspace! {:name "test-ws"- :databases {(mt/id) {:input_schemas ["_"]- :output {:schema output-schema}}}})+ (ws.instance/set-instance-workspace! {:name "test-ws"+ :databases {(mt/id) {:input_schemas ["_"]+ :output {:schema output-schema}}}})(body-fn)(finally- (ws/clear-instance-workspace!))))+ (ws.instance/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"diff --git a/enterprise/frontend/src/metabase-enterprise/api/advanced-config.ts b/enterprise/frontend/src/metabase-enterprise/api/advanced-config.tsdeleted file mode 100644index fcf2e66bc7e..00000000000--- a/enterprise/frontend/src/metabase-enterprise/api/advanced-config.ts+++ /dev/null@@ -1,28 +0,0 @@-import type { ApplyAdvancedConfigRequest } from "metabase-types/api";--import { EnterpriseApi } from "./api";-import { invalidateTags, listTag, tag } from "./tags";--export const advancedConfigApi = EnterpriseApi.injectEndpoints({- endpoints: (builder) => ({- applyAdvancedConfig: builder.mutation<void, ApplyAdvancedConfigRequest>({- query: ({ config }) => {- const formData = new FormData();- formData.append("config", config);- return {- method: "POST",- url: "/api/ee/advanced-config",- body: formData,- };- },- invalidatesTags: (_, error) =>- invalidateTags(error, [- tag("workspace"),- listTag("workspace"),- listTag("database"),- ]),- }),- }),-});--export const { useApplyAdvancedConfigMutation } = advancedConfigApi;diff --git a/enterprise/frontend/src/metabase-enterprise/api/index.ts b/enterprise/frontend/src/metabase-enterprise/api/index.tsindex 141a5c307fb..b8b8e61be4f 100644--- a/enterprise/frontend/src/metabase-enterprise/api/index.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/index.ts@@ -1,4 +1,3 @@-export * from "./advanced-config";export * from "./advanced-permissions";export * from "./ai-controls";export * from "./application-permissions";diff --git a/enterprise/frontend/src/metabase-enterprise/api/workspace-instance.ts b/enterprise/frontend/src/metabase-enterprise/api/workspace-instance.tsindex be0ecd73a20..c19bb29256d 100644--- a/enterprise/frontend/src/metabase-enterprise/api/workspace-instance.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/workspace-instance.ts@@ -6,7 +6,7 @@ import type {} from "metabase-types/api";import { EnterpriseApi } from "./api";-import { invalidateTags, tag } from "./tags";+import { tag } from "./tags";export const workspaceInstanceApi = EnterpriseApi.injectEndpoints({endpoints: (builder) => ({@@ -19,14 +19,6 @@ export const workspaceInstanceApi = EnterpriseApi.injectEndpoints({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",@@ -38,8 +30,5 @@ export const workspaceInstanceApi = EnterpriseApi.injectEndpoints({}),});-export const {- useGetCurrentWorkspaceQuery,- useDeleteCurrentWorkspaceMutation,- useListTableRemappingsQuery,-} = workspaceInstanceApi;+export const { useGetCurrentWorkspaceQuery, useListTableRemappingsQuery } =+ workspaceInstanceApi;diff --git a/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts b/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.tsindex 1d3b22c4653..f3ea5aeafd0 100644--- a/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/workspace-manager.ts@@ -56,6 +56,22 @@ export const workspaceManagerApi = EnterpriseApi.injectEndpoints({invalidatesTags: (_, error, id) =>invalidateTags(error, [listTag("workspace"), idTag("workspace", id)]),}),+ provisionWorkspace: builder.mutation<Workspace, WorkspaceId>({+ query: (id) => ({+ method: "POST",+ url: `/api/ee/workspace-manager/${id}/provision`,+ }),+ invalidatesTags: (_, error, id) =>+ invalidateTags(error, [listTag("workspace"), idTag("workspace", id)]),+ }),+ deprovisionWorkspace: builder.mutation<Workspace, WorkspaceId>({+ query: (id) => ({+ method: "POST",+ url: `/api/ee/workspace-manager/${id}/deprovision`,+ }),+ invalidatesTags: (_, error, id) =>+ invalidateTags(error, [listTag("workspace"), idTag("workspace", id)]),+ }),}),});@@ -66,4 +82,6 @@ export const {useCreateWorkspaceMutation,useUpdateWorkspaceMutation,useDeleteWorkspaceMutation,+ useProvisionWorkspaceMutation,+ useDeprovisionWorkspaceMutation,} = workspaceManagerApi;diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/analytics.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/analytics.tsindex 61d43d5672f..3082dfd4e7d 100644--- a/enterprise/frontend/src/metabase-enterprise/workspaces/analytics.ts+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/analytics.ts@@ -11,33 +11,3 @@ export function trackWorkspaceCreated({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/constants.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/constants.tsnew file mode 100644index 00000000000..bf31193782d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/constants.ts@@ -0,0 +1,3 @@+import { isCypressActive } from "metabase/env";++export const POLLING_INTERVAL = isCypressActive ? 200 : 3000;diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/CurrentWorkspacePage.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/CurrentWorkspacePage.tsxindex c58225ffb32..f77d3506778 100644--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/CurrentWorkspacePage.tsx+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/CurrentWorkspacePage/CurrentWorkspacePage.tsx@@ -16,7 +16,6 @@ import type {TableRemapping,} from "metabase-types/api";-import { DeleteSection } from "./DeleteSection";import { TableRemappingSection } from "./TableRemappingSection";import { getDatabasesInfo } from "./utils";@@ -90,7 +89,6 @@ function CurrentWorkspacePageBody({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.tsxindex 4b766202680..a86bc299833 100644--- 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.tsx@@ -28,7 +28,6 @@ function setup({output: { schema: "ws_dev" },}),},- can_write: true,}),} = {}) {setupDatabasesEndpoints([POSTGRES]);@@ -68,33 +67,4 @@ describe("CurrentWorkspacePage", () => {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/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.tsxindex 09376acc35b..c8070890504 100644--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/NewWorkspaceButton.tsx+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceButton/NewWorkspaceButton.tsx@@ -1,13 +1,11 @@import { useDisclosure } from "@mantine/hooks";import { t } from "ttag";-import {- hasFeature,- hasWorkspacesEnabled,-} from "metabase/common/utils/database";+import { useSetting } from "metabase/common/hooks";import { Button, FixedSizeIcon, Tooltip } from "metabase/ui";import type { Database } from "metabase-types/api";+import { getEligibleDatabases } from "../../../utils";import { NewWorkspaceModal } from "../NewWorkspaceModal";export type NewWorkspaceButtonProps = {@@ -20,25 +18,28 @@ export function NewWorkspaceButton({primary,}: NewWorkspaceButtonProps) {const [opened, { open, close }] = useDisclosure(false);+ const isRemoteSyncEnabled = useSetting("remote-sync-enabled") ?? false;- const eligibleDatabases = databases.filter(- (database) =>- hasFeature(database, "workspace") && hasWorkspacesEnabled(database),- );+ const eligibleDatabases = getEligibleDatabases(databases);const hasEligibleDatabase = eligibleDatabases.length > 0;+ const canCreateWorkspace = hasEligibleDatabase && isRemoteSyncEnabled;return (<><Tooltip- label={t`You need to enable workspaces on at least one database.`}- disabled={hasEligibleDatabase}+ label={+ hasEligibleDatabase+ ? t`You need to set up remote sync.`+ : t`You need to enable workspaces on at least one database.`+ }+ disabled={canCreateWorkspace}><Buttonvariant={primary ? "filled" : "default"}leftSection={primary ? undefined : <FixedSizeIcon name="add" aria-hidden />}- disabled={!hasEligibleDatabase}+ disabled={!canCreateWorkspace}onClick={open}>{primary ? t`Create a workspace` : t`New`}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.tsxindex 39eb7975856..7578af005eb 100644--- 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.tsx@@ -1,29 +1,35 @@import userEvent from "@testing-library/user-event";import { renderWithProviders, screen } from "__support__/ui";+import { createMockSettingsState } from "metabase/redux/store/mocks";import type { Database } from "metabase-types/api";import { createMockDatabase } from "metabase-types/api/mocks";import { NewWorkspaceButton } from "./NewWorkspaceButton";+const ELIGIBLE_DATABASE = createMockDatabase({+ features: ["workspace"],+ settings: { "database-enable-workspaces": true },+});+type SetupOpts = {databases?: Database[];+ isRemoteSyncEnabled?: boolean;};-function setup({ databases = [] }: SetupOpts = {}) {- renderWithProviders(<NewWorkspaceButton databases={databases} />);+function setup({ databases = [], isRemoteSyncEnabled = true }: SetupOpts = {}) {+ renderWithProviders(<NewWorkspaceButton databases={databases} />, {+ storeInitialState: {+ settings: createMockSettingsState({+ "remote-sync-enabled": isRemoteSyncEnabled,+ }),+ },+ });}describe("NewWorkspaceButton", () => {it("opens the New workspace modal when clicked", async () => {- setup({- databases: [- createMockDatabase({- features: ["workspace"],- settings: { "database-enable-workspaces": true },- }),- ],- });+ setup({ databases: [ELIGIBLE_DATABASE] });await userEvent.click(screen.getByRole("button", { name: /New/ }));@@ -37,4 +43,10 @@ describe("NewWorkspaceButton", () => {expect(screen.getByRole("button", { name: /New/ })).toBeDisabled();});++ it("is disabled when remote sync is not set up", () => {+ setup({ databases: [ELIGIBLE_DATABASE], isRemoteSyncEnabled: false });++ expect(screen.getByRole("button", { name: /New/ })).toBeDisabled();+ });});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.tsxindex 1659e893adb..7d3a2f05d49 100644--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.tsx+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/NewWorkspaceModal/NewWorkspaceModal.tsx@@ -1,7 +1,9 @@+import { useFormikContext } from "formik";+import { type ChangeEvent, useState } from "react";+import slugg from "slugg";import { t } from "ttag";import * as Yup from "yup";-import { useToast } from "metabase/common/hooks/use-toast";import {Form,FormCheckboxGroup,@@ -15,11 +17,11 @@ import * as Errors from "metabase/utils/errors";import {useCreateWorkspaceMutation,useLazyListWorkspacesQuery,+ useProvisionWorkspaceMutation,} 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[];@@ -52,18 +54,23 @@ export function NewWorkspaceModal({type NewWorkspaceFormValues = {name: string;+ target_branch: string;database_ids: string[];};const NEW_WORKSPACE_SCHEMA = Yup.object({name: Yup.string().required(Errors.required),+ target_branch: Yup.string().required(Errors.required),database_ids: Yup.array().of(Yup.string()).min(1, Errors.required),});-const INITIAL_VALUES: NewWorkspaceFormValues = {- name: "",- database_ids: [],-};+function getInitialValues(databases: Database[]): NewWorkspaceFormValues {+ return {+ name: "",+ target_branch: "",+ database_ids: databases.length === 1 ? [String(databases[0].id)] : [],+ };+}type NewWorkspaceFormProps = {databases: Database[];@@ -77,58 +84,34 @@ function NewWorkspaceForm({onClose,}: NewWorkspaceFormProps) {const [createWorkspace] = useCreateWorkspaceMutation();+ const [provisionWorkspace] = useProvisionWorkspaceMutation();const [fetchWorkspaces] = useLazyListWorkspacesQuery();- const [sendToast] = useToast();const handleSubmit = async ({name,+ target_branch,database_ids,}: NewWorkspaceFormValues) => {const workspace = await createWorkspace({name,+ target_branch,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 provisionWorkspace(workspace.id);await fetchWorkspaces();onCreate(workspace);};return (<FormProvider- initialValues={INITIAL_VALUES}+ initialValues={getInitialValues(databases)}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>+ <NewWorkspaceFormFields databases={databases} /><FormErrorMessage /><Group justify="flex-end"><Button onClick={onClose}>{t`Cancel`}</Button>@@ -139,3 +122,47 @@ function NewWorkspaceForm({</FormProvider>);}++type NewWorkspaceFormFieldsProps = {+ databases: Database[];+};++function NewWorkspaceFormFields({ databases }: NewWorkspaceFormFieldsProps) {+ const { setFieldValue } = useFormikContext<NewWorkspaceFormValues>();+ const [isBranchTouched, setIsBranchTouched] = useState(false);++ const handleNameChange = (event: ChangeEvent<HTMLInputElement>) => {+ if (!isBranchTouched) {+ setFieldValue("target_branch", slugg(event.target.value));+ }+ };++ return (+ <>+ <FormTextInput+ name="name"+ label={t`Name`}+ placeholder={t`My workspace`}+ data-autofocus+ onChange={handleNameChange}+ />+ <FormTextInput+ name="target_branch"+ label={t`Branch`}+ placeholder={t`my-workspace`}+ onChange={() => setIsBranchTouched(true)}+ />+ <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>+ </>+ );+}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.tsxindex be957354703..0bc0b5f2097 100644--- 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.tsx@@ -1,16 +1,17 @@import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";import {setupCreateWorkspaceEndpoint,setupListWorkspacesEndpoint,+ setupProvisionWorkspaceEndpoint,} 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 type { Database, Workspace } from "metabase-types/api";import {createMockDatabase,createMockWorkspace,- createMockWorkspaceDatabase,} from "metabase-types/api/mocks";import { NewWorkspaceModal } from "./NewWorkspaceModal";@@ -19,23 +20,27 @@ const { trackSimpleEvent } = jest.requireMock("metabase/analytics");type SetupOpts = {createdWorkspace?: Workspace;+ databases?: Database[];};const DATABASE = createMockDatabase({ id: 10, name: "Postgres" });+const DATABASE_2 = createMockDatabase({ id: 20, name: "MySQL" });function setup({createdWorkspace = createMockWorkspace({ name: "Brand new workspace" }),+ databases = [DATABASE, DATABASE_2],}: SetupOpts = {}) {const onCreate = jest.fn();const onClose = jest.fn();setupCreateWorkspaceEndpoint(createdWorkspace);+ setupProvisionWorkspaceEndpoint(createdWorkspace);setupListWorkspacesEndpoint([createdWorkspace]);renderWithProviders(<><NewWorkspaceModal- databases={[DATABASE]}+ databases={databases}openedonCreate={onCreate}onClose={onClose}@@ -64,32 +69,83 @@ describe("NewWorkspaceModal", () => {await waitFor(() =>expect(onCreate).toHaveBeenCalledWith(createdWorkspace),);+ expect(+ fetchMock.callHistory.called(+ `path:/api/ee/workspace-manager/${createdWorkspace.id}/provision`,+ { method: "POST" },+ ),+ ).toBe(true);});- 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" }),- }),- ],- }),+ it("fills the branch with the sluggified name until manually edited", async () => {+ setup();++ await userEvent.type(screen.getByLabelText("Name"), "Brand New Workspace");+ expect(screen.getByLabelText("Branch")).toHaveValue("brand-new-workspace");+ });++ it("keeps a manually edited branch when the name changes", async () => {+ setup();++ await userEvent.type(screen.getByLabelText("Branch"), "custom-branch");+ await userEvent.type(screen.getByLabelText("Name"), "Brand new workspace");++ expect(screen.getByLabelText("Branch")).toHaveValue("custom-branch");+ });++ it("sends the branch when creating a workspace", async () => {+ const { onCreate } = 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).toHaveBeenCalled());+ const call = fetchMock.callHistory.lastCall(+ "path:/api/ee/workspace-manager",+ { method: "POST" },+ );+ expect(await call?.request?.json()).toMatchObject({+ target_branch: "brand-new-workspace",});+ });++ it("requires a branch", async () => {+ const { onCreate } = setup();await userEvent.type(screen.getByLabelText("Name"), "Brand new workspace");+ await userEvent.clear(screen.getByLabelText("Branch"));await userEvent.click(screen.getByRole("checkbox", { name: "Postgres" }));await userEvent.click(screen.getByRole("button", { name: "Create workspace" }),);+ expect(onCreate).not.toHaveBeenCalled();+ });++ it("preselects the database when it is the only one available", async () => {+ const { onCreate, createdWorkspace } = setup({ databases: [DATABASE] });++ expect(screen.getByRole("checkbox", { name: "Postgres" })).toBeChecked();+ await userEvent.type(screen.getByLabelText("Name"), "Brand new workspace");+ await userEvent.click(+ screen.getByRole("button", { name: "Create workspace" }),+ );++ await waitFor(() =>+ expect(onCreate).toHaveBeenCalledWith(createdWorkspace),+ );+ });++ it("preselects nothing when several databases are available", () => {+ setup();+expect(- await screen.findByText("Failed to provision the workspace."),- ).toBeInTheDocument();- await waitFor(() => expect(onCreate).toHaveBeenCalled());+ screen.getByRole("checkbox", { name: "Postgres" }),+ ).not.toBeChecked();+ expect(screen.getByRole("checkbox", { name: "MySQL" })).not.toBeChecked();});it("requires at least one database", async () => {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/StatusDetailsModal/StatusDetailsModal.module.css b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/StatusDetailsModal/StatusDetailsModal.module.cssnew file mode 100644index 00000000000..0abd65746e0--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/StatusDetailsModal/StatusDetailsModal.module.css@@ -0,0 +1,5 @@+.codeContainer {+ border: var(--border-size) var(--border-style) var(--mb-color-border-neutral);+ border-radius: var(--default-border-radius);+ overflow: hidden;+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/StatusDetailsModal/StatusDetailsModal.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/StatusDetailsModal/StatusDetailsModal.tsxnew file mode 100644index 00000000000..f5735e27aac--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/StatusDetailsModal/StatusDetailsModal.tsx@@ -0,0 +1,33 @@+import { CodeEditor } from "metabase/common/components/CodeEditor";+import { Modal } from "metabase/ui";+import type { Workspace } from "metabase-types/api";++import { getStatusMessage } from "../../../utils";++import S from "./StatusDetailsModal.module.css";++export type StatusDetailsModalProps = {+ workspace: Workspace;+ opened: boolean;+ onClose: () => void;+};++export function StatusDetailsModal({+ workspace,+ opened,+ onClose,+}: StatusDetailsModalProps) {+ return (+ <Modal+ title={getStatusMessage(workspace.status)}+ opened={opened}+ padding="xl"+ size="lg"+ onClose={onClose}+ >+ <div className={S.codeContainer}>+ <CodeEditor value={workspace.status_details ?? ""} readOnly />+ </div>+ </Modal>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/StatusDetailsModal/index.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/StatusDetailsModal/index.tsnew file mode 100644index 00000000000..d2adc285d59--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/StatusDetailsModal/index.ts@@ -0,0 +1 @@+export { StatusDetailsModal } from "./StatusDetailsModal";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.tsxindex 69df2534384..9ed5b50d078 100644--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.tsx+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceEmptyState/WorkspaceEmptyState.tsx@@ -1,13 +1,12 @@-import { useDisclosure } from "@mantine/hooks";-import { t } from "ttag";+import { jt, t } from "ttag";-import { useDocsUrl } from "metabase/common/hooks";+import { useDocsUrl, useSetting } from "metabase/common/hooks";import { useSelector } from "metabase/redux";import { Link } from "metabase/router";import { getApplicationName } from "metabase/selectors/whitelabel";import {+ Anchor,Box,- Button,Card,Divider,FixedSizeIcon,@@ -16,11 +15,11 @@ import {Text,Title,} from "metabase/ui";+import * as Urls from "metabase/urls";import type { Database } from "metabase-types/api";-import { trackWorkspaceSetupButtonClicked } from "../../../analytics";+import { getEligibleDatabases } from "../../../utils";import { NewWorkspaceButton } from "../NewWorkspaceButton";-import { SetupWorkspaceModal } from "../SetupWorkspaceModal";import S from "./WorkspaceEmptyState.module.css";@@ -29,8 +28,6 @@ export type WorkspaceEmptyStateProps = {};export function WorkspaceEmptyState({ databases }: WorkspaceEmptyStateProps) {- const [isSetupOpen, { open: openSetup, close: closeSetup }] =- useDisclosure(false);const applicationName = useSelector(getApplicationName);const { url: fileBasedDevDocsUrl, showMetabaseLinks: showFileBasedDevLink } =@@ -38,60 +35,82 @@ export function WorkspaceEmptyState({ databases }: WorkspaceEmptyStateProps) {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} />- </>+ <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="lg">+ {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>+ <CreateWorkspaceSection databases={databases} />+ {(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>);}+type CreateWorkspaceSectionProps = {+ databases: Database[];+};++function CreateWorkspaceSection({ databases }: CreateWorkspaceSectionProps) {+ const isRemoteSyncEnabled = useSetting("remote-sync-enabled");+ const hasEligibleDatabase = getEligibleDatabases(databases).length > 0;++ if (!hasEligibleDatabase) {+ return (+ <Text>+ {jt`To create a workspace, you need to enable workspaces on at least one database in ${(+ <Anchor+ key="link"+ component={Link}+ to={Urls.viewDatabases()}+ >{t`admin settings`}</Anchor>+ )}.`}+ </Text>+ );+ }++ if (!isRemoteSyncEnabled) {+ return (+ <Text>+ {jt`To create a workspace, you need to set up remote sync in ${(+ <Anchor+ key="link"+ component={Link}+ to={Urls.remoteSyncSettings()}+ >{t`admin settings`}</Anchor>+ )}.`}+ </Text>+ );+ }++ return <NewWorkspaceButton databases={databases} primary />;+}+type DocsLinkProps = {title: string;description: string;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.tsxindex faf8465ab41..9fafd51b68e 100644--- 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.tsx@@ -1,32 +1,80 @@-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 type { Database } from "metabase-types/api";+import { createMockDatabase } from "metabase-types/api/mocks";import { WorkspaceEmptyState } from "./WorkspaceEmptyState";-function setup() {- setupApplyAdvancedConfigEndpoint();+const ELIGIBLE_DATABASE = createMockDatabase({+ features: ["workspace"],+ settings: { "database-enable-workspaces": true },+});++type SetupOpts = {+ databases?: Database[];+ isRemoteSyncEnabled?: boolean;+};++function setup({ databases = [], isRemoteSyncEnabled = true }: SetupOpts = {}) {renderWithProviders(- <Route path="*" element={<WorkspaceEmptyState databases={[]} />} />,+ <Route path="*" element={<WorkspaceEmptyState databases={databases} />} />,{withRouter: true,storeInitialState: {- settings: createMockSettingsState({ "show-metabase-links": true }),+ settings: createMockSettingsState({+ "show-metabase-links": true,+ "remote-sync-enabled": isRemoteSyncEnabled,+ }),},},);}describe("WorkspaceEmptyState", () => {- it("renders the create workspace button", () => {- setup();+ it("renders the create workspace button when there is an eligible database", () => {+ setup({ databases: [ELIGIBLE_DATABASE] });expect(screen.getByRole("button", { name: "Create a workspace" }),).toBeInTheDocument();});+ it("links to the databases admin page when no database is eligible", () => {+ setup({ databases: [createMockDatabase()] });++ expect(+ screen.queryByRole("button", { name: "Create a workspace" }),+ ).not.toBeInTheDocument();+ expect(+ screen.getByText(/enable workspaces on at least one database/),+ ).toBeInTheDocument();+ expect(+ screen.getByRole("link", { name: "admin settings" }),+ ).toHaveAttribute("href", "/admin/databases");+ });++ it("links to the remote sync settings when remote sync is not set up", () => {+ setup({ databases: [ELIGIBLE_DATABASE], isRemoteSyncEnabled: false });++ expect(+ screen.queryByRole("button", { name: "Create a workspace" }),+ ).not.toBeInTheDocument();+ expect(screen.getByText(/set up remote sync/)).toBeInTheDocument();+ expect(+ screen.getByRole("link", { name: "admin settings" }),+ ).toHaveAttribute("href", "/admin/settings/remote-sync");+ });++ it("shows the database error first when both are missing", () => {+ setup({ databases: [], isRemoteSyncEnabled: false });++ expect(+ screen.getByText(/enable workspaces on at least one database/),+ ).toBeInTheDocument();+ expect(screen.queryByText(/set up remote sync/)).not.toBeInTheDocument();+ });+it("renders docs link buttons", () => {setup();@@ -38,11 +86,11 @@ describe("WorkspaceEmptyState", () => {).toBeInTheDocument();});- it("shows the upload-config button", () => {+ it("does not offer a config upload", () => {setup();expect(- screen.getByRole("button", { name: "Upload a workspace config" }),- ).toBeInTheDocument();+ screen.queryByRole("button", { name: /upload/i }),+ ).not.toBeInTheDocument();});});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.tsxindex 4bb84881e54..f776ab002c1 100644--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.tsx+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.tsx@@ -1,29 +1,38 @@import { useDisclosure } from "@mantine/hooks";import { t } from "ttag";+import { useConfirmation } from "metabase/common/hooks/use-confirmation";import {ActionIcon,+ Anchor,Box,Card,FixedSizeIcon,Group,+ Loader,Menu,Stack,+ Tooltip,} from "metabase/ui";import { getRelativeTime } from "metabase/utils/time-dayjs";import { getUserName } from "metabase/utils/user";+import {+ useDeleteWorkspaceMutation,+ useDeprovisionWorkspaceMutation,+ useProvisionWorkspaceMutation,+} from "metabase-enterprise/api";import type { Workspace, WorkspaceDatabase } from "metabase-types/api";-import { trackWorkspaceConfigDownloaded } from "../../../analytics";import {- getProvisioningFailureMessage,- getWorkspaceDatabaseName,- isUnprovisioned,+ getDatabaseName,+ getStatusMessage,+ isDeprovisioned,+ isDeprovisioning,+ isProvisioned,+ isProvisioning,} from "../../../utils";-import { DeleteWorkspaceModal } from "../DeleteWorkspaceModal";import { RenameWorkspaceModal } from "../RenameWorkspaceModal";--const CONFIG_FILENAME = "config.yml";+import { StatusDetailsModal } from "../StatusDetailsModal";export type WorkspaceItemProps = {workspace: Workspace;@@ -47,7 +56,14 @@ export function WorkspaceItem({ workspace }: WorkspaceItemProps) {{workspace.name}</Box><WorkspaceCreatorInfo workspace={workspace} />- {databases.some(isUnprovisioned) && <WorkspaceProvisioningWarning />}+ {isProvisioned(workspace) ? (+ <WorkspaceInstanceItem instanceUrl={workspace.instance_url} />+ ) : (+ <WorkspaceStatusItem workspace={workspace} />+ )}+ {workspace.target_branch != null && (+ <WorkspaceBranchItem targetBranch={workspace.target_branch} />+ )}{databases.map((workspaceDatabase) => (<WorkspaceDatabaseItemkey={workspaceDatabase.database_id}@@ -78,21 +94,96 @@ function WorkspaceCreatorInfo({ workspace }: WorkspaceCreatorInfoProps) {);}-type WorkspaceDatabaseItemProps = {- workspaceDatabase: WorkspaceDatabase;+type WorkspaceStatusItemProps = {+ workspace: Workspace;+};++function WorkspaceStatusItem({ workspace }: WorkspaceStatusItemProps) {+ const showDetails = workspace.status_details != null;++ return (+ <Group gap="xs" wrap="nowrap">+ <WorkspaceStatusIcon workspace={workspace} />+ <Box c="text-primary" lh="1rem">+ {getStatusMessage(workspace.status)}+ </Box>+ {showDetails && <StatusDetailsButton workspace={workspace} />}+ </Group>+ );+}++function StatusDetailsButton({ workspace }: WorkspaceStatusItemProps) {+ const [isDetailsOpen, { open: openDetails, close: closeDetails }] =+ useDisclosure(false);++ return (+ <>+ <Tooltip label={t`See details`}>+ <ActionIcon size="xs" aria-label={t`See details`} onClick={openDetails}>+ <FixedSizeIcon name="info" aria-hidden />+ </ActionIcon>+ </Tooltip>+ <StatusDetailsModal+ workspace={workspace}+ opened={isDetailsOpen}+ onClose={closeDetails}+ />+ </>+ );+}++function WorkspaceStatusIcon({ workspace }: WorkspaceStatusItemProps) {+ if (isProvisioning(workspace) || isDeprovisioning(workspace)) {+ return <Loader size="xs" />;+ }+ return <FixedSizeIcon name="warning" c="text-secondary" aria-hidden />;+}++type WorkspaceInstanceItemProps = {+ instanceUrl: string | null;};-function WorkspaceProvisioningWarning() {+function WorkspaceInstanceItem({ instanceUrl }: WorkspaceInstanceItemProps) {return (<Box c="text-secondary" lh="1rem"><Group gap="xs" wrap="nowrap">- <FixedSizeIcon name="warning" aria-hidden />- {getProvisioningFailureMessage()}+ <FixedSizeIcon name="workspace" aria-hidden />+ {instanceUrl != null ? (+ <Anchor+ href={instanceUrl}+ target="_blank"+ rel="noopener noreferrer"+ lh="inherit"+ >+ {instanceUrl}+ </Anchor>+ ) : (+ t`No workspace instance yet`+ )}+ </Group>+ </Box>+ );+}++type WorkspaceBranchItemProps = {+ targetBranch: string;+};++function WorkspaceBranchItem({ targetBranch }: WorkspaceBranchItemProps) {+ return (+ <Box c="text-secondary" lh="1rem">+ <Group gap="xs" wrap="nowrap">+ <FixedSizeIcon name="git_branch" aria-hidden />+ {targetBranch}</Group></Box>);}+type WorkspaceDatabaseItemProps = {+ workspaceDatabase: WorkspaceDatabase;+};+function WorkspaceDatabaseItem({workspaceDatabase,}: WorkspaceDatabaseItemProps) {@@ -100,7 +191,7 @@ function WorkspaceDatabaseItem({<Box c="text-secondary" lh="1rem"><Group gap="xs" wrap="nowrap"><FixedSizeIcon name="database" aria-hidden />- {getWorkspaceDatabaseName(workspaceDatabase)}+ {getDatabaseName(workspaceDatabase)}</Group></Box>);@@ -113,28 +204,69 @@ type WorkspaceMenuProps = {function WorkspaceMenu({ workspace }: WorkspaceMenuProps) {const [isRenameOpen, { open: openRename, close: closeRename }] =useDisclosure(false);- const [isDeleteOpen, { open: openDelete, close: closeDelete }] =- useDisclosure(false);+ const [provisionWorkspace, { isLoading: isProvisionLoading }] =+ useProvisionWorkspaceMutation();+ const [deprovisionWorkspace, { isLoading: isDeprovisionLoading }] =+ useDeprovisionWorkspaceMutation();+ const [deleteWorkspace, { isLoading: isDeleteLoading }] =+ useDeleteWorkspaceMutation();+ const { modalContent, show: showConfirmation } = useConfirmation();+ const isInFlight = isProvisioning(workspace) || isDeprovisioning(workspace);++ const handleProvision = () => {+ showConfirmation({+ title: t`Provision this workspace?`,+ message: t`This will set up temporary database users and schemas and a workspace instance.`,+ confirmButtonText: t`Provision`,+ confirmButtonProps: { color: "core-brand" },+ onConfirm: () => provisionWorkspace(workspace.id),+ });+ };++ const handleDeprovision = () => {+ showConfirmation({+ title: t`Deprovision this workspace?`,+ message: t`This will delete the workspace instance and the temporary database users and schemas that were created for this workspace.`,+ confirmButtonText: t`Deprovision`,+ onConfirm: () => deprovisionWorkspace(workspace.id),+ });+ };++ const handleDelete = () => {+ showConfirmation({+ title: t`Delete this workspace?`,+ message: t`This will delete the workspace. This can't be undone.`,+ confirmButtonText: t`Delete workspace`,+ onConfirm: () => deleteWorkspace(workspace.id),+ });+ };return (<><Menu position="bottom-end"><Menu.Target>- <ActionIcon aria-label={t`Workspace options`}>+ <ActionIcon aria-label={t`Workspace actions`}><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 })+ leftSection={<FixedSizeIcon name="play" aria-hidden />}+ disabled={+ isProvisioned(workspace) || isInFlight || isProvisionLoading+ }+ onClick={handleProvision}+ >+ {t`Provision`}+ </Menu.Item>+ <Menu.Item+ leftSection={<FixedSizeIcon name="revert" aria-hidden />}+ disabled={+ isDeprovisioned(workspace) || isInFlight || isDeprovisionLoading}+ onClick={handleDeprovision}>- {t`Download ${CONFIG_FILENAME}`}+ {t`Deprovision`}</Menu.Item><Menu.ItemleftSection={<FixedSizeIcon name="pencil" aria-hidden />}@@ -144,7 +276,8 @@ function WorkspaceMenu({ workspace }: WorkspaceMenuProps) {</Menu.Item><Menu.ItemleftSection={<FixedSizeIcon name="trash" aria-hidden />}- onClick={openDelete}+ disabled={!isDeprovisioned(workspace) || isDeleteLoading}+ onClick={handleDelete}>{t`Delete`}</Menu.Item>@@ -156,13 +289,7 @@ function WorkspaceMenu({ workspace }: WorkspaceMenuProps) {onRename={closeRename}onClose={closeRename}/>- {isDeleteOpen && (- <DeleteWorkspaceModal- workspaceId={workspace.id}- onDelete={closeDelete}- onClose={closeDelete}- />- )}+ {modalContent}</>);}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.tsxindex 9aed518284b..d5e71692ef1 100644--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceItem/WorkspaceItem.unit.spec.tsx@@ -1,7 +1,13 @@import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";-import { setupGetWorkspaceEndpoint } from "__support__/server-mocks";-import { renderWithProviders, screen } from "__support__/ui";+import {+ setupDeleteWorkspaceEndpoint,+ setupDeprovisionWorkspaceEndpoint,+ setupGetWorkspaceEndpoint,+ setupProvisionWorkspaceEndpoint,+} from "__support__/server-mocks";+import { renderWithProviders, screen, waitFor } from "__support__/ui";import {createMockDatabase,createMockUserInfo,@@ -25,6 +31,168 @@ describe("WorkspaceItem", () => {expect(screen.getByText("My workspace")).toBeInTheDocument();});+ it("provisions from the menu after confirmation", async () => {+ const workspace = createMockWorkspace({+ name: "My workspace",+ status: "unprovisioned",+ });+ setupProvisionWorkspaceEndpoint(workspace);+ setup({ workspace });++ await userEvent.click(+ screen.getByRole("button", { name: "Workspace actions" }),+ );+ await userEvent.click(+ await screen.findByRole("menuitem", { name: "Provision" }),+ );++ expect(+ await screen.findByRole("heading", { name: "Provision this workspace?" }),+ ).toBeInTheDocument();+ await userEvent.click(screen.getByRole("button", { name: "Provision" }));++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called(+ `path:/api/ee/workspace-manager/${workspace.id}/provision`,+ { method: "POST" },+ ),+ ).toBe(true);+ });+ });++ it("deprovisions from the menu after confirmation", async () => {+ const workspace = createMockWorkspace({+ name: "My workspace",+ status: "provisioned",+ });+ setupDeprovisionWorkspaceEndpoint(workspace);+ setup({ workspace });++ await userEvent.click(+ screen.getByRole("button", { name: "Workspace actions" }),+ );+ await userEvent.click(+ await screen.findByRole("menuitem", { name: "Deprovision" }),+ );++ expect(+ await screen.findByRole("heading", {+ name: "Deprovision this workspace?",+ }),+ ).toBeInTheDocument();+ await userEvent.click(screen.getByRole("button", { name: "Deprovision" }));++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called(+ `path:/api/ee/workspace-manager/${workspace.id}/deprovision`,+ { method: "POST" },+ ),+ ).toBe(true);+ });+ });++ it("disables Provision when provisioned and Deprovision when unprovisioned", async () => {+ setup({+ workspace: createMockWorkspace({+ name: "My workspace",+ status: "provisioned",+ }),+ });++ await userEvent.click(+ screen.getByRole("button", { name: "Workspace actions" }),+ );+ expect(+ await screen.findByRole("menuitem", { name: "Provision" }),+ ).toBeDisabled();+ expect(screen.getByRole("menuitem", { name: "Deprovision" })).toBeEnabled();+ });++ it("disables Deprovision and enables Provision when unprovisioned", async () => {+ setup({+ workspace: createMockWorkspace({+ name: "My workspace",+ status: "unprovisioned",+ }),+ });++ await userEvent.click(+ screen.getByRole("button", { name: "Workspace actions" }),+ );+ expect(+ await screen.findByRole("menuitem", { name: "Deprovision" }),+ ).toBeDisabled();+ expect(screen.getByRole("menuitem", { name: "Provision" })).toBeEnabled();+ });++ it("disables Provision and Deprovision while a run is in flight", async () => {+ setup({+ workspace: createMockWorkspace({+ name: "My workspace",+ status: "database-provisioning",+ }),+ });++ await userEvent.click(+ screen.getByRole("button", { name: "Workspace actions" }),+ );+ expect(+ await screen.findByRole("menuitem", { name: "Provision" }),+ ).toBeDisabled();+ expect(+ screen.getByRole("menuitem", { name: "Deprovision" }),+ ).toBeDisabled();+ });++ it("shows status details in a modal via the See details button", async () => {+ setup({+ workspace: createMockWorkspace({+ name: "My workspace",+ status: "database-provisioning-failure",+ status_details: "warehouse down",+ }),+ });++ expect(screen.getByText("Failed to set up databases")).toBeInTheDocument();+ await userEvent.click(screen.getByRole("button", { name: "See details" }));++ const modal = await screen.findByRole("dialog");+ expect(modal).toHaveTextContent("Failed to set up databases");+ expect(modal).toHaveTextContent("warehouse down");+ });++ it("does not offer status details when there are none", () => {+ setup({+ workspace: createMockWorkspace({+ name: "My workspace",+ status: "unprovisioned",+ status_details: null,+ }),+ });++ expect(screen.getByText("Not provisioned")).toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: "See details" }),+ ).not.toBeInTheDocument();+ });++ it("shows the instance link instead of the status when provisioned", () => {+ setup({+ workspace: createMockWorkspace({+ name: "My workspace",+ status: "provisioned",+ instance_url: "https://workspace.example.com",+ }),+ });++ expect(+ screen.getByRole("link", { name: "https://workspace.example.com" }),+ ).toBeInTheDocument();+ expect(screen.queryByText("Provisioned")).not.toBeInTheDocument();+ });+it("renders the creator info when the creator is hydrated", () => {setup({workspace: createMockWorkspace({@@ -47,29 +215,29 @@ describe("WorkspaceItem", () => {expect(screen.getByText(/^Created /)).toBeInTheDocument();});- it("renders databases, falling back to the id when not hydrated", () => {+ it("renders the target branch when set", () => {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,- }),- ],+ target_branch: "feature-x",}),});- expect(screen.getByText("Postgres")).toBeInTheDocument();- expect(screen.getByText("Database 20")).toBeInTheDocument();+ expect(screen.getByText("feature-x")).toBeInTheDocument();+ });++ it("does not render a branch block when the target branch is not set", () => {+ setup({+ workspace: createMockWorkspace({+ name: "My workspace",+ target_branch: null,+ }),+ });++ expect(screen.queryByText("feature-x")).not.toBeInTheDocument();});- it("warns when a database is not provisioned", () => {+ it("renders databases, falling back to the id when not hydrated", () => {setup({workspace: createMockWorkspace({id: 1,@@ -77,17 +245,18 @@ describe("WorkspaceItem", () => {databases: [createMockWorkspaceDatabase({database_id: 10,- status: "unprovisioned",database: createMockDatabase({ id: 10, name: "Postgres" }),}),+ createMockWorkspaceDatabase({+ database_id: 20,+ database: null,+ }),],}),});- expect(- screen.getByText("Failed to provision the workspace."),- ).toBeInTheDocument();expect(screen.getByText("Postgres")).toBeInTheDocument();+ expect(screen.getByText("Database 20")).toBeInTheDocument();});it("does not warn when all databases are provisioned", () => {@@ -110,26 +279,10 @@ describe("WorkspaceItem", () => {).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" }),+ screen.getByRole("button", { name: "Workspace actions" }),);await userEvent.click(await screen.findByRole("menuitem", { name: "Rename" }),@@ -140,10 +293,32 @@ describe("WorkspaceItem", () => {).toBeInTheDocument();});- it("opens the delete modal from the menu", async () => {- setup();+ it("disables Delete unless the workspace is fully deprovisioned", async () => {+ setup({+ workspace: createMockWorkspace({+ name: "My workspace",+ status: "provisioned",+ }),+ });+ await userEvent.click(+ screen.getByRole("button", { name: "Workspace actions" }),+ );++ expect(+ await screen.findByRole("menuitem", { name: "Delete" }),+ ).toBeDisabled();+ });++ it("deletes an unprovisioned workspace from the menu after confirmation", async () => {+ const workspace = createMockWorkspace({+ name: "My workspace",+ status: "unprovisioned",+ });+ setupDeleteWorkspaceEndpoint(workspace.id);+ setup({ workspace });+await userEvent.click(- screen.getByRole("button", { name: "Workspace options" }),+ screen.getByRole("button", { name: "Workspace actions" }),);await userEvent.click(await screen.findByRole("menuitem", { name: "Delete" }),@@ -152,5 +327,17 @@ describe("WorkspaceItem", () => {expect(await screen.findByRole("heading", { name: "Delete this workspace?" }),).toBeInTheDocument();+ await userEvent.click(+ screen.getByRole("button", { name: "Delete workspace" }),+ );++ await waitFor(() => {+ expect(+ fetchMock.callHistory.called(+ `path:/api/ee/workspace-manager/${workspace.id}`,+ { method: "DELETE" },+ ),+ ).toBe(true);+ });});});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceListPage.tsx b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceListPage.tsxindex 8afb7eff734..22d74a748c3 100644--- a/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceListPage.tsx+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/pages/WorkspaceListPage/WorkspaceListPage.tsx@@ -1,3 +1,4 @@+import { useEffect, useState } from "react";import { t } from "ttag";import { useListDatabasesQuery } from "metabase/api";@@ -9,16 +10,32 @@ import { Stack } from "metabase/ui";import { useListWorkspacesQuery } from "metabase-enterprise/api";import type { Database, Workspace } from "metabase-types/api";+import { POLLING_INTERVAL } from "../../constants";+import { isDeprovisioning, isProvisioning } from "../../utils";+import { NewWorkspaceButton } from "./NewWorkspaceButton";import { WorkspaceEmptyState } from "./WorkspaceEmptyState";import { WorkspaceItem } from "./WorkspaceItem";+function shouldPoll(workspaces: Workspace[] | undefined) {+ return (workspaces ?? []).some(+ (workspace) => isProvisioning(workspace) || isDeprovisioning(workspace),+ );+}+export function WorkspaceListPage() {+ const [isPolling, setIsPolling] = useState(false);const {data: workspaces,isLoading: isLoadingWorkspaces,error: workspacesError,- } = useListWorkspacesQuery();+ } = useListWorkspacesQuery(undefined, {+ pollingInterval: isPolling ? POLLING_INTERVAL : undefined,+ });++ useEffect(() => {+ setIsPolling(shouldPoll(workspaces));+ }, [workspaces]);const {data: databasesResponse,isLoading: isLoadingDatabases,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.tsxindex 038f5e1e17c..7cfc4024283 100644--- 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.tsx@@ -6,6 +6,7 @@ import {setupListWorkspacesEndpoint,} from "__support__/server-mocks";import { renderWithProviders, screen } from "__support__/ui";+import { createMockSettingsState } from "metabase/redux/store/mocks";import { Route } from "metabase/router";import type { Workspace } from "metabase-types/api";import {@@ -28,6 +29,9 @@ function setup({ workspaces = [] as Workspace[] } = {}) {renderWithProviders(<Route path="*" element={<WorkspaceListPage />} />, {withRouter: true,+ storeInitialState: {+ settings: createMockSettingsState({ "remote-sync-enabled": true }),+ },});}@@ -55,7 +59,7 @@ describe("WorkspaceListPage", () => {const item = await screen.findByRole("region", { name: "Existing" });expect(item).toBeInTheDocument();expect(- screen.getByRole("button", { name: "Workspace options" }),+ screen.getByRole("button", { name: "Workspace actions" }),).toBeInTheDocument();});});diff --git a/enterprise/frontend/src/metabase-enterprise/workspaces/utils.ts b/enterprise/frontend/src/metabase-enterprise/workspaces/utils.tsindex b7a6c53d1c5..9cfc4178541 100644--- a/enterprise/frontend/src/metabase-enterprise/workspaces/utils.ts+++ b/enterprise/frontend/src/metabase-enterprise/workspaces/utils.ts@@ -1,23 +1,99 @@import { t } from "ttag";-import type { WorkspaceDatabase } from "metabase-types/api";+import {+ hasFeature,+ hasWorkspacesEnabled,+} from "metabase/common/utils/database";+import type {+ Database,+ Workspace,+ WorkspaceDatabase,+ WorkspaceStatus,+} from "metabase-types/api";-export function getWorkspaceDatabaseName(workspaceDatabase: WorkspaceDatabase) {+export function getDatabaseName(workspaceDatabase: WorkspaceDatabase) {return (workspaceDatabase.database?.name ??t`Database ${workspaceDatabase.database_id}`);}-export function isUnprovisioned(workspaceDatabase: WorkspaceDatabase) {- return workspaceDatabase.status === "unprovisioned";+export function getEligibleDatabases(databases: Database[]) {+ return databases.filter(+ (database) =>+ hasFeature(database, "workspace") && hasWorkspacesEnabled(database),+ );+}++export function isProvisioningFailed(workspace: Workspace) {+ return (+ workspace.status === "database-provisioning-failure" ||+ workspace.status === "branch-provisioning-failure" ||+ workspace.status === "instance-provisioning-failure"+ );}-export function isPending(workspaceDatabase: WorkspaceDatabase) {- const { status } = workspaceDatabase;- return status === "provisioning" || status === "deprovisioning";+export function isDeprovisioningFailed(workspace: Workspace) {+ return (+ workspace.status === "instance-deprovisioning-failure" ||+ workspace.status === "branch-deprovisioning-failure" ||+ workspace.status === "database-deprovisioning-failure"+ );+}++export function isProvisioned(workspace: Workspace) {+ return workspace.status === "provisioned";+}++export function isDeprovisioned(workspace: Workspace) {+ return workspace.status === "unprovisioned";+}++export function isProvisioning(workspace: Workspace) {+ return (+ workspace.status === "database-provisioning" ||+ workspace.status === "branch-provisioning" ||+ workspace.status === "instance-provisioning"+ );+}++export function isDeprovisioning(workspace: Workspace) {+ return (+ workspace.status === "instance-deprovisioning" ||+ workspace.status === "branch-deprovisioning" ||+ workspace.status === "database-deprovisioning"+ );}-export function getProvisioningFailureMessage() {- return t`Failed to provision the workspace.`;+export function getStatusMessage(status: WorkspaceStatus): string {+ switch (status) {+ case "unprovisioned":+ return t`Not provisioned`;+ case "database-provisioning":+ return t`Setting up databases…`;+ case "database-provisioning-failure":+ return t`Failed to set up databases`;+ case "branch-provisioning":+ return t`Creating the git branch…`;+ case "branch-provisioning-failure":+ return t`Failed to create the git branch`;+ case "instance-provisioning":+ return t`Setting up the instance…`;+ case "instance-provisioning-failure":+ return t`Failed to set up the instance`;+ case "provisioned":+ return t`Provisioned`;+ case "instance-deprovisioning":+ return t`Deprovisioning the instance…`;+ case "instance-deprovisioning-failure":+ return t`Failed to deprovision the instance`;+ case "branch-deprovisioning":+ return t`Deleting the git branch…`;+ case "branch-deprovisioning-failure":+ return t`Failed to delete the git branch`;+ case "database-deprovisioning":+ return t`Deprovisioning databases…`;+ case "database-deprovisioning-failure":+ return t`Failed to deprovision databases`;+ }}diff --git a/frontend/src/metabase-types/api/advanced-config.ts b/frontend/src/metabase-types/api/advanced-config.tsdeleted file mode 100644index ac4d17fa56c..00000000000--- a/frontend/src/metabase-types/api/advanced-config.ts+++ /dev/null@@ -1,26 +0,0 @@-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 = {- version: number;- config: AdvancedConfigContent;-};--export type ApplyAdvancedConfigRequest = {- config: File;-};diff --git a/frontend/src/metabase-types/api/index.ts b/frontend/src/metabase-types/api/index.tsindex 35bfd36b158..974e2b57ed6 100644--- a/frontend/src/metabase-types/api/index.ts+++ b/frontend/src/metabase-types/api/index.ts@@ -1,7 +1,6 @@export * from "./actions";export * from "./activity";export * from "./admin";-export * from "./advanced-config";export * from "./advanced-permission";export * from "./ai-entity-analysis";export * from "./alert";diff --git a/frontend/src/metabase-types/api/mocks/workspace-instance.ts b/frontend/src/metabase-types/api/mocks/workspace-instance.tsindex a900d36b0ab..c70ddbf2c52 100644--- a/frontend/src/metabase-types/api/mocks/workspace-instance.ts+++ b/frontend/src/metabase-types/api/mocks/workspace-instance.ts@@ -20,10 +20,6 @@ export function createMockCurrentWorkspace(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,};}diff --git a/frontend/src/metabase-types/api/mocks/workspace.ts b/frontend/src/metabase-types/api/mocks/workspace.tsindex 137e2a1480b..32599f53495 100644--- a/frontend/src/metabase-types/api/mocks/workspace.ts+++ b/frontend/src/metabase-types/api/mocks/workspace.ts@@ -7,6 +7,7 @@ export function createMockWorkspaceDatabase(database_id: 1,input_schemas: [],status: "provisioned",+ status_details: null,...opts,};}@@ -15,6 +16,11 @@ export function createMockWorkspace(opts?: Partial<Workspace>): Workspace {return {id: 1,name: "Test workspace",+ target_branch: null,+ status: "unprovisioned",+ status_details: null,+ instance_id: null,+ instance_url: null,databases: [],created_at: "2026-01-01T00:00:00Z",creator_id: 1,diff --git a/frontend/src/metabase-types/api/workspace-instance.ts b/frontend/src/metabase-types/api/workspace-instance.tsindex da6714e279e..3f38bc4dbca 100644--- a/frontend/src/metabase-types/api/workspace-instance.ts+++ b/frontend/src/metabase-types/api/workspace-instance.ts@@ -13,7 +13,6 @@ export type CurrentWorkspaceDatabase = {export type CurrentWorkspace = {name: string;databases: Record<DatabaseId, CurrentWorkspaceDatabase>;- can_write: boolean;};export type GetCurrentWorkspaceResponse = {diff --git a/frontend/src/metabase-types/api/workspace-manager.ts b/frontend/src/metabase-types/api/workspace-manager.tsindex 6e7f9d6bd2d..0877d9d87fc 100644--- a/frontend/src/metabase-types/api/workspace-manager.ts+++ b/frontend/src/metabase-types/api/workspace-manager.ts@@ -6,13 +6,32 @@ export type WorkspaceId = number;export type WorkspaceDatabaseStatus =| "unprovisioned"| "provisioning"+ | "provisioning-failure"| "provisioned"- | "deprovisioning";+ | "deprovisioning"+ | "deprovisioning-failure";++export type WorkspaceStatus =+ | "unprovisioned"+ | "database-provisioning"+ | "database-provisioning-failure"+ | "branch-provisioning"+ | "branch-provisioning-failure"+ | "instance-provisioning"+ | "instance-provisioning-failure"+ | "provisioned"+ | "instance-deprovisioning"+ | "instance-deprovisioning-failure"+ | "branch-deprovisioning"+ | "branch-deprovisioning-failure"+ | "database-deprovisioning"+ | "database-deprovisioning-failure";export type WorkspaceDatabase = {database_id: DatabaseId;input_schemas: string[];status: WorkspaceDatabaseStatus;+ status_details: string | null;database?: Database | null;};@@ -20,6 +39,11 @@ export type WorkspaceDatabase = {export type Workspace = {id: WorkspaceId;name: string;+ target_branch: string | null;+ status: WorkspaceStatus;+ status_details: string | null;+ instance_id: string | null;+ instance_url: string | null;created_at: string;creator_id: UserId;@@ -29,6 +53,7 @@ export type Workspace = {export type CreateWorkspaceRequest = {name: string;+ target_branch?: string;database_ids: DatabaseId[];};diff --git a/frontend/src/metabase/urls/admin.ts b/frontend/src/metabase/urls/admin.tsindex 0af27fc9a17..dc00f0cef85 100644--- a/frontend/src/metabase/urls/admin.ts+++ b/frontend/src/metabase/urls/admin.ts@@ -147,6 +147,10 @@ export function uploadsSettings() {return "/admin/settings/uploads";}+export function remoteSyncSettings() {+ return "/admin/settings/remote-sync";+}+export function adminLicense() {return "/admin/settings/license";}diff --git a/frontend/test/__support__/server-mocks/advanced-config.ts b/frontend/test/__support__/server-mocks/advanced-config.tsdeleted file mode 100644index 8d7ebf7e788..00000000000--- a/frontend/test/__support__/server-mocks/advanced-config.ts+++ /dev/null@@ -1,5 +0,0 @@-import fetchMock from "fetch-mock";--export function setupApplyAdvancedConfigEndpoint() {- fetchMock.post("path:/api/ee/advanced-config", 204);-}diff --git a/frontend/test/__support__/server-mocks/index.ts b/frontend/test/__support__/server-mocks/index.tsindex 4877b0d5cc9..b9b4c73d815 100644--- a/frontend/test/__support__/server-mocks/index.ts+++ b/frontend/test/__support__/server-mocks/index.ts@@ -1,6 +1,5 @@export * from "./action";export * from "./activity";-export * from "./advanced-config";export * from "./ai-entity-analysis";export * from "./alert";export * from "./api-key";diff --git a/frontend/test/__support__/server-mocks/workspace-instance.ts b/frontend/test/__support__/server-mocks/workspace-instance.tsindex 69a00dae6da..24bbc177571 100644--- a/frontend/test/__support__/server-mocks/workspace-instance.ts+++ b/frontend/test/__support__/server-mocks/workspace-instance.ts@@ -10,10 +10,6 @@ export function setupGetCurrentWorkspaceEndpoint(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.tsindex 86b3c713937..b8890ca3c59 100644--- a/frontend/test/__support__/server-mocks/workspace-manager.ts+++ b/frontend/test/__support__/server-mocks/workspace-manager.ts@@ -20,6 +20,14 @@ export function setupUpdateWorkspaceEndpoint(workspace: Workspace) {fetchMock.put(`${BASE_URL}/${workspace.id}`, workspace);}+export function setupProvisionWorkspaceEndpoint(workspace: Workspace) {+ fetchMock.post(`${BASE_URL}/${workspace.id}/provision`, workspace);+}++export function setupDeprovisionWorkspaceEndpoint(workspace: Workspace) {+ fetchMock.post(`${BASE_URL}/${workspace.id}/deprovision`, workspace);+}+export function setupDeleteWorkspaceEndpoint(workspaceId: WorkspaceId) {fetchMock.delete(`${BASE_URL}/${workspaceId}`, { status: 204 });}diff --git a/mage/src/mage/modules.clj b/mage/src/mage/modules.cljindex ce76fab3ab6..d3b67ef4604 100644--- a/mage/src/mage/modules.clj+++ b/mage/src/mage/modules.clj@@ -111,6 +111,7 @@eid-translationembeddingenterprise/api+ enterprise/remote-syncenterprise/scimenterprise/serializationenterprise/ssodiff --git a/resources/migrations/062/20260421_workspaces.yaml b/resources/migrations/062/20260421_workspaces.yamlindex 7ce68490fe8..26b7b19f3cb 100644--- a/resources/migrations/062/20260421_workspaces.yaml+++ b/resources/migrations/062/20260421_workspaces.yaml@@ -286,3 +286,9 @@ databaseChangeLog:columns:- column:name: workspace_id+ ## No-op rollback: after any later ALTER TABLE on workspace_database, H2+ ## re-binds fk_workspace_database_workspace_id to this index, so the+ ## auto-generated DROP INDEX fails and aborts the whole downgrade. Any+ ## rollback that crosses this changeset also rolls back the createTable+ ## below, which removes the index with the table.+ rollback: []diff --git a/resources/migrations/064/20260718_workspace_status_columns.yaml b/resources/migrations/064/20260718_workspace_status_columns.yamlnew file mode 100644index 00000000000..41b7ecfaaa7--- /dev/null+++ b/resources/migrations/064/20260718_workspace_status_columns.yaml@@ -0,0 +1,115 @@+## Workspace lifecycle statuses: workspaces get their own status/status_details+## columns (provisioning is now started explicitly and runs async), and+## workspace_database rows record the failure message of their last+## provision/deprovision attempt in status_details.++databaseChangeLog:+ - changeSet:+ id: v64.2026-07-18T10:00:00+ author: ranquild+ comment: Add workspace.status+ changes:+ - addColumn:+ tableName: workspace+ columns:+ - column:+ name: status+ type: varchar(254)+ defaultValue: unprovisioned+ remarks: Provisioning lifecycle state of the workspace+ constraints:+ nullable: false++ - changeSet:+ id: v64.2026-07-18T10:00:01+ author: ranquild+ comment: Add workspace.status_details+ changes:+ - addColumn:+ tableName: workspace+ columns:+ - column:+ name: status_details+ type: ${text.type}+ remarks: Encrypted details for the current status, e.g. the provisioning failure message+ constraints:+ nullable: true++ - changeSet:+ id: v64.2026-07-18T10:00:02+ author: ranquild+ comment: Add workspace_database.status_details+ changes:+ - addColumn:+ tableName: workspace_database+ columns:+ - column:+ name: status_details+ type: ${text.type}+ remarks: Encrypted details for the current status, e.g. the provisioning failure message+ constraints:+ nullable: true++ - changeSet:+ id: v64.2026-07-18T10:00:03+ author: ranquild+ comment: Add workspace.instance_id+ changes:+ - addColumn:+ tableName: workspace+ columns:+ - column:+ name: instance_id+ type: varchar(254)+ remarks: External identifier of the provisioned child Metabase instance+ constraints:+ nullable: true++ - changeSet:+ id: v64.2026-07-18T10:00:04+ author: ranquild+ comment: Add workspace.instance_url+ changes:+ - addColumn:+ tableName: workspace+ columns:+ - column:+ name: instance_url+ type: ${text.type}+ remarks: URL of the provisioned child Metabase instance+ constraints:+ nullable: true++ - changeSet:+ id: v64.2026-07-18T10:00:05+ author: ranquild+ comment: Widen workspace_database.status for the longer failure statuses+ changes:+ - modifyDataType:+ tableName: workspace_database+ columnName: status+ newDataType: varchar(254)+ rollback: []++ - changeSet:+ id: v64.2026-07-18T10:00:06+ author: ranquild+ comment: Preserve NOT NULL on workspace_database.status after widening+ changes:+ - addNotNullConstraint:+ tableName: workspace_database+ columnName: status+ columnDataType: varchar(254)+ rollback: []++ - changeSet:+ id: v64.2026-07-18T10:00:07+ author: ranquild+ comment: Preserve the default on workspace_database.status after widening+ changes:+ - addDefaultValue:+ tableName: workspace_database+ columnName: status+ columnDataType: varchar(254)+ defaultValue: unprovisioned+ rollback: []diff --git a/resources/migrations/064/20260720_workspace_target_branch.yaml b/resources/migrations/064/20260720_workspace_target_branch.yamlnew file mode 100644index 00000000000..3df6f6d3f05--- /dev/null+++ b/resources/migrations/064/20260720_workspace_target_branch.yaml@@ -0,0 +1,32 @@+## Workspaces get a target_branch: the git branch the workspace's child+## instance commits to via remote sync. Unique across workspaces so two+## workspaces can't fight over the same branch.++databaseChangeLog:+ - changeSet:+ id: v64.2026-07-20T10:00:00+ author: ranquild+ comment: Add workspace.target_branch+ changes:+ - addColumn:+ tableName: workspace+ columns:+ - column:+ name: target_branch+ type: varchar(254)+ remarks: Git branch the workspace's child instance commits to via remote sync+ constraints:+ nullable: true++ - changeSet:+ id: v64.2026-07-20T10:00:01+ author: ranquild+ comment: Unique index on workspace.target_branch+ changes:+ - createIndex:+ tableName: workspace+ indexName: idx_workspace_target_branch+ unique: true+ columns:+ - column:+ name: target_branchdiff --git a/src/metabase/driver.clj b/src/metabase/driver.cljindex cf7b82c2f6f..1ae9d9582c0 100644--- a/src/metabase/driver.clj+++ b/src/metabase/driver.clj@@ -2048,9 +2048,9 @@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."+ overlay when configured. The default `database-provisioner` in+ `metabase-enterprise.workspaces.provisioning.database` 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)diff --git a/src/metabase/models/interface.clj b/src/metabase/models/interface.cljindex f483a45bbd4..8b2ffd55587 100644--- a/src/metabase/models/interface.clj+++ b/src/metabase/models/interface.clj@@ -327,6 +327,11 @@{:in encrypted-json-in:out cached-encrypted-json-out})+(def transform-encrypted-text+ "Transform for encrypted plain-text columns."+ {:in encryption/maybe-encrypt+ :out encryption/maybe-decrypt})+;;; TODO (Cam 10/27/25) -- this stuff should be moved into a different module instead of the general models interface,;;; either `queries` or a new module along with [[metabase.models.visualization-settings]].(mr/def ::viz-settings-refdiff --git a/src/metabase/workspaces/core.clj b/src/metabase/workspaces/core.cljindex ba30d97aee6..17028c7f68c 100644--- a/src/metabase/workspaces/core.clj+++ b/src/metabase/workspaces/core.clj@@ -35,7 +35,7 @@## OSS vs EEThe OSS jar has no workspace mode (`workspace-mode?` returns false), so- the gates are no-ops. The EE impl in `metabase-enterprise.workspaces.core`+ the gates are no-ops. The EE impl in `metabase-enterprise.workspaces.instance`reads the `instance-workspace` setting."(:require[metabase.premium-features.core :refer [defenterprise]]@@ -81,12 +81,12 @@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+ `metabase-enterprise.workspaces.instance/db-workspace-namespace` (returns theworkspace-isolated output namespace map if this instance is in workspace modeAND the workspace's `:databases` map includes this database).The OSS fallback returns false."- metabase-enterprise.workspaces.core+ metabase-enterprise.workspaces.instance[]false)