Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: batch request db tab

ecmadao1e09a6c24ecd
diff --git a/frontend/src/modules/sql-editor/components/ResultPanel/BatchQuerySelect.test.tsx b/frontend/src/modules/sql-editor/components/ResultPanel/BatchQuerySelect.test.tsx
new file mode 100644
index 0000000000..8e6f414ccb
--- /dev/null
+++ b/frontend/src/modules/sql-editor/components/ResultPanel/BatchQuerySelect.test.tsx
@@ -0,0 +1,224 @@
+import { act, type ReactNode } from "react";
+import { createRoot } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { Engine } from "@/types/proto-es/v1/common_pb";
+import type { Database } from "@/types/proto-es/v1/database_service_pb";
+import { BatchQuerySelect } from "./BatchQuerySelect";
+
+(
+ globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
+).IS_REACT_ACT_ENVIRONMENT = true;
+
+const mocks = vi.hoisted(() => ({
+ databasesByName: {} as Record<string, Database>,
+ databaseQueryContexts: new Map<string, unknown[]>(),
+ environment: {
+ name: "environments/prod",
+ id: "prod",
+ title: "Production",
+ color: "#4f46e5",
+ order: 0,
+ tags: {},
+ },
+ deleteDatabaseQueryContext: vi.fn(),
+}));
+
+vi.mock("react-i18next", () => ({
+ initReactI18next: { type: "3rdParty", init: () => {} },
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+
+vi.mock("@/components/DataExportButton", () => ({
+ DataExportButton: () => null,
+}));
+
+vi.mock("@/components/database", () => ({
+ DatabaseTableView: () => null,
+}));
+
+vi.mock("@/components/EngineIcon", () => ({
+ EngineIcon: ({ className }: { className?: string }) => (
+ <span className={className} data-testid="engine-icon" />
+ ),
+}));
+
+vi.mock("@/components/EnvironmentLabel", () => ({
+ EnvironmentLabel: ({ environment }: { environment: { title: string } }) => (
+ <span>{environment.title}</span>
+ ),
+}));
+
+vi.mock("@/components/ui/ellipsis-text", () => ({
+ EllipsisText: ({
+ children,
+ className,
+ text,
+ }: {
+ children?: ReactNode;
+ className?: string;
+ text: string;
+ }) => <span className={className}>{children ?? text}</span>,
+}));
+
+vi.mock("@/modules/sql-editor/components/RequestExportButton", () => ({
+ RequestExportButton: () => null,
+}));
+
+vi.mock("@/modules/sql-editor/components/useExportGrantBypass", () => ({
+ useExportGrantBypass: () => ({
+ matchedDatabases: [],
+ unmatchedDatabases: [],
+ tooltip: undefined,
+ }),
+}));
+
+vi.mock("@/modules/sql-editor/hooks/useSQLEditorState", () => ({
+ useSQLEditorQueryDataPolicy: () => ({
+ disableExport: true,
+ maximumResultRows: 100,
+ }),
+}));
+
+vi.mock("@/modules/sql-editor/store/editor", () => ({
+ useSQLEditorEditorState: (
+ selector: (state: { project: string }) => unknown
+ ) => selector({ project: "projects/p" }),
+}));
+
+vi.mock("@/modules/sql-editor/store/tab", () => {
+ const state = () => ({
+ currentTabId: "tab-1",
+ tabsById: new Map([
+ [
+ "tab-1",
+ {
+ databaseQueryContexts: mocks.databaseQueryContexts,
+ },
+ ],
+ ]),
+ deleteDatabaseQueryContext: mocks.deleteDatabaseQueryContext,
+ });
+ return {
+ getSQLEditorTabsState: state,
+ useCurrentSQLEditorTab: () =>
+ state().tabsById.get(state().currentTabId),
+ useSQLEditorTabState: (
+ selector: (value: ReturnType<typeof state>) => unknown
+ ) => selector(state()),
+ };
+});
+
+vi.mock("@/stores/app", () => {
+ const state = () => ({
+ databasesByName: mocks.databasesByName,
+ environmentList: [mocks.environment],
+ exportData: vi.fn(),
+ getDatabaseByName: (name: string) => mocks.databasesByName[name],
+ getEnvironmentByName: () => mocks.environment,
+ });
+ return {
+ useAppStore: Object.assign(
+ (selector: (value: ReturnType<typeof state>) => unknown) =>
+ selector(state()),
+ { getState: state }
+ ),
+ };
+});
+
+vi.mock("./ContextMenu", () => ({
+ TabContextMenu: ({ children }: { children: ReactNode }) => children,
+}));
+
+describe("BatchQuerySelect", () => {
+ let container: HTMLDivElement;
+ let root: ReturnType<typeof createRoot>;
+ let cellA: Database;
+
+ beforeEach(() => {
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+
+ cellA = {
+ name: "instances/cell-a/databases/linear",
+ effectiveEnvironment: mocks.environment.name,
+ instanceResource: {
+ engine: Engine.POSTGRES,
+ name: "instances/cell-a",
+ title: "cell-a",
+ },
+ } as Database;
+ const cellB = {
+ name: "instances/cell-b/databases/linear",
+ effectiveEnvironment: mocks.environment.name,
+ instanceResource: {
+ engine: Engine.POSTGRES,
+ name: "instances/cell-b",
+ title: "cell-b",
+ },
+ } as Database;
+ mocks.databasesByName = {
+ [cellA.name]: cellA,
+ [cellB.name]: cellB,
+ };
+ mocks.databaseQueryContexts = new Map([
+ [
+ cellA.name,
+ [
+ {
+ id: "query-a",
+ params: { connection: {}, statement: "SELECT 1" },
+ status: "PENDING",
+ },
+ ],
+ ],
+ [
+ cellB.name,
+ [
+ {
+ id: "query-b",
+ params: { connection: {}, statement: "SELECT 1" },
+ status: "PENDING",
+ },
+ ],
+ ],
+ ]);
+ });
+
+ afterEach(() => {
+ act(() => root.unmount());
+ container.remove();
+ vi.clearAllMocks();
+ });
+
+ const render = () => {
+ act(() => {
+ root.render(
+ <BatchQuerySelect
+ selectedDatabase={cellA}
+ onSelectedDatabaseChange={vi.fn()}
+ />
+ );
+ });
+ return Array.from(container.querySelectorAll("button"));
+ };
+
+ it("distinguishes same-named databases by instance and environment", () => {
+ const buttons = render();
+
+ expect(buttons).toHaveLength(2);
+ expect(buttons[0].textContent).toContain("cell-a");
+ expect(buttons[0].textContent).toContain("Production");
+ expect(buttons[0].textContent).toContain("linear");
+ expect(buttons[1].textContent).toContain("cell-b");
+ expect(buttons[1].textContent).toContain("Production");
+ expect(buttons[1].textContent).toContain("linear");
+ });
+
+ it("keeps each result tab from shrinking inside the horizontal scroller", () => {
+ const buttons = render();
+
+ expect(buttons[0].className).toContain("shrink-0");
+ expect(buttons[1].className).toContain("shrink-0");
+ });
+});
diff --git a/frontend/src/modules/sql-editor/components/ResultPanel/BatchQuerySelect.tsx b/frontend/src/modules/sql-editor/components/ResultPanel/BatchQuerySelect.tsx
index 1ff214accc..9eb8efe1fd 100644
--- a/frontend/src/modules/sql-editor/components/ResultPanel/BatchQuerySelect.tsx
+++ b/frontend/src/modules/sql-editor/components/ResultPanel/BatchQuerySelect.tsx
@@ -4,13 +4,13 @@ import { head } from "lodash-es";
import { CircleAlert, Eye, EyeOff, X } from "lucide-react";
import { useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
+import { DatabaseTargetDisplay } from "@/components/DatabaseTargetDisplay";
import {
DataExportButton,
type DataExportRequest,
type DownloadContent,
} from "@/components/DataExportButton";
import { DatabaseTableView } from "@/components/database";
-import { EngineIconPath } from "@/components/instance/constants";
import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";
import { cn } from "@/lib/utils";
@@ -32,7 +32,6 @@ import { DEFAULT_ENVIRONMENT_COLOR } from "@/types/v1/environment";
import {
extractDatabaseResourceName,
getDatabaseEnvironment,
- getInstanceResource,
hexToRgb,
} from "@/utils";
import { TabContextMenu } from "./ContextMenu";
@@ -464,8 +463,6 @@ function TabButton({
borderTop: isSelected ? "3px solid" : "",
...styleProp,
};
- const instance = getInstanceResource(item.database);
-
return (
<button
type="button"
@@ -473,22 +470,17 @@ function TabButton({
style={style}
onClick={onSelect}
className={cn(
- "inline-flex items-center gap-x-1 h-7 px-2 rounded-xs text-xs font-medium",
+ "inline-flex shrink-0 items-center gap-x-1 h-7 px-2 rounded-xs text-xs font-medium",
"border border-control-border cursor-pointer whitespace-nowrap",
className
)}
{...rest}
>
- {EngineIconPath[instance.engine] && (
- <img
- src={EngineIconPath[instance.engine]}
- alt=""
- className="size-4 shrink-0"
- />
- )}
- <span className="truncate">
- {extractDatabaseResourceName(item.database.name).databaseName}
- </span>
+ <DatabaseTargetDisplay
+ database={item.database}
+ showEnvironment
+ className="text-xs"
+ />
{isFailed && <CircleAlert className="ml-1 text-error size-4 shrink-0" />}
{isEmpty && (
<span className="text-control-placeholder italic ml-1">