diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts index 47386e8b87..260e62723d 100644 --- a/apps/desktop/src/stores/queryStore.ts +++ b/apps/desktop/src/stores/queryStore.ts @@ -38,6 +38,7 @@ import { decodeQueryResultArchive, encodeQueryResultArchive, type DecodedQueryRe import * as api from "@/lib/api"; import { useConnectionStore } from "@/stores/connectionStore"; import { useSettingsStore } from "@/stores/settingsStore"; +import { useSavedSqlStore } from "@/stores/savedSqlStore"; import type { SavedSqlFile } from "@/types/database"; const STORAGE_KEY = "dbx-open-tabs"; @@ -942,6 +943,22 @@ export const useQueryStore = defineStore("query", () => { tab.resultSortedSql = undefined; clearExplain(tab); tab.tableMeta = undefined; + + // Sync connection change back to the saved SQL file if this tab is linked + if (tab.savedSqlId) { + const savedSqlStore = useSavedSqlStore(); + const existing = savedSqlStore.getFile(tab.savedSqlId); + if (existing) { + void savedSqlStore.saveFile({ + id: existing.id, + connectionId, + name: existing.name, + database, + schema: existing.schema, + sql: existing.sql, + }); + } + } } function setTableMeta(id: string, meta: NonNullable) { diff --git a/apps/desktop/src/stores/savedSqlStore.ts b/apps/desktop/src/stores/savedSqlStore.ts index 07e97cd039..e32badefd9 100644 --- a/apps/desktop/src/stores/savedSqlStore.ts +++ b/apps/desktop/src/stores/savedSqlStore.ts @@ -196,6 +196,7 @@ export const useSavedSqlStore = defineStore("savedSql", () => { database: input.database, schema: input.schema, sql: input.sql, + connectionId: input.connectionId, updatedAt: timestamp, } : {