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
Offer the Library toggle in remote sync settings without a Library, and don't conflict on an empty Library (#84665)
Alexander Polyankin07917967db0f
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/db.cljindex 7fb787efdbb..b036956cac0 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/db.clj@@ -480,6 +480,19 @@[collections :- [:sequential ::collections.schema/collection]](t2/select-pks-set :model/Collection {:where (subtree-expr collections)}))+(mu/defn collection-content-exists? :- :boolean+ "Whether a Card, Dashboard, Document, Timeline, or Action is in the Collections with `collection-ids`, or a Table is+ published to one."+ [collection-ids :- [:sequential ::lib.schema.id/collection]]+ (boolean+ (or (t2/exists? :model/Card :collection_id [:in collection-ids])+ (t2/exists? :model/Dashboard :collection_id [:in collection-ids])+ (t2/exists? :model/Document :collection_id [:in collection-ids])+ (t2/exists? :model/Timeline :collection_id [:in collection-ids])+ (t2/exists? :model/Action :collection_id [:in collection-ids])+ (t2/exists? :model/Table {:from [(warehouse-schema-overlay/table-query)]+ :where [:and [:= :is_published true] [:in :collection_id collection-ids]]}))))+(mu/defn remote-synced-subtree-collection-ids"The IDs of the remote-synced Collections among `collections` and their descendants."[collections :- [:sequential ::collections.schema/collection]]diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.cljindex d0faa82c818..b3476651eb9 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj@@ -189,6 +189,13 @@:else(format "Failed to reload from git repository: %s" (or (ex-message e) (.getName (class e)))))))+(defn- library-has-content?+ "Whether `library` holds anything beyond its system collections."+ [library]+ (let [collection-ids (vec (remote-sync.db/subtree-collection-ids [library]))]+ (or (some (complement collection/library-root-collection?) (remote-sync.db/collections collection-ids))+ (remote-sync.db/collection-content-exists? collection-ids))))+(defn- get-conflicts"Detects conflicts that would prevent or complicate import. Returns a map with two classes:- :first-import-conflicts - feature/namespace/library conflicts that only block on the very first import@@ -196,7 +203,7 @@would wholesale-delete; these block on EVERY import, not just the firstConflict types detected:- - :library-conflict - First import only, local Library exists, import has Library+ - :library-conflict - First import only, local unsynced Library with content, import has Library- :transforms-conflict / :snippets-conflict - import has matching content but local has unsynced entities- :*-conflict (deletion) - import lacks transforms/tags/libraries that exist locally and unsynced (GHY-3900)"[ingestable first-import?]@@ -226,7 +233,8 @@(when (and first-import?(contains? (get-in imported-data [:by-entity-id "Collection"] #{})collection/library-entity-id)- (not (remote-sync.db/rso-exists? "Collection" (:id local-library))))+ (not (remote-sync.db/rso-exists? "Collection" (:id local-library)))+ (library-has-content? local-library)){:type :library-conflict:category "Library":message "Import contains Library but local instance has an unsynced Library collection"}))diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.cljindex fb987714a16..d937f0362bb 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj@@ -1325,10 +1325,11 @@ serdes/meta:(deftest import!-blocks-if-it-encounters-library-conflicts(let [task-id (t2/insert-returning-pk! :model/RemoteSyncTask {:sync_task_type "import" :initiated_by (mt/user->id :rasta)})](t2/delete! :model/Collection :entity_id collection/library-entity-id)- (mt/with-temp [:model/Collection _ {:name "Test Library Collection"- :type "library"- :entity_id collection/library-entity-id- :location "/"}]+ (mt/with-temp [:model/Collection {library-id :id} {:name "Test Library Collection"+ :type "library"+ :entity_id collection/library-entity-id+ :location "/"}+ :model/Collection _ {:name "Local Library Content" :location (str "/" library-id "/")}](let [test-files {"main" {"collections/main/test_collection/test_collection.yaml"(test-helpers/generate-collection-yaml collection/library-entity-id "Another Library")}}mock-source (test-helpers/create-mock-source :initial-files test-files)@@ -1336,6 +1337,22 @@ serdes/meta:(is (= :conflict (:status result)))(is (= #{"Library"} (:conflicts result)))))))+(deftest import!-does-not-conflict-on-empty-library+ (testing "First import into empty unsynced Library syncs it instead of reporting conflict"+ (let [task-id (t2/insert-returning-pk! :model/RemoteSyncTask {:sync_task_type "import" :initiated_by (mt/user->id :rasta)})]+ (t2/delete! :model/Collection :entity_id collection/library-entity-id)+ (mt/with-temp [:model/Collection {library-id :id} {:name "Library"+ :type "library"+ :entity_id collection/library-entity-id+ :location "/"}]+ (let [test-files {"main" {"collections/main/lib/lib.yaml"+ (test-helpers/generate-collection-yaml collection/library-entity-id "Library"+ :type "library" :is-remote-synced true)}}+ mock-source (test-helpers/create-mock-source :initial-files test-files)+ result (impl/import! (source.p/snapshot mock-source) task-id)]+ (is (= :success (:status result)))+ (is (true? (t2/select-one-fn :is_remote_synced :model/Collection :id library-id))))))))+(deftest import!-blocks-if-it-encounters-snippet-conflicts(let [task-id (t2/insert-returning-pk! :model/RemoteSyncTask {:sync_task_type "import" :initiated_by (mt/user->id :rasta)})](mt/with-temp [:model/NativeQuerySnippet _ {:name "Test Snippet"}]@@ -1360,10 +1377,11 @@ serdes/meta:(testing "when multiple conflict types exist, all are reported in the :conflicts set"(let [task-id (t2/insert-returning-pk! :model/RemoteSyncTask {:sync_task_type "import" :initiated_by (mt/user->id :rasta)})](t2/delete! :model/Collection :entity_id collection/library-entity-id)- (mt/with-temp [:model/Collection _ {:name "Test Library Collection"- :type "library"- :entity_id collection/library-entity-id- :location "/"}+ (mt/with-temp [:model/Collection {library-id :id} {:name "Test Library Collection"+ :type "library"+ :entity_id collection/library-entity-id+ :location "/"}+ :model/Collection _ {:name "Local Library Content" :location (str "/" library-id "/")}:model/NativeQuerySnippet _ {:name "Test Snippet"}:model/Transform _ {:name "Test Transform"}](let [test-files {"main" {"collections/main/lib/lib.yaml"@@ -1441,10 +1459,11 @@ serdes/meta:(testing "import! returns detailed conflict information in :conflict-details"(let [task-id (t2/insert-returning-pk! :model/RemoteSyncTask {:sync_task_type "import" :initiated_by (mt/user->id :rasta)})](t2/delete! :model/Collection :entity_id collection/library-entity-id)- (mt/with-temp [:model/Collection _ {:name "Test Library Collection"- :type "library"- :entity_id collection/library-entity-id- :location "/"}]+ (mt/with-temp [:model/Collection {library-id :id} {:name "Test Library Collection"+ :type "library"+ :entity_id collection/library-entity-id+ :location "/"}+ :model/Collection _ {:name "Local Library Content" :location (str "/" library-id "/")}](let [test-files {"main" {"collections/main/test_collection/test_collection.yaml"(test-helpers/generate-collection-yaml collection/library-entity-id "Another Library")}}mock-source (test-helpers/create-mock-source :initial-files test-files)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 4a8379b448d..79adc288e6e 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@@ -1,6 +1,7 @@import fetchMock from "fetch-mock";import {+ setupCreateLibraryEndpoint,setupPropertiesEndpoints,setupRemoteSyncEndpoints,setupRootCollectionItemsEndpoint,@@ -10,6 +11,7 @@ import {import { mockSettings } from "__support__/settings";import { createMockState } from "__support__/state";import { renderWithProviders } from "__support__/ui";+import { PLUGIN_LIBRARY } from "metabase/plugins";import type {Collection,CollectionItem,@@ -153,6 +155,8 @@ interface SetupOpts {envSettings?: EnterpriseSettingKey[];isDevInstance?: boolean;upsellDismissed?: boolean;+ hasLibraryFeature?: boolean;+ createdLibraryCollection?: Collection;}export const setup = ({@@ -172,7 +176,11 @@ export const setup = ({envSettings = [],isDevInstance = false,upsellDismissed = false,+ hasLibraryFeature = false,+ createdLibraryCollection = createMockLibraryCollection(),}: SetupOpts = {}) => {+ PLUGIN_LIBRARY.isEnabled = hasLibraryFeature;+ setupCreateLibraryEndpoint(createdLibraryCollection);setupEndpoints({remoteSyncEnabled,remoteSyncUrl,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 1f3d23e8630..bd8313d35e2 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@@ -58,8 +58,7 @@ export const RemoteSyncSettingsForm = ({const { data: dirtyData } = useGetRemoteSyncChangesQuery(undefined, {refetchOnMountOrArgChange: true,});- const { initialValues, libraryCollection } =- useRemoteSyncInitialValues(variant);+ const { initialValues, libraryCollection } = useRemoteSyncInitialValues();const {handleSubmit,branchChangeModal,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 fdf8d788fee..7dfc3c0ecde 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@@ -280,6 +280,7 @@ describe("RemoteSyncSettingsForm", () => {remoteSyncType: "read-write",variant: "settings-modal",libraryCollection: null,+ hasLibraryFeature: true,});await waitFor(() => {@@ -296,6 +297,7 @@ describe("RemoteSyncSettingsForm", () => {remoteSyncEnabled: false,variant: "settings-modal",libraryCollection: null,+ hasLibraryFeature: true,});await waitFor(() => {@@ -311,6 +313,7 @@ describe("RemoteSyncSettingsForm", () => {remoteSyncType: "read-write",variant: "settings-modal",libraryCollection: null,+ hasLibraryFeature: true,});await waitFor(() => {@@ -323,6 +326,120 @@ describe("RemoteSyncSettingsForm", () => {await userEvent.click(libraryToggle);expect(libraryToggle).not.toBeChecked();});+ it("should show library toggle in admin settings when library does not exist", async () => {+ setup({+ remoteSyncType: "read-write",+ libraryCollection: null,+ hasLibraryFeature: true,+ });++ expect(+ await screen.findByLabelText("Sync Library"),+ ).toBeInTheDocument();+ });++ it("should check existing library by default in admin settings during first-time setup", async () => {+ setup({+ remoteSyncType: "read-write",+ remoteSyncEnabled: false,+ libraryCollection: createMockLibraryCollection({+ is_remote_synced: false,+ }),+ hasLibraryFeature: true,+ });++ expect(await screen.findByLabelText("Sync Library")).toBeChecked();+ });++ it("should check transforms by default in admin settings during first-time setup", async () => {+ PLUGIN_TRANSFORMS.isEnabled = true;+ setup({+ remoteSyncType: "read-write",+ remoteSyncEnabled: false,+ libraryCollection: null,+ hasLibraryFeature: true,+ });++ expect(await screen.findByLabelText("Sync Transforms")).toBeChecked();+ PLUGIN_TRANSFORMS.isEnabled = false;+ });++ it("should neither create library nor change transforms sync when setting up read-only sync", async () => {+ PLUGIN_TRANSFORMS.isEnabled = true;+ setup({+ remoteSyncType: "read-only",+ remoteSyncEnabled: false,+ libraryCollection: null,+ hasLibraryFeature: true,+ });++ await userEvent.type(+ screen.getByLabelText(/Repository URL/i),+ "https://github.com/foo/bar.git",+ );+ await userEvent.click(+ screen.getByRole("button", { name: /Set up remote sync/i }),+ );++ const puts = await findRequests("PUT");+ const settingsRequest = puts.find((request) =>+ request.url.includes("/api/ee/remote-sync/settings"),+ );+ expect(settingsRequest?.body).not.toHaveProperty(+ "remote-sync-transforms",+ );+ const posts = await findRequests("POST");+ expect(+ posts.filter((request) => request.url.includes("/api/ee/library")),+ ).toHaveLength(0);+ PLUGIN_TRANSFORMS.isEnabled = false;+ });++ it("should not show library toggle without library feature", async () => {+ setup({+ remoteSyncType: "read-write",+ variant: "settings-modal",+ libraryCollection: null,+ hasLibraryFeature: false,+ });++ await waitFor(() => {+ expect(screen.getByText("Content to sync")).toBeInTheDocument();+ });++ expect(screen.queryByLabelText("Sync Library")).not.toBeInTheDocument();+ });++ it("should create library and sync it when saving admin settings", async () => {+ setup({+ remoteSyncType: "read-write",+ remoteSyncEnabled: false,+ libraryCollection: null,+ hasLibraryFeature: true,+ createdLibraryCollection: createMockLibraryCollection({ id: 555 }),+ });++ expect(await screen.findByLabelText("Sync Library")).toBeChecked();+ await userEvent.type(+ screen.getByLabelText(/Repository URL/i),+ "https://github.com/foo/bar.git",+ );+ await userEvent.click(+ screen.getByRole("button", { name: /Set up remote sync/i }),+ );++ const posts = await findRequests("POST");+ expect(+ posts.filter((request) => request.url.includes("/api/ee/library")),+ ).toHaveLength(1);+ const puts = await findRequests("PUT");+ const settingsRequest = puts.find((request) =>+ request.url.includes("/api/ee/remote-sync/settings"),+ );+ expect(settingsRequest?.body).toMatchObject({+ collections: { 555: true },+ });+ });});describe("transforms toggle", () => {diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/TopLevelCollectionsList/TopLevelCollectionsList.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/TopLevelCollectionsList/TopLevelCollectionsList.tsxindex c7710eb4fa5..30a26f1cedd 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/TopLevelCollectionsList/TopLevelCollectionsList.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/TopLevelCollectionsList/TopLevelCollectionsList.tsx@@ -2,7 +2,7 @@ import { useMemo } from "react";import { t } from "ttag";import { useListCollectionItemsQuery } from "metabase/api";-import { PLUGIN_TRANSFORMS } from "metabase/plugins";+import { PLUGIN_LIBRARY, PLUGIN_TRANSFORMS } from "metabase/plugins";import { useGetLibraryCollectionQuery } from "metabase-enterprise/api";import type { CollectionItem } from "metabase-types/api";@@ -62,7 +62,7 @@ export const TopLevelCollectionsList = ({error={error ? t`Failed to load collections` : null}isLoading={isLoading || isLoadingLibrary}showTransformsRow={PLUGIN_TRANSFORMS.isEnabled}- showLibraryPlaceholder={skipCollections && !libraryCollection}+ showLibraryPlaceholder={PLUGIN_LIBRARY.isEnabled && !libraryCollection}/>);};diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/hooks/use-remote-sync-initial-values.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/hooks/use-remote-sync-initial-values.tsindex 74adbd6fd83..fb5d174493c 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/hooks/use-remote-sync-initial-values.ts+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/hooks/use-remote-sync-initial-values.ts@@ -18,15 +18,9 @@ import {TOKEN_KEY,TRANSFORMS_KEY,} from "../constants";-import type {- RemoteSyncSettingsFormState,- RemoteSyncSettingsVariant,-} from "../types";+import type { RemoteSyncSettingsFormState } from "../types";-export const useRemoteSyncInitialValues = (- variant: RemoteSyncSettingsVariant,-) => {- const isModalVariant = variant === "settings-modal";+export const useRemoteSyncInitialValues = () => {const { data: settingValues } = useGetSettingsQuery();const { data: settingDetails } = useGetAdminSettingsDetailsQuery();const isRemoteSyncEnabled = !!useSetting(REMOTE_SYNC_KEY);@@ -37,11 +31,7 @@ export const useRemoteSyncInitialValues = ({ skip: !isRemoteSyncEnabled },);- // The modal always fetches so its toggles can default to checked.- const { data: libraryCollectionData } = useGetLibraryCollectionQuery(- undefined,- { skip: !isRemoteSyncEnabled && !isModalVariant },- );+ const { data: libraryCollectionData } = useGetLibraryCollectionQuery();// Library collection endpoint returns { data: null } when not foundconst libraryCollection: LibraryCollection | undefined =@@ -62,10 +52,9 @@ export const useRemoteSyncInitialValues = (settingDetails?.[TOKEN_KEY]?.value ?? settingValues?.[TOKEN_KEY];const collectionSyncMap: Record<number, boolean> = {};- const shouldDefaultToChecked = isModalVariant && !isRemoteSyncEnabled;if (libraryCollection) {- collectionSyncMap[libraryCollection.id] = shouldDefaultToChecked+ collectionSyncMap[libraryCollection.id] = !isRemoteSyncEnabled? true: (libraryCollection.is_remote_synced ?? false);}@@ -85,10 +74,10 @@ export const useRemoteSyncInitialValues = ([TOKEN_KEY]: tokenValue,[COLLECTIONS_KEY]: collectionSyncMap,[TRANSFORMS_KEY]:- shouldDefaultToChecked && PLUGIN_TRANSFORMS.isEnabled+ !isRemoteSyncEnabled && PLUGIN_TRANSFORMS.isEnabled? true: values[TRANSFORMS_KEY],- [SYNC_LIBRARY_PENDING_KEY]: shouldDefaultToChecked && !libraryCollection,+ [SYNC_LIBRARY_PENDING_KEY]: !isRemoteSyncEnabled && !libraryCollection,};}, [settingValues,@@ -96,7 +85,6 @@ export const useRemoteSyncInitialValues = (libraryCollection,topLevelCollectionsData,tenantCollectionsData,- isModalVariant,isRemoteSyncEnabled,]);diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/hooks/use-remote-sync-submit.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/hooks/use-remote-sync-submit.tsindex cfffb631084..a87e64feb73 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/hooks/use-remote-sync-submit.ts+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/hooks/use-remote-sync-submit.ts@@ -71,8 +71,9 @@ export const useRemoteSyncSubmit = ({...values[COLLECTIONS_KEY],};- const wantsSyncLibrary = values[SYNC_LIBRARY_PENDING_KEY];- if (isModalVariant && !libraryCollection && wantsSyncLibrary) {+ const isReadOnly = values[TYPE_KEY] === "read-only";+ const wantsSyncLibrary = !isReadOnly && values[SYNC_LIBRARY_PENDING_KEY];+ if (!libraryCollection && wantsSyncLibrary) {try {const newLibrary = await createLibrary().unwrap();// A freshly created library always has a numeric id; only seeded collections use string ids.@@ -97,7 +98,6 @@ export const useRemoteSyncSubmit = ({const hasCollectionChanges = Object.keys(changedCollections).length > 0;// Listed key by key so the sync-library-pending form field, which is not a setting, stays out.- const isReadOnly = values[TYPE_KEY] === "read-only";const settingsToSave: RemoteSyncConfigurationSettings = {[REMOTE_SYNC_KEY]: values[REMOTE_SYNC_KEY],[URL_KEY]: values[URL_KEY],@@ -105,7 +105,7 @@ export const useRemoteSyncSubmit = ({[TYPE_KEY]: values[TYPE_KEY],[BRANCH_KEY]: values[BRANCH_KEY],[AUTO_IMPORT_KEY]: values[AUTO_IMPORT_KEY],- [TRANSFORMS_KEY]: values[TRANSFORMS_KEY],+ ...(isReadOnly ? {} : { [TRANSFORMS_KEY]: values[TRANSFORMS_KEY] }),...(isReadOnly || !hasCollectionChanges? {}: {diff --git a/frontend/src/metabase/admin/settings/tests/setup.tsx b/frontend/src/metabase/admin/settings/tests/setup.tsxindex d1c7e0154fe..10a09cb333e 100644--- a/frontend/src/metabase/admin/settings/tests/setup.tsx+++ b/frontend/src/metabase/admin/settings/tests/setup.tsx@@ -10,6 +10,7 @@ import {setupDatabasesEndpoints,setupEmailEndpoints,setupGroupsEndpoint,+ setupLibraryEndpoints,setupNotificationChannelsEndpoints,setupPropertiesEndpoints,setupSettingEndpoint,@@ -179,6 +180,7 @@ export const setup = async ({});fetchMock.get("path:/api/cloud-migration", { status: 204 });fetchMock.get("path:/api/ee/sso/oidc", []);+ setupLibraryEndpoints();fetchMock.get("path:/api/ee/remote-sync/dirty", {data: [],metadata: {