Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 38 additions & 41 deletions TablePro/Core/Database/DatabaseManager+Health.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,39 +147,57 @@ extension DatabaseManager {
throw error
}

await applyTimeoutAndStartupCommands(
on: driver,
startupCommands: session.connection.startupCommands,
connectionName: session.connection.name
)
await restoreSchemaAndDatabase(
on: driver,
savedSchema: session.currentSchema,
savedDatabase: session.currentDatabase
)

return ReconnectResult(driver: driver, effectiveConnection: connectionForDriver)
}

func applyTimeoutAndStartupCommands(
on driver: DatabaseDriver,
startupCommands: String?,
connectionName: String
) async {
let timeoutSeconds = AppSettingsManager.shared.general.queryTimeoutSeconds
do {
try await driver.applyQueryTimeout(timeoutSeconds)
} catch {
Self.logger.warning(
"Query timeout not supported for \(session.connection.name): \(error.localizedDescription)"
"Query timeout not supported for \(connectionName): \(error.localizedDescription)"
)
}

await executeStartupCommands(
session.connection.startupCommands, on: driver, connectionName: session.connection.name
)
await executeStartupCommands(startupCommands, on: driver, connectionName: connectionName)
}

if let savedSchema = session.currentSchema,
let schemaDriver = driver as? SchemaSwitchable {
func restoreSchemaAndDatabase(
on driver: DatabaseDriver,
savedSchema: String?,
savedDatabase: String?
) async {
if let savedSchema, let schemaDriver = driver as? SchemaSwitchable {
do {
try await schemaDriver.switchSchema(to: savedSchema)
} catch {
Self.logger.warning("Failed to restore schema '\(savedSchema)' on reconnect: \(error.localizedDescription)")
}
}

// Restore database for MSSQL if session had a non-default database
if let savedDatabase = session.currentDatabase,
let adapter = driver as? PluginDriverAdapter {
if let savedDatabase, let adapter = driver as? PluginDriverAdapter {
do {
try await adapter.switchDatabase(to: savedDatabase)
} catch {
Self.logger.warning("Failed to restore database '\(savedDatabase)' on reconnect: \(error.localizedDescription)")
}
}

return ReconnectResult(driver: driver, effectiveConnection: connectionForDriver)
}

/// Stop health monitoring for a connection
Expand Down Expand Up @@ -243,37 +261,16 @@ extension DatabaseManager {
)
try await driver.connect()

let timeoutSeconds = AppSettingsManager.shared.general.queryTimeoutSeconds
do {
try await driver.applyQueryTimeout(timeoutSeconds)
} catch {
Self.logger.warning(
"Query timeout not supported for \(session.connection.name): \(error.localizedDescription)"
)
}

await executeStartupCommands(
session.connection.startupCommands, on: driver, connectionName: session.connection.name
await applyTimeoutAndStartupCommands(
on: driver,
startupCommands: session.connection.startupCommands,
connectionName: session.connection.name
)
await restoreSchemaAndDatabase(
on: driver,
savedSchema: activeSessions[sessionId]?.currentSchema,
savedDatabase: activeSessions[sessionId]?.currentDatabase
)

if let savedSchema = activeSessions[sessionId]?.currentSchema,
let schemaDriver = driver as? SchemaSwitchable {
do {
try await schemaDriver.switchSchema(to: savedSchema)
} catch {
Self.logger.warning("Failed to restore schema '\(savedSchema)' on reconnect: \(error.localizedDescription)")
}
}

// Restore database for MSSQL if session had a non-default database
if let savedDatabase = activeSessions[sessionId]?.currentDatabase,
let adapter = driver as? PluginDriverAdapter {
do {
try await adapter.switchDatabase(to: savedDatabase)
} catch {
Self.logger.warning("Failed to restore database '\(savedDatabase)' on reconnect: \(error.localizedDescription)")
}
}

// Update session
updateSession(sessionId) { session in
Expand Down
15 changes: 4 additions & 11 deletions TablePro/Core/Database/DatabaseManager+Sessions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,10 @@ extension DatabaseManager {
try await driver.connect()
try Task.checkCancellation()

let timeoutSeconds = AppSettingsManager.shared.general.queryTimeoutSeconds
do {
try await driver.applyQueryTimeout(timeoutSeconds)
} catch {
Self.logger.warning(
"Query timeout not supported for \(connection.name): \(error.localizedDescription)"
)
}

await executeStartupCommands(
resolvedConnection.startupCommands, on: driver, connectionName: connection.name
await applyTimeoutAndStartupCommands(
on: driver,
startupCommands: resolvedConnection.startupCommands,
connectionName: connection.name
)

if let schemaDriver = driver as? SchemaSwitchable {
Expand Down
Loading
Loading