Skip to content

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

fix(iam): page the database resource selector to avoid freeze at scale (BYT-9785) (#20678)

boojacka0dc62676099
diff --git a/frontend/src/react/components/DatabaseResourceSelector.test.tsx b/frontend/src/react/components/DatabaseResourceSelector.test.tsx
index 0fdedb04c9..01d30d7fda 100644
--- a/frontend/src/react/components/DatabaseResourceSelector.test.tsx
+++ b/frontend/src/react/components/DatabaseResourceSelector.test.tsx
@@ -335,6 +335,194 @@ describe("DatabaseResourceSelector", () => {
unmount();
});
+ test("loads one page at a time instead of draining every page", async () => {
+ mocks.fetchDatabases.mockReset();
+ mocks.fetchDatabases
+ .mockResolvedValueOnce({
+ databases: [
+ {
+ name: "instances/prod/databases/db1",
+ environment: "environments/prod",
+ effectiveEnvironment: "environments/prod",
+ instanceResource: { title: "Prod" },
+ },
+ ],
+ nextPageToken: "page-2",
+ })
+ .mockResolvedValueOnce({
+ databases: [
+ {
+ name: "instances/prod/databases/db2",
+ environment: "environments/prod",
+ effectiveEnvironment: "environments/prod",
+ instanceResource: { title: "Prod" },
+ },
+ ],
+ nextPageToken: "",
+ });
+
+ const { container, unmount } = renderIntoContainer(<Harness />);
+ await flushPromises();
+ await waitForText(container, "db1");
+
+ // Opening the selector fetches exactly one bounded page, not the whole
+ // list — this is the BYT-9785 freeze fix.
+ expect(mocks.fetchDatabases).toHaveBeenCalledTimes(1);
+ expect(mocks.fetchDatabases).toHaveBeenLastCalledWith(
+ expect.objectContaining({ pageSize: 200, pageToken: "" })
+ );
+ expect(container.textContent).not.toContain("db2");
+
+ const loadMoreButton = Array.from(
+ container.querySelectorAll("button")
+ ).find((button) => button.textContent?.includes("load-more"));
+ expect(loadMoreButton).toBeTruthy();
+
+ act(() => {
+ loadMoreButton?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ });
+ await flushPromises();
+ await waitForText(container, "db2");
+
+ expect(mocks.fetchDatabases).toHaveBeenCalledTimes(2);
+ expect(mocks.fetchDatabases).toHaveBeenLastCalledWith(
+ expect.objectContaining({ pageSize: 200, pageToken: "page-2" })
+ );
+
+ unmount();
+ });
+
+ test("discards stale load-more results when the filter changes mid-flight", async () => {
+ mocks.fetchDatabases.mockReset();
+ let resolveStaleLoadMore: (value: unknown) => void = () => {};
+ mocks.fetchDatabases
+ // Initial first page.
+ .mockResolvedValueOnce({
+ databases: [
+ {
+ name: "instances/prod/databases/db1",
+ instanceResource: { title: "Prod" },
+ },
+ ],
+ nextPageToken: "page-2",
+ })
+ // Load-more request: stays pending until we resolve it by hand.
+ .mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ resolveStaleLoadMore = resolve;
+ })
+ )
+ // Refetch triggered by the filter change.
+ .mockResolvedValueOnce({
+ databases: [
+ {
+ name: "instances/prod/databases/db3",
+ instanceResource: { title: "Prod" },
+ },
+ ],
+ nextPageToken: "",
+ });
+
+ const { container, unmount } = renderIntoContainer(<Harness />);
+ await flushPromises();
+ await waitForText(container, "db1");
+
+ // Kick off load more — the request hangs.
+ const loadMoreButton = Array.from(
+ container.querySelectorAll("button")
+ ).find((button) => button.textContent?.includes("load-more"));
+ act(() => {
+ loadMoreButton?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ });
+ await flushPromises();
+
+ // Change the filter before load-more resolves -> new first page (db3).
+ clickFirstButtonInRow(container, "advanced-search:");
+ await flushPromises();
+ await waitForText(container, "db3");
+
+ // The stale load-more now resolves with a page from the old filter.
+ act(() => {
+ resolveStaleLoadMore({
+ databases: [
+ {
+ name: "instances/prod/databases/db2",
+ instanceResource: { title: "Prod" },
+ },
+ ],
+ nextPageToken: "",
+ });
+ });
+ await flushPromises();
+
+ // Stale page must not leak into the filtered list.
+ expect(container.textContent).not.toContain("db2");
+ expect(container.textContent).toContain("db3");
+
+ unmount();
+ });
+
+ test("hides Load more while a new filter's first page is refetching", async () => {
+ mocks.fetchDatabases.mockReset();
+ let resolveRefetch: (value: unknown) => void = () => {};
+ mocks.fetchDatabases
+ // Initial first page with more pages available.
+ .mockResolvedValueOnce({
+ databases: [
+ {
+ name: "instances/prod/databases/db1",
+ instanceResource: { title: "Prod" },
+ },
+ ],
+ nextPageToken: "page-2",
+ })
+ // First page after the filter change — stays pending so we can inspect
+ // the in-between state.
+ .mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ resolveRefetch = resolve;
+ })
+ );
+
+ const { container, unmount } = renderIntoContainer(<Harness />);
+ await flushPromises();
+ await waitForText(container, "db1");
+
+ const hasLoadMore = () =>
+ Array.from(container.querySelectorAll("button")).some((button) =>
+ button.textContent?.includes("load-more")
+ );
+ expect(hasLoadMore()).toBe(true);
+
+ // Change the filter; the new first page is still in flight.
+ clickFirstButtonInRow(container, "advanced-search:");
+ await flushPromises();
+
+ // The stale page token must be cleared so Load more cannot fire a request
+ // pairing the old offset token with the new filter.
+ expect(hasLoadMore()).toBe(false);
+
+ // Once the new first page resolves, Load more reflects the new token.
+ act(() => {
+ resolveRefetch({
+ databases: [
+ {
+ name: "instances/prod/databases/db3",
+ instanceResource: { title: "Prod" },
+ },
+ ],
+ nextPageToken: "new-page-2",
+ });
+ });
+ await flushPromises();
+ await waitForText(container, "db3");
+ expect(hasLoadMore()).toBe(true);
+
+ unmount();
+ });
+
test("selecting a table replaces child column selections", async () => {
const onValueChange = vi.fn();
const { container, unmount } = renderIntoContainer(
diff --git a/frontend/src/react/components/DatabaseResourceSelector.tsx b/frontend/src/react/components/DatabaseResourceSelector.tsx
index 300d26f2d6..c5b7d2b39d 100644
--- a/frontend/src/react/components/DatabaseResourceSelector.tsx
+++ b/frontend/src/react/components/DatabaseResourceSelector.tsx
@@ -7,7 +7,7 @@ import {
Table2,
X,
} from "lucide-react";
-import { useCallback, useEffect, useMemo, useState } from "react";
+import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import {
AdvancedSearch,
@@ -66,6 +66,13 @@ const instanceNamePrefix = "instances/";
const UNKNOWN_ENVIRONMENT_ID = "-1";
const UNKNOWN_ENVIRONMENT_NAME = `${environmentNamePrefix}${UNKNOWN_ENVIRONMENT_ID}`;
+// Fetch and render databases one page at a time. The selector used to drain
+// every page in a do-while loop and render all rows at once, which froze the
+// tab for projects with thousands of databases (BYT-9785). One bounded page
+// keeps both the network payload and the DOM node count in check; the user
+// loads more on demand or narrows with the instance/environment filter.
+const DATABASE_PAGE_SIZE = 200;
+
interface DatabaseFilter {
instance?: string;
environment?: string;
@@ -98,6 +105,8 @@ export function DatabaseResourceSelector({
);
const [databases, setDatabases] = useState<Database[]>([]);
+ const [nextPageToken, setNextPageToken] = useState("");
+ const [loadingMore, setLoadingMore] = useState(false);
const [searchParams, setSearchParams] = useState<SearchParams>({
query: "",
scopes: [],
@@ -221,29 +230,56 @@ export function DatabaseResourceSelector({
};
}, [searchParams]);
+ // Bumped whenever the project or filter changes so that in-flight requests
+ // (first page or load-more) from a prior filter discard their results
+ // instead of appending stale databases into the current filtered list.
+ const requestGenerationRef = useRef(0);
+
useEffect(() => {
- let cancelled = false;
- const fetchAll = async () => {
- let allDatabases: Database[] = [];
- let pageToken = "";
- do {
- const result = await useAppStore.getState().fetchDatabases({
- parent: projectName,
- pageSize: 1000,
- pageToken,
- filter: databaseFilter,
- });
- allDatabases = [...allDatabases, ...result.databases];
- pageToken = result.nextPageToken;
- } while (pageToken);
- if (!cancelled) setDatabases(allDatabases);
- };
- fetchAll();
- return () => {
- cancelled = true;
+ requestGenerationRef.current += 1;
+ const generation = requestGenerationRef.current;
+ // A new filter context begins. Clear the pending load-more flag and the
+ // previous page token so the stale Load more button can't fire a request
+ // that pairs the old offset token with the new filter (page tokens are
+ // offsets bound to the same list params). The new token arrives with the
+ // first page below.
+ setLoadingMore(false);
+ setNextPageToken("");
+ const fetchFirstPage = async () => {
+ const result = await useAppStore.getState().fetchDatabases({
+ parent: projectName,
+ pageSize: DATABASE_PAGE_SIZE,
+ pageToken: "",
+ filter: databaseFilter,
+ });
+ if (generation !== requestGenerationRef.current) return;
+ setDatabases(result.databases);
+ setNextPageToken(result.nextPageToken);
};
+ fetchFirstPage();
}, [projectName, databaseFilter]);
+ const loadMore = useCallback(async () => {
+ if (!nextPageToken || loadingMore) return;
+ const generation = requestGenerationRef.current;
+ setLoadingMore(true);
+ try {
+ const result = await useAppStore.getState().fetchDatabases({
+ parent: projectName,
+ pageSize: DATABASE_PAGE_SIZE,
+ pageToken: nextPageToken,
+ filter: databaseFilter,
+ });
+ if (generation !== requestGenerationRef.current) return;
+ setDatabases((prev) => [...prev, ...result.databases]);
+ setNextPageToken(result.nextPageToken);
+ } finally {
+ if (generation === requestGenerationRef.current) {
+ setLoadingMore(false);
+ }
+ }
+ }, [projectName, databaseFilter, nextPageToken, loadingMore]);
+
const selectedResourceMap = useMemo(() => {
const map = new Map<string, DatabaseSelection>();
for (const resource of value) {
@@ -836,7 +872,9 @@ export function DatabaseResourceSelector({
: t("common.select-all")}
</button>
<span>
- {t("common.total")} {databases.length}{" "}
+ {nextPageToken
+ ? `${databases.length}+`
+ : `${t("common.total")} ${databases.length}`}{" "}
{t("common.items", { count: databases.length })}
</span>
</div>
@@ -971,6 +1009,18 @@ export function DatabaseResourceSelector({
</div>
);
})}
+ {nextPageToken && (
+ <button
+ type="button"
+ className="w-full px-2 py-1.5 text-sm text-accent hover:underline cursor-pointer disabled:opacity-50 disabled:no-underline"
+ onClick={loadMore}
+ disabled={loadingMore}
+ >
+ {loadingMore
+ ? `${t("common.loading")}...`
+ : t("common.load-more")}
+ </button>
+ )}
</div>
</div>
<div className="flex-1 flex flex-col min-w-0">