Skip to content

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

fix: add new table (#20717)

ecmadaod31146d2ce24
diff --git a/frontend/src/react/components/SchemaEditorLite/Panels/DatabaseEditor.test.tsx b/frontend/src/react/components/SchemaEditorLite/Panels/DatabaseEditor.test.tsx
new file mode 100644
index 0000000000..39b1a81278
--- /dev/null
+++ b/frontend/src/react/components/SchemaEditorLite/Panels/DatabaseEditor.test.tsx
@@ -0,0 +1,87 @@
+import { create } from "@bufbuild/protobuf";
+import { fireEvent, render, screen } from "@testing-library/react";
+import { describe, expect, test, vi } from "vitest";
+import type { Database } from "@/types/proto-es/v1/database_service_pb";
+import {
+ DatabaseMetadataSchema,
+ SchemaMetadataSchema,
+} from "@/types/proto-es/v1/database_service_pb";
+import { SchemaEditorProvider } from "../context";
+import type { SchemaEditorContextValue } from "../types";
+import { DatabaseEditor } from "./DatabaseEditor";
+
+vi.mock("react-i18next", () => ({
+ initReactI18next: {
+ type: "3rdParty",
+ init: vi.fn(),
+ },
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+
+vi.mock("@/utils", () => ({
+ getDatabaseEngine: () => 0,
+ hasSchemaProperty: () => true,
+}));
+
+vi.mock("./TableList", () => ({
+ TableList: () => <div data-testid="table-list" />,
+}));
+
+vi.mock("../Modals/TableNamePopover", () => ({
+ TableNamePopover: ({
+ open,
+ schema,
+ }: {
+ open: boolean;
+ schema: { name: string };
+ }) =>
+ open ? <div data-testid="table-name-popover">{schema.name}</div> : null,
+}));
+
+const makeContext = (): SchemaEditorContextValue =>
+ ({
+ readonly: false,
+ tabs: {
+ addTab: vi.fn(),
+ },
+ editStatus: {
+ getSchemaStatus: vi.fn(() => "normal"),
+ },
+ }) as unknown as SchemaEditorContextValue;
+
+describe("DatabaseEditor", () => {
+ test("opens the create-table popover from the toolbar button", () => {
+ const db = { name: "instances/test/databases/db" } as Database;
+ const schema = create(SchemaMetadataSchema, {
+ name: "public",
+ tables: [],
+ });
+ const database = create(DatabaseMetadataSchema, {
+ name: "instances/test/databases/db/metadata",
+ schemas: [schema],
+ });
+
+ render(
+ <SchemaEditorProvider value={makeContext()}>
+ <DatabaseEditor
+ db={db}
+ database={database}
+ selectedSchemaName="public"
+ onSelectedSchemaNameChange={vi.fn()}
+ />
+ </SchemaEditorProvider>
+ );
+
+ expect(screen.queryByTestId("table-name-popover")).not.toBeInTheDocument();
+
+ fireEvent.click(
+ screen.getByRole("button", {
+ name: "schema-editor.actions.create-table",
+ })
+ );
+
+ expect(screen.getByTestId("table-name-popover")).toHaveTextContent(
+ "public"
+ );
+ });
+});
diff --git a/frontend/src/react/components/SchemaEditorLite/Panels/DatabaseEditor.tsx b/frontend/src/react/components/SchemaEditorLite/Panels/DatabaseEditor.tsx
index 79b27cbe2f..93e6e63cb9 100644
--- a/frontend/src/react/components/SchemaEditorLite/Panels/DatabaseEditor.tsx
+++ b/frontend/src/react/components/SchemaEditorLite/Panels/DatabaseEditor.tsx
@@ -1,5 +1,5 @@
import { Plus } from "lucide-react";
-import { useCallback, useEffect, useMemo } from "react";
+import { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { Button } from "@/react/components/ui/button";
import { Combobox } from "@/react/components/ui/combobox";
@@ -9,6 +9,7 @@ import type {
} from "@/types/proto-es/v1/database_service_pb";
import { getDatabaseEngine, hasSchemaProperty } from "@/utils";
import { useSchemaEditorContext } from "../context";
+import { TableNamePopover } from "../Modals/TableNamePopover";
import { TableList } from "./TableList";
interface Props {
@@ -29,6 +30,8 @@ export function DatabaseEditor({
const { t } = useTranslation();
const { readonly, tabs, editStatus } = useSchemaEditorContext();
const engine = getDatabaseEngine(db);
+ const [tableNamePopoverAnchorPoint, setTableNamePopoverAnchorPoint] =
+ useState<{ x: number; y: number } | null>(null);
const shouldShowSchemaSelector = hasSchemaProperty(engine);
@@ -121,8 +124,12 @@ export function DatabaseEditor({
variant="outline"
size="sm"
className="gap-1.5"
- onClick={() => {
- // Will be wired to TableNameDialog in T22
+ onClick={(e) => {
+ const rect = e.currentTarget.getBoundingClientRect();
+ setTableNamePopoverAnchorPoint({
+ x: rect.left,
+ y: rect.bottom,
+ });
}}
>
<Plus className="size-4" />
@@ -140,6 +147,16 @@ export function DatabaseEditor({
onEditTable={handleEditTable}
/>
</div>
+ {tableNamePopoverAnchorPoint && (
+ <TableNamePopover
+ open
+ onClose={() => setTableNamePopoverAnchorPoint(null)}
+ anchorPoint={tableNamePopoverAnchorPoint}
+ db={db}
+ database={database}
+ schema={effectiveSchema}
+ />
+ )}
</div>
);
}