Skip to content

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

GHY-3728 Strip transient lib markers from serdes MBQL export (#74980)

Eric Normand7cf846f5d274
diff --git a/src/metabase/models/serialization.clj b/src/metabase/models/serialization.clj
index 45dad90cadc..b98457bcbc7 100644
--- a/src/metabase/models/serialization.clj
+++ b/src/metabase/models/serialization.clj
@@ -1291,13 +1291,16 @@
"Given an MBQL expression, convert it to an EDN structure and turn the non-portable Database, Table and Field IDs
inside it into portable references."
[x]
- ;; if required UUIDs are already calculated don't recalculate when we recurse.
- (binding [*required-lib-uuids-for-export* (or *required-lib-uuids-for-export* (collect-required-lib-uuids x))]
- (cond
- (mbql-ref? x) (export-mbql-ref x)
- (sequential? x) (mapv export-mbql x)
- (map? x) (export-mbql-map x)
- :else x)))
+ (let [x (cond-> x
+ (and (map? x) (= :mbql/query (:lib/type x)))
+ lib/prepare-for-serialization)]
+ ;; if required UUIDs are already calculated don't recalculate when we recurse.
+ (binding [*required-lib-uuids-for-export* (or *required-lib-uuids-for-export* (collect-required-lib-uuids x))]
+ (cond
+ (mbql-ref? x) (export-mbql-ref x)
+ (sequential? x) (mapv export-mbql x)
+ (map? x) (export-mbql-map x)
+ :else x))))
(defn- portable-id?
"True if the provided string is either an Entity ID or identity-hash string."
diff --git a/test/metabase/models/serialization_test.clj b/test/metabase/models/serialization_test.clj
index daa3831ac87..b574e15f6d6 100644
--- a/test/metabase/models/serialization_test.clj
+++ b/test/metabase/models/serialization_test.clj
@@ -44,6 +44,42 @@
(#'serdes/export-mbql-ref [:field {:lib/uuid (fake-uuid 1)}
"name"]))))))
+(defn- transient-lib-markers
+ "All `:lib`/`:lib.convert` namespaced keys anywhere in `query`, except the structural `:lib/type`."
+ [query]
+ (->> (tree-seq coll? seq query)
+ (filter qualified-keyword?)
+ (filter #(#{"lib" "lib.convert"} (namespace %)))
+ (remove #{:lib/type})
+ set))
+
+(deftest ^:parallel strip-transient-markers-on-export-test
+ (testing (str "Transient runtime markers that get added when a legacy query is normalized to MBQL 5 on read "
+ "(`:lib.convert/converted?` and `:lib/transformation-added-base-type`) must not leak into serdes "
+ "output, so a query exports identically whether it happened to be stored as legacy MBQL or MBQL 5 "
+ "in the app DB (GHY-3728).")
+ (let [legacy-query {:database (meta/id)
+ :type :query
+ :query {:source-table (meta/id :venues)
+ :filter [:> [:field (meta/id :venues :price) nil] 1]}}
+ ;; how a card is read from the app DB when its `dataset_query` is still stored as legacy MBQL
+ from-legacy (lib/query meta/metadata-provider legacy-query)
+ ;; how the same card is read after being (re-)saved, i.e. persisted to the app DB as MBQL 5
+ from-mbql5 (lib/prepare-for-serialization from-legacy)]
+ (testing "sanity check: a query freshly converted from legacy carries the transient markers"
+ (is (contains? (transient-lib-markers from-legacy) :lib.convert/converted?))
+ (is (contains? (transient-lib-markers from-legacy) :lib/transformation-added-base-type)))
+ (binding [serdes/*export-database-fk* (constantly "DATABASE")
+ serdes/*export-table-fk* (constantly ["DATABASE" "SCHEMA" "TABLE"])
+ serdes/*export-field-fk* (constantly ["DATABASE" "SCHEMA" "TABLE" "FIELD"])]
+ (testing "the markers are stripped on export"
+ (is (not (contains? (transient-lib-markers (serdes/export-mbql from-legacy)) :lib.convert/converted?)))
+ (is (not (contains? (transient-lib-markers (serdes/export-mbql from-legacy))
+ :lib/transformation-added-base-type))))
+ (testing "export is identical regardless of whether the query was stored as legacy MBQL or MBQL 5"
+ (is (= (serdes/export-mbql from-mbql5)
+ (serdes/export-mbql from-legacy))))))))
+
(deftest ^:parallel drop-mbql-5-uuids-on-export-test-2
(let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :venues))
(lib/filter (lib/= (meta/field-metadata :venues :id) 1))