Skip to content

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

Add table-publish and table-unpublish events to the Activity log (#68899)

metamben47304d3144c2
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 5e53fdaa165..e7eb1ca3a5a 100644
--- a/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj
@@ -254,12 +254,12 @@
:set {:collection_id (:id target-collection)
:is_published true}
:where update-where})
- ;; Publish update events for remote sync tracking
+ ;; Publish events for audit log and remote sync tracking
(when (seq table-ids-to-update)
(let [updated-tables (t2/select :model/Table :id [:in table-ids-to-update])]
(doseq [table updated-tables]
- (events/publish-event! :event/table-update {:object table
- :user-id api/*current-user-id*}))))
+ (events/publish-event! :event/table-publish {:object table
+ :user-id api/*current-user-id*}))))
{:target_collection target-collection}))
(api.macros/defendpoint :post "/unpublish-tables" :- :nil
@@ -279,12 +279,12 @@
:set {:collection_id nil
:is_published false}
:where update-where})
- ;; Publish update events for remote sync tracking
+ ;; Publish events for audit log and remote sync tracking
(when (seq table-ids-to-update)
(let [updated-tables (t2/select :model/Table :id [:in table-ids-to-update])]
(doseq [table updated-tables]
- (events/publish-event! :event/table-update {:object table
- :user-id api/*current-user-id*}))))
+ (events/publish-event! :event/table-unpublish {:object table
+ :user-id api/*current-user-id*}))))
nil))
(defn- sync-schema-async!
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 f0482fe8fbd..5a37ea4c51b 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
@@ -17,7 +17,7 @@
(use-fixtures :once (fixtures/initialize :db))
(deftest publish-table-test
- (mt/with-premium-features #{:data-studio}
+ (mt/with-premium-features #{:data-studio :audit-app}
(without-library
(testing "POST /api/ee/data-studio/table/(un)publish-table"
(testing "publishes tables into the library-data collection"
@@ -36,6 +36,11 @@
:collection_id collection-id
:is_published true}]
(t2/select :model/Table :id [:in [(mt/id :users) (mt/id :venues)]] {:order-by [:display_name]}))))
+ (testing "audit log entries are created for publish"
+ (is (=? {:topic :table-publish, :model "Table", :model_id (mt/id :users)}
+ (mt/latest-audit-log-entry "table-publish" (mt/id :users))))
+ (is (=? {:topic :table-publish, :model "Table", :model_id (mt/id :venues)}
+ (mt/latest-audit-log-entry "table-publish" (mt/id :venues)))))
(testing "unpublishing"
(testing "normal users are not allowed"
(mt/user-http-request :rasta :post 403 "ee/data-studio/table/unpublish-tables"
@@ -45,22 +50,25 @@
(is (=? {:display_name "Venues"
:collection_id nil
:is_published false}
- (t2/select-one :model/Table (mt/id :venues)))))))
- (testing "deleting the collection unpublishes"
- (is (=? {:display_name "Users"
- :collection_id nil
- :is_published false}
- (t2/select-one :model/Table (mt/id :users))))))
- (testing "returns 404 when no library-data collection exists"
- (is (= "Not found."
- (mt/user-http-request :crowberto :post 404 "ee/data-studio/table/publish-tables"
- {:table_ids [(mt/id :users)]}))))
- (testing "returns 409 when multiple library-data collections exist"
- (mt/with-temp [:model/Collection _ {:type collection/library-data-collection-type}
- :model/Collection _ {:type collection/library-data-collection-type}]
- (is (= "Multiple library-data collections found."
- (mt/user-http-request :crowberto :post 409 "ee/data-studio/table/publish-tables"
- {:table_ids [(mt/id :users)]})))))))))
+ (t2/select-one :model/Table (mt/id :venues))))
+ (testing "audit log entry is created for unpublish"
+ (is (=? {:topic :table-unpublish, :model "Table", :model_id (mt/id :venues)}
+ (mt/latest-audit-log-entry "table-unpublish" (mt/id :venues)))))))))
+ (testing "deleting the collection unpublishes"
+ (is (=? {:display_name "Users"
+ :collection_id nil
+ :is_published false}
+ (t2/select-one :model/Table (mt/id :users))))))
+ (testing "returns 404 when no library-data collection exists"
+ (is (= "Not found."
+ (mt/user-http-request :crowberto :post 404 "ee/data-studio/table/publish-tables"
+ {:table_ids [(mt/id :users)]}))))
+ (testing "returns 409 when multiple library-data collections exist"
+ (mt/with-temp [:model/Collection _ {:type collection/library-data-collection-type}
+ :model/Collection _ {:type collection/library-data-collection-type}]
+ (is (= "Multiple library-data collections found."
+ (mt/user-http-request :crowberto :post 409 "ee/data-studio/table/publish-tables"
+ {:table_ids [(mt/id :users)]}))))))))
(deftest bulk-edit-visibility-sync-test
(mt/with-premium-features #{:data-studio}
diff --git a/src/metabase/audit_app/events/audit_log.clj b/src/metabase/audit_app/events/audit_log.clj
index f9568389d8f..16181ca1d89 100644
--- a/src/metabase/audit_app/events/audit_log.clj
+++ b/src/metabase/audit_app/events/audit_log.clj
@@ -69,6 +69,8 @@
(derive ::table-event ::event)
(derive :event/table-manual-scan ::table-event)
(derive :event/table-manual-sync ::table-event)
+(derive :event/table-publish ::table-event)
+(derive :event/table-unpublish ::table-event)
(methodical/defmethod events/publish-event! ::table-event
[topic event]
diff --git a/src/metabase/events/schema.clj b/src/metabase/events/schema.clj
index f84fa6605ca..7e47c6b7a27 100644
--- a/src/metabase/events/schema.clj
+++ b/src/metabase/events/schema.clj
@@ -195,6 +195,8 @@
(mr/def :event/table-create ::table)
(mr/def :event/table-update ::table)
(mr/def :event/table-delete ::table)
+(mr/def :event/table-publish ::table)
+(mr/def :event/table-unpublish ::table)
(mr/def ::permission-failure
[:map {:closed true}
diff --git a/src/metabase/remote_sync/events.clj b/src/metabase/remote_sync/events.clj
index 32342879e9c..98f0ecd9b0c 100644
--- a/src/metabase/remote_sync/events.clj
+++ b/src/metabase/remote_sync/events.clj
@@ -14,6 +14,8 @@
;; Table events
(derive ::table-event :metabase/event)
(derive :event/table-update ::table-event)
+(derive :event/table-publish ::table-event)
+(derive :event/table-unpublish ::table-event)
;; Timeline events
(derive ::timeline-event :metabase/event)
diff --git a/test/metabase/audit_app/events/audit_log_test.clj b/test/metabase/audit_app/events/audit_log_test.clj
index fc4fa1cfc38..59cfaf9dd78 100644
--- a/test/metabase/audit_app/events/audit_log_test.clj
+++ b/test/metabase/audit_app/events/audit_log_test.clj
@@ -782,3 +782,29 @@
:model "Table"
:model_id 123}
(mt/latest-audit-log-entry :table-data-edit)))))))
+
+(deftest table-publish-event-test
+ (testing ":event/table-publish event"
+ (mt/with-temp [:model/Table table {:name "My Cool Table"}]
+ (is (= {:object table :user-id (mt/user->id :rasta)}
+ (events/publish-event! :event/table-publish {:object table :user-id (mt/user->id :rasta)})))
+ (is (=?
+ {:topic :table-publish
+ :user_id (mt/user->id :rasta)
+ :model "Table"
+ :model_id (:id table)
+ :details {:name "My Cool Table", :id (:id table), :db_id (:db_id table)}}
+ (mt/latest-audit-log-entry "table-publish" (:id table)))))))
+
+(deftest table-unpublish-event-test
+ (testing ":event/table-unpublish event"
+ (mt/with-temp [:model/Table table {:name "My Cool Table"}]
+ (is (= {:object table :user-id (mt/user->id :rasta)}
+ (events/publish-event! :event/table-unpublish {:object table :user-id (mt/user->id :rasta)})))
+ (is (=?
+ {:topic :table-unpublish
+ :user_id (mt/user->id :rasta)
+ :model "Table"
+ :model_id (:id table)
+ :details {:name "My Cool Table", :id (:id table), :db_id (:db_id table)}}
+ (mt/latest-audit-log-entry "table-unpublish" (:id table)))))))