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
Hydrate root collection in transform runs (#69047)
Alexander Polyankindb53feb9ab61
diff --git a/enterprise/backend/src/metabase_enterprise/transforms/models/transform_run.clj b/enterprise/backend/src/metabase_enterprise/transforms/models/transform_run.cljindex 542437ff53c..f2e29912f57 100644--- a/enterprise/backend/src/metabase_enterprise/transforms/models/transform_run.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms/models/transform_run.clj@@ -3,6 +3,7 @@[medley.core :as m][metabase-enterprise.transforms.models.transform-run-cancelation :as cancel][metabase.app-db.core :as mdb]+ [metabase.collections.models.collection.root :as collection.root][metabase.events.core :as events][metabase.models.interface :as mi][metabase.query-processor.parameters.dates :as params.dates]@@ -290,17 +291,19 @@Follows the conventions used by the FE."[{:keys [offset limit] :as params}]- (let [offset (or offset 0)- limit (or limit 20)- order-by (paged-runs-order-by-clause params)- where-clause (paged-runs-where-clause params)- join-clause (paged-runs-join-clause params)- count-options (m/assoc-some {} :where where-clause)- query-options (m/assoc-some {:order-by order-by :offset offset :limit limit}- :where where-clause- :left-join join-clause)- runs (t2/select :model/TransformRun query-options)]- {:data (t2/hydrate runs [:transform :transform_tag_ids])+ (let [offset (or offset 0)+ limit (or limit 20)+ order-by (paged-runs-order-by-clause params)+ where-clause (paged-runs-where-clause params)+ join-clause (paged-runs-join-clause params)+ count-options (m/assoc-some {} :where where-clause)+ query-options (m/assoc-some {:order-by order-by :offset offset :limit limit}+ :where where-clause+ :left-join join-clause)+ runs (t2/select :model/TransformRun query-options)+ root-collection (collection.root/hydrated-root-collection :transforms)]+ {:data (->> (t2/hydrate runs [:transform :collection :transform_tag_ids])+ (map #(update % :transform collection.root/hydrate-root-collection root-collection))):limit limit:offset offset:total (t2/count :model/TransformRun count-options)}))diff --git a/enterprise/backend/test/metabase_enterprise/transforms/api_test.clj b/enterprise/backend/test/metabase_enterprise/transforms/api_test.cljindex f6fd3809983..ae3c447bfb9 100644--- a/enterprise/backend/test/metabase_enterprise/transforms/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/transforms/api_test.clj@@ -1876,3 +1876,22 @@(is (= (cond-> [daily-run-id hourly-run-id monthly-run-id weekly-run-id](= sort-direction :desc) reverse)(->> response :data (map :id)))))))))))++(deftest get-runs-hydrate-collection-test+ (testing "GET /api/ee/transform/run - hydrates collection on transform"+ (mt/with-premium-features #{:transforms}+ (mt/with-temp [:model/Collection {collection-id :id} {:name "Subfolder" :namespace :transforms}+ :model/Transform {transform-in-collection-id :id} {:collection_id collection-id}+ :model/Transform {transform-in-root-id :id} {:collection_id nil}+ :model/TransformRun {run-in-collection-id :id} {:transform_id transform-in-collection-id}+ :model/TransformRun {run-in-root-id :id} {:transform_id transform-in-root-id}]+ (let [response (mt/user-http-request :crowberto :get 200 "ee/transform/run"+ :transform_ids [transform-in-collection-id+ transform-in-root-id])+ runs-by-id (m/index-by :id (:data response))]+ (testing "transform in explicit collection has that collection hydrated"+ (is (= "Subfolder"+ (get-in (runs-by-id run-in-collection-id) [:transform :collection :name]))))+ (testing "transform in root collection has root collection hydrated"+ (is (= "Transforms"+ (get-in (runs-by-id run-in-root-id) [:transform :collection :name])))))))))diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/pages/RunListPage/RunSidebar/LocationSection/LocationSection.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/pages/RunListPage/RunSidebar/LocationSection/LocationSection.tsxindex 562eb16591f..22f3be6035a 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/pages/RunListPage/RunSidebar/LocationSection/LocationSection.tsx+++ b/enterprise/frontend/src/metabase-enterprise/transforms/pages/RunListPage/RunSidebar/LocationSection/LocationSection.tsx@@ -4,7 +4,6 @@ import { t } from "ttag";import CS from "metabase/css/core/index.css";import * as Urls from "metabase/lib/urls";import { Anchor, Breadcrumbs, FixedSizeIcon, Group } from "metabase/ui";-import { useCollectionPath } from "metabase-enterprise/data-studio/common/hooks/use-collection-path/useCollectionPath";import type { TransformRun } from "metabase-types/api";type LocationSectionProps = {@@ -12,13 +11,9 @@ type LocationSectionProps = {};export function LocationSection({ run }: LocationSectionProps) {- const collectionId = run.transform?.collection_id ?? null;- const { path } = useCollectionPath({- collectionId,- namespace: "transforms",- });+ const collection = run.transform?.collection;- if (collectionId == null || path == null || path.length === 0) {+ if (collection == null) {return null;}@@ -28,20 +23,17 @@ export function LocationSection({ run }: LocationSectionProps) {lh="1rem"separator={<FixedSizeIcon name="chevronright" size={12} />}>- {path.map((folder, index) => (- <Anchor- key={folder.id}- component={Link}- className={CS.textWrap}- lh="1rem"- to={Urls.transformList({ collectionId: folder.id })}- >- <Group gap="sm" wrap="nowrap">- {index === 0 && <FixedSizeIcon name="folder" />}- {folder.name}- </Group>- </Anchor>- ))}+ <Anchor+ component={Link}+ className={CS.textWrap}+ lh="1rem"+ to={Urls.transformList({ collectionId: collection.id })}+ >+ <Group gap="sm" wrap="nowrap">+ <FixedSizeIcon name="folder" />+ {collection.name}+ </Group>+ </Anchor></Breadcrumbs></div>);diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/pages/RunListPage/RunSidebar/LocationSection/LocationSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/transforms/pages/RunListPage/RunSidebar/LocationSection/LocationSection.unit.spec.tsxnew file mode 100644index 00000000000..eff11e0a82e--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/transforms/pages/RunListPage/RunSidebar/LocationSection/LocationSection.unit.spec.tsx@@ -0,0 +1,53 @@+import { renderWithProviders, screen } from "__support__/ui";+import type { TransformRun } from "metabase-types/api";+import {+ createMockCollection,+ createMockTransform,+ createMockTransformRun,+} from "metabase-types/api/mocks";++import { LocationSection } from "./LocationSection";++type SetupOpts = {+ run?: TransformRun;+};++function setup({ run = createMockTransformRun() }: SetupOpts = {}) {+ renderWithProviders(<LocationSection run={run} />);+}++describe("LocationSection", () => {+ it("should render the collection name when the transform has a collection", () => {+ const collection = createMockCollection({ name: "My Folder" });+ const transform = createMockTransform({+ collection_id: collection.id,+ collection,+ });+ const run = createMockTransformRun({ transform });+ setup({ run });++ expect(+ screen.getByRole("region", { name: "Location" }),+ ).toBeInTheDocument();+ expect(screen.getByText("My Folder")).toBeInTheDocument();+ });++ it("should not render when the transform has no collection", () => {+ const transform = createMockTransform({ collection_id: null });+ const run = createMockTransformRun({ transform });+ setup({ run });++ expect(+ screen.queryByRole("region", { name: "Location" }),+ ).not.toBeInTheDocument();+ });++ it("should not render when the run has no transform", () => {+ const run = createMockTransformRun({ transform: undefined });+ setup({ run });++ expect(+ screen.queryByRole("region", { name: "Location" }),+ ).not.toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformListPage/utils.ts b/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformListPage/utils.tsindex a9959898781..69cd7a40776 100644--- a/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformListPage/utils.ts+++ b/enterprise/frontend/src/metabase-enterprise/transforms/pages/TransformListPage/utils.ts@@ -1,5 +1,5 @@import { getCollectionIcon } from "metabase/entities/collections/utils";-import type { Collection, Transform } from "metabase-types/api";+import type { Collection, CollectionId, Transform } from "metabase-types/api";import {type TreeNode,@@ -15,7 +15,7 @@ export function buildTreeData(return [];}- const transformsByCollectionId = new Map<number | null, Transform[]>();+ const transformsByCollectionId = new Map<CollectionId | null, Transform[]>();for (const transform of transforms ?? []) {const collectionId = transform.collection_id;if (!transformsByCollectionId.has(collectionId)) {diff --git a/frontend/src/metabase-types/api/transform.ts b/frontend/src/metabase-types/api/transform.tsindex b96d9d7c44b..7751abc7418 100644--- a/frontend/src/metabase-types/api/transform.ts+++ b/frontend/src/metabase-types/api/transform.ts@@ -1,3 +1,4 @@+import type { Collection, CollectionId } from "./collection";import type { DatabaseId } from "./database";import type { RowValue } from "./dataset";import type { PaginationRequest, PaginationResponse } from "./pagination";@@ -24,7 +25,7 @@ export type Transform = {source: TransformSource;source_type: "native" | "python" | "mbql";target: TransformTarget;- collection_id: number | null;+ collection_id: CollectionId | null;created_at: string;updated_at: string;source_readable: boolean;@@ -41,6 +42,7 @@ export type Transform = {owner?: TransformOwner | null;// hydrated fields+ collection?: Collection | null;tag_ids?: TransformTagId[];table?: Table | null;last_run?: TransformRun | null;