Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Always use "public" schema for x-rays if it exists (#62285)

Anton Kulyke4744ae67f5e
diff --git a/frontend/src/metabase/home/components/HomeXraySection/HomeXraySection.tsx b/frontend/src/metabase/home/components/HomeXraySection/HomeXraySection.tsx
index dfce5a33015..dca4427de03 100644
--- a/frontend/src/metabase/home/components/HomeXraySection/HomeXraySection.tsx
+++ b/frontend/src/metabase/home/components/HomeXraySection/HomeXraySection.tsx
@@ -12,6 +12,7 @@ import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErr
import Select from "metabase/common/components/Select";
import { useSelector } from "metabase/lib/redux";
import { isSyncCompleted } from "metabase/lib/syncing";
+import { isNotNull } from "metabase/lib/types";
import * as Urls from "metabase/lib/urls";
import { getApplicationName } from "metabase/selectors/whitelabel";
import type { Database, DatabaseXray } from "metabase-types/api";
@@ -65,11 +66,11 @@ interface HomeXrayViewProps {
const HomeXrayView = ({ database, candidates = [] }: HomeXrayViewProps) => {
const isSample = database.is_sample;
const schemas = candidates.map((d) => d.schema);
- const [schema, setSchema] = useState(schemas[0]);
+ const [schema, setSchema] = useState(getDefaultSchema(schemas));
const candidate = candidates.find((d) => d.schema === schema);
const tableCount = candidate ? candidate.tables.length : 0;
const tableMessages = useMemo(() => getMessages(tableCount), [tableCount]);
- const canSelectSchema = schemas.length > 1;
+ const canSelectSchema = schemas.length > 1 && schema !== null;
const applicationName = useSelector(getApplicationName);
return (
@@ -110,6 +111,14 @@ const HomeXrayView = ({ database, candidates = [] }: HomeXrayViewProps) => {
);
};
+const getDefaultSchema = (schemas: Array<string | null>) => {
+ return (
+ schemas
+ .filter(isNotNull)
+ .find((schema) => schema.toLowerCase() === "public") || schemas[0]
+ );
+};
+
interface SchemaSelectProps {
schema: string;
schemas: string[];
@@ -119,7 +128,9 @@ interface SchemaSelectProps {
const SchemaSelect = ({ schema, schemas, onChange }: SchemaSelectProps) => {
const trigger = (
<SchemaTrigger>
- <SchemaTriggerText>{schema}</SchemaTriggerText>
+ <SchemaTriggerText data-testid="xray-schema-name">
+ {schema}
+ </SchemaTriggerText>
<SchemaTriggerIcon name="chevrondown" />
</SchemaTrigger>
);
diff --git a/frontend/src/metabase/home/components/HomeXraySection/HomeXraySection.unit.spec.tsx b/frontend/src/metabase/home/components/HomeXraySection/HomeXraySection.unit.spec.tsx
index 32b95fc33e8..33d98966c8c 100644
--- a/frontend/src/metabase/home/components/HomeXraySection/HomeXraySection.unit.spec.tsx
+++ b/frontend/src/metabase/home/components/HomeXraySection/HomeXraySection.unit.spec.tsx
@@ -74,4 +74,27 @@ describe("HomeXraySection", () => {
expect(screen.getByText("Orders")).toBeInTheDocument();
expect(screen.queryByText("People")).not.toBeInTheDocument();
});
+
+ it("should default to 'public' schema when it's present", async () => {
+ await setup({
+ database: createMockDatabase({
+ name: "H2",
+ is_sample: false,
+ }),
+ candidates: [
+ createMockDatabaseCandidate({
+ id: "1/auth",
+ schema: "auth",
+ tables: [createMockTableCandidate({ title: "People" })],
+ }),
+ createMockDatabaseCandidate({
+ id: "1/public",
+ schema: "public",
+ tables: [createMockTableCandidate({ title: "Orders" })],
+ }),
+ ],
+ });
+
+ expect(screen.getByTestId("xray-schema-name")).toHaveTextContent("public");
+ });
});