diff --git a/src/metabase/models/card.clj b/src/metabase/models/card.clj index 5a89912e54c714602e48a21d86f190c5622bf15c..4c5c7b7fe9a0605a4cf89a57ab78678d19207076 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 429a3081594b52567705843b2ca9c95a22840922..82ad3caa0d641b6ccd118980575280626850de90 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 ------------------------------------------