Skip to content

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

Publish / unpublish tables trigger hooks properly (#77374)

Chris Truter6097aa447b5f
diff --git a/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj b/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj
index fb3d58159d5..5ee7159af4f 100644
--- a/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj
@@ -127,10 +127,9 @@
table-ids-to-update (when (seq downstream-ids)
(t2/select-pks-set :model/Table :id [:in downstream-ids] :is_published true))]
(when (seq table-ids-to-update)
- (t2/query {:update (t2/table-name :model/Table)
- :set {:collection_id nil
- :is_published false}
- :where [:in :id table-ids-to-update]})
+ (t2/update! :model/Table :id [:in table-ids-to-update]
+ {:collection_id nil
+ :is_published false})
;; Publish events for audit log and remote sync tracking
(let [updated-tables (t2/select :model/Table :id [:in table-ids-to-update])]
(doseq [table updated-tables]
@@ -175,12 +174,11 @@
;; Get table IDs before update for event publishing
table-ids-to-update (t2/select-pks-set :model/Table {:where update-where})]
(api/check-403 (can-publish-all-tables? table-ids-to-update))
- (t2/query {:update (t2/table-name :model/Table)
- :set {:collection_id (:id target-collection)
- :is_published true}
- :where update-where})
- ;; Publish events for audit log and remote sync tracking
(when (seq table-ids-to-update)
+ (t2/update! :model/Table :id [:in table-ids-to-update]
+ {:collection_id (:id target-collection)
+ :is_published true})
+ ;; Publish events for audit log and remote sync tracking
(let [updated-tables (t2/select :model/Table :id [:in table-ids-to-update])]
(doseq [table updated-tables]
(events/publish-event! :event/table-publish {:object table
@@ -201,12 +199,11 @@
;; Get table IDs before update for event publishing
table-ids-to-update (t2/select-pks-set :model/Table {:where update-where})]
(api/check-403 (can-publish-all-tables? table-ids-to-update))
- (t2/query {:update (t2/table-name :model/Table)
- :set {:collection_id nil
- :is_published false}
- :where update-where})
- ;; Publish events for audit log and remote sync tracking
(when (seq table-ids-to-update)
+ (t2/update! :model/Table :id [:in table-ids-to-update]
+ {:collection_id nil
+ :is_published false})
+ ;; Publish events for audit log and remote sync tracking
(let [updated-tables (t2/select :model/Table :id [:in table-ids-to-update])]
(doseq [table updated-tables]
(events/publish-event! :event/table-unpublish {:object table
diff --git a/enterprise/backend/test/metabase_enterprise/data_studio/api/table_test.clj b/enterprise/backend/test/metabase_enterprise/data_studio/api/table_test.clj
index e8a9e8b7c2d..2c3a08ec9ab 100644
--- a/enterprise/backend/test/metabase_enterprise/data_studio/api/table_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_studio/api/table_test.clj
@@ -2,6 +2,7 @@
"Tests for /api/ee/data-studio/table endpoints (enterprise-only: publish-tables, unpublish-tables)."
(:require
[clojure.test :refer :all]
+ [java-time.api :as t]
[medley.core :as m]
[metabase.collections.models.collection :as collection]
[metabase.collections.test-utils :refer [without-library]]
@@ -100,6 +101,31 @@
:is_published true}
(t2/select-one :model/Table (mt/id :users)))))))))
+(deftest publish-tables-goes-through-toucan-hooks-test
+ (testing "publish/unpublish update via the Toucan pipeline, so model hooks fire"
+ (mt/with-premium-features #{:library}
+ (without-library
+ (mt/with-temp [:model/Collection {collection-id :id} {:type collection/library-data-collection-type}]
+ ;; a bumped updated_at proves the write went through the Toucan pipeline (:hook/timestamped?),
+ ;; the same pipeline that fires :hook/search-index; a raw UPDATE would leave it unchanged
+ (let [baseline (t/offset-date-time 2020)
+ ;; raw update, precisely to keep the timestamped hook from overwriting the backdate
+ backdate! #(t2/query {:update (t2/table-name :model/Table)
+ :set {:updated_at baseline}
+ :where [:= :id (mt/id :venues)]})
+ updated-at #(t2/select-one-fn :updated_at :model/Table (mt/id :venues))]
+ (backdate!)
+ (mt/user-http-request :crowberto :post 200 "ee/data-studio/table/publish-tables"
+ {:table_ids [(mt/id :venues)]
+ :collection_id collection-id})
+ (testing "updated_at bumps on publish"
+ (is (pos? (compare (updated-at) baseline))))
+ (backdate!)
+ (mt/user-http-request :crowberto :post 204 "ee/data-studio/table/unpublish-tables"
+ {:table_ids [(mt/id :venues)]})
+ (testing "updated_at bumps on unpublish"
+ (is (pos? (compare (updated-at) baseline))))))))))
+
(deftest requests-data-studio-feature-flag-test
(mt/with-premium-features #{}
(is (= "Library is a paid feature not currently available to your instance. Please upgrade to use it. Learn more at metabase.com/upgrade/"