The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
[sync] Remove sync records for contents of de-synced collections (#74187)
Braden Shepherdson32768c936b69
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/core.cljindex f89dde6835c..15b5404d481 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj@@ -113,13 +113,22 @@(for [collection sync-on][:like :location (str (collections/location-path collection) "%")]))]}))(when (seq sync-off)- (t2/query {:update (t2/table-name :model/Collection)- :set {:is_remote_synced false}- :where [:and- [:= :is_remote_synced true]- (into [:or [:in :id (map :id sync-off)]]- (for [collection sync-off]- [:like :location (str (collections/location-path collection) "%")]))]}))+ (let [affected-collection-ids+ (t2/select-pks-set :model/Collection+ {:where [:and+ [:= :is_remote_synced true]+ (into [:or [:in :id (map :id sync-off)]]+ (for [collection sync-off]+ [:like :location (str (collections/location-path collection) "%")]))]})]+ (when (seq affected-collection-ids)+ (t2/query {:update (t2/table-name :model/Collection)+ :set {:is_remote_synced false}+ :where [:in :id affected-collection-ids]})+ (t2/delete! :model/RemoteSyncObject+ :model_type "Collection"+ :model_id [:in affected-collection-ids])+ (t2/delete! :model/RemoteSyncObject+ :model_collection_id [:in affected-collection-ids]))))(doseq [collection sync-on](collections/check-non-remote-synced-dependencies collection))(doseq [collection sync-off]diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/core_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/core_test.cljindex 6d1ef16838f..a933cc27832 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/core_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/core_test.clj@@ -2,6 +2,7 @@{:clj-kondo/config '{:linters {:deprecated-var {:exclude {metabase.test.data/mbql-query {:namespaces [metabase-enterprise.remote-sync.core-test]}}}}}}(:require[clojure.test :refer :all]+ [java-time.api :as t][metabase-enterprise.remote-sync.core :as core][metabase-enterprise.remote-sync.guards :as guards][metabase.collections.test-utils :refer [with-library-synced with-library-not-synced]]@@ -264,6 +265,56 @@(is (false? (:is_remote_synced (t2/select-one :model/Collection :id child1-id))))(is (false? (:is_remote_synced (t2/select-one :model/Collection :id child2-id)))))))+;;; ------------------------------------------- RSO Cleanup Tests -------------------------------------------++(deftest bulk-set-remote-sync-cleans-up-rsos-on-disable-test+ (testing "bulk-set-remote-sync deletes RemoteSyncObject entries for un-synced collections and their contents"+ (mt/with-temp [:model/Collection {coll-id :id} {:name "Test Collection" :location "/" :is_remote_synced true}+ :model/Card {card-id :id} {:name "Test Card" :collection_id coll-id+ :database_id (mt/id)+ :dataset_query (mt/mbql-query venues)}]+ (let [now (t/offset-date-time)]+ (t2/insert! :model/RemoteSyncObject+ {:model_type "Collection" :model_id coll-id :model_name "Test Collection"+ :status "update" :status_changed_at now})+ (t2/insert! :model/RemoteSyncObject+ {:model_type "Card" :model_id card-id :model_name "Test Card"+ :model_collection_id coll-id :status "update" :status_changed_at now})+ (is (= 2 (t2/count :model/RemoteSyncObject+ {:where [:or+ [:and [:= :model_type "Collection"] [:= :model_id coll-id]]+ [:= :model_collection_id coll-id]]})))+ (mt/with-dynamic-fn-redefs [events/publish-event! (constantly nil)]+ (core/bulk-set-remote-sync {coll-id false}))+ (is (= 0 (t2/count :model/RemoteSyncObject+ {:where [:or+ [:and [:= :model_type "Collection"] [:= :model_id coll-id]]+ [:= :model_collection_id coll-id]]})))))))++(deftest bulk-set-remote-sync-cleans-up-descendant-rsos-test+ (testing "bulk-set-remote-sync deletes RSOs for descendant collections and their contents"+ (mt/with-temp [:model/Collection {parent-id :id} {:name "Parent" :location "/" :is_remote_synced true}+ :model/Collection {child-id :id} {:name "Child" :location (format "/%d/" parent-id)+ :is_remote_synced true}+ :model/Card {card-id :id} {:name "Child Card" :collection_id child-id+ :database_id (mt/id)+ :dataset_query (mt/mbql-query venues)}]+ (let [now (t/offset-date-time)]+ (t2/insert! :model/RemoteSyncObject+ {:model_type "Collection" :model_id parent-id :model_name "Parent"+ :status "update" :status_changed_at now})+ (t2/insert! :model/RemoteSyncObject+ {:model_type "Collection" :model_id child-id :model_name "Child"+ :status "update" :status_changed_at now})+ (t2/insert! :model/RemoteSyncObject+ {:model_type "Card" :model_id card-id :model_name "Child Card"+ :model_collection_id child-id :status "create" :status_changed_at now})+ (mt/with-dynamic-fn-redefs [events/publish-event! (constantly nil)]+ (core/bulk-set-remote-sync {parent-id false}))+ (is (false? (t2/exists? :model/RemoteSyncObject :model_type "Collection" :model_id parent-id)))+ (is (false? (t2/exists? :model/RemoteSyncObject :model_type "Collection" :model_id child-id)))+ (is (false? (t2/exists? :model/RemoteSyncObject :model_id card-id)))))))+;;; ------------------------------------------- batch-model-eligible? Tests -------------------------------------------(deftest batch-model-eligible-for-remote-sync-cards-in-remote-synced-collection-test