From d8e804c0acf1ef0e7a3bf295e3507b7c7ccbaad3 Mon Sep 17 00:00:00 2001 From: "metabase-bot[bot]" <109303359+metabase-bot[bot]@users.noreply.github.com> Date: Tue, 9 Apr 2024 10:49:17 +0000 Subject: [PATCH] [backport] serialize result metadata for any card (#41131) (#41160) Co-authored-by: Alexander Solovyov <alexander@solovyov.net> --- src/metabase/models/card.clj | 6 +++--- test/metabase/models/card_test.clj | 31 +++++++++++++++--------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/metabase/models/card.clj b/src/metabase/models/card.clj index 5a89912e54c..4c5c7b7fe9a 100644 --- a/src/metabase/models/card.clj +++ b/src/metabase/models/card.clj @@ -887,8 +887,8 @@ saved later when it is ready." (defmethod serdes/extract-query "Card" [_ opts] (serdes/extract-query-collections Card opts)) -(defn- export-result-metadata [card metadata] - (when (and metadata (model? card)) +(defn- export-result-metadata [metadata] + (when metadata (for [m metadata] (-> (dissoc m :fingerprint) (m/update-existing :table_id serdes/*export-table-fk*) @@ -927,7 +927,7 @@ saved later when it is ready." (update :parameters serdes/export-parameters) (update :parameter_mappings serdes/export-parameter-mappings) (update :visualization_settings serdes/export-visualization-settings) - (update :result_metadata (partial export-result-metadata card))) + (update :result_metadata export-result-metadata)) (catch Exception e (throw (ex-info (format "Failed to export Card: %s" (ex-message e)) {:card card} e))))) diff --git a/test/metabase/models/card_test.clj b/test/metabase/models/card_test.clj index 429a3081594..82ad3caa0d6 100644 --- a/test/metabase/models/card_test.clj +++ b/test/metabase/models/card_test.clj @@ -639,22 +639,21 @@ (deftest extract-test (let [metadata (qp/query->expected-cols (mt/mbql-query venues)) query (mt/mbql-query venues)] - (testing "normal cards omit result_metadata" - (t2.with-temp/with-temp [:model/Card {card-id :id} {:dataset_query query - :result_metadata metadata}] - (let [extracted (serdes/extract-one "Card" nil (t2/select-one :model/Card :id card-id))] - (is (not (:dataset extracted))) - (is (nil? (:result_metadata extracted)))))) - (testing "dataset cards (models) retain result_metadata" - (t2.with-temp/with-temp [:model/Card {card-id :id} {:dataset true - :dataset_query query - :result_metadata metadata}] - (let [extracted (serdes/extract-one "Card" nil (t2/select-one :model/Card :id card-id))] - (is (:dataset extracted)) - (is (string? (:display_name (first (:result_metadata extracted))))) - ;; this is a quick comparison, since the actual stored metadata is quite complex - (is (= (map :display_name metadata) - (map :display_name (:result_metadata extracted))))))))) + (testing "every card retains result_metadata" + (t2.with-temp/with-temp [:model/Card {card1-id :id} {:dataset_query query + :result_metadata metadata} + :model/Card {card2-id :id} {:type "model" + :dataset_query query + :result_metadata metadata}] + (doseq [card-id [card1-id card2-id]] + (let [extracted (serdes/extract-one "Card" nil (t2/select-one :model/Card :id card-id))] + ;; card2 is model, but card1 is not + (is (= (= card-id card2-id) + (= "model" (:type extracted)))) + (is (string? (:display_name (first (:result_metadata extracted))))) + ;; this is a quick comparison, since the actual stored metadata is quite complex + (is (= (map :display_name metadata) + (map :display_name (:result_metadata extracted)))))))))) ;;; ------------------------------------------ Viz Settings Tests ------------------------------------------ -- GitLab