Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Don't attempt to add revisions for viewing docs (#69305)

John Swanson4dcab243b975
diff --git a/src/metabase/documents/view_log.clj b/src/metabase/documents/view_log.clj
index 82ffd9b2a60..6b4fe7d3b03 100644
--- a/src/metabase/documents/view_log.clj
+++ b/src/metabase/documents/view_log.clj
@@ -26,7 +26,9 @@
(fn [xs] (apply t/max (map :timestamp xs))))]
(try
(cluster-lock/with-cluster-lock document-statistics-lock
- (t2/update! :model/Document :id [:in (keys document-id->timestamp)]
+ ;; use `t2/table-name` to avoid triggering `after-update` hooks and creating a revision
+ (t2/update! (t2/table-name :model/Document)
+ :id [:in (keys document-id->timestamp)]
{:last_viewed_at (into [:case]
(mapcat (fn [[id timestamp]]
[[:= :id id] [:greatest [:coalesce :last_viewed_at (t/offset-date-time 0)] timestamp]])
diff --git a/test/metabase/documents/view_log_test.clj b/test/metabase/documents/view_log_test.clj
index 6620cd171e1..501b879f342 100644
--- a/test/metabase/documents/view_log_test.clj
+++ b/test/metabase/documents/view_log_test.clj
@@ -37,16 +37,17 @@
(latest-view (:id user) (:id document))))))))
(deftest document-read-oss-no-view-logging-test
- (mt/with-temp [:model/Collection collection {}
- :model/User user {}
- :model/Document document {:collection_id (:id collection)
- :name "Test Document"
- :document "{\"type\":\"doc\",\"content\":[]}"
- :creator_id (:id user)}]
- (testing "A basic document read event is not recorded without audit-app"
- (events/publish-event! :event/document-read {:object-id (:id document) :user-id (:id user)})
- (is (nil? (latest-view (:id user) (:id document)))
- "view log entries should not be made without audit-app feature"))))
+ (mt/with-premium-features #{}
+ (mt/with-temp [:model/Collection collection {}
+ :model/User user {}
+ :model/Document document {:collection_id (:id collection)
+ :name "Test Document"
+ :document "{\"type\":\"doc\",\"content\":[]}"
+ :creator_id (:id user)}]
+ (testing "A basic document read event is not recorded without audit-app"
+ (events/publish-event! :event/document-read {:object-id (:id document) :user-id (:id user)})
+ (is (nil? (latest-view (:id user) (:id document)))
+ "view log entries should not be made without audit-app feature")))))
(deftest document-read-view-count-test
(mt/test-helpers-set-global-values!
@@ -100,6 +101,23 @@
t/offset-date-time
(.withNano 0))))))))
+(deftest update-last-viewed-at-does-not-trigger-after-update-test
+ (testing "updating last_viewed_at should not trigger the Document after-update hook"
+ (mt/with-temp [:model/Collection collection {}
+ :model/User user {}
+ :model/Document document {:collection_id (:id collection)
+ :name "Test Document"
+ :document "{\"type\":\"doc\",\"content\":[]}"
+ :creator_id (:id user)}]
+ (let [events-published (atom [])]
+ (with-redefs [events/publish-event! (fn [topic event]
+ (swap! events-published conj topic)
+ event)]
+ (#'documents.view-log/update-document-last-viewed-at!*
+ [{:id (:id document) :timestamp (t/offset-date-time)}])
+ (is (not (contains? (set @events-published) :event/document-update))
+ "updating last_viewed_at should not publish :event/document-update"))))))
+
(deftest document-event-derivation-test
(testing "Document events are properly derived from base events"
(is (isa? :metabase.documents.view-log/document-read :metabase/event))