Skip to content

fix: keep active database across new tabs after Cmd+K switch#1049

Merged
datlechin merged 1 commit intomainfrom
fix/active-database-on-tab-open-1043
May 6, 2026
Merged

fix: keep active database across new tabs after Cmd+K switch#1049
datlechin merged 1 commit intomainfrom
fix/active-database-on-tab-open-1043

Conversation

@datlechin
Copy link
Copy Markdown
Member

Summary

  • Cmd+T (and several other tab-creation paths) reset the active database back to the connection's saved default after the user had switched to a different database via Cmd+K. The visible symptom was a "flash, then revert" — the new window briefly rendered the right database before snapping back. Fixes New query tab resets active database to connection default #1043.
  • Same root cause caused query history to record the wrong database, the column-types cache to collide on same-named tables across databases after a switch, and AI / autocomplete schema context to describe the wrong database to the model.
  • The codebase had no canonical accessor for "active database for this connection," so every call site re-derived it inline against connection.database (the saved default) and most got it wrong. Two new helpers — DatabaseManager.activeDatabaseName(for:) for service-layer code and MainContentCoordinator.activeDatabaseName for coordinator extensions — replace every inline lookup with a single source of truth.

Trace

  1. MainContentCommandActions.newTab() built an EditorTabPayload with no databaseName.
  2. SessionStateFactory.create() fell back to connection.database (saved default) when the payload had no databaseName, so the new tab's tableContext.databaseName recorded the wrong database.
  3. MainContentCoordinator.handleTabChange saw the mismatch between that recorded value and session.activeDatabase, fired switchDatabase, and dragged the shared session back to the saved default.

Files changed

  • New helpers: DatabaseManager.swift (+activeDatabaseName(for:)), MainContentCoordinator.swift (+activeDatabaseName computed property).
  • Tab creation (Class 1): MainContentCommandActions.swift, SessionStateFactory.swift, MainContentCoordinator.swift, and 5 coordinator extensions (+SidebarActions, +Favorites, +ServerDashboard, +Navigation).
  • Query history (Class 2): 4 coordinator extensions (+QueryHelpers, +MultiStatement, +SaveChanges, +QueryParameters), ImportService.swift, CreateTableView.swift, TableStructureView.swift, DatabaseManager+Schema.swift.
  • Schema cache key (Class 3): MainContentCoordinator+QueryHelpers.swift (write + read sites in lockstep).
  • AI / autocomplete (Class 4): AIChatViewModel.swift, SQLSchemaProvider.swift.
  • Cleanup (existing inline pattern → helper, no behavior change): +ERDiagram, +Terminal, +TabSwitch, +FKNavigation, TerminalTabContentView.swift.

Test plan

  • Cmd+K to a non-default database, then Cmd+T → new window opens on the switched database without flashing back.
  • Cmd+K to a non-default database, run a query, check query history → row records the switched database.
  • Cmd+K to a non-default database, then "Create Table" / "Create View" / "Server Dashboard" / "Open in New Tab" from a favorite → new tab targets the switched database.
  • Two databases with same-named tables: query in db1, switch to db2, query the same table name → second query shows db2 columns (Class 3 cache key check).
  • AI chat with a switched database → prompt context references the switched database.
  • Open Terminal tab after Cmd+K → terminal launches against the switched database.
  • No regression on first connect (no Cmd+K): all flows still use the connection's configured default.

@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@datlechin datlechin merged commit ac157ff into main May 6, 2026
2 checks passed
@datlechin datlechin deleted the fix/active-database-on-tab-open-1043 branch May 6, 2026 14:03
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.

New query tab resets active database to connection default

1 participant