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
feat: snippets under 'library' in changes view (#68910)
Edward Pagetaa7f30169f86
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.tsxindex add064b1669..f6476ec9f79 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.tsx@@ -3,6 +3,7 @@ import { t } from "ttag";import _ from "underscore";import { useListCollectionsTreeQuery } from "metabase/api";+import { isLibraryCollection } from "metabase/collections/utils";import { useSetting } from "metabase/common/hooks";import {Box,@@ -21,7 +22,7 @@ import {getSyncStatusIcon,isTableChildModel,} from "metabase-enterprise/remote_sync/utils";-import type { RemoteSyncEntity } from "metabase-types/api";+import type { Collection, RemoteSyncEntity } from "metabase-types/api";import { CollectionPath } from "./CollectionPath";import { EntityLink } from "./EntityLink";@@ -65,11 +66,15 @@ export const AllChangesView = ({ entities, title }: AllChangesViewProps) => {});// Build a set of snippet collection IDs for icon selection+ // Only include collections with namespace "snippets" to avoid false positivesconst snippetCollectionIds = useMemo(() => {const ids = new Set<number>();const collectIds = (collections: typeof snippetCollectionTree) => {for (const collection of collections) {- if (typeof collection.id === "number") {+ if (+ typeof collection.id === "number" &&+ collection.namespace === "snippets"+ ) {ids.add(collection.id);}if (collection.children) {@@ -81,6 +86,25 @@ export const AllChangesView = ({ entities, title }: AllChangesViewProps) => {return ids;}, [snippetCollectionTree]);+ // Find the library collection ID for placing snippets without a collection_id+ const libraryCollectionId = useMemo(() => {+ const findLibrary = (collections: Collection[]): number | null => {+ for (const col of collections) {+ if (isLibraryCollection(col)) {+ return typeof col.id === "number" ? col.id : null;+ }+ if (col.children?.length) {+ const found = findLibrary(col.children);+ if (found !== null) {+ return found;+ }+ }+ }+ return null;+ };+ return findLibrary(collectionTree);+ }, [collectionTree]);+const collectionMap = useMemo(() => {// Merge regular collections with snippet collectionsreturn buildCollectionMap([...collectionTree, ...snippetCollectionTree]);@@ -95,7 +119,17 @@ export const AllChangesView = ({ entities, title }: AllChangesViewProps) => {}, [entities]);const groupedData = useMemo(() => {- const byCollection = _.groupBy(entities, (e) => e.collection_id || 0);+ const byCollection = _.groupBy(entities, (e) => {+ if (e.collection_id != null) {+ return e.collection_id;+ }+ // Snippets without a collection_id should go under the Library collection+ if (e.model === "nativequerysnippet" && libraryCollectionId != null) {+ return libraryCollectionId;+ }+ // Default to Root for other entities without a collection_id+ return 0;+ });const sortByStatus = (items: RemoteSyncEntity[]) =>items.sort((a, b) => {@@ -158,18 +192,35 @@ export const AllChangesView = ({ entities, title }: AllChangesViewProps) => {);const numericCollectionId = Number(collectionId) || undefined;+ const isSnippetCollection =+ numericCollectionId !== undefined &&+ snippetCollectionIds.has(numericCollectionId);++ // Get base path segments for this collection+ let pathSegments = getCollectionPathSegments(+ numericCollectionId,+ collectionMap,+ );++ // For snippet collections, prepend the Library collection path+ // This makes them appear as descendants of the Library collection+ if (isSnippetCollection && libraryCollectionId != null) {+ const libraryCollection = collectionMap.get(libraryCollectionId);+ if (libraryCollection) {+ pathSegments = [+ { id: libraryCollection.id, name: libraryCollection.name },+ ...pathSegments,+ ];+ }+ }+return {- pathSegments: getCollectionPathSegments(- numericCollectionId,- collectionMap,- ),+ pathSegments,collectionId: numericCollectionId,collectionEntity,tableGroups,items: sortByStatus(otherItems),- isSnippetCollection:- numericCollectionId !== undefined &&- snippetCollectionIds.has(numericCollectionId),+ isSnippetCollection,};}).sort((a, b) =>@@ -178,7 +229,7 @@ export const AllChangesView = ({ entities, title }: AllChangesViewProps) => {.join(" / ").localeCompare(b.pathSegments.map((s) => s.name).join(" / ")),);- }, [entities, collectionMap, snippetCollectionIds]);+ }, [entities, collectionMap, snippetCollectionIds, libraryCollectionId]);return (<Box>diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.unit.spec.tsxindex 4757e8bfd31..a162dc0cc58 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.unit.spec.tsx@@ -2,16 +2,39 @@ import {setupCollectionTreeEndpoint,setupPropertiesEndpoints,} from "__support__/server-mocks";+import { mockSettings } from "__support__/settings";import { renderWithProviders, screen } from "__support__/ui";-import type { Collection, RemoteSyncEntity } from "metabase-types/api";+import type { LibraryCollectionType } from "metabase/plugins";+import { PLUGIN_DATA_STUDIO } from "metabase/plugins";+import type {+ Collection,+ CollectionType,+ RemoteSyncEntity,+} from "metabase-types/api";import {createMockCollection,createMockSettings,+ createMockTokenFeatures,} from "metabase-types/api/mocks";import { createMockRemoteSyncEntity } from "metabase-types/api/mocks/remote-sync";+import { createMockState } from "metabase-types/store/mocks";import { AllChangesView } from "./AllChangesView";+// Helper function to detect library collections (mirrors enterprise implementation)+function getLibraryCollectionType(+ type: CollectionType | null | undefined,+): LibraryCollectionType | undefined {+ switch (type) {+ case "library":+ return "root";+ case "library-data":+ return "data";+ case "library-metrics":+ return "metrics";+ }+}+const defaultCollection = createMockCollection({id: 1,name: "Entity Collection",@@ -37,21 +60,46 @@ const setup = ({entities = [updatedEntity],collections = [defaultCollection],isTransformsSyncEnabled = false,+ enableDataStudio = false,}: {entities: RemoteSyncEntity[];collections?: Collection[];isTransformsSyncEnabled?: boolean;+ enableDataStudio?: boolean;}) => {- // Use setupCollectionTreeEndpoint for simple tree mocking without complex filtering+ if (enableDataStudio) {+ PLUGIN_DATA_STUDIO.getLibraryCollectionType = getLibraryCollectionType;+ } else {+ PLUGIN_DATA_STUDIO.getLibraryCollectionType = () => undefined;+ }++ const tokenFeatures = enableDataStudio+ ? createMockTokenFeatures({ data_studio: true })+ : createMockTokenFeatures();++ const settingsState = mockSettings({+ "use-tenants": false,+ "remote-sync-transforms": isTransformsSyncEnabled,+ "token-features": tokenFeatures,+ });+setupCollectionTreeEndpoint(collections);+setupPropertiesEndpoints(createMockSettings({"use-tenants": false,"remote-sync-transforms": isTransformsSyncEnabled,+ "token-features": tokenFeatures,}),);- renderWithProviders(<AllChangesView entities={entities} />);+ const storeInitialState = createMockState({+ settings: settingsState,+ });++ renderWithProviders(<AllChangesView entities={entities} />, {+ storeInitialState,+ });};describe("AllChangesView", () => {@@ -110,7 +158,6 @@ describe("AllChangesView", () => {name: "Child Tenant Collection",namespace: "shared-tenant-collection",});- // Use nested tree structure - parent contains childconst parentCollection = createMockCollection({id: 5,name: "Parent Tenant Collection",@@ -401,4 +448,282 @@ describe("AllChangesView", () => {expect(screen.getByText("SELECT Query")).toBeInTheDocument();});});++ describe("snippets and snippet collections with Library (data_studio)", () => {+ it("should display snippets without collection_id under Root when data_studio feature is not enabled", async () => {+ const libraryCollection = createMockCollection({+ id: 99,+ name: "Library",+ type: "library",+ effective_ancestors: [],+ });+ const snippetEntity = createMockRemoteSyncEntity({+ id: 300,+ name: "Root Snippet",+ model: "nativequerysnippet",+ collection_id: undefined,+ sync_status: "create",+ });++ setup({+ entities: [snippetEntity],+ collections: [libraryCollection],+ enableDataStudio: false,+ });++ expect(await screen.findByText("Root")).toBeInTheDocument();+ expect(screen.getByText("Root Snippet")).toBeInTheDocument();+ });++ it("should display snippets without collection_id under the Library collection", async () => {+ const libraryCollection = createMockCollection({+ id: 99,+ name: "Library",+ type: "library",+ effective_ancestors: [],+ });+ const snippetEntity = createMockRemoteSyncEntity({+ id: 300,+ name: "Root Snippet",+ model: "nativequerysnippet",+ collection_id: undefined,+ sync_status: "create",+ });++ setup({+ entities: [snippetEntity],+ collections: [libraryCollection],+ enableDataStudio: true,+ });++ expect(await screen.findByText("Library")).toBeInTheDocument();+ expect(screen.getByText("Root Snippet")).toBeInTheDocument();+ expect(screen.queryByText("Root")).not.toBeInTheDocument();+ });++ it("should display snippets without collection_id under Root when no Library collection exists", async () => {+ const regularCollection = createMockCollection({+ id: 1,+ name: "Regular Collection",+ effective_ancestors: [],+ });+ const snippetEntity = createMockRemoteSyncEntity({+ id: 300,+ name: "Root Snippet",+ model: "nativequerysnippet",+ collection_id: undefined,+ sync_status: "create",+ });++ setup({+ entities: [snippetEntity],+ collections: [regularCollection],+ enableDataStudio: true,+ });++ expect(await screen.findByText("Root")).toBeInTheDocument();+ expect(screen.getByText("Root Snippet")).toBeInTheDocument();+ });++ it("should display multiple snippets without collection_id under the Library collection", async () => {+ const libraryCollection = createMockCollection({+ id: 99,+ name: "Library",+ type: "library",+ effective_ancestors: [],+ });+ const snippetEntity1 = createMockRemoteSyncEntity({+ id: 300,+ name: "First Snippet",+ model: "nativequerysnippet",+ collection_id: undefined,+ sync_status: "create",+ });+ const snippetEntity2 = createMockRemoteSyncEntity({+ id: 301,+ name: "Second Snippet",+ model: "nativequerysnippet",+ collection_id: undefined,+ sync_status: "update",+ });++ setup({+ entities: [snippetEntity1, snippetEntity2],+ collections: [libraryCollection],+ enableDataStudio: true,+ });++ expect(await screen.findByText("Library")).toBeInTheDocument();+ expect(screen.getByText("First Snippet")).toBeInTheDocument();+ expect(screen.getByText("Second Snippet")).toBeInTheDocument();+ });++ it("should display snippets with explicit collection_id in their assigned collection under Library", async () => {+ const libraryCollection = createMockCollection({+ id: 99,+ name: "Library",+ type: "library",+ effective_ancestors: [],+ });+ const snippetCollection = createMockCollection({+ id: 50,+ name: "My Snippets",+ namespace: "snippets",+ effective_ancestors: [],+ });+ const snippetWithCollection = createMockRemoteSyncEntity({+ id: 300,+ name: "Assigned Snippet",+ model: "nativequerysnippet",+ collection_id: 50,+ sync_status: "create",+ });+ const snippetWithoutCollection = createMockRemoteSyncEntity({+ id: 301,+ name: "Unassigned Snippet",+ model: "nativequerysnippet",+ collection_id: undefined,+ sync_status: "create",+ });++ setup({+ entities: [snippetWithCollection, snippetWithoutCollection],+ collections: [libraryCollection, snippetCollection],+ enableDataStudio: true,+ });++ expect(await screen.findByText("My Snippets")).toBeInTheDocument();+ expect(screen.getByText("Assigned Snippet")).toBeInTheDocument();++ const libraryElements = screen.getAllByText("Library");+ expect(libraryElements.length).toBeGreaterThanOrEqual(2);+ expect(screen.getByText("Unassigned Snippet")).toBeInTheDocument();+ });++ it("should display non-snippet entities without collection_id under Root even when Library exists", async () => {+ const libraryCollection = createMockCollection({+ id: 99,+ name: "Library",+ type: "library",+ effective_ancestors: [],+ });+ const cardEntity = createMockRemoteSyncEntity({+ id: 300,+ name: "Root Card",+ model: "card",+ collection_id: undefined,+ sync_status: "create",+ });++ setup({+ entities: [cardEntity],+ collections: [libraryCollection],+ enableDataStudio: true,+ });++ expect(await screen.findByText("Root")).toBeInTheDocument();+ expect(screen.getByText("Root Card")).toBeInTheDocument();+ expect(screen.queryByText("Library")).not.toBeInTheDocument();+ });++ it("should find Library collection in nested tree structure", async () => {+ const libraryCollection = createMockCollection({+ id: 99,+ name: "Library",+ type: "library",+ effective_ancestors: [],+ });+ const parentCollection = createMockCollection({+ id: 1,+ name: "Parent Collection",+ children: [libraryCollection],+ });+ const snippetEntity = createMockRemoteSyncEntity({+ id: 300,+ name: "Nested Library Snippet",+ model: "nativequerysnippet",+ collection_id: undefined,+ sync_status: "create",+ });++ setup({+ entities: [snippetEntity],+ collections: [parentCollection],+ enableDataStudio: true,+ });++ expect(await screen.findByText("Library")).toBeInTheDocument();+ expect(screen.getByText("Nested Library Snippet")).toBeInTheDocument();+ });++ it("should display snippet collections as descendants of Library", async () => {+ const libraryCollection = createMockCollection({+ id: 99,+ name: "Library",+ type: "library",+ effective_ancestors: [],+ });+ const snippetCollection = createMockCollection({+ id: 50,+ name: "My Snippets",+ namespace: "snippets",+ effective_ancestors: [],+ });+ const snippetEntity = createMockRemoteSyncEntity({+ id: 300,+ name: "SELECT Query",+ model: "nativequerysnippet",+ collection_id: 50,+ sync_status: "create",+ });++ setup({+ entities: [snippetEntity],+ collections: [libraryCollection, snippetCollection],+ enableDataStudio: true,+ });++ expect(await screen.findByText("Library")).toBeInTheDocument();+ expect(screen.getByText("My Snippets")).toBeInTheDocument();+ expect(screen.getByText("SELECT Query")).toBeInTheDocument();+ });++ it("should display nested snippet collections under Library with full hierarchy", async () => {+ const libraryCollection = createMockCollection({+ id: 99,+ name: "Library",+ type: "library",+ effective_ancestors: [],+ });+ const childSnippetCollection = createMockCollection({+ id: 51,+ name: "Child Snippet Folder",+ namespace: "snippets",+ });+ const parentSnippetCollection = createMockCollection({+ id: 50,+ name: "Parent Snippet Folder",+ namespace: "snippets",+ children: [childSnippetCollection],+ });+ const snippetEntity = createMockRemoteSyncEntity({+ id: 300,+ name: "SELECT Query",+ model: "nativequerysnippet",+ collection_id: 51,+ sync_status: "update",+ });++ setup({+ entities: [snippetEntity],+ collections: [libraryCollection, parentSnippetCollection],+ enableDataStudio: true,+ });++ expect(await screen.findByText("Library")).toBeInTheDocument();+ expect(screen.getByText("Parent Snippet Folder")).toBeInTheDocument();+ expect(screen.getByText("Child Snippet Folder")).toBeInTheDocument();+ expect(screen.getByText("SELECT Query")).toBeInTheDocument();+ });+ });});diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/register-listeners.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/register-listeners.tsindex 09e1c2f2c9d..c28dbaaf35b 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/register-listeners.ts+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/register-listeners.ts@@ -78,6 +78,11 @@ function shouldInvalidateForCollection(return true;}+ // Also invalidate for snippets namespace collections+ if (newCollection.namespace === "snippets") {+ return true;+ }+return false;}diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.unit.spec.tsindex 32ece0bd850..2c56c158cd2 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.unit.spec.ts+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.unit.spec.ts@@ -354,6 +354,50 @@ describe("remote-sync-listener-middleware", () => {expect(callsAfter).toBe(callsBefore);});+ it("should invalidate tags when creating a snippets namespace collection", async () => {+ const snippetsCollection = createMockCollection({+ id: 100,+ name: "My Snippets Collection",+ namespace: "snippets",+ is_remote_synced: false,+ });++ setupCreateCollectionEndpoint(snippetsCollection);+ setupRemoteSyncDirtyEndpoint();++ const store = createTestStore();++ // Subscribe to the dirty query first so RTK Query will refetch when tags are invalidated+ store.dispatch(+ remoteSyncApi.endpoints.getRemoteSyncChanges.initiate(undefined),+ );++ // Wait for initial dirty query to complete+ await waitForCondition(() =>+ fetchMock.callHistory.done("remote-sync-dirty"),+ );++ // Dispatch the create collection mutation+ store.dispatch(+ collectionApi.endpoints.createCollection.initiate({+ name: "My Snippets Collection",+ namespace: "snippets",+ }),+ );++ // Wait for the request to complete+ await waitForCondition(() =>+ fetchMock.callHistory.done("create-collection"),+ );++ // Give middleware time to process and trigger invalidation+ await new Promise((resolve) => setTimeout(resolve, 100));++ // Verify the dirty endpoint was called more than once (initial + refetch after invalidation)+ const dirtyCalls = fetchMock.callHistory.calls("remote-sync-dirty");+ expect(dirtyCalls.length).toBeGreaterThan(1);+ });+it("should invalidate tags when creating a remote-synced collection", async () => {const remoteSyncedCollection = createMockCollection({id: 100,