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-3170] keep subscriptions when a dashboard is trashed (#82999)
YBbbc2eefceb93
diff --git a/src/metabase/collections/db.clj b/src/metabase/collections/db.cljindex 977b832995e..187166f3cf9 100644--- a/src/metabase/collections/db.clj+++ b/src/metabase/collections/db.clj@@ -378,10 +378,12 @@:join [[:collection :c] [:= :collection_id :c.id]]}))(mu/defn set-pulse-archived-in-collections!- "Set `archived?` on the Pulses in the Collections with `collection-ids`, returning the number updated."+ "Set `archived?` on the Pulses in the Collections with `collection-ids`, returning the number updated.+ Dashboard subscriptions are skipped: they follow their Dashboard's `archived` flag, and their own `archived` flag+ records only that the user deleted them."[collection-ids :- [:sequential ::lib.schema.id/collection]archived? :- :boolean]- (t2/update! :model/Pulse {:collection_id [:in collection-ids]} {:archived archived?}))+ (t2/update! :model/Pulse {:collection_id [:in collection-ids] :dashboard_id nil} {:archived archived?}))(mu/defn set-native-query-snippet-archived-in-collections!"Set `archived?` on the NativeQuerySnippets in the Collections with `collection-ids`, returning the numberdiff --git a/src/metabase/dashboards/models/dashboard.clj b/src/metabase/dashboards/models/dashboard.cljindex b194b259550..2840632f71b 100644--- a/src/metabase/dashboards/models/dashboard.clj+++ b/src/metabase/dashboards/models/dashboard.clj@@ -78,7 +78,9 @@[dashboard](let [dashboard-id (u/the-id dashboard)](queries/delete-all-parameter-cards-for-parameterized-object! "dashboard" dashboard-id)- (dashboards.db/delete-dashboard-revisions! dashboard-id)))+ (dashboards.db/delete-dashboard-revisions! dashboard-id)+ ;; delete through Toucan rather than the FK cascade so the PulseChannel hook removes the SendPulse triggers+ (dashboards.db/delete-pulses-for-dashboard! dashboard-id)))(t2/define-before-insert :model/Dashboard[dashboard]@@ -106,9 +108,7 @@(params/assert-valid-parameters dashboard)(when (:parameters changes)(queries/upsert-or-delete-parameter-cards-from-parameters! "dashboard" (:id dashboard) (:parameters dashboard)))- (collection/check-collection-namespace :model/Dashboard (:collection_id dashboard))- (when (:archived changes)- (dashboards.db/delete-pulses-for-dashboard! (u/the-id dashboard))))))+ (collection/check-collection-namespace :model/Dashboard (:collection_id dashboard)))))(mu/defn- migrate-parameter [p :- ::parameters.schema/parameter](cond-> pdiff --git a/src/metabase/pulse/db.clj b/src/metabase/pulse/db.cljindex 32154a459d3..61626f13627 100644--- a/src/metabase/pulse/db.clj+++ b/src/metabase/pulse/db.clj@@ -31,6 +31,11 @@[dashboard-id :- [:maybe ::lib.schema.id/dashboard]](t2/select-one :model/Dashboard :id dashboard-id))+(mu/defn dashboard-archived? :- :boolean+ "Whether the Dashboard with `dashboard-id` exists and is archived."+ [dashboard-id :- [:maybe ::lib.schema.id/dashboard]]+ (t2/exists? :model/Dashboard :id dashboard-id :archived true))+(mu/defn dashboard-collection-id"The Collection id of the Dashboard with `dashboard-id`, or nil."[dashboard-id :- ::lib.schema.id/dashboard]diff --git a/src/metabase/pulse/task/send_pulses.clj b/src/metabase/pulse/task/send_pulses.cljindex 5fb609de8fb..96384fcaf33 100644--- a/src/metabase/pulse/task/send_pulses.clj+++ b/src/metabase/pulse/task/send_pulses.clj@@ -34,14 +34,18 @@:archived false;; alerts should all be migrated to notifications by now:alert_condition nil)]- (task-history/with-task-run (some-> (pulse.send/pulse->task-run-info pulse) (assoc :auto-complete false))- (task-history/with-task-history {:task "send-pulse"- :task_details {:pulse-id pulse-id- :channel-ids (seq channel-ids)}}- (log/debugf "Starting Pulse Execution: %d" pulse-id)- (pulse.send/send-pulse! pulse :channel-ids channel-ids :async? true)- (log/debugf "Finished Pulse Execution: %d" pulse-id)- :done))+ ;; a trashed Dashboard keeps its triggers so restoring it resumes them; skip before opening a task run,+ ;; which nothing would complete since `pulse.send/send-pulse!` sends nothing for an archived Dashboard+ (if (pulse.db/dashboard-archived? (:dashboard_id pulse))+ (log/debugf "Dashboard of Pulse %d is archived, Skipping." pulse-id)+ (task-history/with-task-run (some-> (pulse.send/pulse->task-run-info pulse) (assoc :auto-complete false))+ (task-history/with-task-history {:task "send-pulse"+ :task_details {:pulse-id pulse-id+ :channel-ids (seq channel-ids)}}+ (log/debugf "Starting Pulse Execution: %d" pulse-id)+ (pulse.send/send-pulse! pulse :channel-ids channel-ids :async? true)+ (log/debugf "Finished Pulse Execution: %d" pulse-id)+ :done)))(log/debugf "Pulse %d not found, Skipping." pulse-id))(catch Throwable e(log/errorf "Error sending Pulse %d to channel ids: %s: %s" pulse-id (str/join ", " channel-ids) (ex-message e))))))diff --git a/test/metabase/dashboards/models/dashboard_test.clj b/test/metabase/dashboards/models/dashboard_test.cljindex e61421a09da..f6a66fb34a9 100644--- a/test/metabase/dashboards/models/dashboard_test.clj+++ b/test/metabase/dashboards/models/dashboard_test.clj@@ -85,23 +85,105 @@:slug "unnamed"}(first (:parameters dashboard))))))))-(deftest archive-dashboard-delete-pulse-test+(defn- do-with-dashboard-subscription!+ "Call `f` with the ids of a Dashboard in a Collection and a subscription to it, with one enabled PulseChannel and a+ trigger."+ [f](pulse-channel-test/with-send-pulse-setup!- (mt/with-temp [:model/Card {card-id :id} {}- :model/Dashboard {dash-id :id} {}- :model/DashboardCard {dc-id :id} {:dashboard_id dash-id- :card_id card-id}- :model/Pulse {pulse-id :id} {:dashboard_id dash-id}- :model/PulseChannel _ {:pulse_id pulse-id}- :model/PulseCard _ {:pulse_id pulse-id- :card_id card-id- :dashboard_card_id dc-id}]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-id :id} {:collection_id coll-id}+ :model/Dashboard {dash-id :id} {:collection_id coll-id}+ :model/DashboardCard {dc-id :id} {:dashboard_id dash-id+ :card_id card-id}+ :model/Pulse {pulse-id :id} {:dashboard_id dash-id+ :collection_id coll-id}+ :model/PulseChannel {pc-id :id} {:pulse_id pulse-id}+ :model/PulseCard _ {:pulse_id pulse-id+ :card_id card-id+ :dashboard_card_id dc-id}](testing "sanity check that we have a trigger"(is (= 1 (count (pulse-channel-test/send-pulse-triggers pulse-id)))))- (t2/update! :model/Dashboard dash-id {:archived true})- (testing "archiving a Dashboard should delete its Pulse and SendPulse triggers"- (is (nil? (t2/select-one :model/Pulse pulse-id)))- (is (= 0 (count (pulse-channel-test/send-pulse-triggers pulse-id))))))))+ (f {:coll-id coll-id :dash-id dash-id :pulse-id pulse-id :pc-id pc-id}))))++(defn- trigger-pc-ids [pulse-id]+ (into #{} (mapcat #(get-in % [:data "channel-ids"])) (pulse-channel-test/send-pulse-triggers pulse-id)))++(deftest archive-dashboard-keeps-subscriptions-test+ (do-with-dashboard-subscription!+ (fn [{:keys [dash-id pulse-id pc-id]}]+ (let [pc-ids-before (trigger-pc-ids pulse-id)]+ (t2/update! :model/Dashboard dash-id {:archived true})+ (testing "archiving a Dashboard keeps its Pulse, PulseChannel, PulseCard and SendPulse trigger"+ (is (=? {:archived false} (t2/select-one :model/Pulse pulse-id)))+ (is (=? {:enabled true} (t2/select-one :model/PulseChannel pc-id)))+ (is (= 1 (t2/count :model/PulseCard :pulse_id pulse-id)))+ (is (= pc-ids-before (trigger-pc-ids pulse-id))))+ (t2/update! :model/Dashboard dash-id {:archived false})+ (testing "restoring the Dashboard leaves the subscription as it was"+ (is (=? {:archived false} (t2/select-one :model/Pulse pulse-id)))+ (is (=? {:enabled true} (t2/select-one :model/PulseChannel pc-id)))+ (is (= 1 (t2/count :model/PulseCard :pulse_id pulse-id)))+ (is (= pc-ids-before (trigger-pc-ids pulse-id))))))))++(deftest archive-dashboard-keeps-deleted-subscription-deleted-test+ (do-with-dashboard-subscription!+ (fn [{:keys [dash-id pulse-id]}]+ (testing "a subscription the user deleted before the Dashboard was trashed stays deleted after restore"+ (t2/update! :model/Pulse pulse-id {:archived true})+ (t2/update! :model/Dashboard dash-id {:archived true})+ (t2/update! :model/Dashboard dash-id {:archived false})+ (is (=? {:archived true} (t2/select-one :model/Pulse pulse-id)))+ (is (= #{} (pulse-channel-test/send-pulse-triggers pulse-id)))))))++(deftest archive-collection-keeps-dashboard-subscriptions-test+ (do-with-dashboard-subscription!+ (fn [{:keys [coll-id dash-id pulse-id pc-id]}]+ (let [pc-ids-before (trigger-pc-ids pulse-id)]+ (mt/with-current-user (mt/user->id :crowberto)+ (collection/archive-or-unarchive-collection! (t2/select-one :model/Collection coll-id) {:archived true})+ (testing "archiving the parent Collection leaves the subscription and its trigger as they were"+ ;; the trashed Dashboard is what stops the trigger from sending+ (is (=? {:archived true} (t2/select-one :model/Dashboard dash-id)))+ (is (=? {:archived false} (t2/select-one :model/Pulse pulse-id)))+ (is (=? {:enabled true} (t2/select-one :model/PulseChannel pc-id)))+ (is (= pc-ids-before (trigger-pc-ids pulse-id))))+ (collection/archive-or-unarchive-collection! (t2/select-one :model/Collection coll-id) {:archived false})+ (testing "restoring the Collection leaves the subscription and its trigger as they were"+ (is (=? {:archived false} (t2/select-one :model/Pulse pulse-id)))+ (is (=? {:enabled true} (t2/select-one :model/PulseChannel pc-id)))+ (is (= pc-ids-before (trigger-pc-ids pulse-id)))))))))++(deftest archive-collection-keeps-deleted-subscription-deleted-test+ (do-with-dashboard-subscription!+ (fn [{:keys [coll-id pulse-id pc-id]}]+ (testing "a subscription the user deleted before its Collection was trashed stays deleted after restore"+ (t2/update! :model/Pulse pulse-id {:archived true})+ (mt/with-current-user (mt/user->id :crowberto)+ (collection/archive-or-unarchive-collection! (t2/select-one :model/Collection coll-id) {:archived true})+ (collection/archive-or-unarchive-collection! (t2/select-one :model/Collection coll-id) {:archived false}))+ (is (=? {:archived true} (t2/select-one :model/Pulse pulse-id)))+ (is (=? {:enabled false} (t2/select-one :model/PulseChannel pc-id)))+ (is (= #{} (pulse-channel-test/send-pulse-triggers pulse-id)))))))++(deftest delete-dashboard-deletes-subscriptions-test+ (doseq [archived? [true false]]+ (testing (format "hard deleting a Dashboard (archived? %s) deletes its Pulse and SendPulse triggers" archived?)+ (do-with-dashboard-subscription!+ (fn [{:keys [dash-id pulse-id]}]+ (when archived?+ (t2/update! :model/Dashboard dash-id {:archived true}))+ (t2/delete! :model/Dashboard dash-id)+ (is (nil? (t2/select-one :model/Pulse pulse-id)))+ (is (= #{} (pulse-channel-test/send-pulse-triggers pulse-id))))))))++(deftest delete-collection-deletes-trashed-dashboard-subscriptions-test+ (testing "hard deleting a Collection holding a directly trashed Dashboard deletes its Pulse and SendPulse triggers"+ (do-with-dashboard-subscription!+ (fn [{:keys [coll-id dash-id pulse-id]}]+ (t2/update! :model/Dashboard dash-id {:archived true})+ (t2/delete! :model/Collection coll-id)+ (is (nil? (t2/select-one :model/Pulse pulse-id)))+ (is (= #{} (pulse-channel-test/send-pulse-triggers pulse-id)))))))(deftest ^:parallel parameter-card-test(testing "A new dashboard creates a new ParameterCard"diff --git a/test/metabase/dashboards_rest/api_test.clj b/test/metabase/dashboards_rest/api_test.cljindex 85a6d0fae2e..0ed03eb8cc9 100644--- a/test/metabase/dashboards_rest/api_test.clj+++ b/test/metabase/dashboards_rest/api_test.clj@@ -6095,6 +6095,55 @@(is (=? {:archived false :collection_id collection-id}(t2/select-one :model/Dashboard :id dashboard-id))))))+;; Subscription setup and listing go through these helpers, so the round trip below outlives a move of Dashboard+;; subscriptions off Pulse: swap the helpers, keep the assertions.+(defn- create-dashboard-subscription!+ [dashboard-id card-id dashcard-id recipient-id]+ (mt/user-http-request :crowberto :post 200 "pulse"+ {:name "Subscription"+ :dashboard_id dashboard-id+ :cards [{:id card-id :include_csv false :include_xls false :dashboard_card_id dashcard-id}]+ :channels [{:enabled true+ :channel_type "email"+ :schedule_type "daily"+ :schedule_hour 12+ :schedule_day nil+ :recipients [{:id recipient-id}]}]}))++(defn- dashboard-subscriptions+ "The Dashboard's subscriptions, each as its card ids and its channels' type, schedule and recipients."+ [dashboard-id]+ (for [subscription (mt/user-http-request :crowberto :get 200 "pulse" :dashboard_id dashboard-id)]+ {:cards (map :id (:cards subscription))+ :channels (for [channel (:channels subscription)]+ {:channel_type (:channel_type channel)+ :schedule_type (:schedule_type channel)+ :schedule_hour (:schedule_hour channel)+ :recipients (map :id (:recipients channel))})}))++(deftest trash-and-restore-dashboard-keeps-subscriptions-test+ (testing "trashing then restoring a Dashboard brings its subscriptions back unchanged"+ (mt/with-fake-inbox+ (mt/with-temp [:model/Collection {collection-id :id} {}+ :model/Card {card-id :id} {:collection_id collection-id}+ :model/Dashboard {dashboard-id :id} {:collection_id collection-id}+ :model/DashboardCard {dashcard-id :id} {:dashboard_id dashboard-id :card_id card-id}]+ (mt/with-model-cleanup [:model/Pulse]+ (let [recipient-id (mt/user->id :rasta)+ {subscription-id :id} (create-dashboard-subscription! dashboard-id card-id dashcard-id recipient-id)+ before (dashboard-subscriptions dashboard-id)]+ (is (=? [{:cards [card-id] :channels [{:recipients [recipient-id]}]}]+ before))+ (mt/user-http-request :crowberto :put 200 (str "dashboard/" dashboard-id) {:archived true})+ (testing "a trashed Dashboard lists no subscriptions"+ (is (empty? (dashboard-subscriptions dashboard-id))))+ (mt/user-http-request :crowberto :put 200 (str "dashboard/" dashboard-id) {:archived false})+ (is (= before (dashboard-subscriptions dashboard-id)))+ (testing "permanently deleting the Dashboard deletes its subscriptions"+ (mt/user-http-request :crowberto :put 200 (str "dashboard/" dashboard-id) {:archived true})+ (mt/user-http-request :crowberto :delete 204 (str "dashboard/" dashboard-id))+ (mt/user-http-request :crowberto :get 404 (str "pulse/" subscription-id)))))))))+(deftest copy-dashboard-permission-model-test(testing "POST /api/dashboard/:id/copy needs only read-on-source + create-on-destination"(mt/with-non-admin-groups-no-root-collection-permsdiff --git a/test/metabase/pulse/task/send_pulses_test.clj b/test/metabase/pulse/task/send_pulses_test.cljindex 5eab7f21ccb..4c42ebbd04c 100644--- a/test/metabase/pulse/task/send_pulses_test.clj+++ b/test/metabase/pulse/task/send_pulses_test.clj@@ -6,6 +6,7 @@[java-time.api :as t][metabase.driver :as driver][metabase.notification.test-util :as notification.tu]+ [metabase.pulse.db :as pulse.db][metabase.pulse.models.pulse-channel-test :as pulse-channel-test][metabase.pulse.send :as pulse.send][metabase.pulse.task.send-pulses :as task.send-pulses]@@ -130,6 +131,43 @@(testing "channels that has no recipients are deleted"(is (false? (t2/exists? :model/PulseChannel pc-no-recipient)))))))))+(deftest send-pulse!*-skips-archived-dashboard-test+ (testing "a trigger firing for a trashed Dashboard sends nothing and records no task run"+ (let [sent-channel-ids (atom #{})]+ (mt/with-dynamic-fn-redefs [pulse.send/send-pulse! (fn [_pulse & {:keys [channel-ids]}]+ (swap! sent-channel-ids set/union channel-ids))]+ (mt/with-temp [:model/Dashboard {dash-id :id} {}+ :model/Pulse {pulse-id :id} {:dashboard_id dash-id}+ :model/PulseChannel {pc-id :id} (merge {:pulse_id pulse-id+ :channel_type :slack+ :details {:channel "#random"}}+ daily-at-1am)]+ ;; keyed on the Dashboard rather than the Pulse: subscription runs are recorded this way on both the Pulse and+ ;; the Notification send paths+ (let [task-runs #(t2/select-pks-set :model/TaskRun :entity_type :dashboard :entity_id dash-id)]+ (try+ (t2/update! :model/Dashboard dash-id {:archived true})+ (#'task.send-pulses/send-pulse!* pulse-id #{pc-id})+ (is (= #{} @sent-channel-ids))+ (is (empty? (task-runs)))+ (testing "once the Dashboard is restored, the same fire sends and records a task run"+ (t2/update! :model/Dashboard dash-id {:archived false})+ (#'task.send-pulses/send-pulse!* pulse-id #{pc-id})+ (is (= #{pc-id} @sent-channel-ids))+ (is (= 1 (count (task-runs)))))+ (finally+ (t2/delete! :model/TaskHistory :run_id [:in (conj (task-runs) -1)])+ (t2/delete! :model/TaskRun :entity_type :dashboard :entity_id dash-id)))))))))++(deftest dashboard-archived?-test+ (mt/with-temp [:model/Dashboard {live-id :id} {}+ :model/Dashboard {archived-id :id} {:archived true}]+ (is (false? (pulse.db/dashboard-archived? live-id)))+ (is (true? (pulse.db/dashboard-archived? archived-id)))+ (testing "a Pulse with no Dashboard, or a missing Dashboard, is not archived"+ (is (false? (pulse.db/dashboard-archived? nil)))+ (is (false? (pulse.db/dashboard-archived? Integer/MAX_VALUE))))))+(deftest init-dashboard-subscription-triggers!-group-runs-test(testing "a SendPulse trigger will send pulse to channels that have the same schedueld time"(pulse-channel-test/with-send-pulse-setup!