Skip to content

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

Fix model filter dropdown lost on serdes sync (GHY-3946) (#76349)

Eric Normanda242d4378890
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
index 9d4bfe34b25..cca41b76fbd 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
@@ -321,6 +321,57 @@
:database (:id @db1d)}
(:dataset_query @card1d))))))))))
+(deftest model-result-metadata-id-roundtrips-test
+ (testing "GHY-3946: deserializing a model re-derives its result_metadata :id from the query when the underlying Fields are already present, so a filter mapped to the model column keeps its value dropdown; overrides are preserved"
+ (let [serialized (atom nil)]
+ (ts/with-dbs [source-db dest-db]
+ (testing "serialize a model whose CATEGORY column carries a user override"
+ (ts/with-db source-db
+ (let [coll (ts/create! :model/Collection :name "models")
+ db (ts/create! :model/Database :name "my-db")
+ table (ts/create! :model/Table :name "products" :db_id (:id db))
+ field (ts/create! :model/Field :name "category" :table_id (:id table)
+ :base_type :type/Text :semantic_type :type/Category
+ :has_field_values :list)]
+ (ts/create! :model/Card
+ :type :model
+ :name "Products Model"
+ :collection_id (:id coll)
+ :database_id (:id db)
+ :table_id (:id table)
+ :query_type :query
+ :dataset_query {:type :query
+ :query {:source-table (:id table)}
+ :database (:id db)}
+ :result_metadata [{:name "category" :display_name "Custom Category"
+ :base_type :type/Text :semantic_type :type/Category
+ :id (:id field) :field_ref [:field (:id field) nil]
+ :table_id (:id table)}])
+ (reset! serialized (into [] (serdes.extract/extract {}))))))
+ (testing "the serialized model column keeps its user override"
+ (let [card (first (filter #(= "Products Model" (:name %)) (by-model @serialized "Card")))
+ cat (first (filter #(= "category" (:name %)) (:result_metadata card)))]
+ (is (some? cat))
+ (is (= "Custom Category" (:display_name cat)))))
+ (testing "loading re-derives the local :id when the data model already exists on the destination"
+ (ts/with-db dest-db
+ ;; the data model is already present on the destination (and at different IDs than the source)
+ (ts/create! :model/Database :name "decoy")
+ (let [db (ts/create! :model/Database :name "my-db")
+ table (ts/create! :model/Table :name "products" :db_id (:id db))
+ field (ts/create! :model/Field :name "category" :table_id (:id table)
+ :base_type :type/Text :semantic_type :type/Category
+ :has_field_values :list)]
+ (serdes.load/load-metabase! (ingestion-in-memory @serialized))
+ (let [dest-card (t2/select-one :model/Card :name "Products Model")
+ cat (first (filter #(= "category" (:name %)) (:result_metadata dest-card)))]
+ (is (number? (:id cat))
+ "model column kept a numeric :id link after the round-trip")
+ (is (= (:id field) (:id cat))
+ "model column :id points at the dest Field id")
+ (is (= "Custom Category" (:display_name cat))
+ "user override on the column survived the round-trip")))))))))
+
(deftest card-with-unexported-table-and-field-test
(testing "a Card referencing a Table/Field absent from the bundle still loads by synthesizing inactive rows"
(let [serialized (atom nil)]
diff --git a/src/metabase/queries/models/card/metadata.clj b/src/metabase/queries/models/card/metadata.clj
index 8fc12faa0af..7d1139e4155 100644
--- a/src/metabase/queries/models/card/metadata.clj
+++ b/src/metabase/queries/models/card/metadata.clj
@@ -8,6 +8,7 @@
[metabase.lib.core :as lib]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.lib.schema.metadata :as lib.schema.metadata]
+ [metabase.models.interface :as mi]
[metabase.query-processor.metadata :as qp.metadata]
[metabase.query-processor.preprocess :as qp.preprocess]
[metabase.query-processor.schema :as qp.schema]
@@ -224,6 +225,9 @@ saved later when it is ready."
(log/debug "Not inferring result metadata for Card: query was not updated")
card)
+ (and mi/*deserializing?* (= (:type card) :model) query (seq metadata) (not-any? :id metadata))
+ (assoc card :result_metadata (or (infer-metadata-with-model-overrides query card) metadata))
+
;; passing in metadata => use that metadata, but replace any placeholder idents in it.
(or (and (not-empty changes) (contains? changes :result_metadata))
(and (empty? changes) metadata))