The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Fix check for card changes (#64374)
John Swanson11c56c57fe70
diff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.cljindex 24060643e83..bffd016ee51 100644--- a/src/metabase/queries/models/card.clj+++ b/src/metabase/queries/models/card.clj@@ -2,9 +2,7 @@"Underlying DB model for what is now most commonly referred to as a 'Question' in most user-facing situations. Cardis a historical name, but is the same thing; both terms are used interchangeably in the backend codebase."(:require- [clojure.data :as data][clojure.set :as set]- [clojure.walk :as walk][honey.sql.helpers :as sql.helpers][medley.core :as m][metabase.analytics.core :as analytics]@@ -981,27 +979,41 @@(-> card :moderation_reviews first :status #{"verified"} boolean))(defn- changed?- "Return whether there were any changes in the objects at the keys for `consider`.-- returns false because changes to collection_id are ignored:- (changed? #{:description}- {:collection_id 1 :description \"foo\"}- {:collection_id 2 :description \"foo\"})-- returns true:- (changed? #{:description}- {:collection_id 1 :description \"foo\"}- {:collection_id 2 :description \"diff\"})"- [consider card-before updates]- ;; have to ignore keyword vs strings over api. `{:type :query}` vs `{:type "query"}`- (let [prepare (fn prepare [card] (walk/prewalk (fn [x] (if (keyword? x)- (name x)- x))- card))- before (prepare (select-keys card-before consider))- after (prepare (select-keys updates consider))- [_ changes-in-after] (data/diff before after)]- (boolean (seq changes-in-after))))+ "Return whether there were any changes in the objects. ONLY keys in `after` are compared. If any are missing from+ `before` an exception will be thrown.++ returns false, `description` has not changed:+ (changed? {:collection_id 1 :description \"foo\"}+ {:description \"foo\"})++ returns true, `description` has changed:+ (changed? {:collection_id 1 :description \"foo\"}+ {:description \"diff\"})++ throws an exception, `before` is missing keys from `after`:+ (changed? {:collection_id 1}+ {:collection_id 1 :description \"foobar\"})++ For keys that will be transformed before we insert them in the database, automatically runs+ `(out-transform (in-transform ...))` before comparing to make sure we are comparing the canonical form."+ [card-before updates]+ ;; normalize the query before comparison+ (let [transforms (t2/transforms :model/Card)+ after (->> updates+ (map (fn [[k v]]+ (let [tvi (get-in transforms [k :in] identity)+ tvo (get-in transforms [k :out] identity)+ tv (tvo (tvi v))]+ [k tv])))+ (into {}))+ before (select-keys card-before (keys after))]+ (when-not (set/subset? (set (keys after))+ (set (keys before)))+ (throw (ex-info "`before-card` card is missing keys from `updates`"+ {:missing-keys (apply disj+ (set (keys after))+ (set (keys before)))})))+ (boolean (some #(do (api/column-will-change? % before after)) (keys after)))))(def ^:private card-compare-keys"When comparing a card to possibly unverify, only consider these keys as changing something 'important' about the@@ -1117,29 +1129,30 @@(api/maybe-reconcile-collection-position! card-before-update card-updates)(autoplace-or-remove-dashcards-for-card! card-before-update card-updates delete-old-dashcards?)- (assert-is-valid-dashboard-internal-update card-updates card-before-update)-- (when (and (card-is-verified? card-before-update)- (changed? card-compare-keys card-before-update card-updates))- ;; this is an enterprise feature but we don't care if enterprise is enabled here. If there is a review we need- ;; to remove it regardless if enterprise edition is present at the moment.- (moderation/create-review! {:moderated_item_id (:id card-before-update)- :moderated_item_type "card"- :moderator_id (:id actor)- :status nil- :text (tru "Unverified due to edit")}))- ;; Invalidate the cache for card- (cache/invalidate-config! {:questions [(:id card-before-update)]- :with-overrides? true})- ;; ok, now save the Card- (t2/update! :model/Card (:id card-before-update)- ;; `collection_id` and `description` can be `nil` (in order to unset them).- ;; Other values should only be modified if they're passed in as non-nil- (u/select-keys-when card-updates- :present #{:collection_id :collection_position :description :cache_ttl :archived_directly :dashboard_id :document_id :embedding_type}- :non-nil #{:dataset_query :display :name :visualization_settings :archived- :enable_embedding :type :parameters :parameter_mappings :embedding_params- :result_metadata :collection_preview :verified-result-metadata?}))+ (let [updated-fields (u/select-keys-when card-updates+ ;; `collection_id` and `description` can be `nil` (in order to unset them).+ ;; Other values should only be modified if they're passed in as non-nil+ :present #{:collection_id :collection_position :description :cache_ttl :archived_directly :dashboard_id :document_id :embedding_type}+ :non-nil #{:dataset_query :display :name :visualization_settings :archived+ :enable_embedding :type :parameters :parameter_mappings :embedding_params+ :result_metadata :collection_preview :verified-result-metadata?})]++ (assert-is-valid-dashboard-internal-update card-updates card-before-update)++ (when (and (card-is-verified? card-before-update)+ (changed? card-before-update (select-keys updated-fields card-compare-keys)))+ ;; this is an enterprise feature but we don't care if enterprise is enabled here. If there is a review we need+ ;; to remove it regardless if enterprise edition is present at the moment.+ (moderation/create-review! {:moderated_item_id (:id card-before-update)+ :moderated_item_type "card"+ :moderator_id (:id actor)+ :status nil+ :text (tru "Unverified due to edit")}))+ ;; Invalidate the cache for card+ (cache/invalidate-config! {:questions [(:id card-before-update)]+ :with-overrides? true})+ ;; ok, now save the Card+ (t2/update! :model/Card (:id card-before-update) updated-fields));; ok, now update dependent dashcard parameters(try(update-associated-parameters! card-before-update card-updates)diff --git a/test/metabase/queries/models/card_test.clj b/test/metabase/queries/models/card_test.cljindex bc8d040b233..423e1240d18 100644--- a/test/metabase/queries/models/card_test.clj+++ b/test/metabase/queries/models/card_test.clj@@ -721,24 +721,13 @@(t2/select-one-fn :metabase_version :model/Card :id (:id card)))))))(deftest ^:parallel changed?-test- (letfn [(changed? [before after]- (#'card/changed? @#'card/card-compare-keys before after))]- (testing "Ignores keyword/string"- (is (false? (changed? {:dataset_query {:type :query}} {:dataset_query {:type "query"}}))))- (testing "Ignores properties not in `api.card/card-compare-keys"- (is (false? (changed? {:collection_id 1- :collection_position 0}- {:collection_id 2- :collection_position 1}))))- (testing "Sees changes"- (is (true? (changed? {:dataset_query {:type :query}}- {:dataset_query {:type :query- :query {}}})))- (testing "But only when they are different in the after, not just omitted"- (is (false? (changed? {:dataset_query {} :collection_id 1}- {:collection_id 1})))- (is (true? (changed? {:dataset_query {} :collection_id 1}- {:dataset_query nil :collection_id 1})))))))+ (let [changed? (fn [a b] (#'card/changed? a b))]+ (is (changed? {:a "a"} {:a "b"}))+ (is (not (changed? {:a "a" :b "b"} {:b "b"})))+ (is (not (changed? {:a "a"} {})))+ (is (not (changed? {} {})))+ (is (thrown? clojure.lang.ExceptionInfo (changed? {:a "a"} {:b "b"})))+ (is (thrown? clojure.lang.ExceptionInfo (changed? {:a "a"} {:a "a" :b "b"})))))(deftest hydrate-dashboard-count-test(testing "cards associated with more than 1 dashboard"