Skip to content

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

fix: cosmos container selection loss (#21074)

ecmadao7555b2a61c12
diff --git a/frontend/src/modules/sql-editor/components/SQLEditorRouteShell.test.tsx b/frontend/src/modules/sql-editor/components/SQLEditorRouteShell.test.tsx
index 793f820d60..1d49b87887 100644
--- a/frontend/src/modules/sql-editor/components/SQLEditorRouteShell.test.tsx
+++ b/frontend/src/modules/sql-editor/components/SQLEditorRouteShell.test.tsx
@@ -49,6 +49,8 @@ const mocks = vi.hoisted(() => {
name,
project: "projects/proj1",
})),
+ getWorksheetByName: vi.fn(),
+ getOrFetchWorksheetByName: vi.fn(),
searchProjects: vi.fn(),
beforeEach: vi.fn(() => vi.fn()),
navigateReplace: vi.fn(),
@@ -138,6 +140,8 @@ vi.mock("@/stores/app", () => ({
useAppStore: {
getState: () => ({
getOrFetchDatabaseByName: mocks.getOrFetchDatabaseByName,
+ getWorksheetByName: mocks.getWorksheetByName,
+ getOrFetchWorksheetByName: mocks.getOrFetchWorksheetByName,
searchProjects: mocks.searchProjects,
serverInfo: {
defaultProject: "projects/proj1",
@@ -163,6 +167,11 @@ vi.mock("@/modules/sql-editor/store", () => ({
),
}));
+vi.mock("@/utils", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@/utils")>()),
+ isWorksheetReadableV1: vi.fn(() => true),
+}));
+
vi.mock("@/modules/sql-editor/store/editor", () => ({
getSQLEditorEditorState: () => mocks.editorState,
useSQLEditorEditorState: (selector: (state: unknown) => unknown) =>
@@ -225,6 +234,17 @@ beforeEach(() => {
name,
project: "projects/proj1",
}));
+ mocks.getWorksheetByName.mockImplementation((name: string) => ({
+ name,
+ project: "projects/proj1",
+ }));
+ mocks.getOrFetchWorksheetByName.mockImplementation(async (name: string) => ({
+ name,
+ project: "projects/proj1",
+ database: "instances/inst1/databases/db1",
+ content: new TextEncoder().encode("select 1"),
+ contentSize: BigInt(new TextEncoder().encode("select 1").length),
+ }));
mocks.editorState.project = "projects/proj1";
mocks.tabsState.tabsById = new Map();
mocks.tabsState.openTmpTabList = [];
@@ -503,4 +523,111 @@ describe("SQLEditorRouteShell", () => {
unmount();
});
+
+ test("seeds worksheet route tabs with schema and table from the URL", async () => {
+ mocks.renderRoute = {
+ ...mocks.renderRoute,
+ name: "sql-editor.worksheet",
+ params: {
+ project: "proj1",
+ sheet: "sheet1",
+ },
+ query: {
+ schema: "public",
+ table: "SUPPORDERS_VIS.items",
+ },
+ requiredPermissions: [],
+ };
+ mocks.currentRoute = {
+ ...mocks.currentRoute,
+ name: "sql-editor.worksheet",
+ params: {
+ project: "proj1",
+ sheet: "sheet1",
+ },
+ query: {
+ schema: "public",
+ table: "SUPPORDERS_VIS.items",
+ },
+ requiredPermissions: [],
+ };
+
+ const { unmount } = renderShell();
+
+ await act(async () => {
+ await Promise.resolve();
+ await Promise.resolve();
+ await Promise.resolve();
+ });
+
+ expect(mocks.tabsState.addTab).toHaveBeenCalledWith({
+ id: undefined,
+ connection: {
+ instance: "instances/inst1",
+ database: "instances/inst1/databases/db1",
+ schema: "public",
+ table: "SUPPORDERS_VIS.items",
+ },
+ worksheet: "projects/proj1/worksheets/sheet1",
+ title: undefined,
+ statement: "select 1",
+ status: "CLEAN",
+ });
+
+ unmount();
+ });
+
+ test("keeps schema and table query parameters when syncing worksheet tabs", async () => {
+ mocks.renderRoute = {
+ ...mocks.renderRoute,
+ name: "sql-editor.project",
+ params: {
+ project: "proj1",
+ },
+ query: {},
+ requiredPermissions: [],
+ };
+ mocks.currentRoute = {
+ ...mocks.currentRoute,
+ name: "sql-editor.project",
+ params: {
+ project: "proj1",
+ },
+ query: {},
+ requiredPermissions: [],
+ };
+ mocks.tabsState.tabsById.set("tab-worksheet", {
+ id: "tab-worksheet",
+ worksheet: "projects/proj1/worksheets/sheet1",
+ connection: {
+ instance: "instances/inst1",
+ database: "instances/inst1/databases/db1",
+ schema: "public",
+ table: "SUPPORDERS_VIS.items",
+ },
+ } as SQLEditorTab);
+ mocks.tabsState.currentTabId = "tab-worksheet";
+
+ const { unmount } = renderShell();
+
+ await act(async () => {
+ await Promise.resolve();
+ await Promise.resolve();
+ await Promise.resolve();
+ });
+
+ expect(mocks.navigateReplace).toHaveBeenCalledWith({
+ name: "sql-editor.worksheet",
+ params: {
+ project: "proj1",
+ sheet: "sheet1",
+ },
+ query: {
+ table: "SUPPORDERS_VIS.items",
+ schema: "public",
+ },
+ });
+
+ unmount();
+ });
});
diff --git a/frontend/src/modules/sql-editor/components/SQLEditorRouteShell.tsx b/frontend/src/modules/sql-editor/components/SQLEditorRouteShell.tsx
index 309ba8c220..d4ad062847 100644
--- a/frontend/src/modules/sql-editor/components/SQLEditorRouteShell.tsx
+++ b/frontend/src/modules/sql-editor/components/SQLEditorRouteShell.tsx
@@ -221,6 +221,15 @@ export function SQLEditorRouteShell() {
return false;
}
const connection = await extractWorksheetConnection(sheet);
+ const schema = route.query.schema;
+ const table = route.query.table;
+ if (typeof schema === "string" && schema) {
+ connection.schema = schema;
+ }
+ if (typeof table === "string" && table) {
+ connection.table = table;
+ connection.schema ??= "";
+ }
tabsState.addTab({
id: openedSheetTab?.id,
connection,
@@ -449,6 +458,11 @@ export function SQLEditorRouteShell() {
if (vals.sheetName) {
const sheet = useAppStore.getState().getWorksheetByName(vals.sheetName);
if (sheet) {
+ if (vals.schema) query.schema = vals.schema;
+ if (vals.table) {
+ query.table = vals.table;
+ query.schema = vals.schema ?? "";
+ }
await navigate.replace({
name: SQL_EDITOR_WORKSHEET_MODULE,
params: {
diff --git a/frontend/src/modules/sql-editor/store/worksheet.test.ts b/frontend/src/modules/sql-editor/store/worksheet.test.ts
index 9dd302ba33..cc9d50ab13 100644
--- a/frontend/src/modules/sql-editor/store/worksheet.test.ts
+++ b/frontend/src/modules/sql-editor/store/worksheet.test.ts
@@ -1,6 +1,7 @@
import { beforeEach, describe, expect, test, vi } from "vitest";
import { create, type StoreApi } from "zustand";
import { getSQLEditorEditorState } from "./editor";
+import { getSQLEditorTabsState } from "./tab";
import type {
QueryHistorySlice,
SQLEditorStoreState,
@@ -113,18 +114,14 @@ vi.mock("@/modules/sql-editor/model/Sheet", () => ({
openWorksheetByName: vi.fn(),
}));
-vi.mock("@/utils", async (importOriginal) => {
- const actual = (await importOriginal()) as Record<string, unknown>;
- return {
- ...actual,
- extractWorksheetConnection: vi.fn(
- async ({ database }: { database: string }) => ({
- database,
- instance: "",
- })
- ),
- };
-});
+vi.mock("@/lib/sqlEditorConnection", () => ({
+ extractWorksheetConnection: vi.fn(
+ async ({ database }: { database: string }) => ({
+ instance: database ? "instances/cosmos" : "",
+ database,
+ })
+ ),
+}));
const makeStore = (): StoreApi<SQLEditorStoreState> =>
create<SQLEditorStoreState>()((...args) => ({
@@ -146,6 +143,7 @@ beforeEach(() => {
piniaMocks.editorStore.project = "projects/default";
piniaMocks.editorStore.projectContextReady = true;
getSQLEditorEditorState().setProject("");
+ getSQLEditorTabsState().reset();
});
describe("worksheet save slice — autoSaveController", () => {
@@ -178,6 +176,43 @@ describe("worksheet save slice — autoSaveController", () => {
});
});
+describe("worksheet save slice — maybeUpdateWorksheet", () => {
+ test("preserves the selected Cosmos DB container when saving the worksheet", async () => {
+ const store = makeStore();
+ const tab = getSQLEditorTabsState().addTab({
+ worksheet: "projects/default/worksheets/cosmos-sheet",
+ connection: {
+ instance: "instances/cosmos",
+ database: "instances/cosmos/databases/grs",
+ table: "SUPPORDERS_VIS.items",
+ },
+ statement: "select * from SUPPORDERS_VIS.items",
+ status: "DIRTY",
+ });
+ piniaMocks.worksheetStore.getWorksheetByName.mockReturnValue({
+ name: tab.worksheet,
+ title: "Cosmos worksheet",
+ database: tab.connection.database,
+ });
+ piniaMocks.worksheetStore.patchWorksheet.mockResolvedValue({
+ name: tab.worksheet,
+ });
+
+ await store.getState().maybeUpdateWorksheet({
+ tabId: tab.id,
+ worksheet: tab.worksheet,
+ database: tab.connection.database,
+ statement: tab.statement,
+ });
+
+ expect(getSQLEditorTabsState().tabsById.get(tab.id)?.connection).toEqual({
+ instance: "instances/cosmos",
+ database: "instances/cosmos/databases/grs",
+ table: "SUPPORDERS_VIS.items",
+ });
+ });
+});
+
describe("worksheet save slice — maybeSwitchProject", () => {
test("with an invalid project name returns undefined without setting project", async () => {
const store = makeStore();
diff --git a/frontend/src/modules/sql-editor/store/worksheet.ts b/frontend/src/modules/sql-editor/store/worksheet.ts
index 989ad95f8e..363dc35710 100644
--- a/frontend/src/modules/sql-editor/store/worksheet.ts
+++ b/frontend/src/modules/sql-editor/store/worksheet.ts
@@ -75,6 +75,13 @@ export const createWorksheetSaveSlice: SQLEditorSliceCreator<
const worksheetStore = useAppStore.getState();
const connection = await extractWorksheetConnection({ database });
+ const currentTab = tabStore.tabsById.get(tabId);
+ const nextConnection =
+ currentTab &&
+ currentTab.connection.instance === connection.instance &&
+ currentTab.connection.database === connection.database
+ ? { ...currentTab.connection, ...connection }
+ : connection;
// `title === undefined` means "don't change the title" — preserves
// the current title on auto-save calls that never pass one.
@@ -115,7 +122,7 @@ export const createWorksheetSaveSlice: SQLEditorSliceCreator<
return tabStore.updateTab(tabId, {
status: "CLEAN",
- connection,
+ connection: nextConnection,
title: worksheetTitle,
worksheet,
});