Skip to content

A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.

C++

fix(postgres): own split sidebar reload tokens (#80)

Tobi5e6b8ca9c314
diff --git a/macos/App/State/AppState.swift b/macos/App/State/AppState.swift
index da009b7..252df45 100644
--- a/macos/App/State/AppState.swift
+++ b/macos/App/State/AppState.swift
@@ -103,6 +103,10 @@ final class AppState: ObservableObject {
var schema: String?
}
+ struct SidebarReloadHandle: Hashable, Sendable {
+ fileprivate let generation: Int
+ }
+
// MARK: - Tab State
@Published var tabs: [ContentTab] = []
@@ -373,12 +377,14 @@ final class AppState: ObservableObject {
config: ConnectionConfig,
adapter: any DatabaseAdapter,
preferredSchema: String? = nil
- ) async {
- _ = await prepareSidebarReload(
+ ) async -> SidebarReloadHandle? {
+ guard let reload = await prepareSidebarReload(
config: config,
adapter: adapter,
preferredSchema: preferredSchema
- )
+ ) else { return nil }
+ pendingSidebarReload = reload
+ return SidebarReloadHandle(generation: reload.generation)
}
func loadSidebar(
@@ -386,21 +392,34 @@ final class AppState: ObservableObject {
adapter: any DatabaseAdapter,
schema: String? = nil
) async {
- let connection = sidebarConnectionContext(config: config, adapter: adapter)
- if let pendingSidebarReload,
- pendingSidebarReload.connection == connection,
- isCurrentSidebarReload(pendingSidebarReload) {
- await loadSidebarItems(using: pendingSidebarReload, adapter: adapter)
- return
- }
-
- guard var reload = beginSidebarReload(
+ await reloadSidebar(
config: config,
adapter: adapter,
preferredSchema: schema
- ) else { return }
- reload.schemas = sidebarSchemas
- reload.schema = schema
+ )
+ }
+
+ func loadSidebar(
+ config: ConnectionConfig,
+ adapter: any DatabaseAdapter,
+ schema: String? = nil,
+ using handle: SidebarReloadHandle?
+ ) async {
+ let connection = sidebarConnectionContext(config: config, adapter: adapter)
+ guard let handle,
+ var reload = pendingSidebarReload,
+ reload.generation == handle.generation,
+ reload.connection == connection,
+ isCurrentSidebarReload(reload) else { return }
+
+ // Consume the exact prepared operation before any item query suspends.
+ // A duplicate or superseded caller can no longer reuse this token.
+ pendingSidebarReload = nil
+ if let schema,
+ config.databaseType == .postgresql,
+ reload.schemas.contains(schema) {
+ reload.schema = schema
+ }
await loadSidebarItems(using: reload, adapter: adapter)
}
@@ -443,12 +462,10 @@ final class AppState: ObservableObject {
for: config.databaseType,
schemas: schemas
)
- pendingSidebarReload = reload
return reload
} catch {
guard isCurrentSidebarReload(reload) else { return nil }
publishSidebarSnapshot(reload.previous, using: reload)
- pendingSidebarReload = nil
print("Sidebar schema load error: \(error)")
return nil
}
@@ -542,15 +559,9 @@ final class AppState: ObservableObject {
),
using: reload
)
- if pendingSidebarReload?.generation == reload.generation {
- pendingSidebarReload = nil
- }
} catch {
guard isCurrentSidebarReload(reload) else { return }
publishSidebarSnapshot(reload.previous, using: reload)
- if pendingSidebarReload?.generation == reload.generation {
- pendingSidebarReload = nil
- }
print("Sidebar load error: \(error)")
}
}
diff --git a/macos/Tests/GridexTests/PostgreSQLSchemaActionRegressionTests.swift b/macos/Tests/GridexTests/PostgreSQLSchemaActionRegressionTests.swift
index a965c5f..5bcf64b 100644
--- a/macos/Tests/GridexTests/PostgreSQLSchemaActionRegressionTests.swift
+++ b/macos/Tests/GridexTests/PostgreSQLSchemaActionRegressionTests.swift
@@ -55,11 +55,11 @@ final class SidebarLoadPublicationTests: XCTestCase {
appState.activeConfig = staleConfig
appState.activeAdapter = staleAdapter
let staleLoad = Task {
- await appState.loadSidebarSchemas(config: staleConfig, adapter: staleAdapter)
+ let reload = await appState.loadSidebarSchemas(config: staleConfig, adapter: staleAdapter)
await appState.loadSidebar(
config: staleConfig,
adapter: staleAdapter,
- schema: appState.selectedSidebarSchema
+ using: reload
)
}
await staleSchemaGate.waitUntilBlocked()
@@ -67,11 +67,11 @@ final class SidebarLoadPublicationTests: XCTestCase {
appState.activeConnectionId = activeConfig.id
appState.activeConfig = activeConfig
appState.activeAdapter = activeAdapter
- await appState.loadSidebarSchemas(config: activeConfig, adapter: activeAdapter)
+ let activeReload = await appState.loadSidebarSchemas(config: activeConfig, adapter: activeAdapter)
await appState.loadSidebar(
config: activeConfig,
adapter: activeAdapter,
- schema: appState.selectedSidebarSchema
+ using: activeReload
)
await staleSchemaGate.release()
@@ -96,11 +96,11 @@ final class SidebarLoadPublicationTests: XCTestCase {
appState.activeConfig = config
appState.activeAdapter = adapter
let staleLoad = Task {
- await appState.loadSidebarSchemas(config: config, adapter: adapter)
+ let reload = await appState.loadSidebarSchemas(config: config, adapter: adapter)
await appState.loadSidebar(
config: config,
adapter: adapter,
- schema: appState.selectedSidebarSchema
+ using: reload
)
}
await schemaGate.waitUntilBlocked()
@@ -126,21 +126,21 @@ final class SidebarLoadPublicationTests: XCTestCase {
appState.activeConfig = config
appState.activeAdapter = adapter
- await appState.loadSidebarSchemas(config: config, adapter: adapter)
+ let initialReload = await appState.loadSidebarSchemas(config: config, adapter: adapter)
await appState.loadSidebar(
config: config,
adapter: adapter,
- schema: appState.selectedSidebarSchema
+ using: initialReload
)
XCTAssertEqual(appState.selectedSidebarSchema, "tenant_a")
XCTAssertEqual(visibleTableNames(in: appState), ["orders_tenant_a"])
appState.selectedSidebarSchema = "tenant_b"
- await appState.loadSidebarSchemas(config: config, adapter: adapter)
+ let failingReload = await appState.loadSidebarSchemas(config: config, adapter: adapter)
await appState.loadSidebar(
config: config,
adapter: adapter,
- schema: appState.selectedSidebarSchema
+ using: failingReload
)
XCTAssertTrue(
@@ -158,7 +158,7 @@ final class SidebarLoadPublicationTests: XCTestCase {
appState.activeConfig = config
appState.activeAdapter = adapter
- await appState.loadSidebarSchemas(
+ let reload = await appState.loadSidebarSchemas(
config: config,
adapter: adapter,
preferredSchema: "tenant_a"
@@ -166,7 +166,8 @@ final class SidebarLoadPublicationTests: XCTestCase {
await appState.loadSidebar(
config: config,
adapter: adapter,
- schema: "tenant_b"
+ schema: "tenant_b",
+ using: reload
)
XCTAssertEqual(appState.selectedSidebarSchema, "tenant_b")
@@ -183,7 +184,7 @@ final class SidebarLoadPublicationTests: XCTestCase {
appState.activeAdapter = adapter
let olderReload = Task {
- await appState.loadSidebarSchemas(
+ let reload = await appState.loadSidebarSchemas(
config: config,
adapter: adapter,
preferredSchema: "tenant_a"
@@ -192,12 +193,13 @@ final class SidebarLoadPublicationTests: XCTestCase {
await appState.loadSidebar(
config: config,
adapter: adapter,
- schema: "tenant_a"
+ schema: "tenant_a",
+ using: reload
)
}
await olderItemsGate.waitUntilBlocked()
- await appState.loadSidebarSchemas(
+ let newerReload = await appState.loadSidebarSchemas(
config: config,
adapter: adapter,
preferredSchema: "tenant_b"
@@ -205,7 +207,8 @@ final class SidebarLoadPublicationTests: XCTestCase {
await appState.loadSidebar(
config: config,
adapter: adapter,
- schema: "tenant_b"
+ schema: "tenant_b",
+ using: newerReload
)
XCTAssertEqual(appState.selectedSidebarSchema, "tenant_b")
XCTAssertEqual(visibleTableNames(in: appState), ["orders_tenant_b"])