Skip to content
Snippets Groups Projects
Unverified Commit 8fe0b175 authored by Jerry Huang's avatar Jerry Huang Committed by GitHub
Browse files

Fix pulse channel sending n+1 sql calls (#31413)

* initial changes

* revert test

* fix test
parent 5f6f1d23
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,7 @@
[clojurewerkz.quartzite.schedule.cron :as cron]
[clojurewerkz.quartzite.triggers :as triggers]
[metabase.driver :as driver]
[metabase.models :refer [PulseChannel PulseChannelRecipient]]
[metabase.models :refer [PulseChannel]]
[metabase.models.pulse :as pulse]
[metabase.models.pulse-channel :as pulse-channel]
[metabase.models.task-history :as task-history]
......@@ -63,14 +63,19 @@
(catch Throwable e
(on-error pulse-id e)))))))
; Clearing pulse channels is not done synchronously in order to support undoing feature.
(s/defn ^:private clear-pulse-channels!
[]
(doseq [channel (t2/select PulseChannel)]
(let [pulse-channel-id (:id channel)]
(when (and (nil? (get-in channel [:details :emails]))
(nil? (get-in channel [:details :channel]))
(zero? (t2/count PulseChannelRecipient :pulse_channel_id pulse-channel-id)))
(t2/delete! PulseChannel :id pulse-channel-id)))))
(when-let [ids-to-delete (seq
(for [channel (t2/select [PulseChannel :id :details]
:id [:not-in {:select [[:pulse_channel_id :id]]
:from :pulse_channel_recipient
:group-by [:pulse_channel_id]
:having [:>= :%count.* [:raw 1]]}])]
(when (and (empty? (get-in channel [:details :emails]))
(not (get-in channel [:details :channel])))
(:id channel))))]
(t2/delete! PulseChannel :id [:in ids-to-delete])))
;;; ------------------------------------------------------ Task ------------------------------------------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment