Skip to content

fix: clear stale filters and tables on database/schema switch#240

Merged
datlechin merged 2 commits intomainfrom
fix/stale-filter-on-restore
Mar 9, 2026
Merged

fix: clear stale filters and tables on database/schema switch#240
datlechin merged 2 commits intomainfrom
fix/stale-filter-on-restore

Conversation

@datlechin
Copy link
Owner

Summary

  • Rebuild clean base queries for table tabs on restore, stripping stale filter WHERE clauses that may reference columns from a different schema
  • Clear filterStateManager when switching databases/schemas to prevent stale filters from being applied
  • Invalidate schema provider cache in loadSchema() so sidebar fetcher doesn't return stale cached tables
  • Hoist UI clearing (tabs, tables, toolbar) to top of switchDatabase/switchSchema with Task.yield() so sidebar immediately shows loading state instead of stale tables
  • Extract QueryTab.buildBaseTableQuery() to share query-building logic between addTableTab and tab restore

Closes #237

Test plan

  • Connect to PostgreSQL with multiple schemas
  • Apply a filter on a table, switch schema, verify no error on restore
  • Close and reopen app after switching schema with active filter — verify no repeated error
  • Switch database/schema and verify sidebar immediately clears (shows loading, not old tables)
  • Verify tables load correctly after switch completes
  • Test with MySQL, MSSQL, MongoDB, Redis database switching

@datlechin datlechin merged commit 0b1cd62 into main Mar 9, 2026
2 checks passed
@datlechin datlechin deleted the fix/stale-filter-on-restore branch March 9, 2026 16:34
Copy link

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

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: 9b1befd7ff

ℹ️ 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".

Comment on lines +311 to +314
toolbarState.databaseName = database
closeSiblingNativeWindows()
tabManager.tabs = []
tabManager.selectedTabId = nil

Choose a reason for hiding this comment

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

P1 Badge Defer tab/session reset until switch succeeds

switchDatabase now clears UI/session state (toolbarState.databaseName, open tabs, sibling windows, and filter state) before any of the throwing switch operations run. If USE, switchDatabase, switchSchema, or reconnectSession fails, the catch path only shows an error and does not restore prior state, so users can lose their open tabs/windows and see the toolbar pointing at a database that was never actually switched to; this is a regression in failure scenarios and the reset should be moved after success (the same failure pattern is present in switchSchema).

Useful? React with 👍 / 👎.


func loadSchema() async {
guard let driver = DatabaseManager.shared.driver(for: connectionId) else { return }
await schemaProvider.invalidateCache()

Choose a reason for hiding this comment

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

P2 Badge Remove unconditional cache invalidation from loadSchema

Calling invalidateCache() at the start of every loadSchema() introduces a race when schema loads are triggered close together (for example, PostgreSQL switch calls loadSchema() and reconnectSession also emits .databaseDidConnect, whose handler calls coordinator.loadSchema()). If one load is in flight, a second call can clear cachedDriver via invalidateCache() and then return early because isLoading is true, leaving schema tables loaded but column fetches/autocomplete broken until another reload.

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.

Error while applying last session filter

1 participant