Skip to content

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

Fix serialization in the presence of xray of usage analytics (#67848)

Nicola Mometto9f3040507537
diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj
index 1a87c9bc3dd..8bb156623b9 100644
--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj
+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj
@@ -76,19 +76,48 @@
[model-name (serdes/eid->id model-name id)]
target))
-(defn- escape-analysis [{colls "Collection" cards "Card" :as _by-model} nodes]
+(defn- analytics-collection-ids
+ "Returns a set of collection IDs that are in the 'analytics' namespace (internal analytics collections).
+ These collections are intentionally excluded from serialization."
+ []
+ (let [analytics-roots (t2/select :model/Collection {:where [:= :namespace "analytics"]})]
+ (into (set (map :id analytics-roots))
+ (mapcat collection/descendant-ids)
+ analytics-roots)))
+
+(defn- escape-analysis
+ "Analyzes the dependency graph to find cards that are outside the collection set (escapees).
+ Returns a map with:
+ - :reportable-escaped - escapees that should trigger warnings (non-analytics)
+ - :analytics-card-ids - card IDs in analytics collections (should be removed from extraction but not block export)
+
+ Cards that depend on analytics cards are allowed to be exported - the references will be converted
+ to entity_ids during export and resolved on import since analytics cards have stable entity_ids."
+ [{colls "Collection" cards "Card" :as _by-model} nodes]
(log/tracef "Running escape analysis for %d colls and %d cards" (count colls) (count cards))
(when-let [colls (-> colls set not-empty)]
- (let [clause {:where [:or
- [:in :collection_id colls]
- (when (contains? colls nil)
- [:= :collection_id nil])]}
- possible-pks (t2/select-pks-set :model/Card clause)]
- (->> (set/difference (set cards) possible-pks)
- (mapv (fn [id]
- (-> (get nodes ["Card" id])
- (assoc :escapee {:model :model/Card
- :id id}))))))))
+ (let [clause {:where [:or
+ [:in :collection_id colls]
+ (when (contains? colls nil)
+ [:= :collection_id nil])]}
+ possible-pks (t2/select-pks-set :model/Card clause)
+ escaped-card-ids (set/difference (set cards) possible-pks)]
+ (when (seq escaped-card-ids)
+ ;; Analytics cards have stable entity_ids across instances, so cards that depend on them
+ ;; can still be exported: the references will be resolved on import
+ (let [analytics-colls (analytics-collection-ids)
+ escaped-in-analytics (if (seq analytics-colls)
+ (t2/select-pks-set :model/Card {:where [:and
+ [:in :id escaped-card-ids]
+ [:in :collection_id analytics-colls]]})
+ #{})
+ reportable-escaped (set/difference escaped-card-ids escaped-in-analytics)]
+ {:reportable-escaped (->> reportable-escaped
+ (mapv (fn [id]
+ (-> (get nodes ["Card" id])
+ (assoc :escapee {:model :model/Card
+ :id id})))))
+ :analytics-card-ids escaped-in-analytics})))))
(defn- log-escape-report! [escaped]
(let [dashboards (group-by #(get % "Dashboard") escaped)]
@@ -137,9 +166,9 @@
(let [nodes (resolve-targets opts user-id)
;; by model is a map of `{model-name [ids ...]}`
by-model (u/group-by first second (keys nodes))
- escaped (escape-analysis by-model nodes)]
- (if (seq escaped)
- (log-escape-report! escaped)
+ {:keys [reportable-escaped analytics-card-ids]} (escape-analysis by-model nodes)]
+ (if (seq reportable-escaped)
+ (log-escape-report! reportable-escaped)
(let [models (model-set opts)
coll-set (get by-model "Collection")
;; When targets are specified, also include Tables found via descendants
@@ -147,7 +176,11 @@
targeted-tables (when (seq targets) (get by-model "Table"))
by-model (cond-> (select-keys by-model models)
;; Add Tables back if they were found in descendants
- (seq targeted-tables) (assoc "Table" targeted-tables))
+ (seq targeted-tables) (assoc "Table" targeted-tables)
+ ;; Remove analytics cards from extraction - they have stable entity_ids across instances
+ ;; so cards that reference them can still be exported and imported correctly
+ (and analytics-card-ids (contains? by-model "Card"))
+ (update "Card" (fn [ids] (vec (remove analytics-card-ids ids)))))
extract-by-ids (fn [[model ids]]
(serdes/extract-all model (merge opts {:collection-set coll-set
:where [:in :id ids]})))
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj
index 77b968eb7e9..e27041bc332 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj
@@ -1773,6 +1773,40 @@
:no-data-model true})]
(is (= #{} (ids-by-model "Collection" ser)))))))
+(deftest xray-of-analytics-model-export-test
+ (testing "X-rays of analytics models can be exported without errors"
+ (mt/with-empty-h2-app-db!
+ (mbc/ensure-audit-db-installed!)
+ (testing "sanity check that the analytics content exists"
+ (is (some? (audit/default-audit-collection)))
+ ;; Find the Query log model (entity_id: QOtZaiTLf2FDD4AT6Oinb)
+ (let [query-log-card (t2/select-one :model/Card :entity_id "QOtZaiTLf2FDD4AT6Oinb")]
+ (is (some? query-log-card) "Query log model should exist in analytics")
+ (when query-log-card
+ ;; Create a card that depends on the analytics model (simulating an x-ray)
+ (mt/with-temp [:model/Collection {coll-id :id coll-eid :entity_id} {:name "Test Collection"}
+ :model/Card {_xray-card-id :id xray-card-eid :entity_id}
+ {:name "X-ray of Query log"
+ :collection_id coll-id
+ :database_id (:database_id query-log-card)
+ :dataset_query {:type :query
+ :database (:database_id query-log-card)
+ :query {:source-table (str "card__" (:id query-log-card))}}}]
+ (testing "Export should succeed without warnings about analytics dependencies"
+ (mt/with-log-messages-for-level [messages [metabase-enterprise :warn]]
+ (let [ser (extract/extract {:targets [["Collection" coll-id]]
+ :no-settings true
+ :no-data-model true})]
+ (is (contains? (ids-by-model "Card" ser) xray-card-eid)
+ "X-ray card should be exported")
+ (is (contains? (ids-by-model "Collection" ser) coll-eid)
+ "Collection should be exported")
+ (is (not (contains? (ids-by-model "Card" ser) "QOtZaiTLf2FDD4AT6Oinb"))
+ "Analytics model should NOT be exported")
+ (let [warn-msgs (into #{} (map :message) (messages))]
+ (is (not (some #(str/starts-with? % "Failed to export") warn-msgs))
+ (str "Should not have export warnings, got: " warn-msgs)))))))))))))
+
(deftest entity-id-in-targets-test
(mt/with-temp [:model/Collection c {:name "Top-Level Collection"}]
(testing "Conversion from eid to id works"