-
-
Notifications
You must be signed in to change notification settings - Fork 291
fix(coordinator): keep open tabs when switching schemas #1677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // | ||
| // SwitchSchemaTests.swift | ||
| // TableProTests | ||
| // | ||
| // Tests for the "switch schema" flow: switching the active schema changes | ||
| // the connection's search_path but must not close or clear open tabs. | ||
| // Regression coverage for #1669: switching schemas wiped every tab, | ||
| // discarding unsaved SQL in query editor tabs. | ||
| // | ||
|
|
||
| import Foundation | ||
| import Testing | ||
|
|
||
| @testable import TablePro | ||
|
|
||
| @Suite("SwitchSchema") | ||
| @MainActor | ||
| struct SwitchSchemaTests { | ||
| private func makeTab(title: String, query: String, tabType: TabType, tableName: String? = nil) -> QueryTab { | ||
| QueryTab(title: title, query: query, tabType: tabType, tableName: tableName) | ||
| } | ||
|
|
||
| private func withSchemaSwitchingConnection( | ||
| _ body: (DatabaseConnection, MockDatabaseDriver) -> Void | ||
| ) { | ||
| let connection = TestFixtures.makeConnection(type: .postgresql) | ||
| let driver = MockDatabaseDriver(connection: connection) | ||
| DatabaseManager.shared.injectSession( | ||
| ConnectionSession(connection: connection, driver: driver), | ||
| for: connection.id | ||
| ) | ||
| defer { DatabaseManager.shared.removeSession(for: connection.id) } | ||
| body(connection, driver) | ||
| } | ||
|
|
||
| @Test("switchSchema keeps query and table tabs and their contents") | ||
| func switchSchemaPreservesTabs() async { | ||
| await withSchemaSwitchingConnection { connection, driver in | ||
|
Check failure on line 38 in TableProTests/Views/SwitchSchemaTests.swift
|
||
| let tabManager = QueryTabManager() | ||
| let coordinator = MainContentCoordinator( | ||
| connection: connection, | ||
| tabManager: tabManager, | ||
| changeManager: DataChangeManager(), | ||
| toolbarState: ConnectionToolbarState() | ||
| ) | ||
| defer { coordinator.teardown() } | ||
|
|
||
| let queryTab = makeTab(title: "Query 1", query: "SELECT 42", tabType: .query) | ||
| let tableTab = makeTab(title: "users", query: "SELECT * FROM users", tabType: .table, tableName: "users") | ||
| tabManager.tabs = [queryTab, tableTab] | ||
| tabManager.selectedTabId = queryTab.id | ||
| let idsBefore = tabManager.tabs.map(\.id) | ||
|
|
||
| await coordinator.switchSchema(to: "s2") | ||
|
|
||
| #expect(tabManager.tabs.map(\.id) == idsBefore) | ||
| #expect(tabManager.selectedTabId == queryTab.id) | ||
| #expect(tabManager.tabs.contains { $0.content.query == "SELECT 42" }) | ||
| #expect(driver.currentSchema == "s2") | ||
| #expect(coordinator.toolbarState.currentSchema == "s2") | ||
| } | ||
| } | ||
|
|
||
| @Test("switchSchema leaves tabs untouched when the schema is unchanged") | ||
| func switchSchemaToSameSchemaKeepsTabs() async { | ||
| await withSchemaSwitchingConnection { connection, _ in | ||
|
Check failure on line 66 in TableProTests/Views/SwitchSchemaTests.swift
|
||
| let tabManager = QueryTabManager() | ||
| let coordinator = MainContentCoordinator( | ||
| connection: connection, | ||
| tabManager: tabManager, | ||
| changeManager: DataChangeManager(), | ||
| toolbarState: ConnectionToolbarState() | ||
| ) | ||
| defer { coordinator.teardown() } | ||
|
|
||
| let queryTab = makeTab(title: "Query 1", query: "SELECT 1", tabType: .query) | ||
| tabManager.tabs = [queryTab] | ||
| tabManager.selectedTabId = queryTab.id | ||
|
|
||
| await coordinator.switchSchema(to: "s1") | ||
| await coordinator.switchSchema(to: "s1") | ||
|
|
||
| #expect(tabManager.tabs.count == 1) | ||
| #expect(tabManager.tabs.first?.content.query == "SELECT 1") | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a table tab was opened from the current schema, many schema-switching drivers leave
tableContext.schemaNamenil (for example PostgreSQLfetchTablesreturnsPluginTableInfo(name:type:), andPluginDriverAdapter.fetchTables()maps current-schema tables with no fallback schema). Those tabs therefore store unqualified browse/edit SQL such asSELECT ... FROM "users"; after this call changes the connection search path while keeping the tab, refreshing, filtering, or saving edits in that preserved tab can targetusersin 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 👍 / 👎.