The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Workspaces for editing Transforms (#66015)
Vamsi Peridbe8b1f36d26
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.ednindex f1208dcde82..cd5abf82220 100644--- a/.clj-kondo/config.edn+++ b/.clj-kondo/config.edn@@ -1,7 +1,7 @@;; -*- comment-column: 100; -*-{:config-paths ["macros" "src" "config"]:linters- { ;;+ {;;;; enabled optional linters (these are disabled by default);;@@ -179,6 +179,11 @@metabase.notification.models/create-notification!metabase.pulse.send/send-pulse!metabase-enterprise.transforms-python.python-runner-test/execute!+ ;; Workspace isolation functions are safe in parallel tests because they operate on+ ;; isolated, test-specific workspaces and databases that don't affect other tests+ metabase-enterprise.workspaces.isolation/destroy-workspace-isolation!+ metabase-enterprise.workspaces.isolation/ensure-database-isolation!+ metabase-enterprise.workspaces.test-util/create-ready-ws!;; Safe in tests, where we typically use a private, temporary index per thread.metabase.search.core/reindex!metabase.util/run-count!@@ -192,7 +197,7 @@:metabase/disallow-hardcoded-driver-names-in-tests;; don't include `:h2` since that will probably lead to a mountain of tests where we are specifically testing;; middleware or compilation behavior with just H2 since it's the default.- {:drivers #{ ;; core drivers+ {:drivers #{;; core drivers#_:h2:postgres:mysql@@ -424,6 +429,8 @@metabase-enterprise.transforms.api-test/with-worker-runs clojure.core/letmetabase-enterprise.transforms.test-util/with-transform-cleanup! clojure.core/letmetabase-enterprise.transforms.execute/with-transform-lifecycle clojure.core/let+ metabase-enterprise.workspaces.test-util/with-resources! clojure.core/let+ metabase-enterprise.workspaces.test-util/with-workspaces! clojure.core/letmetabase.actions.test-util/with-actions clojure.core/letmetabase.api.common/let-404 clojure.core/letmetabase.app-db.custom-migrations.util/with-temp-scheduler! clojure.core/fndiff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex e4c6f7341eb..06d522ca6a2 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -547,6 +547,11 @@metabase.driver.sql-jdbc.executemetabase.driver.sql-jdbc.execute.diagnosticmetabase.driver.sql-jdbc.sync+ ;; TODO (lbrdnk 2025/12/17): We should revisit addition of the following line. I've added it to make+ ;; tests pass as we are using it directly with ee/workspaces module. I believe later, when workspaces+ ;; are in better state quality wise, it should become part of the driver api and that should be used+ ;; instead of calling directly into the normalize module.+ metabase.driver.sql.normalizemetabase.driver.sql.query-processormetabase.driver.sql.utilmetabase.driver.sync@@ -2349,6 +2354,7 @@{:team "Querying":api #{metabase-enterprise.transforms.apimetabase-enterprise.transforms.core+ metabase-enterprise.transforms.executemetabase-enterprise.transforms.initmetabase-enterprise.transforms.instrumentationmetabase-enterprise.transforms.interface@@ -2381,7 +2387,8 @@enterprise/transforms-python{:team "Querying":api #{metabase-enterprise.transforms-python.api- metabase-enterprise.transforms-python.init}+ metabase-enterprise.transforms-python.init+ metabase-enterprise.transforms-python.python-runner}:uses #{analyticsapiapp-db@@ -2404,7 +2411,29 @@modelspremium-featuresupload- util}}}+ util}}++ enterprise/workspaces+ {:team "Metabot"+ :api #{metabase-enterprise.workspaces.api+ metabase-enterprise.workspaces.init}+ :uses #{api+ api-keys+ config+ driver+ events+ lib+ lib-be+ models+ queries+ ;; NOTE: we will remove this once we get "pure transforms" module.+ query-processor+ request+ settings+ system+ util+ enterprise/transforms+ enterprise/transforms-python}}};; namespaces matching these patterns (with `re-find`) are excluded from the module linter. Since regex literals;; aren't allowed in EDN just used the `(str <regex>)` version i.e. two slashes instead of one.diff --git a/.cljfmt.edn b/.cljfmt.ednindex bc562ca772b..614be11fd7c 100644--- a/.cljfmt.edn+++ b/.cljfmt.edn@@ -90,6 +90,7 @@metabase-enterprise.test/with-gtaps! [[:inner 0]]metabase-enterprise.test/with-gtaps-for-user! [[:inner 0]]metabase-enterprise.test/with-user-attributes [[:block 2]]+ metabase-enterprise.workspaces.models.workspace-log/track! [[:block 2]]metabase.actions.actions-test/with-actions-test-data-and-actions-permissively-enabled [[:block 0]]metabase.actions.test-util/with-actions [[:block 1]]metabase.actions.test-util/with-actions-disabled [[:block 0]]@@ -133,6 +134,7 @@metabase.driver.sql-jdbc.execute.diagnostic/capturing-diagnostic-info [[:block 1]]metabase.driver.sql.query-processor/apply-top-level-clause [[:default]]metabase.driver/with-driver [[:block 1]]+ metabase.driver/with-swapped-connection-details [[:block 2]]metabase.embedding-rest.api.embed-test/do-response-formats [[:block 1]]metabase.embedding-rest.api.embed-test/with-embedding-enabled-and-new-secret-key [[:block 0]]metabase.embedding-rest.api.embed-test/with-embedding-enabled-and-temp-card-referencing [[:block 3]]diff --git a/.dir-locals.el b/.dir-locals.elindex aad57d9e25e..3df80a91c14 100644--- a/.dir-locals.el+++ b/.dir-locals.el@@ -61,6 +61,7 @@(eval . (put-clojure-indent 'u/prog1 1))(eval . (put-clojure-indent 'u/select-keys-when '(:form)))(eval . (put-clojure-indent 'with-meta '(:form)))+ (eval . (put-clojure-indent 'ws.log/track! 2));; these ones have to be done with `define-clojure-indent' for now because of upstream bug;; https://github.com/clojure-emacs/clojure-mode/issues/600 once that's resolved we should use `put-clojure-indent';; instead. Please don't add new entries unless they don't work with `put-clojure-indent'diff --git a/.gitconfig b/.gitconfigindex 71a856daffc..2d2349c8487 100644--- a/.gitconfig+++ b/.gitconfig@@ -1,7 +1,5 @@# execute this in your shell to enable custom merge driver:# echo '\n[include]\npath=../.gitconfig' >> .git/config-#-# TODO (Bryan 12/12/25) -- reenable this when we can completely verify correctness-# [merge "yaml-migrations"]-# name = YAML migration changeset-aware merge-# driver = mage -merge-yaml-migrations %O %A %B %L %P+[merge "yaml-migrations"]+ name = YAML migration changeset-aware merge+ driver = bin/mage -merge-yaml-migrations %O %A %B %L %Pdiff --git a/bin/build/resources/overrides.edn b/bin/build/resources/overrides.ednindex 21b76fd62e7..7061feae08f 100644--- a/bin/build/resources/overrides.edn+++ b/bin/build/resources/overrides.edn@@ -24,6 +24,9 @@"dev.failsafe" {"failsafe" {:resource "LICENSE"}}"colorize" {"colorize" {:resource "EPL.txt"}}"com.github.jnr" {"jffi$native" {:resource "apache2_0.txt"}}+ "com.google.api" {"gax" {:resource "BSD.txt"}+ "gax-grpc" {:resource "BSD.txt"}+ "gax-httpjson" {:resource "BSD.txt"}}"com.google.api-client" {"google-api-client" {:resource "apache2_0.txt"}}"com.google.auto.value" {"auto-value-annotations" {:resource "apache2_0.txt"}}"com.google.http-client" {"google-http-client" {:resource "apache2_0.txt"}@@ -60,6 +63,7 @@"net.thisptr" {"jackson-jq" {:resource "apache2_0.txt"}}"org.antlr" {"antlr-runtime" {:resource "BSD.txt"}"antlr4-runtime" {:resource "BSD.txt"}}+ "org.codehaus.mojo" {"animal-sniffer-annotations" {:resource "MIT.txt"}}"org.eclipse.jgit" {"org.eclipse.jgit" {:resource "BSD.txt"}}"org.eclipse.jetty" {"jetty-webapp" {:resource "EPL.txt"}}"org.eclipse.jetty.toolchain" {"jetty-jakarta-servlet-api" {:resource "EPL.txt"}}diff --git a/bin/lint-migrations-file/src/change_set/strict.clj b/bin/lint-migrations-file/src/change_set/strict.cljindex fe6a4678846..506acf3744c 100644--- a/bin/lint-migrations-file/src/change_set/strict.clj+++ b/bin/lint-migrations-file/src/change_set/strict.clj@@ -23,7 +23,7 @@;; new ids should conform this format:id-with-timestamp ;; e.g: v49.2023-12-14T08:54:54(s/and #(re-matches id-timestamp-format-re %))- :id-with-old-verion-format ;; e.g: v49.00-008+ :id-with-old-version-format ;; e.g: v49.00-008(s/and #(re-matches id-number-format-re %);; the cut off id for this old format is v49.00-060;; see #36787 for contextdiff --git a/bin/lint-migrations-file/src/lint_migrations_file.cljc b/bin/lint-migrations-file/src/lint_migrations_file.cljcindex 5af0c8a19a4..726ca6b0615 100644--- a/bin/lint-migrations-file/src/lint_migrations_file.cljc+++ b/bin/lint-migrations-file/src/lint_migrations_file.cljc@@ -144,6 +144,18 @@change-log#(pos? (compare % "v49.00-000"))))+(defn require-primary-key-exists-has-table-name+ "Ensures that all primaryKeyExists preconditions specify a tableName."+ [change-log]+ (doseq [{:keys [changeSet]} change-log+ :when changeSet+ :let [s (pr-str (:preConditions changeSet))]+ :when (and (str/includes? s ":primaryKeyExists")+ (not (str/includes? s ":tableName")))]+ (throw (validation-error+ (format "Migration '%s' has primaryKeyExists precondition without tableName" (:id changeSet))+ {:id (:id changeSet)}))))+(s/def ::changeSet(s/spec :change-set.strict/change-set))@@ -163,6 +175,7 @@(require-no-bare-blob-or-text-types change-log)(require-no-bare-boolean-types change-log)(require-no-datetime-type change-log)+ (require-primary-key-exists-has-table-name change-log)(let [{:keys [changeSet]:as all} (group-by only-key change-log)](when-not (set/subset? (set (keys all)) #{:property :objectQuotingStrategy :changeSet})diff --git a/deps.edn b/deps.ednindex 844d3febde8..059de38362c 100644--- a/deps.edn+++ b/deps.edn@@ -33,7 +33,7 @@com.clearspring.analytics/stream {:mvn/version "2.9.8" ; Various sketching algorithms:exclusions [it.unimi.dsi/fastutilorg.slf4j/slf4j-api]}- com.clojure-goes-fast/jvm-alloc-rate-meter{:mvn/version "0.1.4"} ; Monitor heap allocation rate.+ com.clojure-goes-fast/jvm-alloc-rate-meter {:mvn/version "0.1.4"} ; Monitor heap allocation rate.com.clojure-goes-fast/jvm-hiccup-meter {:mvn/version "0.1.1"} ; Monitor GC pauses and other system-induced pauses.com.draines/postal {:mvn/version "2.0.5" ; SMTP library:exclusions [com.sun.mail/jakarta.mail]}@@ -42,7 +42,7 @@com.github.albfernandez/juniversalchardet {:mvn/version "2.5.0"}com.github.blagerweij/liquibase-sessionlock {:mvn/version "1.6.9"} ; session lock for liquibase migrationscom.github.jknack/handlebars ^:antq/exclude ; 4.4.0 requires java 17+- {:mvn/version "4.3.1"} ; templating engine+ {:mvn/version "4.3.1"} ; templating enginecom.github.oliyh/martian-clj-http {:mvn/version "0.2.1"} ; openapi clientcom.github.seancorfield/honeysql {:mvn/version "2.7.1350"} ; Honey SQL 2. SQL generation from Clojure data mapscom.github.seancorfield/next.jdbc {:mvn/version "1.3.1070"} ; Talk to JDBC DBs@@ -50,12 +50,12 @@com.github.vertical-blank/sql-formatter {:mvn/version "2.0.5"} ; Java SQL formatting library https://github.com/vertical-blank/sql-formattercom.google.guava/guava {:mvn/version "33.5.0-jre"} ; dep for BigQuery, Spark, and GA. Require here rather than letting different dep versions stomp on each other — see comments on #9697com.h2database/h2 ^:antq/exclude ; https://metaboat.slack.com/archives/CKZEMT1MJ/p1727191010259979- {:mvn/version "2.1.214"} ; embedded SQL database+ {:mvn/version "2.1.214"} ; embedded SQL databasecom.ibm.icu/icu4j {:mvn/version "78.1"} ; Used to transliterate Unicode characters into Latin characterscom.knuddels/jtokkit {:mvn/version "1.1.0"} ; Java tokenizer library for OpenAI models, used for token counting in semantic searchcom.mchange/c3p0 {:mvn/version "0.11.2"} ; DB connection poolcom.snowplowanalytics/snowplow-java-tracker ^:antq/exclude ; 2+ depend on apache httpclient 5.x, but saml and clj-http depend on 4.x- {:mvn/version "1.0.1"} ; Snowplow analytics+ {:mvn/version "1.0.1"} ; Snowplow analyticscom.sun.mail/jakarta.mail ^:antq/exclude ; 2+ changes imports from javax.mail to jakarta.mail{:mvn/version "1.6.8"} ; SMTP lib, required by postalcom.taoensso/encore {:mvn/version "3.158.0"} ; Utilities library (used by Nippy)@@ -891,8 +891,7 @@{:exec-args {:only ["enterprise/backend/test/metabase_enterprise/transforms""enterprise/backend/test/metabase_enterprise/transforms_python"]}}-- ;; test all MBQL related stuff: MLv2, the legacy shared `metabase.legacy-mbql` code, and the QP+;; test all MBQL related stuff: MLv2, the legacy shared `metabase.legacy-mbql` code, and the QP;;;; clj -X:dev:ee:ee-dev:test:test/mbql:test/mbqldiff --git a/docs/developers-guide/driver-changelog.md b/docs/developers-guide/driver-changelog.mdindex c3b120ba21a..e21324c7c7f 100644--- a/docs/developers-guide/driver-changelog.md+++ b/docs/developers-guide/driver-changelog.md@@ -6,9 +6,17 @@ title: Driver interface changelog## Metabase 0.59.0+- Added `sql-jdbc.execute/db-type-name` multimethod. Override this method to customize how your SQL JDBC driver+ retrieves database type names from result set metadata. See the `:mysql` implementation for an example of remapping+ `TINYINT` to `BIT` based on precision.+- Added `metabase.driver/llm-sql-dialect-resource` multimethod. Returns the resource path for dialect-specific LLM prompt instructions, or nil if no dialect-specific instructions exist for this driver.-- Added `sql-jdbc.execute/db-type-name` multimethod. Override this if something more than the default is needed in your sql-jdbc-based driver. See the `:mysql` implementation as an example.+- Added workspace isolation multimethods for the enterprise workspaces feature:+ - `init-workspace-isolation!` - Create an isolated schema or database with user credentials for workspace usage.+ - `destroy-workspace-isolation!` - Destroy all database resources created for workspace isolation.+ - `grant-workspace-read-access!` - Grant read access on specified tables to a workspace's isolated user.+ - `check-isolation-permissions` - Test whether the database connection has sufficient permissions.## Metabase 0.58.0@@ -40,10 +48,17 @@ title: Driver interface changeloglonger include parent column names for drivers like MongoDB -- use `qp.add.nfc-path` instead to qualify the`qp.add.source-column-alias` with parent column names as needed.-- Added metabase.driver/compile-transform, metabase.driver/compile-drop-table, metabase.driver/execute-raw-queries!,- metabase.driver/run-transform!, metabase.driver/drop-transform-target!, metabase.driver/native-query-deps,- metabase.driver/connection-spec, metabase.driver/table-exists?, metabase.driver.sql/normalize-name,- and metabase.driver.sql/default-schema to implement sql transforms.+- Added the following driver methods to implement sql transforms.+ - metabase.driver/compile-drop-table+ - metabase.driver/compile-transform+ - metabase.driver/connection-spec+ - metabase.driver/drop-transform-target!+ - metabase.driver/execute-raw-queries!+ - metabase.driver/native-query-deps+ - metabase.driver/run-transform!+ - metabase.driver/table-exists?+ - metabase.driver.sql/default-schema+ - metabase.driver.sql/normalize-name- Added `metabase.driver/rename-tables!*` multimethod for atomic table renaming operations. Takes a map of {from-table to-table}pairs that has been topologically sorted.@@ -57,7 +72,15 @@ title: Driver interface changelog- Added `metabase.driver/type->database-type` multimethod that returns the database type for a given Metabasetype (from the type hierarchy) as a HoneySQL spec. This method handles general Metabase base types.-- Added driver multimethods driver/native-result-metadata, driver/validate-native-query-fields, driver.sql/resolve-field, driver.sql.normalize-unquoted-name, driver.sql.normalize/reserved-literal, driver.sql.references/find-used-fields, driver.sql.references/find-returned-fields, and driver.sql.references/field-references-impl for use with the :dependencies/native feature.+- Added the following driver multimethods for use with the :dependencies/native feature:+ - driver/native-result-metadata+ - driver/validate-native-query-fields+ - driver.sql.normalize-unquoted-name+ - driver.sql.normalize/reserved-literal+ - driver.sql.references/field-references-impl+ - driver.sql.references/find-returned-fields+ - driver.sql.references/find-used-fields+ - driver.sql/resolve-field- Added `metabase.driver/insert-from-source!` multimethod that abstracts data insertion from various sourcesinto existing tables. This multimethod dispatches on both the driver and the data source typediff --git a/docs/embedding/llms.txt b/docs/embedding/llms.txtnew file mode 100644index 00000000000..e7f13671d69--- /dev/null+++ b/docs/embedding/llms.txt@@ -0,0 +1,1264 @@+# Metabase Modular Embedding Reference++> Complete guide for embedding Metabase charts, dashboards, query builder, and AI chat into your applications.++## Overview++Metabase offers two primary ways to embed analytics:++1. **Modular Embedding JS** - Embed individual components (dashboards, questions, query builder, AI chat) with full customization+2. **Modular Embedding SDK** - React SDK for advanced embedding with custom layouts and plugins++### When to Use Modular Embedding++- **SSO Embeds**: Multi-tenant self-service analytics with drill-through, query builder, and AI chat. Requires JWT or SAML authentication.+- **Guest Embeds**: Simple, secure charts and dashboards without SSO. View-only with locked parameters for data filtering.++### Feature Comparison++| Feature | SSO | Guest | SDK |+|---------|-----|-------|-----|+| Charts & Dashboards | ✅ | ✅ | ✅ |+| Drill-through | ✅ | ❌ | ✅ |+| Query Builder | ✅ | ❌ | ✅ |+| AI Chat (Metabot) | ✅ | ❌ | ✅ |+| Collection Browser | ✅ | ❌ | ✅ |+| Advanced Theming | ✅ | ❌ | ✅ |+| Custom Layouts | ❌ | ❌ | ✅ |+| Plugins | ❌ | ❌ | ✅ |+| Data Permissions | ✅ | ❌ | ✅ |++---++## Quick Start Paths++### Path 1: Simple Web Components (No React Required)++Best for: Adding analytics to any web app with minimal code.++```html+<!-- Load embedding library -->+<script defer src="https://your-metabase-url/app/embed.js"></script>++<script>+ defineMetabaseConfig({+ instanceUrl: "https://your-metabase-url",+ apiKey: "mb_dev_api_key", // For local testing only+ theme: {+ colors: {+ brand: "#9B5966",+ background: "#FFFFFF"+ }+ }+ });+</script>++<!-- Embed a dashboard -->+<metabase-dashboard dashboard-id="1" with-title="true"></metabase-dashboard>++<!-- Embed a question -->+<metabase-question question-id="5"></metabase-question>+```++### Path 2: React SDK (Advanced)++Best for: React apps needing custom layouts, plugins, and full control.++```bash+# Install (use dist-tag matching your Metabase version)+npm install @metabase/embedding-sdk-react@58-stable+```++```tsx+import { MetabaseProvider, StaticDashboard } from "@metabase/embedding-sdk-react";++function App() {+ return (+ <MetabaseProvider+ authConfig={{+ metabaseInstanceUrl: "https://your-metabase-url",+ apiKey: "mb_dev_api_key" // For local testing only+ }}+ >+ <StaticDashboard dashboardId={1} />+ </MetabaseProvider>+ );+}+```++---++## Authentication++### Development: API Keys (localhost only)++```javascript+// Web components+defineMetabaseConfig({+ instanceUrl: "https://your-metabase-url",+ apiKey: "mb_your_api_key"+});++// React SDK+authConfig={{+ metabaseInstanceUrl: "https://your-metabase-url",+ apiKey: "mb_your_api_key"+}}+```++**⚠️ API keys only work on localhost and should never be used in production.**++### Production: JWT SSO (Requires Pro/Enterprise)++**⚠️ Important:** The SDK automatically uses the JWT endpoint configured in Metabase Admin settings. You don't need `fetchRequestToken` unless you require custom headers/auth.++#### Backend Setup (Node.js example)++```javascript+const jwt = require("jsonwebtoken");++app.get("/sso/metabase", async (req, res) => {+ const user = getCurrentUser(req);++ const token = jwt.sign(+ {+ email: user.email,+ first_name: user.firstName,+ last_name: user.lastName,+ groups: [user.group],+ exp: Math.round(Date.now() / 1000) + 60 * 10 // 10 min expiration+ },+ METABASE_JWT_SHARED_SECRET+ );++ // For SDK requests (check for response=json query param)+ if (req.query.response === "json") {+ res.status(200).json({ jwt: token });+ } else {+ // For full app embedding+ const ssoUrl = `${METABASE_INSTANCE_URL}/auth/sso?token=true&jwt=${token}`;+ res.redirect(ssoUrl);+ }+});+```++#### Frontend Configuration++```tsx+// React SDK+import { defineMetabaseAuthConfig } from "@metabase/embedding-sdk-react";++// ✅ CORRECT - Default setup (works for most cases)+const authConfig = defineMetabaseAuthConfig({+ metabaseInstanceUrl: "https://your-metabase-url"+ // SDK automatically uses JWT endpoint configured in Metabase admin+ // No fetchRequestToken needed!+});++// ❌ DEPRECATED - DO NOT USE+const authConfig = defineMetabaseAuthConfig({+ metabaseInstanceUrl: "https://your-metabase-url",+ authProviderUri: "..." // REMOVED - configure in Metabase admin instead+});+```++### Production: SAML SSO (Requires Enterprise)++```tsx+const authConfig = defineMetabaseAuthConfig({+ metabaseInstanceUrl: "https://your-metabase-url",+ preferredAuthMethod: "saml"+});+```++### Guest Embeds (No User Accounts Required)++Backend generates signed JWT tokens containing resource ID and locked parameters:++```javascript+const jwt = require("jsonwebtoken");++const payload = {+ resource: { dashboard: 10 }, // or { question: 5 }+ params: { category: ["Gizmo"] }, // Locked parameters+ exp: Math.round(Date.now() / 1000) + 10 * 60+};++const token = jwt.sign(payload, METABASE_SECRET_KEY);+```++Frontend:++```html+<script>+ defineMetabaseConfig({+ isGuest: true,+ instanceUrl: "https://your-metabase-url"+ });+</script>++<metabase-dashboard token="YOUR_JWT_TOKEN"></metabase-dashboard>+```++**⚠️ Never hardcode tokens in HTML. Fetch from your backend dynamically.**++## ⚠️ Common Gotchas for Code Generation++### 1. Deprecated Props (DO NOT USE)+- ❌ `authProviderUri` - DEPRECATED, remove from all code+- ❌ `jwtProviderUri` - DEPRECATED, remove from all code+- ❌ `config` prop on MetabaseProvider - DEPRECATED, use `authConfig` instead++### 2. fetchRequestToken Usually Not Needed+**DON'T add `fetchRequestToken` unless explicitly needed for custom headers/auth.**++The SDK automatically fetches tokens from the JWT endpoint configured in Metabase Admin settings. Adding `fetchRequestToken` unnecessarily can break backend code that handles legacy interactive embedding redirects, especially if you forget to pass `response=json` in the request.++**Only use `fetchRequestToken` when:**+- You need custom headers (e.g., `Authorization: Bearer ${token}`)+- You need custom fetch logic beyond the default++**Default behavior (works for most cases):**+```tsx+const authConfig = defineMetabaseAuthConfig({+ metabaseInstanceUrl: "https://your-metabase-url"+ // No fetchRequestToken needed - SDK uses admin-configured JWT endpoint+});+```++**Custom fetchRequestToken (only when needed):**+```tsx+const authConfig = defineMetabaseAuthConfig({+ metabaseInstanceUrl: "https://your-metabase-url",+ fetchRequestToken: async () => {+ const response = await fetch("https://your-backend/sso/metabase", {+ headers: { "Authorization": `Bearer ${yourToken}` }+ // SDK adds ?response=json automatically+ });+ return await response.json();+ }+});+```++### 3. Numeric IDs Must Be Integers, Not Strings+**ALWAYS parse URL parameters and strings to integers using `parseInt()`.**++```tsx+// ❌ WRONG - String IDs will cause errors+const dashboardId = searchParams.get("id"); // "123" (string)+<StaticDashboard dashboardId={dashboardId} /> // ERROR!++// ✅ CORRECT - Parse to integer+const dashboardId = parseInt(searchParams.get("id"), 10); // 123 (number)+<StaticDashboard dashboardId={dashboardId} /> // Works!++// ✅ Also works with entity IDs (strings starting with letters)+const dashboardId = searchParams.get("id"); // "abc123def"+<StaticDashboard dashboardId={dashboardId} /> // Works!+```++**Rule of thumb:**+- Numeric IDs (e.g., `123`): Must be `number` type → use `parseInt(value, 10)`+- Entity IDs (e.g., `"abc123def"`): Must be `string` type → use as-is++---++## React SDK Components++### Dashboards++#### StaticDashboard+Lightweight, view-only dashboard.++```tsx+import { StaticDashboard } from "@metabase/embedding-sdk-react";++<StaticDashboard+ dashboardId={123}+ withTitle={true}+ withDownloads={false}+ initialParameters={{ productId: 42 }}+ hiddenParameters={["category"]}+/>+```++#### InteractiveDashboard+Dashboard with drill-downs, click behaviors, and question navigation.++```tsx+import { InteractiveDashboard } from "@metabase/embedding-sdk-react";++<InteractiveDashboard+ dashboardId={123}+ withTitle={true}+ withDownloads={true}+ plugins={{+ dashboardCardMenu: {+ withDownloads: true,+ withEditLink: false,+ customItems: [+ {+ label: "Custom Action",+ icon: "info",+ action: (question) => console.log(question)+ }+ ]+ }+ }}+/>+```++#### EditableDashboard+Fully editable dashboard with add/edit/layout capabilities.++```tsx+import { EditableDashboard } from "@metabase/embedding-sdk-react";++<EditableDashboard+ dashboardId={123}+ onSave={(dashboard) => console.log("Saved:", dashboard)}+/>+```++### Questions++#### StaticQuestion+Lightweight, view-only question/chart.++```tsx+import { StaticQuestion } from "@metabase/embedding-sdk-react";++<StaticQuestion+ questionId={456}+ height="400px"+ initialSqlParameters={{ productId: 42 }} // For SQL questions only+/>+```++#### InteractiveQuestion+Question with filters, summarize, drill-through, and query builder.++```tsx+import { InteractiveQuestion } from "@metabase/embedding-sdk-react";++// Default layout+<InteractiveQuestion questionId={456} />++// Custom layout with namespaced components+<InteractiveQuestion questionId={456}>+ <InteractiveQuestion.Title />+ <InteractiveQuestion.Filter />+ <InteractiveQuestion.Summarize />+ <InteractiveQuestion.QuestionVisualization />+ <InteractiveQuestion.SaveButton />+</InteractiveQuestion>+```++#### Query Builder (New Question)++```tsx+<InteractiveQuestion questionId="new">+ <InteractiveQuestion.Editor />+ <InteractiveQuestion.QuestionVisualization />+ <InteractiveQuestion.SaveButton />+</InteractiveQuestion>+```++### AI Chat (Metabot)++Embed AI-powered natural language querying.++```tsx+import { MetabotQuestion } from "@metabase/embedding-sdk-react";++<MetabotQuestion+ collectionId={7} // Scope AI to specific collection+ layout="sidebar" // "auto" | "stacked" | "sidebar"+/>+```++### Collection Browser++Let users navigate and explore Metabase collections.++```tsx+import { CollectionBrowser } from "@metabase/embedding-sdk-react";++<CollectionBrowser+ initialCollectionId="root"+ readOnly={false}+ visibleEntityTypes={["dashboard", "question"]}+/>+```++### Create Dashboard Modal++```tsx+import { CreateDashboardModal } from "@metabase/embedding-sdk-react";++<CreateDashboardModal+ collectionId={7}+ onClose={() => setIsOpen(false)}+ onCreate={(dashboard) => console.log("Created:", dashboard)}+/>+```++---++## Web Component Reference++### Dashboard Component++```html+<metabase-dashboard+ dashboard-id="1" <!-- Required: ID or entity ID -->+ with-title="true" <!-- Show title (default: true) -->+ with-downloads="false" <!-- Enable downloads (default: false) -->+ drills="true" <!-- Enable drill-through (default: true) -->+ with-subscriptions="false" <!-- Enable subscriptions -->+ refresh="60" <!-- Auto-refresh in seconds -->+ initial-parameters='{"productId": "42"}'+ hidden-parameters='["category"]'+></metabase-dashboard>+```++### Question Component++```html+<metabase-question+ question-id="5" <!-- Required: ID or entity ID, or "new" -->+ with-title="true" <!-- Show title (default: true) -->+ with-downloads="false" <!-- Enable downloads (default: false) -->+ drills="true" <!-- Enable drill-through (default: true) -->+ initial-sql-parameters='{"productId": "42"}' <!-- SQL questions only -->+ is-save-enabled="false" <!-- Allow saving -->+ target-collection="personal" <!-- Force save location -->+></metabase-question>+```++### Browser Component++```html+<metabase-browser+ initial-collection="root" <!-- Required: collection ID or "root" -->+ read-only="false" <!-- Allow create/edit (default: true) -->+></metabase-browser>+```++### AI Chat Component++```html+<metabase-metabot+ layout="auto" <!-- "auto" | "stacked" | "sidebar" -->+></metabase-metabot>+```++---++## Theming and Appearance++### Complete Theme Object++```javascript+{+ fontFamily: "Lato", // or "Custom" for Metabase custom font+ fontSize: "16px",+ lineHeight: 1.5,++ colors: {+ // Primary colors+ brand: "#9B5966",+ "brand-hover": "#DDECFA",+ "brand-hover-light": "#EEF6FC",++ // Text colors+ "text-primary": "#4C5773",+ "text-secondary": "#696E7B",+ "text-tertiary": "#949AAB",++ // Background colors+ background: "#FFFFFF",+ "background-light": "#F0F2F5",+ "background-secondary": "#EDF2F5",+ "background-hover": "#F9FBFC",+ "background-disabled": "#F3F5F7",++ // UI elements+ border: "#EEECEC",+ filter: "#7172AD",+ summarize: "#88BF4D",+ positive: "#BADC58",+ negative: "#FF7979",+ focus: "#CAE1F7",+ shadow: "rgba(0,0,0,0.08)",++ // Chart colors (up to 8)+ charts: [+ "#9B59B6",+ { base: "#E74C3C", tint: "#EE6B56", shade: "#CB4436" }+ ]+ },++ components: {+ dashboard: {+ backgroundColor: "#2F3640",+ gridBorderColor: "#EEECEC",+ card: {+ backgroundColor: "#2D2D30",+ border: "1px solid #EEECEC"+ }+ },++ question: {+ backgroundColor: "#2E353B",+ toolbar: {+ backgroundColor: "#F3F5F7"+ }+ },++ tooltip: {+ textColor: "#FFFFFF",+ secondaryTextColor: "#949AAB",+ backgroundColor: "#2E353B",+ focusedBackgroundColor: "#0A0E10"+ },++ table: {+ cell: {+ textColor: "#4C5773",+ backgroundColor: "#FFFFFF",+ fontSize: "12.5px"+ },+ idColumn: {+ textColor: "#9B5966",+ backgroundColor: "#F5E9EB"+ }+ },++ number: {+ value: {+ fontSize: "24px",+ lineHeight: "21px"+ }+ },++ cartesian: {+ padding: "4px 8px"+ },++ pivotTable: {+ cell: { fontSize: "12px" },+ rowToggle: {+ textColor: "#FFFFFF",+ backgroundColor: "#95A5A6"+ }+ },++ popover: {+ zIndex: 4+ }+ }+}+```++### Applying Themes++#### Web Components++```html+<script>+ defineMetabaseConfig({+ instanceUrl: "https://your-metabase-url",+ theme: {+ colors: {+ brand: "#9B5966",+ background: "#FFFFFF"+ }+ }+ });+</script>+```++#### React SDK++```tsx+import { defineMetabaseTheme } from "@metabase/embedding-sdk-react";++const theme = defineMetabaseTheme({+ colors: {+ brand: "#9B5966",+ background: "#FFFFFF"+ }+});++<MetabaseProvider theme={theme} authConfig={authConfig}>+ {/* Your components */}+</MetabaseProvider>+```++---++## Plugins (React SDK Only)++### Global Plugins++Applied to all components via `MetabaseProvider`:++```tsx+<MetabaseProvider+ authConfig={authConfig}+ pluginsConfig={{+ mapQuestionClickActions: (clickActions, dataPoint) => {+ // Return custom actions or default actions+ return [+ ...clickActions,+ {+ name: "custom-action",+ title: "Custom Action",+ icon: "info",+ action: () => console.log(dataPoint)+ }+ ];+ },++ handleLink: (url) => {+ console.log("Link clicked:", url);+ return false; // Prevent default+ },++ getNoDataIllustration: () => "data:image/svg+xml;base64,...",+ getNoObjectIllustration: () => "data:image/svg+xml;base64,..."+ }}+>+ {/* Your components */}+</MetabaseProvider>+```++### Component-Level Plugins++Applied to individual components:++```tsx+<InteractiveQuestion+ questionId={456}+ plugins={{+ mapQuestionClickActions: (clickActions, dataPoint) => {+ // Custom behavior for this question only+ if (dataPoint.column.name === "Plan") {+ return [{+ name: "alert",+ title: "Show Alert",+ action: () => alert(dataPoint.value)+ }];+ }+ return clickActions; // Default for other columns+ }+ }}+/>+```++### Dashboard Card Menu Plugin++```tsx+<InteractiveDashboard+ dashboardId={123}+ plugins={{+ dashboardCardMenu: {+ withDownloads: true,+ withEditLink: false,+ customItems: [+ {+ label: "Export to CSV",+ icon: "download",+ action: (question) => {+ // Custom export logic+ console.log("Exporting:", question);+ }+ }+ ]+ }+ }}+/>+```++Or replace the entire menu:++```tsx+<InteractiveDashboard+ dashboardId={123}+ plugins={{+ dashboardCardMenu: (question) => (+ <CustomMenu question={question} />+ )+ }}+/>+```++---++## Setup Checklist++### Metabase Configuration++1. **Enable Modular Embedding**+ - Go to **Admin Settings > Embedding**+ - Toggle **Enable modular embedding** (for web components) or **Modular embedding SDK** (for React)++2. **Configure CORS**+ - Add allowed origins: `https://*.example.com`+ - `localhost` is automatically included for development++3. **SameSite Cookie Setting** (if embedding on different domain)+ - Go to **Admin Settings > Embedding > Security**+ - Set **SameSite cookie** to **None** (requires HTTPS)++4. **Authentication Setup**+ - **Development**: Create API key at **Settings > Authentication > API keys**+ - **Production (JWT)**: Configure at **Settings > Authentication > JWT**+ - **Production (SAML)**: Configure at **Settings > Authentication > SAML**++### Application Setup++#### Web Components++```html+<!DOCTYPE html>+<html>+<head>+ <title>Embedded Analytics</title>+</head>+<body>+ <!-- 1. Load embedding library -->+ <script defer src="https://your-metabase-url/app/embed.js"></script>++ <!-- 2. Configure -->+ <script>+ function defineMetabaseConfig(config) {+ window.metabaseConfig = config;+ }++ defineMetabaseConfig({+ instanceUrl: "https://your-metabase-url",+ apiKey: "mb_dev_api_key" // For local testing+ });+ </script>++ <!-- 3. Embed components -->+ <metabase-dashboard dashboard-id="1"></metabase-dashboard>+</body>+</html>+```++#### React SDK++```bash+# 1. Install+npm install @metabase/embedding-sdk-react@58-stable+```++```tsx+// 2. Configure and embed+import { MetabaseProvider, StaticDashboard } from "@metabase/embedding-sdk-react";++function App() {+ return (+ <MetabaseProvider+ authConfig={{ // ✅ authConfig NOT config+ metabaseInstanceUrl: "https://your-metabase-url",+ apiKey: "mb_dev_api_key"+ }}+ >+ <StaticDashboard dashboardId={1} /> {/* ✅ number type */}+ </MetabaseProvider>+ );+}+```++---++## Common Patterns++### Multi-Tenant Analytics with Data Segregation++```javascript+// Backend: Sign JWT with user's group+const token = jwt.sign(+ {+ email: user.email,+ groups: [user.tenantId], // e.g., "tenant_123"+ exp: Math.round(Date.now() / 1000) + 60 * 10+ },+ METABASE_JWT_SHARED_SECRET+);+```++In Metabase, configure [data sandboxing](https://www.metabase.com/docs/latest/permissions/data-sandboxes) to filter data based on `groups`.++### Custom JWT Fetch (Only When You Need Custom Headers)++**⚠️ Only use this pattern if you need custom headers or auth. Most apps don't need this.**++```tsx+import { defineMetabaseAuthConfig } from "@metabase/embedding-sdk-react";++// Only add fetchRequestToken if you need custom headers/auth+const authConfig = defineMetabaseAuthConfig({+ metabaseInstanceUrl: "https://your-metabase-url",+ fetchRequestToken: async () => {+ const response = await fetch("https://your-backend/sso/metabase", {+ method: "GET",+ credentials: "include", // For cookies+ headers: {+ "Authorization": `Bearer ${yourAuthToken}` // Custom header+ }+ // SDK automatically adds ?response=json query param+ });+ return await response.json(); // { jwt: "..." }+ }+});+```++### Dynamic Parameters++```tsx+const [productId, setProductId] = useState(42);++<InteractiveDashboard+ dashboardId={123} // ✅ number type+ initialParameters={{ productId }} // ✅ values can be any type+/>+```++### Parsing IDs from URL Parameters++```tsx+import { useSearchParams } from "react-router-dom";++function DashboardPage() {+ const [searchParams] = useSearchParams();++ // ✅ CORRECT - Parse numeric IDs to integers+ const dashboardId = parseInt(searchParams.get("id"), 10);++ // ❌ WRONG - Don't pass string IDs for numeric dashboards+ // const dashboardId = searchParams.get("id"); // "123" won't work!++ return <InteractiveDashboard dashboardId={dashboardId} />;+}+```++### Guest Embed with Locked Parameters++```javascript+// Backend: Lock parameters to prevent tampering+const payload = {+ resource: { dashboard: 10 },+ params: {+ // User can't change these+ tenant_id: user.tenantId,+ // User can change these+ date_range: null+ },+ exp: Math.round(Date.now() / 1000) + 10 * 60+};+```++### Checking Authentication Status++```tsx+import { useMetabaseAuthStatus } from "@metabase/embedding-sdk-react";++function MyComponent() {+ const { status, user } = useMetabaseAuthStatus();++ if (status === "loading") return <Spinner />;+ if (status === "error") return <Error />;+ if (status === "success") return <Dashboard user={user} />;+}+```++### Custom Loading and Error Components++```tsx+const CustomLoader = () => <div>Loading analytics...</div>;+const CustomError = ({ error }) => <div>Error: {error.message}</div>;++<MetabaseProvider+ authConfig={authConfig}+ loaderComponent={CustomLoader}+ errorComponent={CustomError}+>+ {/* Your components */}+</MetabaseProvider>+```++---++## SDK Hooks++### useMetabaseAuthStatus+Get current authentication status.++```tsx+const { status, user } = useMetabaseAuthStatus();+// status: "loading" | "success" | "error"+// user: { id, email, first_name, last_name, groups }+```++### useCurrentUser+Get current authenticated user.++```tsx+const user = useCurrentUser();+// Returns: { id, email, first_name, last_name, groups }+```++### useCreateDashboardApi+Programmatically create dashboards.++```tsx+const createDashboard = useCreateDashboardApi();++const handleCreate = async () => {+ const dashboard = await createDashboard({+ name: "New Dashboard",+ description: "Description",+ collection_id: 7+ });+ console.log("Created:", dashboard);+};+```++### useApplicationName+Get Metabase instance name.++```tsx+const appName = useApplicationName();+```++### useAvailableFonts+Get available fonts in Metabase.++```tsx+const fonts = useAvailableFonts();+```++---++## Versioning++**Critical**: Use npm dist-tag matching your Metabase version.++| Metabase Version | npm Install Command |+|------------------|---------------------|+| 1.58.x | `npm install @metabase/embedding-sdk-react@58-stable` |+| 1.57.x | `npm install @metabase/embedding-sdk-react@57-stable` |+| 1.56.x | `npm install @metabase/embedding-sdk-react@56-stable` |+| 1.55.x | `npm install @metabase/embedding-sdk-react@55-stable` |+| 1.54.x | `npm install @metabase/embedding-sdk-react@54-stable` |++Starting with Metabase 57, the SDK uses a two-part architecture:+- **SDK Package**: Lightweight npm package (`@metabase/embedding-sdk-react`)+- **SDK Bundle**: Full SDK code served from your Metabase instance++This ensures version compatibility between SDK and Metabase.++---++## Limitations++### SDK Limitations+- No server-side rendering (SSR)+- No verified content or official collections+- No subscriptions or alerts in SDK components+- No custom click behavior destinations to other Metabase items+- Only one interactive dashboard per page (multiple static dashboards OK)+- Leaflet 1.x compatibility issues (use Leaflet 2.x)++### Guest Embed Limitations+- No drill-through or click actions+- No query builder+- No AI chat+- No row/column-level security (use locked parameters instead)+- No usage analytics++---++## Security Best Practices++1. **Never share Metabase accounts**+ - Each end-user must have their own account (for SSO embeds)+ - Session tokens can be used to access Metabase API directly++2. **Never hardcode secrets**+ - API keys and JWT secrets belong on backend only+ - Fetch tokens dynamically from your server++3. **Use data sandboxing**+ - Configure [row-level permissions](https://www.metabase.com/docs/latest/permissions/data-sandboxes) in Metabase+ - Map user groups to data filters++4. **Validate JWT payloads**+ - Always validate exp (expiration)+ - Keep expiration short (10-15 minutes)++5. **Secure CORS**+ - Only allow specific origins+ - Never use wildcard (`*`) in production++6. **HTTPS required**+ - SameSite=None cookies require HTTPS+ - Use HTTPS for all production embeds++---++## Troubleshooting++### "Failed to fetch" errors+- Check CORS configuration in Metabase+- Verify `instanceUrl` matches your Metabase domain exactly+- Check browser console for specific CORS errors++### Authentication failures+- Verify JWT signature matches secret key+- Check JWT expiration hasn't passed+- Ensure user exists in Metabase with correct email+- Check groups match Metabase group names++### Components not rendering+- Ensure `MetabaseProvider` wraps all SDK components+- Check IDs are correct (numeric IDs must be `number` type, not strings)+- Parse URL parameters with `parseInt(value, 10)` for numeric IDs+- Verify user has permissions to view the item+- Check browser console for errors++### TypeScript errors about ID types+```tsx+// ❌ WRONG+const id = "123"; // string+<StaticDashboard dashboardId={id} /> // Type error!++// ✅ CORRECT+const id = 123; // number+<StaticDashboard dashboardId={id} />++// ✅ CORRECT - Parse from URL+const id = parseInt(urlParams.get("id"), 10);+<StaticDashboard dashboardId={id} />+```++### Theme not applying+- Colors must be valid CSS color values+- Component-level styles override global theme+- Visualization settings in Metabase override theme++### Cross-domain issues+- Set SameSite=None in Metabase settings+- Use HTTPS (required for SameSite=None)+- Safari may require "Allow cross-site tracking"++---++## Resources++- **Live Demo**: [Shoppy Demo App](https://metaba.se/sdk-demo)+- **Sample Code**: [Shoppy Source](https://github.com/metabase/shoppy)+- **Sample App**: [Embedding Sample](https://github.com/metabase/modular-embedding-sample-app)+- **SDK on npm**: [metaba.se/sdk-npm](https://metaba.se/sdk-npm)+- **Documentation**: [Metabase Embedding Docs](https://www.metabase.com/docs/latest/embedding/introduction)+- **Bug Reports**: [GitHub Issues](https://github.com/metabase/metabase/issues)+- **Community**: [Discourse Forums](https://discourse.metabase.com/)++---++## Architecture Notes++### Web Components Architecture+- Metabase serves `embed.js` from your instance+- Custom elements: `<metabase-dashboard>`, `<metabase-question>`, etc.+- Framework-agnostic (works with any web stack)++### React SDK Architecture (v57+)+- **SDK Package** (`@metabase/embedding-sdk-react`): Lightweight bootstrapper+- **SDK Bundle**: Full code served from Metabase instance+- Ensures SDK always matches Metabase version++### Authentication Flow (JWT)+1. User accesses your app+2. Your app sends request to your backend+3. Backend generates signed JWT with user info+4. Backend returns `{ jwt: "..." }` to frontend+5. SDK sends JWT to Metabase+6. Metabase validates signature and creates session+7. Session cookie enables subsequent API calls++### Entity IDs vs Numeric IDs+- **Numeric IDs**: `dashboardId={123}` - Changes when importing/exporting+- **Entity IDs**: `dashboardId="abc123def"` - Stable across environments+- Use Entity IDs for production embeds that sync across instances++---++## Quick Reference++### ⚠️ Deprecated Fields - DO NOT USE+```+authProviderUri - REMOVED: Configure JWT endpoint in Metabase admin instead+jwtProviderUri - REMOVED: Configure JWT endpoint in Metabase admin instead+config (MetabaseProvider) - REMOVED: Use authConfig prop instead+```++### Essential Web Component Attributes+```+dashboard-id, question-id - Required: resource identifier+with-title - Boolean: show/hide title+with-downloads - Boolean: enable/disable downloads+drills - Boolean: enable/disable drill-through+initial-parameters - JSON: default filter values+hidden-parameters - Array: filters to hide+token - String: JWT for guest embeds+refresh - Number: auto-refresh interval (seconds)+```++### Essential SDK Props+```+dashboardId, questionId - Required: number (not string!) or entity ID string+ ⚠️ Parse URL params: parseInt(value, 10)+withTitle - Boolean: show/hide title+withDownloads - Boolean: enable/disable downloads+initialParameters - Object: default filter values+hiddenParameters - Array: filters to hide+isSaveEnabled - Boolean: allow saving+targetCollection - String/Number: force save location+plugins - Object: plugin configuration+```++### Essential MetabaseProvider Props+```+authConfig - Required: authentication config (NOT "config")+theme - Object: theme configuration+pluginsConfig - Object: global plugins+loaderComponent - Component: custom loader+errorComponent - Component: custom error display+```++**⚠️ DEPRECATED Props (DO NOT USE):**+```+config - DEPRECATED: Use authConfig instead+```++---++## Entity ID Usage Pattern++```tsx+// Get entity ID from Metabase+// Admin > Tools > Model Caching > Click info icon on any item++// Use in embeds (stable across environments)+<StaticDashboard dashboardId="abc123def456" />+<StaticQuestion questionId="xyz789ghi012" />+```++---++## Example: Complete Production Setup++**⚠️ Note:** This example shows `fetchRequestToken` for demonstration, but most apps using cookie-based auth don't need it. Only add `fetchRequestToken` if you need custom headers.++### Backend (Express.js)++```javascript+const express = require("express");+const jwt = require("jsonwebtoken");+const cors = require("cors");++const app = express();++// CORS for SDK requests+app.use(cors({+ origin: "https://your-app.com",+ credentials: true+}));++// JWT endpoint+app.get("/sso/metabase", async (req, res) => {+ const user = await getCurrentUser(req);++ const token = jwt.sign(+ {+ email: user.email,+ first_name: user.firstName,+ last_name: user.lastName,+ groups: [`tenant_${user.tenantId}`],+ exp: Math.round(Date.now() / 1000) + 60 * 10+ },+ process.env.METABASE_JWT_SECRET+ );++ if (req.query.response === "json") {+ res.json({ jwt: token });+ } else {+ res.redirect(`${process.env.METABASE_URL}/auth/sso?token=true&jwt=${token}`);+ }+});++app.listen(3001);+```++### Frontend (React)++```tsx+import {+ MetabaseProvider,+ InteractiveDashboard,+ defineMetabaseAuthConfig+} from "@metabase/embedding-sdk-react";++// ✅ CORRECT - Default (no fetchRequestToken needed for cookie-based auth)+const authConfig = defineMetabaseAuthConfig({+ metabaseInstanceUrl: "https://metabase.company.com"+});++// Or if you need custom headers:+// const authConfig = defineMetabaseAuthConfig({+// metabaseInstanceUrl: "https://metabase.company.com",+// fetchRequestToken: async () => {+// const response = await fetch("https://api.company.com/sso/metabase", {+// credentials: "include",+// headers: { "Authorization": `Bearer ${token}` }+// });+// return await response.json();+// }+// });++function Analytics() {+ return (+ <MetabaseProvider authConfig={authConfig}> {/* ✅ authConfig NOT config */}+ <InteractiveDashboard dashboardId="abc123def" /> {/* ✅ Entity ID (string) */}+ </MetabaseProvider>+ );+}+```++### Metabase Configuration++1. **JWT Setup** (Admin > Authentication > JWT)+ - JWT Identity Provider URI: `https://api.company.com/sso/metabase`+ - String used by Metabase to encode: `[Copy shared secret from backend]`++2. **Embedding** (Admin > Embedding)+ - Enable modular embedding SDK: ✓+ - Authorized origins: `https://your-app.com`++3. **SameSite Cookie** (Admin > Embedding > Security)+ - SameSite cookie setting: None++4. **Data Permissions** (Admin > Permissions > Data)+ - Create groups: `tenant_123`, `tenant_456`, etc.+ - Configure data sandboxes per group++---++*Last Updated: December 2025*+*Metabase Version: 1.58+*\ No newline at end of filediff --git a/docs/llms.txt b/docs/llms.txtnew file mode 100644index 00000000000..3de079fde03--- /dev/null+++ b/docs/llms.txt@@ -0,0 +1,6 @@+# Metabase++Links to `llms.txt` files for Metabase. These files contains the full documentation and guide for AI agents on these Metabase-related topics:++- [Embedding Metabase with Modular Embedding](./embedding/llms.txt)+diff --git a/e2e/support/helpers/api/index.ts b/e2e/support/helpers/api/index.tsindex 63d6ed09561..5796ca82e2c 100644--- a/e2e/support/helpers/api/index.ts+++ b/e2e/support/helpers/api/index.ts@@ -42,4 +42,8 @@ export { remapDisplayValueToFK } from "./remapDisplayValueToFK";export { unpublishTables } from "./unpublishTables";export { updateComment } from "./updateComment";export { updateDashboardCards } from "./updateDashboardCards";-export { updateSetting, updateEnterpriseSetting } from "./updateSetting";+export {+ updateSetting,+ updateEnterpriseSetting,+ updateEnterpriseSettings,+} from "./updateSetting";diff --git a/e2e/support/helpers/api/updateSetting.ts b/e2e/support/helpers/api/updateSetting.tsindex d7b8bb23851..706418e7156 100644--- a/e2e/support/helpers/api/updateSetting.ts+++ b/e2e/support/helpers/api/updateSetting.ts@@ -19,3 +19,12 @@ export const updateEnterpriseSetting = <): Cypress.Chainable<Cypress.Response<never>> => {return cy.request<never>("PUT", `/api/setting/${setting}`, { value });};++export const updateEnterpriseSettings = <+ TKey extends keyof EnterpriseSettings,+ TValue extends EnterpriseSettings[TKey],+>(+ settings: Record<TKey, TValue>,+): Cypress.Chainable<Cypress.Response<never>> => {+ return cy.request<never>("PUT", "/api/setting", settings);+};diff --git a/e2e/support/helpers/e2e-codemirror-helpers.ts b/e2e/support/helpers/e2e-codemirror-helpers.tsindex 6da51822fcc..91f0342b5bd 100644--- a/e2e/support/helpers/e2e-codemirror-helpers.ts+++ b/e2e/support/helpers/e2e-codemirror-helpers.ts@@ -11,7 +11,17 @@ export function codeMirrorHelpers<T extends object>(testId: string, extra: T) {return cy.get(`[data-testid=${testId}] .cm-content`);},focus() {- helpers.get().should("be.visible").click("right");+ helpers.get().should("be.visible").click("right", {+ /**+ * We want to click on the right, because we want the caret to be positioned at the end of the editor.+ * Transform editor in workspaces (data studio) has a resize handle at the right of the editor,+ * which causes this click("right") to fail.+ * Alternative approach with pressing End key for moving the caret to the end made+ * repro for (metabase#49882-2, metabase#15892) fail in CI every time.+ * Hence force: true.+ */+ force: true,+ });helpers.get().get(".cm-editor").should("have.class", "cm-focused");return helpers;},@@ -172,6 +182,13 @@ export function codeMirrorHelpers<T extends object>(testId: string, extra: T) {cy.wait(300);helpers.type(`{${key}}`, { focus: false });},+ rejectCompletion() {+ helpers.completions().should("be.visible");++ // Avoid flakiness with CodeMirror not processing the escape immediately+ cy.wait(300);+ cy.realPress(["Escape"]);+ },selectCompletion(name: string) {helpers.completions().should("be.visible");diff --git a/e2e/support/helpers/e2e-data-studio-helpers.ts b/e2e/support/helpers/e2e-data-studio-helpers.tsindex beff92bba3a..d846117ed39 100644--- a/e2e/support/helpers/e2e-data-studio-helpers.ts+++ b/e2e/support/helpers/e2e-data-studio-helpers.ts@@ -17,9 +17,33 @@ export const DataStudio = {header: () => cy.findByTestId("transforms-header"),list: () => cy.findByTestId("transforms-list"),saveChangesButton: () => DataStudio.Transforms.queryEditor().button("Save"),- editDefinition: () => cy.findByRole("link", { name: "Edit definition" }),+ editTransform: () => cy.findByRole("button", { name: "Edit" }),+ editDefinitionButton: () =>+ cy.get(+ '[data-testid="edit-definition-button"], [data-testid="transform-edit-menu-button"]',+ ),+ getEditDefinitionLink: () => {+ // When workspaces are available, "Edit definition" is inside the "Edit" menu+ // When workspaces are not available, "Edit definition" is a direct link+ return DataStudio.Transforms.editDefinitionButton()+ .first()+ .then(($el) => {+ if ($el.attr("data-testid") === "edit-definition-button") {+ return cy.wrap($el);+ } else {+ cy.wrap($el).click();+ return popover().findByRole("menuitem", {+ name: /Edit definition/,+ });+ }+ });+ },+ clickEditDefinition: () => {+ DataStudio.Transforms.getEditDefinitionLink().click();+ },queryEditor: () => cy.findByTestId("transform-query-editor"),runTab: () => DataStudio.Transforms.header().findByText("Run"),+ targetTab: () => DataStudio.Transforms.header().findByText("Target"),settingsTab: () => DataStudio.Transforms.header().findByText("Settings"),dependenciesTab: () =>DataStudio.Transforms.header().findByText("Dependencies"),diff --git a/e2e/support/helpers/e2e-python-helpers.ts b/e2e/support/helpers/e2e-python-helpers.tsindex 6b073adb074..bbc01daa1b9 100644--- a/e2e/support/helpers/e2e-python-helpers.ts+++ b/e2e/support/helpers/e2e-python-helpers.ts@@ -1,3 +1,19 @@+import { updateEnterpriseSettings } from "./api/updateSetting";import { codeMirrorHelpers } from "./e2e-codemirror-helpers";export const PythonEditor = codeMirrorHelpers("python-editor", {});++export function setPythonRunnerSettings() {+ updateEnterpriseSettings({+ "python-runner-url": "http://localhost:5001",+ "python-runner-api-token": "dev-token-12345",+ "python-storage-s-3-endpoint": "http://localhost:4566",+ "python-storage-s-3-region": "us-east-1",+ "python-storage-s-3-bucket": "metabase-python-runner",+ "python-storage-s-3-prefix": "test-prefix",+ "python-storage-s-3-access-key": "test",+ "python-storage-s-3-secret-key": "test",+ "python-storage-s-3-container-endpoint": "http://localstack:4566",+ "python-storage-s-3-path-style-access": true,+ });+}diff --git a/e2e/support/helpers/e2e-qa-databases-helpers.js b/e2e/support/helpers/e2e-qa-databases-helpers.jsindex d5adb98ca40..26b3553d9fa 100644--- a/e2e/support/helpers/e2e-qa-databases-helpers.js+++ b/e2e/support/helpers/e2e-qa-databases-helpers.js@@ -287,11 +287,15 @@ export function createTestRoles({ type, isWritable }) {* @param {Object} obj* @param {number} [obj.databaseId] - Defaults to WRITABLE_DB_ID* @param {string} obj.name - The table's real name, not its display name+ * @param {string} [obj.schema] - The table's schema name*/-export function getTableId({ databaseId = WRITABLE_DB_ID, name }) {+export function getTableId({ databaseId = WRITABLE_DB_ID, name, schema }) {return cy.request("GET", "/api/table").then(({ body: tables }) => {const table = tables.find(- (table) => table.db_id === databaseId && table.name === name,+ (table) =>+ table.db_id === databaseId &&+ table.name === name &&+ (schema ? table.schema === schema : true),);if (!table) {throw new TypeError(`Table with name ${name} cannot be found`);diff --git a/e2e/support/helpers/e2e-ui-elements-helpers.js b/e2e/support/helpers/e2e-ui-elements-helpers.jsindex 8ae3ad2e756..ffccb2fc0f8 100644--- a/e2e/support/helpers/e2e-ui-elements-helpers.js+++ b/e2e/support/helpers/e2e-ui-elements-helpers.js@@ -84,6 +84,33 @@ export function entityPickerModalItem(level, name) {);}+export function entityPickerModalTab(name) {+ if (typeof name === "string") {+ return cy.findAllByRole("tab").filter(`:contains(${name})`);+ } else {+ return cy.findAllByRole("tab").first().parent().findByText(name);+ }+}++// displays at least these tabs:+export function shouldDisplayTabs(tabs) {+ tabs.forEach((tab) => {+ entityPickerModalTab(tab).should("exist");+ });+}++export function tabsShouldBe(selected, tabs) {+ cy.log(tabs);+ cy.findAllByRole("tab").should("have.length", tabs.length);+ tabs.forEach((tab) => {+ if (tab === selected) {+ entityPickerModalTab(tab).and("have.attr", "aria-selected", "true");+ } else {+ entityPickerModalTab(tab).should("exist");+ }+ });+}+export function collectionOnTheGoModal() {return cy.findByTestId("create-collection-on-the-go");}diff --git a/e2e/support/helpers/e2e-workspaces-helpers.ts b/e2e/support/helpers/e2e-workspaces-helpers.tsnew file mode 100644index 00000000000..722243252dc--- /dev/null+++ b/e2e/support/helpers/e2e-workspaces-helpers.ts@@ -0,0 +1,149 @@+import type { WorkspaceId } from "metabase-types/api";++export const Workspaces = {+ visit: (id: WorkspaceId) => {+ return cy.visit(`/data-studio/workspace/${id}`);+ },++ visitDataStudio() {+ return cy.visit("/data-studio");+ },++ visitWorkspaces() {+ return cy.visit("/data-studio/workspaces");+ },++ visitTransformListPage() {+ return cy.visit("/data-studio/transforms");+ },++ getWorkspacesPage() {+ return cy.findByTestId("workspaces-page");+ },++ getWorkspacePage() {+ return cy.findByTestId("workspace-page");+ },++ getWorkspaceContent() {+ return cy.findByTestId("workspace-content");+ },++ getWorkspaceTabs() {+ return cy.findByTestId("workspace-tabs");+ },++ getWorkspaceSidebar() {+ return cy.findByTestId("workspace-sidebar");+ },++ openSetupTab() {+ this.getWorkspaceContent().findByRole("tab", { name: "Setup" }).click();+ },++ openCodeTab() {+ this.getWorkspaceSidebar().findByRole("tab", { name: "Code" }).click();+ },++ openDataTab() {+ this.getWorkspaceSidebar().findByRole("tab", { name: "Data" }).click();+ },++ openGraphTab() {+ this.getWorkspaceContent().findByRole("tab", { name: "Graph" }).click();+ },++ getWorkspacesSection() {+ return cy.findByTestId("workspaces-section");+ },++ getWorkspaceItem(name: string) {+ return this.getWorkspacesSection().find(`a[name="${name}"]`);+ },++ getWorkspaceItemStatus(name: string) {+ return this.getWorkspaceItem(name).findByTestId("workspace-status");+ },++ getWorkspaceItemActions(name: string) {+ return this.getWorkspaceItem(name).findByLabelText("More actions");+ },++ getWorkspaceNameInput() {+ return cy.findByPlaceholderText("Workspace name");+ },++ getRunAllTransformsButton() {+ return cy.findByTestId("run-all-button");+ },++ getMergeWorkspaceButton() {+ return cy.findByRole("button", { name: /Merge/ });+ },++ getMergeCommitInput() {+ return cy.findByPlaceholderText(+ "Describe the changes you made in this workspace...",+ );+ },++ getWorkspaceDatabaseSelect() {+ return cy.findByPlaceholderText("Select a database");+ },++ getTransformTabHeader() {+ return cy.findByTestId("transform-tab-header");+ },++ getTransformTargetButton() {+ return this.getTransformTabHeader().findByRole("button", {+ name: /Change target/,+ });+ },++ getRunTransformButton() {+ return this.getTransformTabHeader().findByTestId("run-button", {+ timeout: 10_000,+ });+ },++ getRunStatus() {+ return cy.findByTestId("run-status");+ },++ getSaveTransformButton() {+ return this.getTransformTabHeader().findByRole("button", { name: /Save/ });+ },++ getWorkspaceTransforms() {+ return cy.findByTestId("workspace-transforms");+ },++ getMainlandTransforms() {+ return cy.findByTestId("mainland-transforms", { timeout: 10_000 });+ },++ getNewWorkspaceButton() {+ return cy.findByRole("button", { name: /New workspace/ });+ },++ getNewWorkspaceNameInput() {+ return cy.findByPlaceholderText("Enter workspace name");+ },++ getNewWorkspaceDatabaseInput() {+ return cy.findByPlaceholderText("Select a database");+ },++ getTransformStatusDot(name: string | RegExp) {+ return cy.findByText(name).parent().findByTestId("status-dot");+ },++ getTransformTargetDiff() {+ return cy.findByTestId("transform-target-diff");+ },++ getSourceTablesDiff() {+ return cy.findByTestId("source-tables-diff");+ },+};diff --git a/e2e/support/helpers/index.ts b/e2e/support/helpers/index.tsindex 233dd819446..d32a1aef7c6 100644--- a/e2e/support/helpers/index.ts+++ b/e2e/support/helpers/index.ts@@ -14,9 +14,9 @@ export * from "./e2e-comment-helpers";export * from "./e2e-custom-column-helpers";export * from "./e2e-dashboard-helpers";export * from "./e2e-dashboard-visualizer-helpers";+export * from "./e2e-data-studio-helpers";export * from "./e2e-database-metadata-helpers";export * from "./e2e-datamodel-helpers";-export * from "./e2e-data-studio-helpers";export * from "./e2e-dependency-helpers";export * from "./e2e-detail-view-helpers";export * from "./e2e-dimension-list-helpers";@@ -61,4 +61,5 @@ export * from "./e2e-upload-helpers";export * from "./e2e-users-helpers";export * from "./e2e-visual-tests-helpers";export * from "./e2e-viz-settings-helpers";+export * from "./e2e-workspaces-helpers";export * from "./embedding-sdk-helpers/constants";diff --git a/e2e/test/scenarios/admin/databases.cy.spec.js b/e2e/test/scenarios/admin/databases.cy.spec.jsindex 40015aba443..b03a135fa6a 100644--- a/e2e/test/scenarios/admin/databases.cy.spec.js+++ b/e2e/test/scenarios/admin/databases.cy.spec.js@@ -40,6 +40,109 @@ describe(},);+describe(+ "admin > database > external databases > workspaces",+ { tags: ["@external"] },+ () => {+ beforeEach(() => {+ cy.intercept("POST", "/api/database/*/permission/workspace/check").as(+ "checkPermissions",+ );+ });++ [+ { dbName: "Writable Postgres12", snapshot: "postgres-writable" },+ { dbName: "Writable MySQL8", snapshot: "mysql-writable" },+ ].forEach(({ dbName, snapshot }) => {+ it(`should allow to enable and disable workspaces in ${snapshot} database`, () => {+ H.restore(snapshot);+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ H.addPostgresDatabase("Test DB");++ visitDatabase(WRITABLE_DB_ID);++ cy.findByLabelText("Enable workspaces").should("not.be.checked");+ cy.findByLabelText("Enable workspaces").parent().click();++ cy.wait("@checkPermissions");+ cy.findByLabelText("Enable workspaces").should("be.checked");++ cy.findByLabelText("Settings").click();+ H.popover().findByText("Data studio").click();+ H.Workspaces.getNewWorkspaceButton().click();+ cy.findByPlaceholderText("Select a database").click();+ H.popover().within(() => {+ cy.findByText(dbName).should("be.visible");+ cy.findByText("Test DB").should("not.exist");+ });++ cy.go(-2);+ cy.findByLabelText("Enable workspaces").should("be.checked");+ cy.findByLabelText("Enable workspaces").parent().click();+ cy.findByLabelText("Enable workspaces").should("not.be.checked");++ cy.go(2);+ cy.findByPlaceholderText("No database supports workspaces").should(+ "be.visible",+ );+ });+ });++ it("should not show workspaces setting for unsupported mysql database", () => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");++ visitDatabase(WRITABLE_DB_ID);++ cy.findByLabelText("Enable workspaces").should("not.exist");+ });++ it("should not allow to enable workspaces for a db user that cannot create users/schemas", () => {+ H.restore("postgres-writable");+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");++ // Create a limited postgres user without CREATE USER/SCHEMA permissions+ const limitedUser = "limited_user";+ const limitedPassword = "limited_pass";++ H.queryWritableDB(`+ DROP USER IF EXISTS ${limitedUser};+ CREATE USER ${limitedUser} WITH PASSWORD '${limitedPassword}';+ `);++ // Update the existing database connection to use the limited user+ cy.request("PUT", `/api/database/${WRITABLE_DB_ID}`, {+ details: {+ host: "localhost",+ port: QA_POSTGRES_PORT,+ dbname: "writable_db",+ user: limitedUser,+ password: limitedPassword,+ },+ });++ visitDatabase(WRITABLE_DB_ID);++ cy.findByLabelText("Enable workspaces").should("not.be.checked");+ cy.findByLabelText("Enable workspaces").parent().click();++ cy.wait("@checkPermissions");++ cy.findByTestId("database-workspaces-section").should(+ "contain.text",+ "Failed to initialize workspace isolation",+ );+ cy.findByLabelText("Enable workspaces").should("not.be.checked");++ // Cleanup: just drop the postgres user, H.restore() resets the DB connection+ H.queryWritableDB(`DROP USER IF EXISTS ${limitedUser};`);+ });+ },+);+describe("admin > database > add", () => {function toggleFieldWithDisplayName(displayName) {cy.findByLabelText(new RegExp(displayName)).click({ force: true });diff --git a/e2e/test/scenarios/custom-column/custom-column-reproductions.cy.spec.js b/e2e/test/scenarios/custom-column/custom-column-reproductions.cy.spec.jsindex f02f259731f..e29dbfbab22 100644--- a/e2e/test/scenarios/custom-column/custom-column-reproductions.cy.spec.js+++ b/e2e/test/scenarios/custom-column/custom-column-reproductions.cy.spec.js@@ -1049,11 +1049,11 @@ describe("issue 49882", () => {beforeEach(() => {H.restore();cy.signInAsNormalUser();- cy.intercept("POST", "/api/dataset/query_metadata").as("queryMetadata");-H.openOrdersTable({ mode: "notebook" });- cy.wait("@queryMetadata");- cy.findByLabelText("Custom column").click();+ cy.findByTestId("head-crumbs-container")+ .should("contain", "Sample Database")+ .and("contain", "Orders");+ H.addCustomColumn();});it("should not eat up subsequent characters when applying a suggestion (metabase#49882-1)", () => {@@ -1075,7 +1075,8 @@ describe("issue 49882", () => {.should("not.exist");});- it("does not clear expression input when expression is invalid (metabase#49882-2, metabase#15892)", () => {+ // TODO (Kamil 2026-02-02) -- https://linear.app/metabase/issue/GDGT-1658/unskip-metabase49882-2-metabase15892-repro+ it.skip("does not clear expression input when expression is invalid (metabase#49882-2, metabase#15892)", () => {// This test used to use keyboard shortcuts to cut and paste but this// seem impossible to emulate with CodeMirror in Cypress, so it's using// a synthetic paste event instead.diff --git a/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts b/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.tsindex 7d739e89122..fe74a0a08df 100644--- a/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts+++ b/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts@@ -235,7 +235,7 @@ describe("scenarios > admin > transforms", () => {"should be possible to create and run a Python transform",{ tags: ["@python"] },() => {- setPythonRunnerSettings();+ H.setPythonRunnerSettings();cy.log("create a new transform");visitTransformListPage();cy.button("Create a transform").click();@@ -416,7 +416,7 @@ LIMIT5`;createMbqlTransform({ visitTransform: true });- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();cy.url().should("include", "/edit");getQueryEditor().findByLabelText("View SQL").click();@@ -646,7 +646,7 @@ LIMITvisitTransform: true,});- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();getQueryEditor().within(() => {cy.findByTestId("run-button").eq(0).click();@@ -1468,8 +1468,7 @@ LIMIT});H.NativeEditor.get().should("have.attr", "contenteditable", "false");H.NativeEditor.get().should("have.attr", "aria-readonly", "true");- H.DataStudio.Transforms.editDefinition().should("be.visible");- H.DataStudio.Transforms.editDefinition().should(+ H.DataStudio.Transforms.getEditDefinitionLink().should("have.attr","href","/data-studio/transforms/1/edit",@@ -1483,8 +1482,7 @@ LIMIT.findByText("Animals").closest("button").should("be.disabled");- H.DataStudio.Transforms.editDefinition().should("be.visible");- H.DataStudio.Transforms.editDefinition().should(+ H.DataStudio.Transforms.getEditDefinitionLink().should("have.attr","href","/data-studio/transforms/1/edit",@@ -1496,7 +1494,7 @@ LIMITcreateMbqlTransform({ visitTransform: true });cy.log("visit edit mode");- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();cy.url().should("include", "/edit");cy.log("update the query");@@ -1528,7 +1526,7 @@ LIMIT});cy.log("visit edit mode");- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();cy.url().should("include", "/edit");cy.log("update the query");@@ -1620,7 +1618,7 @@ LIMITH.visitTransform(transformId);cy.log("remove score column (breaking change)");- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();H.getNotebookStep("data").findByLabelText("Pick columns").click();H.popover().findByLabelText("Score").click();H.DataStudio.Transforms.saveChangesButton().click();@@ -1637,7 +1635,7 @@ LIMITcy.button("Cancel").click();});- H.DataStudio.Transforms.editDefinition().should("not.exist");+ H.DataStudio.Transforms.editDefinitionButton().should("not.exist");H.DataStudio.Transforms.saveChangesButton().should("be.visible").and("be.enabled");@@ -1649,7 +1647,7 @@ LIMIT});it("should be able to update a Python query", { tags: ["@python"] }, () => {- setPythonRunnerSettings();+ H.setPythonRunnerSettings();cy.log("create a new transform");H.getTableId({ name: "Animals", databaseId: WRITABLE_DB_ID }).then((id) => {@@ -1667,7 +1665,7 @@ LIMIT);cy.log("enter edit mode");- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();cy.log("update the query");H.PythonEditor.type("{backspace}{backspace}{backspace} + 10 }])");@@ -1687,7 +1685,7 @@ LIMIT"should show Python transforms in view-only mode",{ tags: ["@python"] },() => {- setPythonRunnerSettings();+ H.setPythonRunnerSettings();cy.log("create a new Python transform");H.getTableId({ name: "Animals", databaseId: WRITABLE_DB_ID }).then((id) => {@@ -1705,8 +1703,7 @@ LIMIT);cy.log("should be in read-only mode by default");- H.DataStudio.Transforms.editDefinition().should("be.visible");- H.DataStudio.Transforms.editDefinition().should(+ H.DataStudio.Transforms.getEditDefinitionLink().should("have.attr","href","/data-studio/transforms/1/edit",@@ -1728,7 +1725,7 @@ LIMIT"should transition from read-only to edit mode for Python transforms",{ tags: ["@python"] },() => {- setPythonRunnerSettings();+ H.setPythonRunnerSettings();cy.log("create a new Python transform");H.getTableId({ name: "Animals", databaseId: WRITABLE_DB_ID }).then((id) => {@@ -1746,7 +1743,7 @@ LIMIT);cy.log("click Edit definition to enter edit mode");- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();cy.url().should("include", "/edit");cy.log("sidebar should be visible in edit mode");@@ -1756,7 +1753,7 @@ LIMITH.DataStudio.Transforms.pythonResults().should("be.visible");cy.log("Edit definition button should be hidden in edit mode");- H.DataStudio.Transforms.editDefinition().should("not.exist");+ H.DataStudio.Transforms.editDefinitionButton().should("not.exist");},);@@ -1764,7 +1761,7 @@ LIMIT"should return to read-only mode after saving a Python transform",{ tags: ["@python"] },() => {- setPythonRunnerSettings();+ H.setPythonRunnerSettings();cy.log("create a new Python transform");H.getTableId({ name: "Animals", databaseId: WRITABLE_DB_ID }).then((id) => {@@ -1782,7 +1779,7 @@ LIMIT);cy.log("enter edit mode");- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();cy.url().should("include", "/edit");cy.log("make a change to trigger dirty state");@@ -1794,7 +1791,7 @@ LIMITcy.log("should return to read-only mode after save");cy.url().should("not.include", "/edit");- H.DataStudio.Transforms.editDefinition().should("be.visible");+ H.DataStudio.Transforms.editDefinitionButton().should("be.visible");cy.findByTestId("python-data-picker").should("not.exist");H.DataStudio.Transforms.pythonResults().should("not.exist");},@@ -1810,7 +1807,7 @@ LIMIT});cy.log("visit edit mode and change to a complex query with LIMIT");- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();cy.url().should("include", "/edit");H.NativeEditor.type(" LIMIT 10");@@ -2096,7 +2093,7 @@ LIMITit("should be possible to cancel a SQL transform from the preview (metabase#64474)", () => {createSlowTransform(500);- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();cy.url().should("include", "/edit");getQueryEditor().within(() => {@@ -2196,7 +2193,7 @@ LIMIT"should be possible to use the common library",{ tags: ["@python"] },() => {- setPythonRunnerSettings();+ H.setPythonRunnerSettings();createPythonLibrary("common.py",dedent`@@ -2313,7 +2310,7 @@ LIMIT"should be able to run a transform with default import common even without custom library code",{ tags: ["@python"] },() => {- setPythonRunnerSettings();+ H.setPythonRunnerSettings();visitTransformListPage();cy.button("Create a transform").click();@@ -2862,7 +2859,7 @@ LIMITcy.visit("/data-studio/transforms/1");cy.log("'edit definition' button is not displayed");- H.DataStudio.Transforms.editDefinition().should("not.exist");+ H.DataStudio.Transforms.editDefinitionButton().should("not.exist");cy.log("visit the Run tab");H.DataStudio.Transforms.runTab().click();@@ -3670,7 +3667,7 @@ describe(H.activateToken("bleeding-edge");H.resyncDatabase({ dbId: WRITABLE_DB_ID, tableName: SOURCE_TABLE });- setPythonRunnerSettings();+ H.setPythonRunnerSettings();});afterEach(() => {@@ -3705,7 +3702,7 @@ describe();cy.log("enter edit mode");- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();cy.log("running the script should work");runPythonScriptAndWaitForSuccess();@@ -3746,7 +3743,7 @@ describe(},);- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();H.DataStudio.Transforms.pythonResults().findByText("Done")@@ -4073,28 +4070,6 @@ function createPythonLibrary(path: string, source: string) {});}-function setPythonRunnerSettings() {- H.updateEnterpriseSetting("python-runner-url", "http://localhost:5001");- H.updateEnterpriseSetting("python-runner-api-token", "dev-token-12345");- H.updateEnterpriseSetting(- "python-storage-s-3-endpoint",- "http://localhost:4566",- );- H.updateEnterpriseSetting("python-storage-s-3-region", "us-east-1");- H.updateEnterpriseSetting(- "python-storage-s-3-bucket",- "metabase-python-runner",- );- H.updateEnterpriseSetting("python-storage-s-3-prefix", "test-prefix");- H.updateEnterpriseSetting("python-storage-s-3-access-key", "test");- H.updateEnterpriseSetting("python-storage-s-3-secret-key", "test");- H.updateEnterpriseSetting(- "python-storage-s-3-container-endpoint",- "http://localstack:4566",- );- H.updateEnterpriseSetting("python-storage-s-3-path-style-access", true);-}-function runPythonScriptAndWaitForSuccess() {getQueryEditor().findByTestId("run-button").click();diff --git a/e2e/test/scenarios/data-studio/workspaces-metabot.cy.spec.ts b/e2e/test/scenarios/data-studio/workspaces-metabot.cy.spec.tsnew file mode 100644index 00000000000..473d9823afc--- /dev/null+++ b/e2e/test/scenarios/data-studio/workspaces-metabot.cy.spec.ts@@ -0,0 +1,433 @@+import { WRITABLE_DB_ID } from "e2e/support/cypress_data";++const { H } = cy;+const { Workspaces } = H;++const SOURCE_TABLE = "Animals";++// temporarily disabled, should be enabled in upcoming iteration+describe.skip("scenarios > data studio > workspaces > metabot", () => {+ beforeEach(() => {+ H.restore("postgres-writable");+ H.resetTestTable({ type: "postgres", table: "many_schemas" });+ H.resetSnowplow();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ H.resyncDatabase({ dbId: WRITABLE_DB_ID, tableName: SOURCE_TABLE });++ cy.request("PUT", "/api/permissions/graph", {+ groups: {+ "1": {+ "1": { download: { schemas: "full" }, "view-data": "unrestricted" },+ "2": {+ "view-data": "unrestricted",+ download: { schemas: "full" },+ "create-queries": "query-builder-and-native",+ },+ },+ },+ revision: 1,+ sandboxes: [],+ impersonations: [],+ });++ cy.intercept("POST", "/api/ee/workspace").as("createWorkspace");+ cy.intercept("POST", "/api/ee/metabot-v3/agent-streaming").as("agentReq");+ });++ it("should be able to create a transform via metabot in workspace", () => {+ Workspaces.visitWorkspaces();++ cy.log("Create a new workspace");+ Workspaces.getNewWorkspaceButton().click();+ cy.wait("@createWorkspace");++ cy.log("Open the Agent Chat tab");+ cy.findByRole("tab", { name: "Agent Chat" }).click();++ cy.log("Verify metabot chat is visible");+ cy.findByTestId("metabot-chat").should("be.visible");++ cy.log("Send a message to metabot asking to create a transform");+ H.mockMetabotResponse({+ body: createMockTransformSuggestionResponse(+ "I'll create a SQL transform that selects all animals for you.",+ createMockNativeTransformJSON(+ null,+ WRITABLE_DB_ID,+ 'SELECT * FROM "Schema A"."Animals"',+ ),+ ),+ });+ H.sendMetabotMessage("Create a SQL transform that selects all animals");++ cy.log("Verify the suggestion appears in the chat");+ cy.findByTestId("metabot-chat-suggestion").should("be.visible");+ cy.findByTestId("metabot-chat-suggestion").should(+ "contain",+ 'SELECT * FROM "Schema A"."Animals"',+ );++ cy.log("Apply the suggestion to create the transform");+ cy.findByTestId("metabot-chat-suggestion")+ .findByRole("button", { name: /create/i })+ .click();++ cy.log("Verify transform tab is opened with the suggested code");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("New transform", [+ "Setup",+ "Agent Chat",+ "Graph",+ "New transform",+ ]);+ });+ H.NativeEditor.get().should("contain", "SELECT * FROM");++ cy.log("Verify the transform appears in the workspace transforms list");+ Workspaces.getWorkspaceTransforms()+ .findByText("New transform")+ .should("be.visible");++ cy.log("Accept the suggested transform");+ cy.findByTestId("accept-proposed-changes-button").click();++ cy.log("Run preview to verify the transform works");+ cy.findByTestId("run-button").click();++ cy.log("Verify preview tab opens with results");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("Preview (New transform)", [+ "Setup",+ "Agent Chat",+ "Graph",+ "New transform",+ "Preview (New transform)",+ ]);+ });+ H.assertTableData({+ columns: ["name", "score"],+ firstRows: [+ ["Duck", "10"],+ ["Horse", "20"],+ ["Cow", "30"],+ ],+ });++ cy.log("Save the transform with a name and target table");+ cy.findByRole("tab", { name: "New transform" }).click();+ Workspaces.getSaveTransformButton().click();++ const transformName = "My transform";+ H.modal().within(() => {+ cy.findByLabelText(/Name/).clear().type(transformName);+ cy.findByLabelText(/Table name/)+ .clear()+ .type("animals_transform");+ cy.findByLabelText("Schema").click();+ cy.document()+ .findByRole("option", { name: /Schema A/ })+ .click();+ cy.findByRole("button", { name: /Save/ }).click();+ });+ verifyAndCloseToast("Transform saved successfully");++ cy.log("Verify the saved transform appears in workspace transforms list");+ Workspaces.getWorkspaceTransforms()+ .findByText(transformName)+ .should("be.visible");++ cy.log("Run the transform to materialize the output table");+ Workspaces.getRunTransformButton().click();+ Workspaces.getRunTransformButton().should("have.text", "Ran successfully");++ cy.log("Verify the target table appears in the Data tab");+ Workspaces.openDataTab();+ Workspaces.getWorkspaceSidebar()+ .findByText("Schema A.animals_transform")+ .should("be.visible");+ });++ it("should be able to reject a metabot suggestion and keep the editor empty", () => {+ Workspaces.visitWorkspaces();++ cy.log("Create a new workspace");+ Workspaces.getNewWorkspaceButton().click();+ cy.wait("@createWorkspace");++ cy.log("Open the Agent Chat tab");+ cy.findByRole("tab", { name: "Agent Chat" }).click();++ cy.log("Verify metabot chat is visible");+ cy.findByTestId("metabot-chat").should("be.visible");++ cy.log("Send a message to metabot asking to create a transform");+ H.mockMetabotResponse({+ body: createMockTransformSuggestionResponse(+ "I'll create a SQL transform that selects all animals for you.",+ createMockNativeTransformJSON(+ null,+ WRITABLE_DB_ID,+ 'SELECT * FROM "Schema A"."Animals"',+ ),+ ),+ });+ H.sendMetabotMessage("Create a SQL transform that selects all animals");++ cy.log("Verify the suggestion appears in the chat");+ cy.findByTestId("metabot-chat-suggestion").should("be.visible");++ cy.log("Apply the suggestion to create the transform");+ cy.findByTestId("metabot-chat-suggestion")+ .findByRole("button", { name: /create/i })+ .click();++ cy.log("Verify transform tab is opened with the suggested code");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("New transform", [+ "Setup",+ "Agent Chat",+ "Graph",+ "New transform",+ ]);+ });+ H.NativeEditor.get().should("contain", "SELECT * FROM");++ cy.log("Reject the suggested transform");+ cy.findByTestId("reject-proposed-changes-button").click();++ cy.log("Verify the editor is now empty after rejection");+ H.NativeEditor.value().should("be.empty");++ cy.log("Verify accept/reject buttons are no longer visible");+ cy.findByTestId("accept-proposed-changes-button").should("not.exist");+ cy.findByTestId("reject-proposed-changes-button").should("not.exist");++ cy.log("Verify save button is disabled since editor is empty");+ Workspaces.getSaveTransformButton().should("be.disabled");++ cy.log("Verify the transform still appears in workspace list as unsaved");+ Workspaces.getWorkspaceTransforms()+ .findByText("New transform")+ .should("be.visible");+ });++ it(+ "should be able to create a Python transform via metabot in workspace",+ { tags: ["@python"] },+ () => {+ Workspaces.visitWorkspaces();++ cy.log("Create a new workspace");+ Workspaces.getNewWorkspaceButton().click();+ cy.wait("@createWorkspace");++ cy.log("Open the Agent Chat tab");+ cy.findByRole("tab", { name: "Agent Chat" }).click();++ cy.log("Verify metabot chat is visible");+ cy.findByTestId("metabot-chat").should("be.visible");++ cy.log("Send a message to metabot asking to create a Python transform");+ H.mockMetabotResponse({+ body: createMockTransformSuggestionResponse(+ "I'll create a Python transform that returns a simple DataFrame.",+ createMockPythonTransformJSON(+ null,+ WRITABLE_DB_ID,+ {},+ "import pandas as pd\\n\\ndef transform():\\n return pd.DataFrame({'message': ['Hello from Python!']})",+ ),+ ),+ });+ H.sendMetabotMessage("Create a Python transform that returns a greeting");++ cy.log("Verify the suggestion appears in the chat");+ cy.findByTestId("metabot-chat-suggestion").should("be.visible");+ cy.findByTestId("metabot-chat-suggestion").should(+ "contain",+ "Hello from Python!",+ );++ cy.log("Apply the suggestion to create the transform");+ cy.findByTestId("metabot-chat-suggestion")+ .findByRole("button", { name: /create/i })+ .click();++ cy.log("Verify transform tab is opened with the suggested code");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("New transform", [+ "Setup",+ "Agent Chat",+ "Graph",+ "New transform",+ ]);+ });+ H.PythonEditor.get().should("contain", "Hello from Python!");++ cy.log("Verify the transform appears in the workspace transforms list");+ Workspaces.getWorkspaceTransforms()+ .findByText("New transform")+ .should("be.visible");++ cy.log("Accept the suggested transform");+ cy.findByTestId("accept-proposed-changes-button").click();++ cy.log("Run preview to verify the transform works");+ cy.findByTestId("run-button").click();++ cy.log("Verify preview tab opens with results");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("Preview (New transform)", [+ "Setup",+ "Agent Chat",+ "Graph",+ "New transform",+ "Preview (New transform)",+ ]);+ });+ H.assertTableData({+ columns: ["message"],+ firstRows: [["Hello from Python!"]],+ });++ cy.log("Save the transform with a name and target table");+ cy.findByRole("tab", { name: "New transform" }).click();+ Workspaces.getSaveTransformButton().click();++ const transformName = "My Python transform";+ H.modal().within(() => {+ cy.findByLabelText(/Name/).clear().type(transformName);+ cy.findByLabelText(/Table name/)+ .clear()+ .type("python_transform");+ cy.findByLabelText("Schema").click();+ cy.document()+ .findByRole("option", { name: /Schema A/ })+ .click();+ cy.findByRole("button", { name: /Save/ }).click();+ });+ verifyAndCloseToast("Transform saved successfully");++ cy.log("Verify the saved transform appears in workspace transforms list");+ Workspaces.getWorkspaceTransforms()+ .findByText(transformName)+ .should("be.visible");++ cy.log("Run the transform to materialize the output table");+ Workspaces.getRunTransformButton().click();+ Workspaces.getRunTransformButton().should(+ "have.text",+ "Ran successfully",+ );++ cy.log("Verify the target table appears in the Data tab");+ Workspaces.openDataTab();+ Workspaces.getWorkspaceSidebar()+ .findByText("Schema A.python_transform")+ .should("be.visible");+ },+ );++ it(+ "should be able to reject a Python metabot suggestion and keep the editor with default template",+ { tags: ["@python"] },+ () => {+ Workspaces.visitWorkspaces();++ cy.log("Create a new workspace");+ Workspaces.getNewWorkspaceButton().click();+ cy.wait("@createWorkspace");++ cy.log("Open the Agent Chat tab");+ cy.findByRole("tab", { name: "Agent Chat" }).click();++ cy.log("Verify metabot chat is visible");+ cy.findByTestId("metabot-chat").should("be.visible");++ cy.log("Send a message to metabot asking to create a Python transform");+ H.mockMetabotResponse({+ body: createMockTransformSuggestionResponse(+ "I'll create a Python transform that returns a simple DataFrame.",+ createMockPythonTransformJSON(+ null,+ WRITABLE_DB_ID,+ {},+ "import pandas as pd\\n\\ndef transform():\\n return pd.DataFrame({'message': ['Hello from Python!']})",+ ),+ ),+ });+ H.sendMetabotMessage("Create a Python transform that returns a greeting");++ cy.log("Verify the suggestion appears in the chat");+ cy.findByTestId("metabot-chat-suggestion").should("be.visible");++ cy.log("Apply the suggestion to create the transform");+ cy.findByTestId("metabot-chat-suggestion")+ .findByRole("button", { name: /create/i })+ .click();++ cy.log("Verify transform tab is opened with the suggested code");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("New transform", [+ "Setup",+ "Agent Chat",+ "Graph",+ "New transform",+ ]);+ });+ H.PythonEditor.get().should("contain", "Hello from Python!");++ cy.log("Reject the suggested transform");+ cy.findByTestId("reject-proposed-changes-button").click();++ cy.log(+ "Verify the editor has default Python template after rejection (not the suggestion)",+ );+ H.PythonEditor.get().should("not.contain", "Hello from Python!");+ H.PythonEditor.get().should("contain", "Your transformation function");++ cy.log("Verify accept/reject buttons are no longer visible");+ cy.findByTestId("accept-proposed-changes-button").should("not.exist");+ cy.findByTestId("reject-proposed-changes-button").should("not.exist");++ cy.log("Verify the transform still appears in workspace list as unsaved");+ Workspaces.getWorkspaceTransforms()+ .findByText("New transform")+ .should("be.visible");+ },+ );+});++function createMockNativeTransformJSON(+ id: number | null,+ databaseId: number,+ sql: string,+) {+ const escapedSql = sql.replace(/"/g, '\\"');+ return `{"id":${id},"name":"New transform","entity_id":null,"description":"","source":{"type":"query","query":{"database":${databaseId},"type":"native","native":{"query":"${escapedSql}","template-tags":{}}}},"target":{"type":"table","name":""},"created_at":null,"updated_at":null}`;+}++function createMockPythonTransformJSON(+ id: number | null,+ databaseId: number,+ sourceTables: { [tableName: string]: number },+ body: string,+) {+ return `{"id":${id},"name":"New transform","entity_id":null,"description":"","source":{"type":"python","source-database":${databaseId},"source-tables":${JSON.stringify(sourceTables)},"body":"${body}"},"target":{"type":"table","name":""},"created_at":null,"updated_at":null}`;+}++function createMockTransformSuggestionResponse(+ text: string,+ transformJSON: string,+) {+ return `0:"${text}"+2:{"type":"transform_suggestion","version":1,"value":${transformJSON}}+2:{"type":"state","version":1,"value":{}}+d:{"finishReason":"stop","usage":{}}`;+}++function verifyAndCloseToast(message: string) {+ H.undoToast().should("contain.text", message);+ H.undoToast().icon("close").click();+}diff --git a/e2e/test/scenarios/data-studio/workspaces.cy.spec.ts b/e2e/test/scenarios/data-studio/workspaces.cy.spec.tsnew file mode 100644index 00000000000..bc414f66411--- /dev/null+++ b/e2e/test/scenarios/data-studio/workspaces.cy.spec.ts@@ -0,0 +1,2603 @@+import dedent from "ts-dedent";++import { WRITABLE_DB_ID } from "e2e/support/cypress_data";+import { NativeEditor } from "e2e/support/helpers";+import type {+ DatabaseId,+ PythonTransformTableAliases,+ TransformId,+ TransformTagId,+ WorkspaceRunResponse,+} from "metabase-types/api";++const { H } = cy;+const { DataStudio, Workspaces } = H;+const { Transforms } = DataStudio;++const SOURCE_TABLE = "Animals";+const TARGET_TABLE_MBQL = "transform_table_1";+const TARGET_TABLE_SQL = "transform_table_2";+const TARGET_TABLE_PYTHON = "transform_table_3";+const TARGET_SCHEMA = "Schema A";++const COLOR_TEXT = "rgba(7, 23, 34, 0.84)";+const COLOR_SUCCESS = "rgb(104, 151, 53)";+const COLOR_DANGER = "rgb(227, 89, 94)";++describe("scenarios > data studio > workspaces", () => {+ beforeEach(() => {+ H.restore("postgres-writable");+ H.resetTestTable({ type: "postgres", table: "many_schemas" });+ H.resetSnowplow();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ H.resyncDatabase({ dbId: WRITABLE_DB_ID, tableName: SOURCE_TABLE });++ // TODO (Stas 2025-12-31) -- Is this correct way to grant querying permissions?+ cy.request("PUT", "/api/permissions/graph", {+ groups: {+ "1": {+ "1": { download: { schemas: "full" }, "view-data": "unrestricted" },+ "2": {+ "view-data": "unrestricted",+ download: { schemas: "full" },+ "create-queries": "query-builder-and-native",+ },+ },+ },+ revision: 1,+ sandboxes: [],+ impersonations: [],+ });++ enableWorkspacesInDb(WRITABLE_DB_ID);++ cy.intercept("POST", "/api/ee/workspace").as("createWorkspace");+ cy.intercept("POST", "/api/ee/workspace/*/transform/*/run").as(+ "runTransform",+ );+ cy.intercept("POST", "/api/ee/workspace/*/run").as("runTransforms");+ cy.intercept("POST", "/api/dataset").as("dataset");+ });++ describe("should be able to create, navigate, archive, unarchive, rename, and delete workspaces", () => {+ it("creates, navigates, archives, renames and deletes workspaces", () => {+ Workspaces.visitWorkspaces();+ Workspaces.getWorkspacesPage()+ .findByText("No active workspaces")+ .should("be.visible");++ cy.log("Properly shows empty state.");+ Workspaces.getWorkspacesSection()+ .findByText("No workspaces yet")+ .should("be.visible");++ createWorkspace();+ registerWorkspaceAliasName("workspaceNameA");++ cy.log("Navigates to available workspace page.");+ Workspaces.visitWorkspaces();+ cy.location("pathname").should("match", /data-studio\/workspaces\/\d+/);++ cy.log("Shows workspace name");+ cy.get("@workspaceNameA").then((workspaceName) => {+ Workspaces.getWorkspaceNameInput().should("have.value", workspaceName);+ });+ Workspaces.getMergeWorkspaceButton().should("be.disabled");++ Workspaces.getWorkspaceContent().within(() => {+ cy.log("Starts on setup tab, and has only 2 tabs");+ H.tabsShouldBe("Setup", ["Setup", "Graph"]);++ cy.log("shows workspace db");+ Workspaces.getWorkspaceDatabaseSelect()+ .should("have.value", "Writable Postgres12")+ .should("be.enabled");++ cy.log("Doesn't show workspace setup logs");+ cy.findByText("Setting up the workspace").should("not.exist");+ });++ Workspaces.getWorkspaceSidebar().within(() => {+ cy.log("starts on Code tab, and has only 2 tabs");+ H.tabsShouldBe("Code", ["Code", "Data"]);++ cy.log("shows transforms lists");+ cy.findByText("Workspace is empty").should("be.visible");+ cy.findByText("No available transforms").should("be.visible");+ });++ createWorkspace();+ registerWorkspaceAliasName("workspaceNameB");+ cy.get<string>("@workspaceNameB").then((workspaceNameB) => {+ Workspaces.getWorkspaceNameInput().should("have.value", workspaceNameB);++ cy.get<string>("@workspaceNameA").then((workspaceNameA) => {+ Workspaces.getWorkspacesSection().within(() => {+ cy.findByText(workspaceNameB).should("be.visible");+ cy.findByText(workspaceNameA).should("be.visible").click();+ });++ cy.log("can archive a workspace");+ Workspaces.getWorkspaceItemStatus(workspaceNameA).should(+ "contain.text",+ "Ready",+ );+ Workspaces.getWorkspaceItemActions(workspaceNameA).click();+ H.popover().findByText("Delete").should("not.exist");+ H.popover().findByText("Archive").click();+ verifyAndCloseToast("Workspace archived successfully");+ cy.log("Updates setup logs (GDGT-1583)");+ Workspaces.getWorkspaceContent()+ .findByText("Workspace is archived")+ .should("be.visible");++ cy.log("shows archived workspaces and their status");+ Workspaces.getWorkspaceItem(workspaceNameA).should(+ "contain.text",+ "Archived",+ );++ cy.log("can delete a workspace");+ Workspaces.getWorkspaceItemActions(workspaceNameA).click();+ H.popover().findByText("Delete").click();+ H.modal().findByText("Delete").click();+ verifyAndCloseToast("Workspace deleted successfully");+ Workspaces.getWorkspaceItem(workspaceNameA).should("not.exist");++ cy.location("pathname").should(+ "match",+ /data-studio\/workspaces\/\d+/,+ );+ Workspaces.getWorkspaceNameInput().should(+ "have.value",+ workspaceNameB,+ );++ Workspaces.getWorkspaceNameInput()+ .clear()+ .type("Renamed workspace")+ .blur();+ verifyAndCloseToast("Workspace renamed successfully");+ Workspaces.getWorkspaceItem("Renamed workspace").should("be.visible");+ });+ });+ });++ it("shows unsaved changes warning", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ Workspaces.getMainlandTransforms().findByText("SQL transform").click();+ H.NativeEditor.type(" LIMIT 2");++ cy.log("try to navigate away with unsaved changes - cancel navigation");+ DataStudio.nav().findByText("Glossary").click();+ H.modal().findByText("Discard your changes?").should("be.visible");+ H.modal().button("Cancel").click();++ Workspaces.getSaveTransformButton().click();+ Workspaces.getSaveTransformButton().should("be.disabled");++ cy.log("try to navigate away without unsaved changes");+ DataStudio.nav().findByText("Glossary").click();+ H.modal().should("not.exist");+ cy.location("pathname").should("eq", "/data-studio/glossary");+ cy.go("back");++ Workspaces.getWorkspaceTransforms().findByText("SQL transform").click();+ H.NativeEditor.type(";");++ cy.log("try to navigate away with unsaved changes - discard changes");+ DataStudio.nav().findByText("Glossary").click();+ H.modal().findByText("Discard your changes?").should("be.visible");+ H.modal().button("Discard changes").click();+ H.modal().should("not.exist");+ cy.location("pathname").should("eq", "/data-studio/glossary");+ });++ it("preserves workspace tabs state", () => {+ const sourceTable = `${TARGET_SCHEMA}.${SOURCE_TABLE}`;+ createTransforms();+ Workspaces.visitWorkspaces();+ cy.log("Create workspace, open transform tab");+ createWorkspace();+ registerWorkspaceAliasName("workspaceA");++ cy.log("Open the transform tab");+ Workspaces.getWorkspaceSidebar()+ .findByText("SQL transform")+ .should("be.visible")+ .click();++ H.NativeEditor.type(" LIMIT 2");+ Workspaces.getSaveTransformButton().click();++ cy.log("Create a second workspace");+ createWorkspace();+ registerWorkspaceAliasName("workspaceB");++ cy.log("Second workspace should start with default tabs");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("Setup", ["Setup", "Graph"]);+ });++ cy.log("Navigate back to first workspace");+ cy.get<string>("@workspaceA").then((workspaceNameA) => {+ Workspaces.getWorkspacesSection()+ .findByText(workspaceNameA)+ .should("be.visible")+ .click();++ cy.log("First workspace should preserve its tabs state");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe(sourceTable, ["Setup", "Graph", "SQL transform"]);+ });++ H.NativeEditor.value().should("contain", "LIMIT 2");+ });+ });++ it("archives and unarchives populated workspace (GDGT-1583)", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();+ registerWorkspaceAliasName("workspaceA");++ cy.get<string>("@workspaceA").then((workspaceName) => {+ cy.log("Add a transform to the workspace");+ Workspaces.getWorkspaceSidebar()+ .findByText("SQL transform")+ .should("be.visible")+ .click();++ H.NativeEditor.type(" LIMIT 2");+ Workspaces.getSaveTransformButton().click();++ cy.log("Archive the workspace");+ Workspaces.openSetupTab();+ Workspaces.getWorkspaceItemActions(workspaceName).click();+ H.popover().findByText("Archive").click();+ verifyAndCloseToast("Workspace archived successfully");+ cy.log("Updates setup logs (GDGT-1583)");+ Workspaces.getWorkspaceContent()+ .findByText(/Workspace is archived/)+ .should("be.visible");+ Workspaces.getWorkspaceItem(workspaceName).should(+ "contain.text",+ "Archived",+ );++ cy.log("Block UI for archived workspace");+ Workspaces.getMergeWorkspaceButton().should("be.disabled");+ Workspaces.getWorkspaceDatabaseSelect().should("be.disabled");++ Workspaces.getWorkspaceTransforms().findByText("SQL transform").click();+ Workspaces.getRunTransformButton().should("be.disabled");+ Workspaces.getSaveTransformButton().should("be.disabled");+ Workspaces.getTransformTargetButton().should("be.disabled");+ Workspaces.getWorkspaceTransforms()+ .findByText("SQL transform")+ .realHover();+ Workspaces.getWorkspaceTransforms()+ .findByLabelText("More actions")+ .should("not.exist");+ H.NativeEditor.get().should("have.attr", "aria-readonly", "true");++ cy.log("Unarchive the workspace");+ Workspaces.openSetupTab();+ Workspaces.getWorkspaceItemActions(workspaceName).click();+ H.popover().findByText("Restore").click();+ verifyAndCloseToast("Workspace restored successfully");+ cy.log("Updates setup logs (GDGT-1583)");+ Workspaces.getWorkspaceContent()+ .findByText("Workspace ready!")+ .should("be.visible");+ Workspaces.getWorkspaceItemStatus(workspaceName).should(+ "contain.text",+ "Ready",+ );+ Workspaces.getWorkspaceTransforms()+ .findByText("SQL transform")+ .realHover();+ Workspaces.getWorkspaceTransforms()+ .findByLabelText("More actions")+ .should("be.visible");+ });+ });++ it("should be able to check out existing SQL transform into a new workspace from the transform page", () => {+ cy.log("Prepare available transforms: MBQL, Python, SQL");+ const sourceTable = `${TARGET_SCHEMA}.${SOURCE_TABLE}`;+ const targetTableSql = `${TARGET_SCHEMA}.${TARGET_TABLE_SQL}`;+ createTransforms({ visit: true });++ cy.log("Create a workspace, open transform page in it");+ cy.findByRole("button", { name: /Edit/ }).click();+ H.popover().within(() => {+ cy.findByText("No workspaces yet").should("be.visible");+ cy.findByText("New workspace").should("be.visible").click();+ });+ registerWorkspaceAliasName("workspaceA");++ cy.location("pathname").should("match", /data-studio\/workspaces\/\d+/);+ cy.get("@workspaceA").then((workspaceName) => {+ Workspaces.getWorkspaceNameInput().should("have.value", workspaceName);+ });++ cy.log("No transforms are checked out yet");+ Workspaces.getWorkspacePage()+ .findByText("Workspace is empty")+ .should("be.visible");+ Workspaces.getMainlandTransforms()+ .findByText("Python transform")+ .should("be.visible");+ Workspaces.getMainlandTransforms()+ .findByText("MBQL transform")+ .should("be.visible")+ .realHover();+ H.tooltip()+ .findByText(+ "Transforms based on the query builder cannot be edited in a workspace.",+ )+ .should("be.visible");+ Workspaces.getMainlandTransforms()+ .findByText("SQL transform")+ .should("be.visible")+ .click();++ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("SQL transform", ["Setup", "Graph", "SQL transform"]);+ });+ cy.log("UI Controls are hidden/disabled until changes are made");+ Workspaces.getMergeWorkspaceButton().should("be.disabled");+ Workspaces.getTransformTargetButton().should("not.exist");+ Workspaces.getRunTransformButton().should("not.exist");+ Workspaces.getSaveTransformButton().should("be.enabled");++ H.NativeEditor.type(" LIMIT 2");++ cy.log("UI Controls are enabled after changes");+ Workspaces.getMergeWorkspaceButton().should("be.disabled");+ Workspaces.getTransformTargetButton().should("not.exist");+ Workspaces.getRunTransformButton().should("not.exist");+ Workspaces.getSaveTransformButton().should("be.enabled").click();++ cy.log(+ "Merge/Run controls are enabled after transform is saved to a workspace",+ );+ Workspaces.getMergeWorkspaceButton().should("be.enabled");+ Workspaces.getSaveTransformButton().should("be.disabled");+ Workspaces.getRunTransformButton().should("be.enabled").click();++ Workspaces.getWorkspaceSidebar().within(() => {+ cy.findByRole("tab", { name: "Data" }).click();+ cy.findByLabelText(sourceTable).should("be.visible").click();+ });++ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe(sourceTable, [+ "Setup",+ "Graph",+ "SQL transform",+ sourceTable,+ ]);+ H.assertTableData({+ columns: ["Name", "Score"],+ firstRows: [+ ["Duck", "10"],+ ["Horse", "20"],+ ["Cow", "30"],+ ],+ });+ });++ cy.findByLabelText(targetTableSql).should("be.visible").click();+ cy.wait("@dataset");++ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe(targetTableSql, [+ "Setup",+ "Graph",+ "SQL transform",+ sourceTable,+ targetTableSql,+ ]);+ H.assertTableData({+ columns: ["Name", "Score"],+ firstRows: [+ ["Duck", "10"],+ ["Horse", "20"],+ ],+ });+ cy.findByText("Cow").should("not.exist");+ cy.findByText("30").should("not.exist");+ });++ Workspaces.getMergeWorkspaceButton().click();+ Workspaces.getMergeCommitInput().type("Merge transform");+ H.modal().within(() => {+ cy.log("All quality checks should be passed");+ cy.findByText("Quality Checks").should("exist");+ cy.findByText("External dependencies").should("exist");+ cy.findByText("Internal dependencies").should("exist");+ cy.findByText("Structural issues").should("exist");+ cy.findByText("Unused outputs").should("exist");+ cy.findByText("1 transform will be merged").should("exist");++ cy.log("Transform diffs are displayed");+ cy.findByText("Modified transforms").should("exist");+ cy.findByTestId("transform-list-item")+ .should("contain.text", "SQL transform")+ .should("be.visible")+ .should(($el) => {+ expect($el.text()).to.include("SQL transform");+ expect($el.text()).to.match(/\+1\b/);+ expect($el.text()).to.match(/-1\b/);+ })+ .click();+ cy.contains(+ '[class*="cm-deletedLine"]',+ 'SELECT * FROM "Schema A"."Animals"',+ );+ cy.contains(+ '[class*="cm-insertedLine"]',+ 'SELECT * FROM "Schema A"."Animals" LIMIT 2',+ );++ cy.findByRole("button", { name: /Merge/ }).click();+ });+ verifyAndCloseToast("merged successfully");++ Transforms.list()+ .findByRole("row", { name: /SQL transform/ })+ .click();+ H.NativeEditor.value().should(+ "eq",+ 'SELECT * FROM "Schema A"."Animals" LIMIT 2',+ );+ Transforms.runTab().click();+ runTransformAndWaitForSuccessOnTransformsPage();+ Transforms.settingsTab().click();+ getTableLink().should("contain.text", TARGET_TABLE_SQL).click();++ H.assertTableData({+ columns: ["Name", "Score"],+ firstRows: [+ ["Duck", "10"],+ ["Horse", "20"],+ ],+ });+ H.tableInteractiveBody().findByText("Cow").should("not.exist");+ H.tableInteractiveBody().findByText("30").should("not.exist");+ });+ });++ describe("should show tabs UI correctly", () => {+ it("should allow closing tabs with close button, selects fallback tab", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("Setup", ["Setup", "Graph"]);+ });++ cy.log("Open transform tabs");+ Workspaces.getMainlandTransforms().findByText("Python transform").click();++ Workspaces.getMainlandTransforms().findByText("SQL transform").click();++ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("SQL transform", [+ "Setup",+ "Graph",+ "Python transform",+ "SQL transform",+ ]);+ });++ cy.log("Reorder and close tabs");+ Workspaces.getWorkspaceContent().within(() => {+ cy.findAllByRole("tab").eq(3).as("sqlTransformTab");+ H.moveDnDKitElementByAlias("@sqlTransformTab", {+ horizontal: -150,+ });+ cy.wait(100);+ cy.findAllByRole("tab")+ .eq(0)+ .findByLabelText("close icon")+ .should("not.exist");+ cy.findAllByRole("tab")+ .eq(1)+ .findByLabelText("close icon")+ .should("not.exist");+ cy.findAllByRole("tab")+ .eq(2)+ .findByLabelText("close icon")+ .should("exist");+ cy.findAllByRole("tab")+ .eq(3)+ .findByLabelText("close icon")+ .should("exist")+ .click();+ });++ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("SQL transform", ["Setup", "Graph", "SQL transform"]);+ });+ });++ it("closes relevant tabs after ws transform is removed", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ Workspaces.getMainlandTransforms().findByText("SQL transform").click();+ H.NativeEditor.type(" LIMIT 2");+ Workspaces.getSaveTransformButton().click();+ runTransformAndWaitForSuccess();++ cy.log("Open the transform table tab");+ Workspaces.openDataTab();+ Workspaces.getWorkspaceSidebar()+ .findByText(`${TARGET_SCHEMA}.${TARGET_TABLE_SQL}`)+ .should("be.visible")+ .click();++ cy.log("Verify both tabs are open");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe(TARGET_TABLE_SQL, [+ "Setup",+ "Graph",+ "SQL transform",+ "transform_table",+ ]);+ });++ Workspaces.openCodeTab();++ cy.log("Remove the transform from the workspace");+ Workspaces.getWorkspaceTransforms()+ .findByText("SQL transform")+ .realHover();+ Workspaces.getWorkspaceTransforms()+ .findByLabelText("More actions")+ .click();+ H.popover().findByText("Remove").click();+ verifyAndCloseToast("Transform removed from the workspace");++ cy.log("Verify both transform tab and table tab have been closed");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("Setup", ["Setup", "Graph"]);+ });+ });+ });++ describe("setup tab", () => {+ it("should allow to change database before transforms are added", () => {+ H.addPostgresDatabase("Test DB", undefined, undefined, "testDbId");+ cy.get<DatabaseId>("@testDbId").then((testDbId) => {+ enableWorkspacesInDb(testDbId);+ });++ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Database dropdown should be enabled initially");+ Workspaces.getWorkspaceDatabaseSelect()+ .should("have.value", "Test DB")+ .click();+ H.popover()+ .findByRole("option", { name: "Sample Database" })+ .should("not.exist");+ H.popover().findByRole("option", { name: "Writable Postgres12" }).click();++ cy.log("Verify database was changed");+ verifyAndCloseToast("Successfully updated workspace database");+ Workspaces.getWorkspaceDatabaseSelect().should(+ "have.value",+ "Writable Postgres12",+ );+ });++ it("should lock database dropdown when workspace has been initialized, shows setup log", () => {+ // @flaky+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Add WS transform to lock DB selection");+ Workspaces.getMainlandTransforms()+ .findByText("SQL transform")+ .should("be.visible")+ .click();+ Workspaces.getWorkspaceContent().within(() => {+ H.NativeEditor.type(" LIMIT 2");+ Workspaces.getSaveTransformButton().click();++ cy.findByRole("tab", { name: "Setup" }).click();++ cy.log("Database dropdown should be disabled after adding transforms");+ Workspaces.getWorkspaceDatabaseSelect()+ .should("be.disabled")+ .should("have.value", "Writable Postgres12");++ cy.log("Setup log should be visible");+ cy.findByText(/Setting up the workspace/).should("be.visible");+ });+ });++ it("should disable workspace actions while workspace is in pending status", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Mock /log endpoint to return pending status");+ cy.log("Add transform to workspace");+ Workspaces.getMainlandTransforms().findByText("SQL transform").click();+ H.NativeEditor.type(" LIMIT 2");++ cy.intercept("GET", "/api/ee/workspace/*/log", {+ body: {+ workspace_id: 1,+ status: "pending",+ updated_at: null,+ last_completed_at: null,+ logs: [+ {+ id: 1,+ task: "workspace-setup",+ description: "Setting up workspace",+ status: "started",+ message: null,+ },+ ],+ },+ }).as("getWorkspaceLog");++ Workspaces.getSaveTransformButton().click();++ cy.log("Verify merge button is disabled");+ Workspaces.getMergeWorkspaceButton().should("be.disabled");++ cy.log("Verify transform actions menu is disabled");+ Workspaces.getWorkspaceTransforms()+ .findByText("SQL transform")+ .parent()+ .findByLabelText("More actions")+ .should("not.exist");++ cy.log(+ "Switch to Data tab and verify clicking uninitialized output table does not run transform",+ );+ Workspaces.openDataTab();+ cy.intercept("POST", "/api/ee/workspace/*/transform/*/run").as(+ "runTransformRequest",+ );+ cy.findByTestId("workspace-sidebar")+ .findByLabelText(/transform_table_2/)+ .click();+ cy.get("@runTransformRequest.all").should("have.length", 0);+ });+ });++ describe("graph tab", () => {+ it(+ "should display multiple transforms and their connections",+ { tags: ["@python"] },+ () => {+ H.setPythonRunnerSettings();+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ Workspaces.getMainlandTransforms().findByText("SQL transform").click();+ Workspaces.getSaveTransformButton().click();+ Workspaces.getWorkspaceTransforms()+ .findByText("SQL transform")+ .should("be.visible");++ Workspaces.getMainlandTransforms()+ .findByText("Python transform")+ .click();+ Workspaces.getSaveTransformButton().click();+ Workspaces.getWorkspaceTransforms()+ .findByText("Python transform")+ .should("be.visible");++ Workspaces.getRunAllTransformsButton().click();+ H.popover().findByText("Run all transforms").click();+ verifyAndCloseToast("Transforms ran successfully");++ Workspaces.openGraphTab();++ // Verify nodes+ H.DependencyGraph.graph().should("be.visible");+ H.DependencyGraph.graph()+ .findByLabelText("SQL transform")+ .should("be.visible");+ H.DependencyGraph.graph()+ .findByLabelText("Python transform")+ .should("be.visible");+ H.DependencyGraph.graph()+ .findByLabelText(TARGET_TABLE_SQL)+ .should("be.visible");+ H.DependencyGraph.graph()+ .findByLabelText(TARGET_TABLE_PYTHON)+ .should("be.visible");++ // Verify edges: input tables -> transforms -> output tables+ // To simplify test logic, we just check that all edges are present.+ // In total there should be 4 edges:+ // - Animals -> SQL transform+ // - Animals -> Python transform+ // - SQL transform -> target table+ // - Python transform -> target table+ H.DependencyGraph.graph()+ .findAllByLabelText(/Edge from.*workspace-transform.*to.*Animals/)+ .should("have.length", 2);+ H.DependencyGraph.graph()+ .findAllByLabelText(/Edge from.*target.*to.*workspace-transform/)+ .should("have.length", 2);+ },+ );++ it("should allow clicking on graph nodes to see details", () => {+ // @flaky+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ Workspaces.getMainlandTransforms().findByText("SQL transform").click();+ Workspaces.getSaveTransformButton().click();+ runTransformAndWaitForSuccess();++ Workspaces.openGraphTab();++ H.DependencyGraph.graph().findByLabelText(SOURCE_TABLE).click();+ H.DependencyGraph.graph()+ .findByLabelText(SOURCE_TABLE)+ .should("have.attr", "aria-selected", "true");++ cy.log("Verify info panel shows fields for input table");+ cy.findByTestId("graph-info-panel").within(() => {+ cy.findByText("2 fields").should("be.visible");+ cy.findByText("Name").should("be.visible");+ cy.findByText("Score").should("be.visible");+ cy.findByLabelText("Close").click();+ });++ H.DependencyGraph.graph().findByLabelText("SQL transform").click();+ H.DependencyGraph.graph()+ .findByLabelText("SQL transform")+ .should("have.attr", "aria-selected", "true");++ cy.log("Verify info panel shows transform link");+ cy.findByTestId("graph-info-panel").within(() => {+ cy.findByLabelText("View this workspace transform").should(+ "be.visible",+ );+ });++ cy.log("Click transform link and verify navigation to transform tab");+ cy.findByTestId("graph-info-panel")+ .findByLabelText("View this workspace transform")+ .click();+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("SQL transform", ["Setup", "Graph", "SQL transform"]);+ });+ });+ });++ describe("code tab", () => {+ it("should check out transform into the workspace and remove it", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Open mainland transform");+ Workspaces.getMainlandTransforms()+ .findByText("SQL transform")+ .should("be.visible")+ .click();++ cy.log("Make changes to the transform");+ Workspaces.getWorkspaceContent().within(() => {+ H.NativeEditor.type(" LIMIT 2");+ });++ cy.log(+ "Check that transform has yellow dot change indicator after editing",+ );+ Workspaces.getMainlandTransforms().within(() => {+ Workspaces.getTransformStatusDot("SQL transform").should("be.visible");+ });++ cy.log("Save the transform to add it to workspace");+ Workspaces.getSaveTransformButton().click();+ cy.log("Save button should be disabled after saving");+ Workspaces.getSaveTransformButton().should("be.disabled");++ cy.log("Transform should be removed from available list after saving");+ Workspaces.getMainlandTransforms()+ .findByText("SQL transform")+ .should("not.exist");++ cy.log("Transform should appear in workspace transforms list");+ Workspaces.getWorkspaceTransforms()+ .findByText("SQL transform")+ .should("be.visible");++ cy.log("Make additional changes to the saved transform");+ Workspaces.getWorkspaceContent().within(() => {+ H.NativeEditor.type(";");+ });++ cy.log("Check that transform has yellow dot status again");+ Workspaces.getWorkspaceTransforms().within(() => {+ Workspaces.getTransformStatusDot("SQL transform").should("be.visible");+ });++ cy.log("Save it again");+ cy.log(+ "Add delay to API response to verify button is disabled immediately on click",+ );+ cy.intercept(+ { method: "PUT", url: "/api/ee/workspace/*/transform/*" },+ (req) => {+ req.on("response", (res) => {+ res.setDelay(500);+ });+ },+ ).as("updateTransformSlow");++ Workspaces.getWorkspaceContent().within(() => {+ Workspaces.getSaveTransformButton().click();+ cy.log("Save button should show loading state immediately after click");+ Workspaces.getSaveTransformButton().should("have.attr", "data-loading");+ });+ cy.log("Wait for save to complete");+ cy.wait("@updateTransformSlow");+ cy.log(+ "Save button should be disabled after save completes (no changes)",+ );+ Workspaces.getSaveTransformButton().should("be.disabled");++ cy.log("Check that yellow dot doesn't exist anymore after saving");+ Workspaces.getWorkspaceTransforms().within(() => {+ Workspaces.getTransformStatusDot("SQL transform").should("not.exist");+ });++ cy.log("Remove transform from the workspace through ellipsis menu");+ Workspaces.getWorkspaceTransforms()+ .findByText("SQL transform")+ .realHover();+ Workspaces.getWorkspaceTransforms()+ .findByLabelText("More actions")+ .click();+ H.popover()+ .findByRole("menuitem", { name: /Remove/ })+ .click();++ cy.log("Check that it's not present in workspace transforms list");+ Workspaces.getWorkspaceTransforms()+ .findByText("SQL transform")+ .should("not.exist");++ cy.log("Check that it's displayed back in available transforms list");+ Workspaces.getMainlandTransforms()+ .findByText("SQL transform")+ .should("be.visible");+ });+ });++ describe("transform tab", () => {+ it("should create new transform - SQL", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Click Add Transform button");+ Workspaces.getWorkspaceSidebar().findByLabelText("Add transform").click();++ cy.log("Check that SQL and Python options are provided");+ H.popover()+ .findByRole("menuitem", { name: /Python Script/ })+ .should("be.visible");+ H.popover()+ .findByRole("menuitem", { name: /SQL Transform/ })+ .should("be.visible")+ .click();++ cy.log("Check that it opens new empty tab");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("New transform", ["Setup", "Graph", "New transform"]);+ H.NativeEditor.value().should("be.empty");+ Workspaces.getSaveTransformButton().should("be.disabled");+ });++ cy.log(+ "Check that 'New transform' is added to workspace transforms list",+ );+ Workspaces.getWorkspaceTransforms()+ .findByText("New transform")+ .should("be.visible");++ cy.log("Type a query in the editor");+ Workspaces.getWorkspaceContent().within(() => {+ H.NativeEditor.paste("SELECT * FROM many_schemas.animals");+ });++ cy.log("Open transform settings");+ Workspaces.getSaveTransformButton().click();++ cy.log(+ "Check that table name gets automatically populated based on transform name",+ );+ H.modal().within(() => {+ cy.findByText("Incremental transformation").should("not.exist");+ cy.findByLabelText(/Table name/).should("have.value", "new_transform");+ cy.findByDisplayValue("new_transform").clear().type("test_table");++ cy.findByLabelText("Schema").click();+ cy.document()+ .findByRole("option", { name: /Schema B/ })+ .click();++ cy.findByRole("button", { name: /Save/ }).click();+ });+ verifyAndCloseToast("Transform saved successfully");++ cy.log("Verify transform is saved with new name");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("New transform", ["Setup", "Graph", "New transform"]);+ });++ cy.log(+ "Check transform appears in workspace transforms list with correct name",+ );+ Workspaces.getWorkspaceTransforms()+ .findByText("New transform")+ .should("be.visible");++ Workspaces.openDataTab();+ Workspaces.getWorkspaceSidebar()+ .findByText("Schema B.test_table")+ .should("be.visible");++ Workspaces.getTransformTargetButton().click();+ H.modal().within(() => {+ cy.findByLabelText("Schema").should("have.value", "Schema B");+ cy.findByLabelText("New table name")+ .should("have.value", "test_table")+ .clear()+ .type("new_table");+ cy.findByRole("button", { name: /Change target/ }).click();+ });+ verifyAndCloseToast("Transform target updated");++ Workspaces.getWorkspaceSidebar()+ .findByText("Schema B.new_table")+ .should("be.visible");++ Workspaces.openCodeTab();+ Workspaces.getWorkspaceTransforms().within(() => {+ Workspaces.getTransformStatusDot("New transform").should("not.exist");+ });+ });++ it("should create new transform - Python", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Click Add Transform button");+ Workspaces.getWorkspaceSidebar().findByLabelText("Add transform").click();++ cy.log("Check that Python and SQL options are provided");+ H.popover()+ .findByRole("menuitem", { name: /SQL Transform/ })+ .should("be.visible");+ H.popover()+ .findByRole("menuitem", { name: /Python Script/ })+ .should("be.visible")+ .click();++ cy.log("Check that it opens new empty tab");+ Workspaces.getWorkspaceTabs().within(() => {+ H.tabsShouldBe("New transform", ["Setup", "Graph", "New transform"]);+ });+ H.PythonEditor.value().should("not.be.empty");++ cy.log(+ "Check that 'New transform' is added to workspace transforms list",+ );+ Workspaces.getWorkspaceTransforms()+ .findByText("New transform")+ .should("be.visible");++ cy.log("Type a query in the editor");+ Workspaces.getWorkspaceContent().within(() => {+ H.PythonEditor.clear().paste(TEST_PYTHON_TRANSFORM);++ cy.findByTestId("python-data-picker")+ .findByText("Select a table…")+ .click();+ });++ H.entityPickerModal().within(() => {+ cy.findByText("Schema A").click();+ cy.findByText("Animals").click();+ });++ cy.log("Open transform settings");+ Workspaces.getSaveTransformButton().click();++ cy.log(+ "Check that table name gets automatically populated based on transform name",+ );+ H.modal().within(() => {+ cy.findByText("Incremental transformation").should("not.exist");+ cy.findByLabelText(/Table name/).should("have.value", "new_transform");+ cy.findByDisplayValue("new_transform").clear().type("test_table");++ cy.findByLabelText("Schema").click();+ cy.document()+ .findByRole("option", { name: /Schema B/ })+ .click();++ cy.findByRole("button", { name: /Save/ }).click();+ });+ verifyAndCloseToast("Transform saved successfully");++ cy.log("Verify transform is saved with new name");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("New transform", ["Setup", "Graph", "New transform"]);+ });++ cy.log(+ "Check transform appears in workspace transforms list with correct name",+ );+ Workspaces.getWorkspaceTransforms()+ .findByText("New transform")+ .should("be.visible");++ Workspaces.openDataTab();+ Workspaces.getWorkspaceSidebar().within(() => {+ cy.findByText("Schema B.test_table").should("be.visible");+ cy.findByText("Schema A.Animals").should("be.visible");+ });++ cy.findByTestId("python-data-picker").findByText("Animals").click();++ H.entityPickerModal().within(() => {+ cy.findByText("Schema B").click();+ cy.findByText("Animals").click();+ });++ Workspaces.getSaveTransformButton().click();++ Workspaces.openDataTab();+ Workspaces.getWorkspaceSidebar()+ .findByText("Schema B.Animals")+ .should("be.visible");++ Workspaces.getTransformTargetButton().click();+ H.modal().within(() => {+ cy.findByLabelText("Schema").should("have.value", "Schema B");+ cy.findByLabelText("New table name")+ .should("have.value", "test_table")+ .clear()+ .type("new_table");+ cy.findByRole("button", { name: /Change target/ }).click();+ });+ verifyAndCloseToast("Transform target updated");++ Workspaces.getWorkspaceSidebar()+ .findByText("Schema B.new_table")+ .should("be.visible");++ Workspaces.openCodeTab();+ Workspaces.getWorkspaceTransforms().within(() => {+ Workspaces.getTransformStatusDot("New transform").should("not.exist");+ });+ });++ it("should validate target name", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Add existing transform");+ Workspaces.getMainlandTransforms()+ .findByText("SQL transform")+ .should("be.visible")+ .click();++ cy.log("Make changes to the transform");+ Workspaces.getWorkspaceContent().within(() => {+ H.NativeEditor.type(" LIMIT 2");+ });+ Workspaces.getSaveTransformButton().click();++ cy.log("Create new transform");+ Workspaces.getWorkspaceSidebar().findByLabelText("Add transform").click();++ H.popover()+ .findByRole("menuitem", { name: /SQL Transform/ })+ .click();++ Workspaces.getWorkspaceContent().within(() => {+ H.NativeEditor.paste("any text");+ });+ Workspaces.getSaveTransformButton().click();++ H.modal().within(() => {+ cy.findByDisplayValue("new_transform").clear();+ cy.findByText("Target table name is required").should("be.visible");+ cy.realType(TARGET_TABLE_SQL);+ cy.findByText(+ "Another transform in this workspace already targets that table",+ ).should("be.visible");+ cy.realType("1");+ cy.findByText(+ "Another transform in this workspace already targets that table",+ ).should("not.exist");+ });+ });+ });++ describe("data tab", () => {+ it("should list input and output tables", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ Workspaces.getMainlandTransforms().findByText("SQL transform").click();++ H.NativeEditor.type(" LIMIT 1;");+ Workspaces.getSaveTransformButton().click();++ cy.log("Close saved transform tab");+ Workspaces.getWorkspaceContent().within(() => {+ cy.findByRole("tab", { name: "SQL transform" })+ .findByLabelText("close icon")+ .click();+ });++ Workspaces.openDataTab();+ Workspaces.getWorkspaceSidebar()+ .findByText(`${TARGET_SCHEMA}.${SOURCE_TABLE}`)+ .should("be.visible");+ Workspaces.getWorkspaceSidebar()+ .findByText(`${TARGET_SCHEMA}.${TARGET_TABLE_SQL}`)+ .as("outputTable")+ .should("be.visible")+ .realHover();+ cy.root()+ .findByText("Run transform to see the results")+ .should("be.visible");++ cy.get("@outputTable").realHover().click();++ Workspaces.getWorkspaceContent().within(() => {+ cy.findByText("Loading...").should("be.visible");+ H.assertTableData({+ columns: ["Name", "Score"],+ firstRows: [["Duck", "10"]],+ });+ });++ Workspaces.getWorkspaceTabs().within(() => {+ H.tabsShouldBe(`${TARGET_SCHEMA}.${TARGET_TABLE_SQL}`, [+ "Setup",+ "Graph",+ `${TARGET_SCHEMA}.${TARGET_TABLE_SQL}`,+ ]);+ });++ Workspaces.getWorkspaceSidebar()+ .findByLabelText(`${TARGET_SCHEMA}.${TARGET_TABLE_SQL}`)+ .findByLabelText("Open transform")+ .as("transformLink")+ .realHover();+ cy.root().findByText("Open transform").should("be.visible");+ cy.get("@transformLink").click();++ Workspaces.getWorkspaceTabs().within(() => {+ H.tabsShouldBe("SQL transform", [+ "Setup",+ "Graph",+ `${TARGET_SCHEMA}.${TARGET_TABLE_SQL}`,+ "SQL transform",+ ]);+ });+ });+ });++ describe("run transform", () => {+ it("should run and fail transform runs", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Run transform");+ Workspaces.getMainlandTransforms().findByText("SQL transform").click();++ H.NativeEditor.type(" LIMIT");+ Workspaces.getSaveTransformButton().click();++ Workspaces.getWorkspaceContent().findByText(+ "This transform hasn't been run before.",+ );++ Workspaces.getRunTransformButton().click();++ H.undoToast().findByText(/Transform run failed/);++ H.NativeEditor.type(" 1;");+ Workspaces.getSaveTransformButton().click();+ Workspaces.getRunTransformButton().click();++ Workspaces.getWorkspaceContent().findByText("Ran successfully");+ Workspaces.getWorkspaceContent().findByText(+ "Last ran a few seconds ago successfully.",+ );++ H.NativeEditor.type("{backspace}{backspace}");+ Workspaces.getSaveTransformButton().click();+ Workspaces.getRunTransformButton().click();++ H.undoToast().findByText(/Transform run failed/);+ });++ it("should show ad-hoc results", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Run ad-hoc query");+ Workspaces.getMainlandTransforms().findByText("SQL transform").click();++ H.NativeEditor.type(" LIMIT 1;");+ Workspaces.getSaveTransformButton().click();+ cy.log("open preview");+ cy.findByLabelText("Get Answer").click();++ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("Preview (SQL transform)", [+ "Setup",+ "Graph",+ "SQL transform",+ "Preview (SQL transform)",+ ]);+ cy.findByText("Preview (SQL transform)").click();+ // TODO (Stas 2025-12-30) -- Is it expected that ad-hoc cols have lowercase names?+ H.assertTableData({+ columns: ["name", "score"],+ firstRows: [["Duck", "10"]],+ });+ });+ });++ it("should open preview on Cmd+Enter when no unsaved changes, and not when there are changes", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Open SQL transform and save");+ Workspaces.getMainlandTransforms().findByText("SQL transform").click();+ H.NativeEditor.type(" LIMIT 1;");+ Workspaces.getSaveTransformButton().click();++ cy.log("Cmd+Enter opens preview when transform is saved");+ H.NativeEditor.focus();+ cy.realPress([+ Cypress.platform === "darwin" ? "Meta" : "Control",+ "Enter",+ ]);+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("Preview (SQL transform)", [+ "Setup",+ "Graph",+ "SQL transform",+ "Preview (SQL transform)",+ ]);+ });++ cy.log("Close preview tab");+ Workspaces.getWorkspaceContent().within(() => {+ cy.findByRole("tab", { name: "Preview (SQL transform)" })+ .icon("close")+ .click();+ });++ cy.log("Go back to SQL transform tab and make a change");+ Workspaces.getWorkspaceContent()+ .findByRole("tab", { name: "SQL transform" })+ .click();+ H.NativeEditor.type(" ");++ cy.log("Cmd+Enter does not open preview when there are unsaved changes");+ cy.realPress([+ Cypress.platform === "darwin" ? "Meta" : "Control",+ "Enter",+ ]);+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("SQL transform", ["Setup", "Graph", "SQL transform"]);+ });+ });++ it("should not show Python transform preview section in workspace", () => {+ H.setPythonRunnerSettings();+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Open Python transform in workspace");+ Workspaces.getMainlandTransforms().findByText("Python transform").click();++ cy.log("Inline preview section (Results/Output) should not be displayed");+ cy.findByTestId("python-results").should("not.exist");++ cy.log("Python editor should fill available height (no white box)");+ const MIN_EDITOR_HEIGHT = 590;+ cy.findByTestId("python-editor").should(($editor) => {+ const height = $editor.height();+ expect(height).to.be.at.least(+ MIN_EDITOR_HEIGHT,+ `Editor height should be at least ${MIN_EDITOR_HEIGHT}px, got ${height}`,+ );+ });+ });++ it(+ "should show ad-hoc results for Python transform",+ { tags: ["@python"] },+ () => {+ H.setPythonRunnerSettings();+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Run ad-hoc Python query");+ Workspaces.getMainlandTransforms()+ .findByText("Python transform")+ .click();++ Workspaces.getSaveTransformButton().click();+ cy.findByLabelText("Get Answer").click();++ Workspaces.getWorkspaceTabs().within(() => {+ H.tabsShouldBe("Preview (Python transform)", [+ "Setup",+ "Graph",+ "Python transform",+ "Preview (Python transform)",+ ]);+ cy.findByText("Preview (Python transform)").click();+ });+ H.assertTableData({+ columns: ["foo"],+ firstRows: [["42"]],+ });+ },+ );++ it("should show ad-hoc error for SQL transform", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Run invalid ad-hoc SQL query");+ Workspaces.getMainlandTransforms().findByText("SQL transform").click();++ H.NativeEditor.type(" INVALID SYNTAX");+ Workspaces.getSaveTransformButton().click();+ cy.findByLabelText("Get Answer").click();++ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("Preview (SQL transform)", [+ "Setup",+ "Graph",+ "SQL transform",+ "Preview (SQL transform)",+ ]);+ cy.findByText("Preview (SQL transform)").click();+ cy.findByText(/Error loading data/i).should("be.visible");+ });+ });++ it(+ "should show ad-hoc error for Python transform",+ { tags: ["@python"] },+ () => {+ H.setPythonRunnerSettings();+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Run invalid ad-hoc Python query");+ Workspaces.getMainlandTransforms()+ .findByText("Python transform")+ .click();++ H.PythonEditor.clear().paste("invalid python syntax !!!");+ Workspaces.getSaveTransformButton().click();+ cy.findByLabelText("Get Answer").click();++ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("Preview (Python transform)", [+ "Setup",+ "Graph",+ "Python transform",+ "Preview (Python transform)",+ "Results",+ "Output",+ ]);+ cy.findByText("Preview (Python transform)").click();+ cy.findByText(/Transform preview failed/i).should("be.visible");+ });+ },+ );+ });++ describe("run all transforms", () => {+ it("should run all transforms", { tags: ["@python"] }, () => {+ H.setPythonRunnerSettings();+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ Workspaces.getRunAllTransformsButton().should("be.disabled");++ Workspaces.getMainlandTransforms().findByText("SQL transform").click();+ cy.log("Verify native editor is loaded");+ H.NativeEditor.get().should("contain.text", "SELECT");+ Workspaces.getSaveTransformButton().click();+ cy.log("Verify sql transform is saved to the workspace");+ Workspaces.getWorkspaceTransforms().should(+ "contain.text",+ "SQL transform",+ );++ Workspaces.getMainlandTransforms().findByText("Python transform").click();+ cy.log("Verify Python editor is loaded");+ H.PythonEditor.get().should("contain.text", "import");+ Workspaces.getSaveTransformButton().click();+ cy.log("Verify python transform is saved to the workspace");+ Workspaces.getWorkspaceTransforms().should(+ "contain.text",+ "Python transform",+ );++ Workspaces.getRunAllTransformsButton().should("be.enabled").click();++ H.popover().within(() => {+ cy.findByText("Run stale transforms").should("be.visible");+ cy.findByText("Run all transforms").should("be.visible").click();+ });++ Workspaces.getRunAllTransformsButton().should(+ "not.have.attr",+ "data-loading",+ );+ verifyAndCloseToast("Transforms ran successfully");++ Workspaces.getWorkspaceContent()+ .findByText("Last ran a few seconds ago successfully.")+ .should("be.visible");++ cy.findByRole("tab", { name: "SQL transform" }).click();++ Workspaces.getWorkspaceContent()+ .findByText("Last ran a few seconds ago successfully.")+ .should("be.visible");++ Workspaces.openDataTab();+ Workspaces.getWorkspaceSidebar()+ .findByText(`${TARGET_SCHEMA}.${TARGET_TABLE_SQL}`)+ .realHover();+ H.tooltip().should("not.exist");++ Workspaces.getWorkspaceSidebar()+ .findByText(`${TARGET_SCHEMA}.${TARGET_TABLE_PYTHON}`)+ .realHover();+ H.tooltip().should("not.exist");+ });++ it("should run all stale transforms", { tags: ["@python"] }, () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ Workspaces.openCodeTab();++ Workspaces.getMainlandTransforms().findByText("SQL transform").click();+ Workspaces.getSaveTransformButton().click();++ Workspaces.getMainlandTransforms().findByText("Python transform").click();+ Workspaces.getSaveTransformButton().click();++ Workspaces.getRunTransformButton().click();+ Workspaces.getWorkspaceContent()+ .findByText("Last ran a few seconds ago successfully.")+ .should("be.visible");++ Workspaces.openDataTab();+ Workspaces.getWorkspaceSidebar()+ .findByText(`${TARGET_SCHEMA}.${TARGET_TABLE_PYTHON}`)+ .realHover();+ H.tooltip().should("not.exist");++ Workspaces.getRunAllTransformsButton().should("be.enabled").click();++ H.popover().within(() => {+ cy.findByText("Run all transforms").should("be.visible");+ cy.findByText("Run stale transforms").should("be.visible").click();+ });++ Workspaces.getRunAllTransformsButton().should(+ "have.attr",+ "data-loading",+ "true",+ );+ cy.wait<WorkspaceRunResponse, WorkspaceRunResponse>(+ "@runTransforms",+ ).then(({ response }) => {+ cy.log("only 1 stale transform has been executed");+ expect(response?.body.succeeded).to.be.an("array").with.length(1);+ });+ Workspaces.getRunAllTransformsButton().should(+ "not.have.attr",+ "data-loading",+ );+ verifyAndCloseToast("Transforms ran successfully");++ cy.findByRole("tab", { name: "SQL transform" }).click();++ Workspaces.getWorkspaceContent()+ .findByText("Last ran a few seconds ago successfully.")+ .should("be.visible");++ Workspaces.getWorkspaceSidebar()+ .findByText(`${TARGET_SCHEMA}.${TARGET_TABLE_SQL}`)+ .realHover();+ H.tooltip().should("not.exist");+ });+ });++ describe("transform -> workspace > ", () => {+ it("should check out transform into a new workspace from the transform page", () => {+ cy.log("Create 2 workspaces, add transform to the second one");+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();+ registerWorkspaceAliasName("workspaceA");+ createWorkspace();+ registerWorkspaceAliasName("workspaceB");++ Workspaces.getMainlandTransforms()+ .findByText("SQL transform")+ .should("be.visible")+ .click();++ Workspaces.getWorkspaceContent().within(() => {+ H.NativeEditor.type(" LIMIT 2");+ });+ Workspaces.getSaveTransformButton().click();++ cy.log(+ "Check that all workspaces are available and sorted by checked status",+ );+ cy.visit("/data-studio/transforms");+ Transforms.list()+ .findByRole("row", { name: /SQL transform/ })+ .click();++ cy.findByRole("button", { name: /Edit/ }).click();++ cy.get<string>("@workspaceA").then((workspaceA) => {+ cy.get<string>("@workspaceB").then((workspaceB) => {+ H.popover().within(() => {+ cy.findAllByRole("menuitem").eq(0).contains("Edit definition");+ cy.findAllByRole("menuitem").eq(1).contains("New workspace");+ cy.findAllByRole("menuitem").eq(2).contains(workspaceB);+ cy.log("Check that edit redirects to correct workspace");+ cy.findAllByRole("menuitem").eq(3).contains(workspaceA).click();+ });+ });+ });++ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("SQL transform", ["Setup", "Graph", "SQL transform"]);+ });+ Workspaces.getWorkspaceTransforms()+ .findByText("SQL transform")+ .should("not.exist");+ });++ it("should open checked out transform in existing workspace from the transform page", () => {+ // @flaky+ cy.log("Create 2 workspaces, add transform to the second one");+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();+ registerWorkspaceAliasName("workspaceA");+ createWorkspace();+ registerWorkspaceAliasName("workspaceB");++ Workspaces.getMainlandTransforms()+ .findByText("SQL transform")+ .should("be.visible")+ .click();++ Workspaces.getWorkspaceContent().within(() => {+ H.NativeEditor.type(" LIMIT 2");+ });+ Workspaces.getSaveTransformButton().click();++ cy.visit("/data-studio/transforms");+ Transforms.list()+ .findByRole("row", { name: /SQL transform/ })+ .click();++ cy.findByRole("button", { name: /Edit/ }).click();+ cy.get<string>("@workspaceA").then((workspaceA) => {+ cy.get<string>("@workspaceB").then((workspaceB) => {+ H.popover().within(() => {+ cy.findAllByRole("menuitem").eq(1).contains("New workspace");+ cy.findAllByRole("menuitem").eq(3).contains(workspaceA);+ cy.log("Edit transform in a workspace where it's been checked out");+ cy.findAllByRole("menuitem").eq(2).contains(workspaceB).click();+ });+ });+ });++ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("SQL transform", ["Setup", "Graph", "SQL transform"]);+ });+ H.NativeEditor.value().should("contain", " LIMIT 2");+ Workspaces.getWorkspaceTransforms().findByText("SQL transform").click();+ cy.log(+ "Tabs state should stay the same, because this transform is already checked and its tab should be opened after redirect",+ );+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("SQL transform", ["Setup", "Graph", "SQL transform"]);+ });+ });++ it("should not allow to checkout transform if checkout_disabled is received", () => {+ cy.intercept("GET", "/api/ee/workspace/checkout*").as(+ "checkoutWorkspace",+ );++ H.createModelFromTableName({+ tableName: "Animals",+ modelName: "Animals",+ idAlias: "modelId",+ });++ cy.log("Create transform via UI with model reference");+ Workspaces.visitTransformListPage();+ cy.button("Create a transform").click();+ H.popover().findByText("SQL query").click();+ H.popover().findByText("Writable Postgres12").click();++ cy.get("@modelId").then((modelId) => {+ H.NativeEditor.type(`SELECT * FROM {{#${modelId}-animals}} as t;`);+ });++ H.DataStudio.Transforms.saveChangesButton().click();++ H.modal().within(() => {+ cy.findByLabelText("Name").clear().type("Model Reference Transform");+ cy.findByLabelText("Table name").clear().type("model_ref_transform");+ cy.button("Save").click();+ });++ // TODO (Kamil 2026-02-02) -- the tooltip assertions are flaky https://linear.app/metabase/issue/GDGT-1620+ // cy.log("Verify Edit transform button is disabled");+ // cy.findByRole("button", { name: /Edit/ }).click();+ // cy.wait("@checkoutWorkspace");+ // H.popover().contains("New workspace").should("be.disabled");+ // H.popover().contains("New workspace").realHover();+ // H.tooltip().should(+ // "contain.text",+ // "Transforms referencing other questions cannot be edited in a workspace.",+ // );+ // cy.log("Close tooltip");+ // cy.get("body").click();++ cy.log("Edit transform to remove model reference");+ Transforms.clickEditDefinition();+ H.NativeEditor.type(+ '{selectall}SELECT * FROM "Schema A"."Animals" as t;',+ );+ Transforms.saveChangesButton().click();++ cy.log("Verify Edit transform button is now enabled");+ cy.findByRole("button", { name: /Edit/ }).should("be.enabled");+ });++ // unskip once GDGT-1564 is unblocked+ it.skip("should not allow workspace checkout when workspaces are disabled on DB (blocked by backend)", () => {+ createSqlTransform({+ sourceQuery: `SELECT * FROM "${TARGET_SCHEMA}"."${SOURCE_TABLE}"`,+ visitTransform: false,+ });++ cy.log("Disable workspaces on the database");+ cy.request("PUT", `/api/database/${WRITABLE_DB_ID}`, {+ settings: { "database-enable-workspaces": false },+ });++ cy.get<TransformId>("@transformId").then((transformId) => {+ H.visitTransform(transformId);+ });++ H.DataStudio.Transforms.editDefinitionButton().click();++ cy.log("Verify workspace checkout is disabled");+ // The backend should return checkout_disabled with a reason like "database-workspaces-disabled"+ // Frontend maps this to a user-friendly message via getCheckoutDisabledMessage()+ H.popover().contains("New workspace").should("be.disabled");+ H.popover().contains("New workspace").realHover();+ // TODO (Uladzimir 2026-01-30) -- update tooltip text once backend is done+ H.tooltip().should("contain.text", "cannot be edited in a workspace");+ });+ });++ describe("merge workspace", () => {+ it("shows target table diff and input tables diff", () => {+ H.getTableId({+ name: "Animals",+ databaseId: WRITABLE_DB_ID,+ schema: "Schema A",+ }).then((id1) => {+ H.getTableId({+ name: "Animals",+ databaseId: WRITABLE_DB_ID,+ schema: "Schema B",+ }).then((id2) => {+ createPythonTransform({+ body: TEST_PYTHON_TRANSFORM,+ sourceTables: { foo: id1, bar: id2 },+ visitTransform: true,+ });+ });+ });++ cy.findByRole("button", { name: /Edit/ }).click();+ H.popover().findByText("New workspace").click();++ cy.location("pathname").should("match", /data-studio\/workspaces\/\d+/);++ cy.log("rename 1st table's name");+ cy.findAllByLabelText("Reset alias to default")+ .should("have.length", 2)+ .eq(0)+ .click();++ cy.log("change 2nd table");+ cy.findByTestId("python-data-picker")+ .findAllByText("Animals")+ .should("have.length", 2)+ .eq(1)+ .click();+ H.entityPickerModal().within(() => {+ cy.findByText("Schema C").click();+ cy.findByText("Animals").click();+ });++ cy.log("add 3rd table");+ cy.button(/Add a table/).click();+ cy.findByTestId("python-data-picker")+ .findByText("Select a table…")+ .click();+ H.entityPickerModal().within(() => {+ cy.findByText("Schema D").click();+ cy.findByText("Animals").click();+ });++ Workspaces.getSaveTransformButton().click();++ cy.log("change target table");+ cy.button(/Change target/).click();+ H.modal().within(() => {+ cy.findByLabelText("Schema").clear().type("new_schema");+ cy.findByLabelText("New table name").clear().type("epic_table");+ cy.button("Change target").click();+ });+ verifyAndCloseToast("Transform target updated");++ cy.button("Merge").click();+ H.modal().within(() => {+ cy.findByText("Python transform").click();++ Workspaces.getTransformTargetDiff().within(() => {+ cy.findByText("Transform target").should("be.visible");+ verifyNormalText("Schema A");+ verifyRemovedText("transform_table_3");+ verifyAddedText("epic_table");+ });++ Workspaces.getSourceTablesDiff().within(() => {+ cy.findByText("Source tables").should("be.visible");++ cy.log("1st table");+ verifyRemovedText("foo");+ verifyAddedText("animals");+ verifyNormalText("Schema A");+ cy.findAllByText("Animals")+ .should("have.length", 3)+ .eq(0)+ .should("have.css", "text-decoration-line", "none")+ .and("have.css", "color", COLOR_TEXT);++ cy.log("2nd table");+ verifyNormalText("bar");+ verifyRemovedText("Schema B");+ verifyAddedText("Schema C");+ cy.findAllByText("Animals")+ .should("have.length", 3)+ .eq(1)+ .should("have.css", "text-decoration-line", "none")+ .and("have.css", "color", COLOR_TEXT);++ cy.log("3rd table");+ verifyAddedText("animals_1");+ verifyAddedText("Schema D");+ cy.findAllByText("Animals")+ .should("have.length", 3)+ .eq(2)+ .should("have.css", "text-decoration-line", "none")+ .and("have.css", "color", COLOR_SUCCESS);+ });+ });++ cy.realPress("Escape");++ cy.findAllByLabelText("Remove this table")+ .should("have.length", 3)+ .last()+ .click();+ cy.findAllByLabelText("Remove this table")+ .should("have.length", 2)+ .last()+ .click();+ Workspaces.getSaveTransformButton().click();++ cy.button("Merge").click();+ H.modal().within(() => {+ cy.findByText("Python transform").click();++ Workspaces.getSourceTablesDiff().within(() => {+ cy.findByText("Source tables").should("be.visible");++ cy.log("2nd table - removed");+ verifyRemovedText("bar");+ verifyRemovedText("Schema B");+ cy.findAllByText("Animals")+ .should("have.length", 2)+ .eq(1)+ .should("have.css", "text-decoration-line", "line-through")+ .and("have.css", "color", COLOR_DANGER);+ });+ });+ });++ it(+ "should be able to check out existing Python transform with multiple input tables into a new workspace",+ { tags: ["@python"] },+ () => {+ H.setPythonRunnerSettings();+ cy.log("Create Python transform with 2 input tables");+ H.getTableId({+ name: "Animals",+ databaseId: WRITABLE_DB_ID,+ schema: "Schema A",+ }).then((id1) => {+ H.getTableId({+ name: "Animals",+ databaseId: WRITABLE_DB_ID,+ schema: "Schema B",+ }).then((id2) => {+ createPythonTransform({+ body: TEST_PYTHON_TRANSFORM_MULTI_TABLE,+ sourceTables: { animals_a: id1, animals_b: id2 },+ visitTransform: true,+ });+ });+ });++ cy.log("Create a workspace and check out the transform");+ cy.findByRole("button", { name: /Edit/ }).click();+ H.popover().findByText("New workspace").click();++ cy.location("pathname").should("match", /data-studio\/workspaces\/\d+/);++ cy.log("Update the Python transform code");+ H.PythonEditor.clear().paste(TEST_PYTHON_TRANSFORM_MULTI_TABLE_UPDATED);+ Workspaces.getSaveTransformButton().click();++ cy.log("Run the transform");+ Workspaces.getRunTransformButton().click();+ Workspaces.getRunTransformButton().should(+ "have.text",+ "Ran successfully",+ );++ cy.log("Merge the workspace");+ Workspaces.getMergeWorkspaceButton().click();+ Workspaces.getMergeCommitInput().type("Merge Python transform");+ H.modal().within(() => {+ cy.findByText("1 transform will be merged").should("exist");+ cy.findByRole("button", { name: /Merge/ }).click();+ });+ verifyAndCloseToast("merged successfully");++ cy.log("Verify the merged transform runs correctly");+ Transforms.list()+ .findByRole("row", { name: /Python transform/ })+ .click();+ Transforms.runTab().click();+ runTransformAndWaitForSuccessOnTransformsPage();+ Transforms.settingsTab().click();+ getTableLink().should("contain.text", TARGET_TABLE_PYTHON).click();++ H.assertTableData({+ columns: ["Name", "Total Score"],+ firstRows: [+ ["Duck", "20"],+ ["Horse", "40"],+ ["Cow", "60"],+ ],+ });+ },+ );++ it(+ "should be able to create new SQL and Python transforms in a workspace and merge them to global list",+ { tags: ["@python"] },+ () => {+ H.setPythonRunnerSettings();+ cy.log("Create a new workspace");+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Create a new SQL transform");+ Workspaces.getWorkspaceSidebar()+ .findByLabelText("Add transform")+ .click();+ H.popover()+ .findByRole("menuitem", { name: /SQL Transform/ })+ .click();++ H.NativeEditor.type(+ 'SELECT * FROM "Schema A"."Animals" WHERE name = \'Duck\';',+ );+ Workspaces.getSaveTransformButton().click();+ H.modal().within(() => {+ cy.findByLabelText(/Table name/)+ .clear()+ .type("sql_table");+ cy.findByLabelText("Schema").click();+ cy.document()+ .findByRole("option", { name: /Schema A/ })+ .click();+ cy.findByRole("button", { name: /Save/ }).click();+ });+ verifyAndCloseToast("Transform saved successfully");++ cy.log("Create a new Python transform with 2 input tables");+ Workspaces.getWorkspaceSidebar()+ .findByLabelText("Add transform")+ .click();+ H.popover()+ .findByRole("menuitem", { name: /Python Script/ })+ .click();++ H.PythonEditor.clear().paste(TEST_PYTHON_TRANSFORM_MULTI_TABLE);+ Workspaces.getWorkspaceContent().within(() => {+ cy.findByTestId("python-data-picker")+ .findAllByText("Select a table…")+ .first()+ .click();+ });+ H.entityPickerModal().within(() => {+ cy.findByText("Schema A").click();+ cy.findByText("Animals").click();+ });+ Workspaces.getWorkspaceContent().within(() => {+ cy.button(/Add a table/).click();+ cy.findByTestId("python-data-picker")+ .findByText("Select a table…")+ .click();+ });+ H.entityPickerModal().within(() => {+ cy.findByText("Schema B").click();+ cy.findByText("Animals").click();+ });++ Workspaces.getSaveTransformButton().click();+ H.modal().within(() => {+ cy.findByLabelText(/Table name/)+ .clear()+ .type("python_table");+ cy.findByLabelText("Schema").click();+ cy.document()+ .findByRole("option", { name: /Schema A/ })+ .click();+ cy.findByRole("button", { name: /Save/ }).click();+ });+ verifyAndCloseToast("Transform saved successfully");++ cy.log("Merge the workspace");+ Workspaces.getMergeWorkspaceButton().click();+ Workspaces.getMergeCommitInput().type("Merge new transforms");+ H.modal().within(() => {+ cy.findByText("2 transforms will be merged").should("exist");+ cy.findByRole("button", { name: /Merge/ }).click();+ });+ verifyAndCloseToast("merged successfully");++ cy.log("Verify both transforms appear in global transforms list");+ Transforms.list()+ .findByRole("row", { name: /sql_table/ })+ .should("exist");+ Transforms.list()+ .findByRole("row", { name: /python_table/ })+ .should("exist");++ cy.log("Run SQL transform and verify results");+ Transforms.list()+ .findByRole("row", { name: /sql_table/ })+ .click();+ Transforms.runTab().click();+ runTransformAndWaitForSuccessOnTransformsPage();+ Transforms.settingsTab().click();+ getTableLink().should("contain.text", "sql_table").click();++ H.assertTableData({+ columns: ["Name", "Score"],+ firstRows: [["Duck", "10"]],+ });++ cy.log("Run Python transform and verify results");+ Workspaces.visitTransformListPage();+ Transforms.list()+ .findByRole("row", { name: /python_table/ })+ .click();+ Transforms.runTab().click();+ runTransformAndWaitForSuccessOnTransformsPage();+ Transforms.settingsTab().click();+ getTableLink().should("contain.text", "python_table").click();++ H.assertTableData({+ columns: ["Name", "Total Score"],+ firstRows: [["test", "0"]],+ });+ },+ );++ it("should preserve changed target table after merging workspace", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Check out existing SQL transform");+ Workspaces.getMainlandTransforms().findByText("SQL transform").click();+ Workspaces.getSaveTransformButton().click();++ cy.log("Change target table settings");+ Workspaces.getTransformTargetButton().click();+ H.modal().within(() => {+ cy.findByLabelText("Schema").should("have.value", "Schema A");+ cy.findByLabelText("New table name").clear().type("renamed_table");+ cy.findByRole("button", { name: /Change target/ }).click();+ });+ verifyAndCloseToast("Transform target updated");++ cy.log("Merge the workspace");+ Workspaces.getMergeWorkspaceButton().click();+ Workspaces.getMergeCommitInput().type("Rename target table");+ H.modal().within(() => {+ cy.findByText("1 transform will be merged").should("exist");++ cy.log("Verify target table diff is displayed");+ cy.findByText("SQL transform").click();+ Workspaces.getTransformTargetDiff().within(() => {+ verifyRemovedText("transform_table_2");+ verifyAddedText("renamed_table");+ });++ cy.findByRole("button", { name: /Merge/ }).click();+ });+ verifyAndCloseToast("merged successfully");++ cy.log("Verify target table is updated in global transform");+ Transforms.list()+ .findByRole("row", { name: /SQL transform/ })+ .click();+ Transforms.settingsTab().click();+ getTableLink({ isActive: false }).should("contain.text", "renamed_table");+ });++ it("should merge a chain of 3 dependent transforms created in workspace", () => {+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Create Transform 1: Base transform selecting from Animals");+ Workspaces.getWorkspaceSidebar().findByLabelText("Add transform").click();+ H.popover()+ .findByRole("menuitem", { name: /SQL Transform/ })+ .click();++ H.NativeEditor.type('SELECT name, score FROM "Schema A"."Animals";');+ Workspaces.getSaveTransformButton().click();+ H.modal().within(() => {+ cy.findByLabelText(/Table name/)+ .clear()+ .type("chain_step_1");+ cy.findByLabelText("Schema").click();+ cy.document()+ .findByRole("option", { name: /Schema A/ })+ .click();+ cy.findByRole("button", { name: /Save/ }).click();+ });+ verifyAndCloseToast("Transform saved successfully");++ cy.log("Run Transform 1 to create the output table");+ Workspaces.getRunTransformButton().click();+ Workspaces.getRunTransformButton().should(+ "have.text",+ "Ran successfully",+ );++ cy.log("Create Transform 2: Depends on Transform 1");+ Workspaces.getWorkspaceSidebar().findByLabelText("Add transform").click();+ H.popover()+ .findByRole("menuitem", { name: /SQL Transform/ })+ .click();++ H.NativeEditor.type(+ 'SELECT name, score * 2 as score FROM "Schema A"."chain_step_1";',+ );+ Workspaces.getSaveTransformButton().click();+ H.modal().within(() => {+ cy.findByLabelText(/Table name/)+ .clear()+ .type("chain_step_2");+ cy.findByLabelText("Schema").click();+ cy.document()+ .findByRole("option", { name: /Schema A/ })+ .click();+ cy.findByRole("button", { name: /Save/ }).click();+ });+ verifyAndCloseToast("Transform saved successfully");++ cy.log("Run Transform 2");+ Workspaces.getRunTransformButton().click();+ Workspaces.getRunTransformButton().should(+ "have.text",+ "Ran successfully",+ );++ cy.log("Create Transform 3: Depends on Transform 2");+ Workspaces.getWorkspaceSidebar().findByLabelText("Add transform").click();+ H.popover()+ .findByRole("menuitem", { name: /SQL Transform/ })+ .click();++ H.NativeEditor.type(+ 'SELECT name, score * 2 as score FROM "Schema A"."chain_step_2";',+ );+ Workspaces.getSaveTransformButton().click();+ H.modal().within(() => {+ cy.findByLabelText(/Table name/)+ .clear()+ .type("chain_step_3");+ cy.findByLabelText("Schema").click();+ cy.document()+ .findByRole("option", { name: /Schema A/ })+ .click();+ cy.findByRole("button", { name: /Save/ }).click();+ });+ verifyAndCloseToast("Transform saved successfully");++ cy.log("Run Transform 3");+ Workspaces.getRunTransformButton().click();+ Workspaces.getRunTransformButton().should(+ "have.text",+ "Ran successfully",+ );++ cy.log("Merge the workspace with all 3 transforms");+ Workspaces.getMergeWorkspaceButton().click();+ Workspaces.getMergeCommitInput().type("Merge transform chain");+ H.modal().within(() => {+ cy.findByText("3 transforms will be merged").should("exist");+ cy.findByRole("button", { name: /Merge/ }).click();+ });+ verifyAndCloseToast("merged successfully");++ cy.log("Run chain_step_1 (first in chain)");+ Transforms.list()+ .findByRole("row", { name: /chain_step_1/ })+ .click();+ Transforms.runTab().click();+ runTransformAndWaitForSuccessOnTransformsPage();++ DataStudio.nav().findByRole("link", { name: "Transforms" }).click();++ cy.log("Run chain_step_2 (second in chain)");+ Transforms.list()+ .findByRole("row", { name: /chain_step_2/ })+ .click();+ Transforms.runTab().click();+ runTransformAndWaitForSuccessOnTransformsPage();++ DataStudio.nav().findByRole("link", { name: "Transforms" }).click();++ cy.log("Run chain_step_3 (final in chain) and verify results");+ Transforms.list()+ .findByRole("row", { name: /chain_step_3/ })+ .click();+ Transforms.runTab().click();+ runTransformAndWaitForSuccessOnTransformsPage();+ Transforms.settingsTab().click();+ getTableLink().should("contain.text", "chain_step_3").click();++ // Original scores: Duck=10, Horse=20, Cow=30+ // After 2 doublings (x2 x2 = x4): Duck=40, Horse=80, Cow=120+ H.assertTableData({+ columns: ["Name", "Score"],+ firstRows: [+ ["Duck", "40"],+ ["Horse", "80"],+ ["Cow", "120"],+ ],+ });+ });+ });++ describe("repros > ", () => {+ it("should not show error when editing a new transform in a workspace (GDGT-1445)", () => {+ Workspaces.visitTransformListPage();+ cy.findByLabelText("Create a transform").click();+ H.popover().findByText("SQL query").click();+ // to avoid flakiness on editor's focus+ cy.findByTestId("native-query-top-bar").should(+ "contain.text",+ "Writable Postgres12",+ );+ NativeEditor.type("select 1");+ cy.button("Save").click();+ cy.findByPlaceholderText("My Great Transform").type("My transform");+ H.modal().button("Save").click();++ cy.button(/Edit/).click();+ H.popover().findByText("New workspace").click();+ H.undoToast().should("not.exist");+ Workspaces.getWorkspaceTabs().within(() => {+ H.tabsShouldBe("My transform", ["Setup", "Graph", "My transform"]);+ });+ });++ it("should detect changes after closing and reopening a transform tab (GDGT-1535, GDGT-1551)", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Open transform and make edits");+ Workspaces.getMainlandTransforms().findByText("SQL transform").click();+ H.NativeEditor.type(" LIMIT 2");++ cy.log("Save the transform to workspace");+ Workspaces.getSaveTransformButton().click();+ Workspaces.getSaveTransformButton().should("be.disabled");++ cy.log("Make additional edits");+ H.NativeEditor.type(";");+ Workspaces.getSaveTransformButton().should("be.enabled");++ cy.log("Close the transform tab");+ Workspaces.getWorkspaceContent().within(() => {+ cy.findByRole("tab", { name: "SQL transform" })+ .findByLabelText("close icon")+ .click();+ });++ cy.log("Reopen the transform tab by clicking in Code tab");+ Workspaces.getWorkspaceTransforms().findByText("SQL transform").click();++ cy.log("Verify Save button is enabled (changes should be detected)");+ Workspaces.getSaveTransformButton().should("be.enabled");++ cy.log("Verify the edited content is preserved");+ H.NativeEditor.value().should("contain", "LIMIT 2;");++ cy.log("Create a new unsaved transform in the workspace");+ Workspaces.getWorkspaceSidebar().findByLabelText("Add transform").click();+ H.popover()+ .findByRole("menuitem", { name: /SQL Transform/ })+ .click();++ cy.log("Verify the unsaved transform tab is opened");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("New transform", [+ "Setup",+ "Graph",+ "SQL transform",+ "New transform",+ ]);+ });++ cy.log("Close the unsaved transform tab");+ Workspaces.getWorkspaceContent().within(() => {+ cy.findByRole("tab", { name: "New transform" })+ .findByLabelText("close icon")+ .click();+ });++ cy.log("Reopen the unsaved transform tab by clicking in Code tab");+ Workspaces.getWorkspaceTransforms().findByText("New transform").click();++ cy.log("Verify the unsaved transform tab is reopened");+ Workspaces.getWorkspaceContent().within(() => {+ H.tabsShouldBe("New transform", [+ "Setup",+ "Graph",+ "SQL transform",+ "New transform",+ ]);+ });+ });++ it("should show empty state after creating, archiving, and deleting a workspace", () => {+ Workspaces.visitWorkspaces();++ // Create a new workspace+ createWorkspace();+ registerWorkspaceAliasName("workspaceToDelete");++ // Archive the workspace immediately+ cy.get<string>("@workspaceToDelete").then((workspaceName) => {+ Workspaces.getWorkspaceItemActions(workspaceName).click();+ H.popover().findByText("Archive").click();+ verifyAndCloseToast("Workspace archived successfully");++ // Verify it's archived+ Workspaces.getWorkspaceItem(workspaceName).should(+ "contain.text",+ "Archived",+ );++ // Delete the archived workspace+ Workspaces.getWorkspaceItemActions(workspaceName).click();+ H.popover().findByText("Delete").click();+ H.modal().findByText("Delete").click();+ verifyAndCloseToast("Workspace deleted successfully");++ // Verify workspace is gone+ Workspaces.getWorkspaceItem(workspaceName).should("not.exist");+ });++ // Verify empty state is shown again+ Workspaces.getWorkspacesPage()+ .findByText("No active workspaces")+ .should("be.visible");+ Workspaces.getWorkspacesSection()+ .findByText("No workspaces yet")+ .should("be.visible");+ });++ it("should show correct run status when switching tabs (GDGT-1571)", () => {+ createTransforms();+ Workspaces.visitWorkspaces();+ createWorkspace();++ cy.log("Create first SQL transform that will succeed");+ Workspaces.getMainlandTransforms().findByText("SQL transform").click();+ H.NativeEditor.type(" LIMIT 1;");+ Workspaces.getSaveTransformButton().click();++ cy.log("Run the successful transform");+ Workspaces.getRunTransformButton().click();+ Workspaces.getRunTransformButton().should(+ "have.text",+ "Ran successfully",+ );++ cy.log("Create second SQL transform that will fail");+ Workspaces.getWorkspaceSidebar().findByLabelText("Add transform").click();+ H.popover()+ .findByRole("menuitem", { name: /SQL Transform/ })+ .click();+ H.NativeEditor.type("INVALID SQL QUERY");+ H.NativeEditor.rejectCompletion();+ Workspaces.getSaveTransformButton().click();++ H.modal().within(() => {+ cy.findByLabelText("Name").clear().type("Failing transform");+ cy.findByText("Save").click();+ });++ cy.log("Run the failing transform");+ Workspaces.getRunTransformButton().click();+ Workspaces.getRunTransformButton().should("have.text", "Run failed");+ Workspaces.getRunStatus().should("contain.text", "Last run failed");++ cy.log("Switch to the first transform tab");+ Workspaces.getWorkspaceContent().within(() => {+ cy.findByRole("tab", { name: "SQL transform" }).click();+ });+ Workspaces.getRunStatus().should(+ "have.text",+ "Last ran a few seconds ago successfully.",+ );+ });+ });+});++function enableWorkspacesInDb(id: DatabaseId) {+ cy.request("POST", `/api/database/${id}/permission/workspace/check`, {+ cached: false,+ });+ cy.request("PUT", `/api/database/${id}`, {+ settings: { "database-enable-workspaces": true },+ });+}++function createWorkspace() {+ Workspaces.getNewWorkspaceButton().click();+}++const TEST_PYTHON_TRANSFORM = dedent`+ import pandas as pd++ def transform(foo):+ return pd.DataFrame([{"foo": 42 }])+`;++const TEST_PYTHON_TRANSFORM_MULTI_TABLE = dedent`+ import pandas as pd++ def transform(animals_a, animals_b):+ return pd.DataFrame([{"name": "test", "total_score": 0}])+`;++const TEST_PYTHON_TRANSFORM_MULTI_TABLE_UPDATED = dedent`+ import pandas as pd++ def transform(animals_a, animals_b):+ # Combine scores from both tables by name+ merged = animals_a.merge(animals_b, on="name", suffixes=("_a", "_b"))+ result = pd.DataFrame({+ "name": merged["name"],+ "total_score": merged["score_a"] + merged["score_b"]+ })+ return result+`;++function createTransforms({ visit }: { visit?: boolean } = { visit: false }) {+ createMbqlTransform({+ targetTable: TARGET_TABLE_MBQL,+ });++ H.getTableId({ name: "Animals", databaseId: WRITABLE_DB_ID }).then((id) => {+ createPythonTransform({+ body: TEST_PYTHON_TRANSFORM,+ sourceTables: { foo: id },+ });+ });++ createSqlTransform({+ sourceQuery: `SELECT * FROM "${TARGET_SCHEMA}"."${SOURCE_TABLE}"`,+ visitTransform: visit,+ });+}++function createMbqlTransform(+ opts: {+ sourceTable?: string;+ targetTable?: string;+ targetSchema?: string | null;+ tagIds?: TransformTagId[];+ name?: string;+ databaseId?: number;+ visitTransform?: boolean;+ } = {},+) {+ return H.createMbqlTransform({+ sourceTable: SOURCE_TABLE,+ targetTable: TARGET_TABLE_MBQL,+ targetSchema: TARGET_SCHEMA,+ name: "MBQL transform",+ ...opts,+ });+}++function createSqlTransform(opts: {+ sourceQuery: string;+ targetTable?: string;+ targetSchema?: string;+ tagIds?: TransformTagId[];+ visitTransform?: boolean;+}) {+ return H.createSqlTransform({+ targetTable: TARGET_TABLE_SQL,+ targetSchema: TARGET_SCHEMA,+ ...opts,+ });+}++function createPythonTransform(opts: {+ body: string;+ sourceTables: PythonTransformTableAliases;+ targetTable?: string;+ targetSchema?: string;+ tagIds?: TransformTagId[];+ visitTransform?: boolean;+}) {+ return H.createPythonTransform({+ targetTable: TARGET_TABLE_PYTHON,+ targetSchema: TARGET_SCHEMA,+ ...opts,+ });+}++function getTableLink({ isActive = true }: { isActive?: boolean } = {}) {+ return cy+ .findByTestId("table-link")+ .should("have.attr", "aria-disabled", String(!isActive));+}++function runTransformAndWaitForSuccess() {+ Workspaces.getRunTransformButton().click();+ Workspaces.getRunTransformButton().should("have.text", "Ran successfully");+}++function runTransformAndWaitForSuccessOnTransformsPage() {+ getRunButton().click();+ getRunButton().should("have.text", "Ran successfully");+}++function getRunButton(options: { timeout?: number } = {}) {+ return cy.findAllByTestId("run-button").eq(0, options);+}++function verifyAndCloseToast(message: string) {+ H.undoToast().should("contain.text", message);+ H.undoToast().icon("close").click({ force: true });+}++function registerWorkspaceAliasName(name: string) {+ cy.wait("@createWorkspace").then((interception) => {+ const workspaceName = interception.response?.body?.name;+ // Backend returns randomized name.+ cy.wrap(workspaceName).as(name);+ });+}++function verifyNormalText(text: string) {+ cy.findByText(text)+ .should("have.css", "text-decoration-line", "none")+ .and("have.css", "color", COLOR_TEXT);+}++function verifyAddedText(text: string) {+ cy.findByText(text)+ .should("have.css", "text-decoration-line", "none")+ .and("have.css", "color", COLOR_SUCCESS);+}++function verifyRemovedText(text: string) {+ cy.findByText(text)+ .should("have.css", "text-decoration-line", "line-through")+ .and("have.css", "color", COLOR_DANGER);+}diff --git a/e2e/test/scenarios/dependencies/dependency-checks.cy.spec.ts b/e2e/test/scenarios/dependencies/dependency-checks.cy.spec.tsindex ba4f913554b..2f195578032 100644--- a/e2e/test/scenarios/dependencies/dependency-checks.cy.spec.ts+++ b/e2e/test/scenarios/dependencies/dependency-checks.cy.spec.ts@@ -149,7 +149,7 @@ describe("scenarios > dependencies > dependency checks", () => {describe("transforms", () => {const goToEditorAndType = (queryString: string) => {- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();cy.url().should("include", "/edit");H.NativeEditor.clear().type(queryString);};@@ -179,7 +179,7 @@ describe("scenarios > dependencies > dependency checks", () => {cy.log("make breaking changes");cy.get<number>("@transformId").then(H.visitTransform);- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();H.getNotebookStep("data").findByLabelText("Pick columns").click();H.popover().findByLabelText("Score").click();@@ -206,7 +206,7 @@ describe("scenarios > dependencies > dependency checks", () => {it("should not show a confirmation if there are no breaking changes when updating a MBQL transform before it was run", () => {createMbqlTransformWithDependentMbqlTransforms();cy.get<number>("@transformId").then(H.visitTransform);- H.DataStudio.Transforms.editDefinition().click();+ H.DataStudio.Transforms.clickEditDefinition();H.getNotebookStep("data").button("Sort").click();H.popover().findByText("Score").click();H.DataStudio.Transforms.saveChangesButton().click();diff --git a/e2e/test/scenarios/metrics/metrics-editing.cy.spec.js b/e2e/test/scenarios/metrics/metrics-editing.cy.spec.jsindex d4715a3738e..a0374a22800 100644--- a/e2e/test/scenarios/metrics/metrics-editing.cy.spec.js+++ b/e2e/test/scenarios/metrics/metrics-editing.cy.spec.js@@ -386,12 +386,12 @@ describe("scenarios > metrics > editing", () => {it("should create a metric with a custom aggregation expression based on 1 metric", () => {H.createQuestion(ORDERS_SCALAR_METRIC);H.startNewMetric();- cy.intercept("POST", "/api/dataset/query_metadata").as("queryMetadata");+ cy.intercept("POST", "/api/dataset/query_metadata").as("metadata");H.miniPicker().within(() => {cy.findByText("Our analytics").click();cy.findByText(ORDERS_SCALAR_METRIC.name).click();});- cy.wait("@queryMetadata");+ cy.wait("@metadata");H.getNotebookStep("summarize").findByText(ORDERS_SCALAR_METRIC.name).click();diff --git a/e2e/test/scenarios/permissions/transforms-permissions.cy.spec.ts b/e2e/test/scenarios/permissions/transforms-permissions.cy.spec.tsindex cad7b22860c..a9d3f11c16e 100644--- a/e2e/test/scenarios/permissions/transforms-permissions.cy.spec.ts+++ b/e2e/test/scenarios/permissions/transforms-permissions.cy.spec.ts@@ -241,7 +241,7 @@ describe(H.DataStudio.Transforms.header().findByDisplayValue("Read Only Transform").should("be.disabled");- H.DataStudio.Transforms.editDefinition().should("not.exist");+ H.DataStudio.Transforms.editDefinitionButton().should("not.exist");});});});diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/api/application.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/api/application.cljindex 77614721624..69af811e250 100644--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/api/application.clj+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/api/application.clj@@ -1,5 +1,5 @@(ns metabase-enterprise.advanced-permissions.api.application- "`/advanced-permisisons/application` Routes.+ "`/advanced-permissions/application` Routes.Implements the Permissions routes needed for application permission - a class of permissions that control access tofeatures like access Setting pages, access monitoring tools ... etc"(:requirediff --git a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj b/enterprise/backend/src/metabase_enterprise/api_routes/routes.cljindex a27b22c6ea7..aa055513bd9 100644--- a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj+++ b/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj@@ -41,6 +41,7 @@[metabase-enterprise.transforms-python.api][metabase-enterprise.transforms.api][metabase-enterprise.upload-management.api]+ [metabase-enterprise.workspaces.api][metabase.api.macros :as api.macros][metabase.api.util.handlers :as handlers][metabase.util.i18n :refer [deferred-tru]]))@@ -76,8 +77,9 @@:upload-management (deferred-tru "Upload Management"):database-routing (deferred-tru "Database Routing"):cloud-custom-smtp (deferred-tru "Custom SMTP")- :support-users (deferred-tru "Support Users")- :transforms-python (deferred-tru "Transforms Python")})+ :support-users (deferred-tru "Support Users")+ :transforms-python (deferred-tru "Transforms Python")+ :workspaces (deferred-tru "Workspaces")})(defn- premium-handler [handler required-feature](let [handler (cond-> handler@@ -138,7 +140,8 @@"/transform-job" (premium-handler metabase-enterprise.transforms.api/transform-job-routes :transforms)"/transform-tag" (premium-handler metabase-enterprise.transforms.api/transform-tag-routes :transforms)"/tenant" (premium-handler metabase-enterprise.tenants.api/routes :tenants)- "/upload-management" (premium-handler metabase-enterprise.upload-management.api/routes :upload-management)})+ "/upload-management" (premium-handler metabase-enterprise.upload-management.api/routes :upload-management)+ "/workspace" (premium-handler metabase-enterprise.workspaces.api/routes :workspaces)});;; ↑↑↑ KEEP THIS SORTED OR ELSE ↑↑↑(def ^:private routes-mapdiff --git a/enterprise/backend/src/metabase_enterprise/core/init.clj b/enterprise/backend/src/metabase_enterprise/core/init.cljindex 7fd6a17bec4..41b616c3439 100644--- a/enterprise/backend/src/metabase_enterprise/core/init.clj+++ b/enterprise/backend/src/metabase_enterprise/core/init.clj@@ -22,4 +22,5 @@[metabase-enterprise.stale.init][metabase-enterprise.support-access-grants.init][metabase-enterprise.transforms-python.init]- [metabase-enterprise.transforms.init]))+ [metabase-enterprise.transforms.init]+ [metabase-enterprise.workspaces.init]))diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/calculation.clj b/enterprise/backend/src/metabase_enterprise/dependencies/calculation.cljindex 0dfbc75fb3d..6b2e7473f01 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/calculation.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/calculation.clj@@ -45,14 +45,10 @@param-card-ids)))(mu/defn upstream-deps:python-transform :- ::deps.schema/upstream-deps- "Given a Python Transform (in Toucan form), return its upstream dependencies."- [transform]- {:table (->> (get-in transform [:source :source-tables])- vals- (into #{}- (keep #(if (map? %)- (:table_id %)- %))))})+ "Given a Toucan `:model/Transform`, return its upstream dependencies as a map from the kind to a set of IDs."+ [{{tables :source-tables} :source :as _py-transform}+ :- [:map [:source-tables {:optional true} [:map-of :string [:or :int [:map [:table_id :int]]]]]]]+ {:table (into #{} (keep (fn [v] (if (map? v) (:table_id v) v))) (vals tables))})(mu/defn upstream-deps:transform :- ::deps.schema/upstream-deps"Given a Transform (in Toucan form), return its upstream dependencies."@@ -62,7 +58,8 @@[:query[:map [:query ::lib.schema/query]]][:python- [:map]]]]]]+ ;; If the upstream table doesn't exist yet, table_id will be nil+ [:map [:source-tables {:optional true} [:map-of :string [:or :int [:map [:table_id [:maybe :int]]]]]]]]]]]](let [source-type (transforms/transform-type transform)](case source-type:query (upstream-deps:query query)diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.cljindex f215ea54f85..1089be59da4 100644--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj@@ -127,4 +127,16 @@"User""UserKeyValue""UserParameterValue"- "ViewLog"])+ "ViewLog"+ "Workspace"+ ;; TODO (lbrdnk 2025-12-17) -- I've added rest of the workspace models. I believe some of those will be available for+ ;; serialization, hence we should revisit this later in the project.+ "WorkspaceInput"+ "WorkspaceInputExternal"+ "WorkspaceLog"+ "WorkspaceMerge"+ "WorkspaceMergeTransform"+ "WorkspaceGraph"+ "WorkspaceOutput"+ "WorkspaceOutputExternal"+ "WorkspaceTransform"])diff --git a/enterprise/backend/src/metabase_enterprise/transforms/api.clj b/enterprise/backend/src/metabase_enterprise/transforms/api.cljindex 06ffaba434a..e411fbfbb7c 100644--- a/enterprise/backend/src/metabase_enterprise/transforms/api.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms/api.clj@@ -127,7 +127,8 @@[:map[:last_run_start_time {:optional true} [:maybe ms/NonBlankString]][:last_run_statuses {:optional true} [:maybe (ms/QueryVectorOf [:enum "started" "succeeded" "failed" "timeout"])]]- [:tag_ids {:optional true} [:maybe (ms/QueryVectorOf ms/IntGreaterThanOrEqualToZero)]]]]+ [:tag_ids {:optional true} [:maybe (ms/QueryVectorOf ms/IntGreaterThanOrEqualToZero)]]+ [:database_id {:optional true} [:maybe ms/PositiveInt]]]](get-transforms query-params))(defn- extract-all-columns-from-query@@ -207,6 +208,32 @@checkpoint-filter(pr-str (:base_type column)))))))))))+(defn create-transform!+ "Create new transform in the appdb.+ Optionally accepts a creator-id to use instead of the current user (for workspace merges)."+ ([body]+ (create-transform! body nil))+ ([body creator-id]+ (let [creator-id (or creator-id api/*current-user-id*)+ transform (t2/with-transaction [_]+ (let [tag-ids (:tag_ids body)+ ;; Set owner_user_id to current user if not explicitly provided+ owner-user-id (when-not (:owner_email body)+ (or (:owner_user_id body) creator-id))+ transform (t2/insert-returning-instance!+ :model/Transform+ (assoc (select-keys body [:name :description :source :target :run_trigger+ :collection_id :owner_email])+ :creator_id creator-id+ :owner_user_id owner-user-id))]+ ;; Add tag associations if provided+ (when (seq tag-ids)+ (transform.model/update-transform-tags! (:id transform) tag-ids))+ ;; Return with hydrated tag_ids+ (t2/hydrate transform :transform_tag_ids :creator :owner)))]+ (events/publish-event! :event/transform-create {:object transform :user-id creator-id})+ transform)))+;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to;; use our API + we will need it when we make auto-TypeScript-signature generation happen;;@@ -233,25 +260,9 @@(api/check (not (transforms.util/target-table-exists? body))403(deferred-tru "A table with that name already exists."))- (let [transform (t2/with-transaction [_]- (let [tag-ids (:tag_ids body)- ;; Set owner_user_id to current user if not explicitly provided- owner-user-id (when-not (:owner_email body)- (or (:owner_user_id body) api/*current-user-id*))- transform (t2/insert-returning-instance!- :model/Transform- (assoc (select-keys body [:name :description :source :target :run_trigger :collection_id :owner_email])- :creator_id api/*current-user-id*- :owner_user_id owner-user-id))]- ;; Add tag associations if provided- (when (seq tag-ids)- (transform.model/update-transform-tags! (:id transform) tag-ids))- ;; Return with hydrated tag_ids- (t2/hydrate transform :transform_tag_ids :creator :owner)))]- (events/publish-event! :event/transform-create {:object transform :user-id api/*current-user-id*})- (-> transform- python-source-table-ref->table-id- transforms.util/add-source-readable)))+ (-> (create-transform! body)+ python-source-table-ref->table-id+ transforms.util/add-source-readable))(defn get-transform"Get a specific transform."@@ -292,6 +303,34 @@(mapv python-source-table-ref->table-id)transforms.util/add-source-readable)))+(def ^:private MergeHistoryEntry+ [:map+ [:id ms/PositiveInt]+ [:workspace_merge_id ms/PositiveInt]+ [:commit_message :string]+ [:workspace_id [:maybe ms/PositiveInt]]+ [:workspace_name :string]+ [:merging_user_id ms/PositiveInt]+ [:created_at :any]])++(api.macros/defendpoint :get "/:id/merge-history"+ :- [:sequential MergeHistoryEntry]+ "Get merge history for a transform. Returns all merge events that affected this transform,+ ordered by created_at descending (newest first)."+ [{:keys [id]} :- [:map [:id ms/PositiveInt]]]+ (api/check-superuser)+ (api/check-404 (t2/select-one :model/Transform id))+ (t2/select [:model/WorkspaceMergeTransform+ :id+ :workspace_merge_id+ :commit_message+ :workspace_id+ :workspace_name+ :merging_user_id+ :created_at]+ {:where [:= :transform_id id]+ :order-by [[:created_at :desc]]}))+;; TODO (Cam 10/28/25) -- fix this endpoint so it uses kebab-case for query parameters for consistency with the rest;; of the REST API;;@@ -319,26 +358,10 @@:limit (request/limit)))(update :data #(map transforms.util/localize-run-timestamps %))))-;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to-;; use our API + we will need it when we make auto-TypeScript-signature generation happen-;;-#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}-(api.macros/defendpoint :put "/:id"- "Update a transform."- [{:keys [id]} :- [:map- [:id ms/PositiveInt]]- _query-params- body :- [:map- [:name {:optional true} :string]- [:description {:optional true} [:maybe :string]]- [:source {:optional true} ::transforms.schema/transform-source]- [:target {:optional true} ::transforms.schema/transform-target]- [:run_trigger {:optional true} ::run-trigger]- [:tag_ids {:optional true} [:sequential ms/PositiveInt]]- [:collection_id {:optional true} [:maybe ms/PositiveInt]]- [:owner_user_id {:optional true} [:maybe ms/PositiveInt]]- [:owner_email {:optional true} [:maybe :string]]]]- (api/write-check :model/Transform id)+(defn update-transform!+ "Update a transform. Validates features, database support, cycles, and target conflicts.+ Returns the updated transform with hydrated associations."+ [id body](let [transform (t2/with-transaction [_];; Cycle detection should occur within the transaction to avoid race(let [old (t2/select-one :model/Transform id)@@ -368,6 +391,37 @@python-source-table-ref->table-idtransforms.util/add-source-readable)))+;; TODO (Cam 2025-11-25) -- please add a response schema to this API endpoint, it makes it easier for our customers to+;; use our API + we will need it when we make auto-TypeScript-signature generation happen+;;+#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}+(api.macros/defendpoint :put "/:id"+ "Update a transform."+ [{:keys [id]} :- [:map+ [:id ms/PositiveInt]]+ _query-params+ body :- [:map+ [:name {:optional true} :string]+ [:description {:optional true} [:maybe :string]]+ [:source {:optional true} ::transforms.schema/transform-source]+ [:target {:optional true} ::transforms.schema/transform-target]+ [:run_trigger {:optional true} ::run-trigger]+ [:tag_ids {:optional true} [:sequential ms/PositiveInt]]+ [:collection_id {:optional true} [:maybe ms/PositiveInt]]+ [:owner_user_id {:optional true} [:maybe ms/PositiveInt]]+ [:owner_email {:optional true} [:maybe :string]]]]+ (api/write-check :model/Transform id)+ (update-transform! id body))++(defn delete-transform!+ "Delete a transform and publish the delete event."+ [transform]+ (t2/delete! :model/Transform (:id transform))+ (events/publish-event! :event/transform-delete+ {:object transform+ :user-id api/*current-user-id*})+ nil)+;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to;; use our API + we will need it when we make auto-TypeScript-signature generation happen;;@@ -376,12 +430,7 @@"Delete a transform."[{:keys [id]} :- [:map[:id ms/PositiveInt]]]- (let [transform (api/write-check :model/Transform id)]- (t2/delete! :model/Transform id)- (events/publish-event! :event/transform-delete- {:object transform- :user-id api/*current-user-id*}))- nil)+ (delete-transform! (api/write-check :model/Transform id)));; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to;; use our API + we will need it when we make auto-TypeScript-signature generation happen@@ -410,17 +459,12 @@(transforms.canceling/cancel-run! (:id run))))nil)-;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to-;; use our API + we will need it when we make auto-TypeScript-signature generation happen-;;-#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}-(api.macros/defendpoint :post "/:id/run"- "Run a transform."- [{:keys [id]} :- [:map- [:id ms/PositiveInt]]]- (let [transform (api/write-check :model/Transform id)- _ (check-feature-enabled! transform)- start-promise (promise)]+(defn run-transform!+ "Run a transform. Returns a 202 response with run_id.+ The transform must already be fetched and validated."+ [transform]+ (check-feature-enabled! transform)+ (let [start-promise (promise)](u.jvm/in-virtual-thread*(transforms.execute/execute! transform {:start-promise start-promise:run-method :manual@@ -434,7 +478,16 @@:run_id run-id})(assoc :status 202)))))-;; TODO (Chris 2026-01-22) -- Remove jsqlparser typehints to be SQL parser-agnostic+;; TODO (Cam 2025-11-25) -- please add a response schema to this API endpoint, it makes it easier for our customers to+;; use our API + we will need it when we make auto-TypeScript-signature generation happen+;;+#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}+(api.macros/defendpoint :post "/:id/run"+ "Run a transform."+ [{:keys [id]} :- [:map+ [:id ms/PositiveInt]]]+ (run-transform! (api/write-check :model/Transform id)))+(defn- simple-native-query?"Checks if a native SQL query string is simple enough for automatic checkpoint insertion."[sql-string]@@ -442,6 +495,7 @@;; BEWARE: The API endpoint (caller) does not have info on database engine this query should run on. Hence;; there's no way of providing appropriate [[metabase.driver.util/macaw-options]]. `nil` is best-effort;; adding at least default :non-resserved-words.+ ;; TODO (Chris 2026-01-22) -- Remove jsqlparser typehints to be SQL parser-agnostic(let [^PlainSelect parsed (driver.u/parsed-query sql-string nil)](cond(not (instance? PlainSelect parsed))diff --git a/enterprise/backend/src/metabase_enterprise/transforms/core.clj b/enterprise/backend/src/metabase_enterprise/transforms/core.cljindex 2b407d611ea..23d86551884 100644--- a/enterprise/backend/src/metabase_enterprise/transforms/core.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms/core.clj@@ -2,6 +2,7 @@"API namespace for the `metabase-enterprise.transform` module."(:require[java-time.api :as t]+ [metabase-enterprise.transforms.models.transform][metabase-enterprise.transforms.models.transform-run][metabase-enterprise.transforms.settings][metabase-enterprise.transforms.util]@@ -17,10 +18,13 @@python-transform?query-transform?transform-source-database+ transform-source-typetransform-typehas-db-transforms-permission?][metabase-enterprise.transforms.models.transform-run- timeout-run!])+ timeout-run!]+ [metabase-enterprise.transforms.models.transform+ update-transform-tags!])(defenterprise transform-stats"Calculate successful transform runs over a window of the previous UTC day 00:00-23:59.diff --git a/enterprise/backend/src/metabase_enterprise/transforms/models/transform.clj b/enterprise/backend/src/metabase_enterprise/transforms/models/transform.cljindex 98a457ff60e..a67a5d948c6 100644--- a/enterprise/backend/src/metabase_enterprise/transforms/models/transform.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms/models/transform.clj@@ -17,6 +17,7 @@[metabase.search.ingestion :as search][metabase.search.spec :as search.spec][metabase.util :as u]+ [metabase.util.log :as log][methodical.core :as methodical][toucan2.core :as t2][toucan2.instance :as t2.instance]))@@ -100,20 +101,35 @@(collection/check-collection-namespace :model/Transform collection_id)(when collection_id(collection/check-allowed-content :model/Transform collection_id))- (assoc transform+ (let [target-db-id (transforms.i/target-db-id transform)+ ;; This is defensive code to cope with some tests for remote sync, where we deserialize a transform+ ;; with a concrete database id within it, for a potentially non-existent database.+ ;; In practice, our serialized representation should not contain any database ids, and this should+ ;; not be required.+ ;; TODO (Chris 2026-02-02) -- Update tests so this workaround is unnecessary.+ valid-db-id? (and target-db-id (t2/exists? :model/Database :id target-db-id))]+ (when-not valid-db-id?+ (log/warnf "Invalid target database id (%s) ignored for new transform (%s)" target-db-id (:name transform)))+ (-> transform+ (assoc-in [:target :database] target-db-id)+ (assoc:source_type (transforms.util/transform-source-type source)- :source_database_id (or source_database_id (transforms.i/source-db-id transform))))+ :target_db_id (when valid-db-id? target-db-id)+ :source_database_id (or source_database_id (transforms.i/source-db-id transform))))))(t2/define-before-update :model/Transform[{:keys [source source_database_id] :as transform}](when-let [new-collection (:collection_id (t2/changes transform))](collection/check-collection-namespace :model/Transform new-collection)(collection/check-allowed-content :model/Transform new-collection))- (if source- (assoc transform- :source_type (transforms.util/transform-source-type source)+ (cond-> transform+ source+ (assoc :source_type (transforms.util/transform-source-type source):source_database_id (or source_database_id (transforms.i/source-db-id transform)))- transform))++ (or (:source (t2/changes transform)) (:target (t2/changes transform)))+ ;; No database existence check added here, unlike for insert. Just allow updates for an invalid target to fail.+ (assoc :target_db_id (transforms.i/target-db-id transform))))(t2/define-after-select :model/Transform[{:keys [source] :as transform}]@@ -306,11 +322,11 @@(defmethod serdes/make-spec "Transform"[_model-name opts]{:copy [:name :description :entity_id :owner_email]- :skip [:dependency_analysis_version :source_type]+ :skip [:dependency_analysis_version :source_type :target_db_id]:transform {:created_at (serdes/date):creator_id (serdes/fk :model/User)- :collection_id (serdes/fk :model/Collection):owner_user_id (serdes/fk :model/User)+ :collection_id (serdes/fk :model/Collection):source_database_id (serdes/fk :model/Database :name):source {:export #(update % :query serdes/export-mbql):import #(update % :query serdes/import-mbql)}diff --git a/enterprise/backend/src/metabase_enterprise/transforms/ordering.clj b/enterprise/backend/src/metabase_enterprise/transforms/ordering.cljindex c9927fd162a..9476cc2082f 100644--- a/enterprise/backend/src/metabase_enterprise/transforms/ordering.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms/ordering.clj@@ -92,11 +92,11 @@the transforms in the original list -- if a transform depends on some transform not in the list, the 'extra'dependency is ignored. Both query and Python transforms can have dependencies on tables produced by other transforms."[transforms]- (let [;; Group all transforms by their database+ (let [;; Group all transforms by their database, skipping transforms with no target dbtransforms-by-db (->> transforms- (map (fn [transform]- (let [db-id (transforms.i/target-db-id transform)]- {db-id [transform]})))+ (keep (fn [transform]+ (when-let [db-id (transforms.i/target-db-id transform)]+ {db-id [transform]})))(apply merge-with into))transform-ids (into #{} (map :id) transforms)target-refs (target-ref-map transforms)@@ -109,9 +109,15 @@{:output-tables (output-table-map mp db-transforms):dependencies (dependency-map db-transforms)})))(apply merge-with merge))]- (update-vals dependencies #(into #{}- (keep (fn [dep] (resolve-dependency dep output-tables transform-ids target-refs)))- %))))+ ;; Transforms without a target database are invalid and shouldn't form part of the dependency graph.+ ;; Give them empty dependency sets so they don't interfere with ordering.+ (into (zipmap (map :id transforms) (repeat #{}))+ (update-vals dependencies+ (fn [deps]+ (into #{}+ (keep (fn [dep]+ (resolve-dependency dep output-tables transform-ids target-refs)))+ deps))))))(defn find-cycle"Finds a path containing a cycle in the directed graph `node->children`.diff --git a/enterprise/backend/src/metabase_enterprise/transforms/query_impl.clj b/enterprise/backend/src/metabase_enterprise/transforms/query_impl.cljindex 5d6f728e0a2..4b2ed1fd02e 100644--- a/enterprise/backend/src/metabase_enterprise/transforms/query_impl.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms/query_impl.clj@@ -20,7 +20,11 @@(defmethod transforms.i/target-db-id :query[transform]- (-> transform :source :query :database))+ ;; For query transforms, the target needs to match the source, so use the query as the source of truth.+ (or (-> transform :source :query :database)+ ;; Fallback to using a configured value.+ (get-in transform [:target :database])+ (:target_db_id transform)))(mr/def ::transform-details[:mapdiff --git a/enterprise/backend/src/metabase_enterprise/transforms/schema.clj b/enterprise/backend/src/metabase_enterprise/transforms/schema.cljindex 197ee88aec7..46954a10090 100644--- a/enterprise/backend/src/metabase_enterprise/transforms/schema.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms/schema.clj@@ -59,14 +59,14 @@[:map[:database {:optional true} :int][:type [:= "table"]]- [:schema {:optional true} [:or ms/NonBlankString :nil]]+ [:schema {:optional true} [:maybe ms/NonBlankString]][:name :string]])(mr/def ::table-incremental-target[:map[:database {:optional true} :int][:type [:= "table-incremental"]]- [:schema {:optional true} [:or ms/NonBlankString :nil]]+ [:schema {:optional true} [:maybe ms/NonBlankString]][:name :string][:target-incremental-strategy ::target-incremental-strategy]])diff --git a/enterprise/backend/src/metabase_enterprise/transforms/util.clj b/enterprise/backend/src/metabase_enterprise/transforms/util.cljindex fc208c3cdad..f6d50258ec7 100644--- a/enterprise/backend/src/metabase_enterprise/transforms/util.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms/util.clj@@ -264,11 +264,15 @@[{:keys [id target], :as transform}](when target(let [target (update target :type keyword)- database-id (transforms.i/target-db-id transform)- {driver :engine :as database} (t2/select-one :model/Database database-id)]- (driver/drop-transform-target! driver database target)- (log/info "Deactivating target " (pr-str target) "for transform" id)- (deactivate-table! database target))))+ database-id (transforms.i/target-db-id transform)]+ (when database-id+ (if-let [{driver :engine :as database} (t2/select-one :model/Database database-id)]+ (do+ (driver/drop-transform-target! driver database target)+ (log/info "Deactivating target " (pr-str target) "for transform" id)+ (deactivate-table! database target))+ (log/warnf "Skipping drop of transform target %s for transform %d: database %d not found"+ (pr-str target) id database-id))))))(defn delete-target-table-by-id!"Delete the target table of the transform specified by `transform-id`."@@ -602,42 +606,38 @@:ref v})](when (seq unresolved)(throw (ex-info (str "Tables not found: " (str/join ", " (map :table unresolved)))- {:unresolved unresolved})))+ {:unresolved unresolved+ :transform-message "Input table not found"})))resolved))(defn- matching-timestamp?[job field-path {:keys [start end]}](when-let [field-instant (->instant (get-in job field-path))]- (let [start-instant (some-> start u.date/parse ->instant)- end-instant (some-> end u.date/parse ->instant)]- (and (or (nil? start)- (not (.isBefore field-instant start-instant)))- (or (nil? end)- (.isAfter end-instant field-instant))))))+ (let [parse #(-> % u.date/parse ->instant)]+ ;; logic here is to find when it's not matching and invert this+ (not (or (and start (.isBefore field-instant (parse start)))+ (and end (.isAfter field-instant (parse end))))))))(defn ->date-field-filter-xf"Returns an xform for a date filter."[field-path filter-value]- (let [range (some-> filter-value (params.dates/date-string->range {:inclusive-end? false}))]- (if range- (filter #(matching-timestamp? % field-path range))- identity)))+ (if-let [range (some-> filter-value (params.dates/date-string->range {:inclusive-end? false}))]+ (filter #(matching-timestamp? % field-path range))+ identity))(defn ->status-filter-xf"Returns an xform for a transform run status filter."[field-path statuses]- (let [statuses (->> statuses (map keyword) set not-empty)]- (if statuses- (filter #(statuses (get-in % field-path)))- identity)))+ (if-let [statuses (->> statuses (map keyword) set not-empty)]+ (filter #(statuses (get-in % field-path)))+ identity))(defn ->tag-filter-xf"Returns an xform for a transform tag filter."[field-path tag-ids]- (let [tag-ids (-> tag-ids set not-empty)]- (if tag-ids- (filter #(some tag-ids (get-in % field-path)))- identity)))+ (if-let [tag-ids (-> tag-ids set not-empty)]+ (filter #(some tag-ids (get-in % field-path)))+ identity))(def ^:private metabase-index-prefix "mb_transform_idx_")diff --git a/enterprise/backend/src/metabase_enterprise/transforms_python/api.clj b/enterprise/backend/src/metabase_enterprise/transforms_python/api.cljindex a66691150c7..6916ea08e35 100644--- a/enterprise/backend/src/metabase_enterprise/transforms_python/api.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms_python/api.clj@@ -1,19 +1,15 @@(ns metabase-enterprise.transforms-python.api(:require- [clojure.java.io :as io][clojure.string :as str][metabase-enterprise.transforms-python.models.python-library :as python-library][metabase-enterprise.transforms-python.python-runner :as python-runner]- [metabase-enterprise.transforms-python.s3 :as s3][metabase-enterprise.transforms-python.settings :as transforms-python.settings][metabase.api.common :as api][metabase.api.macros :as api.macros][metabase.api.routes.common :refer [+auth]][metabase.api.util.handlers :as handlers][metabase.permissions.core :as perms]- [metabase.util :as u][metabase.util.i18n :as i18n]- [metabase.util.json :as json][metabase.util.malli.schema :as ms][toucan2.core :as t2]))@@ -48,10 +44,6 @@(api/check-403 (perms/has-any-transforms-permission? api/*current-user-id*))(python-library/update-python-library-source! path (:source body)))-;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to-;; use our API + we will need it when we make auto-TypeScript-signature generation happen-;;-#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}(api.macros/defendpoint :post "/test-run":- [:map[:logs :string]@@ -67,7 +59,7 @@source_tablesoutput_row_limitper_input_row_limit]- :or {output_row_limit 100+ :or {output_row_limit 100per_input_row_limit 100}}:- [:map[:code :string]@@ -78,40 +70,19 @@(api/check-400 (= (count db-ids) 1) (i18n/deferred-tru "All source tables must belong to the same database."))(api/check-403 (perms/has-db-transforms-permission? api/*current-user-id* (first db-ids))));; NOTE: we do not test database support, as there is no write target.- (with-open [shared-storage-ref (s3/open-shared-storage! source_tables)]- (let [server-url (transforms-python.settings/python-runner-url)- _ (python-runner/copy-tables-to-s3! {:shared-storage @shared-storage-ref- :source {:source-tables source_tables}- :limit per_input_row_limit})- {runner-status :status- runner-body :body}- (python-runner/execute-python-code-http-call!- {:server-url server-url- :code code- :request-id (u/generate-nano-id)- :table-name->id source_tables- :timeout-secs (transforms-python.settings/python-runner-test-run-timeout-seconds)- :shared-storage @shared-storage-ref})- events (python-runner/read-events @shared-storage-ref)- run-logs (str/join "\n" (map :message events))]- (cond- (:timeout runner-body)- {:logs run-logs- :error {:message (i18n/deferred-tru "Python execution timed out")}}-- (not= 200 runner-status)- {:logs run-logs- :error {:message (i18n/deferred-tru "Python execution failure (exit code {0})" (:exit_code runner-body "?"))}}-- :else- (let [output-manifest (python-runner/read-output-manifest @shared-storage-ref)]- {:logs run-logs- :output (with-open [in (python-runner/open-output @shared-storage-ref)- rdr (io/reader in)]- (let [sample-rows (take output_row_limit (line-seq rdr))- {:keys [fields]} output-manifest]- {:cols (mapv #(select-keys % [:name]) fields)- :rows (mapv json/decode (remove str/blank? sample-rows))}))})))))+ (let [result (python-runner/execute-and-read-output!+ {:code code+ :source-tables source_tables+ :per-input-limit per_input_row_limit+ :row-limit output_row_limit+ :timeout-secs (transforms-python.settings/python-runner-test-run-timeout-seconds)})+ logs (str/join "\n" (map :message (:logs result)))]+ (if (= :succeeded (:status result))+ {:logs logs+ :output {:cols (mapv #(select-keys % [:name]) (:cols result))+ :rows (:rows result)}}+ {:logs logs+ :error {:message (:message result)}})))(def ^{:arglists '([request respond raise])} routes"`/api/ee/transforms-python` routes."diff --git a/enterprise/backend/src/metabase_enterprise/transforms_python/execute.clj b/enterprise/backend/src/metabase_enterprise/transforms_python/execute.cljindex c833f919d0c..d4acf0bfc59 100644--- a/enterprise/backend/src/metabase_enterprise/transforms_python/execute.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms_python/execute.clj@@ -8,6 +8,7 @@[metabase-enterprise.transforms-python.settings :as transforms-python.settings][metabase-enterprise.transforms.core :as transforms][metabase-enterprise.transforms.instrumentation :as transforms.instrumentation]+ [metabase-enterprise.transforms.interface :as transforms.i][metabase-enterprise.transforms.util :as transforms.util][metabase.app-db.core :as app-db][metabase.driver :as driver]@@ -352,7 +353,7 @@(try(let [message-log (empty-message-log){:keys [target owner_user_id creator_id] transform-id :id} transform- {driver :engine :as db} (t2/select-one :model/Database (:database target))+ {driver :engine :as db} (t2/select-one :model/Database (transforms.i/target-db-id transform));; For manual runs, use the triggering user; for cron, use owner/creatorrun-user-id (if (and (= run-method :manual) user-id)user-id@@ -363,6 +364,7 @@(log/info "Executing Python transform" transform-id "with target" (pr-str target))(let [start-ms (u/start-timer)transform-details {:db-id (:id db)+ :transform-id transform-id:transform-type (keyword (:type target)):conn-spec (driver/connection-spec driver db):output-schema (:schema target)diff --git a/enterprise/backend/src/metabase_enterprise/transforms_python/python_runner.clj b/enterprise/backend/src/metabase_enterprise/transforms_python/python_runner.cljindex 5501d52e3e0..94c85f76d31 100644--- a/enterprise/backend/src/metabase_enterprise/transforms_python/python_runner.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms_python/python_runner.clj@@ -3,6 +3,7 @@[clj-http.client :as http][clojure.core.async :as a][clojure.java.io :as io]+ [clojure.string :as str][medley.core :as m][metabase-enterprise.transforms-python.s3 :as s3][metabase-enterprise.transforms-python.settings :as transforms-python.settings]@@ -15,6 +16,7 @@[metabase.lib.metadata :as lib.metadata][metabase.query-processor :as qp][metabase.query-processor.pipeline :as qp.pipeline]+ [metabase.util :as u][metabase.util.i18n :as i18n][metabase.util.json :as json][metabase.util.log :as log]@@ -323,8 +325,78 @@(throw (ex-info "An error occurred while copying table data to S3"{:table-id table-id:transform-message (or (:transform-message (ex-data t))- (i18n/tru "Failed to copy table contents to shared storage {0} ({1})" table-name table-id))}+ ;; Cast table-id to string manually, to avoid thousands separators.+ (i18n/tru "Failed to copy table contents to shared storage: {0} ({1})" table-name (str table-id)))}t)))(finally(safe-delete tmp-data-file)(safe-delete tmp-meta-file))))))++(defn execute-and-read-output!+ "Execute Python code and return output rows without persisting to a database.+ Used for dry-run/preview/test-run scenarios.++ Args:+ :code - Python code to execute+ :source-tables - Map of table-name -> table-id (already resolved)+ :row-limit - Max rows to return (also limits input rows)+ :timeout-secs - Optional timeout override++ Returns:+ {:status :succeeded/:failed+ :cols [{:name ...} ...] ; on success+ :rows [[...] ...] ; on success, values in column order+ :logs [{:message ...} ...] ; events from Python execution+ :message \"error message\"} ; on failure+"+ [{:keys [code source-tables per-input-limit row-limit timeout-secs]}]+ (with-open [shared-storage-ref (s3/open-shared-storage! source-tables)]+ (let [server-url (transforms-python.settings/python-runner-url)+ _ (copy-tables-to-s3! {:shared-storage @shared-storage-ref+ :source {:source-tables source-tables}+ :limit (or per-input-limit row-limit)})+ {:keys [status body]}+ (execute-python-code-http-call!+ {:server-url server-url+ :code code+ :request-id (u/generate-nano-id)+ :table-name->id source-tables+ :timeout-secs timeout-secs+ :shared-storage @shared-storage-ref})+ events (read-events @shared-storage-ref)]+ (cond+ (:timeout body)+ {:status :failed+ :logs events+ :message (i18n/deferred-tru "Python execution timed out")}++ (not= 200 status)+ {:status :failed+ :logs events+ :message (i18n/deferred-tru "Python execution failure (exit code {0})" (:exit_code body "?"))}++ :else+ (let [output-manifest (read-output-manifest @shared-storage-ref)+ {:keys [fields]} output-manifest]+ ;; TODO (Chris 2026-01-27) -- Disabled this check to match behavior in master, but *real* execution does it.+ ;; It seems we added the check as part of DRY-ing up transforms code to reuse with workspaces.+ #_(if-not (seq fields)+ {:status :failed+ :logs events+ :message (i18n/deferred-tru "No fields in output metadata")})+ (with-open [in (open-output @shared-storage-ref)+ rdr (io/reader in)]+ (let [cols (mapv (fn [c]+ {:name (:name c)+ :base_type (some-> c :base_type keyword)})+ fields)+ rows (into []+ (comp+ (remove str/blank?)+ (take row-limit)+ (map json/decode))+ (line-seq rdr))]+ {:status :succeeded+ :cols cols+ :rows rows+ :logs events})))))))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/api.clj b/enterprise/backend/src/metabase_enterprise/workspaces/api.cljnew file mode 100644index 00000000000..be90bcb1203--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/api.clj@@ -0,0 +1,1143 @@+(ns metabase-enterprise.workspaces.api+ "`/api/ee/workspace/` routes"+ (:require+ [clojure.set :as set]+ [clojure.string :as str]+ [clojure.walk :as walk]+ [honey.sql.helpers :as sql.helpers]+ [medley.core :as m]+ [metabase-enterprise.transforms.core :as transforms]+ [metabase-enterprise.transforms.util :as transforms.util]+ [metabase-enterprise.workspaces.common :as ws.common]+ [metabase-enterprise.workspaces.impl :as ws.impl]+ [metabase-enterprise.workspaces.merge :as ws.merge]+ [metabase-enterprise.workspaces.models.workspace :as ws.model]+ [metabase-enterprise.workspaces.models.workspace-input-external]+ [metabase-enterprise.workspaces.models.workspace-log]+ [metabase-enterprise.workspaces.models.workspace-output-external]+ [metabase-enterprise.workspaces.types :as ws.t]+ [metabase-enterprise.workspaces.validation :as ws.validation]+ [metabase.api.common :as api]+ [metabase.api.macros :as api.macros]+ [metabase.api.routes.common :as routes.common :refer [+auth]]+ [metabase.config.core :as config]+ ^{:clj-kondo/ignore [:metabase/modules]}+ [metabase.driver.sql.normalize :as sql.normalize]+ [metabase.driver.util :as driver.u]+ [metabase.lib.core :as lib]+ [metabase.queries.schema :as queries.schema]+ [metabase.request.core :as request]+ [metabase.util :as u]+ [metabase.util.i18n :as i18n :refer [deferred-tru tru]]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(def ^:private log-limit "Maximum number of recent workspace log items to show" 20)++(mr/def ::appdb-or-ref-id [:or ::ws.t/appdb-id ::ws.t/ref-id])++(def ^:private computed-statuses+ "All possible values returned by computed-status (computed from base_status, etc.)"+ #{:uninitialized :pending :ready :broken :archived})++(mr/def ::status (into [:enum] computed-statuses))++(def ^:private Workspace+ [:map+ [:id ::ws.t/appdb-id]+ [:name :string]+ [:collection_id ::ws.t/appdb-id]+ [:database_id ::ws.t/appdb-id]+ [:status ::status]+ [:created_at ms/TemporalInstant]+ [:updated_at ms/TemporalInstant]])++;; Transform-related schemas (adapted from transforms/api.clj)+;; TODO (Chris 2026-02-02) -- We should reuse these schemas, by exposing common types from the transforms module. They *can* match exactly.++(mr/def ::transform-source+ [:multi {:dispatch (comp keyword :type)}+ [:query+ [:map+ [:type [:= "query"]]+ [:query ::queries.schema/query]]]+ [:python+ [:map {:closed true}+ [:source-database {:optional true} :int]+ [:source-tables [:map-of [:string {:min 1}] :int]]+ [:type [:= "python"]]+ [:body :string]]]])++(mr/def ::transform-target+ [:map {:closed true}+ [:database {:optional true} ::ws.t/appdb-id]+ [:type [:enum "table"]]+ [:schema {:optional true} [:maybe [:string {:min 1}]]]+ [:name [:string {:min 1}]]])++(mr/def ::run-trigger+ [:enum "none" "global-schedule"])++(defn- check-transforms-enabled!+ [db-id]+ (let [database (api/check-400 (t2/select-one :model/Database db-id)+ (deferred-tru "The target database cannot be found."))]+ (api/check (transforms.util/check-feature-enabled nil)+ [402 (deferred-tru "Premium features required for transforms are not enabled.")])+ (api/check-400 (not (:is_sample database))+ (deferred-tru "Cannot run transforms on the sample database."))+ (api/check-400 (not (:is_audit database))+ (deferred-tru "Cannot run transforms on audit databases."))+ (api/check-400 (driver.u/supports? (:engine database) :transforms/table database)+ (deferred-tru "The database does not support the requested transform target type."))+ (api/check-400 (not (transforms.util/db-routing-enabled? database))+ (deferred-tru "Transforms are not supported on databases with DB routing enabled."))))++(def ^:private ws-prefix "/api/ee/workspace/\\d+")++(defn- ws-pattern+ "Compile a regex matching the workspace API prefix followed by `suffix`."+ ^java.util.regex.Pattern [suffix]+ (re-pattern (str ws-prefix suffix)))++;; Service users may read workspace state and manage transforms within their workspace.+;; All other routes relate to the lifecycle of the workspace itself, and require superuser — including:+;; GET/POST / (list/create workspaces)+;; GET /enabled, /database, /checkout (cross-workspace state)+;; PUT /:ws-id (reconfigure workspace)+;; POST /:ws-id/archive (archive workspace)+;; POST /:ws-id/unarchive (unarchive workspace)+;; DELETE /:ws-id (delete workspace)+;; POST /:ws-id/merge (merge workspace)+;; POST /:ws-id/transform/:tx-id/merge (merge single transform)+;; POST /test-resources (create test resources - available in non-prod environments only)+(def ^:private service-user-patterns+ "URI patterns that workspace service users may access.+ New routes default to admin-only for safety."+ (update-vals+ {;; Read workspace state+ :get ["$"+ "/table$"+ "/log$"+ "/graph$"+ "/problem$"+ "/external/transform$"+ "/transform$"+ "/transform/[^/]+$"]+ ;; Manage & run transforms+ :post ["/transform$"+ "/transform/[^/]+/archive$"+ "/transform/[^/]+/unarchive$"+ "/transform/validate/target$"+ "/run$"+ "/transform/[^/]+/run$"+ "/transform/[^/]+/dry-run$"]+ :put ["/transform/[^/]+$"]+ :delete ["/transform/[^/]+$"]}+ (partial mapv ws-pattern)))++(defn- service-user-allowed?+ "True if this request can be made by a workspace's service user."+ [{:keys [uri request-method]}]+ (when-let [patterns (get service-user-patterns request-method)]+ (some #(re-matches % uri) patterns)))++(defn- owns-workspace?+ "True if the current user is the service user for the workspace in this request's URI."+ [uri]+ (when-let [[_ ws-id-str] (re-find #"/api/ee/workspace/(\d+)" uri)]+ (let [ws-id (parse-long ws-id-str)+ user-id api/*current-user-id*]+ (and user-id (t2/exists? :model/Workspace :id ws-id :execution_user user-id)))))++(defn- authorize*+ "Authorization middleware for workspace routes.++ Access rules:+ - Service user routes: superuser OR the workspace's own service user+ - All other routes: superuser required (default)"+ [handler]+ (fn [request respond raise]+ (if (service-user-allowed? request)+ (api/check-403 (or api/*is-superuser?* (owns-workspace? (:uri request))))+ (api/check-superuser))+ (handler request respond raise)))++(def ^:private +authorize+ (routes.common/wrap-middleware-for-open-api-spec-generation authorize*))++(defn- ws->response+ "Transform a workspace record into an API response, computing the backwards-compatible status."+ [ws]+ (-> ws+ (select-keys [:id :name :collection_id :database_id :created_at :updated_at])+ (assoc :status (ws.model/computed-status ws))))++;;; routes++(def ^:private WorkspaceListing+ [:map {:closed true}+ [:id ::ws.t/appdb-id]+ [:database_id ::ws.t/appdb-id]+ [:name :string]+ [:status ::status]+ [:updated_at ms/TemporalInstant]])++(api.macros/defendpoint :get "/" :- [:map {:closed true}+ [:items [:sequential WorkspaceListing]]+ [:limit [:maybe :int]]+ [:offset [:maybe :int]]]+ "Get a list of all workspaces"+ [_route-params+ _query-params]+ {:items (->> (t2/select [:model/Workspace :id :name :database_id :base_status :db_status :updated_at]+ (cond-> {:order-by [[:created_at :desc]]}+ (request/limit) (sql.helpers/limit (request/limit))+ (request/offset) (sql.helpers/offset (request/offset))))+ (mapv #(-> %+ (dissoc :base_status :db_status)+ (assoc :status (ws.model/computed-status %)))))+ :limit (request/limit)+ :offset (request/offset)})++(mr/def ::input-table+ [:map+ ;; Future-proof with cross-db Python transforms+ [:db_id ::ws.t/appdb-id]+ [:schema [:maybe :string]]+ [:table :string]+ [:table_id [:maybe ::ws.t/appdb-id]]])++(mr/def ::output-table+ [:map+ ;; Future-proof with multi-db Workspaces, plus necessary to resolve table references when id is null.+ [:db_id ::ws.t/appdb-id]+ [:global [:map+ ;; transform_id is nil for workspace outputs, int for external outputs+ [:transform_id [:maybe ::ws.t/appdb-id]]+ [:schema [:maybe :string]]+ [:table :string]+ [:table_id [:maybe ::ws.t/appdb-id]]]]+ [:isolated [:map+ ;; transform_id is ref_id (string) for workspace outputs, int for external outputs+ [:transform_id [:or ::ws.t/ref-id ::ws.t/appdb-id]]+ [:schema :string]+ [:table :string]+ [:table_id [:maybe ::ws.t/appdb-id]]]]])++(api.macros/defendpoint :get "/:ws-id/table"+ :- [:map {:closed true}+ [:inputs [:sequential ::input-table]]+ [:outputs [:sequential ::output-table]]]+ "Get workspace tables"+ {:access :workspace}+ [{:keys [ws-id]} :- [:map [:ws-id ms/PositiveInt]]+ _query-params]+ (let [workspace (api/check-404 (t2/select-one :model/Workspace :id ws-id))+ ;; Trigger creation of the Workspace*External entries+ _ (ws.impl/get-or-calculate-graph! workspace)+ order-by {:order-by [:db_id :global_schema :global_table]}+ outputs (t2/select [:model/WorkspaceOutput+ :db_id :global_schema :global_table :global_table_id+ :isolated_schema :isolated_table :isolated_table_id :ref_id]+ :workspace_id ws-id order-by)+ external-outputs (t2/select [:model/WorkspaceOutputExternal+ :db_id :global_schema :global_table :global_table_id+ :isolated_schema :isolated_table :isolated_table_id :transform_id]+ :workspace_id ws-id order-by)+ all-outputs (concat outputs external-outputs)+ raw-inputs (distinct+ (t2/select [:model/WorkspaceInput :db_id :schema :table :table_id]+ :workspace_id ws-id {:order-by [:db_id :schema :table]}))+ external-inputs (distinct+ (t2/select [:model/WorkspaceInputExternal :db_id :schema :table :table_id]+ :workspace_id ws-id {:order-by [:db_id :schema :table]}))+ all-raw-inputs (concat raw-inputs external-inputs)+ ;; Some of our inputs may be shadowed by the outputs of other transforms. We only want external inputs.+ shadowed? (into #{} (map (juxt :db_id :global_schema :global_table)) all-outputs)+ inputs (remove (comp shadowed? (juxt :db_id :schema :table)) all-raw-inputs)+ ;; Build a map of [d s t] => id for every table that has been synced since the output row was written.+ fallback-map (merge+ (ws.impl/table-ids-fallbacks :global_schema :global_table :global_table_id all-outputs)+ (ws.impl/table-ids-fallbacks :isolated_schema :isolated_table :isolated_table_id all-outputs))]+ {:inputs (sort-by (juxt :db_id :schema :table) inputs)+ :outputs (sort-by+ (juxt :db_id (comp (juxt :schema :table) :global))+ (concat+ ;; Workspace transform outputs+ (for [{:keys [ref_id db_id global_schema global_table global_table_id+ isolated_schema isolated_table isolated_table_id]} outputs]+ {:db_id db_id+ :global {:transform_id nil+ :schema global_schema+ :table global_table+ :table_id (or global_table_id (get fallback-map [db_id global_schema global_table]))}+ :isolated {:transform_id ref_id+ :schema isolated_schema+ :table isolated_table+ :table_id (or isolated_table_id (get fallback-map [db_id isolated_schema isolated_table]))}})+ ;; External transform outputs+ (for [{:keys [transform_id db_id global_schema global_table global_table_id+ isolated_schema isolated_table isolated_table_id]} external-outputs]+ {:db_id db_id+ :global {:transform_id transform_id+ :schema global_schema+ :table global_table+ :table_id (or global_table_id (get fallback-map [db_id global_schema global_table]))}+ :isolated {:transform_id transform_id+ :schema isolated_schema+ :table isolated_table+ :table_id (or isolated_table_id (get fallback-map [db_id isolated_schema isolated_table]))}})))}))++(api.macros/defendpoint :get "/:ws-id" :- Workspace+ "Get a single workspace by ID"+ {:access :workspace}+ [{:keys [ws-id]} :- [:map [:ws-id ms/PositiveInt]]+ _query-params]+ (-> (t2/select-one :model/Workspace :id ws-id) api/check-404 ws->response))++(api.macros/defendpoint :get "/:ws-id/log"+ :- [:map+ [:workspace_id ms/PositiveInt]+ [:status ::status]+ [:updated_at :any]+ [:last_completed_at [:maybe :any]]+ [:logs [:sequential [:map+ [:id ms/PositiveInt]+ [:task :keyword]+ [:description ms/LocalizedString]+ [:started_at :any]+ [:updated_at :any]+ [:completed_at [:maybe :any]]+ [:status [:maybe :keyword]]+ [:message [:maybe :string]]]]]]+ "Get workspace creation status and recent log entries for polling during async setup"+ {:access :workspace}+ [{:keys [ws-id]} :- [:map [:ws-id ms/PositiveInt]]+ _query-params]+ (let [workspace (api/check-404 (t2/select-one :model/Workspace :id ws-id))+ logs (t2/select [:model/WorkspaceLog+ :id :task :started_at :completed_at :status :message+ :updated_at]+ :workspace_id ws-id+ {:order-by [[:started_at :desc]]+ :limit log-limit})]+ {:workspace_id ws-id+ :status (ws.model/computed-status workspace)+ :logs logs+ :updated_at (->> (map :updated_at logs) sort reverse first)+ :last_completed_at (->> (seq (keep :completed_at logs)) sort reverse first)}))++(def ^:private CreateWorkspace+ [:map+ [:name {:optional true} [:string {:min 1}]]+ [:database_id {:optional true} ::ws.t/appdb-id]])++(defn- first-supported-database-id+ "Return the ID of the first database that supports workspaces, or nil if none."+ []+ (:id+ (u/seek #(driver.u/supports? (:engine %) :workspace %)+ (t2/select :model/Database :is_audit false :is_sample false {:order-by [:name]}))))++(api.macros/defendpoint :post "/" :- Workspace+ "Create a new workspace++ Potential payload:+ {:name \"a\" :database_id 2}}"+ [_route-params+ _query-params+ {:keys [database_id] :as body} :- CreateWorkspace]+ (when database_id+ (check-transforms-enabled! database_id))++ ;; If no database_id provided, use first supported DB as provisional default (uninitialized workspace)+ (let [provisional? (not database_id)+ database-id (or database_id+ (api/check-400 (first-supported-database-id)+ (tru "No supported databases configured. Please add a database that supports workspaces.")))]+ (ws->response (ws.common/create-workspace! api/*current-user-id*+ (assoc body+ :database_id database-id+ :provisional? provisional?)))))++(defn- db-unsupported-reason [db]+ (when (not (driver.u/supports? (:engine db) :workspace db))+ "Database type not supported."))++(api.macros/defendpoint :get "/enabled" :- [:map [:supported :boolean] [:reason {:optional true} :string]]+ "Test whether the current user can use Workspaces. Optionally takes a specific database.+ Factors include: driver support, database user privileges, metabase permissions."+ [_url-params+ {:keys [database-id]} :- [:map [:database-id {:optional true} ms/PositiveInt]]]+ (if-let [reason (when database-id+ (db-unsupported-reason (api/check-404 (t2/select-one :model/Database database-id))))]+ {:supported false, :reason reason}+ {:supported true}))++(api.macros/defendpoint :get "/database" :- [:map+ [:databases+ [:sequential [:map+ [:id ms/PositiveInt]+ [:name :string]+ [:enabled :boolean]+ [:permissions_status {:optional true}+ [:map+ [:status :string]+ [:checked_at :string]+ [:error {:optional true} :string]]]]]]]+ "Get a list supported databases, and whether they're enabled and have required permissions."+ [_url-params+ _query-params]+ (let [databases (->> (t2/select [:model/Database :id :name :engine :settings :workspace_permissions_status]+ :is_audit false :is_sample false {:order-by [:name]})+ (filter #(driver.u/supports? (:engine %) :workspace %)))]+ {:databases (mapv (fn [{:keys [id name workspace_permissions_status settings]}]+ {:id id+ :name name+ :enabled (boolean (:database-enable-workspaces settings))+ :workspace_permissions_status (or workspace_permissions_status {:status "unknown"})})+ databases)}))++(api.macros/defendpoint :put "/:ws-id" :- Workspace+ "Update simple workspace properties.++ Can set database_id only on uninitialized workspaces."+ [{:keys [ws-id]} :- [:map [:ws-id ms/PositiveInt]]+ _query-params+ {:keys [name database_id]} :- [:map {:closed true}+ [:name {:optional true} [:string {:min 1}]]+ [:database_id {:optional true} ::ws.t/appdb-id]]]+ (let [workspace (api/check-404 (t2/select-one :model/Workspace :id ws-id))+ _ (api/check-400 (not= :archived (:base_status workspace)) "Cannot update an archived workspace")+ data (cond-> {}+ database_id (-> (u/prog1+ (api/check-400 (= :uninitialized (:db_status workspace))+ "Can only set database_id on uninitialized workspace")+ (check-transforms-enabled! database_id))+ (assoc :database_id database_id))+ name (assoc :name name))]+ (ws->response+ (if (seq data)+ (do+ (t2/update! :model/Workspace ws-id data)+ (t2/select-one :model/Workspace :id ws-id))+ workspace))))++(api.macros/defendpoint :post "/:ws-id/archive" :- Workspace+ "Archive a workspace. Deletes the isolated schema and tables, but preserves mirrored entities."+ [{:keys [ws-id]} :- [:map [:ws-id ms/PositiveInt]]+ _query-params+ _body-params]+ (let [ws (api/check-404 (t2/select-one :model/Workspace :id ws-id))]+ (api/check-400 (not= :archived (:base_status ws)) "You cannot archive an archived workspace")+ (ws.model/archive! ws)+ (-> (t2/select-one :model/Workspace :id ws-id)+ ws->response)))++(api.macros/defendpoint :post "/:ws-id/unarchive" :- Workspace+ "Restore an archived workspace. Recreates the isolated schema and tables."+ [{:keys [ws-id]} :- [:map [:ws-id ms/PositiveInt]]+ _query-params+ _body-params]+ (let [ws (api/check-404 (t2/select-one :model/Workspace :id ws-id))]+ (api/check-400 (= :archived (:base_status ws)) "You cannot unarchive a workspace that is not archived")+ (ws.model/unarchive! ws)+ (-> (t2/select-one :model/Workspace :id ws-id)+ ws->response)))++(api.macros/defendpoint :delete "/:ws-id" :- [:map [:ok [:= true]]]+ "Delete a workspace and all its contents, including mirrored entities."+ [{:keys [ws-id]} :- [:map [:ws-id ms/PositiveInt]]+ _query-params]+ (let [ws (api/check-404 (t2/select-one :model/Workspace :id ws-id))]+ (api/check-400 (= :archived (:base_status ws)) "You cannot delete a workspace without first archiving it")+ (ws.model/delete! ws)+ {:ok true}))++(defn- checkout-disabled-reason+ "Returns reason why a transform cannot be checked out, or nil if checkout is allowed."+ [{:keys [source_type source]}]+ (case source_type+ :mbql "mbql"+ :native (when (seq (lib/template-tags-referenced-cards (:query source)))+ "card-reference")+ :python nil+ "unknown-type"))++(def ^:private ExternalTransform+ ;; Might be interesting to show whether they're enclosed, once we have the graph.+ ;; When they're enclosed, it could also be interesting to know whether they're stale.+ [:map+ [:id ::ws.t/appdb-id]+ [:name :string]+ [:source_type :keyword]+ [:checkout_disabled [:maybe :string]]])++(api.macros/defendpoint :get "/:ws-id/external/transform" :- [:map [:transforms [:sequential ExternalTransform]]]+ "Get transforms that are external to the workspace, i.e. no matching workspace_transform row exists."+ {:access :workspace}+ [{:keys [ws-id]} :- [:map [:ws-id ::ws.t/appdb-id]]+ {:keys [database-id]} :- [:map [:database-id {:optional true} ::ws.t/appdb-id]]]+ (let [db-id (or database-id+ (:database_id (api/check-404 (t2/select-one [:model/Workspace :database_id] ws-id))))+ transforms (t2/select [:model/Transform :id :name :source_type :source]+ {:left-join [[:workspace_transform :wt]+ [:and+ [:= :transform.id :wt.global_id]+ [:= :wt.workspace_id ws-id]]]+ ;; NULL workspace_id means transform is not checked out into this workspace+ :where [:and+ [:= nil :wt.workspace_id]+ [:= :target_db_id db-id]]+ :order-by [[:id :asc]]})]+ {:transforms+ (into []+ (map #(-> %+ (dissoc :source)+ (assoc :checkout_disabled (checkout-disabled-reason %))))+ transforms)}))++(api.macros/defendpoint :post "/:ws-id/run"+ :- [:map+ [:succeeded [:sequential ::ws.t/ref-id]]+ [:failed [:sequential ::ws.t/ref-id]]+ [:not_run [:sequential ::ws.t/ref-id]]]+ "Execute all transforms in the workspace in dependency order.+ Returns which transforms succeeded, failed, and were not run."+ {:access :workspace}+ [{:keys [ws-id]} :- [:map [:ws-id ::ws.t/appdb-id]]+ _query-params+ ;; Hmmm, I wonder why this isn't a boolean? T_T+ {:keys [stale_only]} :- [:map [:stale_only {:optional true} [:or [:= 1] :boolean]]]]+ (let [workspace (t2/select-one :model/Workspace :id ws-id)+ _ (api/check-404 workspace)+ _ (api/check-400 (not= :archived (:base_status workspace)) "Cannot execute archived workspace")+ graph (ws.impl/get-or-calculate-graph! workspace)]+ (ws.impl/execute-workspace! workspace graph {:stale-only? (boolean (#{1 true} stale_only))})))++(mr/def ::graph-node-type [:enum :input-table :external-transform :workspace-transform])++(mr/def ::graph-node+ [:map+ [:id ::appdb-or-ref-id]+ [:type [:enum :input-table :external-transform :workspace-transform]]+ [:dependents_count [:map-of ::graph-node-type ms/PositiveInt]]+ [:data :map]])++(mr/def ::graph-edge+ [:map+ [:from_entity_id ::appdb-or-ref-id]+ [:from_entity_type :string]+ [:to_entity_id ::appdb-or-ref-id]+ [:to_entity_type :string]])++(def ^:private GraphResult+ [:map+ [:nodes [:sequential ::graph-node]]+ [:edges [:sequential ::graph-edge]]])++(defn- node-type [node]+ (let [nt (:node-type node)]+ (case nt+ :table :input-table+ nt)))++(defn- node-id [{:keys [node-type id]}]+ (case node-type+ :workspace-transform id+ :external-transform id+ :table (str (:db id) "-" (:schema id) "-" (:table id))))++(defn- table-ids-by-target+ "Batch lookup table IDs for transform targets.+ Returns {[db_id normalized_schema normalized_name] -> table-record}.+ Schema and table names are normalized per database driver to match how tables are stored in metabase_table."+ [db-id driver transforms]+ (when (seq transforms)+ (let [table-keys (into #{}+ (keep (fn [{:keys [target]}]+ (when (:database target)+ [db-id+ (sql.normalize/normalize-name driver (:schema target))+ (sql.normalize/normalize-name driver (:name target))])))+ transforms)+ with-schema (filter second table-keys)+ without-schema (keep (fn [[db-id schema table-name]]+ (when-not schema [db-id table-name]))+ table-keys)]+ (when (or (seq with-schema) (seq without-schema))+ (let [tables (t2/select [:model/Table :id :db_id :schema :name]+ {:where [:or+ (when (seq with-schema)+ [:in [:composite :db_id :schema :name] with-schema])+ (when (seq without-schema)+ [:and+ [:= :schema nil]+ [:in [:composite :db_id :name] without-schema]])]})]+ (m/index-by (juxt :db_id :schema :name) tables))))))++(defn- target->spec [driver table-id-map {:keys [target] :as tx}]+ (let [table-key [(:database target)+ (sql.normalize/normalize-name driver (:schema target))+ (sql.normalize/normalize-name driver (:name target))]+ table-id (:id (get table-id-map table-key))]+ (assoc tx :target {:db (:database target)+ :schema (:schema target)+ :table (:name target)+ :table_id table-id})))++(defn- fetch-transforms-for-graph+ "Batch fetch all transforms needed for the graph. Returns {:external {id -> tx}, :workspace {ref_id -> tx}}."+ [ws-id entities]+ (let [external-ids (into [] (keep #(when (= :external-transform (:node-type %)) (:id %))) entities)+ ref-ids (into [] (keep #(when (= :workspace-transform (:node-type %)) (:id %))) entities)+ external-txs (when (seq external-ids)+ (m/index-by :id (t2/select [:model/Transform :id :name :target] :id [:in external-ids])))+ workspace-txs (when (seq ref-ids)+ (m/index-by :ref_id (t2/select [:model/WorkspaceTransform :ref_id :name :target]+ :workspace_id ws-id :ref_id [:in ref-ids])))]+ {:external external-txs+ :workspace workspace-txs}))++(defn- node-data [driver transforms-map table-id-map {:keys [node-type id]}]+ (case node-type+ :table id+ :external-transform (some->> (get-in transforms-map [:external id])+ (target->spec driver table-id-map))+ :workspace-transform (some->> (get-in transforms-map [:workspace id])+ (target->spec driver table-id-map))))++(api.macros/defendpoint :get "/:ws-id/graph" :- GraphResult+ "Display the dependency graph between the Changeset and the (potentially external) entities that they depend on."+ {:access :workspace}+ [{:keys [ws-id]} :- [:map [:ws-id ms/PositiveInt]]+ _query-params]+ (let [workspace (api/check-404 (t2/select-one :model/Workspace :id ws-id))+ db-id (:database_id workspace)+ driver (t2/select-one-fn :engine [:model/Database :engine] :id db-id)+ {:keys [inputs entities dependencies]} (ws.impl/get-or-calculate-graph! workspace)+ ;; Batch fetch all transforms and build table-id lookup map+ transforms-map (fetch-transforms-for-graph ws-id entities)+ all-transforms (concat (vals (:external transforms-map))+ (vals (:workspace transforms-map)))+ table-id-map (table-ids-by-target db-id driver all-transforms)+ ;; We may want to cache this inverted graph in the database JSON to avoid recalculating it each time.+ inverted (reduce+ (fn [inv [c parents]]+ (reduce (fn [inv p] (update inv p (fnil conj #{}) c)) inv parents))+ {}+ dependencies)+ dep-count #(frequencies (map node-type (get inverted %)))]++ {:nodes (concat (for [i inputs]+ {:type :input-table+ ;; Use an id that's independent of whether the table exists yet.+ :id (node-id {:id i, :node-type :table})+ :data i+ :dependents_count (dep-count {:node-type :table, :id i})})+ (for [e entities]+ {:type (node-type e)+ :id (:id e)+ :data (node-data driver transforms-map table-id-map e)+ :dependents_count (dep-count e)}))+ :edges (for [[child parents] dependencies, parent parents]+ {:to_entity_type (name (node-type parent))+ :to_entity_id (node-id parent)+ :from_entity_type (name (node-type child))+ :from_entity_id (node-id child)})}))++;;; ---------------------------------------- Problems/Validation ----------------------------------------++(api.macros/defendpoint :get "/:ws-id/problem" :- [:sequential ::ws.t/problem]+ "Detect problems in the workspace that would affect downstream transforms after merge.++ Returns a list of problems, each with:+ - category: the problem category (e.g. 'unused', 'internal-downstream', 'external-downstream')+ - problem: the specific problem (e.g. 'not-run', 'stale', 'removed-field')+ - severity: :error, :warning, or :info+ - block-merge: whether this problem prevents merging+ - data: extra information, shape depends on the problem type++ See `metabase-enterprise.workspaces.types/problem-types` for the full list."+ {:access :workspace}+ [{:keys [ws-id]} :- [:map [:ws-id ms/PositiveInt]]+ _query-params]+ (let [workspace (api/check-404 (t2/select-one :model/Workspace :id ws-id))+ graph (ws.impl/get-or-calculate-graph! workspace)]+ (ws.validation/find-downstream-problems ws-id graph)))++(def ^:private db+schema+table (juxt :database :schema :name))++(defn- internal-target-conflict?+ "Check whether the given table is the target of another transform within the workspace. Ignores global transforms."+ [ws-id target & [tx-id]]+ (contains?+ (t2/select-fn-set (comp db+schema+table :target)+ [:model/WorkspaceTransform :target]+ :ref_id [:not= tx-id]+ :workspace_id ws-id)+ (db+schema+table target)))++(api.macros/defendpoint :post "/:ws-id/transform/validate/target"+ :- [:map [:status :int] [:body [:or :string i18n/LocalizedString]]]+ "Validate the target table for a workspace transform"+ {:access :workspace}+ [{:keys [ws-id]} :- [:map [:ws-id ::ws.t/appdb-id]]+ {:keys [transform-id]} :- [:map [:transform-id {:optional true} ::ws.t/ref-id]]+ {:keys [db_id target]} :- [:map+ [:db_id {:optional true} ::ws.t/appdb-id]+ [:target [:map+ [:database {:optional true} ::ws.t/appdb-id]+ [:type :string]+ [:schema [:maybe :string]]+ [:name :string]]]]]+ (let [workspace (api/check-404 (t2/select-one [:model/Workspace :database_id :db_status] ws-id))+ target (update target :database #(or % db_id))+ tx-id (when transform-id (parse-long transform-id))+ ;; For uninitialized workspaces we skip over checks for their db id+ ws-db-id (when (not= :uninitialized (:db_status workspace)) (:database_id workspace))]+ (cond+ (not= "table" (:type target))+ {:status 403 :body (deferred-tru "Unsupported target type")}++ (and db_id ws-db-id (not= db_id ws-db-id))+ {:status 403 :body (deferred-tru "Must target the workspace database")}++ (not (or db_id ws-db-id))+ {:status 403 :body (deferred-tru "Must target a database")}++ (when-let [schema (:schema target)] (str/starts-with? schema "mb__isolation_"))+ {:status 403 :body (deferred-tru "Must not target an isolated workspace schema")}++ ;; Within a workspace, we defer blocking on conflicts outside the workspace+ #_{:status 403 :body (deferred-tru "A table with that name already exists.")}++ ;; Consider deferring this validation until merge also.+ (internal-target-conflict? ws-id target tx-id)+ {:status 403 :body (deferred-tru "Another transform in this workspace already targets that table")}++ :else+ {:status 200 :body "OK"})))++(defn- malli-map-keys [schema]+ (into [] (map first) (rest schema)))++(defn- select-malli-keys+ "Like select-keys, but with the arguments reversed, and taking the malli schema for the output map.+ Nothing smart - for example, it doesn't understand optional verus maybe."+ ([schema m]+ (select-keys m (malli-map-keys schema)))+ ([schema alias-map m]+ (merge (select-malli-keys schema m)+ (u/for-map [[to from] alias-map] [to (from m)]))))++(defn- select-model-malli-keys+ "Build the model-fields vector for a t2/select from a malli schema for the output row(s)"+ ([model schema]+ (into [model] (malli-map-keys schema)))+ ([model schema alias-map]+ (into [model]+ (map (fn [to] (if-let [from (to alias-map)] [from to] to)))+ (malli-map-keys schema))))++(def ^:private WorkspaceTransform+ [:map+ [:ref_id ::ws.t/ref-id]+ [:global_id [:maybe ::ws.t/appdb-id]]+ [:name :string]+ [:description [:maybe :string]]+ [:source :map]+ [:target :map]+ ;; Not yet calculated, see https://linear.app/metabase/issue/BOT-684/mark-stale-transforms-workspace-only+ [:target_stale [:maybe :boolean]]+ [:workspace_id ::ws.t/appdb-id]+ [:creator_id [:maybe ::ws.t/appdb-id]]+ [:archived_at :any]+ [:created_at :any]+ [:updated_at :any]+ [:last_run_at :any]+ [:last_run_status [:maybe :string]]+ [:last_run_message [:maybe :string]]])++(def ^:private workspace-transform-alias {:target_stale :definition_changed})++(defn- attach-isolated-target [{:keys [workspace_id ref_id] :as ws-transform}]+ (let [{:keys [db_id isolated_schema isolated_table]}+ (t2/select-one :model/WorkspaceOutput :workspace_id workspace_id :ref_id ref_id)]+ (assoc ws-transform :target_isolated {:type "table"+ :database db_id+ :schema isolated_schema+ :name isolated_table})))++(api.macros/defendpoint :post "/:ws-id/transform"+ :- WorkspaceTransform+ "Add another transform to the Changeset. This could be a fork of an existing global transform, or something new."+ {:access :workspace}+ [{:keys [ws-id]} :- [:map [:ws-id ::ws.t/appdb-id]]+ _query-params+ body :- [:map #_{:closed true}+ [:name :string]+ [:description {:optional true} [:maybe :string]]+ [:source ::transform-source]+ ;; Not sure why this schema is giving trouble+ #_[:target ::transform-target]]]+ (api/check (transforms.util/check-feature-enabled body)+ [402 (deferred-tru "Premium features required for this transform type are not enabled.")])+ (t2/with-transaction [_tx]+ (let [workspace (u/prog1 (api/check-404 (t2/select-one :model/Workspace :id ws-id))+ (api/check-400 (not= :archived (:base_status <>)) "Cannot create transforms in an archived workspace"))+ ;; TODO (Chris 2026-02-02) -- We use 400 here, but 403 in the validation route. Consistency would be nice.+ _ (api/check-400 (not (internal-target-conflict? ws-id (:target body)))+ (deferred-tru "Another transform in this workspace already targets that table"))+ global-id (:global_id body (:id body))+ ;; Verify transform source is allowed in workspaces (not MBQL, no card references, etc.)+ _ (let [source-type (transforms/transform-source-type (:source body))+ reason (checkout-disabled-reason {:source_type source-type :source (:source body)})]+ (api/check-400 (nil? reason)+ (case reason+ "mbql" (deferred-tru "MBQL transforms cannot be added to workspaces.")+ "card-reference" (deferred-tru "Transforms that reference other questions cannot be added to workspaces.")+ (deferred-tru "This transform cannot be added to a workspace: {0}." reason))))+ ;; For uninitialized workspaces, preserve the target database from the request body+ ;; (add-to-changeset! will reinitialize the workspace with it if different from provisional)+ ;; For initialized workspaces, ensure the target database matches the workspace database+ body (-> body (dissoc :global_id)+ (cond-> (not= :uninitialized (:db_status workspace))+ (update :target assoc :database (:database_id workspace))))+ transform (ws.common/add-to-changeset! api/*current-user-id* workspace :transform global-id body)]+ (attach-isolated-target (select-malli-keys WorkspaceTransform workspace-transform-alias transform)))))++;; TODO (Sanya 2025-12-12) -- Confirm precisely which fields are needed by the FE+(def ^:private WorkspaceTransformListing+ "Schema for a transform in a workspace"+ [:map {:closed true}+ [:ref_id ::ws.t/ref-id]+ [:global_id [:maybe ::ws.t/appdb-id]]+ [:name :string]+ [:source_type [:maybe :keyword]]+ [:creator_id [:maybe ::ws.t/appdb-id]]+ ;[:last_run :map]+ ; See https://metaboat.slack.com/archives/C099RKNLP6U/p1765205882655869?thread_ts=1765205222.888209&cid=C099RKNLP6U+ #_[:target_stale :boolean]])++;; Global transforms precalculate these in a toucan hook - maybe we should do that too? Let's review later.+(defn- map-source-type [ws-tx]+ (-> ws-tx+ (assoc :source_type (transforms/transform-source-type (:source ws-tx)))+ (dissoc :source)))++(api.macros/defendpoint :get "/:ws-id/transform" :- [:map [:transforms [:sequential WorkspaceTransformListing]]]+ "Get all transforms in a workspace."+ {:access :workspace}+ [{:keys [ws-id]} :- [:map [:ws-id ::ws.t/appdb-id]]]+ (api/check-404 (t2/select-one :model/Workspace :id ws-id))+ {:transforms (->> (t2/select [:model/WorkspaceTransform :ref_id :global_id :name :source :creator_id]+ :workspace_id ws-id {:order-by [:created_at]})+ (map map-source-type))})++(defn- fetch-ws-transform [ws-id tx-id]+ (-> (select-model-malli-keys :model/WorkspaceTransform WorkspaceTransform workspace-transform-alias)+ (t2/select-one :ref_id tx-id :workspace_id ws-id)+ api/check-404+ attach-isolated-target))++(api.macros/defendpoint :get "/:ws-id/transform/:tx-id" :- WorkspaceTransform+ "Get a specific transform in a workspace."+ {:access :workspace}+ [{:keys [ws-id tx-id]} :- [:map [:ws-id ::ws.t/appdb-id] [:tx-id ::ws.t/ref-id]]]+ (fetch-ws-transform ws-id tx-id))++(api.macros/defendpoint :put "/:ws-id/transform/:tx-id" :- WorkspaceTransform+ "Update a transform in a workspace."+ {:access :workspace}+ [{:keys [ws-id tx-id]} :- [:map [:ws-id ::ws.t/appdb-id] [:tx-id ::ws.t/ref-id]]+ _query-params+ body :- [:map+ [:name {:optional true} :string]+ [:description {:optional true} [:maybe :string]]+ [:source {:optional true} ::transform-source]+ [:target {:optional true} ::transform-target]]]+ (t2/with-transaction [_tx]+ (api/check-404 (t2/select-one :model/WorkspaceTransform :ref_id tx-id :workspace_id ws-id))+ (let [source-or-target-changed? (or (:source body) (:target body))]+ (t2/update! :model/WorkspaceTransform {:workspace_id ws-id :ref_id tx-id} body)+ ;; If source or target changed, increment versions for re-analysis+ (when source-or-target-changed?+ (ws.impl/increment-analysis-version! ws-id tx-id)+ ;; It's unfortunate that we are using an extra SQL statement for this, rather than doing it in the earlier+ ;; statement that set the [[body]]. Combining them would be tricky, as we still need the model transforms to+ ;; serialize the fields in the body, but the increment requires using an SQL expression.+ (ws.impl/increment-graph-version! ws-id)))+ (fetch-ws-transform ws-id tx-id)))++(api.macros/defendpoint :post "/:ws-id/transform/:tx-id/archive" :- :nil+ "Mark the given transform to be archived when the workspace is merged.+ For provisional transforms we will skip even creating it in the first place."+ {:access :workspace}+ [{:keys [ws-id tx-id]} :- [:map [:ws-id ::ws.t/appdb-id] [:tx-id ::ws.t/ref-id]]]+ (api/check-404 (pos? (t2/update! :model/WorkspaceTransform+ {:ref_id tx-id :workspace_id ws-id}+ {:archived_at [:now]})))+ ;; Increment graph version since transform is leaving the graph+ (ws.impl/increment-graph-version! ws-id)+ nil)++(api.macros/defendpoint :post "/:ws-id/transform/:tx-id/unarchive" :- :nil+ "Unmark the given transform for archival. This will recall the last definition it had within the workspace."+ {:access :workspace}+ [{:keys [ws-id tx-id]} :- [:map [:ws-id ::ws.t/appdb-id] [:tx-id ::ws.t/ref-id]]]+ (api/check-404 (pos? (t2/update! :model/WorkspaceTransform+ {:ref_id tx-id :workspace_id ws-id}+ {:archived_at nil})))+ ;; Increment both versions - transform re-enters graph and needs re-analysis+ (ws.impl/increment-analysis-version! ws-id tx-id)+ ;; We could merge this with the initial WorkspaceTransform update to save a statement, but it adds complexity.+ (ws.impl/increment-graph-version! ws-id)+ nil)++(api.macros/defendpoint :delete "/:ws-id/transform/:tx-id" :- :nil+ "Discard a transform from the changeset.+ Equivalent to resetting a checked-out transform to its global definition, or deleting a provisional transform."+ {:access :workspace}+ [{:keys [ws-id tx-id]} :- [:map [:ws-id ::ws.t/appdb-id] [:tx-id ::ws.t/ref-id]]]+ (api/check-404 (pos? (t2/delete! :model/WorkspaceTransform :ref_id tx-id :workspace_id ws-id)))+ ;; Increment graph version since transform is potentially leaving the graph, or reverting to the global definition.+ (ws.impl/increment-graph-version! ws-id)+ nil)++(api.macros/defendpoint :post "/:ws-id/transform/:tx-id/run"+ :- ::ws.t/execution-result+ "Run a transform in a workspace.++ App DB changes are rolled back. Warehouse DB changes persist."+ {:access :workspace}+ [{:keys [ws-id tx-id]} :- [:map [:ws-id ::ws.t/appdb-id] [:tx-id ::ws.t/ref-id]]]+ (let [workspace (api/check-404 (t2/select-one :model/Workspace ws-id))+ transform (api/check-404 (t2/select-one :model/WorkspaceTransform :ref_id tx-id :workspace_id ws-id))+ _ (api/check-400 (not= :archived (:base_status workspace)) "Cannot execute archived workspace")+ _ (check-transforms-enabled! (:database_id workspace))+ graph (ws.impl/get-or-calculate-graph! workspace)]+ (ws.impl/run-transform! workspace graph transform)))++(api.macros/defendpoint :post "/:ws-id/transform/:tx-id/dry-run"+ :- ::ws.t/dry-run-result+ "Dry-run a transform in a workspace without persisting to the target table.++ Returns the first 2000 rows of transform output for preview purposes.+ Does not update last_run_at or create any database tables."+ {:access :workspace}+ [{:keys [ws-id tx-id]} :- [:map [:ws-id ::ws.t/appdb-id] [:tx-id ::ws.t/ref-id]]]+ (let [workspace (api/check-404 (t2/select-one :model/Workspace ws-id))+ transform (api/check-404 (t2/select-one :model/WorkspaceTransform :ref_id tx-id :workspace_id ws-id))+ _ (api/check-400 (not= :archived (:base_status workspace)) "Cannot execute archived workspace")+ _ (check-transforms-enabled! (:database_id workspace))+ graph (ws.impl/get-or-calculate-graph! workspace)]+ (ws.impl/dry-run-transform workspace graph transform)))++(def ^:private CheckoutTransformLegacy+ "Legacy format for workspace checkout transforms (DEPRECATED)."+ [:map+ [:id ::ws.t/ref-id]+ [:name :string]+ [:workspace [:map+ [:id ms/PositiveInt]+ [:name :string]]]])++(def ^:private WorkspaceWithCheckout+ "Workspace with checkout status information."+ [:map+ [:id ::ws.t/appdb-id]+ [:name :string]+ [:status ::status]+ [:existing [:maybe [:map+ [:ref_id ::ws.t/ref-id]+ [:name :string]]]]])++#_{:clj-kondo/ignore [:metabase/validate-defendpoint-route-uses-kebab-case]}+(api.macros/defendpoint :get "/checkout"+ :- [:map+ [:checkout_disabled [:maybe [:enum "mbql" "card-reference" "unknown-type"]]]+ [:workspaces [:sequential WorkspaceWithCheckout]]+ [:transforms [:sequential CheckoutTransformLegacy]]]+ "Get checkout status for a global transform.++ Returns:+ - checkout_disabled: Why this transform cannot be checked out (nil if allowed)+ - workspaces: All workspaces for this transform's database, with checkout status+ - transforms: (DEPRECATED) Use :workspaces instead"+ [_route-params+ {:keys [transform-id]} :- [:map {:closed true} [:transform-id ms/PositiveInt]]]+ (let [transform (api/check-404+ (t2/select-one [:model/Transform :id :target_db_id :source_type :source]+ :id transform-id))+ db-id (:target_db_id transform)+ workspaces (t2/select [:model/Workspace :id :name :base_status :db_status]+ :database_id db-id+ :base_status [:not= :archived]+ {:order-by [[:name :asc]]})+ checkouts (t2/select [:model/WorkspaceTransform :ref_id :name :workspace_id]+ :global_id transform-id)+ ws-id->checkouts (into {} (map (juxt :workspace_id identity) checkouts))+ id->workspace (into {} (map (juxt :id identity) workspaces))]+ {:checkout_disabled (checkout-disabled-reason transform)+ :workspaces (for [{:keys [id name] :as ws} workspaces+ :let [checkout (get ws-id->checkouts id)]]+ {:id id+ :name name+ :status (ws.model/computed-status ws)+ :existing (when checkout+ {:ref_id (:ref_id checkout)+ :name (:name checkout)})})+ :transforms (for [{:keys [ref_id name workspace_id]} checkouts+ :let [ws (get id->workspace workspace_id)]+ :when ws]+ {:id ref_id+ :name name+ :workspace {:id (:id ws) :name (:name ws)}})}))++(api.macros/defendpoint :post "/:ws-id/merge"+ :- [:or+ [:map+ [:merged+ [:map+ [:transforms [:sequential+ [:map+ [:op [:enum :create :delete :update :noop]]+ [:global_id {:optional true} [:maybe ::ws.t/appdb-id]]+ [:ref_id ::ws.t/ref-id]]]]]]+ [:errors+ [:sequential+ [:map+ [:op [:enum :create :delete :update :noop]]+ [:global_id {:optional true} [:maybe ::ws.t/appdb-id]]+ [:ref_id ::ws.t/ref-id]]]]+ [:workspace [:map [:id ::ws.t/appdb-id] [:name :string]]]]+ ;; error message from check-404 or check-400+ :string]+ "This will:+ 1. Update original transforms with workspace versions+ 2. Archive the workspace and clean up isolated resources+ Returns a report of merged entities, or error in errors key.++ Request body may include:+ - commit-message: A description of what changes are being merged (optional, will be required in future)"+ [{:keys [ws-id] :as _query-params} :- [:map [:ws-id ::ws.t/appdb-id]]+ _query-params+ {:keys [commit-message]+ :or {commit-message "Placeholder for merge commit message. Should be required on FE"}} :- [:map+ [:commit-message {:optional true} [:string {:min 1}]]]]+ (let [ws (u/prog1 (t2/select-one :model/Workspace :id ws-id)+ (api/check-404 <>)+ (api/check-400 (not= :archived (:base_status <>)) "Cannot merge an archived workspace"))+ {:keys [merged+ errors]} (-> (ws.merge/merge-workspace! ws api/*current-user-id* commit-message)+ (update :errors+ (partial mapv #(-> %+ (update :error (fn [e] (.getMessage ^Throwable e)))+ (set/rename-keys {:error :message})))))]+ (u/prog1+ {:merged merged+ :errors errors+ :workspace {:id ws-id, :name (:name ws)}}+ (when-not (seq errors)+ (ws.model/archive! ws)))))++(api.macros/defendpoint :post "/:ws-id/transform/:tx-id/merge"+ :- [:map+ [:op [:enum :create :delete :update :noop]]+ [:global_id [:maybe ::ws.t/appdb-id]]+ [:ref_id ::ws.t/ref-id]+ [:message {:optional true} :string]]+ "Merge single transform from workspace back to the core. If workspace transform is archived+ the corresponding core transform is deleted.++ Request body may include:+ - commit-message: A description of what changes are being merged (optional, will be required in future)"+ [{:keys [ws-id tx-id] :as _query-params} :- [:map+ [:ws-id ::ws.t/appdb-id]+ [:tx-id ::ws.t/ref-id]]+ _query-params+ {:keys [commit-message]+ :or {commit-message "Placeholder for merge commit message. Should be required on FE"}} :- [:map+ [:commit-message {:optional true} [:string {:min 1}]]]]+ (let [ws (api/check-404 (t2/select-one [:model/Workspace :id :name] :id ws-id))+ ws-transform (api/check-404 (t2/select-one :model/WorkspaceTransform :workspace_id ws-id :ref_id tx-id))+ ws-merge-id (t2/insert-returning-pk!+ :model/WorkspaceMerge+ {:workspace_id (:id ws)+ :workspace_name (:name ws)+ :commit_message commit-message+ :creator_id api/*current-user-id*})+ {:keys [error] :as result} (ws.merge/merge-transform! {:ws-transform ws-transform+ :workspace ws+ :workspace-merge-id ws-merge-id+ :merging-user-id api/*current-user-id*+ :commit-message commit-message})]+ (if error+ (throw (ex-info "Failed to merge transform."+ (-> result+ (dissoc :error)+ (assoc :status-code 500))+ error))+ result)))++;;; ---------------------------------------- Test Resources ----------------------------------------++(defn- shorthand-ref?+ "Check if string matches shorthand pattern like x1, t1, x23, etc."+ [s]+ (and (string? s) (boolean (re-matches #"[xt]\d+" s))))++(defn- parse-magic-references+ "Recursively walk data structure converting shorthand refs to keywords."+ [x]+ (walk/postwalk+ (fn [v]+ (if (shorthand-ref? v)+ (keyword v)+ v))+ x))++(when (or config/is-dev? config/is-test?)+ (mr/def ::dependency-graph+ "Map of shorthand symbols to lists of dependencies. The latter are all strings, and refs need to be detected."+ [:map-of :keyword [:sequential :string]])++ (mr/def ::test-resources-request+ "Request body for creating test resources."+ [:map+ [:database_id {:optional true} ::ws.t/appdb-id]+ [:global {:optional true} ::dependency-graph]+ [:workspace {:optional true} [:map+ [:name {:optional true} :string]+ [:checkouts {:optional true} [:sequential :keyword]]+ [:definitions {:optional true} ::dependency-graph]]]])++ (mr/def ::test-resources-response+ "Response from creating test resources."+ [:map+ [:workspace-id [:maybe :int]]+ [:global-map [:map-of [:or :keyword :string] :int]]+ [:workspace-map [:map-of :keyword :string]]])++ (api.macros/defendpoint :post "/test-resources" :- ::test-resources-response+ "Create test resources for workspace e2e tests. Only available in dev/test mode.++ Optionally accepts a database_id in the body; if not provided it will try to use the sample database."+ [_route-params+ _query-params+ {db-id :database_id :as body} :- ::test-resources-request]+ (if-let [create-fn (requiring-resolve 'metabase-enterprise.workspaces.test-util/create-resources!)]+ (create-fn (-> body+ (dissoc :database_id)+ (cond-> db-id (assoc :database-id db-id))+ parse-magic-references))+ (throw (ex-info "Workspace test utilities not available" {:status-code 501})))))++(def ^{:arglists '([request respond raise])} routes+ "`/api/ee/workspace/` routes."+ (api.macros/ns-handler *ns* +authorize +auth))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/common.clj b/enterprise/backend/src/metabase_enterprise/workspaces/common.cljnew file mode 100644index 00000000000..9a9bb280922--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/common.clj@@ -0,0 +1,176 @@+(ns metabase-enterprise.workspaces.common+ (:require+ [clojure.string :as str]+ [metabase-enterprise.transforms.interface :as transforms.i]+ [metabase-enterprise.workspaces.isolation :as ws.isolation]+ [metabase-enterprise.workspaces.models.workspace-log :as ws.log]+ [metabase-enterprise.workspaces.util :as ws.u]+ [metabase.api-keys.core :as api-key]+ [metabase.api.common :as api]+ [metabase.util.log :as log]+ [metabase.util.quick-task :as quick-task]+ [toucan2.core :as t2]))++(defn- extract-suffix-number+ "Extract the numeric suffix from a workspace name like 'Foo (3)', or nil if no valid suffix."+ [ws-name base-name]+ (when (= (str/replace ws-name #"\s*\(\d+\)$" "") base-name)+ (when-let [[_ n] (re-find #"\((\d+)\)$" ws-name)]+ (parse-long n))))++(defn- generate-unique-workspace-name+ "Generate a unique workspace name by appending (N) if the name already exists.++ Strategy:+ 1. If the base name doesn't exist, use it as-is+ 2. If it exists, strip any existing (N) suffix and find the highest N among similar names+ 3. Return the base name with (N+1) appended++ Note: This function has a potential race condition between checking existence and insertion.+ Callers should handle unique constraint violations by retrying."+ [base-name]+ (when (str/blank? base-name)+ (throw (ex-info "Workspace name cannot be empty" {:status-code 400})))+ (if-not (t2/exists? :model/Workspace :name base-name)+ base-name+ (let [stripped-name (str/replace base-name #"\s*\(\d+\)$" "")+ existing (t2/select-fn-set :name :model/Workspace+ :name [:like (str stripped-name " (%)")])+ numbers (keep #(extract-suffix-number % stripped-name) existing)+ next-num (inc (apply max 0 numbers))]+ (str stripped-name " (" next-num ")"))))++(defn- create-workspace-container!+ "Create the workspace and its related collection, user, and api key."+ [creator-id db-id workspace-name]+ (let [key-name (format "API key for Workspace: %s" workspace-name)+ ;; Ensure the key-name is unique.+ key-name (if-not (t2/exists? :model/ApiKey :name key-name)+ key-name+ ;; Rather than figuring out all the escapes we need for [[workspace-name]], just over fetch.+ (let [used? (t2/select-fn-set :name [:model/ApiKey :name] :name [:like "API key for Workspace: %"])]+ ;; By trying N + 1 names, we know at least 1 of them must be available.+ (first (remove used? (for [i (range (inc (count used?)))]+ (str key-name "(" (inc i) ")"))))))+ api-key (api-key/create-api-key-with-new-user! {:key-name key-name})+ ws (t2/insert-returning-instance! :model/Workspace+ {:name workspace-name+ :creator_id creator-id+ :database_id db-id+ :api_key_id (:id api-key)+ :execution_user (:user_id api-key)+ :base_status :empty+ :db_status :uninitialized})+ coll (t2/insert-returning-instance! :model/Collection+ {:name (format "Collection for Workspace %s" workspace-name)+ :namespace "workspace"+ :workspace_id (:id ws)})+ ws (assoc ws :collection_id (:id coll))]+ ;; Set the backlink from the workspace to the collection inside it and set the schema.+ (t2/update! :model/Workspace (:id ws) {:collection_id (:id coll)})+ ;; TODO (Sanya 2025-11-18) -- For now we expose this in logs for manual testing. In future we need a secure channel.+ (log/infof "Generated API key for workspace: %s" (:unmasked_key api-key))+ ws))++(defn- unique-constraint-violation?+ "Check if an exception is due to a unique constraint violation."+ [e]+ (let [msg (str (ex-message e) (some-> (ex-cause e) ex-message))]+ (or (str/includes? msg "unique")+ (str/includes? msg "duplicate")+ (str/includes? msg "UNIQUE"))))++(defn- run-workspace-setup!+ "Background job: runs isolation, grants. Updates db_status to :ready when done."+ [{ws-id :id :as workspace} database]+ (try+ (ws.log/track! ws-id :workspace-setup+ (let [isolation-details (ws.log/track! ws-id :database-isolation+ (ws.isolation/ensure-database-isolation! workspace database))]+ (t2/update! :model/Workspace ws-id (merge (select-keys isolation-details [:schema :database_details])+ {:db_status :ready}))))+ (catch Exception e+ (log/error e "Failed to setup workspace")+ (t2/update! :model/Workspace ws-id {:db_status :broken})+ (throw e))))++(defn initialize-workspace!+ "Initialize an uninitialized workspace with the given database_id.+ Updates database_id (if different from provisional), sets schema, creates isolation resources async,+ and transitions db_status to :pending. Returns the updated workspace with schema set."+ [workspace database-id]+ (let [database (t2/select-one :model/Database database-id)+ schema (ws.u/isolation-namespace-name workspace)+ res (t2/update! :model/Workspace {:id (:id workspace)+ :db_status :uninitialized}+ {:database_id database-id+ :schema schema+ :db_status :pending})]+ (when (zero? res)+ (let [new-db-id (t2/select-one-fn :database_id :model/Workspace (:id workspace))]+ (when (not= database-id new-db-id)+ (throw (ex-info "Workspace has been initialized already with a different database"+ {:requested-db-id database-id+ :actual-db-id new-db-id})))))+ (let [ws (t2/select-one :model/Workspace (:id workspace))]+ ;; TODO (Chris 2026-02-02) -- Allow this to be fully async as part of BOT-746.+ (try+ @(quick-task/submit-task! #(run-workspace-setup! ws database))+ (catch Exception e+ (log/error e "Failed to initialize workspace")))+ ;; Querying again to get the database_details+ (t2/select-one :model/Workspace (:id workspace)))))++(defn- create-uninitialized-workspace!+ "Create a workspace with a provisional database_id but no isolation resources.+ Retries on unique constraint violations for workspace name."+ [creator-id db-id ws-name max-retries]+ (loop [attempt 1]+ (let [unique-name (generate-unique-workspace-name ws-name)+ {:keys [retry+ workspace]} (try+ {:workspace (create-workspace-container! creator-id db-id unique-name)}+ (catch Exception e+ (if (and (< attempt max-retries) (unique-constraint-violation? e))+ {:retry true}+ (throw e))))]+ (if retry+ (recur (inc attempt))+ workspace))))++(defn create-workspace!+ "Create workspace."+ [creator-id {ws-name-maybe :name+ db-id :database_id}]+ (let [ws-name (or ws-name-maybe (ws.u/generate-name))]+ (create-uninitialized-workspace! creator-id db-id ws-name 5)))++(defn add-to-changeset!+ "Add the given transform to the workspace changeset.+ If workspace db_status is uninitialized, initializes it with the transform's target database.+ If workspace base_status is empty, transitions it to active."+ [creator-id workspace entity-type global-id body]+ (ws.u/assert-transform! entity-type)+ ;; Initialize workspace if uninitialized (outside transaction so async task can see committed data)+ (let [workspace (if (= :uninitialized (:db_status workspace))+ (let [target-db-id (transforms.i/target-db-id body)]+ (api/check-400 target-db-id "Transform must have a target database")+ (initialize-workspace! workspace target-db-id))+ workspace)]+ (t2/with-transaction [_]+ (let [workspace-id (:id workspace)+ workspace-db-id (:database_id workspace)+ body (assoc-in body [:target :database] workspace-db-id)+ transform (ws.u/insert-returning-ws-tx!+ (assoc (select-keys body [:name :description :source :target])+ :ref_id (ws.u/generate-ref-id)+ :creator_id creator-id+ :global_id global-id+ :workspace_id workspace-id))]+ (t2/update! :model/Workspace workspace-id+ ;; Increment graph_version since new transform = graph needs recalculation+ (cond-> {:graph_version [:+ :graph_version 1]}+ ;; Transition base_status from :empty to :active when first transform is added+ (= :empty (:base_status workspace))+ (assoc :base_status :active)))+ transform))))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/core.clj b/enterprise/backend/src/metabase_enterprise/workspaces/core.cljnew file mode 100644index 00000000000..9346331b947--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/core.clj@@ -0,0 +1,2 @@+;; For now, we do not anticipate any non-API usage of this module.+(ns metabase-enterprise.workspaces.core)diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/dag.clj b/enterprise/backend/src/metabase_enterprise/workspaces/dag.cljnew file mode 100644index 00000000000..d218e19c594--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/dag.clj@@ -0,0 +1,298 @@+(ns metabase-enterprise.workspaces.dag+ "Used to do graph computations with respect to dataflow within a workspace.++ This namespace should encapsulate all coupling to the [[metabase-enterprise.dependencies]] module, including through+ database artifacts.++ It is purely concerned with calculation, any side effects wanted as a result of the analysis should happen outside."+ (:require+ [metabase-enterprise.workspaces.util :as ws.u]+ [metabase.util :as u]+ [toucan2.core :as t2]+ [toucan2.realize :as t2.realize]))++;; TODO (Chris 2025-12-15) -- Should we leverage data-authority table metadata to speed things up?++(defn- table-id->coord [id]+ (t2/select-one [:model/Table :id :schema [:name :table] [:db_id :db]] id))++(defn- target->coord [{:keys [database schema name] :as _target}]+ {:db database+ :schema schema+ :table name})++(defn- global-parents+ "Dependencies analyzed by global hooks - may be incomplete. For example, excluding transforms that have not been run."+ [ws-id node-type id]+ ;; Note: from_entity_type is transformed to a keyword by the Dependency model+ (t2/select-fn-vec (fn [{id :to_entity_id entity-type :to_entity_type}]+ (case entity-type+ :card {:node-type :external-card, :id id}+ :table {:node-type :table, :id (table-id->coord id)}+ :transform {:node-type :external-transform, :id id}))+ [:model/Dependency :to_entity_type :to_entity_id]+ :from_entity_type node-type+ :from_entity_id id+ ;; Exclude transforms which are being overridden in the workspace.+ ;; Note: use string "transform" in SQL WHERE clause (db stores strings)+ {:where [:not [:and+ [:= "transform" :to_entity_type]+ [:exists {:select [1]+ :from [[:workspace_transform :wt]]+ :where [:and+ [:= :wt.workspace_id ws-id]+ [:= :wt.global_id :to_entity_id]]}]]]}))++;; Workaround for change in how Dependencies table works (inlining of transform dependencies)+(defn- global-tx-parents+ "Dependencies analyzed by global hooks - may be incomplete. For example, excluding transforms that have not been run."+ [id]+ ;; Note: from_entity_type is transformed to a keyword by the Dependency model+ (mapv+ (fn [{:keys [node-type id] :as node}]+ (if (= :external-transform node-type)+ {:node-type :table, :id (target->coord (t2/select-one-fn :target [:model/Transform :target] id))}+ node))+ (t2/select-fn-vec (fn [{id :to_entity_id entity-type :to_entity_type}]+ (case entity-type+ :card {:node-type :external-card, :id id}+ :table {:node-type :table, :id (table-id->coord id)}+ :transform {:node-type :external-transform, :id id}))+ [:model/Dependency :to_entity_type :to_entity_id]+ :from_entity_type "transform"+ :from_entity_id id)))++(defn- ws-transform-parents [ws-id ref-id]+ ;; We assume there are no card dependencies yet+ ;; Get the latest version - which may have changed since we started this graph analysis (!!!)+ ;; Unfortunately, since each transform is versioned separately, it's not trivial to freeze to a snapshot.+ ;; NOTE: Perhaps this is a reason to snapshot these rows, or pre-query them all :thinking:+ (t2/select-fn-vec (fn [table-coord]+ {:node-type :table, :id (t2.realize/realize table-coord)})+ [:model/WorkspaceInput [:db_id :db] :schema :table [:table_id :id]]+ :workspace_id ws-id+ :ref_id ref-id+ {:where [:= :transform_version+ {:select [[:%max.transform_version]]+ :from [:workspace_input]+ :where [:and+ [:= :workspace_id ws-id]+ [:= :ref_id ref-id]]}]}))++(defn- table-producers [ws-id id-or-coord]+ ;; Work with either logical co-ords or an id+ (let [{:keys [db schema table id]} (if (map? id-or-coord) id-or-coord (table-id->coord id-or-coord))+ ;; Get the latest version - which may have changed since we started this graph analysis (!!!)+ ;; Unfortunately, since each transform is versioned separately, it's not trivial to freeze to a snapshot.+ ;; NOTE: Perhaps this is a reason to snapshot these rows, or pre-query them all :thinking:+ tx-ref-id (t2/select-one-fn :ref_id [:model/WorkspaceOutput :ref_id]+ {:where [:and+ [:= :workspace_id ws-id]+ [:or+ (when id+ [:= :global_table_id id])+ [:and+ [:= :db_id db]+ [:= :global_schema schema]+ [:= :global_table table]]]]+ :order-by [[:id :desc]]+ :limit 1})]+ (if tx-ref-id+ ;; If there is a workspace transform that targets this table, ignore any global transform that also targets it.+ [{:node-type :workspace-transform :id tx-ref-id}]+ (global-parents ws-id "table" id))))++(defn- table? [{:keys [node-type]}] (= :table node-type))++(defn- node-parents [ws-id {:keys [node-type id]}]+ (case node-type+ :workspace-transform (ws-transform-parents ws-id id)+ :external-transform (global-tx-parents id)+ :external-card (global-parents ws-id "card" id)+ :table (table-producers ws-id id)))++(defn- node->allowed-parents+ [pred deps node]+ (if-not (pred node)+ [node]+ ;; Collapse to the things that *produce* node. If it is a root node, leave it in.+ (or (seq (mapcat (partial node->allowed-parents pred deps) (deps node)))+ [node])))++(defn- collapse+ "Remove nodes that satisfy pred, in-lining their transitive dependencies instead."+ [pred deps]+ (u/for-map [[child parents] deps+ :when (not (pred child))]+ [child (mapcat (partial node->allowed-parents pred deps) parents)]))++(defn- render-graph [entities parents deps & {:keys [table? table-sort unwrap-table]+ :or {table? table?+ table-sort (juxt :db :schema :table)+ unwrap-table :id}}]+ (let [table-nodes (filter table? entities)+ ;; Any table that has a parent in the subgraph is an output+ outputs (filter deps table-nodes)+ ;; Anything other parent table is an input+ inputs (->> entities (mapcat parents) (filter table?) (remove (set outputs)) distinct)+ entities (->> (ws.u/toposort-dfs deps) (remove table?))+ deps+input (reduce+ (fn [deps [c parents]]+ (if-not (deps c)+ deps+ (update deps c into parents)))+ deps+ parents)]+ {:inputs (sort-by table-sort (map unwrap-table inputs))+ :outputs (sort-by table-sort (map unwrap-table outputs))+ :entities entities+ ;; collapse tables out, directly connecting transforms? smaller graphs are easier for humans to read+ :dependencies (collapse table? deps+input)}))++(defn- path-induced-subgraph*+ "Implementation for [[path-induced-subgraph]] that takes the lookup functions as arguments. Useful for testing."+ [init-nodes fns]+ (loop [members (set init-nodes)+ cache {}+ ;; Association list sets from nodes to their direct dependencies+ deps (u/for-map [node init-nodes] [node #{}])+ ;; Paths are vectors sorted from child to parent+ [path & paths] (for [node members] [node])]+ (if-not path+ ;; Finished walking all paths, render the result.+ (render-graph members cache deps fns)+ ;; Look-up from cache first+ (let [_ (when (some #(not= 1 (val %)) (frequencies path))+ (throw (ex-info "Cycle detected" {:path path})))+ head (peek path)+ parents-fn (:node-parents fns)+ parents (cache head (parents-fn head))+ cache (assoc cache head parents)+ continue (when parents (remove members parents))]+ (if (not= parents continue)+ ;; At least one parent is in the enclosed subgraph, so this entire path is as well.+ (let [add-dep (fn [deps [child parent]]+ (update deps child (fnil conj #{}) parent))+ ;; Break up the path into adjacent child-parent pairs.+ pairs (concat (partition 2 1 path)+ (map (fn [end] [(peek path) end]) (filter members parents)))+ ;; Since everything along this path will now be added to deps already, we can truncate the paths.+ next-paths (into paths (for [c continue] [(peek path) c]))]+ (recur (into members path) cache (reduce add-dep deps pairs) next-paths))+ ;; We have not reached another member of the enclosed subgraph yet, so keep extending.+ (recur members cache deps (into paths (for [c continue] (conj path c)))))))))++;;;; Public API++(defn reverse-graph+ "Reverse edge direction: child->parents becomes parent->children.+ Takes a dependency map and returns a map where each parent points to its children."+ [deps-map]+ (reduce (fn [m [child parents]]+ (reduce (fn [acc parent]+ (update acc parent (fnil conj []) child))+ m+ parents))+ {}+ deps-map))++(defn bfs-reduce+ "BFS traversal from start node(s) through an edge map.++ edges-map - adjacency map from node to its neighbors+ starts - a collection of start nodes++ Options:+ - :include-start? - include start node(s) in result (default: false)+ - :rf - reducing function for collecting results (default: conj)+ - :init - initial value for the result accumulator (default: [])++ Returns the accumulated result in BFS order (no duplicates)."+ [edges-map starts & {:keys [include-start? rf init]+ :or {rf conj, init []}}]+ (let [init-set (set starts)+ get-children #(get edges-map % [])+ ;; Queue always starts with children of start nodes (excluding start nodes themselves)+ init-children (into [] (comp (mapcat get-children) (remove init-set)) starts)]+ (loop [queue init-children+ ;; Only mark start nodes as visited if we're including them in result+ ;; This allows routes between start nodes to be detected when include-start? is false+ visited (transient (if include-start? init-set #{}))+ result (if include-start? (reduce rf init starts) init)]+ (if (empty? queue)+ result+ (let [current (first queue)+ queue (subvec queue 1)]+ (if (visited current)+ (recur queue visited result)+ (let [children (get-children current)+ new-nodes (remove visited children)]+ (recur (into queue new-nodes)+ (conj! visited current)+ (rf result current)))))))))++(defn path-induced-subgraph+ "Given a list of internal entities, compute the path-induced subgraph.++ Returns a map with:+ - :inputs - tables that the subgraph directly depends on, that are not themselves produced by the subgraph.+ ordered lexically by db+schema+table, to be stable across metabase instances, and visibly sorted.+ - :outputs - outputs of both the changeset transforms, and the external transforms enclosed by the changeset.+ both the external names, and the internal (isolated) names. sorted by the external names.+ point back to their relevant transform.++ - :entities - the full list of entities, both internal, and those that are enclosed. topo-sorted.+ - :dependencies - association list for the subgraph, with keys and values both in topological order"+ [ws-id internal-entities]+ (ws.u/assert-transforms! internal-entities)+ (if (empty? internal-entities)+ {:inputs []+ :outputs []+ :entities []+ :dependencies {}}+ (let [tx-nodes (for [{:keys [entity-type id]} internal-entities]+ (case entity-type+ :transform {:node-type :workspace-transform, :id id}))+ ->latest (fn [versioned-entities]+ (let [versions (u/group-by (juxt :node-type :id) versioned-entities)+ latest? (into #{}+ (map (fn [[entity versions]]+ (if (= 1 (count versions))+ (first versions)+ (assoc entity :version (transduce (map :version) max versions)))))+ versions)]+ (filter latest? versioned-entities)))+ outputs (when (seq tx-nodes)+ (->latest+ (t2/select-fn-vec (fn [row]+ {:node-type :table+ :id (t2.realize/realize row)+ :version (:version row)})+ ;; We use workspace_output.id (i.e. analysis write order) as a proxy for+ ;; the order in which the corresponding transforms were updated.+ [:model/WorkspaceOutput+ [:id :version]+ [:db_id :db]+ [:global_schema :schema]+ [:global_table :table]+ [:global_table_id :id]]+ :workspace_id ws-id+ :ref_id [:in (map :id tx-nodes)])))+ init-nodes (concat tx-nodes outputs)+ fns {:node-parents (partial node-parents ws-id)+ :table? table?}]+ (path-induced-subgraph* init-nodes fns))))++;; source-table ---------> checked-out-transform+;; \____external-transform__/+;;+;; should external-transform be enclosed? answer: no, because it won't become stale from changes in the workspace+;;+;; source-table ----> model ---> checked-out-transform+;;+;; this time we *need* the model to be serialized, because we need to read it in a fresh instance.+;; it doesn't need to be remapped or "run" independently though, so does it make sense to call it enclosed?+;;+;; enclosed: between changeset only? or including the model in the example above?+;; ancestors: as far back as you can go+;;diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/dependencies.clj b/enterprise/backend/src/metabase_enterprise/workspaces/dependencies.cljnew file mode 100644index 00000000000..8ab567e6ac9--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/dependencies.clj@@ -0,0 +1,310 @@+(ns metabase-enterprise.workspaces.dependencies+ "Workspace-local dependency tracking.++ Unlike the global `dependency` table which links to `table_id` (requiring the table to exist),+ workspace dependencies use two tables that support logical references:++ - `workspace_input` - external tables transforms consume (may not exist yet), with ref_id linking to transform+ - `workspace_output` - tables transforms produce++ ## Example: Internal Dependency (Transform B depends on Transform A's output)++ Transform A produces `analytics.orders_summary`:+ ```+ {:ref_id \"happy-dolphin-a1b2\"+ :target {:database 1 :schema \"analytics\" :name \"orders_summary\"}}+ ```++ Transform B reads from `analytics.orders_summary`:+ ```+ {:ref_id \"brave-lion-g7h8\"+ :source {:type \"query\" :query \"SELECT * FROM analytics.orders_summary\"}+ :target {:database 1 :schema \"analytics\" :name \"orders_report\"}}+ ```++ After processing both transforms:++ workspace_output:+ | id | ref_id | schema | table |+ |-----|--------------------|-----------|----------------|+ | 101 | happy-dolphin-a1b2 | analytics | orders_summary |+ | 104 | brave-lion-g7h8 | analytics | orders_report |++ Transform B depends on Transform A's output - this is detected by matching table coordinates+ against workspace_output records during DAG traversal (no explicit edge storage needed).++ If Transform A also depended on an external table ORDERS:++ workspace_input:+ | id | ref_id | schema | table | table_id |+ |-----|-------------------|--------|--------|----------|+ | 201 | happy-dolphin-a1b2 | PUBLIC | ORDERS | 42 |++ The ref_id directly links the input to its transform, no join table needed."+ (:require+ [metabase-enterprise.workspaces.models.workspace-input]+ [metabase-enterprise.workspaces.models.workspace-output]+ [metabase-enterprise.workspaces.util :as ws.u]+ [metabase.driver :as driver]+ [metabase.driver.sql :as driver.sql]+ ;; TODO (Chris 2025-12-17) -- I solemnly declare that we will clean up this coupling nightmare for table normalization+ ^{:clj-kondo/ignore [:metabase/modules]}+ [metabase.driver.sql.normalize :as sql.normalize]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.util :as u]+ [metabase.util.log :as log]+ [metabase.util.malli :as mu]+ [metabase.util.malli.registry :as mr]+ [metabase.util.performance :as perf]+ [toucan2.core :as t2]))++;;; ---------------------------------------- Schemas ----------------------------------------++(mr/def ::table-ref+ "A logical reference to a table by schema and name. Includes table_id if the table exists."+ [:map+ [:db_id :int]+ [:schema [:maybe :string]]+ [:table :string]+ [:table_id {:optional true} [:maybe :int]]])++(mr/def ::output-ref+ "A reference to an output table (transform target)."+ [:map+ [:db_id :int]+ [:schema [:maybe :string]]+ [:table :string]])++(mr/def ::analysis+ "The result of analyzing an entity's dependencies."+ [:map+ [:output ::output-ref]+ [:inputs [:sequential ::table-ref]]])++;;; ---------------------------------------- Batch table lookups ----------------------------------------++(defn- batch-table-refs-from-ids+ "Look up table metadata for multiple table IDs in a single query.+ Returns a map of table-id -> {:db_id :schema :table :table_id}."+ [table-ids]+ (when (seq table-ids)+ (t2/select-fn->fn :id+ (fn [{:keys [id db_id schema name]}]+ {:db_id db_id+ :schema schema+ :table name+ :table_id id})+ [:model/Table :id :db_id :schema :name]+ :id [:in table-ids])))++(defn- batch-lookup-table-ids+ "Look up table_ids for multiple logical table references in a single query.+ Uses the same matching logic as find-table-or-transform: if parsed schema is nil,+ it matches against the driver's default schema.+ Returns a map of [db_id search-schema table] -> table_id where search-schema+ is the schema used for lookup (defaulted if nil)."+ [driver db-id table-refs]+ (when (seq table-refs)+ (let [default-schema (driver.sql/default-schema driver)+ ;; Include default-schema in filter so nil refs can match that too+ nil-schema? (some (comp nil? :schema) table-refs)+ schemas-to-query (into #{} (map #(or (:schema %) default-schema)) table-refs)+ db-table-lookup (t2/select-fn->fn (juxt :schema :name) :id+ [:model/Table :id :schema :name]+ :db_id db-id+ :name [:in (map :table table-refs)]+ {:where (if nil-schema?+ [:or [:= :schema nil] [:in :schema schemas-to-query]]+ [:in :schema schemas-to-query])})]+ (into {}+ (keep (fn [{:keys [schema table]}]+ (let [table-id (or (get db-table-lookup [schema table])+ (when (nil? schema)+ (get db-table-lookup [default-schema table])))]+ (when table-id+ [[db-id schema table] table-id]))))+ table-refs))))++(defn- output-from-target+ "Extract output table info from a transform's :target field."+ [{:keys [database schema name] :as _target}]+ {:db_id database+ :schema schema+ :table name})++;;; ---------------------------------------- Input extraction ----------------------------------------++(defn- inputs-from-mbql-query+ "Extract table refs from an MBQL query. Tables are looked up by ID in batch."+ [query]+ (let [table-ids (-> #{}+ (into (lib/all-source-table-ids query))+ (into (lib/all-implicitly-joined-table-ids query)))+ table-refs (batch-table-refs-from-ids table-ids)]+ (u/keepv table-refs table-ids)))++(defn- inputs-from-native-query+ "Extract table refs from a native query. Uses driver/native-query-table-refs to get+ schema+table directly, then batch looks up table_ids for tables that exist.+ Uses the same default schema logic as find-table-or-transform."+ [query db-id]+ (let [driver (:engine (lib.metadata/database query))+ parsed-refs (driver/native-query-table-refs driver query)+ ;; Batch lookup existing table IDs (handles default schema logic)+ table-id-map (batch-lookup-table-ids driver db-id parsed-refs)]+ (perf/for [{:keys [schema table]} parsed-refs+ :let [table-id (get table-id-map [db-id schema table])]]+ (cond-> {:db_id db-id+ :schema schema+ :table table}+ table-id (assoc :table_id table-id)))))++(defn- inputs-from-query+ "Extract table refs from a query (MBQL or native)."+ [query db-id]+ (if (lib/native-only-query? query)+ (inputs-from-native-query query db-id)+ (inputs-from-mbql-query query)))++(defn- inputs-from-python-transform+ "Extract table refs from a python transform's source-tables.+ Python transforms require tables to exist (they map name -> table_id). Batch lookup."+ [source-tables]+ (let [table-ids (set (vals source-tables))+ table-refs (batch-table-refs-from-ids table-ids)]+ (u/keepv table-refs (vals source-tables))))++(mu/defn analyze-entity :- ::analysis+ "Analyze a workspace entity to find its dependencies.++ Arguments:+ - entity-type: keyword, must be :transform (asserted)+ - entity: the workspace transform map with :source and :target++ Returns:+ {:output {:db_id int :schema string :table string}+ :inputs [{:db_id int :schema string :table string} ...]}"+ [entity-type :- :keyword+ entity :- :map]+ (ws.u/assert-transform! entity-type)+ (let [{{:keys [query source-tables], source-type :type} :source+ target :target} entity+ db-id (:database target)+ inputs (cond+ (= (keyword source-type) :query)+ (inputs-from-query query db-id)++ (= (keyword source-type) :python)+ (inputs-from-python-transform source-tables)++ :else+ (do (log/warnf "Don't know how to analyze deps of transform with source type '%s'" source-type)+ []))]+ {:output (output-from-target target)+ :inputs inputs}))++;;; ---------------------------------------- Write helpers ----------------------------------------++(defn- insert-workspace-output!+ "Insert a workspace_output record for the transform's target table with the given transform_version.+ Stores both global (original) and isolated (workspace-specific) table identifiers.+ With epochal versioning, we always insert new rows - cleanup of old versions happens separately.+ Silently ignores constraint violations from concurrent inserts."+ [workspace-id ref-id isolated-schema {:keys [db_id schema table]} normalize-sql transform-version]+ (let [isolated-table (ws.u/isolated-table-name schema table)+ qry-table-id (fn [s t]+ (or (t2/select-one-fn :id [:model/Table :id]+ :db_id db_id+ :schema (normalize-sql s)+ :name (normalize-sql t))+ ;; Turns out transforms don't normalize the metadata they create+ ;; TODO (Chris 2026-01-26) -- This is getting really tangled and expensive, revisit+ (t2/select-one-fn :id [:model/Table :id]+ :db_id db_id+ :schema s+ :name t)))+ global-table-id (qry-table-id schema table)+ isolated-table-id (qry-table-id isolated-schema isolated-table)]+ (ws.u/ignore-constraint-violation+ (t2/insert! :model/WorkspaceOutput+ {:workspace_id workspace-id+ :ref_id ref-id+ :transform_version transform-version+ :db_id db_id+ :global_schema schema+ :global_table table+ :global_table_id global-table-id+ :isolated_schema isolated-schema+ :isolated_table isolated-table+ :isolated_table_id isolated-table-id}))))++(defn- normalize-input-schema+ "Normalize an input's schema: replace nil with the driver's default schema.+ This ensures consistent comparison with output schemas which are always explicit."+ [default-schema input]+ (update input :schema #(or % default-schema)))++(defn- insert-workspace-inputs!+ "Insert workspace_input records for the given inputs with the given transform_version.+ Each input row is linked to a specific transform via ref_id.+ With epochal versioning, we always insert new rows - cleanup of old versions happens separately.+ Silently ignores constraint violations from concurrent inserts."+ [workspace-id ref-id inputs transform-version]+ (when (seq inputs)+ (ws.u/ignore-constraint-violation+ (t2/insert! :model/WorkspaceInput+ (for [{:keys [db_id schema table table_id]} inputs]+ {:workspace_id workspace-id+ :ref_id ref-id+ :transform_version transform-version+ :db_id db_id+ :schema schema+ :table table+ :table_id table_id}))))+ nil)++(defn transform-output-exists-for-version?+ "Check if workspace_output exists for a transform at the given version or later."+ [ws-id ref-id transform-version]+ (t2/exists? :model/WorkspaceOutput+ :workspace_id ws-id+ :ref_id ref-id+ :transform_version [:>= transform-version]))++(mu/defn write-entity-analysis! :- :nil+ "Persist dependency analysis for a workspace entity.++ Arguments:+ - workspace-id: int+ - isolated-schema: string, the workspace's isolated schema name+ - entity-type: keyword, must be :transform+ - ref-id: string, the workspace_transform.ref_id+ - analysis: result from analyze-entity+ - transform-version: long, the analysis_version to write (for epochal versioning)++ Side effects:+ - Inserts workspace_output for this transform (with both global and isolated identifiers)+ - Inserts workspace_input records for this transform's external dependencies+ - Old version cleanup happens separately in impl.clj"+ [workspace-id :- :int+ isolated-schema :- :string+ entity-type :- :keyword+ ref-id :- :string+ {:keys [output inputs]} :- ::analysis+ transform-version :- :int]+ (ws.u/assert-transform! entity-type)+ (t2/with-transaction [_conn]+ (when-not (transform-output-exists-for-version? workspace-id ref-id transform-version)+ (let [driver (t2/select-one-fn :engine [:model/Database :engine]+ :id [:in {:select [:database_id]+ :from [:workspace]+ :where [:= :id workspace-id]}])+ normalize (partial sql.normalize/normalize-name driver)+ default-schema (driver.sql/default-schema driver)+ ;; Normalize external inputs so schemas are consistent+ normalized-inputs (map (partial normalize-input-schema default-schema) inputs)]+ ;; Insert inputs first, then output - output row acts as "commit marker" for version check+ (insert-workspace-inputs! workspace-id ref-id normalized-inputs transform-version)+ (insert-workspace-output! workspace-id ref-id isolated-schema output normalize transform-version)+ nil))))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/events.clj b/enterprise/backend/src/metabase_enterprise/workspaces/events.cljnew file mode 100644index 00000000000..f674d60af07--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/events.clj@@ -0,0 +1,33 @@+(ns metabase-enterprise.workspaces.events+ "Event handlers to increment workspace graph versions when global transforms change."+ (:require+ [metabase.events.core :as events]+ [metabase.util.log :as log]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++;; When a global transform is created, updated, or deleted, increment graph_version for all workspaces.+;; This is a conservative approach - we could be more selective by tracking which workspaces+;; reference which transforms, but the complexity of doing so would be brittle.++(defn- increment-all-workspace-graph-versions!+ "Increment graph_version for all workspaces, triggering graph recalculation."+ []+ (let [result (t2/query {:update :workspace+ :set {:graph_version [:+ :graph_version 1]}})]+ (when (pos? (:next.jdbc/update-count result 0))+ (log/debug "Incremented graph_version for all workspaces due to global transform change"))))++;; These events are published by metabase-enterprise.transforms.api when transforms are+;; created, updated, or deleted. We subscribe to increment graph versions.++(derive ::workspace-staleness :metabase/event)+(derive :event/transform-create ::workspace-staleness)+(derive :event/transform-update ::workspace-staleness)+(derive :event/transform-delete ::workspace-staleness)++(methodical/defmethod events/publish-event! ::workspace-staleness+ [_ _event]+ ;; Increment graph_version for all workspaces when any global transform changes.+ ;; The graph will be recalculated lazily the next time we need it.+ (increment-all-workspace-graph-versions!))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/execute.clj b/enterprise/backend/src/metabase_enterprise/workspaces/execute.cljnew file mode 100644index 00000000000..1a712cd0f14--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/execute.clj@@ -0,0 +1,200 @@+(ns metabase-enterprise.workspaces.execute+ "This namespace is concerned with executing non-AppDB transforms, with optional reference re-mappings.++ It currently lives inside the workspace module, but eventually will become part of the transform module, so it should+ not make any assumptions about being run within a workspace, having the input / output semantics of a workspace, and+ especially it should not touch any of the Workspace entities inside AppDb, or any of the other workspace namespaces.++ For now, it uses AppDB as a side-channel with the transforms module, but in future this module should be COMPLETELY+ decoupled from AppDb."+ (:require+ [clojure.string :as str]+ [macaw.core :as macaw]+ [metabase-enterprise.transforms-python.python-runner :as python-runner]+ [metabase-enterprise.transforms.core :as transforms]+ [metabase-enterprise.transforms.execute :as transforms.execute]+ [metabase-enterprise.transforms.util :as transforms.util]+ [metabase.api.common :as api]+ [metabase.query-processor :as qp]+ [metabase.util :as u]+ [metabase.util.log :as log]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(defn- execution-results+ "Extract execution metadata from transform_run and target table info.+ Must be called within the transaction before rollback."+ [{xf-id :id :keys [target]}]+ (let [run (t2/select-one :model/TransformRun :transform_id xf-id)]+ (merge+ (select-keys run [:status :start_time :end_time :message])+ {:table {:name (:name target)+ :schema (:schema target)}})))++(defn- remap-python-source+ "Remap source-tables in a Python transform source to point to isolated tables.+ Handles both legacy integer format and new map format (from PR #66934)."+ [table-mapping source]+ (letfn [(remap [{:keys [database_id schema table table_id] :as table-ref}]+ (if-let [{:keys [id db-id schema table]} (or (table-mapping table-ref)+ (some-> table_id table-mapping)+ (when table+ (table-mapping [database_id schema table])))]+ ;; Prefer table ID when available; map format won't work until PR #66934 is merged:+ ;; https://github.com/metabase/metabase/pull/66934+ (or id {:database_id db-id, :schema schema, :table table})+ ;; Leave it un-mapped if we don't have an override.+ table-ref))]+ (update source :source-tables update-vals remap)))++(defn- remap-sql-source [table-mapping source]+ (let [remapping (reduce+ (fn [remapping [[_ source-schema source-table] {target-schema :schema, target-table :table}]]+ (assoc-in remapping [:tables {:schema source-schema+ :table source-table}]+ {:schema target-schema+ :table target-table}))+ {:schemas {}+ :tables {}}+ ;; Strip out the numeric keys (table ids)+ (filter (comp vector? key) table-mapping))]+ ;; We may need to set other options, like the case insensitivity (driver dependent)+ (update-in source [:query :stages 0 :native] #(macaw/replace-names % remapping {:allow-unused? true}))))++(defn- remap-mbql-source [_table-mapping _field-map source]+ (throw (ex-info "Remapping MBQL queries is not supported yet" {:source source})))++(defn- remap-source [table-map field-map source-type source]+ (case source-type+ :mbql (remap-mbql-source table-map field-map source)+ ;; TODO (Chris 2025-12-12) -- make sure it's actually a SQL dialect though..+ :native (remap-sql-source table-map source)+ :python (remap-python-source table-map source)))++;; You might prefer a multi-method? I certainly would.++(defn- remap-target [table-map target-fallback {d :database, s :schema, t :name :as target}]+ (if-let [replacement (or (table-map [d s t]) (target-fallback [d s t]))]+ ;; Always fallback to tables for re-mapped outputs, regardless of the type used in the original target.+ {:type (:type replacement "table")+ :database (:db-id replacement)+ :schema (:schema replacement)+ :name (:table replacement)}+ target))++(def ^:private no-mapping {})++(defn remapped-target+ "Extracts and applies remapping to a transform's target table reference.++ Used when needing to determine the isolated table location without running the full transform.+ Useful for error reporting and result construction.++ `remapping` is a map with:+ - `:tables` - map of table references to isolated table specs+ - `:target-fallback` - function to generate fallback mappings for unmapped targets++ Returns the remapped target as a map with `:type`, `:database`, `:schema`, and `:name`."+ [{:keys [target] :as _transform} remapping]+ (let [table-mapping (:tables remapping no-mapping)+ target-fallback (:target-fallback remapping no-mapping)]+ (remap-target table-mapping target-fallback target)))++(def ^:private preview-row-limit+ "Maximum number of rows to return in dry-run/preview mode."+ 2000)++(defn- dry-run-python+ "Run Python transform and return first 2000 rows without persisting.+ Returns a map with :status and :data (on success) or :message (on failure)."+ [{:keys [source]} remapping]+ (let [table-mapping (:tables remapping no-mapping)+ remapped-source (remap-python-source table-mapping source)+ resolved-source-tables (transforms.util/resolve-source-tables (:source-tables remapped-source))+ {:as result+ :keys [cols rows]} (python-runner/execute-and-read-output!+ {:code (:body remapped-source)+ :source-tables resolved-source-tables+ :row-limit preview-row-limit})+ flatrows (apply juxt (map (fn [c] (let [cname (:name c)] #(get % cname))) cols))]+ (if (= :succeeded (:status result))+ {:status :succeeded+ ;; return logs for debugging+ :logs (str/join "\n" (:logs result))+ :data {:cols cols+ :rows (mapv flatrows rows)}}+ {:status :failed+ :message (:message result)})))++(defn- dry-run-sql+ "Run SQL transform query and return first 2000 rows without persisting.+ Returns a ::ws.t/dry-run-result map with data nested under :data to match /api/dataset format."+ [{:keys [source]} remapping]+ (try+ (let [s-type (transforms/transform-source-type source)+ table-mapping (:tables remapping no-mapping)+ field-mapping (:fields remapping no-mapping)+ remapped-source (remap-source table-mapping field-mapping s-type source)+ query (:query remapped-source)+ result (qp/process-query+ (assoc query :constraints {:max-results preview-row-limit+ :max-results-bare-rows preview-row-limit}))+ data (:data result)]+ (if (= :completed (:status result))+ {:status :succeeded+ :data (select-keys data [:rows :cols :results_metadata])}+ {:status :failed+ :message (or (:error result) "Query execution failed")}))+ (catch Exception e+ (log/error e "Failed to run sql dry-run")+ {:status :failed+ :message (ex-message e)})))++(defn run-transform-preview+ "Execute transform and return first 2000 rows without persisting.+ Returns a ::ws.t/dry-run-result map with data nested under :data."+ [{:keys [source] :as transform} remapping]+ (let [s-type (transforms/transform-source-type source)]+ (case s-type+ (:native :mbql) (dry-run-sql transform remapping)+ :python (dry-run-python transform remapping))))++(defn run-transform-with-remapping+ "Execute a given collection with the given table and field re-mappings.++ This is used by Workspaces to re-route the output of each transform to a non-production table, and+ to re-write their queries where these outputs transitively become inputs to other transforms.++ Returns an ::ws.t/execution-result map with status, timing, and table metadata.++ -------++ NOTE: currently execution is done using transaction-rollback pattern, as a short-term hack.++ 1. Creating temporary Transform/TransformRun records in a transaction+ 2. Executing using existing transform infrastructure+ 3. Scraping metadata (execution stats + table schema)+ 4. Rolling back the transaction (no app DB records persist)++ The warehouse DB changes (actual table data) DO persist in the isolated schema."+ [{:keys [source target] :as transform} remapping]+ (t2/with-transaction [_conn]+ (let [s-type (transforms/transform-source-type source)+ table-mapping (:tables remapping no-mapping)+ target-fallback (:target-fallback remapping no-mapping)+ field-mapping (:fields remapping no-mapping)+ new-xf (-> (select-keys transform [:name :description])+ (assoc :creator_id api/*current-user-id*+ :source (remap-source table-mapping field-mapping s-type source)+ :target (remap-target table-mapping target-fallback target)))+ _ (assert (:target new-xf) "Target mapping must not be nil")+ temp-xf (t2/insert-returning-instance! :model/Transform new-xf)]+ (try+ (transforms.execute/execute! temp-xf {:run-method :manual})+ (catch Exception _e+ ;; Execution failed - the TransformRun record has been updated with failure status and message+ nil))+ ;; Return execution results whether succeeded or failed+ (u/prog1 (execution-results temp-xf)+ (t2/delete! :model/Transform (:id temp-xf))))))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/impl.clj b/enterprise/backend/src/metabase_enterprise/workspaces/impl.cljnew file mode 100644index 00000000000..827e5b2c5ca--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/impl.clj@@ -0,0 +1,783 @@+(ns metabase-enterprise.workspaces.impl+ "Glue code connecting workspace subsystems (dependencies, isolation)."+ (:require+ [clojure.set :as set]+ [metabase-enterprise.workspaces.dag :as ws.dag]+ [metabase-enterprise.workspaces.dependencies :as ws.deps]+ [metabase-enterprise.workspaces.execute :as ws.execute]+ [metabase-enterprise.workspaces.isolation :as ws.isolation]+ [metabase-enterprise.workspaces.util :as ws.u]+ [metabase.driver.sql :as driver.sql]+ [metabase.driver.sql.util :as sql.util]+ [metabase.util :as u]+ [metabase.util.log :as log]+ [toucan2.core :as t2]))++(defn- query-ungranted-external-inputs+ "Query for external inputs in a workspace that haven't been granted access yet.+ External inputs are tables that are not shadowed by any output tables.+ Returns seq of WorkspaceInput records where the table is active, and access_granted is false."+ [workspace-id]+ ;; It would be nice if we could optimize this join to use table_id, since we ignore tables that don't exist anyway.+ (t2/select :model/WorkspaceInput+ :workspace_id workspace-id+ {:where [:and+ [:= :workspace_input.access_granted false]+ ;; Ignore tables that will be shadowed by outputs of other transforms.+ [:not [:exists {:select [1]+ :from [[:workspace_output :wo]]+ :where [:and+ [:= :wo.workspace_id :workspace_input.workspace_id]+ [:= :wo.db_id :workspace_input.db_id]+ [:or+ [:and [:= :wo.global_schema nil] [:= :workspace_input.schema nil]]+ [:= :wo.global_schema :workspace_input.schema]]+ [:= :wo.global_table :workspace_input.table]]}]]+ [:not [:exists {:select [1]+ :from [[:workspace_output_external :woe]]+ :where [:and+ [:= :woe.workspace_id :workspace_input.workspace_id]+ [:= :woe.db_id :workspace_input.db_id]+ [:or+ [:and [:= :woe.global_schema nil] [:= :workspace_input.schema nil]]+ [:= :woe.global_schema :workspace_input.schema]]+ [:= :woe.global_table :workspace_input.table]]}]]+ ;; Ignore tables that don't currently exist.+ [:exists {:select [1]+ :from [[:metabase_table :t]]+ :where [:and+ [:= :active true]+ [:= :t.db_id :workspace_input.db_id]+ [:or+ [:and [:= :t.schema nil] [:= :workspace_input.schema nil]]+ [:= :t.schema :workspace_input.schema]]+ [:= :t.name :workspace_input.table]]}]]}))++(defn- external-input->table+ [{:keys [schema table]}]+ {:schema schema+ :name table})++(defn sync-grant-accesses!+ "Grant read access to external input tables for a workspace that haven't been granted yet.+ External inputs are tables that are read by transforms but not produced by any transform in the workspace.+ This should be called after adding transforms to a workspace or when re-initializing workspace isolation+ (e.g., after unarchiving)."+ [{workspace-id :id :as workspace}]+ (let [ungranted-inputs (query-ungranted-external-inputs workspace-id)]+ (if-not (:database_details workspace)+ ;; TODO (Chris 2025-12-15) -- this should throw, but we're cautious about racing with initialization+ #_(throw (ex-info "No database details, unable to grant read only access to the service account." {}))+ (log/warn "No database details, unable to grant read only access to the service account.")+ (when (seq ungranted-inputs)+ (let [database (t2/select-one :model/Database :id (:database_id workspace))+ tables (mapv external-input->table ungranted-inputs)]+ (try+ (ws.isolation/grant-read-access-to-tables! database workspace tables)+ ;; Mark inputs as granted after successful grant+ (t2/update! :model/WorkspaceInput {:id [:in (map :id ungranted-inputs)]}+ {:access_granted true})+ (catch Exception e+ ;; Suppress errors for tables that don't exist (common in tests with fake tables)+ (when-not (some->> (ex-message e) (re-find #"(?i)(does not exist|not found|no such table)"))+ (log/warn e "Error granting RO table permissions")))))))))++(defn- batch-lookup-table-ids+ "Given a bounded list of tables, all within the same database, return an association list of [db schema table] => id"+ [db-id schema-key table-key table-refs]+ (when (seq table-refs)+ (t2/select-fn-vec (juxt (juxt (constantly db-id) :schema :name) :id)+ [:model/Table :id :schema :name]+ :db_id db-id+ {:where (into [:or] (for [tr table-refs]+ [:and+ [:= :schema (get tr schema-key)]+ [:= :name (get tr table-key)]]))})))++(defn table-ids-fallbacks+ "Given a list of maps holding [db_id schema table], return a mapping from those tuples => table_id"+ ([table-refs]+ (table-ids-fallbacks :schema :name :id table-refs))+ ([schema-key table-key id-key table-refs]+ (when-let [table-refs (seq (remove id-key table-refs))]+ ;; These are ordered by db, so this will partition fine.+ (u/for-map [table-refs (partition-by :db_id table-refs)+ :let [db_id (:db_id (first table-refs))]+ ;; Guesstimating a number that prevents this query being too large.+ table-refs (partition-all 20 table-refs)+ map-entry (batch-lookup-table-ids db_id schema-key table-key table-refs)]+ map-entry))))++(defn- add-table-mapping-entries+ "Add table mapping entries for a single table.+ Adds:+ - [db-id schema table] -> replacement (for qualified SQL references)+ - table-id -> replacement (for MBQL/Python, if table-id provided)+ - [db-id nil table] -> replacement (for unqualified SQL, if in-default-schema? is true)"+ [m {:keys [db-id schema table table-id replacement in-default-schema?]}]+ (cond-> m+ true (assoc [db-id schema table] replacement)+ table-id (assoc table-id replacement)+ in-default-schema? (assoc [db-id nil table] replacement)))++(defn- quote-name [driver s] (when s (sql.util/quote-name driver :table s)))++(defn- quote-default-schema+ "Return the quoted default schema for a database.+ Pre-quotes the name because it will be spliced into SQL for unqualified references+ (e.g. `orders` → `\"public\".\"orders\"` or `test-data`.`orders`).+ These names don't appear in the original SQL, so macaw won't quote them for us."+ [{:keys [engine details]}]+ (quote-name engine (or (driver.sql/default-schema engine)+ ;; For MySQL and similar, use database name from connection details+ ((some-fn :dbname :db) details))))++(defn- build-remapping+ "Build table remapping from the analyzed input and output tables.++ Uses stored WorkspaceOutput, WorkspaceOutputExternal, WorkspaceInput, and WorkspaceInputExternal data.++ For OUTPUTS (isolated tables):+ Maps [db_id global_schema global_table] -> {:db-id :schema :table :id} for isolated tables.+ Also maps global_table_id -> same. This is more convenient and reliable for MBQL queries and Python transforms.+ Also maps [db_id nil global_table] for tables in the default schema, so unqualified SQL references work.++ For INPUTS (external tables read from source DB):+ Maps [db_id schema table] -> {:db-id :schema :table :id} to qualify references.+ This ensures \"SELECT * FROM orders\" becomes \"SELECT * FROM public.orders\" (or \"mydb.orders\" for MySQL).++ Output mappings take precedence over input mappings (merged last).+ This is used to remap queries, sources and targets to reflect the \"isolated\" tables used to seal the Workspace."+ [workspace _graph]+ (let [outputs (t2/select [:model/WorkspaceOutput+ :db_id :global_schema :global_table :global_table_id+ :isolated_schema :isolated_table :isolated_table_id]+ :workspace_id (:id workspace))+ external-outputs (t2/select [:model/WorkspaceOutputExternal+ :db_id :global_schema :global_table :global_table_id+ :isolated_schema :isolated_table :isolated_table_id]+ :workspace_id (:id workspace))+ all-outputs (concat outputs external-outputs)+ ;; Fetch input tables (external tables that transforms read from)+ inputs (t2/select [:model/WorkspaceInput :db_id :schema :table :table_id]+ :workspace_id (:id workspace))+ external-inputs (t2/select [:model/WorkspaceInputExternal :db_id :schema :table :table_id]+ :workspace_id (:id workspace))+ all-inputs (concat inputs external-inputs)+ ;; Get default schema for each database involved (both inputs and outputs).+ ;; For databases with schemas (PostgreSQL), this is the default schema (e.g., "public").+ ;; For databases without schemas (MySQL), we use the database name from connection details.+ db-ids (into #{} (map :db_id) (concat all-outputs all-inputs))+ databases (when (seq db-ids)+ (t2/select [:model/Database :id :engine :details] :id [:in db-ids]))+ db-id->default (u/index-by :id+ (fn [{:keys [engine details]}]+ (or (driver.sql/default-schema engine)+ ((some-fn :dbname :db) details)))+ databases)+ db-id->quoted (u/index-by :id quote-default-schema databases)+ fallback-map (merge+ (table-ids-fallbacks :global_schema :global_table :global_table_id all-outputs)+ (table-ids-fallbacks :isolated_schema :isolated_table :isolated_table_id all-outputs))+ ;; Build output mappings (remap to isolated tables)+ output-map (reduce+ (fn [m {:keys [db_id global_schema global_table global_table_id+ isolated_schema isolated_table isolated_table_id]}]+ (add-table-mapping-entries m+ {:db-id db_id+ :schema global_schema+ :table global_table+ :table-id (or global_table_id+ (fallback-map [db_id global_schema global_table]))+ :replacement {:db-id db_id+ :schema isolated_schema+ :table isolated_table+ :id (or isolated_table_id+ (fallback-map [db_id isolated_schema isolated_table]))}+ :in-default-schema? (= global_schema (db-id->default db_id))}))+ {}+ all-outputs)+ ;; Build input mappings (qualify unqualified references to external tables).+ ;; For inputs, we only need to add a mapping for unqualified references (nil schema).+ ;; When the table already has an explicit schema, Macaw will preserve it as-is.+ input-map (reduce+ (fn [m {:keys [db_id schema table table_id]}]+ (let [quoted-schema (db-id->quoted db_id)]+ ;; Only add a mapping for unqualified references (nil schema).+ ;; When the table already has an explicit schema, Macaw preserves it.+ (cond-> m+ (and (nil? schema) (some? quoted-schema))+ (assoc [db_id nil table] {:db-id db_id+ :schema quoted-schema+ :table table+ :id table_id}))))+ {}+ all-inputs)+ ;; Merge with outputs taking precedence+ table-map (merge input-map output-map)]+ {:tables table-map+ ;; We never want to write to any global tables, so remap on-the-fly if we hit an un-mapped target.+ :target-fallback (fn [[d s t]]+ (log/warn "Missing remapping for" {:db d :schema s, :table t})+ {:db-id d, :schema (:schema workspace), :table (ws.u/isolated-table-name s t), :id nil})+ ;; We won't need the field-map until we support MBQL.+ :fields nil}))++(defn- backfill-isolated-table-id!+ "Backfill workspace_output.isolated_table_id FK in the case where we just created the table for the first time."+ [ref-id]+ (when-let [table-id (:id (t2/query-one+ {:from [[:workspace_output :wo]]+ :join [[:metabase_table :t] [:and+ [:= :t.db_id :wo.db_id]+ [:= :t.schema :wo.isolated_schema]+ [:= :t.name :wo.isolated_table]]]+ :select [:t.id]+ :where [:and+ [:= :wo.ref_id ref-id]+ [:or [:= :wo.isolated_table_id nil] [:not= :t.id :wo.isolated_table_id]]]}))]+ (t2/update! :model/WorkspaceOutput {:ref_id ref-id} {:isolated_table_id table-id})))++(defn- backfill-external-isolated-table-id!+ "Backfill workspace_output_external.isolated_table_id FK after running an enclosed external transform."+ [transform-id]+ (when-let [table-id (:id (t2/query-one+ {:from [[:workspace_output_external :woe]]+ :join [[:metabase_table :t] [:and+ [:= :t.db_id :woe.db_id]+ [:= :t.schema :woe.isolated_schema]+ [:= :t.name :woe.isolated_table]]]+ :select [:t.id]+ :where [:and+ [:= :woe.transform_id transform-id]+ [:or [:= :woe.isolated_table_id nil] [:not= :t.id :woe.isolated_table_id]]]}))]+ (t2/update! :model/WorkspaceOutputExternal {:transform_id transform-id} {:isolated_table_id table-id})))++(defn- db-time+ "Returns the current timestamp from the database. Avoids timezone differences and clock skew with app server."+ []+ (:t (first (t2/query {:select [[:%now :t]]}))))++(def ^:private workspace-transform-id-xf+ "Transducer that extracts :id from workspace-transform nodes."+ (keep #(when (= :workspace-transform (:node-type %)) (:id %))))++(defn- workspace-transform-ids+ "Extract ref-ids from nodes that are workspace transforms."+ [nodes]+ (into [] workspace-transform-id-xf nodes))++(declare get-or-calculate-graph!)++(defn upstream-ancestors+ "Given a graph and a ref-id, find all upstream workspace transform ancestors.+ Returns ancestor ref-ids (not including the starting node).++ Takes a graph with :dependencies map (child->parents) and traverses upstream."+ [graph ref-id]+ (when-let [deps-map (:dependencies graph)]+ (ws.dag/bfs-reduce deps-map [{:node-type :workspace-transform :id ref-id}]+ :rf (workspace-transform-id-xf conj))))++(defn downstream-descendants+ "Given a graph and a ref-id, find all downstream workspace transform descendants.+ Returns descendant ref-ids (not including the starting node).++ Takes a graph with :dependencies map (child->parents), reverses it for downstream traversal."+ [graph ref-id]+ (when-let [deps-map (:dependencies graph)]+ (let [forward-edges (ws.dag/reverse-graph deps-map)]+ (ws.dag/bfs-reduce forward-edges [{:node-type :workspace-transform :id ref-id}]+ :rf (workspace-transform-id-xf conj)))))++(defn- any-internal-ancestor-stale?+ "Check if any in-workspace entity ancestor is stale.+ Returns true if any ancestor had its definition or input data changed since it last ran.+ Note: external transforms are not checked here, their staleness must be checked separately."+ [graph workspace ref-id]+ (when-let [upstream-ids (seq (upstream-ancestors graph ref-id))]+ (t2/exists? :model/WorkspaceTransform+ :workspace_id (:id workspace)+ :ref_id [:in upstream-ids]+ {:where [:or+ [:= :definition_changed true]+ [:= :input_data_changed true]]})))++(defn- mark-descendants-input-datastale!+ "Mark all transitive downstream workspace transforms as input_data_changed.+ Traverses the dependency graph downward from the given transform."+ [graph workspace ref-id]+ (when-let [downstream-ids (seq (downstream-descendants graph ref-id))]+ (t2/update! :model/WorkspaceTransform+ {:workspace_id (:id workspace)+ :ref_id [:in downstream-ids]}+ {:input_data_changed true})))++(defn run-transform!+ "Execute the given workspace transform or enclosed external transform."+ ([workspace graph transform]+ (run-transform! workspace graph transform (build-remapping workspace graph)))+ ([workspace graph transform remapping]+ (let [ref-id (:ref_id transform)+ external-id (:id transform)+ start-time (db-time)+ result (try+ (ws.isolation/with-workspace-isolation+ workspace+ (ws.execute/run-transform-with-remapping transform remapping))+ (catch Exception e+ (log/errorf e "Failed to execute %s transform %s"+ (if ref-id "workspace" "external")+ (or ref-id external-id))+ {:status :failed+ :message (ex-message e)+ :start_time start-time+ :end_time (db-time)+ :table (select-keys (ws.execute/remapped-target transform remapping) [:schema :name])}))]+ ;; We don't currently keep any record of when enclosed transforms were run.+ (when ref-id+ (let [succeeded? (= :succeeded (:status result))+ pre-stale? (any-internal-ancestor-stale? graph workspace ref-id)]+ (t2/update! :model/WorkspaceTransform {:ref_id ref-id :workspace_id (:id workspace)}+ (cond-> {:last_run_at (:end_time result)+ :last_run_status (some-> (:status result) name)+ :last_run_message (:message result)}+ ;; On success, always clear definition_changed+ succeeded? (assoc :definition_changed false)+ ;; On success, clear input_data_changed only if no ancestors are stale+ (and succeeded? (not pre-stale?))+ (assoc :input_data_changed false)))+ ;; Always mark transitive downstream as stale when we run successfully+ ;; (their input data may have changed even if nothing was "stale")+ (when succeeded?+ (mark-descendants-input-datastale! graph workspace ref-id))))+ (when (= :succeeded (:status result))+ (if ref-id+ ;; Workspace transform+ (backfill-isolated-table-id! ref-id)+ ;; External transform (enclosed in workspace)+ (backfill-external-isolated-table-id! external-id)))+ result)))++(defn dry-run-transform+ "Execute the given workspace transform without persisting to the target table.+ Returns the first 2000 rows of transform output for preview purposes."+ [workspace graph transform]+ (ws.isolation/with-workspace-isolation+ workspace+ (ws.execute/run-transform-preview transform (build-remapping workspace graph))))++;;;; ---------------------------------------- External Transform Sync ----------------------------------------++(defn- extract-external-transform-ids+ "Extract IDs of external transforms from graph entities."+ [entities]+ (into [] (comp (filter #(= :external-transform (:node-type %)))+ (map :id))+ entities))++(defn- normalize-table-schema+ "Normalize a table's schema: replace nil with the driver's default schema.+ Takes a map of db_id -> default-schema for lookup."+ [db-id->default-schema {:keys [db_id schema] :as table}]+ (if (some? schema)+ table+ (assoc table :schema (get db-id->default-schema db_id))))++;;;; ---------------------------------------- Epochal Versioning ----------------------------------------+;; Two-level versioning for thread-safe graph calculation:+;; - Transform level: analysis_version on workspace_transform, transform_version on workspace_input/workspace_output+;; - Graph level: graph_version on workspace, workspace_graph, workspace_input_external, workspace_output_external++(defn increment-graph-version!+ "Atomically increment graph_version for a workspace. Used to invalidate cached analysis."+ [workspace-id]+ (t2/update! :model/Workspace workspace-id {:graph_version [:+ :graph_version 1]}))++(defn increment-analysis-version!+ "Atomically increment analysis_version for a transform. Used to invalidate cached analysis."+ [workspace-id ref-id]+ ;; definition_changed is also set by the WorkspaceTransform before-update hook when source/target changes,+ ;; but we set it here as well since this function is also called for unarchive (where source/target don't change).+ (t2/update! :model/WorkspaceTransform+ {:workspace_id workspace-id, :ref_id ref-id}+ {:analysis_version [:+ :analysis_version 1]+ :definition_changed true}))++;;;; ---------------------------------------- Lazy Graph Calculation ----------------------------------------++(defn- workspace-transforms+ "Return the entity values for the internal transforms."+ [ws-id]+ (t2/select-fn-vec (fn [{:keys [ref_id]}] {:entity-type :transform :id ref_id})+ [:model/WorkspaceTransform :ref_id]+ :workspace_id ws-id))++(defn- calculate-graph+ "Calculate the dependency graph for a workspace.+ Returns the graph without caching - caller is responsible for caching."+ [ws-id]+ (ws.dag/path-induced-subgraph ws-id (workspace-transforms ws-id)))++(defn- cleanup-old-transform-versions!+ "Delete obsolete analysis for a given workspace transform."+ [ws-id ref-id]+ (doseq [model [:model/WorkspaceOutput :model/WorkspaceInput]]+ (t2/delete! model+ :workspace_id ws-id+ :ref_id ref-id+ ;; Use a subselect to avoid left over gunk from race conditions.+ {:where [:< :transform_version {:select [:analysis_version]+ :from [:workspace_transform]+ :where [:and+ [:= :workspace_id ws-id]+ [:= :ref_id ref-id]]}]})))++(defn- cleanup-old-graph-versions!+ "Delete obsolete graph-level rows."+ [ws-id]+ (doseq [model [:model/WorkspaceGraph+ :model/WorkspaceInputExternal+ :model/WorkspaceOutputExternal]]+ (t2/delete! model+ :workspace_id ws-id+ ;; Use a subselect to avoid left over gunk from race conditions.+ {:where [:< :graph_version {:select [:graph_version]+ :from [:workspace]+ :where [:= :id ws-id]}]})))++(defn- analyze-transform!+ "Analyze a transform and write its outputs/inputs with the given transform_version.+ Called when the transform's output doesn't exist for current analysis_version."+ [{ws-id :id, isolated-schema :schema :as _workspace}+ {:keys [ref_id analysis_version] :as transform}]+ (let [analysis (ws.deps/analyze-entity :transform transform)]+ (ws.deps/write-entity-analysis! ws-id isolated-schema :transform ref_id analysis analysis_version)+ (cleanup-old-transform-versions! ws-id ref_id)))++(defn- analyze-stale-transforms!+ "Analyze transforms where workspace_output doesn't exist for current analysis_version.+ Skips transforms that haven't changed since last analysis.+ Returns true if any transforms were analyzed."+ [{ws-id :id, db-status :db_status :as workspace}]+ ;; Find transforms needing analysis: those without output at current analysis_version+ (let [stale-transforms (t2/select :model/WorkspaceTransform+ ;; Ordering is only for determinism in tests.+ {:order-by [:updated_at]+ :where [:and+ [:= :workspace_transform.workspace_id ws-id]+ [:not [:exists+ {:select [1]+ :from [[:workspace_output :wo]]+ :where [:and+ [:= :wo.workspace_id ws-id]+ [:= :wo.ref_id :workspace_transform.ref_id]+ [:= :wo.transform_version+ :workspace_transform.analysis_version]]}]]]})]+ ;; Analyze each stale transform+ (doseq [transform stale-transforms]+ (analyze-transform! workspace transform))+ ;; Grant any missing read access. Do this even if no transforms were stale, as we may have been unable to grant+ ;; some permissions previously (insufficient permissions, table didn't exist yet, etc.)+ (when (= :ready db-status)+ (sync-grant-accesses! workspace))+ (boolean (seq stale-transforms))))++(defn analyze-transform-if-stale!+ "Analyze a single transform if its workspace_output doesn't exist for current analysis_version.+ Used before running a transform to ensure grants are up-to-date."+ [{ws-id :id :as workspace} {:keys [ref_id analysis_version] :as transform}]+ (when-not (ws.deps/transform-output-exists-for-version? ws-id ref_id analysis_version)+ (analyze-transform! workspace transform)+ (sync-grant-accesses! workspace)))++(defn- insert-workspace-graph!+ "Insert a new graph for the given version. Silently ignores constraint violations+ from concurrent inserts (another process already inserted this version)."+ [ws-id graph-version graph]+ (when-not (t2/exists? :model/WorkspaceGraph+ :workspace_id ws-id+ :graph_version [:>= graph-version])+ (ws.u/ignore-constraint-violation+ (t2/insert! :model/WorkspaceGraph+ {:workspace_id ws-id+ :graph_version graph-version+ :graph graph}))))++(defn- insert-external-outputs-for-version!+ "Sync workspace_output_external for a specific graph version.+ Creates new rows with graph_version set. Silently ignores constraint violations."+ [workspace-id isolated-schema entities graph-version]+ (when-not (t2/exists? :model/WorkspaceOutputExternal :workspace_id workspace-id :graph_version [:>= graph-version])+ (let [external-tx-ids (extract-external-transform-ids entities)]+ (when (seq external-tx-ids)+ (let [transforms (t2/select [:model/Transform :id :target] :id [:in external-tx-ids])+ rows (for [{tx-id :id, {:keys [database schema name]} :target} transforms]+ (let [isolated-table (ws.u/isolated-table-name schema name)+ ;; TODO (Chris 2026-01-26) -- 2N + 1 is really not great here...+ global-table-id (t2/select-one-fn :id [:model/Table :id]+ :db_id database :schema schema :name name)+ isolated-table-id (t2/select-one-fn :id [:model/Table :id]+ :db_id database+ :schema isolated-schema+ :name isolated-table)]+ {:workspace_id workspace-id+ :transform_id tx-id+ :graph_version graph-version+ :db_id database+ :global_schema schema+ :global_table name+ :global_table_id global-table-id+ :isolated_schema isolated-schema+ :isolated_table isolated-table+ :isolated_table_id isolated-table-id}))]+ (when (seq rows)+ (ws.u/ignore-constraint-violation+ (t2/insert! :model/WorkspaceOutputExternal rows))))))))++(defn- insert-external-inputs-for-version!+ "Write `workspace_input_external` for a specific graph version, unless they (or a new version) already have been."+ [workspace-id entities graph-version]+ (when-not (t2/exists? :model/WorkspaceInputExternal :workspace_id workspace-id :graph_version [:>= graph-version])+ (let [external-tx-ids (extract-external-transform-ids entities)]+ (when (seq external-tx-ids)+ (let [input-tables (t2/select [:model/Dependency :to_entity_id]+ :from_entity_type :transform+ :from_entity_id [:in external-tx-ids]+ :to_entity_type :table)+ input-table-ids (into #{} (map :to_entity_id) input-tables)+ ;; Get workspace output table IDs (both workspace and external outputs)+ workspace-output-ids (t2/select-fn-set :global_table_id+ [:model/WorkspaceOutput :global_table_id]+ :workspace_id workspace-id+ {:where [:not= :global_table_id nil]})+ external-output-ids (t2/select-fn-set :global_table_id+ [:model/WorkspaceOutputExternal :global_table_id]+ :workspace_id workspace-id+ :graph_version graph-version+ {:where [:not= :global_table_id nil]})+ all-output-ids (set/union workspace-output-ids external-output-ids)+ external-input-ids (set/difference input-table-ids all-output-ids)]+ (when (seq external-input-ids)+ (let [tables (t2/select [:model/Table :id :db_id :schema :name] :id [:in external-input-ids])+ db-ids (into #{} (map :db_id) tables)+ db-id->default-schema (when (seq db-ids)+ (into {}+ (map (fn [{:keys [id engine]}]+ [id (driver.sql/default-schema engine)]))+ (t2/select [:model/Database :id :engine] :id [:in db-ids])))+ normalized-tables (map (partial normalize-table-schema db-id->default-schema) tables)]+ (when (seq normalized-tables)+ (ws.u/ignore-constraint-violation+ (t2/insert! :model/WorkspaceInputExternal+ (for [{:keys [id db_id schema name]} normalized-tables]+ {:workspace_id workspace-id+ :graph_version graph-version+ :db_id db_id+ :schema schema+ :table name+ :table_id id+ :access_granted false})))))))))))++(defn- fully-persisted-graph+ "Fetch the latest graph for which all the related tables have all been populated, that meets a minimum version.+ Returns nil if no sufficiently recent graph, with all its derivatives persisted, exists.+ It uses a subselect to avoid races where the latest version could be superseded and deleted before we can fetch it."+ [ws-id min-version]+ (t2/select-one-fn :graph+ [:model/WorkspaceGraph :graph]+ :workspace_id ws-id+ :graph_version [:>= min-version]+ {:order-by [[:workspace_graph.graph_version :desc]]+ :limit 1}))++(defn- graph-with-minimum-version+ "Fetch the latest graph (and its version) that meets a minimum version constraint."+ [ws-id min-version]+ (t2/select-one [:model/WorkspaceGraph :graph [:graph_version :version]]+ :workspace_id ws-id+ {:where [:>= :workspace_graph.graph_version min-version]+ :order-by [[:workspace_graph.graph_version :desc]]+ :limit 1}))++(defn- calculate-and-persist-graph!+ "Return a graph for version > min-version.++ Non-blocking implementation that uses epochal steps to avoid co-ordination while providing workable consistency.+ If at any step we find results for a later version, we proceed to calculating (if necessary) that version.+ In the best case, we find complete results for the latest version and simply return them, without any computation.++ The analysis is computed in various stages, and written to the following tables in the given order:++ 1. workspace_input (per tx)+ 2. workspace_output (per tx, marks that we're done for that tx)+ 3. workspace_graph+ 4. workspace_input_external+ 5. workspace_output_external++ Returns the graph."+ [{ws-id :id, isolated-schema :schema :as workspace} min-version]+ ;; Analyze transforms that need it (transform-level versioning)+ ;; This writes workspace_input and workspace_output.+ (analyze-stale-transforms! workspace)+ ;; See if there's already a computed graph we can use, otherwise compute a new one.+ (let [{:keys [graph version]} (graph-with-minimum-version ws-id min-version)+ graph (or graph+ ;; Note that we can't freeze the versions of the transform's input and outputs, used to+ ;; calculate the graph, they don't use the same version numbering.+ ;; Instead, it will use the latest version across its various reads.+ ;; This means the graph is only eventually consistent - it doesn't have snapshot consistency+ ;; across the reads we make to the transforms. There are perverse cases where this can lead+ ;; to cycles or other data defects. Since such a graph will itself be stale, we will recover+ ;; from those defects on the next read.+ (u/prog1 (calculate-graph ws-id)+ (insert-workspace-graph! ws-id min-version <>)))+ version (or version min-version)+ ;; Write workspace_input_external if needed, bump version if we have been overtaken.+ _ (insert-external-inputs-for-version! ws-id (:entities graph) version)+ ;; Write workspace_output_external if needed, bump version if we have been overtaken.+ _ (insert-external-outputs-for-version! ws-id isolated-schema (:entities graph) version)]+ (log/debugf "Have graph for workspace %d at version %d (target was %d)" ws-id version min-version)+ ;; Discard any obsolete rows (including our own, if we've been overtaken)+ (cleanup-old-graph-versions! ws-id)+ ;; If there's an even more recent version in the database now, return that.+ ;; This decreases the chance of returning a graph with transient corruption.+ (or (graph-with-minimum-version ws-id (inc version)) graph)))++(defn- fetch-staleness-map+ "Fetch staleness flags for workspace transforms from the database.+ Returns a map of ref-id -> {:definition_changed bool, :input_data_changed bool}.+ Only fetches rows where at least one flag is true; callers can rely on nil-punning+ for missing entries (missing = not stale)."+ [ws-id ref-ids]+ (when (seq ref-ids)+ (t2/select-fn->fn :ref_id #(select-keys % [:definition_changed :input_data_changed])+ :model/WorkspaceTransform+ :workspace_id ws-id+ :ref_id [:in ref-ids]+ {:where [:or+ [:= :definition_changed true]+ [:= :input_data_changed true]]})))++(defn- workspace-transform? [entity]+ (= :workspace-transform (:node-type entity)))++(defn- external-transform? [entity]+ (= :external-transform (:node-type entity)))++(defn- transform? [entity]+ (#{:workspace-transform :external-transform} (:node-type entity)))++(defn annotate-staleness+ "Annotate graph entities with transitive staleness given a staleness-map.+ An entity is stale if:+ - Its definition_changed is true (definition changed since last run), OR+ - Its input_data_changed is true (input data changed since last run), OR+ - Any of its ancestors are stale (transitively)++ Takes a graph and a staleness-map: {entity-id -> {:definition_changed bool, :input_data_changed bool}}+ Returns updated graph with :stale, :definition_changed, :input_data_changed on each transform entity.++ This propagates staleness down the graph using a BFS from all initially stale nodes.+ Both workspace transforms and external transforms are marked as stale so they can be+ executed in the correct toposorted order."+ [graph staleness-map]+ (let [locally-stale? (fn [entity]+ (let [staleness (get staleness-map (:id entity))]+ (or (:definition_changed staleness)+ (:input_data_changed staleness))))+ init-stale (filter (every-pred workspace-transform? locally-stale?)+ (:entities graph))+ forward-edges (some-> (:dependencies graph) ws.dag/reverse-graph)+ ;; BFS traverses through ALL nodes (including external transforms) to find all stale transforms+ all-reachable (if forward-edges+ (ws.dag/bfs-reduce forward-edges init-stale :include-start? true :init #{})+ (set init-stale))+ ;; Include both workspace and external transforms in the stale set+ all-stale (set (filter transform? all-reachable))+ annotate (fn [entity]+ (cond+ (workspace-transform? entity)+ (-> entity+ (merge (get staleness-map (:id entity)+ {:definition_changed false :input_data_changed false}))+ (assoc :stale (contains? all-stale entity)))++ (external-transform? entity)+ (assoc entity :stale (contains? all-stale entity))++ :else+ entity))]+ (update graph :entities #(mapv annotate %))))++(defn with-staleness+ "Annotate graph entities with transitive staleness.+ Takes a workspace and a graph, fetches staleness from the database, and annotates the graph.+ Returns updated graph with :stale, :definition_changed, :input_data_changed on each workspace transform entity.++ Use `annotate-staleness` directly if you already have a staleness-map."+ [{ws-id :id} graph]+ (let [staleness-map (->> (:entities graph) workspace-transform-ids (fetch-staleness-map ws-id))]+ (annotate-staleness graph staleness-map)))++(defn get-or-calculate-graph!+ "Return the dependency graph for a workspace.+ Uses cached graph if version matches, otherwise recalculates it.+ Thread-safe via epochal versioning.+ Also syncs workspace_output_external and workspace_input_external tables when recalculating.++ Use `with-staleness` to annotate the graph with transitive staleness."+ [{ws-id :id, graph-version :graph_version :as workspace}]+ (or (fully-persisted-graph ws-id graph-version)+ (calculate-and-persist-graph! workspace graph-version)))++(defn- transforms-to-execute+ "Given a workspace, graph, and an optional filter, return the global and workspace definitions to run, in order."+ [{ws-id :id :as workspace} graph & {:keys [stale-only?]}]+ (let [graph (cond->> graph+ stale-only? (with-staleness workspace))+ entities (:entities graph)+ entities (if stale-only?+ (filter :stale entities)+ entities)+ type->ids (u/group-by :node-type :id entities)+ id->tx (merge+ {}+ (when-let [ids (seq (type->ids :external-transform))]+ (t2/select-fn->fn :id identity+ [:model/Transform :id :name :source :target]+ :id [:in ids]))+ (when-let [ref-ids (seq (type->ids :workspace-transform))]+ (t2/select-fn->fn :ref_id identity+ [:model/WorkspaceTransform :ref_id :name :source :target]+ :workspace_id ws-id+ :ref_id [:in ref-ids])))]+ (keep (comp id->tx :id) entities)))++(defn- id->str [ref-id-or-id]+ (if (string? ref-id-or-id)+ ref-id-or-id+ (str "global-id:" ref-id-or-id)))++(defn execute-workspace!+ "Execute all the transforms within a given workspace."+ [workspace graph & {:keys [stale-only?] :or {stale-only? false}}]+ (let [ws-id (:id workspace)+ remapping (build-remapping workspace graph)]+ (reduce+ (fn [acc {external-id :id ref-id :ref_id :as transform}]+ (let [node-type (if external-id :external-transform :workspace-transform)+ id-str (id->str (or external-id ref-id))]+ (try+ ;; Perhaps we want to return some of the metadata from this as well?+ (if (= :succeeded (:status (run-transform! workspace graph transform remapping)))+ (update acc :succeeded conj id-str)+ ;; Perhaps the status might indicate it never ran?+ (update acc :failed conj id-str))+ (catch Exception e+ (log/error e "Failed to execute transform" {:workspace-id ws-id :node-type node-type :id id-str})+ (update acc :failed conj id-str)))))+ {:succeeded []+ :failed []+ :not_run []}+ (transforms-to-execute workspace graph {:stale-only? stale-only?}))))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/init.clj b/enterprise/backend/src/metabase_enterprise/workspaces/init.cljnew file mode 100644index 00000000000..04f7482afd3--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/init.clj@@ -0,0 +1,9 @@+(ns metabase-enterprise.workspaces.init+ "Initialize workspace models. Driver workspace isolation methods are now+ defined directly in the driver files."+ (:require+ [metabase-enterprise.workspaces.events]+ [metabase-enterprise.workspaces.models.workspace]+ [metabase-enterprise.workspaces.models.workspace-graph]+ [metabase-enterprise.workspaces.models.workspace-transform]+ [metabase-enterprise.workspaces.settings]))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/isolation.clj b/enterprise/backend/src/metabase_enterprise/workspaces/isolation.cljnew file mode 100644index 00000000000..4754cf04984--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/isolation.clj@@ -0,0 +1,53 @@+(ns metabase-enterprise.workspaces.isolation+ "Workspace database isolation functions.++ The actual driver-specific implementations are now in the driver files:+ - metabase.driver.h2+ - metabase.driver.postgres+ - metabase.driver.redshift+ - metabase.driver.snowflake+ - metabase.driver.sqlserver+ - metabase.driver.clickhouse+ - metabase.driver.bigquery-cloud-sdk++ This namespace provides the public API for workspace isolation."+ (:require+ [metabase.driver :as driver]+ [metabase.driver.util :as driver.u]))++;;;; Delegation to driver methods+;; The actual multimethod implementations are now in the individual driver files.+;; These functions dispatch through the driver multimethods.++(defn grant-read-access-to-tables!+ "Grant read access to these tables.+ Delegates to driver/grant-workspace-read-access!"+ [database workspace tables]+ (driver/grant-workspace-read-access! (driver.u/database->driver database) database workspace tables))++(defn destroy-workspace-isolation!+ "Destroy all database resources created for workspace isolation.+ This includes dropping tables, schemas/databases, users, roles, and logins.+ Fails fast on first error. Should be called when deleting a workspace.+ Delegates to driver/destroy-workspace-isolation!"+ [database workspace]+ (driver/destroy-workspace-isolation! (driver.u/database->driver database) database workspace))++;;;; Public API++(defn ensure-database-isolation!+ "Wrapper around the driver method, to make migrations easier in future."+ [workspace database]+ (driver/init-workspace-isolation! (driver.u/database->driver database) database workspace))++(defn do-with-workspace-isolation+ "Impl of* with-workspace-isolation*."+ [workspace thunk]+ (driver/with-swapped-connection-details (:database_id workspace)+ (:database_details workspace)+ (thunk)))++(defmacro with-workspace-isolation+ "Execute body with necessary isolation."+ [workspace & body]+ `(do-with-workspace-isolation ~workspace (fn [] ~@body)))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/merge.clj b/enterprise/backend/src/metabase_enterprise/workspaces/merge.cljnew file mode 100644index 00000000000..e9dac5d23cd--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/merge.clj@@ -0,0 +1,128 @@+(ns metabase-enterprise.workspaces.merge+ "Update global entities based on the definitions within a workspace.+ All functions assumes that validation of the dependencies AFTER merge is done BEFORE calling any of these methods."+ (:require+ [metabase-enterprise.transforms.api :as transforms.api]+ [metabase-enterprise.workspaces.models.workspace-merge]+ [metabase-enterprise.workspaces.models.workspace-merge-transform]+ [metabase-enterprise.workspaces.types :as ws.types]+ [metabase.util.malli :as mu]+ [toucan2.core :as t2])+ (:import (java.sql Connection)))++(set! *warn-on-reflection* true)++(defn- create-merge-transform-history!+ "Create a workspace_merge_transform record to track this merge operation.+ Does nothing for :noop operations (archived transforms that don't exist globally)+ or :delete operations (history would be CASCADE deleted with the transform anyway)."+ [op merge-map]+ (when (#{:create :update} op)+ (t2/insert! :model/WorkspaceMergeTransform merge-map)))++(mu/defn merge-transform! :- [:map+ [:op [:enum :create :delete :noop :update]]+ [:ref_id ::ws.types/ref-id]+ [:global_id {:optional true} [:maybe ::ws.types/appdb-id]]+ [:error {:optional true} :any]]+ "Make the given transform in the Changeset public, i.e. create or update the relevant model/Transform entities.+ This should also clear it out from the Changset, as it no longer has any changes.++ Creates a WorkspaceMergeTransform history record.+ - :ws-transform - The workspace transform to merge+ - :workspace - The workspace map (must have :id and :name keys)+ - :workspace-merge-id - ID of parent WorkspaceMerge (caller must create this)+ - :merging-user-id - user performing the merge+ - :commit-message - description of the merge"+ [{:keys [ws-transform workspace workspace-merge-id merging-user-id commit-message]}]+ ;; Problems this may run into:+ ;; It will recalculate and validate the dag greedily as it inserts each items, and this might fail of temporary conflicts.+ ;; Some of these conflicts could be solved by ordering things smartly, but in the general case (e.g. reversing a+ ;; chain) this will not be possible - we would need to defer all validation and calculation, which is a bit scary.+ ;; Let's just focus on the happy path for now though, as this is a fundamental problem and unrelated to the design.+ (let [{:keys [global_id ref_id archived_at]} ws-transform+ {:keys [error] :as result}+ (cond (and global_id archived_at)+ (merge+ {:op :delete :global_id global_id :ref_id ref_id}+ (try+ (transforms.api/delete-transform! (t2/select-one :model/Transform :id global_id))+ nil+ (catch Throwable e+ {:error e})))++ global_id+ (merge+ {:op :update :global_id global_id :ref_id ref_id}+ (try+ (transforms.api/update-transform!+ global_id (select-keys ws-transform [:name :description :source :target]))+ nil+ (catch Throwable e+ {:error e})))++ archived_at+ {:op :noop :global_id nil :ref_id ref_id}++ :else+ (merge+ {:op :create :global_id nil :ref_id ref_id}+ (try+ {:global_id (:id (transforms.api/create-transform!+ (select-keys ws-transform [:name :description :source :target])+ (:creator_id ws-transform)))}+ (catch Throwable e+ {:error e}))))]++ (when-not error+ ;; See https://linear.app/metabase/issue/GDGT-1591/merging-a-workspace-removes-transforms-from-it+ ;; Keeping this stub as we may want to revisit it in the future.+ #_(t2/delete! :model/WorkspaceTransform :workspace_id (:id workspace) :ref_id ref_id)+ (create-merge-transform-history!+ (:op result)+ {:workspace_merge_id workspace-merge-id+ :workspace_id (:id workspace)+ :workspace_name (:name workspace)+ :transform_id (:global_id result)+ :commit_message commit-message+ :merging_user_id merging-user-id}))++ result))++(defn merge-workspace!+ "Make all the transforms in the Changeset public, i.e. create or update the relevant model/Transform entities.++ - workspace: The workspace map (must have :id and :name keys)+ - merging-user-id: user performing the merge+ - commit-message: description of the merge"+ [workspace merging-user-id commit-message]+ ;; Perhaps we want to solve the N+1?+ ;; This will require reworking the lifecycle for validating and recalculating dependencies for the transforms.+ (t2/with-transaction [tx]+ (let [savepoint (.setSavepoint ^Connection tx)+ ws-merge-id (t2/insert-returning-pk!+ :model/WorkspaceMerge+ {:workspace_id (:id workspace)+ :workspace_name (:name workspace)+ :commit_message commit-message+ :creator_id merging-user-id})+ result (reduce+ (fn [acc ws-transform]+ (let [{:keys [error] :as result} (merge-transform! {:ws-transform ws-transform+ :workspace workspace+ :workspace-merge-id ws-merge-id+ :merging-user-id merging-user-id+ :commit-message commit-message})]+ (if error+ (reduced (-> acc+ (update :errors conj result)+ (assoc-in [:merged :transforms] [])))+ (update-in acc [:merged :transforms] conj result))))+ {:merged {:transforms []}+ :errors []}+ (t2/select :model/WorkspaceTransform :workspace_id (:id workspace)+ ;; Deterministic ordering; ref_id breaks created_at ties+ {:order-by [[:created_at :asc] [:ref_id :asc]]}))]+ (when (seq (:errors result))+ (.rollback ^Connection tx savepoint))+ result)))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace.cljnew file mode 100644index 00000000000..9f223cd13f7--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace.clj@@ -0,0 +1,100 @@+(ns metabase-enterprise.workspaces.models.workspace+ (:require+ [metabase-enterprise.workspaces.impl :as ws.impl]+ [metabase-enterprise.workspaces.isolation :as ws.isolation]+ [metabase.models.interface :as mi]+ [metabase.util.log :as log]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(methodical/defmethod t2/table-name :model/Workspace [_model] :workspace)++(def ^:private base-statuses+ "Set of valid base_status values, representing the synchronous lifecycle."+ #{:empty :active :archived})++(def ^:private db-statuses+ "Set of valid db_status values, representing the status of the data warehouse."+ #{:uninitialized :pending :ready :broken})++(t2/deftransforms :model/Workspace+ {:database_details mi/transform-encrypted-json+ :base_status (mi/transform-validator mi/transform-keyword (partial mi/assert-enum base-statuses))+ :db_status (mi/transform-validator mi/transform-keyword (partial mi/assert-enum db-statuses))})++(defn computed-status+ "Compute backwards-compatible status from base_status and db_status.+ Returns :archived if workspace is archived, otherwise returns db_status."+ [{:keys [base_status db_status]}]+ (case base_status+ :empty :uninitialized+ :archived :archived+ db_status))++(doto :model/Workspace+ (derive :metabase/model)+ (derive :hook/timestamped?))++(defn archive!+ "Archive a workspace. Destroys database isolation resources (best-effort), revokes access grants,+ sets base_status to :archived and db_status to :uninitialized.+ Cleanup failures are logged but don't block archiving - the workspace can be unarchived to retry cleanup,+ or deleted once the underlying permission issues are resolved."+ [{workspace-id :id :as workspace}]+ (let [database (t2/select-one :model/Database :id (:database_id workspace))+ ;; Best-effort cleanup - don't block archiving on cleanup failures+ ;; Only destroy isolation if workspace was initialized (not uninitialized db_status)+ cleaned? (when (not= :uninitialized (:db_status workspace))+ (try+ (ws.isolation/destroy-workspace-isolation! database workspace)+ true+ (catch Exception e+ (log/warnf e "Failed to cleanup isolation resources for workspace %s, proceeding with archive" workspace-id)+ false)))]+ ;; Mark all inputs as un-granted since the user *may* have been dropped (even if there were some failures)+ (t2/update! :model/WorkspaceInput {:workspace_id workspace-id}+ {:access_granted false})+ ;; Update workspace status and increment graph_version (will need recalculation on unarchive)+ (t2/update! :model/Workspace workspace-id+ {:base_status :archived+ :db_status (if cleaned? :uninitialized :broken)+ :graph_version [:+ :graph_version 1]})))++(defn unarchive!+ "Unarchive a workspace. If workspace has transforms, re-initializes database isolation resources,+ re-grants read access to source tables, and sets base_status to :active with db_status :ready.+ If workspace is empty (no transforms), sets base_status to :empty and leaves db_status as :uninitialized."+ [workspace]+ (let [workspace-id (:id workspace)+ has-transforms? (t2/exists? :model/WorkspaceTransform :workspace_id workspace-id)]+ ;; Delete old graph data - will be recalculated+ (t2/delete! :model/WorkspaceGraph :workspace_id workspace-id)+ (if has-transforms?+ ;; Workspace has transforms - re-initialize database isolation+ (let [database (t2/select-one :model/Database (:database_id workspace))+ isolation-result (ws.isolation/ensure-database-isolation! workspace database)+ ;; Update workspace status and increment graph_version for recalculation+ _ (t2/update! :model/Workspace workspace-id+ (merge {:base_status :active+ :db_status :ready+ :graph_version [:+ :graph_version 1]}+ (select-keys isolation-result [:schema :database_details])))+ ;; Re-fetch workspace to get updated database_details and schema for granting permissions+ updated-ws (t2/select-one :model/Workspace workspace-id)]+ ;; Re-grant read access to source tables (inputs were marked un-granted during archive)+ (ws.impl/sync-grant-accesses! updated-ws))+ ;; Workspace is empty - just set to empty status+ (t2/update! :model/Workspace workspace-id {:base_status :empty}))))++(defn delete!+ "Delete a workspace. Destroys database isolation resources and deletes the workspace."+ [workspace]+ ;; Only destroy isolation if workspace was initialized+ (when (not= :uninitialized (:db_status workspace))+ (when-let [database (t2/select-one :model/Database :id (:database_id workspace))]+ (try+ (ws.isolation/destroy-workspace-isolation! database workspace)+ (catch Exception _))+ (t2/update! :model/WorkspaceInput {:workspace_id (:id workspace)}+ {:access_granted false})))+ (t2/delete! :model/Workspace (:id workspace)))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_graph.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_graph.cljnew file mode 100644index 00000000000..7888cb25471--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_graph.clj@@ -0,0 +1,50 @@+(ns metabase-enterprise.workspaces.models.workspace-graph+ "Model for cached workspace dependency graphs."+ (:require+ [metabase.models.interface :as mi]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(methodical/defmethod t2/table-name :model/WorkspaceGraph [_model] :workspace_graph)++;; The graph structure contains :dependencies with map keys that need special handling.+;; We use a custom transform that converts map keys to/from association lists.++(defn- graph-out+ "Transform graph from DB (JSON string) to Clojure data.+ Converts :dependencies association list back to a map with proper keys."+ [graph-json]+ (when graph-json+ (let [graph (mi/json-out-with-keywordization graph-json)]+ (-> graph+ (update :dependencies (fn [assoc-list]+ (into {} (map (fn [[k vs]]+ [(update k :node-type keyword)+ (mapv #(update % :node-type keyword) vs)])+ assoc-list))))+ (update :entities (fn [entities]+ (mapv #(update % :node-type keyword) entities)))+ (update :inputs #(or % []))+ (update :outputs #(or % []))))))++(defn- graph-in+ "Transform graph from Clojure data to DB (JSON string).+ Converts :dependencies map to association list since JSON can't have map keys."+ [graph]+ (when graph+ (-> graph+ (update :dependencies (fn [deps]+ (mapv (fn [[k vs]]+ [(select-keys k [:node-type :id])+ (mapv #(select-keys % [:node-type :id]) vs)])+ deps)))+ (update :entities (fn [entities]+ (mapv #(select-keys % [:node-type :id]) entities)))+ mi/json-in)))++(t2/deftransforms :model/WorkspaceGraph+ {:graph {:in graph-in :out graph-out}})++(doto :model/WorkspaceGraph+ (derive :metabase/model)+ (derive :hook/timestamped?))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_input.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_input.cljnew file mode 100644index 00000000000..5345d57138b--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_input.clj@@ -0,0 +1,16 @@+(ns metabase-enterprise.workspaces.models.workspace-input+ "Model for WorkspaceInput - external tables that workspace transforms consume.+ Each input is linked to a specific transform via ref_id."+ (:require+ [metabase.models.interface :as mi]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(methodical/defmethod t2/table-name :model/WorkspaceInput [_model] :workspace_input)++(doto :model/WorkspaceInput+ (derive :metabase/model)+ (derive :hook/timestamped?))++(t2/deftransforms :model/WorkspaceInput+ {:ref_id mi/transform-trim})diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_input_external.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_input_external.cljnew file mode 100644index 00000000000..554e27e21ad--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_input_external.clj@@ -0,0 +1,11 @@+(ns metabase-enterprise.workspaces.models.workspace-input-external+ "Model for WorkspaceInputExternal - external tables that enclosed external transforms consume."+ (:require+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(methodical/defmethod t2/table-name :model/WorkspaceInputExternal [_model] :workspace_input_external)++(doto :model/WorkspaceInputExternal+ (derive :metabase/model)+ (derive :hook/timestamped?))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_log.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_log.cljnew file mode 100644index 00000000000..91980b7cce9--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_log.clj@@ -0,0 +1,70 @@+(ns metabase-enterprise.workspaces.models.workspace-log+ (:require+ [java-time.api :as t]+ [metabase.models.interface :as mi]+ [metabase.util.i18n :refer [deferred-tru]]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(methodical/defmethod t2/table-name :model/WorkspaceLog [_model] :workspace_log)++(doto :model/WorkspaceLog+ (derive :metabase/model)+ (derive :hook/timestamped?))++(t2/deftransforms :model/WorkspaceLog+ {:task mi/transform-keyword+ :status mi/transform-keyword})++(defn- task->description+ "Return a human-readable description for a workspace task."+ [task]+ (case task+ :workspace-setup (deferred-tru "Setting up the workspace")+ :database-isolation (deferred-tru "Provisioning database isolation")+ :mirror-entities (deferred-tru "Mirroring entities")+ :grant-read-access (deferred-tru "Granting permissions")+ (name task)))++(t2/define-after-select :model/WorkspaceLog+ [{:keys [task] :as log}]+ (assoc log :description (task->description task)))++(defn start!+ "Create a log entry for a workspace setup task. Returns the created log."+ [workspace-id task]+ (t2/insert-returning-instance! :model/WorkspaceLog+ {:workspace_id workspace-id+ :task task+ :started_at (t/offset-date-time)+ :status :started}))++(defn- complete!+ [status log-id & [message]]+ (assert (#{:success :failure} status) "It can only be either success or failure")+ (let [data (cond-> {:completed_at (t/offset-date-time)+ :status status}+ message (assoc :message message))]+ (t2/update! :model/WorkspaceLog log-id data)))++(defn success!+ "Task was completed successfully"+ [log-id & [message]]+ (complete! :success log-id message))++(defn failure!+ "Task has failed"+ [log-id & [message]]+ (complete! :failure log-id message))++(defmacro track!+ "Wrap body with automatic logging: starts a log entry, marks success on completion, failure on exception."+ [workspace-id task & body]+ `(let [log# (start! ~workspace-id ~task)]+ (try+ (let [result# (do ~@body)]+ (success! (:id log#))+ result#)+ (catch Exception e#+ (failure! (:id log#) (ex-message e#))+ (throw e#)))))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_merge.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_merge.cljnew file mode 100644index 00000000000..ce3e0b34659--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_merge.clj@@ -0,0 +1,14 @@+(ns metabase-enterprise.workspaces.models.workspace-merge+ "Model for WorkspaceMerge - tracks batch merge events with commit messages.+ Survives workspace deletion through inlined workspace_name."+ (:require+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++;;; -------------------------------------------------- Model Setup --------------------------------------------------++(methodical/defmethod t2/table-name :model/WorkspaceMerge [_model] :workspace_merge)++(doto :model/WorkspaceMerge+ (derive :metabase/model)+ (derive :hook/created-at-timestamped?))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_merge_transform.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_merge_transform.cljnew file mode 100644index 00000000000..8dfa06e1c42--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_merge_transform.clj@@ -0,0 +1,15 @@+(ns metabase-enterprise.workspaces.models.workspace-merge-transform+ "Model for WorkspaceMergeTransform - tracks individual transform merges.+ Linked to WorkspaceMerge for batch merges, standalone for single-transform merges.+ Survives workspace deletion through inlined workspace_name."+ (:require+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++;;; -------------------------------------------------- Model Setup --------------------------------------------------++(methodical/defmethod t2/table-name :model/WorkspaceMergeTransform [_model] :workspace_merge_transform)++(doto :model/WorkspaceMergeTransform+ (derive :metabase/model)+ (derive :hook/created-at-timestamped?))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_output.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_output.cljnew file mode 100644index 00000000000..95f6f7433bd--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_output.clj@@ -0,0 +1,15 @@+(ns metabase-enterprise.workspaces.models.workspace-output+ "Model for WorkspaceOutput - tables that workspace transforms produce."+ (:require+ [metabase.models.interface :as mi]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(methodical/defmethod t2/table-name :model/WorkspaceOutput [_model] :workspace_output)++(doto :model/WorkspaceOutput+ (derive :metabase/model)+ (derive :hook/timestamped?))++(t2/deftransforms :model/WorkspaceOutput+ {:ref_id mi/transform-trim})diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_output_external.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_output_external.cljnew file mode 100644index 00000000000..8c481e1e166--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_output_external.clj@@ -0,0 +1,11 @@+(ns metabase-enterprise.workspaces.models.workspace-output-external+ "Model for WorkspaceOutputExternal - tables that enclosed external transforms produce."+ (:require+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(methodical/defmethod t2/table-name :model/WorkspaceOutputExternal [_model] :workspace_output_external)++(doto :model/WorkspaceOutputExternal+ (derive :metabase/model)+ (derive :hook/timestamped?))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_transform.clj b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_transform.cljnew file mode 100644index 00000000000..59a72030f4b--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/models/workspace_transform.clj@@ -0,0 +1,73 @@+(ns metabase-enterprise.workspaces.models.workspace-transform+ "Model for WorkspaceTransform - holds the changeset of transforms being created+ and edited within a workspace."+ (:require+ [clojure.string :as str]+ [medley.core :as m]+ [metabase.lib-be.core :as lib-be]+ [metabase.lib.core :as lib]+ [metabase.models.interface :as mi]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(methodical/defmethod t2/table-name :model/WorkspaceTransform [_model] :workspace_transform)++;; IMPORTANT: ref_id is NOT unique across workspaces - it corresponds to a Representation id and+;; the same transform can be checked out into multiple workspaces. Always use BOTH workspace_id+;; AND ref_id to identify a specific WorkspaceTransform record.+;; Ordering matches the composite PK index in the database (workspace_id, ref_id),+;; with workspace_id first to support looking up all transforms for a given workspace.+(methodical/defmethod t2/primary-keys :model/WorkspaceTransform [_model] [:workspace_id :ref_id])++(doto :model/WorkspaceTransform+ (derive :metabase/model)+ (derive :hook/timestamped?)+ (derive :hook/entity-id))++;; TODO (Chris 2025-12-11) -- we need to share a bunch of stuff with transforms, i think we'll need to reorganize modules+;; suggestion: add a transforms-interfaces module which both transforms and workspaces depend on.++(defn- transform-source-out-DUPLICATED [m]+ (-> m+ mi/json-out-without-keywordization+ (update-keys keyword)+ (m/update-existing :query lib-be/normalize-query)+ (m/update-existing :type keyword)+ (m/update-existing :source-incremental-strategy #(update-keys % keyword))))++(defn- transform-source-in-DUPLICATED [m]+ (-> m+ (m/update-existing :query (comp lib/prepare-for-serialization lib-be/normalize-query))+ mi/json-in))++(t2/deftransforms :model/WorkspaceTransform+ {:ref_id {:in identity :out str/trim}+ :source {:out transform-source-out-DUPLICATED, :in transform-source-in-DUPLICATED}+ :target mi/transform-json})++(t2/define-before-insert :model/WorkspaceTransform+ [instance]+ ;; Ref id can not be added here due to mysql t2 bug+ (when-not (string? (not-empty (:ref_id instance)))+ (throw (ex-info "ref_id required for WorkspaceTransform insertion."+ {:instance instance})))+ instance)++(t2/define-before-update :model/WorkspaceTransform+ [instance]+ ;; global_id is immutable - a transform's link to the global transform it was checked out from cannot change+ (when (contains? (t2/changes instance) :global_id)+ (let [original (t2/original instance)]+ (throw (ex-info "Cannot change global_id of an existing workspace transform."+ {:status-code 400+ :workspace_id (:workspace_id original)+ :ref_id (:ref_id original)+ :old_global_id (:global_id original)+ :new_global_id (:global_id instance)}))))+ ;; Mark the definition as changed when source or target is updated.+ ;; This is also set redundantly by [[metabase-enterprise.workspaces.impl/increment-analysis-version!]] in the API+ ;; layer, but having it here ensures correctness regardless of the code path used to update the transform.+ (cond-> instance+ (or (contains? (t2/changes instance) :source)+ (contains? (t2/changes instance) :target))+ (assoc :definition_changed true)))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/settings.clj b/enterprise/backend/src/metabase_enterprise/workspaces/settings.cljnew file mode 100644index 00000000000..77b14fbd071--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/settings.clj@@ -0,0 +1,25 @@+(ns metabase-enterprise.workspaces.settings+ (:require+ [metabase.settings.core :as setting]+ [metabase.util.i18n :refer [deferred-tru]]))++(setting/defsetting database-enable-workspaces+ (deferred-tru "Whether the database has workspaces enabled")+ :default false+ :feature :workspaces+ :driver-feature :workspace+ :enabled-for-db? (fn [db]+ (setting/custom-disabled-reasons!+ (if-let [permission-status (:workspace_permissions_status db)]+ (when-let [error (:error permission-status)]+ [{:type :error+ :key :workspaces/permissions-missing+ ;; TODO (Ngoc 2026-01-20) -- localize error message - GDGT-1552+ :message error}])+ [{:type :error+ :key :workspaces/permissions-unchecked+ :message (deferred-tru "Workspace connection must be tested explicitly.")}])))+ :type :boolean+ :visibility :public+ :database-local :only+ :export? true)diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/types.clj b/enterprise/backend/src/metabase_enterprise/workspaces/types.cljnew file mode 100644index 00000000000..a8408ce2c49--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/types.clj@@ -0,0 +1,174 @@+(ns metabase-enterprise.workspaces.types+ (:require+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(mr/def ::appdb-id ms/PositiveInt)++(mr/def ::ref-id [:string {:min 1 :api/regex #".+"}])++(def entity-types+ "The kinds of entities we can store within a Workspace."+ [{:name "Transform"+ :key :transform+ :group :transforms+ :model :model/Transform}])++(mr/def ::entity-type (into [:enum] (map :key) entity-types))++(mr/def ::entity-grouping (into [:enum] (map :group) entity-types))++;; Map like {:transforms [1 2 3]}+(mr/def ::entity-map+ [:map-of ::entity-grouping [:sequential ms/PositiveInt]])++(mr/def ::execution-result+ [:map+ [:status [:enum :succeeded :failed]]+ [:start_time {:optional true} [:maybe some?]]+ [:end_time {:optional true} [:maybe some?]]+ [:message {:optional true} [:maybe :string]]+ [:table [:map+ [:name :string]+ [:schema {:optional true} [:maybe :string]]]]])++(mr/def ::dry-run-result+ "Result of a transform dry-run (preview without persisting).+ Data is nested under :data to match /api/dataset response format."+ [:map+ [:status [:enum :succeeded :failed]]+ [:message {:optional true} [:maybe :string]]+ [:data {:optional true}+ [:map+ [:rows {:optional true} [:sequential :any]]+ [:cols {:optional true} [:sequential :map]]+ [:results_metadata {:optional true} [:map+ [:columns {:optional true} [:sequential :map]]]]]]])++;;; ---------------------------------------- Graph/Problem Types ----------------------------------------++;; Node types used in workspace graphs and problem detection+(mr/def ::node-type [:enum :input-table :external-transform :workspace-transform])++(mr/def ::table-coord+ "Logical reference to a table by database, schema, and name."+ [:map+ [:db_id ::appdb-id]+ [:schema [:maybe :string]]+ [:table :string]])++(mr/def ::node-ref+ "Reference to a node in the graph or a related entity."+ [:map+ [:type ::node-type]+ [:id [:or ::appdb-id ::ref-id]]+ [:name {:optional true} :string]])++;;; ---------------------------------------- Problem Types ----------------------------------------++;; Problem type definitions with metadata for FE display and merge-blocking behavior+;; See validation.clj docstring for full documentation table++(def problem-types+ "All problem types with their metadata.++ Each entry has:+ - :description - human-readable explanation+ - :severity - :error, :warning, or :info+ - :block-merge - whether this problem should prevent merging+ - :static? - true if determinable from metadata alone, false if requires runtime info++ ## Categories++ Dependency-based (about output state) - prefix indicates who depends on the output:+ - unused-* : nothing depends on this output+ - internal-downstream-* : transforms within the workspace graph depend on this+ - external-downstream-* : transforms outside the workspace depend on this++ Structural (about graph shape) - prefix indicates scope of the issue:+ - internal-* : between workspace transforms only+ - external-* : involves global transforms (upstream or downstream)"+ {;; No dependents (informational)+ :unused/not-run {:description "Output hasn't been created yet, but nothing depends on it"+ :severity :info+ :block-merge false+ :static? true}+ :unused/stale {:description "Output is stale (needs re-run), but nothing depends on it"+ :severity :info+ :block-merge false+ :static? true}+ :unused/failed {:description "Transform failed on last run, but nothing depends on it"+ :severity :error+ :block-merge false+ :static? false}++ ;; Internal dependents (blocks workspace progress)+ :internal-downstream/not-run {:description "Output hasn't been created yet, other workspace transforms need it"+ :severity :warning+ :block-merge true+ :static? true}+ :internal-downstream/stale {:description "Output is stale, other workspace transforms need fresh data"+ :severity :warning+ :block-merge true+ :static? true}+ :internal-downstream/failed {:description "Transform failed on last run, other workspace transforms need it"+ :severity :error+ :block-merge true+ :static? false}++ ;; External dependents (would affect things after merge)+ :external-downstream/not-run {:description "Output hasn't been created yet, external transforms depend on it"+ :severity :warning+ :block-merge true+ :static? true}+ :external-downstream/stale {:description "Output is stale, external transforms are using outdated data"+ :severity :warning+ :block-merge true+ :static? true}+ :external-downstream/removed-field {:description "Field was removed that external transforms reference"+ :severity :error+ :block-merge true+ :static? true}+ :external-downstream/removed-table {:description "Table was removed, external transforms will stop receiving updates"+ :severity :warning+ :block-merge false+ :static? true}++ ;; Structural issues (conflicts/cycles)+ :internal/target-conflict {:description "Multiple workspace transforms target the same table"+ :severity :error+ :block-merge true+ :static? true}+ :internal/cycle {:description "Circular dependency between workspace transforms"+ :severity :error+ :block-merge true+ :static? true}+ :external/target-conflict {:description "Workspace transform conflicts with an existing global transform's target"+ :severity :error+ :block-merge true+ :static? true}+ :external/cycle {:description "Workspace changes would create a circular dependency with global transforms"+ :severity :error+ :block-merge true+ :static? true}})++(mr/def ::problem-type (into [:enum] (keys problem-types)))++(mr/def ::severity [:enum :error :warning :info])++;; Categories derived from problem-type namespaces+(mr/def ::problem-category+ (into [:enum] (distinct (map (comp keyword namespace) (keys problem-types)))))++;; Problem names derived from problem-type names+(mr/def ::problem-name+ (into [:enum] (distinct (map (comp keyword name) (keys problem-types)))))++(mr/def ::problem+ "A problem detected during workspace validation."+ [:map+ [:category ::problem-category]+ [:problem ::problem-name]+ [:severity ::severity]+ [:block_merge :boolean]+ [:data :map]])diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/util.clj b/enterprise/backend/src/metabase_enterprise/workspaces/util.cljnew file mode 100644index 00000000000..b31278856e7--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/util.clj@@ -0,0 +1,205 @@+(ns metabase-enterprise.workspaces.util+ (:require+ [clojure.string :as str]+ [metabase.system.core :as system]+ [metabase.util.log :as log]+ [toucan2.core :as t2]))++(defn assert-transform!+ "Test whether we support the given entity type within workspaces yet.+ Named for the only case we support currently, to make call sites assumptions more obvious."+ [entity-type]+ (when (not= "transform" (name entity-type))+ (throw (ex-info "Only transform entity type is supported"+ {:status-code 400+ :entity-type entity-type}))))++(defn assert-transforms!+ "Test that only supported types are given in the given list.+ Named for the only case we support currently, to make call sites assumptions more obvious."+ [entities]+ (when-let [other-types (seq (remove #{:transform} (map :entity-type entities)))]+ (throw (ex-info "Only transform entities are currently supported"+ {:status-code 400+ :unsupported-types other-types}))))++(defn- toposort-visit [node child->parents visited result]+ (cond+ (visited node) [visited result]+ :else (let [parents (child->parents node [])+ [visited' result'] (reduce (fn [[v r] p]+ (toposort-visit p child->parents v r))+ [(conj visited node) result]+ parents)]+ [visited' (conj result' node)])))++(defn toposort-dfs+ "Perform a topological sort using depth-first search.+ Takes a map from child nodes to their parent nodes (dependencies).+ Returns nodes in topological order (dependencies before dependents)."+ [child->parents]+ ;; TODO (Chris 2025-11-20) -- Detect cycles and throw an error.+ (let [all-nodes (set (keys child->parents))]+ (loop [visited #{}+ result []+ remaining all-nodes]+ (if (empty? remaining)+ result+ (let [node (first remaining)+ [visited' result'] (toposort-visit node child->parents visited result)]+ (recur visited' result' (disj remaining node)))))))++;;; Naming++;; re-using https://github.com/metabase/metabase/pull/61887/commits/c92e4a9cc451c61a13fef19ed9d6107873b17f07+;; (original ws isolation code)+(defn- instance-uuid-slug+ "Create a slug from the site UUID, taking the first character of each section."+ [site-uuid-string]+ (->> (str/split site-uuid-string #"-")+ (map first)+ (apply str)))++;; WARNING: Changing this prefix requires backwards compatibility handling for existing workspaces.+;; The prefix is used to identify isolation namespaces in the database, and existing workspaces+;; will have namespaces created with the current prefix.+(def ^:private isolated-prefix "mb__isolation")++(defn isolation-namespace-name+ "Generate namespace/database name for workspace isolation following mb__isolation_<slug>_<workspace-id> pattern.+ Uses 'namespace' as the generic term that maps to 'schema' in Postgres, 'database' in ClickHouse, etc."+ [workspace]+ (assert (some? (:id workspace)) "Workspace must have an :id")+ (let [instance-slug (instance-uuid-slug (str (system/site-uuid)))+ clean-workspace-id (str/replace (str (:id workspace)) #"[^a-zA-Z0-9]" "_")]+ (format "%s_%s_%s" isolated-prefix instance-slug clean-workspace-id)))++(defn isolated-table-name+ "Generate name for a table mirroring transform target table in the isolated database namespace.+ Returns schema__name when schema is present, or __name when schema is nil (to distinguish from global tables)."+ [schema name]+ ;; In perverse cases, this might not be unique.+ ;; Since we persist the mappings in the appdb associated with each transform output, we *could* detect conflicts and+ ;; restore uniqueness. Given the low risk, we chose to do nothing for now.+ (if schema+ (format "%s__%s" schema name)+ (format "__%s" name)))++(defn isolation-user-name+ "Generate username for workspace isolation."+ [workspace]+ (let [instance-slug (instance-uuid-slug (str (system/site-uuid)))]+ (format "%s_%s_%s" isolated-prefix instance-slug (:id workspace))))++(def ^:private password-char-sets+ "Character sets for password generation. Cycles through these to ensure representation from each."+ ["ABCDEFGHJKLMNPQRSTUVWXYZ"+ "abcdefghjkmnpqrstuvwxyz"+ "123456789"+ "!#$%&*+-="])++(defn random-isolated-password+ "Generate a random password suitable for most database engines.+ Ensures the password contains characters from all sets (uppercase, lowercase, digits, special)+ by cycling through the character sets. Result is shuffled for randomness."+ []+ (->> (cycle password-char-sets)+ (take (+ 32 (rand-int 32)))+ (map rand-nth)+ shuffle+ (apply str)))++;;; -------------------------------------------------- Cute Names --------------------------------------------------++(def ^:private adjectives+ "Adjectives for generating cute ref IDs, inspired by Docker's naming scheme."+ ["admiring" "adoring" "affectionate" "agitated" "amazing" "angry" "awesome"+ "beautiful" "blissful" "bold" "boring" "brave" "busy" "charming" "clever"+ "cool" "compassionate" "competent" "condescending" "confident" "cranky"+ "crazy" "dazzling" "determined" "distracted" "dreamy" "eager" "ecstatic"+ "elastic" "elated" "elegant" "eloquent" "epic" "exciting" "fervent"+ "festive" "flamboyant" "focused" "friendly" "frosty" "funny" "gallant"+ "gifted" "goofy" "gracious" "great" "happy" "hardcore" "heuristic" "hopeful"+ "hungry" "infallible" "inspiring" "intelligent" "interesting" "jolly"+ "jovial" "keen" "kind" "laughing" "loving" "lucid" "magical" "modest"+ "musing" "mystifying" "naughty" "nervous" "nice" "nifty" "nostalgic"+ "objective" "optimistic" "peaceful" "pedantic" "pensive" "practical"+ "priceless" "quirky" "quizzical" "recursing" "relaxed" "reverent" "romantic"+ "sad" "serene" "sharp" "silly" "sleepy" "stoic" "strange" "stupefied"+ "suspicious" "sweet" "tender" "thirsty" "trusting" "unruffled" "upbeat"+ "vibrant" "vigilant" "vigorous" "wizardly" "wonderful" "xenodochial" "youthful"+ "zealous" "zen"])++(def ^:private animals+ "Animals for generating cute ref IDs."+ ["albatross" "alligator" "alpaca" "ant" "anteater" "antelope" "armadillo"+ "baboon" "badger" "bat" "bear" "beaver" "bee" "bison" "boar" "buffalo"+ "butterfly" "camel" "capybara" "caribou" "cat" "caterpillar" "cheetah"+ "chicken" "chimpanzee" "chinchilla" "cobra" "condor" "cormorant" "coyote"+ "crab" "crane" "crocodile" "crow" "deer" "dingo" "dog" "dolphin" "donkey"+ "dove" "dragonfly" "duck" "eagle" "echidna" "eel" "elephant" "elk" "emu"+ "falcon" "ferret" "finch" "firefly" "flamingo" "fox" "frog" "gazelle"+ "gerbil" "giraffe" "goat" "goose" "gopher" "gorilla" "grasshopper" "grouse"+ "hamster" "hare" "hawk" "hedgehog" "heron" "hippopotamus" "hornet" "horse"+ "hummingbird" "hyena" "ibex" "iguana" "impala" "jackal" "jaguar" "jellyfish"+ "kangaroo" "kingfisher" "kiwi" "koala" "komodo" "kookaburra" "lemur"+ "leopard" "lion" "lizard" "llama" "lobster" "lynx" "magpie" "mallard"+ "manatee" "mandrill" "marmot" "meerkat" "mink" "mole" "mongoose" "monkey"+ "moose" "moth" "mouse" "mule" "narwhal" "newt" "nightingale" "ocelot"+ "octopus" "okapi" "opossum" "orangutan" "orca" "oryx" "osprey" "ostrich"+ "otter" "owl" "ox" "panda" "panther" "parrot" "partridge" "peacock"+ "pelican" "penguin" "pheasant" "pig" "pigeon" "platypus" "pony" "porcupine"+ "porpoise" "puma" "quail" "rabbit" "raccoon" "ram" "raven" "reindeer"+ "rhinoceros" "salamander" "salmon" "sardine" "scorpion" "seahorse" "seal"+ "shark" "sheep" "shrew" "skunk" "sloth" "snail" "snake" "sparrow" "spider"+ "squid" "squirrel" "starfish" "stingray" "stork" "swallow" "swan" "tapir"+ "tiger" "toad" "toucan" "turkey" "turtle" "viper" "vulture" "wallaby"+ "walrus" "warthog" "wasp" "weasel" "whale" "wolf" "wolverine" "wombat"+ "woodpecker" "yak" "zebra"])++(defn generate-name+ "Generate a cute adjective-animal style name. Making sure it's unique is your job."+ []+ (str (rand-nth adjectives) "-" (rand-nth animals)))++(defn generate-ref-id+ "Generate a cute adjective-animal style ref ID with a random suffix.+ Format: adjective-animal-xxxx (where xxxx is a random 4-char hex suffix)"+ []+ (str (generate-name) "-" (format "%04x" (rand-int 65536))))++;;; -------------------------------- Epochal Versioning Helpers ------------------------------------------++(defmacro ignore-constraint-violation+ "Execute body and silently return nil if a unique constraint violation occurs.+ Used in epochal versioning where concurrent processes may race to insert the same version.+ If the exception is not a constraint violation, it is rethrown."+ [& body]+ `(try+ ~@body+ (catch Exception e#+ ;; Check if this is a constraint violation (database-specific messages)+ ;; H2: "Unique index or primary key violation"+ ;; PostgreSQL: "duplicate key value violates unique constraint"+ ;; MySQL: "Duplicate entry"+ (let [msg# (or (ex-message e#) "")+ cause-msg# (or (some-> e# ex-cause ex-message) "")]+ (if (or (re-find #"(?i)unique|duplicate|constraint" msg#)+ (re-find #"(?i)unique|duplicate|constraint" cause-msg#))+ ;; It's good to keep these visible, as they should be rare and can cause transient consistency issues.+ (log/info "Ignoring constraint violation (concurrent insert race):" msg#)+ (throw e#))))))++;;; -------------------------------- Toucan2 MySQL Workarounds ------------------------------------------++(defn insert-returning-ws-tx!+ "Insert a WorkspaceTransform and return the inserted instance.++ Works around a Toucan2 limitation where `t2/insert-returning-instance!` returns nil+ for tables with composite primary keys on MySQL and H2. These databases don't reliably+ support RETURNING for composite PKs, so we fall back to select after insert.++ See: https://github.com/camsaul/toucan2/issues/204"+ [row]+ (or (t2/insert-returning-instance! :model/WorkspaceTransform row)+ (t2/select-one :model/WorkspaceTransform :workspace_id (:workspace_id row), :ref_id (:ref_id row))))diff --git a/enterprise/backend/src/metabase_enterprise/workspaces/validation.clj b/enterprise/backend/src/metabase_enterprise/workspaces/validation.cljnew file mode 100644index 00000000000..2d27343fd84--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/workspaces/validation.clj@@ -0,0 +1,280 @@+(ns metabase-enterprise.workspaces.validation+ "Validation of workspace changes against downstream dependencies.++ ASSUMPTION: All transforms in the workspace have been run, and their outputs are not stale.+ This validation checks the *current* isolated table schema against external transform requirements.++ ## Problem Types++ See `metabase-enterprise.workspaces.types/problem-types` for the full list of problem types+ with their descriptions, severity levels, and merge-blocking behavior.++ ## Currently Implemented++ | Category | Problem | Description |+ |---------------------|---------------|----------------------------------------------------------------|+ | unused | not-run | Output hasn't been created, nothing depends on it |+ | internal-downstream | not-run | Output hasn't been created, other workspace transforms need it |+ | external-downstream | not-run | Output hasn't been created, external transforms need it |+ | external-downstream | removed-field | Field was removed that external transforms reference |"+ (:require+ [metabase-enterprise.workspaces.types :as ws.t]+ [metabase.lib-be.core :as lib-be]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata.protocols :as lib.metadata.protocols]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++;;;; ---------------------------------------- Metadata Provider Override ----------------------------------------++(defn- make-field-override-provider+ "Create a metadata provider that overrides a table's fields with a different set of fields.+ Used to simulate what external transforms would see after a merge.++ - `base-provider`: the underlying metadata provider+ - `target-table-id`: the global table ID whose fields we're overriding+ - `replacement-fields`: the fields from the isolated table (what fields will exist after merge)"+ [base-provider target-table-id replacement-fields]+ (let [replacement-field-names (into #{} (map :name) replacement-fields)+ ;; TODO (Chris 2026-02-02) -- better to do 1 query up-front for *all* the tables we will validate.+ table-field-id? (t2/select-fn-set :id [:model/Field :id] :table_id target-table-id)]+ (reify+ lib.metadata.protocols/MetadataProvider+ (database [_this]+ (lib.metadata.protocols/database base-provider))+ (metadatas [_this metadata-spec]+ ;; Yes, for some reason :id is actually a set of ids+ (let [{spec-type :lib/type spec-field-ids :id} metadata-spec]+ (if (and (= :metadata/column spec-type) (some table-field-id? spec-field-ids))+ ;; Return replacement fields with :active based on whether they exist+ (let [original-fields (lib.metadata.protocols/metadatas base-provider metadata-spec)]+ (map (fn [field]+ (if (contains? replacement-field-names (:name field))+ field+ (assoc field :active false)))+ original-fields))+ ;; Delegate everything else+ (lib.metadata.protocols/metadatas base-provider metadata-spec))))+ (setting [_this setting-key]+ (lib.metadata.protocols/setting base-provider setting-key))++ lib.metadata.protocols/CachedMetadataProvider+ (cached-metadatas [_this metadata-type metadata-ids]+ (lib.metadata.protocols/cached-metadatas base-provider metadata-type metadata-ids))+ (store-metadata! [_this a-metadata]+ (lib.metadata.protocols/store-metadata! base-provider a-metadata))+ (cached-value [_this k not-found]+ (lib.metadata.protocols/cached-value base-provider k not-found))+ (cache-value! [_this k v]+ (lib.metadata.protocols/cache-value! base-provider k v))+ (has-cache? [_this]+ (lib.metadata.protocols/has-cache? base-provider))+ (clear-cache! [_this]+ (lib.metadata.protocols/clear-cache! base-provider)))))++;;;; ---------------------------------------- External Transform Discovery ----------------------------------------++(defn- workspace-outputs+ "Get all workspace outputs with their global and isolated table info.+ Includes both workspace transform outputs and enclosed external transform outputs."+ [workspace-id]+ ;; TODO (Chris 2026-02-02) -- rather fetch all this as an input to graph analysis, and save it in [[graph]].+ (let [ws-outputs (t2/select [:model/WorkspaceOutput+ :id :ref_id :db_id+ :global_schema :global_table :global_table_id+ :isolated_schema :isolated_table :isolated_table_id]+ :workspace_id workspace-id)+ ;; External outputs use transform_id (int) instead of ref_id (string)+ ;; Map transform_id to ref_id for compatibility with the rest of the validation code+ ext-outputs (map #(assoc % :ref_id (:transform_id %) :external? true)+ (t2/select [:model/WorkspaceOutputExternal+ :id :transform_id :db_id+ :global_schema :global_table :global_table_id+ :isolated_schema :isolated_table :isolated_table_id]+ :workspace_id workspace-id))]+ (concat ws-outputs ext-outputs)))++(defn- ids-for-type [entities desired-node-type]+ (for [{:keys [node-type id]} entities, :when (= node-type desired-node-type)] id))++(defn- checked-out-transform-ids+ "Get the global IDs of transforms that are checked out into this workspace.+ Extracts workspace-transform ref_ids from the graph and looks up their global_ids."+ [{:keys [entities] :as _graph}]+ (if-let [ref-ids (seq (ids-for-type entities :workspace-transform))]+ ;; TODO (Chris 2026-02-02) -- rather fetch this as an input to graph analysis, and save it in [[graph]].+ (t2/select-fn-set :global_id+ [:model/WorkspaceTransform :global_id]+ :ref_id [:in ref-ids]+ {:where [:not= :global_id nil]})+ #{}))++(defn- enclosed-transform-ids+ "Get the IDs of external transforms that are enclosed by the workspace graph.+ These transforms run within the workspace, so they don't need further static validation."+ [{:keys [entities] :as _graph}]+ (ids-for-type entities :external-transform))++(def ^:private table? #{:table})++(defn- internal-dependents+ "Build a map of non-table ids to the non-table ids that directly depend on it (internal to workspace).+ This is essentially an inversion of the dependencies map, with some filtering."+ [{:keys [dependencies] :as _graph}]+ ;; :dependencies is {child #{parents...}}, we need to invert to {parent #{children...}}+ (reduce (fn [acc [{child-type :node-type, child-id :id} parents]]+ (if (table? child-type)+ acc+ (reduce (fn [acc {parent-type :node-type, parent-id :id}]+ (if (table? parent-type)+ acc+ (update acc parent-id (fnil conj #{}) child-id)))+ acc+ parents)))+ {}+ dependencies))++(defn- external-transform-dependents+ "Get external transforms that depend on a given table (by global_table_id).+ Excludes transforms that are checked out or enclosed in the workspace."+ [global-table-id checked-out-ids enclosed-ids]+ (when global-table-id+ (let [dependent-transform-ids (t2/select-fn-set :from_entity_id+ [:model/Dependency :from_entity_id]+ :to_entity_type :table+ :to_entity_id global-table-id+ :from_entity_type :transform)+ ;; Remove checked out and enclosed transforms+ external-ids (-> dependent-transform-ids+ (disj nil)+ (as-> ids (apply disj ids checked-out-ids))+ (as-> ids (apply disj ids enclosed-ids)))]+ (when (seq external-ids)+ (t2/select [:model/Transform :id :name :source] :id [:in external-ids])))))++;;;; ---------------------------------------- Validation Logic ----------------------------------------++(defn- validate-transform-against-fields+ "Check if a transform would break given a modified set of fields.+ Returns bad refs if the transform's query references fields that don't exist in replacement-fields,+ or nil if there are no bad refs."+ [transform global-table-id replacement-fields]+ (let [db-id (get-in transform [:source :query :database])]+ (when db-id+ (let [base-provider (lib-be/application-database-metadata-provider db-id)+ override-provider (make-field-override-provider base-provider global-table-id replacement-fields)+ query (lib/query override-provider (get-in transform [:source :query]))]+ (not-empty (lib/find-bad-refs query))))))++(defn- get-table-fields+ "Get all active fields for a table by ID."+ [table-id]+ (when table-id+ (t2/select [:model/Field :id :name :base_type :semantic_type]+ :table_id table-id+ :active true)))++(defn- make-problem+ "Construct a problem map with type metadata from ws.t/problem-types.+ Splits the namespaced problem-type into :category and :problem keys."+ [problem-type data]+ (let [{:keys [severity block-merge description]} (get ws.t/problem-types problem-type)]+ {:category (keyword (namespace problem-type))+ :problem (keyword (name problem-type))+ :severity severity+ :block_merge block-merge+ :description description+ :data data}))++(defn- problems-for-output+ "Find problems related to a single workspace output.+ Returns a sequence of problem maps."+ [output checked-out-ids enclosed-ids internal-dependents-map]+ (let [{:keys [ref_id db_id global_schema global_table global_table_id+ isolated_table_id external?]} output+ ;; For external outputs, ref_id is actually the transform_id (an int)+ transform-type (if external? :external-transform :workspace-transform)+ external-transforms (external-transform-dependents global_table_id checked-out-ids enclosed-ids)+ has-external-dependents? (seq external-transforms)+ internal-dependents (get internal-dependents-map ref_id)+ has-internal-dependents? (seq internal-dependents)+ table-coord {:db_id db_id :schema global_schema :table global_table}]++ (cond+ ;; Case 1: Isolated table doesn't exist yet+ (nil? isolated_table_id)+ (cond+ ;; External transforms depend on this output that doesn't exist+ has-external-dependents?+ [(make-problem :external-downstream/not-run+ {:output table-coord+ :transform {:type transform-type+ :id ref_id}+ :dependents (mapv (fn [{:keys [id name]}]+ {:type :external-transform+ :id id+ :name name})+ external-transforms)})]+ ;; Internal workspace transforms depend on this output that doesn't exist+ has-internal-dependents?+ [(make-problem :internal-downstream/not-run+ {:output table-coord+ :transform {:type transform-type+ :id ref_id}+ :dependents (mapv (fn [dep-ref-id]+ {:type :workspace-transform+ :id dep-ref-id})+ internal-dependents)})]+ ;; No dependents - just informational+ :else+ [(make-problem :unused/not-run+ {:output table-coord+ :transform {:type transform-type+ :id ref_id}})])++ ;; Case 2: Isolated table exists - check for field breakages+ has-external-dependents?+ (let [isolated-fields (get-table-fields isolated_table_id)]+ (into []+ (mapcat (fn [transform]+ (when-let [bad-refs (validate-transform-against-fields+ transform global_table_id isolated-fields)]+ [(make-problem :external-downstream/removed-field+ {:output table-coord+ :transform {:type :external-transform+ :id (:id transform)+ :name (:name transform)}+ :bad-refs bad-refs})])))+ external-transforms))++ ;; Case 3: No problems+ :else [])))++;;;; ---------------------------------------- Public API ----------------------------------------++(defn find-downstream-problems+ "Find problems that would affect transforms outside the workspace after merge.++ Takes the workspace graph (from `ws.dag/path-induced-subgraph`) as an argument.++ Returns a sequence of problem maps, each with:+ - :category - the problem category (e.g. :unused, :internal-downstream, :external-downstream)+ - :problem - the specific problem (e.g. :not-run, :removed-field)+ - :severity - :error, :warning, or :info+ - :block-merge - whether this problem should prevent merging+ - :data - further data, depending on type++ Currently implemented problem types (category + problem):+ - :unused/not-run - output table hasn't been created (informational, no dependents)+ - :internal-downstream/not-run - output table hasn't been created, other workspace transforms need it+ - :external-downstream/not-run - output table hasn't been created, but external transforms depend on it+ - :external-downstream/removed-field - a field was removed that external transforms need"+ [workspace-id graph]+ (let [outputs (workspace-outputs workspace-id)+ checked-out-ids (checked-out-transform-ids graph)+ enclosed-ids (enclosed-transform-ids graph)+ dependents (internal-dependents graph)]+ (into []+ (mapcat #(problems-for-output % checked-out-ids enclosed-ids dependents))+ outputs)))diff --git a/enterprise/backend/test/metabase_enterprise/api/session_test.clj b/enterprise/backend/test/metabase_enterprise/api/session_test.cljindex 310c3d5f24f..7346d1a4f67 100644--- a/enterprise/backend/test/metabase_enterprise/api/session_test.clj+++ b/enterprise/backend/test/metabase_enterprise/api/session_test.clj@@ -56,7 +56,8 @@:collection-cleanup:database-routing:tenants- :cloud-custom-smtp}+ :cloud-custom-smtp+ :workspaces}(is (= {:advanced_permissions true:attached_dwh true:audit_app true@@ -107,5 +108,6 @@:cloud_custom_smtp true:etl_connections false:etl_connections_pg false- :dependencies false}+ :dependencies false+ :workspaces true}(:token-features (mt/user-http-request :crowberto :get 200 "session/properties"))))))diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/calculation_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/calculation_test.cljindex 80997d6e006..b0dabaf479f 100644--- a/enterprise/backend/test/metabase_enterprise/dependencies/calculation_test.clj+++ b/enterprise/backend/test/metabase_enterprise/dependencies/calculation_test.clj@@ -193,6 +193,22 @@:table #{checkins-id venues-id}}(calculation/upstream-deps:transform transform)))))))+(deftest ^:parallel upstream-deps-python-transform-test+ (mt/with-premium-features #{:transforms}+ (let [products-id (mt/id :products)+ orders-id (mt/id :orders)]+ (mt/with-temp [:model/Transform transform {:name "Test Transform"+ :source {:type :python+ :source-tables {"PRODUCTS" products-id+ "ORDERS" orders-id}+ ;; A problematic field, hopefully removed again.+ :source-database (mt/id)+ :body "..."}+ :target {:schema "PUBLIC"+ :name "test_output"}}]+ (is (= {:table #{products-id orders-id}}+ (calculation/upstream-deps:transform transform)))))))+(deftest ^:parallel upstream-deps-card-native-with-parameter-source-test(let [mp (mt/metadata-provider)products-id (mt/id :products)diff --git a/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.cljindex 78877a5c9b1..899db5d9d41 100644--- a/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj+++ b/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj@@ -109,7 +109,19 @@:model/ConnectionImpersonation:model/CloudMigration:model/Comment- :model/CommentReaction})+ :model/CommentReaction+ ;; TODO (lbrdnk 2025-12-17) -- I've added rest of the workspace models here as Workspace was present. I believe+ ;; going forward all of that will be available for export. We should revisit this later in the project.+ :model/Workspace+ :model/WorkspaceInput+ :model/WorkspaceInputExternal+ :model/WorkspaceLog+ :model/WorkspaceMerge+ :model/WorkspaceMergeTransform+ :model/WorkspaceGraph+ :model/WorkspaceOutput+ :model/WorkspaceOutputExternal+ :model/WorkspaceTransform})(deftest ^:parallel comprehensive-entity-id-test(let [entity-id-models (->> (v2.entity-ids/toucan-models)diff --git a/enterprise/backend/test/metabase_enterprise/transforms/jobs_test.clj b/enterprise/backend/test/metabase_enterprise/transforms/jobs_test.cljindex 2619a380c9a..e6d09b32d1e 100644--- a/enterprise/backend/test/metabase_enterprise/transforms/jobs_test.clj+++ b/enterprise/backend/test/metabase_enterprise/transforms/jobs_test.clj@@ -305,7 +305,7 @@;; depends on faulty transform:model/Transform t2 {:name "transform2":source {:type :query- :query (lib/native-query mp (str/replace sql "transforms_products" (:name target1)))}+ :query (lib/native-query mp (str/replace sql (:name table) (:name target1)))}:creator_id (mt/user->id :crowberto):target target2}:model/TransformTransformTag _tag2 {:transform_id (:id t2)diff --git a/enterprise/backend/test/metabase_enterprise/transforms/ordering_test.clj b/enterprise/backend/test/metabase_enterprise/transforms/ordering_test.cljindex f2a1cc03957..709e907fcfd 100644--- a/enterprise/backend/test/metabase_enterprise/transforms/ordering_test.clj+++ b/enterprise/backend/test/metabase_enterprise/transforms/ordering_test.clj@@ -11,11 +11,13 @@[metabase.test.data.sql :as sql.tx][toucan2.core :as t2]))+(defn- default-schema-or-public [& [fallback-driver]]+ (let [driver (or driver/*driver* fallback-driver)]+ (or (and driver (driver.sql/default-schema driver)) "public")))+(defn- make-transform [query & [name schema]](let [name (or name (mt/random-name))- default-schema (when (get-method driver.sql/default-schema driver/*driver*)- (driver.sql/default-schema driver/*driver*))- schema (or schema default-schema "public")]+ schema (or schema (default-schema-or-public))]{:source {:type :query:query query}:name (str "transform_" name)@@ -23,41 +25,43 @@:name name:type :table}}))+(use-fixtures :once (fn [thunk]+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)+ (thunk))))+(deftest basic-ordering-test- (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)- (mt/with-temp [:model/Transform {t1 :id} (make-transform- {:database (mt/id),- :type "query",- :query {:source-table (mt/id :orders)}})]- (is (= {t1 #{}}- (ordering/transform-ordering (t2/select :model/Transform :id t1)))))))+ (mt/with-temp [:model/Transform {t1 :id} (make-transform+ {:database (mt/id),+ :type "query",+ :query {:source-table (mt/id :orders)}})]+ (is (= {t1 #{}}+ (ordering/transform-ordering (t2/select :model/Transform :id t1))))))(deftest dependency-ordering-test- (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)- (mt/with-temp [:model/Table {table :id} {:schema "public"- :name "orders_2"}- :model/Field _ {:table_id table- :name "foo"}- :model/Transform {parent :id} (make-transform- {:database (mt/id),- :type "query",- :query {:source-table (mt/id :orders)}}- "orders_2")- :model/Transform {child :id} (make-transform- {:database (mt/id)- :type "query"- :query {:source-table table}}- "orders_3")]- (is (= {parent #{}- child #{parent}}- (ordering/transform-ordering (t2/select :model/Transform :id [:in [parent child]])))))))+ (mt/with-temp [:model/Table {table :id} {:schema (default-schema-or-public)+ :name "orders_2"}+ :model/Field _ {:table_id table+ :name "foo"}+ :model/Transform {parent :id} (make-transform+ {:database (mt/id),+ :type "query",+ :query {:source-table (mt/id :orders)}}+ "orders_2")+ :model/Transform {child :id} (make-transform+ {:database (mt/id)+ :type "query"+ :query {:source-table table}}+ "orders_3")]+ (is (= {parent #{}+ child #{parent}}+ (ordering/transform-ordering (t2/select :model/Transform :id [:in [parent child]]))))))(defn- transform-deps-for-db [transform](mt/with-metadata-provider (mt/id)(#'transforms.i/table-dependencies transform)))(deftest not-run-transform-dependency-ordering-test- (mt/test-driver :postgres+ (mt/test-driver (mt/normal-driver-select {:+parent :sql-jdbc})(testing "dependencies are correctly identified when no transforms have been run yet"(mt/with-temp [:model/Transform {t1 :id} (make-transform{:database (mt/id),@@ -124,31 +128,31 @@(t2/delete! :model/Table :name "checkins_transform")))))))(deftest bigquery-native-transform-dependency-ordering-test- (mt/test-driver :bigquery-cloud-sdk+ (mt/test-driver :biquery-cloud-sdk(let [[dataset] (sql.tx/qualified-name-components :bigquery-cloud-sdk nil)](testing "dependencies are correctly identified when some transform have been run and some haven't"(mt/with-temp [:model/Transform {t1 :id} (make-transform{:database (mt/id),- :type :native,- :native {:query (format "SELECT * FROM `%s.checkins` LIMIT 100" dataset)}}+ :type :native,+ :native {:query (format "SELECT * FROM `%s.checkins` LIMIT 100" dataset)}}"checkins_transform" dataset):model/Table {table1 :id} {:schema dataset- :name "checkins_transform"}+ :name "checkins_transform"}:model/Transform {t2 :id} (make-transform{:database (mt/id),- :type :native,- :native {:query (format "SELECT * FROM `%s.venues` LIMIT 100" dataset)}}+ :type :native,+ :native {:query (format "SELECT * FROM `%s.venues` LIMIT 100" dataset)}}"venues_transform" dataset):model/Transform {t3 :id} (make-transform{:database (mt/id),- :type :native,- :native {:query (format "SELECT * FROM `%s.venues_transform` vt+ :type :native,+ :native {:query (format "SELECT * FROM `%s.venues_transform` vtLEFT JOIN `%s.checkins_transform` ctON vt.id = ct.venue_idLIMIT 100" dataset dataset)}}"venues_transform_2" dataset)](is (= #{{:table table1} {:transform t2}}- (transform-deps-for-db (t2/select-one :model/Transform t3))))+ (transform-deps-for-db (t2/select-one :model/Transform t3))))(is (= {t1 #{}t2 #{}t3 #{t1 t2}}@@ -163,28 +167,29 @@(transform-deps-for-db (t2/select-one :model/Transform :id t1))))))(deftest ^:parallel joined-dependencies-test- (mt/with-temp [:model/Transform {t1 :id} (make-transform- {:database (mt/id),- :type "query",- :query {:source-table (mt/id :orders),- :joins- [{:fields "all",- :strategy "left-join",- :alias "Products",- :condition- [:=- [:field- (mt/id :orders :product_id)- {:base-type "type/Integer"}]- [:field- (mt/id :products :id)- {:base-type "type/Integer",- :join-alias "Products"}]],- :source-table (mt/id :products)}]},- :parameters []})]- (is (= #{{:table (mt/id :orders)}- {:table (mt/id :products)}}- (transform-deps-for-db (t2/select-one :model/Transform :id t1))))))+ (mt/test-drivers (mt/normal-drivers-with-feature :left-join)+ (mt/with-temp [:model/Transform {t1 :id} (make-transform+ {:database (mt/id),+ :type "query",+ :query {:source-table (mt/id :orders),+ :joins+ [{:fields "all",+ :strategy "left-join",+ :alias "Products",+ :condition+ [:=+ [:field+ (mt/id :orders :product_id)+ {:base-type "type/Integer"}]+ [:field+ (mt/id :products :id)+ {:base-type "type/Integer",+ :join-alias "Products"}]],+ :source-table (mt/id :products)}]},+ :parameters []})]+ (is (= #{{:table (mt/id :orders)}+ {:table (mt/id :products)}}+ (transform-deps-for-db (t2/select-one :model/Transform :id t1)))))))(deftest card-dependencies-test(mt/test-drivers (mt/normal-driver-select {:+features [:transforms/table :left-join]})@@ -219,16 +224,15 @@(transform-deps-for-db (t2/select-one :model/Transform :id t1)))))))(deftest native-dependencies-test- (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)- (mt/with-temp [:model/Transform {t1 :id} (make-transform- {:database (mt/id),- :type :native- :native (qp.compile/compile+ (mt/with-temp [:model/Transform {t1 :id} (make-transform+ {:database (mt/id),+ :type :native+ :native (qp.compile/compile{:database (mt/id),- :type "query",- :query {:source-table (mt/id :orders)}})})]- (is (= #{{:table (mt/id :orders)}}- (transform-deps-for-db (t2/select-one :model/Transform :id t1)))))))+ :type "query",+ :query {:source-table (mt/id :orders)}})})]+ (is (= #{{:table (mt/id :orders)}}+ (transform-deps-for-db (t2/select-one :model/Transform :id t1))))))(deftest native-card-dependencies-test(mt/test-drivers (mt/normal-driver-select {:+features [:transforms/table@@ -290,79 +294,75 @@(ordering/find-cycle test-graph)))))(deftest get-transform-cycle-test- (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)- (testing "cycle is detected in transform referencing itself"- (mt/with-temp [:model/Table {table1 :id} {:schema "public"- :name "table_1"}- :model/Field _ {:table_id table1- :name "foo"}- :model/Transform {t1 :id} (make-transform- {:database (mt/id),- :type "query",- :query {:source-table table1}}- "table_1")]- (is (= {:cycle-str "transform_table_1"- :cycle [t1]}- (ordering/get-transform-cycle (t2/select-one :model/Transform :id t1))))))- (testing "cycle is caught in 2 transforms referencing each other"- (mt/with-temp [:model/Table {table1 :id} {:schema "public"- :name "table_1"}- :model/Field _ {:table_id table1- :name "foo"}- :model/Table {table2 :id} {:schema "public"- :name "table_2"}- :model/Field _ {:table_id table2- :name "foo"}- :model/Transform {t1 :id} (make-transform- {:database (mt/id),- :type "query",- :query {:source-table table1}}- "table_2")- :model/Transform {t2 :id} (make-transform- {:database (mt/id),- :type "query",- :query {:source-table table2}}- "table_1")]- (is (= {:cycle-str "transform_table_2 -> transform_table_1",- :cycle [t1 t2]}- (ordering/get-transform-cycle (t2/select-one :model/Transform :id t1))))))- (testing "cycle is detected in 3 transforms referencing each other"- (mt/with-temp [:model/Table {table1 :id} {:schema "public"- :name "table_1"}- :model/Field _ {:table_id table1- :name "foo"}- :model/Table {table2 :id} {:schema "public"- :name "table_2"}- :model/Field _ {:table_id table2- :name "foo"}- :model/Table {table3 :id} {:schema "public"- :name "table_3"}- :model/Field _ {:table_id table3- :name "foo"}- :model/Transform {t1 :id} (make-transform- {:database (mt/id),- :type "query",- :query {:source-table table1}}- "table_2")- :model/Transform {t2 :id} (make-transform- {:database (mt/id),- :type "query",- :query {:source-table table2}}- "table_3")- :model/Transform {t3 :id} (make-transform- {:database (mt/id),- :type "query",- :query {:source-table table3}}- "table_1")]- (is (= {:cycle-str "transform_table_2 -> transform_table_1 -> transform_table_3",- :cycle [t1 t3 t2]}- (ordering/get-transform-cycle (t2/select-one :model/Transform :id t1))))))))+ (testing "cycle is detected in transform referencing itself"+ (mt/with-temp [:model/Table {table1 :id} {:schema (default-schema-or-public)+ :name "table_1"}+ :model/Field _ {:table_id table1+ :name "foo"}+ :model/Transform {t1 :id} (make-transform+ {:database (mt/id),+ :type "query",+ :query {:source-table table1}}+ "table_1")]+ (is (= {:cycle-str "transform_table_1"+ :cycle [t1]}+ (ordering/get-transform-cycle (t2/select-one :model/Transform :id t1))))))+ (testing "cycle is caught in 2 transforms referencing each other"+ (mt/with-temp [:model/Table {table1 :id} {:schema (default-schema-or-public), :name "table_1"}+ :model/Field _ {:table_id table1+ :name "foo"}+ :model/Table {table2 :id} {:schema (default-schema-or-public), :name "table_2"}+ :model/Field _ {:table_id table2+ :name "foo"}+ :model/Transform {t1 :id} (make-transform+ {:database (mt/id),+ :type "query",+ :query {:source-table table1}}+ "table_2")+ :model/Transform {t2 :id} (make-transform+ {:database (mt/id),+ :type "query",+ :query {:source-table table2}}+ "table_1")]+ (is (= {:cycle-str "transform_table_2 -> transform_table_1",+ :cycle [t1 t2]}+ (ordering/get-transform-cycle (t2/select-one :model/Transform :id t1))))))+ (testing "cycle is detected in 3 transforms referencing each other"+ (mt/with-temp [:model/Table {table1 :id} {:schema (default-schema-or-public)+ :name "table_1"}+ :model/Field _ {:table_id table1+ :name "foo"}+ :model/Table {table2 :id} {:schema (default-schema-or-public)+ :name "table_2"}+ :model/Field _ {:table_id table2+ :name "foo"}+ :model/Table {table3 :id} {:schema (default-schema-or-public)+ :name "table_3"}+ :model/Field _ {:table_id table3+ :name "foo"}+ :model/Transform {t1 :id} (make-transform+ {:database (mt/id),+ :type "query",+ :query {:source-table table1}}+ "table_2")+ :model/Transform {t2 :id} (make-transform+ {:database (mt/id),+ :type "query",+ :query {:source-table table2}}+ "table_3")+ :model/Transform {t3 :id} (make-transform+ {:database (mt/id),+ :type "query",+ :query {:source-table table3}}+ "table_1")]+ (is (= {:cycle-str "transform_table_2 -> transform_table_1 -> transform_table_3",+ :cycle [t1 t3 t2]}+ (ordering/get-transform-cycle (t2/select-one :model/Transform :id t1)))))))(defn- make-python-transform"Create a python transform definition with the given source-tables and target name."[source-tables target-name & [target-schema]]- (let [default-schema "public"- schema (or target-schema default-schema)]+ (let [schema (or target-schema (default-schema-or-public (:engine (mt/db))))]{:source {:type "python":source-database (mt/id):source-tables source-tables@@ -375,48 +375,50 @@(deftest python-transform-table-ref-ordering-test(testing "Python transform with name-based source table ref resolves to producing transform"- (mt/with-temp [;; Transform A produces table "intermediate_output"- :model/Transform {t-a :id} (make-python-transform- {"input" (mt/id :orders)}- "intermediate_output")- ;; Transform B references intermediate_output by name (table doesn't exist yet)- :model/Transform {t-b :id} (make-python-transform- {"source" {:database_id (mt/id)- :schema "public"- :table "intermediate_output"}}- "final_output")]- (testing "table-dependencies returns table-ref for unresolved name reference"- (let [deps (transforms.i/table-dependencies (t2/select-one :model/Transform :id t-b))]- (is (contains? deps {:table-ref {:database_id (mt/id)- :schema "public"- :table "intermediate_output"}}))))+ (let [default-schema (default-schema-or-public)]+ (mt/with-temp [;; Transform A produces table "intermediate_output"+ :model/Transform {t-a :id} (make-python-transform+ {"input" (mt/id :orders)}+ "intermediate_output")+ ;; Transform B references intermediate_output by name (table doesn't exist yet)+ :model/Transform {t-b :id} (make-python-transform+ {"source" {:database_id (mt/id)+ :schema default-schema+ :table "intermediate_output"}}+ "final_output")]+ (testing "table-dependencies returns table-ref for unresolved name reference"+ (let [deps (transforms.i/table-dependencies (t2/select-one :model/Transform :id t-b))]+ (is (contains? deps {:table-ref {:database_id (mt/id)+ :schema default-schema+ :table "intermediate_output"}}))))- (testing "transform-ordering correctly resolves the dependency"- (is (= {t-a #{}- t-b #{t-a}}- (ordering/transform-ordering (t2/select :model/Transform :id [:in [t-a t-b]]))))))))+ (testing "transform-ordering correctly resolves the dependency"+ (is (= {t-a #{}+ t-b #{t-a}}+ (ordering/transform-ordering (t2/select :model/Transform :id [:in [t-a t-b]])))))))))(deftest python-transform-mixed-source-tables-test(testing "Python transform with mixed int and name-based refs"- (mt/with-temp [:model/Transform {t-a :id} (make-python-transform- {"input" (mt/id :orders)}- "output_a")- :model/Transform {t-b :id} (make-python-transform- {;; Direct table reference (existing table)- "existing" (mt/id :products)- ;; Name-based reference (table doesn't exist yet)- "from_transform" {:database_id (mt/id)- :schema "public"- :table "output_a"}}- "output_b")]- (testing "table-dependencies includes both types"- (let [deps (transforms.i/table-dependencies (t2/select-one :model/Transform :id t-b))]- (is (contains? deps {:table (mt/id :products)}))- (is (contains? deps {:table-ref {:database_id (mt/id)- :schema "public"- :table "output_a"}}))))+ (let [default-schema (default-schema-or-public)]+ (mt/with-temp [:model/Transform {t-a :id} (make-python-transform+ {"input" (mt/id :orders)}+ "output_a")+ :model/Transform {t-b :id} (make-python-transform+ {;; Direct table reference (existing table)+ "existing" (mt/id :products)+ ;; Name-based reference (table doesn't exist yet)+ "from_transform" {:database_id (mt/id)+ :schema default-schema+ :table "output_a"}}+ "output_b")]+ (testing "table-dependencies includes both types"+ (let [deps (transforms.i/table-dependencies (t2/select-one :model/Transform :id t-b))]+ (is (contains? deps {:table (mt/id :products)}))+ (is (contains? deps {:table-ref {:database_id (mt/id)+ :schema default-schema+ :table "output_a"}}))))- (testing "transform-ordering resolves both dependencies"- (is (= {t-a #{}- t-b #{t-a}}- (ordering/transform-ordering (t2/select :model/Transform :id [:in [t-a t-b]]))))))))+ (testing "transform-ordering resolves both dependencies"+ (is (= {t-a #{}+ t-b #{t-a}}+ (ordering/transform-ordering (t2/select :model/Transform :id [:in [t-a t-b]])))))))))diff --git a/enterprise/backend/test/metabase_enterprise/transforms/test_util.clj b/enterprise/backend/test/metabase_enterprise/transforms/test_util.cljindex 247d0353be3..6fde854b922 100644--- a/enterprise/backend/test/metabase_enterprise/transforms/test_util.clj+++ b/enterprise/backend/test/metabase_enterprise/transforms/test_util.clj@@ -4,9 +4,11 @@[clojure.test :refer :all][metabase.api.common :as api][metabase.driver :as driver]+ [metabase.driver.sql :as driver.sql][metabase.test :as mt][metabase.test.data.interface :as tx][metabase.util :as u]+ [metabase.util.log :as log][toucan2.core :as t2])(:import(java.time Instant LocalDateTime ZonedDateTime ZoneId)))@@ -15,21 +17,29 @@(defn drop-target!"Drop transform target `target` and clean up its metadata.- `target` can be a string or a map. If `target` is a string, type :table is assumed."+ `target` can be a string or a map. If `target` is a string, type :table is assumed.+ If no schema is provided, uses the driver's default schema."[target]- (let [target (if (map? target)- target- ;; assume this is just a plain table name- {:type :table, :name target})- driver driver/*driver*]+ (let [driver driver/*driver*+ target (cond-> (if (map? target)+ target+ ;; assume this is just a plain table name+ {:type :table, :name target})+ (and (nil? (:schema target)) (isa? driver/hierarchy driver/*driver* :sql))+ (assoc :schema (driver.sql/default-schema driver)))](binding [api/*is-superuser?* trueapi/*current-user-id* (mt/user->id :crowberto)];; Drop the actual table/view from the database- (-> (driver/drop-transform-target! driver (mt/db) target)- u/ignore-exceptions)+ (try+ (driver/drop-transform-target! driver (mt/db) target)+ (catch Exception e+ (log/warnf e "Failed to drop transform target table %s.%s for driver %s"+ (:schema target) (:name target) driver)));; Also clean up the Metabase metadata- (-> (t2/delete! :model/Table :name (:name target) :db_id (:id (mt/db)))- u/ignore-exceptions))))+ (try+ (t2/delete! :model/Table :name (:name target) :db_id (:id (mt/db)))+ (catch Exception e+ (log/warnf e "Failed to delete Table metadata for %s" (:name target)))))))(defn gen-table-name"Generate a random table name with prefix `table-name-prefix`."@@ -37,7 +47,8 @@(if (map? table-name-prefix);; table-name-prefix is a whole target, randomize the name(update table-name-prefix :name gen-table-name)- (let [table-name (str table-name-prefix \_ (str/replace (str (random-uuid)) \- \_))]+ ;; Strip off some of the randomness, so that less tests run afoul of the length limit.+ (let [table-name (str table-name-prefix \_ (subs (str/replace (str (random-uuid)) \- \_) 0 26))];; this caught me out when testing, was annoying to debug - hence assert(assert (< (count table-name) (driver/table-name-length-limit driver/*driver*))"chosen identifier prefix should not cause identifiers longer than the driver/table-name-length-limit")@@ -139,6 +150,14 @@[](t2/select-one-fn :schema :model/Table (mt/id :transforms_products)))+(defn default-schema-or-public+ "Returns the driver's default schema (e.g., 'dbo' for SQL Server) or 'public' as fallback.+ Useful for tests that need to create tables with a schema matching transform targets."+ []+ (or (when (get-method driver.sql/default-schema driver/*driver*)+ (driver.sql/default-schema driver/*driver*))+ "public"))+(defmulti delete-schema!"Deletes a schema."{:arglists '([driver db schema])}diff --git a/enterprise/backend/test/metabase_enterprise/transforms/util_test.clj b/enterprise/backend/test/metabase_enterprise/transforms/util_test.cljindex 242fe5da276..2baab485aca 100644--- a/enterprise/backend/test/metabase_enterprise/transforms/util_test.clj+++ b/enterprise/backend/test/metabase_enterprise/transforms/util_test.clj@@ -111,6 +111,47 @@;; Ignore cleanup errorsnil)))))))))+;;; ------------------------------------------------------------+;;; Filter xf tests+;;; ------------------------------------------------------------++(deftest ^:parallel matching-timestamp?-test+ (testing "matching-timestamp? checks if a timestamp falls within a date range [start, end)"+ (let [matching-timestamp? #'transforms.util/matching-timestamp?+ field-path [:start_time]+ range-jan-feb {:start "2024-01-01T00:00:00Z" :end "2024-02-01T00:00:00Z"}+ range-start-only {:start "2024-01-01T00:00:00Z" :end nil}+ range-end-only {:start nil :end "2024-02-01T00:00:00Z"}]++ (testing "with both start and end bounds"+ (are [expected timestamp]+ (= expected (matching-timestamp? {:start_time timestamp} field-path range-jan-feb))+ nil nil ; missing field returns nil+ true "2024-01-15T12:00:00Z" ; timestamp in middle of range+ false "2023-12-15T12:00:00Z" ; timestamp before range+ false "2024-02-15T12:00:00Z" ; timestamp after range+ true "2024-01-01T00:00:00Z" ; start boundary is inclusive+ true "2024-02-01T00:00:00Z")) ; end boundary is inclusive too 🤷++ (testing "with only start bound"+ (are [expected timestamp]+ (= expected (matching-timestamp? {:start_time timestamp} field-path range-start-only))+ true "2024-01-15T12:00:00Z" ; timestamp after start+ true "2024-02-15T12:00:00Z" ; any timestamp after start+ false "2023-12-15T12:00:00Z")) ; timestamp before start++ (testing "with only end bound"+ (are [expected timestamp]+ (= expected (matching-timestamp? {:start_time timestamp} field-path range-end-only))+ true "2024-01-15T12:00:00Z" ; timestamp before end+ true "2023-12-15T12:00:00Z" ; any timestamp before end+ false "2024-02-15T12:00:00Z")) ; timestamp after end++ (testing "returns nil when field value is missing"+ (are [job] (nil? (matching-timestamp? job field-path range-jan-feb))+ {}+ {:other "value"})))))+;;; ------------------------------------------------- Source Table Resolution Tests ----------------------------------(deftest batch-lookup-table-ids-testdiff --git a/enterprise/backend/test/metabase_enterprise/transforms_python/ordering_test.clj b/enterprise/backend/test/metabase_enterprise/transforms_python/ordering_test.cljindex 2e1d86396b5..6c8da98f71e 100644--- a/enterprise/backend/test/metabase_enterprise/transforms_python/ordering_test.clj+++ b/enterprise/backend/test/metabase_enterprise/transforms_python/ordering_test.clj@@ -3,16 +3,13 @@[clojure.test :refer :all][metabase-enterprise.transforms.interface :as transforms.i][metabase-enterprise.transforms.ordering :as ordering]- [metabase.driver :as driver]- [metabase.driver.sql :as driver.sql]+ [metabase-enterprise.transforms.test-util :as transforms.tu][metabase.test :as mt][toucan2.core :as t2]))(defn- make-transform [query & [name schema]](let [name (or name (mt/random-name))- default-schema (when (get-method driver.sql/default-schema driver/*driver*)- (driver.sql/default-schema driver/*driver*))- schema (or schema default-schema "public")]+ schema (or schema (transforms.tu/default-schema-or-public))]{:source {:type :query:query query}:name (str "transform_" name)@@ -22,9 +19,7 @@(defn- make-python-transform [source-tables & [name schema target-db]](let [name (or name (mt/random-name))- default-schema (when (get-method driver.sql/default-schema driver/*driver*)- (driver.sql/default-schema driver/*driver*))- schema (or schema default-schema "public")+ schema (or schema (transforms.tu/default-schema-or-public))target-db (or target-db (mt/id))]{:source {:type "python":source-tables source-tables@@ -51,62 +46,65 @@(deftest python-transform-ordering-test(mt/test-drivers (mt/normal-drivers-with-feature :transforms/python)(testing "Python transforms are ordered correctly based on source-tables dependencies"- (mt/with-temp [:model/Table {table1 :id} {:schema "public"- :name "output_1"}- :model/Field _ {:table_id table1- :name "foo"}- :model/Transform {t1 :id} (make-python-transform {"orders" (mt/id :orders)} "output_1")- :model/Transform {t2 :id} (make-python-transform {"output_1" table1} "output_2")]- (is (= {t1 #{}- t2 #{t1}}- (ordering/transform-ordering (t2/select :model/Transform :id [:in [t1 t2]]))))))))+ (let [schema (transforms.tu/default-schema-or-public)]+ (mt/with-temp [:model/Table {table1 :id} {:schema schema+ :name "output_1"}+ :model/Field _ {:table_id table1+ :name "foo"}+ :model/Transform {t1 :id} (make-python-transform {"orders" (mt/id :orders)} "output_1")+ :model/Transform {t2 :id} (make-python-transform {"output_1" table1} "output_2")]+ (is (= {t1 #{}+ t2 #{t1}}+ (ordering/transform-ordering (t2/select :model/Transform :id [:in [t1 t2]])))))))))(deftest python-transform-multiple-dependencies-test(mt/test-drivers (mt/normal-drivers-with-feature :transforms/python)(testing "Python transforms with multiple table dependencies"- (mt/with-temp [:model/Table {table1 :id} {:schema "public"- :name "output_1"}- :model/Field _ {:table_id table1- :name "foo"}- :model/Table {table2 :id} {:schema "public"- :name "output_2"}- :model/Field _ {:table_id table2- :name "bar"}- :model/Transform {t1 :id} (make-python-transform {"orders" (mt/id :orders)} "output_1")- :model/Transform {t2 :id} (make-python-transform {"products" (mt/id :products)} "output_2")- :model/Transform {t3 :id} (make-python-transform {"output_1" table1- "output_2" table2} "final_output")]- (is (= {t1 #{}- t2 #{}- t3 #{t1 t2}}- (ordering/transform-ordering (t2/select :model/Transform :id [:in [t1 t2 t3]]))))))))+ (let [schema (transforms.tu/default-schema-or-public)]+ (mt/with-temp [:model/Table {table1 :id} {:schema schema+ :name "output_1"}+ :model/Field _ {:table_id table1+ :name "foo"}+ :model/Table {table2 :id} {:schema schema+ :name "output_2"}+ :model/Field _ {:table_id table2+ :name "bar"}+ :model/Transform {t1 :id} (make-python-transform {"orders" (mt/id :orders)} "output_1")+ :model/Transform {t2 :id} (make-python-transform {"products" (mt/id :products)} "output_2")+ :model/Transform {t3 :id} (make-python-transform {"output_1" table1+ "output_2" table2} "final_output")]+ (is (= {t1 #{}+ t2 #{}+ t3 #{t1 t2}}+ (ordering/transform-ordering (t2/select :model/Transform :id [:in [t1 t2 t3]])))))))))(deftest mixed-transform-ordering-test(mt/test-drivers (mt/normal-drivers-with-feature :transforms/python :transforms/table)(testing "Python and query transforms are ordered together correctly"- (mt/with-temp [:model/Table {table1 :id} {:schema "public"- :name "sql_output"}- :model/Field _ {:table_id table1- :name "foo"}- :model/Table {table2 :id} {:schema "public"- :name "python_output"}- :model/Field _ {:table_id table2- :name "bar"}- ;; SQL transform that depends on orders table- :model/Transform {t1 :id} (make-transform- {:database (mt/id),- :type "query",- :query {:source-table (mt/id :orders)}}- "sql_output")- ;; Python transform that depends on the SQL transform's output- :model/Transform {t2 :id} (make-python-transform {"sql_output" table1} "python_output")- ;; Another SQL transform that depends on the Python transform's output- :model/Transform {t3 :id} (make-transform- {:database (mt/id)- :type "query"- :query {:source-table table2}}- "final_output")]- (is (= {t1 #{}- t2 #{t1}- t3 #{t2}}- (ordering/transform-ordering (t2/select :model/Transform :id [:in [t1 t2 t3]]))))))))+ (let [schema (transforms.tu/default-schema-or-public)]+ (mt/with-temp [:model/Table {table1 :id} {:schema schema+ :name "sql_output"}+ :model/Field _ {:table_id table1+ :name "foo"}+ :model/Table {table2 :id} {:schema schema+ :name "python_output"}+ :model/Field _ {:table_id table2+ :name "bar"}+ ;; SQL transform that depends on orders table+ :model/Transform {t1 :id} (make-transform+ {:database (mt/id),+ :type "query",+ :query {:source-table (mt/id :orders)}}+ "sql_output")+ ;; Python transform that depends on the SQL transform's output+ :model/Transform {t2 :id} (make-python-transform {"sql_output" table1} "python_output")+ ;; Another SQL transform that depends on the Python transform's output+ :model/Transform {t3 :id} (make-transform+ {:database (mt/id)+ :type "query"+ :query {:source-table table2}}+ "final_output")]+ (is (= {t1 #{}+ t2 #{t1}+ t3 #{t2}}+ (ordering/transform-ordering (t2/select :model/Transform :id [:in [t1 t2 t3]])))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/api_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/api_test.cljnew file mode 100644index 00000000000..7c0daa9660c--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/api_test.clj@@ -0,0 +1,1964 @@+(ns ^:mb/driver-tests metabase-enterprise.workspaces.api-test+ (:require+ [clojure.string :as str]+ [clojure.test :refer :all]+ [java-time.api :as t]+ [medley.core :as m]+ [metabase-enterprise.transforms.api :as transforms.api]+ [metabase-enterprise.transforms.execute :as transforms.execute]+ [metabase-enterprise.transforms.test-dataset :as transforms-dataset]+ [metabase-enterprise.transforms.test-util :as transforms.tu :refer [with-transform-cleanup!]]+ [metabase-enterprise.workspaces.common :as ws.common]+ [metabase-enterprise.workspaces.impl :as ws.impl]+ [metabase-enterprise.workspaces.isolation :as ws.isolation]+ [metabase-enterprise.workspaces.test-util :as ws.tu]+ [metabase-enterprise.workspaces.util :as ws.u]+ [metabase.audit-app.core :as audit]+ [metabase.driver :as driver]+ [metabase.driver.sql :as driver.sql]+ [metabase.driver.sql.normalize :as sql.normalize]+ [metabase.driver.sql.util :as sql.u]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.query-processor.compile :as qp.compile]+ [metabase.test :as mt]+ [metabase.util :as u]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(ws.tu/ws-fixtures!)++(use-fixtures :once (fn [thunk] (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table) (thunk))))++(def ws-url+ "Alias for ws.tu/ws-url for convenience."+ ws.tu/ws-url)++(def ^:private ->native+ "It's convenient to construct queries using MBQL helper, but only native queries can be used in workspaces."+ (comp mt/native-query ws.tu/mbql->native))++;;; Authorization tests for all workspace routes are in service-user-authorization-test at the bottom of this file.++(deftest workspace-crud-flow-test+ (let [workspace-name (str "Workspace " (random-uuid))+ created (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name workspace-name+ :database_id (mt/id)})+ workspace-id (:id created)+ collection-id (:collection_id created)]+ (is (=? {:id int?+ :collection_id int?+ :name workspace-name}+ created))+ (testing "the collection exists, and we have a back reference"+ (is (t2/exists? :model/Workspace :id workspace-id :collection_id collection-id))+ (is (t2/exists? :model/Collection :id collection-id :workspace_id workspace-id)))++ (testing "workspace appears in list response"+ (let [{:keys [items]} (mt/user-http-request :crowberto :get 200 "ee/workspace")]+ (is (some #(= workspace-id (:id %)) items))))++ (testing "workspace can be fetched individually"+ (is (=? {:id workspace-id}+ (mt/user-http-request :crowberto :get 200 (ws-url workspace-id)))))++ (testing "workspace can be archived"+ (let [updated (mt/user-http-request :crowberto :post 200 (ws-url workspace-id "/archive"))]+ (is (= "archived" (:status updated)))))++ (testing "workspace can be unarchived"+ (let [updated (mt/user-http-request :crowberto :post 200 (ws-url workspace-id "/unarchive"))]+ (is (= "uninitialized" (:status updated)))))++ (testing "workspace cannot be deleted if it is not archived"+ (let [message (mt/user-http-request :crowberto :delete 400 (ws-url workspace-id))]+ (is (= "You cannot delete a workspace without first archiving it" message))))++ (testing "workspace can be deleted if it is archived"+ (let [updated (mt/user-http-request :crowberto :post 200 (ws-url workspace-id "/archive"))]+ (is (= "archived" (:status updated))))+ (let [response (mt/user-http-request :crowberto :delete 200 (ws-url workspace-id))]+ (is (= {:ok true} response))+ ;; TODO (Chris 2026-02-02) -- would be good to check that the user, schema, and table metadata is gone too.+ (is (false? (t2/exists? :model/Workspace workspace-id)))))))++(deftest archive-workspace-calls-destroy-isolation-test+ (testing "POST /api/ee/workspace/:id/archive calls destroy-workspace-isolation!"+ (let [called? (atom false)+ workspace (ws.tu/create-ready-ws! "Archive Isolation Test")]+ (mt/with-dynamic-fn-redefs [ws.isolation/destroy-workspace-isolation!+ (fn [_database _workspace]+ (reset! called? true))]+ (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/archive"))+ (is @called? "destroy-workspace-isolation! should be called when archiving")))))++(deftest archive-workspace-succeeds-when-cleanup-fails-test+ (testing "POST /api/ee/workspace/:id/archive succeeds even when destroy-workspace-isolation! fails"+ (ws.tu/with-workspaces! [workspace {:name "Archive Cleanup Fail Test"}]+ (mt/with-dynamic-fn-redefs [ws.isolation/destroy-workspace-isolation!+ (fn [_database _workspace]+ (throw (ex-info "Simulated cleanup failure" {:test true})))]+ (is (some? (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/archive"))))+ (is (= :archived (t2/select-one-fn :base_status :model/Workspace :id (:id workspace)))+ "Workspace should have status :archived despite cleanup failure")))))++(deftest ^:synchronized delete-workspace-calls-destroy-isolation-test+ (testing "DELETE /api/ee/workspace/:id calls destroy-workspace-isolation!"+ (let [called? (atom false)+ workspace (ws.tu/create-ready-ws! "Delete Isolation Test")]+ (mt/with-dynamic-fn-redefs [ws.isolation/destroy-workspace-isolation!+ (fn [_database _workspace]+ (reset! called? true))]+ (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/archive"))+ (mt/user-http-request :crowberto :delete 200 (ws-url (:id workspace)))+ (is @called? "destroy-workspace-isolation! should be called when deleting")))))++(deftest ^:synchronized merge-workspace-calls-destroy-isolation-test+ (testing "POST /api/ee/workspace/:id/merge calls destroy-workspace-isolation!"+ (let [called? (atom false)+ workspace (ws.tu/create-ready-ws! "Merge Isolation Test")]+ (mt/with-dynamic-fn-redefs [ws.isolation/destroy-workspace-isolation!+ (fn [_database _workspace]+ (reset! called? true))]+ (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/merge"))+ (is @called? "destroy-workspace-isolation! should be called when merging")))))++(deftest unarchive-workspace-calls-ensure-isolation-test+ (testing "POST /api/ee/workspace/:id/unarchive calls ensure-database-isolation!"+ (let [called? (atom false)+ workspace (ws.tu/create-ready-ws! "Unarchive Isolation Test")]+ (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/archive"))+ (testing "ensure-database-isolation! should be called when unarchiving"+ (mt/with-dynamic-fn-redefs [ws.isolation/ensure-database-isolation!+ (fn [_workspace _database]+ (reset! called? true)+ {:schema "test_schema" :database_details {}})]+ (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/unarchive"))+ (is @called?))))))++(deftest unarchive-workspace-calls-sync-grant-accesses-test+ (testing "POST /api/ee/workspace/:id/unarchive calls sync-grant-accesses!"+ (let [called? (atom false)+ workspace (ws.tu/create-ready-ws! "Unarchive Grant Test")]+ (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/archive"))+ (mt/with-dynamic-fn-redefs [ws.impl/sync-grant-accesses!+ (fn [_workspace]+ (reset! called? true)+ nil)]+ (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/unarchive"))+ (is @called? "sync-grant-accesses! should be called when unarchiving")))))++(deftest archive-unarchive-access-granted-test+ (testing "Archive/unarchive properly manages access_granted flags and grants"+ (let [workspace (ws.tu/create-ready-ws! "Archive Grant Test")+ granted-tables (atom [])]+ (testing "Archive resets access_granted to false"+ (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/archive"))+ (ws.tu/analyze-workspace! (:id workspace))+ (is (false? (t2/select-one-fn :access_granted :model/WorkspaceInput :workspace_id (:id workspace)))))++ (testing "Unarchive re-grants access and sets access_granted to true"+ (mt/with-dynamic-fn-redefs [ws.isolation/grant-read-access-to-tables!+ (fn [_database _workspace tables]+ (reset! granted-tables tables))]+ (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/unarchive"))+ (ws.tu/analyze-workspace! (:id workspace))+ (is (=? [{:schema string? :name "test_table_1"}] @granted-tables)+ "grant-read-access-to-tables! should be called with the input tables")+ (is (true? (t2/select-one-fn :access_granted :model/WorkspaceInput :workspace_id (:id workspace)))+ "access_granted should be true after unarchive"))))))++(deftest merge-workspace-test+ (testing "Cannot merge an already archived workspace"+ (ws.tu/with-workspaces! [workspace {:name "Archived Workspace"}]+ (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "/archive"))+ (is (= "Cannot merge an archived workspace"+ (mt/user-http-request :crowberto :post 400 (ws-url (:id workspace) "/merge")))))))++(deftest merge-workspace-with-transform-test+ (testing "POST /api/ee/workspace/:id/merge promotes transforms and archives workspace"+ (mt/with-temp [:model/Table _table {:schema "public" :name "merge_test_table"}+ :model/Transform x1 {:name "Upstream Transform"+ :description "Original description"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "merge_test_table"}}]+ (let [{ws-id :id ws-name :name} (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name (mt/random-name)+ :database_id (mt/id)})+ {ws-tx-ref-id :ref_id} (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ (merge {:global_id (:id x1)}+ (select-keys x1 [:name :description :source :target])))+ commit-msg "Test batch merge commit"]+ (testing "We've got our workspace with transform to merge"+ (is (int? ws-id))+ ;; We could make an API call here instead :shrug:+ (t2/update! :model/WorkspaceTransform {:workspace_id ws-id :ref_id ws-tx-ref-id} {:description "Modified in workspace"}))+ (testing "returns merged transforms"+ (is (=? {:merged {:transforms [{:global_id (:id x1)}]}+ :errors []+ :workspace {:id ws-id :name ws-name}}+ (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/merge")+ {:commit-message commit-msg}))))+ (testing "workspace was archived after successful merge"+ (is (= :archived (t2/select-one-fn :base_status :model/Workspace :id ws-id))))+ (testing "merge history was created"+ (is (=? {:commit_message commit-msg+ :creator_id (mt/user->id :crowberto)}+ (t2/select-one :model/WorkspaceMerge :workspace_name ws-name)))+ (is (=? {:transform_id (:id x1)+ :commit_message commit-msg}+ (t2/select-one :model/WorkspaceMergeTransform+ :transform_id (:id x1)))))))))++(deftest merge-sets-transform-creator-from-workspace-transform-test+ (testing "POST /api/ee/workspace/:id/merge sets transform creator_id from workspace_transform creator"+ (mt/with-temp [:model/User user-a {:first_name "UserA" :last_name "Test" :email "[email protected]" :is_superuser true}+ :model/User user-b {:first_name "UserB" :last_name "Test" :email "[email protected]" :is_superuser true}+ :model/User user-c {:first_name "UserC" :last_name "Test" :email "[email protected]" :is_superuser true}]+ ;; mt/with-temp with users will trigger their cleanup and those tables fail with their fks to core_user+ (mt/with-model-cleanup [:model/WorkspaceMerge :model/ApiKey]+ ;; User A creates the workspace+ (let [{ws-id :id ws-name :name} (mt/user-http-request user-a :post 200 "ee/workspace"+ {:name (mt/random-name)+ :database_id (mt/id)})+ ;; User B creates a new workspace transform (not a checkout of existing)+ {ws-tx-ref-id :ref_id} (mt/user-http-request user-b :post 200 (ws-url ws-id "/transform")+ {:name "Transform created by User B"+ :source {:type :query+ :database (mt/id)+ :query (mt/native-query {:query "SELECT count(*) from orders"})}+ :target {:type "table"+ :schema "public"+ :name "creator_test_table"}})]++ (testing "workspace transform has User B as creator"+ (is (= (:id user-b)+ (t2/select-one-fn :creator_id :model/WorkspaceTransform :workspace_id ws-id :ref_id ws-tx-ref-id))))++ ;; NOTE: it's user-c merging the workspace+ (let [res (mt/user-http-request user-c :post 200 (ws-url ws-id "/merge")+ {:commit-message "Test creator attribution"})+ new-transform-id (-> res :merged :transforms first :global_id)]++ (testing "merge succeeded"+ (is (empty? (:errors res)))+ (is (some? new-transform-id)))++ (testing "newly created Transform has User B (ws transform creator) as creator, not User C (merger)"+ (is (= (:id user-b)+ (t2/select-one-fn :creator_id :model/Transform :id new-transform-id))))++ (testing "merge history still records User C as the merging user"+ (is (=? {:creator_id (:id user-c)}+ (t2/select-one :model/WorkspaceMerge :workspace_name ws-name))))))))))++(deftest merge-workspace-transaction-failure-test+ (testing "transactions"+ (mt/with-temp [:model/Table _table {:schema "public" :name "merge_test_table"}+ :model/Table _table {:schema "public" :name "merge_test_table_2"}+ :model/Transform x1 {:name "Upstream Transform 1"+ :description "Original description 2"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "merge_test_table"}}+ :model/Transform x2 {:name "Upstream Transform 2"+ :description "Original description 2"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "merge_test_table_2"}}]+ (let [;; Create a workspace+ {ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name "Merge test"+ :database_id (mt/id)})+ ;; Add 2 transforms+ {ws-x-1-id :ref_id}+ (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ (merge {:global_id (:id x1)}+ (select-keys x1 [:name :description :source :target])))+ {ws-x-2-id :ref_id}+ (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ (merge {:global_id (:id x2)}+ (select-keys x2 [:name :description :source :target])))+ ;; Ensure deterministic merge order by setting x2's created_at later than x1's.+ x1-created-at (t2/select-one-fn :created_at :model/WorkspaceTransform+ :workspace_id ws-id :ref_id ws-x-1-id)+ _ (t2/update! (t2/table-name :model/WorkspaceTransform) {:workspace_id ws-id :ref_id ws-x-2-id}+ {:created_at (t/plus x1-created-at (t/hours 1))})++ ;; Update transform names+ {ws-x-1-id :ref_id :as ws-x-1}+ (mt/user-http-request :crowberto :put 200+ (ws-url ws-id "/transform" ws-x-1-id)+ {:name "UPDATED 1"})+ {ws-x-2-id :ref_id :as ws-x-2}+ (mt/user-http-request :crowberto :put 200+ (ws-url ws-id "/transform" ws-x-2-id)+ {:name "UPDATED 2"})]++ (testing "Base: Workspace transforms are updated"+ (testing "X1"+ (is (= "UPDATED 1"+ (t2/select-one-fn :name [:model/WorkspaceTransform :name] :workspace_id ws-id :ref_id ws-x-1-id))))+ (testing "X2"+ (is (= "UPDATED 2"+ (t2/select-one-fn :name [:model/WorkspaceTransform :name] :workspace_id ws-id :ref_id ws-x-2-id)))))++ (testing "No updates are propagated back to core app on merge failure"+ (let [update-transform! transforms.api/update-transform!]+ (with-redefs [transforms.api/update-transform! (let [call-count (atom 0)]+ (fn [& args]+ (when (> @call-count 0)+ (throw (Exception. "boom")))+ (swap! call-count inc)+ (apply update-transform! args)))]+ (testing "API response: empty merged, single error"+ (let [resp (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/merge"))]+ (is (empty? (get-in resp [:merged :transforms])))+ (is (= 1 (count (:errors resp))))+ (is (= {:op "update"+ :global_id (:id x2)+ :ref_id ws-x-2-id+ :message "boom"}+ (first (:errors resp))))))+ (testing "Core transforms are left unchanged"+ (is (= (:name x1)+ (t2/select-one-fn :name :model/Transform (:id x1))))+ (is (= (:name x2)+ (t2/select-one-fn :name :model/Transform (:id x2)))))+ (testing "Workspace transforms are left unchanged"+ (is (=? (:name ws-x-1)+ (:name (t2/select-one :model/WorkspaceTransform+ :workspace_id ws-id :ref_id (:ref_id ws-x-1)))))+ (is (=? (:name ws-x-2)+ (:name (t2/select-one :model/WorkspaceTransform+ :workspace_id ws-id :ref_id (:ref_id ws-x-2)))))))))))))++(deftest merge-workspace-update-core-test+ (testing "transactions"+ (mt/with-temp [:model/Table _table {:schema "public" :name "merge_test_table"}+ :model/Table _table {:schema "public" :name "merge_test_table_2"}+ :model/Transform x1 {:name "Upstream Transform 1"+ :description "Original description 2"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "merge_test_table"}}+ :model/Transform x2 {:name "Upstream Transform 2"+ :description "Original description 2"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "merge_test_table_2"}}]+ (let [;; Create a workspace+ {ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name "Merge test"+ :database_id (mt/id)})+ ;; Add 2 transforms+ {ws-x-1-id :ref_id}+ (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ (merge {:global_id (:id x1)}+ (select-keys x1 [:name :description :source :target])))+ {ws-x-2-id :ref_id}+ (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ (merge {:global_id (:id x2)}+ (select-keys x2 [:name :description :source :target])))++ ;; Update transform names+ {ws-x-1-id :ref_id :as ws-x-1}+ (mt/user-http-request :crowberto :put 200+ (ws-url ws-id "/transform" ws-x-1-id)+ {:name "UPDATED 1"})+ {ws-x-2-id :ref_id :as ws-x-2}+ (mt/user-http-request :crowberto :put 200+ (ws-url ws-id "/transform" ws-x-2-id)+ {:name "UPDATED 2"})]++ (testing "Global transforms are updated"+ (testing "API response: empty errors, all updates present in merge"+ (let [resp (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/merge"))]+ (is (= 2 (count (get-in resp [:merged :transforms]))))+ (is (= 0 (count (:errors resp))))+ (is (some? (m/find-first #{{:op "update"+ :global_id (:id x1)+ :ref_id ws-x-1-id}}+ (get-in resp [:merged :transforms]))))+ (is (some? (m/find-first #{{:op "update"+ :global_id (:id x2)+ :ref_id ws-x-2-id}}+ (get-in resp [:merged :transforms]))))))+ (testing "Core transforms names are updated"+ (is (= (:name ws-x-1)+ (t2/select-one-fn :name :model/Transform (:id x1))))+ (is (= (:name ws-x-2)+ (t2/select-one-fn :name :model/Transform (:id x2)))))+ (testing "Workspace transforms are not deleted"+ (is (= 2 (t2/count :model/WorkspaceTransform+ :workspace_id ws-id+ :ref_id [:in (map :ref_id [ws-x-1 ws-x-2])])))))+ (testing "Workspace has been archived"+ (is (= :archived (t2/select-one-fn :base_status :model/Workspace :id ws-id))))))))++(deftest merge-empty-workspace-test+ (let [{ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name "Merge test"+ :database_id (mt/id)})]++ (testing "API response: empty errors, empty updates"+ (let [resp (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/merge"))]+ (is (=? {:errors []+ :merged {:transforms []}}+ resp))))+ (testing "Workspace has been archived"+ (is (= :archived (t2/select-one-fn :base_status :model/Workspace :id ws-id))))))++(deftest merge-transfom-test+ (mt/with-temp [:model/Table _table {:schema "public" :name "merge_test_table"}+ :model/Table _table {:schema "public" :name "merge_test_table_2"}+ :model/Transform x1 {:name "Upstream Transform 1"+ :description "Original description 2"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "merge_test_table"}}+ :model/Transform x2 {:name "Upstream Transform 2"+ :description "Original description 2"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "merge_test_table_2"}}]+ (let [;; Create a workspace+ {ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name "Merge test"+ :database_id (mt/id)})+ ;; Add 2 transforms+ {ws-x-1-id :ref_id}+ (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ (merge {:global_id (:id x1)}+ (select-keys x1 [:name :description :source :target])))+ {ws-x-2-id :ref_id}+ (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ (merge {:global_id (:id x2)}+ (select-keys x2 [:name :description :source :target])))++ ;; Update transform names+ {ws-x-1-id :ref_id :as ws-x-1}+ (mt/user-http-request :crowberto :put 200+ (ws-url ws-id "/transform" ws-x-1-id)+ {:name "UPDATED 1"})+ {ws-x-2-id :ref_id :as ws-x-2}+ (mt/user-http-request :crowberto :put 200+ (ws-url ws-id "/transform" ws-x-2-id)+ {:name "UPDATED 2"})]+ (testing "Merging first of 2 workspace transfroms"+ (let [commit-msg "Single transform merge 1"+ resp (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform" ws-x-1-id "/merge")+ {:commit-message commit-msg})+ remaining (t2/select :model/WorkspaceTransform :workspace_id ws-id)]+ (testing "Response"+ (is (empty? (:errors resp)))+ (is (= {:op "update"+ :global_id (:id x1)+ :ref_id ws-x-1-id}+ resp)))+ (testing "Workspace transforms are left untouched"+ (is (= 2 (count remaining)))+ (is (=? (into #{} (map :name [ws-x-1 ws-x-2]))+ (into #{} (map :name remaining)))))+ (testing "Propagation back to core"+ (is (= (:name ws-x-1)+ (t2/select-one-fn :name :model/Transform :id (:global_id resp)))))+ (testing "merge history was created for single transform merge"+ (let [merge-transform (t2/select-one :model/WorkspaceMergeTransform+ :transform_id (:id x1))]+ (is (=? {:workspace_merge_id pos-int?+ :transform_id (:id x1)+ :commit_message commit-msg}+ merge-transform))+ (testing "workspace_merge record was also created"+ (is (=? {:commit_message commit-msg+ :creator_id (mt/user->id :crowberto)}+ (t2/select-one :model/WorkspaceMerge+ :id (:workspace_merge_id merge-transform)))))))))+ (testing "Merging last workspace transfrom"+ (let [resp (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform" ws-x-2-id "/merge"))+ remaining (t2/select :model/WorkspaceTransform :workspace_id ws-id)]+ (testing "Response"+ (is (empty? (:errors resp)))+ (is (= {:op "update"+ :global_id (:id x2)+ :ref_id ws-x-2-id}+ resp)))+ (testing "Workspace transforms still remain"+ (is (= 2 (count remaining))))+ (testing "Propagation back to core"+ (is (= (:name ws-x-2)+ (t2/select-one-fn :name :model/Transform :id (:global_id resp)))))+ (testing "Workspace is not archived nor deleted"+ (let [ws-after (t2/select-one :model/Workspace :id ws-id)]+ (is (some? ws-after))+ (is (not= :archived (:status ws-after))))))))))++(deftest merge-history-endpoint-test+ (testing "GET /api/ee/transform/:id/merge-history"+ (mt/with-temp [:model/Table _table {:schema "public" :name "merge_history_test_table"}+ :model/Transform x1 {:name "Transform for history"+ :description "Test transform"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "merge_history_test_table"}}]+ (let [{ws-id :id ws-name :name} (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name (mt/random-name)+ :database_id (mt/id)})+ {ws-tx-ref-id :ref_id} (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ (merge {:global_id (:id x1)}+ (select-keys x1 [:name :description :source :target])))+ commit-msg "Test merge for history endpoint"]+ ;; Modify and merge the transform+ (t2/update! :model/WorkspaceTransform {:workspace_id ws-id :ref_id ws-tx-ref-id} {:description "Modified for history test"})+ (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/merge")+ {:commit-message commit-msg})++ (testing "returns merge history for a transform"+ ;; workspace_id is preserved since workspace is archived (not deleted) after merge+ (is (=? [{:id pos-int?+ :workspace_merge_id pos-int?+ :commit_message commit-msg+ :workspace_id ws-id+ :workspace_name ws-name+ :merging_user_id (mt/user->id :crowberto)+ :created_at some?}]+ (mt/user-http-request :crowberto :get 200+ (str "ee/transform/" (:id x1) "/merge-history")))))++ (testing "returns 404 for non-existent transform"+ (is (= "Not found."+ (mt/user-http-request :crowberto :get 404+ "ee/transform/999999/merge-history"))))++ (testing "requires superuser"+ (is (= "You don't have permissions to do that."+ (mt/user-http-request :rasta :get 403+ (str "ee/transform/" (:id x1) "/merge-history")))))))))++(deftest merge-single-transform-failure-test+ (mt/with-temp [:model/Table _table {:schema "public" :name "merge_test_table"}+ :model/Transform x1 {:name "Upstream Transform 1"+ :description "Original description 2"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "merge_test_table"}}]+ (let [;; Create a workspace+ {ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name "Merge test"+ :database_id (mt/id)})+ ;; Add transform+ {ws-x-1-id :ref_id :as ws-x-1}+ (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ (merge {:global_id (:id x1)}+ (select-keys x1 [:name :description :source :target])))++ _ (mt/user-http-request :crowberto :post 204+ (ws-url ws-id "/transform" ws-x-1-id "/archive"))]+ (testing "Failure on merge"+ (with-redefs [transforms.api/delete-transform! (fn [& _args]+ (throw (Exception. "boom")))]+ (let [resp (mt/user-http-request :crowberto :post 500+ (ws-url ws-id "/transform" ws-x-1-id "/merge"))]+ (testing "Response"+ (is (=? {:op "delete"+ :global_id (:id x1)+ :ref_id ws-x-1-id+ :message "Failed to merge transform."}+ resp)))+ (testing "no changes"+ (let [ws-xs (t2/select :model/WorkspaceTransform :workspace_id ws-id)]+ (is (= 1 (count ws-xs)))+ (is (= (:name ws-x-1)+ (:name (first ws-xs))))))))))))++(deftest merging-multiple-transforms-incl-ws-only-test+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Table _table {:schema "public" :name "merge_test_table"}+ :model/Table _table {:schema "public" :name "merge_test_table_2"}+ :model/Table _table {:schema "public" :name "merge_test_table_3"}+ :model/Transform x1 {:name "Upstream Transform 1"+ :description "Original description 2"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "merge_test_table"}}+ :model/Transform x2 {:name "Upstream Transform 2"+ :description "Original description 2"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "merge_test_table_2"}}]+ (let [;; Create a workspace+ {ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name "Merge test"+ :database_id (mt/id)})+ ;; Add 2 transforms+ {ws-x-1-id :ref_id :as ws-x-1}+ (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ (merge {:global_id (:id x1)}+ (select-keys x1 [:name :description :source :target])))+ {ws-x-2-id :ref_id}+ (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ (merge {:global_id (:id x2)}+ (select-keys x2 [:name :description :source :target])))+ ;; Update first+ _ (mt/user-http-request :crowberto :put 200+ (ws-url ws-id "/transform" ws-x-1-id)+ {:name "UPDATED 1"})+ ;; Archive second+ _ (mt/user-http-request :crowberto :post 204+ (ws-url ws-id "/transform" ws-x-2-id "/archive"))+ ;; And add _workspace only transform_+ {ws-x-3-id :ref_id :as ws-x-3}+ (mt/user-http-request :crowberto :post 200+ (ws-url ws-id "/transform")+ {:name "WS only transform"+ :description "this is ws only x"+ :source {:type :query+ :database (mt/id)+ :query (lib/native-query mp "select 1")}+ :target {:type "table"+ :schema "public"+ :name "merge_test_table_3"}})]+ (testing "Merge all 3"+ (let [resp (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/merge"))+ new-global-id (:global_id (m/find-first (comp #{"create"} :op) (get-in resp [:merged :transforms])))]+ (testing "Response"+ (is (empty? (:errors resp)))+ (is (some? (m/find-first+ #{{:op "update"+ :global_id (:id x1)+ :ref_id ws-x-1-id}}+ (get-in resp [:merged :transforms]))))+ (is (some? (m/find-first+ #{{:op "delete"+ :global_id (:id x2)+ :ref_id ws-x-2-id}}+ (get-in resp [:merged :transforms]))))+ (is (some? (m/find-first+ #{{:op "create"+ :global_id new-global-id+ :ref_id ws-x-3-id}}+ (get-in resp [:merged :transforms])))))+ (testing "No transforms are deleted on merge"+ (is (= 3 (count (t2/select :model/WorkspaceTransform :workspace_id ws-id)))))+ (testing "Propagation back to core"+ (is (= "UPDATED 1"+ (t2/select-one-fn :name :model/Transform :id (:global_id ws-x-1))))+ (is (not (t2/exists? :model/Transform :id (:id x2))))+ (is (= (:name ws-x-3)+ (t2/select-one-fn :name :model/Transform :id new-global-id))))))))))++(deftest create-workspace-transform-archived-test+ (testing "Cannot create transform in archived workspace"+ (ws.tu/with-workspaces! [workspace {:name "Archived"}]+ (t2/update! :model/Workspace (:id workspace) {:base_status :archived})+ (is (= "Cannot create transforms in an archived workspace"+ (mt/user-http-request :crowberto :post 400 (ws-url (:id workspace) "/transform")+ {:name "Should Fail"+ :source {:type "query"+ :query (mt/mbql-query venues)}+ :target {:type "table"+ :name "should_fail"}}))))))++(deftest add-transforms-to-workspace-test+ (testing "Add transforms to workspace via POST /transform"+ (with-transform-cleanup! [orig-name "ws_add_transforms_test"]+ (mt/with-temp [:model/Transform {x1-id :id :as x1} {:name "Transform to Check Out"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name orig-name}}]+ (let [{ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name "Add Transforms Test"+ :database_id (mt/id)})]+ (is (int? ws-id))+ (testing "Can check out a global transform into workspace"+ (let [response (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ (merge {:global_id x1-id}+ (select-keys x1 [:name :description :source :target])))]+ (is (=? {:ref_id string?+ :global_id x1-id+ :name "Transform to Check Out"+ :target_stale true}+ response))+ (is (= response (mt/user-http-request :crowberto :get 200 (ws-url ws-id "/transform" (:ref_id response)))))))++ (testing "Can create a new provisional transform"+ (let [response (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ {:name "New Transform"+ :source {:type "query"+ :query (->native (mt/mbql-query venues))}+ :target {:type "table"+ :name "new_transform_output"}})]++ ;; Fetch the graph, to trigger analysis+ (is (=? {:ref_id string?+ :global_id nil?+ :name "New Transform"+ :target_stale true+ ;; TODO (Chris 2026-01-07) -- this is now only populated lazily - is this OK?+ #_#_:target_isolated {:type "table"+ :database pos-int?+ :schema string?+ :name string?}}+ response))+ (is (= response (mt/user-http-request :crowberto :get 200 (ws-url ws-id "transform" (:ref_id response)))))))++ (testing "Cannot add transforms to archived workspace"+ (t2/update! :model/Workspace ws-id {:base_status :archived})+ (is (= "Cannot create transforms in an archived workspace"+ (mt/user-http-request :crowberto :post 400 (ws-url ws-id "/transform")+ {:name "Should Fail"+ :source {:type "query"+ :query (->native (mt/mbql-query venues))}+ :target {:type "table"+ :name "should_fail"}})))))))))++(deftest create-workspace-transform-test+ (mt/dataset transforms-dataset/transforms-test+ (let [{ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name "Test Workspace"+ :database_id (mt/id)})]+ (with-transform-cleanup! [table-name "workspace_transform_test"]+ (is (=? {:ref_id string?+ :workspace_id ws-id+ ;:creator_id (mt/user->id :crowberto)+ :target {:database (mt/id)}}+ (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform")+ {:name "Workspace Transform"+ :source {:type "query"+ :query (->native (mt/mbql-query transforms_products))}+ :target {:type "table"+ :name table-name}})))+ (is (=? {:id ws-id, :status "ready"}+ (mt/user-http-request :crowberto :get 200 (ws-url ws-id))))+ (is (=? {:transforms [{:ref_id string?, :name "Workspace Transform", :source_type "native"}]}+ (mt/user-http-request :crowberto :get 200 (ws-url ws-id "/transform"))))))))++(deftest tables-endpoint-empty-ws-test+ (let [user-id (mt/user->id :crowberto)+ ws (mt/user-http-request :crowberto :post 200 "ee/workspace/"+ {:name "My test ws"+ :creator_id user-id+ :database_id (mt/id)})]+ (is (= {:inputs []+ :outputs []}+ (mt/user-http-request :crowberto :get 200 (ws-url (:id ws) "/table"))))))++(deftest tables-endpoint-transform-not-run-test+ (with-transform-cleanup! [orig-name "ws_tables_not_run_test"]+ (mt/with-temp [:model/Transform x1 {:name "My X1"+ :source {:type "query"+ :query (mt/native-query (ws.tu/mbql->native (mt/mbql-query orders {:limit 10})))}+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name orig-name}}]+ (let [{ws-id :id} (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name "Test Workspace"+ :database_id (mt/id)})+ ;; add the transform+ req (assoc (select-keys x1 [:name :description :source :target]) :global_id (:id x1))+ ref-id (:ref_id (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/transform") req))+ ;; get the tables+ tables-result (mt/user-http-request :crowberto :get 200 (ws-url ws-id "/table"))]+ (testing "/tables returns expected results"+ (is (=? {:inputs [{:db_id (mt/id), :schema (t2/select-one-fn :schema :model/Table (mt/id :orders)), :table "orders", :table_id int?}]+ :outputs [{:db_id (mt/id)+ :global {:schema "public", :table orig-name}+ :isolated {:transform_id ref-id}}]}+ tables-result)))))))++(deftest tables-endpoint-test+ (let [mp (mt/metadata-provider)+ ;; Following needed for driver specific sql.+ orders-meta (lib.metadata/table mp (mt/id :orders))+ sql-str (-> (lib/query mp orders-meta)+ (lib/limit 10)+ (qp.compile/compile)+ :query)+ query (lib/native-query mp sql-str)+ orig-schema (or (:schema orders-meta) (driver.sql/default-schema driver/*driver*))+ orig-name (:name orders-meta)+ target-schema (driver.sql/default-schema driver/*driver*)]+ (with-transform-cleanup! [target-name "ws_tables_endpoint_test"]+ (mt/with-temp [:model/Transform x1 {:name "My X1"+ :source {:type "query"+ :query query}+ :target {:type "table"+ :database (mt/id)+ :schema target-schema+ :name target-name}}]+ ;; create the global table+ (transforms.execute/execute! x1 {:run-method :manual})+ (ws.tu/with-workspaces! [workspace {:name "Test Workspace"+ :database_id (mt/id)}]+ (let [body {:name (:name x1)+ :source (:source x1)+ :target (:target x1)}+ ws-transform (ws.common/add-to-changeset! (mt/user->id :crowberto) workspace :transform (:id x1) body)+ workspace (ws.tu/ws-done! (:id workspace))+ ref-id (:ref_id ws-transform)+ orig-id (t2/select-one-fn :id [:model/Table :id]+ :db_id (:database_id workspace)+ :schema (-> ws-transform :target :schema)+ :name (-> ws-transform :target :name))]+ (testing "/table returns expected results"+ ;; Schema is normalized to driver's default schema ("public" for Postgres) when stored+ (is (=? {:inputs [{:db_id (mt/id), :schema orig-schema, :table orig-name, :table_id int?}]+ :outputs+ [{:db_id (mt/id)+ :global {:schema target-schema+ :table target-name+ :table_id orig-id}+ :isolated {:transform_id ref-id+ #_#_:schema (:schema workspace)+ :table string?+ :table_id nil}}]}+ (mt/user-http-request :crowberto :get 200 (ws-url (:id workspace) "/table")))))+ (testing "and after we run the transform, id for isolated table appears"+ (is (=? {:status "succeeded"}+ (mt/user-http-request :crowberto :post 200 (ws-url (:id workspace) "transform" ref-id "run"))))+ (is (=? {:inputs [{:db_id (mt/id), :schema orig-schema, :table orig-name, :table_id int?}]+ :outputs+ [{:db_id (mt/id)+ :global {:schema target-schema+ :table target-name+ :table_id orig-id}+ :isolated {:transform_id ref-id+ :schema (:schema workspace)+ ;; maybe this is a bit too specific, but gives me a peace of mind for now+ :table (ws.u/isolated-table-name target-schema target-name)+ :table_id int?}}]}+ (mt/user-http-request :crowberto :get 200 (ws-url (:id workspace) "/table")))))))))))++;; TODO (Chris 2025-12-12) -- write a test for /table that covers the shadowing+;; e.g. have two transforms in a chain connecting 3 tables: (A -> X1 -> B -> X2 -> C)+;; raw-inputs: A (from X1) and B (from X2)+;; outputs: B (from X1) and C (from X2)+;; external-inputs: A (raw-inputs - outputs)++(deftest rename-workspace-test+ (testing "POST /api/ee/workspace/:id/name updates the workspace name"+ (ws.tu/with-workspaces! [workspace {:name "Original Name"}]+ (let [response (mt/user-http-request :crowberto :put 200 (ws-url (:id workspace))+ {:name "Updated Name"})]+ (is (= "Updated Name"+ (:name response)+ (t2/select-one-fn :name :model/Workspace :id (:id workspace)))))))++ (testing "Cannot rename an archived workspace"+ (ws.tu/with-workspaces! [workspace {:name "Archived"}]+ (t2/update! :model/Workspace (:id workspace) {:base_status :archived})+ (is (= "Cannot update an archived workspace"+ (mt/user-http-request :crowberto :put 400 (ws-url (:id workspace))+ {:name "Should Fail"}))))))++(deftest validate-target-test+ (let [table (t2/select-one :model/Table :db_id (mt/id) :active true {:where [:not [:like :schema "mb__%"]]})]+ (ws.tu/with-workspaces! [ws {:name "test" :database_id (:db_id table)}]+ (mt/with-temp [:model/WorkspaceTransform _x1 {:workspace_id (:id ws)+ :target {:database (:db_id table)+ :type "table"+ :schema (:schema table)+ :name (str "q_" (:name table))}}]+ (testing "Unique"+ (is (= "OK"+ (mt/with-log-level [metabase.driver.sql-jdbc.sync.describe-table :fatal]+ (mt/user-http-request :crowberto :post 200 (ws-url (:id ws) "/transform/validate/target")+ {:db_id (:db_id table)+ :target {:type "table"+ :schema "public"+ :name (str/replace (str (random-uuid)) "-" "_")}})))))+ ;; We've decided to defer this error until merge.+ ;; Also, this logic is going to become more relaxed, where we're allowed to take over a "dormant" table.+ #_(testing "Conflict outside of workspace"+ (is (= "A table with that name already exists."+ (mt/user-http-request :crowberto :post 403 (ws-url (:id ws) "/transform/validate/target")+ {:db_id (:db_id table)+ :target {:type "table"+ :schema (:schema table)+ :name (:name table)}}))))+ (testing "Must not target the isolated schema"+ (is (= "Must not target an isolated workspace schema"+ (mt/with-log-level [metabase.driver.sql-jdbc.sync.describe-table :fatal]+ (mt/user-http-request :crowberto :post 403 (ws-url (:id ws) "/transform/validate/target")+ {:db_id (:db_id table)+ :target {:type "table"+ ;; TODO (Chris 2026-01-05) -- the schema on the workspace is only set as part of adding tx+ :schema "mb__isolation_blah" #_(:schema ws)+ :name (str "q_" (:name table))}})))))++ (testing "Conflict inside of workspace"+ (is (= "Another transform in this workspace already targets that table"+ (mt/with-log-level [metabase.driver.sql-jdbc.sync.describe-table :fatal]+ (mt/user-http-request :crowberto :post 403 (ws-url (:id ws) "/transform/validate/target")+ {:db_id (:db_id table)+ :target {:type "table"+ :schema (:schema table)+ :name (str "q_" (:name table))}})))))))))++;;;; Async workspace creation tests++(deftest workspace-log-endpoint-test+ (testing "GET /api/ee/workspace/:id/log returns status and log entries"+ (let [{ws-id :id} (ws.tu/create-ready-ws! "Log tester")]+ (is (=? {:workspace_id ws-id+ :status "ready"+ :updated_at some?+ :last_completed_at some?+ :logs [{:task "database-isolation"+ :status "success"}+ {:task "workspace-setup"}]}+ (mt/user-http-request :crowberto :get 200 (ws-url ws-id "/log")))))))++(deftest workspace-log-endpoint-404-test+ (testing "GET /api/ee/workspace/:id/log returns 404 for non-existent workspace"+ (is (= "Not found."+ (mt/user-http-request :crowberto :get 404 "ee/workspace/999999/log")))))++(deftest workspace-log-entries-created-test+ (testing "WorkspaceLog entries are created during setup"+ (let [{ws-id :id} (ws.tu/create-ready-ws! "Log tester #2")]+ (is (=? [{:task :database-isolation+ :status :success}+ {:task :workspace-setup+ :status :success}]+ (t2/select :model/WorkspaceLog :workspace_id ws-id {:order-by [[:started_at :desc]]}))))))++(deftest workspace-setup-failure-logs-error-test+ (testing "Failed workspace setup logs error message"+ (mt/with-dynamic-fn-redefs [ws.isolation/ensure-database-isolation!+ (fn [& _] (throw (ex-info "Test isolation error" {})))]+ (let [{ws-id :id} (ws.tu/initialize-ws! "Log tester #3")]+ (is (=? [{:task :database-isolation+ :status :failure+ :message "Test isolation error"}+ {:task :workspace-setup+ :status :failure}]+ (t2/select :model/WorkspaceLog :workspace_id ws-id {:order-by [[:started_at :desc]]})))))))++;;; ---------------------------------------- Uninitialized Workspace Tests ----------------------------------------++(deftest uninitialized-workspace-lifecycle-test+ (testing "Workspace can be created without database_id (uses provisional default)"+ (let [{ws-id :id :as ws} (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name "uninitialized-lifecycle"})]+ ;; database_id is set to provisional default, but :status is uninitialized+ (is (=? {:status "uninitialized" :database_id pos-int?} ws))+ (testing "can be archived and deleted"+ (is (= "archived" (:status (mt/user-http-request :crowberto :post 200 (ws-url ws-id "/archive")))))+ (is (= {:ok true} (mt/user-http-request :crowberto :delete 200 (ws-url ws-id))))+ (is (not (t2/exists? :model/Workspace :id ws-id)))))))++(deftest ^:synchronized initialize-uninitialized-workspace-test+ (testing "via adding transform"+ (let [ws (mt/user-http-request :crowberto :post 200 "ee/workspace"+ {:name "init-via-transform"})]+ (is (= "uninitialized" (:status ws)))+ (let [transform (mt/user-http-request :crowberto :post 200 (ws-url (:id ws) "/transform")+ {:name "New Transform"+ :source {:type "query"+ :query (mt/native-query (ws.tu/mbql->native (mt/mbql-query venues)))}+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "init_transform_output"}})]+ (is (some? (:ref_id transform)))+ (let [ws (ws.tu/ws-done! ws)]+ (is (=? {:db_status :ready+ :database_id (mt/id)}+ ws)))+ (testing "PUT database_id fails on already initialized workspace"+ (is (= "Can only set database_id on uninitialized workspace"+ (mt/user-http-request :crowberto :put 400 (ws-url (:id ws))+ {:database_id (mt/id)}))))))))++;;; ---------------------------------------- Workspace Transform CRUD Tests ----------------------------------------++(deftest get-workspace-transforms-test+ (testing "GET /api/ee/workspace/:id/transform"+ (ws.tu/with-workspaces! [workspace {:name "List Transforms Test"}]+ (mt/with-temp [:model/WorkspaceTransform tx1 {:name "Transform 1"+ :workspace_id (:id workspace)}+ :model/WorkspaceTransform tx2 {:name "Transform 2"+ :workspace_id (:id workspace)}+ :model/Transform _tx3 {:name "Global Transform"}]+ (testing "returns transforms in workspace"+ (is (=? {:transforms [{:ref_id (:ref_id tx1)}+ {:ref_id (:ref_id tx2)}]}+ (mt/user-http-request :crowberto :get 200 (ws-url (:id workspace) "/transform")))))+ (testing "returns empty list when no transforms"+ (ws.tu/with-workspaces! [workspace {:name "Empty Workspace"}]+ (is (= {:transforms []}+ (mt/user-http-request :crowberto :get 200 (ws-url (:id workspace) "/transform"))))))+ (testing "returns 404 for non-existent workspace"+ (is (= "Not found."+ (mt/user-http-request :crowberto :get 404 "ee/workspace/999999/transform"))))))))++(deftest get-workspace-transform-by-id-test+ (testing "GET /api/ee/workspace/:id/transform/:txid"+ (ws.tu/with-workspaces! [workspace1 {:name "Workspace 1"}+ workspace2 {:name "Workspace 2"}]+ (mt/with-temp [:model/WorkspaceTransform transform {:name "My Transform"+ :description "Test description"+ :workspace_id (:id workspace1)}]+ (testing "returns specific transform"+ (is (=? {:ref_id (:ref_id transform)+ :name "My Transform"+ :description "Test description"}+ (mt/user-http-request :crowberto :get 200+ (ws-url (:id workspace1) "/transform" (:ref_id transform))))))+ (testing "returns 404 if transform not in workspace"+ (is (= "Not found."+ (mt/user-http-request :crowberto :get 404+ (ws-url (:id workspace2) "/transform" (:ref_id transform))))))))))++(deftest update-workspace-transform-test+ (testing "PUT /api/ee/workspace/:id/transform/:txid"+ (ws.tu/with-workspaces! [workspace1 {:name "Workspace 1"}+ workspace2 {:name "Workspace 2"}]+ (mt/with-temp [:model/WorkspaceTransform transform {:name "Original Name"+ :description "Original description"+ :workspace_id (:id workspace1)}]+ (testing "updates transform"+ (is (=? {:ref_id (:ref_id transform)+ :name "Updated Name"+ :description "Updated description"}+ (mt/user-http-request :crowberto :put 200+ (ws-url (:id workspace1) "/transform" (:ref_id transform))+ {:name "Updated Name"+ :description "Updated description"})))+ (is (= "Updated Name" (t2/select-one-fn :name :model/WorkspaceTransform :workspace_id (:id workspace1) :ref_id (:ref_id transform)))))+ (testing "returns 404 if transform not in workspace"+ (is (= "Not found."+ (mt/user-http-request :crowberto :put 404+ (ws-url (:id workspace2) "/transform" (:ref_id transform))+ {:name "Should Fail"}))))))))++(deftest delete-workspace-transform-test+ (testing "DELETE /api/ee/workspace/:id/transform/:txid"+ (ws.tu/with-workspaces! [workspace1 {:name "Workspace 1"}+ workspace2 {:name "Workspace 2"}]+ (mt/with-temp [:model/WorkspaceTransform transform1 {:name "Transform in WS1"+ :workspace_id (:id workspace1)}+ :model/WorkspaceTransform transform2 {:name "To Delete"+ :workspace_id (:id workspace1)}]+ (is (:ref_id transform1) "Transform 1 created successfully")+ (is (:ref_id transform2) "Transform 2 created successfully")+ (testing "returns 404 if transform not in workspace"+ (is (= "Not found."+ (mt/user-http-request :crowberto :delete 404+ (ws-url (:id workspace2) "/transform" (:ref_id transform1))))))+ (testing "deletes transform"+ (is (nil? (mt/user-http-request :crowberto :delete 204+ (ws-url (:id workspace1) "/transform" (:ref_id transform2)))))+ (is (t2/exists? :model/WorkspaceTransform :workspace_id (:id workspace1) :ref_id (:ref_id transform1)))+ (is (not (t2/exists? :model/WorkspaceTransform :workspace_id (:id workspace1) :ref_id (:ref_id transform2)))))))))++(deftest run-workspace-transform-not-found-test+ (testing "POST /api/ee/workspace/:id/transform/:txid/run returns 404 if transform not in workspace"+ (transforms.tu/with-transform-cleanup! [output-table "ws_api_notfound"]+ (ws.tu/with-workspaces! [ws1 {:name "Workspace 1"}+ ws2 {:name "Workspace 2"}]+ (mt/with-temp [:model/Transform x1 {:name "Transform in WS1"+ :source {:type "query"+ :query (mt/native-query {:query "SELECT count(*) from orders"})}+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name output-table}}]+ (let [ref-id (:ref_id (mt/user-http-request :crowberto :post 200 (ws-url (:id ws1) "/transform") x1))]+ (is (= "Not found."+ (mt/user-http-request :crowberto :post 404+ (ws-url (:id ws2) "/transform/" ref-id "/run"))))))))))++(deftest run-workspace-transform-success-test+ (testing "POST /api/ee/workspace/:id/transform/:txid/run successful execution"+ (transforms.tu/with-transform-cleanup! [output-table "ws_api_success"]+ (ws.tu/with-workspaces! [ws {:name "Workspace 1"}]+ (let [target-schema (t2/select-one-fn :schema :model/Table (mt/id :orders))]+ (mt/with-temp [:model/Transform x1 {:name "Transform"+ :source {:type "query"+ :query (mt/native-query (ws.tu/mbql->native (mt/mbql-query orders {:aggregation [[:count]]})))}+ :target {:type "table"+ :database (mt/id)+ :schema target-schema+ :name output-table}}]+ (let [ref-id (:ref_id (mt/user-http-request :crowberto :post 200 (ws-url (:id ws) "/transform") x1))+ ws (ws.tu/ws-done! (:id ws))]+ (testing "returns succeeded status with isolated table info"+ (let [result (mt/user-http-request :crowberto :post 200 (ws-url (:id ws) "transform" ref-id "run"))]+ (is (=? {:status "succeeded"+ :message nil+ :start_time some?+ :end_time some?+ :table {:schema (:schema ws)+ :name (str target-schema "__" output-table)}}+ result))))+ (testing "doesn't create excessive transforms in the db"+ (is (= (:id x1)+ (t2/select-one-fn :id [:model/Transform :id] {:order-by [[:id :desc]]}))))+ (testing "transform has last_run_at and last_run_status after success"+ (is (=? {:last_run_at some?+ :last_run_status "succeeded"}+ (mt/user-http-request :crowberto :get 200 (ws-url (:id ws) "transform" ref-id))))))))))))++(deftest run-workspace-transform-failure-test+ (testing "POST /api/ee/workspace/:id/transform/:txid/run failed execution"+ (transforms.tu/with-transform-cleanup! [output-table "ws_api_fail"]+ (ws.tu/with-workspaces! [ws {:name "Workspace for failure test"}]+ (let [bad-transform {:name "Bad Transform"+ :source {:type "query"+ :query (mt/native-query {:query "SELECT 1 LIMIT"})}+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name output-table}}+ ref-id (:ref_id+ (mt/user-http-request :crowberto :post 200 (ws-url (:id ws) "/transform") bad-transform))+ ws (ws.tu/ws-done! (:id ws))]+ (testing "returns failed status with error message and isolated table info"+ (let [result (mt/with-log-level [metabase-enterprise.transforms.query-impl :fatal]+ (mt/user-http-request :crowberto :post 200 (ws-url (:id ws) "transform" ref-id "run")))]+ (is (=? {:status "failed"+ :message some?+ :start_time some?+ :end_time some?+ :table {:schema (:schema ws)+ :name (str "public__" output-table)}}+ result))))+ (testing "transform has last_run_at, last_run_status, and last_run_message after failure"+ (is (=? {:last_run_at some?+ :last_run_status "failed"+ :last_run_message some?}+ (mt/user-http-request :crowberto :get 200 (ws-url (:id ws) "transform" ref-id))))))))))++(defn- quote-table-name+ [driver {:keys [schema], table :name}]+ (sql.u/quote-name driver :table schema table))++(deftest run-workspace-transform-bad-column-test+ (testing "POST /api/ee/workspace/:id/transform/:txid/run with non-existent column"+ (transforms.tu/with-transform-cleanup! [output-table "ws_api_badcol"]+ (ws.tu/with-workspaces! [ws {:name "Workspace for bad column test"}]+ (let [order-table (t2/select-one :model/Table (mt/id :orders))+ bad-transform {:name "Bad Column Transform"+ :source {:type "query"+ :query (mt/native-query {:query (format "SELECT nocolumn FROM %s" (quote-table-name (:engine (mt/db)) order-table))})}+ :target {:type "table"+ :database (mt/id)+ :schema (:schema order-table)+ :name output-table}}+ ref-id (:ref_id+ (mt/user-http-request :crowberto :post 200 (ws-url (:id ws) "/transform") bad-transform))+ ws (ws.tu/ws-done! (:id ws))]++ (testing "returns failed status with error message mentioning the bad column"+ (let [result (mt/with-log-level [metabase-enterprise.transforms.query-impl :fatal]+ (mt/user-http-request :crowberto :post 200 (ws-url (:id ws) "transform" ref-id "run")))]+ (is (=? {:status "failed"+ :message #"(?si).*nocolumn.*"+ :start_time some?+ :end_time some?+ :table {:schema (:schema ws)+ :name (str (:schema order-table) "__" output-table)}}+ result))))+ (testing "transform has last_run_message mentioning the bad column"+ (is (=? {:last_run_at some?+ :last_run_status "failed"+ :last_run_message #"(?si).*nocolumn.*"}+ (mt/user-http-request :crowberto :get 200 (ws-url (:id ws) "transform" ref-id))))))))))++(deftest execute-workspace-test+ (testing "POST /api/ee/workspace/:id/execute"+ (transforms.tu/with-transform-cleanup! [output-table "ws_execute_test"]+ (ws.tu/with-workspaces! [ws-1 {:name "Workspace 1"}+ ws-2 {:name "Workspace 2"}]+ (let [body {:name "Transform for execute test"+ :source {:type "query"+ :query (mt/native-query (ws.tu/mbql->native (mt/mbql-query orders {:aggregation [[:count]]})))}+ :target {:type "table"+ :database (mt/id)+ :schema nil+ :name output-table}}+ ws-transform (ws.common/add-to-changeset! (mt/user->id :crowberto) ws-1 :transform nil body)+ ws-1 (ws.tu/ws-done! (:id ws-1))]+ (testing "returns empty when no transforms"+ (is (= {:succeeded []+ :failed []+ :not_run []}+ (mt/user-http-request :crowberto :post 200 (ws-url (:id ws-2) "/run")))))+ (testing "executes transforms in workspace"+ (is (= {:succeeded [(:ref_id ws-transform)]+ :failed []+ :not_run []}+ (mt/user-http-request :crowberto :post 200 (ws-url (:id ws-1) "/run"))))))))))++(deftest execute-workspace-stale-only-test+ (testing "POST /api/ee/workspace/:id/run?stale_only=true respects staleness with enclosed transforms"+ (ws.tu/with-resources! [{:keys [workspace-id workspace-map global-map]}+ ;; (x1) -> x2 -> (x3)+ {:global {:x1 [:t0], :x2 [:x1], :x3 [:x2]}+ :workspace {:checkouts [:x1 :x3]}}]+ (ws.tu/ws-done! workspace-id)+ (let [ref-x1 (workspace-map :x1)+ ref-x3 (workspace-map :x3)+ global-x2 (str "global-id:" (global-map :x2))+ ->source (t2/select-fn->fn :ref_id :source [:model/WorkspaceTransform :ref_id :source]+ :workspace_id workspace-id)+ counter (atom 1)+ run! #(ws.tu/with-mocked-execution+ (mt/user-http-request :crowberto :post 200 (ws-url workspace-id "/run") {:stale_only 1}))+ inc-limit #(str (str/replace % #" LIMIT \d+" "") " LIMIT " (swap! counter inc))+ edit! (fn [ref-id]+ (let [current (->source ref-id)+ tweaked (update-in current [:query :stages 0 :native] inc-limit)]+ (mt/user-http-request :crowberto :put 200+ (ws-url workspace-id "/transform/" ref-id)+ {:source tweaked})))]+ (testing "initial run executes all stale transforms"+ (is (= {:succeeded [ref-x1 global-x2 ref-x3]+ :failed []+ :not_run []}+ (run!))))+ (testing "second run with nothing stale executes nothing"+ (is (= {:succeeded []+ :failed []+ :not_run []}+ (run!))))+ (edit! ref-x3)+ (testing "after editing x3, only x3 runs"+ (is (= {:succeeded [ref-x3]+ :failed []+ :not_run []}+ (run!))))+ (testing "after running x3, nothing stale"+ (is (= {:succeeded []+ :failed []+ :not_run []}+ (run!))))+ (edit! ref-x1)+ (testing "after editing x1, staleness propagates through enclosed x2 to x3"+ (is (= {:succeeded [ref-x1 global-x2 ref-x3]+ :failed []+ :not_run []}+ (run!))))+ (testing "after running all, nothing stale"+ (is (= {:succeeded []+ :failed []+ :not_run []}+ (run!))))))))++(deftest dry-run-workspace-transform-test+ (testing "POST /api/ee/workspace/:id/transform/:txid/dry-run returns 404 if transform not in workspace"+ (ws.tu/with-workspaces! [ws1 {:name "Workspace 1"}+ ws2 {:name "Workspace 2"}]+ (mt/with-temp [:model/Transform x1 {:name "Transform in WS1"+ :source {:type "query"+ :query (mt/native-query+ {:query "SELECT 1 as id, 'hello' as name UNION ALL SELECT 2, 'world' ORDER BY 1"})}+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "ws_dryrun"}}]+ (let [ref-id (:ref_id (mt/user-http-request :crowberto :post 200 (ws-url (:id ws1) "/transform") x1))+ ws1 (ws.tu/ws-done! (:id ws1))]+ (testing "Not found in different workspace"+ (is (= "Not found."+ (mt/user-http-request :crowberto :post 404+ (ws-url (:id ws2) "/transform/" ref-id "/dry-run")))))+ (testing "returns succeeded status with data"+ (let [result (mt/user-http-request :crowberto :post 200 (ws-url (:id ws1) "transform" ref-id "dry-run"))]+ (is (=? {:status "succeeded"+ :data {:rows [[1 "hello"] [2 "world"]]+ :cols [{:name #"(?i)id"} {:name #"(?i)name"}]}}+ result))))+ (testing "does NOT update last_run_at"+ (is (nil? (:last_run_at (mt/user-http-request :crowberto :get 200 (ws-url (:id ws1) "transform" ref-id))))))+ (testing "returns failed status with message"+ (t2/update! :model/WorkspaceTransform {:workspace_id (:id ws1) :ref_id ref-id}+ {:source {:type "query"+ :query (mt/native-query {:query "SELECT 1 LIMIT"})}})+ (is (=? {:status "failed"+ :message string?}+ (mt/user-http-request :crowberto :post 200 (ws-url (:id ws1) "transform" ref-id "dry-run"))))))))))++(defn- random-target [db-id]+ {:type "table"+ :database db-id+ :schema "transform_output"+ :name (str/replace (str "t_" (random-uuid)) "-" "_")})++(defn- my-native-query [db-id sql & [card-mapping]]+ ;; TODO (Chris 2025-12-11) -- don't build MBQL manually+ ;; For some reason, when is use mt/native-query the transforms hook thinks this is MBQL.+ ;; It's probably a dialect version issue.+ {:database db-id+ :lib/type :mbql/query+ :stages [{:lib/type :mbql.stage/native+ :native sql+ :template-tags (u/for-map [[tag card-id] card-mapping]+ [tag {:name tag+ :display-name tag+ :type :card+ :card-id card-id}])}]})++(deftest external-transforms-test+ (testing "GET /api/ee/workspace/id/external/transform"+ (mt/with-premium-features #{:transforms :workspaces}+ (let [db-1 (mt/id)]+ (ws.tu/with-workspaces! [ws1 {:name "Our Workspace"}+ ws2 {:name "Their Workspace"}]+ (mt/with-temp [;; Global transforms (workspace_id = null)+ :model/Database {db-2 :id} {:name "Other Db"}+ :model/Transform {xf1-id :id} {:name "Checked out - 1"+ :target (random-target db-1)}+ :model/Transform {xf2-id :id} {:name "Checked out - 2"+ :target (random-target db-1)}+ :model/Transform {xf3-id :id} {:name "Not checked out - python"+ :source {:type "python" :source-database db-1}+ :target (random-target db-1)}+ :model/Transform {xf4-id :id} {:name "Not checked out - mbql"+ :source {:type :query+ :query (mt/mbql-query venues)}+ :target (random-target db-1)}+ :model/Transform {xf5-id :id} {:name "Not checked out - native"+ :source {:type "query"+ :query (my-native-query db-1 "SELECT 1")}+ :target (random-target db-1)}+ ;; Native transform referencing a card - should be disabled once BOT-694 is implemented+ :model/Card {card-id :id} {:name "Source Card"+ :database_id db-1+ :dataset_query (mt/mbql-query venues)}+ :model/Transform {xf6-id :id} {:name "Not checked out - native with card dep"+ :source {:type "query"+ :query (my-native-query+ db-1+ "SELECT * FROM {{card}}"+ {"card" card-id})}+ :target (random-target db-1)}+ :model/Transform {xf7-id :id} {:name "Using another database"+ :source {:type "python" :source-database db-1}+ :target (random-target db-2)}+ ;; Workspace transforms (mirrored from global1 and global2)+ :model/WorkspaceTransform _ {:global_id xf1-id+ :workspace_id (:id ws1)}+ :model/WorkspaceTransform _ {:global_id xf2-id+ :workspace_id (:id ws2)}]+ (testing "excludes irrelevant transforms, and indicates which remaining transforms cannot be checked out."+ (let [transforms (:transforms (mt/user-http-request :crowberto :get 200 (ws-url (:id ws1) "/external/transform")))+ test-ids #{xf1-id xf2-id xf3-id xf4-id xf5-id xf6-id xf7-id}+ ;; Filter out cruft from dev, leaky tests, etc+ ids (into #{} (comp (map :id) (filter test-ids)) transforms)]+ (testing "we filter out the expected transforms"+ ;; ❌ xf1 is checked out in this workspace, so it's filtered out+ ;; ✅ xf2 is only checked out in another workspace, so it's kept+ ;; ❌ xf7 is in another database, so it's filtered out+ (is (= (disj test-ids xf1-id xf7-id) ids)))+ (testing "we get the correct checkout_disabled reasons"+ (is (= {xf2-id nil+ xf3-id nil #_python-is-supported+ xf4-id "mbql"+ xf5-id nil #_native-is-supported+ xf6-id "card-reference"}+ (u/index-by :id :checkout_disabled transforms))))))+ (testing "passing database-id shows transforms from that database"+ (is (= [xf7-id]+ (map :id+ (:transforms+ (mt/user-http-request :crowberto :get 200+ (ws-url (:id ws1) (str "external/transform?database-id=" db-2))))))))))))))++;;; ---------------------------------------- Table ID Fallback Tests ----------------------------------------+;; These tests verify that when WorkspaceOutput rows have null table IDs (e.g., because sync+;; hasn't run yet), the /table endpoint will look up the IDs from metabase_table as a fallback.++(deftest tables-endpoint-fallback-global-table-id-test+ (testing "GET /api/ee/workspace/:id/table uses fallback for null global_table_id"+ (mt/with-premium-features #{:workspaces}+ ;; TODO (Chris 2026-01-05) -- remove :schema workaround once this adds a transform properly, triggering async initialization+ (ws.tu/with-workspaces! [workspace {:name "Fallback Test WS"}]+ ;; TODO (Chris 2026-01-07) -- things fail because the workspace won't have been initialized yet.+ ;; ... the correct thing to do would be for the relevant code to initialize it!+ (t2/update! :model/Workspace (:id workspace) {:schema "isolated_schema"})+ (mt/with-temp [:model/WorkspaceTransform ws-tx {:workspace_id (:id workspace)+ :name "Test Transform"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "fallback_test_table"}}+ ;; Create a table that exists but WorkspaceOutput doesn't know about yet+ :model/Table table {:db_id (mt/id)+ :schema "public"+ :name "fallback_test_table"+ :active true}+ ;; Create WorkspaceOutput with null global_table_id (simulating pre-sync state)+ :model/WorkspaceOutput _ {:workspace_id (:id workspace)+ :ref_id (:ref_id ws-tx)+ :db_id (mt/id)+ :global_schema "public"+ :global_table "fallback_test_table"+ :global_table_id nil+ ;; TODO (Chris 2026-01-05) -- another spot+ :isolated_schema (or (:schema workspace) "workaround")+ :isolated_table "public__fallback_test_table"+ :isolated_table_id nil}]+ (let [result (mt/user-http-request :crowberto :get 200 (ws-url (:id workspace) "/table"))]+ (testing "fallback populates global table_id from metabase_table"+ (is (= (:id table)+ (-> result :outputs first :global :table_id))))+ (testing "isolated table_id remains nil when table doesn't exist"+ (is (nil? (-> result :outputs first :isolated :table_id))))))))))++(deftest ^:parallel tables-endpoint-fallback-isolated-table-id-test+ (testing "GET /api/ee/workspace/:id/table uses fallback for null isolated_table_id"+ (mt/with-premium-features #{:workspaces}+ (mt/with-temp [:model/Workspace workspace {:name "Isolated Fallback Test WS"+ :database_id (mt/id)+ :schema "ws_iso_fallback"}+ :model/WorkspaceTransform ws-tx {:workspace_id (:id workspace)+ :name "Test Transform"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "iso_fallback_table"}}+ ;; Create the isolated table in metabase_table+ :model/Table iso-table {:db_id (mt/id)+ :schema "ws_iso_fallback"+ :name "public__iso_fallback_table"+ :active true}+ ;; Create WorkspaceOutput with null isolated_table_id+ :model/WorkspaceOutput _ {:workspace_id (:id workspace)+ :ref_id (:ref_id ws-tx)+ :db_id (mt/id)+ :global_schema "public"+ :global_table "iso_fallback_table"+ :global_table_id nil+ :isolated_schema "ws_iso_fallback"+ :isolated_table "public__iso_fallback_table"+ :isolated_table_id nil}]+ (let [result (mt/user-http-request :crowberto :get 200 (ws-url (:id workspace) "/table"))]+ (testing "fallback populates isolated table_id from metabase_table"+ (is (= (:id iso-table)+ (-> result :outputs first :isolated :table_id)))))))))++(deftest ^:parallel tables-endpoint-fallback-both-table-ids-test+ (testing "GET /api/ee/workspace/:id/table uses fallback for both null table IDs"+ (mt/with-premium-features #{:workspaces}+ (mt/with-temp [:model/Workspace workspace {:name "Both Fallback Test WS"+ :database_id (mt/id)+ :schema "ws_both_fallback"}+ :model/WorkspaceTransform ws-tx {:workspace_id (:id workspace)+ :name "Test Transform"+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "both_fallback_table"}}+ ;; Create both tables+ :model/Table global-table {:db_id (mt/id)+ :schema "public"+ :name "both_fallback_table"+ :active true}+ :model/Table iso-table {:db_id (mt/id)+ :schema "ws_both_fallback"+ :name "public__both_fallback_table"+ :active true}+ ;; Create WorkspaceOutput with both table IDs null+ :model/WorkspaceOutput _ {:workspace_id (:id workspace)+ :ref_id (:ref_id ws-tx)+ :db_id (mt/id)+ :global_schema "public"+ :global_table "both_fallback_table"+ :global_table_id nil+ :isolated_schema "ws_both_fallback"+ :isolated_table "public__both_fallback_table"+ :isolated_table_id nil}]+ (let [result (mt/user-http-request :crowberto :get 200 (ws-url (:id workspace) "/table"))]+ (testing "fallback populates both table IDs"+ (is (= (:id global-table)+ (-> result :outputs first :global :table_id)))+ (is (= (:id iso-table)+ (-> result :outputs first :isolated :table_id)))))))))++(deftest graph-test+ (testing "GET /api/ee/workspace/:id/graph - explicit shape for a smaller graph"+ (let [{ws-id :workspace-id+ id-map :global-map+ tx-ids :workspace-map} (ws.tu/create-resources! {:global {:x1 [:t0]+ :x2 [:x1]}+ :workspace {:checkouts [:x1]+ :definitions {:x3 [:x2]}}})++ ws (ws.tu/ws-done! ws-id)+ tx-1 (t2/select-one :model/WorkspaceTransform :workspace_id ws-id, :ref_id (tx-ids :x1))+ tx-2 (t2/select-one :model/Transform (id-map :x2))+ tx-3 (t2/select-one :model/WorkspaceTransform :workspace_id ws-id, :ref_id (tx-ids :x3))++ driver (t2/select-one-fn :engine [:model/Database :engine] (:database_id ws))+ tx-schema (:schema (:target tx-1))+ tx-1-output (:name (:target tx-1))+ tx-2-output (sql.normalize/normalize-name driver (:name (:target tx-2)))+ tx-3-output (:name (:target tx-3))+ {input-id :id+ input-schema :schema+ input-table :name} (t2/select-one :model/Table (id-map :t0))+ ;; Look up output table IDs for transform targets+ tx-1-table-id (t2/select-one-fn :id :model/Table :db_id (mt/id) :schema tx-schema :name tx-1-output)+ tx-2-table-id (t2/select-one-fn :id :model/Table :db_id (mt/id) :schema tx-schema :name tx-2-output)+ tx-3-table-id (t2/select-one-fn :id :model/Table :db_id (mt/id) :schema tx-schema :name tx-3-output)+ tx-1-input (str (mt/id) "-" input-schema "-" input-table)+ t1-ref (:ref_id tx-1)+ t3-ref (:ref_id tx-3)]++ (testing "returns enclosed external transform too"+ (is (= {:nodes #{{:type "input-table"+ :id tx-1-input+ :data {:db (mt/id), :schema input-schema, :table input-table, :id input-id}+ :dependents_count {:workspace-transform 1}}+ {:type "workspace-transform"+ :id t1-ref+ :data {:ref_id t1-ref+ :name (:name tx-1)+ :target {:db (mt/id)+ :schema tx-schema+ :table tx-1-output+ :table_id tx-1-table-id}}+ :dependents_count {:external-transform 1}}+ {:type "external-transform"+ :id (:id tx-2)+ :data {:id (:id tx-2)+ :name (:name tx-2)+ :target {:db (mt/id)+ :schema tx-schema+ :table tx-2-output+ :table_id tx-2-table-id}}+ :dependents_count {:workspace-transform 1}}+ {:type "workspace-transform"+ :id t3-ref+ :data {:ref_id t3-ref+ :name (:name tx-3)+ :target {:db (mt/id)+ :schema tx-schema+ :table tx-3-output+ :table_id tx-3-table-id}}+ :dependents_count {}}},+ :edges #{{:to_entity_type "input-table"+ :to_entity_id tx-1-input+ :from_entity_type "workspace-transform"+ :from_entity_id t1-ref}+ {:to_entity_type "workspace-transform"+ :to_entity_id t1-ref+ :from_entity_type "external-transform"+ :from_entity_id (:id tx-2)}+ {:to_entity_type "external-transform"+ :to_entity_id (:id tx-2)+ :from_entity_type "workspace-transform"+ :from_entity_id (:ref_id tx-3)}}}+ (-> (mt/user-http-request :crowberto :get 200 (ws-url (:id ws) "graph"))+ (update-vals set))))))))++;; Edge cases covered:+;;+;; 1. excluded ancestor+;; 2. excluded descendant+;; 3. disconnected components+;; 4. enclosed by chain+;; 5. workspace tx input+;; 6. enclosed tx input+;; 7. overridden dependency+;;+;; Ideas for more:+;;+;; - Removed dependency+;; - Inverted dependency+;; - ...+(deftest larger-test+ (testing "GET /api/ee/workspace/:id/graph - structure for a larger and more complex graph"+ (let [{ws-id :workspace-id+ :as resources-map} (ws.tu/create-resources! {:global {:x1 [:t100]+ :x2 [:x1 :t101]+ :x3 [:x2 :t102]+ :x4 [:x3 :t103]+ :x5 [:x4 :t104]+ :x6 [:t105]+ :x7 [:106]}+ :workspace {:checkouts [:x2 :x4 :x6]+ :definitions {:x4 [:x3 :t199]}}})]+ (testing "returns enclosed external transform too"+ (is (= {:nodes #{;; checked out+ :x2+ :x4+ :x6+ ;; enclosed+ :x3+ ;; output of non-enclosed ancestor+ :t1+ ;; global input for workspace transforms+ :t101+ :t105+ ;; global input for enclosed transform+ :t102+ ;; overridden input+ :t199},+ :edges {:x2 #{:t1 :t101}+ :x3 #{:x2 :t102}+ :x4 #{:x3 :t199}+ :x6 #{:t105}}}+ (-> (mt/user-http-request :crowberto :get 200 (ws-url ws-id "graph"))+ (ws.tu/translate-graph resources-map))))))))++(deftest enabled-test+ (let [url "ee/workspace/enabled"]+ (is (= {:supported true} (mt/user-http-request :crowberto :get 200 url)))+ (mt/with-temp [:model/Database {db-1 :id} {:name "Y", :engine "postgres"}+ ;; For some reason, using a real but unsupported value like "databricks" is returning support :-C+ :model/Database {db-2 :id} {:name "N", :engine "crazy"}]+ (testing "Unsupported driver"+ (is (= {:supported false, :reason "Database type not supported."}+ (mt/user-http-request :crowberto :get 200 (str url "?database-id=" db-2)))))+ (testing "Supported driver"+ (is (= {:supported true}+ (mt/user-http-request :crowberto :get 200 (str url "?database-id=" db-1)))))+ (testing "Audit not returned"+ (is (nil?+ (m/find-first (comp #{audit/audit-db-id} :id)+ (:databases (mt/user-http-request :crowberto :get 200 "ee/workspace/database")))))))))++(deftest workspace-database-listing-test+ (testing "GET /api/ee/workspace/database"+ (testing "databases with workspaces_enabled=true show as enabled"+ (mt/with-temp [:model/Database {db-enabled :id} {:name "DB Enabled"+ :engine driver/*driver*+ :is_audit false+ :is_sample false+ :workspace_permissions_status {:status "ok" :checked_at "2025-01-01"}+ :settings {:database-enable-workspaces true}}+ :model/Database {db-disabled :id} {:name "DB Disabled"+ :engine driver/*driver*+ :is_audit false+ :is_sample false+ :workspace_permissions_status {:status "ok" :checked_at "2025-01-01"}+ :settings {}}]+ (let [response (mt/user-http-request :crowberto :get 200 "ee/workspace/database")+ enabled-entry (m/find-first #(= (:id %) db-enabled) (:databases response))+ disabled-entry (m/find-first #(= (:id %) db-disabled) (:databases response))]+ (is (true? (:enabled enabled-entry)))+ (is (= "ok" (get-in enabled-entry [:workspace_permissions_status :status])))+ (is (false? (:enabled disabled-entry)))+ (is (= "ok" (get-in disabled-entry [:workspace_permissions_status :status]))))))++ (testing "databases with failed permission check include permissions_status"+ (mt/with-temp [:model/Database {db-failed :id} {:name "DB Failed"+ :engine driver/*driver*+ :is_audit false+ :is_sample false+ :workspace_permissions_status {:status "failed" :error "permission denied" :checked_at "2025-01-01"}}]+ (let [response (mt/user-http-request :crowberto :get 200 "ee/workspace/database")+ fail-entry (m/find-first #(= (:id %) db-failed) (:databases response))]+ (is (false? (:enabled fail-entry)))+ (is (= "failed" (get-in fail-entry [:workspace_permissions_status :status])))+ (is (= "permission denied" (get-in fail-entry [:workspace_permissions_status :error]))))))++ (testing "databases without permission check have unknown permissions_status"+ (mt/with-temp [:model/Database {db-uncached :id} {:name "DB Uncached"+ :engine driver/*driver*+ :is_audit false+ :is_sample false}]+ (let [response (mt/user-http-request :crowberto :get 200 "ee/workspace/database")+ entry (m/find-first #(= (:id %) db-uncached) (:databases response))]+ (is (false? (:enabled entry)))+ (is (= {:status "unknown"} (:workspace_permissions_status entry))))))))++(deftest checkout-endpoint-test+ (testing "GET /api/ee/workspace/checkout returns checkout status for a transform"+ (mt/with-temp [:model/Transform tx {:name "Native Transform"+ :source {:type :query+ :query {:database (mt/id)+ :type :native+ :native {:query "SELECT 1"}}}+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "checkout_test_table"}}]+ (ws.tu/with-workspaces! [ws1 {:name "Workspace One" :database_id (mt/id)}+ ws2 {:name "Workspace Two" :database_id (mt/id)}]+ (testing "initially all workspaces for the database are returned, none checked out"+ (is (=? {:checkout_disabled nil+ :workspaces [{:id (:id ws1) :name "Workspace One" :status string? :existing nil}+ {:id (:id ws2) :name "Workspace Two" :status string? :existing nil}]+ :transforms []}+ (mt/user-http-request :crowberto :get 200 "ee/workspace/checkout"+ :transform-id (:id tx)))))++ (testing "after checkout, the workspace shows the existing checkout info"+ (mt/user-http-request :crowberto :post 200 (ws-url (:id ws1) "/transform")+ (merge {:global_id (:id tx)}+ (select-keys tx [:name :source :target])))+ (is (=? {:checkout_disabled nil+ :workspaces [{:id (:id ws1) :name "Workspace One" :status string?+ :existing {:ref_id string? :name "Native Transform"}}+ {:id (:id ws2) :name "Workspace Two" :status string? :existing nil}]+ :transforms [{:id string? :name "Native Transform"+ :workspace {:id (:id ws1) :name "Workspace One"}}]}+ (mt/user-http-request :crowberto :get 200 "ee/workspace/checkout"+ :transform-id (:id tx)))))++ (testing "returns 404 for non-existent transform"+ (is (= "Not found."+ (mt/user-http-request :crowberto :get 404 "ee/workspace/checkout"+ :transform-id 999999))))))))++(deftest checkout-disabled-reason-test+ (testing "GET /api/ee/workspace/checkout returns correct checkout_disabled reasons"+ (testing "MBQL transforms cannot be checked out"+ (mt/with-temp [:model/Transform tx {:name "MBQL Transform"+ :source {:type :query+ :query (mt/mbql-query venues)}+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "mbql_checkout_test"}}]+ (is (=? {:checkout_disabled "mbql"+ :workspaces []+ :transforms []}+ (mt/user-http-request :crowberto :get 200 "ee/workspace/checkout"+ :transform-id (:id tx))))))++ (testing "Python transforms can be checked out"+ (mt/with-temp [:model/Transform tx {:name "Python Transform"+ :source {:type :python :code "print(1)" :source-database (mt/id)}+ :source_type :python+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "python_checkout_test"}}]+ (is (=? {:checkout_disabled nil+ :workspaces []+ :transforms []}+ (mt/user-http-request :crowberto :get 200 "ee/workspace/checkout"+ :transform-id (:id tx))))))))++(deftest checkout-blocked-for-mbql-transforms-test+ (testing "POST /api/ee/workspace/:id/transform blocks MBQL transform checkout"+ (mt/with-temp [:model/Transform tx {:name "MBQL Transform"+ :source {:type :query+ :query (mt/mbql-query venues)}+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "mbql_blocked_test"}}]+ (ws.tu/with-workspaces! [ws {:name "Test Workspace" :database_id (mt/id)}]+ (is (= "MBQL transforms cannot be added to workspaces."+ (mt/user-http-request :crowberto :post 400 (ws-url (:id ws) "/transform")+ (merge {:global_id (:id tx)}+ (select-keys tx [:name :source :target])))))))))++(deftest checkout-blocked-for-card-reference-transforms-test+ (testing "POST /api/ee/workspace/:id/transform blocks transform with card references"+ (mt/with-temp [:model/Card card {:name "Referenced Card"+ :database_id (mt/id)+ :dataset_query (mt/native-query {:query "SELECT 1"})}+ :model/Transform tx {:name "Card Reference Transform"+ :source {:type :query+ :database (mt/id)+ :query (mt/native-query {:query "SELECT * FROM {{#card}}"+ :template-tags {"card" {:type "card"+ :card-id (:id card)+ :display-name "Card"}}})}+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "card_ref_blocked_test"}}]+ (ws.tu/with-workspaces! [ws {:name "Test Workspace" :database_id (mt/id)}]+ (is (= "Transforms that reference other questions cannot be added to workspaces."+ (mt/user-http-request :crowberto :post 400 (ws-url (:id ws) "/transform")+ (merge {:global_id (:id tx)}+ (select-keys tx [:name :source :target])))))))))++(deftest global-id-immutable-test+ (testing "PUT /api/ee/workspace/:id/transform/:tx-id cannot change global_id"+ (let [{:keys [workspace-id global-map workspace-map]}+ (ws.tu/create-resources!+ {:global {:x1 [:t1]+ :x2 [:t2]}+ :workspace {:checkouts [:x1]+ :definitions {:x3 [:t3]}}})]+ (testing "cannot clear global_id on a checked-out transform"+ (is (=? {:cause "Cannot change global_id of an existing workspace transform."}+ (mt/user-http-request :crowberto :put 400 (ws-url workspace-id "/transform/" (workspace-map :x1))+ {:global_id nil}))))+ (testing "cannot set global_id on a workspace-only transform"+ (is (=? {:cause "Cannot change global_id of an existing workspace transform."}+ (mt/user-http-request :crowberto :put 400 (ws-url workspace-id "/transform/" (workspace-map :x3))+ {:global_id (global-map :x2)})))))))++(defmacro ^:private with-test-resources-cleanup! [& body]+ `(mt/with-model-cleanup [:model/Transform :model/Workspace :model/Table]+ ~@body))++(deftest test-resources-empty+ (testing "POST /api/ee/workspace/test-resources with global transforms only (no workspace)"+ (with-test-resources-cleanup!+ (is (=? {:workspace-id nil+ :global-map {}+ :workspace-map {}}+ (mt/user-http-request :crowberto :post 200 "ee/workspace/test-resources"+ {:global {}}))))))++(deftest test-resources-single+ (testing "POST /api/ee/workspace/test-resources with global transforms only (no workspace)"+ (with-test-resources-cleanup!+ (is (=? {:workspace-id nil+ :global-map {:x1 int?}+ :workspace-map {}}+ (mt/user-http-request :crowberto :post 200 "ee/workspace/test-resources"+ {:global {:x1 []}}))))))++(deftest test-resources-global-only-test+ (testing "POST /api/ee/workspace/test-resources with global transforms only (no workspace)"+ (with-test-resources-cleanup!+ (let [orders (mt/format-name :orders)]+ (is (=? {:workspace-id nil+ :global-map {(keyword orders) int?, :x1 int?, :x2 int?}+ :workspace-map {}}+ (mt/user-http-request :crowberto :post 200 "ee/workspace/test-resources"+ {:global {:x1 [orders], :x2 [:x1]}})))))))++(deftest test-resources-complex-graph-test+ (testing "POST /api/ee/workspace/test-resources with complex graph"+ (with-test-resources-cleanup!+ (let [orders (mt/format-name :orders)+ products (mt/format-name :products)+ people (mt/format-name :people)]+ (is (=? {:workspace-id int?+ :global-map {(keyword orders) int?+ (keyword products) int?+ :t4 int?+ :x1 int?+ :x2 int?+ :x3 int?+ :x4 int?}+ :workspace-map {:x5 string?, :x6 string?}}+ (mt/user-http-request :crowberto :post 200 "ee/workspace/test-resources"+ {:global {:x1 [orders]+ :x2 [products]+ :x3 [:x1 :x2]+ :x4 [:t4]}+ :workspace {:definitions {:x2 [:x1]+ :x5 [orders :x3]+ :x6 [:x5 people]}}})))))))++(deftest test-resources-custom-database-test+ (testing "POST /api/ee/workspace/test-resources with custom database_id"+ (mt/with-temp [:model/Database {other-db-id :id} {:name "Other DB" :engine driver/*driver*}+ :model/Table {table-id :id} {:name "coffee", :db_id other-db-id}]+ (with-test-resources-cleanup!+ (let [result (mt/user-http-request :crowberto :post 200 "ee/workspace/test-resources"+ {:database_id other-db-id+ :global {:x1 [:t1 "coffee"]}+ :workspace {:definitions {:x2 [:t2]}}})]+ (is (=? {:workspace-id int?+ :global-map {:t1 int?, :t2 int?, :x1 int?, :coffee table-id}+ :workspace-map {:x2 string?}}+ result))+ (testing "workspace uses the specified database"+ (is (= other-db-id+ (t2/select-one-fn :database_id :model/Workspace :id (:workspace-id result)))))+ (testing "mock tables are created in the specified database"+ (is (= other-db-id+ (t2/select-one-fn :db_id :model/Table :id (:t1 (:global-map result)))))+ (is (= other-db-id+ (t2/select-one-fn :db_id :model/Table :id (:t2 (:global-map result))))))+ (testing "transform targets the specified database"+ (is (= other-db-id+ (get-in (t2/select-one :model/Transform :id (:x1 (:global-map result))) [:target :database])))))))))++;;; ============================================ Authorization Test Matrix ============================================++(def ^:private admin-only-routes+ "Routes that require superuser access"+ [[:get "/"]+ [:post "/"]+ [:get "/enabled"]+ [:get "/database"]+ [:get "/checkout"]+ [:put "/:ws-id"]+ [:post "/:ws-id/archive"]+ [:post "/:ws-id/unarchive"]+ [:delete "/:ws-id"]+ [:post "/:ws-id/merge"]+ [:post "/:ws-id/transform/:tx-id/merge"]+ [:post "/test-resources"]])++(def ^:private service-user-routes+ "Routes that allow workspace service users ({:access :workspace})"+ [[:get "/:ws-id"]+ [:get "/:ws-id/table"]+ [:get "/:ws-id/log"]+ [:get "/:ws-id/graph"]+ [:get "/:ws-id/problem"]+ [:get "/:ws-id/external/transform"]+ [:get "/:ws-id/transform"]+ [:post "/:ws-id/transform"]+ [:get "/:ws-id/transform/:tx-id"]+ [:put "/:ws-id/transform/:tx-id"]+ [:delete "/:ws-id/transform/:tx-id"]+ [:post "/:ws-id/transform/:tx-id/archive"]+ [:post "/:ws-id/transform/:tx-id/unarchive"]+ [:post "/:ws-id/run"]+ [:post "/:ws-id/transform/:tx-id/run"]+ [:post "/:ws-id/transform/:tx-id/dry-run"]+ [:post "/:ws-id/transform/validate/target"]])++(def ^:private permission-denied-msg "You don't have permissions to do that.")++(deftest service-user-authorization-test+ (ws.tu/with-workspaces! [ws1 {:name "Workspace 1"}+ ws2 {:name "Workspace 2"}]+ (mt/with-temp [:model/WorkspaceTransform tx {:name "Test Transform"+ :workspace_id (:id ws1)}]+ (let [service-user-1 (:execution_user ws1)+ service-user-2 (:execution_user ws2)+ resolve-url (fn [pattern]+ (-> pattern+ (str/replace ":ws-id" (str (:id ws1)))+ (str/replace ":tx-id" (:ref_id tx))+ (->> (str "ee/workspace"))))]++ ;; We don't test whether an admin can access the routes - that's implicit in the regular tests for each route.++ (testing "Admin-only routes reject service users and other non-admins"+ (doseq [[method pattern] admin-only-routes+ :let [url (resolve-url pattern)]]+ (testing (str method " " pattern)+ (is (= permission-denied-msg (mt/user-http-request :rasta method 403 url))+ "Should reject regular users")+ (is (= permission-denied-msg (mt/user-http-request service-user-1 method 403 url))+ "Should reject even its own service user"))))++ (testing "Workspace routes allow own service user, reject others"+ (doseq [[method pattern] service-user-routes+ :let [url (resolve-url pattern)]]+ (testing (str method " " pattern)+ ;; We check for NOT 403 since some routes may 404 without full setup.+ ;; If auth passes, we won't get the permission-denied message.+ (let [resp (mt/user-http-request-full-response service-user-1 method url)]+ (is (not= 403 (:status_code resp)) "Should allow its own service user")+ (is (not= permission-denied-msg (:body resp)) "Should allow its own service user"))+ (is (= permission-denied-msg (mt/user-http-request service-user-2 method 403 url))+ "Should reject other service users"))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/dag_abstract.clj b/enterprise/backend/test/metabase_enterprise/workspaces/dag_abstract.cljnew file mode 100644index 00000000000..9d390b34c51--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/dag_abstract.clj@@ -0,0 +1,117 @@+(ns metabase-enterprise.workspaces.dag-abstract+ "Abstract graph solver using shorthand notation for testing.++ Shorthand uses keywords like :x1, :t2, :m3 where:+ - :x<n> is a transform+ - :t<n> is a table (source/generated)+ - :m<n> is a model (card)"+ (:require+ [clojure.set :as set]+ [flatland.ordered.map :as ordered-map]+ [metabase-enterprise.workspaces.util :as ws.u]+ [metabase.util :as u]))++;;;; Shorthand notation helpers++(defn- related [t id]+ (keyword (str (name t) (subs (name id) 1))))++(def ^:private x->t (partial related :t))++(defn is?+ "Check if id is of type t (e.g., (is? :x :x1) => true)."+ [t id]+ (= (name t) (str (first (name id)))))++(defn- lexico [id]+ (let [n (name id)+ prefix (when (keyword? id) (first n))]+ (if-let [type-order ({\x 1 \t 2 \m 3} prefix)]+ ;; Standard shorthand keyword like :x1, :t2 - sort by number then type+ [(parse-long (subs n 1)) type-order]+ ;; Real table string reference - sort after all standard keywords+ [Long/MAX_VALUE n])))++(defn expand-shorthand+ "As shorthand, we can use transforms in the place of their induced tables.+ This function expands the graph to insert the induced tables, and replace the corresponding references."+ [dependencies]+ (let [xs (set (filter #(is? :x %) (concat (keys dependencies) (mapcat val dependencies))))]+ (into (sorted-map-by #(compare (lexico %1) (lexico %2)))+ (merge (zipmap (map x->t xs) (map vector xs))+ (into {} (filter (comp #(is? :t %) key)) dependencies)+ (update-vals+ (into {} (remove (comp #(is? :t %) key)) dependencies)+ (fn [deps] (->> deps+ (map (fn [id]+ (if (is? :x id)+ (x->t id)+ id)))+ (sort-by lexico)+ (vec))))))))++;;;; Graph traversal helpers++(defn- children [dependencies]+ (reduce+ (fn [acc [child parents]]+ (reduce (fn [acc p] (update acc p u/conjv child)) acc parents))+ (sorted-map-by #(compare (lexico %1) (lexico %2)))+ dependencies))++(defn- ->ts [ids]+ (into #{}+ (keep (fn [id]+ (cond (is? :t id) id+ (is? :x id) (x->t id))))+ ids))++;;;; Toposort++(defn- toposort-map [child->parents]+ (let [child->parents (if (map? child->parents) child->parents (into {} child->parents))]+ (into (ordered-map/ordered-map)+ (keep (fn [n] (when-let [deps (get child->parents n)] [n deps])))+ (ws.u/toposort-dfs child->parents))))++;;;; Abstract path-induced subgraph solver++(def ^:private max-iterations 100)++(defn path-induced-subgraph+ "Abstract solver for path-induced subgraph using shorthand notation."+ [{:keys [check-outs dependencies] :as _graph}]+ (let [->parents dependencies+ ->children (children dependencies)]+ (loop [iteration 1+ below check-outs+ above check-outs+ seen-down? check-outs+ seen-up? check-outs+ to-descend check-outs+ to-ascend check-outs]+ (if (and (empty? to-descend) (empty? to-ascend))+ (let [nodes (set/intersection above below)+ entities (remove #(is? :t %) nodes)+ tables (sort-by lexico (filter #(is? :t %) nodes))]+ {:check-outs (sort-by lexico check-outs)+ :inputs (sort-by lexico (remove nodes (->ts (mapcat ->parents check-outs))))+ :tables tables+ :transforms (sort-by lexico (filter #(is? :x %) nodes))+ :entities (sort-by lexico entities)+ :dependencies (apply dissoc+ (toposort-map+ (for [n nodes]+ [n (vec (filter nodes (get dependencies n)))]))+ tables)})+ (if (> iteration max-iterations)+ {:error "max iterations exceeded"}+ (let [next-down (into #{} (comp (mapcat ->children) (remove seen-down?)) to-descend)+ next-up (into #{} (comp (mapcat ->parents) (remove seen-up?)) to-ascend)]+ (recur (inc iteration)+ (into below next-down)+ (into above next-up)+ (into seen-down? next-down)+ (into seen-up? next-up)+ next-down+ next-up)))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/dag_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/dag_test.cljnew file mode 100644index 00000000000..a20ea51cbd6--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/dag_test.clj@@ -0,0 +1,288 @@+(ns metabase-enterprise.workspaces.dag-test+ (:require+ [clojure.test :refer :all]+ [flatland.ordered.map :as ordered-map]+ [metabase-enterprise.workspaces.dag :as ws.dag]+ [metabase-enterprise.workspaces.dag-abstract :as dag-abstract]+ [metabase-enterprise.workspaces.test-util :as ws.tu]+ [metabase.test :as mt]+ [metabase.util :as u]+ [toucan2.core :as t2]))++(use-fixtures+ :once+ (fn [f]+ (mt/with-premium-features [:dependencies :transforms :workspaces]+ (mt/with-model-cleanup [:model/Dependency :model/Transform :model/Table :model/Workspace]+ (f)))))++;;;; Example graphs for testing++(def ^:private example-graph+ {:x3 [:x1 :t2]+ :x4 [:x3]+ :m6 [:x4 :t5]+ :m10 [:t9]+ :x11 [:m10]+ :m12 [:x11]+ :m13 [:x11 :m12]})++;;;; Test data helpers++(defn- translate-result+ "Translate result from real IDs back to shorthand notation for easier comparison."+ [workspace-id {:keys [inputs outputs entities dependencies] :as _result} id-map]+ (let [reverse-map (u/for-map [[k v] id-map] [v k])+ table->kw (comp reverse-map :id)+ node->kw (fn [{:keys [node-type id]}]+ (reverse-map (case node-type+ :table (:id id)+ :external-transform id+ :workspace-transform (t2/select-one-fn :global_id [:model/WorkspaceTransform :global_id]+ :workspace_id workspace-id :ref_id id))))]+ {:inputs (into #{} (map table->kw) inputs)+ :outputs (into #{} (map table->kw) outputs)+ :entities (into #{} (map node->kw) entities)+ :dependencies (u/for-map [[child parents] dependencies]+ [(node->kw child) (into #{} (map node->kw parents))])}))++;;;; Tests++(deftest path-induced-subgraph-shorthand-test+ (testing "graph built from shorthand matches abstract solver"+ (let [{:keys [workspace-id global-map workspace-map]}+ (ws.tu/create-resources!+ {:global {:x2 [:t1]}+ :workspace {:checkouts [:x2]}})]+ (ws.tu/analyze-workspace! workspace-id)+ (let [entity {:entity-type :transform, :id (workspace-map :x2)}+ result (ws.dag/path-induced-subgraph workspace-id [entity])+ translated (translate-result workspace-id result global-map)]+ (is (=? {:inputs #{:t1}+ :outputs #{:t2}+ :entities #{:x2}+ :dependencies {:x2 #{:t1}}}+ translated))))))++(deftest path-induced-subgraph-larger-test+ (testing "graph built from shorthand matches abstract solver"+ ;; check-outs x2, x4+ (let [{:keys [workspace-id global-map workspace-map]}+ (ws.tu/create-resources!+ {:global {:x1 [:t0]+ :x2 [:x1 :t10]+ :x3 [:x2 :t8]+ :x4 [:x3]+ :x5 [:x2 :x4 :t9]}+ :workspace {:checkouts [:x2 :x4]}})]+ (ws.tu/analyze-workspace! workspace-id)+ (let [entities (for [tx [:x2 :x4]]+ {:entity-type :transform, :id (workspace-map tx)})+ result (ws.dag/path-induced-subgraph workspace-id entities)+ translated (translate-result workspace-id result global-map)]+ (is (=? {:inputs #{:t1 :t8 :t10}+ :outputs #{:t2 :t3 :t4}+ :entities #{:x2 :x3 :x4}+ :dependencies {:x2 #{:t1 :t10}+ :x3 #{:x2 :t8}+ :x4 #{:x3}}}+ translated))))))++(deftest expand-solver-test+ (testing "expand-shorthand inserts interstitial nodes for transform output tables"+ (is (= {:t1 [:x1]+ :x3 [:t1 :t2]+ :t3 [:x3]+ :x4 [:t3]+ :t4 [:x4]+ :m6 [:t4 :t5]+ :m10 [:t9]+ :x11 [:m10]+ :t11 [:x11]+ :m12 [:t11]+ :m13 [:t11 :m12]}+ (dag-abstract/expand-shorthand example-graph)))))++(deftest abstract-path-induced-subgraph-test+ (testing "path-induced-subgraph computes correct result for example graph"+ (is (= {:check-outs [:x3 :m6 :m10 :m13]+ :inputs [:t1 :t2 :t5 :t9]+ :tables [:t3 :t4 :t11]+ :transforms [:x3 :x4 :x11]+ :entities [:x3 :x4 :m6 :m10 :x11 :m12 :m13]+ :dependencies (ordered-map/ordered-map+ :m10 []+ :x11 [:m10]+ :m12 [:t11]+ :m13 [:t11 :m12]+ :x3 []+ :x4 [:t3]+ :m6 [:t4])}+ (dag-abstract/path-induced-subgraph+ {:check-outs #{:x3 :m6 :m10 :m13}+ :dependencies (dag-abstract/expand-shorthand example-graph)})))))++;;;; Graph utility tests++(deftest reverse-graph-test+ (testing "empty graph"+ (is (= {} (ws.dag/reverse-graph {}))))++ (testing "single edge"+ (is (= {:a [:b]}+ (ws.dag/reverse-graph {:b [:a]}))))++ (testing "chain graph - reverses direction"+ (is (= {:a [:b], :b [:c], :c [:d]}+ (ws.dag/reverse-graph {:b [:a], :c [:b], :d [:c]}))))++ (testing "diamond graph - a -> b -> d, a -> c -> d"+ (is (= {:a [:b :c], :b [:d], :c [:d]}+ (ws.dag/reverse-graph {:b [:a], :c [:a], :d [:b :c]}))))++ (testing "multiple parents become multiple children - x has parents [a b c] => a, b, c each get child x"+ (is (= {:a [:x], :b [:x], :c [:x]}+ (ws.dag/reverse-graph {:x [:a :b :c]})))))++(deftest bfs-reduce-test+ (testing "empty adjacency returns empty"+ (is (= [] (ws.dag/bfs-reduce {} [:a]))))++ (testing "no neighbors returns empty"+ (is (= [] (ws.dag/bfs-reduce {:a []} [:a]))))++ (testing "single hop"+ (is (= [:b :c]+ (ws.dag/bfs-reduce {:a [:b :c]} [:a]))))++ (testing "chain traversal - collects all reachable nodes"+ (let [graph {:a [:b], :b [:c], :c [:d], :d []}]+ (is (= [:b :c :d] (ws.dag/bfs-reduce graph [:a])))+ (is (= [:c :d] (ws.dag/bfs-reduce graph [:b])))+ (is (= [:d] (ws.dag/bfs-reduce graph [:c])))+ (is (= [] (ws.dag/bfs-reduce graph [:d])))))++ (testing "diamond graph - no duplicates (a -> b -> d, a -> c -> d)"+ (let [graph {:a [:b :c], :b [:d], :c [:d], :d []}]+ (is (= [:b :c :d] (ws.dag/bfs-reduce graph [:a])))))++ (testing "works with map nodes (like workspace transform nodes)"+ (let [tx1 {:node-type :workspace-transform :id "tx1"}+ tx2 {:node-type :workspace-transform :id "tx2"}+ tx3 {:node-type :workspace-transform :id "tx3"}+ tbl {:node-type :table :id {:db 1 :schema "public" :table "foo"}}+ graph {tx1 [tx2 tbl], tx2 [tx3], tx3 [], tbl []}]+ (is (= [tx2 tbl tx3] (ws.dag/bfs-reduce graph [tx1])))+ (is (= [tx3] (ws.dag/bfs-reduce graph [tx2])))))++ (testing "handles cycles gracefully - a -> b -> c -> a (cycle), should not infinite loop"+ (let [graph {:a [:b], :b [:c], :c [:a]}]+ (is (= [:b :c :a] (ws.dag/bfs-reduce graph [:a])))))++ (testing "include-start? option"+ (let [graph {:a [:b], :b [:c], :c []}]+ (is (= [:a :b :c] (ws.dag/bfs-reduce graph [:a] :include-start? true)))+ (is (= [:b :c] (ws.dag/bfs-reduce graph [:a] :include-start? false)))))++ (testing "multiple start nodes"+ (let [graph {:a [:x], :b [:y], :x [], :y []}]+ (is (= [:x :y] (ws.dag/bfs-reduce graph [:a :b])))+ (is (= [:a :b :x :y] (ws.dag/bfs-reduce graph [:a :b] :include-start? true)))))++ (testing "traverses through different node types"+ (let [tx1 {:node-type :workspace-transform :id "tx1"}+ tx2 {:node-type :workspace-transform :id "tx2"}+ ext {:node-type :external-transform :id "ext"}+ graph {tx1 [ext], ext [tx2], tx2 []}]+ (is (= [ext tx2] (ws.dag/bfs-reduce graph [tx1])))))++ (testing "custom :init collects into a set"+ (let [graph {:a [:b :c], :b [:d], :c [:d], :d []}]+ (is (= #{:b :c :d} (ws.dag/bfs-reduce graph [:a] :init #{})))+ (is (= #{:a :b :c :d} (ws.dag/bfs-reduce graph [:a] :init #{} :include-start? true)))))++ (testing "custom :rf filters during traversal"+ (let [tx1 {:node-type :workspace-transform :id "t1"}+ tx2 {:node-type :workspace-transform :id "t2"}+ tbl {:node-type :table :id "tbl"}+ graph {tx1 [tx2 tbl], tx2 [], tbl []}+ xf (keep #(when (= :workspace-transform (:node-type %)) (:id %)))]+ (is (= ["t2"] (ws.dag/bfs-reduce graph [tx1] :rf (xf conj)))))))++(deftest collapse-test+ (is (= {:x1 [:x2 :x3]+ :x2 [:t3]+ :x3 [:x5]+ :x4 []+ :x5 []}+ (#'ws.dag/collapse+ ws.tu/mock-table?+ {:x1 [:t1 :t2]+ :t1 [:x2]+ :t2 [:x3]+ :x2 [:t3]+ :x3 [:t4]+ :t4 [:x5]+ :t5 [:x4]+ :x4 []+ :x5 []}))))++(defn tx->table [kw]+ (when (ws.tu/transform? kw)+ (keyword (str "t" (ws.tu/kw->id kw)))))++(defn- solve-in-memory [init-nodes graph]+ (let [tx-nodes (filter ws.tu/transform? init-nodes)+ tables (map tx->table tx-nodes)]+ (#'ws.dag/path-induced-subgraph*+ ;; Include all changeset targets in the init-nodes+ (distinct (into init-nodes tables))+ {:node-parents (dag-abstract/expand-shorthand graph)+ :table? ws.tu/mock-table?+ :table-sort ws.tu/kw->id+ :unwrap-table identity})))++(defn- chain->deps [chain]+ (reduce+ (fn [deps [from to]]+ (assoc deps from [to]))+ {}+ (partition 2 1 (reverse chain))))++(deftest in-memory-path-induced-subgraph-test+ (testing "singleton"+ (is (= {:inputs [:t1]+ :outputs [:t2]+ :entities [:x2]+ :dependencies {:x2 [:t1]}}+ (solve-in-memory [:x2] {:x2 [:t1]}))))++ (testing "encloses middle of a chain"+ (is (= {:inputs [:t1]+ :outputs [:t2 :t3 :t4]+ :entities [:x2 :x3 :x4]+ :dependencies {:x2 [:t1]+ :x3 [:x2]+ :x4 [:x3]}}+ (solve-in-memory [:x2 :x4] (chain->deps [:x1 :x2 :x3 :x4 :x5])))))++ (testing "larger graph"+ (is (= {:inputs [:t1 :t2 :t5 :t9]+ :outputs [:t3 :t4 :t11]+ :entities [:m10 :x11 :m12 :m13 :x3 :x4 :m6]+ :dependencies {:m10 [:t9]+ :m12 [:x11]+ :m13 [:m12 :x11]+ :m6 [:x4 :t5]+ :x11 [:m10]+ :x3 [:t2 :t1]+ :x4 [:x3]}}+ (solve-in-memory+ [:x3 :m6 :m10 :m13]+ {:x3 [:x1 :t2]+ :x4 [:x3]+ :m6 [:x4 :t5]+ :m10 [:t9]+ :x11 [:m10]+ :m12 [:x11]+ :m13 [:x11 :m12]})))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/dependencies_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/dependencies_test.cljnew file mode 100644index 00000000000..4102e1b616e--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/dependencies_test.clj@@ -0,0 +1,268 @@+(ns metabase-enterprise.workspaces.dependencies-test+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.workspaces.dependencies :as ws.deps]+ [metabase-enterprise.workspaces.impl :as ws.impl]+ [metabase-enterprise.workspaces.test-util :as ws.tu]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(ws.tu/ws-fixtures!)++;;; ---------------------------------------- analyze-entity tests ----------------------------------------++(deftest ^:parallel analyze-entity-query-transform-test+ (testing "analyze-entity extracts dependencies from a query transform"+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ transform {:source {:type "query"+ :query query}+ :target {:database (mt/id)+ :schema "public"+ :name "orders_with_products"}}+ result (ws.deps/analyze-entity :transform transform)]+ (testing "output is extracted from target"+ (is (= {:db_id (mt/id)+ :schema "public"+ :table "orders_with_products"}+ (:output result))))+ (testing "inputs contain the source table with table_id (since table exists)"+ (let [inputs (:inputs result)+ table-names (set (map :table inputs))]+ (is (= 1 (count inputs)))+ (is (contains? table-names (mt/format-name :orders)))+ ;; MBQL queries have table_id since the tables must exist+ (is (every? #(and (:db_id %)+ (:table %)+ (:table_id %))+ inputs)))))))++(deftest ^:parallel analyze-entity-native-query-existing-table-test+ (testing "analyze-entity extracts dependencies from native query referencing existing table"+ (let [mp (mt/metadata-provider)+ query (lib/native-query mp "SELECT * FROM ORDERS WHERE total > 100")+ transform {:source {:type "query"+ :query query}+ :target {:database (mt/id)+ :schema "analytics"+ :name "high_value_orders"}}+ result (ws.deps/analyze-entity :transform transform)]+ (testing "output is extracted from target"+ (is (= {:db_id (mt/id)+ :schema "analytics"+ :table "high_value_orders"}+ (:output result))))+ (testing "inputs include table_id since ORDERS table exists"+ (let [inputs (:inputs result)]+ ;; Native query parsing should detect ORDERS table reference and look up table_id+ (is (pos? (count inputs)))+ (is (every? #(and (:db_id %)+ (:table %)+ (:table_id %))+ inputs)))))))++(deftest ^:parallel analyze-entity-native-query-nonexistent-table-test+ (testing "analyze-entity handles native query referencing non-existent table"+ (let [mp (mt/metadata-provider)+ ;; Reference a table that doesn't exist in the database+ query (lib/native-query mp "SELECT * FROM future_sales_data WHERE region = 'US'")+ transform {:source {:type "query"+ :query query}+ :target {:database (mt/id)+ :schema "analytics"+ :name "regional_sales"}}+ result (ws.deps/analyze-entity :transform transform)]+ (testing "output is extracted from target"+ (is (= {:db_id (mt/id)+ :schema "analytics"+ :table "regional_sales"}+ (:output result))))+ (testing "inputs have logical reference but no table_id (table doesn't exist)"+ (let [inputs (:inputs result)]+ ;; Should still parse the table reference even though table doesn't exist+ (is (pos? (count inputs)))+ ;; Has db_id and table name but NOT table_id+ (is (every? #(and (:db_id %)+ (:table %))+ inputs))+ ;; Verify table_id is nil or missing for non-existent tables+ (is (every? #(nil? (:table_id %)) inputs)))))))++(deftest ^:parallel analyze-entity-python-transform-test+ (testing "analyze-entity extracts dependencies from a python transform"+ (let [transform {:source {:type "python"+ :source-tables {"orders" (mt/id :orders)+ "products" (mt/id :products)}+ :body "def transform(orders, products): return orders"}+ :target {:database (mt/id)+ :schema "public"+ :name "python_result"}}+ result (ws.deps/analyze-entity :transform transform)]+ (testing "output is extracted from target"+ (is (= {:db_id (mt/id)+ :schema "public"+ :table "python_result"}+ (:output result))))+ (testing "inputs contain the source tables with table_ids"+ (let [inputs (:inputs result)+ table-names (set (map :table inputs))]+ (is (= 2 (count inputs)))+ (is (contains? table-names (mt/format-name :orders)))+ (is (contains? table-names (mt/format-name :products)))+ ;; Python transforms have table_ids since source-tables maps to existing table IDs+ (is (every? :table_id inputs)))))))++(deftest analyze-entity-asserts-transform-type-test+ (testing "analyze-entity asserts entity-type is :transform"+ (is (thrown-with-msg? Exception #"Only transform entity type is supported"+ (ws.deps/analyze-entity :card {})))))++;;; ---------------------------------------- write-analysis! tests ----------------------------------------++(defn- write-analysis! [workspace-id isolated-schema entity-type ref-id analysis]+ ;; Always do new analysis+ (t2/query {:update :workspace_transform+ :set {:analysis_version [:+ :analysis_version 1]}+ :where [:and [:= :workspace_id workspace-id] [:= :ref_id ref-id]]})+ (ws.deps/write-entity-analysis! workspace-id isolated-schema entity-type ref-id analysis+ (case entity-type+ :transform (t2/select-one-fn :analysis_version+ [:model/WorkspaceTransform :analysis_version]+ :workspace_id workspace-id+ :ref_id ref-id))))++(deftest write-dependencies-creates-output-test+ (testing "write-dependencies! creates workspace_output record"+ (mt/with-temp [:model/Workspace workspace {:name (ws.tu/unique-name)+ :database_id (mt/id)+ :schema "test_isolated_schema"}+ :model/WorkspaceTransform wt {:workspace_id (:id workspace)+ :name "Test Transform"+ :source {:type "query" :query {}}+ :target {:database (mt/id)+ :schema "public"+ :name "test_output"}}]+ (let [analysis {:output {:db_id (mt/id)+ :schema "public"+ :table "test_output"}+ :inputs []}]+ (write-analysis! (:id workspace) "test_isolated_schema" :transform (:ref_id wt) analysis)+ (let [output (t2/select-one :model/WorkspaceOutput+ :workspace_id (:id workspace)+ :ref_id (:ref_id wt))]+ (is (some? output))+ (is (= (mt/id) (:db_id output)))+ (is (= "public" (:global_schema output)))+ (is (= "test_output" (:global_table output)))+ (is (= "test_isolated_schema" (:isolated_schema output)))+ (is (= "public__test_output" (:isolated_table output))))))))++(deftest write-dependencies-creates-inputs-test+ (testing "write-dependencies! creates workspace_input records for external dependencies"+ (let [iso-schema "test_isolated_schema"]+ (mt/with-temp [:model/Workspace workspace {:name (ws.tu/unique-name)+ :database_id (mt/id)+ :schema iso-schema}+ :model/WorkspaceTransform wt {:workspace_id (:id workspace)+ :name "Test Transform"+ :source {:type "query" :query {}}+ :target {:database (mt/id)+ :schema "public"+ :name "test_output"}}]+ (let [orders-table (t2/select-one [:model/Table :db_id :schema :name] :id (mt/id :orders))+ analysis {:output {:db_id (mt/id)+ :schema "public"+ :table "test_output"}+ :inputs [{:db_id (:db_id orders-table)+ :schema (:schema orders-table)+ :table (:name orders-table)}]}]+ (write-analysis! (:id workspace) iso-schema :transform (:ref_id wt) analysis)+ (let [input (t2/select-one :model/WorkspaceInput+ :workspace_id (:id workspace)+ :table (:name orders-table))]+ (is (some? input))+ (is (= (:db_id orders-table) (:db_id input)))+ (is (= (:name orders-table) (:table input)))+ (testing "input has ref_id linking to transform"+ (is (= (:ref_id wt) (:ref_id input))))))))))++(deftest write-dependencies-updates-on-change-test+ (testing "write-dependencies! replaces inputs when dependencies change"+ (mt/with-temp [:model/Workspace workspace {:name (ws.tu/unique-name)+ :database_id (mt/id)+ :schema "test_isolated_schema"}+ :model/WorkspaceTransform wt {:workspace_id (:id workspace)+ :name "Test Transform"+ :source {:type "query" :query {}}+ :target {:database (mt/id)+ :schema "public"+ :name "test_output"}}]+ (let [orders-table (t2/select-one [:model/Table :db_id :schema :name] :id (mt/id :orders))+ products-table (t2/select-one [:model/Table :db_id :schema :name] :id (mt/id :products))]+ ;; First write with orders dependency+ (write-analysis! (:id workspace) "test_isolated_schema" :transform (:ref_id wt)+ {:output {:db_id (mt/id)+ :schema "public"+ :table "test_output"}+ :inputs [{:db_id (:db_id orders-table)+ :schema (:schema orders-table)+ :table (:name orders-table)}]})+ (is (= 1 (t2/count :model/WorkspaceInput :workspace_id (:id workspace) :ref_id (:ref_id wt))))++ ;; Update to depend on products instead+ (write-analysis! (:id workspace) "test_isolated_schema" :transform (:ref_id wt)+ {:output {:db_id (mt/id)+ :schema "public"+ :table "test_output"}+ :inputs [{:db_id (:db_id products-table)+ :schema (:schema products-table)+ :table (:name products-table)}]})++ ;; Trigger clean-up+ (#'ws.impl/cleanup-old-transform-versions! (:id workspace) (:ref_id wt))++ (testing "old input is removed, new input exists"+ (is (= 1 (t2/count :model/WorkspaceInput :workspace_id (:id workspace) :ref_id (:ref_id wt))))+ (is (t2/exists? :model/WorkspaceInput+ :workspace_id (:id workspace)+ :ref_id (:ref_id wt)+ :table (:name products-table)))+ (is (not (t2/exists? :model/WorkspaceInput+ :workspace_id (:id workspace)+ :ref_id (:ref_id wt)+ :table (:name orders-table)))))))))++;;; ---------------------------------------- Integration test ----------------------------------------++(deftest analyze-and-write-integration-test+ (testing "Full flow: analyze a real transform and write its dependencies"+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]+ (mt/with-temp [:model/Workspace workspace {:name "Integration Test Workspace"+ :database_id (mt/id)+ :schema "test_isolated_schema"}+ :model/WorkspaceTransform wt {:workspace_id (:id workspace)+ :name "Orders Analysis"+ :source {:type "query" :query {}}+ :target {:database (mt/id)+ :schema "analytics"+ :name "orders_analysis"}}]+ ;; Create the transform entity with the proper query (not from DB)+ (let [transform-entity {:source {:type "query"+ :query query}+ :target {:database (mt/id)+ :schema "analytics"+ :name "orders_analysis"}}+ analysis (ws.deps/analyze-entity :transform transform-entity)]+ (write-analysis! (:id workspace) "test_isolated_schema" :transform (:ref_id wt) analysis)++ (testing "output record created"+ (is (= 1 (t2/count :model/WorkspaceOutput :workspace_id (:id workspace)))))+ (testing "input record created for ORDERS table with ref_id"+ (is (= 1 (t2/count :model/WorkspaceInput :workspace_id (:id workspace))))+ (let [input (t2/select-one :model/WorkspaceInput :workspace_id (:id workspace))]+ (is (= (:ref_id wt) (:ref_id input))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/drivers_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/drivers_test.cljnew file mode 100644index 00000000000..fafef33b444--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/drivers_test.clj@@ -0,0 +1,20 @@+(ns ^:mb/driver-tests metabase-enterprise.workspaces.drivers-test+ (:require+ [clojure.test :refer :all]+ [metabase.driver :as driver]+ [metabase.test :as mt]))++(deftest drivers-support-workspaces-has-to-support-db-swapping-test+ (mt/test-drivers (mt/normal-drivers-with-feature :workspace)+ (let [db-swapped? (fn []+ (try+ (mt/run-mbql-query venues {:limit 1})+ false+ (catch Exception _e+ true)))]+ (testing "db is swapped if being executed inside a macros"+ (driver/with-swapped-connection-details (mt/id) {:user "unicorn"}+ (is (db-swapped?))))++ (testing "sanity check that it's not swapped outside of the macros"+ (is (not (db-swapped?)))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.cljnew file mode 100644index 00000000000..8e1100a6594--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/e2e_test.clj@@ -0,0 +1,65 @@+(ns ^:mb/driver-tests metabase-enterprise.workspaces.e2e-test+ (:require+ [clojure.string :as str]+ [clojure.test :refer :all]+ [metabase-enterprise.transforms.test-util :as transforms.tu]+ [metabase-enterprise.workspaces.common :as ws.common]+ [metabase-enterprise.workspaces.impl :as ws.impl]+ [metabase-enterprise.workspaces.test-util :as ws.tu]+ [metabase.sync.core :as sync]+ [metabase.test :as mt]+ [metabase.util :as u]+ [toucan2.core :as t2]))++(ws.tu/ws-fixtures!)++(deftest isolation-e2e-test+ (mt/test-drivers (mt/normal-drivers-with-feature :workspace)+ (transforms.tu/with-transform-cleanup! [output-table-name (str "test_table_1_" (mt/random-name))]+ (mt/with-model-cleanup [:model/Transform :model/Workspace]+ (mt/with-temp [:model/Transform {transform-id :id} {:name "Transform 1"+ :source {:type "query"+ :query (mt/native-query (ws.tu/mbql->native (mt/mbql-query orders {:limit 3})))}+ :target {:type "table"+ :name output-table-name+ :database (mt/id)+ :schema (t2/select-one-fn :schema :model/Table (mt/id :orders))}}]+ (ws.tu/with-workspaces! [workspace {:name (mt/random-name)+ :database_id (mt/id)}]+ (ws.common/add-to-changeset! (mt/user->id :crowberto) workspace+ :transform transform-id+ (t2/select-one :model/Transform transform-id))+ (u/poll {:thunk #(t2/select-one :model/Workspace (:id workspace))+ :done? #(= :ready (:db_status %))+ :timeout-ms 5000})+ (let [workspace (t2/select-one :model/Workspace (:id workspace))+ graph (ws.impl/get-or-calculate-graph! workspace)+ isolated-transform (t2/select-one :model/WorkspaceTransform :workspace_id (:id workspace))+ executed-transform (mt/with-current-user (mt/user->id :crowberto)+ ;; trigger analysis, so we get the RO grants+ (ws.impl/analyze-transform-if-stale! workspace isolated-transform)+ (ws.impl/run-transform! workspace graph isolated-transform))+ output-table (-> executed-transform :table)+ schema (:schema output-table)+ table-name (:name output-table)]+ (testing "execute the transform with the original query is fine and the output is in an isolated schema"+ (when (u/poll {:thunk #(t2/select-one :model/Table :name table-name :schema schema)+ :done? some?+ :timeout-ms 1000})+ ;; TODO (Ngoc 2026-01-21) -- the table is synced but there are no fields even though the table and fields exist in the db+ ;; ... even force sync-ing it again doesn't seem to help ;_;+ (sync/sync-table! (t2/select-one :model/Table :name table-name :schema schema))+ #_(transforms.tu/wait-for-table (:name output-table) 1000))+ (is (str/starts-with? (:schema output-table) "mb__isolation"))+ #_(is (= 1 (count (transforms.tu/table-rows table-name)))))++ (testing "changing the query without granting access will fail"+ (t2/update! :model/WorkspaceTransform+ {:workspace_id (:id workspace) :ref_id (:ref_id isolated-transform)}+ {:source {:type "query"+ :query (mt/native-query (ws.tu/mbql->native (mt/mbql-query venues {:limit 1})))}})+ (let [ref-id (:ref_id isolated-transform)+ transform (t2/select-one :model/WorkspaceTransform :workspace_id (:id workspace) :ref_id ref-id)]+ (is (=? {:status :failed}+ (mt/with-current-user (mt/user->id :crowberto)+ (ws.impl/run-transform! workspace graph transform)))))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/execute_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/execute_test.cljnew file mode 100644index 00000000000..3c634d16fe4--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/execute_test.clj@@ -0,0 +1,342 @@+(ns ^:mb/driver-tests metabase-enterprise.workspaces.execute-test+ (:require+ [clojure.string :as str]+ [clojure.test :refer :all]+ [metabase-enterprise.transforms.test-util :as transforms.tu]+ [metabase-enterprise.workspaces.common :as ws.common]+ [metabase-enterprise.workspaces.execute :as ws.execute]+ [metabase-enterprise.workspaces.impl :as ws.impl]+ [metabase-enterprise.workspaces.test-util :as ws.tu]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(ws.tu/ws-fixtures!)++(deftest run-workspace-transform-no-input-test+ (testing "Executing a workspace transform returns results and rolls back app DB records"+ (transforms.tu/with-transform-cleanup! [output-table "ws_execute_test"]+ (let [workspace (ws.tu/create-ready-ws! "Execute Test Workspace")+ db-id (:database_id workspace)+ body {:name "Test Transform"+ :source {:type "query"+ :query (mt/native-query {:query "SELECT 1 as id, 'hello' as name"})}+ :target {:type "table"+ :database db-id+ :schema nil+ :name output-table}}+ ws-transform (ws.common/add-to-changeset! (mt/user->id :crowberto) workspace :transform nil body)+ ;; get initialized fields+ workspace (t2/select-one :model/Workspace (:id workspace))+ graph (ws.impl/get-or-calculate-graph! workspace)+ ws-schema (t2/select-one-fn :schema :model/Workspace (:id workspace))+ before {:xf (t2/count :model/Transform)+ :xfrun (t2/count :model/TransformRun)}]+ (testing "execution returns expected result structure"+ (is (=? {:status :succeeded+ :start_time some?+ :end_time some?+ :table {:name #(str/includes? % output-table)+ :schema ws-schema}}+ (mt/with-current-user (mt/user->id :crowberto)+ (ws.impl/run-transform! workspace graph ws-transform))))+ (is (=? {:last_run_at some?}+ (t2/select-one :model/WorkspaceTransform :workspace_id (:id workspace) :ref_id (:ref_id ws-transform))))++ (testing "The isolated_table_id gets populated"+ (ws.impl/get-or-calculate-graph! workspace)+ (is (=? [{:workspace_id (:id workspace)+ :global_table output-table+ :global_schema nil+ :global_table_id nil+ :isolated_schema string?+ :isolated_table string?+ :isolated_table_id number?}]+ (t2/select :model/WorkspaceOutput :workspace_id (:id workspace) :ref_id (:ref_id ws-transform))))))++ (testing "app DB records are rolled back"+ ;; TransformRun is +1 because cascade delete was removed in d1e940e66b5+ (is (= (update before :xfrun inc)+ {:xf (t2/count :model/Transform)+ :xfrun (t2/count :model/TransformRun)})))))))++(deftest dry-run-workspace-transform-test+ (testing "Dry-running a workspace transform returns rows without persisting"+ (let [workspace (ws.tu/create-ready-ws! "Dry-Run Test Workspace")+ db-id (:database_id workspace)+ body {:name "Dry-Run Transform"+ :source {:type "query"+ :query (mt/native-query {:query "SELECT 1 as id, 'hello' as name UNION ALL SELECT 2, 'world'"})}+ :target {:type "table"+ :database db-id+ :schema nil+ :name "ws_dryrun_test"}}+ ws-transform (ws.common/add-to-changeset! (mt/user->id :crowberto) workspace :transform nil body)+ graph (ws.impl/get-or-calculate-graph! workspace)+ before {:xf (t2/count :model/Transform)+ :xfrun (t2/count :model/TransformRun)}]++ (testing "dry-run returns data nested under :data like /api/dataset"+ (is (=? {:status :succeeded+ :data {:rows [[1 "hello"] [2 "world"]]+ :cols [{:name #"(ID)|(id)"} {:name #"(NAME)|(name)"}]}}+ (mt/with-current-user (mt/user->id :crowberto)+ (ws.impl/dry-run-transform workspace graph ws-transform)))))++ (testing "last_run_at is NOT updated in dry-run mode"+ (is (nil? (:last_run_at (t2/select-one :model/WorkspaceTransform :workspace_id (:id workspace) :ref_id (:ref_id ws-transform))))))++ (testing "app DB records are NOT created in dry-run mode"+ (is (= before+ {:xf (t2/count :model/Transform)+ :xfrun (t2/count :model/TransformRun)}))))))++(deftest ^:mb/transforms-python-test dry-run-python-workspace-transform-test+ (testing "Dry-running a Python workspace transform returns rows without persisting"+ (mt/test-drivers #{:mysql :postgres}+ (let [workspace (ws.tu/create-ready-ws! "Python Dry-Run Test")+ db-id (:database_id workspace)+ body {:name "Python Dry-Run Transform"+ :source {:type "python"+ :source-tables {}+ :body (str "import pandas as pd\n"+ "\n"+ "def transform():\n"+ " return pd.DataFrame({'id': [1, 2], 'name': ['hello', 'world']})")}+ :target {:type "table"+ :database db-id+ :schema nil+ :name "ws_python_dryrun_test"}}+ ws-transform (ws.common/add-to-changeset! (mt/user->id :crowberto) workspace :transform nil body)+ graph (ws.impl/get-or-calculate-graph! workspace)+ before {:xf (t2/count :model/Transform)+ :xfrun (t2/count :model/TransformRun)}]++ (testing "dry-run returns data with rows from Python output"+ (is (=? {:status :succeeded+ :data {:rows [[1 "hello"] [2 "world"]]+ :cols [{:name "id"} {:name "name"}]}}+ (mt/with-current-user (mt/user->id :crowberto)+ (ws.impl/dry-run-transform workspace graph ws-transform)))))++ (testing "last_run_at is NOT updated in dry-run mode"+ (is (nil? (:last_run_at (t2/select-one :model/WorkspaceTransform :workspace_id (:id workspace) :ref_id (:ref_id ws-transform))))))++ (testing "app DB records are NOT created in dry-run mode"+ (is (= before+ {:xf (t2/count :model/Transform)+ :xfrun (t2/count :model/TransformRun)})))))))++(deftest remap-python-source-test+ (let [remap-python-source #'ws.execute/remap-python-source]+ (testing "remaps to table ID when mapping has :id"+ (let [table-mapping {[1 "public" "orders"] {:db-id 1+ :schema "ws_isolated_123"+ :table "orders_isolated"+ :id 456}}+ source {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" {:database_id 1+ :schema "public"+ :table "orders"+ :table_id 123}}}]+ (is (= {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" 456}}+ (remap-python-source table-mapping source)))))++ (testing "remaps to map format when mapping has nil :id"+ (let [table-mapping {[1 "public" "orders"] {:db-id 1+ :schema "ws_isolated_123"+ :table "orders_isolated"+ :id nil}}+ source {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" {:database_id 1+ :schema "public"+ :table "orders"+ :table_id 123}}}]+ (is (= {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" {:database_id 1+ :schema "ws_isolated_123"+ :table "orders_isolated"}}}+ (remap-python-source table-mapping source)))))++ (testing "leaves source-tables unchanged when no mapping exists"+ (let [table-mapping {}+ source {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" {:database_id 1+ :schema "public"+ :table "orders"+ :table_id 123}}}]+ (is (= source (remap-python-source table-mapping source)))))++ (testing "remaps legacy integer to table ID when mapping has :id"+ (let [table-mapping {123 {:db-id 1+ :schema "ws_isolated_123"+ :table "orders_isolated"+ :id 456}}+ source {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" 123}}]+ (is (= {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" 456}}+ (remap-python-source table-mapping source)))))++ (testing "remaps legacy integer to map format when mapping has nil :id"+ (let [table-mapping {123 {:db-id 1+ :schema "ws_isolated_123"+ :table "orders_isolated"+ :id nil}}+ source {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" 123}}]+ (is (= {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" {:database_id 1+ :schema "ws_isolated_123"+ :table "orders_isolated"}}}+ (remap-python-source table-mapping source)))))++ (testing "leaves legacy integer table-id unchanged when no mapping exists"+ (let [table-mapping {}+ source {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" 123}}]+ (is (= source (remap-python-source table-mapping source)))))++ (testing "handles mixed formats with mappings for both"+ (let [table-mapping {[1 "public" "orders"] {:db-id 1+ :schema "ws_isolated_123"+ :table "orders_isolated"+ :id 456}+ 789 {:db-id 1+ :schema "ws_isolated_123"+ :table "customers_isolated"+ :id 999}}+ source {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" {:database_id 1+ :schema "public"+ :table "orders"+ :table_id 123}+ "customers" 789}}]+ (is (= {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" 456+ "customers" 999}}+ (remap-python-source table-mapping source)))))++ (testing "prefers table_id lookup over triple lookup for map format"+ (let [table-mapping {123 {:db-id 1+ :schema "ws_isolated_123"+ :table "orders_by_id"+ :id 456}+ [1 "public" "orders"] {:db-id 1+ :schema "ws_isolated_123"+ :table "orders_by_triple"+ :id 789}}+ source {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" {:database_id 1+ :schema "public"+ :table "orders"+ :table_id 123}}}]+ (is (= {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" 456}}+ (remap-python-source table-mapping source)))))++ (testing "falls back to triple lookup when table_id not in mapping"+ (let [table-mapping {[1 "public" "orders"] {:db-id 1+ :schema "ws_isolated_123"+ :table "orders_isolated"+ :id 456}}+ source {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" {:database_id 1+ :schema "public"+ :table "orders"+ :table_id 999}}}]+ (is (= {:type "python"+ :body "import pandas as pd"+ :source-tables {"orders" 456}}+ (remap-python-source table-mapping source)))))))++(deftest remap-sql-source-test+ (let [remap-sql-source #'ws.execute/remap-sql-source+ make-source (fn [sql] {:type "query", :query {:database 1, :type :native, :stages [{:native sql}]}})]+ ;; Test cases as [label, table-mapping, input-sql, expected-sql]+ (doseq [[label table-mapping input-sql expected-sql]+ [["remaps qualified table reference to isolated table"+ {[1 "public" "orders"] {:db-id 1, :schema "ws_isolated_123", :table "public__orders", :id 456}}+ "SELECT * FROM public.orders"+ "SELECT * FROM ws_isolated_123.public__orders"]++ ["remaps unqualified table reference using nil-schema mapping"+ {[1 nil "orders"] {:db-id 1, :schema "ws_isolated_123", :table "public__orders", :id 456}}+ "SELECT * FROM orders"+ "SELECT * FROM ws_isolated_123.public__orders"]++ ;; This is what build-remapping creates for unqualified input tables+ ["qualifies unqualified input table reference (no isolation, just adds schema)"+ {[1 nil "orders"] {:db-id 1, :schema "public", :table "orders", :id 123}}+ "SELECT * FROM orders"+ "SELECT * FROM public.orders"]++ ["leaves unmapped tables unchanged"+ {[1 nil "other_table"] {:db-id 1, :schema "public", :table "other_table", :id 999}}+ "SELECT * FROM orders"+ "SELECT * FROM orders"]++ ;; Note: macaw only renames tables in FROM/JOIN clauses, not table qualifiers in column references.+ ;; The output SQL is semantically valid since `orders` resolves to `public.orders` in scope.+ ["handles multiple tables in same query"+ {[1 nil "orders"] {:db-id 1, :schema "public", :table "orders", :id 123}+ [1 nil "products"] {:db-id 1, :schema "public", :table "products", :id 456}}+ "SELECT * FROM orders JOIN products ON orders.product_id = products.id"+ "SELECT * FROM public.orders JOIN public.products ON orders.product_id = products.id"]]]+ (testing label+ (is (= (make-source expected-sql)+ (remap-sql-source table-mapping (make-source input-sql))))))))++(deftest run-transform-marks-not-stale-on-success+ (testing "Successful transform run marks definition_changed=false"+ (ws.tu/with-resources! [{:keys [workspace-id workspace-map]}+ {:workspace {:definitions {:x1 [:t0]}+ :properties {:x1 {:definition_changed true :input_data_changed false}}}}]+ (let [t1-ref (workspace-map :x1)]+ (testing "initially stale"+ (is (= {t1-ref {:definition_changed true :input_data_changed false}}+ (ws.tu/staleness-flags workspace-id))))++ (ws.tu/mock-run-transform! workspace-id t1-ref)++ (testing "after run: definition_changed cleared"+ (is (= {t1-ref {:definition_changed false :input_data_changed false}}+ (ws.tu/staleness-flags workspace-id))))))))++(deftest transform-stale-lifecycle+ (testing "Transform stale lifecycle: create -> run (not stale) -> update (stale)"+ (ws.tu/with-resources! [{:keys [workspace-id workspace-map]}+ {:workspace {:definitions {:x1 [:t0]}+ :properties {:x1 {:definition_changed true :input_data_changed false}}}}]+ (let [t1-ref (workspace-map :x1)]+ (testing "sanity check that it's stale to start with"+ (is (= {t1-ref {:definition_changed true :input_data_changed false}}+ (ws.tu/staleness-flags workspace-id))))++ (testing "Run (mocked): should mark definition_changed as false"+ (ws.tu/mock-run-transform! workspace-id t1-ref)+ (is (= {t1-ref {:definition_changed false :input_data_changed false}}+ (ws.tu/staleness-flags workspace-id))))++ (testing "Update source: should mark definition_changed as true"+ (t2/update! :model/WorkspaceTransform {:workspace_id workspace-id :ref_id t1-ref}+ {:source {:type "query" :query (mt/native-query {:query "SELECT 2 as id, 'world' as name"})}})+ (is (= {t1-ref {:definition_changed true :input_data_changed false}}+ (ws.tu/staleness-flags workspace-id))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/impl_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/impl_test.cljnew file mode 100644index 00000000000..e78ca914381--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/impl_test.clj@@ -0,0 +1,479 @@+(ns metabase-enterprise.workspaces.impl-test+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.workspaces.common :as ws.common]+ [metabase-enterprise.workspaces.impl :as ws.impl]+ [metabase-enterprise.workspaces.isolation :as ws.isolation]+ [metabase-enterprise.workspaces.test-util :as ws.tu]+ [metabase.driver.sql :as driver.sql]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(ws.tu/ws-fixtures!)++(deftest sync-transform-dependencies-test+ (testing "sync-transform-dependencies! writes dependencies and grants access to external inputs"+ (let [mp (mt/metadata-provider)+ query-a (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ query-b (lib/query mp (lib.metadata/table mp (mt/id :products)))+ workspace (ws.tu/create-empty-ws! "Test Workspace")+ grant-calls (atom [])]+ ;; Set up mock BEFORE calling add-to-changeset! since it calls sync-transform-dependencies! internally+ (mt/with-dynamic-fn-redefs [ws.isolation/grant-read-access-to-tables!+ (fn [_database _workspace tables]+ (swap! grant-calls conj (set (map :name tables))))]+ (testing "Transform A depends on ORDERS"+ (ws.common/add-to-changeset! (mt/user->id :crowberto) workspace+ :transform nil+ {:name "Transform A"+ :source {:type "query" :query query-a}+ :target {:database (mt/id)+ :schema "analytics"+ :name "table_a"}})+ (ws.tu/analyze-workspace! (:id workspace))+ (testing "creates output and input records"+ (is (= 1 (t2/count :model/WorkspaceOutput :workspace_id (:id workspace))))+ (is (= 1 (t2/count :model/WorkspaceInput :workspace_id (:id workspace)))))+ (testing "input record has correct table name"+ (is (= (mt/format-name :orders)+ (t2/select-one-fn :table :model/WorkspaceInput :workspace_id (:id workspace)))))+ (testing "grants access to ORDERS"+ (is (= 1 (count @grant-calls)))+ (is (contains? (first @grant-calls) (mt/format-name :orders)))))++ (testing "Transform B depends on PRODUCTS, table_a is internal output"+ (reset! grant-calls [])+ (ws.common/add-to-changeset! (mt/user->id :crowberto) workspace+ :transform nil+ {:name "Transform B"+ :source {:type "query" :query query-b}+ :target {:database (mt/id)+ :schema "analytics"+ :name "table_b"}})+ (ws.tu/analyze-workspace! (:id workspace))+ (testing "creates records for both transforms"+ (is (= 2 (t2/count :model/WorkspaceOutput :workspace_id (:id workspace))))+ (is (= 2 (t2/count :model/WorkspaceInput :workspace_id (:id workspace)))))+ (testing "only grants access to newly discovered inputs (PRODUCTS), not already-granted ones"+ (is (= 1 (count @grant-calls)))+ ;; ORDERS was already granted in Transform A, so only PRODUCTS is in this grant call+ (is (not (contains? (first @grant-calls) (mt/format-name :orders))))+ (is (contains? (first @grant-calls) (mt/format-name :products)))+ (is (not (contains? (first @grant-calls) "table_a")))))))))++(deftest sync-transform-dependencies-idempotent-test+ (testing "sync-transform-dependencies! is idempotent"+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ ws (ws.tu/create-empty-ws! "Test Workspace")]+ (mt/with-dynamic-fn-redefs [ws.isolation/grant-read-access-to-tables! (constantly nil)]+ (let [_ (ws.common/add-to-changeset! (mt/user->id :crowberto) ws+ :transform nil+ {:name "Transform"+ :source {:type "query" :query query}+ :target {:database (mt/id)+ :schema "analytics"+ :name "output_table"}})+ ws (t2/select-one :model/Workspace (:id ws))]+ ;; Trigger analysis again to test idempotency+ (ws.impl/increment-graph-version! (:id ws))+ (ws.tu/analyze-workspace! (:id ws))+ (ws.impl/increment-graph-version! (:id ws))+ (ws.tu/analyze-workspace! (:id ws))+ (testing "still has exactly one of each after multiple syncs"+ (is (= 1 (t2/count :model/WorkspaceOutput :workspace_id (:id ws))))+ (is (= 1 (t2/count :model/WorkspaceInput :workspace_id (:id ws))))))))))++;;;; build-remapping tests++(deftest build-remapping-creates-nil-schema-entries-for-default-schema-outputs-test+ (testing "build-remapping creates [db_id nil table] entries for outputs in the default schema"+ (ws.tu/with-resources! [{:keys [workspace-id]} {:workspace {:definitions {:x1 [:t0]}}}]+ (let [db (t2/select-one [:model/Database :id :engine :details] (mt/id))+ default-schema (or (driver.sql/default-schema (:engine db))+ ((some-fn :dbname :db) (:details db)))+ ws-transform (t2/select-one :model/WorkspaceTransform :workspace_id workspace-id)]+ ;; Update the transform's target schema to the default schema before analysis,+ ;; so analysis naturally produces an output in the default schema.+ (t2/update! :model/WorkspaceTransform+ {:workspace_id workspace-id, :ref_id (:ref_id ws-transform)}+ {:target (assoc (:target ws-transform) :schema default-schema)})+ (let [workspace (t2/select-one :model/Workspace workspace-id)+ target-table (-> ws-transform :target :name)+ graph (ws.impl/get-or-calculate-graph! workspace)+ result (#'ws.impl/build-remapping workspace graph)+ ws-output (t2/select-one :model/WorkspaceOutput+ :workspace_id workspace-id+ :global_table target-table)+ isolated ((juxt :isolated_schema :isolated_table) ws-output)]+ (doseq [[desc lookup-key] [["qualified [db_id schema table]" [(mt/id) default-schema target-table]]+ ["unqualified [db_id nil table]" [(mt/id) nil target-table]]]]+ (testing (str desc " maps to the isolated table")+ (is (= isolated ((juxt :schema :table) (get (:tables result) lookup-key)))))))))))++;;;; Two-flag staleness tests++(deftest staleness-recomputed-on-graph-read+ (testing "Graph staleness reflects current DB state with two flags"+ (ws.tu/with-resources! [{:keys [workspace-id workspace-map]}+ {:workspace {:definitions {:x1 [:t0]}+ :properties {:x1 {:definition_changed true+ :input_data_changed false}}}}]+ (let [t1-ref (workspace-map :x1)+ find-t1 (fn [graph] (first (filter #(= t1-ref (:id %)) (:entities graph))))]+ (testing "initial read shows transform as definition_changed"+ (let [workspace (t2/select-one :model/Workspace workspace-id)+ graph (ws.impl/with-staleness workspace (ws.impl/get-or-calculate-graph! workspace))+ entity (find-t1 graph)]+ (is (= {:definition_changed true :input_data_changed false}+ (select-keys entity [:definition_changed :input_data_changed])))))++ (t2/update! :model/WorkspaceTransform {:workspace_id workspace-id :ref_id t1-ref}+ {:definition_changed false :input_data_changed true})++ (testing "after updating flags in DB, graph read reflects the change"+ (let [workspace (t2/select-one :model/Workspace workspace-id)+ graph (ws.impl/with-staleness workspace (ws.impl/get-or-calculate-graph! workspace))+ entity (find-t1 graph)]+ (is (= {:definition_changed false :input_data_changed true}+ (select-keys entity [:definition_changed :input_data_changed])))))))))++(deftest run-transform-marks-downstream-stale-test+ (testing "Running a transform marks all transitive downstream as input_data_changed"+ (ws.tu/with-resources! [{:keys [workspace-id workspace-map]}+ {:workspace {:definitions {:x1 [:t0] :x2 [:x1]}+ :properties {:x1 {:definition_changed true :input_data_changed false}+ :x2 {:definition_changed false :input_data_changed false}}}}]+ (let [t1-ref (workspace-map :x1)+ t2-ref (workspace-map :x2)]+ (ws.tu/mock-run-transform! workspace-id t1-ref)++ (testing "after running t1: t1 is fresh, t2 is input_data_changed"+ (is (= {t1-ref {:definition_changed false :input_data_changed false}+ t2-ref {:definition_changed false :input_data_changed true}}+ (ws.tu/staleness-flags workspace-id))))))))++(deftest run-transform-marks-transitive-downstream-test+ (testing "Running a transform marks all transitive downstream, not just direct"+ (ws.tu/with-resources! [{:keys [workspace-id workspace-map]}+ {:workspace {:definitions {:x1 [:t0] :x2 [:x1] :x3 [:x2]}+ :properties {:x1 {:definition_changed true :input_data_changed false}+ :x2 {:definition_changed false :input_data_changed false}+ :x3 {:definition_changed false :input_data_changed false}}}}]+ (let [t1-ref (workspace-map :x1)+ t2-ref (workspace-map :x2)+ t3-ref (workspace-map :x3)]+ (ws.tu/mock-run-transform! workspace-id t1-ref)++ (testing "after running t1: both t2 and t3 are input_data_changed"+ (is (= {t1-ref {:definition_changed false :input_data_changed false}+ t2-ref {:definition_changed false :input_data_changed true}+ t3-ref {:definition_changed false :input_data_changed true}}+ (ws.tu/staleness-flags workspace-id))))))))++(deftest run-transform-clears-both-flags-when-fresh-test+ (testing "Running a transform with fresh ancestor clears both flags"+ (ws.tu/with-resources! [{:keys [workspace-id workspace-map]}+ {:workspace {:definitions {:x1 [:t0] :x2 [:x1]}+ :properties {:x1 {:definition_changed false :input_data_changed false}+ :x2 {:definition_changed true :input_data_changed true}}}}]+ (let [t1-ref (workspace-map :x1)+ t2-ref (workspace-map :x2)]+ (testing "initial state: t1 is fresh, t2 has both flags set"+ (is (= {t1-ref {:definition_changed false :input_data_changed false}+ t2-ref {:definition_changed true :input_data_changed true}}+ (ws.tu/staleness-flags workspace-id))))++ (ws.tu/mock-run-transform! workspace-id t2-ref)++ (testing "after running t2 with fresh ancestor: both flags are cleared"+ (is (= {t1-ref {:definition_changed false :input_data_changed false}+ t2-ref {:definition_changed false :input_data_changed false}}+ (ws.tu/staleness-flags workspace-id))))))))++;;;; Static graph tests for annotate-staleness++(defn- workspace-transform+ "Create a workspace transform entity for testing."+ [id]+ {:node-type :workspace-transform :id id})++(defn- global-transform+ "Create a global/external transform entity for testing."+ [id]+ {:node-type :external-transform :id id})++(defn- stale->id+ "Group entity IDs by stale status: {true #{stale-ids} false #{non-stale-ids}}"+ [entities]+ (reduce (fn [acc {:keys [id stale]}]+ (update acc (boolean stale) (fnil conj #{}) id))+ {true #{} false #{}}+ entities))++(deftest annotate-staleness-linear-chain-test+ (testing "Linear chain: staleness propagates downstream"+ (let [t1 (workspace-transform "t1")+ t2 (workspace-transform "t2")+ t3 (workspace-transform "t3")+ ;; t1 -> t2 -> t3+ graph {:entities [t1 t2 t3]+ :dependencies {t2 [t1]+ t3 [t2]}}]+ (testing "stale root propagates to all descendants"+ (let [staleness {"t1" {:definition_changed true :input_data_changed false}}+ result (ws.impl/annotate-staleness graph staleness)]+ (is (= {true #{"t1" "t2" "t3"} false #{}}+ (stale->id (:entities result))))))++ (testing "stale middle only propagates downstream"+ (let [staleness {"t2" {:definition_changed true :input_data_changed false}}+ result (ws.impl/annotate-staleness graph staleness)]+ (is (= {true #{"t2" "t3"} false #{"t1"}}+ (stale->id (:entities result))))))++ (testing "stale leaf doesn't propagate upstream"+ (let [staleness {"t3" {:definition_changed true :input_data_changed false}}+ result (ws.impl/annotate-staleness graph staleness)]+ (is (= {true #{"t3"} false #{"t1" "t2"}}+ (stale->id (:entities result)))))))))++(deftest annotate-staleness-diamond-graph-test+ (testing "Diamond graph: staleness propagates through all paths"+ (let [t1 (workspace-transform "t1")+ t2 (workspace-transform "t2")+ t3 (workspace-transform "t3")+ t4 (workspace-transform "t4")+ ;; t1+ ;; / \+ ;; t2 t3+ ;; \ /+ ;; t4+ graph {:entities [t1 t2 t3 t4]+ :dependencies {t2 [t1]+ t3 [t1]+ t4 [t2 t3]}}]+ (testing "stale root propagates through all paths"+ (let [staleness {"t1" {:definition_changed true :input_data_changed false}}+ result (ws.impl/annotate-staleness graph staleness)]+ (is (= {true #{"t1" "t2" "t3" "t4"} false #{}}+ (stale->id (:entities result))))))++ (testing "stale on one branch only propagates through that path"+ (let [staleness {"t2" {:definition_changed true :input_data_changed false}}+ result (ws.impl/annotate-staleness graph staleness)]+ (is (= {true #{"t2" "t4"} false #{"t1" "t3"}}+ (stale->id (:entities result)))))))))++(deftest annotate-staleness-independent-subgraphs-test+ (testing "Independent subgraphs: staleness doesn't cross between them"+ (let [a1 (workspace-transform "a1")+ a2 (workspace-transform "a2")+ b1 (workspace-transform "b1")+ b2 (workspace-transform "b2")+ ;; Subgraph A: a1 -> a2+ ;; Subgraph B: b1 -> b2+ graph {:entities [a1 a2 b1 b2]+ :dependencies {a2 [a1]+ b2 [b1]}}]+ (testing "staleness in subgraph A doesn't affect subgraph B"+ (let [staleness {"a1" {:definition_changed true :input_data_changed false}}+ result (ws.impl/annotate-staleness graph staleness)]+ (is (= {true #{"a1" "a2"} false #{"b1" "b2"}}+ (stale->id (:entities result)))))))))++(deftest annotate-staleness-wide-graph-test+ (testing "Wide graph: stale root propagates to all children"+ (let [t1 (workspace-transform "t1")+ t2 (workspace-transform "t2")+ t3 (workspace-transform "t3")+ t4 (workspace-transform "t4")+ t5 (workspace-transform "t5")+ ;; t1+ ;; / | | | \+ ;; t2 t3 t4 t5 ...+ graph {:entities [t1 t2 t3 t4 t5]+ :dependencies {t2 [t1]+ t3 [t1]+ t4 [t1]+ t5 [t1]}}+ staleness {"t1" {:definition_changed true :input_data_changed false}}+ result (ws.impl/annotate-staleness graph staleness)]+ (is (= {true #{"t1" "t2" "t3" "t4" "t5"} false #{}}+ (stale->id (:entities result)))))))++(deftest annotate-staleness-mixed-transforms-test+ (testing "Mixed transforms: staleness propagates through external transforms"+ (let [t1 (workspace-transform "t1")+ ext (global-transform 100)+ t2 (workspace-transform "t2")+ ;; t1 -> ext -> t2+ graph {:entities [t1 ext t2]+ :dependencies {ext [t1]+ t2 [ext]}}]+ (testing "staleness propagates through external transform to downstream workspace transform"+ (let [staleness {"t1" {:definition_changed true :input_data_changed false}}+ result (ws.impl/annotate-staleness graph staleness)]+ ;; All transforms (workspace and external) downstream of stale t1 should be marked stale+ (is (= {true #{"t1" 100 "t2"} false #{}}+ (stale->id (filter #(#{:workspace-transform :external-transform} (:node-type %))+ (:entities result))))))))))++(deftest annotate-staleness-empty-stale-set-test+ (testing "Empty stale set: nothing is marked stale"+ (let [t1 (workspace-transform "t1")+ t2 (workspace-transform "t2")+ graph {:entities [t1 t2]+ :dependencies {t2 [t1]}}+ staleness {}+ result (ws.impl/annotate-staleness graph staleness)]+ (is (= {true #{} false #{"t1" "t2"}}+ (stale->id (:entities result)))))))++(deftest annotate-staleness-input-data-changed-test+ (testing "input_data_changed triggers staleness just like definition_changed"+ (let [t1 (workspace-transform "t1")+ t2 (workspace-transform "t2")+ graph {:entities [t1 t2]+ :dependencies {t2 [t1]}}+ staleness {"t1" {:definition_changed false :input_data_changed true}}+ result (ws.impl/annotate-staleness graph staleness)]+ (is (= {true #{"t1" "t2"} false #{}}+ (stale->id (:entities result)))))))++(deftest annotate-staleness-preserves-other-fields-test+ (testing "compute-staleness preserves other entity fields"+ (let [t1 {:node-type :workspace-transform :id "t1" :extra-field "preserved"}+ graph {:entities [t1]+ :dependencies {}}+ staleness {"t1" {:definition_changed true :input_data_changed false}}+ result (ws.impl/annotate-staleness graph staleness)+ entity (first (:entities result))]+ (is (= "preserved" (:extra-field entity)))+ (is (true? (:stale entity)))+ (is (true? (:definition_changed entity)))+ (is (false? (:input_data_changed entity))))))++;;;; Static graph tests for marking functions (upstream-ancestors, downstream-descendants, any-ancestor-stale?)++(deftest upstream-ancestors-linear-chain-test+ (testing "Linear chain: computes all upstream ancestors"+ (let [t1 (workspace-transform "t1")+ t2 (workspace-transform "t2")+ t3 (workspace-transform "t3")+ ;; t1 -> t2 -> t3+ graph {:entities [t1 t2 t3]+ :dependencies {t2 [t1]+ t3 [t2]}}]+ (testing "t3 has t1 and t2 as ancestors"+ (is (= #{"t1" "t2"}+ (set (ws.impl/upstream-ancestors graph "t3")))))+ (testing "t2 has only t1 as ancestor"+ (is (= #{"t1"}+ (set (ws.impl/upstream-ancestors graph "t2")))))+ (testing "t1 has no ancestors"+ (is (empty? (ws.impl/upstream-ancestors graph "t1")))))))++(deftest upstream-ancestors-diamond-test+ (testing "Diamond graph: computes all paths"+ (let [t1 (workspace-transform "t1")+ t2 (workspace-transform "t2")+ t3 (workspace-transform "t3")+ t4 (workspace-transform "t4")+ ;; t1+ ;; / \+ ;; t2 t3+ ;; \ /+ ;; t4+ graph {:entities [t1 t2 t3 t4]+ :dependencies {t2 [t1]+ t3 [t1]+ t4 [t2 t3]}}]+ (testing "t4 has all transforms as ancestors"+ (is (= #{"t1" "t2" "t3"}+ (set (ws.impl/upstream-ancestors graph "t4")))))+ (testing "t2 has only t1 as ancestor"+ (is (= #{"t1"}+ (set (ws.impl/upstream-ancestors graph "t2"))))))))++(deftest upstream-ancestors-with-external-transforms-test+ (testing "External transforms are traversed but not included"+ (let [t1 (workspace-transform "t1")+ ext (global-transform 100)+ t2 (workspace-transform "t2")+ ;; t1 -> ext -> t2+ graph {:entities [t1 ext t2]+ :dependencies {ext [t1]+ t2 [ext]}}]+ (testing "t2 can reach through external transform to t1"+ (is (= #{"t1"}+ (set (ws.impl/upstream-ancestors graph "t2")))))+ (testing "ext cannot be returned as it's external"+ (is (not (contains? (set (ws.impl/upstream-ancestors graph "t2")) 100)))))))++(deftest downstream-descendants-linear-chain-test+ (testing "Linear chain: computes all downstream descendants"+ (let [t1 (workspace-transform "t1")+ t2 (workspace-transform "t2")+ t3 (workspace-transform "t3")+ ;; t1 -> t2 -> t3+ graph {:entities [t1 t2 t3]+ :dependencies {t2 [t1]+ t3 [t2]}}]+ (testing "t1 has t2 and t3 as descendants"+ (is (= #{"t2" "t3"}+ (set (ws.impl/downstream-descendants graph "t1")))))+ (testing "t2 has only t3 as descendant"+ (is (= #{"t3"}+ (set (ws.impl/downstream-descendants graph "t2")))))+ (testing "t3 has no descendants"+ (is (empty? (ws.impl/downstream-descendants graph "t3")))))))++(deftest downstream-descendants-diamond-test+ (testing "Diamond graph: computes all downstream paths"+ (let [t1 (workspace-transform "t1")+ t2 (workspace-transform "t2")+ t3 (workspace-transform "t3")+ t4 (workspace-transform "t4")+ ;; t1+ ;; / \+ ;; t2 t3+ ;; \ /+ ;; t4+ graph {:entities [t1 t2 t3 t4]+ :dependencies {t2 [t1]+ t3 [t1]+ t4 [t2 t3]}}]+ (testing "t1 has all transforms as descendants"+ (is (= #{"t2" "t3" "t4"}+ (set (ws.impl/downstream-descendants graph "t1")))))+ (testing "t2 has only t4 as descendant"+ (is (= #{"t4"}+ (set (ws.impl/downstream-descendants graph "t2"))))))))++(deftest downstream-descendants-with-external-transforms-test+ (testing "External transforms are traversed but not included"+ (let [t1 (workspace-transform "t1")+ ext (global-transform 100)+ t2 (workspace-transform "t2")+ ;; t1 -> ext -> t2+ graph {:entities [t1 ext t2]+ :dependencies {ext [t1]+ t2 [ext]}}]+ (testing "t1 can reach through external transform to t2"+ (is (= #{"t2"}+ (set (ws.impl/downstream-descendants graph "t1")))))+ (testing "ext cannot be returned as it's external"+ (is (not (contains? (set (ws.impl/downstream-descendants graph "t1")) 100)))))))++(deftest downstream-descendants-empty-graph-test+ (testing "Empty graph returns nil"+ (is (nil? (ws.impl/downstream-descendants {:entities [] :dependencies nil} "t1")))))++(deftest upstream-ancestors-empty-graph-test+ (testing "Empty graph returns nil"+ (is (nil? (ws.impl/upstream-ancestors {:entities [] :dependencies nil} "t1")))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/isolation_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/isolation_test.cljnew file mode 100644index 00000000000..3fc005d042d--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/isolation_test.clj@@ -0,0 +1,203 @@+(ns ^:mb/driver-tests metabase-enterprise.workspaces.isolation-test+ (:require+ [clojure.java.jdbc :as jdbc]+ [clojure.test :refer :all]+ [metabase-enterprise.workspaces.common :as ws.common]+ [metabase-enterprise.workspaces.test-util :as ws.tu]+ [metabase-enterprise.workspaces.util :as ws.u]+ [metabase.driver :as driver]+ [metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]+ [metabase.driver.util :as driver.u]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(ws.tu/ws-fixtures!)++(set! *warn-on-reflection* true)++;;; Test helper multimethod to verify cleanup++(defn- has-results?+ "Returns true if the query returns any results."+ [conn-spec query]+ (-> (jdbc/query conn-spec query) seq boolean))++(defmulti workspace-isolation-resources-exist?+ "Check if isolation resources still exist for the workspace.+ Returns a map with keys indicating which resources exist."+ {:arglists '([database workspace])}+ (fn [database _workspace]+ (driver/the-driver (:engine database)))+ :hierarchy #'driver/hierarchy)++(defmethod workspace-isolation-resources-exist? :postgres+ [database workspace]+ (let [schema-name (ws.u/isolation-namespace-name workspace)+ username (ws.u/isolation-user-name workspace)+ conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ {:schema (has-results? conn-spec+ ["SELECT 1 FROM information_schema.schemata WHERE schema_name = ?" schema-name])+ :user (has-results? conn-spec+ ["SELECT 1 FROM pg_user WHERE usename = ?" username])}))++(defmethod workspace-isolation-resources-exist? :h2+ [database workspace]+ (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)+ username (driver.u/workspace-isolation-user-name workspace)+ conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ {:schema (has-results? conn-spec+ ["SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE UPPER(SCHEMA_NAME) = UPPER(?)" schema-name])+ :user (has-results? conn-spec+ ["SELECT 1 FROM INFORMATION_SCHEMA.USERS WHERE UPPER(USER_NAME) = UPPER(?)" username])}))++(defmethod workspace-isolation-resources-exist? :snowflake+ [database workspace]+ (let [schema-name (ws.u/isolation-namespace-name workspace)+ db-name (-> database :details :db)+ role-name (format "MB_ISOLATION_ROLE_%s" (:id workspace))+ username (ws.u/isolation-user-name workspace)+ conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ {:schema (has-results? conn-spec+ [(format "SHOW SCHEMAS LIKE '%s' IN DATABASE \"%s\"" schema-name db-name)])+ :user (has-results? conn-spec+ [(format "SHOW USERS LIKE '%s'" username)])+ :role (has-results? conn-spec+ [(format "SHOW ROLES LIKE '%s'" role-name)])}))++(defmethod workspace-isolation-resources-exist? :sqlserver+ [database workspace]+ (let [schema-name (ws.u/isolation-namespace-name workspace)+ username (ws.u/isolation-user-name workspace)+ conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ {:schema (has-results? conn-spec+ ["SELECT 1 FROM sys.schemas WHERE name = ?" schema-name])+ :user (has-results? conn-spec+ ["SELECT 1 FROM sys.database_principals WHERE name = ?" username])+ :login (has-results? conn-spec+ ["SELECT 1 FROM master.sys.server_principals WHERE name = ?" username])}))++(defmethod workspace-isolation-resources-exist? :clickhouse+ [database workspace]+ (let [db-name (ws.u/isolation-namespace-name workspace)+ username (ws.u/isolation-user-name workspace)+ conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ {:database (has-results? conn-spec+ ["SELECT 1 FROM system.databases WHERE name = ?" db-name])+ :user (has-results? conn-spec+ ["SELECT 1 FROM system.users WHERE name = ?" username])}))++(defmethod workspace-isolation-resources-exist? :mysql+ [database workspace]+ (let [db-name (ws.u/isolation-namespace-name workspace)+ username (ws.u/isolation-user-name workspace)+ conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ {:database (has-results? conn-spec+ ["SELECT 1 FROM information_schema.schemata WHERE schema_name = ?" db-name])+ :user (has-results? conn-spec+ ["SELECT 1 FROM mysql.user WHERE user = ?" username])}))++;;; Tests+(deftest destroy-workspace-isolation-test+ (mt/test-drivers (mt/normal-drivers-with-feature :workspace)+ (ws.tu/with-workspaces! [workspace {:name "Test destroy isolation"}]+ (ws.common/add-to-changeset! (mt/user->id :crowberto) workspace+ :transform nil+ {:name "Transform A"+ :source {:type "query" :query (mt/mbql-query orders {:limit 1})}+ :target {:database (mt/id)+ :schema "analytics"+ :name "table_a"}})+ (let [workspace (ws.tu/ws-done! workspace)+ database (mt/db)]+ (testing "resources are created during workspace initialization"+ (let [resources (workspace-isolation-resources-exist? database workspace)]+ (is (every? true? (vals resources)))))++ (testing "destroy removes all isolation resources"+ (driver/destroy-workspace-isolation! (driver.u/database->driver database) database workspace)+ (let [resources (workspace-isolation-resources-exist? database workspace)]+ (is (every? false? (vals resources)))))++ (testing "destroy is idempotent"+ (driver/destroy-workspace-isolation! (driver.u/database->driver database) database workspace))))))++;;; check-isolation-permissions tests++(deftest check-isolation-permissions-success-test+ (mt/test-drivers (mt/normal-drivers-with-feature :workspace)+ (testing "returns nil when connection has all required permissions"+ (let [database (mt/db)+ test-table (t2/select-one [:model/Table :schema :name] (mt/id :orders))]+ (is (nil? (driver/check-isolation-permissions+ (driver/the-driver (:engine database))+ database+ test-table)))))))++(deftest check-isolation-permissions-no-artifacts-test+ (mt/test-drivers (mt/normal-drivers-with-feature :workspace)+ (testing "leaves no artifacts after check due to transaction rollback"+ (let [database (mt/db)+ test-workspace {:id "00000000-0000-0000-0000-000000000000"+ :name "_mb_perm_check_"}+ test-table (t2/select-one [:model/Table :schema :name] (mt/id :orders))]+ ;; Run the check+ (driver/check-isolation-permissions+ (driver/the-driver (:engine database))+ database+ test-table)+ ;; Verify no artifacts remain - the test workspace should not have any resources+ (let [resources (workspace-isolation-resources-exist? database test-workspace)]+ (is (every? false? (vals resources))+ "No isolation resources should exist after permission check"))))))++(deftest check-isolation-permissions-init-failure-test+ (mt/test-drivers (mt/normal-drivers-with-feature :workspace)+ (testing "returns error message when init fails"+ (let [database (mt/db)+ test-table (t2/select-one [:model/Table :schema :name] (mt/id :orders))]+ (with-redefs [driver/init-workspace-isolation!+ (fn [_driver _database _workspace]+ (throw (ex-info "permission denied" {:step :init})))]+ (is (some? (driver/check-isolation-permissions+ (driver/the-driver (:engine database))+ database+ test-table))+ "Should return error message when init fails"))))))++(deftest check-isolation-permissions-grant-failure-test+ (mt/test-drivers (mt/normal-drivers-with-feature :workspace)+ (testing "returns error message when grant fails"+ (let [database (mt/db)+ test-table (t2/select-one [:model/Table :schema :name] (mt/id :orders))]+ (with-redefs [driver/grant-workspace-read-access!+ (fn [_driver _database _workspace _tables]+ (throw (ex-info "permission denied" {:step :grant})))]+ (is (some? (driver/check-isolation-permissions+ (driver/the-driver (:engine database))+ database+ test-table))+ "Should return error message when grant fails"))))))++(deftest check-isolation-permissions-destroy-failure-test+ (mt/test-drivers (mt/normal-drivers-with-feature :workspace)+ (testing "returns error message when destroy fails"+ (let [database (mt/db)+ test-table (t2/select-one [:model/Table :schema :name] (mt/id :orders))]+ (with-redefs [driver/destroy-workspace-isolation!+ (fn [_driver _database _workspace]+ (throw (ex-info "permission denied" {:step :destroy})))]+ (is (some? (driver/check-isolation-permissions+ (driver/the-driver (:engine database))+ database+ test-table))+ "Should return error message when destroy fails"))))))++(deftest check-isolation-permissions-nil-table-test+ (mt/test-drivers (mt/normal-drivers-with-feature :workspace)+ (testing "returns nil when test-table is nil (skips grant step)"+ (let [database (mt/db)]+ (is (nil? (driver/check-isolation-permissions+ (driver/the-driver (:engine database))+ database+ nil))+ "Should succeed when test-table is nil")))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_transform_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_transform_test.cljnew file mode 100644index 00000000000..967600cebed--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/models/workspace_transform_test.clj@@ -0,0 +1,32 @@+(ns metabase-enterprise.workspaces.models.workspace-transform-test+ "Tests for WorkspaceTransform model behavior."+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.workspaces.test-util :as ws.tu]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(ws.tu/ws-fixtures!)++(deftest workspace-transform-definition-stale-marking+ (testing "WorkspaceTransform definition_changed is marked based on source/target changes"+ (ws.tu/with-resources! [{ws-id :workspace-id, :keys [workspace-map]}+ {:workspace {:definitions {:x1 [:t0]}}}]+ (let [t1-ref (workspace-map :x1)+ ws+ref {:workspace_id ws-id, :ref_id t1-ref}+ unchanged {:definition_changed false, :input_data_changed false}+ change! (fn [updates]+ ;; First reset the stale tracking+ (t2/update! :model/WorkspaceTransform ws+ref unchanged)+ (t2/update! :model/WorkspaceTransform ws+ref updates))]+ (testing "initially definition is stale - as we've never been run"+ (is (= {t1-ref {:definition_changed true, :input_data_changed false}} (ws.tu/staleness-flags ws-id))))+ (testing "marks as stale when source changes"+ (change! {:source {:type "query" :query (mt/native-query {:query "SELECT 2 as id"})}})+ (is (= {t1-ref {:definition_changed true, :input_data_changed false}} (ws.tu/staleness-flags ws-id))))+ (testing "marks as stale when target changes"+ (change! {:target {:type "table", :database (mt/id), :schema nil, :name "new_table"}})+ (is (= {t1-ref {:definition_changed true, :input_data_changed false}} (ws.tu/staleness-flags ws-id))))+ (testing "preserves definition_changed when other fields change"+ (change! {:name "New Name"})+ (is (= {t1-ref {:definition_changed false :input_data_changed false}} (ws.tu/staleness-flags ws-id))))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj b/enterprise/backend/test/metabase_enterprise/workspaces/test_util.cljnew file mode 100644index 00000000000..779960b8568--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/test_util.clj@@ -0,0 +1,488 @@+(ns ^:mb/driver-tests metabase-enterprise.workspaces.test-util+ (:require+ [clojure.set :as set]+ [clojure.string :as str]+ [clojure.test :refer :all]+ [metabase-enterprise.workspaces.common :as ws.common]+ [metabase-enterprise.workspaces.dag-abstract :as dag-abstract]+ [metabase-enterprise.workspaces.execute :as ws.execute]+ [metabase-enterprise.workspaces.impl :as ws.impl]+ [metabase-enterprise.workspaces.isolation :as ws.isolation]+ [metabase-enterprise.workspaces.models.workspace :as ws.model]+ [metabase.app-db.core :as app-db]+ [metabase.config.core :as config]+ [metabase.driver :as driver]+ [metabase.driver.sql.normalize :as sql.normalize]+ [metabase.driver.sql.query-processor :as sql.qp]+ [metabase.query-processor.preprocess :as qp.preprocess]+ ^{:clj-kondo/ignore [:deprecated-namespace]}+ [metabase.query-processor.store :as qp.store]+ [metabase.search.test-util :as search.tu]+ [metabase.test :as mt]+ [metabase.util :as u]+ [metabase.util.log :as log]+ [toucan2.core :as t2])+ (:import+ (java.time Instant)))++(set! *warn-on-reflection* true)++(defn unique-name+ "Generate a unique name for test resources to avoid conflicts."+ ([] (unique-name "Test"))+ ([prefix] (str prefix " " (random-uuid))))++;;;; Shorthand notation helpers++(defn transform?+ "Check if keyword represents a transform (keyword starting with 'x')."+ [kw]+ (and (keyword? kw)+ (= \x (first (name kw)))))++(defn mock-table?+ "Check if keyword represents a mock table (keyword starting with 't')."+ [kw]+ (and (keyword? kw)+ (= \t (first (name kw)))))++(defn real-table?+ "Check if ref is a string (existing table reference by name)."+ [ref]+ (string? ref))++(defn kw->id+ "Extract numeric id from shorthand keyword (e.g., :x1 -> 1, :t2 -> 2)."+ [kw]+ (assert (keyword? kw) (str "kw->id requires a keyword, got: " (pr-str kw)))+ (parse-long (subs (name kw) 1)))++(defn- real-table-id+ "Look up an existing table by name (case-insensitive). Returns table ID."+ [db-id table-name]+ (or (t2/select-one-fn :id :model/Table+ :db_id db-id+ :active true+ {:where [:= [:lower :name] (u/lower-case-en table-name)]})+ (throw (ex-info (str "Existing table not found: " table-name) {:table table-name}))))++;;;; Query helpers++(defn mbql->native+ "Convert an MBQL query to a native query map suitable for use in transform tests.+ This generates driver-specific SQL with properly qualified table names."+ [query]+ (qp.store/with-metadata-provider (mt/id)+ (sql.qp/mbql->native driver/*driver* (qp.preprocess/preprocess query))))++;;;; Building blocks for test resource creation++(defn- build-source-query+ "Build a native source query referencing the given table IDs."+ [db-id table-ids]+ (if (seq table-ids)+ {:database db-id+ :type :native+ :native {:query (str "SELECT * FROM "+ (->> (t2/select [:model/Table :schema :name] :id [:in table-ids])+ (map #(str (:schema %) "." (:name %)))+ (str/join ", ")))}}+ {:database db-id+ :type :native+ :native {:query "SELECT 1"}}))++(defn- build-transform-target+ "Build target map for a transform."+ [db-id tx-sym schema]+ (let [driver (t2/select-one-fn :engine [:model/Database :engine] db-id)+ normalize #(sql.normalize/normalize-name driver %)]+ {:type "table"+ :database db-id+ :schema (some-> schema normalize)+ :name (normalize (str "test_table_" (kw->id tx-sym)))}))++(defn create-tables!+ "Create test tables for the given table symbols. Returns {symbol -> table-id}."+ [db-id table-syms schema]+ (let [driver (t2/select-one-fn :engine [:model/Database :engine] db-id)+ normalize #(sql.normalize/normalize-name driver %)]+ (into {}+ (for [t table-syms]+ [t (t2/insert-returning-pk! :model/Table+ {:db_id db-id+ :schema (some-> schema normalize)+ :name (normalize (str "test_table_" (kw->id t)))+ :active true})]))))++(defn create-transform!+ "Create a global transform with given dependencies. Returns transform ID."+ [db-id tx-sym deps id-map schema]+ (let [table-ids (->> deps+ (keep #(cond+ (mock-table? %) (id-map %)+ (real-table? %) (real-table-id db-id %)+ :else nil)))+ source (build-source-query db-id table-ids)+ target (build-transform-target db-id tx-sym schema)]+ (t2/insert-returning-pk! :model/Transform+ {:name (str "Test " (name tx-sym))+ :source {:type :query :query source}+ :target target})))++(defn add-transform-to-workspace!+ "Add a transform to a workspace via API. Returns ref-id.++ For checkouts: provide :global-tx (the global transform to checkout)+ For new transforms: provide :deps and :id-map to build the source+ If both :global-tx and :deps are provided, the deps override the global source."+ [db-id ws-id tx-sym {:keys [global-tx deps id-map schema]}]+ (let [resolve-table-ids (fn [deps]+ (->> deps+ (keep #(cond+ (mock-table? %) (id-map %)+ (real-table? %) (real-table-id db-id %)+ :else nil))))+ body (if global-tx+ ;; Checkout: use global transform's body (can be overridden with deps)+ (cond-> (select-keys global-tx [:name :description :source :target])+ deps (assoc :source {:type :query+ :query (build-source-query db-id (resolve-table-ids deps))}))+ ;; New: build from deps+ {:name (str "Test " (name tx-sym))+ :source {:type :query :query (build-source-query db-id (resolve-table-ids deps))}+ :target (build-transform-target db-id tx-sym schema)})+ response (mt/user-http-request :crowberto :post 200+ (str "ee/workspace/" ws-id "/transform")+ (cond-> body+ global-tx (assoc :global_id (:id global-tx))))]+ (:ref_id response)))++;;;; Orchestration functions++(defn- create-workspace-for-test! [props]+ (let [creator-id (or (:creator_id props) (mt/user->id :crowberto))+ props (-> props+ (dissoc :creator_id)+ (update :database_id #(or % (mt/id))))]+ (mt/with-current-user creator-id+ (ws.common/create-workspace! creator-id props))))++(defn create-resources!+ "Create test resources from shorthand notation for both global and workspace transforms.++ Input: {:database-id int (optional, defaults to mt/id)+ :global dependencies-graph (shorthand)+ :workspace {:checkouts ids, :definitions dependencies-graph (shorthand)}}++ Returns: {:workspace-id int (or nil if no workspace)+ :global-map {symbol -> db-id}+ :workspace-map {symbol -> ref-id}}++ NOTE: Caller should wrap in mt/with-model-cleanup for proper cleanup."+ [{:keys [database-id global workspace]}]+ (let [db-id (or database-id (mt/id))+ definitions (:definitions workspace)+ checkouts (set (:checkouts workspace))+ schema (str/replace (str (random-uuid)) "-" "_")++ ;; Expand shorthand to insert intermediate table nodes+ expanded-global (dag-abstract/expand-shorthand global)+ expanded-ws-defs (when definitions (dag-abstract/expand-shorthand definitions))++ ;; Find all mock tables needed (union of global and workspace)+ global-tables (set (filter mock-table? (concat (keys expanded-global)+ (mapcat val expanded-global))))+ ws-tables (when expanded-ws-defs+ (set (filter mock-table? (concat (keys expanded-ws-defs)+ (mapcat val expanded-ws-defs)))))+ all-tables (set/union global-tables (or ws-tables #{}))++ ;; Create all mock tables upfront+ table-ids (create-tables! db-id all-tables schema)++ ;; Collect and resolve all real table references+ real-table-ids (into {}+ (for [ref (distinct+ (filter real-table?+ (concat (mapcat val expanded-global)+ (mapcat val (or expanded-ws-defs {})))))]+ [ref (real-table-id db-id ref)]))++ ;; Create global transforms+ global-tx-syms (filter transform? (keys expanded-global))+ global-tx-ids (into {}+ (for [tx global-tx-syms]+ [tx (create-transform! db-id tx+ (get expanded-global tx [])+ (merge table-ids real-table-ids) schema)]))++ ;; Insert global dependencies+ _ (doseq [[tx-kw tx-id] global-tx-ids]+ (let [output-table-sym (keyword (str "t" (kw->id tx-kw)))]+ (when-let [table-id (table-ids output-table-sym)]+ (app-db/update-or-insert! :model/Dependency+ {:to_entity_type "transform"+ :to_entity_id tx-id+ :from_entity_type "table"+ :from_entity_id table-id}))))++ global-map (merge table-ids global-tx-ids real-table-ids)++ ;; Create workspace (only if workspace key was provided)+ ws (when workspace+ (create-workspace-for-test! {:name (or (:name workspace)+ (str "test-ws-" (random-uuid)))+ :database_id db-id}))+ ws-id (:id ws)++ ;; Determine which workspace transforms to create+ ws-tx-syms (distinct (concat+ ;; Checkouts not overridden in definitions+ (filter #(not (contains? (set (keys definitions)) %)) checkouts)+ ;; All transforms from definitions+ (when expanded-ws-defs+ (filter transform? (keys expanded-ws-defs)))))++ ;; Create workspace transforms+ workspace-map (into {}+ (for [tx ws-tx-syms]+ (let [is-checkout? (contains? checkouts tx)+ in-defs? (contains? (set (keys definitions)) tx)+ global-tx (when is-checkout?+ (t2/select-one :model/Transform (global-map tx)))+ deps (when (or (not is-checkout?) in-defs?)+ (get expanded-ws-defs tx []))]+ [tx (add-transform-to-workspace! db-id ws-id tx+ {:global-tx global-tx+ :deps deps+ :id-map global-map+ :schema schema})])))++ ;; Apply per-transform properties (e.g., staleness flags)+ _ (when-let [props (:properties workspace)]+ (doseq [[tx-sym updates] props]+ (when-let [ref-id (workspace-map tx-sym)]+ (t2/update! :model/WorkspaceTransform {:workspace_id ws-id :ref_id ref-id} updates))))]+ {:workspace-id ws-id+ :global-map global-map+ :workspace-map workspace-map}))++(defmacro with-resources!+ "Create test resources and bind the result map. Cleanup is handled by ws-fixtures!.++ Usage: (with-resources! [res {:workspace {:definitions {:x2 [:x1]}+ :properties {:x1 {:definition_changed true}}}}]+ (:workspace-id res))"+ [[binding resource-spec] & body]+ `(let [~binding (create-resources! ~resource-spec)]+ ~@body))++(defn- append-url-part [url part]+ (case [(str/starts-with? part "/")+ (str/ends-with? url "/")]+ [false false] (str url \/ part)+ ([true false]+ [false true]) (str url part)+ (str url (subs part 1))))++(defn ws-url+ "Build an API URL for a workspace resource, e.g. (ws-url 42 \"/transform/\" ref-id)."+ [id & path]+ (when (some nil? (cons id path))+ (throw (ex-info "Cannot build workspace URL without key resources"+ {:id id, :path path})))+ (reduce append-url-part (str "ee/workspace/" id) (map str path)))++(defn staleness-flags+ "Return a map of ref-id to staleness flags for all transforms in a workspace.+ E.g. {\"ref-1\" {:definition_changed false :input_data_changed true}}"+ [workspace-id]+ (t2/select-fn->fn :ref_id #(select-keys % [:definition_changed :input_data_changed])+ [:model/WorkspaceTransform :ref_id :definition_changed :input_data_changed]+ :workspace_id workspace-id))++(defmacro with-mocked-execution+ "Execute body with ws.execute/run-transform-with-remapping stubbed to return success.+ Useful for testing workspace execution logic without running real transforms."+ [& body]+ `(mt/with-dynamic-fn-redefs [ws.execute/run-transform-with-remapping+ (fn [_transform# _remapping#]+ {:status :succeeded+ :end_time (Instant/now)+ :message "Mocked execution"})]+ ~@body))++(defn mock-run-transform!+ "Mock-execute a workspace transform by ref-id.+ Stubs the execution engine to return success, then calls ws.impl/run-transform!."+ [workspace-id ref-id]+ (with-mocked-execution+ (let [workspace (t2/select-one :model/Workspace workspace-id)+ graph (ws.impl/get-or-calculate-graph! workspace)+ ws-transform (t2/select-one :model/WorkspaceTransform :workspace_id workspace-id :ref_id ref-id)]+ (mt/with-current-user (mt/user->id :crowberto)+ (ws.impl/run-transform! workspace graph ws-transform)))))++(defn ws-fixtures!+ "Sets up test fixtures for workspace tests. Must be called at the top level of test namespaces."+ []+ (use-fixtures :each (fn [tests]+ (mt/test-drivers (mt/normal-drivers-with-feature :workspace)+ (mt/with-premium-features [:workspaces :dependencies :transforms :transforms-python]+ (search.tu/with-index-disabled+ (mt/with-model-cleanup [:model/Collection+ :model/Transform+ :model/TransformRun+ :model/Workspace+ :model/WorkspaceTransform+ :model/WorkspaceInput+ :model/WorkspaceOutput]+ (tests))))))))++(derive :model/Workspace :model/WorkspaceCleanUpInTest)++(t2/define-before-delete :model/WorkspaceCleanUpInTest+ [workspace]+ (try+ (if (:database_details workspace)+ (let [database (t2/select-one :model/Database (:database_id workspace))]+ (log/infof "Cleaning up workspace %d in tests" (:id workspace))+ (ws.isolation/destroy-workspace-isolation! database workspace))+ (log/infof "Skip cleaning up workspace %d due to no database details" (:id workspace)))+ (catch Exception e+ (log/warn e "Failed to destroy isolation" {:workspace workspace})))+ workspace)++(defn ws-done!+ "Poll until workspace status is no longer :pending.+ Returns immediately if workspace has not started initializing, which requires a transform being added."+ [ws-or-id]+ (let [ws-id (cond-> ws-or-id+ (map? ws-or-id) :id)]+ (or (u/poll {:thunk #(t2/select-one :model/Workspace :id ws-id)+ :done? #(not= :pending (:db_status %))+ ;; some cloud drivers are really slow+ :timeout-ms (if config/is-dev? 10000 60000)})+ (throw (ex-info "Timeout waiting for workspace to finish initializing" {:workspace-id ws-id})))))++(defn create-empty-ws!+ "Create a simple workspace and wait for it to be ready."+ [name]+ (t2/select-one :model/Workspace (:workspace-id (create-resources! {:workspace {:name name}}))))++(defn initialize-ws!+ "Create a workspace with a transform to trigger initialization, and wait for it to finish.+ Returns the workspace regardless of its final status (ready, broken, etc.)."+ [name]+ (let [graph {:workspace {:name name, :definitions {:x2 [:t1]}}}+ ws-id (:workspace-id (create-resources! graph))]+ (ws-done! ws-id)))++(defn create-ready-ws!+ "Create a simple workspace and wait for it to finish initializing database resources.+ Throws if workspace does not become ready."+ [name]+ (let [ws (initialize-ws! name)]+ (if (= :ready (:db_status ws))+ ws+ (throw (ex-info "Workspace failed to become ready"+ {:name name :db_status (:db_status ws) :workspace-id (:id ws)})))))++(defn do-with-workspaces!+ "Function that sets up workspaces for testing and cleans up afterwards.+ Takes a sequence of props for workspace creation and a thunk that receives+ the created workspaces as a vector. Each workspace is cleaned up by its own+ stack frame, so cleanup naturally happens in reverse creation order."+ [props-list thunk]+ (if (empty? props-list)+ (thunk [])+ (let [ws (create-workspace-for-test! (first props-list))]+ (try+ (do-with-workspaces! (rest props-list)+ (fn [rest-workspaces]+ (thunk (into [ws] rest-workspaces))))+ (finally+ (when ws+ (try+ (ws.model/delete! (t2/select-one :model/Workspace :id (:id ws)))+ (catch Exception _+ ;; Workspace may already be deleted by the test+ nil))))))))++(defmacro with-workspaces!+ "Execute body with properly initialized workspaces that are cleaned up afterward.++ Creates each workspace using `ws.common/create-workspace!` and waits for it to+ be ready. After body execution (or on error), cleans up using `ws.model/delete!`+ which properly destroys database isolation resources.++ Usage:+ (with-workspaces [ws1 {:name \"Test WS 1\"}+ ws2 {:name \"Test WS 2\" :database_id (mt/id)}]+ (testing \"workspace operations\"+ (is (= :ready (:status ws1)))))"+ [bindings & body]+ (assert (vector? bindings) "bindings must be a vector")+ (assert (even? (count bindings)) "bindings must have an even number of forms")+ (let [pairs (partition 2 bindings)+ syms (mapv first pairs)+ props-list (mapv second pairs)]+ `(do-with-workspaces!+ [~@props-list]+ (fn [[~@syms]]+ ~@body))))++(defn analyze-workspace!+ "Trigger the reconstruction and persistence of the workspace graph."+ [id]+ (mt/user-http-request :crowberto :get 200 (str "ee/workspace/" id "/graph")))++(defn- replace-entity [{:keys [input-table workspace-transform external-transform]} entity-type entity-id]+ (get+ (case entity-type+ "input-table" input-table+ "workspace-transform" workspace-transform+ "external-transform" external-transform)+ entity-id entity-id))++(defn- table-ref? [sym]+ (or (mock-table? sym)+ (real-table? sym)))++(defn translate-graph+ "Turn a real workspace graph back into :x1 etc symbols"+ [{:keys [nodes edges]} resources-map]+ (let [mapping (merge (reduce+ (fn [acc [sym id]]+ (assoc-in acc+ [(cond+ (table-ref? sym) :input-table+ (transform? sym) :external-transform+ :else (throw (ex-info "Unexpected symbol" {:symbol sym :id id})))+ (if (table-ref? sym)+ (let [{:keys [db_id schema name]} (t2/select-one [:model/Table :db_id :schema :name] id)]+ (str db_id "-" schema "-" name))+ id)]+ sym))+ {:input-table {}+ :external-transform {}}+ (:global-map resources-map))+ (reduce+ (fn [acc [sym id]]+ (assoc-in acc+ [(cond+ (transform? sym) :workspace-transform+ :else (throw (ex-info "Unexpected symbol" {:symbol sym :id id})))+ id]+ sym))+ {:workspace-transform {}}+ (:workspace-map resources-map)))]+ {:nodes (into #{} (map #(replace-entity mapping (:type %) (:id %))) nodes)+ :edges (reduce+ (fn [acc e]+ (update acc+ (replace-entity mapping (:from_entity_type e) (:from_entity_id e))+ (fnil conj #{})+ (replace-entity mapping (:to_entity_type e) (:to_entity_id e))))+ {}+ edges)}))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/util_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/util_test.cljnew file mode 100644index 00000000000..69a2b141015--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/util_test.clj@@ -0,0 +1,46 @@+(ns metabase-enterprise.workspaces.util-test+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.workspaces.test-util :as ws.tu]+ [metabase-enterprise.workspaces.util :as ws.u]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(deftest ignore-constraint-violation-test+ (testing "ignore-constraint-violation catches duplicate key violations"+ (let [ws (ws.tu/create-empty-ws! "Test Workspace")]+ (testing "first insert succeeds and returns the inserted row"+ (let [result (ws.u/ignore-constraint-violation+ (t2/insert-returning-instance! :model/WorkspaceGraph+ {:workspace_id (:id ws)+ :graph_version 1+ :graph {:marker "first"}}))]+ (is (= 1 (:graph_version result)))))++ (testing "duplicate insert is silently ignored (returns nil)"+ (let [result (ws.u/ignore-constraint-violation+ (t2/insert-returning-instance! :model/WorkspaceGraph+ {:workspace_id (:id ws)+ :graph_version 1+ :graph {:marker "second"}}))]+ (is (nil? result))))++ (testing "only one row exists in the database"+ (is (= #{"first"} (t2/select-fn-set (comp :marker :graph) [:model/WorkspaceGraph :graph] :workspace_id (:id ws)))))++ (testing "we can insert a non-conflicting row"+ (let [result (ws.u/ignore-constraint-violation+ (t2/insert-returning-instance! :model/WorkspaceGraph+ {:workspace_id (:id ws)+ :graph_version 2+ :graph {:marker "third"}}))]+ (is (some? result))+ (is (= #{"first" "third"} (t2/select-fn-set (comp :marker :graph) [:model/WorkspaceGraph :graph] :workspace_id (:id ws)))))))))++(deftest ignore-constraint-violation-rethrows-other-exceptions-test+ (testing "ignore-constraint-violation rethrows non-constraint exceptions"+ (is (thrown-with-msg? Exception+ #"Test exception"+ (ws.u/ignore-constraint-violation+ (throw (Exception. "Test exception")))))))diff --git a/enterprise/backend/test/metabase_enterprise/workspaces/validation_test.clj b/enterprise/backend/test/metabase_enterprise/workspaces/validation_test.cljnew file mode 100644index 00000000000..609468b0d45--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/workspaces/validation_test.clj@@ -0,0 +1,178 @@+(ns ^:mb/driver-tests metabase-enterprise.workspaces.validation-test+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.workspaces.impl :as ws.impl]+ [metabase-enterprise.workspaces.test-util :as ws.tu]+ [metabase-enterprise.workspaces.validation :as ws.validation]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(ws.tu/ws-fixtures!)++(defn- build-graph!+ "Build the workspace graph for validation tests."+ [ws-id]+ (ws.impl/get-or-calculate-graph! (t2/select-one :model/Workspace ws-id)))++;;; ---------------------------------------- Basic API Tests ----------------------------------------++(deftest find-downstream-problems-empty-workspace-test+ (testing "find-downstream-problems returns empty for workspace with no transforms"+ (ws.tu/with-workspaces! [workspace {:name "Empty Workspace"}]+ (is (= [] (ws.validation/find-downstream-problems (:id workspace) nil))))))++(deftest problems-endpoint-returns-empty-for-new-workspace-test+ (testing "GET /api/ee/workspace/:id/problems returns empty list for workspace with no transforms"+ (ws.tu/with-workspaces! [workspace {:name "Test Workspace"}]+ (let [response (mt/user-http-request :crowberto :get 200+ (str "ee/workspace/" (:id workspace) "/problem"))]+ (is (= [] response))))))++(deftest problems-endpoint-requires-superuser-test+ (testing "GET /api/ee/workspace/:id/problems requires superuser"+ (ws.tu/with-workspaces! [workspace {:name "Private Workspace"}]+ (is (= "You don't have permissions to do that."+ (mt/user-http-request :rasta :get 403+ (str "ee/workspace/" (:id workspace) "/problem")))))))++(deftest problems-endpoint-404-for-nonexistent-workspace-test+ (testing "GET /api/ee/workspace/:id/problems returns 404 for non-existent workspace"+ (is (= "Not found."+ (mt/user-http-request :crowberto :get 404 "ee/workspace/999999/problem")))))++;;; ---------------------------------------- Problem Detection Tests ----------------------------------------++(deftest unused-output-not-yet-created-test+ (testing "detects when a workspace output table hasn't been created yet (no external dependents)"+ (ws.tu/with-workspaces! [workspace {:name "Test Workspace"}]+ (let [ws-id (:id workspace)+ ;; Create a workspace transform+ {ref-id :ref_id} (mt/user-http-request :crowberto :post 200+ (str "ee/workspace/" ws-id "/transform")+ {:name "Test Transform"+ :source {:type "query"+ :query {:database (mt/id)+ :type :native+ :native {:query "SELECT 1 as col"}}}+ :target {:type "table"+ :schema "public"+ :name "test_output_table"}})+ ;; The transform hasn't been run, so isolated_table_id is nil+ ;; No external transforms depend on it+ graph (build-graph! ws-id)+ problems (ws.validation/find-downstream-problems ws-id graph)]+ (is (= [{:category :unused+ :problem :not-run+ :severity :info+ :block_merge false+ :description "Output hasn't been created yet, but nothing depends on it"+ :data {:output {:db_id (mt/id), :schema "public", :table "test_output_table"}+ :transform {:type :workspace-transform, :id ref-id}}}]+ problems))))))++(deftest dependent-transform-output-not-yet-created-test+ (testing "detects when output table hasn't been created but external transforms depend on it"+ (ws.tu/with-workspaces! [workspace {:name "Test Workspace"}]+ (let [ws-id (:id workspace)]+ ;; First, create the global output table that we'll have a dependency on+ (mt/with-temp [:model/Table output-table {:db_id (mt/id)+ :schema "public"+ :name "global_output_table"+ :active true}]+ ;; Create an external transform that depends on this table+ ;; Note: The dependency will be created automatically by the Transform model hooks+ (mt/with-temp [:model/Transform external-tx {:name "External Transform"+ :source {:type :query+ :query {:database (mt/id)+ :type :query+ :query {:source-table (:id output-table)}}}+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "external_output"}}]+ ;; Create a workspace transform that targets the same table+ (let [{ref-id :ref_id} (mt/user-http-request :crowberto :post 200+ (str "ee/workspace/" ws-id "/transform")+ {:name "Workspace Transform"+ :source {:type "query"+ :query {:database (mt/id)+ :type :native+ :native {:query "SELECT 1 as col"}}}+ :target {:type "table"+ :schema "public"+ :name "global_output_table"}})]+ ;; Manually update the WorkspaceOutput to point to the global table+ ;; (simulating a checked-out transform scenario)+ (t2/update! :model/WorkspaceOutput+ {:workspace_id ws-id :ref_id ref-id}+ {:global_table_id (:id output-table)})++ (is (= [{:category :external-downstream+ :problem :not-run+ :severity :warning+ :block_merge true+ :description "Output hasn't been created yet, external transforms depend on it"+ :data {:output {:db_id (mt/id), :schema "public", :table "global_output_table"}+ :transform {:type :workspace-transform, :id ref-id}+ :dependents [{:type :external-transform+ :id (:id external-tx)+ :name "External Transform"}]}}]+ (ws.validation/find-downstream-problems ws-id (build-graph! ws-id)))))))))))++(deftest no-problems-when-output-exists-and-no-field-changes-test+ (testing "no problems when isolated table exists and has all required fields"+ (ws.tu/with-workspaces! [workspace {:name "Test Workspace"}]+ (let [ws-id (:id workspace)]+ ;; Create both global and isolated tables with matching fields+ (mt/with-temp [:model/Table global-table {:db_id (mt/id)+ :schema "public"+ :name "output_table"+ :active true}+ :model/Field _ {:table_id (:id global-table)+ :name "col1"+ :database_type "TEXT"+ :base_type :type/Text+ :active true}+ :model/Table isolated-table {:db_id (mt/id)+ :schema "mb__isolation_test"+ :name "public__output_table"+ :active true}+ :model/Field _ {:table_id (:id isolated-table)+ :name "col1"+ :database_type "TEXT"+ :base_type :type/Text+ :active true}]+ ;; External transform that uses col1 via MBQL source-table+ (mt/with-temp [:model/Transform _external-tx {:name "External Transform"+ :source {:type :query+ :query {:database (mt/id)+ :type :query+ :query {:source-table (:id global-table)}}}+ :target {:type "table"+ :database (mt/id)+ :schema "public"+ :name "external_output"}}]+ ;; Create workspace transform and output+ (let [{ref-id :ref_id} (mt/user-http-request :crowberto :post 200+ (str "ee/workspace/" ws-id "/transform")+ {:name "Workspace Transform"+ :source {:type "query"+ :query {:database (mt/id)+ :type :native+ :native {:query "SELECT 'test' as col1"}}}+ :target {:type "table"+ :schema "public"+ :name "output_table"}})]+ ;; First trigger graph calculation to create the WorkspaceOutput row+ (build-graph! ws-id)++ ;; Now the row exists, so we can update it with our test table IDs+ (t2/update! :model/WorkspaceOutput+ {:workspace_id ws-id :ref_id ref-id}+ {:global_table_id (:id global-table)+ :isolated_table_id (:id isolated-table)})++ ;; Should have no problems since isolated table has all required fields+ (is (empty? (ws.validation/find-downstream-problems ws-id (build-graph! ws-id)))))))))))diff --git a/enterprise/frontend/src/metabase-enterprise/ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton.tsx b/enterprise/frontend/src/metabase-enterprise/ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton.tsxindex 8abb47d04ac..e2c340492b7 100644--- a/enterprise/frontend/src/metabase-enterprise/ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton.tsx+++ b/enterprise/frontend/src/metabase-enterprise/ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton.tsx@@ -5,12 +5,27 @@ import { useMetabotAgent } from "metabase-enterprise/metabot/hooks";import { trackQueryFixClicked } from "../../analytics";-export function FixSqlQueryButton() {+type FixSqlQueryButtonProps = {+ rawSql?: string | null;+ errorMessage?: string | null;+};++export function FixSqlQueryButton({+ rawSql,+ errorMessage,+}: FixSqlQueryButtonProps) {const { submitInput } = useMetabotAgent("omnibot");const handleClick = () => {trackQueryFixClicked();- submitInput("Fix this SQL query");+ const promptParts = ["Fix this SQL query"];+ if (errorMessage) {+ promptParts.push(`The database returned this error: ${errorMessage}`);+ }+ if (rawSql) {+ promptParts.push(`SQL:\n${rawSql}`);+ }+ submitInput(promptParts.join("\n\n"));};return <Button onClick={handleClick}>{t`Have Metabot fix it`}</Button>;diff --git a/enterprise/frontend/src/metabase-enterprise/api/index.ts b/enterprise/frontend/src/metabase-enterprise/api/index.tsindex d5c412f9ab8..84f72603397 100644--- a/enterprise/frontend/src/metabase-enterprise/api/index.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/index.ts@@ -20,3 +20,4 @@ export * from "./transform";export * from "./transform-job";export * from "./transform-tag";export * from "./upload-management";+export * from "./workspace";diff --git a/enterprise/frontend/src/metabase-enterprise/api/tags.ts b/enterprise/frontend/src/metabase-enterprise/api/tags.tsindex 5b71c65482e..9c8b808d340 100644--- a/enterprise/frontend/src/metabase-enterprise/api/tags.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/tags.ts@@ -18,6 +18,7 @@ import {type DependencyGraph,type DependencyNode,type DocumentDependencyNode,+ type ExternalTransform,type MeasureDependencyNode,type PythonLibrary,type SandboxDependencyNode,@@ -30,6 +31,9 @@ import {type TransformJob,type TransformRun,type TransformTag,+ type Workspace,+ type WorkspaceAllowedDatabase,+ type WorkspaceItem,} from "metabase-types/api";export const ENTERPRISE_TAG_TYPES = [@@ -44,6 +48,10 @@ export const ENTERPRISE_TAG_TYPES = ["transform-job","transform-job-via-tag","transform-run",+ "workspace-transforms",+ "workspace-transform",+ "workspace-tables",+ "external-transform","git-tree","git-file-content","collection-dirty-entities",@@ -52,6 +60,7 @@ export const ENTERPRISE_TAG_TYPES = ["remote-sync-current-task","remote-sync-has-remote-changes","python-transform-library",+ "workspace","support-access-grant","support-access-grant-current","library-collection",@@ -85,6 +94,18 @@ export function invalidateTags(return !error ? tags : [];}+export function provideWorkspacesTags(+ workspaces: Workspace[],+): TagDescription<EnterpriseTagType>[] {+ return [listTag("workspace"), ...workspaces.flatMap(provideWorkspaceTags)];+}++export function provideWorkspaceTags(+ workspace: Workspace | WorkspaceItem,+): TagDescription<EnterpriseTagType>[] {+ return [idTag("workspace", workspace.id)];+}+export function provideTransformTags(transform: Transform,): TagDescription<EnterpriseTagType>[] {@@ -127,6 +148,21 @@ export function provideTransformTagListTags(return [listTag("transform-tag"), ...tags.flatMap(provideTransformTagTags)];}+export function provideExternalTransformTags(+ transform: ExternalTransform,+): TagDescription<EnterpriseTagType>[] {+ return [idTag("external-transform", transform.id)];+}++export function provideExternalTransformListTags(+ transforms: ExternalTransform[],+): TagDescription<EnterpriseTagType>[] {+ return [+ listTag("external-transform"),+ ...transforms.flatMap(provideExternalTransformTags),+ ];+}+export function provideTransformJobTags(job: TransformJob,): TagDescription<EnterpriseTagType>[] {@@ -270,6 +306,8 @@ export function provideDependencyNodeTags(return provideSegmentDependencyNodeTags(node);case "measure":return provideMeasureDependencyNodeTags(node);+ case "workspace-transform":+ return [idTag("workspace-transform", node.id)];}}@@ -319,3 +357,12 @@ export function provideBulkTableSelectionInfoTags({...unpublished_upstream_tables.flatMap(provideBulkTableInfoTags),];}++export function provideWorkspaceAllowedDatabaseTags(+ databases: WorkspaceAllowedDatabase[],+) {+ return [+ listTag("database"),+ ...databases.map((db) => idTag("database", db.id)),+ ];+}diff --git a/enterprise/frontend/src/metabase-enterprise/api/transform.ts b/enterprise/frontend/src/metabase-enterprise/api/transform.tsindex ee762820064..74bdd141957 100644--- a/enterprise/frontend/src/metabase-enterprise/api/transform.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/transform.ts@@ -155,7 +155,10 @@ export const transformApi = EnterpriseApi.injectEndpoints({body,}),invalidatesTags: (_, error) =>- invalidateTags(error, [listTag("transform")]),+ invalidateTags(error, [+ listTag("transform"),+ listTag("external-transform"),+ ]),}),updateTransform: builder.mutation<Transform, UpdateTransformRequest>({query: ({ id, ...body }) => ({diff --git a/enterprise/frontend/src/metabase-enterprise/api/workspace.ts b/enterprise/frontend/src/metabase-enterprise/api/workspace.tsnew file mode 100644index 00000000000..7bc3eec8d99--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/api/workspace.ts@@ -0,0 +1,426 @@+import type {+ CreateWorkspaceRequest,+ CreateWorkspaceTransformRequest,+ CreateWorkspaceTransformResponse,+ ExternalTransformRequest,+ ExternalTransformResponse,+ TransformId,+ UpdateWorkspaceTransformRequest,+ ValidateTableNameRequest,+ ValidateTableNameResponse,+ Workspace,+ WorkspaceAllowedDatabasesResponse,+ WorkspaceCheckoutResponse,+ WorkspaceGraphResponse,+ WorkspaceId,+ WorkspaceListResponse,+ WorkspaceLogResponse,+ WorkspaceMergeResponse,+ WorkspaceProblem,+ WorkspaceRunRequest,+ WorkspaceRunResponse,+ WorkspaceTablesResponse,+ WorkspaceTransform,+ WorkspaceTransformDryRunResponse,+ WorkspaceTransformListItem,+ WorkspaceTransformListResponse,+ WorkspaceTransformMergeResponse,+ WorkspaceTransformRef,+ WorkspaceTransformRunResponse,+} from "metabase-types/api";++import { EnterpriseApi } from "./api";+import {+ idTag,+ invalidateTags,+ listTag,+ provideWorkspaceAllowedDatabaseTags,+ provideWorkspaceTags,+ provideWorkspacesTags,+ tag,+} from "./tags";++export const workspaceApi = EnterpriseApi.injectEndpoints({+ endpoints: (builder) => ({+ getWorkspaces: builder.query<WorkspaceListResponse, void>({+ query: () => ({+ method: "GET",+ url: "/api/ee/workspace",+ }),+ providesTags: (response) =>+ response ? provideWorkspacesTags(response.items) : [],+ }),+ getWorkspace: builder.query<Workspace, WorkspaceId>({+ query: (id) => ({+ method: "GET",+ url: `/api/ee/workspace/${id}`,+ }),+ providesTags: (workspace) =>+ workspace ? provideWorkspaceTags(workspace) : [],+ }),+ createWorkspace: builder.mutation<Workspace, CreateWorkspaceRequest | void>(+ {+ query: (body) => ({+ method: "POST",+ url: "/api/ee/workspace",+ body,+ }),+ invalidatesTags: (_, error) =>+ invalidateTags(error, [listTag("workspace"), listTag("transform")]),+ },+ ),+ updateWorkspace: builder.mutation<+ Workspace,+ { id: WorkspaceId; name?: string; database_id?: number }+ >({+ query: ({ id, ...body }) => ({+ method: "PUT",+ url: `/api/ee/workspace/${id}`,+ body,+ }),+ invalidatesTags: (_, error, { id }) =>+ invalidateTags(error, [idTag("workspace", id)]),+ }),+ createWorkspaceTransform: builder.mutation<+ WorkspaceTransform,+ CreateWorkspaceTransformRequest+ >({+ query: ({ id, ...body }) => ({+ method: "POST",+ url: `/api/ee/workspace/${id}/transform`,+ body,+ }),+ transformResponse: (+ response: CreateWorkspaceTransformResponse,+ ): WorkspaceTransform => ({+ ...response,+ type: "workspace-transform",+ }),+ invalidatesTags: (_, error, { id, global_id }) =>+ invalidateTags(error, [+ idTag("workspace", id),+ idTag("workspace-transforms", id),+ idTag("external-transform", id),+ idTag("workspace-tables", id),+ listTag("transform"),+ ...(global_id != null ? [idTag("transform", global_id)] : []),+ ]),+ }),+ getWorkspaceCheckout: builder.query<WorkspaceCheckoutResponse, TransformId>(+ {+ query: (transformId) => ({+ method: "GET",+ url: `/api/ee/workspace/checkout`,+ params: { "transform-id": transformId },+ }),+ providesTags: (_, __, transformId) => [idTag("transform", transformId)],+ },+ ),+ mergeWorkspace: builder.mutation<+ WorkspaceMergeResponse,+ { id: WorkspaceId; commit_message: string }+ >({+ query: ({ id, commit_message }) => ({+ method: "POST",+ url: `/api/ee/workspace/${id}/merge`,+ body: { commit_message },+ }),+ invalidatesTags: (_, error) =>+ invalidateTags(error, [tag("workspace"), tag("transform")]),+ }),+ mergeWorkspaceTransform: builder.mutation<+ WorkspaceTransformMergeResponse,+ WorkspaceTransformRef+ >({+ query: ({ workspaceId, transformId }) => ({+ method: "POST",+ url: `/api/ee/workspace/${workspaceId}/transform/${transformId}/merge`,+ }),+ invalidatesTags: (_, error) =>+ invalidateTags(error, [tag("workspace"), tag("transform")]),+ }),+ archiveWorkspace: builder.mutation<Workspace, WorkspaceId>({+ query: (id) => ({+ method: "POST",+ url: `/api/ee/workspace/${id}/archive`,+ }),+ invalidatesTags: (_, error) =>+ invalidateTags(error, [tag("workspace"), tag("transform")]),+ }),+ unarchiveWorkspace: builder.mutation<Workspace, WorkspaceId>({+ query: (id) => ({+ method: "POST",+ url: `/api/ee/workspace/${id}/unarchive`,+ }),+ invalidatesTags: (_, error) =>+ invalidateTags(error, [tag("workspace"), tag("transform")]),+ }),+ deleteWorkspace: builder.mutation<void, WorkspaceId>({+ query: (id) => ({+ method: "DELETE",+ url: `/api/ee/workspace/${id}`,+ }),+ async onQueryStarted(id, { dispatch, queryFulfilled }) {+ const patchResult = dispatch(+ workspaceApi.util.updateQueryData(+ "getWorkspaces",+ undefined,+ (draft) => {+ const idx = draft.items.findIndex((w) => w.id === id);+ if (idx !== -1) {+ draft.items.splice(idx, 1);+ }+ },+ ),+ );++ try {+ await queryFulfilled;+ } catch {+ patchResult.undo();+ }+ },+ invalidatesTags: (_, error) =>+ invalidateTags(error, [tag("workspace"), tag("transform")]),+ }),+ validateTableName: builder.mutation<+ ValidateTableNameResponse,+ ValidateTableNameRequest+ >({+ query: ({ id, ...body }) => ({+ method: "POST",+ url: `/api/ee/workspace/${id}/transform/validate/target`,+ body,+ }),+ }),+ getWorkspaceTables: builder.query<WorkspaceTablesResponse, WorkspaceId>({+ query: (id) => ({+ method: "GET",+ url: `/api/ee/workspace/${id}/table`,+ }),+ providesTags: (_, __, id) => [idTag("workspace-tables", id)],+ }),+ getWorkspaceGraph: builder.query<WorkspaceGraphResponse, WorkspaceId>({+ query: (id) => ({+ method: "GET",+ url: `/api/ee/workspace/${id}/graph`,+ }),+ providesTags: (_, __, id) => [idTag("workspace", id)],+ }),+ getWorkspaceProblems: builder.query<WorkspaceProblem[], WorkspaceId>({+ query: (id) => ({+ method: "GET",+ url: `/api/ee/workspace/${id}/problem`,+ }),+ providesTags: (_, __, id) => [idTag("workspace", id)],+ }),+ getWorkspaceTransforms: builder.query<+ WorkspaceTransformListItem[],+ WorkspaceId+ >({+ query: (id) => ({+ method: "GET",+ url: `/api/ee/workspace/${id}/transform`,+ }),+ providesTags: (_, __, id) => [idTag("workspace-transforms", id)],+ transformResponse: (response: WorkspaceTransformListResponse) =>+ response.transforms,+ }),+ getExternalTransforms: builder.query<+ ExternalTransformResponse["transforms"],+ ExternalTransformRequest+ >({+ query: ({ workspaceId, databaseId }) => ({+ method: "GET",+ url: `/api/ee/workspace/${workspaceId}/external/transform`,+ params: { database_id: databaseId },+ }),+ providesTags: (_, __, { workspaceId: id }) => [+ listTag("external-transform"),+ idTag("external-transform", id),+ ],+ transformResponse: (response: ExternalTransformResponse) =>+ response.transforms,+ }),+ getWorkspaceTransform: builder.query<+ WorkspaceTransform,+ { workspaceId: WorkspaceId; transformId: string }+ >({+ query: ({ workspaceId, transformId }) => ({+ method: "GET",+ url: `/api/ee/workspace/${workspaceId}/transform/${transformId}`,+ }),+ transformResponse: (+ response: Omit<WorkspaceTransform, "type">,+ ): WorkspaceTransform => ({+ ...response,+ type: "workspace-transform",+ }),+ providesTags: (_, __, { transformId }) => [+ idTag("workspace-transform", transformId),+ ],+ }),+ getWorkspaceLog: builder.query<WorkspaceLogResponse, WorkspaceId>({+ query: (id) => ({+ method: "GET",+ url: `/api/ee/workspace/${id}/log`,+ }),+ providesTags: (_, _error, id) => [idTag("workspace", id)],+ }),+ runWorkspace: builder.mutation<WorkspaceRunResponse, WorkspaceRunRequest>({+ query: ({ id, stale_only }) => ({+ method: "POST",+ url: `/api/ee/workspace/${id}/run`,+ params: stale_only ? { stale_only: 1 } : undefined,+ }),+ invalidatesTags: (_, error, { id }) =>+ invalidateTags(error, [+ idTag("workspace", id),+ idTag("workspace-transforms", id),+ idTag("workspace-tables", id),+ tag("workspace-transform"),+ tag("transform"),+ ]),+ }),+ runWorkspaceTransform: builder.mutation<+ WorkspaceTransformRunResponse,+ WorkspaceTransformRef+ >({+ query: ({ workspaceId, transformId }) => ({+ method: "POST",+ url: `/api/ee/workspace/${workspaceId}/transform/${transformId}/run`,+ }),+ invalidatesTags: (_, error, { workspaceId, transformId }) =>+ invalidateTags(error, [+ idTag("workspace", workspaceId),+ idTag("workspace-transforms", workspaceId),+ idTag("workspace-transform", transformId),+ idTag("workspace-tables", workspaceId),+ ]),+ }),+ dryRunWorkspaceTransform: builder.mutation<+ WorkspaceTransformDryRunResponse,+ WorkspaceTransformRef+ >({+ query: ({ workspaceId, transformId }) => ({+ method: "POST",+ url: `/api/ee/workspace/${workspaceId}/transform/${transformId}/dry-run`,+ }),+ }),+ updateWorkspaceTransform: builder.mutation<+ WorkspaceTransform,+ UpdateWorkspaceTransformRequest+ >({+ query: ({ workspaceId, transformId, ...body }) => ({+ method: "PUT",+ url: `/api/ee/workspace/${workspaceId}/transform/${transformId}`,+ body,+ }),+ transformResponse: (+ response: Omit<WorkspaceTransform, "type">,+ ): WorkspaceTransform => ({+ ...response,+ type: "workspace-transform",+ }),+ invalidatesTags: (_, error, { workspaceId, transformId }) =>+ invalidateTags(error, [+ idTag("workspace", workspaceId),+ idTag("workspace-transforms", workspaceId),+ idTag("workspace-transform", transformId),+ idTag("workspace-tables", workspaceId),+ ]),+ }),+ archiveWorkspaceTransform: builder.mutation<void, WorkspaceTransformRef>({+ query: ({ workspaceId, transformId }) => ({+ method: "POST",+ url: `/api/ee/workspace/${workspaceId}/transform/${transformId}/archive`,+ }),+ invalidatesTags: (_, error, { workspaceId, transformId }) =>+ invalidateTags(error, [+ idTag("workspace", workspaceId),+ idTag("workspace-transforms", workspaceId),+ idTag("workspace-transform", transformId),+ idTag("workspace-tables", workspaceId),+ tag("transform"),+ ]),+ }),+ unarchiveWorkspaceTransform: builder.mutation<void, WorkspaceTransformRef>({+ query: ({ workspaceId, transformId }) => ({+ method: "POST",+ url: `/api/ee/workspace/${workspaceId}/transform/${transformId}/unarchive`,+ }),+ invalidatesTags: (_, error, { workspaceId, transformId }) =>+ invalidateTags(error, [+ idTag("workspace", workspaceId),+ idTag("workspace-transforms", workspaceId),+ idTag("workspace-transform", transformId),+ idTag("workspace-tables", workspaceId),+ tag("transform"),+ ]),+ }),+ deleteWorkspaceTransform: builder.mutation<void, WorkspaceTransformRef>({+ query: ({ workspaceId, transformId }) => ({+ method: "DELETE",+ url: `/api/ee/workspace/${workspaceId}/transform/${transformId}`,+ }),+ invalidatesTags: (_, error, { workspaceId, transformId }) =>+ invalidateTags(error, [+ idTag("workspace", workspaceId),+ idTag("workspace-transforms", workspaceId),+ idTag("external-transform", workspaceId),+ idTag("workspace-transform", transformId),+ idTag("workspace-tables", workspaceId),+ tag("transform"),+ ]),+ }),+ getWorkspaceAllowedDatabases: builder.query<+ WorkspaceAllowedDatabasesResponse,+ void+ >({+ query: () => ({+ method: "GET",+ url: `/api/ee/workspace/database`,+ }),+ providesTags: (response) =>+ provideWorkspaceAllowedDatabaseTags(response?.databases ?? []),+ }),+ }),+});++export const {+ useGetWorkspacesQuery,+ useGetWorkspaceQuery,+ useGetWorkspaceTransformsQuery,+ useGetExternalTransformsQuery,+ useGetWorkspaceTransformQuery,+ useLazyGetWorkspaceTransformQuery,+ useCreateWorkspaceMutation,+ useCreateWorkspaceTransformMutation,+ useUpdateWorkspaceTransformMutation,+ useArchiveWorkspaceTransformMutation,+ useUnarchiveWorkspaceTransformMutation,+ useDeleteWorkspaceTransformMutation,+ useGetWorkspaceCheckoutQuery,+ useMergeWorkspaceMutation,+ useMergeWorkspaceTransformMutation,+ useArchiveWorkspaceMutation,+ useUnarchiveWorkspaceMutation,+ useDeleteWorkspaceMutation,+ useValidateTableNameMutation,+ useUpdateWorkspaceMutation,+ useGetWorkspaceTablesQuery,+ useLazyGetWorkspaceTablesQuery,+ useGetWorkspaceGraphQuery,+ useGetWorkspaceProblemsQuery,+ useGetWorkspaceLogQuery,+ useRunWorkspaceMutation,+ useRunWorkspaceTransformMutation,+ useDryRunWorkspaceTransformMutation,+ useGetWorkspaceAllowedDatabasesQuery,+} = workspaceApi;++export const DEFAULT_WORKSPACE_TABLES_QUERY_RESPONSE: WorkspaceTablesResponse =+ {+ inputs: [],+ outputs: [],+ };diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.module.css b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.module.cssindex 27aa0e18707..8c04de21a5e 100644--- a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.module.css+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.module.css@@ -48,6 +48,106 @@}}+.workspacesSection {+ border-top: 1px solid var(--mb-color-border);+ padding-top: 0.75rem;+}++.workspacesSectionHeader {+ background-color: var(--mb-color-background-secondary);+ color: var(--mb-color-text-primary);+ font-weight: 600;+ transition: background-color 150ms ease;++ &:hover {+ background-color: var(--mb-color-background-selected);+ color: var(--mb-color-brand);+ }++ &.selected {+ background-color: var(--mb-color-background-selected);+ color: var(--mb-color-brand);+ }+}++.workspaceItem {+ background-color: var(--mb-color-background-primary);+ color: var(--mb-color-text-primary);+ cursor: pointer;+ border: 1px solid var(--mb-color-border);+ transition:+ background-color 150ms ease,+ border-color 150ms ease,+ color 150ms ease;++ &:hover {+ background-color: var(--mb-color-background-secondary);+ border-color: var(--mb-color-border-strong);+ }++ &:hover .workspaceMenuButton {+ opacity: 1;+ }++ &.selected {+ background-color: var(--mb-color-background-hover);+ border-color: var(--mb-color-brand);+ }++ &.selected .workspaceMenuButton {+ opacity: 1;+ }++ &:disabled {+ opacity: 0.5;+ cursor: not-allowed;+ }+}++.workspaceMenuButton {+ color: var(--mb-color-text-secondary);+ opacity: 0;+ transition:+ opacity 150ms ease,+ background-color 150ms ease,+ color 150ms ease;+ flex-shrink: 0;++ &:hover {+ background-color: var(--mb-color-background-tertiary);+ color: var(--mb-color-text-primary);+ }+}++.newWorkspaceButton {+ background-color: transparent;+ color: var(--mb-color-text-primary);+ cursor: pointer;+ border: none;+ transition: color 150ms ease;++ &:hover {+ color: var(--mb-color-brand);+ }++ &:disabled {+ opacity: 0.5;+ cursor: not-allowed;+ }++ .hoverButton {+ &:hover {+ .showOnHover {+ opacity: 1;+ }++ .hideOnHover {+ opacity: 0;+ }+ }+ }+}+.logoWrapper {position: relative;diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.setup.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.setup.spec.tsxindex bfd70c709cd..df4241937b7 100644--- a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.setup.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.setup.spec.tsx@@ -6,10 +6,12 @@ import {setupRemoteSyncEndpoints,setupSettingsEndpoints,setupUserKeyValueEndpoints,+ setupWorkspacesEndpoint,} from "__support__/server-mocks";import { mockSettings } from "__support__/settings";import { renderWithProviders } from "__support__/ui";import { reinitialize } from "metabase/plugins";+import { hasPremiumFeature } from "metabase-enterprise/settings";import type { Collection, RemoteSyncEntity } from "metabase-types/api";import {createMockDirtyCardEntity,@@ -55,6 +57,14 @@ const createRemoteSyncSettings = ({"remote-sync-transforms": transforms,});+jest.mock("metabase-enterprise/settings", () => ({+ hasPremiumFeature: jest.fn(),+}));++export const mockHasPremiumFeature = hasPremiumFeature as jest.MockedFunction<+ typeof hasPremiumFeature+>;+// ============================================================================// Endpoint Setup Functions// ============================================================================@@ -69,9 +79,11 @@ const setupRemoteSyncSettingsEndpoints = (const setupDirtyEndpoints = ({dirty = [],collections = [],+ hasWorkspacesFeature = false,}: {dirty?: RemoteSyncEntity[];collections?: Collection[];+ hasWorkspacesFeature?: boolean;} = {}) => {const changedCollections: Record<number, boolean> = {};for (const entity of dirty) {@@ -87,6 +99,18 @@ const setupDirtyEndpoints = ({});setupCollectionsEndpoints({ collections });++ mockHasPremiumFeature.mockImplementation((feature) => {+ if (feature === "workspaces") {+ return hasWorkspacesFeature;+ }+ // Allow other features to pass (e.g., remote_sync for git sync tests)+ return true;+ });++ if (hasWorkspacesFeature) {+ setupWorkspacesEndpoint([]);+ }};const setupNavbarEndpoints = (isOpened = true) => {@@ -162,6 +186,7 @@ interface SetupOpts {hasTransformDirtyChanges?: boolean;remoteSyncTransforms?: boolean;isNavbarOpened?: boolean;+ hasWorkspacesFeature?: boolean;}export const setup = ({@@ -172,6 +197,7 @@ export const setup = ({hasTransformDirtyChanges = false,remoteSyncTransforms = false,isNavbarOpened = true,+ hasWorkspacesFeature = false,}: SetupOpts = {}) => {// Build collections listconst collections: Collection[] = [];@@ -200,7 +226,7 @@ export const setup = ({setupSettingsEndpoints([]);setupRemoteSyncSettingsEndpoints(remoteSyncSettings);- setupDirtyEndpoints({ dirty, collections });+ setupDirtyEndpoints({ dirty, collections, hasWorkspacesFeature });setupNavbarEndpoints(isNavbarOpened);setupLibraryEndpoints(false);diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.tsxindex 8b43aa4d191..72fb5e9b55f 100644--- a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.tsx+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.tsx@@ -32,8 +32,10 @@ import {Text,Tooltip,} from "metabase/ui";+import { hasPremiumFeature } from "metabase-enterprise/settings";import S from "./DataStudioLayout.module.css";+import { WorkspacesSection } from "./WorkspaceSection";import { getCurrentTab } from "./utils";type DataStudioLayoutProps = {@@ -113,7 +115,7 @@ function DataStudioNav({ isNavbarOpened, onNavbarToggle }: DataStudioNavProps) {justify="space-between"data-testid="data-studio-nav">- <Stack gap="0.75rem">+ <Stack gap="0.75rem" flex={1} mih={0}><DataStudioNavbarToggleisNavbarOpened={isNavbarOpened}onNavbarToggle={onNavbarToggle}@@ -181,6 +183,10 @@ function DataStudioNav({ isNavbarOpened, onNavbarToggle }: DataStudioNavProps) {}/>)}+ {(canAccessTransforms || isAdmin) &&+ hasPremiumFeature("workspaces") && (+ <WorkspacesSection showLabel={isNavbarOpened} />+ )}</Stack><Stack gap="0.75rem">{hasRemoteSyncFeature ? (diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.unit.spec.tsxindex e0a63f18d9d..29b0318074e 100644--- a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/DataStudioLayout.unit.spec.tsx@@ -3,6 +3,8 @@ import fetchMock from "fetch-mock";import { screen, waitFor, within } from "__support__/ui";+import { mockHasPremiumFeature, setup } from "./DataStudioLayout.setup.spec";+// Mock useHasTokenFeature to return true for required featuresjest.mock("metabase/common/hooks/use-has-token-feature/use-has-token-feature",@@ -20,8 +22,6 @@ jest.mock(}),);-import { setup } from "./DataStudioLayout.setup.spec";-describe("DataStudioLayout", () => {beforeEach(() => {fetchMock.removeRoutes();@@ -209,4 +209,34 @@ describe("DataStudioLayout", () => {).not.toBeInTheDocument();});});++ describe("workspaces feature", () => {+ afterEach(() => {+ mockHasPremiumFeature.mockReset();+ });++ it("should not render WorkspacesSection when workspaces feature is not available", async () => {+ setup({ hasWorkspacesFeature: false, isNavbarOpened: true });++ await waitFor(() => {+ expect(screen.getByTestId("data-studio-nav")).toBeInTheDocument();+ });++ expect(+ screen.queryByTestId("workspaces-section"),+ ).not.toBeInTheDocument();+ expect(screen.queryByText("Workspaces")).not.toBeInTheDocument();+ });++ it("should render WorkspacesSection when workspaces feature is available", async () => {+ setup({ hasWorkspacesFeature: true, isNavbarOpened: true });++ await waitFor(() => {+ expect(screen.getByTestId("data-studio-nav")).toBeInTheDocument();+ });++ expect(screen.getByTestId("workspaces-section")).toBeInTheDocument();+ expect(screen.getByText("Workspaces")).toBeInTheDocument();+ });+ });});diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/WorkspaceSection.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/WorkspaceSection.tsxnew file mode 100644index 00000000000..540960cb4db--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/WorkspaceSection.tsx@@ -0,0 +1,391 @@+import { useDisclosure } from "@mantine/hooks";+import cx from "classnames";+import dayjs from "dayjs";+import { useCallback, useMemo, useState } from "react";+import { push } from "react-router-redux";+import { t } from "ttag";++import { ConfirmModal } from "metabase/common/components/ConfirmModal/ConfirmModal";+import { ForwardRefLink } from "metabase/common/components/Link";+import type { MetabaseColorKey } from "metabase/lib/colors/types";+import { useDispatch, useSelector } from "metabase/lib/redux";+import * as Urls from "metabase/lib/urls";+import { useMetadataToasts } from "metabase/metadata/hooks";+import { getLocation } from "metabase/selectors/routing";+import {+ ActionIcon,+ FixedSizeIcon,+ Flex,+ Group,+ Icon,+ type IconName,+ Loader,+ Menu,+ Skeleton,+ Stack,+ Text,+ Tooltip,+ UnstyledButton,+} from "metabase/ui";+import {+ useArchiveWorkspaceMutation,+ useCreateWorkspaceMutation,+ useDeleteWorkspaceMutation,+ useGetWorkspacesQuery,+ useUnarchiveWorkspaceMutation,+} from "metabase-enterprise/api/workspace";+import type {+ Workspace,+ WorkspaceId,+ WorkspaceItem as WorkspaceItemType,+} from "metabase-types/api";++import S from "./DataStudioLayout.module.css";++const TOOLTIP_OPEN_DELAY = 700;++type WorkspacesSectionProps = {+ showLabel: boolean;+};++function WorkspacesSection({ showLabel }: WorkspacesSectionProps) {+ const dispatch = useDispatch();+ const [isWorkspacesExpanded, setIsWorkspacesExpanded] = useState(true);+ const { pathname } = useSelector(getLocation);+ const { data: workspacesData, isLoading: areWorkspacesLoading } =+ useGetWorkspacesQuery();+ const [createWorkspace, { isLoading: isCreatingWorkspace }] =+ useCreateWorkspaceMutation();++ const workspaces = useMemo(() => {+ return sortWorkspaceItems(workspacesData?.items ?? []);+ }, [workspacesData]);++ const { sendErrorToast, sendSuccessToast } = useMetadataToasts();++ const handleCreateWorkspace = useCallback(async () => {+ try {+ const workspace = await createWorkspace().unwrap();+ dispatch(push(Urls.dataStudioWorkspace(workspace.id)));+ } catch (error) {+ sendErrorToast(t`Failed to create workspace`);+ }+ }, [createWorkspace, dispatch, sendErrorToast]);++ const [archiveWorkspace] = useArchiveWorkspaceMutation();+ const [unarchiveWorkspace] = useUnarchiveWorkspaceMutation();+ const [deleteWorkspace] = useDeleteWorkspaceMutation();++ const handleWorkspaceArchive = async (id: WorkspaceId) => {+ try {+ await archiveWorkspace(id).unwrap();+ sendSuccessToast(t`Workspace archived successfully`);+ } catch (error) {+ sendErrorToast(t`Failed to archive workspace`);+ }+ };++ const handleWorkspaceUnarchive = async (id: WorkspaceId) => {+ try {+ await unarchiveWorkspace(id).unwrap();+ sendSuccessToast(t`Workspace restored successfully`);+ } catch (error) {+ sendErrorToast(t`Failed to restore workspace`);+ }+ };++ const handleWorkspaceDelete = async (id: WorkspaceId) => {+ try {+ await deleteWorkspace(id).unwrap();+ sendSuccessToast(t`Workspace deleted successfully`);+ dispatch(push(Urls.dataStudioWorkspaceList()));+ } catch (error) {+ sendErrorToast(t`Failed to delete workspace`);+ }+ };++ const isWorkspaceListPage = pathname === Urls.dataStudioWorkspaceList();++ if (!showLabel) {+ // In collapsed mode, show only an icon button with tooltip+ return (+ <Tooltip+ label={t`Workspaces`}+ position="right"+ openDelay={TOOLTIP_OPEN_DELAY}+ >+ <UnstyledButton+ className={cx(S.tab, { [S.selected]: isWorkspaceListPage })}+ component={ForwardRefLink}+ to={Urls.dataStudioWorkspaceList()}+ p="0.75rem"+ bdrs="md"+ >+ <Flex align="center" justify="center">+ <FixedSizeIcon name="git_branch" display="block" />+ </Flex>+ </UnstyledButton>+ </Tooltip>+ );+ }++ return (+ <Stack+ data-testid="workspaces-section"+ gap="0.5rem"+ className={S.workspacesSection}+ flex={1}+ mih={0}+ >+ <UnstyledButton+ className={cx(S.workspacesSectionHeader, {+ [S.selected]: isWorkspaceListPage,+ })}+ onClick={() => setIsWorkspacesExpanded(!isWorkspacesExpanded)}+ p="0.75rem"+ bdrs="md"+ >+ <Flex align="center" justify="space-between" w="100%">+ <Text size="sm" fw={600}>+ {t`Workspaces`}+ </Text>+ <Icon+ name={isWorkspacesExpanded ? "chevrondown" : "chevronright"}+ size={16}+ />+ </Flex>+ </UnstyledButton>++ {isWorkspacesExpanded && (+ <>+ <UnstyledButton+ className={S.newWorkspaceButton}+ onClick={handleCreateWorkspace}+ disabled={isCreatingWorkspace || areWorkspacesLoading}+ p="0.5rem"+ bdrs="md"+ >+ <Flex align="center" gap="xs">+ {isCreatingWorkspace ? (+ <Loader size="xs" />+ ) : (+ <Icon name="add" size={16} />+ )}+ <Text c="inherit" size="sm" fw={500}>+ {t`New workspace`}+ </Text>+ </Flex>+ </UnstyledButton>+ <Stack+ gap="0.75rem"+ style={{ flex: 1, overflowY: "auto", minHeight: 0 }}+ >+ {areWorkspacesLoading ? (+ <>+ <Skeleton height={80} radius="md" />+ <Skeleton height={80} radius="md" />+ </>+ ) : workspaces.length === 0 ? (+ <Text c="text-secondary" size="xs" px="0.5rem">+ {t`No workspaces yet`}+ </Text>+ ) : (+ workspaces.map((workspace) => {+ const isSelected =+ pathname === Urls.dataStudioWorkspace(workspace.id);++ return (+ <WorkspaceItem+ key={workspace.id}+ workspace={workspace}+ isSelected={isSelected}+ onArchive={handleWorkspaceArchive}+ onUnarchive={handleWorkspaceUnarchive}+ onDelete={handleWorkspaceDelete}+ />+ );+ })+ )}+ </Stack>+ </>+ )}+ </Stack>+ );+}++export { WorkspacesSection };++interface WorkspaceItemProps {+ workspace: Workspace;+ isSelected: boolean;+ onArchive: (workspaceId: WorkspaceId) => Promise<void>;+ onUnarchive: (workspaceId: WorkspaceId) => Promise<void>;+ onDelete: (workspaceId: WorkspaceId) => Promise<void>;+}++function sortWorkspaceItems(+ items: WorkspaceItemType[] | undefined,+): WorkspaceItemType[] {+ const isArchived = (item: WorkspaceItemType) => item.status === "archived";++ return [...(items ?? [])].sort((a, b) => {+ // Archived workspaces should be placed at the end+ if (isArchived(a) !== isArchived(b)) {+ return isArchived(a) ? 1 : -1;+ }++ // Within each group (archived/unarchived), sort by updated_at descending+ const aDate = a.updated_at ? new Date(a.updated_at).getTime() : 0;+ const bDate = b.updated_at ? new Date(b.updated_at).getTime() : 0;+ return bDate - aDate;+ });+}++function WorkspaceItem({+ workspace,+ isSelected,+ onArchive,+ onUnarchive,+ onDelete,+}: WorkspaceItemProps) {+ const [+ deleteModalOpened,+ { open: openDeleteModal, close: closeDeleteModal },+ ] = useDisclosure();+ const timeAgo = workspace.updated_at+ ? dayjs(workspace.updated_at).fromNow()+ : null;++ const handleArchive = () => {+ onArchive(workspace.id);+ };++ const handleUnarchive = () => {+ onUnarchive(workspace.id);+ };++ const handleDelete = () => {+ openDeleteModal();+ };++ const handleConfirmDelete = async () => {+ await onDelete(workspace.id);+ closeDeleteModal();+ };++ const status = getWorkspaceListStatus(workspace);++ return (+ <UnstyledButton+ className={cx(S.workspaceItem, { [S.selected]: isSelected })}+ component={ForwardRefLink}+ to={Urls.dataStudioWorkspace(workspace.id)}+ p="0.75rem"+ bdrs="md"+ name={workspace.name}+ >+ <Flex align="flex-start" justify="space-between" gap="xs">+ <Stack gap="xs" style={{ flex: 1, minWidth: 0 }}>+ <Text size="sm" fw={600} truncate>+ {workspace.name}+ </Text>+ {status && (+ <Group+ gap="xs"+ align="center"+ wrap="nowrap"+ data-testid="workspace-status"+ >+ <Icon name={status.icon} size={10} c={status.color} />+ <Text size="xs" fw={500} c={status.color}>+ {status.label}+ </Text>+ </Group>+ )}+ {timeAgo && (+ <Text size="xs" c="text-secondary" truncate>+ {t`Updated ${timeAgo}`}+ </Text>+ )}+ </Stack>+ <Menu position="right" withinPortal>+ <Menu.Target>+ <ActionIcon+ className={S.workspaceMenuButton}+ component="span"+ aria-label={t`More actions`}+ onClick={(e: React.MouseEvent) => {+ e.preventDefault();+ e.stopPropagation();+ }}+ size="sm"+ variant="subtle"+ >+ <Icon name="ellipsis" size={16} />+ </ActionIcon>+ </Menu.Target>+ <Menu.Dropdown+ onClick={(e: React.MouseEvent) => {+ e.preventDefault();+ e.stopPropagation();+ }}+ >+ {workspace.status === "archived" ? (+ <>+ <Menu.Item+ leftSection={<Icon name="revert" />}+ onClick={handleUnarchive}+ >+ {t`Restore`}+ </Menu.Item>+ <Menu.Divider />+ <Menu.Item+ c="error"+ leftSection={<Icon name="trash" />}+ onClick={handleDelete}+ color="danger"+ >+ {t`Delete`}+ </Menu.Item>+ </>+ ) : (+ <Menu.Item+ leftSection={<Icon name="archive" />}+ onClick={handleArchive}+ >+ {t`Archive`}+ </Menu.Item>+ )}+ </Menu.Dropdown>+ </Menu>+ </Flex>++ <ConfirmModal+ confirmButtonText={t`Delete`}+ message={t`This can't be undone.`}+ opened={deleteModalOpened}+ title={t`Delete ${workspace.name}?`}+ onClose={closeDeleteModal}+ onConfirm={handleConfirmDelete}+ />+ </UnstyledButton>+ );+}++type WorkspaceListStatus = {+ label: string;+ icon: IconName;+ color: MetabaseColorKey;+};++function getWorkspaceListStatus(workspace: Workspace): WorkspaceListStatus {+ if (workspace.status === "archived") {+ return { label: t`Archived`, icon: "archive", color: "text-tertiary" };+ }++ if (workspace.status === "pending") {+ return { label: t`Pending setup`, icon: "clock", color: "warning" };+ }++ return { label: t`Ready`, icon: "check", color: "success" };+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/utils.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/utils.tsindex a88c5788830..116306d60d4 100644--- a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/utils.ts+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/DataStudioLayout/utils.ts@@ -7,6 +7,7 @@ type TabName =| "jobs"| "runs"| "dependencies"+ | "workspace"| "dependency-diagnostics"| "glossary"| "git-sync";@@ -29,6 +30,8 @@ export const getCurrentTab = (pathname: string): TabName => {return "runs";case pathname.startsWith(Urls.transformList()):return "transforms";+ case pathname.startsWith(Urls.dataStudioWorkspaceList()):+ return "workspace";default:return "data";}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/WorkspacesSectionLayout/WorkspacesSectionLayout.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/WorkspacesSectionLayout/WorkspacesSectionLayout.tsxnew file mode 100644index 00000000000..4b5d18cf39e--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/WorkspacesSectionLayout/WorkspacesSectionLayout.tsx@@ -0,0 +1,17 @@+import { t } from "ttag";++import { usePageTitle } from "metabase/hooks/use-page-title";++import { SectionLayout } from "../../components/SectionLayout";++type WorkspacesSectionLayoutProps = {+ children?: React.ReactNode;+};++export function WorkspacesSectionLayout({+ children,+}: WorkspacesSectionLayoutProps) {+ usePageTitle(t`Workspaces`);++ return <SectionLayout>{children}</SectionLayout>;+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/WorkspacesSectionLayout/index.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/WorkspacesSectionLayout/index.tsnew file mode 100644index 00000000000..85d3fb9745d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/app/pages/WorkspacesSectionLayout/index.ts@@ -0,0 +1 @@+export * from "./WorkspacesSectionLayout";diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/routes.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/routes.tsxindex d610a175521..ad68904f78b 100644--- a/enterprise/frontend/src/metabase-enterprise/data-studio/routes.tsx+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/routes.tsx@@ -8,6 +8,7 @@ import {PLUGIN_FEATURE_LEVEL_PERMISSIONS,PLUGIN_TRANSFORMS,} from "metabase/plugins";+import { hasPremiumFeature } from "metabase-enterprise/settings";import type { State } from "metabase-types/store";import { DataSectionLayout } from "./app/pages/DataSectionLayout";@@ -17,6 +18,7 @@ import { DependencyDiagnosticsSectionLayout } from "./app/pages/DependencyDiagnoimport { GitSyncSectionLayout } from "./app/pages/GitSyncSectionLayout";import { LibrarySectionLayout } from "./app/pages/LibrarySectionLayout";import { TransformsSectionLayout } from "./app/pages/TransformsSectionLayout";+import { WorkspacesSectionLayout } from "./app/pages/WorkspacesSectionLayout";import { getDataStudioMetadataRoutes } from "./data-model/routes";import { getDataStudioGlossaryRoutes } from "./glossary/routes";import { getDataStudioMetricRoutes } from "./metrics/routes";@@ -25,6 +27,7 @@ import { getDataStudioSnippetRoutes } from "./snippets/routes";import { getDataStudioTableRoutes } from "./tables/routes";import { DependenciesUpsellPage } from "./upsells";import { DependencyDiagnosticsUpsellPage } from "./upsells/DependencyDiagnosticsUpsellPage";+import { getDataStudioWorkspaceRoutes } from "./workspaces/routes";export function getDataStudioRoutes(store: Store<State>,@@ -56,6 +59,11 @@ export function getDataStudioRoutes({getDataStudioSegmentRoutes()}{getDataStudioSnippetRoutes()}</Route>+ {hasPremiumFeature("workspaces") && (+ <Route path="workspaces" component={WorkspacesSectionLayout}>+ {getDataStudioWorkspaceRoutes()}+ </Route>+ )}{PLUGIN_DEPENDENCIES.isEnabled ? (<Route path="dependencies" component={DependenciesSectionLayout}>{PLUGIN_DEPENDENCIES.getDataStudioDependencyRoutes()}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/admin/AdminDatabaseWorkspacesSection.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/admin/AdminDatabaseWorkspacesSection.tsxnew file mode 100644index 00000000000..108169216ba--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/admin/AdminDatabaseWorkspacesSection.tsx@@ -0,0 +1,179 @@+import { type ChangeEvent, useState } from "react";+import { t } from "ttag";++import {+ Description,+ Error,+ Label,+} from "metabase/admin/databases/components/DatabaseFeatureComponents";+import { DatabaseInfoSection } from "metabase/admin/databases/components/DatabaseInfoSection";+import { hasFeature } from "metabase/admin/databases/utils";+import { useCheckWorkspacePermissionsMutation } from "metabase/api";+import { getErrorMessage } from "metabase/api/utils";+import { Group, List, Loader, Stack, Switch } from "metabase/ui";+import type {+ Database,+ DatabaseData,+ DatabaseId,+ DatabaseLocalSettingAvailability,+} from "metabase-types/api";++import { DATABASE_WORKSPACES_SETTING } from "../settings";++export function AdminDatabaseWorkspacesSection({+ database,+ settingsAvailable,+ updateDatabase,+}: {+ database: Database;+ settingsAvailable?: Record<string, DatabaseLocalSettingAvailability>;+ updateDatabase: (+ database: { id: DatabaseId } & Partial<DatabaseData>,+ ) => Promise<void>;+}) {+ const isEnabled = Boolean(database.settings?.[DATABASE_WORKSPACES_SETTING]);+ const workspacesSetting = settingsAvailable?.[DATABASE_WORKSPACES_SETTING];+ const isSettingDisabled = !workspacesSetting;+ const areWorkspacesSupported = hasFeature(database, "workspace");++ const [error, setError] = useState<string | null>(null);+ const [isCheckingPermissions, setIsCheckingPermissions] = useState(false);+ const [isUpdating, setIsUpdating] = useState(false);+ const [permissionsError, setPermissionsError] = useState<string | null>(null);++ const [checkWorkspacePermissions] = useCheckWorkspacePermissionsMutation();++ const enableWorkspaces = async () => {+ setIsUpdating(true);++ try {+ await updateDatabase({+ id: database.id,+ settings: { [DATABASE_WORKSPACES_SETTING]: true },+ });+ } finally {+ setIsUpdating(false);+ }+ };++ const disableWorkspaces = async () => {+ setIsUpdating(true);++ try {+ await updateDatabase({+ id: database.id,+ settings: { [DATABASE_WORKSPACES_SETTING]: false },+ });+ } finally {+ setIsUpdating(false);+ }+ };++ const tryToEnableWorkspaces = async () => {+ setIsCheckingPermissions(true);++ try {+ const result = await checkWorkspacePermissions({+ id: database.id,+ cached: false,+ }).unwrap();++ if (result.status === "ok") {+ await enableWorkspaces();+ } else {+ setPermissionsError(+ getErrorMessage(+ result.error,+ t`This database connection does not have sufficient permissions`,+ ),+ );+ }+ } catch (error) {+ setPermissionsError(+ getErrorMessage(+ error,+ t`Failed to check workspace permissions for this database`,+ ),+ );+ } finally {+ setIsCheckingPermissions(false);+ }+ };++ const handleToggle = async (event: ChangeEvent<HTMLInputElement>) => {+ try {+ setError(null);+ setPermissionsError(null);++ if (event.target.checked) {+ await tryToEnableWorkspaces();+ } else {+ await disableWorkspaces();+ }+ } catch (error) {+ setError(getErrorMessage(error));+ }+ };++ if (!areWorkspacesSupported || isSettingDisabled) {+ return null;+ }++ return (+ <DatabaseInfoSection+ data-testid="database-workspaces-section"+ description={t`Enable workspaces for this database to create isolated data environments for transforms.`}+ name={t`Workspaces`}+ >+ <Stack gap="md">+ <Group align="center" justify="space-between" wrap="nowrap">+ <Label htmlFor="workspaces-toggle">{t`Enable workspaces`}</Label>++ <Group align="center" gap="md" wrap="nowrap">+ {(isCheckingPermissions || isUpdating) && <Loader size="sm" />}++ <Switch+ checked={isEnabled}+ disabled={isCheckingPermissions}+ id="workspaces-toggle"+ onChange={handleToggle}+ />+ </Group>+ </Group>++ <Stack maw="22.5rem">+ {!permissionsError && (+ <Description>+ {t`Your database connection will need permissions to create users, schemas, and grant table permissions.`}+ </Description>+ )}++ {permissionsError && (+ <Stack c="text-secondary" gap="xs" lh={1.4}>+ {t`To enable workspaces, the database connection user needs permissions to:`}++ <List>+ <List.Item+ c="text-secondary"+ lh={1.4}+ >{t`Create users and schemas`}</List.Item>+ <List.Item+ c="text-secondary"+ lh={1.4}+ >{t`Grant table permissions`}</List.Item>+ <List.Item+ c="text-secondary"+ lh={1.4}+ >{t`Manage database resources`}</List.Item>+ </List>+ </Stack>+ )}++ {error && <Error>{error}</Error>}++ {permissionsError && <Error>{permissionsError}</Error>}+ </Stack>+ </Stack>+ </DatabaseInfoSection>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/MergeWorkspaceModal.module.css b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/MergeWorkspaceModal.module.cssnew file mode 100644index 00000000000..a6494638bba--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/MergeWorkspaceModal.module.css@@ -0,0 +1,31 @@+.sidebarItem {+ cursor: pointer;+ border-left: 2px solid transparent;++ &:hover {+ background: var(--mb-color-background-secondary);+ }+}++.sidebarItemActive {+ background: var(--mb-color-background-secondary);+ border-left-color: var(--mb-color-brand);+}++.diffEditor {+ height: 100%;++ :global {+ .cm-content {+ padding: 0;+ }++ .cm-collapsedLines {+ padding-left: 0;+ }++ .cm-scroller {+ font-family: monospace;+ }+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/MergeWorkspaceModal.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/MergeWorkspaceModal.tsxnew file mode 100644index 00000000000..23c257da5ae--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/MergeWorkspaceModal.tsx@@ -0,0 +1,245 @@+import cx from "classnames";+import { useMemo, useState } from "react";+import { t } from "ttag";+import * as Yup from "yup";++import { Form, FormProvider, FormSubmitButton } from "metabase/forms";+import { useMetadataToasts } from "metabase/metadata/hooks";+import { Box, Flex, Icon, Modal, Text, Title } from "metabase/ui";+import { useGetWorkspaceProblemsQuery } from "metabase-enterprise/api";+import type {+ WorkspaceId,+ WorkspaceTransformListItem,+} from "metabase-types/api";++import S from "./MergeWorkspaceModal.module.css";+import { DiffView } from "./components/DiffView";+import { OverviewPanel } from "./components/OverviewPanel";+import { TransformListItem } from "./components/TransformListItem";++type MergeWorkspaceModalProps = {+ onClose: VoidFunction;+ onSubmit: (commitMessage: string) => Promise<void>;+ isLoading?: boolean;+ isDisabled?: boolean;+ workspaceId: WorkspaceId;+ workspaceName: string;+ workspaceTransforms: WorkspaceTransformListItem[];+};++export type MergeWorkspaceFormValues = {+ commit_message: string;+};++export const initialFormValues: MergeWorkspaceFormValues = {+ commit_message: "",+};+export const MAX_COMMIT_MESSAGE_LENGTH = 255;++const getMergeWorkspaceSchema = () =>+ Yup.object().shape({+ commit_message: Yup.string()+ .trim()+ .max(MAX_COMMIT_MESSAGE_LENGTH, t`Commit message is too long.`)+ .required(t`Please provide a commit message.`),+ });++export const MergeWorkspaceModal = ({+ onClose,+ onSubmit,+ isLoading = false,+ isDisabled = false,+ workspaceId,+ workspaceName,+ workspaceTransforms,+}: MergeWorkspaceModalProps) => {+ const { sendErrorToast } = useMetadataToasts();+ const validationSchema = useMemo(() => getMergeWorkspaceSchema(), []);++ const {+ data: workspaceProblems = [],+ isFetching: isLoadingWorkspaceProblems,+ } = useGetWorkspaceProblemsQuery(workspaceId);+ const hasBlockingProblems = useMemo(+ () => workspaceProblems.some((p) => p.block_merge === true),+ [workspaceProblems],+ );++ const updatedTransforms = useMemo(+ () => workspaceTransforms.filter((t) => t.global_id != null),+ [workspaceTransforms],+ );++ const newTransforms = useMemo(+ () => workspaceTransforms.filter((t) => t.global_id == null),+ [workspaceTransforms],+ );++ const [selectedTransformId, setSelectedTransformId] = useState<+ string | "overview"+ >("overview");++ const selectedTransform =+ selectedTransformId !== "overview"+ ? workspaceTransforms.find((t) => t.ref_id === selectedTransformId)+ : null;++ const handleSubmit = async (values: MergeWorkspaceFormValues) => {+ try {+ await onSubmit(values.commit_message.trim());+ onClose();+ } catch (error) {+ sendErrorToast(t`Failed to merge workspace`);+ throw error;+ }+ };++ return (+ <Modal+ data-testid="review-changes-modal"+ onClose={onClose}+ opened+ title={+ <Flex align="center" gap="sm">+ <Icon name="git_branch" c="brand" />+ <Flex direction="column" gap="xs">+ <Title order={3}>{t`Review Changes`}</Title>+ <Text size="sm" c="text-secondary">+ {workspaceName}+ </Text>+ </Flex>+ </Flex>+ }+ size="xl"+ padding={0}+ styles={{+ header: {+ padding: "1rem 1.5rem",+ borderBottom: "1px solid var(--mb-color-border)",+ },+ }}+ >+ <FormProvider+ initialValues={initialFormValues}+ validationSchema={validationSchema}+ onSubmit={handleSubmit}+ >+ {({ values, touched, errors }) => (+ <Form style={{ display: "flex", flexDirection: "column" }}>+ <Flex h={540}>+ <Box+ pt="md"+ w={220}+ miw={220}+ style={{+ borderRight: "1px solid var(--mb-color-border)",+ overflowY: "auto",+ }}+ >+ <Flex+ align="center"+ gap="sm"+ px="md"+ py="sm"+ className={cx(+ S.sidebarItem,+ selectedTransformId === "overview" && S.sidebarItemActive,+ )}+ onClick={() => setSelectedTransformId("overview")}+ >+ <Icon name="document" size={14} c="text-secondary" />+ <Text>{t`Overview`}</Text>+ </Flex>+ {newTransforms.length > 0 && (+ <>+ <Text+ px="md"+ py="sm"+ fz="xs"+ fw={700}+ tt="uppercase"+ c="text-secondary"+ >+ {t`New transforms`}+ </Text>+ {newTransforms.map((transform) => (+ <TransformListItem+ key={transform.ref_id}+ transform={transform}+ isSelected={selectedTransformId === transform.ref_id}+ onClick={() => setSelectedTransformId(transform.ref_id)}+ workspaceId={workspaceId}+ />+ ))}+ </>+ )}+ {updatedTransforms.length > 0 && (+ <>+ <Text+ px="md"+ py="sm"+ fz="xs"+ fw={700}+ tt="uppercase"+ c="text-secondary"+ >+ {t`Modified transforms`}+ </Text>+ {updatedTransforms.map((transform) => (+ <TransformListItem+ key={transform.ref_id}+ transform={transform}+ isSelected={selectedTransformId === transform.ref_id}+ onClick={() => setSelectedTransformId(transform.ref_id)}+ workspaceId={workspaceId}+ />+ ))}+ </>+ )}+ </Box>+ <Box flex={1} miw={0} style={{ overflow: "auto" }}>+ {selectedTransformId === "overview" ? (+ <OverviewPanel+ commitMessageLength={values.commit_message?.length}+ hasCommitMessageError={!!errors.commit_message}+ workspaceId={workspaceId}+ transformCount={workspaceTransforms.length}+ />+ ) : selectedTransform ? (+ <DiffView+ key={selectedTransform.ref_id}+ transform={selectedTransform}+ workspaceId={workspaceId}+ />+ ) : (+ <Flex align="center" justify="center" h="100%">+ <Text c="text-secondary">{t`Transform not found`}</Text>+ </Flex>+ )}+ </Box>+ </Flex>+ <Flex+ justify="flex-end"+ align="center"+ p="md"+ style={{ borderTop: "1px solid var(--mb-color-border)" }}+ >+ <FormSubmitButton+ disabled={+ !values.commit_message?.trim() ||+ (touched.commit_message && !!errors.commit_message) ||+ hasBlockingProblems ||+ isLoadingWorkspaceProblems ||+ isDisabled+ }+ label={t`Merge`}+ loading={isLoading}+ variant="filled"+ />+ </Flex>+ </Form>+ )}+ </FormProvider>+ </Modal>+ );+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/DiffView.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/DiffView.tsxnew file mode 100644index 00000000000..e49da4ad49d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/DiffView.tsx@@ -0,0 +1,131 @@+import { unifiedMergeView } from "@codemirror/merge";+import cx from "classnames";+import { useMemo } from "react";+import { t } from "ttag";+import _ from "underscore";++import { CodeMirror } from "metabase/common/components/CodeMirror";+import EditorS from "metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/CodeMirrorEditor.module.css";+import { Flex, Loader, Stack, Text } from "metabase/ui";+import type {+ WorkspaceId,+ WorkspaceTransformListItem,+} from "metabase-types/api";++import S from "../MergeWorkspaceModal.module.css";+import { useTransformSources } from "../hooks/useTransformSources";+import { areSourceTablesEqual } from "../utils";++import { TableDiff } from "./TableDiff";+import { TransformSourceTablesDiff } from "./TransformSourceTablesDiff";++type DiffViewProps = {+ transform: WorkspaceTransformListItem;+ workspaceId: WorkspaceId;+};++export const DiffView = ({ transform, workspaceId }: DiffViewProps) => {+ const {+ oldSource,+ oldSourceTables,+ oldTarget,+ newSource,+ newSourceTables,+ newTarget,+ isLoading,+ hasError,+ } = useTransformSources(workspaceId, transform);++ const extensions = useMemo(+ () =>+ _.compact([+ oldSource &&+ unifiedMergeView({+ original: oldSource,+ mergeControls: false,+ }),+ ]),+ [oldSource],+ );++ if (isLoading) {+ return (+ <Flex+ align="center"+ justify="center"+ h="100%"+ direction="column"+ gap="sm"+ >+ <Loader size="sm" />+ <Text c="text-secondary">{t`Loading diff...`}</Text>+ </Flex>+ );+ }++ if (hasError) {+ return (+ <Flex align="center" justify="center" h="100%">+ <Text c="danger">{t`Failed to load diff`}</Text>+ </Flex>+ );+ }++ const schemaChanged = oldTarget?.schema !== newTarget?.schema;+ const tableChanged = oldTarget?.name !== newTarget?.name;+ const targetChanged = schemaChanged || tableChanged;+ const sourceTablesChanged = !areSourceTablesEqual(+ oldSourceTables,+ newSourceTables,+ );++ return (+ <Stack gap={0} h="100%">+ {oldTarget && newTarget && targetChanged && (+ <Stack+ data-testid="transform-target-diff"+ gap="xs"+ px="md"+ py="sm"+ style={{+ borderBottom: "1px solid var(--mb-color-border)",+ }}+ >+ <Text component="label" fw="bold">{t`Transform target`}</Text>+ <TableDiff+ newSchema={newTarget.schema}+ newTable={newTarget.name}+ oldSchema={oldTarget.schema}+ oldTable={oldTarget.name}+ />+ </Stack>+ )}++ {oldSourceTables && newSourceTables && sourceTablesChanged && (+ <Stack+ data-testid="source-tables-diff"+ gap="xs"+ px="md"+ py="sm"+ style={{+ borderBottom: "1px solid var(--mb-color-border)",+ }}+ >+ <Text component="label" fw="bold">{t`Source tables`}</Text>+ <TransformSourceTablesDiff+ newSourceTables={newSourceTables}+ oldSourceTables={oldSourceTables}+ />+ </Stack>+ )}++ <CodeMirror+ className={cx(EditorS.editor, S.diffEditor)}+ extensions={extensions}+ value={newSource}+ readOnly+ autoCorrect="off"+ />+ </Stack>+ );+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/OverviewPanel.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/OverviewPanel.tsxnew file mode 100644index 00000000000..0d8d9b6a67e--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/OverviewPanel.tsx@@ -0,0 +1,67 @@+import { msgid, ngettext, t } from "ttag";++import { FormTextarea } from "metabase/forms";+import { Box, Stack, Text } from "metabase/ui";+import type { WorkspaceId } from "metabase-types/api";++import { MAX_COMMIT_MESSAGE_LENGTH } from "../MergeWorkspaceModal";++import { QualityChecksSection } from "./QualityChecksSection";++type OverviewPanelProps = {+ commitMessageLength?: number;+ hasCommitMessageError: boolean;+ workspaceId: WorkspaceId;+ transformCount: number;+};++export const OverviewPanel = ({+ commitMessageLength = 0,+ hasCommitMessageError,+ workspaceId,+ transformCount,+}: OverviewPanelProps) => {+ return (+ <Box p="xl" h="100%" style={{ overflowY: "auto" }}>+ <Stack>+ <Box>+ <Text>{t`This will merge all changes from this workspace back to the source transforms.`}</Text>+ <Text mt="xs">+ {t`The commit message will be used to display the history of transform changes.`}+ </Text>+ </Box>+ <Stack mt="md" gap={0}>+ <FormTextarea+ data-autofocus+ label={t`Commit message`}+ name="commit_message"+ placeholder={t`Describe the changes you made in this workspace...`}+ minRows={4}+ required+ />+ {!hasCommitMessageError && (+ <Text size="sm" c="text-tertiary" pt="sm">+ {commitMessageLength}/{MAX_COMMIT_MESSAGE_LENGTH}+ </Text>+ )}+ </Stack>+ <QualityChecksSection workspaceId={workspaceId} />+ <Box+ p="md"+ style={{+ backgroundColor: "var(--mb-color-background-secondary)",+ borderRadius: "var(--mantine-radius-sm)",+ }}+ >+ <Text>+ {ngettext(+ msgid`${transformCount} transform will be merged`,+ `${transformCount} transforms will be merged`,+ transformCount,+ )}+ </Text>+ </Box>+ </Stack>+ </Box>+ );+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/QualityChecksSection.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/QualityChecksSection.tsxnew file mode 100644index 00000000000..ea83c265a98--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/QualityChecksSection.tsx@@ -0,0 +1,129 @@+import { useMemo, useState } from "react";+import { t } from "ttag";++import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";+import { Box, Collapse, Group, Icon, Loader, Stack, Text } from "metabase/ui";+import { useGetWorkspaceProblemsQuery } from "metabase-enterprise/api";+import type { WorkspaceId } from "metabase-types/api";++import type { ProblemCheckCategory } from "../problemUtils";+import {+ formatProblemDetails,+ getCheckStatus,+ getCheckStatusWithUnused,+ getCheckTitle,+ groupProblemsByCategory,+} from "../problemUtils";++const CHECK_CATEGORIES: ProblemCheckCategory[] = [+ "external-dependencies",+ "internal-dependencies",+ "structural-issues",+ "unused-outputs",+];++type QualityChecksSectionProps = {+ workspaceId: WorkspaceId;+};++export const QualityChecksSection = ({+ workspaceId,+}: QualityChecksSectionProps) => {+ const {+ data: problems = [],+ error,+ isLoading,+ } = useGetWorkspaceProblemsQuery(workspaceId);+ const grouped = useMemo(() => groupProblemsByCategory(problems), [problems]);+ const [expandedChecks, setExpandedChecks] = useState<+ Set<ProblemCheckCategory>+ >(new Set());++ const toggleCheck = (category: ProblemCheckCategory) => {+ setExpandedChecks((prev) => {+ const next = new Set(prev);+ if (next.has(category)) {+ next.delete(category);+ } else {+ next.add(category);+ }+ return next;+ });+ };++ return (+ <Stack gap="sm">+ <Text fw="bold">{t`Quality Checks`}</Text>++ <LoadingAndErrorWrapper error={error}>+ <Stack gap="sm">+ {CHECK_CATEGORIES.map((category) => {+ const categoryProblems = grouped[category];+ const checkStatus =+ category === "unused-outputs"+ ? getCheckStatusWithUnused(categoryProblems)+ : getCheckStatus(categoryProblems);+ const isExpanded = expandedChecks.has(category);+ const hasProblems = checkStatus.count > 0;++ return (+ <Box key={category}>+ <Group+ justify="space-between"+ style={{ cursor: hasProblems ? "pointer" : "default" }}+ onClick={() => hasProblems && toggleCheck(category)}+ >+ <Text>{getCheckTitle(category)}</Text>++ {isLoading ? (+ <Loader size="xs" aria-label={t`Loading`} />+ ) : (+ <Group gap="xs">+ {checkStatus.status === "passed" && (+ <Icon name="check" size={14} c="success" />+ )}++ <Text+ c={+ checkStatus.status === "passed"+ ? "success"+ : checkStatus.problems.some(+ (p) => p.severity === "error",+ )+ ? "error"+ : "warning"+ }+ >+ {checkStatus.status === "passed"+ ? category === "unused-outputs" &&+ checkStatus.count > 0+ ? t`${checkStatus.count} info`+ : t`Passed`+ : checkStatus.count === 1+ ? t`Failed`+ : t`${checkStatus.count} issues`}+ </Text>+ </Group>+ )}+ </Group>+ {hasProblems && (+ <Collapse in={isExpanded}>+ <Stack gap="xs" mt="xs" pl="md">+ {checkStatus.problems.map((problem, idx) => (+ <Box key={idx}>+ <Text size="sm" c="text-secondary">+ {formatProblemDetails(problem)}+ </Text>+ </Box>+ ))}+ </Stack>+ </Collapse>+ )}+ </Box>+ );+ })}+ </Stack>+ </LoadingAndErrorWrapper>+ </Stack>+ );+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/QualityChecksSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/QualityChecksSection.unit.spec.tsxnew file mode 100644index 00000000000..d6c62a00719--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/QualityChecksSection.unit.spec.tsx@@ -0,0 +1,234 @@+import { setupWorkspaceProblemsEndpoint } from "__support__/server-mocks";+import {+ renderWithProviders,+ screen,+ waitFor,+ waitForLoaderToBeRemoved,+} from "__support__/ui";+import type { WorkspaceId, WorkspaceProblem } from "metabase-types/api";+import { createMockWorkspaceProblem } from "metabase-types/api/mocks";++import { QualityChecksSection } from "./QualityChecksSection";++const MOCK_WORKSPACE_ID: WorkspaceId = 1;++describe("QualityChecksSection", () => {+ it("should render all check categories", async () => {+ setupWorkspaceProblemsEndpoint(MOCK_WORKSPACE_ID, []);+ renderWithProviders(+ <QualityChecksSection workspaceId={MOCK_WORKSPACE_ID} />,+ );+ await waitForLoaderToBeRemoved();++ expect(screen.getByText("External dependencies")).toBeInTheDocument();+ expect(screen.getByText("Internal dependencies")).toBeInTheDocument();+ expect(screen.getByText("Structural issues")).toBeInTheDocument();+ expect(screen.getByText("Unused outputs")).toBeInTheDocument();+ });++ it("should show loading state while fetching problems", async () => {+ // Use a delayed promise to simulate loading+ let resolvePromise: (value: WorkspaceProblem[]) => void = () => {};+ const delayedPromise = new Promise<WorkspaceProblem[]>((resolve) => {+ resolvePromise = resolve;+ });++ setupWorkspaceProblemsEndpoint(MOCK_WORKSPACE_ID, delayedPromise);++ renderWithProviders(+ <QualityChecksSection workspaceId={MOCK_WORKSPACE_ID} />,+ );++ // Verify loaders are rendered (one for each of the 4 categories)+ const loaders = screen.getAllByLabelText("Loading");+ expect(loaders).toHaveLength(4);++ // Verify "Passed" status is not shown while loading+ expect(screen.queryByText("Passed")).not.toBeInTheDocument();++ // Resolve the promise to clean up+ resolvePromise([]);+ });++ it("should show 'Passed' status when there are no problems", async () => {+ setupWorkspaceProblemsEndpoint(MOCK_WORKSPACE_ID, []);+ renderWithProviders(+ <QualityChecksSection workspaceId={MOCK_WORKSPACE_ID} />,+ );+ await waitForLoaderToBeRemoved();++ await waitFor(() => {+ const passedTexts = screen.getAllByText("Passed");+ expect(passedTexts.length).toBe(4); // All 4 categories should pass+ });+ });++ it("should show check icon for passed checks", async () => {+ setupWorkspaceProblemsEndpoint(MOCK_WORKSPACE_ID, []);+ renderWithProviders(+ <QualityChecksSection workspaceId={MOCK_WORKSPACE_ID} />,+ );+ await waitForLoaderToBeRemoved();++ await waitFor(() => {+ const checkIcons = screen.getAllByLabelText("check icon");+ expect(checkIcons.length).toBe(4); // All 4 categories should have check icons+ });+ });++ describe("External dependencies", () => {+ it("should display failed status when there are external dependency problems", async () => {+ const problems: WorkspaceProblem[] = [+ createMockWorkspaceProblem({+ category: "external-downstream",+ problem: "not-run",+ severity: "error",+ description: "External transform depends on this",+ }),+ ];++ setupWorkspaceProblemsEndpoint(MOCK_WORKSPACE_ID, problems);+ renderWithProviders(+ <QualityChecksSection workspaceId={MOCK_WORKSPACE_ID} />,+ );+ await waitForLoaderToBeRemoved();++ await waitFor(() => {+ expect(screen.getByText("Failed")).toBeInTheDocument();+ });+ });++ it("should display issue count when there are multiple external dependency problems", async () => {+ const problems: WorkspaceProblem[] = [+ createMockWorkspaceProblem({+ category: "external-downstream",+ problem: "not-run",+ severity: "error",+ description: "External transform 1 depends on this",+ }),+ createMockWorkspaceProblem({+ category: "external-downstream",+ problem: "not-run",+ severity: "warning",+ description: "External transform 2 depends on this",+ }),+ ];++ setupWorkspaceProblemsEndpoint(MOCK_WORKSPACE_ID, problems);+ renderWithProviders(+ <QualityChecksSection workspaceId={MOCK_WORKSPACE_ID} />,+ );+ await waitForLoaderToBeRemoved();++ await waitFor(() => {+ expect(screen.getByText("2 issues")).toBeInTheDocument();+ });+ });+ });++ describe("Internal dependencies", () => {+ it("should display failed status when there are internal dependency problems", async () => {+ const problems: WorkspaceProblem[] = [+ createMockWorkspaceProblem({+ category: "internal-downstream",+ problem: "not-run",+ severity: "error",+ description: "Internal transform depends on this",+ }),+ ];++ setupWorkspaceProblemsEndpoint(MOCK_WORKSPACE_ID, problems);+ renderWithProviders(+ <QualityChecksSection workspaceId={MOCK_WORKSPACE_ID} />,+ );+ await waitForLoaderToBeRemoved();++ await waitFor(() => {+ expect(screen.getByText("Failed")).toBeInTheDocument();+ });+ });+ });++ describe("Structural issues", () => {+ it("should display failed status for internal structural issues", async () => {+ const problems: WorkspaceProblem[] = [+ createMockWorkspaceProblem({+ category: "internal",+ problem: "removed-field",+ severity: "error",+ description: "Field was removed",+ }),+ ];++ setupWorkspaceProblemsEndpoint(MOCK_WORKSPACE_ID, problems);+ renderWithProviders(+ <QualityChecksSection workspaceId={MOCK_WORKSPACE_ID} />,+ );+ await waitForLoaderToBeRemoved();++ await waitFor(() => {+ expect(screen.getByText("Failed")).toBeInTheDocument();+ });+ });++ it("should display failed status for external structural issues", async () => {+ const problems: WorkspaceProblem[] = [+ createMockWorkspaceProblem({+ category: "external",+ problem: "removed-field",+ severity: "error",+ description: "External field was removed",+ }),+ ];++ setupWorkspaceProblemsEndpoint(MOCK_WORKSPACE_ID, problems);+ renderWithProviders(+ <QualityChecksSection workspaceId={MOCK_WORKSPACE_ID} />,+ );+ await waitForLoaderToBeRemoved();++ await waitFor(() => {+ expect(screen.getByText("Failed")).toBeInTheDocument();+ });+ });+ });++ describe("Multiple categories with problems", () => {+ it("should display problems from multiple categories", async () => {+ const problems: WorkspaceProblem[] = [+ createMockWorkspaceProblem({+ category: "external-downstream",+ problem: "not-run",+ severity: "error",+ description: "External dependency issue",+ }),+ createMockWorkspaceProblem({+ category: "internal",+ problem: "removed-field",+ severity: "warning",+ description: "Structural issue",+ }),+ createMockWorkspaceProblem({+ category: "unused",+ problem: "not-run",+ severity: "info",+ description: "Unused output",+ }),+ ];++ setupWorkspaceProblemsEndpoint(MOCK_WORKSPACE_ID, problems);+ renderWithProviders(+ <QualityChecksSection workspaceId={MOCK_WORKSPACE_ID} />,+ );+ await waitForLoaderToBeRemoved();++ await waitFor(() => {+ // There should be multiple "Failed" texts (external dependencies and structural issues)+ const failedTexts = screen.getAllByText("Failed");+ expect(failedTexts.length).toBeGreaterThanOrEqual(2);+ });++ expect(screen.getByText("1 info")).toBeInTheDocument();+ });+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/TableDiff.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/TableDiff.tsxnew file mode 100644index 00000000000..24e51b41ffd--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/TableDiff.tsx@@ -0,0 +1,57 @@+import { Group, Icon, Text } from "metabase/ui";+import type { SchemaName } from "metabase-types/api";++interface Props {+ newSchema: SchemaName | null | undefined;+ newTable: string | undefined;+ oldSchema: SchemaName | null | undefined;+ oldTable: string | undefined;+}++export const TableDiff = ({+ newSchema,+ newTable,+ oldSchema,+ oldTable,+}: Props) => {+ const schemaChanged = oldSchema !== newSchema;+ const tableChanged = oldTable !== newTable;++ return (+ <Group gap="sm">+ <Group gap="xs">+ <Icon c="text-secondary" name="folder" />++ {schemaChanged && oldSchema && (+ <Text c="danger" component="s" td="line-through">+ {oldSchema}+ </Text>+ )}++ {newSchema && (+ <Text c={schemaChanged ? "success" : undefined}>{newSchema}</Text>+ )}+ </Group>++ <Divider />++ <Group gap="xs">+ <Icon c="text-secondary" name="table2" />++ {tableChanged && oldTable && (+ <Text c="danger" component="s" td="line-through">+ {oldTable}+ </Text>+ )}++ {newTable && (+ <Text c={tableChanged ? "success" : undefined}>{newTable}</Text>+ )}+ </Group>+ </Group>+ );+};++function Divider() {+ return <Icon name="chevronright" size={8} />;+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/TransformListItem.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/TransformListItem.tsxnew file mode 100644index 00000000000..7782ff2379e--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/TransformListItem.tsx@@ -0,0 +1,53 @@+import cx from "classnames";++import { Flex, Icon, Text } from "metabase/ui";+import type {+ WorkspaceId,+ WorkspaceTransformListItem,+} from "metabase-types/api";++import S from "../MergeWorkspaceModal.module.css";+import { useTransformSources } from "../hooks/useTransformSources";++type TransformListItemProps = {+ transform: WorkspaceTransformListItem;+ isSelected: boolean;+ onClick: () => void;+ workspaceId: WorkspaceId;+};++export const TransformListItem = ({+ transform,+ isSelected,+ onClick,+ workspaceId,+}: TransformListItemProps) => {+ const { diffStats } = useTransformSources(workspaceId, transform);++ return (+ <Flex+ align="center"+ justify="space-between"+ px="md"+ py="sm"+ className={cx(S.sidebarItem, isSelected && S.sidebarItemActive)}+ onClick={onClick}+ data-testid={`transform-list-item`}+ >+ <Flex align="center" gap="sm" style={{ overflow: "hidden" }}>+ <Icon name="code_block" size={14} c="text-secondary" />+ <Text truncate>{transform.name}</Text>+ </Flex>+ {diffStats && (diffStats.additions > 0 || diffStats.deletions > 0) && (+ <Flex gap="xs" fz="xs" style={{ flexShrink: 0 }}>+ {diffStats.additions > 0 && (+ <Text c="success">+{diffStats.additions}</Text>+ )}+ {diffStats.deletions > 0 && (+ <Text c="danger">-{diffStats.deletions}</Text>+ )}+ </Flex>+ )}+ </Flex>+ );+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/TransformSourceTablesDiff.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/TransformSourceTablesDiff.tsxnew file mode 100644index 00000000000..809f6aa58e3--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/components/TransformSourceTablesDiff.tsx@@ -0,0 +1,108 @@+import { Fragment } from "react";++import { skipToken, useGetTableQuery } from "metabase/api";+import { Box, Group, Loader, Text } from "metabase/ui";+import type { PythonTransformTableAliases, TableId } from "metabase-types/api";++import { TableDiff } from "./TableDiff";++interface Props {+ newSourceTables: PythonTransformTableAliases;+ oldSourceTables: PythonTransformTableAliases;+}++export const TransformSourceTablesDiff = ({+ newSourceTables,+ oldSourceTables,+}: Props) => {+ const oldSourceTablesIds = Object.values(oldSourceTables);+ const newSourceTablesIds = Object.values(newSourceTables);++ const oldSourceNames = Object.fromEntries(+ Object.entries(oldSourceTables).map(([k, v]) => [v, k]),+ );+ const newSourceNames = Object.fromEntries(+ Object.entries(newSourceTables).map(([k, v]) => [v, k]),+ );++ const removedTablesIds = oldSourceTablesIds.slice(newSourceTablesIds.length);++ return (+ <Box+ display="grid"+ style={{+ alignItems: "center",+ gridTemplateColumns: "auto 1fr",+ rowGap: 4,+ columnGap: 32,+ }}+ >+ {newSourceTablesIds.map((id, index) => {+ const oldTableId = oldSourceTablesIds[index];+ const oldSourceName = oldSourceNames[oldTableId];+ const newSourceName = newSourceNames[id];+ const sourceNameChanged = oldSourceName !== newSourceName;++ return (+ <Fragment key={id}>+ <Group gap="xs">+ {sourceNameChanged && oldSourceName && (+ <Text c="danger" component="s" td="line-through">+ {oldSourceName}+ </Text>+ )}++ <Text c={sourceNameChanged ? "success" : undefined}>+ {newSourceName}+ </Text>+ </Group>++ <TableItem oldTableId={oldTableId} tableId={id} />+ </Fragment>+ );+ })}++ {removedTablesIds.map((id) => {+ const oldSourceName = oldSourceNames[id];++ return (+ <Fragment key={id}>+ <Text c="danger" component="s" td="line-through">+ {oldSourceName}+ </Text>++ <TableItem oldTableId={id} tableId={undefined} />+ </Fragment>+ );+ })}+ </Box>+ );+};++function TableItem({+ oldTableId,+ tableId,+}: {+ oldTableId: TableId | undefined;+ tableId: TableId | undefined;+}) {+ const { data: oldTable } = useGetTableQuery(+ oldTableId ? { id: oldTableId } : skipToken,+ );+ const { data: table } = useGetTableQuery(+ tableId ? { id: tableId } : skipToken,+ );++ if (!table && tableId) {+ return <Loader size="xs" />;+ }++ return (+ <TableDiff+ newSchema={table?.schema}+ newTable={table?.name}+ oldSchema={oldTable?.schema}+ oldTable={oldTable?.name}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/hooks/useTransformSources.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/hooks/useTransformSources.tsnew file mode 100644index 00000000000..abed3ee2cbf--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/hooks/useTransformSources.ts@@ -0,0 +1,105 @@+import { useMemo } from "react";++import { useSelector } from "metabase/lib/redux";+import { getMetadata } from "metabase/selectors/metadata";+import {+ useGetTransformQuery,+ useGetWorkspaceTransformQuery,+} from "metabase-enterprise/api";+import type {+ PythonTransformSource,+ PythonTransformTableAliases,+ TransformSource,+ TransformTarget,+ WorkspaceId,+ WorkspaceTransformListItem,+} from "metabase-types/api";++import { computeDiffStats, getSourceCode } from "../utils";++type UseTransformSourcesResult = {+ oldSource: string;+ oldSourceTables: PythonTransformTableAliases | undefined;+ oldTarget: TransformTarget | undefined;+ newSource: string;+ newSourceTables: PythonTransformTableAliases | undefined;+ newTarget: TransformTarget | undefined;+ isLoading: boolean;+ hasError: boolean;+ diffStats: { additions: number; deletions: number } | null;+};++export function useTransformSources(+ workspaceId: WorkspaceId,+ transform: WorkspaceTransformListItem,+): UseTransformSourcesResult {+ const metadata = useSelector(getMetadata);+ const isNewTransform = transform.global_id == null;++ const workspaceTransformResult = useGetWorkspaceTransformQuery({+ workspaceId,+ transformId: transform.ref_id,+ });++ const globalTransformResult = useGetTransformQuery(transform.global_id!, {+ skip: isNewTransform,+ });++ const globalTransform = globalTransformResult.data;+ const workspaceTransform = workspaceTransformResult.data;++ const isLoading =+ workspaceTransformResult.isLoading ||+ (!isNewTransform && globalTransformResult.isLoading);++ const hasError =+ !!workspaceTransformResult.error ||+ (!isNewTransform && !!globalTransformResult.error);++ const oldSource = useMemo(() => {+ return globalTransform ? getSourceCode(globalTransform, metadata) : "";+ }, [globalTransform, metadata]);++ const newSource = useMemo(() => {+ return workspaceTransform+ ? getSourceCode(workspaceTransform, metadata)+ : "";+ }, [workspaceTransform, metadata]);++ const diffStats = useMemo(() => {+ if (isNewTransform && newSource) {+ const lineCount = newSource.split("\n").length;+ return { additions: lineCount, deletions: 0 };+ }+ if (oldSource && newSource) {+ return computeDiffStats(oldSource, newSource);+ }+ return null;+ }, [isNewTransform, oldSource, newSource]);++ return {+ oldSource,+ oldSourceTables: isPythonTransformSource(globalTransform?.source)+ ? globalTransform.source["source-tables"]+ : undefined,+ oldTarget: globalTransform?.target,+ newSource,+ newSourceTables: isPythonTransformSource(workspaceTransform?.source)+ ? workspaceTransform.source["source-tables"]+ : undefined,+ newTarget: workspaceTransform?.target,+ hasError,+ isLoading,+ diffStats,+ };+}++function isPythonTransformSource(+ source: TransformSource | undefined,+): source is PythonTransformSource {+ return (+ source != null &&+ source.type === "python" &&+ source["source-database"] !== undefined+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/index.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/index.tsnew file mode 100644index 00000000000..d6c209dcd2a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/index.ts@@ -0,0 +1,4 @@+export {+ MergeWorkspaceModal,+ MergeWorkspaceModal as ReviewChangesModal,+} from "./MergeWorkspaceModal";diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/problemUtils.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/problemUtils.tsnew file mode 100644index 00000000000..541e53860c5--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/problemUtils.ts@@ -0,0 +1,174 @@+import { match } from "ts-pattern";+import { t } from "ttag";++import type { WorkspaceProblem } from "metabase-types/api";+import {+ isKnownWorkspaceProblemData,+ isWorkspaceProblemDataExternalDownstreamNotRun,+ isWorkspaceProblemDataInternalDownstreamNotRun,+ isWorkspaceProblemDataRemovedField,+} from "metabase-types/guards";++export type ProblemCheckCategory =+ | "external-dependencies"+ | "internal-dependencies"+ | "structural-issues"+ | "unused-outputs";++export type GroupedProblems = {+ "external-dependencies": WorkspaceProblem[];+ "internal-dependencies": WorkspaceProblem[];+ "structural-issues": WorkspaceProblem[];+ "unused-outputs": WorkspaceProblem[];+};++export type CheckStatus = {+ status: "passed" | "failed";+ count: number;+ problems: WorkspaceProblem[];+};++/**+ * Groups problems by category into logical Quality Check categories+ */+export function groupProblemsByCategory(+ problems: WorkspaceProblem[],+): GroupedProblems {+ const grouped: GroupedProblems = {+ "external-dependencies": problems.filter(+ (problem) => problem.category === "external-downstream",+ ),+ "internal-dependencies": problems.filter(+ (problem) => problem.category === "internal-downstream",+ ),+ "structural-issues": problems.filter(+ (problem) =>+ problem.category === "internal" || problem.category === "external",+ ),+ "unused-outputs": problems.filter(+ (problem) => problem.category === "unused",+ ),+ };++ return grouped;+}++/**+ * Calculates the status for a group of problems+ * Unused problems are informational and don't affect status (always "passed")+ */+export function getCheckStatus(problems: WorkspaceProblem[]): CheckStatus {+ // Filter out unused problems for status calculation - they're informational only+ const relevantProblems = problems.filter(+ (problem) => problem.category !== "unused",+ );++ // If all problems are unused, return passed with empty array+ // If there are any non-unused problems, return failed with those problems+ return {+ status: relevantProblems.length === 0 ? "passed" : "failed",+ count: relevantProblems.length,+ problems: relevantProblems,+ };+}++/**+ * Calculates status including unused problems (for unused-outputs check)+ */+export function getCheckStatusWithUnused(+ problems: WorkspaceProblem[],+): CheckStatus {+ return {+ status: "passed", // Unused is always passed (informational)+ count: problems.length,+ problems: problems, // Include all problems for display+ };+}++/**+ * Formats problem details for display+ */+export function formatProblemDetails(problem: WorkspaceProblem): string {+ const parts: string[] = [];++ const transform = isKnownWorkspaceProblemData(problem)+ ? problem.transform+ : undefined;++ // Add transform reference if available+ if (transform) {+ const transformName = transform.name ?? transform.id.toString();+ const transformType =+ transform.type === "workspace-transform"+ ? t`workspace transform`+ : t`external transform`;++ parts.push(`${transformType} "${transformName}"`);+ }++ const output = isKnownWorkspaceProblemData(problem)+ ? problem.output+ : undefined;++ // Add table reference if available+ if (output) {+ const schema = output.schema || "";+ const table = output.table || "";++ if (schema && table) {+ parts.push(t`table "${schema}.${table}"`);+ } else if (table) {+ parts.push(t`table "${table}"`);+ }+ }++ const badRefs = isWorkspaceProblemDataRemovedField(problem)+ ? problem["bad-refs"]+ : undefined;++ // Add bad-refs for removed-field problems+ if (badRefs) {+ const fieldNames = badRefs+ .map((ref) => ref.name || ref.message || null)+ .filter(Boolean);++ if (fieldNames.length > 0) {+ parts.push(t`removed fields: ${fieldNames.join(", ")}`);+ }+ }++ const dependents =+ isWorkspaceProblemDataInternalDownstreamNotRun(problem) ||+ isWorkspaceProblemDataExternalDownstreamNotRun(problem)+ ? problem.dependents+ : undefined;++ // Add dependents if available+ if (dependents) {+ const dependentNames = dependents+ .map((dep: { name?: string; id?: string | number }) =>+ dep.name ? dep.name : dep.id?.toString(),+ )+ .filter(Boolean);++ if (dependentNames.length > 0) {+ parts.push(t`affects: ${dependentNames.join(", ")}`);+ }+ }++ // If we have the description and no specific details, use description+ if (parts.length === 0 && problem.description) {+ return problem.description;+ }++ return [problem.description, ...parts].join(". ");+}++export function getCheckTitle(category: ProblemCheckCategory): string {+ return match(category)+ .with("external-dependencies", () => t`External dependencies`)+ .with("internal-dependencies", () => t`Internal dependencies`)+ .with("structural-issues", () => t`Structural issues`)+ .with("unused-outputs", () => t`Unused outputs`)+ .exhaustive();+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/utils.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/utils.tsnew file mode 100644index 00000000000..a0258aa2dba--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/utils.ts@@ -0,0 +1,68 @@+import { diffLines } from "diff";++import * as Lib from "metabase-lib";+import type Metadata from "metabase-lib/v1/metadata/Metadata";+import type {+ PythonTransformTableAliases,+ TransformSource,+} from "metabase-types/api";++export function getSourceCode(+ transform: { source: TransformSource },+ metadata: Metadata,+): string {+ if (transform.source.type === "python") {+ return transform.source.body;+ }+ if (transform.source.type === "query") {+ const metadataProvider = Lib.metadataProvider(+ transform.source.query.database,+ metadata,+ );+ const query = Lib.fromJsQuery(metadataProvider, transform.source.query);+ if (Lib.queryDisplayInfo(query).isNative) {+ return Lib.rawNativeQuery(query);+ }+ }+ return "";+}++export function computeDiffStats(+ oldSource: string,+ newSource: string,+): { additions: number; deletions: number } {+ const changes = diffLines(oldSource, newSource);+ let additions = 0;+ let deletions = 0;++ for (const change of changes) {+ const lineCount = change.count ?? 0;+ if (change.added) {+ additions += lineCount;+ } else if (change.removed) {+ deletions += lineCount;+ }+ }++ return { additions, deletions };+}++export function areSourceTablesEqual(+ a: PythonTransformTableAliases | undefined,+ b: PythonTransformTableAliases | undefined,+) {+ if (a == null || b == null) {+ return a === b;+ }++ const aEntries = Object.entries(a);+ const bEntries = Object.entries(b);++ if (aEntries.length !== bEntries.length) {+ return false;+ }++ return aEntries.every(([key, value], index) => {+ return bEntries[index][0] === key && bEntries[index][1] === value;+ });+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/RunWorkspaceMenu/RunWorkspaceMenu.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/RunWorkspaceMenu/RunWorkspaceMenu.tsxnew file mode 100644index 00000000000..df723a9d7c3--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/RunWorkspaceMenu/RunWorkspaceMenu.tsx@@ -0,0 +1,67 @@+import { useCallback } from "react";+import { t } from "ttag";++import { useMetadataToasts } from "metabase/metadata/hooks";+import { Button, Icon, Menu } from "metabase/ui";+import { useRunWorkspaceMutation } from "metabase-enterprise/api";+import type { WorkspaceId } from "metabase-types/api";++type RunWorkspaceMenuProps = {+ workspaceId: WorkspaceId;+ disabled?: boolean;+};++export function RunWorkspaceMenu({+ workspaceId,+ disabled = false,+}: RunWorkspaceMenuProps) {+ const { sendErrorToast, sendSuccessToast } = useMetadataToasts();+ const [runWorkspace, { isLoading: isRunning }] = useRunWorkspaceMutation();++ const handleRunWorkspace = useCallback(+ async ({ staleOnly } = { staleOnly: false }) => {+ try {+ const response = await runWorkspace({+ id: workspaceId,+ stale_only: staleOnly,+ }).unwrap();++ if (response.failed.length > 0) {+ sendErrorToast(t`Some transforms failed to run`);+ } else if (response.succeeded.length > 0) {+ sendSuccessToast(t`Transforms ran successfully`);+ } else {+ sendSuccessToast(t`No transforms were run`);+ }+ } catch (error) {+ sendErrorToast(t`Failed to run transforms`);+ }+ },+ [workspaceId, runWorkspace, sendErrorToast, sendSuccessToast],+ );++ return (+ <Menu position="bottom-end">+ <Menu.Target>+ <Button+ data-testid="run-all-button"+ disabled={disabled}+ loading={isRunning}+ rightSection={<Icon name="chevrondown" />}+ size="xs"+ variant="default"+ >+ {t`Run transforms`}+ </Button>+ </Menu.Target>+ <Menu.Dropdown>+ <Menu.Item onClick={() => handleRunWorkspace()}>+ {t`Run all transforms`}+ </Menu.Item>+ <Menu.Item onClick={() => handleRunWorkspace({ staleOnly: true })}>+ {t`Run stale transforms`}+ </Menu.Item>+ </Menu.Dropdown>+ </Menu>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/WorkspaceRunButton/WorkspaceRunButton.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/WorkspaceRunButton/WorkspaceRunButton.tsxnew file mode 100644index 00000000000..6a7fbd775bb--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/components/WorkspaceRunButton/WorkspaceRunButton.tsx@@ -0,0 +1,31 @@+import { RunButton } from "metabase-enterprise/transforms/components/RunButton";+import type {+ TransformId,+ TransformJobId,+ TransformRun,+} from "metabase-types/api";++type WorkspaceRunButtonProps = {+ id: TransformId | TransformJobId | undefined;+ run: TransformRun | null | undefined;+ isDisabled?: boolean;+ allowCancellation?: boolean;+ onRun: () => void;+ onCancel?: () => void;+};++// A workspace-friendly wrapper that shows visual feedback during and after runs+export function WorkspaceRunButton({ run, ...rest }: WorkspaceRunButtonProps) {+ // Pass through active/completed run states for visual feedback+ // - started/canceling: show in-progress state+ // - succeeded/failed: show completion state (controlled by parent)+ const activeRun =+ run?.status === "started" ||+ run?.status === "canceling" ||+ run?.status === "succeeded" ||+ run?.status === "failed"+ ? run+ : null;++ return <RunButton run={activeRun} size="sm" {...rest} />;+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/hooks/index.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/hooks/index.tsnew file mode 100644index 00000000000..90775b479df--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/hooks/index.ts@@ -0,0 +1 @@+export { useWorkspaceTransformRun } from "./use-workspace-transform-run";diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/hooks/use-workspace-transform-run.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/hooks/use-workspace-transform-run.tsnew file mode 100644index 00000000000..f486eaaca0d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/hooks/use-workspace-transform-run.ts@@ -0,0 +1,204 @@+import { useCallback, useMemo, useState } from "react";+import { t } from "ttag";++import { useDispatch } from "metabase/lib/redux";+import { useMetadataToasts } from "metabase/metadata/hooks";+import {+ useGetWorkspaceTransformQuery,+ useRunWorkspaceTransformMutation,+ workspaceApi,+} from "metabase-enterprise/api";+import type {+ TransformRun,+ WorkspaceId,+ WorkspaceTransform,+} from "metabase-types/api";++type UseWorkspaceTransformRunOptions = {+ workspaceId: WorkspaceId;+ transform: WorkspaceTransform | null;+};++type UseWorkspaceTransformRunResult = {+ /** Run object for RunStatus component - always shows last run info */+ statusRun: TransformRun | null;+ /** Run object for button - only shows feedback during/after user-triggered runs */+ buttonRun: TransformRun | null;+ /** Whether the run status is still loading */+ isRunStatusLoading: boolean;+ /** Whether a run is currently in progress */+ isRunning: boolean;+ /** Trigger a transform run */+ handleRun: () => Promise<void>;+};++type TransformRunState = {+ isRunTriggered: boolean;+ lastRunResult: {+ status: "succeeded" | "failed";+ end_time: string;+ message: string | null;+ } | null;+};++export function useWorkspaceTransformRun({+ workspaceId,+ transform,+}: UseWorkspaceTransformRunOptions): UseWorkspaceTransformRunResult {+ const dispatch = useDispatch();+ const { sendErrorToast } = useMetadataToasts();+ const [runTransform] = useRunWorkspaceTransformMutation();++ // Store run state per transform ref_id to preserve state when switching tabs+ const [runStateByTransform, setRunStateByTransform] = useState<+ Map<string, TransformRunState>+ >(new Map());++ const transformRefId = transform?.ref_id ?? "";+ const currentRunState = runStateByTransform.get(transformRefId);+ const isRunTriggered = currentRunState?.isRunTriggered ?? false;+ const lastRunResult = currentRunState?.lastRunResult ?? null;++ const updateRunState = useCallback(+ (transformId: string, patch: Partial<TransformRunState>) => {+ setRunStateByTransform((prev) => {+ const current = prev.get(transformId);+ const newMap = new Map(prev);+ newMap.set(transformId, {+ isRunTriggered:+ patch.isRunTriggered ?? current?.isRunTriggered ?? false,+ lastRunResult: patch.lastRunResult ?? current?.lastRunResult ?? null,+ });+ return newMap;+ });+ },+ [],+ );++ // Fetch workspace transform to get updated last_run_at+ const { data: fetchedWorkspaceTransform, isFetching } =+ useGetWorkspaceTransformQuery(+ {+ workspaceId,+ transformId: transform?.ref_id ?? "",+ },+ { skip: !transform?.ref_id },+ );++ const lastRunAt =+ fetchedWorkspaceTransform?.last_run_at ?? transform?.last_run_at;+ const lastRunMessage =+ fetchedWorkspaceTransform?.last_run_message ?? transform?.last_run_message;+ const lastRunStatus =+ fetchedWorkspaceTransform?.last_run_status ?? transform?.last_run_status;++ const statusRun: TransformRun | null = useMemo(+ () =>+ lastRunResult+ ? {+ id: -1,+ status: lastRunResult.status,+ start_time: lastRunResult.end_time,+ end_time: lastRunResult.end_time,+ message: lastRunResult.message,+ run_method: "manual",+ }+ : lastRunAt && lastRunStatus != null+ ? {+ id: -1,+ status: lastRunStatus,+ start_time: lastRunAt,+ end_time: lastRunAt,+ message: lastRunMessage ?? null,+ run_method: "manual",+ }+ : null,+ [lastRunResult, lastRunAt, lastRunMessage, lastRunStatus],+ );++ // Run object for button - only shows feedback during/after user-triggered runs+ const buttonRun: TransformRun | null = useMemo(+ () =>+ isRunTriggered+ ? {+ id: -1,+ status: "started",+ start_time: new Date().toISOString(),+ end_time: null,+ message: null,+ run_method: "manual",+ }+ : lastRunResult+ ? {+ id: -1,+ status: lastRunResult.status,+ start_time: lastRunResult.end_time,+ end_time: lastRunResult.end_time,+ message: lastRunResult.message,+ run_method: "manual",+ }+ : null,+ [isRunTriggered, lastRunResult],+ );++ const handleRun = useCallback(async () => {+ if (!transformRefId) {+ return;+ }++ try {+ updateRunState(transformRefId, {+ isRunTriggered: true,+ lastRunResult: null,+ });++ const result = await runTransform({+ workspaceId,+ transformId: transformRefId,+ }).unwrap();++ // Transform completed - store the result+ const endTime = result.end_time ?? new Date().toISOString();++ updateRunState(transformRefId, {+ isRunTriggered: false,+ lastRunResult: {+ status: result.status,+ end_time: endTime,+ message: result.message ?? null,+ },+ });++ // Invalidate caches to refetch updated data+ dispatch(+ workspaceApi.util.invalidateTags([+ { type: "workspace", id: workspaceId },+ { type: "workspace-transform", id: transformRefId },+ { type: "workspace-tables", id: workspaceId },+ ]),+ );++ if (result.status === "failed") {+ sendErrorToast(t`Transform run failed`);+ }+ } catch (error) {+ updateRunState(transformRefId, { isRunTriggered: false });+ sendErrorToast(t`Failed to run transform`);+ }+ }, [+ updateRunState,+ runTransform,+ workspaceId,+ dispatch,+ transformRefId,+ sendErrorToast,+ ]);++ return {+ statusRun,+ buttonRun,+ isRunStatusLoading: statusRun == null && isFetching,+ isRunning: isRunTriggered,+ handleRun,+ };+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/index.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/index.tsxnew file mode 100644index 00000000000..e2483819f32--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/index.tsx@@ -0,0 +1,11 @@+import { PLUGIN_WORKSPACES } from "metabase/plugins";+import { hasPremiumFeature } from "metabase-enterprise/settings";++import { AdminDatabaseWorkspacesSection } from "./admin/AdminDatabaseWorkspacesSection";++export function initializePlugin() {+ if (hasPremiumFeature("workspaces")) {+ PLUGIN_WORKSPACES.AdminDatabaseWorkspacesSection =+ AdminDatabaseWorkspacesSection;+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspaceListPage/WorkspaceListPage.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspaceListPage/WorkspaceListPage.tsxnew file mode 100644index 00000000000..abc291c76b4--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspaceListPage/WorkspaceListPage.tsx@@ -0,0 +1,50 @@+import { useEffect, useMemo } from "react";+import { replace } from "react-router-redux";+import { t } from "ttag";++import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";+import { useDispatch } from "metabase/lib/redux";+import * as Urls from "metabase/lib/urls";+import { Box, Text, Title } from "metabase/ui";+import { useGetWorkspacesQuery } from "metabase-enterprise/api";++export function WorkspaceListPage() {+ const dispatch = useDispatch();+ const { data: workspacesData, error, isFetching } = useGetWorkspacesQuery();++ const workspaces = useMemo(+ () => workspacesData?.items ?? [],+ [workspacesData],+ );++ const firstWorkspaceId = workspaces[0]?.id;++ useEffect(() => {+ if (error || firstWorkspaceId == null) {+ return;+ }++ dispatch(replace(Urls.dataStudioWorkspace(firstWorkspaceId)));+ }, [dispatch, error, firstWorkspaceId]);++ if (error) {+ return <LoadingAndErrorWrapper error={error} loading={false} />;+ }++ if (firstWorkspaceId != null) {+ return null;+ }++ if (isFetching) {+ return <LoadingAndErrorWrapper error={error} loading={isFetching} />;+ }++ return (+ <Box data-testid="workspaces-page" p="lg">+ <Title size="lg">{t`No active workspaces`}</Title>+ <Text c="text-secondary">+ {t`Create a new one from the left panel or by editing an existing transform.`}+ </Text>+ </Box>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/AddTransformMenu.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/AddTransformMenu.tsxnew file mode 100644index 00000000000..e29f39e1c3f--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/AddTransformMenu.tsx@@ -0,0 +1,82 @@+import { t } from "ttag";++import { ActionIcon, Icon, Menu, Tooltip } from "metabase/ui";+import {+ getInitialNativeSource,+ getInitialPythonSource,+} from "metabase-enterprise/transforms/pages/NewTransformPage/utils";+import type { DatabaseId, TransformSource } from "metabase-types/api";+import { createMockTransform } from "metabase-types/api/mocks/transform";++import { useWorkspace } from "./WorkspaceProvider";++type TransformType = "sql" | "python";++interface Props {+ databaseId: DatabaseId;+ disabled?: boolean;+}++export const AddTransformMenu = ({ databaseId, disabled }: Props) => {+ const { addUnsavedTransform } = useWorkspace();++ const getSource = (type: TransformType): TransformSource => {+ if (type === "sql") {+ const source = getInitialNativeSource();+ return {+ ...source,+ query: { ...source.query, database: databaseId },+ };+ }+ return {+ ...getInitialPythonSource(),+ "source-database": databaseId,+ };+ };++ return (+ <>+ <Menu position="bottom-end">+ <Menu.Target>+ <Tooltip label={t`Add transform`}>+ <ActionIcon+ size="2rem"+ p="0"+ ml="auto"+ aria-label={t`Add transform`}+ disabled={disabled}+ >+ <Icon name="add" size={16} aria-hidden />+ </ActionIcon>+ </Tooltip>+ </Menu.Target>+ <Menu.Dropdown>+ <Menu.Item+ leftSection={<Icon name="sql" />}+ onClick={() => {+ const mockTransform = createMockTransform({+ source: getSource("sql"),+ });++ addUnsavedTransform(mockTransform.source);+ }}+ >+ {t`SQL Transform`}+ </Menu.Item>+ <Menu.Item+ leftSection={<Icon name="code_block" />}+ onClick={() => {+ const mockTransform = createMockTransform({+ source: getSource("python"),+ });++ addUnsavedTransform(mockTransform.source);+ }}+ >+ {t`Python Script`}+ </Menu.Item>+ </Menu.Dropdown>+ </Menu>+ </>+ );+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/CodeTab/CodeTab.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/CodeTab/CodeTab.tsxnew file mode 100644index 00000000000..d09e68c7293--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/CodeTab/CodeTab.tsx@@ -0,0 +1,226 @@+import { useCallback } from "react";+import { t } from "ttag";++import { skipToken } from "metabase/api";+import { EmptyState } from "metabase/common/components/EmptyState";+import { useMetadataToasts } from "metabase/metadata/hooks";+import { Stack, Text } from "metabase/ui";+import {+ useGetExternalTransformsQuery,+ useLazyGetTransformQuery,+ useLazyGetWorkspaceTransformQuery,+} from "metabase-enterprise/api";+import { getCheckoutDisabledMessage } from "metabase-enterprise/data-studio/workspaces/utils";+import type {+ DatabaseId,+ ExternalTransform,+ TaggedTransform,+ UnsavedTransform,+ WorkspaceId,+ WorkspaceTransform,+ WorkspaceTransformListItem,+} from "metabase-types/api";+import { isUnsavedTransform } from "metabase-types/api";++import { useWorkspace } from "../WorkspaceProvider";++import {+ TransformListItem,+ TransformListItemSkeleton,+} from "./TransformListItem";+import { TransformListItemMenu } from "./TransformListItemMenu";++/** Item that can be displayed in the workspace transforms list */+type WorkspaceTransformItem = UnsavedTransform | WorkspaceTransformListItem;++function getWorkspaceTransformItemId(+ item: WorkspaceTransformItem,+): string | number {+ if (isUnsavedTransform(item)) {+ return item.id;+ }+ return item.ref_id;+}++type CodeTabProps = {+ activeTransformId?: number | string;+ databaseId: DatabaseId | null | undefined;+ workspaceId: WorkspaceId;+ workspaceTransforms: WorkspaceTransformItem[];+ isLoadingWorkspaceTransforms?: boolean;+ readOnly: boolean;+ onTransformClick: (+ transform: TaggedTransform | WorkspaceTransform | UnsavedTransform,+ ) => void;+};++export const CodeTab = ({+ activeTransformId,+ databaseId,+ workspaceId,+ workspaceTransforms,+ isLoadingWorkspaceTransforms,+ readOnly,+ onTransformClick,+}: CodeTabProps) => {+ const { editedTransforms } = useWorkspace();+ const { sendErrorToast } = useMetadataToasts();++ const {+ data: availableTransforms,+ error,+ isLoading,+ } = useGetExternalTransformsQuery(+ databaseId ? { workspaceId, databaseId } : skipToken,+ );++ const [fetchWorkspaceTransform] = useLazyGetWorkspaceTransformQuery();+ const [fetchTransform] = useLazyGetTransformQuery();++ const handleExternalTransformClick = useCallback(+ async (externalTransform: ExternalTransform) => {+ if (externalTransform.checkout_disabled) {+ return;+ }++ const { data: transform, error } = await fetchTransform(+ externalTransform.id,+ true,+ );++ if (error) {+ sendErrorToast(t`Failed to fetch transform`);+ } else if (transform) {+ const taggedTransform: TaggedTransform = {+ ...transform,+ type: "transform",+ };+ onTransformClick(taggedTransform);+ }+ },+ [fetchTransform, onTransformClick, sendErrorToast],+ );++ const handleWorkspaceTransformItemClick = useCallback(+ async (item: WorkspaceTransformItem) => {+ // Unsaved transforms are stored locally.+ // They should open directly without API fetch.+ if (isUnsavedTransform(item)) {+ // Unsaved transforms should already be opened via the provider+ // This path shouldn't normally be hit, but if it is, we can't+ // call onTransformClick because we don't have a full transform+ return onTransformClick(item);+ }++ const { data: transform, error } = await fetchWorkspaceTransform(+ {+ workspaceId,+ transformId: item.ref_id,+ },+ true,+ );++ if (error) {+ sendErrorToast(t`Failed to fetch transform`);+ } else if (transform) {+ onTransformClick(transform);+ }+ },+ [fetchWorkspaceTransform, workspaceId, onTransformClick, sendErrorToast],+ );++ return (+ <Stack h="100%" gap={0}>+ <Stack+ data-testid="workspace-transforms"+ gap="xs"+ pb="sm"+ style={{+ borderBottom: "1px solid var(--mb-color-border)",+ }}+ >+ <Stack gap={0}>+ <Text fw={600}>{t`Workspace transforms`}</Text>+ {isLoadingWorkspaceTransforms ? (+ <>+ <TransformListItemSkeleton />+ <TransformListItemSkeleton />+ <TransformListItemSkeleton />+ </>+ ) : (+ workspaceTransforms.map((item) => {+ const itemId = getWorkspaceTransformItemId(item);+ const isUnsaved = isUnsavedTransform(item);+ const globalId = isUnsaved ? null : item.global_id;+ const isEdited = isUnsaved || editedTransforms.has(itemId);+ const isActive =+ activeTransformId === itemId || activeTransformId === globalId;++ return (+ <TransformListItem+ key={itemId}+ name={item.name}+ icon="pivot_table"+ fw={600}+ isActive={isActive}+ isEdited={isEdited}+ menu={+ readOnly ? null : (+ <TransformListItemMenu+ transform={item}+ workspaceId={workspaceId}+ />+ )+ }+ onClick={() => {+ handleWorkspaceTransformItemClick(item);+ }}+ />+ );+ })+ )}+ </Stack>++ {!isLoadingWorkspaceTransforms && workspaceTransforms.length === 0 && (+ <EmptyState message={t`Workspace is empty`} />+ )}+ </Stack>++ <Stack data-testid="mainland-transforms" py="sm" gap="xs">+ <Text fw={600} mt="sm">{t`Available transforms`}</Text>++ {isLoading ? (+ <>+ <TransformListItemSkeleton />+ <TransformListItemSkeleton />+ <TransformListItemSkeleton />+ </>+ ) : error ? (+ <Text c="error" size="sm">{t`Failed to load transforms`}</Text>+ ) : (+ <>+ {availableTransforms?.map((transform) => (+ <TransformListItem+ key={transform.id}+ name={transform.name}+ isActive={activeTransformId === transform.id}+ isEdited={editedTransforms.has(transform.id)}+ onClick={() => {+ handleExternalTransformClick(transform);+ }}+ opacity={transform.checkout_disabled ? 0.5 : 1}+ tooltipLabel={getCheckoutDisabledMessage(+ transform.checkout_disabled,+ )}+ />+ ))}++ {availableTransforms?.length === 0 && (+ <EmptyState message={t`No available transforms`} />+ )}+ </>+ )}+ </Stack>+ </Stack>+ );+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/CodeTab/TransformListItem.module.css b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/CodeTab/TransformListItem.module.cssnew file mode 100644index 00000000000..67007100e69--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/CodeTab/TransformListItem.module.css@@ -0,0 +1,33 @@+.root {+ display: flex;+ align-items: center;+ gap: var(--mantine-spacing-sm);+ padding: var(--mantine-spacing-xs) var(--mantine-spacing-sm);+ border-radius: var(--mantine-radius-sm);+ cursor: default;+ flex-wrap: nowrap;++ &:hover .menu {+ visibility: visible;+ }++ &:hover {+ background-color: var(--mb-color-background-secondary);+ }+}++.label {+ display: flex;+ align-items: center;+ gap: var(--mantine-spacing-sm);+ flex: 1;+ min-width: 0;+}++.cursorPointer {+ cursor: pointer;+}++.menu {+ visibility: hidden;+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/CodeTab/TransformListItem.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/CodeTab/TransformListItem.tsxnew file mode 100644index 00000000000..a0cf7d49bdb--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/CodeTab/TransformListItem.tsx@@ -0,0 +1,80 @@+import cx from "classnames";+import type { ReactNode } from "react";+import { t } from "ttag";++import {+ Box,+ type BoxProps,+ Flex,+ Icon,+ type IconName,+ Skeleton,+ Text,+ Tooltip,+} from "metabase/ui";++import { StatusDot } from "../components/StatusDot/StatusDot";++import S from "./TransformListItem.module.css";++type TransformListItemProps = {+ name: string;+ icon?: IconName;+ isActive?: boolean;+ isEdited?: boolean;+ tooltipLabel?: string;+ menu?: ReactNode;+ onClick?: () => void;+} & BoxProps;++export const TransformListItem = ({+ name,+ icon = "database",+ isActive,+ isEdited,+ tooltipLabel,+ menu,+ onClick,+ ...props+}: TransformListItemProps) => {+ const label = (+ <Box className={cx(S.label, { [S.cursorPointer]: !tooltipLabel })}>+ <Icon name={icon} size={14} c="brand" />+ <Text c={isActive ? "brand" : "text-primary"} truncate>+ {name}+ </Text>+ {isEdited && (+ <Tooltip label={t`Unsaved changes`}>+ <Box>+ <StatusDot data-testid="status-dot" />+ </Box>+ </Tooltip>+ )}+ </Box>+ );++ return (+ <Box className={S.root} onClick={onClick} pl="sm" {...props}>+ {tooltipLabel ? (+ <Tooltip label={tooltipLabel} position="top">+ {label}+ </Tooltip>+ ) : (+ label+ )}++ <Box className={S.menu} flex="0 0 auto">+ {menu}+ </Box>+ </Box>+ );+};++export function TransformListItemSkeleton() {+ return (+ <Flex align="center" gap="sm" py="xs" px="sm" pl="md">+ <Skeleton h={14} w={14} circle />+ <Skeleton h={14} w="70%" />+ </Flex>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/CodeTab/TransformListItemMenu.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/CodeTab/TransformListItemMenu.tsxnew file mode 100644index 00000000000..3a7b16d02f8--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/CodeTab/TransformListItemMenu.tsx@@ -0,0 +1,75 @@+import { t } from "ttag";++import { useMetadataToasts } from "metabase/metadata/hooks";+import { ActionIcon, Icon, Menu } from "metabase/ui";+import { useDeleteWorkspaceTransformMutation } from "metabase-enterprise/api";+import type {+ UnsavedTransform,+ WorkspaceId,+ WorkspaceTransformListItem,+} from "metabase-types/api";+import { isUnsavedTransform } from "metabase-types/api";++import { useWorkspace } from "../WorkspaceProvider";++/** Item that can be displayed in the workspace transforms list */+type WorkspaceTransformItem = UnsavedTransform | WorkspaceTransformListItem;++interface Props {+ transform: WorkspaceTransformItem;+ workspaceId: WorkspaceId;+}++export function TransformListItemMenu({ transform, workspaceId }: Props) {+ const [deleteWorkspaceTransform] = useDeleteWorkspaceTransformMutation();++ const {+ removeEditedTransform,+ removeWorkspaceTransform,+ removeUnsavedTransform,+ } = useWorkspace();+ const { sendErrorToast, sendSuccessToast } = useMetadataToasts();++ const handleRemove = async () => {+ // Handle unsaved transforms locally+ if (isUnsavedTransform(transform)) {+ removeUnsavedTransform(transform.id);+ return;+ }++ // For WorkspaceTransformListItem, use ref_id+ try {+ await deleteWorkspaceTransform({+ workspaceId,+ transformId: transform.ref_id,+ }).unwrap();++ sendSuccessToast(t`Transform removed from the workspace`);+ removeEditedTransform(transform.ref_id);+ removeWorkspaceTransform(transform.ref_id);+ } catch (error) {+ sendErrorToast(t`Failed to remove transform from the workspace`);+ }+ };++ return (+ <Menu position="bottom-end" withinPortal>+ <Menu.Target>+ <ActionIcon+ onClick={(event) => event.stopPropagation()}+ size="sm"+ variant="subtle"+ aria-label={t`More actions`}+ >+ <Icon name="ellipsis" />+ </ActionIcon>+ </Menu.Target>++ <Menu.Dropdown onClick={(event) => event.stopPropagation()}>+ <Menu.Item leftSection={<Icon name="trash" />} onClick={handleRemove}>+ {t`Remove`}+ </Menu.Item>+ </Menu.Dropdown>+ </Menu>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/DataTab.module.css b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/DataTab.module.cssnew file mode 100644index 00000000000..749525de8ab--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/DataTab.module.css@@ -0,0 +1,16 @@+.tabsPanel {+ display: flex;+ gap: 0.5rem;+ flex-wrap: nowrap;+ overflow: auto;+ scrollbar-width: thin;+ border-bottom: 1px solid var(--mb-color-border);++ &::before {+ display: none;+ }++ & button {+ border-radius: 0;+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/DataTab.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/DataTab.tsxnew file mode 100644index 00000000000..9b18a684129--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/DataTab.tsx@@ -0,0 +1,191 @@+import { useMemo, useState } from "react";+import { t } from "ttag";++import { createMockMetadata } from "__support__/metadata";+import { skipToken, useGetTableQuery } from "metabase/api";+import { AnsiLogs } from "metabase/common/components/AnsiLogs";+import { EmptyState } from "metabase/common/components/EmptyState";+import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";+import CS from "metabase/css/core/index.css";+import { Box, Group, Icon, Stack, Tabs } from "metabase/ui";+import Visualization from "metabase/visualizations/components/Visualization";+import { useGetWorkspaceTransformQuery } from "metabase-enterprise/api";+import { ExecutionOutputTable } from "metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonEditorResults/ExecutionOutputTable";+import type {+ DatabaseId,+ DatasetQuery,+ TableId,+ TestPythonTransformResponse,+} from "metabase-types/api";++import S from "./DataTab.module.css";+import { useTablePreview } from "./useTablePreview";+import { useTransformDryRunPreview } from "./useTransformDryRunPreview";++interface DataTabProps {+ workspaceId: number;+ databaseId: DatabaseId | null;+ tableId: TableId | null;+ transformId?: string;+ query?: DatasetQuery;+ pythonPreviewResult?: any;+}++export function DataTab({+ workspaceId,+ databaseId,+ tableId,+ transformId,+ query,+ pythonPreviewResult,+}: DataTabProps) {+ const {+ data: table,+ error: tableError,+ isLoading: tableIsLoading,+ } = useGetTableQuery(tableId && !query ? { id: tableId } : skipToken);+ const { data: transform } = useGetWorkspaceTransformQuery(+ transformId ? { workspaceId, transformId } : skipToken,+ );++ // Use existing table preview logic when no query+ const metadata = useMemo(+ () =>+ createMockMetadata({+ tables: table+ ? [+ {+ ...table,+ visibility_type: null,+ },+ ]+ : [],+ }),+ [table],+ );++ // we want to use /dataset for table preview+ const shouldUseDryRunPreview = Boolean(query && transformId && workspaceId);+ const tablePreviewResult = useTablePreview({+ databaseId,+ tableId: shouldUseDryRunPreview ? null : tableId,+ metadata,+ last_transform_run_time: transform?.last_run_at,+ query: shouldUseDryRunPreview ? undefined : query,+ });+ const dryRunPreviewResult = useTransformDryRunPreview({+ workspaceId,+ transformId: shouldUseDryRunPreview ? transformId : undefined,+ query: shouldUseDryRunPreview ? query : undefined,+ });+ const previewResult = shouldUseDryRunPreview+ ? dryRunPreviewResult+ : tablePreviewResult;+ const { rawSeries, isFetching, error: previewResultError } = previewResult;++ const error = tableError || previewResultError;+ const isLoading = tableIsLoading || isFetching;++ if (pythonPreviewResult) {+ return <PythonPreviewResults executionResult={pythonPreviewResult} />;+ }++ if (isLoading) {+ return <LoadingAndErrorWrapper loading={isLoading} />;+ }++ if (error) {+ const errorMessage =+ typeof error === "string" ? error : t`An error occurred`;+ return <ErrorState error={errorMessage} title={t`Error loading data`} />;+ }++ if (!databaseId || (!tableId && !query)) {+ return (+ <Stack p="md">+ <EmptyState message={t`Select a table to view its data`} />+ </Stack>+ );+ }++ if (!rawSeries) {+ return (+ <Stack p="md">+ <EmptyState message={t`No data available`} />+ </Stack>+ );+ }++ return (+ <Box h="100%" className={CS.relative}>+ <Visualization queryBuilderMode="dataset" rawSeries={rawSeries} />+ </Box>+ );+}++function PythonPreviewResults({+ executionResult,+}: {+ executionResult: TestPythonTransformResponse;+}) {+ const [tab, setTab] = useState<string>("results");++ return (+ <Stack gap={0} h="100%">+ <Tabs+ h="100%"+ value={tab}+ onChange={(newTab) => {+ if (newTab) {+ setTab(newTab);+ }+ }}+ >+ <Tabs.List className={S.tabsPanel} px="md">+ <Tabs.Tab value="results">{t`Results`}</Tabs.Tab>+ <Tabs.Tab value="output">{t`Output`}</Tabs.Tab>+ </Tabs.List>++ <Tabs.Panel h="100%" p="md" value="results">+ {executionResult?.error ? (+ <ErrorState error={executionResult.error.message} />+ ) : (+ <Box h="100%" style={{ overflow: "auto" }}>+ <ExecutionOutputTable output={executionResult?.output} />+ </Box>+ )}+ </Tabs.Panel>++ <Tabs.Panel bg="background-secondary" h="100%" p="md" value="output">+ <Box+ fz="sm"+ h="100%"+ style={{+ whiteSpace: "pre",+ fontFamily: "var(--mb-default-monospace-font-family)",+ overflow: "auto",+ }}+ >+ {executionResult?.logs ? (+ <AnsiLogs>{executionResult.logs}</AnsiLogs>+ ) : (+ <EmptyState message={t`No logs to display`} />+ )}+ </Box>+ </Tabs.Panel>+ </Tabs>+ </Stack>+ );+}++function ErrorState({ error, title }: { error: string; title?: string }) {+ return (+ <Stack gap="sm" h="100%" p="md" c="error">+ <Group fw="bold" gap="sm">+ <Icon name="warning" />+ {title ?? t`Error`}+ </Group>+ <Box fz="sm">{error}</Box>+ </Stack>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/DataTabSidebar.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/DataTabSidebar.tsxnew file mode 100644index 00000000000..336ff9aac8a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/DataTabSidebar.tsx@@ -0,0 +1,201 @@+import { useMemo } from "react";+import { t } from "ttag";++import { EmptyState } from "metabase/common/components/EmptyState";+import { Stack, Text } from "metabase/ui";+import {+ DEFAULT_WORKSPACE_TABLES_QUERY_RESPONSE,+ useGetWorkspaceTablesQuery,+ useListTransformsQuery,+} from "metabase-enterprise/api";+import type {+ DatabaseId,+ TableId,+ WorkspaceId,+ WorkspaceTransformListItem,+} from "metabase-types/api";++import { type OpenTable, useWorkspace } from "../WorkspaceProvider";++import { TableListItem, TableListItemSkeleton } from "./TableListItem";++type DataTabSidebarProps = {+ databaseId?: DatabaseId | null;+ readOnly?: boolean;+ runningTransforms?: Set<string>;+ selectedTableId?: TableId | null;+ workspaceId: WorkspaceId;+ workspaceTransforms: WorkspaceTransformListItem[];+ onTransformClick?: (transform: WorkspaceTransformListItem) => void;+ onTableSelect?: (table: OpenTable) => void;+ onRunTransform?: (transform: WorkspaceTransformListItem) => void;+};++export const DataTabSidebar = ({+ databaseId,+ readOnly,+ runningTransforms,+ selectedTableId,+ workspaceId,+ workspaceTransforms,+ onTransformClick,+ onTableSelect,+ onRunTransform,+}: DataTabSidebarProps) => {+ const { hasTransformEdits } = useWorkspace();+ const {+ data: tables = DEFAULT_WORKSPACE_TABLES_QUERY_RESPONSE,+ error: tablesError,+ isLoading: isLoadingTables,+ } = useGetWorkspaceTablesQuery(workspaceId);+ const {+ data: allDbTransforms = [],+ error: allDbTransformsError,+ isLoading: isLoadingAllDbTransforms,+ } = useListTransformsQuery({});++ const dbTransforms = useMemo(+ () =>+ allDbTransforms.filter((t) => {+ if (t.source_type === "python") {+ return (+ "source-database" in t.source &&+ t.source["source-database"] === databaseId+ );+ }+ if (t.source_type === "native") {+ return "query" in t.source && t.source.query.database === databaseId;+ }+ return false;+ }),+ [allDbTransforms, databaseId],+ );++ const error = tablesError || allDbTransformsError;+ const isLoading = isLoadingTables || isLoadingAllDbTransforms;++ if (error) {+ return <Text c="error" size="sm">{t`Failed to load tables`}</Text>;+ }++ return (+ <Stack h="100%" gap="sm">+ <Stack gap="sm" pb="sm">+ <Text fw={600}>{t`Data active in this workspace`}</Text>++ {isLoading ? (+ <>+ <Stack gap="xs">+ <Text+ size="sm"+ fw={600}+ c="text-secondary"+ >{t`Output tables`}</Text>+ <Stack gap={0}>+ <TableListItemSkeleton />+ <TableListItemSkeleton />+ </Stack>+ </Stack>+ <Stack gap="xs">+ <Text+ size="sm"+ fw={600}+ c="text-secondary"+ >{t`Input tables`}</Text>+ <Stack gap={0}>+ <TableListItemSkeleton />+ <TableListItemSkeleton />+ </Stack>+ </Stack>+ </>+ ) : (+ <>+ {tables.outputs.length + tables.inputs.length === 0 && (+ <EmptyState message={t`No tables in this workspace`} />+ )}++ {tables.outputs.length > 0 && (+ <Stack gap="xs">+ <Text+ size="sm"+ fw={600}+ c="text-secondary"+ >{t`Output tables`}</Text>++ <Stack gap={0}>+ {tables.outputs.map((table, index: number) => {+ const workspaceTransform = workspaceTransforms.find(+ (t) => t.ref_id === table.isolated.transform_id,+ );+ const originalTransform = workspaceTransform?.global_id+ ? dbTransforms.find(+ (t) => t.id === workspaceTransform.global_id,+ )+ : undefined;+ const hasChanges = originalTransform+ ? hasTransformEdits({+ ...originalTransform,+ type: "transform",+ })+ : false;+ const tableId = table.isolated.table_id;++ return (+ <TableListItem+ key={`output-${index}`}+ name={table.global.table}+ schema={table.global.schema}+ icon="pivot_table"+ type="output"+ hasChanges={hasChanges}+ transform={workspaceTransform}+ tableId={tableId ?? undefined}+ isSelected={tableId === selectedTableId}+ isRunning={+ workspaceTransform+ ? runningTransforms?.has(workspaceTransform.ref_id)+ : false+ }+ onTransformClick={onTransformClick}+ onTableClick={onTableSelect}+ onRunTransform={onRunTransform}+ readOnly={readOnly}+ />+ );+ })}+ </Stack>+ </Stack>+ )}++ {tables.inputs.length > 0 && (+ <Stack gap="xs">+ <Text+ size="sm"+ fw={600}+ c="text-secondary"+ >{t`Input tables`}</Text>++ <Stack gap={0}>+ {tables.inputs.map((table, index) => (+ <TableListItem+ key={`input-${index}`}+ name={table.table}+ schema={table.schema}+ icon="table"+ type="input"+ isSelected={table.table_id === selectedTableId}+ tableId={table.table_id ?? undefined}+ onTransformClick={onTransformClick}+ onTableClick={onTableSelect}+ readOnly={readOnly}+ />+ ))}+ </Stack>+ </Stack>+ )}+ </>+ )}+ </Stack>+ </Stack>+ );+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/TableListItem.module.css b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/TableListItem.module.cssnew file mode 100644index 00000000000..4135c2af98b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/TableListItem.module.css@@ -0,0 +1,39 @@+.root {+ display: flex;+ align-items: center;+ gap: var(--mantine-spacing-sm);+ padding: var(--mantine-spacing-xs) var(--mantine-spacing-sm);+ border-radius: var(--mantine-radius-sm);+ flex-wrap: nowrap;++ &:hover .menu {+ visibility: visible;+ }++ &:hover {+ background-color: var(--mb-color-background-secondary);+ }++ &.noResults .name {+ opacity: 0.6;+ }+}++.clickable {+ cursor: pointer;+}++.selected {+ background-color: var(--mb-color-background-tertiary);+}++.name {+ flex: 1;+ min-width: 0;+}++.actionIcon {+ &:hover {+ background-color: var(--mb-color-background-tertiary) !important;+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/TableListItem.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/TableListItem.tsxnew file mode 100644index 00000000000..d1a59b041e6--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/TableListItem.tsx@@ -0,0 +1,149 @@+import cx from "classnames";+import { t } from "ttag";++import {+ ActionIcon,+ Box,+ Flex,+ Icon,+ type IconName,+ Loader,+ Skeleton,+ Text,+ Tooltip,+} from "metabase/ui";+import type { TableId, WorkspaceTransformListItem } from "metabase-types/api";++import type { OpenTable } from "../WorkspaceProvider";+import { StatusDot } from "../components/StatusDot/StatusDot";++import S from "./TableListItem.module.css";++type TableListItemProps = {+ name: string;+ schema?: string | null;+ icon?: IconName;+ type: "input" | "output";+ hasChanges?: boolean;+ transform?: WorkspaceTransformListItem;+ tableId?: TableId;+ isSelected?: boolean;+ isRunning?: boolean;+ readOnly?: boolean;+ onTransformClick?: (transform: WorkspaceTransformListItem) => void;+ onTableClick?: (table: OpenTable) => void;+ onRunTransform?: (transform: WorkspaceTransformListItem) => void;+};++export const TableListItem = ({+ name,+ schema,+ icon = "table",+ type,+ hasChanges = false,+ transform,+ tableId,+ isSelected = false,+ isRunning = false,+ readOnly = false,+ onTransformClick,+ onTableClick,+ onRunTransform,+}: TableListItemProps) => {+ const displayName = schema ? `${schema}.${name}` : name;++ const handleTransformClick = (e: React.MouseEvent) => {+ e.stopPropagation();+ if (transform && onTransformClick) {+ onTransformClick(transform);+ }+ };++ const handleTableClick = () => {+ if (isRunning) {+ return;+ }+ if (tableId && onTableClick) {+ onTableClick({ tableId, name, schema, transformId: transform?.ref_id });+ } else if (type === "output" && transform && onRunTransform && !readOnly) {+ onRunTransform(transform);+ }+ };++ const hasResults = type === "output" && !!tableId;+ const canRunTransform =+ type === "output" && !hasResults && transform && onRunTransform;+ const isClickable = (tableId && onTableClick) || canRunTransform;+ const displayTooltip = type !== "input" && !hasResults && !isRunning;++ return (+ <Box+ className={cx(S.root, {+ [S.selected]: isSelected,+ [S.clickable]: isClickable,+ [S.noResults]: type === "output" && !tableId,+ })}+ aria-label={displayName}+ onClick={isClickable ? handleTableClick : undefined}+ >+ {isRunning ? (+ <Loader size="xs" />+ ) : (+ <Icon+ name={icon}+ size={14}+ c={type === "input" ? "saturated-green" : "saturated-yellow"}+ />+ )}+ <Tooltip+ label={+ isRunning+ ? t`Running transform...`+ : t`Run transform to see the results`+ }+ position="top"+ disabled={!displayTooltip && !isRunning}+ >+ <Text+ className={S.name}+ c={displayTooltip ? "text-tertiary" : "text-primary"}+ truncate+ >+ {displayName}+ </Text>+ </Tooltip>++ {hasChanges && (+ <Tooltip label={t`Unsaved changes`}>+ <Box>+ <StatusDot />+ </Box>+ </Tooltip>+ )}++ {type === "output" && transform && (+ <Tooltip label={t`Open transform`} position="top">+ <ActionIcon+ className={S.actionIcon}+ ml="auto"+ size="sm"+ variant="subtle"+ onClick={handleTransformClick}+ aria-label={t`Open transform`}+ >+ <Icon name="code_block" size={14} c="text-secondary" />+ </ActionIcon>+ </Tooltip>+ )}+ </Box>+ );+};++export function TableListItemSkeleton() {+ return (+ <Flex align="center" gap="sm" py="xs" px="sm" h="2rem">+ <Skeleton h={14} w={14} circle />+ <Skeleton h={14} w="70%" />+ </Flex>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/index.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/index.tsnew file mode 100644index 00000000000..05a9e475e7c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/index.ts@@ -0,0 +1,4 @@+export { DataTab } from "./DataTab";+export { DataTabSidebar } from "./DataTabSidebar";+export { useTablePreview as useTableQuestion } from "./useTablePreview";+export type { OpenTable } from "../WorkspaceProvider";diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/useTablePreview.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/useTablePreview.tsnew file mode 100644index 00000000000..5bda3a1edb6--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/useTablePreview.ts@@ -0,0 +1,107 @@+import { useMemo } from "react";+import _ from "underscore";++import { skipToken, useGetAdhocQueryQuery } from "metabase/api";+import { getErrorMessage } from "metabase/api/utils";+import * as Lib from "metabase-lib";+import type Metadata from "metabase-lib/v1/metadata/Metadata";+import type {+ DatabaseId,+ DatasetQuery,+ RawSeries,+ TableId,+} from "metabase-types/api";+import { createMockCard } from "metabase-types/api/mocks";++interface UseTableQuestionProps {+ databaseId: DatabaseId | null;+ tableId: TableId | null;+ metadata: Metadata;+ last_transform_run_time?: string | null;+ query?: DatasetQuery;+}++interface UseTablePreviewResult {+ rawSeries?: RawSeries;+ isFetching: boolean;+ isLoading: boolean;+ isError: boolean;+ error?: string;+}++export function useTablePreview({+ databaseId,+ tableId,+ metadata,+ last_transform_run_time,+ query,+}: UseTableQuestionProps): UseTablePreviewResult {+ const metadataProvider =+ Object.keys(metadata.tables).length > 0+ ? Lib.metadataProvider(databaseId, metadata)+ : null;++ const table =+ metadataProvider && tableId+ ? Lib.tableOrCardMetadata(metadataProvider, tableId)+ : null;++ const tableBasedQuery =+ table && metadataProvider+ ? Lib.queryFromTableOrCardMetadata(metadataProvider, table)+ : null;+ const queryToFetch = query || tableBasedQuery;++ const { data, ...rest } = useGetAdhocQueryQuery(+ queryToFetch == null+ ? skipToken+ : query+ ? {+ ...query,+ ignore_error: true,+ _refetchDeps: last_transform_run_time,+ }+ : tableBasedQuery+ ? {+ ...Lib.toJsQuery(tableBasedQuery),+ ignore_error: true,+ _refetchDeps: last_transform_run_time,+ }+ : skipToken,+ );++ const base = {+ ...rest,+ error: rest.error ? getErrorMessage(rest.error) : undefined,+ rawSeries: undefined,+ };++ const rawSeries = useMemo<RawSeries>(+ () =>+ queryToFetch != null && data+ ? [+ {+ card: createMockCard({+ display: "table",+ visualization_settings: {},+ }),+ data: {+ ...data.data,+ rows: _.uniq(data.data.rows ?? []),+ },+ },+ ]+ : [],+ [data, queryToFetch],+ );++ if (data?.status === "failed") {+ return { ...base, isError: true, error: getErrorMessage(data) };+ }++ if (queryToFetch == null || !data?.data || data.data.cols.length === 0) {+ return base;+ }++ return { ...base, rawSeries };+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/useTransformDryRunPreview.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/useTransformDryRunPreview.tsnew file mode 100644index 00000000000..9562cbc4be1--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/DataTab/useTransformDryRunPreview.ts@@ -0,0 +1,130 @@+import { useEffect, useMemo, useRef } from "react";+import _ from "underscore";++import { getErrorMessage } from "metabase/api/utils";+import { useDryRunWorkspaceTransformMutation } from "metabase-enterprise/api";+import type {+ DatasetColumn,+ DatasetQuery,+ RawSeries,+ WorkspaceId,+} from "metabase-types/api";+import {+ createMockCard,+ createMockDatasetData,+} from "metabase-types/api/mocks";++interface UseTransformDryRunPreviewOptions {+ workspaceId?: WorkspaceId;+ transformId?: string;+ query?: DatasetQuery;+}++interface UseTransformDryRunPreviewResult {+ rawSeries?: RawSeries;+ isFetching: boolean;+ isLoading: boolean;+ isError: boolean;+ error?: string;+}++export function useTransformDryRunPreview({+ workspaceId,+ transformId,+ query,+}: UseTransformDryRunPreviewOptions): UseTransformDryRunPreviewResult {+ const [dryRunTransform, { data, error, isLoading }] =+ useDryRunWorkspaceTransformMutation();+ const abortRef = useRef<(() => void) | null>(null);++ // Track whether we should fetch based on having a query+ // We use a boolean to avoid re-fetching when query object reference changes+ const hasQuery = Boolean(query);++ useEffect(() => {+ if (!workspaceId || !transformId || !hasQuery) {+ return;+ }++ abortRef.current?.();++ const request = dryRunTransform({ workspaceId, transformId });+ abortRef.current = request.abort;++ return () => {+ abortRef.current?.();+ abortRef.current = null;+ };+ }, [workspaceId, transformId, hasQuery, dryRunTransform]);++ const normalizedCols = useMemo<DatasetColumn[]>(() => {+ const cols: Partial<DatasetColumn>[] = data?.data?.cols ?? [];+ return cols.map((col, index) => {+ const name = col.name ?? `column_${index + 1}`;++ return {+ ...col,+ name,+ display_name: col.display_name ?? name,+ source: col.source ?? "native",+ };+ });+ }, [data]);++ const previewData = useMemo(() => {+ if (+ !data?.data ||+ data.status === "failed" ||+ normalizedCols.length === 0+ ) {+ return null;+ }++ return createMockDatasetData({+ cols: normalizedCols,+ rows: data.data.rows ?? [],+ results_metadata: data.data.results_metadata,+ });+ }, [data, normalizedCols]);++ const rawSeries = useMemo<RawSeries | undefined>(() => {+ if (!previewData) {+ return undefined;+ }++ return [+ {+ card: createMockCard({+ display: "table",+ visualization_settings: {},+ }),+ data: {+ ...previewData,+ rows: _.uniq(previewData.rows ?? []),+ },+ },+ ];+ }, [previewData]);++ const errorMessage = useMemo(() => {+ if (error) {+ return getErrorMessage(error);+ }+ if (data?.status === "failed") {+ return data.message ?? "Transform preview failed";+ }+ return undefined;+ }, [data, error]);++ const hasRequest = Boolean(workspaceId && transformId && query);+ const isPending = hasRequest && !data && !error && !isLoading;+ const isFetching = isLoading || isPending;++ return {+ rawSeries,+ isFetching,+ isLoading,+ isError: Boolean(errorMessage),+ error: errorMessage,+ };+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/GraphTab.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/GraphTab.tsxnew file mode 100644index 00000000000..3087c396579--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/GraphTab.tsx@@ -0,0 +1,170 @@+import { useMemo } from "react";+import { t } from "ttag";++import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";+import { Box } from "metabase/ui";+import { useGetWorkspaceGraphQuery } from "metabase-enterprise/api";+import { DependencyGraph as DependencyGraphComponent } from "metabase-enterprise/dependencies/components/DependencyGraph/DependencyGraph";+import { WorkspaceGraphNode } from "metabase-enterprise/dependencies/components/DependencyGraph/GraphNode/WorkspaceGraphNode";+import type {+ DependencyEntry,+ WorkspaceDependencyGraph,+ WorkspaceGraphDependencyEdge,+ WorkspaceGraphDependencyNode,+ WorkspaceGraphResponse,+ WorkspaceGraphTableNode,+ WorkspaceGraphTransformNode,+ WorkspaceId,+} from "metabase-types/api";++type GraphTabProps = {+ workspaceId: WorkspaceId;+};++const NODE_TYPES = {+ node: WorkspaceGraphNode,+};++export function GraphTab({ workspaceId }: GraphTabProps) {+ const {+ data: graphData,+ isFetching,+ error,+ } = useGetWorkspaceGraphQuery(workspaceId);++ const dependencyGraph = useDependencyGraph(graphData, workspaceId);++ // Create a dummy entry to initialize GraphComponent correctly (not empty).+ const dummyEntry: DependencyEntry = {+ id: workspaceId,+ type: "workspace-transform",+ };++ if (isFetching || error) {+ return <LoadingAndErrorWrapper error={error} loading={isFetching} />;+ }++ const getGraphUrl = () => `/api/ee/workspace/${workspaceId}/graph`;++ return (+ <Box h="100%" w="100%">+ <DependencyGraphComponent+ entry={dummyEntry}+ graph={dependencyGraph}+ getGraphUrl={getGraphUrl}+ withEntryPicker={false}+ nodeTypes={NODE_TYPES}+ openLinksInNewTab={false}+ />+ </Box>+ );+}++function useDependencyGraph(+ graphData: WorkspaceGraphResponse | undefined,+ workspaceId: WorkspaceId,+): WorkspaceDependencyGraph | undefined {+ return useMemo(() => {+ if (!graphData) {+ return undefined;+ }++ // Transform workspace graph data to match WorkspaceDependencyGraph format+ // Data Studio graph doesn't have a concept of input and output tables,+ // so we need to transform the data to match the expected format.+ const dependencyNodes = [...graphData.nodes].reduce<+ WorkspaceGraphDependencyNode[]+ >((acc, node) => {+ if (node.type === "input-table") {+ const tableNode: WorkspaceGraphTableNode = {+ id: node.id,+ type: "table",+ data: {+ name: node.data?.table || t`Table ${node.id}`,+ display_name: node.data?.table || t`Table ${node.id}`,+ description: null,+ db_id: node.data?.db,+ schema: node.data?.schema,+ fields: [],+ table_id: node.data?.id,+ },+ dependents_count: node.dependents_count,+ };+ acc.push(tableNode);+ } else if (node.type === "workspace-transform") {+ const transformNode: WorkspaceGraphTransformNode = {+ id: node.id,+ type: "workspace-transform",+ data: {+ name: node.data?.name || `Transform ${node.id}`,+ workspace_id: workspaceId,+ ref_id: node.data?.ref_id,+ target: node.data?.target,+ },+ dependents_count: node.dependents_count,+ };+ acc.push(transformNode);++ // Create target table node if target information is available+ if (node.data?.target) {+ const targetTableNode: WorkspaceGraphTableNode = {+ id: `target-${node.id}`,+ type: "table",+ data: {+ name: node.data.target.table || t`Target table ${node.id}`,+ display_name:+ node.data.target.table || t`Target table ${node.id}`,+ description: null,+ db_id: node.data.target.db,+ schema: node.data.target.schema,+ fields: [],+ table_id: node.data.target.table_id ?? undefined,+ },+ dependents_count: {},+ };+ acc.push(targetTableNode);+ }+ }+ return acc;+ }, []);++ // Transform edges to match WorkspaceGraphDependencyEdge format+ const dependencyEdges: WorkspaceGraphDependencyEdge[] = graphData.edges.map(+ (edge) => {+ const fromEntityType: WorkspaceGraphDependencyEdge["from_entity_type"] =+ edge.from_entity_type === "workspace-transform"+ ? "workspace-transform"+ : "table";+ const toEntityType: WorkspaceGraphDependencyEdge["to_entity_type"] =+ edge.to_entity_type === "workspace-transform"+ ? "workspace-transform"+ : "table";++ return {+ from_entity_id: edge.from_entity_id,+ from_entity_type: fromEntityType,+ to_entity_id: edge.to_entity_id,+ to_entity_type: toEntityType,+ };+ },+ );++ // Add edges from workspace-transforms to their target tables+ graphData.nodes.forEach((node) => {+ if (node.type === "workspace-transform" && node.data?.target) {+ const targetEdge: WorkspaceGraphDependencyEdge = {+ from_entity_id: `target-${node.id}`,+ from_entity_type: "table",+ to_entity_id: node.id,+ to_entity_type: "workspace-transform",+ };+ dependencyEdges.push(targetEdge);+ }+ });++ return {+ nodes: dependencyNodes,+ edges: dependencyEdges,+ };+ }, [graphData, workspaceId]);+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/MetabotTab.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/MetabotTab.tsxnew file mode 100644index 00000000000..c7344248c8c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/MetabotTab.tsx@@ -0,0 +1,59 @@+import { PLUGIN_METABOT } from "metabase/plugins";+import type { MetabotSuggestionActions } from "metabase-enterprise/metabot/context";+import { useRegisterMetabotSuggestionActions } from "metabase-enterprise/metabot/context";+import { useRegisterMetabotTransformContext } from "metabase-enterprise/transforms/hooks/use-register-transform-metabot-context";+import type {+ DraftTransformSource,+ TaggedTransform,+ UnsavedTransform,+ WorkspaceTransform,+} from "metabase-types/api";++type AnyWorkspaceTransform =+ | TaggedTransform+ | WorkspaceTransform+ | UnsavedTransform;++type MetabotTabProps = {+ transform?: AnyWorkspaceTransform;+ source?: DraftTransformSource;+ workspaceId?: number;+ suggestionActions?: MetabotSuggestionActions;+};++const MetabotContextRegistration = ({ transform, source }: MetabotTabProps) => {+ useRegisterMetabotTransformContext(transform, source);+ return null;+};++const MetabotSuggestionActionsRegistration = ({+ suggestionActions,+}: {+ suggestionActions?: MetabotSuggestionActions;+}) => {+ useRegisterMetabotSuggestionActions(suggestionActions ?? null);+ return null;+};++export const MetabotTab = ({+ transform,+ source,+ suggestionActions,+}: MetabotTabProps) => {+ const MetabotProvider = PLUGIN_METABOT.getMetabotProvider();+ const MetabotChat = PLUGIN_METABOT.MetabotChat;++ return (+ <MetabotProvider>+ <MetabotContextRegistration transform={transform} source={source} />+ <MetabotSuggestionActionsRegistration+ suggestionActions={suggestionActions}+ />+ <MetabotChat+ config={{+ suggestionModels: ["transform", "database", "table"],+ }}+ />+ </MetabotProvider>+ );+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/SetupLog.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/SetupLog.tsxnew file mode 100644index 00000000000..d6df014c6de--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/SetupLog.tsx@@ -0,0 +1,87 @@+import { match } from "ts-pattern";+import { t } from "ttag";++import { LoadingAndGenericErrorWrapper } from "metabase/common/components/LoadingAndGenericErrorWrapper";+import { Group, Icon, Loader, Stack, Text, Tooltip } from "metabase/ui";+import type { WorkspaceLogStatus } from "metabase-types/api";++import type { SetupStatus } from "./useWorkspaceData";++type SetupLogProps = {+ setupStatus: SetupStatus;+};+export const SetupLog = ({ setupStatus }: SetupLogProps) => {+ const { workspace, error, isLoading, logs } = setupStatus;++ if (error || isLoading) {+ return <LoadingAndGenericErrorWrapper error={error} loading={isLoading} />;+ }++ return (+ <Stack+ bd="1px solid var(--mb-color-border)"+ bg="background-info"+ bdrs="md"+ gap="sm"+ p="md"+ >+ {logs.length === 0 && workspace?.status === "pending" && (+ <Loader size="xs" />+ )}++ {logs.length === 0 && workspace?.status !== "pending" && (+ <Text>{t`No logs to display`}</Text>+ )}++ {logs.map((log) => {+ return (+ <Group gap="xs" key={log.id} wrap="nowrap">+ <Tooltip disabled={!log.message} label={log.message}>+ <Group>+ <LogIcon status={log.status} />+ </Group>+ </Tooltip>++ <span>+ {log.description}+ {log.status === "started" && "…"}+ </span>+ </Group>+ );+ })}++ {workspace?.status === "ready" && (+ <Group gap="xs" wrap="nowrap">+ <LogIcon status="success" />++ {t`Workspace ready!`}+ </Group>+ )}++ {workspace?.status === "archived" && (+ <Group gap="xs" wrap="nowrap">+ <Icon c="text-tertiary" name="archive" />++ {t`Workspace is archived`}+ </Group>+ )}+ </Stack>+ );+};++function LogIcon({ status }: { status: WorkspaceLogStatus | null }) {+ return match(status)+ .with("failure", () => {+ return <Icon c="warning" name="warning_triangle_filled" />;+ })+ .with("started", () => {+ return <Loader size="xs" />;+ })+ .with("success", () => {+ return <Icon c="success" name="check" />;+ })+ .with(null, () => {+ return <Icon name="chevronright" />;+ })+ .exhaustive();+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/SetupTab.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/SetupTab.tsxnew file mode 100644index 00000000000..ff2cc87e382--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/SetupTab.tsx@@ -0,0 +1,115 @@+import { useCallback, useMemo } from "react";+import { t } from "ttag";++import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";+import { useMetadataToasts } from "metabase/metadata/hooks";+import { Select, Stack, Text, Title } from "metabase/ui";+import {+ useGetWorkspaceAllowedDatabasesQuery,+ useUpdateWorkspaceMutation,+} from "metabase-enterprise/api/workspace";+import type { DatabaseId, Workspace } from "metabase-types/api";++import { isWorkspaceUninitialized } from "../../utils";++import { SetupLog } from "./SetupLog";+import type { SetupStatus } from "./useWorkspaceData";++interface SetupTabProps {+ databaseId?: DatabaseId | null;+ workspace: Workspace;+ setupStatus: SetupStatus;+}++export const SetupTab = ({+ databaseId,+ workspace,+ setupStatus,+}: SetupTabProps) => {+ const {+ data: allowedDatabases,+ isLoading,+ error,+ } = useGetWorkspaceAllowedDatabasesQuery();+ const [updateWorkspace] = useUpdateWorkspaceMutation();+ const { sendErrorToast, sendSuccessToast } = useMetadataToasts();+ const workspaceId = workspace.id;++ const handleDatabaseChange = useCallback(+ async (value: string) => {+ const newDatabaseId = Number(value);+ if (newDatabaseId !== databaseId) {+ try {+ await updateWorkspace({+ id: workspaceId,+ database_id: newDatabaseId,+ }).unwrap();+ sendSuccessToast(t`Successfully updated workspace database`);+ } catch (error) {+ sendErrorToast(t`Failed to update workspace database`);+ }+ }+ },+ [+ databaseId,+ workspaceId,+ updateWorkspace,+ sendErrorToast,+ sendSuccessToast,+ ],+ );++ const databaseOptions = useMemo(+ () =>+ allowedDatabases?.databases+ ?.filter((db) => db.enabled)+ ?.map((db) => ({+ value: db.id.toString(),+ label: db.name,+ })) ?? [],+ [allowedDatabases?.databases],+ );++ return (+ <Stack gap="lg">+ <Stack gap="xs">+ <Title order={3}>{t`Workspace Setup`}</Title>++ <Text c="text-secondary">+ {t`Configure your data warehouse connection for this workspace`}+ </Text>+ </Stack>++ <Stack gap="xs">+ <Text fw="bold">{t`Data warehouse`}</Text>++ <LoadingAndErrorWrapper error={error} loading={isLoading}>+ <Select+ data={databaseOptions}+ value={databaseId?.toString() ?? ""}+ onChange={handleDatabaseChange}+ placeholder={+ allowedDatabases != null && databaseOptions.length === 0+ ? t`No database supports workspaces`+ : t`Select a database`+ }+ disabled={!isWorkspaceUninitialized(workspace)}+ maw="20rem"+ />+ </LoadingAndErrorWrapper>++ <Text c="text-tertiary" size="sm">+ {t`Data warehouse selection is locked after adding transforms to the workspace`}+ </Text>+ </Stack>++ {!isWorkspaceUninitialized(workspace) && (+ <Stack gap="xs">+ <Text fw="bold">{t`Setup log`}</Text>++ {setupStatus && <SetupLog setupStatus={setupStatus} />}+ </Stack>+ )}+ </Stack>+ );+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformEditor.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformEditor.tsxnew file mode 100644index 00000000000..a2c41e8ccba--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformEditor.tsx@@ -0,0 +1,81 @@+import { useMemo, useState } from "react";+import _ from "underscore";++import {+ TransformQueryPageEditor,+ type TransformQueryPageEditorUiState,+} from "metabase-enterprise/transforms/pages/TransformQueryPage/TransformQueryPage";+import type { DatasetQuery, DraftTransformSource } from "metabase-types/api";++type TransformEditorProps = {+ disabled: boolean;+ hideRunButton?: boolean;+ source: DraftTransformSource;+ onChange: (source: DraftTransformSource) => void;+ proposedSource?: DraftTransformSource;+ onAcceptProposed?: () => void;+ onRejectProposed?: () => void;+ onRunQueryStart?: (query: DatasetQuery) => boolean | void;+ onRunTransform?: (result: any) => void;+ /** Custom run handler for Python transforms (used for dry-run) */+ onRun?: () => void;+};++export function TransformEditor({+ disabled,+ hideRunButton,+ source,+ onChange,+ proposedSource,+ onAcceptProposed,+ onRejectProposed,+ onRunQueryStart,+ onRunTransform,+ onRun,+}: TransformEditorProps) {+ const [uiState, setUiState] = useState(getInitialUiStateForTransform);++ const uiOptions = useMemo(+ () => ({+ canChangeDatabase: false,+ readOnly: disabled,+ hidePreview: true,+ hideRunButton,+ resizable: false,+ }),+ [disabled, hideRunButton],+ );++ const handleSourceChange = (source: DraftTransformSource) => {+ onChange?.(source);+ };++ return (+ <TransformQueryPageEditor+ source={source}+ proposedSource={proposedSource}+ uiState={uiState}+ setUiState={setUiState}+ uiOptions={uiOptions}+ databases={[]}+ isEditMode={true}+ setSourceAndRejectProposed={handleSourceChange}+ acceptProposed={onAcceptProposed ?? _.noop}+ rejectProposed={onRejectProposed ?? _.noop}+ onRunQueryStart={onRunQueryStart}+ onRunTransform={onRunTransform}+ onRun={onRun}+ />+ );+}++function getInitialUiStateForTransform(): TransformQueryPageEditorUiState {+ return {+ lastRunResult: null,+ lastRunQuery: null,+ selectionRange: [],+ modalSnippet: null,+ sidebarType: null,+ modalType: null,+ };+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformEditor.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformEditor.unit.spec.tsxnew file mode 100644index 00000000000..94caf230a85--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformEditor.unit.spec.tsx@@ -0,0 +1,123 @@+import {+ setupAdhocQueryMetadataEndpoint,+ setupDatabasesEndpoints,+ setupSearchEndpoints,+ setupTablesEndpoints,+ setupWorkspaceCheckoutEndpoint,+ setupWorkspacesEndpoint,+} from "__support__/server-mocks";+import { renderWithProviders } from "__support__/ui";+import type { DraftTransformSource } from "metabase-types/api";+import {+ createMockCardQueryMetadata,+ createMockDatabase,+ createMockTable,+} from "metabase-types/api/mocks";+import { createMockState } from "metabase-types/store/mocks";++import { TransformEditor } from "./TransformEditor";++type UiOptions = {+ hideRunButton?: boolean;+};++type MockProps = {+ uiOptions?: UiOptions;+};++// Mock the TransformQueryPageEditor to capture uiOptions+const mockTransformQueryPageEditor = jest.fn((_props: MockProps) => (+ <div data-testid="mock-transform-query-page-editor" />+));++jest.mock(+ "metabase-enterprise/transforms/pages/TransformQueryPage/TransformQueryPage",+ () => ({+ TransformQueryPageEditor: (props: MockProps) =>+ mockTransformQueryPageEditor(props),+ }),+);++const DATABASE_ID = 1;+const mockDatabase = createMockDatabase({+ id: DATABASE_ID,+ name: "Test Database",+});+const mockTable = createMockTable({+ id: 1,+ db_id: DATABASE_ID,+ name: "Test Table",+});++const mockNativeSource: DraftTransformSource = {+ type: "query",+ query: {+ type: "native",+ database: DATABASE_ID,+ native: {+ query: "SELECT * FROM test_table",+ },+ },+};++type SetupOpts = {+ source?: DraftTransformSource;+ hideRunButton?: boolean;+ disabled?: boolean;+};++function setup({+ source = mockNativeSource,+ hideRunButton = false,+ disabled = false,+}: SetupOpts = {}) {+ setupDatabasesEndpoints([mockDatabase]);+ setupTablesEndpoints([mockTable]);+ setupSearchEndpoints([]);+ setupWorkspacesEndpoint([]);+ setupWorkspaceCheckoutEndpoint({});+ setupAdhocQueryMetadataEndpoint(+ createMockCardQueryMetadata({ databases: [mockDatabase] }),+ );++ mockTransformQueryPageEditor.mockClear();++ renderWithProviders(+ <TransformEditor+ source={source}+ hideRunButton={hideRunButton}+ disabled={disabled}+ onChange={jest.fn()}+ onAcceptProposed={jest.fn()}+ onRejectProposed={jest.fn()}+ />,+ {+ storeInitialState: createMockState(),+ },+ );+}++function getLastUiOptions(): UiOptions | undefined {+ const calls = mockTransformQueryPageEditor.mock.calls;+ if (calls.length === 0) {+ return undefined;+ }+ const lastCall = calls[calls.length - 1];+ return lastCall[0]?.uiOptions;+}++describe("TransformEditor", () => {+ describe("hideRunButton prop", () => {+ it("should not hide run button when hideRunButton is false", () => {+ setup({ hideRunButton: false });+ const uiOptions = getLastUiOptions();+ expect(uiOptions?.hideRunButton).toBe(false);+ });++ it("should hide run button when hideRunButton is true", () => {+ setup({ hideRunButton: true });+ const uiOptions = getLastUiOptions();+ expect(uiOptions?.hideRunButton).toBe(true);+ });+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/SaveTransformButton.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/SaveTransformButton.tsxnew file mode 100644index 00000000000..da24eafb6e9--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/SaveTransformButton.tsx@@ -0,0 +1,263 @@+import { useMemo, useState } from "react";+import { t } from "ttag";++import { useDispatch, useSelector } from "metabase/lib/redux";+import { useMetadataToasts } from "metabase/metadata/hooks";+import { getMetadata } from "metabase/selectors/metadata";+import { Button } from "metabase/ui";+import {+ useCreateWorkspaceTransformMutation,+ useUpdateWorkspaceTransformMutation,+ workspaceApi,+} from "metabase-enterprise/api";+import { idTag, listTag } from "metabase-enterprise/api/tags";+import { CreateTransformModal } from "metabase-enterprise/transforms/pages/NewTransformPage/CreateTransformModal/CreateTransformModal";+import type { NewTransformValues } from "metabase-enterprise/transforms/pages/NewTransformPage/CreateTransformModal/form";+import { isSourceEmpty } from "metabase-enterprise/transforms/utils";+import type {+ CreateWorkspaceTransformRequest,+ DatabaseId,+ SchemaName,+ TaggedTransform,+ TransformSource,+ WorkspaceId,+ WorkspaceTransform,+ WorkspaceTransformListItem,+} from "metabase-types/api";+import {+ isTaggedTransform,+ isUnsavedTransform,+ isWorkspaceTransform,+} from "metabase-types/api";++import type { AnyWorkspaceTransform } from "../WorkspaceProvider";+import { useWorkspace } from "../WorkspaceProvider";++import { useEditedTransform } from "./useEditedTransform";+import { useTransformValidation } from "./useTransformValidation";++const schemasFilter = (schema: SchemaName) =>+ !schema.startsWith("mb__isolation");++type SaveTransformButtonProps = {+ databaseId: DatabaseId;+ workspaceId: WorkspaceId;+ workspaceTransforms: WorkspaceTransformListItem[];+ transform: AnyWorkspaceTransform;+ isDisabled: boolean;+ onSaveTransform: (transform: TaggedTransform | WorkspaceTransform) => void;+};++export const SaveTransformButton = ({+ databaseId,+ workspaceId,+ workspaceTransforms,+ transform,+ isDisabled,+ onSaveTransform,+}: SaveTransformButtonProps) => {+ const dispatch = useDispatch();+ const metadata = useSelector(getMetadata);+ const { sendSuccessToast, sendErrorToast } = useMetadataToasts();+ const {+ updateTransformState,+ removeUnsavedTransform,+ removeWorkspaceTransform,+ removeEditedTransform,+ } = useWorkspace();++ const [updateTransformMutation, { isLoading: isUpdating }] =+ useUpdateWorkspaceTransformMutation();+ const [createWorkspaceTransform] = useCreateWorkspaceTransformMutation();+ const [saveModalOpen, setSaveModalOpen] = useState(false);++ const { editedTransform, hasChanges } = useEditedTransform(transform);++ // Determine transform state+ const isSaved =+ isWorkspaceTransform(transform) &&+ workspaceTransforms.some((t) => t.ref_id === transform.ref_id);+ const isNewTransform = isUnsavedTransform(transform);++ const validationSchemaExtension = useTransformValidation({+ databaseId,+ target: isUnsavedTransform(transform) ? undefined : transform.target,+ workspaceId,+ });++ const initialCreateTransformValues = useMemo(+ () => ({+ name: transform.name,+ }),+ [transform.name],+ );++ // Handler for updating existing workspace transform (scenario 1)+ const handleUpdateTransform = async () => {+ if (!isWorkspaceTransform(transform)) {+ throw new Error(t`This is not a workspace transform`);+ }++ try {+ const updated = await updateTransformMutation({+ workspaceId,+ transformId: transform.ref_id,+ source: editedTransform.source as TransformSource,+ name: editedTransform.name,+ target: {+ type: "table",+ name:+ "target" in editedTransform+ ? editedTransform.target.name+ : transform.target.name,+ schema: transform.target.schema,+ database: databaseId,+ },+ }).unwrap();++ updateTransformState(updated);+ } catch (error) {+ sendErrorToast(t`Failed to save transform`);+ }+ };++ // Handler for creating new transform via modal (scenario 2)+ const handleCreateNewTransform = async (+ values: NewTransformValues,+ ): Promise<WorkspaceTransform> => {+ try {+ const request: CreateWorkspaceTransformRequest = values.incremental+ ? {+ id: workspaceId,+ name: values.name,+ description: null,+ source: editedTransform.source as TransformSource,+ target: {+ type: "table-incremental" as const,+ name: values.targetName,+ schema: values.targetSchema,+ database: databaseId,+ "target-incremental-strategy": {+ type: "append" as const,+ },+ },+ }+ : {+ id: workspaceId,+ name: values.name,+ description: null,+ source: editedTransform.source as TransformSource,+ target: {+ type: "table" as const,+ name: values.targetName,+ schema: values.targetSchema,+ database: databaseId,+ },+ };++ const savedTransform = await createWorkspaceTransform(request).unwrap();++ // Remove from unsaved transforms and refresh workspace+ if (isNewTransform) {+ removeUnsavedTransform(transform.id);+ }++ // Invalidate workspace transforms after creating new one+ dispatch(+ workspaceApi.util.invalidateTags([+ idTag("workspace-transforms", workspaceId),+ listTag("external-transform"),+ ]),+ );++ onSaveTransform(savedTransform);++ sendSuccessToast(t`Transform saved successfully`);+ setSaveModalOpen(false);++ return savedTransform;+ } catch (error) {+ sendErrorToast(t`Failed to save transform`);+ throw error;+ }+ };++ // Handler for checking out external transform (scenario 3)+ const handleSaveExternalTransform = async () => {+ if (!isTaggedTransform(transform)) {+ return;+ }++ try {+ const savedTransform = await createWorkspaceTransform({+ id: workspaceId,+ global_id: transform.id,+ name: editedTransform.name,+ description: transform.description,+ source: editedTransform.source as TransformSource,+ target: transform.target,+ tag_ids: transform.tag_ids,+ }).unwrap();++ removeEditedTransform(transform.id);+ removeWorkspaceTransform(transform.id);+ onSaveTransform(savedTransform);+ } catch (error) {+ sendErrorToast(t`Failed to save transform`);+ }+ };++ // Determine button props based on scenario+ const getButtonProps = () => {+ if (isSaved) {+ return {+ disabled: !hasChanges || isDisabled,+ loading: isUpdating,+ variant: "filled" as const,+ onClick: handleUpdateTransform,+ };+ }++ if (isNewTransform) {+ const hasEmptyContent = isSourceEmpty(+ editedTransform.source,+ databaseId,+ metadata,+ );+ return {+ disabled: isDisabled || hasEmptyContent,+ variant: "filled" as const,+ onClick: () => setSaveModalOpen(true),+ };+ }++ // External transform+ return {+ disabled: isDisabled,+ variant: hasChanges ? ("filled" as const) : ("default" as const),+ onClick: handleSaveExternalTransform,+ };+ };++ const buttonProps = getButtonProps();++ return (+ <>+ <Button size="sm" {...buttonProps}>{t`Save`}</Button>++ {saveModalOpen && (+ <CreateTransformModal+ source={editedTransform.source as TransformSource}+ defaultValues={initialCreateTransformValues}+ onClose={() => setSaveModalOpen(false)}+ schemasFilter={schemasFilter}+ validationSchemaExtension={validationSchemaExtension}+ validateOnMount+ handleSubmit={handleCreateNewTransform}+ targetDescription={t`This is the main table this transform owns. Runs from this workspace write to an isolated workspace copy, so the original table isn't changed until you merge the workspace.`}+ showIncrementalSettings={false}+ />+ )}+ </>+ );+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/TransformTab.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/TransformTab.tsxnew file mode 100644index 00000000000..9fc0a2828d6--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/TransformTab.tsx@@ -0,0 +1,448 @@+import { useDisclosure } from "@mantine/hooks";+import { useCallback, useEffect, useRef, useState } from "react";+import { t } from "ttag";++import { useDispatch, useSelector } from "metabase/lib/redux";+import { useMetadataToasts } from "metabase/metadata/hooks";+import { getMetadata } from "metabase/selectors/metadata";+import { Box, Button, Group, Icon, Stack } from "metabase/ui";+import { useDryRunWorkspaceTransformMutation } from "metabase-enterprise/api";+import { useWorkspaceTransformRun } from "metabase-enterprise/data-studio/workspaces/hooks";+import {+ deactivateSuggestedTransform,+ getMetabotSuggestedTransform,+} from "metabase-enterprise/metabot/state";+import { RunStatus } from "metabase-enterprise/transforms/components/RunStatus";+import {+ getInitialNativeSource,+ getInitialPythonSource,+} from "metabase-enterprise/transforms/pages/NewTransformPage/utils";+import * as Lib from "metabase-lib";+import Question from "metabase-lib/v1/Question";+import type {+ DatabaseId,+ DatasetQuery,+ DraftTransformSource,+ TaggedTransform,+ TestPythonTransformResponse,+ WorkspaceId,+ WorkspaceTransform,+ WorkspaceTransformDryRunResponse,+ WorkspaceTransformListItem,+} from "metabase-types/api";+import { isUnsavedTransform, isWorkspaceTransform } from "metabase-types/api";++import { WorkspaceRunButton } from "../../../components/WorkspaceRunButton/WorkspaceRunButton";+import { TransformEditor } from "../TransformEditor";+import type { AnyWorkspaceTransform, TableTab } from "../WorkspaceProvider";+import {+ getNumericTransformId,+ getTransformId,+ useWorkspace,+} from "../WorkspaceProvider";++import { SaveTransformButton } from "./SaveTransformButton";+import { UpdateTargetModal } from "./UpdateTargetModal/UpdateTargetModal";+import { useEditedTransform } from "./useEditedTransform";++interface Props {+ databaseId: DatabaseId;+ transform: AnyWorkspaceTransform;+ workspaceId: WorkspaceId;+ workspaceTransforms: WorkspaceTransformListItem[];+ isDisabled: boolean;+ onSaveTransform: (transform: TaggedTransform | WorkspaceTransform) => void;+}++export function TransformTab({+ databaseId,+ transform,+ workspaceId,+ workspaceTransforms,+ isDisabled,+ onSaveTransform,+}: Props) {+ const {+ updateTransformState,+ addOpenedTab,+ editedTransforms,+ patchEditedTransform,+ } = useWorkspace();+ const { sendErrorToast } = useMetadataToasts();+ const [+ isChangeTargetModalOpen,+ { open: openChangeTargetModal, close: closeChangeTargetModal },+ ] = useDisclosure();+ const dispatch = useDispatch();+ const transformId = getTransformId(transform);+ const numericTransformId = getNumericTransformId(transform);+ const suggestedTransform = useSelector((state) =>+ getMetabotSuggestedTransform(state, numericTransformId),+ );+ const metadata = useSelector(getMetadata);++ // Local source state to prevent re-renders on every keystroke+ // This is the key optimization: we manage source locally and sync to provider+ const [localSource, setLocalSource] = useState<DraftTransformSource>(() => {+ const edited = editedTransforms.get(transformId);+ return edited?.source ?? transform.source;+ });++ // Track previous transform ID to detect when we switch transforms+ const prevTransformIdRef = useRef(transformId);++ // Reset local source when switching to a different transform+ useEffect(() => {+ if (prevTransformIdRef.current !== transformId) {+ const edited = editedTransforms.get(transformId);+ setLocalSource(edited?.source ?? transform.source);+ prevTransformIdRef.current = transformId;+ }+ // Note: editedTransforms is intentionally excluded from deps+ // We only want to sync when transform ID changes, not on every edit+ }, [transformId, transform.source, editedTransforms]);++ const { editedTransform, hasChanges, providerEdited } =+ useEditedTransform(transform);++ const wsTransform = isWorkspaceTransform(transform) ? transform : null;+ const currentSource = providerEdited?.source ?? localSource;++ // Run transform hook - handles run state, API calls, and error handling+ const { statusRun, buttonRun, isRunStatusLoading, isRunning, handleRun } =+ useWorkspaceTransformRun({+ workspaceId,+ transform: wsTransform,+ });++ const handleRunQueryStart = useCallback(+ (query: DatasetQuery) => {+ // Preview doesn't have a real table ID, but we need to provide a unique identifier+ // to open preview tab correctly.+ const tableTabId = `table-${transformId}`;++ // Use dry-run API to preview the saved transform's source.+ // The preview button is only shown when transform is saved and has no unsaved changes,+ // so we can safely use dry-run which executes the saved source.+ const tableTab: TableTab = {+ id: tableTabId,+ name: t`Preview (${transform.name})`,+ type: "table",+ table: {+ tableId: null,+ name: t`Preview (${transform.name})`,+ query,+ transformId: String(transformId),+ },+ };+ addOpenedTab(tableTab);+ return false;+ },+ [transformId, transform.name, addOpenedTab],+ );++ const handleRunTransform = useCallback(+ (result: unknown) => {+ const tableTabId = `table-${transformId}`;++ const tableTab: TableTab = {+ id: tableTabId,+ name: t`Preview (${transform.name})`,+ type: "table",+ table: {+ tableId: null,+ name: t`Preview (${transform.name})`,+ transformId: String(transformId),+ pythonPreviewResult: result,+ },+ };++ addOpenedTab(tableTab);+ },+ [transformId, transform.name, addOpenedTab],+ );++ // Dry-run handler for Python transforms+ const [dryRunTransform] = useDryRunWorkspaceTransformMutation();++ const handlePythonRun = useCallback(async () => {+ if (!wsTransform) {+ return;+ }++ try {+ const result = await dryRunTransform({+ workspaceId,+ transformId: wsTransform.ref_id,+ }).unwrap();++ // Convert dry-run response to TestPythonTransformResponse format+ const converted = convertDryRunToTestResponse(result);+ handleRunTransform(converted);+ } catch (error) {+ // Handle error - show in preview results+ const errorResult: TestPythonTransformResponse = {+ error: {+ message:+ error instanceof Error ? error.message : "Transform preview failed",+ },+ };+ handleRunTransform(errorResult);+ }+ }, [wsTransform, workspaceId, dryRunTransform, handleRunTransform]);++ const normalizeSource = useCallback(+ (source: DraftTransformSource): DraftTransformSource => {+ if (source.type !== "query") {+ return source;+ }++ const question = Question.create({+ dataset_query: source.query,+ metadata,+ });+ const query = question.query();+ const { isNative } = Lib.queryDisplayInfo(query);+ const normalizedQuery = isNative+ ? Lib.withNativeQuery(query, Lib.rawNativeQuery(query))+ : query;++ return {+ type: "query" as const,+ query: question.setQuery(normalizedQuery).datasetQuery(),+ };+ },+ [metadata],+ );++ // Show proposed source when there's an active suggested transform+ // Even if sources are the same (e.g., when transform was created with suggested source),+ // we still want to show the buttons so user can reject the suggestion+ const proposedSource: DraftTransformSource | undefined =+ suggestedTransform?.source+ ? normalizeSource(suggestedTransform.source)+ : undefined;++ const isSaved =+ isWorkspaceTransform(transform) &&+ workspaceTransforms.some((t) => t.ref_id === transform.ref_id);+ const isEditable = !isDisabled;++ const handleSourceChange = useCallback(+ (source: DraftTransformSource) => {+ setLocalSource(source);+ patchEditedTransform(transformId, { source });+ },+ [transformId, patchEditedTransform],+ );++ const handleMetabotAcceptProposed = useCallback(() => {+ if (proposedSource == null) {+ return;+ }++ // Allow applying suggestions to both saved workspace transforms and unsaved transforms+ // Only block if it's a tagged transform (external transform) that hasn't been checked out+ const isUnsaved = isUnsavedTransform(transform);+ if (!isSaved && !isUnsaved) {+ sendErrorToast(+ t`Add this transform to the workspace before applying Metabot changes.`,+ );+ return;+ }++ // When accepting, we DO need to update localSource to reset the editor+ setLocalSource(proposedSource);+ patchEditedTransform(transformId, { source: proposedSource });+ dispatch(deactivateSuggestedTransform(suggestedTransform?.id));+ }, [+ proposedSource,+ dispatch,+ suggestedTransform?.id,+ isSaved,+ transform,+ sendErrorToast,+ transformId,+ patchEditedTransform,+ ]);++ const handleMetabotRejectProposed = useCallback(() => {+ if (suggestedTransform) {+ dispatch(deactivateSuggestedTransform(suggestedTransform.id));++ // For unsaved transforms, clear the source back to empty state when rejecting+ if (isUnsavedTransform(transform)) {+ const emptySource = createEmptySourceForType(currentSource);+ if (emptySource) {+ // When rejecting, we DO need to update localSource to reset the editor+ setLocalSource(emptySource);+ patchEditedTransform(transformId, { source: emptySource });+ }+ }+ }+ }, [+ dispatch,+ suggestedTransform,+ transform,+ currentSource,+ transformId,+ patchEditedTransform,+ ]);++ const handleTargetUpdate = useCallback(+ (updatedTransform?: WorkspaceTransform) => {+ if (updatedTransform) {+ updateTransformState(updatedTransform);+ }+ closeChangeTargetModal();+ },+ [updateTransformState, closeChangeTargetModal],+ );++ return (+ <Stack gap={0} h="100%">+ <Stack+ data-testid="transform-tab-header"+ flex="0 0 auto"+ gap="sm"+ p="md"+ style={{ borderBottom: "1px solid var(--mb-color-border)" }}+ >+ <Group justify="space-between">+ <Group>+ {isSaved && (+ <Button+ leftSection={<Icon name="pencil_lines" />}+ size="sm"+ disabled={isRunning || hasChanges || isDisabled}+ onClick={openChangeTargetModal}+ >{t`Change target`}</Button>+ )}+ </Group>++ <Group>+ {isSaved && (+ <WorkspaceRunButton+ id={numericTransformId}+ run={buttonRun}+ isDisabled={hasChanges || isDisabled}+ onRun={handleRun}+ />+ )}++ <SaveTransformButton+ databaseId={databaseId}+ workspaceId={workspaceId}+ workspaceTransforms={workspaceTransforms}+ transform={transform}+ isDisabled={isDisabled}+ onSaveTransform={onSaveTransform}+ />+ </Group>+ </Group>++ {isSaved &&+ (isRunStatusLoading ? (+ <Group gap="sm">+ <Icon c="text-secondary" name="sync" />+ <Box>{t`Loading run status...`}</Box>+ </Group>+ ) : (+ <RunStatus+ run={statusRun}+ neverRunMessage={t`This transform hasn't been run before.`}+ />+ ))}+ </Stack>++ {editedTransform && (+ <Box+ flex="1 1 auto"+ style={{ overflow: "auto", "--native-query-editor-flex": "1 1 auto" }}+ >+ <TransformEditor+ disabled={!isEditable}+ hideRunButton={!isSaved || hasChanges}+ source={localSource}+ proposedSource={proposedSource}+ onAcceptProposed={handleMetabotAcceptProposed}+ onRejectProposed={handleMetabotRejectProposed}+ onChange={handleSourceChange}+ onRunQueryStart={handleRunQueryStart}+ onRunTransform={handleRunTransform}+ onRun={handlePythonRun}+ />+ </Box>+ )}++ {isChangeTargetModalOpen && wsTransform && (+ <UpdateTargetModal+ transform={wsTransform}+ onUpdate={handleTargetUpdate}+ onClose={closeChangeTargetModal}+ />+ )}+ </Stack>+ );+}++/**+ * Creates an empty source based on the type of the current source.+ * Used when rejecting a metabot suggestion on an unsaved transform.+ */+function createEmptySourceForType(+ currentSource: DraftTransformSource,+): DraftTransformSource | null {+ if (currentSource.type === "query") {+ const emptySource = getInitialNativeSource();+ // Preserve the database ID from the current source+ const databaseId =+ "database" in currentSource.query ? currentSource.query.database : null;+ if (databaseId) {+ return {+ ...emptySource,+ query: { ...emptySource.query, database: databaseId },+ };+ }+ return emptySource;+ }++ if (currentSource.type === "python") {+ const emptySource = getInitialPythonSource();+ // Preserve the database ID from the current source+ return {+ ...emptySource,+ "source-database": currentSource["source-database"],+ };+ }++ return null;+}++/**+ * Converts a dry-run response to the TestPythonTransformResponse format+ * expected by PythonEditorResults component.+ */+function convertDryRunToTestResponse(+ dryRun: WorkspaceTransformDryRunResponse,+): TestPythonTransformResponse {+ if (dryRun.status === "failed") {+ return {+ error: { message: dryRun.message ?? "Transform failed" },+ logs: dryRun.logs ?? "",+ };+ }++ const cols = (dryRun.data?.cols ?? []).map((col) => ({+ name: col.name ?? "",+ }));+ // Convert rows from array format [[val1, val2], ...] to object format [{col1: val1, col2: val2}, ...]+ const rows = (dryRun.data?.rows ?? []).map((row) =>+ Object.fromEntries(cols.map((col, i) => [col.name, row[i]])),+ );++ return {+ logs: dryRun.logs ?? "",+ output: { cols, rows },+ };+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/UpdateTargetModal/UpdateTargetModal.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/UpdateTargetModal/UpdateTargetModal.tsxnew file mode 100644index 00000000000..888166c66ad--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/UpdateTargetModal/UpdateTargetModal.tsx@@ -0,0 +1,201 @@+import { useMemo } from "react";+import { t } from "ttag";+import * as Yup from "yup";++import { hasFeature } from "metabase/admin/databases/utils";+import {+ skipToken,+ useGetDatabaseQuery,+ useListDatabaseSchemasQuery,+} from "metabase/api";+import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";+import {+ Form,+ FormErrorMessage,+ FormProvider,+ FormSubmitButton,+ FormTextInput,+} from "metabase/forms";+import * as Errors from "metabase/lib/errors";+import { useMetadataToasts } from "metabase/metadata/hooks";+import { Box, Button, FocusTrap, Group, Modal, Stack, Text } from "metabase/ui";+import { useUpdateWorkspaceTransformMutation } from "metabase-enterprise/api";+import { SchemaFormSelect } from "metabase-enterprise/transforms/components/SchemaFormSelect";+import { sourceDatabaseId } from "metabase-enterprise/transforms/utils";+import type {+ UpdateWorkspaceTransformRequest,+ WorkspaceTransform,+} from "metabase-types/api";++import { useTransformValidation } from "../useTransformValidation";++type UpdateTargetModalProps = {+ transform: WorkspaceTransform;+ onUpdate: (transform?: WorkspaceTransform) => void;+ onClose: () => void;+};++export function UpdateTargetModal({+ transform,+ onUpdate,+ onClose,+}: UpdateTargetModalProps) {+ return (+ <Modal+ title={t`Change the target for this transform`}+ opened+ padding="xl"+ onClose={onClose}+ >+ <FocusTrap.InitialFocus />+ <Box mb="md">+ <Text size="sm" c="text-secondary">+ {t`When you change the target in a workspace, runs from this workspace write to an isolated workspace table. The original table isn't changed until you merge the workspace.`}+ </Text>+ </Box>+ <UpdateTargetForm+ transform={transform}+ onUpdate={onUpdate}+ onClose={onClose}+ />+ </Modal>+ );+}++type EditTransformValues = {+ targetName: string;+ targetSchema: string | null;+};++const EDIT_TRANSFORM_SCHEMA = Yup.object({+ targetName: Yup.string().required(Errors.required),+ targetSchema: Yup.string().nullable(),+});++type UpdateTargetFormProps = {+ transform: WorkspaceTransform;+ onUpdate: (transform?: WorkspaceTransform) => void;+ onClose: () => void;+};++function UpdateTargetForm({+ transform,+ onUpdate,+ onClose,+}: UpdateTargetFormProps) {+ const { source } = transform;+ const { sendSuccessToast, sendErrorToast } = useMetadataToasts();+ const databaseId = sourceDatabaseId(source);+ const [updateWorkspaceTransform] = useUpdateWorkspaceTransformMutation();+ const initialValues = useMemo(() => getInitialValues(transform), [transform]);+ const validationSchemaExtension = useTransformValidation({+ databaseId,+ target: transform.target,+ workspaceId: transform.workspace_id,+ });++ const validationSchema = useMemo(+ () => EDIT_TRANSFORM_SCHEMA.shape(validationSchemaExtension),+ [validationSchemaExtension],+ );++ const {+ data: database,+ isLoading: isDatabaseLoading,+ error: databaseError,+ } = useGetDatabaseQuery(databaseId ? { id: databaseId } : skipToken);++ const {+ data: schemas = [],+ isLoading: isSchemasLoading,+ error: schemasError,+ } = useListDatabaseSchemasQuery(+ databaseId ? { id: databaseId, include_hidden: true } : skipToken,+ );++ const isLoading = isDatabaseLoading || isSchemasLoading;+ const error = databaseError ?? schemasError;+ const supportsSchemas = database && hasFeature(database, "schemas");++ if (isLoading || error != null) {+ return <LoadingAndErrorWrapper loading={isLoading} error={error} />;+ }++ const handleSubmit = async (values: EditTransformValues) => {+ if (!databaseId) {+ throw new Error("Database ID is required");+ }++ try {+ const updatedTransform = await updateWorkspaceTransform(+ getUpdateTargetRequest(transform, values, databaseId),+ ).unwrap();++ sendSuccessToast(t`Transform target updated`);++ onUpdate(updatedTransform);+ } catch (error) {+ sendErrorToast(t`Failed to update transform target`);+ }+ };++ return (+ <FormProvider+ initialValues={initialValues}+ validationSchema={validationSchema}+ onSubmit={handleSubmit}+ enableReinitialize+ validateOnMount+ >+ {({ dirty }) => (+ <Form>+ <Stack gap="lg">+ {supportsSchemas && (+ <SchemaFormSelect+ name="targetSchema"+ label={t`Schema`}+ data={schemas}+ />+ )}+ <FormTextInput name="targetName" label={t`New table name`} />+ <Group>+ <Box flex={1}>+ <FormErrorMessage />+ </Box>+ <Button onClick={onClose}>{t`Cancel`}</Button>+ <FormSubmitButton+ label={t`Change target`}+ variant="filled"+ disabled={!dirty}+ />+ </Group>+ </Stack>+ </Form>+ )}+ </FormProvider>+ );+}++function getInitialValues({ target }: WorkspaceTransform): EditTransformValues {+ return {+ targetName: target.name,+ targetSchema: target.schema ?? null,+ };+}++function getUpdateTargetRequest(+ { ref_id, workspace_id }: WorkspaceTransform,+ { targetName: name, targetSchema: schema }: EditTransformValues,+ databaseId: number,+): UpdateWorkspaceTransformRequest {+ return {+ workspaceId: workspace_id,+ transformId: ref_id,+ target: {+ type: "table",+ name,+ schema,+ database: databaseId,+ },+ };+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/useEditedTransform.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/useEditedTransform.tsnew file mode 100644index 00000000000..71b841a7374--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/useEditedTransform.ts@@ -0,0 +1,48 @@+import { isSameSource } from "metabase-enterprise/transforms/utils";++import type {+ AnyWorkspaceTransform,+ EditedTransform,+} from "../WorkspaceProvider";+import { getTransformId, useWorkspace } from "../WorkspaceProvider";++interface UseEditedTransformResult {+ editedTransform: EditedTransform;+ hasChanges: boolean;+ providerEdited: EditedTransform | undefined;+}++/**+ * Hook to get the edited transform state and detect changes.+ * Used by both TransformTab and SaveTransformButton to avoid duplication.+ */+export function useEditedTransform(+ transform: AnyWorkspaceTransform,+): UseEditedTransformResult {+ const { editedTransforms } = useWorkspace();+ const transformId = getTransformId(transform);+ const providerEdited = editedTransforms.get(transformId);++ const editedTransform: EditedTransform = {+ name: providerEdited?.name ?? transform.name,+ source: providerEdited?.source ?? transform.source,+ target: providerEdited?.target ?? transform.target,+ };++ const hasSourceChanged = !isSameSource(+ editedTransform.source,+ transform.source,+ );+ const hasTargetNameChanged =+ "target" in editedTransform &&+ "target" in transform &&+ transform.target.name !== editedTransform.target.name;+ const hasTargetSchemaChanged =+ "target" in editedTransform &&+ "target" in transform &&+ transform.target.schema !== editedTransform.target.schema;+ const hasChanges =+ hasSourceChanged || hasTargetNameChanged || hasTargetSchemaChanged;++ return { editedTransform, hasChanges, providerEdited };+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/useTransformValidation.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/useTransformValidation.tsnew file mode 100644index 00000000000..ec7bc701643--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/TransformTab/useTransformValidation.ts@@ -0,0 +1,106 @@+import { useCallback, useMemo, useRef } from "react";+import { t } from "ttag";+import * as Yup from "yup";++import { getErrorMessage } from "metabase/api/utils";+import { useValidateTableNameMutation } from "metabase-enterprise/api";+import type {+ DatabaseId,+ TransformTarget,+ WorkspaceId,+} from "metabase-types/api";++type UseTransformValidationParams = {+ databaseId: DatabaseId | null;+ target?: TransformTarget;+ workspaceId: WorkspaceId;+};++export const useTransformValidation = ({+ databaseId,+ target,+ workspaceId,+}: UseTransformValidationParams) => {+ const [validateTableName] = useValidateTableNameMutation();++ const validateTableNameDebounceRef = useRef<{+ timeoutId?: ReturnType<typeof setTimeout>;+ pending?: { resolve: (message: string) => void };+ }>({});++ // I wasn't able to simplify this logic any further, but maybe a review would be nice.+ // We wrap debounced validation call in a promise to properly set form state,+ // and we need to handle pending state to prevent multiple validation calls.+ const debouncedValidateTableName = useCallback(+ ({ name, schema }: { name: string; schema?: string }) => {+ const debounceState = validateTableNameDebounceRef.current;++ if (debounceState.timeoutId) {+ clearTimeout(debounceState.timeoutId);+ }++ debounceState.pending?.resolve("OK");+ debounceState.pending = undefined;++ if (databaseId == null) {+ return t`Database ID is missing`;+ }++ return new Promise<string>((resolve) => {+ const pending = { resolve };+ debounceState.pending = pending;++ debounceState.timeoutId = setTimeout(async () => {+ debounceState.timeoutId = undefined;++ let message: string;+ try {+ message = await validateTableName({+ id: workspaceId,+ db_id: databaseId,+ target: { type: "table", name, schema: schema ?? null },+ }).unwrap();+ } catch (error) {+ message = getErrorMessage(error);+ }++ if (debounceState.pending === pending) {+ debounceState.pending = undefined;+ resolve(message);+ }+ }, 300);+ });+ },+ [databaseId, validateTableName, workspaceId],+ );++ const yupSchema = useMemo(+ () => ({+ targetName: Yup.string()+ .required("Target table name is required")+ .test(async (value, context) => {+ if (!value) {+ return context.createError({+ message: t`Target table name is required`,+ });+ }++ const schema = context.parent.targetSchema;++ if (target && target.name === value && target.schema === schema) {+ return true;+ }++ const message = await debouncedValidateTableName({+ name: value,+ schema,+ });++ return message === "OK" ? true : context.createError({ message });+ }),+ }),+ [debouncedValidateTableName, target],+ );++ return yupSchema;+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/WorkspacePage.module.css b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/WorkspacePage.module.cssnew file mode 100644index 00000000000..310c010425f--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/WorkspacePage.module.css@@ -0,0 +1,26 @@+.resizing {+ user-select: none;+}++.tabsPanel {+ display: flex;+ gap: 0.5rem;+ flex-wrap: nowrap;+ overflow: auto;+ scrollbar-width: thin;++ &::before {+ display: none;+ }++ & button {+ border-radius: 0;+ }+}++.sidebarResizableBox {+ display: flex;+ flex-shrink: 0;+ position: relative;+ height: 100%;+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/WorkspacePage.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/WorkspacePage.tsxnew file mode 100644index 00000000000..22913cbdce1--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/WorkspacePage.tsx@@ -0,0 +1,598 @@+import type { DragEndEvent } from "@dnd-kit/core";+import { DndContext, PointerSensor, useSensor } from "@dnd-kit/core";+import {+ restrictToHorizontalAxis,+ restrictToParentElement,+} from "@dnd-kit/modifiers";+import {+ SortableContext,+ arrayMove,+ horizontalListSortingStrategy,+} from "@dnd-kit/sortable";+import classNames from "classnames";+import { useCallback, useEffect, useRef, useState } from "react";+import { ResizableBox } from "react-resizable";+import type { Route } from "react-router";+import { replace } from "react-router-redux";+import { useLocation } from "react-use";+import { t } from "ttag";++import { NotFound } from "metabase/common/components/ErrorPages";+import { LeaveRouteConfirmModal } from "metabase/common/components/LeaveConfirmModal";+import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";+import { ResizeHandle } from "metabase/common/components/ResizeHandle";+import { Sortable } from "metabase/common/components/Sortable";+import { useDispatch } from "metabase/lib/redux";+import { checkNotNull } from "metabase/lib/types";+import * as Urls from "metabase/lib/urls";+import {+ ActionIcon,+ Box,+ Button,+ Flex,+ Group,+ Icon,+ Stack,+ Tabs,+ Text,+} from "metabase/ui";+import { PaneHeaderInput } from "metabase-enterprise/data-studio/common/components/PaneHeader";+import { MergeWorkspaceModal } from "metabase-enterprise/data-studio/workspaces/components/MergeWorkspaceModal/MergeWorkspaceModal";+import { RunWorkspaceMenu } from "metabase-enterprise/data-studio/workspaces/components/RunWorkspaceMenu/RunWorkspaceMenu";+import { NAME_MAX_LENGTH } from "metabase-enterprise/transforms/constants";++import { isWorkspaceUninitialized } from "../../utils";++import { AddTransformMenu } from "./AddTransformMenu";+import { CodeTab } from "./CodeTab/CodeTab";+import { DataTab, DataTabSidebar } from "./DataTab";+import { GraphTab } from "./GraphTab";+import { SetupTab } from "./SetupTab";+import { TransformTab } from "./TransformTab/TransformTab";+import styles from "./WorkspacePage.module.css";+import {+ WorkspaceProvider,+ type WorkspaceTab,+ getTransformId,+ getTransformTabId,+ useWorkspace,+} from "./WorkspaceProvider";+import { useWorkspaceActions } from "./useWorkspaceActions";+import { useWorkspaceData } from "./useWorkspaceData";++const DEFAULT_SIDEBAR_WIDTH = 400;++type WorkspacePageProps = {+ params: {+ workspaceId: string;+ };+ route: Route;+ transformId?: string;+};++function WorkspacePageContent({+ params,+ route,+ transformId,+}: WorkspacePageProps) {+ const pointerSensor = useSensor(PointerSensor, {+ activationConstraint: { distance: 10 },+ });+ const dispatch = useDispatch();++ const {+ openedTabs,+ activeTransform,+ activeEditedTransform,+ activeTable,+ activeTab,+ setActiveTab,+ setActiveTable,+ setActiveTransform,+ removeOpenedTab,+ setOpenedTabs,+ addOpenedTransform,+ hasUnsavedChanges,+ unsavedTransforms,+ } = useWorkspace();++ const [isResizing, setIsResizing] = useState(false);+ const [isMergeModalOpen, setIsMergeModalOpen] = useState(false);++ const { tab, setTab, ref: tabsListRef } = useWorkspaceUiTabs();++ const workspaceId = Number(params.workspaceId);++ // Data fetching+ const {+ workspace,+ workspaceTransforms,+ availableTransforms,+ allTransforms,+ setupStatus,+ error,+ isLoading,+ isLoadingWorkspace,+ isLoadingWorkspaceTransforms,+ isArchived,+ isPending,+ } = useWorkspaceData({ workspaceId, unsavedTransforms });+ const databaseId = workspace?.database_id;++ // Workspace actions+ const {+ isMerging,+ runningTransforms,+ handleMergeWorkspace,+ handleWorkspaceNameChange,+ handleTableSelect,+ handleRunTransformAndShowPreview,+ handleTransformClick,+ handleNavigateToTransform,+ } = useWorkspaceActions({+ workspaceId,+ workspace,+ onOpenTab: setTab,+ workspaceTransforms,+ availableTransforms,+ });++ useEffect(() => {+ // Handle transformId URL param - initialize transform tab if redirected from transform page+ if (!transformId || isLoading) {+ return;+ }++ (async () => {+ const parsedId = parseInt(transformId, 10);+ await handleNavigateToTransform(isNaN(parsedId) ? transformId : parsedId);++ dispatch(replace(Urls.dataStudioWorkspace(workspaceId)));+ })();+ }, [+ transformId,+ isLoading,+ workspaceId,+ handleNavigateToTransform,+ dispatch,+ ]);++ const handleTabClose = useCallback(+ (event: React.MouseEvent, tab: WorkspaceTab, index: number) => {+ event.stopPropagation();++ const isActive =+ (tab.type === "transform" &&+ activeTransform &&+ getTransformId(activeTransform) === getTransformId(tab.transform)) ||+ (tab.type === "table" && activeTable?.tableId === tab.table.tableId);+ const remaining = openedTabs.filter((item) => item.id !== tab.id);++ removeOpenedTab(tab.id);++ if (!isActive) {+ return;+ }++ // Prefer previous tab, otherwise use next tab (which is now at the same index after filtering)+ const fallback = remaining[index - 1] ?? remaining[index] ?? undefined;++ if (fallback) {+ setActiveTab(fallback);+ setTab(fallback.id);+ } else {+ setActiveTab(null);+ setTab("setup");+ }+ },+ [+ activeTransform,+ activeTable,+ removeOpenedTab,+ setActiveTab,+ setTab,+ openedTabs,+ ],+ );++ const handleTabDragEnd = useCallback(+ (event: DragEndEvent) => {+ const activeId = event.active.id;+ const overId = event.over?.id;+ if (typeof activeId === "string" && typeof overId === "string") {+ const activeIndex = openedTabs.findIndex(({ id }) => id === activeId);+ const overIndex = openedTabs.findIndex(({ id }) => id === overId);+ const reorderedTabs = arrayMove(openedTabs, activeIndex, overIndex);+ setOpenedTabs(reorderedTabs);++ // Activate the dragged tab after reordering+ const draggedTab = reorderedTabs.find((tab) => tab.id === activeId);+ if (draggedTab) {+ setActiveTab(draggedTab);+ }+ }+ },+ [openedTabs, setOpenedTabs, setActiveTab],+ );++ const handleTabChange = useCallback(+ (newTab: string | null) => {+ if (newTab) {+ setTab(newTab);+ }+ },+ [setTab],+ );++ if (error || isLoadingWorkspace) {+ return (+ <Stack h="100%" justify="center">+ <LoadingAndErrorWrapper error={error} loading={isLoadingWorkspace} />+ </Stack>+ );+ }++ if (!workspace) {+ return <NotFound />;+ }++ return (+ <Stack+ bg="background-primary"+ className={classNames({+ [styles.resizing]: isResizing,+ })}+ data-testid="workspace-page"+ h="100%"+ gap={0}+ >+ <Group+ px="md"+ py="sm"+ style={{ borderBottom: "1px solid var(--mb-color-border)" }}+ justify="space-between"+ >+ <Flex gap="xs" align="center">+ <Icon name="git_branch" size="1rem" aria-hidden />+ <PaneHeaderInput+ initialValue={workspace.name}+ placeholder={t`Workspace name`}+ maxLength={NAME_MAX_LENGTH}+ onChange={handleWorkspaceNameChange}+ readOnly={isArchived}+ />+ </Flex>+ <Flex gap="sm">+ <RunWorkspaceMenu+ workspaceId={workspaceId}+ disabled={+ isArchived ||+ hasUnsavedChanges ||+ workspaceTransforms.length === 0+ }+ />+ <Button+ variant="filled"+ onClick={() => setIsMergeModalOpen(true)}+ loading={isMerging}+ disabled={+ isArchived ||+ isPending ||+ hasUnsavedChanges ||+ workspaceTransforms.length === 0+ }+ size="xs"+ >+ {t`Merge`}+ </Button>+ </Flex>+ </Group>++ <Group+ align="flex-start"+ gap={0}+ flex="1 1 auto"+ wrap="nowrap"+ style={{ overflow: "hidden" }}+ >+ <Box+ data-testid="workspace-content"+ h="100%"+ style={{+ flex: "1 1 auto",+ minWidth: 0,+ borderRight: "1px solid var(--mb-color-border)",+ }}+ pos="relative"+ >+ <Tabs+ defaultValue="setup"+ display="flex"+ h="100%"+ style={{ flexDirection: "column" }}+ value={tab}+ onChange={handleTabChange}+ >+ <Flex+ wrap="nowrap"+ flex="0 0 auto"+ px="md"+ style={{ borderBottom: "1px solid var(--mb-color-border)" }}+ >+ <DndContext+ onDragEnd={handleTabDragEnd}+ modifiers={[restrictToHorizontalAxis, restrictToParentElement]}+ sensors={[pointerSensor]}+ >+ <SortableContext+ items={openedTabs}+ strategy={horizontalListSortingStrategy}+ >+ <Tabs.List+ ref={tabsListRef}+ className={styles.tabsPanel}+ data-testid="workspace-tabs"+ >+ <Tabs.Tab value="setup" onClick={() => setActiveTab(null)}>+ <Group gap="xs" wrap="nowrap">+ <Icon name="database" aria-hidden />+ {t`Setup`}+ </Group>+ </Tabs.Tab>+ <Tabs.Tab value="graph" onClick={() => setActiveTab(null)}>+ <Group gap="xs" wrap="nowrap">+ <Icon name="dependencies" aria-hidden />+ {t`Graph`}+ </Group>+ </Tabs.Tab>++ {openedTabs.map((tab, index) => (+ <Sortable+ id={tab.id}+ as="div"+ key={tab.id}+ draggingStyle={{ opacity: 0.5 }}+ >+ <Tabs.Tab+ draggable+ key={tab.id}+ value={tab.id}+ onClick={() => {+ setActiveTab(tab);+ }}+ onMouseDown={(event) => {+ // close tab on middle-click (mouse wheel)+ if (event.button === 1) {+ handleTabClose(event, tab, index);+ }+ }}+ >+ <Group gap="xs" wrap="nowrap">+ <Icon+ name={+ tab.type === "transform"+ ? "pivot_table"+ : "table"+ }+ aria-hidden+ />+ {tab.name}+ <ActionIcon+ component="span"+ size="1rem"+ p="0"+ ml="xs"+ onClick={(event) =>+ handleTabClose(event, tab, index)+ }+ >+ <Icon name="close" size={10} aria-hidden />+ </ActionIcon>+ </Group>+ </Tabs.Tab>+ </Sortable>+ ))}+ </Tabs.List>+ </SortableContext>+ </DndContext>+ </Flex>++ <Box flex={1} mih={0}>+ <Tabs.Panel value="setup" h="100%" p="md">+ <SetupTab+ databaseId={databaseId}+ workspace={workspace}+ setupStatus={setupStatus}+ />+ </Tabs.Panel>+ <Tabs.Panel+ bg="background-secondary"+ value="graph"+ h="100%"+ p="md"+ >+ <GraphTab workspaceId={workspace?.id} />+ </Tabs.Panel>++ {activeTable && activeTab?.type === "table" && (+ <Tabs.Panel value={activeTab.id} h="100%">+ <DataTab+ workspaceId={workspace?.id}+ databaseId={workspace?.database_id ?? null}+ tableId={activeTable.tableId}+ transformId={activeTable.transformId}+ query={activeTable.query}+ pythonPreviewResult={activeTable.pythonPreviewResult}+ />+ </Tabs.Panel>+ )}++ {activeTransform && (+ <Tabs.Panel+ value={getTransformTabId(activeTransform)}+ h="100%"+ style={{ overflow: "auto" }}+ >+ {!activeEditedTransform ? (+ <Text c="text-secondary">+ {t`Select a transform on the right.`}+ </Text>+ ) : (+ <TransformTab+ databaseId={checkNotNull(workspace.database_id)}+ transform={activeTransform}+ workspaceId={workspaceId}+ workspaceTransforms={workspaceTransforms}+ isDisabled={isArchived || isPending}+ onSaveTransform={(transform) => {+ // After adding first transform to a workspace,+ // show 'Setup' tab with initialization status log.+ if (isWorkspaceUninitialized(workspace)) {+ setActiveTransform(transform);+ return setTab("setup");+ }+ setActiveTransform(transform);+ setTab(getTransformTabId(transform));+ }}+ />+ )}+ </Tabs.Panel>+ )}+ </Box>+ </Tabs>+ </Box>++ <ResizableBox+ axis="x"+ width={DEFAULT_SIDEBAR_WIDTH}+ height={Infinity}+ resizeHandles={["w"]}+ handle={<ResizeHandle />}+ minConstraints={[250, Infinity]}+ maxConstraints={[600, Infinity]}+ onResizeStart={() => setIsResizing(true)}+ onResizeStop={() => setIsResizing(false)}+ className={styles.sidebarResizableBox}+ >+ <Box data-testid="workspace-sidebar" h="100%" w="100%">+ <Tabs defaultValue="code">+ <Flex+ px="md"+ align="center"+ style={{ borderBottom: "1px solid var(--mb-color-border)" }}+ >+ <Tabs.List className={styles.tabsPanel}>+ <Tabs.Tab value="code">{t`Code`}</Tabs.Tab>+ <Tabs.Tab value="data">{t`Data`}</Tabs.Tab>+ </Tabs.List>+ {databaseId && (+ <AddTransformMenu+ databaseId={databaseId}+ disabled={isArchived}+ />+ )}+ </Flex>+ <Tabs.Panel value="code" p="md">+ <CodeTab+ readOnly={isArchived || isPending}+ activeTransformId={+ activeTransform+ ? getTransformId(activeTransform)+ : undefined+ }+ databaseId={databaseId}+ workspaceId={workspace.id}+ workspaceTransforms={allTransforms}+ isLoadingWorkspaceTransforms={isLoadingWorkspaceTransforms}+ onTransformClick={(transform) => {+ addOpenedTransform(transform);+ setTab(getTransformTabId(transform));+ if (activeTable) {+ setActiveTable(null);+ }+ }}+ />+ </Tabs.Panel>+ <Tabs.Panel value="data" p="md">+ <DataTabSidebar+ readOnly={isArchived || isPending}+ workspaceTransforms={workspaceTransforms}+ databaseId={databaseId}+ selectedTableId={activeTable?.tableId}+ runningTransforms={runningTransforms}+ workspaceId={workspace.id}+ onTransformClick={handleTransformClick}+ onTableSelect={handleTableSelect}+ onRunTransform={handleRunTransformAndShowPreview}+ />+ </Tabs.Panel>+ </Tabs>+ </Box>+ </ResizableBox>+ </Group>++ {isMergeModalOpen && (+ <MergeWorkspaceModal+ onClose={() => setIsMergeModalOpen(false)}+ onSubmit={handleMergeWorkspace}+ isLoading={isMerging}+ isDisabled={isPending}+ workspaceId={workspaceId}+ workspaceName={workspace?.name ?? ""}+ workspaceTransforms={workspaceTransforms}+ />+ )}++ <LeaveRouteConfirmModal isEnabled={hasUnsavedChanges} route={route} />+ </Stack>+ );+}++export const WorkspacePage = ({ params, route }: WorkspacePageProps) => {+ const workspaceId = Number(params.workspaceId);+ const { search } = useLocation();+ const transformId = new URLSearchParams(search).get("transformId");++ return (+ <WorkspaceProvider workspaceId={workspaceId}>+ <WorkspacePageContent+ key={workspaceId}+ params={params}+ route={route}+ transformId={transformId ?? undefined}+ />+ </WorkspaceProvider>+ );+};++function useWorkspaceUiTabs() {+ const { activeTab } = useWorkspace();+ const tabsListRef = useRef<HTMLDivElement>(null);+ const [tab, setTab] = useState<string>("setup");++ useEffect(() => {+ // Sync UI tabs with active tab changes from workspace.+ if (activeTab) {+ setTab(activeTab.id);+ } else {+ setTab((tab) => (["setup", "graph"].includes(tab) ? tab : "setup"));+ }+ }, [activeTab, setTab]);++ useEffect(() => {+ // Scroll to active tab on change.+ if (tabsListRef.current && tab) {+ const activeTabElement =+ tabsListRef.current.querySelector(`[data-active="true"]`);++ if (activeTabElement) {+ activeTabElement.scrollIntoView({+ behavior: "smooth",+ block: "nearest",+ });+ }+ }+ }, [tab]);++ return { tab, setTab, ref: tabsListRef };+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/WorkspaceProvider.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/WorkspaceProvider.tsxnew file mode 100644index 00000000000..b163eabb7fd--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/WorkspaceProvider.tsx@@ -0,0 +1,752 @@+import type { ReactNode } from "react";+import {+ createContext,+ useCallback,+ useContext,+ useMemo,+ useState,+} from "react";++import { checkNotNull } from "metabase/lib/types";+import { isSameSource } from "metabase-enterprise/transforms/utils";+import type {+ DatasetQuery,+ DraftTransformSource,+ TableId,+ TaggedTransform,+ TransformTargetType,+ UnsavedTransform,+ WorkspaceTransform,+} from "metabase-types/api";+import { isUnsavedTransform, isWorkspaceTransform } from "metabase-types/api";++export interface OpenTable {+ tableId: TableId | null;+ name: string;+ schema?: string | null;+ transformId?: string;+ query?: DatasetQuery;+ pythonPreviewResult?: any;+}++export interface EditedTransform {+ name: string;+ source: DraftTransformSource;+ target: {+ name: string;+ schema: string | null;+ type: TransformTargetType;+ };+}++/** Union type for all transform variants used in workspace */+export type AnyWorkspaceTransform =+ | TaggedTransform+ | WorkspaceTransform+ | UnsavedTransform;++export interface Tab {+ id: string;+ name: string;+ type: "transform" | "table";+}++export interface TransformTab extends Tab {+ type: "transform";+ transform: AnyWorkspaceTransform;+}++export interface TableTab extends Tab {+ type: "table";+ table: OpenTable;+}++export type WorkspaceTab = TransformTab | TableTab;++export interface WorkspaceContextValue {+ workspaceId: number;+ openedTabs: WorkspaceTab[];+ activeTab: WorkspaceTab | null;+ activeTable: OpenTable | null;+ activeTransform: AnyWorkspaceTransform | null;+ activeEditedTransform?: EditedTransform | null;+ setActiveTab: (tab: WorkspaceTab | null) => void;+ setActiveTransform: (transform: AnyWorkspaceTransform) => void;+ setActiveTable: (table: OpenTable | null) => void;+ addOpenedTab: (tab: WorkspaceTab, activate?: boolean) => void;+ removeOpenedTab: (tabId: string) => void;+ setOpenedTabs: (tabs: WorkspaceTab[]) => void;+ addOpenedTransform: (+ transform: TaggedTransform | WorkspaceTransform | UnsavedTransform,+ ) => void;+ removeWorkspaceTransform: (transformId: string | number) => void;+ editedTransforms: Map<number | string, EditedTransform>;+ patchEditedTransform: (+ transformId: number | string,+ patch: Partial<EditedTransform>,+ ) => void;+ removeEditedTransform: (transformId: string | number) => void;+ updateTransformState: (transform: WorkspaceTransform) => void;+ updateTab: <T extends WorkspaceTab>(tabId: string, patch: Partial<T>) => void;+ hasUnsavedChanges: boolean;+ hasTransformEdits: (originalTransform: AnyWorkspaceTransform) => boolean;+ unsavedTransforms: UnsavedTransform[];+ addUnsavedTransform: (source: DraftTransformSource) => void;+ removeUnsavedTransform: (transformId: number) => void;+}++const WorkspaceContext = createContext<WorkspaceContextValue | undefined>(+ undefined,+);++interface WorkspaceState {+ openedTabs: WorkspaceTab[];+ activeTab: WorkspaceTab | null;+ activeTable: OpenTable | null;+ activeTransform: AnyWorkspaceTransform | null;+ activeEditedTransform?: AnyWorkspaceTransform | null;+ editedTransforms: Map<number | string, EditedTransform>;+ unsavedTransforms: UnsavedTransform[];+ nextUnsavedTransformIndex: number;+}++interface WorkspaceProviderProps {+ children: ReactNode;+ workspaceId: number;+}++/** Get the unique identifier used for tabs and transform lookups */+export function getTransformId(+ transform: AnyWorkspaceTransform,+): string | number {+ if (isWorkspaceTransform(transform)) {+ return transform.ref_id;+ }+ return transform.id;+}++/** Get the tab ID for a transform */+export function getTransformTabId(transform: AnyWorkspaceTransform): string {+ return `transform-${getTransformId(transform)}`;+}++/** Get the numeric transform ID (for metabot, RunButton, etc.) */+export function getNumericTransformId(+ transform: AnyWorkspaceTransform,+): number | undefined {+ if (isWorkspaceTransform(transform)) {+ return transform.global_id ?? undefined;+ }+ return transform.id;+}++const createEmptyWorkspaceState = (): WorkspaceState => ({+ openedTabs: [],+ activeTransform: null,+ activeTable: null,+ activeTab: null,+ editedTransforms: new Map<number | string, EditedTransform>(),+ unsavedTransforms: [],+ nextUnsavedTransformIndex: 0,+});++export const WorkspaceProvider = ({+ children,+ workspaceId,+}: WorkspaceProviderProps) => {+ const [workspaceStates, setWorkspaceStates] = useState<+ Map<number, WorkspaceState>+ >(new Map());+ const currentState = useMemo(() => {+ const existing = workspaceStates.get(workspaceId);+ if (existing) {+ return existing;+ }+ const newState = createEmptyWorkspaceState();+ setWorkspaceStates((prev) => new Map(prev).set(workspaceId, newState));+ return newState;+ }, [workspaceId, workspaceStates]);++ const { openedTabs, activeTransform, activeTable, activeTab } = currentState;++ const updateWorkspaceState = useCallback(+ (updater: (state: WorkspaceState) => WorkspaceState) => {+ setWorkspaceStates((prev) => {+ const currentState =+ prev.get(workspaceId) ?? createEmptyWorkspaceState();+ const newState = updater(currentState);+ return new Map(prev).set(workspaceId, newState);+ });+ },+ [workspaceId],+ );++ const setActiveTab = useCallback(+ (tab: WorkspaceTab | null) => {+ updateWorkspaceState((state) => {+ const newActiveTab = tab;+ let newActiveTransform = state.activeTransform;+ let newActiveTable = state.activeTable;++ if (tab?.type === "transform") {+ newActiveTransform = tab.transform;+ newActiveTable = null;+ } else if (tab?.type === "table") {+ newActiveTable = tab.table;+ newActiveTransform = null;+ } else {+ newActiveTransform = null;+ newActiveTable = null;+ }++ return {+ ...state,+ activeTab: newActiveTab,+ activeTransform: newActiveTransform,+ activeTable: newActiveTable,+ };+ });+ },+ [updateWorkspaceState],+ );++ const addOpenedTab = useCallback(+ (tab: WorkspaceTab) => {+ updateWorkspaceState((state) => {+ const exists = state.openedTabs.some((item) => item.id === tab.id);+ if (exists) {+ return {+ ...state,+ openedTabs: state.openedTabs.map((item) =>+ item.id === tab.id ? tab : item,+ ),+ activeTab: tab,+ activeTransform:+ tab.type === "transform" ? tab.transform : state.activeTransform,+ activeTable: tab.type === "table" ? tab.table : state.activeTable,+ };+ }++ const newOpenedTabs = [...state.openedTabs, tab];+ return {+ ...state,+ openedTabs: newOpenedTabs,+ activeTab: tab,+ activeTransform:+ tab.type === "transform" ? tab.transform : state.activeTransform,+ activeTable: tab.type === "table" ? tab.table : state.activeTable,+ };+ });+ },+ [updateWorkspaceState],+ );++ const removeOpenedTab = useCallback(+ (tabId: string) => {+ updateWorkspaceState((state) => {+ const filteredTabs = state.openedTabs.filter(+ (item) => item.id !== tabId,+ );++ const removedTab = state.openedTabs.find((item) => item.id === tabId);+ const wasActive = state.activeTab?.id === tabId;++ let newActiveTab = state.activeTab;+ let newActiveTransform = state.activeTransform;+ let newActiveTable = state.activeTable;++ if (wasActive && removedTab) {+ const currentIndex = state.openedTabs.findIndex(+ (item) => item.id === tabId,+ );++ const preferPrevious = currentIndex > 0;+ const fallbackTab = preferPrevious+ ? state.openedTabs[currentIndex - 1]+ : filteredTabs[0];++ if (fallbackTab) {+ newActiveTab = fallbackTab;+ newActiveTransform =+ fallbackTab.type === "transform" ? fallbackTab.transform : null;+ newActiveTable =+ fallbackTab.type === "table" ? fallbackTab.table : null;+ } else {+ newActiveTab = null;+ newActiveTransform = null;+ newActiveTable = null;+ }+ }++ return {+ ...state,+ openedTabs: filteredTabs,+ activeTab: newActiveTab,+ activeTransform: newActiveTransform,+ activeTable: newActiveTable,+ };+ });+ },+ [updateWorkspaceState],+ );++ const setOpenedTabs = useCallback(+ (tabs: WorkspaceTab[]) => {+ updateWorkspaceState((state) => ({+ ...state,+ openedTabs: tabs,+ }));+ },+ [updateWorkspaceState],+ );++ const setActiveTransform = useCallback(+ (transform: AnyWorkspaceTransform) => {+ updateWorkspaceState((state) => {+ const transformTabId = getTransformTabId(transform);+ const existingTab = state.openedTabs.find(+ (tab) => tab.type === "transform" && tab.id === transformTabId,+ );++ if (existingTab) {+ return {+ ...state,+ activeTab: existingTab,+ activeTransform: transform,+ activeTable: null,+ };+ } else {+ const newTransformTab: TransformTab = {+ id: transformTabId,+ name: transform.name,+ type: "transform",+ transform,+ };++ return {+ ...state,+ openedTabs: [...state.openedTabs, newTransformTab],+ activeTab: newTransformTab,+ activeTransform: transform,+ activeTable: null,+ };+ }+ });+ },+ [updateWorkspaceState],+ );++ const addOpenedTransform = useCallback(+ (transform: TaggedTransform | WorkspaceTransform | UnsavedTransform) => {+ const transformTab: TransformTab = {+ id: getTransformTabId(transform),+ name: transform.name,+ type: "transform",+ transform,+ };+ addOpenedTab(transformTab);+ },+ [addOpenedTab],+ );++ const removeWorkspaceTransform = useCallback(+ (transformId: string | number) => {+ removeOpenedTab(`transform-${transformId}`);+ for (const tab of openedTabs) {+ if (tab.type === "table" && tab.table.transformId === transformId) {+ removeOpenedTab(tab.id);+ }+ }+ },+ [removeOpenedTab, openedTabs],+ );++ const patchEditedTransform = useCallback(+ (transformId: number | string, patch: Partial<EditedTransform>) => {+ updateWorkspaceState((state) => {+ const activeTransform = checkNotNull(state.activeTransform);+ const currentTransform =+ state.editedTransforms.get(transformId) ?? activeTransform;+ const newEditedTransform: EditedTransform = {+ name: patch.name ? patch.name : currentTransform.name,+ source: patch.source ? patch.source : currentTransform.source,+ target: patch.target ? patch.target : currentTransform.target,+ };++ const hasChanges =+ !isSameSource(newEditedTransform.source, activeTransform.source) ||+ newEditedTransform.name !== activeTransform.name ||+ newEditedTransform.target.name !== activeTransform.target.name;++ const newEditedTransforms = new Map(state.editedTransforms);++ if (hasChanges) {+ newEditedTransforms.set(transformId, newEditedTransform);+ } else {+ newEditedTransforms.delete(transformId);+ }++ const newOpenedTabs = state.openedTabs.map((tab) => {+ if (+ tab.type === "transform" &&+ getTransformId(tab.transform) === transformId+ ) {+ return {+ ...tab,+ name: newEditedTransform.name,+ };+ }+ return tab;+ });++ return {+ ...state,+ openedTabs: newOpenedTabs,+ editedTransforms: newEditedTransforms,+ };+ });+ },+ [updateWorkspaceState],+ );++ const removeEditedTransform = useCallback(+ (transformId: string | number) => {+ updateWorkspaceState((state) => {+ const newEditedTransforms = new Map(state.editedTransforms);+ newEditedTransforms.delete(transformId);+ return {+ ...state,+ editedTransforms: newEditedTransforms,+ };+ });+ },+ [updateWorkspaceState],+ );++ const updateTransformState = useCallback(+ (transform: WorkspaceTransform) => {+ updateWorkspaceState((state) => {+ const transformId = transform.ref_id;+ const newOpenedTabs = state.openedTabs.map((tab) => {+ if (+ tab.type === "transform" &&+ getTransformId(tab.transform) === transformId+ ) {+ return {+ ...tab,+ transform,+ name: transform.name,+ };+ }+ return tab;+ });++ const newEditedTransforms = new Map(state.editedTransforms);+ const currentEdit = state.editedTransforms.get(transformId);++ if (currentEdit) {+ const hasNameChanged = currentEdit.name !== transform.name;+ const hasSourceChanged = !isSameSource(+ currentEdit.source,+ transform.source,+ );++ if (hasNameChanged || hasSourceChanged) {+ newEditedTransforms.set(transformId, {+ ...currentEdit,+ target: transform.target,+ });+ } else {+ newEditedTransforms.delete(transformId);+ }+ }++ const activeTransformId = state.activeTransform+ ? getTransformId(state.activeTransform)+ : undefined;++ return {+ ...state,+ openedTabs: newOpenedTabs,+ activeTransform:+ activeTransformId === transformId+ ? transform+ : state.activeTransform,+ editedTransforms: newEditedTransforms,+ };+ });+ },+ [updateWorkspaceState],+ );++ const updateTab = useCallback(+ <T extends WorkspaceTab>(tabId: string, patch: Partial<T>) => {+ updateWorkspaceState((state) => {+ const newOpenedTabs = state.openedTabs.map((tab): WorkspaceTab => {+ if (tab.id === tabId) {+ return { ...tab, ...patch };+ }+ return tab;+ });++ // Also update activeTab if it's the tab being updated+ const newActiveTab: WorkspaceTab | null =+ state.activeTab?.id === tabId+ ? { ...state.activeTab, ...patch }+ : state.activeTab;++ return {+ ...state,+ openedTabs: newOpenedTabs,+ activeTab: newActiveTab,+ };+ });+ },+ [updateWorkspaceState],+ );++ const hasUnsavedChanges =+ currentState.editedTransforms.size > 0 ||+ currentState.unsavedTransforms.length > 0;++ const hasTransformEdits = useCallback(+ (originalTransform: AnyWorkspaceTransform) => {+ // Unsaved transforms always have changes+ if (isUnsavedTransform(originalTransform)) {+ return true;+ }++ const transformId = getTransformId(originalTransform);+ const edited = currentState.editedTransforms.get(transformId);++ // We don't store workspace transforms sources in the provider, so we can't compare+ // changes. So we just mark all "dirty" workspace transforms as edited.+ if (isWorkspaceTransform(originalTransform) && edited) {+ return true;+ }++ return (+ edited != null &&+ (!isSameSource(edited.source, originalTransform.source) ||+ edited.name !== originalTransform.name ||+ edited.target.name !== originalTransform.target.name)+ );+ },+ [currentState.editedTransforms],+ );++ const addUnsavedTransform = useCallback(+ (source: DraftTransformSource) => {+ updateWorkspaceState((state) => {+ const currentIndex = state.nextUnsavedTransformIndex;+ const name =+ currentIndex === 0+ ? "New transform"+ : `New transform (${currentIndex})`;++ const newTransform: UnsavedTransform = {+ type: "unsaved-transform",+ id: -1 - currentIndex, // Use negative IDs to avoid collision with existing transforms+ name,+ source,+ target: {+ name: "",+ schema: null,+ type: "table",+ },+ };++ // Add edited transform to mark it as having changes+ const newEditedTransforms = new Map(state.editedTransforms);+ newEditedTransforms.set(newTransform.id, {+ name: newTransform.name,+ source: newTransform.source,+ target: newTransform.target,+ });++ // Create and add the new transform tab+ const newTransformTab: TransformTab = {+ id: getTransformTabId(newTransform),+ name: newTransform.name,+ type: "transform",+ transform: newTransform,+ };++ return {+ ...state,+ unsavedTransforms: [...state.unsavedTransforms, newTransform],+ nextUnsavedTransformIndex: currentIndex + 1,+ editedTransforms: newEditedTransforms,+ openedTabs: [...state.openedTabs, newTransformTab],+ activeTab: newTransformTab,+ activeTransform: newTransform,+ activeTable: null,+ };+ });+ },+ [updateWorkspaceState],+ );++ const removeUnsavedTransform = useCallback(+ (transformId: number) => {+ updateWorkspaceState((state) => {+ const newUnsavedTransforms = state.unsavedTransforms.filter(+ (transform) => transform.id !== transformId,+ );++ const newEditedTransforms = new Map(state.editedTransforms);+ newEditedTransforms.delete(transformId);++ // Remove from opened tabs if present+ const tabId = `transform-${transformId}`;+ const newOpenedTabs = state.openedTabs.filter(+ (tab) => tab.id !== tabId,+ );++ // Clear active state if this was the active transform+ const activeTransformId = state.activeTransform+ ? getTransformId(state.activeTransform)+ : undefined;+ const newActiveTransform =+ activeTransformId === transformId ? null : state.activeTransform;+ const newActiveTab =+ state.activeTab?.id === tabId ? null : state.activeTab;++ return {+ ...state,+ unsavedTransforms: newUnsavedTransforms,+ editedTransforms: newEditedTransforms,+ openedTabs: newOpenedTabs,+ activeTransform: newActiveTransform,+ activeTab: newActiveTab,+ };+ });+ },+ [updateWorkspaceState],+ );++ const setActiveTable = useCallback(+ (table: OpenTable | null) => {+ updateWorkspaceState((state) => {+ if (table) {+ const existingTab = state.openedTabs.find(+ (tab) =>+ tab.type === "table" && tab.table.tableId === table.tableId,+ );++ if (existingTab) {+ return {+ ...state,+ activeTab: existingTab,+ activeTable: table,+ activeTransform: null,+ };+ } else {+ const newTableTab: TableTab = {+ id: `table-${table.tableId}`,+ name: table.schema ? `${table.schema}.${table.name}` : table.name,+ type: "table",+ table,+ };++ return {+ ...state,+ openedTabs: [...state.openedTabs, newTableTab],+ activeTab: newTableTab,+ activeTable: table,+ activeTransform: null,+ };+ }+ } else {+ return {+ ...state,+ activeTab:+ state.activeTab?.type === "table" ? null : state.activeTab,+ activeTable: null,+ };+ }+ });+ },+ [updateWorkspaceState],+ );++ const activeEditedTransform = activeTransform+ ? (currentState.editedTransforms.get(getTransformId(activeTransform)) ??+ activeTransform)+ : activeTransform;++ const value = useMemo(+ () => ({+ workspaceId,+ openedTabs,+ activeTransform,+ activeTable,+ activeTab,+ activeEditedTransform,+ setActiveTab,+ setActiveTransform,+ setActiveTable,+ addOpenedTab,+ removeOpenedTab,+ removeWorkspaceTransform,+ setOpenedTabs,+ addOpenedTransform,+ editedTransforms: currentState.editedTransforms,+ patchEditedTransform,+ removeEditedTransform,+ updateTransformState,+ updateTab,+ hasUnsavedChanges,+ hasTransformEdits,+ unsavedTransforms: currentState.unsavedTransforms,+ addUnsavedTransform,+ removeUnsavedTransform,+ }),+ [+ workspaceId,+ openedTabs,+ activeTransform,+ activeTable,+ activeTab,+ activeEditedTransform,+ setActiveTab,+ setActiveTransform,+ setActiveTable,+ addOpenedTab,+ removeOpenedTab,+ setOpenedTabs,+ addOpenedTransform,+ removeWorkspaceTransform,+ currentState.editedTransforms,+ currentState.unsavedTransforms,+ patchEditedTransform,+ removeEditedTransform,+ updateTransformState,+ updateTab,+ hasUnsavedChanges,+ hasTransformEdits,+ addUnsavedTransform,+ removeUnsavedTransform,+ ],+ );++ return (+ <WorkspaceContext.Provider value={value}>+ {children}+ </WorkspaceContext.Provider>+ );+};++export const useWorkspace = (): WorkspaceContextValue => {+ const context = useContext(WorkspaceContext);++ if (!context) {+ throw new Error("useWorkspace must be used within a WorkspaceProvider");+ }++ return context;+};++export const useOptionalWorkspace = (): WorkspaceContextValue | undefined => {+ return useContext(WorkspaceContext);+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/components/StatusDot/StatusDot.module.css b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/components/StatusDot/StatusDot.module.cssnew file mode 100644index 00000000000..6755084e41d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/components/StatusDot/StatusDot.module.css@@ -0,0 +1,7 @@+.statusDot {+ width: 0.5rem;+ height: 0.5rem;+ border-radius: 50%;+ background-color: var(--mb-color-warning);+ flex-shrink: 0;+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/components/StatusDot/StatusDot.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/components/StatusDot/StatusDot.tsxnew file mode 100644index 00000000000..5c8ec642f7b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/components/StatusDot/StatusDot.tsx@@ -0,0 +1,11 @@+import { Box } from "metabase/ui";++import S from "./StatusDot.module.css";++export const StatusDot = ({+ "data-testid": dataTestId,+}: {+ "data-testid"?: string;+}) => {+ return <Box className={S.statusDot} data-testid={dataTestId} />;+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/useWorkspaceActions.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/useWorkspaceActions.tsnew file mode 100644index 00000000000..e76c0d74373--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/useWorkspaceActions.ts@@ -0,0 +1,257 @@+import { useCallback, useState } from "react";+import { replace } from "react-router-redux";+import { t } from "ttag";++import { useDispatch } from "metabase/lib/redux";+import * as Urls from "metabase/lib/urls";+import { useMetadataToasts } from "metabase/metadata/hooks";+import {+ useLazyGetTransformQuery,+ useLazyGetWorkspaceTablesQuery,+ useLazyGetWorkspaceTransformQuery,+ useMergeWorkspaceMutation,+ useRunWorkspaceTransformMutation,+ useUpdateWorkspaceMutation,+} from "metabase-enterprise/api";+import type {+ ExternalTransform,+ TaggedTransform,+ Workspace,+ WorkspaceTransformListItem,+} from "metabase-types/api";++import {+ type OpenTable,+ type WorkspaceTab,+ useWorkspace,+} from "./WorkspaceProvider";++type UseWorkspaceActionsParams = {+ workspaceId: number;+ workspace: Workspace | undefined;+ onOpenTab: (tabId: string) => void;+ workspaceTransforms: WorkspaceTransformListItem[];+ availableTransforms: ExternalTransform[];+};++export function useWorkspaceActions({+ workspaceId,+ workspace,+ onOpenTab,+ workspaceTransforms,+ availableTransforms,+}: UseWorkspaceActionsParams) {+ const dispatch = useDispatch();+ const { sendErrorToast, sendSuccessToast } = useMetadataToasts();++ const { addOpenedTab, addOpenedTransform, setActiveTransform } =+ useWorkspace();+ const [mergeWorkspace, { isLoading: isMerging }] =+ useMergeWorkspaceMutation();+ const [updateWorkspace] = useUpdateWorkspaceMutation();+ const [runTransform] = useRunWorkspaceTransformMutation();+ const [fetchWorkspaceTransform] = useLazyGetWorkspaceTransformQuery();+ const [fetchTransform] = useLazyGetTransformQuery();+ const [runningTransforms, setRunningTransforms] = useState<Set<string>>(+ new Set(),+ );+ const [getWorkspaceTables] = useLazyGetWorkspaceTablesQuery();++ const handleMergeWorkspace = useCallback(+ async (commitMessage: string) => {+ try {+ const response = await mergeWorkspace({+ id: workspaceId,+ commit_message: commitMessage,+ }).unwrap();++ if (response.errors && response.errors.length > 0) {+ sendErrorToast(+ t`Failed to merge workspace: ${response.errors.map((e: any) => e.error).join(", ")}`,+ );+ return;+ }+ dispatch(replace(Urls.transformList()));+ sendSuccessToast(+ t`Workspace '${response.workspace.name}' merged successfully`,+ );+ } catch (error) {+ sendErrorToast(t`Failed to merge workspace`);+ throw error;+ }+ },+ [workspaceId, mergeWorkspace, sendErrorToast, dispatch, sendSuccessToast],+ );++ const handleWorkspaceNameChange = useCallback(+ async (newName: string) => {+ if (!workspace || newName.trim() === workspace.name.trim()) {+ return;+ }++ try {+ await updateWorkspace({+ id: workspaceId,+ name: newName.trim(),+ }).unwrap();+ sendSuccessToast(t`Workspace renamed successfully`);+ } catch (error) {+ sendErrorToast(t`Failed to update workspace name`);+ }+ },+ [workspace, workspaceId, updateWorkspace, sendErrorToast, sendSuccessToast],+ );++ const handleTableSelect = useCallback(+ (table: OpenTable) => {+ const tableTab: WorkspaceTab = {+ id: `table-${table.tableId}`,+ name: table.schema ? `${table.schema}.${table.name}` : table.name,+ type: "table",+ table,+ };+ addOpenedTab(tableTab);+ onOpenTab(tableTab.id);+ },+ [addOpenedTab, onOpenTab],+ );++ const handleRunTransformAndShowPreview = useCallback(+ async (transform: WorkspaceTransformListItem) => {+ setRunningTransforms((prev) => new Set(prev).add(transform.ref_id));++ try {+ const result = await runTransform({+ workspaceId,+ transformId: transform.ref_id,+ }).unwrap();++ if (result.status === "failed") {+ sendErrorToast(t`Transform run failed`);+ return;+ }++ const { data: updatedTables, error } =+ await getWorkspaceTables(workspaceId);+ const updatedOutput = updatedTables?.outputs.find(+ (t) => t.isolated.transform_id === transform.ref_id,+ );++ if (error) {+ sendErrorToast(t`Failed to fetch workspace tables`);+ } else if (updatedOutput?.isolated.table_id) {+ handleTableSelect({+ tableId: updatedOutput.isolated.table_id,+ name: updatedOutput.global.table,+ schema: updatedOutput.global.schema,+ transformId: transform.ref_id,+ });+ }+ } catch (error) {+ sendErrorToast(t`Failed to run transform`);+ } finally {+ setRunningTransforms((prev) => {+ const next = new Set(prev);+ next.delete(transform.ref_id);+ return next;+ });+ }+ },+ [+ workspaceId,+ runTransform,+ getWorkspaceTables,+ handleTableSelect,+ sendErrorToast,+ ],+ );++ const handleTransformClick = useCallback(+ async (workspaceTransform: WorkspaceTransformListItem) => {+ const { data: transform, error } = await fetchWorkspaceTransform(+ {+ workspaceId,+ transformId: workspaceTransform.ref_id,+ },+ true,+ );+ if (error) {+ sendErrorToast(t`Failed to fetch transform`);+ } else if (transform) {+ addOpenedTransform(transform);+ }+ },+ [workspaceId, fetchWorkspaceTransform, addOpenedTransform, sendErrorToast],+ );++ // Callback to navigate to a transform (used by metabot reactions and URL param)+ const handleNavigateToTransform = useCallback(+ async (targetTransformId: number | string) => {+ const transform = [...workspaceTransforms, ...availableTransforms].find(+ (transform) => {+ if ("global_id" in transform) {+ return (+ transform.global_id === targetTransformId ||+ transform.ref_id === targetTransformId+ );+ }+ return transform.id === targetTransformId;+ },+ );++ const isWsTransform = !!transform && "global_id" in transform;++ if (transform && !isWsTransform) {+ const { data, error } = await fetchTransform(transform.id, true);++ if (error) {+ sendErrorToast(t`Failed to fetch transform`);+ } else if (data) {+ const taggedTransform: TaggedTransform = {+ ...data,+ type: "transform",+ };+ addOpenedTransform(taggedTransform);+ setActiveTransform(taggedTransform);+ onOpenTab(String(targetTransformId));+ }+ } else if (transform && isWsTransform) {+ const { data, error } = await fetchWorkspaceTransform({+ workspaceId,+ transformId: transform.ref_id,+ });+ if (error) {+ sendErrorToast(t`Failed to fetch transform`);+ } else if (data) {+ addOpenedTransform(data);+ setActiveTransform(data);+ onOpenTab(String(targetTransformId));+ }+ } else {+ sendErrorToast(`Transform ${targetTransformId} not found`);+ }+ },+ [+ workspaceTransforms,+ availableTransforms,+ workspaceId,+ fetchTransform,+ fetchWorkspaceTransform,+ addOpenedTransform,+ setActiveTransform,+ onOpenTab,+ sendErrorToast,+ ],+ );++ return {+ isMerging,+ runningTransforms,+ handleMergeWorkspace,+ handleWorkspaceNameChange,+ handleTableSelect,+ handleRunTransformAndShowPreview,+ handleTransformClick,+ handleNavigateToTransform,+ };+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/useWorkspaceData.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/useWorkspaceData.tsnew file mode 100644index 00000000000..3bc428d6668--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/useWorkspaceData.ts@@ -0,0 +1,108 @@+import { useMemo } from "react";++import { skipToken } from "metabase/api";+import {+ useGetExternalTransformsQuery,+ useGetWorkspaceLogQuery,+ useGetWorkspaceQuery,+ useGetWorkspaceTransformsQuery,+} from "metabase-enterprise/api";+import type {+ UnsavedTransform,+ WorkspaceId,+ WorkspaceTransformListItem,+} from "metabase-types/api";++type UseWorkspaceDataParams = {+ workspaceId: number;+ unsavedTransforms: UnsavedTransform[];+};++export type SetupStatus = ReturnType<typeof useWorkspaceSetupStatus>;++const LOGS_POLLING_INTERVAL_LONG = 5000;+const LOGS_POLLING_INTERVAL_SHORT = 1000;++export function useWorkspaceData({+ workspaceId,+ unsavedTransforms,+}: UseWorkspaceDataParams) {+ const {+ data: workspace,+ error: workspaceError,+ isLoading: isLoadingWorkspace,+ } = useGetWorkspaceQuery(workspaceId);+ const {+ data: workspaceTransforms = [],+ error: workspaceTransformsError,+ isLoading: isLoadingWorkspaceTransforms,+ } = useGetWorkspaceTransformsQuery(workspaceId);+ const { data: externalTransforms, isLoading: isLoadingExternalTransforms } =+ useGetExternalTransformsQuery(+ workspace?.database_id+ ? { workspaceId, databaseId: workspace?.database_id }+ : skipToken,+ );++ const setupStatus = useWorkspaceSetupStatus({+ pollingInterval:+ workspace?.status === "pending"+ ? LOGS_POLLING_INTERVAL_SHORT+ : LOGS_POLLING_INTERVAL_LONG,+ workspaceId,+ });++ const availableTransforms = useMemo(+ () => externalTransforms ?? [],+ [externalTransforms],+ );++ const isLoading =+ isLoadingWorkspace ||+ isLoadingWorkspaceTransforms ||+ isLoadingExternalTransforms;+ const error = workspaceError || workspaceTransformsError;++ const allTransforms: (UnsavedTransform | WorkspaceTransformListItem)[] =+ useMemo(+ () => [...unsavedTransforms, ...workspaceTransforms],+ [unsavedTransforms, workspaceTransforms],+ );++ const isArchived = workspace?.status === "archived";++ return {+ workspace,+ workspaceTransforms,+ availableTransforms,+ allTransforms,+ setupStatus,+ error,+ isLoading,+ isLoadingWorkspace,+ isLoadingWorkspaceTransforms,+ isArchived,+ isPending: setupStatus.workspace?.status === "pending",+ };+}++export function useWorkspaceSetupStatus({+ pollingInterval,+ workspaceId,+}: {+ pollingInterval: number;+ workspaceId: WorkspaceId;+}) {+ const {+ data: workspace,+ error,+ isLoading,+ } = useGetWorkspaceLogQuery(workspaceId, {+ pollingInterval,+ refetchOnMountOrArgChange: true,+ });++ const logs = useMemo(() => workspace?.logs ?? [], [workspace]);++ return { workspace, error, isLoading, logs };+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/useWorkspaceMetabot.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/useWorkspaceMetabot.tsnew file mode 100644index 00000000000..0847eff87af--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/useWorkspaceMetabot.ts@@ -0,0 +1,416 @@+import { useCallback, useEffect, useMemo, useRef, useState } from "react";+import { push } from "react-router-redux";+import { t } from "ttag";++import { useDispatch } from "metabase/lib/redux";+import * as Urls from "metabase/lib/urls";+import { useRegisterMetabotContextProvider } from "metabase/metabot";+import { PLUGIN_METABOT } from "metabase/plugins";+import { getIsWorkspace } from "metabase/selectors/routing";+import { useLazyGetTransformQuery } from "metabase-enterprise/api";+import { METABOT_PROFILE_OVERRIDES } from "metabase-enterprise/metabot/constants";+import type {+ ApplySuggestionPayload,+ ApplySuggestionResult,+ MetabotSuggestionActions,+} from "metabase-enterprise/metabot/context";+import { useMetabotAgent } from "metabase-enterprise/metabot/hooks/use-metabot-agent";+import { useMetabotReactions } from "metabase-enterprise/metabot/hooks/use-metabot-reactions";+import type {+ MetabotConverstationState,+ MetabotState,+ MetabotSuggestedTransform,+} from "metabase-enterprise/metabot/state";+import {+ activateSuggestedTransform,+ updateSuggestedTransformId,+} from "metabase-enterprise/metabot/state/actions";+import { metabotActions } from "metabase-enterprise/metabot/state/reducer";+import { getMetabotState } from "metabase-enterprise/metabot/state/selectors";+import { useEnterpriseSelector } from "metabase-enterprise/redux";+import type {+ DatabaseId,+ DraftTransformSource,+ TaggedTransform,+ Transform,+ UnsavedTransform,+ WorkspaceTransformListItem,+} from "metabase-types/api";+import { isUnsavedTransform } from "metabase-types/api";++import {+ type AnyWorkspaceTransform,+ getTransformId,+ getTransformTabId,+ useWorkspace,+} from "./WorkspaceProvider";+import { isSavedTransformInfo } from "./utils/guards";+import {+ getDatabaseIdFromSource,+ setDatabaseIdOnSource,+} from "./utils/transform-source";++type MetabotConversationSnapshot = Pick<+ MetabotConverstationState,+ | "messages"+ | "history"+ | "state"+ | "activeToolCalls"+ | "errorMessages"+ | "conversationId"+> & { suggestedTransforms: MetabotSuggestedTransform[] };++type UseWorkspaceMetabotParams = {+ workspaceId: number;+ databaseId: DatabaseId | null | undefined;+ allTransforms: (UnsavedTransform | WorkspaceTransformListItem)[];+ setTab: (tab: string) => void;+ handleNavigateToTransform: (transformId: number) => void;+};++type UseWorkspaceMetabotReturn = {+ isMetabotAvailable: boolean;+ metabotContextTransform: AnyWorkspaceTransform | undefined;+ metabotContextSource: DraftTransformSource | undefined;+ setMetabotContextTransform: (+ transform: AnyWorkspaceTransform | undefined,+ ) => void;+ setMetabotContextSource: (source: DraftTransformSource | undefined) => void;+ suggestionActions: MetabotSuggestionActions;+};++export function useWorkspaceMetabot({+ workspaceId,+ databaseId,+ allTransforms,+ setTab,+ handleNavigateToTransform,+}: UseWorkspaceMetabotParams): UseWorkspaceMetabotReturn {+ const dispatch = useDispatch();+ const isWorkspace = useEnterpriseSelector(getIsWorkspace);+ const {+ openedTabs,+ setActiveTab,+ addOpenedTransform,+ setActiveTransform,+ addUnsavedTransform,+ unsavedTransforms,+ patchEditedTransform,+ } = useWorkspace();++ const isMetabotAvailable = PLUGIN_METABOT.isEnabled();+ const { navigateToPath, setNavigateToPath } = useMetabotReactions();+ const {+ resetConversation: resetMetabotConversation,+ visible: isMetabotVisible,+ setProfileOverride,+ } = useMetabotAgent();++ const [getTransform] = useLazyGetTransformQuery();++ const metabotState = useEnterpriseSelector(getMetabotState);+ const metabotStateRef = useRef<MetabotState>(metabotState);+ const metabotSnapshots = useRef<Map<number, MetabotConversationSnapshot>>(+ new Map(),+ );++ const [metabotContextTransform, setMetabotContextTransform] = useState<+ AnyWorkspaceTransform | undefined+ >();+ const [metabotContextSource, setMetabotContextSource] = useState<+ DraftTransformSource | undefined+ >();++ const openTaggedTransformInWorkspace = useCallback(+ (nextTransform: Transform) => {+ const taggedTransform: TaggedTransform = {+ ...nextTransform,+ type: "transform",+ };+ addOpenedTransform(taggedTransform);+ setActiveTransform(taggedTransform);+ setTab(getTransformTabId(taggedTransform));+ },+ [addOpenedTransform, setActiveTransform, setTab],+ );++ const applySuggestion = useCallback(+ async ({+ editorTransform,+ suggestedTransform,+ }: ApplySuggestionPayload): Promise<ApplySuggestionResult> => {+ const existingTransformId =+ typeof suggestedTransform.id === "number"+ ? suggestedTransform.id+ : undefined;++ // If editing an existing transform, fetch and open it+ if (existingTransformId != null) {+ const targetTransform = isSavedTransformInfo(editorTransform)+ ? editorTransform+ : isSavedTransformInfo(suggestedTransform)+ ? suggestedTransform+ : undefined;++ if (targetTransform) {+ openTaggedTransformInWorkspace(targetTransform);+ return { status: "applied" };+ }++ // Try to fetch the transform if we only have an ID+ try {+ const fetchedTransform =+ await getTransform(existingTransformId).unwrap();+ openTaggedTransformInWorkspace(fetchedTransform);+ return { status: "applied" };+ } catch {+ return {+ status: "error",+ message: t`Failed to load transform`,+ };+ }+ }++ // Creating a new transform - create unsaved transform first, then associate suggestion+ if (!suggestedTransform.target) {+ return {+ status: "error",+ message: t`Suggestion is missing a target table`,+ };+ }++ const dbId = getDatabaseIdFromSource(+ suggestedTransform.source,+ databaseId,+ );+ const sourceWithDatabase = setDatabaseIdOnSource(+ suggestedTransform.source,+ dbId,+ );++ const suggestionId = suggestedTransform.suggestionId;+ if (!suggestionId) {+ return {+ status: "error",+ message: t`Suggestion is missing an ID`,+ };+ }++ // Get the count of unsaved transforms before creating a new one+ // This will be used to calculate the ID of the new transform+ const previousUnsavedCount = unsavedTransforms.length;++ // Create unsaved transform with the suggested source+ // This will automatically open the transform tab and set it as active+ addUnsavedTransform(sourceWithDatabase);++ // Calculate the expected ID of the newly created unsaved transform+ // Based on addUnsavedTransform implementation: id = -1 - currentIndex+ const expectedId = -1 - previousUnsavedCount;++ // Update the suggested transform's ID to match the unsaved transform+ // This associates the suggestion with the transform so apply/reject buttons show+ dispatch(+ updateSuggestedTransformId({+ suggestionId,+ newId: expectedId,+ }),+ );++ // Activate the suggested transform so it shows as active in the UI+ dispatch(+ activateSuggestedTransform({+ id: expectedId,+ suggestionId,+ }),+ );++ // Update the transform name to match the suggestion+ patchEditedTransform(expectedId, {+ name: suggestedTransform.name,+ });++ return { status: "applied" };+ },+ [+ databaseId,+ unsavedTransforms,+ addUnsavedTransform,+ patchEditedTransform,+ dispatch,+ getTransform,+ openTaggedTransformInWorkspace,+ ],+ );++ const openTransform = useCallback(+ (nextTransform: { type: "transform" } & Transform) => {+ addOpenedTransform(nextTransform);+ setActiveTransform(nextTransform);+ },+ [addOpenedTransform, setActiveTransform],+ );++ const suggestionActions = useMemo<MetabotSuggestionActions>(+ () => ({+ openTransform,+ applySuggestion,+ }),+ [openTransform, applySuggestion],+ );++ // Keep metabotStateRef in sync+ useEffect(() => {+ metabotStateRef.current = metabotState;+ }, [metabotState]);++ // Register database context provider for Metabot+ useRegisterMetabotContextProvider(async () => {+ if (!databaseId) {+ return;+ }+ return { default_database_id: databaseId };+ }, [databaseId]);++ // Switch to metabot tab when metabot becomes visible+ useEffect(() => {+ if (isMetabotAvailable && isMetabotVisible) {+ setTab("metabot");+ setActiveTab(null);+ }+ }, [isMetabotAvailable, isMetabotVisible, setActiveTab, setTab]);++ // Clear metabot context when the associated transform tab is closed+ useEffect(() => {+ if (+ metabotContextTransform &&+ !openedTabs.some(+ (tab) =>+ tab.type === "transform" &&+ getTransformId(tab.transform) ===+ getTransformId(metabotContextTransform),+ )+ ) {+ setMetabotContextTransform(undefined);+ setMetabotContextSource(undefined);+ }+ }, [openedTabs, metabotContextTransform]);++ // Set profile override to transforms_codegen for workspace metabot+ useEffect(() => {+ if (isMetabotAvailable) {+ setProfileOverride(METABOT_PROFILE_OVERRIDES.TRANSFORMS_CODEGEN);+ }+ }, [isMetabotAvailable, isMetabotVisible, setProfileOverride]);++ // Restore/save metabot conversation snapshots per workspace+ useEffect(() => {+ const snapshots = metabotSnapshots.current;+ const snapshot = snapshots.get(workspaceId);+ if (snapshot) {+ dispatch(metabotActions.setConversationSnapshot(snapshot));+ } else {+ resetMetabotConversation();+ }++ return () => {+ const convo = metabotStateRef.current.conversations["omnibot"];+ if (convo) {+ snapshots.set(workspaceId, {+ messages: convo.messages,+ history: convo.history,+ state: convo.state,+ activeToolCalls: convo.activeToolCalls,+ errorMessages: convo.errorMessages,+ conversationId: convo.conversationId,+ suggestedTransforms:+ metabotStateRef.current.reactions.suggestedTransforms,+ });+ }+ resetMetabotConversation();+ };+ }, [workspaceId, dispatch, resetMetabotConversation]);++ // Handle metabot navigation reactions+ useEffect(() => {+ if (!navigateToPath) {+ return;+ }++ const transformIdFromPath = (() => {+ const match = navigateToPath.match(/\/transform\/(\d+)/);+ const extracted = Urls.extractEntityId(navigateToPath);+ const idString = match?.[1] ?? (extracted ? String(extracted) : null);+ const parsed = idString ? Number(idString) : NaN;+ return Number.isFinite(parsed) ? parsed : undefined;+ })();++ if (transformIdFromPath != null) {+ // Check if transform exists in local workspace state first (already opened/unsaved)+ const localTransform = allTransforms.find(+ (t: UnsavedTransform | WorkspaceTransformListItem) => {+ if (isUnsavedTransform(t)) {+ return t.id === transformIdFromPath;+ }+ return t.ref_id === String(transformIdFromPath);+ },+ );++ if (localTransform && !isUnsavedTransform(localTransform)) {+ // For WorkspaceTransformListItem, we need to navigate to fetch full data+ handleNavigateToTransform(transformIdFromPath);+ setNavigateToPath(null);+ return;+ }++ // Unsaved transforms are already opened when created, so just activate the tab+ if (localTransform && isUnsavedTransform(localTransform)) {+ // Find the existing tab and activate it+ const existingTab = openedTabs.find(+ (tab) =>+ tab.type === "transform" &&+ getTransformId(tab.transform) === localTransform.id,+ );+ if (existingTab) {+ setActiveTab(existingTab);+ }+ setNavigateToPath(null);+ return;+ }++ // Otherwise fetch and open the transform+ handleNavigateToTransform(transformIdFromPath);+ setNavigateToPath(null);+ return;+ }++ // In workspace context, don't redirect to question pages+ // The profile override should prevent most navigate_to chunks,+ // but if one slips through, we'll just ignore it+ if (isWorkspace) {+ setNavigateToPath(null);+ return;+ }++ dispatch(push(navigateToPath));+ setNavigateToPath(null);+ }, [+ navigateToPath,+ allTransforms,+ openedTabs,+ setActiveTab,+ setNavigateToPath,+ handleNavigateToTransform,+ dispatch,+ isWorkspace,+ ]);++ return {+ isMetabotAvailable,+ metabotContextTransform,+ metabotContextSource,+ setMetabotContextTransform,+ setMetabotContextSource,+ suggestionActions,+ };+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/utils/guards.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/utils/guards.tsnew file mode 100644index 00000000000..33b6df17cd2--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/utils/guards.ts@@ -0,0 +1,8 @@+import type { MetabotSuggestedTransform } from "metabase-enterprise/metabot/state";+import type { MetabotTransformInfo, Transform } from "metabase-types/api";++export const isSavedTransformInfo = (+ transform: MetabotTransformInfo | MetabotSuggestedTransform | undefined,+): transform is MetabotTransformInfo & Transform => {+ return Boolean(transform && "source_type" in transform);+};diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/utils/transform-source.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/utils/transform-source.tsnew file mode 100644index 00000000000..dd8960c0f17--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/pages/WorkspacePage/utils/transform-source.ts@@ -0,0 +1,42 @@+import type { DatabaseId, DraftTransformSource } from "metabase-types/api";++/**+ * Extracts the database ID from a transform source, falling back to the provided default.+ */+export function getDatabaseIdFromSource(+ source: DraftTransformSource,+ fallbackDatabaseId: DatabaseId | null | undefined,+): DatabaseId | null | undefined {+ if (source.type === "query") {+ return source.query.database ?? fallbackDatabaseId ?? null;+ }+ if (source.type === "python") {+ return source["source-database"] ?? fallbackDatabaseId ?? undefined;+ }+ return fallbackDatabaseId ?? null;+}++/**+ * Creates a new source with the database ID properly set.+ */+export function setDatabaseIdOnSource(+ source: DraftTransformSource,+ databaseId: DatabaseId | null | undefined,+): DraftTransformSource {+ if (source.type === "python") {+ return {+ ...source,+ "source-database": databaseId ?? undefined,+ };+ }+ if (source.type === "query") {+ return {+ ...source,+ query: {+ ...source.query,+ database: databaseId ?? null,+ },+ };+ }+ return source;+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/routes.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/routes.tsxnew file mode 100644index 00000000000..f4945e1079e--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/routes.tsx@@ -0,0 +1,13 @@+import { IndexRoute, Route } from "react-router";++import { WorkspaceListPage } from "./pages/WorkspaceListPage/WorkspaceListPage";+import { WorkspacePage } from "./pages/WorkspacePage/WorkspacePage";++export function getDataStudioWorkspaceRoutes() {+ return (+ <>+ <IndexRoute component={WorkspaceListPage} />+ <Route path=":workspaceId" component={WorkspacePage} />+ </>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/settings.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/settings.tsxnew file mode 100644index 00000000000..ad94c410578--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/settings.tsx@@ -0,0 +1 @@+export const DATABASE_WORKSPACES_SETTING = "database-enable-workspaces";diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/utils.ts b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/utils.tsnew file mode 100644index 00000000000..d53e77a6338--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/workspaces/utils.ts@@ -0,0 +1,34 @@+import { t } from "ttag";++import type {+ CheckoutDisabledReason,+ WorkspaceSetupStatus,+} from "metabase-types/api";++export const isWorkspaceUninitialized = (workspace: {+ status: WorkspaceSetupStatus;+}) => {+ return workspace.status === "uninitialized";+};++export const isWorkspaceReady = (workspace: {+ status: WorkspaceSetupStatus;+}) => {+ return workspace.status === "ready";+};++export function getCheckoutDisabledMessage(+ reason: CheckoutDisabledReason | undefined,+): string | undefined {+ switch (reason) {+ case "mbql":+ return t`Transforms based on the query builder cannot be edited in a workspace.`;+ case "card-reference":+ return t`Transforms referencing other questions cannot be edited in a workspace.`;+ case null:+ case undefined:+ return undefined;+ default:+ return t`This transform cannot be edited in a workspace.`;+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/DependencyGraph.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/DependencyGraph.tsxindex 1f0ed0440e4..a830cef4cf3 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/DependencyGraph.tsx+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/DependencyGraph.tsx@@ -1,3 +1,4 @@+import { skipToken } from "@reduxjs/toolkit/query";import {Background,Controls,@@ -10,11 +11,13 @@ import {import { useEffect, useMemo, useState } from "react";import { t } from "ttag";-import { skipToken } from "metabase/api";import { useMetadataToasts } from "metabase/metadata/hooks";import { Group, useColorScheme } from "metabase/ui";import { useGetDependencyGraphQuery } from "metabase-enterprise/api";-import type { DependencyEntry } from "metabase-types/api";+import type {+ DependencyGraph,+ WorkspaceDependencyGraph,+} from "metabase-types/api";import S from "./DependencyGraph.module.css";import { GraphContext } from "./GraphContext";@@ -42,21 +45,35 @@ const PRO_OPTIONS = {};type DependencyGraphProps = {- entry?: DependencyEntry;- getGraphUrl: (entry?: DependencyEntry) => string;+ graph?: DependencyGraph | WorkspaceDependencyGraph | null;+ isFetching?: boolean;+ error?: any;+ getGraphUrl: (entry?: any) => string;withEntryPicker?: boolean;+ entry?: any;+ nodeTypes?: typeof NODE_TYPES;+ edgeTypes?: typeof EDGE_TYPES;+ openLinksInNewTab?: boolean;};export function DependencyGraph({entry,+ graph: externalGraph,+ isFetching: isFetchingExternally = false,+ error: externalError,getGraphUrl,withEntryPicker,+ nodeTypes = NODE_TYPES,+ edgeTypes = EDGE_TYPES,+ openLinksInNewTab = true,}: DependencyGraphProps) {- const {- data: graph,- isFetching,- error,- } = useGetDependencyGraphQuery(entry ?? skipToken);+ const shouldFetch = entry != null && !externalGraph;+ const dependencyGraph = useGetDependencyGraphQuery(+ shouldFetch ? entry : skipToken,+ );+ const isFetching = isFetchingExternally || dependencyGraph.isFetching;+ const graph = externalGraph || dependencyGraph.data;+ const error = externalError || dependencyGraph.error;const [nodes, setNodes, onNodesChange] = useNodesState<NodeType>([]);const [edges, setEdges, onEdgesChange] = useEdgesState<Edge>([]);@@ -97,13 +114,15 @@ export function DependencyGraph({};return (- <GraphContext.Provider value={{ selection, setSelection }}>+ <GraphContext.Provider+ value={{ selection, setSelection, openLinksInNewTab }}+ ><ReactFlowclassName={S.reactFlow}nodes={nodes}edges={edges}- nodeTypes={NODE_TYPES}- edgeTypes={EDGE_TYPES}+ nodeTypes={nodeTypes}+ edgeTypes={edgeTypes}proOptions={PRO_OPTIONS}minZoom={MIN_ZOOM}maxZoom={MAX_ZOOM}diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphContext/GraphContext.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphContext/GraphContext.tsxindex b539125ee8c..fbd4bbb5c30 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphContext/GraphContext.tsx+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphContext/GraphContext.tsx@@ -5,4 +5,5 @@ import type { GraphContextType } from "../types";export const GraphContext = createContext<GraphContextType>({selection: null,setSelection: () => undefined,+ openLinksInNewTab: true,});diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphDependencyPanel/PanelHeader/utils.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphDependencyPanel/PanelHeader/utils.tsindex 505678720b6..fe119bc2483 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphDependencyPanel/PanelHeader/utils.ts+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphDependencyPanel/PanelHeader/utils.ts@@ -23,6 +23,8 @@ export function getHeaderLabel(: t`Tables that use ${nodeLabel}`;case "transform":return t`Transforms that use ${nodeLabel}`;+ case "workspace-transform":+ return t`Workspace transforms that use ${nodeLabel}`;case "snippet":return t`Snippets that use ${nodeLabel}`;case "dashboard":diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphExternalLink/GraphExternalLink.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphExternalLink/GraphExternalLink.tsxindex d8864b266a5..4cb88d5b89d 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphExternalLink/GraphExternalLink.tsx+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphExternalLink/GraphExternalLink.tsx@@ -1,7 +1,10 @@+import { useContext } from "react";+import { ForwardRefLink } from "metabase/common/components/Link";import { ActionIcon, FixedSizeIcon, Tooltip } from "metabase/ui";import { TOOLTIP_OPEN_DELAY_MS } from "../../../constants";+import { GraphContext } from "../GraphContext";type GraphExternalLinkProps = {label: string;@@ -9,12 +12,14 @@ type GraphExternalLinkProps = {};export function GraphExternalLink({ label, url }: GraphExternalLinkProps) {+ const { openLinksInNewTab } = useContext(GraphContext);+return (<Tooltip label={label} openDelay={TOOLTIP_OPEN_DELAY_MS}><ActionIconcomponent={ForwardRefLink}to={url}- target="_blank"+ target={openLinksInNewTab ? "_blank" : undefined}m="-sm"aria-label={label}>diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphNode/WorkspaceGraphNode.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphNode/WorkspaceGraphNode.tsxnew file mode 100644index 00000000000..c2ab36c82a6--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphNode/WorkspaceGraphNode.tsx@@ -0,0 +1,168 @@+import {+ Handle,+ type NodeProps,+ Position,+ useNodeConnections,+ useNodesInitialized,+} from "@xyflow/react";+import cx from "classnames";+import type { MouseEvent } from "react";+import { memo, useContext } from "react";++import { useLazyGetTableQueryMetadataQuery } from "metabase/api";+import {+ Box,+ Card,+ FixedSizeIcon,+ Group,+ Pill,+ Stack,+ Text,+ UnstyledButton,+} from "metabase/ui";+import type { DependencyNode } from "metabase-types/api";++import type { DependentGroup } from "../../../types";+import {+ getDependencyGroupType,+ getDependencyGroupTypeInfo,+ getNodeIcon,+ getNodeLabel,+ isSameNode,+} from "../../../utils";+import { GraphContext } from "../GraphContext";+import type { GraphSelection, NodeType } from "../types";++import S from "./GraphNode.module.css";+import {+ getDependencyGroupTitle,+ getDependentGroupLabel,+ getDependentGroups,+} from "./utils";++type WorkspaceGraphNodeProps = NodeProps<NodeType>;++export const WorkspaceGraphNode = memo(function ItemNode({+ data: node,+}: WorkspaceGraphNodeProps) {+ const { selection, setSelection } = useContext(GraphContext);+ const label = getNodeLabel(node);+ const typeInfo = getDependencyGroupTypeInfo(getDependencyGroupType(node));+ const groups = getDependentGroups(node);+ const sources = useNodeConnections({ handleType: "source" });+ const targets = useNodeConnections({ handleType: "target" });+ const isSelected = selection != null && isSameNode(node, selection);+ const isInitialized = useNodesInitialized();++ // Fetch table data for input table nodes+ const [fetchTableMetadata] = useLazyGetTableQueryMetadataQuery();++ const handleClick = async () => {+ // Fetch table metadata when a table node is clicked+ if (node.type === "table" && node.data.table_id) {+ const tableData = await fetchTableMetadata(+ { id: node.data.table_id },+ true,+ ).unwrap();+ node.data.fields = tableData.fields;+ }++ setSelection({ id: node.id, type: node.type });+ };++ return (+ <>+ <Card+ className={cx(S.card, {+ [S.initialized]: isInitialized,+ [S.selected]: isSelected,+ })}+ p="lg"+ withBorder+ aria-label={label}+ aria-selected={isSelected}+ data-testid="graph-node"+ onClick={handleClick}+ >+ <Stack gap="sm">+ <Group c={typeInfo.color} gap="xs">+ <FixedSizeIcon name={getNodeIcon(node)} />+ <Box fz="sm" fw="bold" lh="1rem">+ {typeInfo.label}+ </Box>+ </Group>+ <Box fw="bold" lh="1rem">+ {label}+ </Box>+ </Stack>+ <Stack mt="md" gap="sm" align="start">+ <Box c="text-secondary" fz="sm" lh="1rem">+ {getDependencyGroupTitle(node, groups)}+ </Box>+ {groups.map((group) => (+ <Text key={group.type}>{getDependentGroupLabel(group)}</Text>+ ))}+ </Stack>+ </Card>+ {sources.length > 0 && (+ <Handle+ className={cx(S.handle, { [S.initialized]: isInitialized })}+ type="source"+ position={Position.Left}+ isConnectable={false}+ />+ )}+ {targets.length > 0 && (+ <Handle+ className={cx(S.handle, { [S.initialized]: isInitialized })}+ type="target"+ position={Position.Right}+ isConnectable={false}+ />+ )}+ </>+ );+});++type DependencyGroupButtonProps = {+ node: DependencyNode;+ group: DependentGroup;+ selection: GraphSelection | null;+ onSelectionChange: (selection: GraphSelection) => void;+};++// TODO (stasgavrylov 2026-01-15) --+// Leave a simpler representation for rependency group right now, but will+// probably return to this in+function _DependencyGroupButton({+ node,+ group,+ selection,+ onSelectionChange,+}: DependencyGroupButtonProps) {+ const isSelected =+ selection != null &&+ isSameNode(node, selection) &&+ selection.groupType === group.type;++ const handleClick = (event: MouseEvent<HTMLDivElement>) => {+ event.stopPropagation();+ onSelectionChange({+ id: node.id,+ type: node.type,+ groupType: group.type,+ });+ };++ return (+ <Pill+ key={group.type}+ component={UnstyledButton}+ className={cx(S.pill, { [S.selected]: isSelected })}+ fw="normal"+ onClick={handleClick}+ >+ {getDependentGroupLabel(group)}+ </Pill>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphNode/utils.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphNode/utils.tsnew file mode 100644index 00000000000..c063a5c86fc--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphNode/utils.ts@@ -0,0 +1,103 @@+import { c, msgid, ngettext, t } from "ttag";++import type { DependencyNode } from "metabase-types/api";++import type { DependentGroup } from "../../../types";++export function getDependentGroups(node: DependencyNode): DependentGroup[] {+ const {+ question = 0,+ model = 0,+ metric = 0,+ table = 0,+ transform = 0,+ "workspace-transform": workspaceTransform = 0,+ snippet = 0,+ dashboard = 0,+ document = 0,+ sandbox = 0,+ segment = 0,+ measure = 0,+ } = node.dependents_count ?? {};++ const groups: DependentGroup[] = [+ { type: "question", count: question },+ { type: "model", count: model },+ { type: "metric", count: metric },+ { type: "table", count: table },+ { type: "transform", count: transform },+ { type: "workspace-transform", count: workspaceTransform },+ { type: "snippet", count: snippet },+ { type: "dashboard", count: dashboard },+ { type: "document", count: document },+ { type: "sandbox", count: sandbox },+ { type: "segment", count: segment },+ { type: "measure", count: measure },+ ];++ return groups.filter(({ count }) => count !== 0);+}++export function getDependencyGroupTitle(+ node: DependencyNode,+ groups: DependentGroup[],+) {+ if (node.type === "sandbox") {+ return t`Restricts table data`;+ }+ if (groups.length === 0) {+ return t`Nothing uses this`;+ }+ if (node.type === "transform") {+ return t`Generates`;+ }+ return t`Used by`;+}++export function getDependentGroupLabel({+ type,+ count,+}: DependentGroup): string {+ switch (type) {+ case "question":+ return ngettext(msgid`${count} question`, `${count} questions`, count);+ case "model":+ return ngettext(msgid`${count} model`, `${count} models`, count);+ case "metric":+ return ngettext(msgid`${count} metric`, `${count} metrics`, count);+ case "table":+ return ngettext(msgid`${count} table`, `${count} tables`, count);+ case "transform":+ return ngettext(msgid`${count} transform`, `${count} transforms`, count);+ case "workspace-transform":+ return ngettext(+ msgid`${count} workspace transform`,+ `${count} workspace transforms`,+ count,+ );+ case "snippet":+ return ngettext(msgid`${count} snippet`, `${count} snippet`, count);+ case "dashboard":+ return ngettext(msgid`${count} dashboard`, `${count} dashboards`, count);+ case "document":+ return ngettext(msgid`${count} document`, `${count} documents`, count);+ case "sandbox":+ return ngettext(+ msgid`${count} row and column security rule`,+ `${count} row and column security rules`,+ count,+ );+ case "segment":+ return c("{0} is the number of segments").ngettext(+ msgid`${count} segment`,+ `${count} segments`,+ count,+ );+ case "measure":+ return c("{0} is the number of measures").ngettext(+ msgid`${count} measure`,+ `${count} measures`,+ count,+ );+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/types.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/types.tsindex 455cba0a446..b43e7c25519 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/types.ts+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/types.ts@@ -24,4 +24,5 @@ export type GraphSelection = {export type GraphContextType = {selection: GraphSelection | null;setSelection: (selection: GraphSelection | null) => void;+ openLinksInNewTab: boolean;};diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/utils.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/utils.tsindex 66ff431c8ee..503debd43dc 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/utils.ts+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/utils.ts@@ -5,6 +5,9 @@ import type {DependencyEntry,DependencyGraph,DependencyNode,+ WorkspaceDependencyGraph,+ WorkspaceGraphDependencyEdge,+ WorkspaceGraphDependencyNode,} from "metabase-types/api";import type { NodeId } from "../../types";@@ -16,14 +19,17 @@ export function getEdgeId(sourceId: NodeId, targetId: NodeId): EdgeId {return `${sourceId}-${targetId}`;}-function getNodes(nodes: DependencyNode[]): NodeType[] {+type AnyDependencyNode = DependencyNode | WorkspaceGraphDependencyNode;+type AnyDependencyEdge = DependencyEdge | WorkspaceGraphDependencyEdge;++function getNodes(nodes: AnyDependencyNode[]): NodeType[] {return nodes.map((node) => {const nodeId = getNodeId(node.id, node.type);return {id: nodeId,type: "node",- data: node,+ data: node as DependencyNode, // Cast for NodeType compatibilityposition: { x: 0, y: 0 },draggable: false,deletable: false,@@ -31,7 +37,7 @@ function getNodes(nodes: DependencyNode[]): NodeType[] {});}-function getEdges(edges: DependencyEdge[]): Edge[] {+function getEdges(edges: AnyDependencyEdge[]): Edge[] {return edges.map((edge) => {const sourceId = getNodeId(edge.from_entity_id, edge.from_entity_type);const targetId = getNodeId(edge.to_entity_id, edge.to_entity_type);@@ -48,7 +54,10 @@ function getEdges(edges: DependencyEdge[]): Edge[] {});}-export function getInitialGraph({ nodes, edges }: DependencyGraph): GraphData {+export function getInitialGraph({+ nodes,+ edges,+}: DependencyGraph | WorkspaceDependencyGraph): GraphData {return {nodes: getNodes(nodes),edges: getEdges(edges),diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/pages/DependencyGraphPage/DependencyGraphPage.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/pages/DependencyGraphPage/DependencyGraphPage.tsxindex 15addd26f42..f8f23cb1467 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/pages/DependencyGraphPage/DependencyGraphPage.tsx+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/pages/DependencyGraphPage/DependencyGraphPage.tsx@@ -1,10 +1,12 @@import type { Location } from "history";import { useContext } from "react";+import { skipToken } from "metabase/api";import * as Urls from "metabase/lib/urls";import { AppSwitcher } from "metabase/nav/components/AppSwitcher";import { PLUGIN_DEPENDENCIES } from "metabase/plugins";import { Stack } from "metabase/ui";+import { useGetDependencyGraphQuery } from "metabase-enterprise/api";import { DependencyGraph } from "../../components/DependencyGraph";import { isSameNode } from "../../utils";@@ -29,10 +31,19 @@ export function DependencyGraphPage({ location }: DependencyGraphPageProps) {const withEntryPicker =defaultEntry == null || (entry != null && !isSameNode(entry, defaultEntry));+ const {+ data: graph,+ isFetching,+ error,+ } = useGetDependencyGraphQuery(entry ?? defaultEntry ?? skipToken);+return (<Stack h="100%"><DependencyGraphentry={entry ?? defaultEntry}+ graph={graph}+ isFetching={isFetching}+ error={error}getGraphUrl={(entry) => Urls.dependencyGraph({ entry, baseUrl })}withEntryPicker={withEntryPicker}/>diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/utils.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/utils.tsindex 8eaa4590b32..716ee4177f1 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/utils.ts+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/utils.ts@@ -37,7 +37,10 @@ export function isSameNode(return entry1.id === entry2.id && entry1.type === entry2.type;}-export function getNodeId(id: DependencyId, type: DependencyType): NodeId {+export function getNodeId(+ id: DependencyId | string,+ type: DependencyType | string,+): NodeId {return `${id}-${type}`;}@@ -56,6 +59,7 @@ export function getNodeDescription(node: DependencyNode): string | null {switch (node.type) {case "document":case "sandbox":+ case "workspace-transform":return null;default:return node.data.description ?? "";@@ -98,6 +102,8 @@ export function getNodeIconWithType(return "table";case "transform":return "transform";+ case "workspace-transform":+ return "transform";case "snippet":return "snippet";case "dashboard":@@ -149,6 +155,17 @@ export function getNodeLink(node: DependencyNode): NodeLink | null {label: t`View this transform`,url: Urls.transform(node.id),};+ case "workspace-transform": {+ const workspaceId = node.data.workspace_id;+ const refId = node.data.ref_id;+ if (workspaceId == null) {+ return null;+ }+ return {+ label: t`View this workspace transform`,+ url: Urls.dataStudioWorkspace(workspaceId, refId),+ };+ }case "dashboard":return {label: t`View this dashboard`,@@ -328,6 +345,7 @@ export function getNodeLocationInfo(}return null;case "transform":+ case "workspace-transform":case "sandbox":return null;}@@ -444,12 +462,17 @@ export function getNodeFields(node: DependencyNode): Field[] | null {return node.data.fields ?? [];case "transform":case "sandbox":+ return node.data.table?.fields ?? [];+ case "workspace-transform":+ return [];case "snippet":case "dashboard":case "document":case "segment":case "measure":return null;+ default:+ return null;}}@@ -499,6 +522,8 @@ export function getDependencyGroupType(return "table";case "transform":return "transform";+ case "workspace-transform":+ return "workspace-transform";case "dashboard":return "dashboard";case "document":@@ -528,6 +553,8 @@ export function getDependencyGroupTypeInfo(return { label: t`Table`, color: "brand" };case "transform":return { label: t`Transform`, color: "warning" };+ case "workspace-transform":+ return { label: t`Workspace transform`, color: "warning" };case "snippet":return { label: t`Snippet`, color: "text-secondary" };case "dashboard":@@ -675,6 +702,8 @@ export function getDependentGroupLabel({`${count} measures`,count,);+ default:+ return ngettext(msgid`${count} entity`, `${count} entities`, count);}}diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/Metabot.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/Metabot.tsxindex 973b3355de2..4d10a734f1a 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/Metabot.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/Metabot.tsx@@ -94,7 +94,14 @@ export const MetabotAuthenticated = ({ hide, config }: MetabotProps) => {return (<ErrorBoundary key={errorBoundaryKey} errorComponent={ErrorFallback}>- <MetabotChat config={config} />+ <Sidebar+ isOpen={visible}+ side="right"+ width="30rem"+ aria-hidden={!visible}+ >+ <MetabotChat config={config} />+ </Sidebar></ErrorBoundary>);};diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotAgentSuggestionMessage.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotAgentSuggestionMessage.tsxindex 99e27680623..8d5160ebf1d 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotAgentSuggestionMessage.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotAgentSuggestionMessage.tsx@@ -2,7 +2,7 @@ import { unifiedMergeView } from "@codemirror/merge";import { useDisclosure } from "@mantine/hooks";import type { UnknownAction } from "@reduxjs/toolkit";import cx from "classnames";-import { useMemo } from "react";+import { useMemo, useState } from "react";import { push } from "react-router-redux";import { useLocation, useMount } from "react-use";import { P, match } from "ts-pattern";@@ -12,8 +12,10 @@ import _ from "underscore";import { CodeMirror } from "metabase/common/components/CodeMirror";import { useDispatch, useSelector } from "metabase/lib/redux";import * as Urls from "metabase/lib/urls";+import { useMetadataToasts } from "metabase/metadata/hooks";import EditorS from "metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/CodeMirrorEditor.module.css";import { getMetadata } from "metabase/selectors/metadata";+import { getIsWorkspace } from "metabase/selectors/routing";import {Button,Collapse,@@ -25,6 +27,7 @@ import {Text,} from "metabase/ui";import { useLazyGetTransformQuery } from "metabase-enterprise/api";+import { useMetabotSuggestionActions } from "metabase-enterprise/metabot/context";import {type MetabotAgentEditSuggestionChatMessage,activateSuggestedTransform,@@ -106,8 +109,17 @@ export const AgentSuggestionMessage = ({}) => {const dispatch = useDispatch();const metadata = useSelector(getMetadata);+ const isWorkspace = useSelector(getIsWorkspace);+ const suggestionActions = useMetabotSuggestionActions();+ const { sendErrorToast } = useMetadataToasts();+ const [isApplying, setIsApplying] = useState(false);+ const [hasAppliedInContext, setHasAppliedInContext] = useState(false);const { suggestedTransform, editorTransform } = message.payload;+ const existingTransformId =+ typeof suggestedTransform.id === "number"+ ? suggestedTransform.id+ : undefined;const isActive = useSelector((state) =>getIsSuggestedTransformActive(state, suggestedTransform.suggestionId),);@@ -115,12 +127,16 @@ export const AgentSuggestionMessage = ({const [opened, { toggle }] = useDisclosure(true);const url = useLocation();- const isViewing = url.pathname?.startsWith(- getTransformUrl(suggestedTransform),- );+ // In workspace context, we don't use URL-based navigation, so isViewing should be false+ // This ensures suggestions always show properly in workspace+ const isViewing = isWorkspace+ ? false+ : (url.pathname?.startsWith(getTransformUrl(suggestedTransform)) ?? false);- const canApply = !isViewing || !isActive;- const isNew = !isViewing && !editorTransform && suggestedTransform.id == null;+ const canApply = suggestionActions+ ? !hasAppliedInContext && !isApplying+ : !isViewing || !isActive;+ const isNew = !isViewing && !editorTransform && existingTransformId == null;const {data: originalTransform,@@ -133,8 +149,34 @@ export const AgentSuggestionMessage = ({: "";const newSource = getSourceCode(suggestedTransform, metadata);- const handleApply = () => {+ const handleApply = async () => {dispatch(activateSuggestedTransform(suggestedTransform));++ if (suggestionActions) {+ setIsApplying(true);+ try {+ const result = await suggestionActions.applySuggestion(message.payload);+ if (result.status === "applied") {+ setHasAppliedInContext(true);+ } else {+ sendErrorToast(result.message);+ }+ } finally {+ setIsApplying(false);+ }+ return;+ }++ // In workspace context, don't redirect - the suggestion actions should handle it+ // If we get here, it means suggestionActions is not available, which shouldn't happen+ // in workspace context, but we'll prevent the redirect anyway+ if (isWorkspace) {+ sendErrorToast(+ t`Unable to apply suggestion. Please try again or refresh the page.`,+ );+ return;+ }+dispatch(push(getTransformUrl(suggestedTransform)) as UnknownAction);};@@ -226,11 +268,13 @@ export const AgentSuggestionMessage = ({disabled={!canApply}onClick={handleApply}>- {match({ isNew, canApply })- .with({ canApply: false }, () => t`Applied`)- .with({ isNew: true }, () => t`Create`)- .with({ canApply: true }, () => t`Apply`)- .exhaustive()}+ {isApplying+ ? t`Applying...`+ : match({ isNew, canApply })+ .with({ canApply: false }, () => t`Applied`)+ .with({ isNew: true }, () => t`Create`)+ .with({ canApply: true }, () => t`Apply`)+ .exhaustive()}</Button></Flex></Group>diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChat.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChat.tsxindex 3d5e1493d67..b99358bf3ee 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChat.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChat.tsx@@ -3,7 +3,6 @@ import { useMemo } from "react";import { t } from "ttag";import EmptyDashboardBot from "assets/img/dashboard-empty.svg?component";-import { Sidebar } from "metabase/nav/containers/MainNavbar/MainNavbar.styled";import {ActionIcon,Box,@@ -74,141 +73,131 @@ export const MetabotChat = ({const handleEditorSubmit = () => metabot.submitInput(metabot.prompt);return (- <Sidebar- isOpen={metabot.visible}- side="right"- width="30rem"- aria-hidden={!metabot.visible}- >- <Box className={Styles.container} data-testid="metabot-chat">- {/* header */}- <Box ref={headerRef} className={Styles.header}>- <Flex align-items="center">- <Text lh={1} fz="sm" c="text-secondary">- {t`Metabot isn't perfect. Double-check results.`}- </Text>- </Flex>-- <Flex gap="sm">- <Tooltip label={t`Clear conversation`} position="bottom">- <ActionIcon- onClick={handleResetChat}- data-testid="metabot-reset-chat"- >- <Icon c="text-primary" name="revert" />- </ActionIcon>- </Tooltip>- {!config.preventClose && (- <ActionIcon- onClick={handleClose}- data-testid="metabot-close-chat"- >- <Icon c="text-primary" name="close" />- </ActionIcon>- )}- </Flex>- </Box>-- {/* chat messages */}- <Box- ref={scrollContainerRef}- className={Styles.messagesContainer}- data-testid="metabot-chat-messages"- >- {!hasMessages && !metabot.isDoingScience && (- <>- {/* empty state */}- <Flex- h="100%"- gap="md"- direction="column"- align="center"- justify="center"- data-testid="metabot-empty-chat-info"- >- <Box component={EmptyDashboardBot} w="6rem" />- <Text c="text-tertiary" maw="12rem" ta="center">- {config.emptyText ??- t`I can help you explore your metrics and models.`}- </Text>- </Flex>- {!config.hideSuggestedPrompts && (- <Stack- gap="sm"- className={Styles.promptSuggestionsContainer}- data-testid="metabot-prompt-suggestions"- >- <>- {suggestedPrompts.map(({ prompt }, index) => (- <Box key={index}>- <Button- fz="sm"- size="xs"- onClick={() => metabot.submitInput(prompt)}- className={Styles.promptSuggestionButton}- >- {prompt}- </Button>- </Box>- ))}- </>- </Stack>- )}- </>- )}-- {(hasMessages || metabot.isDoingScience) && (- <Box- className={Styles.messages}- data-testid="metabot-chat-inner-messages"+ <Box className={Styles.container} data-testid="metabot-chat">+ {/* header */}+ <Box ref={headerRef} className={Styles.header}>+ <Flex align-items="center">+ <Text lh={1} fz="sm" c="text-secondary">+ {t`Metabot isn't perfect. Double-check results.`}+ </Text>+ </Flex>++ <Flex gap="sm">+ <Tooltip label={t`Clear conversation`} position="bottom">+ <ActionIcon+ onClick={handleResetChat}+ data-testid="metabot-reset-chat">- {/* conversation messages */}- <Messages- messages={metabot.messages}- errorMessages={metabot.errorMessages}- onRetryMessage={- config.preventRetryMessage ? undefined : metabot.retryMessage- }- isDoingScience={metabot.isDoingScience}- showFeedbackButtons- />- {/* loading */}- {metabot.isDoingScience && (- <MetabotThinking toolCalls={metabot.activeToolCalls} />- )}- {/* filler - height gets set via ref mutation */}- <div ref={fillerRef} data-testid="metabot-message-filler" />- {/* long convo warning */}- {metabot.isLongConversation && (- <MetabotResetLongChatButton- onResetConversation={metabot.resetConversation}- />- )}- </Box>+ <Icon c="text-primary" name="revert" />+ </ActionIcon>+ </Tooltip>+ {!config.preventClose && (+ <ActionIcon onClick={handleClose} data-testid="metabot-close-chat">+ <Icon c="text-primary" name="close" />+ </ActionIcon>)}- </Box>+ </Flex>+ </Box>- <Box className={Styles.textInputContainer}>- <Paper- className={cx(- Styles.inputContainer,- metabot.isDoingScience && Styles.inputContainerLoading,+ {/* chat messages */}+ <Box+ ref={scrollContainerRef}+ className={Styles.messagesContainer}+ data-testid="metabot-chat-messages"+ >+ {!hasMessages && !metabot.isDoingScience && (+ <>+ {/* empty state */}+ <Flex+ h="100%"+ gap="md"+ direction="column"+ align="center"+ justify="center"+ data-testid="metabot-empty-chat-info"+ >+ <Box component={EmptyDashboardBot} w="6rem" />+ <Text c="text-tertiary" maw="12rem" ta="center">+ {config.emptyText ??+ t`I can help you explore your metrics and models.`}+ </Text>+ </Flex>+ {!config.hideSuggestedPrompts && (+ <Stack+ gap="sm"+ className={Styles.promptSuggestionsContainer}+ data-testid="metabot-prompt-suggestions"+ >+ <>+ {suggestedPrompts.map(({ prompt }, index) => (+ <Box key={index}>+ <Button+ fz="sm"+ size="xs"+ onClick={() => metabot.submitInput(prompt)}+ className={Styles.promptSuggestionButton}+ >+ {prompt}+ </Button>+ </Box>+ ))}+ </>+ </Stack>)}+ </>+ )}++ {(hasMessages || metabot.isDoingScience) && (+ <Box+ className={Styles.messages}+ data-testid="metabot-chat-inner-messages">- <MetabotChatEditor- ref={metabot.promptInputRef}- value={metabot.prompt}- autoFocus- isResponding={metabot.isDoingScience}- placeholder={t`Tell me to do something, or ask a question`}- onChange={metabot.setPrompt}- onSubmit={handleEditorSubmit}- onStop={metabot.cancelRequest}- suggestionConfig={{ suggestionModels: config.suggestionModels }}+ {/* conversation messages */}+ <Messages+ messages={metabot.messages}+ errorMessages={metabot.errorMessages}+ onRetryMessage={+ config.preventRetryMessage ? undefined : metabot.retryMessage+ }+ isDoingScience={metabot.isDoingScience}+ showFeedbackButtons/>- </Paper>- </Box>+ {/* loading */}+ {metabot.isDoingScience && (+ <MetabotThinking toolCalls={metabot.activeToolCalls} />+ )}+ {/* filler - height gets set via ref mutation */}+ <div ref={fillerRef} data-testid="metabot-message-filler" />+ {/* long convo warning */}+ {metabot.isLongConversation && (+ <MetabotResetLongChatButton+ onResetConversation={metabot.resetConversation}+ />+ )}+ </Box>+ )}+ </Box>++ <Box className={Styles.textInputContainer}>+ <Paper+ className={cx(+ Styles.inputContainer,+ metabot.isDoingScience && Styles.inputContainerLoading,+ )}+ >+ <MetabotChatEditor+ ref={metabot.promptInputRef}+ value={metabot.prompt}+ autoFocus+ isResponding={metabot.isDoingScience}+ placeholder={t`Tell me to do something, or ask a question`}+ onChange={metabot.setPrompt}+ onSubmit={handleEditorSubmit}+ onStop={metabot.cancelRequest}+ suggestionConfig={{ suggestionModels: config.suggestionModels }}+ />+ </Paper></Box>- </Sidebar>+ </Box>);};diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/constants.ts b/enterprise/frontend/src/metabase-enterprise/metabot/constants.tsindex b79844e2072..4356a186f61 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/constants.ts+++ b/enterprise/frontend/src/metabase-enterprise/metabot/constants.ts@@ -20,6 +20,7 @@ export const FIXED_METABOT_ENTITY_IDS = {export const METABOT_PROFILE_OVERRIDES = {DEFAULT: undefined,SQL: "sql",+ TRANSFORMS_CODEGEN: "transforms_codegen",};export const METABOT_ERR_MSG = {diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/context.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/context.tsxindex 40c81c60a59..9f0a37b8b55 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/context.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/context.tsx@@ -1,6 +1,13 @@import dayjs from "dayjs";import type React from "react";-import { createContext, useCallback, useRef, useState } from "react";+import {+ createContext,+ useCallback,+ useContext,+ useEffect,+ useRef,+ useState,+} from "react";import _ from "underscore";import { useStore } from "metabase/lib/redux";@@ -14,9 +21,24 @@ import {canUserCreateQueries,getUserIsAdmin,} from "metabase/selectors/user";-import type { MetabotChatContext } from "metabase-types/api";+import type {+ MetabotChatContext,+ MetabotTransformInfo,+ TaggedTransform,+} from "metabase-types/api";++import type { MetabotSuggestedTransform } from "./state";++export type ApplySuggestionPayload = {+ editorTransform: MetabotTransformInfo | undefined;+ suggestedTransform: MetabotSuggestedTransform;+};++export type ApplySuggestionResult =+ | { status: "applied" }+ | { status: "error"; message: string };-export const defaultContext = {+export const defaultContext: MetabotCtx = {prompt: "",setPrompt: () => {},promptInputRef: undefined,@@ -28,6 +50,9 @@ export const defaultContext = {capabilities: [],}),registerChatContextProvider: () => () => {},++ suggestionActions: null,+ setSuggestionActions: () => {},};const mergeCtx = (@@ -43,7 +68,38 @@ const mergeCtx = (};};-export const MetabotContext = createContext<MetabotCtx>(defaultContext);+export type MetabotSuggestionActions = {+ openTransform: (transform: TaggedTransform) => void;+ applySuggestion: (+ payload: ApplySuggestionPayload,+ ) => Promise<ApplySuggestionResult>;+};++interface EnterpriseMetabotContext+ extends Omit<MetabotCtx, "suggestionActions" | "setSuggestionActions"> {+ suggestionActions: MetabotSuggestionActions | null;+ setSuggestionActions: (actions: MetabotSuggestionActions | null) => void;+}++export const MetabotContext = createContext<EnterpriseMetabotContext>(+ defaultContext as EnterpriseMetabotContext,+);++export const useMetabotSuggestionActions = () => {+ const { suggestionActions } = useContext(MetabotContext);+ return suggestionActions;+};++export const useRegisterMetabotSuggestionActions = (+ actions: MetabotSuggestionActions | null | undefined,+) => {+ const { setSuggestionActions } = useContext(MetabotContext);++ useEffect(() => {+ setSuggestionActions(actions ?? null);+ return () => setSuggestionActions(null);+ }, [actions, setSuggestionActions]);+};export const MetabotProvider = ({children,@@ -53,6 +109,8 @@ export const MetabotProvider = ({/* Metabot input */const [prompt, setPrompt] = useState("");const promptInputRef = useRef<MetabotPromptInputRef>(null);+ const [suggestionActions, setSuggestionActions] =+ useState<MetabotSuggestionActions | null>(null);/* Metabot context */const providerFnsRef = useRef<Set<ChatContextProviderFn>>(new Set());@@ -107,6 +165,8 @@ export const MetabotProvider = ({promptInputRef,getChatContext,registerChatContextProvider,+ suggestionActions,+ setSuggestionActions,}}>{children}diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-reactions.ts b/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-reactions.tsindex 7479385fe96..6ce815d2b2c 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-reactions.ts+++ b/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-reactions.ts@@ -14,7 +14,7 @@ export const useMetabotReactions = () => {const navigateToPath = useMetabotSelector(getNavigateToPath);const setNavigateToPath = useCallback(- async (navigateToPath: string) => {+ async (navigateToPath: string | null) => {dispatch(setNavigateToPathAction(navigateToPath));},[dispatch],diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/index.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/index.tsxindex 4abd1271a57..c3c8177f961 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/index.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/index.tsx@@ -1,5 +1,6 @@import { Route } from "react-router";+import type { MetabotContext as MetabotContextType } from "metabase/metabot";import { PLUGIN_METABOT, PLUGIN_REDUCERS } from "metabase/plugins";import { useLazyMetabotGenerateContentQuery } from "metabase-enterprise/api";import { getAdminRoutes as getAdminUpsellRoutes } from "metabase-enterprise/metabot/components/MetabotAdmin/MetabotPurchasePage";@@ -9,6 +10,7 @@ import { hasPremiumFeature } from "metabase-enterprise/settings";import { Metabot } from "./components/Metabot";import { getAdminRoutes } from "./components/MetabotAdmin/MetabotAdminPage";import { MetabotAppBarButton } from "./components/MetabotAppBarButton";+import { MetabotChat } from "./components/MetabotChat";import MetabotThinkingStyles from "./components/MetabotChat/MetabotThinking.module.css";import { MetabotDataStudioButton } from "./components/MetabotDataStudioButton";import { MetabotQueryBuilder } from "./components/MetabotQueryBuilder";@@ -26,7 +28,8 @@ import { getMetabotVisible, metabotReducer } from "./state";*/PLUGIN_METABOT.getMetabotProvider = () => MetabotProvider;PLUGIN_METABOT.defaultMetabotContextValue = defaultContext;-PLUGIN_METABOT.MetabotContext = MetabotContext;+PLUGIN_METABOT.MetabotContext =+ MetabotContext as React.Context<MetabotContextType>;PLUGIN_REDUCERS.metabotPlugin = metabotReducer;@@ -48,6 +51,7 @@ export function initializePlugin() {),// componentsMetabot,+ MetabotChat,MetabotAppBarButton,MetabotDataStudioButton,MetabotDataStudioSidebar,diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/state/actions.ts b/enterprise/frontend/src/metabase-enterprise/metabot/state/actions.tsindex e9a1e2eb245..d6edc5f276d 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/state/actions.ts+++ b/enterprise/frontend/src/metabase-enterprise/metabot/state/actions.ts@@ -10,6 +10,7 @@ import _ from "underscore";import { addUndo } from "metabase/redux/undo";import { getIsEmbedding } from "metabase/selectors/embed";+import { getIsWorkspace } from "metabase/selectors/routing";import { getUser } from "metabase/selectors/user";import {type JSONValue,@@ -73,6 +74,7 @@ export const {addSuggestedTransform,activateSuggestedTransform,deactivateSuggestedTransform,+ updateSuggestedTransformId,createAgent,destroyAgent,addSuggestedCodeEdit,@@ -301,6 +303,7 @@ export const sendAgentRequest = createAsyncThunk<{ dispatch, getState, signal, rejectWithValue, fulfillWithValue },) => {const isEmbedding = getIsEmbedding(getState());+ const isWorkspace = getIsWorkspace(getState());const { agentId, ...request } = payload;try {@@ -338,7 +341,7 @@ export const sendAgentRequest = createAsyncThunk<.with({ type: "navigate_to" }, (part) => {dispatch(setNavigateToPath(part.value));- if (!isEmbedding) {+ if (!isEmbedding && !isWorkspace) {dispatch(push(part.value) as UnknownAction);}})diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/state/reducer.ts b/enterprise/frontend/src/metabase-enterprise/metabot/state/reducer.tsindex ba12bba3466..a02b21972e4 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/state/reducer.ts+++ b/enterprise/frontend/src/metabase-enterprise/metabot/state/reducer.ts@@ -3,7 +3,12 @@ import { castDraft } from "immer";import _ from "underscore";import { logout } from "metabase/auth/actions";-import type { MetabotCodeEdit, SuggestedTransform } from "metabase-types/api";+import { uuid } from "metabase/lib/uuid";+import type {+ MetabotCodeEdit,+ MetabotHistory,+ SuggestedTransform,+} from "metabase-types/api";import { TOOL_CALL_MESSAGES } from "../constants";@@ -18,8 +23,10 @@ import {} from "./reducer-utils";import type {MetabotAgentChatMessage,+ MetabotChatMessage,MetabotErrorMessage,MetabotSuggestedTransform,+ MetabotToolCall,MetabotUserChatMessage,} from "./types";import { createMessageId } from "./utils";@@ -204,7 +211,7 @@ export const metabot = createSlice({},),// REACTIONS REDUCERS- setNavigateToPath: (state, action: PayloadAction<string>) => {+ setNavigateToPath: (state, action: PayloadAction<string | null>) => {state.reactions.navigateToPath = action.payload;},addSuggestedTransform: (@@ -246,6 +253,21 @@ export const metabot = createSlice({}});},+ updateSuggestedTransformId: (+ state,+ action: PayloadAction<{+ suggestionId: string;+ newId: number | undefined;+ }>,+ ) => {+ const { suggestionId, newId } = action.payload;+ const transform = state.reactions.suggestedTransforms.find(+ (t) => t.suggestionId === suggestionId,+ );+ if (transform) {+ transform.id = newId;+ }+ },addSuggestedCodeEdit: (state,{ payload: codeEdit }: PayloadAction<MetabotCodeEdit>,@@ -258,6 +280,43 @@ export const metabot = createSlice({) => {delete state.reactions.suggestedCodeEdits[action.payload];},+ setConversationSnapshot: (+ state,+ action: PayloadAction<{+ messages: MetabotChatMessage[];+ history: MetabotHistory;+ state: any;+ suggestedTransforms: MetabotSuggestedTransform[];+ activeToolCalls: MetabotToolCall[];+ errorMessages: MetabotErrorMessage[];+ conversationId: string;+ }>,+ ) => {+ const convo = state.conversations["omnibot"];+ if (!convo) {+ return;+ }++ const {+ messages,+ history,+ state: snapshotState,+ suggestedTransforms,+ activeToolCalls,+ errorMessages,+ conversationId,+ } = action.payload;++ convo.messages = castDraft(messages ?? []);+ convo.history = history ?? [];+ convo.state = snapshotState ?? {};+ convo.activeToolCalls = activeToolCalls ?? [];+ convo.errorMessages = errorMessages ?? [];+ convo.conversationId = conversationId ?? uuid();+ convo.isProcessing = false;++ state.reactions.suggestedTransforms = (suggestedTransforms ?? []) as any;+ },},extraReducers: (builder) => {builder@@ -317,3 +376,4 @@ export const metabot = createSlice({});export const metabotReducer = metabot.reducer;+export const metabotActions = metabot.actions;diff --git a/enterprise/frontend/src/metabase-enterprise/plugins.js b/enterprise/frontend/src/metabase-enterprise/plugins.jsindex 8d2a88c474b..955111e3e72 100644--- a/enterprise/frontend/src/metabase-enterprise/plugins.js+++ b/enterprise/frontend/src/metabase-enterprise/plugins.js@@ -21,6 +21,7 @@ import { initializePlugin as initializeCollections } from "./collections";import { initializePlugin as initializeContentTranslation } from "./content_translation";import { initializePlugin as initializeContentVerification } from "./content_verification";import { initializePlugin as initializeDataStudio } from "./data-studio";+import { initializePlugin as initializeWorkspaces } from "./data-studio/workspaces";import { initializePlugin as initializeDatabaseReplication } from "./database_replication";import { initializePlugin as initializeDatabaseRouting } from "./database_routing";import { initializePlugin as initializeDependencies } from "./dependencies";@@ -95,4 +96,5 @@ export function initializePlugins() {initializeTransformsPython();initializeSupport();initializeTenants?.();+ initializeWorkspaces();}diff --git a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonDataPicker/PythonDataPicker.tsx b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonDataPicker/PythonDataPicker.tsxindex e57f9ed1d4d..5b252faeda2 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonDataPicker/PythonDataPicker.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonDataPicker/PythonDataPicker.tsx@@ -23,6 +23,7 @@ import {type PythonDataPickerProps = {database?: DatabaseId;+ disabled?: boolean;tables: PythonTransformTableAliases;readOnly?: boolean;onChange: (@@ -34,6 +35,7 @@ type PythonDataPickerProps = {export function PythonDataPicker({database,+ disabled,tables,readOnly,onChange,@@ -118,31 +120,35 @@ export function PythonDataPicker({className={S.dataPicker}data-testid="python-data-picker">- <Box>- <Text fw="bold">{t`Pick tables and alias them`}</Text>- <Text size="sm" c="text-tertiary" mb="sm">- {t`Select tables to use as data sources and provide aliases that can be referenced in your Python script.`}- </Text>- <Stack gap="md">- {tableSelections.map((selection, index) => (- <SelectionInput- key={index}- selection={selection}- database={database}- tables={tables}- usedAliases={usedAliases}- availableTables={availableTables}- onChange={(selection) => handleSelectionChange(index, selection)}- onRemove={() => handleRemoveTable(index)}- disabled={isLoadingTables || readOnly}+ {database && (+ <Box>+ <Text fw="bold">{t`Pick tables and alias them`}</Text>+ <Text size="sm" c="text-tertiary" mb="sm">+ {t`Select tables to use as data sources and provide aliases that can be referenced in your Python script.`}+ </Text>+ <Stack gap="md">+ {tableSelections.map((selection, index) => (+ <SelectionInput+ key={index}+ selection={selection}+ database={database}+ tables={tables}+ usedAliases={usedAliases}+ availableTables={availableTables}+ onChange={(selection) =>+ handleSelectionChange(index, selection)+ }+ onRemove={() => handleRemoveTable(index)}+ disabled={disabled || isLoadingTables || readOnly}+ />+ ))}+ <AddTableButton+ onClick={handleAddTable}+ disabled={availableTables.length === 0 || readOnly}/>- ))}- <AddTableButton- onClick={handleAddTable}- disabled={availableTables.length === 0 || readOnly}- />- </Stack>- </Box>+ </Stack>+ </Box>+ )}</Stack>);}diff --git a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonEditorBody/PythonEditorBody.tsx b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonEditorBody/PythonEditorBody.tsxindex 9bad6bc81ff..3367eaede98 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonEditorBody/PythonEditorBody.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonEditorBody/PythonEditorBody.tsx@@ -17,10 +17,12 @@ import { ResizableBoxHandle } from "./ResizableBoxHandle";import { createPythonImportTokenLocator } from "./utils";type PythonEditorBodyProps = {+ disabled?: boolean;source: string;proposedSource?: string;isRunnable: boolean;isEditMode?: boolean;+ hideRunButton?: boolean;onChange: (source: string) => void;onRun?: () => void;onCancel?: () => void;@@ -37,11 +39,13 @@ const HEADER_HEIGHT = 65 + 50; // Top bar height + transform header heightconst PREVIEW_MAX_INITIAL_HEIGHT = 192;export function PythonEditorBody({+ disabled,source,proposedSource,onChange,isRunnable,isEditMode,+ hideRunButton,onRun,onCancel,isRunning,@@ -51,7 +55,8 @@ export function PythonEditorBody({onRejectProposed,}: PythonEditorBodyProps) {const [isResizing, setIsResizing] = useState(false);- const editorHeight = useInitialEditorHeight(isEditMode);+ const showResizeHandle = isEditMode && withDebugger;+ const editorHeight = useInitialEditorHeight(isEditMode, showResizeHandle);const dispatch = useDispatch();const navigateToCommonLibrary = useCallback(@@ -80,57 +85,49 @@ export function PythonEditorBody({[navigateToCommonLibrary],);- return (- <ResizableBox- axis="y"- height={editorHeight}- handle={<ResizableBoxHandle />}- resizeHandles={!isEditMode || !withDebugger ? [] : ["s"]}- style={isResizing ? undefined : { transition: "height 0.25s" }}- onResizeStart={() => setIsResizing(true)}- onResizeStop={() => setIsResizing(false)}- >- <Flex h="100%" align="end" bg="background-secondary" pos="relative">- <PythonEditor- value={source}- proposedValue={proposedSource}- onChange={onChange}- withPandasCompletions- readOnly={!isEditMode}- extensions={[clickableTokensExtension]}- data-testid="python-editor"- />-- {isEditMode && (- <Stack m="1rem" gap="md" mt="auto">- {proposedSource && onRejectProposed && onAcceptProposed && (- <>- <Tooltip label={t`Accept proposed changes`} position="left">- <Button- data-testid="accept-proposed-changes-button"- variant="filled"- bg="success"- px="0"- w="2.5rem"- onClick={onAcceptProposed}- >- <Icon name="check" />- </Button>- </Tooltip>- <Tooltip label={t`Reject proposed changes`} position="left">- <Button- data-testid="reject-proposed-changes-button"- w="2.5rem"- px="0"- variant="filled"- bg="danger"- onClick={onRejectProposed}- >- <Icon name="close" />- </Button>- </Tooltip>- </>- )}+ const editorContent = (+ <Flex h="100%" align="end" bg="background-secondary" pos="relative">+ <PythonEditor+ value={source}+ proposedValue={proposedSource}+ onChange={onChange}+ withPandasCompletions+ readOnly={!isEditMode || disabled}+ extensions={[clickableTokensExtension]}+ data-testid="python-editor"+ />++ {isEditMode && (+ <Stack m="1rem" gap="md" mt="auto">+ {proposedSource && onRejectProposed && onAcceptProposed && (+ <>+ <Tooltip label={t`Accept proposed changes`} position="left">+ <Button+ data-testid="accept-proposed-changes-button"+ variant="filled"+ bg="success"+ px="0"+ w="2.5rem"+ onClick={onAcceptProposed}+ >+ <Icon name="check" />+ </Button>+ </Tooltip>+ <Tooltip label={t`Reject proposed changes`} position="left">+ <Button+ data-testid="reject-proposed-changes-button"+ w="2.5rem"+ px="0"+ variant="filled"+ bg="danger"+ onClick={onRejectProposed}+ >+ <Icon name="close" />+ </Button>+ </Tooltip>+ </>+ )}+ {!hideRunButton && (<RunButtonWithTooltipdisabled={!isRunnable}isRunning={isRunning}@@ -139,14 +136,39 @@ export function PythonEditorBody({onCancel={onCancel}getTooltip={() => t`Run Python script`}/>- </Stack>- )}- </Flex>- </ResizableBox>+ )}+ </Stack>+ )}+ </Flex>+ );++ if (showResizeHandle) {+ return (+ <ResizableBox+ axis="y"+ height={editorHeight}+ handle={<ResizableBoxHandle />}+ resizeHandles={["s"]}+ style={isResizing ? undefined : { transition: "height 0.25s" }}+ onResizeStart={() => setIsResizing(true)}+ onResizeStop={() => setIsResizing(false)}+ >+ {editorContent}+ </ResizableBox>+ );+ }++ return (+ <Flex h="100%" direction="column">+ {editorContent}+ </Flex>);}-function useInitialEditorHeight(isEditMode?: boolean) {+function useInitialEditorHeight(+ isEditMode?: boolean,+ showResizeHandle?: boolean,+) {const { height: windowHeight } = useWindowSize();const availableHeight = windowHeight - HEADER_HEIGHT;@@ -155,6 +177,11 @@ function useInitialEditorHeight(isEditMode?: boolean) {return availableHeight;}+ if (!showResizeHandle) {+ // No preview panel (e.g. workspace) – height is not used; container uses 100%+ return availableHeight;+ }+// Let's make the preview initial height be half of the available height at mostconst previewInitialHeight = Math.min(availableHeight / 2,diff --git a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformEditor.tsx b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformEditor.tsxindex 8ee834f919b..3a5ae7be40e 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformEditor.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformEditor.tsx@@ -1,4 +1,6 @@import { useHotkeys } from "@mantine/hooks";+import { useEffect } from "react";+import { usePrevious } from "react-use";import type { PythonTransformEditorProps } from "metabase/plugins";import { Flex, Stack } from "metabase/ui";@@ -21,16 +23,20 @@ import { updateTransformSignature } from "./utils";export function PythonTransformEditor({source,proposedSource,+ uiOptions,isEditMode,- readOnly,- transformId,+ transform,onChangeSource,onAcceptProposed,onRejectProposed,+ onRunTransform,+ onRun,}: PythonTransformEditorProps) {const { isRunning, cancel, run, executionResult, isDirty } =useTestPythonTransform(source);+ const wasRunning = usePrevious(isRunning);+const handleScriptChange = (body: string) => {const newSource = {...source,@@ -50,7 +56,7 @@ export function PythonTransformEditor({};const handleDataChange = (- database: number,+ database: DatabaseId,sourceTables: PythonTransformTableAliases,tableInfo: Table[],) => {@@ -69,14 +75,46 @@ export function PythonTransformEditor({onChangeSource(newSource);};+ const handleRun = () => {+ // Use custom onRun handler if provided (workspace dry-run), otherwise use internal test-run+ if (onRun) {+ onRun();+ } else {+ run();+ }+ };++ // Notify workspace when test-run completes in workspace context+ useEffect(() => {+ const runJustCompleted = wasRunning && !isRunning;+ if (+ runJustCompleted &&+ executionResult &&+ onRunTransform &&+ uiOptions?.hidePreview+ ) {+ onRunTransform(executionResult);+ }+ }, [+ wasRunning,+ isRunning,+ executionResult,+ onRunTransform,+ uiOptions?.hidePreview,+ ]);+const handleCmdEnter = () => {if (!isEditMode) {return;}+ // In workspaces, disable run shortcut when transform has unsaved changes (hideRunButton)+ // if (uiOptions?.hideRunButton) {+ // return;+ // }if (isRunning) {cancel();} else if (isPythonTransformSource(source)) {- run();+ handleRun();}};@@ -87,13 +125,15 @@ export function PythonTransformEditor({<PythonTransformTopBardatabaseId={source["source-database"]}isEditMode={isEditMode}- readOnly={readOnly}- transformId={transformId}+ readOnly={uiOptions?.readOnly}+ transform={transform}onDatabaseChange={handleDatabaseChange}+ canChangeDatabase={uiOptions?.canChangeDatabase}/><Flex className={S.editorBodyWrapper}>{isEditMode && (<PythonDataPicker+ disabled={uiOptions?.readOnly}database={source["source-database"]}tables={source["source-tables"]}onChange={handleDataChange}@@ -101,20 +141,22 @@ export function PythonTransformEditor({)}<Stack w="100%" h="100%" gap={0}><PythonEditorBody+ disabled={uiOptions?.readOnly}isRunnable={isPythonTransformSource(source)}isRunning={isRunning}isDirty={isDirty}isEditMode={isEditMode}- onRun={run}+ hideRunButton={uiOptions?.hideRunButton}+ onRun={handleRun}onCancel={cancel}source={source.body}proposedSource={proposedSource?.body}onChange={handleScriptChange}- withDebugger={isEditMode}+ withDebugger={isEditMode && !uiOptions?.hidePreview}onAcceptProposed={onAcceptProposed}onRejectProposed={onRejectProposed}/>- {isEditMode && (+ {!uiOptions?.hidePreview && isEditMode && (<PythonEditorResultsisRunning={isRunning}executionResult={executionResult}diff --git a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformEditor.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformEditor.unit.spec.tsxindex ef400157991..37783fac8c9 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformEditor.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformEditor.unit.spec.tsx@@ -2,44 +2,85 @@ import {setupDatabasesEndpoints,setupSearchEndpoints,setupTablesEndpoints,+ setupWorkspaceCheckoutEndpoint,+ setupWorkspacesEndpoint,} from "__support__/server-mocks";import { renderWithProviders, screen } from "__support__/ui";-import type { PythonTransformSourceDraft } from "metabase-types/api";-import { createMockDatabase, createMockTable } from "metabase-types/api/mocks";+import type { PythonTransformEditorUiOptions } from "metabase/plugins/oss/transforms";+import { hasPremiumFeature } from "metabase-enterprise/settings";+import type { PythonTransformSourceDraft, Transform } from "metabase-types/api";+import {+ createMockDatabase,+ createMockTable,+ createMockTransform,+} from "metabase-types/api/mocks";import { createMockState } from "metabase-types/store/mocks";import { PythonTransformEditor } from "./PythonTransformEditor";-const mockDatabase = createMockDatabase({ id: 1, name: "Test Database" });-const mockTable = createMockTable({ id: 1, db_id: 1, name: "Test Table" });+jest.mock("metabase-enterprise/settings", () => ({+ hasPremiumFeature: jest.fn(),+}));++const mockHasPremiumFeature = hasPremiumFeature as jest.MockedFunction<+ typeof hasPremiumFeature+>;++const DATABASE_ID = 1;+const mockDatabase = createMockDatabase({+ id: DATABASE_ID,+ name: "Test Database",+});+const mockTable = createMockTable({+ id: 1,+ db_id: DATABASE_ID,+ name: "Test Table",+});const mockPythonSource: PythonTransformSourceDraft = {type: "python",body: "# Python script\nprint('Hello, world!')",- "source-database": 1,+ "source-database": DATABASE_ID,"source-tables": {},};+const mockPythonTransform = createMockTransform({+ id: 1,+ name: "Test Python Transform",+ source_type: "python",+ source: {+ type: "python",+ body: "def transform(): pass",+ "source-database": DATABASE_ID,+ "source-tables": {},+ },+});+type SetupOpts = {source?: PythonTransformSourceDraft;isEditMode?: boolean;- transformId?: number;+ transform?: Transform;+ uiOptions?: PythonTransformEditorUiOptions;};function setup({source = mockPythonSource,isEditMode = true,- transformId = 1,+ transform,+ uiOptions,}: SetupOpts = {}) {setupDatabasesEndpoints([mockDatabase]);setupTablesEndpoints([mockTable]);setupSearchEndpoints([]);+ setupWorkspacesEndpoint([]);+ setupWorkspaceCheckoutEndpoint({});renderWithProviders(<PythonTransformEditorsource={source}isEditMode={isEditMode}- transformId={transformId}+ transform={transform}+ uiOptions={uiOptions}onChangeSource={jest.fn()}onAcceptProposed={jest.fn()}onRejectProposed={jest.fn()}@@ -51,6 +92,10 @@ function setup({}describe("PythonTransformEditor", () => {+ beforeEach(() => {+ mockHasPremiumFeature.mockReturnValue(false);+ });+describe("view mode (not editing)", () => {it("should not render the data picker sidebar when not in edit mode", () => {setup({ isEditMode: false });@@ -72,7 +117,7 @@ describe("PythonTransformEditor", () => {});it("should render EditDefinitionButton when not in edit mode", () => {- setup({ isEditMode: false, transformId: 1 });+ setup({ isEditMode: false, transform: mockPythonTransform });expect(screen.getByText(/edit definition/i)).toBeInTheDocument();});});@@ -102,4 +147,21 @@ describe("PythonTransformEditor", () => {expect(screen.queryByText(/edit definition/i)).not.toBeInTheDocument();});});++ describe("hideRunButton option", () => {+ it("should render run button when hideRunButton is not set", () => {+ setup({ isEditMode: true });+ expect(screen.getByTestId("run-button")).toBeInTheDocument();+ });++ it("should render run button when hideRunButton is false", () => {+ setup({ isEditMode: true, uiOptions: { hideRunButton: false } });+ expect(screen.getByTestId("run-button")).toBeInTheDocument();+ });++ it("should not render run button when hideRunButton is true", () => {+ setup({ isEditMode: true, uiOptions: { hideRunButton: true } });+ expect(screen.queryByTestId("run-button")).not.toBeInTheDocument();+ });+ });});diff --git a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformTopBar/PythonTransformTopBar.tsx b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformTopBar/PythonTransformTopBar.tsxindex 02738be31db..dfb99d0e772 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformTopBar/PythonTransformTopBar.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformTopBar/PythonTransformTopBar.tsx@@ -8,9 +8,11 @@ import { useSelector } from "metabase/lib/redux";import { DatabaseDataSelector } from "metabase/query_builder/components/DataSelector";import { Flex } from "metabase/ui";import { getIsRemoteSyncReadOnly } from "metabase-enterprise/remote_sync/selectors";+import { hasPremiumFeature } from "metabase-enterprise/settings";import { EditDefinitionButton } from "metabase-enterprise/transforms/components/TransformEditor/EditDefinitionButton";+import { EditTransformMenu } from "metabase-enterprise/transforms/components/TransformHeader/EditTransformMenu";import { doesDatabaseSupportTransforms } from "metabase-enterprise/transforms/utils";-import type { Database, DatabaseId, TransformId } from "metabase-types/api";+import type { Database, DatabaseId, Transform } from "metabase-types/api";import S from "./PythonTransformTopBar.module.css";@@ -18,20 +20,22 @@ type PythonTransformTopBarProps = {databaseId?: DatabaseId;isEditMode?: boolean;readOnly?: boolean;- transformId?: TransformId;+ transform?: Transform;onDatabaseChange?: (databaseId: DatabaseId) => void;+ canChangeDatabase?: boolean;};export function PythonTransformTopBar({databaseId,isEditMode,readOnly,- transformId,+ transform,onDatabaseChange,+ canChangeDatabase = true,}: PythonTransformTopBarProps) {const isRemoteSyncReadOnly = useSelector(getIsRemoteSyncReadOnly);- const showEditDefinitionButton =- !isEditMode && transformId && !isRemoteSyncReadOnly && !readOnly;+ const showEditButton =+ !isEditMode && transform && !isRemoteSyncReadOnly && !readOnly;const { data: database } = useGetDatabaseQuery(databaseId != null ? { id: databaseId } : skipToken,@@ -52,7 +56,7 @@ export function PythonTransformTopBar({data-testid="python-transform-top-bar"className={S.TopBar}>- {isEditMode ? (+ {isEditMode && canChangeDatabase ? (<Flex h="3rem" ml="sm" align="center" data-testid="selected-database"><DatabaseDataSelectorclassName={S.databaseSelector}@@ -77,16 +81,20 @@ export function PythonTransformTopBar({{database?.name}</Flex>)}- {showEditDefinitionButton && (+ {showEditButton && (<Flex ml="auto" mr="lg" align="center" h="3rem">- <EditDefinitionButton- bg="transparent"- fz="sm"- h="1.5rem"- px="sm"- size="xs"- transformId={transformId}- />+ {hasPremiumFeature("workspaces") ? (+ <EditTransformMenu transform={transform} />+ ) : (+ <EditDefinitionButton+ bg="transparent"+ fz="sm"+ h="1.5rem"+ px="sm"+ size="xs"+ transformId={transform.id}+ />+ )}</Flex>)}</Flex>diff --git a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformTopBar/PythonTransformTopBar.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformTopBar/PythonTransformTopBar.unit.spec.tsxindex 50f905104b0..e2474b04280 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformTopBar/PythonTransformTopBar.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms-python/components/PythonTransformEditor/PythonTransformTopBar/PythonTransformTopBar.unit.spec.tsx@@ -1,32 +1,66 @@+import userEvent from "@testing-library/user-event";import { Route } from "react-router";import {setupDatabasesEndpoints,setupSearchEndpoints,+ setupWorkspaceCheckoutEndpoint,+ setupWorkspacesEndpoint,} from "__support__/server-mocks";-import { renderWithProviders, screen } from "__support__/ui";-import { createMockDatabase } from "metabase-types/api/mocks";+import { renderWithProviders, screen, waitFor } from "__support__/ui";+import { hasPremiumFeature } from "metabase-enterprise/settings";+import type { Transform } from "metabase-types/api";+import {+ createMockDatabase,+ createMockTransform,+} from "metabase-types/api/mocks";import { createMockState } from "metabase-types/store/mocks";import { PythonTransformTopBar } from "./PythonTransformTopBar";-const mockDatabase = createMockDatabase({ id: 1, name: "Test Database" });+jest.mock("metabase-enterprise/settings", () => ({+ hasPremiumFeature: jest.fn(),+}));++const mockHasPremiumFeature = hasPremiumFeature as jest.MockedFunction<+ typeof hasPremiumFeature+>;++const DATABASE_ID = 1;+const mockDatabase = createMockDatabase({+ id: DATABASE_ID,+ name: "Test Database",+});++const mockPythonTransform = createMockTransform({+ id: 1,+ name: "Test Python Transform",+ source_type: "python",+ source: {+ type: "python",+ body: "def transform(): pass",+ "source-database": DATABASE_ID,+ "source-tables": {},+ },+});type SetupOpts = {databaseId?: number;isEditMode?: boolean;- transformId?: number;+ transform?: Transform;};function setup({- databaseId = 1,+ databaseId = DATABASE_ID,isEditMode = true,- transformId = 1,+ transform,}: SetupOpts = {}) {const onDatabaseChange = jest.fn();setupDatabasesEndpoints([mockDatabase]);setupSearchEndpoints([]);+ setupWorkspacesEndpoint([]);+ setupWorkspaceCheckoutEndpoint({});renderWithProviders(<Route@@ -34,7 +68,7 @@ function setup({<PythonTransformTopBardatabaseId={databaseId}isEditMode={isEditMode}- transformId={transformId}+ transform={transform}onDatabaseChange={onDatabaseChange}/>)}@@ -51,27 +85,62 @@ function setup({}describe("PythonTransformTopBar", () => {+ beforeEach(() => {+ mockHasPremiumFeature.mockReturnValue(false);+ });+describe("view mode (not editing)", () => {- it("should render EditDefinitionButton when not in edit mode", () => {- setup({ isEditMode: false, transformId: 1 });+ it("should render EditDefinitionButton when not in edit mode and workspaces feature is disabled", () => {+ mockHasPremiumFeature.mockReturnValue(false);+ setup({ isEditMode: false, transform: mockPythonTransform });expect(screen.getByRole("link", { name: /edit definition/i }),).toBeInTheDocument();});+ it("should render EditTransformMenu when not in edit mode and workspaces feature is enabled", async () => {+ mockHasPremiumFeature.mockImplementation(+ (feature) => feature === "workspaces",+ );+ setup({ isEditMode: false, transform: mockPythonTransform });++ const editButton = await screen.findByRole("button", { name: /Edit/i });+ await waitFor(() => {+ expect(editButton).not.toHaveAttribute("data-loading", "true");+ });++ await userEvent.click(editButton);++ expect(screen.getByText("Edit definition")).toBeInTheDocument();+ expect(screen.getByText("Add to workspace")).toBeInTheDocument();+ });++ it("should not render edit button when transform is not provided", () => {+ setup({ isEditMode: false, transform: undefined });+ expect(+ screen.queryByRole("link", { name: /edit definition/i }),+ ).not.toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: /Edit/i }),+ ).not.toBeInTheDocument();+ });+it("should display database name as static text when not in edit mode", async () => {- setup({ isEditMode: false, databaseId: 1 });+ setup({ isEditMode: false, databaseId: DATABASE_ID });// When not in edit mode, database name is displayed as text, not a dropdownexpect(await screen.findByText("Test Database")).toBeInTheDocument();});});describe("edit mode", () => {- it("should not render EditDefinitionButton in edit mode", () => {- setup({ isEditMode: true });+ it("should not render edit button in edit mode", () => {+ setup({ isEditMode: true, transform: mockPythonTransform });expect(screen.queryByRole("link", { name: /edit definition/i }),).not.toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: /Edit/i }),+ ).not.toBeInTheDocument();});it("should render database selector in edit mode", () => {diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/RunButton/RunButton.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/components/RunButton/RunButton.tsxindex c318f6ff6ca..7a5d8ec37c2 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/components/RunButton/RunButton.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/RunButton/RunButton.tsx@@ -8,7 +8,7 @@ import {import { t } from "ttag";import type { ColorName } from "metabase/lib/colors/types";-import { Button, Icon, Loader, Tooltip } from "metabase/ui";+import { Button, type ButtonProps, Icon, Loader, Tooltip } from "metabase/ui";import type {TransformId,TransformJobId,@@ -22,6 +22,7 @@ type RunButtonProps = {run: TransformRun | null | undefined;isDisabled?: boolean;allowCancellation?: boolean;+ size?: ButtonProps["size"];onRun: () => void;onCancel?: () => void;};@@ -32,6 +33,7 @@ export const RunButton = forwardRef(function RunButton(run,isDisabled: isExternallyDisabled = false,allowCancellation = false,+ size = "md",onRun,onCancel,}: RunButtonProps,@@ -63,6 +65,7 @@ export const RunButton = forwardRef(function RunButton(leftSection={leftSection}disabled={isDisabled}data-testid="run-button"+ size={size}onClick={onRun}>{label}diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/RunCancelButton/RunCancelButton.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/components/RunCancelButton/RunCancelButton.tsxnew file mode 100644index 00000000000..7c3a7e9bbdc--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/RunCancelButton/RunCancelButton.tsx@@ -0,0 +1,82 @@+import { useDisclosure } from "@mantine/hooks";+import { t } from "ttag";++import { ConfirmModal } from "metabase/common/components/ConfirmModal";+import { isResourceNotFoundError } from "metabase/lib/errors";+import { useMetadataToasts } from "metabase/metadata/hooks";+import { ActionIcon, Icon, Tooltip } from "metabase/ui";+import {+ useCancelCurrentTransformRunMutation,+ useLazyGetTransformQuery,+} from "metabase-enterprise/api";+import type { Transform, TransformRunStatus } from "metabase-types/api";++export function RunCancelButton({+ status,+ transform,+}: {+ transform?: Transform;+ status: TransformRunStatus | null;+}) {+ const isRunning = status === "started";+ const [+ isConfirmCancelationModalOpen,+ { open: openCancelConfirmationModal, close: closeCancelConfirmationModal },+ ] = useDisclosure();+ const { sendErrorToast } = useMetadataToasts();+ const [fetchTransform, { isFetching }] = useLazyGetTransformQuery();+ const [cancelTransformRun, { isLoading: isCanceling }] =+ useCancelCurrentTransformRunMutation();++ const handleCancel = async () => {+ if (transform == null) {+ return;+ }+ const { error } = await cancelTransformRun(transform.id);+ closeCancelConfirmationModal();+ if (error && !isResourceNotFoundError(error)) {+ sendErrorToast(t`Failed to cancel transform`);+ } else {+ // fetch the transform to get the correct `last_run` info+ fetchTransform(transform.id);+ }+ return { error };+ };++ return (+ <>+ {isRunning && (+ <Tooltip label={t`Cancel`} position="bottom">+ <ActionIcon+ aria-label={t`Cancel run`}+ onClickCapture={(evt) => {+ evt.stopPropagation();+ openCancelConfirmationModal();+ }}+ disabled={isCanceling || isFetching}+ p={0}+ m={0}+ size="xs"+ >+ <Icon name="close" size={12} />+ </ActionIcon>+ </Tooltip>+ )}+ <ConfirmModal+ onClick={(evt) => {+ // Prevent click events from within the modal from bubbling up out of it.+ // This is needed because the model is rendered within the table row and the table+ // row has a click handler that navigates to the transform page.+ evt.stopPropagation();+ }}+ title={t`Cancel this run?`}+ opened={isConfirmCancelationModalOpen}+ onClose={closeCancelConfirmationModal}+ onConfirm={() => {+ handleCancel();+ }}+ closeButtonText={t`No`}+ />+ </>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/RunStatus/RunStatus.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/components/RunStatus/RunStatus.tsxindex e15cd5a68ad..4568b3c0e50 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/components/RunStatus/RunStatus.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/RunStatus/RunStatus.tsx@@ -37,7 +37,7 @@ export function RunStatus({const endTimeText = endTime != null ? endTime.fromNow() : null;const errorInfo =- message != null ? (+ message != null && status != null ? (<RunInfostatus={status}message={message}diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/RunStatusInfo/RunStatusInfo.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/components/RunStatusInfo/RunStatusInfo.tsxnew file mode 100644index 00000000000..f635c9ba320--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/RunStatusInfo/RunStatusInfo.tsx@@ -0,0 +1,34 @@+import { Box, Group } from "metabase/ui";+import type { Transform, TransformRunStatus } from "metabase-types/api";++import { formatStatus } from "../../utils";+import { RunCancelButton } from "../RunCancelButton/RunCancelButton";+import { RunInfo } from "../RunInfo";++import S from "./RunStatusInfo.module.css";++type RunStatusInfoProps = {+ transform?: Transform;+ status: TransformRunStatus | null;+ message: string | null;+ endTime: Date | null;+};++export function RunStatusInfo({+ transform,+ status,+ message,+ endTime,+}: RunStatusInfoProps) {+ const isError = status === "failed" || status === "timeout";++ return (+ <Group gap="xs" className={S.runStatusInfo} wrap="nowrap">+ <Box c={isError ? "error" : undefined}>{formatStatus(status)}</Box>+ {isError && message != null && (+ <RunInfo status={status} message={message} endTime={endTime} />+ )}+ <RunCancelButton transform={transform} status={status} />+ </Group>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformEditor/EditDefinitionButton.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformEditor/EditDefinitionButton.tsxindex ac4597271e7..44ffe025709 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformEditor/EditDefinitionButton.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformEditor/EditDefinitionButton.tsx@@ -16,6 +16,7 @@ export const EditDefinitionButton = ({return (<Buttoncomponent={Link}+ data-testid="edit-definition-button"style={{ flexShrink: 0 }}to={transformEdit(transformId)}{...buttonProps}diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformEditor/TransformEditor.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformEditor/TransformEditor.tsxindex be0c64e7ff8..45270a26dad 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformEditor/TransformEditor.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformEditor/TransformEditor.tsx@@ -3,33 +3,39 @@ import { useMemo } from "react";import { useSelector } from "metabase/lib/redux";import {QueryEditor,+ type QueryEditorUiOptions,type QueryEditorUiState,} from "metabase/querying/editor/components/QueryEditor";import { getMetadata } from "metabase/selectors/metadata";import { getIsRemoteSyncReadOnly } from "metabase-enterprise/remote_sync/selectors";+import { hasPremiumFeature } from "metabase-enterprise/settings";+import { EditDefinitionButton } from "metabase-enterprise/transforms/components/TransformEditor/EditDefinitionButton";+import { EditTransformMenu } from "metabase-enterprise/transforms/components/TransformHeader/EditTransformMenu";import * as Lib from "metabase-lib";import type {Database,+ DatasetQuery,QueryTransformSource,- TransformId,+ Transform,} from "metabase-types/api";-import { EditDefinitionButton } from "./EditDefinitionButton";import { getEditorOptions } from "./utils";-type TransformEditorProps = {+export type TransformEditorProps = {source: QueryTransformSource;uiState: QueryEditorUiState;+ uiOptions?: QueryEditorUiOptions;proposedSource: QueryTransformSource | undefined;databases: Database[];onChangeSource: (source: QueryTransformSource) => void;onChangeUiState: (state: QueryEditorUiState) => void;onAcceptProposed: () => void;onRejectProposed: () => void;+ onRunQueryStart?: (query: DatasetQuery) => boolean | void;onBlur?: () => void;+ transform?: Transform;isEditMode?: boolean;readOnly?: boolean;- transformId?: TransformId;};export function TransformEditor({@@ -37,14 +43,16 @@ export function TransformEditor({proposedSource,databases,uiState,+ uiOptions,onChangeSource,onChangeUiState,onAcceptProposed,onRejectProposed,+ onRunQueryStart,onBlur,+ transform,isEditMode,readOnly,- transformId,}: TransformEditorProps) {const metadata = useSelector(getMetadata);const query = useMemo(@@ -58,14 +66,15 @@ export function TransformEditor({: undefined,[proposedSource, metadata],);- const uiOptions = useMemo(- () => getEditorOptions(databases, !isEditMode),- [databases, isEditMode],+ const mergedUiOptions = useMemo(+ () => ({ ...getEditorOptions(databases, !isEditMode), ...uiOptions }),+ [databases, isEditMode, uiOptions],);const isRemoteSyncReadOnly = useSelector(getIsRemoteSyncReadOnly);- const showEditDefinitionButton =- !!transformId && !readOnly && !isEditMode && !isRemoteSyncReadOnly;++ const showEditButton =+ !!transform && !readOnly && !isEditMode && !isRemoteSyncReadOnly;const handleQueryChange = (query: Lib.Query) => {const newSource: QueryTransformSource = {@@ -81,24 +90,28 @@ export function TransformEditor({<QueryEditorquery={query}uiState={uiState}- uiOptions={uiOptions}+ uiOptions={mergedUiOptions}proposedQuery={proposedQuery}onChangeQuery={handleQueryChange}onChangeUiState={onChangeUiState}onAcceptProposed={onAcceptProposed}onRejectProposed={onRejectProposed}+ onRunQueryStart={onRunQueryStart}onBlur={onBlur}topBarInnerContent={- showEditDefinitionButton && (+ showEditButton &&+ (hasPremiumFeature("workspaces") && transform ? (+ <EditTransformMenu transform={transform} />+ ) : (<EditDefinitionButtonbg="transparent"fz="sm"h="1.5rem"px="sm"size="xs"- transformId={transformId}+ transformId={transform.id}/>- )+ ))}/>);diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformHeader/EditTransformMenu/EditTransformMenu.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformHeader/EditTransformMenu/EditTransformMenu.tsxnew file mode 100644index 00000000000..58dbcfb7540--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformHeader/EditTransformMenu/EditTransformMenu.tsx@@ -0,0 +1,237 @@+import { useMemo } from "react";+import { Link } from "react-router";+import { push } from "react-router-redux";+import { t } from "ttag";++import { useDispatch } from "metabase/lib/redux";+import { transformEdit } from "metabase/lib/urls";+import * as Urls from "metabase/lib/urls";+import { useMetadataToasts } from "metabase/metadata/hooks";+import {+ Box,+ Button,+ Flex,+ Icon,+ Loader,+ Menu,+ ScrollArea,+ Stack,+ Text,+ Tooltip,+} from "metabase/ui";+import {+ useCreateWorkspaceMutation,+ useGetWorkspaceCheckoutQuery,+ useGetWorkspacesQuery,+} from "metabase-enterprise/api";+import { getCheckoutDisabledMessage } from "metabase-enterprise/data-studio/workspaces/utils";+import type { DatabaseId, Transform } from "metabase-types/api";++type EditTransformMenuProps = {+ transform: Transform;+};++export function EditTransformMenu({ transform }: EditTransformMenuProps) {+ const dispatch = useDispatch();+ const { sendErrorToast } = useMetadataToasts();++ const sourceDatabaseId = getTransformDatabaseId(transform);++ const { data: workspacesData, isLoading: isLoadingWorkspaces } =+ useGetWorkspacesQuery();+ const [createWorkspace, { isLoading: isCreatingWorkspace }] =+ useCreateWorkspaceMutation();+ const { data: checkoutData, isLoading: isWorkspaceCheckoutLoading } =+ useGetWorkspaceCheckoutQuery(transform.id);++ const workspaces = useMemo(+ () =>+ workspacesData?.items.filter((item) => item?.status !== "archived") ?? [],+ [workspacesData],+ );++ const matchingWorkspaces = useMemo(() => {+ const allMatchingWorkspaceIds =+ workspaces+ ?.filter((item) => item.database_id === sourceDatabaseId)+ ?.map((item) => item.id) ?? [];++ const workspacesMap = new Map(workspaces.map((w) => [w.id, w]));++ // Workspaces which already include this transform.+ const checkedWorkspaceIds = new Set(+ checkoutData?.workspaces+ ?.toSorted((a, b) => {+ if (a.existing && !b.existing) {+ return -1;+ }+ if (!a.existing && b.existing) {+ return 1;+ }+ return 0;+ })+ ?.map((item) => item?.id),+ );++ return Array.from(+ new Set([...checkedWorkspaceIds, ...allMatchingWorkspaceIds]),+ )+ .map((id) => {+ const workspace = workspacesMap.get(id);+ if (!workspace) {+ return null;+ }+ return {+ id,+ isChecked: checkedWorkspaceIds.has(id),+ name: workspace.name,+ };+ })+ .filter((workspace) => !!workspace);+ }, [checkoutData?.workspaces, workspaces, sourceDatabaseId]);++ const isBusy =+ isCreatingWorkspace || isLoadingWorkspaces || isWorkspaceCheckoutLoading;+ const emptyMessage =+ workspaces.length === 0 || sourceDatabaseId == null+ ? t`No workspaces yet`+ : t`No workspaces for this database yet`;++ const handleWorkspaceSelect = async (workspace: {+ id: number;+ name?: string;+ }) => {+ dispatch(push(Urls.dataStudioWorkspace(workspace.id, transform.id)));+ };++ const handleCreateWorkspace = async ({+ databaseId,+ }: {+ databaseId?: DatabaseId | null;+ }) => {+ if (!databaseId) {+ sendErrorToast(t`Failed to create workspace, no database id`);+ return;+ }++ try {+ const workspace = await createWorkspace({+ database_id: Number(databaseId),+ }).unwrap();++ dispatch(push(Urls.dataStudioWorkspace(workspace.id, transform.id)));+ } catch (error) {+ sendErrorToast(t`Failed to create workspace`);+ }+ };++ return (+ <Menu position="bottom-end" width={280}>+ <Menu.Target>+ <Button+ role="button"+ data-testid="transform-edit-menu-button"+ rightSection={<Icon name="chevrondown" />}+ loading={isBusy}+ size="xs"+ >+ {t`Edit`}+ </Button>+ </Menu.Target>+ <Menu.Dropdown>+ <Menu.Item+ component={Link}+ to={transformEdit(transform.id)}+ leftSection={<Icon name="pencil" />}+ >+ {t`Edit definition`}+ </Menu.Item>++ {+ <>+ <Menu.Divider />+ <Menu.Label>{t`Add to workspace`}</Menu.Label>+ <Tooltip+ label={getCheckoutDisabledMessage(+ checkoutData?.checkout_disabled,+ )}+ disabled={!checkoutData?.checkout_disabled}+ >+ <Menu.Item+ disabled={isBusy || !!checkoutData?.checkout_disabled}+ leftSection={<Icon name="add" />}+ onClick={() => {+ handleCreateWorkspace({ databaseId: sourceDatabaseId });+ }}+ >+ <Stack gap={0} align="flex-start">+ <Text fw={600}>{t`New workspace`}</Text>+ <Text size="sm" c="text-tertiary">+ {t`Create a new workspace`}+ </Text>+ </Stack>+ </Menu.Item>+ </Tooltip>+ <Menu.Divider />++ {isLoadingWorkspaces || isWorkspaceCheckoutLoading ? (+ <Flex justify="center" align="center" py="md">+ <Loader size="sm" />+ </Flex>+ ) : matchingWorkspaces.length === 0 ? (+ <Box px="md" py="sm">+ <Text size="sm" c="text-tertiary">+ {emptyMessage}+ </Text>+ </Box>+ ) : (+ <ScrollArea.Autosize mah={320} type="scroll">+ <Stack gap={0}>+ {matchingWorkspaces.map((workspace) => (+ <Tooltip+ key={workspace.id}+ label={getCheckoutDisabledMessage(+ checkoutData?.checkout_disabled,+ )}+ disabled={!checkoutData?.checkout_disabled}+ >+ <Menu.Item+ leftSection={+ <Icon+ name="sparkles"+ c={workspace.isChecked ? "brand" : "text-primary"}+ />+ }+ onClick={() => handleWorkspaceSelect(workspace)}+ disabled={isBusy || !!checkoutData?.checkout_disabled}+ >+ <Stack gap={2} align="flex-start">+ <Text fw={600}>{workspace.name}</Text>+ </Stack>+ </Menu.Item>+ </Tooltip>+ ))}+ </Stack>+ </ScrollArea.Autosize>+ )}+ </>+ }+ </Menu.Dropdown>+ </Menu>+ );+}++function getTransformDatabaseId(transform: Transform) {+ if (+ transform.source_type === "python" &&+ "source-database" in transform.source+ ) {+ return transform.source["source-database"];+ }++ if ("query" in transform.source) {+ return transform.source.query.database;+ }++ return undefined;+}diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformHeader/EditTransformMenu/EditTransformMenu.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformHeader/EditTransformMenu/EditTransformMenu.unit.spec.tsxnew file mode 100644index 00000000000..e2ce610fed8--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformHeader/EditTransformMenu/EditTransformMenu.unit.spec.tsx@@ -0,0 +1,255 @@+import userEvent from "@testing-library/user-event";+import { Route } from "react-router";++import {+ setupCreateWorkspaceEndpoint,+ setupWorkspaceCheckoutEndpoint,+ setupWorkspacesEndpoint,+} from "__support__/server-mocks";+import { renderWithProviders, screen, waitFor } from "__support__/ui";+import type {+ Transform,+ Workspace,+ WorkspaceCheckoutResponse,+ WorkspaceItem,+} from "metabase-types/api";+import { createMockTransform } from "metabase-types/api/mocks";++import { EditTransformMenu } from "./EditTransformMenu";++const DATABASE_ID = 1;++function createMockWorkspaceItem(opts?: Partial<WorkspaceItem>): WorkspaceItem {+ return {+ id: 1,+ name: "Workspace 1",+ database_id: DATABASE_ID,+ status: "ready",+ updated_at: "2024-01-01T00:00:00Z",+ ...opts,+ };+}++function createMockWorkspace(opts?: Partial<Workspace>): Workspace {+ return {+ id: 1,+ name: "Workspace 1",+ status: "ready",+ database_id: DATABASE_ID,+ ...opts,+ };+}++type SetupOpts = {+ transform?: Transform;+ workspaces?: WorkspaceItem[];+ checkoutResponse?: Partial<WorkspaceCheckoutResponse>;+};++function setup({+ transform = createMockTransform({+ id: 1,+ name: "Test Transform",+ source: {+ type: "query",+ query: { database: DATABASE_ID, type: "native", native: { query: "" } },+ },+ }),+ workspaces = [],+ checkoutResponse = {},+}: SetupOpts = {}) {+ setupWorkspacesEndpoint(workspaces);+ setupWorkspaceCheckoutEndpoint(checkoutResponse);+ setupCreateWorkspaceEndpoint(+ createMockWorkspace({ id: 99, name: "New Workspace" }),+ );++ renderWithProviders(+ <Route+ component={() => <EditTransformMenu transform={transform} />}+ path="/"+ />,+ {+ withRouter: true,+ initialRoute: "/",+ },+ );+}++function findMenuItemByText(text: string) {+ return screen.getAllByRole("menuitem").find((item) => {+ return item.textContent?.includes(text);+ });+}++describe("EditTransformMenu", () => {+ it("should render menu items and sort workspaces with existing checkouts first", async () => {+ setup({+ workspaces: [+ createMockWorkspaceItem({ id: 1, name: "Workspace A" }),+ createMockWorkspaceItem({ id: 2, name: "Workspace B" }),+ createMockWorkspaceItem({ id: 3, name: "Workspace C" }),+ ],+ checkoutResponse: {+ // Return workspaces in order where existing ones come last from backend+ workspaces: [+ { id: 1, name: "Workspace A", status: "ready", existing: null },+ { id: 2, name: "Workspace B", status: "ready", existing: null },+ {+ id: 3,+ name: "Workspace C",+ status: "ready",+ existing: { ref_id: "abc123", name: "Test Transform" },+ },+ ],+ },+ });++ // Wait for button to finish loading before clicking+ const editButton = await screen.findByRole("button", { name: /Edit/i });+ await waitFor(() => {+ expect(editButton).not.toHaveAttribute("data-loading", "true");+ });++ await userEvent.click(editButton);++ // Verify basic menu structure renders+ expect(screen.getByText("Add to workspace")).toBeInTheDocument();+ expect(screen.getByText("New workspace")).toBeInTheDocument();++ // Wait for workspaces to load+ await waitFor(() => {+ expect(screen.getByText("Workspace C")).toBeInTheDocument();+ });+ expect(screen.getByText("Workspace A")).toBeInTheDocument();+ expect(screen.getByText("Workspace B")).toBeInTheDocument();++ // Verify sorting: workspaces with existing checkouts should be first+ const menuItems = screen.getAllByRole("menuitem").filter((item) => {+ const text = item.textContent;+ return (+ text?.includes("Workspace A") ||+ text?.includes("Workspace B") ||+ text?.includes("Workspace C")+ );+ });++ // Workspace C should be first because it has existing checkout+ expect(menuItems[0]).toHaveTextContent("Workspace C");+ expect(menuItems[1]).toHaveTextContent("Workspace A");+ expect(menuItems[2]).toHaveTextContent("Workspace B");+ });++ describe("checkout disabled tooltips", () => {+ it("should disable 'New workspace' and workspace items when checkout_disabled is 'mbql'", async () => {+ setup({+ workspaces: [createMockWorkspaceItem({ id: 1, name: "Workspace A" })],+ checkoutResponse: {+ checkout_disabled: "mbql",+ workspaces: [+ { id: 1, name: "Workspace A", status: "ready", existing: null },+ ],+ },+ });++ const editButton = await screen.findByRole("button", { name: /Edit/i });+ await waitFor(() => {+ expect(editButton).not.toHaveAttribute("data-loading", "true");+ });++ await userEvent.click(editButton);++ // Find menu items+ const newWorkspaceItem = findMenuItemByText("New workspace");+ const workspaceAItem = findMenuItemByText("Workspace A");++ expect(newWorkspaceItem).toHaveAttribute("data-disabled", "true");+ expect(workspaceAItem).toHaveAttribute("data-disabled", "true");+ });++ it("should show tooltip with MBQL message when hovering disabled items", async () => {+ setup({+ workspaces: [createMockWorkspaceItem({ id: 1, name: "Workspace A" })],+ checkoutResponse: {+ checkout_disabled: "mbql",+ workspaces: [+ { id: 1, name: "Workspace A", status: "ready", existing: null },+ ],+ },+ });++ const editButton = await screen.findByRole("button", { name: /Edit/i });+ await waitFor(() => {+ expect(editButton).not.toHaveAttribute("data-loading", "true");+ });++ await userEvent.click(editButton);++ const newWorkspaceItem = findMenuItemByText("New workspace");+ await userEvent.hover(newWorkspaceItem!);++ await waitFor(() => {+ expect(+ screen.getByText(+ "Transforms based on the query builder cannot be edited in a workspace.",+ ),+ ).toBeInTheDocument();+ });+ });++ it("should show tooltip with card-reference message when checkout is disabled", async () => {+ setup({+ workspaces: [createMockWorkspaceItem({ id: 1, name: "Workspace A" })],+ checkoutResponse: {+ checkout_disabled: "card-reference",+ workspaces: [+ { id: 1, name: "Workspace A", status: "ready", existing: null },+ ],+ },+ });++ const editButton = await screen.findByRole("button", { name: /Edit/i });+ await waitFor(() => {+ expect(editButton).not.toHaveAttribute("data-loading", "true");+ });++ await userEvent.click(editButton);++ const workspaceItem = findMenuItemByText("Workspace A");+ await userEvent.hover(workspaceItem!);++ await waitFor(() => {+ expect(+ screen.getByText(+ "Transforms referencing other questions cannot be edited in a workspace.",+ ),+ ).toBeInTheDocument();+ });+ });++ it("should not disable items when checkout_disabled is null", async () => {+ setup({+ workspaces: [createMockWorkspaceItem({ id: 1, name: "Workspace A" })],+ checkoutResponse: {+ checkout_disabled: null,+ workspaces: [+ { id: 1, name: "Workspace A", status: "ready", existing: null },+ ],+ },+ });++ const editButton = await screen.findByRole("button", { name: /Edit/i });+ await waitFor(() => {+ expect(editButton).not.toHaveAttribute("data-loading", "true");+ });++ await userEvent.click(editButton);++ const newWorkspaceItem = findMenuItemByText("New workspace");+ const workspaceAItem = findMenuItemByText("Workspace A");++ expect(newWorkspaceItem).not.toHaveAttribute("data-disabled", "true");+ expect(workspaceAItem).not.toHaveAttribute("data-disabled", "true");+ });+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformHeader/EditTransformMenu/index.ts b/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformHeader/EditTransformMenu/index.tsnew file mode 100644index 00000000000..3670f139598--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/transforms/components/TransformHeader/EditTransformMenu/index.ts@@ -0,0 +1 @@+export * from "./EditTransformMenu";diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/hooks/use-register-transform-metabot-context.ts b/enterprise/frontend/src/metabase-enterprise/transforms/hooks/use-register-transform-metabot-context.tsindex 9ecd8397e26..1d5f12983c8 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/hooks/use-register-transform-metabot-context.ts+++ b/enterprise/frontend/src/metabase-enterprise/transforms/hooks/use-register-transform-metabot-context.ts@@ -1,18 +1,38 @@import { useRegisterMetabotContextProvider } from "metabase/metabot";import type {- PythonTransformSourceDraft,+ DraftTransformSource,+ MetabotTransformInfo,SuggestedTransform,+ TaggedTransform,Transform,- TransformSource,+ UnsavedTransform,+ WorkspaceTransform,} from "metabase-types/api";+type AnyTransform =+ | Transform+ | TaggedTransform+ | WorkspaceTransform+ | UnsavedTransform+ | SuggestedTransform;+export const useRegisterMetabotTransformContext = (- transform: Transform | SuggestedTransform | undefined,- source: TransformSource | PythonTransformSourceDraft,+ transform: AnyTransform | undefined,+ source?: DraftTransformSource,) => {useRegisterMetabotContextProvider(async () => {+ if (!transform && !source) {+ return {};+ }+return {- user_is_viewing: [{ type: "transform", ...(transform || {}), source }],+ user_is_viewing: [+ {+ ...transform,+ type: "transform",+ ...(source !== undefined && { source }),+ } as MetabotTransformInfo,+ ],};}, [transform, source]);};diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/pages/NewTransformPage/CreateTransformModal/CreateTransformModal.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/pages/NewTransformPage/CreateTransformModal/CreateTransformModal.tsxindex caff90a03a9..71ab6c39248 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/pages/NewTransformPage/CreateTransformModal/CreateTransformModal.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms/pages/NewTransformPage/CreateTransformModal/CreateTransformModal.tsx@@ -1,6 +1,8 @@import { useFormikContext } from "formik";-import { useState } from "react";+import { useMemo, useState } from "react";import { t } from "ttag";+import _ from "underscore";+import type * as Yup from "yup";import { hasFeature } from "metabase/admin/databases/utils";import {@@ -25,8 +27,10 @@ import {} from "metabase-enterprise/transforms/components/QueryComplexityWarning";import type {QueryComplexity,+ SchemaName,Transform,TransformSource,+ WorkspaceTransform,} from "metabase-types/api";import { SchemaFormSelect } from "../../../components/SchemaFormSelect";@@ -35,11 +39,23 @@ import { TargetNameInput } from "./TargetNameInput";import type { NewTransformValues } from "./form";import { useCreateTransform } from "./hooks";+export type ValidationSchemaExtension = Record<string, Yup.AnySchema>;++type SchemasFilter = (schema: SchemaName) => boolean;+type CreateTransformModalProps = {source: TransformSource;defaultValues: Partial<NewTransformValues>;- onCreate: (transform: Transform) => void;+ onCreate?: (transform: Transform) => void;onClose: () => void;+ schemasFilter?: SchemasFilter;+ validationSchemaExtension?: ValidationSchemaExtension;+ handleSubmit?: (+ values: NewTransformValues,+ ) => Promise<Transform | WorkspaceTransform>;+ targetDescription?: string;+ validateOnMount?: boolean;+ showIncrementalSettings?: boolean;};export function CreateTransformModal({@@ -47,6 +63,12 @@ export function CreateTransformModal({defaultValues,onCreate,onClose,+ schemasFilter,+ validationSchemaExtension,+ handleSubmit,+ targetDescription,+ validateOnMount,+ showIncrementalSettings,}: CreateTransformModalProps) {const databaseId =source.type === "query" ? source.query.database : source["source-database"];@@ -58,29 +80,43 @@ export function CreateTransformModal({} = useGetDatabaseQuery(databaseId ? { id: databaseId } : skipToken);const {- data: schemas = [],+ data: fetchedSchemas = [],isLoading: isSchemasLoading,error: schemasError,} = useListSyncableDatabaseSchemasQuery(databaseId ?? skipToken);+ const schemas = useMemo(() => {+ return (fetchedSchemas ?? []).filter(schemasFilter || _.identity);+ }, [schemasFilter, fetchedSchemas]);const isLoading = isDatabaseLoading || isSchemasLoading;const error = databaseError ?? schemasError;const supportsSchemas = database && hasFeature(database, "schemas");- const { initialValues, validationSchema, createTransform } =- useCreateTransform(schemas, defaultValues);+ const {+ initialValues,+ validationSchema: defaultSchema,+ createTransform,+ } = useCreateTransform(schemas, defaultValues);++ const validationSchema = useMemo(+ () =>+ validationSchemaExtension+ ? defaultSchema.shape(validationSchemaExtension)+ : defaultSchema,+ [validationSchemaExtension, defaultSchema],+ );if (isLoading || error != null) {return <LoadingAndErrorWrapper loading={isLoading} error={error} />;}- const handleSubmit = async (values: NewTransformValues) => {+ const defaultHandleSubmit = async (values: NewTransformValues) => {if (!databaseId) {throw new Error("Database ID is required");}const transform = await createTransform(databaseId, source, values);- onCreate(transform);+ onCreate?.(transform);};return (@@ -88,13 +124,16 @@ export function CreateTransformModal({<FormProviderinitialValues={initialValues}validationSchema={validationSchema}- onSubmit={handleSubmit}+ onSubmit={handleSubmit || defaultHandleSubmit}+ validateOnMount={validateOnMount}><CreateTransformFormsource={source}supportsSchemas={supportsSchemas}schemas={schemas}onClose={onClose}+ targetDescription={targetDescription}+ showIncrementalSettings={showIncrementalSettings}/></FormProvider></Modal>@@ -106,6 +145,8 @@ type CreateTransformFormFieldsProps = {supportsSchemas: boolean | undefined;schemas: string[];onClose: () => void;+ targetDescription?: string;+ showIncrementalSettings?: boolean;};function CreateTransformForm({@@ -113,6 +154,8 @@ function CreateTransformForm({supportsSchemas,schemas,onClose,+ targetDescription,+ showIncrementalSettings = true,}: CreateTransformFormFieldsProps) {const { values, setFieldValue } = useFormikContext<NewTransformValues>();const { checkComplexity } = useQueryComplexityChecks();@@ -144,18 +187,20 @@ function CreateTransformForm({data={schemas}/>)}- <TargetNameInput />+ <TargetNameInput description={targetDescription} /><FormCollectionPickername="collection_id"title={t`Collection`}collectionPickerModalProps={{ namespaces: ["transforms"] }}style={{ marginBottom: 0 }}/>- <IncrementalTransformSettings- source={source}- incremental={values.incremental}- onIncrementalChange={handleIncrementalChange}- />+ {showIncrementalSettings && (+ <IncrementalTransformSettings+ source={source}+ incremental={values.incremental}+ onIncrementalChange={handleIncrementalChange}+ />+ )}{complexity && (<QueryComplexityWarning complexity={complexity} variant="standout" />)}diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/pages/NewTransformPage/CreateTransformModal/TargetNameInput.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/pages/NewTransformPage/CreateTransformModal/TargetNameInput.tsxindex 5e487fda5d1..344caa38751 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/pages/NewTransformPage/CreateTransformModal/TargetNameInput.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms/pages/NewTransformPage/CreateTransformModal/TargetNameInput.tsx@@ -5,7 +5,11 @@ import { t } from "ttag";import { FormTextInput } from "metabase/forms";import { slugify } from "metabase/lib/formatting/url";-export const TargetNameInput = () => {+type TargetNameInputProps = {+ description?: string;+};++export const TargetNameInput = ({ description }: TargetNameInputProps) => {const isDirtyRef = useRef(false);const [{ value: name }] = useField<string>("name");@@ -27,6 +31,7 @@ export const TargetNameInput = () => {name="targetName"label={t`Table name`}placeholder={t`descriptive_name`}+ description={description}onChange={handleTargetNameChange}/>);diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformQueryPage/TransformPaneHeaderActions.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformQueryPage/TransformPaneHeaderActions.tsxindex 34dfb3534d2..9cc061a8e54 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformQueryPage/TransformPaneHeaderActions.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformQueryPage/TransformPaneHeaderActions.tsx@@ -8,7 +8,7 @@ import { getIsRemoteSyncReadOnly } from "metabase-enterprise/remote_sync/selectoimport { EditDefinitionButton } from "metabase-enterprise/transforms/components/TransformEditor/EditDefinitionButton";import { getValidationResult } from "metabase-enterprise/transforms/utils";import * as Lib from "metabase-lib";-import type { DraftTransformSource, TransformId } from "metabase-types/api";+import type { DraftTransformSource, Transform } from "metabase-types/api";type Props = {handleCancel: VoidFunction;@@ -18,7 +18,7 @@ type Props = {isSaving: boolean;readOnly?: boolean;source: DraftTransformSource;- transformId: TransformId;+ transform: Transform;};export const TransformPaneHeaderActions = (props: Props) => {@@ -29,8 +29,8 @@ export const TransformPaneHeaderActions = (props: Props) => {handleCancel,isSaving,isEditMode,+ transform,readOnly,- transformId,} = props;const metadata = useSelector(getMetadata);const isRemoteSyncReadOnly = useSelector(getIsRemoteSyncReadOnly);@@ -60,7 +60,7 @@ export const TransformPaneHeaderActions = (props: Props) => {!isEditMode &&!isRemoteSyncReadOnly) {- return <EditDefinitionButton transformId={transformId} />;+ return <EditDefinitionButton transformId={transform.id} />;}if (!isEditMode && isNative) {diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformQueryPage/TransformPaneHeaderActions.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformQueryPage/TransformPaneHeaderActions.unit.spec.tsxindex 8026ce24232..199b53cd9f0 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformQueryPage/TransformPaneHeaderActions.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformQueryPage/TransformPaneHeaderActions.unit.spec.tsx@@ -3,9 +3,14 @@ import { Route } from "react-router";import { renderWithProviders, screen } from "__support__/ui";import * as transformsUtils from "metabase-enterprise/transforms/utils";import type { DraftTransformSource } from "metabase-types/api";+import { createMockTransform } from "metabase-types/api/mocks";import { TransformPaneHeaderActions } from "./TransformPaneHeaderActions";+jest.mock("metabase-enterprise/settings", () => ({+ hasPremiumFeature: jest.fn(),+}));+const mockQuerySource: DraftTransformSource = {type: "query",query: {@@ -59,6 +64,11 @@ function setup({? nativeSource: source;+ const transform = createMockTransform({+ id: 1,+ source_type: isPython ? "python" : isNative ? "native" : "mbql",+ });+const { unmount } = renderWithProviders(<Routecomponent={() => (@@ -69,7 +79,7 @@ function setup({isEditMode={isEditMode}isSaving={isSaving}source={resolvedSource}- transformId={1}+ transform={transform}/>)}path="/"@@ -144,27 +154,55 @@ describe("TransformPaneHeaderActions", () => {});describe("read-only mode (not edit mode)", () => {- it("should render EditDefinitionButton", () => {- setup({ isEditMode: false, isNative: false });+ it("should not render Save and Cancel", () => {+ setup({ isEditMode: false, isNative: true });+expect(- screen.getByRole("link", { name: /edit definition/i }),- ).toBeInTheDocument();+ screen.queryByRole("button", { name: /cancel/i }),+ ).not.toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: /save/i }),+ ).not.toBeInTheDocument();});- it("should not render Save and Cancel", () => {+ it("should render nothing for native transforms when workspaces not available", () => {setup({ isEditMode: false, isNative: true });+ expect(+ screen.queryByRole("button", { name: /save/i }),+ ).not.toBeInTheDocument();expect(screen.queryByRole("button", { name: /cancel/i }),).not.toBeInTheDocument();expect(- screen.queryByRole("button", { name: /save/i }),+ screen.queryByRole("link", { name: /edit definition/i }),+ ).not.toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: /edit/i }),).not.toBeInTheDocument();});++ it("should render EditDefinitionButton for MBQL transforms", () => {+ setup({ isEditMode: false, isNative: false });++ expect(+ screen.getByRole("link", { name: /edit definition/i }),+ ).toBeInTheDocument();+ });++ describe("workspaces feature availability", () => {+ it("should render EditDefinitionButton when workspaces feature is not available", () => {+ setup({ isEditMode: false, isNative: false });++ expect(+ screen.getByRole("link", { name: /edit definition/i }),+ ).toBeInTheDocument();+ });+ });});describe("Python transforms", () => {- it("should not render EditDefinitionButton for Python transforms (handled by PythonTransformTopBar)", () => {+ it("should not render EditDefinitionButton for Python transforms (moved to EditTransformMenu in header)", () => {setup({ isEditMode: false, isPython: true });expect(screen.queryByRole("link", { name: /edit definition/i }),diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformQueryPage/TransformQueryPage.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformQueryPage/TransformQueryPage.tsxindex 2b1e41561d7..3450e291067 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformQueryPage/TransformQueryPage.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformQueryPage/TransformQueryPage.tsx@@ -16,7 +16,7 @@ import {PLUGIN_TRANSFORMS_PYTHON,} from "metabase/plugins";import { getInitialUiState } from "metabase/querying/editor/components/QueryEditor";-import { Box, Center, Icon } from "metabase/ui";+import { Box, Center, Group, Icon } from "metabase/ui";import {useGetTransformQuery,useUpdateTransformMutation,@@ -24,10 +24,18 @@ import {import { PageContainer } from "metabase-enterprise/data-studio/common/components/PageContainer";import { getIsRemoteSyncReadOnly } from "metabase-enterprise/remote_sync/selectors";import { useTransformPermissions } from "metabase-enterprise/transforms/hooks/use-transform-permissions";-import type { Database, Transform } from "metabase-types/api";+import type {+ Database,+ DatasetQuery,+ DraftTransformSource,+ Transform,+} from "metabase-types/api";import { useQueryComplexityChecks } from "../../components/QueryComplexityWarning";-import { TransformEditor } from "../../components/TransformEditor";+import {+ TransformEditor,+ type TransformEditorProps,+} from "../../components/TransformEditor";import { TransformHeader } from "../../components/TransformHeader";import { useRegisterMetabotTransformContext } from "../../hooks/use-register-transform-metabot-context";import { useSourceState } from "../../hooks/use-source-state";@@ -181,16 +189,18 @@ function TransformQueryPageBody({<TransformHeadertransform={transform}actions={- <TransformPaneHeaderActions- source={source}- isSaving={isSaving}- isDirty={isDirty}- handleSave={handleSave}- handleCancel={handleCancel}- transformId={transform.id}- isEditMode={isEditMode}- readOnly={readOnly}- />+ <Group gap="sm">+ <TransformPaneHeaderActions+ source={source}+ isSaving={isSaving}+ isDirty={isDirty}+ handleSave={handleSave}+ handleCancel={handleCancel}+ transform={transform}+ readOnly={readOnly}+ isEditMode={isEditMode}+ />+ </Group>}hasMenu={!isEditMode && !isDirty}isEditMode={isEditMode}@@ -219,9 +229,9 @@ function TransformQueryPageBody({proposedSource={proposedSource?.type === "python" ? proposedSource : undefined}- readOnly={readOnly}+ uiOptions={{ readOnly }}isEditMode={isEditMode}- transformId={transform.id}+ transform={transform}onChangeSource={setSourceAndRejectProposed}onAcceptProposed={acceptProposed}onRejectProposed={rejectProposed}@@ -233,14 +243,16 @@ function TransformQueryPageBody({proposedSource?.type === "query" ? proposedSource : undefined}uiState={uiState}- readOnly={readOnly}+ // TODO (Uladzimir 2026-01-28) -- probably not the proper fix+ uiOptions={{ resizable: isEditMode && !readOnly }}isEditMode={isEditMode}databases={databases}onChangeSource={setSourceAndRejectProposed}onChangeUiState={setUiState}onAcceptProposed={acceptProposed}onRejectProposed={rejectProposed}- transformId={transform.id}+ transform={transform}+ readOnly={readOnly}/>)}</Box>@@ -262,3 +274,73 @@ function TransformQueryPageBody({</>);}++export type TransformQueryPageEditorUiState = ReturnType<+ typeof getInitialUiState+>;++export type TransformQueryPageEditorProps = {+ source: DraftTransformSource;+ proposedSource?: DraftTransformSource;+ uiState: TransformQueryPageEditorUiState;+ databases: Database[];+ setSourceAndRejectProposed: (source: DraftTransformSource) => void;+ setUiState: (uiState: TransformQueryPageEditorUiState) => void;+ isEditMode?: boolean;+ acceptProposed: () => void;+ rejectProposed: () => void;+ uiOptions?: TransformEditorProps["uiOptions"];+ onRunQueryStart?: (query: DatasetQuery) => boolean | void;+ onRunTransform?: (result: any) => void;+ /** Custom run handler for Python transforms (used in workspace for dry-run) */+ onRun?: () => void;+};++export function TransformQueryPageEditor({+ source,+ proposedSource,+ uiState,++ uiOptions,+ databases,+ setSourceAndRejectProposed,+ setUiState,+ isEditMode = false,+ acceptProposed,+ rejectProposed,+ onRunQueryStart,+ onRunTransform,+ onRun,+}: TransformQueryPageEditorProps) {+ return source.type === "python" ? (+ <PLUGIN_TRANSFORMS_PYTHON.TransformEditor+ source={source}+ uiOptions={uiOptions}+ proposedSource={+ proposedSource?.type === "python" ? proposedSource : undefined+ }+ isEditMode={isEditMode}+ onChangeSource={setSourceAndRejectProposed}+ onAcceptProposed={acceptProposed}+ onRejectProposed={rejectProposed}+ onRunTransform={onRunTransform}+ onRun={onRun}+ />+ ) : (+ <TransformEditor+ source={source}+ proposedSource={+ proposedSource?.type === "query" ? proposedSource : undefined+ }+ uiState={uiState}+ uiOptions={uiOptions}+ databases={databases}+ isEditMode={isEditMode}+ onChangeSource={setSourceAndRejectProposed}+ onChangeUiState={setUiState}+ onAcceptProposed={acceptProposed}+ onRejectProposed={rejectProposed}+ onRunQueryStart={onRunQueryStart}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformSettingsPage/TransformSettingsSection/UpdateTargetModal/UpdateTargetModal.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformSettingsPage/TransformSettingsSection/UpdateTargetModal/UpdateTargetModal.tsxindex d4dfc693389..4d33c942c8b 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformSettingsPage/TransformSettingsSection/UpdateTargetModal/UpdateTargetModal.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformSettingsPage/TransformSettingsSection/UpdateTargetModal/UpdateTargetModal.tsx@@ -36,7 +36,7 @@ import type { Transform, UpdateTransformRequest } from "metabase-types/api";type UpdateTargetModalProps = {transform: Transform;- onUpdate: () => void;+ onUpdate: (transform?: Transform) => void;onClose: () => void;};@@ -74,7 +74,7 @@ const EDIT_TRANSFORM_SCHEMA = Yup.object({type UpdateTargetFormProps = {transform: Transform;- onUpdate: () => void;+ onUpdate: (transform?: Transform) => void;onClose: () => void;};@@ -119,10 +119,10 @@ function UpdateTargetForm({if (shouldDeleteTarget) {await deleteTransformTarget(transform.id).unwrap();}- await updateTransform(+ const updatedTransform = await updateTransform(getUpdateRequest(transform, values, databaseId),).unwrap();- onUpdate();+ onUpdate(updatedTransform);};return (diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/utils.ts b/enterprise/frontend/src/metabase-enterprise/transforms/utils.tsindex 5d843eee0da..8ee16f47360 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/utils.ts+++ b/enterprise/frontend/src/metabase-enterprise/transforms/utils.ts@@ -35,8 +35,10 @@ export function parseTimestampWithTimezone(}}-export function formatStatus(status: TransformRunStatus) {+export function formatStatus(status: TransformRunStatus | null) {switch (status) {+ case null:+ return t`Unknown`;case "started":return t`In progress`;case "succeeded":@@ -91,7 +93,7 @@ export function getTransformRunName(run: TransformRun): string {return run.transform?.name ?? t`Unknown transform`;}-export function isErrorStatus(status: TransformRunStatus) {+export function isErrorStatus(status: TransformRunStatus | null) {return status === "failed" || status === "timeout";}@@ -137,6 +139,27 @@ export function isSameSource(return false;}+export function isSourceEmpty(+ source: DraftTransformSource,+ databaseId: DatabaseId,+ metadata: Metadata,+): boolean {+ if (source.type !== "query") {+ return false;+ }++ const metadataProvider = Lib.metadataProvider(databaseId, metadata);+ const query = Lib.fromJsQuery(metadataProvider, source.query);+ const { isNative } = Lib.queryDisplayInfo(query);++ if (!isNative) {+ return false;+ }++ const nativeQuery = Lib.rawNativeQuery(query);+ return !nativeQuery?.trim();+}+export function isCompleteSource(source: DraftTransformSource,): source is TransformSource {diff --git a/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/SdkError.tsx b/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/SdkError.tsxindex e21fdaf4c55..da6580be47b 100644--- a/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/SdkError.tsx+++ b/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/SdkError.tsx@@ -95,7 +95,7 @@ export function SdkPortalErrorWrapper({ children }: PropsWithChildren) {const FORCE_DARK_TEXT_COLOR = {// The Alert component has a light background, we need to force a dark text- // color. The sdk aliases text-dark to the primary color, which in dark themes+ // color. The sdk aliases text-primary to the primary color, which in dark themes// is a light color, making the text un-readable"--mb-color-text-primary": color("text-primary"),"--mb-color-text-secondary": color("text-secondary"),diff --git a/frontend/src/metabase-types/api/database.ts b/frontend/src/metabase-types/api/database.tsindex 9daddfd5e4b..9ba8ff96064 100644--- a/frontend/src/metabase-types/api/database.ts+++ b/frontend/src/metabase-types/api/database.ts@@ -57,7 +57,8 @@ export type DatabaseFeature =| "split-part"| "collate"| "transforms/python"- | "transforms/table";+ | "transforms/table"+ | "workspace";export interface Database extends DatabaseData {id: DatabaseId;@@ -84,6 +85,7 @@ export interface Database extends DatabaseData {// Only appears in GET /api/database/:id"can-manage"?: boolean;tables?: Table[];+ workspace_permissions_status: CheckWorkspacePermissionsResponse | null;}export interface DatabaseData {@@ -247,3 +249,14 @@ export interface UpdateDatabaseRouterRequest {id: DatabaseId;user_attribute: string | null;}++export type CheckWorkspacePermissionsRequest = {+ id: DatabaseId;+ cached?: boolean;+};++export type CheckWorkspacePermissionsResponse = {+ status: "ok" | "failed" | "unknown";+ checked_at: string;+ error?: string;+};diff --git a/frontend/src/metabase-types/api/dependencies.ts b/frontend/src/metabase-types/api/dependencies.tsindex 70b417d4257..51f4919ee75 100644--- a/frontend/src/metabase-types/api/dependencies.ts+++ b/frontend/src/metabase-types/api/dependencies.ts@@ -13,6 +13,7 @@ import type { NativeQuerySnippet } from "./snippets";import type { SortDirection } from "./sorting";import type { Table, TableId } from "./table";import type { Transform } from "./transform";+import type { WorkspaceTransform } from "./workspace";export type DependencyId = number;@@ -20,6 +21,7 @@ export const DEPENDENCY_TYPES = ["card","table","transform",+ "workspace-transform","snippet","dashboard","document",@@ -35,6 +37,7 @@ export const DEPENDENCY_GROUP_TYPES = ["metric","table","transform",+ "workspace-transform","snippet","dashboard","document",@@ -70,13 +73,25 @@ export type TableDependencyNodeData = Pick<| "fields"| "transform"| "owner"->;+> & { table_id?: TableId };export type TransformDependencyNodeData = Pick<Transform,"name" | "description" | "table" | "creator" | "created_at" | "owner">;+export type WorkspaceTransformDependencyNodeData = Pick<+ WorkspaceTransform,+ "name" | "ref_id" | "workspace_id"+> & {+ target?: {+ db: number;+ schema: string;+ table: string;+ table_id?: number | null;+ };+};+export type CardDependencyNodeData = Pick<Card,| "name"@@ -148,6 +163,11 @@ export type TransformDependencyNode = BaseDependencyNode<TransformDependencyNodeData>;+export type WorkspaceTransformDependencyNode = BaseDependencyNode<+ "workspace-transform",+ WorkspaceTransformDependencyNodeData+>;+export type CardDependencyNode = BaseDependencyNode<"card",CardDependencyNodeData@@ -200,6 +220,7 @@ export type MeasureDependencyNode = BaseDependencyNode<export type DependencyNode =| TableDependencyNode| TransformDependencyNode+ | WorkspaceTransformDependencyNode| CardDependencyNode| SnippetDependencyNode| DashboardDependencyNodediff --git a/frontend/src/metabase-types/api/index.ts b/frontend/src/metabase-types/api/index.tsindex b86262a0b62..1499fabc0fe 100644--- a/frontend/src/metabase-types/api/index.ts+++ b/frontend/src/metabase-types/api/index.ts@@ -63,6 +63,7 @@ export * from "./util";export * from "./visualization";export * from "./visualization-settings";export * from "./visualizer";+export * from "./workspace";// ISO8601 timestampexport type ISO8601Time = string;diff --git a/frontend/src/metabase-types/api/metabot.ts b/frontend/src/metabase-types/api/metabot.tsindex 3ed9c1160b1..80ee99d364c 100644--- a/frontend/src/metabase-types/api/metabot.ts+++ b/frontend/src/metabase-types/api/metabot.ts@@ -47,6 +47,8 @@ export type MetabotUserIsViewingContext = Array<export type MetabotChatContext = {user_is_viewing: MetabotUserIsViewingContext;current_time_with_timezone: string;+ default_database_id?: number;+ workspace_id?: number;capabilities: string[];code_editor?: MetabotCodeEditorContext;};diff --git a/frontend/src/metabase-types/api/mocks/database.ts b/frontend/src/metabase-types/api/mocks/database.tsindex 47795d33283..1d32b3e1daa 100644--- a/frontend/src/metabase-types/api/mocks/database.ts+++ b/frontend/src/metabase-types/api/mocks/database.ts@@ -41,6 +41,7 @@ export const createMockDatabase = (opts?: Partial<Database>): Database => ({uploads_enabled: false,uploads_schema_name: null,uploads_table_prefix: null,+ workspace_permissions_status: null,...opts,});diff --git a/frontend/src/metabase-types/api/mocks/index.ts b/frontend/src/metabase-types/api/mocks/index.tsindex c21e81f2d4a..e7053e4b29e 100644--- a/frontend/src/metabase-types/api/mocks/index.ts+++ b/frontend/src/metabase-types/api/mocks/index.ts@@ -44,3 +44,4 @@ export * from "./transform";export * from "./undo";export * from "./user";export * from "./util";+export * from "./workspace";diff --git a/frontend/src/metabase-types/api/mocks/settings.ts b/frontend/src/metabase-types/api/mocks/settings.tsindex b0238276895..8ba51841cbf 100644--- a/frontend/src/metabase-types/api/mocks/settings.ts+++ b/frontend/src/metabase-types/api/mocks/settings.ts@@ -143,6 +143,7 @@ export const createMockTokenFeatures = (data_studio: false,"support-users": false,tenants: false,+ workspaces: false,...opts,});diff --git a/frontend/src/metabase-types/api/mocks/transform.ts b/frontend/src/metabase-types/api/mocks/transform.tsindex b3062b1c42f..e6a34858c57 100644--- a/frontend/src/metabase-types/api/mocks/transform.ts+++ b/frontend/src/metabase-types/api/mocks/transform.ts@@ -79,12 +79,25 @@ export function createMockTransformTarget(}export function createMockTransform(opts?: Partial<Transform>): Transform {+ const source = opts?.source ?? createMockTransformSource();++ function getSourceType() {+ if (source.type === "python") {+ return "python";+ } else if (source.type === "query" && "query" in source) {+ return "native";+ }++ return "mbql";+ }+return {id: 1,name: "Transform",description: null,source: createMockTransformSource(),- target: createMockTransformTarget(),+ source_type: opts?.source_type ?? getSourceType(),+ target: opts?.target ?? createMockTransformTarget(),collection_id: null,created_at: "2000-01-01T00:00:00Z",updated_at: "2000-01-01T00:00:00Z",diff --git a/frontend/src/metabase-types/api/mocks/workspace.ts b/frontend/src/metabase-types/api/mocks/workspace.tsnew file mode 100644index 00000000000..fa39b5b021f--- /dev/null+++ b/frontend/src/metabase-types/api/mocks/workspace.ts@@ -0,0 +1,24 @@+import type { WorkspaceProblem } from "../workspace";++export const createMockWorkspaceProblem = (+ opts?: Partial<WorkspaceProblem>,+): WorkspaceProblem => ({+ category: "internal",+ problem: "not-run",+ severity: "error",+ block_merge: false,+ description: "Test problem",+ data: {+ output: {+ db_id: 1,+ schema: "public",+ table: "test_table",+ },+ transform: {+ type: "workspace-transform",+ id: "1",+ name: "Test Transform",+ },+ },+ ...opts,+});diff --git a/frontend/src/metabase-types/api/settings.ts b/frontend/src/metabase-types/api/settings.tsindex 23be7f59f7f..210be5641f7 100644--- a/frontend/src/metabase-types/api/settings.ts+++ b/frontend/src/metabase-types/api/settings.ts@@ -354,6 +354,7 @@ export const tokenFeatures = ["data_studio","support-users","tenants",+ "workspaces",] as const;export type TokenFeature = (typeof tokenFeatures)[number];diff --git a/frontend/src/metabase-types/api/transform.ts b/frontend/src/metabase-types/api/transform.tsindex 8a1f4061364..b96d9d7c44b 100644--- a/frontend/src/metabase-types/api/transform.ts+++ b/frontend/src/metabase-types/api/transform.ts@@ -4,7 +4,7 @@ import type { PaginationRequest, PaginationResponse } from "./pagination";import type { DatasetQuery } from "./query";import type { ScheduleDisplayType } from "./settings";import type { SortDirection } from "./sorting";-import type { ConcreteTableId, Table } from "./table";+import type { ConcreteTableId, SchemaName, Table } from "./table";import type { UserId, UserInfo } from "./user";export type TransformId = number;@@ -22,6 +22,7 @@ export type Transform = {name: string;description: string | null;source: TransformSource;+ source_type: "native" | "python" | "mbql";target: TransformTarget;collection_id: number | null;created_at: string;@@ -102,15 +103,15 @@ export type TransformTargetType = "table" | "table-incremental";export type TableTarget = {type: "table";name: string;- schema: string | null;- database: number;+ schema: SchemaName | null;+ database: DatabaseId;};export type TableIncrementalTarget = {type: "table-incremental";name: string;- schema: string | null;- database: number;+ schema: SchemaName | null;+ database: DatabaseId;"target-incremental-strategy": TargetIncrementalStrategy;};@@ -118,7 +119,7 @@ export type TransformTarget = TableTarget | TableIncrementalTarget;export type TransformRun = {id: TransformRunId;- status: TransformRunStatus;+ status: TransformRunStatus | null;start_time: string;end_time: string | null;message: string | null;@@ -178,7 +179,7 @@ export type TransformJob = {export type CreateTransformRequest = {name: string;description?: string | null;- source: TransformSource;+ source: DraftTransformSource;target: TransformTarget;tag_ids?: TransformTagId[];collection_id?: number | null;diff --git a/frontend/src/metabase-types/api/workspace.ts b/frontend/src/metabase-types/api/workspace.tsnew file mode 100644index 00000000000..1eb213a524f--- /dev/null+++ b/frontend/src/metabase-types/api/workspace.ts@@ -0,0 +1,565 @@+import type { CollectionId } from "./collection";+import type { DatabaseId } from "./database";+import type { DatasetColumn, ResultsMetadata, RowValues } from "./dataset";+import type {+ DraftTransformSource,+ Transform,+ TransformId,+ TransformSource,+ TransformTagId,+ TransformTarget,+ TransformTargetType,+} from "./transform";++/**+ * A Transform from the global transforms module, tagged for discrimination.+ * Used in workspace context to distinguish from WorkspaceTransform.+ */+export type TaggedTransform = Transform & { type: "transform" };++/**+ * An unsaved transform that exists only in the UI state.+ * Uses a numeric id (negative) for temporary identification.+ */+export type UnsavedTransform = {+ type: "unsaved-transform";+ id: number;+ name: string;+ source: DraftTransformSource;+ target: {+ name: string;+ schema: string | null;+ type: TransformTargetType;+ };+};++export type WorkspaceTransform = Omit<Transform, "id"> & {+ type: "workspace-transform";+ ref_id: WorkspaceTransformId;+ workspace_id: WorkspaceId;+ global_id: TransformId | null;+ target_isolated: TransformTarget;+ target_stale: boolean;+ archived_at: string | null;+ last_run_at: string | null;+ last_run_message: string | null;+ last_run_status: WorkspaceRunStatus | null;+};++export function isTaggedTransform(+ transform:+ | TaggedTransform+ | WorkspaceTransform+ | UnsavedTransform+ | WorkspaceTransformListItem,+): transform is TaggedTransform {+ return "type" in transform && transform.type === "transform";+}++export function isWorkspaceTransform(+ transform:+ | TaggedTransform+ | WorkspaceTransform+ | UnsavedTransform+ | WorkspaceTransformListItem,+): transform is WorkspaceTransform {+ return "type" in transform && transform.type === "workspace-transform";+}++export function isUnsavedTransform(+ transform:+ | TaggedTransform+ | WorkspaceTransform+ | UnsavedTransform+ | WorkspaceTransformListItem,+): transform is UnsavedTransform {+ return "type" in transform && transform.type === "unsaved-transform";+}++export type WorkspaceId = number;++export type Workspace = {+ id: WorkspaceId;+ name: string;+ status: WorkspaceSetupStatus;+ collection_id?: CollectionId | null;+ database_id?: DatabaseId | null;+ created_at?: string;+ updated_at?: string;+};++export type WorkspaceItem = {+ id: WorkspaceId;+ name: string;+ database_id: DatabaseId;+ status: WorkspaceSetupStatus;+ updated_at?: string;+};++export type WorkspaceListResponse = {+ items: WorkspaceItem[];+ limit: number | null;+ offset: number | null;+};++export type CreateWorkspaceRequest = {+ name?: string;+ database_id?: DatabaseId;+};++export type WorkspaceTransformListItem = {+ ref_id: string;+ global_id: TransformId | null;+ name: string;+ source_type: Transform["source_type"] | null;+};++export type WorkspaceTransformListResponse = {+ transforms: WorkspaceTransformListItem[];+};++/**+ * Reason why a transform cannot be checked out to a workspace.+ * null indicates the transform can be checked out.+ */+export type CheckoutDisabledReason =+ | "mbql"+ | "card-reference"+ | "unknown-type"+ | null;++export type ExternalTransform = {+ id: TransformId;+ name: string;+ source_type: Transform["source_type"];+ checkout_disabled: CheckoutDisabledReason;+};++export type ExternalTransformRequest = {+ workspaceId: WorkspaceId;+ databaseId?: DatabaseId | null;+};++export type ExternalTransformResponse = {+ transforms: ExternalTransform[];+};++export type WorkspaceOutputTableRef = {+ transform_id: number | string | null;+ schema: string;+ table: string;+ table_id: number | null;+};++export type WorkspaceTransformId = string;++export type WorkspaceRunStatus = "started" | "succeeded" | "failed" | "timeout";++export type ExistingCheckout = {+ ref_id: string;+ name: string;+};++export type WorkspaceCheckoutStatus = {+ id: WorkspaceId;+ name: string;+ status: WorkspaceSetupStatus;+ existing: ExistingCheckout | null;+};++/** @deprecated Use WorkspaceCheckoutStatus instead */+export type WorkspaceCheckoutItem = {+ id: string;+ name: string;+ workspace: WorkspaceItem;+};++export type WorkspaceCheckoutResponse = {+ checkout_disabled: CheckoutDisabledReason;+ workspaces: WorkspaceCheckoutStatus[];+ /** @deprecated Use workspaces instead */+ transforms: WorkspaceCheckoutItem[];+};++export type WorkspaceMergeTransformResult = {+ op: "create" | "delete" | "update" | "noop";+ global_id: TransformId | null;+ ref_id: string;+ message?: string;+};++export type WorkspaceMergeResponse = {+ merged?: WorkspaceMergeTransformResult[];+ errors?: WorkspaceMergeTransformResult[];+ workspace: WorkspaceItem;+};++export type WorkspaceTransformMergeResponse = WorkspaceMergeTransformResult;++export type ValidateTableNameRequest = {+ id: WorkspaceId;+ db_id: DatabaseId;+ target: {+ type: "table";+ name: string;+ schema: string | null;+ };+};++export type ValidateTableNameResponse =+ | "OK"+ | "A table with that name already exists";++export type CreateWorkspaceTransformRequest = {+ id: WorkspaceId;+ global_id?: TransformId;+ name: string;+ description?: string | null;+ source: DraftTransformSource;+ target: TransformTarget;+ tag_ids?: TransformTagId[];+};++export type UpdateWorkspaceTransformRequest = {+ workspaceId: WorkspaceId;+ transformId: string;+ name?: string;+ description?: string | null;+ source?: TransformSource;+ target?: TransformTarget;+ tag_ids?: TransformTagId[];+};++export type WorkspaceTransformRef = {+ workspaceId: WorkspaceId;+ transformId: string;+};++export type CreateWorkspaceTransformResponse = WorkspaceTransform;++export type WorkspaceInputTable = {+ db_id: DatabaseId;+ schema: string;+ table_id: number | null;+ table: string;+};++export type WorkspaceOutputTableEntry = {+ transform_id: string;+ schema: string;+ table: string;+ table_id: number | null;+};++export type WorkspaceOutputTable = {+ db_id: DatabaseId;+ global: WorkspaceOutputTableEntry;+ isolated: WorkspaceOutputTableEntry;+};++export type WorkspaceTablesResponse = {+ inputs: WorkspaceInputTable[];+ outputs: WorkspaceOutputTable[];+};++// Graph types for React Flow dependency diagram+export type WorkspaceGraphNode = {+ id: string;+ dependents_count: Record<string, number>;+} & (+ | {+ type: "input-table";+ data: {+ db: number;+ id: number;+ schema: string;+ table: string;+ table_id: number;+ };+ }+ | {+ type: "workspace-transform";+ data: {+ ref_id: string;+ name: string;+ target: {+ db: number;+ schema: string;+ table: string;+ table_id: number | null;+ };+ };+ }+);++// Note: entity IDs are strings from backend (node-id returns strings for tables like "1-public-users")+export type WorkspaceGraphEdge = {+ from_entity_type: string;+ from_entity_id: string;+ to_entity_type: string;+ to_entity_id: string;+};++export type WorkspaceGraphResponse = {+ nodes: WorkspaceGraphNode[];+ edges: WorkspaceGraphEdge[];+};++// Workspace graph dependency types that use string IDs (matching backend node-id function)+// These are separate from the standard DependencyNode/DependencyEdge types in dependencies.ts+export type WorkspaceGraphDependencyId = string;++export type WorkspaceGraphTableNodeData = {+ name: string;+ display_name: string;+ description: string | null;+ db_id: number;+ schema: string;+ fields: unknown[];+ table_id?: number;+};++export type WorkspaceGraphTransformNodeData = {+ name: string;+ workspace_id?: WorkspaceId;+ ref_id: string;+ target?: {+ db: number;+ schema: string;+ table: string;+ table_id?: number | null;+ };+};++export type WorkspaceGraphTableNode = {+ id: WorkspaceGraphDependencyId;+ type: "table";+ data: WorkspaceGraphTableNodeData;+ dependents_count?: Record<string, number>;+};++export type WorkspaceGraphTransformNode = {+ id: WorkspaceGraphDependencyId;+ type: "workspace-transform";+ data: WorkspaceGraphTransformNodeData;+ dependents_count?: Record<string, number>;+};++export type WorkspaceGraphDependencyNode =+ | WorkspaceGraphTableNode+ | WorkspaceGraphTransformNode;++export type WorkspaceGraphDependencyEdge = {+ from_entity_id: WorkspaceGraphDependencyId;+ from_entity_type: "table" | "workspace-transform";+ to_entity_id: WorkspaceGraphDependencyId;+ to_entity_type: "table" | "workspace-transform";+};++export type WorkspaceDependencyGraph = {+ nodes: WorkspaceGraphDependencyNode[];+ edges: WorkspaceGraphDependencyEdge[];+};++// Problem types for workspace validation+export type WorkspaceEntityRef =+ | { "entity-type": "workspace-transform"; "entity-id": string }+ | { "entity-type": "global-transform"; "entity-id": number };++export type WorkspaceTableRef = {+ db_id: DatabaseId;+ schema: string;+ table: string;+};++export type WorkspaceProblemCategory =+ | "unused"+ | "internal-downstream"+ | "external-downstream"+ | "internal"+ | "external";++export type WorkspaceProblemType =+ | "not-run"+ | "stale"+ | "failed"+ | "removed-field"+ | "removed-table"+ | "target-conflict"+ | "cycle";++export type WorkspaceProblemSeverity = "error" | "warning" | "info";++// Base types for problem data structures+export type WorkspaceProblemOutput = {+ db_id: DatabaseId;+ schema: string | null;+ table: string;+};++export type WorkspaceProblemTransformRef = {+ type: "workspace-transform" | "external-transform";+ id: string | number;+ name?: string;+};++export type WorkspaceProblemBadRef = {+ type:+ | "validate/missing-column"+ | "validate/missing-table-alias"+ | "validate/duplicate-column"+ | "validate/syntax-error"+ | "validate/validation-exception-error";+ name?: string; // Column/alias name (for missing-column, missing-table-alias, duplicate-column)+ message?: string; // Error message (for validation-exception-error)+};++// Problem data structures matching backend implementation+// See enterprise/backend/src/metabase_enterprise/workspaces/validation.clj++// unused/not-run: Output hasn't been created, nothing depends on it+export type WorkspaceProblemDataUnusedNotRun = {+ output: WorkspaceProblemOutput;+ transform: WorkspaceProblemTransformRef;+};++// internal-downstream/not-run: Output hasn't been created, other workspace transforms need it+export type WorkspaceProblemDataInternalDownstreamNotRun = {+ output: WorkspaceProblemOutput;+ transform: WorkspaceProblemTransformRef;+ dependents: Array<{+ type: "workspace-transform";+ id: string;+ }>;+};++// external-downstream/not-run: Output hasn't been created, external transforms depend on it+export type WorkspaceProblemDataExternalDownstreamNotRun = {+ output: WorkspaceProblemOutput;+ transform: WorkspaceProblemTransformRef;+ dependents: Array<{+ type: "external-transform";+ id: number;+ name: string;+ }>;+};++// external-downstream/removed-field: Field was removed that external transforms reference+export type WorkspaceProblemDataRemovedField = {+ output: WorkspaceProblemOutput;+ transform: {+ type: "external-transform";+ id: number;+ name: string;+ };+ "bad-refs": WorkspaceProblemBadRef[];+};++export type KnownWorkspaceProblemData =+ | WorkspaceProblemDataUnusedNotRun+ | WorkspaceProblemDataInternalDownstreamNotRun+ | WorkspaceProblemDataExternalDownstreamNotRun+ | WorkspaceProblemDataRemovedField;++export type WorkspaceProblemData =+ | KnownWorkspaceProblemData+ | Record<string, unknown>; // For future problem types not yet implemented++export type WorkspaceProblem = {+ category: WorkspaceProblemCategory;+ problem: WorkspaceProblemType;+ severity: WorkspaceProblemSeverity;+ block_merge: boolean;+ description: string;+ data: WorkspaceProblemData;+};++export type WorkspaceLogEntryId = number;++// Status used in workspace log responses (different from archived boolean on Workspace)+export type WorkspaceSetupStatus =+ | "pending"+ | "ready"+ | "archived"+ | "uninitialized"+ | "broken";++export type WorkspaceLogStatus = "started" | "success" | "failure";++export type WorkspaceLogResponse = {+ workspace_id: WorkspaceId;+ status: WorkspaceSetupStatus;+ updated_at: string | null;+ last_completed_at: string | null;+ logs: WorkspaceLogEntry[];+};++export type WorkspaceTask =+ | "workspace-setup"+ | "database-isolation"+ | "mirror-entities"+ | "grant-read-access";++export type WorkspaceLogEntry = {+ id: WorkspaceLogEntryId;+ task: WorkspaceTask | string;+ description: string;+ started_at: string;+ updated_at: string;+ completed_at: string | null;+ status: WorkspaceLogStatus | null;+ message: string | null;+};++export type WorkspaceRunRequest = {+ id: WorkspaceId;+ stale_only?: boolean;+};++export type WorkspaceRunResponse = {+ succeeded: TransformId[];+ failed: TransformId[];+ not_run: TransformId[];+};++export type WorkspaceTransformRunResponse = {+ status: "succeeded" | "failed";+ start_time?: string | null;+ end_time?: string | null;+ message?: string | null;+ table: {+ name: string;+ schema?: string | null;+ };+};++export type WorkspaceTransformDryRunData = {+ rows?: RowValues[];+ cols?: Array<Partial<DatasetColumn>>;+ results_metadata?: ResultsMetadata;+};++export type WorkspaceTransformDryRunResponse = {+ status: "succeeded" | "failed";+ message?: string | null;+ data?: WorkspaceTransformDryRunData;+ /** Logs from Python transform execution (only present for Python transforms) */+ logs?: string;+};++export type WorkspacePermissionsStatus = {+ status: "ok" | "failed" | "unknown";+ checked_at?: string;+ error?: string;+};++export type WorkspaceAllowedDatabase = {+ id: number;+ name: string;+ enabled: boolean;+ workspace_permissions_status?: WorkspacePermissionsStatus;+};++export type WorkspaceAllowedDatabasesResponse = {+ databases: WorkspaceAllowedDatabase[];+};diff --git a/frontend/src/metabase-types/guards/index.ts b/frontend/src/metabase-types/guards/index.tsindex 196d0818197..fc2c396835d 100644--- a/frontend/src/metabase-types/guards/index.ts+++ b/frontend/src/metabase-types/guards/index.ts@@ -2,6 +2,7 @@ export * from "./card";export * from "./click-behavior";export * from "./common";export * from "./dom";-export * from "./settings";export * from "./parameters";export * from "./react";+export * from "./settings";+export * from "./workspace";diff --git a/frontend/src/metabase-types/guards/workspace.ts b/frontend/src/metabase-types/guards/workspace.tsnew file mode 100644index 00000000000..e9da0901a5d--- /dev/null+++ b/frontend/src/metabase-types/guards/workspace.ts@@ -0,0 +1,92 @@+import type {+ KnownWorkspaceProblemData,+ WorkspaceProblemDataExternalDownstreamNotRun,+ WorkspaceProblemDataInternalDownstreamNotRun,+ WorkspaceProblemDataRemovedField,+ WorkspaceProblemDataUnusedNotRun,+} from "metabase-types/api";++import { isObject } from "./common";++export function isKnownWorkspaceProblemData(+ value: unknown,+): value is KnownWorkspaceProblemData {+ return (+ isWorkspaceProblemDataExternalDownstreamNotRun(value) ||+ isWorkspaceProblemDataUnusedNotRun(value) ||+ isWorkspaceProblemDataInternalDownstreamNotRun(value) ||+ isWorkspaceProblemDataExternalDownstreamNotRun(value)+ );+}++export function isWorkspaceProblemDataRemovedField(+ value: unknown,+): value is WorkspaceProblemDataRemovedField {+ return (+ isObject(value) &&+ isObject(value.output) &&+ typeof value.output.db_id === "number" &&+ typeof value.output.table === "string" &&+ isObject(value.transform) &&+ value.transform.type === "external-transform" &&+ typeof value.transform.id === "number" &&+ typeof value.transform.name === "string" &&+ Array.isArray(value["bad-refs"])+ );+}++export function isWorkspaceProblemDataUnusedNotRun(+ value: unknown,+): value is WorkspaceProblemDataUnusedNotRun {+ return (+ isObject(value) &&+ isObject(value.output) &&+ typeof value.output.db_id === "number" &&+ typeof value.output.table === "string" &&+ isObject(value.transform) &&+ (value.transform.type === "workspace-transform" ||+ value.transform.type === "external-transform") &&+ (typeof value.transform.id === "string" ||+ typeof value.transform.id === "number") &&+ !("dependents" in value)+ );+}++export function isWorkspaceProblemDataInternalDownstreamNotRun(+ value: unknown,+): value is WorkspaceProblemDataInternalDownstreamNotRun {+ return (+ isObject(value) &&+ isObject(value.output) &&+ typeof value.output.db_id === "number" &&+ typeof value.output.table === "string" &&+ isObject(value.transform) &&+ Array.isArray(value.dependents) &&+ value.dependents.every(+ (dep: unknown) =>+ isObject(dep) &&+ dep.type === "workspace-transform" &&+ typeof dep.id === "string",+ )+ );+}++export function isWorkspaceProblemDataExternalDownstreamNotRun(+ value: unknown,+): value is WorkspaceProblemDataExternalDownstreamNotRun {+ return (+ isObject(value) &&+ isObject(value.output) &&+ typeof value.output.db_id === "number" &&+ typeof value.output.table === "string" &&+ isObject(value.transform) &&+ Array.isArray(value.dependents) &&+ value.dependents.every(+ (dep: unknown) =>+ isObject(dep) &&+ dep.type === "external-transform" &&+ typeof dep.id === "number" &&+ typeof dep.name === "string",+ )+ );+}diff --git a/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx b/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsxindex a828ab3fb0f..db0d79481e7 100644--- a/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx+++ b/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx@@ -19,6 +19,7 @@ import {PLUGIN_DATABASE_REPLICATION,PLUGIN_DB_ROUTING,PLUGIN_TABLE_EDITING,+ PLUGIN_WORKSPACES,} from "metabase/plugins";import { getUserIsAdmin } from "metabase/selectors/user";import { Box, Divider, Flex } from "metabase/ui";@@ -118,6 +119,12 @@ function DatabaseEditAppInner({updateDatabase={updateDatabase}/>+ <PLUGIN_WORKSPACES.AdminDatabaseWorkspacesSection+ database={database}+ settingsAvailable={settingsAvailable?.settings}+ updateDatabase={updateDatabase}+ />+<PLUGIN_DB_ROUTING.DatabaseRoutingSectiondatabase={database}/>diff --git a/frontend/src/metabase/api/database.ts b/frontend/src/metabase/api/database.tsindex 432097a8cfb..7fae1024e9f 100644--- a/frontend/src/metabase/api/database.ts+++ b/frontend/src/metabase/api/database.ts@@ -5,6 +5,8 @@ import type {AutocompleteSuggestion,CardAutocompleteRequest,CardAutocompleteSuggestion,+ CheckWorkspacePermissionsRequest,+ CheckWorkspacePermissionsResponse,CreateDatabaseRequest,Database,DatabaseId,@@ -96,6 +98,7 @@ export const databaseApi = Api.injectEndpoints({method: "GET",url: `/api/database/${id}/settings-available`,}),+ providesTags: (_response, _error, id) => [idTag("database", id)],}),listDatabaseSchemas: builder.query<SchemaName[],@@ -266,6 +269,18 @@ export const databaseApi = Api.injectEndpoints({invalidatesTags: (_, error) =>invalidateTags(error, [tag("field-values"), tag("parameter-values")]),}),+ checkWorkspacePermissions: builder.mutation<+ CheckWorkspacePermissionsResponse,+ CheckWorkspacePermissionsRequest+ >({+ query: ({ id, cached = true }) => ({+ method: "POST",+ url: `/api/database/${id}/permission/workspace/check`,+ body: { cached },+ }),+ invalidatesTags: (_, error, { id }) =>+ invalidateTags(error, [idTag("database", id)]),+ }),addSampleDatabase: builder.mutation<Database, void>({query: () => ({method: "POST",@@ -323,6 +338,7 @@ export const {useSyncDatabaseSchemaMutation,useRescanDatabaseFieldValuesMutation,useDiscardDatabaseFieldValuesMutation,+ useCheckWorkspacePermissionsMutation,useListAutocompleteSuggestionsQuery,useLazyListAutocompleteSuggestionsQuery,useAddSampleDatabaseMutation,diff --git a/frontend/src/metabase/documents/components/Editor/shared/SuggestionPaper.module.css b/frontend/src/metabase/documents/components/Editor/shared/SuggestionPaper.module.cssindex 020acf22a53..8820b5401fd 100644--- a/frontend/src/metabase/documents/components/Editor/shared/SuggestionPaper.module.css+++ b/frontend/src/metabase/documents/components/Editor/shared/SuggestionPaper.module.css@@ -1,3 +1,5 @@.suggestionPaper {min-height: fit-content;+ max-height: 50vh;+ overflow-y: auto;}diff --git a/frontend/src/metabase/forms/components/FormTextInput/FormTextInput.tsx b/frontend/src/metabase/forms/components/FormTextInput/FormTextInput.tsxindex 6051cc7fc6a..4db949f0acf 100644--- a/frontend/src/metabase/forms/components/FormTextInput/FormTextInput.tsx+++ b/frontend/src/metabase/forms/components/FormTextInput/FormTextInput.tsx@@ -1,4 +1,4 @@-import { useField } from "formik";+import { useField, useFormikContext } from "formik";import type { ChangeEvent, FocusEvent, Ref } from "react";import { forwardRef, useCallback } from "react";@@ -28,6 +28,8 @@ export const FormTextInput = forwardRef(function FormTextInput(const [{ value }, { error, touched }, { setValue, setTouched }] =useField(name);+ const { validateOnMount } = useFormikContext();+const handleChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {const newValue = event.target.value;@@ -62,7 +64,7 @@ export const FormTextInput = forwardRef(function FormTextInput(ref={ref}name={name}value={value ?? ""}- error={touched && error ? error : null}+ error={(validateOnMount || touched) && error ? error : null}onChange={handleChange}onBlur={handleBlur}rightSection={rightSection}diff --git a/frontend/src/metabase/lib/urls/data-studio.ts b/frontend/src/metabase/lib/urls/data-studio.tsindex 20e1fdc422e..bd39b210043 100644--- a/frontend/src/metabase/lib/urls/data-studio.ts+++ b/frontend/src/metabase/lib/urls/data-studio.ts@@ -305,6 +305,20 @@ export function newDataStudioSnippet() {return `${dataStudioLibrary()}/snippets/new`;}+export function dataStudioWorkspaceList() {+ return `${ROOT_URL}/workspaces`;+}++export function dataStudioWorkspace(+ workspaceId: number,+ transformId?: number | string,+) {+ if (transformId) {+ return `${dataStudioWorkspaceList()}/${workspaceId}?transformId=${transformId}`;+ }+ return `${dataStudioWorkspaceList()}/${workspaceId}`;+}+export function dataStudioSegment(segmentId: SegmentId) {return `${dataStudioLibrary()}/segments/${segmentId}`;}diff --git a/frontend/src/metabase/metabot/context.ts b/frontend/src/metabase/metabot/context.tsindex 6ea29be9aee..94ac72762f7 100644--- a/frontend/src/metabase/metabot/context.ts+++ b/frontend/src/metabase/metabot/context.ts@@ -27,6 +27,9 @@ export interface MetabotContext {registerChatContextProvider: (fn: ChatContextProviderFn,) => DeregisterChatContextProviderFn;++ suggestionActions: unknown;+ setSuggestionActions: (actions: unknown) => void;}export const useMetabotContext = () => {diff --git a/frontend/src/metabase/metadata/pages/shared/utils.ts b/frontend/src/metabase/metadata/pages/shared/utils.tsindex 5a16fe18200..7fec852f6b1 100644--- a/frontend/src/metabase/metadata/pages/shared/utils.ts+++ b/frontend/src/metabase/metadata/pages/shared/utils.ts@@ -18,7 +18,7 @@ export function parseRouteParams(params: RouteParams): ParsedRouteParams {}export function getTableMetadataQuery(- tableId: TableId | undefined,+ tableId: TableId | undefined | null,): GetTableQueryMetadataRequest | typeof skipToken {if (tableId == null) {return skipToken;diff --git a/frontend/src/metabase/plugins/index.ts b/frontend/src/metabase/plugins/index.tsindex 8762587a6e9..2412d53e630 100644--- a/frontend/src/metabase/plugins/index.ts+++ b/frontend/src/metabase/plugins/index.ts@@ -70,6 +70,7 @@ export {PLUGIN_DB_ROUTING,PLUGIN_DATABASE_REPLICATION,PLUGIN_TABLE_EDITING,+ PLUGIN_WORKSPACES,} from "./oss/database";export { PLUGIN_EMBEDDING, type SimpleDataPickerProps } from "./oss/embedding";export { PLUGIN_EMBEDDING_IFRAME_SDK } from "./oss/embedding-iframe-sdk";diff --git a/frontend/src/metabase/plugins/oss/ai.ts b/frontend/src/metabase/plugins/oss/ai.tsindex c4d22318ad0..0ed7704eaba 100644--- a/frontend/src/metabase/plugins/oss/ai.ts+++ b/frontend/src/metabase/plugins/oss/ai.ts@@ -23,7 +23,10 @@ import type {import type { State } from "metabase-types/store";export type PluginAiSqlFixer = {- FixSqlQueryButton: ComponentType<Record<string, never>>;+ FixSqlQueryButton: ComponentType<{+ rawSql?: string | null;+ errorMessage?: string | null;+ }>;};export interface AIDashboardAnalysisSidebarProps {@@ -63,6 +66,9 @@ type PluginMetabotType = {hide?: boolean;config?: PluginMetabotConfig;}) => React.ReactElement | null;+ MetabotChat: (props: {+ config?: PluginMetabotConfig;+ }) => React.ReactElement | null;defaultMetabotContextValue: MetabotContext;MetabotContext: React.Context<MetabotContext>;getMetabotProvider: () => ComponentType<{ children: React.ReactNode }>;@@ -126,6 +132,8 @@ const getDefaultMetabotContextValue = (): MetabotContext => ({promptInputRef: undefined,getChatContext: () => ({}) as any,registerChatContextProvider: () => () => {},+ suggestionActions: null,+ setSuggestionActions: () => {},});const defaultMetabotContextValue: MetabotContext =@@ -135,6 +143,8 @@ const getDefaultPluginMetabot = (): PluginMetabotType => ({isEnabled: () => false,Metabot: (_props: { hide?: boolean; config?: PluginMetabotConfig }) =>null as React.ReactElement | null,+ MetabotChat: (_props: { config?: PluginMetabotConfig }) =>+ null as React.ReactElement | null,defaultMetabotContextValue,MetabotContext: React.createContext(defaultMetabotContextValue),getMetabotProvider: () => {diff --git a/frontend/src/metabase/plugins/oss/database.ts b/frontend/src/metabase/plugins/oss/database.tsindex 4f9c363b767..9f799579444 100644--- a/frontend/src/metabase/plugins/oss/database.ts+++ b/frontend/src/metabase/plugins/oss/database.ts@@ -52,6 +52,18 @@ const getDefaultPluginTableEditing = () => ({export const PLUGIN_TABLE_EDITING = getDefaultPluginTableEditing();+const getDefaultPluginWorkspaces = () => ({+ AdminDatabaseWorkspacesSection: PluginPlaceholder as ComponentType<{+ database: DatabaseType;+ settingsAvailable?: Record<string, DatabaseLocalSettingAvailability>;+ updateDatabase: (+ database: { id: DatabaseId } & Partial<DatabaseData>,+ ) => Promise<void>;+ }>,+});++export const PLUGIN_WORKSPACES = getDefaultPluginWorkspaces();+/*** @internal Do not call directly. Use the main reinitialize function from metabase/plugins instead.*/@@ -62,4 +74,5 @@ export function reinitialize() {getDefaultPluginDatabaseReplication(),);Object.assign(PLUGIN_TABLE_EDITING, getDefaultPluginTableEditing());+ Object.assign(PLUGIN_WORKSPACES, getDefaultPluginWorkspaces());}diff --git a/frontend/src/metabase/plugins/oss/transforms.ts b/frontend/src/metabase/plugins/oss/transforms.tsindex 56dbf6a558e..505c5c97aeb 100644--- a/frontend/src/metabase/plugins/oss/transforms.ts+++ b/frontend/src/metabase/plugins/oss/transforms.ts@@ -47,15 +47,26 @@ export type TransformsPlugin = {>;};+export type PythonTransformEditorUiOptions = {+ canChangeDatabase?: boolean;+ readOnly?: boolean;+ hidePreview?: boolean;+ hideRunButton?: boolean;+};+export type PythonTransformEditorProps = {source: PythonTransformSourceDraft;proposedSource?: PythonTransformSourceDraft;+ uiOptions?: PythonTransformEditorUiOptions;isEditMode?: boolean;+ transform?: Transform;readOnly?: boolean;- transformId?: TransformId;onChangeSource: (source: PythonTransformSourceDraft) => void;onAcceptProposed: () => void;onRejectProposed: () => void;+ onRunTransform?: (result: any) => void;+ /** Custom run handler that overrides internal test-run. Used in workspace context for dry-run. */+ onRun?: () => void;};export type PythonTransformSourceSectionProps = {diff --git a/frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.module.css b/frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.module.cssindex f6a0e8b68ef..46485f803f3 100644--- a/frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.module.css+++ b/frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.module.css@@ -2,18 +2,22 @@background-color: var(--mb-color-background-secondary);display: flex;flex-direction: column;+ /* Very crude solution to integrate editor to Workspace Transform Tab */+ flex: var(--native-query-editor-flex, none);width: 100%;}.editorWrapper {overflow: auto;width: 100%;- height: 100%;+ flex: 1 0 auto;+ max-height: 100%;}.resizableArea {display: flex;border-top: 1px solid var(--mb-color-border);+ flex: 1 1 auto;overflow: hidden;flex-direction: column;}diff --git a/frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.tsx b/frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.tsxindex f974bbc0304..85464e13679 100644--- a/frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.tsx+++ b/frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.tsx@@ -344,10 +344,10 @@ export const NativeQueryEditor = forwardRef</><Stack- m="1rem"- mt="auto"display={readOnly ? "none" : undefined}gap="md"+ justify="flex-end"+ p="md">{proposedQuestion && onRejectProposed && onAcceptProposed && (<>diff --git a/frontend/src/metabase/query_builder/components/NativeQueryEditor/ResizableArea/ResizableArea.tsx b/frontend/src/metabase/query_builder/components/NativeQueryEditor/ResizableArea/ResizableArea.tsxindex 412e61e3c63..5d04ba70b66 100644--- a/frontend/src/metabase/query_builder/components/NativeQueryEditor/ResizableArea/ResizableArea.tsx+++ b/frontend/src/metabase/query_builder/components/NativeQueryEditor/ResizableArea/ResizableArea.tsx@@ -89,11 +89,16 @@ export function ResizableArea(props: {maxConstraints={[Infinity, maxHeight]}axis="y"handle={dragHandle}- resizeHandles={["s"]}+ resizeHandles={resizable ? ["s"] : []}onResize={handleResize}onResizeStop={handleResize}>- <Flex w="100%" flex="1" h={height} className={className}>+ <Flex+ w="100%"+ flex="1"+ h={resizable ? height : "100%"}+ className={className}+ >{children}</Flex></Resizable>diff --git a/frontend/src/metabase/query_builder/components/VisualizationError/VisualizationError.tsx b/frontend/src/metabase/query_builder/components/VisualizationError/VisualizationError.tsxindex 2c5b8209672..09525ce0542 100644--- a/frontend/src/metabase/query_builder/components/VisualizationError/VisualizationError.tsx+++ b/frontend/src/metabase/query_builder/components/VisualizationError/VisualizationError.tsx@@ -46,6 +46,7 @@ export function VisualizationError({const isResultDirty = useSelector(getIsResultDirty);const showMetabaseLinks = useSelector(getShowMetabaseLinks);const isNative = question && Lib.queryDisplayInfo(query).isNative;+ const rawSql = isNative ? Lib.rawNativeQuery(query) : null;if (typeof error === "object" && error.status != null) {// Assume if the request took more than 15 seconds it was due to a timeout@@ -113,7 +114,7 @@ export function VisualizationError({if (isNative) {// always show errors for native queries- let processedError = String(error);+ let processedError = typeof error === "string" ? error : error.data;const origSql = getIn(via, [(via || "").length - 1, "ex-data", "sql"]);if (typeof origSql === "string") {processedError = adjustPositions(error, origSql);@@ -143,7 +144,12 @@ export function VisualizationError({{t`Learn how to debug SQL errors`}</ExternalLink>)}- {!isResultDirty && <PLUGIN_AI_SQL_FIXER.FixSqlQueryButton />}+ {!isResultDirty && (+ <PLUGIN_AI_SQL_FIXER.FixSqlQueryButton+ rawSql={rawSql}+ errorMessage={processedError}+ />+ )}</Flex></Flex></Box>diff --git a/frontend/src/metabase/query_builder/components/view/ViewHeader/components/QuestionDataSource/QuestionDataSource.unit.spec.js b/frontend/src/metabase/query_builder/components/view/ViewHeader/components/QuestionDataSource/QuestionDataSource.unit.spec.jsindex 7f27c6e6de8..4100b0a713f 100644--- a/frontend/src/metabase/query_builder/components/view/ViewHeader/components/QuestionDataSource/QuestionDataSource.unit.spec.js+++ b/frontend/src/metabase/query_builder/components/view/ViewHeader/components/QuestionDataSource/QuestionDataSource.unit.spec.js@@ -1,4 +1,4 @@-/* eslint-disable react/display-name, react/prop-types */+/* eslint-disable react/prop-types */import { Component } from "react";import { createMockMetadata } from "__support__/metadata";@@ -254,9 +254,9 @@ function setup({return { onError, question };}-jest.mock("metabase/common/components/Link", () => ({ to: href, ...props }) => (- <a href={href} {...props} />-));+jest.mock("metabase/common/components/Link", () => ({+ Link: ({ to: href, ...props }) => <a href={href} {...props} />,+}));function getQuestionFromUrl(relativeUrl) {const url = new URL(relativeUrl, document.location.href);diff --git a/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditor.tsx b/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditor.tsxindex 5013bb4cf46..4de7dba4e4c 100644--- a/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditor.tsx+++ b/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditor.tsx@@ -6,6 +6,7 @@ import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrimport { NativeQueryPreview } from "metabase/querying/notebook/components/NativeQueryPreview";import { Center, Flex, Modal } from "metabase/ui";import type * as Lib from "metabase-lib";+import type { DatasetQuery } from "metabase-types/api";import { useQueryEditor } from "../../hooks/use-query-editor";import type { QueryEditorUiOptions, QueryEditorUiState } from "../../types";@@ -27,6 +28,7 @@ type QueryEditorProps = {onChangeUiState: (newUiState: QueryEditorUiState) => void;onAcceptProposed?: () => void;onRejectProposed?: () => void;+ onRunQueryStart?: (query: DatasetQuery) => boolean | void;onBlur?: () => void;topBarInnerContent?: ReactNode;height?: string | number;@@ -42,6 +44,7 @@ export function QueryEditor({onChangeUiState,onAcceptProposed,onRejectProposed,+ onRunQueryStart,onBlur,topBarInnerContent,height = "100%",@@ -79,6 +82,7 @@ export function QueryEditor({proposedQuery,onChangeQuery,onChangeUiState,+ onRunQueryStart,});const { ref, height: availableHeight } = useElementSize();@@ -102,10 +106,12 @@ export function QueryEditor({modalSnippet={uiState.modalSnippet}nativeEditorSelectedText={selectedText}readOnly={uiOptions?.readOnly}+ canChangeDatabase={uiOptions?.canChangeDatabase}isNative={isNative}isRunnable={isRunnable}isRunning={isRunning}isResultDirty={isResultDirty}+ resizable={uiOptions?.resizable}isShowingDataReference={uiState.sidebarType === "data-reference"}isShowingSnippetSidebar={uiState.sidebarType === "snippet"}shouldDisableItem={uiOptions?.shouldDisableDataPickerItem}@@ -122,21 +128,25 @@ export function QueryEditor({onChangeNativeEditorSelection={setSelectionRange}onAcceptProposed={onAcceptProposed}onRejectProposed={onRejectProposed}+ editorHeight={uiOptions?.editorHeight}+ hideRunButton={uiOptions?.hideRunButton}onBlur={onBlur}topBarInnerContent={topBarInnerContent}extraButton={extraEditorButton}/>- <QueryEditorVisualization- question={question}- result={result}- rawSeries={rawSeries}- isNative={isNative}- isRunnable={isRunnable}- isRunning={isRunning}- isResultDirty={isResultDirty}- onRunQuery={runQuery}- onCancelQuery={cancelQuery}- />+ {!uiOptions?.hidePreview && (+ <QueryEditorVisualization+ question={question}+ result={result}+ rawSeries={rawSeries}+ isNative={isNative}+ isRunnable={isRunnable}+ isRunning={isRunning}+ isResultDirty={isResultDirty}+ onRunQuery={runQuery}+ onCancelQuery={cancelQuery}+ />+ )}{!isNative && uiOptions?.canConvertToNative && (<NativeQueryPreviewSidebarToggleisNativeQueryPreviewSidebarOpen={diff --git a/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditorBody/QueryEditorBody.tsx b/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditorBody/QueryEditorBody.tsxindex 1f3ed40bf8d..436f8f748c2 100644--- a/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditorBody/QueryEditorBody.tsx+++ b/frontend/src/metabase/querying/editor/components/QueryEditor/QueryEditorBody/QueryEditorBody.tsx@@ -23,7 +23,7 @@ import type {import S from "./QueryEditorBody.module.css";-const EDITOR_HEIGHT = 550;+const DEFAULT_EDITOR_HEIGHT = 550;const NATIVE_HEADER_HEIGHT = 55;const HEADER_HEIGHT = 65 + 50;const PREVIEW_MAX_INITIAL_HEIGHT = 192;@@ -46,6 +46,8 @@ type QueryEditorBodyProps = {| null;nativeEditorSelectedText?: string | null;readOnly?: boolean;+ resizable?: boolean;+ canChangeDatabase?: boolean;isNative: boolean;isRunnable: boolean;isRunning: boolean;@@ -67,6 +69,8 @@ type QueryEditorBodyProps = {onOpenModal: (type: QueryModalType) => void;onAcceptProposed?: () => void;onRejectProposed?: () => void;+ editorHeight?: number;+ hideRunButton?: boolean;topBarInnerContent?: ReactNode;availableHeight?: number;};@@ -78,6 +82,8 @@ export function QueryEditorBody({modalSnippet,nativeEditorSelectedText,readOnly,+ resizable,+ canChangeDatabase,isNative,isRunnable,isRunning,@@ -99,12 +105,18 @@ export function QueryEditorBody({onOpenModal,onAcceptProposed,onRejectProposed,+ editorHeight: editorHeightOverride,+ hideRunButton,topBarInnerContent,availableHeight,}: QueryEditorBodyProps) {const [isResizing, setIsResizing] = useState(false);const reportTimezone = useSetting("report-timezone-long");- const editorHeight = useInitialEditorHeight(isNative, readOnly);+ const editorHeight = useInitialEditorHeight(+ isNative,+ readOnly,+ editorHeightOverride,+ );const dataPickerOptions = useMemo(() => ({ shouldDisableItem, shouldDisableDatabase, shouldShowLibrary }),@@ -137,17 +149,19 @@ export function QueryEditorBody({query={query}placeholder="SELECT * FROM TABLE_NAME"hasTopBar- hasRunButton={!readOnly}+ hasRunButton={!readOnly && !hideRunButton}isInitiallyOpenisNativeEditorOpenreadOnly={readOnly}+ resizable={resizable}+ canChangeDatabase={canChangeDatabase}hasParametersList={false}isRunnable={isRunnable}isRunning={isRunning}isResultDirty={isResultDirty}isShowingDataReference={isShowingDataReference}isShowingSnippetSidebar={isShowingSnippetSidebar}- runQuery={onRunQuery}+ runQuery={hideRunButton ? undefined : onRunQuery}cancelQuery={onCancelQuery}databaseIsDisabled={shouldDisableDatabase}setDatasetQuery={handleNativeQueryChange}@@ -165,7 +179,6 @@ export function QueryEditorBody({onRejectProposed={onRejectProposed}topBarInnerContent={topBarInnerContent}extraButton={extraButton}- resizable={!readOnly}/>);}@@ -205,11 +218,19 @@ function getHeaderHeight(isNative: boolean) {return HEADER_HEIGHT;}-function useInitialEditorHeight(isNative: boolean, readOnly?: boolean) {+function useInitialEditorHeight(+ isNative: boolean,+ readOnly?: boolean,+ editorHeightOverride?: number,+) {const { height: windowHeight } = useWindowSize();const headerHeight = getHeaderHeight(isNative);const availableHeight = windowHeight - headerHeight;+ if (editorHeightOverride) {+ return editorHeightOverride;+ }+if (readOnly) {// When read-only, we don't need to split the container to show the query visualization on the bottomreturn availableHeight;@@ -221,5 +242,8 @@ function useInitialEditorHeight(isNative: boolean, readOnly?: boolean) {PREVIEW_MAX_INITIAL_HEIGHT,);- return Math.min(availableHeight - previewInitialHeight, EDITOR_HEIGHT);+ return Math.min(+ availableHeight - previewInitialHeight,+ DEFAULT_EDITOR_HEIGHT,+ );}diff --git a/frontend/src/metabase/querying/editor/hooks/use-query-editor/use-query-editor.ts b/frontend/src/metabase/querying/editor/hooks/use-query-editor/use-query-editor.tsindex 63fec324183..fefe717bee3 100644--- a/frontend/src/metabase/querying/editor/hooks/use-query-editor/use-query-editor.ts+++ b/frontend/src/metabase/querying/editor/hooks/use-query-editor/use-query-editor.ts@@ -1,4 +1,5 @@import * as Lib from "metabase-lib";+import type { DatasetQuery } from "metabase-types/api";import type { QueryEditorUiOptions, QueryEditorUiState } from "../../types";import { useQueryControls } from "../use-query-controls";@@ -13,6 +14,7 @@ type UseQueryEditorProps = {proposedQuery?: Lib.Query;onChangeQuery: (newQuery: Lib.Query) => void;onChangeUiState: (newUiState: QueryEditorUiState) => void;+ onRunQueryStart?: (query: DatasetQuery) => boolean | void;};export function useQueryEditor({@@ -22,6 +24,7 @@ export function useQueryEditor({proposedQuery,onChangeQuery,onChangeUiState,+ onRunQueryStart,}: UseQueryEditorProps) {const { question, proposedQuestion, setQuestion } = useQueryQuestion(query,@@ -38,7 +41,7 @@ export function useQueryEditor({isResultDirty,runQuery,cancelQuery,- } = useQueryResults(question, uiState, onChangeUiState);+ } = useQueryResults(question, uiState, onChangeUiState, onRunQueryStart);const {selectedText,openModal,diff --git a/frontend/src/metabase/querying/editor/hooks/use-query-results/use-query-results.ts b/frontend/src/metabase/querying/editor/hooks/use-query-results/use-query-results.tsindex 5290f644447..ccc7c0c7ecc 100644--- a/frontend/src/metabase/querying/editor/hooks/use-query-results/use-query-results.ts+++ b/frontend/src/metabase/querying/editor/hooks/use-query-results/use-query-results.ts@@ -4,6 +4,7 @@ import { useLazyGetAdhocQueryQuery } from "metabase/api";import * as Lib from "metabase-lib";import Question from "metabase-lib/v1/Question";import { normalizeParameters } from "metabase-lib/v1/parameters/utils/parameter-values";+import type { DatasetQuery } from "metabase-types/api";import type { QueryEditorUiState } from "../../types";@@ -11,6 +12,7 @@ export function useQueryResults(question: Question,uiState: QueryEditorUiState,onChangeUiState: (newUiState: QueryEditorUiState) => void,+ onRunQueryStart?: (query: DatasetQuery) => boolean | void,) {const { lastRunResult, lastRunQuery } = uiState;const [runAdhocQuery, { isFetching: isRunning = false }] =@@ -48,6 +50,11 @@ export function useQueryResults(const runQuery = async () => {const lastRunQuery = question.datasetQuery();+ const shouldRunQuery = onRunQueryStart?.(lastRunQuery) !== false;+ if (!shouldRunQuery) {+ onChangeUiState({ ...uiState, lastRunResult: null, lastRunQuery });+ return;+ }const action = runAdhocQuery({...lastRunQuery,parameters: normalizeParameters(question.parameters()),diff --git a/frontend/src/metabase/querying/editor/types.ts b/frontend/src/metabase/querying/editor/types.tsindex 8c657b699fa..c31e0c2adf3 100644--- a/frontend/src/metabase/querying/editor/types.ts+++ b/frontend/src/metabase/querying/editor/types.ts@@ -43,6 +43,7 @@ export type QueryEditorUiOptions = {cardType?: CardType;cardDisplay?: CardDisplayType;cardVizSettings?: VisualizationSettings;+ canChangeDatabase?: boolean;readOnly?: boolean;canConvertToNative?: boolean;convertToNativeTitle?: string;@@ -51,5 +52,9 @@ export type QueryEditorUiOptions = {shouldDisableDatabasePickerItem?: (item: QueryEditorDatabasePickerItem,) => boolean;+ editorHeight?: number;shouldShowLibrary?: false;+ hidePreview?: boolean;+ hideRunButton?: boolean;+ resizable?: boolean;};diff --git a/frontend/src/metabase/rich_text_editing/tiptap/extensions/shared/useEntitySuggestions.ts b/frontend/src/metabase/rich_text_editing/tiptap/extensions/shared/useEntitySuggestions.tsindex f9f710c89ff..8766d4206b7 100644--- a/frontend/src/metabase/rich_text_editing/tiptap/extensions/shared/useEntitySuggestions.ts+++ b/frontend/src/metabase/rich_text_editing/tiptap/extensions/shared/useEntitySuggestions.ts@@ -183,6 +183,12 @@ export function useEntitySuggestions({hasMatchingFilteredModels &&(searchModels?.length ?? 0) > 1;+ const shouldFetchRecents =+ enabled &&+ query.length === 0 &&+ !isInModelSelectionMode &&+ !selectedSearchModel;+const {menuItems: entityMenuItems,isLoading,@@ -193,11 +199,7 @@ export function useEntitySuggestions({onSelectSearchResult: handleSearchResultSelect,onSelectUser: handleUserSelect,enabled: enabled && !isInModelSelectionMode,- shouldFetchRecents:- enabled &&- query.length === 0 &&- !isInModelSelectionMode &&- !selectedSearchModel,+ shouldFetchRecents,searchModels: effectiveSearchModels,searchOptions,});diff --git a/frontend/src/metabase/selectors/routing.ts b/frontend/src/metabase/selectors/routing.tsindex 81e43d64a76..97b803c017e 100644--- a/frontend/src/metabase/selectors/routing.ts+++ b/frontend/src/metabase/selectors/routing.ts@@ -1,6 +1,16 @@+import { createSelector } from "@reduxjs/toolkit";+import type { State } from "metabase-types/store";const DEFAULT_LOCATION = { pathname: "" };export const getLocation = (state: State) =>state.routing?.locationBeforeTransitions ?? DEFAULT_LOCATION;++/**+ * Returns true if the current route is within a workspace context.+ * Workspaces are accessed via /data-studio/workspaces/:workspaceId+ */+export const getIsWorkspace = createSelector([getLocation], (location) => {+ return location.pathname?.includes("/data-studio/workspaces/") ?? false;+});diff --git a/frontend/test/__support__/server-mocks/index.ts b/frontend/test/__support__/server-mocks/index.tsindex 05b894e21af..3a5ed7d2a40 100644--- a/frontend/test/__support__/server-mocks/index.ts+++ b/frontend/test/__support__/server-mocks/index.ts@@ -55,3 +55,4 @@ export * from "./transform";export * from "./user";export * from "./user-key-value";export * from "./util";+export * from "./workspace";diff --git a/frontend/test/__support__/server-mocks/workspace.ts b/frontend/test/__support__/server-mocks/workspace.tsnew file mode 100644index 00000000000..416bf9adbc4--- /dev/null+++ b/frontend/test/__support__/server-mocks/workspace.ts@@ -0,0 +1,42 @@+import fetchMock from "fetch-mock";++import type {+ Workspace,+ WorkspaceCheckoutResponse,+ WorkspaceId,+ WorkspaceItem,+ WorkspaceListResponse,+ WorkspaceProblem,+} from "metabase-types/api";++export function setupWorkspaceProblemsEndpoint(+ workspaceId: WorkspaceId,+ problems: WorkspaceProblem[] | Promise<WorkspaceProblem[]> = [],+) {+ fetchMock.get(`path:/api/ee/workspace/${workspaceId}/problem`, problems);+}++export function setupWorkspacesEndpoint(workspaces: WorkspaceItem[] = []) {+ const response: WorkspaceListResponse = {+ items: workspaces,+ limit: null,+ offset: null,+ };+ fetchMock.get("path:/api/ee/workspace", response);+}++export function setupWorkspaceCheckoutEndpoint(+ checkout: Partial<WorkspaceCheckoutResponse> = {},+) {+ const response: WorkspaceCheckoutResponse = {+ checkout_disabled: null,+ workspaces: [],+ transforms: [],+ ...checkout,+ };+ fetchMock.get("express:/api/ee/workspace/checkout", response);+}++export function setupCreateWorkspaceEndpoint(workspace: Workspace) {+ fetchMock.post("path:/api/ee/workspace", workspace);+}diff --git a/modules/drivers/bigquery-cloud-sdk/deps.edn b/modules/drivers/bigquery-cloud-sdk/deps.ednindex 7e6ea6be804..b5906fd7b41 100644--- a/modules/drivers/bigquery-cloud-sdk/deps.edn+++ b/modules/drivers/bigquery-cloud-sdk/deps.edn@@ -10,6 +10,13 @@;; netty is updated because of CVEio.netty/netty-commonio.netty/netty-buffer]}+ ;; Required for service account impersonation in workspace isolation+ ;; Version matches what google-cloud-bigquery depends on+ com.google.auth/google-auth-library-oauth2-http {:mvn/version "1.40.0"}+ ;; Required for granting project-level IAM roles to workspace service accounts+ com.google.cloud/google-cloud-resourcemanager {:mvn/version "1.82.0"}+ ;; Required for creating service accounts for workspace isolation+ com.google.cloud/google-iam-admin {:mvn/version "3.54.0"};; pin arrow formats to 17 for cve about parquet filesorg.apache.arrow/arrow-format {:mvn/version "18.3.0"}org.apache.arrow/arrow-memory-core {:mvn/version "18.3.0"}diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.cljindex b24c184fdf3..6472c192838 100644--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj@@ -31,16 +31,23 @@(:import(clojure.lang PersistentList)(com.google.api.gax.rpc FixedHeaderProvider)+ (com.google.auth.oauth2 ImpersonatedCredentials ServiceAccountCredentials)+ (com.google.cloud Identity Role)(com.google.cloud.bigquery+ Acl Acl$Role Acl$UserBigQuery+ BigQuery$DatasetDeleteOptionBigQuery$DatasetListOption+ BigQuery$DatasetOption+ BigQuery$IAMOptionBigQuery$JobOptionBigQuery$QueryResultsOptionBigQuery$TableDataListOptionBigQuery$TableOptionBigQueryException- BigQueryOptions+ BigQueryOptions BigQueryOptions$BuilderDataset+ DatasetId DatasetInfoFieldField$ModeFieldValue@@ -54,8 +61,13 @@TableDefinition$TypeTableIdTableResult)+ (com.google.cloud.iam.admin.v1 IAMClient IAMSettings)+ (com.google.cloud.resourcemanager.v3 ProjectsClient ProjectsSettings)(com.google.common.collect ImmutableMap)(com.google.gson JsonParser)+ (com.google.iam.admin.v1 CreateServiceAccountRequest DeleteServiceAccountRequest ServiceAccount)+ (com.google.iam.v1 Binding Policy SetIamPolicyRequest GetIamPolicyRequest)+ (java.io ByteArrayInputStream)(java.util Iterator)))(set! *warn-on-reflection* true)@@ -77,15 +89,29 @@(mu/defn- database-details->client^BigQuery [details :- :map]- (let [creds (bigquery.common/database-details->service-account-credential details)- mb-version (:tag driver-api/mb-version-info)- run-mode (name driver-api/run-mode)- user-agent (format "Metabase/%s (GPN:Metabase; %s)" mb-version run-mode)+ (let [base-creds (bigquery.common/database-details->service-account-credential details)+ ;; Check if we should impersonate a different service account+ ;; ImpersonatedCredentials automatically refreshes tokens before expiration,+ ;; so the 1-hour lifetime is just the initial token validity period.+ ;; Each query creates a fresh client, and the credentials handle refresh internally.+ final-creds (if-let [target-sa (:impersonate-service-account details)]+ (do+ (log/debugf "Creating impersonated credentials for service account: %s" target-sa)+ (ImpersonatedCredentials/create+ (.createScoped base-creds bigquery-scopes)+ target-sa+ nil ;; delegates (not needed)+ (java.util.ArrayList. bigquery-scopes)+ 3600)) ;; 1 hour token lifetime+ (.createScoped base-creds bigquery-scopes))+ mb-version (:tag driver-api/mb-version-info)+ run-mode (name driver-api/run-mode)+ user-agent (format "Metabase/%s (GPN:Metabase; %s)" mb-version run-mode)header-provider (FixedHeaderProvider/create(ImmutableMap/of "user-agent" user-agent))- bq-bldr (doto (BigQueryOptions/newBuilder)- (.setCredentials (.createScoped creds bigquery-scopes))- (.setHeaderProvider header-provider))]+ bq-bldr (doto (BigQueryOptions/newBuilder)+ (.setCredentials final-creds)+ (.setHeaderProvider header-provider))](when-let [host (not-empty (:host details))](.setHost bq-bldr host))(.. bq-bldr build getService)))@@ -746,13 +772,15 @@{:pre [(map? database) (map? (:details database))]};; automatically retry the query if it times out or otherwise fails. This is on top of the auto-retry added by;; `execute`- (let [thunk (fn []- (execute-bigquery- respond- (:details database)- sql- parameters- cancel-chan))]+ ;; Apply any swapped connection details (e.g., for workspace isolation)+ (let [details (driver/maybe-swap-details (:id database) (:details database))+ thunk (fn []+ (execute-bigquery+ respond+ details+ sql+ parameters+ cancel-chan))](try(thunk)(catch Throwable e@@ -803,7 +831,9 @@:set-timezone true:split-part true:transforms/python true- :transforms/table true}]+ :transforms/table true+ ;; Workspace isolation using service account impersonation+ :workspace false}](defmethod driver/database-supports? [:bigquery-cloud-sdk feature] [_driver _feature _db] supported?));; BigQuery is always in UTC@@ -1011,7 +1041,7 @@(defmethod driver/connection-spec :bigquery-cloud-sdk[_driver database];; Return the database details directly since we don't use a JDBC spec for bigquery- (:details database))+ (driver/maybe-swap-details (:id database) (:details database)))(defmethod driver.sql/default-schema :bigquery-cloud-sdk[_]@@ -1036,8 +1066,17 @@(defmethod driver/create-schema-if-needed! :bigquery-cloud-sdk[driver conn-spec schema]- (let [sql [[(format "CREATE SCHEMA IF NOT EXISTS `%s`;" schema)]]]- (driver/execute-raw-queries! driver conn-spec sql)))+ ;; Check if dataset exists using the BigQuery API before trying to create.+ ;; This is important for workspace isolation where the impersonated SA has+ ;; access to an existing isolated dataset but cannot create new datasets.+ (let [details (get conn-spec :details conn-spec)+ client (database-details->client details)+ project-id (get-project-id details)+ dataset-id (DatasetId/of project-id schema)]+ (when-not (.getDataset client dataset-id (u/varargs BigQuery$DatasetOption))+ ;; Dataset doesn't exist, try to create it+ (let [sql [[(format "CREATE SCHEMA IF NOT EXISTS `%s`;" schema)]]]+ (driver/execute-raw-queries! driver conn-spec sql)))))(defmethod driver/schema-exists? :bigquery-cloud-sdk[_driver db-id schema]@@ -1053,5 +1092,417 @@;; https://cloud.google.com/bigquery/docs/tables1024)+;;; +----------------------------------------------------------------------------------------------------------------++;;; | Workspace Isolation |+;;; +----------------------------------------------------------------------------------------------------------------++;;; BigQuery workspace isolation uses service account impersonation instead of SQL GRANT statements.+;;; Each workspace gets its own service account (created automatically) with table-level read permissions.+;;;+;;; Required GCP setup for the main service account:+;;; - Roles: `roles/bigquery.admin`, `roles/iam.serviceAccountAdmin`, `roles/resourcemanager.projectIamAdmin`+;;; - APIs: `bigquery.googleapis.com`, `iam.googleapis.com`, `cloudresourcemanager.googleapis.com`++(defn- ws-service-account-credentials+ "Parse ServiceAccountCredentials from database details."+ ^ServiceAccountCredentials [{:keys [service-account-json]}]+ (ServiceAccountCredentials/fromStream+ (ByteArrayInputStream. (.getBytes ^String service-account-json))))++(defn- ws-database-details->client+ "Create a BigQuery client from database details for workspace isolation."+ ^BigQuery [details]+ (let [creds (.createScoped (ws-service-account-credentials details)+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/bigquery")))]+ (-> (BigQueryOptions/newBuilder)+ ^BigQueryOptions$Builder (.setCredentials creds)+ ^BigQueryOptions (.build)+ (.getService))))++(defn- ws-database-details->iam-client+ "Create an IAM Admin client from database details."+ ^IAMClient [details]+ (let [creds (.createScoped (ws-service-account-credentials details)+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/cloud-platform")))]+ (IAMClient/create+ (-> (IAMSettings/newBuilder)+ (.setCredentialsProvider (reify com.google.api.gax.core.CredentialsProvider+ (getCredentials [_] creds)))+ ^IAMSettings (.build)))))++(defn- ws-service-account-id+ "Generate the service account ID for a workspace (max 30 chars, lowercase, alphanumeric + hyphens)."+ [workspace]+ ;; Format: mb-ws-{workspace-id} truncated to 30 chars+ (let [ws-id (str (:id workspace))+ sa-id (str "mb-ws-" ws-id)]+ (-> sa-id+ (subs 0 (min 30 (count sa-id)))+ u/lower-case-en)))++(defn- ws-service-account-exists?+ "Check if a service account exists."+ [^IAMClient iam-client ^String project-id ^String sa-email]+ (try+ (.getServiceAccount iam-client (format "projects/%s/serviceAccounts/%s" project-id sa-email))+ true+ (catch com.google.api.gax.rpc.NotFoundException _+ false)))++(defn- ws-create-service-account!+ "Create a service account for a workspace if it doesn't exist.+ Returns the service account email."+ [^IAMClient iam-client ^String project-id workspace]+ (let [sa-id (ws-service-account-id workspace)+ sa-email (format "%s@%s.iam.gserviceaccount.com" sa-id project-id)+ project-name (format "projects/%s" project-id)]+ ;; Check if already exists first+ (if (ws-service-account-exists? iam-client project-id sa-email)+ (log/debugf "Service account already exists: %s" sa-email)+ (do+ (log/infof "Creating service account %s in project %s" sa-id project-id)+ (let [request (-> (CreateServiceAccountRequest/newBuilder)+ (.setName project-name)+ (.setAccountId sa-id)+ (.setServiceAccount+ (-> (ServiceAccount/newBuilder)+ (.setDisplayName (format "Metabase Workspace %s" (:id workspace)))+ (.setDescription "Auto-created by Metabase for workspace isolation")+ (.build)))+ (.build))]+ (.createServiceAccount iam-client request)+ (log/infof "Created service account: %s" sa-email))))+ sa-email))++(defn- ws-delete-service-account!+ "Delete a service account for a workspace. Idempotent - does nothing if SA doesn't exist."+ [^IAMClient iam-client ^String project-id workspace]+ (let [sa-id (ws-service-account-id workspace)+ sa-email (format "%s@%s.iam.gserviceaccount.com" sa-id project-id)+ sa-name (format "projects/%s/serviceAccounts/%s" project-id sa-email)]+ (when (ws-service-account-exists? iam-client project-id sa-email)+ (log/infof "Deleting service account %s" sa-email)+ (let [request (-> (DeleteServiceAccountRequest/newBuilder)+ (.setName sa-name)+ (.build))]+ (.deleteServiceAccount iam-client request)+ (log/infof "Deleted service account: %s" sa-email)))))++(defn- ws-has-role-binding?+ "Check if a policy already has a binding for the given role and member."+ [^Policy policy ^String role ^String member]+ (some (fn [^Binding binding]+ (and (= (.getRole binding) role)+ (some #(= % member) (.getMembersList binding))))+ (.getBindingsList policy)))++(defn- ws-grant-impersonation-permission!+ "Grant the main service account permission to impersonate the workspace service account."+ [^IAMClient iam-client ^String project-id ^String main-sa-email ^String workspace-sa-email]+ (let [resource (format "projects/%s/serviceAccounts/%s" project-id workspace-sa-email)+ role "roles/iam.serviceAccountTokenCreator"+ member (format "serviceAccount:%s" main-sa-email)]+ (try+ ;; Get current policy+ (let [current-policy (.getIamPolicy iam-client resource)]+ ;; Only add if not already granted+ (when-not (ws-has-role-binding? current-policy role member)+ (let [new-binding (-> (Binding/newBuilder)+ (.setRole role)+ (.addMembers member)+ (.build))+ updated-policy (-> (Policy/newBuilder current-policy)+ (.addBindings new-binding)+ (.build))+ request (-> (SetIamPolicyRequest/newBuilder)+ (.setResource resource)+ (.setPolicy updated-policy)+ (.build))]+ (.setIamPolicy iam-client request)+ (log/infof "Granted impersonation permission on %s to %s" workspace-sa-email main-sa-email))))+ (catch Exception e+ (log/warn e "Failed to grant impersonation permission (may already exist)")))))++(defn- ws-grant-project-role!+ "Grant a project-level IAM role to a service account.+ This is needed for roles like bigquery.jobUser that must be granted at project level."+ [details ^String project-id ^String service-account-email ^String role]+ (log/infof "Granting project role %s to %s" role service-account-email)+ (let [creds (.createScoped (ws-service-account-credentials details)+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/cloud-platform")))+ settings (-> (ProjectsSettings/newBuilder)+ (.setCredentialsProvider (reify com.google.api.gax.core.CredentialsProvider+ (getCredentials [_] creds)))+ ^ProjectsSettings (.build))+ projects-client (ProjectsClient/create settings)+ resource (format "projects/%s" project-id)+ member (format "serviceAccount:%s" service-account-email)]+ (try+ ;; Get current policy+ (let [get-request (-> (GetIamPolicyRequest/newBuilder)+ (.setResource resource)+ (.build))+ current-policy (.getIamPolicy projects-client get-request)]+ ;; Only add if not already granted+ (when-not (ws-has-role-binding? current-policy role member)+ (let [new-binding (-> (Binding/newBuilder)+ (.setRole role)+ (.addMembers member)+ (.build))+ updated-policy (-> (Policy/newBuilder current-policy)+ (.addBindings new-binding)+ (.build))+ set-request (-> (SetIamPolicyRequest/newBuilder)+ (.setResource resource)+ (.setPolicy updated-policy)+ (.build))]+ (.setIamPolicy projects-client set-request)+ (log/infof "Granted %s on project %s to %s" role project-id service-account-email))))+ (finally+ (.close projects-client)))))++(defn- ws-wait-for-impersonation-ready!+ "Poll until impersonation is working. GCP IAM changes can take up to 60 seconds to propagate.+ Tests by actually creating impersonated credentials and making a simple API call."+ [details ^String target-sa-email & {:keys [max-attempts interval-ms]+ :or {max-attempts 120+ interval-ms 1000}}]+ (log/info "Waiting for IAM impersonation to be ready...")+ (let [base-creds (.createScoped (ws-service-account-credentials details)+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/bigquery")))+ project-id (get-project-id details)]+ (loop [attempt 1]+ (log/debugf "Checking impersonation readiness (attempt %d/%d)" attempt max-attempts)+ (let [result (try+ ;; Try to create impersonated credentials and use them+ (let [impersonated (ImpersonatedCredentials/create+ base-creds+ target-sa-email+ nil ;; delegates+ (doto (java.util.ArrayList.)+ (.add "https://www.googleapis.com/auth/bigquery"))+ 3600)+ client (-> (BigQueryOptions/newBuilder)+ ^BigQueryOptions$Builder (.setCredentials impersonated)+ ^BigQueryOptions (.build)+ ^BigQuery (.getService))]+ ;; Try a simple operation - list datasets (limited to 1)+ (.listDatasets client project-id (into-array BigQuery$DatasetListOption []))+ :ready)+ (catch Exception e+ (if (or (str/includes? (str (ex-message e)) "permission")+ (str/includes? (str (ex-message e)) "403")+ (str/includes? (str (ex-message e)) "Access Denied"))+ :not-ready+ (do+ (log/debugf "Unexpected error checking impersonation: %s" (ex-message e))+ :not-ready))))]+ (cond+ (= result :ready)+ (log/info "IAM impersonation is ready")++ (>= attempt max-attempts)+ (throw (ex-info "Timeout waiting for IAM impersonation to propagate"+ {:target-sa target-sa-email+ :attempts attempt}))++ :else+ (do+ (Thread/sleep ^long interval-ms)+ (recur (inc attempt))))))))++(defn- ws-role-name->acl-role+ "Convert a BigQuery IAM role name to an Acl$Role."+ ^Acl$Role [^String role-name]+ (case role-name+ "roles/bigquery.dataEditor" Acl$Role/WRITER+ "roles/bigquery.dataViewer" Acl$Role/READER+ "roles/bigquery.dataOwner" Acl$Role/OWNER+ (throw (ex-info (str "Unknown role: " role-name) {:role role-name}))))++(defn- ws-has-acl-entry?+ "Check if an ACL list already has an entry for the given entity and role."+ [acl-list ^Acl$User entity ^Acl$Role role]+ (some (fn [^Acl acl]+ (and (= (.getEntity acl) entity)+ (= (.getRole acl) role)))+ acl-list))++(defn- ws-grant-dataset-acl!+ "Grant an ACL role on a dataset to a service account."+ [^BigQuery client ^DatasetId dataset-id ^String service-account-email ^String role-name]+ (log/debugf "Granting %s on dataset %s to %s" role-name dataset-id service-account-email)+ (let [dataset ^Dataset (.getDataset client dataset-id+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"+ (into-array BigQuery$DatasetOption []))+ current-acl (into [] (.getAcl dataset))+ acl-role (ws-role-name->acl-role role-name)+ acl-user (Acl$User. service-account-email)]+ ;; Only add if not already granted+ (when-not (ws-has-acl-entry? current-acl acl-user acl-role)+ (let [new-acl-entry (Acl/of acl-user acl-role)+ updated-acl (conj current-acl new-acl-entry)+ updated-dataset (-> (.toBuilder dataset)+ (.setAcl ^"clojure.lang.PersistentVector" updated-acl)+ (.build))]+ (.update client updated-dataset+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"+ (into-array BigQuery$DatasetOption []))))))++(defn- ws-has-table-iam-binding?+ "Check if a table IAM policy already has a binding for the given role and identity."+ [^com.google.cloud.Policy policy ^Role role ^Identity identity]+ (let [bindings (.getBindings policy)+ identity-set (clojure.core/get bindings role)]+ (and identity-set (.contains ^java.util.Set identity-set identity))))++(defn- ws-grant-table-read-access!+ "Grant read access on a specific table to a service account using table-level IAM."+ [^BigQuery client ^TableId table-id ^String service-account-email]+ (log/debugf "Granting read access on table %s to %s" table-id service-account-email)+ (let [current-policy (.getIamPolicy client table-id (into-array BigQuery$IAMOption []))+ role (Role/of "roles/bigquery.dataViewer")+ sa-identity (Identity/serviceAccount service-account-email)]+ ;; Only add if not already granted+ (when-not (ws-has-table-iam-binding? current-policy role sa-identity)+ (let [updated-policy (-> current-policy+ (.toBuilder)+ (.addIdentity role sa-identity (into-array Identity []))+ (.build))]+ (.setIamPolicy client table-id updated-policy (into-array BigQuery$IAMOption []))))))++(defmethod driver/init-workspace-isolation! :bigquery-cloud-sdk+ [_driver database workspace]+ (let [details (:details database)+ client (ws-database-details->client details)+ iam-client (ws-database-details->iam-client details)+ project-id (get-project-id details)+ main-sa-email (.getClientEmail (ws-service-account-credentials details))+ dataset-name (driver.u/workspace-isolation-namespace-name workspace)]++ (try+ ;; Create the workspace service account (or get existing)+ (let [ws-sa-email (ws-create-service-account! iam-client project-id workspace)+ dataset-id (DatasetId/of project-id dataset-name)]++ (log/infof "Initializing BigQuery workspace isolation: dataset=%s, service-account=%s"+ dataset-name ws-sa-email)++ ;; Grant main SA permission to impersonate workspace SA+ (ws-grant-impersonation-permission! iam-client project-id main-sa-email ws-sa-email)++ ;; Grant the workspace SA permission to run BigQuery jobs (queries) at project level+ ;; Note: We intentionally do NOT grant project-level dataEditor as that would give+ ;; access to all datasets. The workspace SA only gets dataEditor on its isolated dataset.+ (ws-grant-project-role! details project-id ws-sa-email "roles/bigquery.jobUser")++ ;; Wait for IAM permissions to propagate by polling until impersonation works+ (ws-wait-for-impersonation-ready! details ws-sa-email)++ ;; Create the isolated dataset if it doesn't exist (using main SA credentials, not impersonated)+ (when-not (.getDataset client dataset-id+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"+ (into-array BigQuery$DatasetOption []))+ (let [dataset-info (-> (DatasetInfo/newBuilder dataset-id)+ (.setDescription (format "Metabase workspace isolation for workspace %s" (:id workspace)))+ (.build))]+ (.create client dataset-info+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"+ (into-array BigQuery$DatasetOption []))))++ ;; Grant the workspace service account dataEditor role on the isolated dataset+ ;; dataEditor allows: create/update/delete tables, insert/update/delete data+ (ws-grant-dataset-acl! client dataset-id ws-sa-email "roles/bigquery.dataEditor")++ ;; Return workspace connection details for impersonation+ ;; :user is used by grant-read-access-to-tables! to know which SA to grant access to+ ;; :impersonate-service-account is used by the connection swap to use impersonated credentials+ {:schema dataset-name+ :database_details {:user ws-sa-email+ :impersonate-service-account ws-sa-email}})+ (finally+ (.close iam-client)))))++(defmethod driver/grant-workspace-read-access! :bigquery-cloud-sdk+ [_driver database workspace tables]+ ;; For BigQuery, the workspace contains the service account email in database_details+ (let [ws-sa-email (-> workspace :database_details :impersonate-service-account)+ details (:details database)+ client (ws-database-details->client details)+ project-id (get-project-id details)]++ (log/debugf "Granting read access to %d tables for %s" (count tables) ws-sa-email)++ ;; Grant dataViewer at table level for each table - proper isolation+ (doseq [{:keys [schema name]} tables]+ (let [table-id (TableId/of project-id schema name)]+ (ws-grant-table-read-access! client table-id ws-sa-email)))))++(def ^:private perm-check-workspace-id "00000000-0000-0000-0000-000000000000")++(defmethod driver/check-isolation-permissions :bigquery-cloud-sdk+ [driver database test-table]+ ;; BigQuery uses GCP IAM APIs instead of SQL, so we can't use transaction rollback.+ ;; We run the actual init/grant/destroy operations and clean up immediately.+ (let [test-workspace {:id perm-check-workspace-id+ :name "_mb_perm_check_"}]+ (try+ (let [init-result (try+ (driver/init-workspace-isolation! driver database test-workspace)+ (catch Exception e+ (throw (ex-info (format "Failed to initialize workspace isolation: %s" (ex-message e))+ {:step :init} e))))+ workspace-with-details (merge test-workspace init-result)]+ (when test-table+ (try+ (driver/grant-workspace-read-access! driver database workspace-with-details [test-table])+ (catch Exception e+ (throw (ex-info (format "Failed to grant read access to table %s.%s: %s"+ (:schema test-table) (:name test-table) (ex-message e))+ {:step :grant :table test-table} e)))))+ (try+ (driver/destroy-workspace-isolation! driver database workspace-with-details)+ (catch Exception e+ (throw (ex-info (format "Failed to destroy workspace isolation: %s" (ex-message e))+ {:step :destroy} e)))))+ nil+ (catch Exception e+ (ex-message e))+ (finally+ (try+ (driver/destroy-workspace-isolation! driver database test-workspace)+ (catch Exception _ nil))))))++(defmethod driver/destroy-workspace-isolation! :bigquery-cloud-sdk+ [_driver database workspace]+ (let [details (:details database)+ client (ws-database-details->client details)+ iam-client (ws-database-details->iam-client details)+ project-id (get-project-id details)+ dataset-name (driver.u/workspace-isolation-namespace-name workspace)+ dataset-id (DatasetId/of project-id dataset-name)]+ (try+ (log/infof "Destroying BigQuery workspace isolation: dataset=%s" dataset-name)++ ;; Delete the dataset if it exists (deleteContents=true removes all tables)+ (when (.getDataset client dataset-id+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetOption;"+ (into-array BigQuery$DatasetOption []))+ (log/infof "Deleting dataset %s" dataset-name)+ (.delete client dataset-id+ ^"[Lcom.google.cloud.bigquery.BigQuery$DatasetDeleteOption;"+ (into-array BigQuery$DatasetDeleteOption [(BigQuery$DatasetDeleteOption/deleteContents)]))+ (log/infof "Deleted dataset %s" dataset-name))++ ;; Delete the service account (this also removes its IAM bindings)+ (ws-delete-service-account! iam-client project-id workspace)++ {:success true}+ (finally+ (.close iam-client)))))+(defmethod driver/llm-sql-dialect-resource :bigquery-cloud-sdk [_]"llm/prompts/dialects/bigquery.md")diff --git a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.cljindex 56f1063ed4a..1203d1b87bc 100644--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj@@ -17,11 +17,13 @@[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute][metabase.driver.sql.query-processor :as sql.qp][metabase.driver.sql.util :as sql.u]+ [metabase.driver.util :as driver.u][metabase.util :as u][metabase.util.log :as log])- (:import [com.clickhouse.client.api.query QuerySettings]- [java.sql SQLException PreparedStatement]- [java.time LocalDate]))+ (:import+ (com.clickhouse.client.api.query QuerySettings)+ (java.sql Connection SQLException Statement PreparedStatement)+ (java.time LocalDate)))(set! *warn-on-reflection* true)@@ -43,6 +45,7 @@;; JDBC driver always provides "NO" for the IS_GENERATEDCOLUMN JDBC metadata:describe-is-generated false:describe-is-nullable true+ :workspace true:expression-literals true:expressions/date true:expressions/float true@@ -346,12 +349,63 @@(log/warn "Clickhouse does not support foreign keys. `describe-table-fks` should not have been called!")#{})+(defmethod driver/table-known-to-not-exist? :clickhouse+ [_driver e]+ (instance? SQLException e))+;; Override clickhouse to not pass in the Types/DATE parameter due to jdbc;; driver issue: https://github.com/ClickHouse/clickhouse-java/issues/2701(defmethod sql-jdbc.execute/set-parameter [:clickhouse LocalDate][_ ^PreparedStatement prepared-statement i object](.setObject prepared-statement i object))+;;; ------------------------------------------ Workspace Isolation ------------------------------------------++(defmethod driver/init-workspace-isolation! :clickhouse+ [_driver database workspace]+ (let [db-name (driver.u/workspace-isolation-namespace-name workspace)+ read-user {:user (driver.u/workspace-isolation-user-name workspace)+ :password (driver.u/random-workspace-password)}]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (with-open [stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql [(format "CREATE DATABASE IF NOT EXISTS `%s`" db-name)+ (format "CREATE USER IF NOT EXISTS `%s` IDENTIFIED BY '%s'"+ (:user read-user) (:password read-user))+ (format "GRANT ALL ON `%s`.* TO `%s`" db-name (:user read-user))]]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt)))+ {:schema db-name+ :database_details read-user}))++(defmethod driver/grant-workspace-read-access! :clickhouse+ [_driver database workspace tables]+ (let [read-user-name (-> workspace :database_details :user)+ sqls (for [table tables]+ (format "GRANT SELECT ON `%s`.`%s` TO `%s`"+ (:schema table)+ (:name table)+ read-user-name))]+ (when-not read-user-name+ (throw (ex-info "Workspace isolation is not properly initialized - missing read user name"+ {:workspace-id (:id workspace) :step :grant})))+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (with-open [stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql sqls]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt)))))++(defmethod driver/destroy-workspace-isolation! :clickhouse+ [_driver database workspace]+ (let [db-name (driver.u/workspace-isolation-namespace-name workspace)+ username (driver.u/workspace-isolation-user-name workspace)]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (with-open [stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql [;; DROP DATABASE cascades to all tables within it+ (format "DROP DATABASE IF EXISTS `%s`" db-name)+ (format "DROP USER IF EXISTS `%s`" username)]]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt)))))+(defmethod sql-jdbc.conn/data-warehouse-connection-pool-properties :clickhouse[driver database](mergediff --git a/modules/drivers/mongo/.clj-kondo/.lock b/modules/drivers/mongo/.clj-kondo/.locknew file mode 100644index 00000000000..e69de29bb2ddiff --git a/modules/drivers/mongo/src/metabase/driver/mongo.clj b/modules/drivers/mongo/src/metabase/driver/mongo.cljindex fd2741081f9..14feb25369b 100644--- a/modules/drivers/mongo/src/metabase/driver/mongo.clj+++ b/modules/drivers/mongo/src/metabase/driver/mongo.clj@@ -440,7 +440,8 @@:index-info false:python-transforms true:transforms/python true- :database-routing true}]+ :database-routing true+ :workspace false}](defmethod driver/database-supports? [:mongo feature] [_driver _feature _db] supported?))(defmethod driver/database-supports? [:mongo :schemas] [_driver _feat _db] false)@@ -598,7 +599,7 @@(defmethod driver/connection-spec :mongo[_driver database]- (:details database))+ (driver/maybe-swap-details (:id database) (:details database)))(defmulti ^:private type->database-type"Internal type->database-type multimethod for MongoDB that dispatches on type."diff --git a/modules/drivers/redshift/src/metabase/driver/redshift.clj b/modules/drivers/redshift/src/metabase/driver/redshift.cljindex 1b95e93f830..ee62f01d805 100644--- a/modules/drivers/redshift/src/metabase/driver/redshift.clj+++ b/modules/drivers/redshift/src/metabase/driver/redshift.clj@@ -1,6 +1,7 @@(ns metabase.driver.redshift"Amazon Redshift Driver."(:require+ [clojure.java.jdbc :as jdbc][clojure.string :as str][honey.sql :as sql][java-time.api :as t]@@ -29,6 +30,7 @@PreparedStatementResultSetResultSetMetaData+ StatementTypes)))(set! *warn-on-reflection* true)@@ -53,7 +55,8 @@:transforms/python true:transforms/table true:transforms/index-ddl false- :uuid-type false}]+ :uuid-type false+ :workspace false}](defmethod driver/database-supports? [:redshift feature] [_driver _feat _db] supported?));;; +----------------------------------------------------------------------------------------------------------------+@@ -272,8 +275,7 @@:geometry :type/* ; spatial data:geography :type/* ; spatial data:intervaly2m :type/* ; interval literal- :intervald2s :type/*} ; interval literal- ))+ :intervald2s :type/*})) ; interval literal(defmethod sql-jdbc.sync/database-type->base-type :redshift[driver column-type]@@ -695,5 +697,62 @@;; 42P01: undefined_table, 3F000: invalid_schema_name(contains? #{"42P01" "3F000"} (sql-jdbc/get-sql-state e)))+;;; +----------------------------------------------------------------------------------------------------------------++;;; | Workspace Isolation |+;;; +----------------------------------------------------------------------------------------------------------------+++;; Redshift inherits init-workspace-isolation! and grant-workspace-read-access! from Postgres.+;; Only destroy needs to be overridden because Redshift doesn't support DROP OWNED BY.++(defn- user-exists?+ "Check if a Redshift user exists."+ [conn username]+ (seq (jdbc/query conn ["SELECT 1 FROM pg_user WHERE usename = ?" username])))++(defn- schema-exists?+ "Check if a schema exists in Redshift."+ [conn schema-name]+ (seq (jdbc/query conn ["SELECT 1 FROM pg_namespace WHERE nspname = ?" schema-name])))++(defn- schemas-with-user-grants+ "Query Redshift to find schemas where the user has been granted privileges."+ [conn username]+ (->> (jdbc/query conn+ ["SELECT DISTINCT namespace_name FROM svv_relation_privileges+ WHERE identity_name = ? AND identity_type = 'user'"+ username])+ (keep :namespace_name)))++(defmethod driver/destroy-workspace-isolation! :redshift+ [_driver database workspace]+ (let [schema-name (:schema workspace)+ username (-> workspace :database_details :user)]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (let [user-exists (user-exists? t-conn username)+ schema-exists (schema-exists? t-conn schema-name)+ granted-schemas (when user-exists+ (schemas-with-user-grants t-conn username))]+ (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]+ ;; Only revoke if user exists+ (when user-exists+ (doseq [schema granted-schemas]+ (.addBatch ^Statement stmt+ ^String (format "REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA \"%s\" FROM \"%s\""+ schema username))+ (.addBatch ^Statement stmt+ ^String (format "REVOKE ALL PRIVILEGES ON SCHEMA \"%s\" FROM \"%s\""+ schema username)))+ ;; Only revoke default privileges if both user and schema exist+ (when schema-exists+ (.addBatch ^Statement stmt+ ^String (format "ALTER DEFAULT PRIVILEGES IN SCHEMA \"%s\" REVOKE ALL ON TABLES FROM \"%s\""+ schema-name username))))+ ;; These are safe with IF EXISTS+ (.addBatch ^Statement stmt+ ^String (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE" schema-name))+ (.addBatch ^Statement stmt+ ^String (format "DROP USER IF EXISTS \"%s\"" username))+ (.executeBatch ^Statement stmt))))))+(defmethod driver/llm-sql-dialect-resource :redshift [_]"llm/prompts/dialects/redshift.md")diff --git a/modules/drivers/redshift/test/metabase/test/data/redshift.clj b/modules/drivers/redshift/test/metabase/test/data/redshift.cljindex 5bf276ea2c2..4148ea49b17 100644--- a/modules/drivers/redshift/test/metabase/test/data/redshift.clj+++ b/modules/drivers/redshift/test/metabase/test/data/redshift.clj@@ -34,6 +34,11 @@;;; need to load this so we can properly override the implementation of `describe-database` below(comment metabase.driver.redshift/keep-me)+(def ^:private workspace-isolation-prefix (or+ @(requiring-resolve 'metabase-enterprise.workspaces.util/isolated-prefix)+ ;; OSS might not be able to require it+ "mb__isolation"))+(defmethod driver/database-supports? [:redshift :test/time-type][_driver _feature _database]false)@@ -165,31 +170,72 @@:unknown-error)))](group-by classify schemas)))))+(defn- classify-isolation-schemas+ "Classifies workspace isolation schemas by age using a single query. Returns a map:+ {:expired schemas older than threshold (safe to delete)+ :recent schemas created within threshold (might be from parallel test)}"+ [^java.sql.Connection conn schemas]+ (if (empty? schemas)+ {}+ (let [threshold (t/minus (t/instant) (t/hours hours-before-expired-threshold))+ schema-list (str/join "," (map #(str "'" % "'") schemas))+ ;; Use pg_class_info joined with pg_namespace to get oldest object creation time per schema+ sql (str "SELECT TRIM(n.nspname) as schema_name, MIN(c.relcreationtime) as oldest "+ "FROM pg_class_info c "+ "JOIN pg_namespace n ON c.relnamespace = n.oid "+ "WHERE TRIM(n.nspname) IN (" schema-list ") "+ "GROUP BY n.nspname")+ schema->time (with-open [stmt (.createStatement conn)+ rset (.executeQuery stmt sql)]+ (loop [result {}]+ (if (.next rset)+ (recur (assoc result+ (.getString rset "schema_name")+ (.getTimestamp rset "oldest")))+ result)))]+ (group-by (fn [schema-name]+ (if-let [oldest (get schema->time schema-name)]+ (if (t/before? (.toInstant oldest) threshold)+ :expired+ :recent)+ ;; Schema not in pg_class_info means no objects - treat as expired+ :expired))+ schemas))))+(defn- delete-old-schemas!"Remove unneeded schemas from redshift. Local databases are thrown away after a test run. Shared cloud instances donot have this luxury. Test runs can create schemas where models are persisted and nothing cleans these up, leading- to redshift clusters hitting the max number of tables allowed."+ to redshift clusters hitting the max number of tables allowed.++ Also cleans up workspace isolation schemas (mb__isolation_*) and their associated users that may have been+ left behind by workspace tests. Only deletes isolation schemas older than [[hours-before-expired-threshold]]+ to avoid interfering with parallel test runs."[^java.sql.Connection conn]- (let [{old-convention :old- caches-with-info :cache} (reduce (fn [acc s]- (cond (sql.tu.unique-prefix/old-dataset-name? s)- (update acc :old conj s)- (str/starts-with? s "metabase_cache_")- (update acc :cache conj s)- :else acc))- {:old [] :cache []}- (fetch-schemas conn))+ (let [isolation-pattern (str workspace-isolation-prefix "_")+ {old-convention :old+ caches-with-info :cache+ isolation :isolation} (reduce (fn [acc s]+ (cond (sql.tu.unique-prefix/old-dataset-name? s)+ (update acc :old conj s)+ (str/starts-with? s "metabase_cache_")+ (update acc :cache conj s)+ (str/starts-with? s isolation-pattern)+ (update acc :isolation conj s)+ :else acc))+ {:old [] :cache [] :isolation []}+ (fetch-schemas conn)){:keys [expiredold-style-cache- lacking-created-at]} (classify-cache-schemas conn caches-with-info)- drop-sql (fn [schema-name] (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE;"- schema-name))]- ;; don't delete unknown-error and recent.+ lacking-created-at]} (classify-cache-schemas conn caches-with-info)+ {expired-isolation :expired} (classify-isolation-schemas conn isolation)+ drop-sql (fn [schema-name] (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE;" schema-name))](with-open [stmt (.createStatement conn)]+ ;; Drop schemas first(doseq [[collection fmt-str] [[old-convention "Dropping old data schema: %s"][expired "Dropping expired cache schema: %s"][lacking-created-at "Dropping cache without created-at info: %s"]- [old-style-cache "Dropping old cache schema without `cache_info` table: %s"]]+ [old-style-cache "Dropping old cache schema without `cache_info` table: %s"]+ [expired-isolation "Dropping expired workspace isolation schema: %s"]]schema collection](log/infof fmt-str schema)(.execute stmt (drop-sql schema))))))diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.cljindex 54802fe1e68..7de56fb0ff2 100644--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj@@ -26,6 +26,7 @@[metabase.driver.sql.query-processor.util :as sql.qp.u][metabase.driver.sql.util :as sql.u][metabase.driver.sync :as driver.s]+ [metabase.driver.util :as driver.u][metabase.util :as u][metabase.util.date-2 :as u.date][metabase.util.honey-sql-2 :as h2x]@@ -78,7 +79,8 @@:metadata/table-existence-check true:regex/lookaheads-and-lookbehinds false:transforms/python true- :transforms/table true}]+ :transforms/table true+ :workspace true}](defmethod driver/database-supports? [:snowflake feature] [_driver _feature _db] supported?))(defmethod driver/humanize-connection-error-message :snowflake@@ -96,6 +98,10 @@[_]:sunday)+(defmethod driver.sql/default-schema :snowflake+ [_]+ "PUBLIC")+(defn- start-of-week-setting->snowflake-offset"Value to use for the `WEEK_START` connection parameter -- seehttps://docs.snowflake.com/en/sql-reference/parameters.html#label-week-start -- based on@@ -961,5 +967,80 @@[driver [_ field arg options]](string-filter driver :endswith field arg options))+;;; +----------------------------------------------------------------------------------------------------------------++;;; | Workspace Isolation |+;;; +----------------------------------------------------------------------------------------------------------------+++(defn- isolation-role-name+ "Generate role name for workspace isolation."+ [workspace]+ (format "MB_ISOLATION_ROLE_%s" (:id workspace)))++(defmethod driver/init-workspace-isolation! :snowflake+ [_driver database workspace]+ (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)+ db-name (-> database :details :db)+ warehouse (-> database :details :warehouse)+ role-name (isolation-role-name workspace)+ read-user {:user (driver.u/workspace-isolation-user-name workspace)+ :password (driver.u/random-workspace-password)}+ conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (when-not db-name+ (throw (ex-info "Snowflake database configuration is missing required 'db' (database name) setting"+ {:database-id (:id database) :step :init})))+ (when-not warehouse+ (throw (ex-info "Snowflake database configuration is missing required 'warehouse' setting"+ {:database-id (:id database) :step :init})))+ ;; Snowflake RBAC: create schema -> create role -> grant privileges to role -> create user -> grant role to user+ (doseq [sql [(format "CREATE SCHEMA IF NOT EXISTS \"%s\".\"%s\"" db-name schema-name)+ (format "CREATE ROLE IF NOT EXISTS \"%s\"" role-name)+ (format "GRANT USAGE ON DATABASE \"%s\" TO ROLE \"%s\"" db-name role-name)+ (format "GRANT USAGE ON WAREHOUSE \"%s\" TO ROLE \"%s\"" warehouse role-name)+ (format "GRANT USAGE ON SCHEMA \"%s\".\"%s\" TO ROLE \"%s\"" db-name schema-name role-name)+ (format "GRANT ALL PRIVILEGES ON SCHEMA \"%s\".\"%s\" TO ROLE \"%s\"" db-name schema-name role-name)+ (format "GRANT ALL ON FUTURE TABLES IN SCHEMA \"%s\".\"%s\" TO ROLE \"%s\"" db-name schema-name role-name)+ (format "CREATE USER IF NOT EXISTS \"%s\" PASSWORD = '%s' MUST_CHANGE_PASSWORD = FALSE DEFAULT_ROLE = \"%s\""+ (:user read-user) (:password read-user) role-name)+ (format "GRANT ROLE \"%s\" TO USER \"%s\"" role-name (:user read-user))]]+ (jdbc/execute! conn-spec [sql]))+ {:schema schema-name+ :database_details (assoc read-user :role role-name :use-password true)}))++(defmethod driver/destroy-workspace-isolation! :snowflake+ [_driver database workspace]+ (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)+ db-name (-> database :details :db)+ role-name (isolation-role-name workspace)+ username (driver.u/workspace-isolation-user-name workspace)+ conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (when-not db-name+ (throw (ex-info "Snowflake database configuration is missing required 'db' (database name) setting"+ {:database-id (:id database) :step :destroy})))+ ;; Drop in reverse order of creation: schema (CASCADE handles tables) -> user -> role+ (doseq [sql [(format "DROP SCHEMA IF EXISTS \"%s\".\"%s\" CASCADE" db-name schema-name)+ (format "DROP USER IF EXISTS \"%s\"" username)+ (format "DROP ROLE IF EXISTS \"%s\"" role-name)]]+ (jdbc/execute! conn-spec [sql]))))++(defmethod driver/grant-workspace-read-access! :snowflake+ [_driver database workspace tables]+ (let [conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))+ db-name (-> database :details :db)+ role-name (-> workspace :database_details :role)]+ (when-not db-name+ (throw (ex-info "Snowflake database configuration is missing required 'db' (database name) setting"+ {:database-id (:id database) :step :grant})))+ (when-not role-name+ (throw (ex-info "Workspace isolation is not properly initialized - missing role name"+ {:workspace-id (:id workspace) :step :grant})))+ ;; Grant USAGE on each unique schema first (required to access tables within)+ (doseq [schema (distinct (map :schema tables))]+ (jdbc/execute! conn-spec [(format "GRANT USAGE ON SCHEMA \"%s\".\"%s\" TO ROLE \"%s\""+ db-name schema role-name)]))+ ;; Grant SELECT on each specific table+ (doseq [table tables]+ (jdbc/execute! conn-spec [(format "GRANT SELECT ON TABLE \"%s\".\"%s\".\"%s\" TO ROLE \"%s\""+ db-name (:schema table) (:name table) role-name)]))))+(defmethod driver/llm-sql-dialect-resource :snowflake [_]"llm/prompts/dialects/snowflake.md")diff --git a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.cljindex ad8b217b2bc..46a8f4c8890 100644--- a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj+++ b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj@@ -301,12 +301,11 @@(mt/dataset airports(tx/with-driver-supports-feature! [:snowflake :test/use-fake-sync false](testing "describe-database"- (let [expected-tables #{:tables- #{{:name "continent", :schema "PUBLIC", :description nil}- {:name "municipality", :schema "PUBLIC", :description nil}- {:name "region", :schema "PUBLIC", :description nil}- {:name "country", :schema "PUBLIC", :description nil}- {:name "airport", :schema "PUBLIC", :description nil}}}]+ (let [expected-tables #{{:name "continent", :schema "PUBLIC", :description nil}+ {:name "municipality", :schema "PUBLIC", :description nil}+ {:name "region", :schema "PUBLIC", :description nil}+ {:name "country", :schema "PUBLIC", :description nil}+ {:name "airport", :schema "PUBLIC", :description nil}}](testing "should work with normal details"(is (= expected-tables(:tables (driver/describe-database :snowflake (mt/db))))))@@ -1429,25 +1428,22 @@;; Requires real sync (not fake-sync) so tables actually exist in Snowflake.(testing "db with a valid db and an invalid dbname in details should be synced with db correctly"(mt/test-driver :snowflake- (tx/with-driver-supports-feature! [:snowflake :test/use-fake-sync false]- (let [priv-key-val (mt/priv-key->base64-uri (tx/db-test-env-var-or-throw :snowflake :private-key))- expected-tables #{{:name "users", :schema "PUBLIC", :description nil}- {:name "venues", :schema "PUBLIC", :description nil}- {:name "checkins", :schema "PUBLIC", :description nil}- {:name "categories", :schema "PUBLIC", :description nil}- {:name "orders", :schema "PUBLIC", :description nil}- {:name "people", :schema "PUBLIC", :description nil}- {:name "products", :schema "PUBLIC", :description nil}- {:name "reviews", :schema "PUBLIC", :description nil}}]- (mt/with-temp [:model/Database db {:engine :snowflake- :details (-> (:details (mt/db))- (dissoc :private-key-id)- (assoc :private-key-options "uploaded")- (assoc :private-key-value priv-key-val)- (assoc :use-password false)- (assoc :dbname nil))}]- (is (= expected-tables- (:tables (driver/describe-database :snowflake db))))))))))+ (mt/dataset airports+ (tx/with-driver-supports-feature! [:snowflake :test/use-fake-sync false]+ (let [priv-key-val (mt/priv-key->base64-uri (tx/db-test-env-var-or-throw :snowflake :private-key))]+ (mt/with-temp [:model/Database db {:engine :snowflake+ :details (-> (:details (mt/db))+ (dissoc :private-key-id)+ (assoc :private-key-options "uploaded")+ (assoc :private-key-value priv-key-val)+ (assoc :use-password false)+ (assoc :dbname nil))}]+ (is (= #{{:name "continent", :schema "PUBLIC", :description nil}+ {:name "municipality", :schema "PUBLIC", :description nil}+ {:name "region", :schema "PUBLIC", :description nil}+ {:name "country", :schema "PUBLIC", :description nil}+ {:name "airport", :schema "PUBLIC", :description nil}}+ (:tables (driver/describe-database :snowflake db)))))))))));;; ------------------------------------------------ Fake Sync Tests ------------------------------------------------;; Tests to validate that fake sync produces correct metadata for Snowflake.diff --git a/modules/drivers/snowflake/test/metabase/test/data/snowflake.clj b/modules/drivers/snowflake/test/metabase/test/data/snowflake.cljindex 232b0f488dc..4ef1f996bcb 100644--- a/modules/drivers/snowflake/test/metabase/test/data/snowflake.clj+++ b/modules/drivers/snowflake/test/metabase/test/data/snowflake.clj@@ -16,7 +16,9 @@[metabase.util :as u][metabase.util.log :as log])(:import- (java.sql PreparedStatement ResultSet)))+ (java.sql PreparedStatement ResultSet)+ (java.time Instant)+ (java.time.temporal ChronoUnit)))(set! *warn-on-reflection* true)@@ -59,9 +61,7 @@:private-key-value (mt/priv-key->base64-uri (tx/db-test-env-var-or-throw :snowflake :private-key)):use-password false:additional-options (tx/db-test-env-var :snowflake :additional-options)- ;; this lowercasing this value is part of testing the fix for- ;; https://github.com/metabase/metabase/issues/9511- :warehouse (u/lower-case-en (tx/db-test-env-var-or-throw :snowflake :warehouse))+ :warehouse (tx/db-test-env-var-or-throw :snowflake :warehouse);;;; SESSION parameters;;@@ -111,21 +111,48 @@and created < dateadd(day, ?, current_timestamp()))"](into [] (map :name) (jdbc/reducible-query (no-db-connection-spec) [query days-ago days-ago]))))-(defn- delete-old-datasets!- "Delete any datasets prefixed by a date that is two days ago or older. See comments above."+(defn- old-isolation-schema-names+ "Return a collection of schema names with mb__isolation_ prefix that are more than 3 hours old,+ along with their database names."+ []+ (sql-jdbc.execute/do-with-connection-with-options+ :snowflake+ (no-db-connection-spec)+ {:write? false}+ (fn [^java.sql.Connection conn]+ (with-open [stmt (.createStatement conn)+ rs (.executeQuery stmt "SHOW SCHEMAS LIKE 'mb__isolation_%' IN ACCOUNT")]+ (let [three-hours-ago (-> (Instant/now)+ (.minus 3 ChronoUnit/HOURS)+ java.util.Date/from)]+ (loop [results []]+ (if (.next rs)+ (let [schema-name (.getString rs "name")+ db-name (.getString rs "database_name")+ created-on (.getTimestamp rs "created_on")]+ (if (and created-on (.before created-on three-hours-ago))+ (recur (conj results {:schema-name schema-name :database-name db-name}))+ (recur results)))+ results)))))))++(defn- delete-old-test-data!+ "Delete old test data:+ - Datasets (databases) prefixed by sha_ that are two days ago or older+ - Isolation schemas prefixed by mb__isolation_ that are more than 3 hours old"[];; the printlns below are on purpose because we want them to show up when running tests, even on CI, to make sure this;; stuff is working correctly. We can change it to `log` in the future when we're satisfied everything is working as;; intended -- Cam#_{:clj-kondo/ignore [:discouraged-var]}- (println "[Snowflake] deleting old datasets...")- (when-let [old-datasets (not-empty (old-dataset-names))]- (sql-jdbc.execute/do-with-connection-with-options- :snowflake- (no-db-connection-spec)- {:write? true}- (fn [^java.sql.Connection conn]- (with-open [stmt (.createStatement conn)]+ (println "[Snowflake] deleting old test data...")+ (sql-jdbc.execute/do-with-connection-with-options+ :snowflake+ (no-db-connection-spec)+ {:write? true}+ (fn [^java.sql.Connection conn]+ (with-open [stmt (.createStatement conn)]+ ;; Delete old datasets+ (when-let [old-datasets (not-empty (old-dataset-names))](doseq [dataset-name old-datasets]#_{:clj-kondo/ignore [:discouraged-var]}(println "[Snowflake] Deleting old dataset:" dataset-name)@@ -139,16 +166,27 @@;; deleting anything it's not the end of the world because it won't affect our ability to run our tests(catch Throwable e#_{:clj-kondo/ignore [:discouraged-var]}- (println "[Snowflake] Error deleting old dataset:" (ex-message e))))))))))+ (println "[Snowflake] Error deleting old dataset:" (ex-message e))))))+ ;; Delete old isolation schemas+ (when-let [old-schemas (not-empty (old-isolation-schema-names))]+ (doseq [{:keys [schema-name database-name]} old-schemas]+ #_{:clj-kondo/ignore [:discouraged-var]}+ (println "[Snowflake] Deleting old isolation schema:" database-name "." schema-name)+ (try+ (.execute stmt (format "DROP SCHEMA IF EXISTS \"%s\".\"%s\";"+ database-name schema-name))+ (catch Throwable e+ #_{:clj-kondo/ignore [:discouraged-var]}+ (println "[Snowflake] Error deleting old isolation schema:" (ex-message e))))))))))-(defonce ^:private deleted-old-datasets?+(defonce ^:private deleted-old-test-data?(atom false))-(defn- delete-old-datasets-if-needed!- "Call [[delete-old-datasets!]], only if we haven't done so already."+(defn- delete-old-test-data-if-needed!+ "Call [[delete-old-test-data!]], only if we haven't done so already."[]- (when (compare-and-set! deleted-old-datasets? false true)- (delete-old-datasets!)))+ (when (compare-and-set! deleted-old-test-data? false true)+ (delete-old-test-data!)))(defn- set-current-user-timezone![timezone]@@ -164,8 +202,8 @@[driver db-def & options];; qualify the DB name with the unique prefix(let [db-def (assoc db-def :database-name (qualified-db-name db-def))]- ;; clean up any old datasets that should be deleted- (delete-old-datasets-if-needed!)+ ;; clean up any old test data (datasets and isolation schemas)+ (delete-old-test-data-if-needed!);; Snowflake by default uses America/Los_Angeles timezone. See https://docs.snowflake.com/en/sql-reference/parameters#timezone.;; We expect UTC in tests. Hence fixing [[metabase.query-processor.timezone/database-timezone-id]] (PR #36413);; produced lot of failures. Following expression addresses that, setting timezone for the test user.diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.cljindex d92a9f7d7b0..99210c54bd3 100644--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj@@ -65,7 +65,8 @@:jdbc/statements false:describe-default-expr true:describe-is-nullable true- :describe-is-generated true}]+ :describe-is-generated true+ :workspace true}](defmethod driver/database-supports? [:sqlserver feature] [_driver _feature _db] supported?))(mu/defmethod driver/database-supports? [:sqlserver :percentile-aggregations]@@ -79,6 +80,10 @@[_]:sunday)+(defmethod driver.sql/default-schema :sqlserver+ [_]+ "dbo")+(defmethod driver/prettify-native-form :sqlserver[_ native-form](sql.u/format-sql-and-fix-params :tsql native-form))@@ -1082,5 +1087,69 @@(str (quote-identifier (name schema)) "." (quote-identifier (name table-name)))(quote-identifier (name table-name))))))+;;; +----------------------------------------------------------------------------------------------------------------++;;; | Workspace Isolation |+;;; +----------------------------------------------------------------------------------------------------------------+++(defmethod driver/init-workspace-isolation! :sqlserver+ [_driver database workspace]+ (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)+ username (driver.u/workspace-isolation-user-name workspace)+ password (driver.u/random-workspace-password)+ escaped-password (sql.u/escape-sql password :ansi)+ conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ ;; SQL Server: create login (server level), then user (database level), then schema+ (doseq [sql [(format (str "IF NOT EXISTS (SELECT name FROM master.sys.server_principals WHERE name = '%s') "+ "CREATE LOGIN [%s] WITH PASSWORD = N'%s'")+ username username escaped-password)+ (format "IF NOT EXISTS (SELECT name FROM sys.database_principals WHERE name = '%s') CREATE USER [%s] FOR LOGIN [%s]"+ username username username)+ (format "IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '%s') EXEC('CREATE SCHEMA [%s]')"+ schema-name schema-name)+ ;; CONTROL ON SCHEMA gives ALTER (needed for creating objects in schema)+ (format "GRANT CONTROL ON SCHEMA::[%s] TO [%s]" schema-name username)+ ;; CREATE TABLE at database level is also required in SQL Server+ (format "GRANT CREATE TABLE TO [%s]" username)]]+ (jdbc/execute! conn-spec [sql]))+ {:schema schema-name+ :database_details {:user username+ :password password}}))++(defmethod driver/destroy-workspace-isolation! :sqlserver+ [_driver database workspace]+ (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)+ username (driver.u/workspace-isolation-user-name workspace)+ conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (doseq [sql [(format (str "DECLARE @sql NVARCHAR(MAX) = ''; "+ "SELECT @sql += 'DROP TABLE [%s].[' + name + ']; ' "+ "FROM sys.tables WHERE schema_id = SCHEMA_ID('%s'); "+ "EXEC sp_executesql @sql")+ schema-name schema-name)+ (format "IF EXISTS (SELECT * FROM sys.schemas WHERE name = '%s') DROP SCHEMA [%s]"+ schema-name schema-name)+ (format "IF EXISTS (SELECT * FROM sys.database_principals WHERE name = '%s') DROP USER [%s]"+ username username)+ ;; Kill all sessions using this login before dropping it+ (format (str "DECLARE @sql NVARCHAR(MAX) = ''; "+ "SELECT @sql += 'KILL ' + CAST(session_id AS VARCHAR(10)) + '; ' "+ "FROM sys.dm_exec_sessions WHERE login_name = '%s'; "+ "EXEC sp_executesql @sql")+ username)+ (format "IF EXISTS (SELECT * FROM master.sys.server_principals WHERE name = '%s') DROP LOGIN [%s]"+ username username)]]+ (jdbc/execute! conn-spec [sql]))))++(defmethod driver/grant-workspace-read-access! :sqlserver+ [_driver database workspace tables]+ (let [conn-spec (sql-jdbc.conn/db->pooled-connection-spec (:id database))+ username (-> workspace :database_details :user)]+ (when-not username+ (throw (ex-info "Workspace isolation is not properly initialized - missing read user name"+ {:workspace-id (:id workspace) :step :grant})))+ ;; Grant SELECT on each specific table only - no schema-level grants+ (doseq [table tables]+ (jdbc/execute! conn-spec [(format "GRANT SELECT ON [%s].[%s] TO [%s]"+ (:schema table) (:name table) username)]))))+(defmethod driver/llm-sql-dialect-resource :sqlserver [_]"llm/prompts/dialects/sqlserver.md")diff --git a/resources/migrations/059_update_migrations.yaml b/resources/migrations/059_update_migrations.yamlindex 003dcd30ba1..f6dcda8faf0 100644--- a/resources/migrations/059_update_migrations.yaml+++ b/resources/migrations/059_update_migrations.yaml@@ -136,9 +136,6 @@ databaseChangeLog:remarks: Version number for dependency analysis to track when dependencies need recalculationconstraints:nullable: false- rollback:- - dropTable:- tableName: measure- changeSet:id: v59.2025-12-04T12:00:01@@ -999,6 +996,1943 @@ databaseChangeLog:tableName: notification_cardcolumnName: disable_links+ # ============================================================================+ # Workspace feature: clean migration path+ # These changesets create the complete workspace schema in a single pass.+ # On existing databases where workspace tables already exist, every changeset+ # is safely skipped via preConditions: onFail: MARK_RAN.+ # ============================================================================++ # ---------- Group 1: workspace table ----------++ - changeSet:+ id: v59.2026-01-31T12:00:00+ author: christruter+ comment: Create workspace table with all columns+ preConditions:+ - onFail: MARK_RAN+ - not:+ - tableExists:+ tableName: workspace+ changes:+ - createTable:+ tableName: workspace+ remarks: Internal Metabase container+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: name+ type: varchar(254)+ remarks: Workspace name+ - column:+ name: collection_id+ type: int+ remarks: FK to workspace's collection+ constraints:+ nullable: true+ - column:+ name: creator_id+ type: int+ remarks: FK to owner+ - column:+ name: api_key_id+ type: int+ remarks: FK to api key used by agent+ - column:+ name: execution_user+ type: int+ remarks: FK to api key's user+ - column:+ name: database_id+ type: int+ remarks: FK to db used for this workspace+ - column:+ name: schema+ type: varchar(254)+ remarks: Isolated schema where work happens+ constraints:+ nullable: true+ - column:+ name: database_details+ type: ${text.type}+ remarks: Database details used for isolation of this workspace+ constraints:+ nullable: true+ - column:+ name: db_status+ type: varchar(20)+ remarks: Status of the database isolation resources (uninitialized, pending, ready, broken)+ defaultValue: uninitialized+ constraints:+ nullable: false+ - column:+ name: base_status+ type: varchar(20)+ remarks: Workspace lifecycle status (empty, active, archived)+ defaultValue: empty+ constraints:+ nullable: false+ - column:+ name: graph_version+ type: bigint+ remarks: Incremented when graph structure changes (transform add/remove, global transform changes)+ defaultValueNumeric: 1+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: Timestamp when the workspace was created+ defaultValueComputed: current_timestamp+ - column:+ name: updated_at+ type: ${timestamp_type}+ remarks: Timestamp when the workspace was last updated+ defaultValueComputed: current_timestamp++ - changeSet:+ id: v59.2026-01-31T12:00:01+ author: christruter+ comment: Add index on workspace.collection_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace+ indexName: idx_workspace_collection_id+ changes:+ - createIndex:+ tableName: workspace+ indexName: idx_workspace_collection_id+ columns:+ - column:+ name: collection_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:02+ author: christruter+ comment: Add index on workspace.creator_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace+ indexName: idx_workspace_creator_id+ changes:+ - createIndex:+ tableName: workspace+ indexName: idx_workspace_creator_id+ columns:+ - column:+ name: creator_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:03+ author: christruter+ comment: Add index on workspace.api_key_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace+ indexName: idx_workspace_api_key_id+ changes:+ - createIndex:+ tableName: workspace+ indexName: idx_workspace_api_key_id+ columns:+ - column:+ name: api_key_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:04+ author: christruter+ comment: Add index on workspace.execution_user+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace+ indexName: idx_workspace_execution_user_id+ changes:+ - createIndex:+ tableName: workspace+ indexName: idx_workspace_execution_user_id+ columns:+ - column:+ name: execution_user+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:05+ author: christruter+ comment: Add unique constraint on workspace.name+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace+ indexName: idx_workspace_name_unique+ changes:+ - addUniqueConstraint:+ tableName: workspace+ columnNames: name+ constraintName: idx_workspace_name_unique+ rollback:++ # ---------- Group 2: collection.workspace_id ----------++ - changeSet:+ id: v59.2026-01-31T12:00:06+ author: christruter+ comment: Add workspace_id column to collection table+ preConditions:+ - onFail: MARK_RAN+ - not:+ - columnExists:+ tableName: collection+ columnName: workspace_id+ changes:+ - addColumn:+ tableName: collection+ columns:+ - column:+ name: workspace_id+ type: int+ remarks: Workspace containing this collection+ constraints:+ nullable: true+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:07+ author: christruter+ comment: Add index on collection.workspace_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: collection+ indexName: idx_collection_workspace_id+ changes:+ - createIndex:+ tableName: collection+ indexName: idx_collection_workspace_id+ columns:+ - column:+ name: workspace_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:08+ author: christruter+ comment: Add foreign key constraint from collection.workspace_id to workspace with CASCADE+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_collection_workspace_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: collection+ baseColumnNames: workspace_id+ constraintName: fk_collection_workspace_id+ referencedTableName: workspace+ referencedColumnNames: id+ onDelete: CASCADE++ # ---------- Group 5: workspace_log ----------++ - changeSet:+ id: v59.2026-01-31T12:00:27+ author: christruter+ comment: Create workspace_log table for async workspace setup progress+ preConditions:+ - onFail: MARK_RAN+ - not:+ - tableExists:+ tableName: workspace_log+ changes:+ - createTable:+ tableName: workspace_log+ remarks: Log entries for async workspace setup progress+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: workspace_id+ type: int+ remarks: FK to workspace being set up+ constraints:+ nullable: false+ - column:+ name: task+ type: varchar(255)+ remarks: Name of the setup task+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: When the task was created+ constraints:+ nullable: false+ - column:+ name: updated_at+ type: ${timestamp_type}+ remarks: When the task was updated+ constraints:+ nullable: false+ - column:+ name: started_at+ type: ${timestamp_type}+ remarks: When the task started+ constraints:+ nullable: false+ - column:+ name: completed_at+ type: ${timestamp_type}+ remarks: When the task completed+ - column:+ name: status+ type: varchar(20)+ remarks: Task status (started, success, failed)+ - column:+ name: message+ type: ${text.type}+ remarks: Details on the task result if any++ - changeSet:+ id: v59.2026-01-31T12:00:28+ author: christruter+ comment: Add index on workspace_log.workspace_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_log+ indexName: idx_workspace_log_workspace_id+ changes:+ - createIndex:+ tableName: workspace_log+ indexName: idx_workspace_log_workspace_id+ columns:+ - column:+ name: workspace_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:29+ author: christruter+ comment: Add FK from workspace_log.workspace_id to workspace with CASCADE+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_log_workspace_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_log+ baseColumnNames: workspace_id+ constraintName: fk_workspace_log_workspace_id+ referencedTableName: workspace+ referencedColumnNames: id+ onDelete: CASCADE++ # ---------- Group 6: workspace_transform ----------++ - changeSet:+ id: v59.2026-01-31T12:00:30+ author: christruter+ comment: Create workspace_transform table with all columns+ preConditions:+ - onFail: MARK_RAN+ - not:+ - tableExists:+ tableName: workspace_transform+ changes:+ - createTable:+ tableName: workspace_transform+ remarks: Holds the changeset of transforms being created and edited within a workspace+ columns:+ - column:+ name: ref_id+ type: char(36)+ remarks: Representation id for the workspace transform, not unique across workspaces+ constraints:+ nullable: false+ - column:+ name: workspace_id+ type: int+ remarks: FK to workspace containing this transform+ constraints:+ nullable: false+ - column:+ name: global_id+ type: int+ remarks: FK to transform table if checked out from global+ constraints:+ nullable: true+ - column:+ name: collection_id+ type: int+ remarks: FK to collection containing this transform+ constraints:+ nullable: true+ - column:+ name: creator_id+ type: int+ remarks: FK to core_user who created this workspace transform+ constraints:+ nullable: true+ - column:+ name: name+ type: ${text.type}+ remarks: Name of the transform+ constraints:+ nullable: false+ - column:+ name: description+ type: ${text.type}+ remarks: Description of the transform+ constraints:+ nullable: true+ - column:+ name: source+ type: ${text.type}+ remarks: JSON of source configuration+ constraints:+ nullable: false+ - column:+ name: target+ type: ${text.type}+ remarks: JSON of target configuration+ constraints:+ nullable: false+ - column:+ name: entity_id+ type: char(21)+ remarks: Random NanoID tag for unique identity of this changeset entry only+ constraints:+ nullable: true+ unique: true+ - column:+ name: checked_out_at+ type: ${timestamp_type}+ remarks: Timestamp when the transform was checked out+ constraints:+ nullable: true+ - column:+ name: archived_at+ type: ${timestamp_type}+ remarks: If set, marks that the global transform should be archived when merging+ constraints:+ nullable: true+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: Timestamp when the workspace transform was created+ constraints:+ nullable: false+ - column:+ name: updated_at+ type: ${timestamp_type}+ remarks: Timestamp when the workspace transform was last updated+ constraints:+ nullable: false+ - column:+ name: last_run_at+ type: ${timestamp_type}+ remarks: Timestamp when this transform was last run+ constraints:+ nullable: true+ - column:+ name: last_run_message+ type: ${text.type}+ remarks: Message from the last transform run (success or failure message)+ constraints:+ nullable: true+ - column:+ name: last_run_status+ type: varchar(20)+ remarks: Status of the last transform run (succeeded or failed)+ constraints:+ nullable: true+ - column:+ name: definition_changed+ type: ${boolean.type}+ remarks: Indicates if the transform definition has changed and needs re-execution+ defaultValueBoolean: true+ constraints:+ nullable: false+ - column:+ name: analysis_version+ type: bigint+ remarks: Incremented when transform source/target changes, triggers re-analysis+ defaultValueNumeric: 1+ constraints:+ nullable: false+ - column:+ name: input_data_changed+ type: ${boolean.type}+ remarks: Indicates if upstream data has changed and transform needs re-execution+ defaultValueBoolean: false+ constraints:+ nullable: false++ - changeSet:+ id: v59.2026-01-31T12:00:31+ author: christruter+ comment: Add index on workspace_transform.workspace_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_transform+ indexName: idx_workspace_transform_workspace_id+ changes:+ - createIndex:+ tableName: workspace_transform+ indexName: idx_workspace_transform_workspace_id+ columns:+ - column:+ name: workspace_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:32+ author: christruter+ comment: Add index on workspace_transform.global_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_transform+ indexName: idx_workspace_transform_global_id+ changes:+ - createIndex:+ tableName: workspace_transform+ indexName: idx_workspace_transform_global_id+ columns:+ - column:+ name: global_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:33+ author: christruter+ comment: Add index on workspace_transform.collection_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_transform+ indexName: idx_workspace_transform_collection_id+ changes:+ - createIndex:+ tableName: workspace_transform+ indexName: idx_workspace_transform_collection_id+ columns:+ - column:+ name: collection_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:34+ author: christruter+ comment: Add index on workspace_transform.creator_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_transform+ indexName: idx_workspace_transform_creator_id+ changes:+ - createIndex:+ tableName: workspace_transform+ indexName: idx_workspace_transform_creator_id+ columns:+ - column:+ name: creator_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:35+ author: christruter+ comment: Add FK from workspace_transform.workspace_id to workspace with CASCADE+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_transform_workspace_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_transform+ baseColumnNames: workspace_id+ constraintName: fk_workspace_transform_workspace_id+ referencedTableName: workspace+ referencedColumnNames: id+ onDelete: CASCADE++ - changeSet:+ id: v59.2026-01-31T12:00:36+ author: christruter+ comment: Add FK from workspace_transform.global_id to transform with SET NULL+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_transform_global_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_transform+ baseColumnNames: global_id+ constraintName: fk_workspace_transform_global_id+ referencedTableName: transform+ referencedColumnNames: id+ onDelete: SET NULL++ - changeSet:+ id: v59.2026-01-31T12:00:37+ author: christruter+ comment: Add FK from workspace_transform.collection_id to collection with SET NULL+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_transform_collection_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_transform+ baseColumnNames: collection_id+ constraintName: fk_workspace_transform_collection_id+ referencedTableName: collection+ referencedColumnNames: id+ onDelete: SET NULL++ - changeSet:+ id: v59.2026-01-31T12:00:38+ author: christruter+ comment: Add FK from workspace_transform.creator_id to core_user with SET NULL+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_transform_creator_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_transform+ baseColumnNames: creator_id+ constraintName: fk_workspace_transform_creator_id+ referencedTableName: core_user+ referencedColumnNames: id+ onDelete: SET NULL++ - changeSet:+ id: v59.2026-01-31T12:00:39+ validCheckSum: ANY+ author: christruter+ comment: Add partial unique index on workspace_transform (workspace_id, global_id) where global_id is not null+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_transform+ indexName: idx_workspace_transform_workspace_global_unique+ changes:+ # PostgreSQL supports partial indexes for smaller index size+ - sql:+ dbms: postgresql+ sql: CREATE UNIQUE INDEX idx_workspace_transform_workspace_global_unique ON workspace_transform (workspace_id, global_id) WHERE global_id IS NOT NULL+ # H2/MySQL/MariaDB don't support partial indexes, but all databases treat NULLs as distinct+ # in unique indexes, so a regular unique index is functionally equivalent+ - sql:+ dbms: h2,mysql,mariadb+ sql: CREATE UNIQUE INDEX idx_workspace_transform_workspace_global_unique ON workspace_transform (workspace_id, global_id)+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:40+ author: christruter+ comment: Add composite primary key on workspace_transform (workspace_id, ref_id)+ preConditions:+ - onFail: MARK_RAN+ - not:+ - primaryKeyExists:+ tableName: workspace_transform+ changes:+ - addPrimaryKey:+ tableName: workspace_transform+ columnNames: workspace_id, ref_id+ constraintName: pk_workspace_transform++ # ---------- Group 7: workspace_input ----------++ - changeSet:+ id: v59.2026-01-31T12:00:41+ author: qnkhuat+ comment: Create workspace_input table for external tables consumed by workspace transforms+ preConditions:+ - onFail: MARK_RAN+ - not:+ - tableExists:+ tableName: workspace_input+ changes:+ - createTable:+ tableName: workspace_input+ remarks: External tables that workspace transforms consume from source databases+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: workspace_id+ type: int+ remarks: FK to workspace+ constraints:+ nullable: false+ - column:+ name: db_id+ type: int+ remarks: Database containing the source table+ constraints:+ nullable: false+ - column:+ name: schema+ type: varchar(254)+ remarks: Schema of the source table (nullable for DBs without schemas)+ constraints:+ nullable: true+ - column:+ name: table+ type: varchar(254)+ remarks: Name of the source table+ constraints:+ nullable: false+ - column:+ name: table_id+ type: int+ remarks: FK to metabase_table if table exists and is synced+ constraints:+ nullable: true+ - column:+ name: ref_id+ type: char(36)+ remarks: ref_id of the workspace_transform that depends on this input+ constraints:+ nullable: false+ - column:+ name: access_granted+ type: ${boolean.type}+ remarks: Whether read access has been granted to the workspace user+ defaultValueBoolean: false+ constraints:+ nullable: false+ - column:+ name: transform_version+ type: bigint+ remarks: The analysis_version of the transform when this row was created+ defaultValueNumeric: 0+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: Timestamp when the input was created+ constraints:+ nullable: false+ - column:+ name: updated_at+ type: ${timestamp_type}+ remarks: Timestamp when the input was last updated+ constraints:+ nullable: false++ - changeSet:+ id: v59.2026-01-31T12:00:42+ author: qnkhuat+ comment: Add index on workspace_input.workspace_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_input+ indexName: idx_workspace_input_workspace_id+ changes:+ - createIndex:+ tableName: workspace_input+ indexName: idx_workspace_input_workspace_id+ columns:+ - column:+ name: workspace_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:43+ author: qnkhuat+ comment: Add index on workspace_input.table_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_input+ indexName: idx_workspace_input_table_id+ changes:+ - createIndex:+ tableName: workspace_input+ indexName: idx_workspace_input_table_id+ columns:+ - column:+ name: table_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:44+ author: qnkhuat+ comment: Add composite index on workspace_input (workspace_id, ref_id)+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_input+ indexName: idx_workspace_input_ref_id+ changes:+ - createIndex:+ tableName: workspace_input+ indexName: idx_workspace_input_ref_id+ columns:+ - column:+ name: workspace_id+ - column:+ name: ref_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:45+ author: qnkhuat+ comment: Add FK from workspace_input.workspace_id to workspace with CASCADE+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_input_workspace_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_input+ baseColumnNames: workspace_id+ constraintName: fk_workspace_input_workspace_id+ referencedTableName: workspace+ referencedColumnNames: id+ onDelete: CASCADE++ - changeSet:+ id: v59.2026-01-31T12:00:46+ author: qnkhuat+ comment: Add FK from workspace_input.table_id to metabase_table with SET NULL+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_input_table_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_input+ baseColumnNames: table_id+ constraintName: fk_workspace_input_table_id+ referencedTableName: metabase_table+ referencedColumnNames: id+ onDelete: SET NULL++ - changeSet:+ id: v59.2026-01-31T12:00:47+ author: qnkhuat+ comment: Add unique constraint on workspace_input (workspace_id, ref_id, db_id, schema, table, transform_version)+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_input+ indexName: uq_workspace_input_versioned+ changes:+ - addUniqueConstraint:+ tableName: workspace_input+ columnNames: workspace_id, ref_id, db_id, schema, table, transform_version+ constraintName: uq_workspace_input_versioned+ rollback:++ # ---------- Group 8: workspace_output ----------++ - changeSet:+ id: v59.2026-01-31T12:00:48+ author: crisptrutski+ comment: Create workspace_output table for tables produced by workspace transforms+ preConditions:+ - onFail: MARK_RAN+ - not:+ - tableExists:+ tableName: workspace_output+ changes:+ - createTable:+ tableName: workspace_output+ remarks: Tables that workspace transforms produce+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: workspace_id+ type: int+ remarks: FK to workspace+ constraints:+ nullable: false+ - column:+ name: ref_id+ type: char(36)+ remarks: FK to workspace_transform.ref_id (producer)+ constraints:+ nullable: false+ - column:+ name: db_id+ type: int+ remarks: Database where the output table is created+ constraints:+ nullable: false+ - column:+ name: global_schema+ type: varchar(254)+ remarks: Schema of the output table+ constraints:+ nullable: true+ - column:+ name: global_table+ type: varchar(254)+ remarks: Name of the output table+ constraints:+ nullable: false+ - column:+ name: global_table_id+ type: int+ remarks: FK to metabase table for the global output table+ constraints:+ nullable: true+ - column:+ name: isolated_schema+ type: varchar(254)+ remarks: Schema name in the isolated workspace namespace+ constraints:+ nullable: true+ - column:+ name: isolated_table+ type: varchar(254)+ remarks: Table name in the isolated workspace namespace+ constraints:+ nullable: true+ - column:+ name: isolated_table_id+ type: int+ remarks: FK to metabase table for the isolated output table+ constraints:+ nullable: true+ - column:+ name: transform_version+ type: bigint+ remarks: The analysis_version of the transform when this row was created+ defaultValueNumeric: 0+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: Timestamp when the output was created+ constraints:+ nullable: false+ - column:+ name: updated_at+ type: ${timestamp_type}+ remarks: Timestamp when the output was last updated+ constraints:+ nullable: false++ - changeSet:+ id: v59.2026-01-31T12:00:49+ author: crisptrutski+ comment: Add index on workspace_output.workspace_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_output+ indexName: idx_workspace_output_workspace_id+ changes:+ - createIndex:+ tableName: workspace_output+ indexName: idx_workspace_output_workspace_id+ columns:+ - column:+ name: workspace_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:50+ author: crisptrutski+ comment: Add index on workspace_output.ref_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_output+ indexName: idx_workspace_output_ref_id+ changes:+ - createIndex:+ tableName: workspace_output+ indexName: idx_workspace_output_ref_id+ columns:+ - column:+ name: ref_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:51+ author: crisptrutski+ comment: Add FK from workspace_output.workspace_id to workspace with CASCADE+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_output_workspace_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_output+ baseColumnNames: workspace_id+ constraintName: fk_workspace_output_workspace_id+ referencedTableName: workspace+ referencedColumnNames: id+ onDelete: CASCADE++ - changeSet:+ id: v59.2026-01-31T12:00:52+ author: crisptrutski+ comment: Add FK from workspace_output.ref_id to workspace_transform with CASCADE+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_output_ref_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_output+ baseColumnNames: workspace_id, ref_id+ constraintName: fk_workspace_output_ref_id+ referencedTableName: workspace_transform+ referencedColumnNames: workspace_id, ref_id+ onDelete: CASCADE++ - changeSet:+ id: v59.2026-01-31T12:00:53+ author: crisptrutski+ comment: Add unique constraint on workspace_output (workspace_id, ref_id, transform_version)+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_output+ indexName: uq_workspace_output_versioned+ changes:+ - addUniqueConstraint:+ tableName: workspace_output+ columnNames: workspace_id, ref_id, transform_version+ constraintName: uq_workspace_output_versioned+ rollback:++ # ---------- Group 9: workspace_merge ----------++ - changeSet:+ id: v59.2026-01-31T12:00:54+ author: qnkhuat+ comment: Create workspace_merge table to track batch merge events+ preConditions:+ - onFail: MARK_RAN+ - not:+ - tableExists:+ tableName: workspace_merge+ changes:+ - createTable:+ tableName: workspace_merge+ remarks: Tracks batch merge events with commit messages. Survives workspace deletion.+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: workspace_id+ type: int+ remarks: Foreign key to workspace, SET NULL on delete+ constraints:+ nullable: true+ - column:+ name: workspace_name+ type: ${text.type}+ remarks: Inlined copy of workspace name (survives deletion)+ constraints:+ nullable: false+ - column:+ name: commit_message+ type: ${text.type}+ remarks: Description of the merge operation+ constraints:+ nullable: false+ - column:+ name: creator_id+ type: int+ remarks: User who performed the merge+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: Timestamp when the merge was performed+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false++ - changeSet:+ id: v59.2026-01-31T12:00:55+ author: qnkhuat+ comment: Add FK from workspace_merge.workspace_id to workspace with SET NULL+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_merge_workspace_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_merge+ baseColumnNames: workspace_id+ constraintName: fk_workspace_merge_workspace_id+ referencedTableName: workspace+ referencedColumnNames: id+ onDelete: SET NULL++ - changeSet:+ id: v59.2026-01-31T12:00:56+ author: qnkhuat+ comment: Add FK from workspace_merge.creator_id to core_user+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_merge_creator_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_merge+ baseColumnNames: creator_id+ constraintName: fk_workspace_merge_creator_id+ referencedTableName: core_user+ referencedColumnNames: id++ - changeSet:+ id: v59.2026-01-31T12:00:57+ author: qnkhuat+ comment: Add index on workspace_merge.workspace_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_merge+ indexName: idx_workspace_merge_workspace_id+ changes:+ - createIndex:+ tableName: workspace_merge+ indexName: idx_workspace_merge_workspace_id+ columns:+ - column:+ name: workspace_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:00:58+ author: qnkhuat+ comment: Add index on workspace_merge.creator_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_merge+ indexName: idx_workspace_merge_creator_id+ changes:+ - createIndex:+ tableName: workspace_merge+ indexName: idx_workspace_merge_creator_id+ columns:+ - column:+ name: creator_id+ rollback:++ # ---------- Group 10: workspace_merge_transform ----------++ - changeSet:+ id: v59.2026-01-31T12:00:59+ author: qnkhuat+ comment: Create workspace_merge_transform table to track individual transform merges+ preConditions:+ - onFail: MARK_RAN+ - not:+ - tableExists:+ tableName: workspace_merge_transform+ changes:+ - createTable:+ tableName: workspace_merge_transform+ remarks: Tracks individual transform merges. Linked to workspace_merge for batch merges, standalone for single-transform merges.+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: workspace_merge_id+ type: int+ remarks: Foreign key to workspace_merge, CASCADE on delete+ constraints:+ nullable: false+ - column:+ name: workspace_id+ type: int+ remarks: Foreign key to workspace, SET NULL on delete+ constraints:+ nullable: true+ - column:+ name: workspace_name+ type: ${text.type}+ remarks: Inlined copy of workspace name (survives deletion)+ constraints:+ nullable: false+ - column:+ name: transform_id+ type: int+ remarks: Foreign key to transform+ constraints:+ nullable: true+ - column:+ name: commit_message+ type: ${text.type}+ remarks: Description of the merge operation+ constraints:+ nullable: false+ - column:+ name: merging_user_id+ type: int+ remarks: User who performed the merge+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: Timestamp when the merge was performed+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false++ - changeSet:+ id: v59.2026-01-31T12:01:00+ author: qnkhuat+ comment: Add FK from workspace_merge_transform.workspace_merge_id to workspace_merge with CASCADE+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_merge_transform_workspace_merge_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_merge_transform+ baseColumnNames: workspace_merge_id+ constraintName: fk_workspace_merge_transform_workspace_merge_id+ referencedTableName: workspace_merge+ referencedColumnNames: id+ onDelete: CASCADE++ - changeSet:+ id: v59.2026-01-31T12:01:01+ author: qnkhuat+ comment: Add FK from workspace_merge_transform.workspace_id to workspace with SET NULL+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_merge_transform_workspace_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_merge_transform+ baseColumnNames: workspace_id+ constraintName: fk_workspace_merge_transform_workspace_id+ referencedTableName: workspace+ referencedColumnNames: id+ onDelete: SET NULL++ - changeSet:+ id: v59.2026-01-31T12:01:02+ author: qnkhuat+ comment: Add FK from workspace_merge_transform.transform_id to transform with CASCADE+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_merge_transform_transform_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_merge_transform+ baseColumnNames: transform_id+ constraintName: fk_workspace_merge_transform_transform_id+ referencedTableName: transform+ referencedColumnNames: id+ onDelete: CASCADE++ - changeSet:+ id: v59.2026-01-31T12:01:03+ author: qnkhuat+ comment: Add FK from workspace_merge_transform.merging_user_id to core_user+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_merge_transform_merging_user_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_merge_transform+ baseColumnNames: merging_user_id+ constraintName: fk_workspace_merge_transform_merging_user_id+ referencedTableName: core_user+ referencedColumnNames: id++ - changeSet:+ id: v59.2026-01-31T12:01:04+ author: qnkhuat+ comment: Add index on workspace_merge_transform.workspace_merge_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_merge_transform+ indexName: idx_workspace_merge_transform_workspace_merge_id+ changes:+ - createIndex:+ tableName: workspace_merge_transform+ indexName: idx_workspace_merge_transform_workspace_merge_id+ columns:+ - column:+ name: workspace_merge_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:01:05+ author: qnkhuat+ comment: Add index on workspace_merge_transform.workspace_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_merge_transform+ indexName: idx_workspace_merge_transform_workspace_id+ changes:+ - createIndex:+ tableName: workspace_merge_transform+ indexName: idx_workspace_merge_transform_workspace_id+ columns:+ - column:+ name: workspace_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:01:06+ author: qnkhuat+ comment: Add index on workspace_merge_transform.transform_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_merge_transform+ indexName: idx_workspace_merge_transform_transform_id+ changes:+ - createIndex:+ tableName: workspace_merge_transform+ indexName: idx_workspace_merge_transform_transform_id+ columns:+ - column:+ name: transform_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:01:07+ author: qnkhuat+ comment: Add index on workspace_merge_transform.merging_user_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_merge_transform+ indexName: idx_workspace_merge_transform_merging_user_id+ changes:+ - createIndex:+ tableName: workspace_merge_transform+ indexName: idx_workspace_merge_transform_merging_user_id+ columns:+ - column:+ name: merging_user_id+ rollback:++ # ---------- Group 11: workspace_output_external ----------++ - changeSet:+ id: v59.2026-01-31T12:01:08+ author: christruter+ comment: Create workspace_output_external table for output tables from external transforms+ preConditions:+ - onFail: MARK_RAN+ - not:+ - tableExists:+ tableName: workspace_output_external+ changes:+ - createTable:+ tableName: workspace_output_external+ remarks: Output tables produced by external transforms enclosed in a workspace+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: workspace_id+ type: int+ remarks: FK to workspace+ constraints:+ nullable: false+ - column:+ name: transform_id+ type: int+ remarks: FK to transform (the external transform)+ constraints:+ nullable: false+ - column:+ name: db_id+ type: int+ remarks: Database where the output table is created+ constraints:+ nullable: false+ - column:+ name: global_schema+ type: varchar(254)+ remarks: Schema of the original output table+ constraints:+ nullable: true+ - column:+ name: global_table+ type: varchar(254)+ remarks: Name of the original output table+ constraints:+ nullable: false+ - column:+ name: global_table_id+ type: int+ remarks: FK to metabase_table for the global output table+ constraints:+ nullable: true+ - column:+ name: isolated_schema+ type: varchar(254)+ remarks: Schema name in the isolated workspace namespace+ constraints:+ nullable: true+ - column:+ name: isolated_table+ type: varchar(254)+ remarks: Table name in the isolated workspace namespace+ constraints:+ nullable: false+ - column:+ name: isolated_table_id+ type: int+ remarks: FK to metabase_table for the isolated output table+ constraints:+ nullable: true+ - column:+ name: graph_version+ type: bigint+ remarks: The graph_version this row was calculated for+ defaultValueNumeric: 0+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: Timestamp when the output was created+ constraints:+ nullable: false+ - column:+ name: updated_at+ type: ${timestamp_type}+ remarks: Timestamp when the output was last updated+ constraints:+ nullable: false++ - changeSet:+ id: v59.2026-01-31T12:01:09+ author: christruter+ comment: Add index on workspace_output_external.workspace_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_output_external+ indexName: idx_workspace_output_external_workspace_id+ changes:+ - createIndex:+ tableName: workspace_output_external+ indexName: idx_workspace_output_external_workspace_id+ columns:+ - column:+ name: workspace_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:01:10+ author: christruter+ comment: Add index on workspace_output_external.transform_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_output_external+ indexName: idx_workspace_output_external_transform_id+ changes:+ - createIndex:+ tableName: workspace_output_external+ indexName: idx_workspace_output_external_transform_id+ columns:+ - column:+ name: transform_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:01:11+ author: christruter+ comment: Add FK from workspace_output_external.workspace_id to workspace with CASCADE+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_output_external_workspace_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_output_external+ baseColumnNames: workspace_id+ constraintName: fk_workspace_output_external_workspace_id+ referencedTableName: workspace+ referencedColumnNames: id+ onDelete: CASCADE++ - changeSet:+ id: v59.2026-01-31T12:01:12+ author: christruter+ comment: Add FK from workspace_output_external.transform_id to transform with CASCADE+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_output_external_transform_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_output_external+ baseColumnNames: transform_id+ constraintName: fk_workspace_output_external_transform_id+ referencedTableName: transform+ referencedColumnNames: id+ onDelete: CASCADE++ - changeSet:+ id: v59.2026-01-31T12:01:13+ author: christruter+ comment: Add unique constraint on workspace_output_external (workspace_id, transform_id, graph_version)+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_output_external+ indexName: uq_workspace_output_external_versioned+ changes:+ - addUniqueConstraint:+ tableName: workspace_output_external+ columnNames: workspace_id, transform_id, graph_version+ constraintName: uq_workspace_output_external_versioned+ rollback:++ # ---------- Group 12: workspace_input_external ----------++ - changeSet:+ id: v59.2026-01-31T12:01:14+ author: christruter+ comment: Create workspace_input_external table for external tables consumed by external transforms+ preConditions:+ - onFail: MARK_RAN+ - not:+ - tableExists:+ tableName: workspace_input_external+ changes:+ - createTable:+ tableName: workspace_input_external+ remarks: External tables consumed by external transforms enclosed in a workspace (deduplicated per workspace)+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: workspace_id+ type: int+ remarks: FK to workspace+ constraints:+ nullable: false+ - column:+ name: db_id+ type: int+ remarks: Database containing the source table+ constraints:+ nullable: false+ - column:+ name: schema+ type: varchar(254)+ remarks: Schema of the source table (nullable for DBs without schemas)+ constraints:+ nullable: true+ - column:+ name: table+ type: varchar(254)+ remarks: Name of the source table+ constraints:+ nullable: false+ - column:+ name: table_id+ type: int+ remarks: FK to metabase_table if table exists and is synced+ constraints:+ nullable: true+ - column:+ name: access_granted+ type: ${boolean.type}+ remarks: Whether read access has been granted to the workspace user+ defaultValueBoolean: false+ constraints:+ nullable: false+ - column:+ name: graph_version+ type: bigint+ remarks: The graph_version this row was calculated for+ defaultValueNumeric: 0+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: Timestamp when the input was created+ constraints:+ nullable: false+ - column:+ name: updated_at+ type: ${timestamp_type}+ remarks: Timestamp when the input was last updated+ constraints:+ nullable: false++ - changeSet:+ id: v59.2026-01-31T12:01:15+ author: christruter+ comment: Add index on workspace_input_external.workspace_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_input_external+ indexName: idx_workspace_input_external_workspace_id+ changes:+ - createIndex:+ tableName: workspace_input_external+ indexName: idx_workspace_input_external_workspace_id+ columns:+ - column:+ name: workspace_id+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:01:16+ author: christruter+ comment: Add FK from workspace_input_external.workspace_id to workspace with CASCADE+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_input_external_workspace_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_input_external+ baseColumnNames: workspace_id+ constraintName: fk_workspace_input_external_workspace_id+ referencedTableName: workspace+ referencedColumnNames: id+ onDelete: CASCADE++ - changeSet:+ id: v59.2026-01-31T12:01:17+ author: christruter+ comment: Add unique constraint on workspace_input_external (workspace_id, db_id, schema, table, graph_version)+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_input_external+ indexName: uq_workspace_input_external_versioned+ changes:+ - addUniqueConstraint:+ tableName: workspace_input_external+ columnNames: workspace_id, db_id, schema, table, graph_version+ constraintName: uq_workspace_input_external_versioned+ rollback:++ # ---------- Group 13: workspace_graph ----------++ - changeSet:+ id: v59.2026-01-31T12:01:18+ author: christruter+ comment: Create workspace_graph table for cached dependency graphs+ preConditions:+ - onFail: MARK_RAN+ - not:+ - tableExists:+ tableName: workspace_graph+ changes:+ - createTable:+ tableName: workspace_graph+ remarks: Stores cached dependency graph for each workspace+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: workspace_id+ type: int+ remarks: Reference to the workspace this graph belongs to+ constraints:+ nullable: false+ - column:+ name: graph+ type: ${text.type}+ remarks: JSON representation of the dependency graph+ constraints:+ nullable: false+ - column:+ name: graph_version+ type: bigint+ remarks: The graph_version this cached graph was calculated for+ defaultValueNumeric: 0+ constraints:+ nullable: false+ - column:+ name: created_at+ type: ${timestamp_type}+ remarks: Timestamp when the cached graph was created+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false+ - column:+ name: updated_at+ type: ${timestamp_type}+ remarks: Timestamp when the cached graph was last updated+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false++ - changeSet:+ id: v59.2026-01-31T12:01:19+ author: christruter+ comment: Add unique index on workspace_graph (workspace_id, graph_version)+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: workspace_graph+ indexName: idx_workspace_graph_workspace_version+ changes:+ - createIndex:+ tableName: workspace_graph+ indexName: idx_workspace_graph_workspace_version+ unique: true+ columns:+ - column:+ name: workspace_id+ - column:+ name: graph_version+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:01:20+ author: christruter+ comment: Add FK from workspace_graph.workspace_id to workspace with CASCADE+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyName: fk_workspace_graph_workspace_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace_graph+ baseColumnNames: workspace_id+ constraintName: fk_workspace_graph_workspace_id+ referencedTableName: workspace+ referencedColumnNames: id+ onDelete: CASCADE++ # ---------- Group 14: Existing table changes ----------++ - changeSet:+ id: v59.2026-01-31T12:01:21+ author: piranha+ comment: Add target_db_id column to transform table+ preConditions:+ - onFail: MARK_RAN+ - not:+ - columnExists:+ tableName: transform+ columnName: target_db_id+ changes:+ - addColumn:+ tableName: transform+ columns:+ - column:+ name: target_db_id+ type: int+ remarks: Target database ID for the transform output+ constraints:+ nullable: true+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:01:22+ author: qnkhuat+ comment: Add workspace_permissions_status column to metabase_database table+ preConditions:+ - onFail: MARK_RAN+ - not:+ - columnExists:+ tableName: metabase_database+ columnName: workspace_permissions_status+ changes:+ - addColumn:+ tableName: metabase_database+ columns:+ - column:+ name: workspace_permissions_status+ type: ${text.type}+ remarks: Status of workspace isolation permission check (JSON)+ constraints:+ nullable: true+ rollback:+++ - changeSet:+ id: v59.2026-01-31T12:01:23+ author: christruter+ comment: Backfill target_db_id for existing transforms+ changes:+ - customChange:+ class: "metabase.app_db.custom_migrations.BackfillTransformTargetDbId"++ - changeSet:+ id: v59.2026-01-31T12:01:24+ author: christruter+ comment: Add index on transform.target_db_id+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: transform+ indexName: idx_transform_target_db_id+ changes:+ - createIndex:+ tableName: transform+ indexName: idx_transform_target_db_id+ columns:+ - column:+ name: target_db_id++ - changeSet:+ id: v59.2026-01-31T12:01:25+ author: christruter+ comment: Clean up orphaned target_db_id references before adding FK constraint+ changes:+ - sql:+ sql: >-+ UPDATE transform+ SET target_db_id = NULL+ WHERE target_db_id IS NOT NULL+ AND target_db_id NOT IN (SELECT id FROM metabase_database)+ rollback:++ - changeSet:+ id: v59.2026-01-31T12:01:26+ author: christruter+ comment: Add foreign key constraint from transform.target_db_id to metabase_database+ validCheckSum: ANY+ preConditions:+ - onFail: MARK_RAN+ - not:+ - foreignKeyConstraintExists:+ foreignKeyTableName: transform+ foreignKeyName: fk_transform_target_db_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: transform+ baseColumnNames: target_db_id+ referencedTableName: metabase_database+ referencedColumnNames: id+ constraintName: fk_transform_target_db_id+ onDelete: SET NULL+# >>>>>>>>>> DO NOT ADD NEW MIGRATIONS BELOW THIS LINE! ADD THEM ABOVE <<<<<<<<<<########################################################################################################################diff --git a/src/metabase/actions/settings.clj b/src/metabase/actions/settings.cljindex 126f9d147b1..df9a18699ae 100644--- a/src/metabase/actions/settings.clj+++ b/src/metabase/actions/settings.clj@@ -39,7 +39,7 @@;; effect once the warning is resolved.(setting/defsetting database-enable-table-editing- (i18n/deferred-tru "Whether to enable table data editing for a specific Database.")+ (i18n/deferred-tru "Whether the Database has table data editing enabled."):default false:feature :table-data-editing:driver-feature :actions/data-editingdiff --git a/src/metabase/analytics/stats.clj b/src/metabase/analytics/stats.cljindex 1092c519c54..770c6469fa9 100644--- a/src/metabase/analytics/stats.clj+++ b/src/metabase/analytics/stats.clj@@ -949,7 +949,10 @@:enabled (premium-features/enable-dependencies?)}{:name :support-users:available (premium-features/enable-support-users?)- :enabled (premium-features/enable-support-users?)}])+ :enabled (premium-features/enable-support-users?)}+ {:name :workspaces+ :available (premium-features/enable-workspaces?)+ :enabled (premium-features/enable-workspaces?)}])(defn- snowplow-features[]diff --git a/src/metabase/api/macros.clj b/src/metabase/api/macros.cljindex 4fb16717f96..5d18a49a802 100644--- a/src/metabase/api/macros.clj+++ b/src/metabase/api/macros.clj@@ -636,11 +636,18 @@(mr/def ::ns-endpoints [:map-of ::unique-key ::info])+(mr/def ::route-metadata+ "Metadata declared on a route via defendpoint, e.g. `{:access :workspace}`."+ :map)+(mr/def ::handler-map- [:map-of ::method [:sequential [:tuple (ms/InstanceOfClass clout.core.CompiledRoute) ::handler]]])+ [:map-of ::method [:sequential [:tuple+ (ms/InstanceOfClass clout.core.CompiledRoute)+ ::handler+ [:maybe ::route-metadata]]]])(mu/defn- ns-handler-map :- ::handler-map- "Build a map of method => [[clout-route handler]+] used to power the combined ns handler built+ "Build a map of method => [[clout-route handler metadata]+] used to power the combined ns handler builtby [[build-ns-handler]]."[endpoints :- ::ns-endpoints](->> endpoints@@ -650,7 +657,8 @@(mapv (fn [route][(clout/route-compile (get-in route [:form :route :path])(get-in route [:form :route :regexes] {}))- (:handler route)])+ (:handler route)+ (get-in route [:form :metadata])])routes)))))(defn- decode-route-params [route-params]@@ -661,7 +669,7 @@[request' handler]- (Request is updated to include parsed Clout parameters.)"+ (Request is updated to include parsed Clout parameters and route metadata.)"[handler-map :- ::handler-maprequest :- ::request](let [request-method (:request-method request)@@ -670,9 +678,11 @@request (cond-> requestpath (assoc :path-info path))];; TODO -- we could probably make this a little faster by unrolling this loop- (some (fn [[route handler]]+ (some (fn [[route handler metadata]](when-let [route-params (clout/route-matches route request)]- [(assoc request :route-params (decode-route-params route-params))+ [(-> request+ (assoc :route-params (decode-route-params route-params))+ (assoc :route-metadata metadata))handler]))handlers)))diff --git a/src/metabase/api/open_api.clj b/src/metabase/api/open_api.cljindex bbeea0d0a0a..143f8e8f3f4 100644--- a/src/metabase/api/open_api.clj+++ b/src/metabase/api/open_api.clj@@ -96,7 +96,7 @@[:map[:type [:= :string]]- [:format {:optional true} [:enum :binary "binary" :byte "byte" :uuid "uuid"]]+ [:format {:optional true} [:enum :binary "binary" :byte "byte" :uuid "uuid" :date-time "date-time"]][:minLength {:optional true} integer?][:maxLength {:optional true} integer?][:pattern {:optional true} (ms/InstanceOfClass java.util.regex.Pattern)]]])diff --git a/src/metabase/api_keys/core.clj b/src/metabase/api_keys/core.cljindex 7e56f823a79..e9edf04f565 100644--- a/src/metabase/api_keys/core.clj+++ b/src/metabase/api_keys/core.clj@@ -9,4 +9,5 @@[metabase.api-keys.models.api-keygenerate-keyis-api-key-user?- prefix])+ prefix+ create-api-key-with-new-user!])diff --git a/src/metabase/api_keys/models/api_key.clj b/src/metabase/api_keys/models/api_key.cljindex 1b6dbb065c0..b6be3996993 100644--- a/src/metabase/api_keys/models/api_key.clj+++ b/src/metabase/api_keys/models/api_key.clj@@ -203,10 +203,10 @@(mu/defn create-api-key-with-new-user!"Create a new API key and a new user for that key at the same time."- [{:keys [key-name group-id]} :- [:map- {:closed true}- [:key-name ::api-keys.schema/name]- [:group-id {:optional true} pos-int?]]]+ [{:keys [key-name group-id]}+ :- [:map {:closed true}+ [:key-name ::api-keys.schema/name]+ [:group-id {:optional true} pos-int?]]](api/checkp (not (t2/exists? :model/ApiKey :name key-name))"name" "An API key with this name already exists.")(let [unhashed-key (key-with-unique-prefix)@@ -218,7 +218,8 @@:last_name "":type :api-key:password (str (random-uuid))})]- (user/set-permissions-groups! user-id [(perms/all-users-group) group-id])+ (when group-id+ (user/set-permissions-groups! user-id [(perms/all-users-group) group-id]))(-> (t2/insert-returning-instance! :model/ApiKey{:user_id user-id:name key-namediff --git a/src/metabase/app_db/custom_migrations.clj b/src/metabase/app_db/custom_migrations.cljindex ed74f2b9e65..a0ef4bfec3d 100644--- a/src/metabase/app_db/custom_migrations.clj+++ b/src/metabase/app_db/custom_migrations.clj@@ -1821,6 +1821,28 @@{:transform-id id :source-type source-type})))](t2/update! :transform id {:source_type transform-type})))))+(define-migration BackfillTransformTargetDbId+ ;; Backfills the target_db_id column for existing transform records.+ ;;+ ;; For query transforms, uses source.query.database as the source of truth.+ ;; For python transforms, uses target.database. Records with no database+ ;; information are left with nil target_db_id.+ ;;+ ;; Skips transforms whose referenced database no longer exists (leaves target_db_id NULL),+ ;; since the subsequent FK constraint migration would fail if stale DB references were set.+ (let [existing-db-ids (into #{} (map :id) (t2/query {:select [:id] :from [:metabase_database]}))]+ (doseq [{:keys [id source target]} (t2/query {:select [:id :source :target]+ :from [:transform]+ :where [:= :target_db_id nil]})]+ (let [source-map (json-out source false)+ target-map (json-out target false)+ ;; Mirror the app-code logic: for query transforms, source.query.database is the source of truth;+ ;; for python transforms, target.database is the only option.+ db-id (or (get-in source-map ["query" "database"])+ (get target-map "database"))]+ (when (and db-id (existing-db-ids db-id))+ (t2/update! :transform id {:target_db_id db-id}))))))+(define-migration SetTransformSourceDatabaseId(doseq [transform (t2/select [:transform :id :source])](let [parsed-source (-> transform :source json/decode+kw)diff --git a/src/metabase/app_db/query.clj b/src/metabase/app_db/query.cljindex 0e135304ce1..93bcde0d750 100644--- a/src/metabase/app_db/query.clj+++ b/src/metabase/app_db/query.clj@@ -209,8 +209,9 @@In the case where there is no underlying db constraint, concurrent calls may still result in duplicates.To prevent this in a database agnostic way, during an existing non-serializable transaction, would be non-trivial."- [model select-map update-fn]- (let [select-kvs (mapcat identity select-map)+ [model select-map & [update-fn]]+ (let [update-fn (or update-fn (constantly select-map))+ select-kvs (mapcat identity select-map)pks (t2/primary-keys model)_ (assert (= 1 (count pks)) "This helper does not currently support compound keys")pk-key (keyword (first pks))diff --git a/src/metabase/collections/models/collection.clj b/src/metabase/collections/models/collection.cljindex cd9b9fc97cf..a6acef491f4 100644--- a/src/metabase/collections/models/collection.clj+++ b/src/metabase/collections/models/collection.clj@@ -2023,7 +2023,8 @@(serdes/fk :model/Collection){:export location-path->parent-id:import parent-id->location-path}))- :personal_owner_id (serdes/fk :model/User)}})+ :personal_owner_id (serdes/fk :model/User)+ :workspace_id (serdes/fk :model/Workspace)}});;; +----------------------------------------------------------------------------------------------------------------+;;; | Perms Checking Helper Fns |diff --git a/src/metabase/driver.clj b/src/metabase/driver.cljindex f006f6338ae..7e57347e684 100644--- a/src/metabase/driver.clj+++ b/src/metabase/driver.clj@@ -21,6 +21,7 @@[metabase.util :as u][metabase.util.i18n :refer [tru]][metabase.util.json :as json]+ [metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.registry :as mr][metabase.util.performance :refer [mapv empty?]]@@ -62,6 +63,91 @@[driver & body]`(do-with-driver ~driver (fn [] ~@body)))+;;; +----------------------------------------------------------------------------------------------------------------++;;; | Connection Details Swapping |+;;; +----------------------------------------------------------------------------------------------------------------+++(def ^:private ^:dynamic *swapped-connection-details*+ "A dynamic var that holds a map of database-id -> swapped-details-map for temporarily swapping connection details.+ When a connection spec is created for a database, if its ID is present in this map, the swap map will be+ merged into the connection `:details` before they are used to create a connection.++ This provides a mechanism for temporarily using different connection details (e.g., using alternative credentials+ for workspaces) without mutating the database record.++ The swap map is merged into the database `:details` map. The swap is applied before any connection-specific+ processing (like hash calculation for connection pooling), so different swaps will result in different+ connection pools.++ Different drivers may apply this swap at different points in their connection lifecycle, but the semantics+ are consistent: swapped details are used for the duration of the dynamic scope.++ See [[with-swapped-connection-details]] for usage."+ nil)++(defn- apply-detail-swaps+ "Merges the `swap-map` into `details`. Supports nested maps via deep merge."+ [details swap-map]+ (reduce-kv+ (fn [acc k v]+ (if (and (map? v) (map? (get acc k)))+ (assoc acc k (apply-detail-swaps (get acc k) v))+ (assoc acc k v)))+ details+ swap-map))++(defn has-connection-swap?+ "Returns true if there is an active connection detail swap for `database-id`."+ [database-id]+ (contains? *swapped-connection-details* database-id))++(defn maybe-swap-details+ "Returns the database details with any swaps applied from [[*swapped-connection-details*]].+ If no swap exists for `database-id`, returns `details` unchanged.++ Drivers should call this function when creating connections to apply any active swaps.+ For JDBC drivers, this is called in [[metabase.driver.sql-jdbc.connection/db->pooled-connection-spec]].+ For other drivers (e.g., MongoDB), this should be called in their connection creation logic."+ [database-id details]+ (if-let [swap-map (get *swapped-connection-details* database-id)]+ (do+ (log/debugf "Applying swapped connection details for database %d, swap keys: %s"+ database-id (keys swap-map))+ (apply-detail-swaps details swap-map))+ details))++(defn do-with-swapped-connection-details+ "Implementation for [[with-swapped-connection-details]]."+ [database-id swap-map thunk]+ (when (contains? *swapped-connection-details* database-id)+ (throw (ex-info "Nested connection detail swaps are not supported for the same database"+ {:database-id database-id})))+ (log/debugf "Entering swapped connection details scope for database %d, swap keys: %s"+ database-id (keys swap-map))+ (binding [*swapped-connection-details* (assoc *swapped-connection-details* database-id swap-map)]+ (thunk)))++(defmacro with-swapped-connection-details+ "Temporarily swap the connection details for a specific database within the dynamic scope of `body`.++ The `swap-map` is a map of detail keys to swap values. These will be merged into the database's+ connection `:details` map. Nested maps are deep-merged.++ Any code that creates a connection for `database-id` within this scope will use the modified details.++ **Important:** Nested swaps for the same database are not supported and will throw an exception.+ Different databases can have concurrent swaps.++ Example:++ ;; Swap connection to use alternate credentials+ (driver/with-swapped-connection-details 1 {:user \"workspace-user\" :password \"workspace-pass\"}+ ;; All connections created in this scope use the swapped credentials+ (qp/process-query query))"+ {:style/indent 2}+ [database-id swap-map & body]+ `(do-with-swapped-connection-details ~database-id ~swap-map (fn [] ~@body)))+;;; +----------------------------------------------------------------------------------------------------------------+;;; | Driver Registration / Hierarchy / Multimethod Dispatch |;;; +----------------------------------------------------------------------------------------------------------------+@@ -815,7 +901,10 @@:describe-is-nullable;; Does this driver provide :database-is-generated on (describe-fields) or (describe-table)- :describe-is-generated})+ :describe-is-generated++ ;; Does this driver support the workspace feature+ :workspace})(defmulti database-supports?"Does this driver and specific instance of a database support a certain `feature`?@@ -862,7 +951,8 @@:test/uuids-in-create-table-statements true:test/use-fake-sync false:metadata/table-existence-check false- :metadata/table-writable-check false}]+ :metadata/table-writable-check false+ :workspace false}](defmethod database-supports? [::driver feature] [_driver _feature _db] supported?));;; By default a driver supports `:native-parameter-card-reference` if it supports `:native-parameters` AND@@ -1290,6 +1380,31 @@:args args}))#{}))+(mr/def ::native-query-table-refs.table-ref+ [:map+ {:closed true}+ [:schema {:optional true} [:maybe :string]]+ [:table :string]])++(mr/def ::native-query-table-refs+ [:set ::native-query-table-refs.table-ref])++(defmulti native-query-table-refs+ "Gets the raw table references from a native query without resolving them to IDs.++ Unlike [[native-query-deps]] which looks up tables in the database to return IDs,+ this method returns just the schema and table names as they appear in the query.+ This is useful for workspace dependency tracking where referenced tables may not+ exist yet.++ `query` is a Lib `:metabase.lib.schema/native-only-query`; you can use+ [[metabase.driver-api.core/raw-native-query]] to get the raw native query as needed.++ The return value should match the `:metabase.driver/native-query-table-refs` schema."+ {:changelog-test/ignore true :added "0.58.0" :arglists '([driver query])}+ dispatch-on-initialized-driver+ :hierarchy #'hierarchy)+(defmulti native-result-metadata"Gets the result-metadata for a native query using static analysis (i.e., without actuallygoing to the database).@@ -1715,3 +1830,75 @@(if (table-known-to-not-exist? driver e)false(throw e)))))++;;; +----------------------------------------------------------------------------------------------------------------++;;; | Workspace Isolation |+;;; +----------------------------------------------------------------------------------------------------------------+++(defmulti init-workspace-isolation!+ "Initialize database isolation for a workspace. Creates an isolated schema/database,+ user credentials, and grants appropriate permissions for the workspace to operate+ within its own namespace.++ Returns a map with:+ - :schema - The name of the isolated schema/database created+ - :database_details - Connection details (user, password, etc.) for the isolated user++ Implementations should:+ - Create an isolated schema or database for the workspace+ - Create a user with credentials that can only access that schema+ - Grant appropriate permissions (CREATE, INSERT, SELECT, etc.) on the isolated schema++ This is an enterprise feature. Drivers must also return true for+ (database-supports? driver :workspace database) to indicate support."+ {:added "0.59.0" :arglists '([driver database workspace])}+ dispatch-on-initialized-driver+ :hierarchy #'hierarchy)++(defmulti destroy-workspace-isolation!+ "Destroy all database resources created for workspace isolation.+ This includes dropping schemas/databases, users, roles, and any other+ resources created by init-workspace-isolation!.++ Should be called when deleting a workspace. Implementations should be+ idempotent - calling on an already-destroyed workspace should not error."+ {:added "0.59.0" :arglists '([driver database workspace])}+ dispatch-on-initialized-driver+ :hierarchy #'hierarchy)++(defmulti grant-workspace-read-access!+ "Grant read access on specified tables to a workspace's isolated user.+ This allows the workspace to read from source tables that it needs as inputs.++ `tables` is a sequence of maps with :schema and :name keys identifying+ the tables to grant access to."+ {:added "0.59.0" :arglists '([driver database workspace tables])}+ dispatch-on-initialized-driver+ :hierarchy #'hierarchy)++(defmulti check-isolation-permissions+ "Check if database connection has sufficient permissions for workspace isolation.++ Rather than directly checking permissions, this method performs the actual isolation+ operations (init workspace, grant access, destroy resources) in a test workspace+ because:++ 1. Some databases don't provide reliable APIs to check permissions a priori.+ 2. Keeping static permission checks in sync with the actual operations is error-prone.+ 3. A database user might have the necessary workspace permissions even if they+ lack the introspection permissions to query permission tables.++ Isolation operations run in a transaction that is always rolled back (for databases+ that support transactional DDL), or are manually cleaned up immediately after testing+ (for databases where transactions don't work, like BigQuery).++ `test-table` is an optional {:schema ... :name ...} map used to test GRANT SELECT.+ If nil, the grant test is skipped.++ Returns nil on success, or an error message string on failure.++ Default :sql-jdbc implementation tests CREATE SCHEMA, CREATE USER, GRANT, and DROP.+ Drivers can override for database-specific syntax."+ {:added "0.59.0" :arglists '([driver database test-table])}+ dispatch-on-initialized-driver+ :hierarchy #'hierarchy)diff --git a/src/metabase/driver/h2.clj b/src/metabase/driver/h2.cljindex 41b3601a985..41b8c194a25 100644--- a/src/metabase/driver/h2.clj+++ b/src/metabase/driver/h2.clj@@ -1,9 +1,11 @@(ns metabase.driver.h2- (:refer-clojure :exclude [some every?])+ (:refer-clojure :exclude [some every? get-in])(:require+ [clojure.java.jdbc :as jdbc][clojure.math.combinatorics :as math.combo][clojure.string :as str][java-time.api :as t]+ [metabase.config.core :as config][metabase.driver :as driver][metabase.driver-api.core :as driver-api][metabase.driver.common :as driver.common]@@ -17,6 +19,7 @@[metabase.driver.sql-jdbc.sync :as sql-jdbc.sync][metabase.driver.sql.normalize :as sql.normalize][metabase.driver.sql.query-processor :as sql.qp]+ [metabase.driver.util :as driver.u][metabase.lib.schema.id :as lib.schema.id][metabase.lib.schema.metadata :as lib.schema.metadata][metabase.util :as u]@@ -24,9 +27,9 @@[metabase.util.i18n :refer [deferred-tru tru]][metabase.util.log :as log][metabase.util.malli :as mu]- [metabase.util.performance :refer [every? some]])+ [metabase.util.performance :refer [every? get-in some]])(:import- (java.sql Clob ResultSet ResultSetMetaData SQLException)+ (java.sql Clob Connection ResultSet ResultSetMetaData SQLException Statement)(java.time OffsetTime)(org.h2.command CommandInterface Parser)(org.h2.engine SessionLocal)))@@ -72,6 +75,9 @@:test/jvm-timezone-setting false:uuid-type true:uploads true+ ;; (Ngoc - 2026-01-27) we have the code to support workspace isolation but since workspace+ ;; is useless with out transforms, so we disable it for now+ :workspace false:database-routing true:describe-is-generated true:describe-is-nullable true@@ -140,7 +146,7 @@:monday);; TODO - it would be better not to put all the options in the connection string in the first place?-(defn- connection-string->file+options+(defn connection-string->file+options"Explode a `connection-string` like `file:my-db;OPTION=100;OPTION_2=TRUE` to a pair of file and an options map.(connection-string->file+options \"file:my-crazy-db;OPTION=100;OPTION_X=TRUE\")@@ -149,7 +155,7 @@{:pre [(string? connection-string)]}(let [[file & options] (str/split connection-string #";+")options (into {} (for [option options]- (str/split option #"=")))]+ (str/split option #"=" 2)))][file options]))(defn- db-details->user [{:keys [db], :as details}]@@ -167,8 +173,9 @@(when (= (keyword query-type) :native)(let [{:keys [details]} (driver-api/database (driver-api/metadata-provider))user (db-details->user details)]- (when (or (str/blank? user)- (= user "sa")) ; "sa" is the default USER+ (when (and config/is-prod? ;; we elevated permissions in workspace tests+ (or (str/blank? user)+ (= user "sa"))) ; "sa" is the default USER(throw(ex-info (tru "Running SQL queries against H2 databases using the default (admin) database user is forbidden."){:type driver-api/qp.error-type.db})))))))@@ -517,7 +524,7 @@;; These functions for exploding / imploding the options in the connection strings are here so we can override shady;; options users might try to put in their connection string, like INIT=...-(defn- file+options->connection-string+(defn file+options->connection-string"Implode the results of `connection-string->file+options` back into a connection string."[file options](apply str file (for [[k v] options]@@ -675,5 +682,68 @@[_]"PUBLIC")+;;; +----------------------------------------------------------------------------------------------------------------++;;; | Workspace Isolation |+;;; +----------------------------------------------------------------------------------------------------------------+++(defn- replace-credentials+ "Replace USER and PASSWORD in an H2 connection string."+ [connection-string new-user new-password]+ (let [[file options] (connection-string->file+options connection-string)]+ (file+options->connection-string file (assoc options "USER" new-user "PASSWORD" new-password))))++(defn- get-user-from-connection-string+ "Extract the USER from an H2 connection string."+ [connection-string]+ (let [[_file options] (connection-string->file+options connection-string)]+ (get options "USER")))++(defmethod driver/init-workspace-isolation! :h2+ [_driver database workspace]+ (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)+ username (driver.u/workspace-isolation-user-name workspace)+ password (driver.u/random-workspace-password)+ ;; H2 embeds credentials in the :db connection string, so we need to build a new one+ original-db (get-in database [:details :db])+ new-db (replace-credentials original-db username password)]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql [(format "CREATE USER IF NOT EXISTS \"%s\" PASSWORD '%s'" username password)+ (format "CREATE SCHEMA IF NOT EXISTS \"%s\" AUTHORIZATION \"%s\"" schema-name username)+ (format "GRANT ALL ON SCHEMA \"%s\" TO \"%s\"" schema-name username)]]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt)))+ {:schema schema-name+ :database_details {:db new-db}}))++(defmethod driver/destroy-workspace-isolation! :h2+ [_driver database workspace]+ (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)+ username (driver.u/workspace-isolation-user-name workspace)]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql [;; CASCADE drops all objects (tables, etc.) in the schema+ (format "DROP SCHEMA IF EXISTS \"%s\" CASCADE" schema-name)+ (format "DROP USER IF EXISTS \"%s\"" username)]]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt)))))++(defmethod driver/grant-workspace-read-access! :h2+ [_driver database workspace tables]+ (let [username (-> workspace :database_details :db get-user-from-connection-string)+ schemas (distinct (map :schema tables))]+ ;; H2 uses GRANT SELECT ON SCHEMA schemaName TO userName+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [schema schemas]+ (.addBatch ^Statement stmt+ ^String (format "GRANT SELECT ON SCHEMA \"%s\" TO \"%s\"" schema username)))+ ;; Also grant on individual tables for more fine-grained access+ (doseq [table tables]+ (.addBatch ^Statement stmt+ ^String (format "GRANT SELECT ON \"%s\".\"%s\" TO \"%s\""+ (:schema table) (:name table) username)))+ (.executeBatch ^Statement stmt)))))+(defmethod driver/llm-sql-dialect-resource :h2 [_]"llm/prompts/dialects/h2.md")diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.cljindex cae2882d652..ba9e27f05d1 100644--- a/src/metabase/driver/mysql.clj+++ b/src/metabase/driver/mysql.clj@@ -24,6 +24,7 @@[metabase.driver.sql.query-processor :as sql.qp][metabase.driver.sql.query-processor.util :as sql.qp.u][metabase.driver.sql.util :as sql.u]+ [metabase.driver.util :as driver.u][metabase.util :as u][metabase.util.date-2 :as u.date][metabase.util.honey-sql-2 :as h2x]@@ -33,10 +34,12 @@(:import(java.io File)(java.sql+ ConnectionDatabaseMetaDataResultSetResultSetMetaDataSQLException+ StatementTypes)(java.timeLocalDateTime@@ -99,7 +102,8 @@:transforms/index-ddl false:describe-default-expr true:describe-is-nullable true- :describe-is-generated true}]+ :describe-is-generated true+ :workspace true}](defmethod driver/database-supports? [:mysql feature] [_driver _feature _db] supported?));; This is a bit of a lie since the JSON type was introduced for MySQL since 5.7.8.@@ -1219,5 +1223,113 @@[_driver]nil)+;;; +----------------------------------------------------------------------------------------------------------------++;;; | Workspace Isolation |+;;; +----------------------------------------------------------------------------------------------------------------+++(defn- mysql-user-exists?+ "Check if a MySQL user exists."+ [conn username]+ (seq (jdbc/query conn ["SELECT 1 FROM mysql.user WHERE user = ?" username])))++(defmethod driver/init-workspace-isolation! :mysql+ [_driver database workspace]+ ;; MySQL doesn't have schemas in the PostgreSQL sense - each database is its own namespace.+ ;; We create a separate database for workspace isolation.+ (let [db-name (driver.u/workspace-isolation-namespace-name workspace)+ user (driver.u/workspace-isolation-user-name workspace)+ password (driver.u/random-workspace-password)+ escaped-password (sql.u/escape-sql password :ansi)]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (let [user-sql (if (mysql-user-exists? t-conn user)+ (format "ALTER USER `%s`@'%%' IDENTIFIED BY '%s'"+ user escaped-password)+ (format "CREATE USER `%s`@'%%' IDENTIFIED BY '%s'"+ user escaped-password))]+ (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql [;; Create the isolated database+ (format "CREATE DATABASE IF NOT EXISTS `%s`" db-name)+ user-sql+ ;; Grant all privileges on the isolated database+ (format "GRANT ALL PRIVILEGES ON `%s`.* TO `%s`@'%%'" db-name user)]]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt))))+ {:schema db-name+ :database_details {:user user, :password password :db db-name}}))++(defmethod driver/destroy-workspace-isolation! :mysql+ [_driver database workspace]+ (let [db-name (:schema workspace)+ username (-> workspace :database_details :user)]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql (cond-> [(format "DROP DATABASE IF EXISTS `%s`" db-name)]+ (mysql-user-exists? t-conn username)+ (conj (format "DROP USER IF EXISTS `%s`@'%%'" username)))]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt)))))++(defmethod driver/grant-workspace-read-access! :mysql+ [_driver database workspace tables]+ (let [username (-> workspace :database_details :user)+ ;; In MySQL, tables don't have separate schemas within a database,+ ;; but the :schema field contains the source database name+ sqls (for [{db :schema, t :name} tables]+ (if (str/blank? db)+ (format "GRANT SELECT ON `%s` TO `%s`@'%%'" t username)+ (format "GRANT SELECT ON `%s`.`%s` TO `%s`@'%%'" db t username)))]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql sqls]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt)))))++;; MySQL doesn't support transactional DDL, so we need to override check-isolation-permissions+;; to manually clean up after testing rather than relying on transaction rollback.+(def ^:private perm-check-workspace-id "-1337")++(defmethod driver/check-isolation-permissions :mysql+ [driver database test-table]+ (let [test-workspace {:id perm-check-workspace-id+ :name "_mb_perm_check_"}]+ (sql-jdbc.execute/do-with-connection-with-options+ driver+ database+ {:write? true}+ (fn [^Connection _conn]+ (let [result (try+ (let [init-result (try+ (driver/init-workspace-isolation! driver database test-workspace)+ (catch Exception e+ (throw (ex-info (format "Failed to initialize workspace isolation (CREATE DATABASE/USER): %s"+ (ex-message e))+ {:step :init} e))))+ workspace-with-details (merge test-workspace init-result)]+ (when test-table+ (try+ (driver/grant-workspace-read-access! driver database workspace-with-details [test-table])+ (catch Exception e+ (throw (ex-info (format "Failed to grant read access to table %s.%s: %s"+ (:schema test-table) (:name test-table) (ex-message e))+ {:step :grant :table test-table} e)))))+ (try+ (driver/destroy-workspace-isolation! driver database workspace-with-details)+ (catch Exception e+ (throw (ex-info (format "Failed to destroy workspace isolation (DROP DATABASE/USER): %s"+ (ex-message e))+ {:step :destroy} e))))+ nil)+ (catch Exception e+ ;; On failure, attempt cleanup+ (try+ (driver/destroy-workspace-isolation! driver database+ (merge test-workspace+ {:schema (driver.u/workspace-isolation-namespace-name test-workspace)+ :database_details {:user (driver.u/workspace-isolation-user-name test-workspace)}}))+ (catch Exception _cleanup-error+ nil))+ (ex-message e)))]+ result)))))+(defmethod driver/llm-sql-dialect-resource :mysql [_]"llm/prompts/dialects/mysql.md")diff --git a/src/metabase/driver/postgres.clj b/src/metabase/driver/postgres.cljindex 1d469338487..b283bed12cd 100644--- a/src/metabase/driver/postgres.clj+++ b/src/metabase/driver/postgres.clj@@ -28,6 +28,7 @@[metabase.driver.sql.query-processor :as sql.qp][metabase.driver.sql.query-processor.util :as sql.qp.u][metabase.driver.sql.util :as sql.u]+ [metabase.driver.util :as driver.u][metabase.util :as u][metabase.util.date-2 :as u.date][metabase.util.honey-sql-2 :as h2x]@@ -42,6 +43,7 @@ConnectionResultSetResultSetMetaData+ StatementTypes)(java.time LocalDateTime OffsetDateTime OffsetTime)(org.apache.commons.codec.binary Hex)@@ -89,7 +91,8 @@:transforms/table true:transforms/python true:transforms/index-ddl true- :metadata/table-existence-check true}]+ :metadata/table-existence-check true+ :workspace true}](defmethod driver/database-supports? [:postgres feature] [_driver _feature _db] supported?))(defmethod driver/database-supports? [:postgres :nested-field-columns]@@ -1313,5 +1316,76 @@value (nippy/thaw-from-in! data-input)](doto (PGobject.) (.setType type) (.setValue value))))+;;; +----------------------------------------------------------------------------------------------------------------++;;; | Workspace Isolation |+;;; +----------------------------------------------------------------------------------------------------------------+++(defn- user-exists?+ "Check if a PostgreSQL user exists. Uses pg_user which also works in Redshift."+ [conn username]+ (seq (jdbc/query conn ["SELECT 1 FROM pg_user WHERE usename = ?" username])))++(defmethod driver/init-workspace-isolation! :postgres+ [_driver database workspace]+ (let [schema-name (driver.u/workspace-isolation-namespace-name workspace)+ read-user {:user (driver.u/workspace-isolation-user-name workspace)+ :password (driver.u/random-workspace-password)}]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ ;; Create user if not exists, otherwise update password+ ;; PostgreSQL doesn't support CREATE USER IF NOT EXISTS, so we need to check first+ (let [user-sql (if (user-exists? t-conn (:user read-user))+ (format "ALTER USER \"%s\" WITH PASSWORD '%s'" (:user read-user) (:password read-user))+ (format "CREATE USER \"%s\" WITH PASSWORD '%s'" (:user read-user) (:password read-user)))]+ (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql [;; PostgreSQL supports IF NOT EXISTS for schemas+ (format "CREATE SCHEMA IF NOT EXISTS \"%s\"" schema-name)+ user-sql+ ;; grant schema access (CREATE to create tables, USAGE to access them)+ ;; GRANT is idempotent in PostgreSQL+ (format "GRANT ALL PRIVILEGES ON SCHEMA \"%s\" TO \"%s\"" schema-name (:user read-user))+ ;; grant all privileges on future tables created in this schema (by admin)+ (format "ALTER DEFAULT PRIVILEGES IN SCHEMA \"%s\" GRANT ALL ON TABLES TO \"%s\"" schema-name (:user read-user))+ ;; grant role membership to admin so DROP OWNED BY works during cleanup+ (format "GRANT \"%s\" TO CURRENT_USER" (:user read-user))]]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt))))+ {:schema schema-name+ :database_details read-user}))++(defmethod driver/destroy-workspace-isolation! :postgres+ [_driver database workspace]+ (let [schema-name (:schema workspace)+ username (-> workspace :database_details :user)]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql (cond-> [(format "DROP SCHEMA IF EXISTS \"%s\" CASCADE" schema-name)]+ (user-exists? t-conn username)+ (into [(format "DROP OWNED BY \"%s\"" username)+ (format "DROP USER IF EXISTS \"%s\"" username)]))]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt)))))++(defmethod driver/grant-workspace-read-access! :postgres+ [_driver database workspace tables]+ (let [username (-> workspace :database_details :user)+ ;; Collect all unique source schemas that contain the tables we need to grant access to+ source-schemas (into #{} (keep :schema) tables)+ ;; Grant USAGE on source schemas, then SELECT on each table+ ;; Note: workspace schema already has ALL PRIVILEGES from init, so no need to grant USAGE there+ sqls (concat+ ;; USAGE on each source schema containing tables we're granting access to+ (for [s source-schemas]+ (format "GRANT USAGE ON SCHEMA \"%s\" TO \"%s\"" s username))+ ;; SELECT on each table+ (for [{s :schema, t :name} tables]+ (if (str/blank? s)+ (format "GRANT SELECT ON TABLE \"%s\" TO \"%s\"" t username)+ (format "GRANT SELECT ON TABLE \"%s\".\"%s\" TO \"%s\"" s t username))))]+ (jdbc/with-db-transaction [t-conn (sql-jdbc.conn/db->pooled-connection-spec (:id database))]+ (with-open [^Statement stmt (.createStatement ^Connection (:connection t-conn))]+ (doseq [sql sqls]+ (.addBatch ^Statement stmt ^String sql))+ (.executeBatch ^Statement stmt)))))+(defmethod driver/llm-sql-dialect-resource :postgres [_]"llm/prompts/dialects/postgresql.md")diff --git a/src/metabase/driver/sql.clj b/src/metabase/driver/sql.cljindex cbdadfb2d55..a962c4a1d29 100644--- a/src/metabase/driver/sql.clj+++ b/src/metabase/driver/sql.clj@@ -197,6 +197,7 @@:name (name output-table)})(driver/compile-insert driver transform-details)(driver/compile-transform driver transform-details))]+ (log/tracef "Executing incremental transform queries: %s" (pr-str queries)){:rows-affected (last (driver/execute-raw-queries! driver conn-spec [queries]))}))(defn qualified-name@@ -247,19 +248,29 @@{:table (sql.normalize/normalize-name driver table):schema (some->> schema (sql.normalize/normalize-name driver))})+(defn- parsed-table-refs+ "Parse a native query and return a sequence of normalized table specs {:table ... :schema ...}."+ [driver query]+ (-> query+ driver-api/raw-native-query+ (driver.u/parsed-query driver)+ (macaw/query->components {:strip-contexts? true})+ :tables+ (->> (map :component)+ (map #(normalize-table-spec driver %)))))++(mu/defmethod driver/native-query-table-refs :sql :- ::driver/native-query-table-refs+ [driver :- :keyword+ query :- :metabase.lib.schema/native-only-query]+ (into #{} (parsed-table-refs driver query)))+(mu/defmethod driver/native-query-deps :sql :- ::driver/native-query-deps[driver :- :keywordquery :- :metabase.lib.schema/native-only-query]- (let [db-tables (driver-api/tables query)+ (let [db-tables (driver-api/tables query)db-transforms (driver-api/transforms query)]- (-> query- driver-api/raw-native-query- (driver.u/parsed-query driver)- (macaw/query->components {:strip-contexts? true})- :tables- (->> (map :component))- (->> (into #{} (keep #(->> (normalize-table-spec driver %)- (find-table-or-transform driver db-tables db-transforms))))))))+ (into #{} (keep #(find-table-or-transform driver db-tables db-transforms %)+ (parsed-table-refs driver query)))));;; +----------------------------------------------------------------------------------------------------------------+;;; | Dependencies |diff --git a/src/metabase/driver/sql_jdbc.clj b/src/metabase/driver/sql_jdbc.cljindex ac8346e3cf8..3ee6341f940 100644--- a/src/metabase/driver/sql_jdbc.clj+++ b/src/metabase/driver/sql_jdbc.clj@@ -375,3 +375,46 @@(->> (.getMetaData conn)sql-jdbc.describe-database/all-schemas(m/find-first #(= % schema))))))++;;; +----------------------------------------------------------------------------------------------------------------++;;; | Workspace Isolation |+;;; +----------------------------------------------------------------------------------------------------------------+++(def ^:private perm-check-workspace-id "00000000-0000-0000-0000-000000000000")++(defmethod driver/check-isolation-permissions :sql-jdbc+ [driver database test-table]+ (let [test-workspace {:id perm-check-workspace-id+ :name "_mb_perm_check_"}]+ (sql-jdbc.execute/do-with-connection-with-options+ driver+ database+ {:write? true}+ (fn [^Connection conn]+ (.setAutoCommit conn false)+ (try+ (let [init-result (try+ (driver/init-workspace-isolation! driver database test-workspace)+ (catch Exception e+ (throw (ex-info (format "Failed to initialize workspace isolation (CREATE SCHEMA/USER): %s"+ (ex-message e))+ {:step :init} e))))+ workspace-with-details (merge test-workspace init-result)]+ (when test-table+ (try+ (driver/grant-workspace-read-access! driver database workspace-with-details [test-table])+ (catch Exception e+ (throw (ex-info (format "Failed to grant read access to table %s.%s: %s"+ (:schema test-table) (:name test-table) (ex-message e))+ {:step :grant :table test-table} e)))))+ (try+ (driver/destroy-workspace-isolation! driver database workspace-with-details)+ (catch Exception e+ (throw (ex-info (format "Failed to destroy workspace isolation (DROP SCHEMA/USER): %s"+ (ex-message e))+ {:step :destroy} e)))))+ nil+ (catch Exception e+ (ex-message e))+ (finally+ (.rollback conn)))))))diff --git a/src/metabase/driver/sql_jdbc/connection.clj b/src/metabase/driver/sql_jdbc/connection.cljindex 633de7e691c..27a2432f9a4 100644--- a/src/metabase/driver/sql_jdbc/connection.clj+++ b/src/metabase/driver/sql_jdbc/connection.clj@@ -18,7 +18,9 @@^{:clj-kondo/ignore [:discouraged-namespace]}[toucan2.core :as t2])(:import+ (com.google.common.cache Cache CacheBuilder RemovalNotification)(com.mchange.v2.c3p0 DataSources)+ (java.util.concurrent TimeUnit)(javax.sql DataSource)(org.apache.logging.log4j Level)))@@ -208,11 +210,35 @@(driver-api/destroy-connection-pool! pool-spec)(ssh/close-tunnel! pool-spec))-(defonce ^:private ^{:doc "A map of our currently open connection pools, keyed by Database `:id`."}+(def ^:private swapped-pool-ttl-minutes+ "Time-to-live in minutes for swapped connection pools.+ Pools are evicted after this duration of inactivity (no access)."+ 15)++(defonce ^:private ^{:doc "A map of our currently open connection pools for canonical (non-swapped) connections,+ keyed by database-id. Each database has at most one canonical pool."}database-id->connection-pool(atom {}))-(defonce ^:private ^{:doc "A map of DB details hash values, keyed by Database `:id`."}+(defonce ^:private ^Cache ^{:doc "A Guava cache of swapped connection pools with TTL-based expiration,+ keyed by [database-id, details-hash]. Pools are evicted after inactivity. Different swapped+ details for the same database get separate pools, while identical details share a pool."}+ swapped-connection-pools+ (.. (CacheBuilder/newBuilder)+ (expireAfterAccess swapped-pool-ttl-minutes TimeUnit/MINUTES)+ (removalListener+ (reify com.google.common.cache.RemovalListener+ (^void onRemoval [_ ^RemovalNotification rn]+ (let [[database-id _details-hash] (.getKey rn)+ pool-spec (.getValue rn)]+ (log/debugf "Evicting swapped pool for database %d (cause: %s)"+ database-id (.getCause rn))+ (destroy-pool! database-id pool-spec)+ nil))))+ (build)))++(defonce ^:private ^{:doc "A map of DB details hash values for the canonical (non-swapped) details, keyed by Database `:id`.+ This is used to detect when database details have been updated in the application database."}database-id->jdbc-spec-hash(atom {}))@@ -223,28 +249,44 @@(when (some? database)(hash (connection-details->spec driver details))))-(defn- set-pool!- "Atomically update the current connection pool for Database `database` with `database-id`. Use this function instead- of modifying database-id->connection-pool` directly because it properly closes down old pools in a thread-safe way,- ensuring no more than one pool is ever open for a single database. Also modifies the [[database-id->jdbc-spec-hash]]- map with the hash value of the given DB's details map."- [database-id pool-spec-or-nil database]- {:pre [(integer? database-id)]}- (let [[old-id->pool] (if pool-spec-or-nil+(defn- set-canonical-pool!+ "Atomically update the canonical connection pool for a database.++ Use this function instead of modifying connection pool atoms directly because it properly closes down old pools in a+ thread-safe way, ensuring no more than one canonical pool is ever open for a specific database.++ This function is only for canonical (non-swapped) pools. Swapped pools are managed by the Guava cache."+ [database-id details-hash pool-spec-or-nil]+ {:pre [(integer? database-id) (some? details-hash)]}+ (let [[old-pool-map] (if pool-spec-or-nil(swap-vals! database-id->connection-pool assoc database-id pool-spec-or-nil)(swap-vals! database-id->connection-pool dissoc database-id))];; if we replaced a different pool with the new pool that is different from the old one, destroy the old pool- (when-let [old-pool-spec (get old-id->pool database-id)]+ (when-let [old-pool-spec (get old-pool-map database-id)](when-not (identical? old-pool-spec pool-spec-or-nil)(destroy-pool! database-id old-pool-spec))))- ;; update the db details hash cache with the new hash value- (swap! database-id->jdbc-spec-hash assoc database-id (jdbc-spec-hash database))+ ;; Update canonical hash cache+ (swap! database-id->jdbc-spec-hash assoc database-id details-hash)nil)(defn invalidate-pool-for-db!- "Invalidates the connection pool for the given database by closing it and removing it from the cache."+ "Invalidates all connection pools for the given database (canonical and swapped) by closing them and removing from cache."[database]- (set-pool! (u/the-id database) nil nil))+ (let [db-id (u/the-id database)+ has-canonical? (contains? @database-id->connection-pool db-id)+ ;; Find all swapped pool keys for this database (keys are [db-id, details-hash] tuples)+ swapped-keys (filter (fn [[cached-db-id _details-hash]]+ (= cached-db-id db-id))+ (keys (.asMap ^Cache swapped-connection-pools)))]+ (log/debugf "Invalidating connection pools for database %d (canonical: %s, swapped count: %d)"+ db-id has-canonical? (count swapped-keys))+ ;; Clear canonical pool+ (when-let [pool-spec (get @database-id->connection-pool db-id)]+ (destroy-pool! db-id pool-spec)+ (swap! database-id->connection-pool dissoc db-id))+ ;; Clear all swapped pools for this DB (removal listener will call destroy-pool!)+ (doseq [cache-key swapped-keys]+ (.invalidate ^Cache swapped-connection-pools cache-key))))(defn- log-ssh-tunnel-reconnect-msg! [db-id](log/warn (u/format-color :red "ssh tunnel for database %s looks closed; marking pool invalid to reopen it" db-id))@@ -258,76 +300,168 @@(log/warn (u/format-color :yellow "Password of database %s expired; marking pool invalid to reopen it" db-id))nil)+(defn- pool-invalidation-reason+ "Returns a keyword describing why a pool is invalid, or nil if valid.+ Possible reasons: :password-expired, :tunnel-closed."+ [pool-spec]+ (let [{:keys [password-expiry-timestamp tunnel-session]} pool-spec]+ (cond+ (and (int? password-expiry-timestamp)+ (<= password-expiry-timestamp (System/currentTimeMillis)))+ :password-expired++ (nil? tunnel-session)+ nil ;; no tunnel, valid++ (ssh/ssh-tunnel-open? pool-spec)+ nil ;; tunnel open, valid++ :else+ :tunnel-closed)))++(defn- log-pool-invalidation!+ "Log a warning about why a pool is being invalidated."+ [database-id reason]+ (case reason+ :password-expired (log-password-expiry! database-id)+ :tunnel-closed (log-ssh-tunnel-reconnect-msg! database-id)+ :hash-changed (log-jdbc-spec-hash-change-msg! database-id)+ nil))++(defn- get-pool-if-valid+ "Returns the pool-spec if it's valid, nil otherwise.+ Optionally logs the invalidation reason."+ [pool-spec database-id log-invalidation?]+ (if-let [reason (pool-invalidation-reason pool-spec)]+ (do+ (when log-invalidation?+ (log-pool-invalidation! database-id reason))+ nil)+ pool-spec))++(defn- swap-cache-key+ "Generate cache key for swapped pools: [database-id, jdbc-spec-hash].+ Different details (e.g., different user credentials) will produce different keys,+ ensuring each unique swap gets its own connection pool.+ Uses jdbc-spec-hash for stable hashing based on the actual connection spec."+ [{:keys [id] :as db}]+ [id (jdbc-spec-hash db)])++(defn- get-swapped-pool+ "Get or create a swapped connection pool from the Guava cache.+ Validates that existing pools are still valid (password not expired, tunnel open).+ Pools are keyed by [database-id, details-hash] so different swapped details get separate pools.+ The `db` should already have swapped details applied."+ [{:keys [id] :as db}]+ (let [cache-key (swap-cache-key db)+ existing (.getIfPresent ^Cache swapped-connection-pools cache-key)]+ (cond+ ;; No existing pool - create a new one+ (nil? existing)+ (.get ^Cache swapped-connection-pools+ cache-key+ (reify java.util.concurrent.Callable+ (call [_]+ (log/debugf "Creating new swapped connection pool for database %d with spec hash %d"+ id (second cache-key))+ (create-pool! db))))++ ;; Existing pool is invalid (password expired, tunnel closed) - invalidate and recreate+ (nil? (get-pool-if-valid existing id true))+ (do+ (log/debugf "Swapped connection pool for database %d is invalid, recreating" id)+ (.invalidate ^Cache swapped-connection-pools cache-key)+ (recur db))++ ;; Existing pool is valid - use it+ :else+ (do+ (log/debugf "Using existing swapped connection pool for database %d" id)+ existing))))++(defn- canonical-pool-hash-changed?+ "Check if the canonical pool's hash differs from the expected hash.+ Handles stale DatabaseInstance by re-fetching from app DB."+ [database-id expected-hash]+ (let [curr-hash (get @database-id->jdbc-spec-hash database-id)]+ (when (and (some? curr-hash) (not= curr-hash expected-hash))+ ;; the hash didn't match, but it's possible that a stale instance of `DatabaseInstance`+ ;; was passed in (ex: from a long-running sync operation); fetch the latest one from+ ;; our app DB, and see if it STILL doesn't match+ (not= curr-hash (-> (t2/select-one [:model/Database :id :engine :details] :id database-id)+ jdbc-spec-hash)))))++(defn- get-canonical-pool+ "Get a canonical pool if it exists and is valid, otherwise return nil."+ [database-id details-hash log-invalidation?]+ (let [pool-spec (get @database-id->connection-pool database-id ::not-found)]+ (cond+ (= ::not-found pool-spec)+ nil++ ;; Check if the hash has changed (details were updated in DB)+ (canonical-pool-hash-changed? database-id details-hash)+ (do+ (when log-invalidation?+ (log-pool-invalidation! database-id :hash-changed))+ nil)++ ;; Check pool validity (password expiry, tunnel status)+ :else+ (get-pool-if-valid pool-spec database-id log-invalidation?))))+(defn db->pooled-connection-spec"Return a JDBC connection spec that includes a c3p0 `ComboPooledDataSource`. These connection pools are cached so we- don't create multiple ones for the same DB."+ don't create multiple ones for the same DB.++ When [[metabase.driver/with-swapped-connection-details]] is active for a database, the database details are+ modified before creating the connection pool. Swapped pools are stored in a separate Guava cache with TTL-based+ eviction (see [[swapped-pool-ttl-minutes]]), while canonical pools are stored in the main atom cache."[db-or-id-or-spec](when-let [db-id (u/id db-or-id-or-spec)](driver-api/check-allowed-access! db-id))(cond;; db-or-id-or-spec is a Database instance or an integer ID(u/id db-or-id-or-spec)- (let [database-id (u/the-id db-or-id-or-spec)- ;; we need the Database instance no matter what (in order to compare details hash with cached value)- db (or (when (driver-api/instance-of? :model/Database db-or-id-or-spec)- (driver-api/instance->metadata db-or-id-or-spec :metadata/database))- (when (= (:lib/type db-or-id-or-spec) :metadata/database)- db-or-id-or-spec)- (driver-api/with-metadata-provider database-id- (driver-api/database (driver-api/metadata-provider))))- get-fn (fn [db-id log-invalidation?]- (let [details (get @database-id->connection-pool db-id ::not-found)]- (cond- ;; for the audit db, we pass the datasource for the app-db. This lets us use fewer db- ;; connections with *application-db* and 1 less connection pool. Note: This data-source is- ;; not in [[database-id->connection-pool]].- (or (:is-audit db) (get-in db [:details :is-audit-dev]))- {:datasource (driver-api/data-source)}-- (= ::not-found details)- nil-- ;; details hash changed from what is cached; invalid- (let [curr-hash (get @database-id->jdbc-spec-hash db-id)- new-hash (jdbc-spec-hash db)]- (when (and (some? curr-hash) (not= curr-hash new-hash))- ;; the hash didn't match, but it's possible that a stale instance of `DatabaseInstance`- ;; was passed in (ex: from a long-running sync operation); fetch the latest one from- ;; our app DB, and see if it STILL doesn't match- (not= curr-hash (-> (t2/select-one [:model/Database :id :engine :details] :id database-id)- jdbc-spec-hash))))- (when log-invalidation?- (log-jdbc-spec-hash-change-msg! db-id))-- (let [{:keys [password-expiry-timestamp]} details]- (and (int? password-expiry-timestamp)- (<= password-expiry-timestamp (System/currentTimeMillis))))- (when log-invalidation?- (log-password-expiry! db-id))-- (nil? (:tunnel-session details)) ; no tunnel in use; valid- details-- (ssh/ssh-tunnel-open? details) ; tunnel in use, and open; valid- details-- :else ; tunnel in use, and not open; invalid- (when log-invalidation?- (log-ssh-tunnel-reconnect-msg! db-id)))))]- (or- ;; we have an existing pool for this database, so use it- (get-fn database-id true)- ;; Even tho `set-pool!` will properly shut down old pools if two threads call this method at the same time, we- ;; don't want to end up with a bunch of simultaneous threads creating pools only to have them destroyed the- ;; very next instant. This will cause their queries to fail. Thus we should do the usual locking here and make- ;; sure only one thread will be creating a pool at a given instant.- (locking database-id->connection-pool- (or- ;; check if another thread created the pool while we were waiting to acquire the lock- (get-fn database-id false)- ;; create a new pool and add it to our cache, then return it- (u/prog1 (create-pool! db)- (set-pool! database-id <> db))))))+ (let [database-id (u/the-id db-or-id-or-spec)+ ;; we need the Database instance no matter what (in order to calculate details hash)+ db-original (or (when (driver-api/instance-of? :model/Database db-or-id-or-spec)+ (driver-api/instance->metadata db-or-id-or-spec :metadata/database))+ (when (= (:lib/type db-or-id-or-spec) :metadata/database)+ db-or-id-or-spec)+ (driver-api/with-metadata-provider database-id+ (driver-api/database (driver-api/metadata-provider))))+ ;; Apply connection detail swaps if present+ has-swap? (driver/has-connection-swap? database-id)+ db (update db-original :details #(driver/maybe-swap-details database-id %))+ ;; Calculate hash from final (possibly swapped) details+ details-hash (jdbc-spec-hash db)]+ (cond+ ;; for the audit db, we pass the datasource for the app-db. This lets us use fewer db+ ;; connections with *application-db* and 1 less connection pool. Note: This data-source is+ ;; not in [[database-id->connection-pool]].+ (or (:is-audit db) (get-in db [:details :is-audit-dev]))+ {:datasource (driver-api/data-source)}++ ;; Swapped pool: use Guava cache with TTL+ has-swap?+ (get-swapped-pool db)++ ;; Canonical pool: use atom cache+ :else+ (or+ ;; we have an existing valid pool for this database, so use it+ (get-canonical-pool database-id details-hash true)+ ;; We don't want to end up with a bunch of simultaneous threads creating pools only to have them destroyed+ ;; the very next instant. This will cause their queries to fail. Thus we should do the usual locking here+ ;; and make sure only one thread will be creating a pool at a given instant.+ (locking database-id->connection-pool+ (or+ ;; check if another thread created the pool while we were waiting to acquire the lock+ (get-canonical-pool database-id details-hash false)+ ;; create a new pool and add it to our cache, then return it+ (u/prog1 (create-pool! db)+ (set-canonical-pool! database-id details-hash <>)))))));; already a `clojure.java.jdbc` spec map(map? db-or-id-or-spec)diff --git a/src/metabase/driver/util.clj b/src/metabase/driver/util.cljindex ead1c976c5d..58f29f7de2e 100644--- a/src/metabase/driver/util.clj+++ b/src/metabase/driver/util.clj@@ -19,6 +19,7 @@[metabase.premium-features.core :as premium-features][metabase.query-processor.error-type :as qp.error-type]^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store]+ [metabase.system.core :as system][metabase.util :as u][metabase.util.i18n :refer [deferred-tru trs]][metabase.util.log :as log]@@ -745,6 +746,55 @@db-details))db-details)))+;;; +----------------------------------------------------------------------------------------------------------------++;;; | Workspace Isolation Utilities |+;;; +----------------------------------------------------------------------------------------------------------------+++(defn- instance-uuid-slug+ "Create a slug from the site UUID, taking the first character of each section."+ [site-uuid-string]+ (->> (str/split site-uuid-string #"-")+ (map first)+ (apply str)))++;; WARNING: Changing this prefix requires backwards compatibility handling for existing workspaces.+;; The prefix is used to identify isolation namespaces in the database, and existing workspaces+;; will have namespaces created with the current prefix.+(def ^:private workspace-isolated-prefix "mb__isolation")++(defn workspace-isolation-namespace-name+ "Generate namespace/database name for workspace isolation following mb__isolation_<slug>_<workspace-id> pattern.+ Uses 'namespace' as the generic term that maps to 'schema' in Postgres, 'database' in ClickHouse, etc."+ [workspace]+ (assert (some? (:id workspace)) "Workspace must have an :id")+ (let [instance-slug (instance-uuid-slug (str (system/site-uuid)))+ clean-workspace-id (str/replace (str (:id workspace)) #"[^a-zA-Z0-9]" "_")]+ (format "%s_%s_%s" workspace-isolated-prefix instance-slug clean-workspace-id)))++(defn workspace-isolation-user-name+ "Generate username for workspace isolation."+ [workspace]+ (let [instance-slug (instance-uuid-slug (str (system/site-uuid)))]+ (format "%s_%s_%s" workspace-isolated-prefix instance-slug (:id workspace))))++(def ^:private workspace-password-char-sets+ "Character sets for password generation. Cycles through these to ensure representation from each."+ ["ABCDEFGHJKLMNPQRSTUVWXYZ"+ "abcdefghjkmnpqrstuvwxyz"+ "123456789"+ "!#$%&*+-="])++(defn random-workspace-password+ "Generate a random password suitable for most database engines.+ Ensures the password contains characters from all sets (uppercase, lowercase, digits, special)+ by cycling through the character sets. Result is shuffled for randomness."+ []+ (->> (cycle workspace-password-char-sets)+ (take (+ 32 (rand-int 32)))+ (map rand-nth)+ shuffle+ (apply str)))+;;; +----------------------------------------------------------------------------------------------------------------+;;; | Macaw parsing helpers |;;; +----------------------------------------------------------------------------------------------------------------+diff --git a/src/metabase/models/interface.clj b/src/metabase/models/interface.cljindex 9699b7ab125..16cc261031c 100644--- a/src/metabase/models/interface.clj+++ b/src/metabase/models/interface.clj@@ -246,6 +246,11 @@{:in u/qualified-name:out keyword})+(def transform-trim+ "Transform that trims whitespace on input and output. Useful for fixed-width char columns that pad with spaces."+ {:in str/trim+ :out str/trim})+(def transform-json-no-keywordization"Transform for json-no-keywordization"{:in json-indiff --git a/src/metabase/models/resolution.clj b/src/metabase/models/resolution.cljindex 754d86f2b82..170de0c8a25 100644--- a/src/metabase/models/resolution.clj+++ b/src/metabase/models/resolution.clj@@ -113,7 +113,17 @@:model/User metabase.users.models.user:model/UserKeyValue metabase.user-key-value.models.user-key-value:model/UserParameterValue metabase.users.models.user-parameter-value- :model/ViewLog metabase.view-log.models.view-log})+ :model/ViewLog metabase.view-log.models.view-log+ :model/Workspace metabase-enterprise.workspaces.models.workspace+ :model/WorkspaceInput metabase-enterprise.workspaces.models.workspace-input+ :model/WorkspaceInputExternal metabase-enterprise.workspaces.models.workspace-input-external+ :model/WorkspaceLog metabase-enterprise.workspaces.models.workspace-log+ :model/WorkspaceMerge metabase-enterprise.workspaces.models.workspace-merge+ :model/WorkspaceMergeTransform metabase-enterprise.workspaces.models.workspace-merge-transform+ :model/WorkspaceOutput metabase-enterprise.workspaces.models.workspace-output+ :model/WorkspaceOutputExternal metabase-enterprise.workspaces.models.workspace-output-external+ :model/WorkspaceGraph metabase-enterprise.workspaces.models.workspace-graph+ :model/WorkspaceTransform metabase-enterprise.workspaces.models.workspace-transform});;; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!;;; !! !!diff --git a/src/metabase/permissions/models/permissions.clj b/src/metabase/permissions/models/permissions.cljindex 554363ecb97..67f3fcf8a5f 100644--- a/src/metabase/permissions/models/permissions.clj+++ b/src/metabase/permissions/models/permissions.clj@@ -89,7 +89,7 @@User would otherwise have. See the `Determining query permissions` section below for more details. As withsegmented permissions, block anti-permissions are only available in Metabase® Enterprise Edition™.- * _Application permisisons_ -- are per-Group permissions that give non-admin users access to features like:+ * _Application permissions_ -- are per-Group permissions that give non-admin users access to features like:change instance's Settings; access Audit, Tools, Troubleshooting ...### Determining CRUD permissions in the REST APIdiff --git a/src/metabase/premium_features/core.clj b/src/metabase/premium_features/core.cljindex 32c184ac5da..20a9376b24b 100644--- a/src/metabase/premium_features/core.clj+++ b/src/metabase/premium_features/core.clj@@ -82,6 +82,7 @@enable-python-transforms?enable-upload-management?enable-whitelabeling?+ enable-workspaces?has-attached-dwh?hide-embed-branding?is-hosted?diff --git a/src/metabase/premium_features/settings.clj b/src/metabase/premium_features/settings.cljindex 7bf18b6a973..2db659149ad 100644--- a/src/metabase/premium_features/settings.clj+++ b/src/metabase/premium_features/settings.clj@@ -316,6 +316,10 @@"Should the multi-tenant feature be enabled?":tenants)+(define-premium-feature ^{:added "0.59.0"} enable-workspaces?+ "Should we allow users to use workspaces?"+ :workspaces)+(defn- -token-features []{:advanced_permissions (enable-advanced-permissions?):ai_sql_fixer (enable-ai-sql-fixer?)@@ -367,7 +371,8 @@:transforms (enable-transforms?):transforms-python (enable-python-transforms?):upload_management (enable-upload-management?)- :whitelabel (enable-whitelabeling?)})+ :whitelabel (enable-whitelabeling?)+ :workspaces (enable-workspaces?)})(defsetting token-features"Features registered for this instance's token"diff --git a/src/metabase/settings/core.clj b/src/metabase/settings/core.cljindex d459ea9168d..e8a0a378076 100644--- a/src/metabase/settings/core.clj+++ b/src/metabase/settings/core.clj@@ -91,6 +91,7 @@can-read-setting?current-user-readable-visibilitiescustom-disabled-reasons!+ default-valuedefsettingdisabled-for-db-reasonsenv-var-valuediff --git a/src/metabase/util.cljc b/src/metabase/util.cljcindex 2200960d4d7..44ea510bd9b 100644--- a/src/metabase/util.cljc+++ b/src/metabase/util.cljc@@ -130,6 +130,11 @@(zero? (count v)) nil:else (nth v (dec (count v)))))+(defn keepv+ "Like `keep`, but returns a vector."+ [f coll]+ (into [] (keep f) coll))+(defmacro prog1"Execute `first-form`, then any other expressions in `body`, presumably for side-effects; return the result of`first-form`.diff --git a/src/metabase/util/malli/schema.clj b/src/metabase/util/malli/schema.cljindex af73a05340b..99ea6882103 100644--- a/src/metabase/util/malli/schema.clj+++ b/src/metabase/util/malli/schema.clj@@ -155,6 +155,10 @@(str message)):api/regex #"-[1-9]\d*"}]))+(def LocalizedString+ "Schema that is a localized string."+ [:fn i18n/localized-string?])+(def KeywordOrString"Schema for something that can be either a `Keyword` or a `String`."(mu/with-api-error-message@@ -238,6 +242,14 @@[:fn {:error/message "valid password that is not too common"} (every-pred string? #'u.password/is-valid?)]](deferred-tru "password is too common.")))+(def TemporalInstant+ "Schema for temporal values (java.time objects) that serialize to ISO-8601 strings in JSON responses."+ (mu/with-api-error-message+ [:fn {:json-schema {:type "string" :format "date-time"}+ :description "ISO-8601 date-time string"}+ #(instance? java.time.temporal.Temporal %)]+ (deferred-tru "value must be a valid date/time/datetime")))+(def TemporalString"Schema for a string that can be parsed by date2/parse."(mu/with-api-error-messagediff --git a/src/metabase/warehouses/models/database.clj b/src/metabase/warehouses/models/database.cljindex 97779a849f4..19e12b3ac5c 100644--- a/src/metabase/warehouses/models/database.clj+++ b/src/metabase/warehouses/models/database.clj@@ -49,13 +49,14 @@(map secret/clean-secret-properties-from-database)))(t2/deftransforms :model/Database- {:details mi/transform-encrypted-json- :engine mi/transform-keyword- :metadata_sync_schedule mi/transform-cron-string- :cache_field_values_schedule mi/transform-cron-string- :start_of_week mi/transform-keyword- :settings mi/transform-encrypted-json- :dbms_version mi/transform-json})+ {:details mi/transform-encrypted-json+ :engine mi/transform-keyword+ :metadata_sync_schedule mi/transform-cron-string+ :cache_field_values_schedule mi/transform-cron-string+ :start_of_week mi/transform-keyword+ :settings mi/transform-encrypted-json+ :dbms_version mi/transform-json+ :workspace_permissions_status mi/transform-json})(methodical/defmethod t2/model-for-automagic-hydration [:default :database] [_model _k] :model/Database)(methodical/defmethod t2/model-for-automagic-hydration [:default :db] [_model _k] :model/Database)@@ -588,6 +589,31 @@(log/debug "Redacting non-user-readable database settings during json encoding.")))))))json-generator))+;;; ------------------------------------------ Workspace Permissions Cache --------------------------------------------++(defn check-workspace-permissions+ "Check isolation permissions for a database. Returns the permission check result:+ {:status \"ok\", :checked_at ...} or {:status \"failed\", :error \"...\", :checked_at ...}+ Does NOT update the database - use [[check-and-cache-workspace-permissions!]] if you need to persist."+ [database]+ (let [checked-at (java.time.Instant/now)]+ (try+ (let [db-driver (driver.u/database->driver database)]+ (if-let [error (driver/check-isolation-permissions db-driver database nil)]+ {:status "failed", :error error, :checked_at (str checked-at)}+ {:status "ok", :checked_at (str checked-at)}))+ (catch Exception e+ {:status "failed", :error (ex-message e), :checked_at (str checked-at)}))))++(defn check-and-cache-workspace-permissions!+ "Check isolation permissions for a database and cache the result in workspace_permissions_status column.+ Returns the permission check result: {:status \"ok\", :checked_at ...}+ or {:status \"failed\", :error \"...\", :checked_at ...}"+ [database]+ (let [result (check-workspace-permissions database)]+ (t2/update! :model/Database (:id database) {:workspace_permissions_status result})+ result))+;;; ------------------------------------------------ Serialization ----------------------------------------------------(defmethod serdes/make-spec "Database"[_model-name {:keys [include-database-secrets]}]@@ -596,7 +622,9 @@:metadata_sync_schedule :name :points_of_interest :provider_name :refingerprint :settings :timezone :uploads_enabled:uploads_schema_name :uploads_table_prefix]:skip [;; deprecated field- :cache_ttl]+ :cache_ttl+ ;; workspace_permissions_status is instance-specific and should not be serialized+ :workspace_permissions_status]:transform {:created_at (serdes/date);; details should be imported if available regardless of options:details {:export-with-contextdiff --git a/src/metabase/warehouses_rest/api.clj b/src/metabase/warehouses_rest/api.cljindex 3beb639884b..f07bf09ad53 100644--- a/src/metabase/warehouses_rest/api.clj+++ b/src/metabase/warehouses_rest/api.clj@@ -1078,10 +1078,11 @@{:status 400:body conn-error};; no error, proceed with update- (let [pending-settings (into {}+ (let [existing-settings (:settings existing-database)+ pending-settings (into {};; upsert settings with a PATCH-style update. `nil` key means unset the Setting.(remove (fn [[_k v]] (nil? v)))- (merge (:settings existing-database) settings))+ (merge existing-settings settings))updates (merge;; TODO - is there really a reason to let someone change the engine on an existing database?;; that seems like the kind of thing that will almost never work in any practical way@@ -1110,8 +1111,19 @@(let [driver-supports? (fn [db feature] (driver.u/supports? (driver.u/database->driver db) feature db))];; ensure we're not trying to set anything we should not be able to.;; Note: it's also possible for existing settings to become invalid when changing things like the engine.- (doseq [setting-kw (keys pending-settings)]- (setting/validate-settable-for-db! setting-kw pending-db driver-supports?)))+ ;; We skip validation for: unchanged values and nil values (resetting to default is always allowed).+ (doseq [[setting-kw new-value] settings+ :when (and (some? new-value)+ ;; Allow explicit default value as well (typically this is what FE will actually do)+ ;; Should we translate this into setting it to NULL? That seems too opinionated.+ (not= new-value (try (setting/default-value setting-kw)+ ;; fallback to a redundant nil check+ (catch Exception _)))+ (not= new-value (get existing-settings setting-kw)))]+ (try+ (setting/validate-settable-for-db! setting-kw pending-db driver-supports?)+ (catch Exception e+ (throw (ex-info (ex-message e) (assoc (ex-data e) :status-code 400) e))))))(t2/update! :model/Database id updates);; unlike the other fields, folks might want to nil out cache_ttl. it should also only be settable on EE;; with the advanced-config feature enabled.@@ -1274,6 +1286,24 @@(delete-all-field-values-for-database! db)){:status :ok})+(api.macros/defendpoint :post "/:id/permission/workspace/check"+ :- [:map+ [:status :string]+ [:checked_at :string]+ [:error {:optional true} :string]]+ "Check if database's connection has the required permissions to manage workspaces.+ By default it'll return the cached permission check."+ [{:keys [id cached]} :- [:map [:id ms/PositiveInt]+ [:cached {:optional true+ :default true} :boolean]]]+ (api/check-superuser)+ (let [db (api/check-404 (t2/select-one :model/Database id))+ _ (api/check-400 (driver.u/supports? (:engine db) :workspace db)+ "Database does not support workspaces")]+ (or (when cached+ (t2/select-one-fn :workspace_permissions_status :model/Database id))+ (database/check-and-cache-workspace-permissions! db))))+;;; ------------------------------------------ GET /api/database/:id/schemas -----------------------------------------(defenterprise current-user-can-manage-schema-metadata?@@ -1318,7 +1348,7 @@(defn database-schemas"Returns a list of all the schemas with tables found for the database `id`. Excludes schemas with no tables."- [id {:keys [include-editable-data-model? include-hidden? can-query? can-write-metadata?]}]+ [id {:keys [include-editable-data-model? include-hidden? can-query? can-write-metadata? include-workspace?]}](let [filter-schemas (fn [schemas](if include-editable-data-model?(if-let [f (u/ignore-exceptions@@ -1327,6 +1357,22 @@(map :schema (f (map (fn [s] {:db_id id :schema s}) schemas)))schemas)(filter (partial can-read-schema? id) schemas)))+ clauses (cond-> []+ ;; a non-nil value means Table is hidden --+ ;; see [[metabase.warehouse-schema.models.table/visibility-types]]+ (not include-hidden?) (conj [:= :visibility_type nil])+ (not include-workspace?) (conj [:or+ [:= :schema nil]+ [:not+ ;; TODO (Chris 2025-12-09) -- dislike coupling to a constant, at least until we have an e2e test+ [:like :schema "mb__isolation_%"]+ ;; TODO (Chris 2025-12-09) -- this might behave terribly without an index when there are lots of workspaces+ #_[:exists {:select [1]+ :from [[(t2/table-name :model/Workspace) :w]]+ :where [:and+ [:= :w.database_id id]+ [:= :w.schema :metabase_table.schema]+ [:= :w.archived_at nil]]}]]]));; For can-query? and can-write-metadata?, we need to filter based on tables in each schemafilter-schemas-by-tables (fn [schemas](if (or can-query? can-write-metadata?)@@ -1342,10 +1388,8 @@:db_id id :active true(merge{:order-by [[:%lower.schema :asc]]}- (when-not include-hidden?- ;; a non-nil value means Table is hidden --- ;; see [[metabase.warehouse-schema.models.table/visibility-types]]- {:where [:= :visibility_type nil]})))+ (when clauses+ {:where (into [:and] clauses)})))filter-schemasfilter-schemas-by-tables;; for `nil` schemas return the empty string@@ -1365,15 +1409,25 @@- `can-write-metadata=true` - filter to only schemas containing tables the user can edit metadata for"[{:keys [id]} :- [:map[:id ms/PositiveInt]]- {:keys [include_editable_data_model include_hidden can-query can-write-metadata]} :- [:map- [:include_editable_data_model {:default false} [:maybe ms/BooleanValue]]- [:include_hidden {:default false} [:maybe ms/BooleanValue]]- [:can-query {:optional true} [:maybe :boolean]]- [:can-write-metadata {:optional true} [:maybe :boolean]]]]+ {:keys [include_editable_data_model+ include_hidden+ can-query+ can-write-metadata+ include_workspace]} :- [:map+ [:include_editable_data_model {:default false} [:maybe ms/BooleanValue]]+ [:include_hidden {:default false} [:maybe ms/BooleanValue]]+ [:can-query {:optional true} [:maybe :boolean]]+ [:can-write-metadata {:optional true} [:maybe :boolean]]+ [:include_workspace {:default false} [:maybe ms/BooleanValue]]]](database-schemas id {:include-editable-data-model? include_editable_data_model- :include-hidden? include_hidden+ :include-hidden? include_hidden:can-query? can-query- :can-write-metadata? can-write-metadata}))+ :can-write-metadata? can-write-metadata+ ;; TODO (Chris 2025-12-09) -- filtering out workspace schemas has a weird FE consequence - if you type one of those+ ;; schemas out manually in the targets, it will offer to create it for you.+ ;; this ends up being a no-op, so i guess it's harmless for now?+ ;; it will look very weird when we add validation to refuse saving that target.+ :include-workspace? include_workspace}));; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to;; use our API + we will need it when we make auto-TypeScript-signature generation happendiff --git a/test/metabase/app_db/custom_migrations_test.clj b/test/metabase/app_db/custom_migrations_test.cljindex 6b0dc5c480e..ad11c2c060b 100644--- a/test/metabase/app_db/custom_migrations_test.clj+++ b/test/metabase/app_db/custom_migrations_test.clj@@ -2699,7 +2699,8 @@;; rollback(migrate! :down 56);; assert pre conditions- (assert-pre-conditions))))))+ (testing "everything back to normal after downgrade"+ (assert-pre-conditions)))))))(deftest escape-existing-at-symbol-user-attributes-test(testing "v58.2025-11-18T12:31:49 : rename any existing `@.+` user attrs to add a preceding underscore"@@ -2725,3 +2726,52 @@(json/decode (t2/select-one-fn :login_attributes :core_user :id user-id))))(is (= {"_@foo" "bang"}(json/decode (t2/select-one-fn :login_attributes :core_user :id other-user-id))))))))++(deftest backfill-transform-target-db-id-test+ (testing "v59.2026-01-31T12:01:23 : backfill target_db_id from target and source JSON"+ (impl/test-migrations ["v59.2026-01-31T12:01:23"] [migrate!]+ (let [db-id (:id (new-instance-with-default :metabase_database))+ deleted-db-id (+ db-id 9999)+ query-source (fn [id] (json/encode {:type "query" :query {:database id}}))+ ;; Python transforms were meant to support 0-N source databases, but the table now has a single source id.+ python-db-id db-id+ python-source (json/encode {:type "python" :script "x = 1", :source-database python-db-id})+ target (fn [& {:keys [database]}]+ (json/encode (cond-> {:schema "public" :table "out" :type "append"}+ database (assoc :database database))))+ insert! (fn [source-type source target]+ (t2/insert-returning-pk!+ :transform+ {:name "t"+ :source source+ :target target+ :source_type source-type+ :source_database_id db-id+ :created_at :%now+ :updated_at :%now}))+ ;; source.query.database takes precedence over target.database+ both-id (insert! "mbql" (query-source db-id) (target :database db-id))+ ;; source.query.database used when target has no database+ source-id (insert! "mbql" (query-source db-id) (target))+ ;; target.database used as fallback for python transforms+ target-id (insert! "python" python-source (target :database db-id))+ ;; no database anywhere — stays nil+ none-id (insert! "python" python-source (target))+ ;; deleted database reference — stays nil+ deleted-id (insert! "mbql" (query-source deleted-db-id) (target :database deleted-db-id))+ id->target #(t2/select-one-fn :target_db_id :transform :id %)]+ (testing "Before backfill, the field is uniformly empty"+ ;; Haven't turned this into a loop, as it would obscure which case failed.+ (is (nil? (id->target both-id)))+ (is (nil? (id->target source-id)))+ (is (nil? (id->target target-id)))+ (is (nil? (id->target none-id)))+ (is (nil? (id->target deleted-id))))+ (migrate!)+ (testing "Valid database ids are backfilled"+ ;; Ditto, since the IDs are opaque in CI, give each case its own line for transparent failures.+ (is (= db-id (id->target both-id)))+ (is (= db-id (id->target source-id)))+ (is (= db-id (id->target target-id)))+ (is (nil? (id->target none-id)))+ (is (nil? (id->target deleted-id))))))))diff --git a/test/metabase/cmd/copy_test.clj b/test/metabase/cmd/copy_test.cljindex 0f2317d9d62..7b333a5ef6b 100644--- a/test/metabase/cmd/copy_test.clj+++ b/test/metabase/cmd/copy_test.clj@@ -58,7 +58,17 @@:model/TransformTag:model/TransformTransformTag:model/Undo- :model/UserKeyValue})+ :model/UserKeyValue+ :model/Workspace+ :model/WorkspaceGraph+ :model/WorkspaceInput+ :model/WorkspaceInputExternal+ :model/WorkspaceLog+ :model/WorkspaceMerge+ :model/WorkspaceMergeTransform+ :model/WorkspaceOutput+ :model/WorkspaceOutputExternal+ :model/WorkspaceTransform})(defn- all-model-names [](into (sorted-set)diff --git a/test/metabase/dashboards_rest/api_test.clj b/test/metabase/dashboards_rest/api_test.cljindex 1e7bcc44b3e..a3491800c64 100644--- a/test/metabase/dashboards_rest/api_test.clj+++ b/test/metabase/dashboards_rest/api_test.clj@@ -2915,7 +2915,7 @@(with-chain-filter-fixtures [{:keys [dashboard param-keys]}](let [url (chain-filter-values-url dashboard (:category-name param-keys))](testing (str "\nGET /api/" url "\n")- (testing "\nShow me names of categories that have expensive venues (price = 4), while I lack permisisons."+ (testing "\nShow me names of categories that have expensive venues (price = 4), while I lack permissions."(with-redefs [chain-filter/use-cached-field-values? (constantly false)](binding [qp.perms/*card-id* nil] ;; this situation was observed when running constrained chain filters.(is (= {:values [["African"] ["American"] ["Artisan"] ["Asian"]] :has_more_values false}@@ -2923,7 +2923,7 @@(let [url (chain-filter-values-url dashboard (:category-name param-keys) (:price param-keys) 4)](testing (str "\nGET /api/" url "\n")- (testing "\nShow me names of categories that have expensive venues (price = 4), while I lack permisisons."+ (testing "\nShow me names of categories that have expensive venues (price = 4), while I lack permissions."(with-redefs [chain-filter/use-cached-field-values? (constantly false)](binding [qp.perms/*card-id* nil](is (= {:values [["Japanese"] ["Steakhouse"]], :has_more_values false}diff --git a/test/metabase/driver/h2_test.clj b/test/metabase/driver/h2_test.cljindex 776ca7b943a..4aeb2cb005b 100644--- a/test/metabase/driver/h2_test.clj+++ b/test/metabase/driver/h2_test.clj@@ -111,21 +111,22 @@t/zone-id.normalized)))))-(deftest ^:parallel disallow-admin-accounts-test+(deftest disallow-admin-accounts-test(testing "Check that we're not allowed to run SQL against an H2 database with a non-admin account"(mt/with-temp [:model/Database db {:name "Fake-H2-DB", :engine "h2", :details {:db "mem:fake-h2-db"}}]- (doseq [[query-type query] {"legacy MBQL query"- {:database (:id db)- :type :native- :native {:query "SELECT 1"}}-- "MBQL 5 query"- (lib/native-query (lib-be/application-database-metadata-provider (:id db)) "SELECT 1")}]- (testing query-type- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Running SQL queries against H2 databases using the default \(admin\) database user is forbidden\.$"- (qp/process-query query))))))))+ (with-redefs [config/is-prod? true]+ (doseq [[query-type query] {"legacy MBQL query"+ {:database (:id db)+ :type :native+ :native {:query "SELECT 1"}}++ "MBQL 5 query"+ (lib/native-query (lib-be/application-database-metadata-provider (:id db)) "SELECT 1")}]+ (testing query-type+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Running SQL queries against H2 databases using the default \(admin\) database user is forbidden\.$"+ (qp/process-query query)))))))))(deftest ^:parallel add-interval-honeysql-form-test(testing "Should convert fractional seconds to milliseconds"diff --git a/test/metabase/driver/sql_jdbc/connection_test.clj b/test/metabase/driver/sql_jdbc/connection_test.cljindex 87b34924007..9473a376946 100644--- a/test/metabase/driver/sql_jdbc/connection_test.clj+++ b/test/metabase/driver/sql_jdbc/connection_test.clj@@ -33,6 +33,7 @@[next.jdbc :as next.jdbc][toucan2.core :as t2])(:import+ (com.google.common.cache Cache)(org.h2.tools Server)))(set! *warn-on-reflection* true)@@ -633,3 +634,198 @@:auth-provider :aws-iam:ssl true})))))(log/info "Skipping test: MB_MYSQL_AWS_IAM_TEST not set")))++(defn- count-swapped-pools-for-db+ "Count the number of swapped connection pools for a given database ID.+ Since pools are keyed by [db-id, details-hash], we need to iterate through+ all cache entries and count those matching the db-id."+ [db-id]+ (let [cache ^Cache @#'sql-jdbc.conn/swapped-connection-pools]+ (count (filter (fn [[cached-db-id _details-hash]]+ (= cached-db-id db-id))+ (keys (.asMap cache))))))++(defn- swap-cache-key+ "Helper to compute the cache key for a swapped pool, matching the implementation in connection.clj.+ Takes a db map (with :id, :engine, :details) and returns [db-id, jdbc-spec-hash]."+ [db]+ [(:id db) (#'sql-jdbc.conn/jdbc-spec-hash db)])++(deftest with-swapped-connection-details-test+ (testing "Swap connection details temporarily"+ (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})+ (let [db (mt/db)+ db-id (u/the-id db)]+ (sql-jdbc.conn/invalidate-pool-for-db! db)+ (let [original-spec (sql-jdbc.conn/db->pooled-connection-spec db)]+ (testing "Swap map is merged into details when creating connection"+ (driver/with-swapped-connection-details db-id {:test-swap true}+ (testing "spec is swapped"+ (is (not= original-spec (sql-jdbc.conn/db->pooled-connection-spec db))))+ (testing "Pool was created with swap in swapped pools cache"+ (is (= 1 (count-swapped-pools-for-db db-id)))))))++ (testing "Connection works normally outside swap scope"+ (sql-jdbc.conn/invalidate-pool-for-db! db)+ (let [spec (sql-jdbc.conn/db->pooled-connection-spec db)]+ (is (some? spec))))))))++(deftest different-swap-details-get-separate-pools-test+ (testing "Different swap details for the same database get separate pools, identical details share pools"+ (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})+ (let [db (mt/db)+ db-id (u/the-id db)]+ (sql-jdbc.conn/invalidate-pool-for-db! db)+ (let [pool-a-1 (atom nil)+ pool-b (atom nil)+ pool-a-2 (atom nil)]+ (testing "User A swaps with their credentials"+ (driver/with-swapped-connection-details db-id {:user "user-a" :password "pass-a" :log-level 100}+ (reset! pool-a-1 (sql-jdbc.conn/db->pooled-connection-spec db))+ (is (= 1 (count-swapped-pools-for-db db-id)) "First swap creates one pool")))+ (testing "User B swaps with different credentials"+ (driver/with-swapped-connection-details db-id {:user "user-b" :password "pass-b" :log-level 99}+ (reset! pool-b (sql-jdbc.conn/db->pooled-connection-spec db))+ (is (= 2 (count-swapped-pools-for-db db-id)) "Different swap details create a second pool")+ (is (not (identical? @pool-a-1 @pool-b)) "Different swap details return different pool instances")))+ (testing "User A returns - should reuse their original pool (still in cache due to TTL)"+ (driver/with-swapped-connection-details db-id {:user "user-a" :password "pass-a" :log-level 100}+ (reset! pool-a-2 (sql-jdbc.conn/db->pooled-connection-spec db))+ (is (= 2 (count-swapped-pools-for-db db-id)) "Identical swap details reuse existing pool")+ (is (identical? @pool-a-1 @pool-a-2) "Identical swap details return the same pool instance"))))))))++(deftest with-swapped-connection-details-nested-test+ (testing "Nested swaps for the same database throw an exception"+ (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})+ (let [db (mt/db)+ db-id (u/the-id db)]+ (sql-jdbc.conn/invalidate-pool-for-db! db)+ (driver/with-swapped-connection-details db-id {:outer-swap true}+ (sql-jdbc.conn/db->pooled-connection-spec db)+ (testing "Attempting nested swap for same database throws"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Nested connection detail swaps are not supported"+ (driver/with-swapped-connection-details db-id {:inner-swap true}+ (sql-jdbc.conn/db->pooled-connection-spec db)))))))))++ (testing "Different databases can have concurrent swaps"+ (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})+ (let [db-1 (mt/db)+ db-1-id (u/the-id db-1)]+ ;; We can only test this with one db in most test setups, but the code path works+ (driver/with-swapped-connection-details db-1-id {:swap-1 true}+ ;; This would work for a different db-id+ (is (some? (sql-jdbc.conn/db->pooled-connection-spec db-1))))))))++(deftest invalidate-pool-clears-both-canonical-and-swapped-test+ (testing "invalidate-pool-for-db! clears both canonical and swapped pools"+ (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})+ (let [db (mt/db)+ db-id (u/the-id db)]+ (sql-jdbc.conn/invalidate-pool-for-db! db)++ (sql-jdbc.conn/db->pooled-connection-spec db)+ (is (contains? @@#'sql-jdbc.conn/database-id->connection-pool db-id))++ (driver/with-swapped-connection-details db-id {:test-swap true}+ (sql-jdbc.conn/db->pooled-connection-spec db))+ (is (= 1 (count-swapped-pools-for-db db-id)))++ ;; Now invalidate - should clear both+ (sql-jdbc.conn/invalidate-pool-for-db! db)++ (testing "Canonical pool is cleared"+ (is (not (contains? @@#'sql-jdbc.conn/database-id->connection-pool db-id))))+ (testing "Swapped pool is cleared"+ (is (= 0 (count-swapped-pools-for-db db-id))))))))++(deftest swapped-pool-recreated-when-expired-test+ (testing "Swapped pools are recreated when password expires"+ (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})+ (let [db (mt/db)+ db-id (u/the-id db)+ swap-details {:test-swap true}+ create-count (atom 0)]+ (sql-jdbc.conn/invalidate-pool-for-db! db)+ (with-redefs [sql-jdbc.conn/create-pool! (let [original @#'sql-jdbc.conn/create-pool!]+ (fn [db]+ (swap! create-count inc)+ (original db)))]+ (driver/with-swapped-connection-details db-id swap-details+ ;; First call creates a pool+ (let [pool-1 (sql-jdbc.conn/db->pooled-connection-spec db)]+ (is (= 1 @create-count))+ (is (some? pool-1))++ ;; Simulate password expiration by modifying the cached pool+ ;; Cache key is [db-id, jdbc-spec-hash-of-swapped-db]+ (let [cache ^Cache @#'sql-jdbc.conn/swapped-connection-pools+ swapped-db (update db :details merge swap-details)+ cache-key (swap-cache-key swapped-db)+ ;; Use a fixed past timestamp (year 2020) to simulate expired password+ expired-timestamp 1577836800000]+ (.put cache cache-key (assoc pool-1 :password-expiry-timestamp expired-timestamp)))++ ;; Next call should detect invalid pool and recreate+ (let [pool-2 (sql-jdbc.conn/db->pooled-connection-spec db)]+ (is (= 2 @create-count) "Pool should have been recreated due to expired password")+ (is (some? pool-2))+ (is (not (identical? pool-1 pool-2)) "Should be a different pool instance")))))))))++(deftest swapped-pool-recreated-when-tunnel-closed-test+ (testing "Swapped pools are recreated when SSH tunnel is closed"+ (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})+ (let [db (mt/db)+ db-id (u/the-id db)+ swap-details {:test-swap true}+ create-count (atom 0)]+ (sql-jdbc.conn/invalidate-pool-for-db! db)+ (with-redefs [sql-jdbc.conn/create-pool! (let [original @#'sql-jdbc.conn/create-pool!]+ (fn [db]+ (swap! create-count inc)+ (original db)))]+ (driver/with-swapped-connection-details db-id swap-details+ ;; First call creates a pool+ (let [pool-1 (sql-jdbc.conn/db->pooled-connection-spec db)]+ (is (= 1 @create-count))+ (is (some? pool-1))++ ;; Simulate closed tunnel by modifying the cached pool+ ;; We add a tunnel-session that reports as closed+ ;; Cache key is [db-id, jdbc-spec-hash-of-swapped-db]+ (let [cache ^Cache @#'sql-jdbc.conn/swapped-connection-pools+ swapped-db (update db :details merge swap-details)+ cache-key (swap-cache-key swapped-db)]+ (.put cache cache-key (assoc pool-1 :tunnel-session :mock-closed-session)))++ ;; Mock ssh-tunnel-open? to return false for our mock session+ (with-redefs [ssh/ssh-tunnel-open? (fn [pool-spec]+ (not= :mock-closed-session (:tunnel-session pool-spec)))]+ ;; Next call should detect invalid pool and recreate+ (let [pool-2 (sql-jdbc.conn/db->pooled-connection-spec db)]+ (is (= 2 @create-count) "Pool should have been recreated due to closed tunnel")+ (is (some? pool-2))+ (is (not (identical? pool-1 pool-2)) "Should be a different pool instance"))))))))))++(deftest swapped-pool-reused-when-valid-test+ (testing "Valid swapped pools are reused without recreation"+ (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})+ (let [db (mt/db)+ db-id (u/the-id db)+ create-count (atom 0)]+ (sql-jdbc.conn/invalidate-pool-for-db! db)+ (with-redefs [sql-jdbc.conn/create-pool! (let [original @#'sql-jdbc.conn/create-pool!]+ (fn [db]+ (swap! create-count inc)+ (original db)))]+ (driver/with-swapped-connection-details db-id {:test-swap true}+ ;; First call creates a pool+ (let [pool-1 (sql-jdbc.conn/db->pooled-connection-spec db)]+ (is (= 1 @create-count))+ (is (some? pool-1))++ ;; Second call should reuse the same pool+ (let [pool-2 (sql-jdbc.conn/db->pooled-connection-spec db)]+ (is (= 1 @create-count) "Pool should be reused, not recreated")+ (is (identical? pool-1 pool-2) "Should be the same pool instance")))))))))diff --git a/test/metabase/driver_test.clj b/test/metabase/driver_test.cljindex 0b8bdb9afee..1a1980a4cbf 100644--- a/test/metabase/driver_test.clj+++ b/test/metabase/driver_test.clj@@ -17,7 +17,6 @@[metabase.test.data.interface :as tx][metabase.util :as u][metabase.util.json :as json]- [metabase.util.log :as log][toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -483,51 +482,46 @@(is (thrown-with-msg? clojure.lang.ExceptionInfo #"Database that supports :dependencies/native does not provide an implementation of driver/native-query-deps"(driver/native-query-deps ::mock-deps-driver nil nil))))-(defn- create-index-test-impl! [schema]- (let [driver driver/*driver*- suffix (str (System/currentTimeMillis))- table-name (str "test_table_" suffix)- qualified-table-name (keyword schema table-name)- index-name #(str "test_index_" % "_" suffix)- database-id (mt/id)- cleanup (fn []- (try- (driver/drop-table! driver database-id qualified-table-name)- (catch Throwable t- (log/fatal t "Could not clean up test table!")- (throw t))))]- (when schema- (driver/create-schema-if-needed! driver (driver/connection-spec driver database-id) schema))- (testing "Throws a driver-specific exception if the table does not exist"- (is (thrown? Throwable (driver/create-index! driver database-id schema table-name (index-name "a") [:a]))))- (try- (driver/create-table! driver database-id qualified-table-name {:a [:int], :b [:int], :c [:int]})- (testing "Create a single column index"- (is (nil? (driver/create-index! driver database-id schema table-name (index-name "a") [:a])))- (testing "Index now exists"- (is (= #{{:type :normal-column-index- :index-name (index-name "a")- :value "a"}}- (driver/describe-table-indexes driver database-id {:schema schema :name table-name}))))- (testing "Drop the index"- (is (nil? (driver/drop-index! driver database-id schema table-name (index-name "a"))))- (testing "Index no longer exists"- (is (empty? (driver/describe-table-indexes driver database-id {:schema schema :name table-name}))))))- (testing "Create a multi column index"- (driver/create-index! driver database-id schema table-name (index-name "b_c") [:b :c])- (testing "Index now exists"- ;; single-part only as non-leading columns currently dropped by describe-table-indexes- (is (= #{{:type :normal-column-index- :index-name (index-name "b_c")- :value "b"}}- (driver/describe-table-indexes driver database-id {:schema schema :name table-name})))))- (finally- (cleanup)))))--(deftest create-index-test- (mt/test-drivers (mt/normal-drivers-with-feature :transforms/index-ddl)- (create-index-test-impl! nil)))--(deftest create-index-schema-test- (mt/test-drivers (mt/normal-drivers-with-feature :transforms/index-ddl :schemas)- (create-index-test-impl! "flibble")))+(deftest ^:parallel maybe-swap-details-test+ (testing "maybe-swap-details merges swap map into details"+ (driver/with-swapped-connection-details 1 {:user "swap-user" :password "swap-pass"}+ (is (= {:host "localhost" :user "swap-user" :password "swap-pass"}+ (driver/maybe-swap-details 1 {:host "localhost" :user "original-user" :password "original-pass"})))))++ (testing "maybe-swap-details returns details unchanged when no swap exists"+ (driver/with-swapped-connection-details 1 {:user "swap-user"}+ (is (= {:host "localhost" :user "original-user"}+ (driver/maybe-swap-details 2 {:host "localhost" :user "original-user"})))))++ (testing "maybe-swap-details supports deep merge for nested maps"+ (driver/with-swapped-connection-details 1 {:ssl {:key-store-password "new-pass"}}+ (is (= {:host "localhost" :ssl {:enabled true :key-store-password "new-pass"}}+ (driver/maybe-swap-details 1 {:host "localhost" :ssl {:enabled true :key-store-password "old-pass"}})))))++ (testing "deep merge adds new keys to nested maps"+ (driver/with-swapped-connection-details 1 {:ssl {:new-key "new-value"}}+ (is (= {:host "localhost" :ssl {:enabled true :new-key "new-value"}}+ (driver/maybe-swap-details 1 {:host "localhost" :ssl {:enabled true}})))))++ (testing "deep merge replaces nested map with non-map value"+ (driver/with-swapped-connection-details 1 {:ssl "disabled"}+ (is (= {:host "localhost" :ssl "disabled"}+ (driver/maybe-swap-details 1 {:host "localhost" :ssl {:enabled true :key-store "path"}})))))++ (testing "deep merge adds nested map where none existed"+ (driver/with-swapped-connection-details 1 {:ssl {:enabled true}}+ (is (= {:host "localhost" :ssl {:enabled true}}+ (driver/maybe-swap-details 1 {:host "localhost"})))))++ (testing "deep merge works with multiple levels of nesting"+ (driver/with-swapped-connection-details 1 {:advanced {:ssl {:cert {:path "/new/path"}}}}+ (is (= {:host "localhost" :advanced {:timeout 30 :ssl {:enabled true :cert {:path "/new/path"}}}}+ (driver/maybe-swap-details 1 {:host "localhost" :advanced {:timeout 30 :ssl {:enabled true :cert {:path "/old/path"}}}})))))++ (testing "nested swaps for the same database throw an exception"+ (driver/with-swapped-connection-details 1 {:user "outer"}+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Nested connection detail swaps are not supported"+ (driver/with-swapped-connection-details 1 {:user "inner"}+ nil))))))diff --git a/test/metabase/permissions_rest/api_test.clj b/test/metabase/permissions_rest/api_test.cljindex 9daf2539841..885378a5903 100644--- a/test/metabase/permissions_rest/api_test.clj+++ b/test/metabase/permissions_rest/api_test.clj@@ -554,7 +554,7 @@:model/PermissionsGroup {group-id :id} {}:model/PermissionsGroupMembership _ {:group_id group-id:user_id user-id}]- (testing "requires superuser permisisons"+ (testing "requires superuser permissions"(is (= "You don't have permissions to do that."(mt/user-http-request :rasta :put 403 (format "permissions/membership/%d/clear" group-id)))))diff --git a/test/metabase/queries/models/card_test.clj b/test/metabase/queries/models/card_test.cljindex 2de75b3cfd2..465bd302ee1 100644--- a/test/metabase/queries/models/card_test.clj+++ b/test/metabase/queries/models/card_test.clj@@ -846,7 +846,7 @@(is (=? {:can_run_adhoc_query false}(t2/hydrate no-query :can_run_adhoc_query)))))))-(deftest audit-card-permisisons-test+(deftest audit-card-permissions-test(testing "Cards in audit collections are not readable or writable on OSS, even if they exist (#42645)";; Here we're testing the specific scenario where an EE instance is downgraded to OSS, but still has the audit;; collections and cards installed. Since we can't load audit content on OSS, let's just redef the audit collectiondiff --git a/test/metabase/revisions/impl/card_test.clj b/test/metabase/revisions/impl/card_test.cljindex f54990d8fb2..c69042ffd99 100644--- a/test/metabase/revisions/impl/card_test.clj+++ b/test/metabase/revisions/impl/card_test.clj@@ -141,7 +141,10 @@:dataset_query_metrics_v2_migration_backup;; `dependency_analysis_version` is an internal bookkeeping field. It doesn't affect the;; actual card itself, so no description is necessary.- :dependency_analysis_version} col)+ :dependency_analysis_version+ ;; this column is immutable in practice, and doesn't warrant a description+ :workspace_id}+ col)(testing (format "we should have a revision description for %s" col)(let [diff-strings (revision/diff-strings;; TODO -- huh? Shouldn't this be testing against `:model/Card` here???diff --git a/test/metabase/search/appdb/index_test.clj b/test/metabase/search/appdb/index_test.cljindex d6ff86fbdc4..3478905e2cb 100644--- a/test/metabase/search/appdb/index_test.clj+++ b/test/metabase/search/appdb/index_test.clj@@ -549,7 +549,8 @@"metabase_table" #{"action" "measure" "model_index_value" "report_card" "segment"}"document" #{"action" "model_index_value" "report_card"}"report_card" #{"action" "model_index_value"}- "report_dashboard" #{"action" "model_index_value" "report_card"}})+ "report_dashboard" #{"action" "model_index_value" "report_card"}+ "workspace" #{"collection"}})(deftest search-model-cascade-test(is (= model->deleted-descendantsdiff --git a/test/metabase/test/data/datasets.clj b/test/metabase/test/data/datasets.cljindex 2eb9a510150..d537ef655bd 100644--- a/test/metabase/test/data/datasets.clj+++ b/test/metabase/test/data/datasets.clj@@ -40,5 +40,8 @@{:style/indent :defn}[drivers & body]`(doseq [driver# ~drivers]- (test-driver driver#- ~@body)))+ ;; If we're already within a mt/test-drivers, this macro acts as an additional filter+ ;; i.e. nesting this macro allows us to test the intersection of the corresponding sets+ (when (or (nil? driver/*driver*) (= driver# driver/*driver*))+ (test-driver driver#+ ~@body))))diff --git a/test/metabase/test/data/h2.clj b/test/metabase/test/data/h2.cljindex 4a7dd616bf1..13ba2d3bfda 100644--- a/test/metabase/test/data/h2.clj+++ b/test/metabase/test/data/h2.clj@@ -34,11 +34,14 @@(defmethod sql.tx/field-base-type->sql-type [:h2 base-type] [_ _] database-type))(defmethod tx/dbdef->connection-details :h2- [_driver context dbdef]- {:db (str "mem:" (tx/escaped-database-name dbdef) (when (= context :db)- ;; Return details with the GUEST user added so SQL queries are- ;; allowed.- ";USER=GUEST;PASSWORD=guest"))})+ [_driver _context dbdef]+ {:db (str "mem:" (tx/escaped-database-name dbdef)+ ;; TODO (Ngoc 2025-12-05) -- we want admins user because workspaces tests need to be able to create+ ;; user and grant privileges and stuffs. is it safe???+ #_(when (= context :db)+ ;; Return details with the GUEST user added so SQL queries are+ ;; allowed.+ ";USER=GUEST;PASSWORD=guest"))})(defmethod sql.tx/pk-sql-type :h2 [_] "BIGINT AUTO_INCREMENT")diff --git a/test/metabase/test/data/mysql.clj b/test/metabase/test/data/mysql.cljindex 4532a53eadc..eea05d411b1 100644--- a/test/metabase/test/data/mysql.clj+++ b/test/metabase/test/data/mysql.clj@@ -67,7 +67,11 @@(merge{:host (tx/db-test-env-var-or-throw :mysql :host "localhost"):port (tx/db-test-env-var-or-throw :mysql :port 3306)- :user (tx/db-test-env-var :mysql :user "root")}+ :user (tx/db-test-env-var :mysql :user "root")+ ;; MySQL 8+ uses caching_sha2_password by default, which requires RSA key exchange+ ;; for password auth over non-SSL connections. Needed for workspace isolation tests+ ;; which create password-authenticated users.+ :additional-options "allowPublicKeyRetrieval=true"}(when-let [password (tx/db-test-env-var :mysql :password)]{:password password})(when (= context :db)diff --git a/test/metabase/test/http_client.clj b/test/metabase/test/http_client.cljindex 2205f42ca21..9a6f7aa2677 100644--- a/test/metabase/test/http_client.clj+++ b/test/metabase/test/http_client.clj@@ -265,8 +265,11 @@(when expected-status-code(is (= expected-status-codeactual-status-code)- (format "%s %s expected a status code of %d, got %d."- method-name url expected-status-code actual-status-code))))+ (format "%s %s expected a status code of %d, got %d.\nResponse body: %s"+ method-name url expected-status-code actual-status-code+ ;; micro-optimization: don't stringify the body unless this assertion will actually fail+ (when (not= expected-status-code actual-status-code)+ body)))))(def ^:private method->request-fn{:get http/getdiff --git a/test/metabase/test/util.clj b/test/metabase/test/util.cljindex f32f97cb234..5be3ff2cc2c 100644--- a/test/metabase/test/util.clj+++ b/test/metabase/test/util.clj@@ -47,7 +47,8 @@[toucan2.model :as t2.model][toucan2.tools.before-update :as t2.before-update][toucan2.tools.transformed :as t2.transformed]- [toucan2.tools.with-temp :as t2.with-temp])+ [toucan2.tools.with-temp :as t2.with-temp]+ [toucan2.util :as t2.u])(:import(java.io File FileInputStream)(java.net ServerSocket)@@ -340,7 +341,8 @@:source {:type "query":query (lib/native-query (data/metadata-provider) "SELECT 1 as num")}:target {:type "table"- :name (str "test_table_" (u/generate-nano-id))}})+ :name (str "test_table_" (str/replace (u/generate-nano-id) "-" "_"))+ :database (data/id)}}):model/TransformJob(fn [_]@@ -372,7 +374,46 @@:email (u.random/random-email):password (u.random/random-name):date_joined (t/zoned-date-time)- :updated_at (t/zoned-date-time)})})+ :updated_at (t/zoned-date-time)})++ :model/Workspace+ (fn [_]+ (default-timestamped+ {:name (str "Test Workspace " (u/generate-nano-id))+ :schema (str "mb__isolation_" (u/generate-nano-id))}))++ :model/WorkspaceTransform+ (fn [_]+ (default-timestamped+ {:name (str "Test Transform " (u/generate-nano-id))+ :ref_id ((requiring-resolve 'metabase-enterprise.workspaces.util/generate-ref-id))+ :source {:type "query"+ :query (lib/native-query (data/metadata-provider) "SELECT 1 as num")}+ :target {:type "table"+ :name (str "test_table_" (str/replace (u/generate-nano-id) "-" "_"))}}))})++;; WorkspaceTransform use composite primary keys are currently t2/insert-returning-instance!+;; does not return the instance for model with composite keys on h2 and mysql+;; so we have to define a custom with-temp here+(methodical/defmethod t2.with-temp/do-with-temp* :model/WorkspaceTransform+ [model explicit-attributes f]+ (assert (some? model) (format "%s model cannot be nil." `with-temp))+ (when (some? explicit-attributes)+ (assert (map? explicit-attributes) (format "attributes passed to %s must be a map." `with-temp)))+ (let [defaults (t2.with-temp/with-temp-defaults model)+ merged-attributes (merge {} defaults explicit-attributes)]+ (t2.u/try-with-error-context ["with temp" {::model model+ ::explicit-attributes explicit-attributes+ ::default-attributes defaults+ ::merged-attributes merged-attributes}]+ (let [temp-object (or (t2/insert-returning-instance! model merged-attributes)+ (t2/select-one :model/WorkspaceTransform :ref_id (:ref_id merged-attributes)+ :workspace_id (:workspace_id merged-attributes)))]+ (try+ (testing (format "\nwith temporary %s\n" (pr-str model))+ (f temp-object))+ (finally+ (t2/delete! model :toucan/pk ((t2/select-pks-fn model) temp-object))))))))(defn- set-with-temp-defaults! [](doseq [[model defaults-fn] with-temp-defaults-fns]@@ -840,6 +881,11 @@[_][:not= :id audit/audit-db-id])+(def ^:private models-with-cleanup-hooks+ "Models that require `t2/delete!` instead of raw SQL delete during cleanup.+ Use this for models that have `before-delete` or `after-delete` hooks that must run."+ #{:model/Workspace})+(defn- model->model&pk [model](if (vector? model)model@@ -871,16 +917,23 @@;; might not have an old max ID if this is the first time the macro is used in this test run.:let [old-max-id (get model->old-max-id model)max-id-condition (if old-max-id [:> pk old-max-id] true)- additional-conditions (with-model-cleanup-additional-conditions model)]]- (t2/query-one- {:delete-from (t2/table-name model)- :where [:and max-id-condition additional-conditions]}))+ additional-conditions (with-model-cleanup-additional-conditions model)+ where-clause [:and max-id-condition additional-conditions]]]+ (if (contains? models-with-cleanup-hooks model)+ ;; Use t2/delete! to trigger before-delete/after-delete hooks+ (t2/delete! model {:where where-clause})+ ;; Fast path: raw SQL for models without hooks+ (t2/query-one+ {:delete-from (t2/table-name model)+ :where where-clause})));; TODO we don't (currently) have index update hooks on deletes, so we need this to ensure rollback happens.(search/reindex! {:in-place? true :async? false})))))(defmacro with-model-cleanup- "Execute `body`, then delete any *new* rows created for each model in `models`. Calls `delete!`, so if the model has- defined any `pre-delete` behavior, that will be preserved.+ "Execute `body`, then delete any *new* rows created for each model in `models`.++ By default, uses raw SQL DELETE for performance. For models in [[models-with-cleanup-hooks]],+ uses `t2/delete!` to ensure `before-delete`/`after-delete` hooks are triggered.It's preferable to use `with-temp` instead, but you can use this macro if `with-temp` wouldn't work in yoursituation (e.g. when creating objects via the API).diff --git a/test/metabase/warehouse_schema_rest/api/table_test.clj b/test/metabase/warehouse_schema_rest/api/table_test.cljindex 3aeaea976f0..eb841f1d43f 100644--- a/test/metabase/warehouse_schema_rest/api/table_test.clj+++ b/test/metabase/warehouse_schema_rest/api/table_test.clj@@ -53,6 +53,7 @@:settings {}:cache_ttl nil:provider_name nil+ :workspace_permissions_status nil:is_audit false}))(defn- table-defaults@@ -1196,7 +1197,7 @@(deftest trigger-metadata-sync-for-table-test(testing "Can we trigger a metadata sync for a table?"(let [sync-called? (promise)- timeout (* 10 60)]+ timeout (* 10 1000)](mt/with-premium-features #{:audit-app}(mt/with-temp [:model/Database {db-id :id} {:engine "h2", :details (:details (mt/db))}:model/Table table {:db_id db-id :schema "PUBLIC"}]@@ -1210,7 +1211,7 @@(testing "POST /api/table/:id/sync_schema"(testing "User with manage-table-metadata permission can sync table"(let [sync-called? (promise)- timeout (* 10 60)]+ timeout (* 10 1000)](mt/with-premium-features #{:audit-app}(mt/with-temp [:model/Database {db-id :id} {:engine "h2", :details (:details (mt/db))}:model/Table table {:db_id db-id :schema "PUBLIC"}]@@ -1218,10 +1219,10 @@;; Grant only manage-table-metadata permission for this table(data-perms/set-table-permission! (perms-group/all-users) (:id table) :perms/manage-table-metadata :yes)(with-redefs [sync/sync-table! (deliver-when-tbl sync-called? table)]- (mt/user-http-request :rasta :post 200 (format "table/%d/sync_schema" (u/the-id table)))))))- (testing "sync called?"- (is (true?- (deref sync-called? timeout :sync-never-called))))))))+ (mt/user-http-request :rasta :post 200 (format "table/%d/sync_schema" (u/the-id table)))+ (testing "sync called?"+ (is (true?+ (deref sync-called? timeout :sync-never-called))))))))))))(deftest sync-schema-mirror-database-test(testing "POST /api/table/:id/sync_schema"diff --git a/test/metabase/warehouses_rest/api_test.clj b/test/metabase/warehouses_rest/api_test.cljindex d2db81399c9..95ebb283de6 100644--- a/test/metabase/warehouses_rest/api_test.clj+++ b/test/metabase/warehouses_rest/api_test.clj@@ -623,6 +623,57 @@{:settings {:database-enable-actions true}})[:settings :database-enable-actions]))))))+(deftest update-database-settings-only-validates-changed-settings-test+ (testing "PUT /api/database/:id only validates settings that are being changed"+ (testing "should not validate existing settings that aren't being changed"+ ;; api-test-disabled-for-database is always disabled, so it would fail validation if we tried to validate it+ ;; Here we create a database with that setting already set, then update a different setting.+ ;; If validation happens on all settings, it would fail. If it only validates changed settings, it should succeed.+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :settings {:api-test-disabled-for-database true+ :database-enable-actions false}}]+ (is (= {:api-test-disabled-for-database true+ :database-enable-actions true}+ (:settings (mt/user-http-request :crowberto :put 200+ (format "database/%s" db-id)+ {:settings {:database-enable-actions true}}))))))++ (testing "should not validate settings where the value hasn't changed"+ ;; Same setup, but we set the same value as before - should skip validation+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :settings {:api-test-disabled-for-database true}}]+ (is (= {:api-test-disabled-for-database true}+ (:settings (mt/user-http-request :crowberto :put 200+ (format "database/%s" db-id)+ {:settings {:api-test-disabled-for-database true}}))))))++ (testing "should still validate settings that are actually being changed to a new value"+ ;; If we try to change api-test-disabled-for-database to a different value, it should fail validation+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :settings {:api-test-disabled-for-database false}}]+ (is (= "Setting api-test-disabled-for-database is not enabled for this database"+ (:message (mt/user-http-request :crowberto :put 400+ (format "database/%s" db-id)+ {:settings {:api-test-disabled-for-database true}}))))))++ (testing "should not validate settings being reset to nil (default)"+ ;; Resetting a setting to nil should always be allowed, even if the setting would fail validation+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :settings {:api-test-disabled-for-database true}}]+ (is (= {}+ (:settings (mt/user-http-request :crowberto :put 200+ (format "database/%s" db-id)+ {:settings {:api-test-disabled-for-database nil}}))))))++ (testing "should not validate settings being reset to default value (literally)"+ ;; Resetting a setting to default should always be allowed, even if the setting would fail validation+ (mt/with-temp [:model/Database {db-id :id} {:engine :h2+ :settings {:api-test-disabled-for-database true}}]+ (is (= {:api-test-disabled-for-database false}+ (:settings (mt/user-http-request :crowberto :put 200+ (format "database/%s" db-id)+ {:settings {:api-test-disabled-for-database false}}))))))))+(deftest update-database-enable-actions-open-connection-test(testing "Updating a database's `database-enable-actions` setting shouldn't close existing connections (metabase#27877)"(mt/test-drivers (filter #(isa? driver/hierarchy % :sql-jdbc) (mt/normal-drivers-with-feature :actions))@@ -2390,6 +2441,7 @@(setting/defsetting api-test-disabled-for-database"A feature used for testing /settings-available (3)":type :boolean+ :default false:database-local :only:enabled-for-db? (constantly false))@@ -2452,6 +2504,62 @@:api-test-disabled-for-custom-reasons:api-test-disabled-for-multiple-reasons])))))))))+;;; ---------------------------------------- workspace permissions endpoint tests ----------------------------------------++(deftest workspace-permission-endpoint-test+ (mt/test-drivers (mt/normal-drivers-with-feature :workspace)+ (testing "POST /api/database/:id/permission/workspace/check"+ (testing "returns cached status when available"+ ;; First call to populate cache+ (mt/user-http-request :crowberto :post 200+ (format "database/%d/permission/workspace/check" (mt/id))+ {:cached false})+ ;; Second call should return cached result+ (let [response (mt/user-http-request :crowberto :post 200+ (format "database/%d/permission/workspace/check" (mt/id)))]+ (is (= "ok" (:status response)))+ (is (some? (:checked_at response)))+ (is (nil? (:error response)))))++ (testing "runs permission check when no cache exists"+ ;; Clear the cache+ (t2/update! :model/Database (mt/id) {:workspace_permissions_status nil})+ (let [response (mt/user-http-request :crowberto :post 200+ (format "database/%d/permission/workspace/check" (mt/id)))]+ (is (= "ok" (:status response)))+ (is (some? (:checked_at response)))+ ;; Verify it was cached+ (let [db (t2/select-one :model/Database (mt/id))]+ (is (= "ok" (:status (:workspace_permissions_status db)))))))++ (testing "cached=false forces permission check"+ ;; Set a stale cache value+ (t2/update! :model/Database (mt/id) {:workspace_permissions_status {:status "stale" :checked_at "2020-01-01T00:00:00Z"}})+ (let [response (mt/user-http-request :crowberto :post 200+ (format "database/%d/permission/workspace/check" (mt/id))+ {:cached false})]+ (is (= "ok" (:status response)))+ ;; Verify cache was updated+ (let [db (t2/select-one :model/Database (mt/id))]+ (is (= "ok" (:status (:workspace_permissions_status db)))))))++ (testing "requires superuser"+ (is (= "You don't have permissions to do that."+ (mt/user-http-request :rasta :post 403+ (format "database/%d/permission/workspace/check" (mt/id)))))))))++(deftest workspace-permission-failure-test+ (mt/test-drivers (mt/normal-drivers-with-feature :workspace)+ (testing "returns failed status when permissions check fails"+ (mt/with-dynamic-fn-redefs [driver/check-isolation-permissions+ (fn [_driver _database _table]+ "permission denied")]+ (let [response (mt/user-http-request :crowberto :post 200+ (format "database/%d/permission/workspace/check" (mt/id))+ {:cached false})]+ (is (= "failed" (:status response)))+ (is (= "permission denied" (:error response))))))))+;;; +----------------------------------------------------------------------------------------------------------------+;;; | can-query filter tests |;;; +----------------------------------------------------------------------------------------------------------------+diff --git a/test_resources/serialization_baseline/collections/JS5JHDitDkWEtTt1-N7zr_qdlkexrocaxgnecmryqu/JS5JHDitDkWEtTt1-N7zr_qdlkexrocaxgnecmryqu.yaml b/test_resources/serialization_baseline/collections/JS5JHDitDkWEtTt1-N7zr_qdlkexrocaxgnecmryqu/JS5JHDitDkWEtTt1-N7zr_qdlkexrocaxgnecmryqu.yamlindex f0e329c30d8..d5f5861e968 100644--- a/test_resources/serialization_baseline/collections/JS5JHDitDkWEtTt1-N7zr_qdlkexrocaxgnecmryqu/JS5JHDitDkWEtTt1-N7zr_qdlkexrocaxgnecmryqu.yaml+++ b/test_resources/serialization_baseline/collections/JS5JHDitDkWEtTt1-N7zr_qdlkexrocaxgnecmryqu/JS5JHDitDkWEtTt1-N7zr_qdlkexrocaxgnecmryqu.yaml@@ -17,3 +17,4 @@ archive_operation_id: nullarchived_directly: nullis_remote_synced: falseis_sample: false+workspace_id: nulldiff --git a/test_resources/serialization_baseline/collections/M-Q4pcV0qkiyJ0kiSWECl_some_collection/M-Q4pcV0qkiyJ0kiSWECl_some_collection.yaml b/test_resources/serialization_baseline/collections/M-Q4pcV0qkiyJ0kiSWECl_some_collection/M-Q4pcV0qkiyJ0kiSWECl_some_collection.yamlindex c677a4c4d34..b9275911124 100644--- a/test_resources/serialization_baseline/collections/M-Q4pcV0qkiyJ0kiSWECl_some_collection/M-Q4pcV0qkiyJ0kiSWECl_some_collection.yaml+++ b/test_resources/serialization_baseline/collections/M-Q4pcV0qkiyJ0kiSWECl_some_collection/M-Q4pcV0qkiyJ0kiSWECl_some_collection.yaml@@ -17,3 +17,4 @@ archive_operation_id: nullarchived_directly: nullis_remote_synced: falseis_sample: false+workspace_id: nulldiff --git a/test_resources/serialization_baseline/collections/Y6d4QwJgGKw-X1tRh3ir2_shared_collection/Y6d4QwJgGKw-X1tRh3ir2_shared_collection.yaml b/test_resources/serialization_baseline/collections/Y6d4QwJgGKw-X1tRh3ir2_shared_collection/Y6d4QwJgGKw-X1tRh3ir2_shared_collection.yamlindex 1194e644482..32da378988b 100644--- a/test_resources/serialization_baseline/collections/Y6d4QwJgGKw-X1tRh3ir2_shared_collection/Y6d4QwJgGKw-X1tRh3ir2_shared_collection.yaml+++ b/test_resources/serialization_baseline/collections/Y6d4QwJgGKw-X1tRh3ir2_shared_collection/Y6d4QwJgGKw-X1tRh3ir2_shared_collection.yaml@@ -17,3 +17,4 @@ archive_operation_id: nullarchived_directly: nullis_remote_synced: falseis_sample: false+workspace_id: nulldiff --git a/test_resources/serialization_baseline/collections/gQuHAIM4C-7fToHzHyNNQ_ai_model/gQuHAIM4C-7fToHzHyNNQ_ai_model.yaml b/test_resources/serialization_baseline/collections/gQuHAIM4C-7fToHzHyNNQ_ai_model/gQuHAIM4C-7fToHzHyNNQ_ai_model.yamlindex b17d6dd2306..006f1f78ef0 100644--- a/test_resources/serialization_baseline/collections/gQuHAIM4C-7fToHzHyNNQ_ai_model/gQuHAIM4C-7fToHzHyNNQ_ai_model.yaml+++ b/test_resources/serialization_baseline/collections/gQuHAIM4C-7fToHzHyNNQ_ai_model/gQuHAIM4C-7fToHzHyNNQ_ai_model.yaml@@ -17,3 +17,4 @@ archive_operation_id: nullarchived_directly: nullis_remote_synced: falseis_sample: false+workspace_id: nulldiff --git a/test_resources/serialization_baseline/collections/olgKH56lYOjakuobH4zPz_grandparent_collection/olgKH56lYOjakuobH4zPz_grandparent_collection.yaml b/test_resources/serialization_baseline/collections/olgKH56lYOjakuobH4zPz_grandparent_collection/olgKH56lYOjakuobH4zPz_grandparent_collection.yamlindex 1c9f2d5d7fe..1fed7b90bbb 100644--- a/test_resources/serialization_baseline/collections/olgKH56lYOjakuobH4zPz_grandparent_collection/olgKH56lYOjakuobH4zPz_grandparent_collection.yaml+++ b/test_resources/serialization_baseline/collections/olgKH56lYOjakuobH4zPz_grandparent_collection/olgKH56lYOjakuobH4zPz_grandparent_collection.yaml@@ -17,3 +17,4 @@ archive_operation_id: nullarchived_directly: nullis_remote_synced: falseis_sample: false+workspace_id: nulldiff --git a/test_resources/serialization_baseline/collections/olgKH56lYOjakuobH4zPz_grandparent_collection/qPZhJMPNGkfd3sq8jWiZS_parent_collection/fJVsXIDzzipSZsaro9Pvu_child_collection/fJVsXIDzzipSZsaro9Pvu_child_collection.yaml b/test_resources/serialization_baseline/collections/olgKH56lYOjakuobH4zPz_grandparent_collection/qPZhJMPNGkfd3sq8jWiZS_parent_collection/fJVsXIDzzipSZsaro9Pvu_child_collection/fJVsXIDzzipSZsaro9Pvu_child_collection.yamlindex 8e6d1dc984d..3a655c6ff3b 100644--- a/test_resources/serialization_baseline/collections/olgKH56lYOjakuobH4zPz_grandparent_collection/qPZhJMPNGkfd3sq8jWiZS_parent_collection/fJVsXIDzzipSZsaro9Pvu_child_collection/fJVsXIDzzipSZsaro9Pvu_child_collection.yaml+++ b/test_resources/serialization_baseline/collections/olgKH56lYOjakuobH4zPz_grandparent_collection/qPZhJMPNGkfd3sq8jWiZS_parent_collection/fJVsXIDzzipSZsaro9Pvu_child_collection/fJVsXIDzzipSZsaro9Pvu_child_collection.yaml@@ -17,3 +17,4 @@ archive_operation_id: nullarchived_directly: nullis_remote_synced: falseis_sample: false+workspace_id: nulldiff --git a/test_resources/serialization_baseline/collections/olgKH56lYOjakuobH4zPz_grandparent_collection/qPZhJMPNGkfd3sq8jWiZS_parent_collection/qPZhJMPNGkfd3sq8jWiZS_parent_collection.yaml b/test_resources/serialization_baseline/collections/olgKH56lYOjakuobH4zPz_grandparent_collection/qPZhJMPNGkfd3sq8jWiZS_parent_collection/qPZhJMPNGkfd3sq8jWiZS_parent_collection.yamlindex 9abdc367fef..3538df8c147 100644--- a/test_resources/serialization_baseline/collections/olgKH56lYOjakuobH4zPz_grandparent_collection/qPZhJMPNGkfd3sq8jWiZS_parent_collection/qPZhJMPNGkfd3sq8jWiZS_parent_collection.yaml+++ b/test_resources/serialization_baseline/collections/olgKH56lYOjakuobH4zPz_grandparent_collection/qPZhJMPNGkfd3sq8jWiZS_parent_collection/qPZhJMPNGkfd3sq8jWiZS_parent_collection.yaml@@ -17,3 +17,4 @@ archive_operation_id: nullarchived_directly: nullis_remote_synced: falseis_sample: false+workspace_id: nulldiff --git a/test_resources/serialization_baseline/collections/p8Zq2ud7JlSoSBXdJWIaW_shared_collection/p8Zq2ud7JlSoSBXdJWIaW_shared_collection.yaml b/test_resources/serialization_baseline/collections/p8Zq2ud7JlSoSBXdJWIaW_shared_collection/p8Zq2ud7JlSoSBXdJWIaW_shared_collection.yamlindex 0ab84dd8db5..a3a940f3a46 100644--- a/test_resources/serialization_baseline/collections/p8Zq2ud7JlSoSBXdJWIaW_shared_collection/p8Zq2ud7JlSoSBXdJWIaW_shared_collection.yaml+++ b/test_resources/serialization_baseline/collections/p8Zq2ud7JlSoSBXdJWIaW_shared_collection/p8Zq2ud7JlSoSBXdJWIaW_shared_collection.yaml@@ -17,3 +17,4 @@ archive_operation_id: nullarchived_directly: nullis_remote_synced: falseis_sample: false+workspace_id: nulldiff --git a/test_resources/serialization_baseline/databases/My Database/My Database.yaml b/test_resources/serialization_baseline/databases/My Database/My Database.yamlindex 8e6598edc8d..c51bcd6d075 100644--- a/test_resources/serialization_baseline/databases/My Database/My Database.yaml+++ b/test_resources/serialization_baseline/databases/My Database/My Database.yaml@@ -12,8 +12,8 @@ is_on_demand: falseis_sample: falseis_audit: falseis_attached_dwh: false-metadata_sync_schedule: 0 51 * * * ? *-cache_field_values_schedule: 0 0 19 * * ? *+metadata_sync_schedule: 0 5 * * * ? *+cache_field_values_schedule: 0 0 23 * * ? *settings:database-source-dataset-name: test-datacaveats: nulldiff --git a/test_resources/serialization_baseline/databases/test-data (h2)/test-data (h2).yaml b/test_resources/serialization_baseline/databases/test-data (h2)/test-data (h2).yamlindex 053e38006ec..beb1365a61e 100644--- a/test_resources/serialization_baseline/databases/test-data (h2)/test-data (h2).yaml+++ b/test_resources/serialization_baseline/databases/test-data (h2)/test-data (h2).yaml@@ -17,8 +17,8 @@ is_on_demand: falseis_sample: falseis_audit: falseis_attached_dwh: false-metadata_sync_schedule: 0 33 * * * ? *-cache_field_values_schedule: 0 0 21 * * ? *+metadata_sync_schedule: 0 23 * * * ? *+cache_field_values_schedule: 0 0 15 * * ? *settings:database-source-dataset-name: test-datacaveats: null