Skip to content

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

Gdgt-3328 importing a collection breaks active custom visualizations (#84259)

Jakub Chodorowicz50e4cd4250ef
diff --git a/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/api.clj b/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/api.clj
index d4a71ebde7c..58b06a10777 100644
--- a/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/api.clj
@@ -265,17 +265,14 @@
tempfile (check-upload! file)]
(try
(let [bundle-bytes (Files/readAllBytes (.toPath tempfile))
- validated (cache/validate-bundle! bundle-bytes)
- new-name (get-in validated [:manifest :name])]
- (api/check-400 (= new-name (:identifier existing))
- (format "Bundle manifest \"name\" (%s) does not match the plugin's identifier (%s)."
- new-name (:identifier existing)))
- (let [result (cache/save-bundle! existing validated)]
- (events/publish-event! :event/custom-viz-plugin-update
- {:object result
- :previous-object existing
- :user-id api/*current-user-id*})
- (plugin->response (dissoc result :bundle))))
+ validated (->> (cache/validate-bundle! bundle-bytes)
+ (cache/check-bundle-identifier! (:identifier existing)))
+ result (cache/save-bundle! existing validated)]
+ (events/publish-event! :event/custom-viz-plugin-update
+ {:object result
+ :previous-object existing
+ :user-id api/*current-user-id*})
+ (plugin->response (dissoc result :bundle)))
(finally
(try (.delete tempfile) (catch Exception _))))))
diff --git a/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/cache.clj b/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/cache.clj
index 3d129490ccd..7967de46de9 100644
--- a/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/cache.clj
+++ b/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/cache.clj
@@ -224,8 +224,19 @@
;;; ------------------------------------------------ Save Bundle ------------------------------------------------
-(defn- derived-columns
- "DB columns derived from a validated bundle."
+(defn check-bundle-identifier!
+ "Throw a 400 ex-info unless the `name` in `validated`'s manifest equals `identifier`, the identity an existing
+ plugin's questions are saved under. Returns `validated`."
+ [identifier {:keys [manifest] :as validated}]
+ (let [bundle-name (:name manifest)]
+ (when-not (= bundle-name identifier)
+ (throw (ex-info (format "Bundle manifest \"name\" (%s) does not match the plugin's identifier (%s)."
+ bundle-name identifier)
+ {:status-code 400})))
+ validated))
+
+(defn derived-columns
+ "DB columns derived from a validated bundle (see [[validate-bundle!]])."
[{:keys [bytes hash manifest version-str]}]
{:status :active
:error_message nil
diff --git a/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/models/custom_viz_plugin.clj b/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/models/custom_viz_plugin.clj
index fb2196c1581..f20d5083c71 100644
--- a/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/models/custom_viz_plugin.clj
+++ b/enterprise/backend/src/metabase_enterprise/custom_viz_plugin/models/custom_viz_plugin.clj
@@ -1,6 +1,7 @@
(ns metabase-enterprise.custom-viz-plugin.models.custom-viz-plugin
(:require
[buddy.core.codecs :as codecs]
+ [metabase-enterprise.custom-viz-plugin.cache :as cache]
[metabase-enterprise.custom-viz-plugin.db :as custom-viz-plugin.db]
[metabase-enterprise.custom-viz-plugin.schema]
[metabase.api.common :as api]
@@ -69,14 +70,38 @@
(defmethod serdes/make-spec "CustomVizPlugin"
[_model-name _opts]
{:copy [:display_name :identifier :enabled :icon :manifest :metabase_version :bundle_hash]
- :skip [:dev_bundle_url :error_message]
+ :skip [;; derived from the bundle at import time (see [[bundle-columns]]), exactly as on upload
+ :status :error_message
+ ;; dev-only plugins are never served from another instance
+ :dev_bundle_url]
:defaults {:enabled true}
:transform {:created_at (serdes/date)
- :status {:export (constantly ::serdes/skip)
- :import (constantly "pending")}
:bundle {:export bundle->b64
:import b64->bundle}}})
+(defn- bundle-columns
+ "Columns derived from validating the imported `bundle` bytes against the imported `identifier`: the `:active` state
+ plus manifest-derived metadata when the bundle is valid and named for this plugin, or the `:error` state with the
+ reason when it is not. The same checks guard a bundle upload."
+ [{:keys [identifier bundle]}]
+ (try
+ (->> (cache/validate-bundle! bundle)
+ (cache/check-bundle-identifier! identifier)
+ cache/derived-columns)
+ (catch Exception e
+ {:status :error
+ :error_message (ex-message e)})))
+
+(defmethod serdes/load-insert! "CustomVizPlugin"
+ [_model-name ingested]
+ (custom-viz-plugin.db/insert-plugin! (merge ingested (bundle-columns ingested))))
+
+(defmethod serdes/load-update! "CustomVizPlugin"
+ [_model-name ingested {:keys [id]}]
+ (custom-viz-plugin.db/update-plugin! id (merge ingested (bundle-columns ingested)))
+ (cache/purge-plugin-cache! {:id id})
+ (custom-viz-plugin.db/non-blob-plugin id))
+
(defmethod serdes/entity-id "CustomVizPlugin" [_ {:keys [identifier]}]
identifier)
diff --git a/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/api_test.clj b/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/api_test.clj
index 14bf0e9e350..1725c67f5ea 100644
--- a/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/api_test.clj
@@ -167,8 +167,6 @@
(is (contains? (warn-types "colon:dev-viz") "invalid-identifier")
"dev-only plugins are exempt from version warnings but not from this one"))))))
-;;; ------------------------------------------------ Bundle bytes not exposed ------------------------------------------------
-
(deftest bundle-bytes-not-exposed-test
(mt/with-premium-features #{:custom-viz}
(mt/with-temp [:model/CustomVizPlugin {id :id} {:identifier "bytes-sec"
diff --git a/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/models/custom_viz_plugin_test.clj b/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/models/custom_viz_plugin_test.clj
index ce33daa9677..45e5f6b0b00 100644
--- a/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/models/custom_viz_plugin_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/models/custom_viz_plugin_test.clj
@@ -1,6 +1,9 @@
(ns metabase-enterprise.custom-viz-plugin.models.custom-viz-plugin-test
(:require
+ [buddy.core.codecs :as codecs]
+ [clojure.string :as str]
[clojure.test :refer :all]
+ [metabase-enterprise.custom-viz-plugin.test-util :as cv.tu]
[metabase.api.common :as api]
[metabase.models.interface :as mi]
[metabase.models.serialization :as serdes]
@@ -103,13 +106,7 @@
(is (contains? (set (:copy spec)) :manifest))
(is (contains? (set (:copy spec)) :bundle_hash))
(is (contains? (set (:skip spec)) :dev_bundle_url))
- (is (contains? (set (:skip spec)) :error_message))))
- (testing "status is always exported as skip and imported as pending"
- (let [spec (serdes/make-spec "CustomVizPlugin" {})
- status-export (get-in spec [:transform :status :export])
- status-import (get-in spec [:transform :status :import])]
- (is (= ::serdes/skip (status-export :active)))
- (is (= "pending" (status-import nil))))))
+ (is (contains? (set (:skip spec)) :error_message)))))
(deftest bundle-b64-round-trip-test
(testing "bundle bytes base64-encode on export and decode back to bytes on import"
@@ -128,3 +125,76 @@
import (get-in spec [:transform :bundle :import])]
(is (nil? (export nil)))
(is (nil? (import nil))))))
+
+(defn- ingested-plugin
+ "What serdes ingests for an export of plugin `identifier` whose bundle bytes are `bundle`."
+ [identifier ^bytes bundle]
+ {:serdes/meta [{:model "CustomVizPlugin" :id identifier :label identifier}]
+ :identifier identifier
+ :display_name identifier
+ :manifest {:name identifier}
+ :bundle_hash (cv.tu/bundle-hash bundle)
+ :bundle (codecs/bytes->b64-str bundle)
+ :created_at "2026-01-01T00:00:00Z"})
+
+(defn- active-plugin-row
+ "Columns of an active, enabled plugin `identifier` backed by `bundle`, for `mt/with-temp`."
+ [identifier ^bytes bundle]
+ {:identifier identifier
+ :display_name identifier
+ :status :active
+ :enabled true
+ :manifest {:name identifier}
+ :bundle bundle
+ :bundle_hash (cv.tu/bundle-hash bundle)})
+
+(deftest load-insert-validates-bundle-test
+ (testing "loading an exported plugin with a valid bundle makes it :active"
+ (mt/with-model-cleanup [:model/CustomVizPlugin]
+ (let [bundle (cv.tu/valid-bundle-bytes "imported-viz" {:icon "chart"})]
+ (serdes/load-one! (ingested-plugin "imported-viz" bundle) nil)
+ (is (=? {:status :active
+ :error_message nil
+ :enabled true
+ :bundle_hash (cv.tu/bundle-hash bundle)}
+ (t2/select-one :model/CustomVizPlugin :identifier "imported-viz"))))))
+ (testing "loading a plugin whose bundle is not a valid archive makes it :error"
+ (mt/with-model-cleanup [:model/CustomVizPlugin]
+ (serdes/load-one! (ingested-plugin "broken-viz" (.getBytes "not a tgz" "UTF-8")) nil)
+ (is (=? {:status :error
+ :error_message string?}
+ (t2/select-one :model/CustomVizPlugin :identifier "broken-viz"))))))
+
+(deftest load-update-validates-bundle-test
+ (testing "re-importing over an existing plugin re-validates the new bundle"
+ (let [old-bundle (cv.tu/valid-bundle-bytes "updated-viz")
+ new-bundle (cv.tu/valid-bundle-bytes "updated-viz" {:icon "star"})]
+ (mt/with-temp [:model/CustomVizPlugin {id :id} (assoc (active-plugin-row "updated-viz" old-bundle)
+ :status :error
+ :error_message "stale")]
+ (serdes/load-one! (ingested-plugin "updated-viz" new-bundle) (t2/select-one :model/CustomVizPlugin :id id))
+ (is (=? {:id id
+ :status :active
+ :error_message nil
+ :icon "star"
+ :bundle_hash (cv.tu/bundle-hash new-bundle)}
+ (t2/select-one :model/CustomVizPlugin :id id)))))))
+
+(deftest load-rejects-bundle-identifier-mismatch-test
+ (testing "a bundle whose manifest name differs from the imported identifier lands as :error, never :active"
+ (let [ingested (ingested-plugin "calendar-heatmap-viz" (cv.tu/valid-bundle-bytes "thumbs-viz"))]
+ (testing "on insert"
+ (mt/with-model-cleanup [:model/CustomVizPlugin]
+ (serdes/load-one! ingested nil)
+ (is (=? {:identifier "calendar-heatmap-viz"
+ :status :error
+ :error_message #(str/includes? % "does not match")}
+ (t2/select-one :model/CustomVizPlugin :identifier "calendar-heatmap-viz")))))
+ (testing "on update over an active plugin"
+ (mt/with-temp [:model/CustomVizPlugin {id :id} (active-plugin-row "calendar-heatmap-viz"
+ (cv.tu/valid-bundle-bytes "calendar-heatmap-viz"))]
+ (serdes/load-one! ingested (t2/select-one :model/CustomVizPlugin :id id))
+ (is (=? {:id id
+ :status :error
+ :error_message #(str/includes? % "does not match")}
+ (t2/select-one :model/CustomVizPlugin :id id))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/test_util.clj b/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/test_util.clj
index 2d55d3101e3..a791388ea7e 100644
--- a/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/test_util.clj
+++ b/enterprise/backend/test/metabase_enterprise/custom_viz_plugin/test_util.clj
@@ -2,6 +2,8 @@
"Shared helpers for custom-viz-plugin tests: building in-memory tar+gzip
archives that match the format `validate-bundle!` expects."
(:require
+ [buddy.core.codecs :as codecs]
+ [buddy.core.hash :as buddy-hash]
[metabase.util.json :as json])
(:import
(java.io ByteArrayOutputStream)
@@ -43,3 +45,8 @@
(make-tgz-bytes
[["metabase-plugin.json" (json/encode manifest)]
["dist/index.js" "console.log('hi')"]])))
+
+(defn bundle-hash
+ "The sha256 hex of `bundle-bytes`, as stored in `custom_viz_plugin.bundle_hash`."
+ ^String [^bytes bundle-bytes]
+ (-> bundle-bytes buddy-hash/sha256 codecs/bytes->hex))
diff --git a/enterprise/frontend/src/metabase-enterprise/custom_viz/components/EditDevCustomVizForm.tsx b/enterprise/frontend/src/metabase-enterprise/custom_viz/components/EditDevCustomVizForm.tsx
index dd77b4e6782..df8eee1bcc3 100644
--- a/enterprise/frontend/src/metabase-enterprise/custom_viz/components/EditDevCustomVizForm.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/custom_viz/components/EditDevCustomVizForm.tsx
@@ -80,14 +80,6 @@ export function EditDevCustomVizForm({ plugin }: Props) {
.with("error", () => (
<Text c="feedback-negative" fw={700}>{t`Error`}</Text>
))
- .with("pending", () => (
- <Text
- c="text-secondary"
- fw={plugin.enabled ? 700 : undefined}
- >
- {t`Pending`}
- </Text>
- ))
.exhaustive()}
</Group>
</Group>
diff --git a/frontend/src/metabase-types/api/custom-viz-plugin.ts b/frontend/src/metabase-types/api/custom-viz-plugin.ts
index 2ba6eff6d0b..7bffb755344 100644
--- a/frontend/src/metabase-types/api/custom-viz-plugin.ts
+++ b/frontend/src/metabase-types/api/custom-viz-plugin.ts
@@ -15,7 +15,7 @@ export interface CustomVizPlugin {
id: CustomVizPluginId;
display_name: string;
identifier: string;
- status: "pending" | "active" | "error";
+ status: "active" | "error";
enabled: boolean;
icon: string | null;
error_message: string | null;
diff --git a/resources/migrations/063/20261014_custom_viz_plugin_resolve_pending.yaml b/resources/migrations/063/20261014_custom_viz_plugin_resolve_pending.yaml
new file mode 100644
index 00000000000..0b6e62b0f06
--- /dev/null
+++ b/resources/migrations/063/20261014_custom_viz_plugin_resolve_pending.yaml
@@ -0,0 +1,42 @@
+## Serialization import used to insert custom visualization plugins with status "pending" and nothing ever
+## resolved it, so every imported plugin was hidden from the visualization picker and its questions fell back
+## to a default visualization. The import now validates the bundle and sets active/error itself. Rows left in
+## "pending" by the old import carry the bundle, hash and manifest exported from an active plugin, so activate
+## them; a bundle that turns out unusable fails at serve time and can be replaced by an admin.
+##
+## The exception is a row with no bundle at all: a dev-only plugin, served from a dev server URL that the export
+## drops. There is nothing to serve, so mark it as an error the same way the import now does.
+
+databaseChangeLog:
+ - changeSet:
+ id: v63.m2tnuz
+ author: chodorowicz
+ comment: Mark custom_viz_plugin rows left "pending" without a bundle by older serialization imports as errors
+ changes:
+ - update:
+ tableName: custom_viz_plugin
+ columns:
+ - column:
+ name: status
+ value: error
+ - column:
+ name: error_message
+ value: Bundle is empty
+ where: status = 'pending' AND bundle IS NULL
+ rollback: # nothing to restore; "pending" was never a valid status to go back to
+ - empty: {}
+
+ - changeSet:
+ id: v63.18voz9
+ author: chodorowicz
+ comment: Activate custom_viz_plugin rows left "pending" by older serialization imports
+ changes:
+ - update:
+ tableName: custom_viz_plugin
+ columns:
+ - column:
+ name: status
+ value: active
+ where: status = 'pending' AND bundle IS NOT NULL
+ rollback: # nothing to restore; "pending" was never a valid status to go back to
+ - empty: {}
diff --git a/test/metabase/app_db/schema_migrations_test.clj b/test/metabase/app_db/schema_migrations_test.clj
index efd2384df29..56685ac5187 100644
--- a/test/metabase/app_db/schema_migrations_test.clj
+++ b/test/metabase/app_db/schema_migrations_test.clj
@@ -3338,6 +3338,34 @@
(migrate! :down 62)
(is (not (column?))))))))
+(deftest resolve-pending-custom-viz-plugins-test
+ (testing "v63.m2tnuz, v63.18voz9: custom_viz_plugin rows left in 'pending' by older serialization imports are resolved"
+ (impl/test-migrations ["v63.m2tnuz" "v63.18voz9"] [migrate!]
+ (let [new-plugin! (fn [identifier status & {:as extra}]
+ (t2/insert-returning-pk! :custom_viz_plugin (merge {:identifier identifier
+ :display_name identifier
+ :status status
+ :created_at :%now
+ :updated_at :%now}
+ extra)))
+ bundle (.getBytes "pretend tgz bytes" "UTF-8")
+ pending-id (new-plugin! "pending-viz" "pending" :bundle bundle :bundle_hash "abc")
+ dev-id (new-plugin! "pending-dev-viz" "pending")
+ active-id (new-plugin! "active-viz" "active" :bundle bundle :bundle_hash "abc")
+ error-id (new-plugin! "error-viz" "error" :error_message "stale")]
+ (migrate!)
+ (testing "a pending row with a bundle becomes active"
+ (is (=? {:status "active" :error_message nil}
+ (t2/select-one [:custom_viz_plugin :status :error_message] :id pending-id))))
+ (testing "a pending row without a bundle (dev-only plugin) becomes an error"
+ (is (=? {:status "error" :error_message "Bundle is empty"}
+ (t2/select-one [:custom_viz_plugin :status :error_message] :id dev-id))))
+ (testing "already resolved rows are untouched"
+ (is (=? {:status "active" :error_message nil}
+ (t2/select-one [:custom_viz_plugin :status :error_message] :id active-id)))
+ (is (=? {:status "error" :error_message "stale"}
+ (t2/select-one [:custom_viz_plugin :status :error_message] :id error-id))))))))
+
(deftest move-metabot-conversation-state-to-messages-test
(testing "v64.2026-07-06: the legacy conversation state blob moves to the earliest live assistant message, then the column drops"
(impl/test-migrations ["v64.2026-07-06T00:00:01" "v64.2026-07-06T00:00:02"] [migrate!]