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
GDGT-1431 Delete notifications orphaned from deleted cards (#71897)
YB12e351567162
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 3b678332446..5bacdc1a01c 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -1640,6 +1640,7 @@:model/Field:model/ModerationReview:model/NativeQuerySnippet+ :model/Notification:model/Revision:model/Table:model/User}}diff --git a/src/metabase/notification/send.clj b/src/metabase/notification/send.cljindex 44b9c01783f..43ceb317b90 100644--- a/src/metabase/notification/send.clj+++ b/src/metabase/notification/send.clj@@ -140,6 +140,16 @@(try(log/info "Sending")(prometheus/inc! :metabase-notification/concurrent-tasks)+ ;; Guard against orphaned card notifications whose payload record was cascade-deleted.+ ;; Only applies to persisted notifications (with a non-nil :payload_id); Pulse-converted+ ;; notifications pass through :payload instead and have no :payload_id.+ (when-let [payload-id (when (= :notification/card payload_type)+ (:payload_id notification-info))]+ (when-not (t2/exists? :model/NotificationCard payload-id)+ (log/warnf "Payload for notification %d no longer exists, deleting" id)+ (t2/delete! :model/Notification id)+ (throw (ex-info "Card no longer exists, notification deleted"+ {:notification-id id}))))(let [hydrated-notification (hydrate-notification notification-info)handlers (:handlers hydrated-notification)](task-history/with-task-history {:task "notification-send"diff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.cljindex b632af209df..98f62aac67a 100644--- a/src/metabase/queries/models/card.clj+++ b/src/metabase/queries/models/card.clj@@ -847,7 +847,16 @@;; delete any ParameterCard linked to this card(t2/delete! :model/ParameterCard :card_id id)(t2/delete! :model/ModerationReview :moderated_item_type "card", :moderated_item_id id)- (t2/delete! :model/Revision :model "Card", :model_id id))+ (t2/delete! :model/Revision :model "Card", :model_id id)+ ;; delete any card-type notifications for this card — must materialize IDs first because+ ;; Notification's before-delete deletes the NotificationCard, which would make a subquery+ ;; return empty by the time the actual DELETE executes.+ (when-let [notification-ids (seq (t2/select-pks-set :model/Notification+ :payload_type :notification/card+ :payload_id [:in {:select [:id]+ :from [:notification_card]+ :where [:= :card_id id]}]))]+ (t2/delete! :model/Notification :id [:in notification-ids])))(defmethod serdes/hash-fields :model/Card[_card]diff --git a/test/metabase/notification/models_test.clj b/test/metabase/notification/models_test.cljindex 2b52bf93732..14ff53bc9e8 100644--- a/test/metabase/notification/models_test.clj+++ b/test/metabase/notification/models_test.clj@@ -80,6 +80,35 @@(t2/delete! :model/Notification (:id notification))(is (not (t2/exists? :model/NotificationCard notification-card-id))))))))+(deftest delete-card-cleans-up-notifications-test+ (testing "deleting a card should remove all associated notifications, both active and inactive"+ (notification.tu/with-notification-cleanup!+ (mt/with-temp [:model/Card {card-id :id} {:name "alert cleanup test card"+ :dataset_query (mt/mbql-query products {:aggregation [[:count]]})}]+ (let [active-notification (models.notification/create-notification!+ {:payload_type :notification/card+ :payload {:card_id card-id}+ :creator_id (mt/user->id :crowberto)}+ []+ [])+ inactive-notification (models.notification/create-notification!+ {:payload_type :notification/card+ :payload {:card_id card-id}+ :active false+ :creator_id (mt/user->id :crowberto)}+ []+ [])]+ (testing "sanity: both notifications and notification cards exist"+ (is (t2/exists? :model/Notification (:id active-notification)))+ (is (t2/exists? :model/Notification (:id inactive-notification)))+ (is (= 2 (t2/count :model/NotificationCard :card_id card-id))))+ (t2/delete! :model/Card card-id)+ (testing "both notifications should be removed when the card is deleted"+ (is (not (t2/exists? :model/Notification (:id active-notification))))+ (is (not (t2/exists? :model/Notification (:id inactive-notification)))))+ (testing "notification card payloads should also be removed"+ (is (not (t2/exists? :model/NotificationCard :card_id card-id)))))))))+(deftest notification-subscription-type-test(mt/with-temp [:model/Notification {n-id :id} {}](testing "success path"diff --git a/test/metabase/notification/payload/impl/card_test.clj b/test/metabase/notification/payload/impl/card_test.cljindex 1629f45a99c..0c598c0a588 100644--- a/test/metabase/notification/payload/impl/card_test.clj+++ b/test/metabase/notification/payload/impl/card_test.clj@@ -6,6 +6,7 @@[medley.core :as m][metabase.channel.core :as channel][metabase.notification.core :as notification]+ [metabase.notification.models :as models.notification][metabase.notification.payload.core :as notification.payload][metabase.notification.payload.execute :as notification.payload.execute][metabase.notification.send :as notification.send]@@ -555,6 +556,35 @@(t2/select [:model/TaskHistory :status :task_details] :task "notification-send"{:order-by [[:started_at :asc]]}))))))+(deftest orphaned-notification-deactivates-on-send-test+ (testing "A notification whose card no longer exists should deactivate itself instead of crashing forever"+ (notification.tu/with-notification-cleanup!+ (mt/with-temp [:model/Card {card-id :id} {:name notification.tu/default-card-name+ :dataset_query (mt/mbql-query products {:aggregation [[:count]]})}]+ (let [notification (models.notification/create-notification!+ {:payload_type :notification/card+ :payload {:card_id card-id}+ :creator_id (mt/user->id :crowberto)}+ []+ [@notification.tu/default-email-handler])]+ (testing "sanity: notification is active"+ (is (true? (t2/select-one-fn :active :model/Notification (:id notification)))))+ ;; Simulate an orphaned notification: delete the NotificationCard directly.+ ;; This mirrors what happens in prod when there is no Malli validation and a card is deleted (FK cascade deletes NotificationCard)+ ;; but the Notification row itself survives.+ (t2/delete! :model/NotificationCard (:payload_id notification))+ (testing "sanity: notification still exists but its payload record is gone"+ (is (t2/exists? :model/Notification (:id notification)))+ (is (not (t2/exists? :model/NotificationCard (:payload_id notification)))))+ (testing "sending the orphaned notification should delete it rather than crash forever"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Card no longer exists"+ (#'notification.send/send-notification-sync!+ (t2/select-one :model/Notification (:id notification))))))+ (testing "the notification should be deleted"+ (is (not (t2/exists? :model/Notification (:id notification))))))))))+(defn- email->attachment-line-count[email](let [attachment (m/find-first #(= "text/csv" (:content-type %)) (:message email))]