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
Refresh child card result metadata on card edit (#68043)
William7976ad4f479a
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/api.clj b/enterprise/backend/src/metabase_enterprise/dependencies/api.cljindex 4a920f25cc8..a8905195f14 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/api.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/api.clj@@ -177,12 +177,10 @@:model/Document (select-keys entity [:id :name])entity))-(mr/def ::entity-id pos-int?)-(mr/def ::usages[:map-of[:enum :table :snippet :transform :dashboard :document :sandbox :segment :question :model :metric :measure]- ::entity-id])+ ::deps.dependency-types/entity-id])(mr/def ::base-entity[:map@@ -511,9 +509,9 @@[:nodes [:sequential ::entity]][:edges [:sequential [:map[:from_entity_type ::deps.dependency-types/dependency-types]- [:from_entity_id ::entity-id]+ [:from_entity_id ::deps.dependency-types/entity-id][:to_entity_type ::deps.dependency-types/dependency-types]- [:to_entity_id ::entity-id]]]]])+ [:to_entity_id ::deps.dependency-types/entity-id]]]]])(api.macros/defendpoint :get "/graph" :- ::graph-response"This endpoint takes an :id and a supported entity :type, and returns a graph of all its upstream dependencies.diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/core.clj b/enterprise/backend/src/metabase_enterprise/dependencies/core.cljindex c8c874d5a0c..b47ba045d15 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/core.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/core.clj@@ -40,7 +40,10 @@;; Reusing the cache with different overrides breaks the caching of [[lib.metadata/card]] calls.(lib.metadata.protocols/clear-cache! base-provider)(let [dependents (or dependents (deps.graph/transitive-dependents graph updated-entities))]- (deps.provider/override-metadata-provider base-provider updated-entities dependents))))+ (deps.provider/override-metadata-provider+ {:base-provider base-provider+ :updated-entities updated-entities+ :dependent-ids dependents}))))(mu/defn- check-query-soundness :- [:map-of ::entity-type [:map-of :int [:set [:ref ::lib.schema.validate/error]]]]"Given a `MetadataProvider` as returned by [[metadata-provider]], scan all its updated entities and their dependentsdiff --git a/enterprise/backend/src/metabase_enterprise/dependencies/dependency_types.clj b/enterprise/backend/src/metabase_enterprise/dependencies/dependency_types.cljindex bbf46871be8..458a06534de 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/dependency_types.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/dependency_types.clj@@ -30,3 +30,5 @@(mr/def ::dependency-types(ms/enum-decode-keyword dependency-types))++(mr/def ::entity-id pos-int?)diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/init.clj b/enterprise/backend/src/metabase_enterprise/dependencies/init.cljindex a3a702e187f..0980db3bfd7 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/init.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/init.clj@@ -1,6 +1,7 @@(ns metabase-enterprise.dependencies.init(:require[metabase-enterprise.dependencies.events]+ [metabase-enterprise.dependencies.metadata-update][metabase-enterprise.dependencies.schema][metabase-enterprise.dependencies.settings][metabase-enterprise.dependencies.task.backfill]diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/metadata_provider.clj b/enterprise/backend/src/metabase_enterprise/dependencies/metadata_provider.cljindex 9077ef739be..ea44af6853b 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/metadata_provider.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/metadata_provider.clj@@ -69,7 +69,7 @@(declare all-overrides setup-transform! setup-transforms!)-(deftype OverridingMetadataProvider [delegate *overrides]+(deftype OverridingMetadataProvider [delegate *overrides returned-columns-fn]lib.metadata.protocols/MetadataProvider(database [_this] (lib.metadata.protocols/database delegate))(metadatas [this metadata-spec]@@ -112,19 +112,25 @@(assoc-in m ks v))% kvs)))-(defn- returned-columns [mp queryable]- (deps.analysis/returned-columns (:engine (lib.metadata/database mp))- (lib/query mp queryable)))+(defn- returned-columns [^OverridingMetadataProvider mp queryable]+ (let [returned-columns-fn (.returned-columns-fn mp)]+ (or (and returned-columns-fn (returned-columns-fn mp queryable))+ (deps.analysis/returned-columns (:engine (lib.metadata/database mp))+ (lib/query mp queryable)))))(defmethod add-override :card [^OverridingMetadataProvider mp _entity-type id updates](with-overrides mp- ;; If the `updates` contain `:result-metadata`, we want to use that. However, any `:result-metadata` from the- ;; inner-mp should be ignored.- {[:metadata/card id] (delay (merge (when id- (-> (inner-mp mp)- (lib.metadata/card id)- (dissoc :result-metadata)))- updates))+ ;; If the `updates` contain `:result-metadata`, we want to use that. Similarly, if the user provides a way to+ ;; calculate `result-metadata`, we should use that function. However, any `:result-metadata` from the inner-mp+ ;; should be ignored.+ {[:metadata/card id] (delay (let [temp (merge (when id+ (-> (inner-mp mp)+ (lib.metadata/card id)))+ updates)+ result-metadata (or (:result-metadata updates)+ (and (.returned-columns-fn mp)+ (returned-columns mp temp)))]+ (assoc temp :result-metadata result-metadata)));; This uses the outer OMP and so the overrides are visible![::card-columns id] (delay (returned-columns mp (lib.metadata/card mp id)))}))@@ -265,19 +271,18 @@(defn override-metadata-provider"Given an underlying `MetadataProvider`, wraps it to support in-memory overrides of cards, fields, etc.- Important note: all overrides must be added first, before this is used as a `MetadataProvider`.- The easiest way to do that is to call the 3-arity, which provides the overrides up front."- ([metadata-provider]- (->OverridingMetadataProvider metadata-provider (atom {})))-- ([metadata-provider updated-entities dependent-ids]- (let [^OverridingMetadataProvider omp (override-metadata-provider metadata-provider)]- (doseq [[entity-type updates] updated-entities- updated-entity updates]- (add-override omp entity-type (:id updated-entity) updated-entity))- (doseq [[entity-type dependents] dependent-ids- :let [updated (into #{} (map :id) (get updated-entities entity-type))]- id dependents- :when (not (updated id))]- (add-override omp entity-type id nil))- omp)))+ Important note: all overrides must be added first, before this is used as a `MetadataProvider`. The easiest way to+ do that is to pass in updated entities and/or dependent ids, which will be immediately added as overrides."+ [{:keys [base-provider updated-entities dependent-ids returned-columns-fn]}]+ (let [^OverridingMetadataProvider omp (->OverridingMetadataProvider base-provider+ (atom {})+ returned-columns-fn)]+ (doseq [[entity-type updates] updated-entities+ updated-entity updates]+ (add-override omp entity-type (:id updated-entity) updated-entity))+ (doseq [[entity-type dependents] dependent-ids+ :let [updated (into #{} (map :id) (get updated-entities entity-type))]+ id dependents+ :when (not (updated id))]+ (add-override omp entity-type id nil))+ omp))diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/metadata_update.clj b/enterprise/backend/src/metabase_enterprise/dependencies/metadata_update.cljnew file mode 100644index 00000000000..9e811d371ed--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/dependencies/metadata_update.clj@@ -0,0 +1,202 @@+(ns metabase-enterprise.dependencies.metadata-update+ (:require+ [clojure.core.cache :as cache]+ [medley.core :as m]+ [metabase-enterprise.dependencies.dependency-types :as deps.dependency-types]+ [metabase-enterprise.dependencies.metadata-provider :as deps.metadata-provider]+ [metabase-enterprise.dependencies.models.dependency :as models.dependency]+ [metabase.events.core :as events]+ [metabase.graph.core :as graph]+ [metabase.lib-be.core :as lib-be]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.lib.schema.metadata :as lib.schema.metadata]+ [metabase.premium-features.core :as premium-features]+ [metabase.queries.core :as queries]+ [metabase.util :as u]+ [metabase.util.malli :as mu]+ [metabase.util.malli.registry :as mr]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(defn- mbql-graph+ "Returns a graph that is limited to sandboxes, segments, measures, and mbql cards.++ All other types of nodes are ignored and are neither included in the graph or traversed to find transitive+ dependents."+ [mp]+ (-> (models.dependency/filtered-graph-dependents+ nil+ (fn [type-field _id-field]+ [:or+ [:= type-field "card"]+ [:= type-field "sandbox"]+ [:= type-field "segment"]+ [:= type-field "measure"]]))+ (graph/filtered-graph (fn [[type id]]+ (or (not= type :card)+ (-> (lib.metadata/card mp id)+ :dataset-query+ lib/any-native-stage?+ not))))+ graph/cached-graph))++(mu/defn- dependent-mbql-cards :- [:sequential ::lib.schema.id/card]+ "Returns a list of all card dependencies in the transitive children of `[start-type start-id]` using `graph`."+ [graph+ start-type :- ::deps.dependency-types/dependency-types+ start-id :- ::deps.dependency-types/entity-id]+ (let [start [start-type start-id]]+ (->> (graph/transitive graph [[start-type start-id]])+ (keep (fn [[node-type node-id :as node]]+ (when (and (= node-type :card)+ (not= node start))+ node-id))))))++(mr/def ::column-metadata-edits [:map-of :keyword :any])+(mr/def ::card-metadata-edits [:map-of :string ::column-metadata-edits])+(mr/def ::card-list-metadata-edits [:map-of ::lib.schema.id/card ::card-metadata-edits])++(mu/defn- column-metadata-edits :- [:maybe ::column-metadata-edits]+ [editable-keys :- [:sequential :keyword]+ original :- [:maybe :metabase.query-processor.middleware.annotate/qp-results-cased-col]+ calculated :- [:maybe :metabase.query-processor.middleware.annotate/qp-results-cased-col]]+ (when (and original calculated)+ (-> (into {}+ (keep (fn [key]+ (let [calculated-value (calculated key)+ original-value (original key)]+ (when (and calculated-value original-value+ (not= calculated-value original-value))+ [key original-value]))))+ editable-keys)+ not-empty)))++(mu/defn- card-metadata-edits :- [:maybe ::card-metadata-edits]+ [mp :- ::lib.schema.metadata/metadata-providerable+ card-id :- ::lib.schema.id/card]+ (let [card (lib.metadata/card mp card-id)+ original-metadata (:result-metadata card)+ ;; Replace the metadata provider here to ensure that it is using the correct one.+ calculated-metadata (->> card :dataset-query (lib/query mp) queries/infer-metadata)+ calculated-by-name (m/index-by :lib/deduplicated-name calculated-metadata)+ editable-keys (->> (lib/model-preserved-keys false)+ (map u/->snake_case_en))]+ (-> (into {}+ (keep (fn [{name :lib/deduplicated-name :as original}]+ (let [calculated (calculated-by-name name)]+ (when-let [edits (column-metadata-edits editable-keys original calculated)]+ [name edits]))))+ original-metadata)+ not-empty)))++(mu/defn- card-list-metadata-edits :- ::card-list-metadata-edits+ "Takes a metadata provider and a list of card ids, and finds the differences between the actual metadata in the db+ and the computed metadata we would expect to see."+ [mp :- ::lib.schema.metadata/metadata-providerable+ card-ids :- [:sequential ::lib.schema.id/card]]+ (into {}+ (keep (fn [card-id]+ (when-let [edit-map (card-metadata-edits mp card-id)]+ [card-id edit-map])))+ card-ids))++(mu/defn- updated-metadata+ "Given a metadata provider, card, and metadata edit map, calculate new metadata with the appropriate metadata edits.++ Takes a metadata provider because the queries' existing metadata provider may not be the one we want to use. The+ parent function should be ensuring that the passed-in metadata provider is correct."+ [mp :- ::lib.schema.metadata/metadata-providerable+ card :- ::lib.schema.metadata/card+ metadata-edits :- ::card-list-metadata-edits]+ (let [new-metadata (->> card :dataset-query (lib/query mp) queries/infer-metadata)+ card-edits (metadata-edits (:id card))]+ (if (= (:type card) :model)+ (for [{name :lib/deduplicated-name :as column} new-metadata]+ (merge column (when card-edits (card-edits name))))+ new-metadata)))++(mu/defn- fresh-mp :- ::lib.schema.metadata/metadata-provider+ "Creates a 'fresh' metadata provider for the given database id with no existing cache."+ [db-id :- ::lib.schema.id/database]+ (lib-be/with-existing-metadata-provider-cache (atom (cache/basic-cache-factory {}))+ (lib-be/application-database-metadata-provider db-id)))++(mu/defn- update-dependent-mbql-cards-metadata!+ [original-mp :- ::lib.schema.metadata/metadata-providerable+ start-type :- ::deps.dependency-types/dependency-types+ start-id :- ::deps.dependency-types/entity-id+ previous-object :- :any+ metadata-type :- :keyword]+ ;; Notes on metadata providers:+ ;;+ ;; The key thing here is that lib.metadata/card caches very aggressively (as of 2026/01, at least). Instead of+ ;; going through the normal cache mechanism, it grabs the card normally, normalizes the query, and then caches the+ ;; result using a special key. The next time it gets called, it will check for that key first and never actually+ ;; hit the standard card fetch flow at all. Meanwhile, override metadata providers delegate all caching to their+ ;; underlying metadata provider. As a result, an override metadata provider and its underlying metadata provider+ ;; effectively use the same card cache. Overriding the card doesn't affect that cache, because lib.metadata/card+ ;; will effectively ask the underlying metadata provider for the card (using that special key) without ever talking+ ;; to the override metadata provider. Both the override metadata provider and the underlying metadata provider will+ ;; always return whichever version of that card was requested and cached first.+ ;;+ ;; The solution here is three different metadata providers. First, we have the original metadata provider (or+ ;; really, metadata-providerable -- a query is fine here). This doesn't have any special overrides and is used to+ ;; fetch the current state of the world before any updates that happen in this function.+ ;;+ ;; Second, we have a "pre-update" metadata provider. This is an override metadata provider based on a "fresh"+ ;; metadata provider with no cached data. This has overrides intended to match the state of the world before+ ;; whatever update triggered this function call.+ ;;+ ;; Third, we have a "updated" metadata provider. This another override metadata provider based on a fresh metadata+ ;; provider, but this one has no initial overrides. Instead, it is set to use our returned-columns logic and asked+ ;; to re-calculate all of the child cards' result metadata. In this case, caching actually works in our favor --+ ;; the override metadata provider does ask its base provider for the original card, and this would cache the old+ ;; version of the card. However, the override metadata provider does this during a call lib.metadata/card. When+ ;; that "parent" call finishes, it stores the updated version of the card in the cache, and that overwrites the old+ ;; version that was previously cached.+ ;;+ ;; If we ever move to a world where model metadata edits are stored separately, we could and should use those+ ;; calculated edits instead of computing them from scratch. However, existing models still wouldn't have edits, so+ ;; we'd need to keep the existing code around for those existing cards. A backfill job could help here, but we'd+ ;; still need to account for cases where we just upgraded an instance and the backfill job hasn't finished yet.+ (let [db-id (-> (lib.metadata/database original-mp)+ :id)+ graph (mbql-graph original-mp)+ cards (dependent-mbql-cards graph start-type start-id)+ previous-metadata (lib-be/instance->metadata previous-object metadata-type)+ pre-update-mp (deps.metadata-provider/override-metadata-provider+ {:base-provider (fresh-mp db-id)+ :updated-entities {start-type [previous-metadata]}})+ edits (card-list-metadata-edits pre-update-mp cards)+ updated-mp (deps.metadata-provider/override-metadata-provider+ {:base-provider (fresh-mp db-id)+ :returned-columns-fn (fn [mp card]+ (updated-metadata mp card edits))+ :dependent-ids {:card cards}})+ start [start-type start-id]+ updates (->> (graph/transitive-children-of graph [start])+ (graph/keep-children+ (fn [[node-type node-id :as node]]+ (when (and (= node-type :card)+ (not= node start))+ (let [new-metadata (-> (lib.metadata/card updated-mp node-id)+ :result-metadata)+ old-metadata (-> (lib.metadata/card original-mp node-id)+ :result-metadata)]+ (if (= new-metadata old-metadata)+ ::graph/stop+ [node-id new-metadata]))))))]++ (doseq [[card-id new-metadata] updates]+ (t2/update! :model/Card card-id {:result_metadata new-metadata}))))++(derive ::update-card-dependents-metadata :metabase/event)+(derive :event/card-update ::update-card-dependents-metadata)++(methodical/defmethod events/publish-event! ::update-card-dependents-metadata+ [_ {{:keys [id dataset_query]} :object :keys [previous-object]}]+ (when (and (premium-features/has-feature? :dependencies)+ (not (lib/any-native-stage? dataset_query)))+ (update-dependent-mbql-cards-metadata! dataset_query :card id previous-object :metadata/card)))diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/events_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/events_test.cljindex 866e31df9c5..23807e0df5c 100644--- a/enterprise/backend/test/metabase_enterprise/dependencies/events_test.clj+++ b/enterprise/backend/test/metabase_enterprise/dependencies/events_test.clj@@ -244,7 +244,7 @@(t2/select-one-fn :dependency_analysis_version :model/Card :id card-id)))(is (empty? (t2/select :model/Dependency :from_entity_id card-id :from_entity_type :card))))(testing "on update event"- (events/publish-event! :event/card-update {:object card :user-id api/*current-user-id*})+ (events/publish-event! :event/card-update {:object card :previous-object card :user-id api/*current-user-id*})(is (some #(and (= "Dependency calculation failed" (ex-message (:e %)))(= :error (:level %)))(messages)))@@ -624,7 +624,7 @@:from_entity_id child-card-id:to_entity_type :card:to_entity_id parent-card-id}]- (events/publish-event! :event/card-update {:object parent-card :user-id api/*current-user-id*})+ (events/publish-event! :event/card-update {:object parent-card :previous-object parent-card :user-id api/*current-user-id*})(assert-has-analyses{:card {parent-card-id -1child-card-id -1}})))))))@@ -655,7 +655,7 @@child-card-id old-versionother-card-id old-version}}))(binding [models.analysis-finding/*current-analysis-finding-version* new-version]- (events/publish-event! :event/card-update {:object parent-card :user-id api/*current-user-id*}))+ (events/publish-event! :event/card-update {:object parent-card :previous-object parent-card :user-id api/*current-user-id*}))(testing "Checking that the analyses were updated appropriately"(assert-has-analyses{:card {parent-card-id new-version@@ -697,7 +697,7 @@child-card-id old-version}:transform {transform-id old-version}}))(binding [models.analysis-finding/*current-analysis-finding-version* new-version]- (events/publish-event! :event/card-update {:object parent-card :user-id api/*current-user-id*}))+ (events/publish-event! :event/card-update {:object parent-card :previous-object parent-card :user-id api/*current-user-id*}))(testing "Checking that analyses were updated correctly"(assert-has-analyses{:card {parent-card-id new-versiondiff --git a/enterprise/backend/test/metabase_enterprise/dependencies/metadata_provider_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/metadata_provider_test.cljindex 266f74745f0..2455a684c8d 100644--- a/enterprise/backend/test/metabase_enterprise/dependencies/metadata_provider_test.clj+++ b/enterprise/backend/test/metabase_enterprise/dependencies/metadata_provider_test.clj@@ -12,9 +12,8 @@(deftest basic-cards-test(testing "overriding a single card, it is returned correctly"(let [card (:orders (lib.tu/mock-cards))- mp (-> card- lib.tu/metadata-provider-with-mock-card- deps.mp/override-metadata-provider)]+ mp (deps.mp/override-metadata-provider+ {:base-provider (lib.tu/metadata-provider-with-mock-card card)})](deps.mp/add-override mp :card (:id card)(-> card(assoc-in [:dataset-query :query :fields]@@ -42,8 +41,8 @@by-name (m/index-by :name (lib/returned-columns (lib/query mp1 card1)))downstream (-> (lib/query mp1 card1)(lib/filter (lib/= (get by-name "count") 1)))- mp (-> (lib.tu/metadata-provider-with-card-from-query mp1 2 downstream)- deps.mp/override-metadata-provider)]+ mp (deps.mp/override-metadata-provider+ {:base-provider (lib.tu/metadata-provider-with-card-from-query mp1 2 downstream)})](deps.mp/add-override mp :card 1(-> upstream(lib/breakout (lib/with-temporal-bucket (meta/field-metadata :orders :created-at) :day))@@ -103,8 +102,8 @@xform-cols (m/index-by :name (lib.metadata/fields mp2 123000000))downstream (-> (lib/query mp2 (lib.metadata/table mp2 123000000))(lib/filter (lib/= (get xform-cols "count") 1)))- mp (-> (lib.tu/metadata-provider-with-card-from-query mp2 2 downstream)- deps.mp/override-metadata-provider)]+ mp (deps.mp/override-metadata-provider+ {:base-provider (lib.tu/metadata-provider-with-card-from-query mp2 2 downstream)})](deps.mp/add-override mp :card 1(-> upstream(lib/remove-clause (first (lib/breakouts upstream)))diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/metadata_update_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/metadata_update_test.cljnew file mode 100644index 00000000000..4b36c0cb476--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/dependencies/metadata_update_test.clj@@ -0,0 +1,275 @@+(ns metabase-enterprise.dependencies.metadata-update-test+ (:require+ [clojure.test :refer [deftest is testing]]+ [metabase-enterprise.dependencies.dependency-types :as deps.dependency-types]+ [metabase-enterprise.dependencies.metadata-update :as deps.metadata-update]+ [metabase.api.common :as api]+ [metabase.events.core :as events]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(defn assert-traverses-grandchild [{:keys [grandchild-dependency-type grandchild-data should-traverse? should-include?]}]+ (let [mp (mt/metadata-provider)+ query (->> (mt/id :products)+ (lib.metadata/table mp)+ (lib/query mp))+ grandchild-model (deps.dependency-types/dependency-type->model grandchild-dependency-type)]+ (mt/with-temp [:model/Card {parent-id :id} {:dataset_query query}+ :model/Card {child-id :id} {:dataset_query query}+ grandchild-model {grandchild-id :id} grandchild-data+ :model/Card {great-grandchild-id :id} {:dataset_query query}+ :model/Dependency _ {:from_entity_type :card+ :from_entity_id child-id+ :to_entity_type :card+ :to_entity_id parent-id}+ :model/Dependency _ {:from_entity_type grandchild-dependency-type+ :from_entity_id grandchild-id+ :to_entity_type :card+ :to_entity_id child-id}+ :model/Dependency _ {:from_entity_type :card+ :from_entity_id great-grandchild-id+ :to_entity_type grandchild-dependency-type+ :to_entity_id grandchild-id}]+ (is (= (cond-> #{child-id}+ should-include? (conj grandchild-id)+ should-traverse? (conj great-grandchild-id))+ (set (#'deps.metadata-update/dependent-mbql-cards+ (#'deps.metadata-update/mbql-graph mp)+ :card parent-id)))))))++(deftest ^:parallel dependent-mbql-cards-traverses-cards-test+ (testing "dependent-mbql-cards looks through card dependencies"+ (let [mp (mt/metadata-provider)+ query (->> (mt/id :products)+ (lib.metadata/table mp)+ (lib/query mp))]+ (assert-traverses-grandchild+ {:grandchild-dependency-type :card+ :grandchild-data {:dataset_query query}+ :should-traverse? true+ :should-include? true}))))++(deftest ^:parallel dependent-mbql-cards-stops-on-native-cards-test+ (testing "dependent-mbql-cards stops when it reaches a native card"+ (let [mp (mt/metadata-provider)+ native-query (lib/native-query mp "select * from orders")]+ (assert-traverses-grandchild+ {:grandchild-dependency-type :card+ :grandchild-data {:dataset_query native-query}}))))++(deftest ^:parallel dependent-mbql-cards-stops-on-snippets-test+ (testing "dependent-mbql-cards stops when it reaches a snippet"+ (assert-traverses-grandchild+ {:grandchild-dependency-type :snippet+ :grandchild-data {:name "test snippet"+ :content "SELECT 1"}})))++(deftest ^:parallel dependent-mbql-cards-stops-on-transforms-test+ (testing "dependent-mbql-cards stops when it reaches a transform"+ (assert-traverses-grandchild+ {:grandchild-dependency-type :transform+ :grandchild-data {}})))++(deftest ^:parallel dependent-mbql-cards-stops-on-dashboards-test+ (testing "dependent-mbql-cards stops when it reaches a dashboard"+ (assert-traverses-grandchild+ {:grandchild-dependency-type :dashboard+ :grandchild-data {}})))++(deftest ^:parallel dependent-mbql-cards-stops-on-documents-test+ (testing "dependent-mbql-cards stops when it reaches a document"+ (assert-traverses-grandchild+ {:grandchild-dependency-type :document+ :grandchild-data {}})))++(deftest ^:parallel dependent-mbql-cards-traverses-through-segments-test+ (testing "dependent-mbql-cards traverses through segments"+ (assert-traverses-grandchild+ {:grandchild-dependency-type :segment+ :grandchild-data {}+ :should-traverse? true})))++(deftest ^:parallel dependent-mbql-cards-traverses-through-measures-test+ (testing "dependent-mbql-cards traverses through measures"+ (assert-traverses-grandchild+ {:grandchild-dependency-type :measure+ :grandchild-data {}+ :should-traverse? true})))++(deftest ^:sequential card-update-updates-child-metadata-test+ (testing "card updates update child card metadata"+ (mt/with-test-user :rasta+ (mt/with-premium-features #{:dependencies}+ (let [mp (mt/metadata-provider)+ products-id (mt/id :products)+ orders-id (mt/id :orders)+ products (lib.metadata/table mp products-id)+ orders (lib.metadata/table mp orders-id)]+ (mt/with-temp [:model/Card {parent-id :id :as parent-card} {:dataset_query (lib/query mp products)}+ :model/Card {child-id :id} {:dataset_query (lib/query mp (lib.metadata/card mp parent-id))}+ :model/Card {grandchild-id :id} {:dataset_query (lib/query mp (lib.metadata/card mp child-id))}+ :model/Dependency _ {:from_entity_type :card+ :from_entity_id child-id+ :to_entity_type :card+ :to_entity_id parent-id}+ :model/Dependency _ {:from_entity_type :card+ :from_entity_id grandchild-id+ :to_entity_type :card+ :to_entity_id child-id}]+ (is (= #{8}+ (t2/select-fn-set (comp count :result_metadata)+ [:model/Card :id :result_metadata :card_schema]+ :id [:in [parent-id child-id grandchild-id]])))+ (t2/update! :model/Card parent-id {:dataset_query (lib/query mp orders)})+ (events/publish-event! :event/card-update+ {:object (assoc parent-card :dataset_query (lib/query mp orders))+ :previous-object parent-card+ :user-id api/*current-user-id*})+ (is (= #{9}+ (t2/select-fn-set (comp count :result_metadata)+ [:model/Card :id :result_metadata :card_schema]+ :id [:in [parent-id child-id grandchild-id]])))))))))++(deftest ^:sequential native-card-update-does-not-update-children-test+ (testing "native card updates do not update children"+ (mt/with-test-user :rasta+ (mt/with-premium-features #{:dependencies}+ (let [mp (mt/metadata-provider)+ products-id (mt/id :products)+ products (lib.metadata/table mp products-id)+ native-query (lib/native-query mp "select * from orders")]+ (mt/with-temp [:model/Card {parent-id :id :as parent-card} {:dataset_query (lib/query mp products)}+ :model/Card {child-id :id} {:dataset_query (lib/query mp (lib.metadata/card mp parent-id))}+ :model/Dependency _ {:from_entity_type :card+ :from_entity_id child-id+ :to_entity_type :card+ :to_entity_id parent-id}]+ (t2/update! :model/Card child-id {:result_metadata nil})+ (events/publish-event! :event/card-update+ {:object (assoc parent-card :dataset_query native-query)+ :previous-object parent-card+ :user-id api/*current-user-id*})+ (is (= nil+ (t2/select-one-fn :result_metadata+ [:model/Card :id :result_metadata :card_schema]+ :id child-id)))))))))++(deftest ^:sequential model-update-passes-down-new-values-test+ (testing "model updates pass down new result metadata"+ (mt/with-test-user :rasta+ (mt/with-premium-features #{:dependencies}+ (let [mp (mt/metadata-provider)+ products-id (mt/id :products)+ products (lib.metadata/table mp products-id)]+ (mt/with-temp [:model/Card {parent-id :id :as parent-card} {:dataset_query (lib/query mp products)+ :type :model}+ :model/Card {child-id :id} {:dataset_query (lib/query mp (lib.metadata/card mp parent-id))++ :type :model}+ :model/Card {grandchild-id :id} {:dataset_query (lib/query mp (lib.metadata/card mp child-id))+ :type :model}+ :model/Dependency _ {:from_entity_type :card+ :from_entity_id child-id+ :to_entity_type :card+ :to_entity_id parent-id}+ :model/Dependency _ {:from_entity_type :card+ :from_entity_id grandchild-id+ :to_entity_type :card+ :to_entity_id child-id}]+ (let [new-result-metadata (assoc-in (:result_metadata parent-card)+ [0 :display_name]+ "new-name")]+ (t2/update! :model/Card parent-id {:result_metadata new-result-metadata})+ (events/publish-event! :event/card-update+ {:object (assoc parent-card :result_metadata new-result-metadata)+ :previous-object parent-card+ :user-id api/*current-user-id*})+ (is (= #{[child-id "new-name"] [grandchild-id "new-name"]}+ (t2/select-fn-set (juxt :id #(get-in % [:result_metadata 0 :display_name]))+ [:model/Card :id :result_metadata :card_schema]+ :id [:in [child-id grandchild-id]]))))))))))++(deftest ^:sequential model-update-respects-child-overrides-test+ (testing "model updates respect child metadata edits"+ (mt/with-test-user :rasta+ (mt/with-premium-features #{:dependencies}+ (let [mp (mt/metadata-provider)+ products-id (mt/id :products)+ products (lib.metadata/table mp products-id)]+ (mt/with-temp [:model/Card {parent-id :id :as parent-card} {:dataset_query (lib/query mp products)+ :type :model}+ :model/Card {child-id :id :as child-card} {:dataset_query (lib/query mp (lib.metadata/card mp parent-id))+ :type :model}+ :model/Card {grandchild-id :id} {:dataset_query (lib/query mp (lib.metadata/card mp child-id))+ :type :model}+ :model/Dependency _ {:from_entity_type :card+ :from_entity_id child-id+ :to_entity_type :card+ :to_entity_id parent-id}+ :model/Dependency _ {:from_entity_type :card+ :from_entity_id grandchild-id+ :to_entity_type :card+ :to_entity_id child-id}]+ (let [new-parent-metadata (assoc-in (:result_metadata parent-card)+ [0 :display_name]+ "new-name")+ new-child-metadata (assoc-in (:result_metadata child-card)+ [0 :display_name]+ "child-name")+ new-grandchild-metadata (assoc-in (:result_metadata child-card)+ [0 :display_name]+ "grandchild-name")]+ (t2/update! :model/Card parent-id {:result_metadata new-parent-metadata})+ (t2/update! :model/Card child-id {:result_metadata new-child-metadata})+ (t2/update! :model/Card grandchild-id {:result_metadata new-grandchild-metadata})+ (events/publish-event! :event/card-update+ {:object (assoc parent-card :result_metadata new-parent-metadata)+ :previous-object parent-card+ :user-id api/*current-user-id*})+ (is (= #{[child-id "child-name"] [grandchild-id "grandchild-name"]}+ (t2/select-fn-set (juxt :id #(get-in % [:result_metadata 0 :display_name]))+ [:model/Card :id :result_metadata :card_schema]+ :id [:in [child-id grandchild-id]]))))))))))++(deftest ^:sequential model-update-stops-recursing-when-child-metadata-is-unchanged-test+ (testing "model updates stop recursing when they hit a child whose metadata didn't change"+ (mt/with-test-user :rasta+ (mt/with-premium-features #{:dependencies}+ (let [mp (mt/metadata-provider)+ products-id (mt/id :products)+ products (lib.metadata/table mp products-id)]+ (mt/with-temp [:model/Card {parent-id :id :as parent-card} {:dataset_query (lib/query mp products)+ :type :model}+ :model/Card {child-id :id :as child-card} {:dataset_query (lib/query mp (lib.metadata/card mp parent-id))+ :type :model}+ :model/Card {grandchild-id :id} {:dataset_query (lib/query mp (lib.metadata/card mp child-id))+ :type :model}+ :model/Dependency _ {:from_entity_type :card+ :from_entity_id child-id+ :to_entity_type :card+ :to_entity_id parent-id}+ :model/Dependency _ {:from_entity_type :card+ :from_entity_id grandchild-id+ :to_entity_type :card+ :to_entity_id child-id}]+ (let [new-parent-metadata (assoc-in (:result_metadata parent-card)+ [0 :display_name]+ "new-name")+ new-child-metadata (-> (:result_metadata child-card)+ (assoc-in [0 :display_name]+ "child-name")+ (assoc-in [0 :lib/model-display-name]+ "new-name"))]+ (t2/update! :model/Card parent-id {:result_metadata new-parent-metadata})+ (t2/update! :model/Card child-id {:result_metadata new-child-metadata})+ (t2/update! :model/Card grandchild-id {:result_metadata nil})+ (events/publish-event! :event/card-update+ {:object (assoc parent-card :result_metadata new-parent-metadata)+ :previous-object parent-card+ :user-id api/*current-user-id*})+ (is (= nil+ (t2/select-one-fn #(get-in % [:result_metadata 0 :display_name])+ [:model/Card :id :result_metadata :card_schema]+ :id grandchild-id))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.cljindex a33e4b93838..2c96f811fe3 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj@@ -63,7 +63,7 @@:collection_id (:id remote-sync-collection)}](t2/delete! :model/RemoteSyncObject)(events/publish-event! :event/card-update- {:object remote-sync-card :user-id (mt/user->id :rasta)})+ {:object remote-sync-card :previous-object remote-sync-card :user-id (mt/user->id :rasta)})(let [entries (t2/select :model/RemoteSyncObject)](is (= 1 (count entries)))(is (=? {:model_type "Card"@@ -80,6 +80,7 @@(t2/delete! :model/RemoteSyncObject)(events/publish-event! :event/card-update{:object (assoc remote-sync-card :archived true)+ :previous-object remote-sync-card:user-id (mt/user->id :rasta)})(let [entries (t2/select :model/RemoteSyncObject)](is (= 1 (count entries)))@@ -121,7 +122,7 @@(let [clock-t2 (t/mock-clock (t/instant "2024-01-01T11:00:00Z") (t/zone-id "UTC"))](t/with-clock clock-t2(events/publish-event! :event/card-update- {:object remote-sync-card :user-id (mt/user->id :rasta)})))+ {:object remote-sync-card :previous-object remote-sync-card :user-id (mt/user->id :rasta)})))(let [entries (t2/select :model/RemoteSyncObject)](is (= 1 (count entries)))(let [update-entry (first entries)]@@ -592,6 +593,7 @@(is (= "synced" (:status initial-entry)))(events/publish-event! :event/card-update{:object (assoc card :collection_id (:id normal-collection))+ :previous-object card:user-id (mt/user->id :rasta)})(let [update-entry (t2/select-one :model/RemoteSyncObject :model_type "Card" :model_id (:id card))]@@ -612,6 +614,7 @@(is (= "create" (:status initial-entry)))(events/publish-event! :event/card-update{:object (assoc card :collection_id (:id normal-collection))+ :previous-object card:user-id (mt/user->id :rasta)})(let [update-entry (t2/select-one :model/RemoteSyncObject :model_type "Card" :model_id (:id card))](is (nil? update-entry)))))))@@ -720,7 +723,7 @@:collection_id (:id normal-collection)}](t2/delete! :model/RemoteSyncObject)(events/publish-event! :event/card-update- {:object card :user-id (mt/user->id :rasta)})+ {:object card :previous-object card :user-id (mt/user->id :rasta)})(let [entries (t2/select :model/RemoteSyncObject :model_type "Card" :model_id (:id card))](is (= 0 (count entries)))))))diff --git a/src/metabase/graph/core.cljc b/src/metabase/graph/core.cljcindex 271c86e9a29..17dec2f2be8 100644--- a/src/metabase/graph/core.cljc+++ b/src/metabase/graph/core.cljc@@ -8,7 +8,9 @@Each graph defines an arbitrary **key**. This can be any hash map key, such as a number, a `[type id]` pair, etc."(:require+ [medley.core :as m][metabase.util :as u]+ [metabase.util.malli :as mu][metabase.util.malli.registry :as mr])#?@(:clj ((:import java.util.LinkedHashSet))))@@ -19,7 +21,7 @@"Given a graph and a seq of keys, returns a map from each input key to the set of keys **directly reachable** fromthat key.- If you want a *transitive* search, call [[transitive]].+ If you want a *transitive* search, call [[transitive]] or [[transitive-children-of]].Contract details:- If a key is not known in the graph, it should be missing from the output map.@@ -52,7 +54,34 @@:seen? #(.has s %):->iterable #(es6-iterator-seq (.values s))})))-(defn transitive+(deftype FilteredGraph [graph filter-fn]+ Graph+ (children-of [_this key-seq]+ (->> (children-of graph key-seq)+ (m/map-vals #(into #{} (filter filter-fn) %)))))++(defn filtered-graph+ "Wraps a graph with an implementation that filters any returned children."+ [graph filter-fn]+ (->FilteredGraph graph filter-fn))++(defn graph?+ "Whether `x` is a valid `Graph`."+ [x]+ #?(:clj (extends? Graph (class x))+ :cljs (satisfies? Graph x)))++(mr/def ::graph+ "Schema for anything that satisfies the [[Graph]] protocol."+ [:fn+ {:error/message "Valid Graph instance"}+ #'graph?])++(mr/def ::node :any)++(mr/def ::child-map [:map-of ::node [:set ::node]])++(mu/defn transitive :- [:sequential ::node]"Given a graph and `key-seq`, returns a seq of all transitive children of those starting keys.The returned seq:@@ -63,7 +92,8 @@- Makes no guarantee about the order of keys at the same \"level\".- That is, if two keys `k1` and `k2` are reachable in no fewer than `n` steps from the starters, then `k1` mightprecede or follow `k2` in the output seq."- [graph key-seq]+ [graph :- ::graph+ key-seq :- [:sequential ::node]](let [{:keys [insert-many! seen? ->iterable]} (stable-iteration-set)](insert-many! key-seq)(loop [new-keys key-seq]@@ -74,6 +104,69 @@(recur new-children))(drop (count key-seq) (->iterable))))))+(mu/defn transitive-children-of :- ::child-map+ "Given a graph and `key-seq`, finds all transitive children and returns a map of `{parent #{child}}`.++ Also takes in an optional filter for those children.++ Effectively, this is just `children-of` except transitive children are included and the potential filter."+ [graph :- ::graph+ key-seq :- [:sequential ::node]]+ (loop [to-traverse (into #{} key-seq)+ child-map {}]+ (let [new-children (children-of graph to-traverse)+ new-traverse (into #{}+ (comp (remove child-map) cat)+ (vals new-children))+ new-child-map (into child-map new-children)]+ (if (seq new-traverse)+ (recur new-traverse new-child-map)+ new-child-map))))++(mu/defn keep-children :- [:sequential :any]+ "Iterates through a child map, calls `f` for each node, and returns the non-nil results as a list.++ Nodes are guaranteed to be in a consistent order, and parents are guaranteed to be before their children.++ If `f` ever returns `:metabase.graph.core/stop`, `keep-children` does not include that in the results and does not+ recurse down the current node's children."+ [f :- [:-> ::node :any]+ children :- ::child-map]+ (let [all-nodes (-> (into (set (keys children))+ cat+ (vals children))+ sort)+ full-parent-map (->> children+ (mapcat (fn [[parent current-children]]+ (map (fn [child]+ {child #{parent}})+ current-children)))+ (apply merge-with into))]+ (loop [nodes-remaining all-nodes+ parent-map full-parent-map+ ignored #{}+ result []]+ (if-let [next-node (some #(when-not (or (seq (parent-map %))+ (ignored %))+ %)+ nodes-remaining)]+ (let [new-value (f next-node)+ new-nodes-remaining (remove #(= % next-node) nodes-remaining)]+ (case new-value+ nil (recur new-nodes-remaining+ (m/map-vals #(disj % next-node) parent-map)+ ignored+ result)+ ::stop (recur new-nodes-remaining+ parent-map+ (conj ignored next-node)+ result)+ (recur new-nodes-remaining+ (m/map-vals #(disj % next-node) parent-map)+ ignored+ (conj result new-value))))+ result))))+(defn edges-between"Calculates the edges within `nodes`, based on `graph`."[graph nodes]@@ -88,18 +181,6 @@:to_entity_id parent-id})]edges))-(defn graph?- "Whether `x` is a valid `Graph`."- [x]- #?(:clj (extends? Graph (class x))- :cljs (satisfies? Graph x)))--(mr/def ::graph- "Schema for anything that satisfies the [[Graph]] protocol."- [:fn- {:error/message "Valid Graph instance"}- #'graph?])-;; ## In-memory Graphs(deftype InMemoryGraph [adjacency-map]Graphdiff --git a/src/metabase/lib/card.cljc b/src/metabase/lib/card.cljcindex 121beff208d..1c7ea5d6a58 100644--- a/src/metabase/lib/card.cljc+++ b/src/metabase/lib/card.cljc@@ -195,7 +195,7 @@(when (= (:type source-card) :model)source-card))))))-(mu/defn- model-preserved-keys :- [:sequential :keyword]+(mu/defn model-preserved-keys :- [:sequential :keyword]"Keys that can survive merging metadata from the database onto metadata computed from the query. When mergingmetadata, the types returned should be authoritative. But things like semantic_type, display_name, and descriptioncan be merged on top.diff --git a/src/metabase/lib/core.cljc b/src/metabase/lib/core.cljcindex 83d1616e100..d9145b4a045 100644--- a/src/metabase/lib/core.cljc+++ b/src/metabase/lib/core.cljc@@ -160,7 +160,8 @@breakouts-metadataremove-all-breakouts][metabase.lib.card- card->underlying-query]+ card->underlying-query+ model-preserved-keys][lib.column-groupcolumns-group-columnsgroup-columns]diff --git a/src/metabase/queries/core.clj b/src/metabase/queries/core.cljindex 7a841c70bd3..32d16d71f90 100644--- a/src/metabase/queries/core.clj+++ b/src/metabase/queries/core.clj@@ -36,6 +36,7 @@;; TODO -- not convinced whether this belongs here or in `permissions`with-can-run-adhoc-query][metabase.queries.models.card.metadata+ infer-metadatamaybe-async-result-metadatarefresh-metadatasave-metadata-async!]diff --git a/src/metabase/queries/events/schema.clj b/src/metabase/queries/events/schema.cljindex ba6146185ec..3a94bc2720e 100644--- a/src/metabase/queries/events/schema.clj+++ b/src/metabase/queries/events/schema.clj@@ -10,9 +10,14 @@[:object [:fn #(t2/instance-of? :model/Card %)]]])(mr/def :event/card-create ::card)-(mr/def :event/card-update ::card)(mr/def :event/card-delete ::card)+(mr/def :event/card-update+ [:map {:closed true}+ [:user-id [:maybe pos-int?]]+ [:object [:fn #(t2/instance-of? :model/Card %)]]+ [:previous-object [:fn #(t2/instance-of? :model/Card %)]]])+(mr/def :event/card-read[:map {:closed true};; context is deliberately coupled to view-log's contextdiff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.cljindex 45262deada6..5b869d244cb 100644--- a/src/metabase/queries/models/card.clj+++ b/src/metabase/queries/models/card.clj@@ -1199,7 +1199,9 @@;; skip publishing the event if it's just a change in its collection position(when-not (= #{:collection_position}(set (keys card-updates)))- (events/publish-event! :event/card-update {:object card :user-id api/*current-user-id*}))+ (events/publish-event! :event/card-update {:object card+ :previous-object card-before-update+ :user-id api/*current-user-id*}))card))(defn sole-dashboard-iddiff --git a/test/metabase/audit_app/events/audit_log_test.clj b/test/metabase/audit_app/events/audit_log_test.cljindex d1af3e4c410..fc4fa1cfc38 100644--- a/test/metabase/audit_app/events/audit_log_test.clj+++ b/test/metabase/audit_app/events/audit_log_test.clj@@ -65,19 +65,19 @@(testing card-type(mt/with-temp [:model/Card card {:name "My Cool Card", :type card-type}](mt/with-test-user :rasta- (is (= {:object card :user-id (mt/user->id :rasta)}- (events/publish-event! :event/card-update {:object card :user-id (mt/user->id :rasta)})))- (is (partial=- {:topic :card-update- :user_id (mt/user->id :rasta)- :model "Card"- :model_id (:id card)- :details (cond-> {:name "My Cool Card"- :description nil- :table_id nil- :database_id (mt/id)}- (= card-type :model) (assoc :model? true))}- (mt/latest-audit-log-entry "card-update" (:id card))))))))))+ (let [old-card (assoc card :name "Old Name", :type :question)]+ (is (= {:object card :previous-object old-card :user-id (mt/user->id :rasta)}+ (events/publish-event! :event/card-update {:object card :previous-object old-card :user-id (mt/user->id :rasta)})))+ (is (partial=+ {:topic :card-update+ :user_id (mt/user->id :rasta)+ :model "Card"+ :model_id (:id card)+ :details {:previous (cond-> {:name "Old Name"}+ (= card-type :model) (assoc :model? false))+ :new (cond-> {:name "My Cool Card"}+ (= card-type :model) (assoc :model? true))}}+ (mt/latest-audit-log-entry "card-update" (:id card)))))))))))(deftest card-delete-event-test(testing :card-deletediff --git a/test/metabase/graph/core_test.cljc b/test/metabase/graph/core_test.cljcindex 294930031e3..ce01a971730 100644--- a/test/metabase/graph/core_test.cljc+++ b/test/metabase/graph/core_test.cljc@@ -53,3 +53,101 @@3 #{2}})];; The cycle-path should be [2 3 2], not [1 2 3 2](is (= [2 3 2] (graph/find-cycle g [1]))))))++(defn- assert-keep-children-order+ ([expected input]+ (assert-keep-children-order expected input identity))+ ([expected input recur?]+ (is (= expected+ (graph/keep-children recur? input)))))++(deftest ^:parallel keep-children-handles-simple-parents-test+ (testing "keep-children puts parents before children"+ (assert-keep-children-order+ [[:card 4] [:card 3] [:card 2]]+ {[:card 4] #{[:card 3]}+ [:card 3] #{[:card 2]}})))++(deftest ^:parallel keep-children-sorts-nodes-at-same-level-test+ (testing "keep-children sorts sibling nodes"+ (assert-keep-children-order+ [[:card 6] [:card 2] [:card 4] [:card 7] [:card 3] [:card 5]]+ {[:card 6] #{[:card 2]+ [:card 4]}+ [:card 7] #{[:card 3]+ [:card 5]}})))++(deftest ^:parallel keep-children-handles-multiple-parents-test+ (testing "keep-children handles children with multiple parents"+ (assert-keep-children-order+ [[:card 2] [:card 4] [:card 3]]+ {[:card 2] #{[:card 3]+ [:card 4]}+ [:card 4] #{[:card 3]}})))++(deftest ^:parallel keep-children-ignores-children-when-asked-test+ (testing "keep-children ignores children when asked"+ (assert-keep-children-order+ [[:card 2] [:card 3] [:card 4]]+ {[:card 2] #{[:card 3]+ [:card 4]}+ [:card 5] #{[:card 6]+ [:card 7]}}+ (fn [node]+ (if (= node [:card 5])+ ::graph/stop+ node)))))++(deftest ^:parallel keep-children-recurses-through-nodes-test+ (testing "keep-children ignores nil values while recursing through the node"+ (assert-keep-children-order+ [[:card 3] [:card 4]]+ {[:segment 2] #{[:card 3]+ [:card 4]}}+ (fn [[node-type _node-id :as node]]+ (when (= node-type :card)+ node)))))++(deftest ^:parallel transitive-children-of-test+ (testing "transitive-children-of returns basic graph unchanged"+ (let [nodes {1 #{2}+ 2 #{3}+ 3 #{}}]+ (is (= nodes+ (graph/transitive-children-of (graph/in-memory nodes)+ [1]))))))++(deftest ^:parallel transitive-children-of-only-returns-connected-nodes-test+ (testing "transitive-children-of only returns connected graph nodes"+ (let [nodes {1 #{2}+ 2 #{3}+ 3 #{}+ 4 #{5}+ 5 #{}}]+ (is (= {1 #{2}+ 2 #{3}+ 3 #{}}+ (graph/transitive-children-of (graph/in-memory nodes)+ [1]))))))++(deftest ^:parallel transitive-children-of-accepts-multiple-starts-test+ (testing "transitive-children-of accepts multiple start nodes"+ (let [nodes {1 #{2}+ 2 #{3}+ 3 #{}+ 4 #{5}+ 5 #{}}]+ (is (= nodes+ (graph/transitive-children-of (graph/in-memory nodes)+ [1 4]))))))++(deftest ^:parallel filtered-graph-test+ (testing "filtered-graph does the thing"+ (let [g (graph/in-memory {1 #{2}+ 2 #{3 5}+ 3 #{4}})]+ (is (= {1 #{2}+ 2 #{3}+ 3 #{}}+ (graph/children-of (graph/filtered-graph g #(<= % 3))+ [1 2 3]))))))diff --git a/test/metabase/revisions/events_test.clj b/test/metabase/revisions/events_test.cljindex 8238b36a788..ab98e4cab6f 100644--- a/test/metabase/revisions/events_test.clj+++ b/test/metabase/revisions/events_test.clj@@ -72,7 +72,7 @@(deftest card-update-test(testing :event/card-update(mt/with-temp [:model/Card {card-id :id, :as card} (card-properties)]- (events/publish-event! :event/card-update {:object card :user-id (mt/user->id :crowberto)})+ (events/publish-event! :event/card-update {:object card :previous-object card :user-id (mt/user->id :crowberto)})(is (=? {:model "Card":model_id card-id:user_id (mt/user->id :crowberto)@@ -86,7 +86,9 @@(deftest card-update-shoud-not-contains-public-info-test(testing :event/card-update(mt/with-temp [:model/Card {card-id :id, :as card} (card-properties)]- (events/publish-event! :event/card-update {:object card :user-id (mt/user->id :crowberto)})+ (events/publish-event! :event/card-update {:object card+ :previous-object card+ :user-id (mt/user->id :crowberto)});; we don't want the public_uuid and made_public_by_id to be recorded in a revision;; otherwise revert a card to earlier revision might toggle the public sharing settings(is (empty? (set/intersection #{:public_uuid :made_public_by_id}