Skip to content

fix(coordinator): keep open tabs when switching schemas#1677

Merged
datlechin merged 1 commit into
mainfrom
fix/schema-switch-preserves-tabs
Jun 13, 2026
Merged

fix(coordinator): keep open tabs when switching schemas#1677
datlechin merged 1 commit into
mainfrom
fix/schema-switch-preserves-tabs

Conversation

@datlechin

Copy link
Copy Markdown
Member

Problem

Fixes #1669. On a PostgreSQL connection with multiple schemas, switching the active schema via the bottom-left schema selector closed every open tab and discarded unsaved SQL, with no way to recover it.

Root cause

MainContentCoordinator.switchSchema(to:) was a near-verbatim copy of switchDatabase(to:). After the driver call it ran tabManager.tabs = [] (plus clearFilterState(), closeSiblingNativeWindows(), a pre-wipe saveNowSync, and tabSessionRegistry.removeAll()). That wipe is correct for a database switch (tables in DB A don't exist in DB B) but wrong for a schema switch, which only changes the session's search_path. Open tabs stay valid: query tabs hold user SQL, table tabs hold fully schema-qualified queries.

Fix

  • switchSchema now only issues the driver call and updates the schema chip. The tab wipe, filter clear, sibling-window close, pre-wipe save, and registry clear are removed. switchDatabase is unchanged.
  • The currentSchemaChanged subscriber now syncs toolbarState.currentSchema from the session before refreshing, so every window of the connection keeps a consistent schema chip, sidebar, and autocomplete now that sibling windows are no longer force-closed. The sidebar picker already reads the session directly.

This matches the established native model (DataGrip: a schema/search_path selector is non-destructive to editor documents; TablePlus filed the tab-closing variant as a bug).

Tests

  • New SwitchSchemaTests: query + table tabs and their SQL survive a schema switch, selectedTabId is kept, and the driver/chip reflect the new schema.
  • MockDatabaseDriver now conforms to SchemaSwitchable (additive) so the success path is reachable in tests.

swiftlint lint --strict passes clean on all changed files.

Notes

  • No docs change (bugfix to existing behavior, not a new shortcut/setting/feature).

@datlechin datlechin merged commit 3aa3ba8 into main Jun 13, 2026
3 of 4 checks passed
@datlechin datlechin deleted the fix/schema-switch-preserves-tabs branch June 13, 2026 12:18

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 40b7306069

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

toolbarState.currentSchema = schema

do {
try await DatabaseManager.shared.switchSchema(to: schema, for: connectionId)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Qualify table tabs before switching schemas

When a table tab was opened from the current schema, many schema-switching drivers leave tableContext.schemaName nil (for example PostgreSQL fetchTables returns PluginTableInfo(name:type:), and PluginDriverAdapter.fetchTables() maps current-schema tables with no fallback schema). Those tabs therefore store unqualified browse/edit SQL such as SELECT ... FROM "users"; after this call changes the connection search path while keeping the tab, refreshing, filtering, or saving edits in that preserved tab can target users in the newly selected schema instead of the table the tab originally displayed whenever schemas share table names. Please stamp existing table tabs with the previous schema (or otherwise qualify/close them) before switching.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query tabs are lost when switching schemas

1 participant