Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/bright-dragons-introspect-once.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@prisma/studio-core": minor
---

# Fix duplicate startup introspection requests

Avoid cancelling and repeating introspection requests when Studio initially mounts.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
1 change: 1 addition & 0 deletions Architecture/introspection.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Failure diagnostics MUST include:

Changes to this subsystem MUST include tests for:

- a single initial introspection without mount-time cancellation or refetch
- failed initial introspection without automatic retry
- stale-data preservation after a failed refetch
- startup recovery UI rendering
Expand Down
1 change: 1 addition & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Each adapter handles introspection, querying, inserts, updates, and deletes whil

Studio introspects connected databases to build schemas, tables, columns, relationships, filter operators, and timezone metadata.
This gives users an accurate live model of the database and keeps table navigation grounded in current structure.
A fresh Studio mount performs this discovery once, while actual adapter or database-availability changes invalidate cached metadata and load it again.

## Deployable Prisma Postgres Demo

Expand Down
61 changes: 57 additions & 4 deletions ui/studio/context.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { QueryClient } from "@tanstack/react-query";
import type { ReactNode } from "react";
import { act } from "react";
import { createRoot } from "react-dom/client";
Expand Down Expand Up @@ -50,10 +51,20 @@ function createAdapter(): Adapter {
} as unknown as Adapter;
}

function renderHarness(props?: { streamsUrl?: string }) {
type RenderHarnessProps = {
adapter?: Adapter;
hasDatabase?: boolean;
streamsUrl?: string;
};

function renderHarness(props?: RenderHarnessProps) {
const container = document.createElement("div");
document.body.appendChild(container);
const root = createRoot(container);
let currentProps = {
...props,
adapter: props?.adapter ?? createAdapter(),
};

let latestStudio: ReturnType<typeof useStudio> | undefined;

Expand All @@ -62,15 +73,20 @@ function renderHarness(props?: { streamsUrl?: string }) {
return null;
}

act(() => {
function render() {
root.render(
<StudioContextProvider
adapter={createAdapter()}
streamsUrl={props?.streamsUrl}
adapter={currentProps.adapter}
hasDatabase={currentProps.hasDatabase}
streamsUrl={currentProps.streamsUrl}
>
<Harness />
</StudioContextProvider>,
);
}

act(() => {
render();
});

return {
Expand All @@ -83,6 +99,16 @@ function renderHarness(props?: { streamsUrl?: string }) {
getLatestStudio() {
return latestStudio;
},
rerender(nextProps: RenderHarnessProps) {
currentProps = {
...currentProps,
...nextProps,
};

act(() => {
render();
});
},
};
}

Expand Down Expand Up @@ -170,6 +196,33 @@ afterEach(() => {
.VERSION_INJECTED_AT_BUILD_TIME;
});

describe("StudioContextProvider database cache lifecycle", () => {
it("resets cached queries only after the database configuration changes", () => {
const resetQueriesSpy = vi
.spyOn(QueryClient.prototype, "resetQueries")
.mockResolvedValue();
const initialAdapter = createAdapter();
const harness = renderHarness({ adapter: initialAdapter });

try {
expect(resetQueriesSpy).not.toHaveBeenCalled();

const nextAdapter = createAdapter();
harness.rerender({ adapter: nextAdapter });
expect(resetQueriesSpy).toHaveBeenCalledTimes(1);

harness.rerender({ adapter: nextAdapter });
expect(resetQueriesSpy).toHaveBeenCalledTimes(1);

harness.rerender({ adapter: nextAdapter, hasDatabase: false });
expect(resetQueriesSpy).toHaveBeenCalledTimes(2);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
} finally {
harness.cleanup();
resetQueriesSpy.mockRestore();
}
});
});

describe("StudioContextProvider pagination preferences", () => {
it("persists shared page-size and infinite-scroll preferences across remounts", () => {
const firstHarness = renderHarness();
Expand Down
13 changes: 12 additions & 1 deletion ui/studio/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ export function StudioContextProvider(props: StudioContextProviderProps) {
} = props;

const queryClientRef = useRef(new QueryClient());
const previousDatabaseConfigRef = useRef({ adapter, hasDatabase });
const signatureRef = useRef(shortUUID.generate());
const rowsCollectionCacheRef = useRef(new Map<string, unknown>());
const tableQueryExecutionStateCacheRef = useRef(
Expand Down Expand Up @@ -526,7 +527,17 @@ export function StudioContextProvider(props: StudioContextProviderProps) {
}, [studioUiCollection]);

useEffect(() => {
// if the adapter has been changed, then we need to reload
const previousDatabaseConfig = previousDatabaseConfigRef.current;
previousDatabaseConfigRef.current = { adapter, hasDatabase };

if (
previousDatabaseConfig.adapter === adapter &&
previousDatabaseConfig.hasDatabase === hasDatabase
) {
return;
}

// If the database configuration changed, then we need to reload.
for (const state of tableQueryExecutionStateCacheRef.current.values()) {
state.activeController?.abort();
}
Expand Down