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
fix: better check for dirty collection state (#69974)
Edward Paget071fc345b49c
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/api.cljindex fe4d3bac4ea..734a911fef9 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj@@ -144,30 +144,42 @@(api/check-superuser)(api/check-400 (not (and (remote-sync.object/dirty?) (= :read-only remote-sync-type)))"There are unsaved changes in the Remote Sync collection which will be overwritten switching to read-only mode.")- ;; Check if trying to change collections while in read-only mode- (let [effective-type (or remote-sync-type (settings/remote-sync-type))]- (api/check-400 (not (and (seq collections) (= :read-only effective-type)))- "Cannot change synced collections when remote-sync-type is read-only."))- (when (seq collections)+ ;; Filter out no-op collection changes before checking read-only mode or running bulk-set+ (let [collections (when (seq collections)+ (let [current-states (into {}+ (map (juxt :id :is_remote_synced))+ (t2/select [:model/Collection :id :is_remote_synced]+ :id [:in (keys collections)]))]+ (not-empty+ (into {}+ (filter (fn [[id desired]]+ (not= (boolean desired)+ (boolean (get current-states id)))))+ collections))))]+ ;; Check if trying to change collections while in read-only mode+ (let [effective-type (or remote-sync-type (settings/remote-sync-type))]+ (api/check-400 (not (and (seq collections) (= :read-only effective-type)))+ "Cannot change synced collections when remote-sync-type is read-only."))+ (when (seq collections)+ (try+ (remote-sync.core/bulk-set-remote-sync collections)+ (catch Exception e+ (throw (ex-info (or (ex-message e) "Invalid collection settings")+ {:error (ex-message e)+ :status-code 400} e)))))(try- (remote-sync.core/bulk-set-remote-sync collections)+ (settings/check-and-update-remote-settings! (dissoc settings :collections))(catch Exception e- (throw (ex-info (or (ex-message e) "Invalid collection settings")+ (throw (ex-info (or (ex-message e) "Invalid settings"){:error (ex-message e)- :status-code 400} e)))))- (try- (settings/check-and-update-remote-settings! (dissoc settings :collections))- (catch Exception e- (throw (ex-info (or (ex-message e) "Invalid settings")- {:error (ex-message e)- :status-code 400} e))))- (events/publish-event! :event/remote-sync-settings-update- {:details {:remote-sync-type remote-sync-type}- :user-id api/*current-user-id*})- (if-let [task-id (impl/finish-remote-config!)]- {:success true- :task_id task-id}- {:success true}))+ :status-code 400} e))))+ (events/publish-event! :event/remote-sync-settings-update+ {:details {:remote-sync-type remote-sync-type}+ :user-id api/*current-user-id*})+ (if-let [task-id (impl/finish-remote-config!)]+ {:success true+ :task_id task-id}+ {:success true})))(api.macros/defendpoint :get "/branches" :- remote-sync.schema/BranchesResponse"Get list of branches from the configured source.diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.cljindex f3ba6977df0..261b57334a9 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj@@ -2,6 +2,7 @@(:require[clojure.test :refer :all][diehard.core :as dh]+ [metabase-enterprise.remote-sync.core :as remote-sync.core][metabase-enterprise.remote-sync.impl :as impl][metabase-enterprise.remote-sync.models.remote-sync-object :as remote-sync.object][metabase-enterprise.remote-sync.models.remote-sync-task :as remote-sync.task]@@ -713,6 +714,43 @@(mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"{:collections {coll-id true}})))))))))+(deftest settings-collections-no-op-skips-read-only-check-test+ (testing "PUT /api/ee/remote-sync/settings skips read-only error when collections have no actual changes"+ (mt/with-temp [:model/Collection {unsynced-id :id} {:name "Unsynced Collection" :location "/" :is_remote_synced false}+ :model/Collection {synced-id :id} {:name "Synced Collection" :location "/" :is_remote_synced true}]+ (with-redefs [settings/check-and-update-remote-settings! (constantly nil)+ impl/finish-remote-config! (constantly nil)]+ (testing "sending false for already-unsynced collection in read-only mode succeeds"+ (is (= {:success true}+ (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"+ {:remote-sync-type :read-only+ :collections {unsynced-id false}}))))+ (testing "sending true for already-synced collection in read-only mode succeeds"+ (is (= {:success true}+ (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"+ {:remote-sync-type :read-only+ :collections {synced-id true}}))))+ (testing "mix of no-op and real change still rejects in read-only mode"+ (is (= "Cannot change synced collections when remote-sync-type is read-only."+ (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"+ {:remote-sync-type :read-only+ :collections {synced-id true unsynced-id true}}))))))))++(deftest settings-collections-no-op-skips-bulk-set-test+ (testing "PUT /api/ee/remote-sync/settings skips bulk-set-remote-sync when collections have no actual changes"+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-temp [:model/Collection {synced-id :id} {:name "Synced Collection" :location "/" :is_remote_synced true}+ :model/Collection {unsynced-id :id} {:name "Unsynced Collection" :location "/" :is_remote_synced false}]+ (let [bulk-set-called? (atom false)]+ (with-redefs [settings/check-and-update-remote-settings! (constantly nil)+ impl/finish-remote-config! (constantly nil)+ remote-sync.core/bulk-set-remote-sync (fn [& _] (reset! bulk-set-called? true))]+ (testing "no-op collections do not call bulk-set-remote-sync"+ (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"+ {:collections {synced-id true unsynced-id false}})]+ (is (= {:success true} response))+ (is (false? @bulk-set-called?))))))))))+(deftest settings-collections-allows-changes-in-read-write-mode-test(testing "PUT /api/ee/remote-sync/settings allows collection changes when remote-sync-type is read-write"(mt/with-temp [:model/Collection {coll-id :id} {:name "Test Collection" :location "/" :is_remote_synced false}]diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/RemoteSyncSettingsForm.setup.spec.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/RemoteSyncSettingsForm.setup.spec.tsxindex a645cebea63..63149c0512a 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/RemoteSyncSettingsForm.setup.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/RemoteSyncSettingsForm.setup.spec.tsx@@ -8,7 +8,7 @@ import {} from "__support__/server-mocks";import { mockSettings } from "__support__/settings";import { renderWithProviders } from "__support__/ui";-import type { Collection } from "metabase-types/api";+import type { Collection, CollectionItem } from "metabase-types/api";import {createMockCollection,createMockSettings,@@ -41,6 +41,8 @@ const setupEndpoints = ({remoteSyncTransforms = false,libraryCollection = null as Collection | null,dirty = [] as any[],+ rootCollectionItems = [] as CollectionItem[],+ settingsError,}: {remoteSyncEnabled?: boolean;remoteSyncUrl?: string;@@ -51,6 +53,8 @@ const setupEndpoints = ({remoteSyncTransforms?: boolean;libraryCollection?: Collection | null;dirty?: any[];+ rootCollectionItems?: CollectionItem[];+ settingsError?: { status: number; message: string };} = {}) => {const settings = createMockSettings({"remote-sync-enabled": remoteSyncEnabled,@@ -64,11 +68,17 @@ const setupEndpoints = ({setupPropertiesEndpoints(settings);setupSettingsEndpoints([]);- setupRemoteSyncEndpoints({ dirty, branches: [remoteSyncBranch] });+ setupRemoteSyncEndpoints({+ dirty,+ branches: [remoteSyncBranch],+ ...(settingsError && {+ settingsResponse: { error: settingsError },+ }),+ });fetchMock.get("express:/api/ee/library", libraryCollection ?? { data: null });- setupRootCollectionItemsEndpoint({ rootCollectionItems: [] });+ setupRootCollectionItemsEndpoint({ rootCollectionItems });};const createStoreState = ({@@ -99,7 +109,9 @@ interface SetupOpts {remoteSyncTransforms?: boolean;libraryCollection?: Collection | null;dirty?: any[];+ rootCollectionItems?: CollectionItem[];variant?: RemoteSyncSettingsFormProps["variant"];+ settingsError?: { status: number; message: string };}export const setup = ({@@ -112,7 +124,9 @@ export const setup = ({remoteSyncTransforms = false,libraryCollection = null,dirty = [],+ rootCollectionItems = [],variant,+ settingsError,}: SetupOpts = {}) => {setupEndpoints({remoteSyncEnabled,@@ -123,6 +137,8 @@ export const setup = ({remoteSyncTransforms,libraryCollection,dirty,+ rootCollectionItems,+ settingsError,});renderWithProviders(diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/RemoteSyncSettingsForm.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/RemoteSyncSettingsForm.tsxindex 98b44a19323..65c60e2ddc0 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/RemoteSyncSettingsForm.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/RemoteSyncSettingsForm.tsx@@ -146,6 +146,60 @@ export const RemoteSyncSettingsForm = (props: RemoteSyncSettingsFormProps) => {modalContent: disableConfirmationModal,} = useConfirmation();+ const initialValues = useMemo(() => {+ const values = REMOTE_SYNC_SCHEMA.cast(settingValues, {+ stripUnknown: true,+ });+ const tokenValue =+ settingDetails?.[TOKEN_KEY]?.value ?? settingValues?.[TOKEN_KEY];++ // Build initial collection sync map from server data+ const collectionSyncMap: Record<number, boolean> = {};++ // For modal variant during first-time setup, default library to checked+ const shouldDefaultToChecked = isModalVariant && !isRemoteSyncEnabled;++ // Add library collection+ if (libraryCollection) {+ collectionSyncMap[libraryCollection.id] = shouldDefaultToChecked+ ? true+ : (libraryCollection.is_remote_synced ?? false);+ }++ // Add top-level collections (excluding personal)+ topLevelCollectionsData?.data+ ?.filter((c) => !c.personal_owner_id)+ .forEach((collection) => {+ collectionSyncMap[collection.id] = collection.is_remote_synced ?? false;+ });++ // Add tenant collections+ tenantCollectionsData?.data?.forEach((collection) => {+ collectionSyncMap[collection.id] = collection.is_remote_synced ?? false;+ });++ return {+ ...values,+ [TOKEN_KEY]: tokenValue,+ [COLLECTIONS_KEY]: collectionSyncMap,+ // For modal variant during first-time setup, default transforms to checked (if enabled)+ [TRANSFORMS_KEY]:+ shouldDefaultToChecked && PLUGIN_TRANSFORMS.isEnabled+ ? true+ : values[TRANSFORMS_KEY],+ // For modal variant when library doesn't exist, default to wanting to create and sync it+ [SYNC_LIBRARY_PENDING_KEY]: shouldDefaultToChecked && !libraryCollection,+ };+ }, [+ settingValues,+ settingDetails,+ libraryCollection,+ topLevelCollectionsData,+ tenantCollectionsData,+ isModalVariant,+ isRemoteSyncEnabled,+ ]);+const handleSubmit = useCallback(async (values: RemoteSyncSettingsFormState) => {const didBranchChange =@@ -171,7 +225,18 @@ export const RemoteSyncSettingsForm = (props: RemoteSyncSettingsFormProps) => {}}- // Don't send collections when in read-only mode+ // Only include collections whose sync state actually changed+ const initialCollections = initialValues[COLLECTIONS_KEY] ?? {};+ const changedCollections: Record<number, boolean> = {};+ for (const [idStr, desired] of Object.entries(collectionsMap)) {+ const id = Number(idStr);+ if (initialCollections[id] !== desired) {+ changedCollections[id] = desired;+ }+ }+ const hasCollectionChanges = Object.keys(changedCollections).length > 0;++ // Don't send collections when in read-only mode or when nothing changed// Also filter out the sync-library-pending key as it's not a real settingconst isReadOnly = values[TYPE_KEY] === "read-only";const settingsToSave: RemoteSyncConfigurationSettings = {@@ -182,10 +247,10 @@ export const RemoteSyncSettingsForm = (props: RemoteSyncSettingsFormProps) => {[BRANCH_KEY]: values[BRANCH_KEY],[AUTO_IMPORT_KEY]: values[AUTO_IMPORT_KEY],[TRANSFORMS_KEY]: values[TRANSFORMS_KEY],- ...(isReadOnly+ ...(isReadOnly || !hasCollectionChanges? {}: {- [COLLECTIONS_KEY]: collectionsMap,+ [COLLECTIONS_KEY]: changedCollections,}),};@@ -246,6 +311,7 @@ export const RemoteSyncSettingsForm = (props: RemoteSyncSettingsFormProps) => {},[settingValues,+ initialValues,updateRemoteSyncSettings,isModalVariant,libraryCollection,@@ -281,60 +347,6 @@ export const RemoteSyncSettingsForm = (props: RemoteSyncSettingsFormProps) => {});}, [updateRemoteSyncSettings, sendToast, showDisableConfirmation]);- const initialValues = useMemo(() => {- const values = REMOTE_SYNC_SCHEMA.cast(settingValues, {- stripUnknown: true,- });- const tokenValue =- settingDetails?.[TOKEN_KEY]?.value ?? settingValues?.[TOKEN_KEY];-- // Build initial collection sync map from server data- const collectionSyncMap: Record<number, boolean> = {};-- // For modal variant during first-time setup, default library to checked- const shouldDefaultToChecked = isModalVariant && !isRemoteSyncEnabled;-- // Add library collection- if (libraryCollection) {- collectionSyncMap[libraryCollection.id] = shouldDefaultToChecked- ? true- : (libraryCollection.is_remote_synced ?? false);- }-- // Add top-level collections (excluding personal)- topLevelCollectionsData?.data- ?.filter((c) => !c.personal_owner_id)- .forEach((collection) => {- collectionSyncMap[collection.id] = collection.is_remote_synced ?? false;- });-- // Add tenant collections- tenantCollectionsData?.data?.forEach((collection) => {- collectionSyncMap[collection.id] = collection.is_remote_synced ?? false;- });-- return {- ...values,- [TOKEN_KEY]: tokenValue,- [COLLECTIONS_KEY]: collectionSyncMap,- // For modal variant during first-time setup, default transforms to checked (if enabled)- [TRANSFORMS_KEY]:- shouldDefaultToChecked && PLUGIN_TRANSFORMS.isEnabled- ? true- : values[TRANSFORMS_KEY],- // For modal variant when library doesn't exist, default to wanting to create and sync it- [SYNC_LIBRARY_PENDING_KEY]: shouldDefaultToChecked && !libraryCollection,- };- }, [- settingValues,- settingDetails,- libraryCollection,- topLevelCollectionsData,- tenantCollectionsData,- isModalVariant,- isRemoteSyncEnabled,- ]);-// eslint-disable-next-line metabase/no-unconditional-metabase-links-render -- This links only shows for admins.const { url: docsUrl } = useDocsUrl("installation-and-operation/remote-sync",diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/RemoteSyncSettingsForm.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/RemoteSyncSettingsForm.unit.spec.tsxindex 50c61821e3e..68b79286222 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/RemoteSyncSettingsForm.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/RemoteSyncSettingsForm.unit.spec.tsx@@ -1,8 +1,9 @@import userEvent from "@testing-library/user-event";-import fetchMock from "fetch-mock";+import { findRequests } from "__support__/server-mocks";import { screen, waitFor } from "__support__/ui";import { PLUGIN_TRANSFORMS } from "metabase/plugins";+import { createMockCollectionItemFromCollection } from "metabase-types/api/mocks";import {createMockLibraryCollection,@@ -532,21 +533,9 @@ describe("RemoteSyncSettingsForm", () => {remoteSyncType: "read-only",remoteSyncUrl: "",remoteSyncEnabled: false,+ settingsError: { status: 400, message: "Invalid branch name" },});- // Override the settings endpoint to return an error- fetchMock.removeRoute("remote-sync-settings");- fetchMock.put(- "path:/api/ee/remote-sync/settings",- {- status: 400,- body: {- message: "Invalid branch name",- },- },- { name: "remote-sync-settings" },- );-const urlInput = screen.getByLabelText(/Repository URL/i);await userEvent.type(urlInput, "https://github.com/foo/bar.git");@@ -561,6 +550,96 @@ describe("RemoteSyncSettingsForm", () => {});});+ describe("collection change filtering", () => {+ it("should not send collections in request body when no collection sync states changed", async () => {+ const syncedCollection = createMockCollectionItemFromCollection({+ id: 10,+ name: "Synced Collection",+ is_remote_synced: true,+ can_write: true,+ });+ const unsyncedCollection = createMockCollectionItemFromCollection({+ id: 20,+ name: "Unsynced Collection",+ is_remote_synced: false,+ can_write: true,+ });++ setup({+ remoteSyncType: "read-write",+ remoteSyncUrl: "https://github.com/test/repo.git",+ remoteSyncEnabled: true,+ rootCollectionItems: [syncedCollection, unsyncedCollection],+ });++ await waitFor(() => {+ expect(screen.getByLabelText("Read-write")).toBeChecked();+ });++ // Change the URL to make the form dirty (but don't change collections)+ const urlInput = screen.getByLabelText(/Repository URL/i);+ await userEvent.clear(urlInput);+ await userEvent.type(urlInput, "https://github.com/test/other-repo.git");++ const submitButton = screen.getByRole("button", {+ name: /Save changes/i,+ });+ await userEvent.click(submitButton);++ const puts = await findRequests("PUT");+ const settingsRequest = puts.find((r) =>+ r.url.includes("/api/ee/remote-sync/settings"),+ );+ expect(settingsRequest).toBeDefined();+ expect(settingsRequest?.body).not.toHaveProperty("collections");+ });++ it("should only send changed collections in request body", async () => {+ const syncedCollection = createMockCollectionItemFromCollection({+ id: 10,+ name: "Synced Collection",+ is_remote_synced: true,+ can_write: true,+ });+ const unsyncedCollection = createMockCollectionItemFromCollection({+ id: 20,+ name: "Unsynced Collection",+ is_remote_synced: false,+ can_write: true,+ });++ setup({+ remoteSyncType: "read-write",+ remoteSyncUrl: "https://github.com/test/repo.git",+ remoteSyncEnabled: true,+ rootCollectionItems: [syncedCollection, unsyncedCollection],+ });++ await waitFor(() => {+ expect(screen.getByLabelText("Read-write")).toBeChecked();+ });++ // Toggle the unsynced collection to synced+ const unsyncedToggle = screen.getByLabelText("Sync Unsynced Collection");+ await userEvent.click(unsyncedToggle);++ const submitButton = screen.getByRole("button", {+ name: /Save changes/i,+ });+ await userEvent.click(submitButton);++ // Should only include collection 20 (changed), not collection 10 (unchanged)+ const puts = await findRequests("PUT");+ const settingsRequest = puts.find((r) =>+ r.url.includes("/api/ee/remote-sync/settings"),+ );+ expect(settingsRequest).toBeDefined();+ expect(settingsRequest?.body).toHaveProperty("collections", {+ "20": true,+ });+ });+ });+describe("collections to sync section", () => {it("should display collections section in admin variant when read-write mode is selected during initial setup", async () => {setup({diff --git a/frontend/test/__support__/server-mocks/remote-sync.ts b/frontend/test/__support__/server-mocks/remote-sync.tsindex 26df1f1cc55..fa4bdccc546 100644--- a/frontend/test/__support__/server-mocks/remote-sync.ts+++ b/frontend/test/__support__/server-mocks/remote-sync.ts@@ -61,13 +61,24 @@ export interface RemoteSyncSettingsResponse {export const setupRemoteSyncSettingsEndpoint = ({success = true,task_id,-}: Partial<RemoteSyncSettingsResponse> = {}) => {+ error,+}: Partial<RemoteSyncSettingsResponse> & {+ error?: { status: number; message: string };+} = {}) => {fetchMock.removeRoute("remote-sync-settings");- fetchMock.put(- "path:/api/ee/remote-sync/settings",- { success, ...(task_id !== undefined && { task_id }) },- { name: "remote-sync-settings" },- );+ if (error) {+ fetchMock.put(+ "path:/api/ee/remote-sync/settings",+ { status: error.status, body: { message: error.message } },+ { name: "remote-sync-settings" },+ );+ } else {+ fetchMock.put(+ "path:/api/ee/remote-sync/settings",+ { success, ...(task_id !== undefined && { task_id }) },+ { name: "remote-sync-settings" },+ );+ }};/**@@ -124,7 +135,9 @@ export const setupRemoteSyncEndpoints = ({changedCollections?: Record<number, boolean>;hasRemoteChanges?: boolean;hasRemoteChangesDelay?: number;- settingsResponse?: Partial<RemoteSyncSettingsResponse>;+ settingsResponse?: Partial<RemoteSyncSettingsResponse> & {+ error?: { status: number; message: string };+ };} = {}) => {setupRemoteSyncBranchesEndpoint(branches);setupRemoteSyncDirtyEndpoint({ dirty, changedCollections });