Skip to content

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

Fix the remote sync eligibility check for Documents (#84697)

Timothy S. Deanec23d7a75575
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/core_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/core_test.clj
index a332a4060fd..8e0d8833a1b 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/core_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/core_test.clj
@@ -9,6 +9,7 @@
[metabase-enterprise.remote-sync.test-helpers :as rs.test]
[metabase.collections.models.collection :as collection]
[metabase.collections.test-utils :refer [with-library with-library-synced with-library-not-synced]]
+ [metabase.documents.test-util :as doc.tu]
[metabase.events.core :as events]
[metabase.remote-sync.test-util :as remote-sync.tu]
[metabase.test :as mt]
@@ -475,6 +476,29 @@
#"Used by remote synced content"
(core/bulk-set-remote-sync {coll1-id false}))))))
+(deftest bulk-set-remote-sync-enables-collection-holding-document-test
+ (testing "a collection's own Document does not block enabling remote sync on that collection"
+ (mt/with-temp [:model/Collection {coll-id :id} {:name "Docs" :location "/" :is_remote_synced false}
+ :model/Document _ {:collection_id coll-id}]
+ (core/bulk-set-remote-sync {coll-id true})
+ (is (true? (:is_remote_synced (t2/select-one :model/Collection :id coll-id)))))))
+
+(deftest bulk-set-remote-sync-throws-on-remote-synced-document-dependent-test
+ (testing "bulk-set-remote-sync throws when disabling a collection whose card a remote-synced Document embeds"
+ (mt/with-temp [:model/Collection {coll1-id :id} {:name "Collection 1" :location "/" :is_remote_synced true}
+ :model/Collection {coll2-id :id} {:name "Collection 2" :location "/" :is_remote_synced true}
+ :model/Card {card-id :id} {:name "Source Card"
+ :collection_id coll1-id
+ :database_id (mt/id)
+ :dataset_query (mt/mbql-query venues)}
+ :model/Document _ {:collection_id coll2-id
+ :document (doc.tu/cards->prose-mirror-ast [card-id])
+ :content_type "application/json+vnd.prose-mirror"}]
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Used by remote synced content"
+ (core/bulk-set-remote-sync {coll1-id false}))))))
+
(deftest bulk-set-remote-sync-empty-map-no-op-test
(testing "bulk-set-remote-sync with empty map is a no-op"
(mt/with-temp [:model/Collection {coll-id :id} {:name "Test Collection" :location "/" :is_remote_synced false}]
diff --git a/src/metabase/collections/models/collection.clj b/src/metabase/collections/models/collection.clj
index a228cad9d6e..cd616561298 100644
--- a/src/metabase/collections/models/collection.clj
+++ b/src/metabase/collections/models/collection.clj
@@ -1419,6 +1419,9 @@
;; Actions without a model are synced with the Library rather than their collection
:model/Action
(collections.db/instances-with-columns [model-key :id :collection_id :model_id] ids)
+ ;; The Document spec's `:conditions` read `:exploration_id`; a row without the key never matches
+ :model/Document
+ (collections.db/instances-with-columns [model-key :id :collection_id :exploration_id] ids)
;; DashboardCard and DashboardCardSeries are nested under Dashboard - skip separate eligibility check
(:model/DashboardCard :model/DashboardCardSeries)
nil