@@ -208,44 +208,9 @@ final class DatabaseManager {
208208 }
209209
210210 // Run post-connect actions declared by the plugin
211- let postConnectActions = PluginMetadataRegistry . shared. snapshot (
212- forTypeId: connection. type. pluginTypeId
213- ) ? . postConnectActions ?? [ ]
214-
215- for action in postConnectActions {
216- switch action {
217- case . selectDatabaseFromLastSession:
218- // Restore saved database (e.g. MSSQL) only when no explicit database is configured
219- if resolvedConnection. database. trimmingCharacters ( in: . whitespacesAndNewlines) . isEmpty,
220- let adapter = driver as? PluginDriverAdapter ,
221- let savedDb = AppSettingsStorage . shared. loadLastDatabase ( for: connection. id) {
222- do {
223- try await adapter. switchDatabase ( to: savedDb)
224- activeSessions [ connection. id] ? . currentDatabase = savedDb
225- } catch {
226- Self . logger. warning ( " Failed to restore saved database ' \( savedDb, privacy: . public) ' for \( connection. id) : \( error. localizedDescription, privacy: . public) " )
227- }
228- }
229- case . selectDatabaseFromConnectionField( let fieldId) :
230- // Select database from a connection field (e.g. Redis database index).
231- // Check additionalFields first, then legacy dedicated properties, then
232- // fall back to parsing the main database field.
233- let initialDb : Int
234- if let fieldValue = resolvedConnection. additionalFields [ fieldId] , let parsed = Int ( fieldValue) {
235- initialDb = parsed
236- } else if fieldId == " redisDatabase " , let legacy = resolvedConnection. redisDatabase {
237- initialDb = legacy
238- } else if let fallback = Int ( resolvedConnection. database) {
239- initialDb = fallback
240- } else {
241- initialDb = 0
242- }
243- if initialDb != 0 {
244- try ? await ( driver as? PluginDriverAdapter ) ? . switchDatabase ( to: String ( initialDb) )
245- }
246- activeSessions [ connection. id] ? . currentDatabase = String ( initialDb)
247- }
248- }
211+ await executePostConnectActions (
212+ for: connection, resolvedConnection: resolvedConnection, driver: driver
213+ )
249214
250215 // Batch all session mutations into a single write to fire objectWillChange once
251216 if var session = activeSessions [ connection. id] {
@@ -301,6 +266,47 @@ final class DatabaseManager {
301266 }
302267 }
303268
269+ private func executePostConnectActions(
270+ for connection: DatabaseConnection ,
271+ resolvedConnection: DatabaseConnection ,
272+ driver: DatabaseDriver
273+ ) async {
274+ let postConnectActions = PluginMetadataRegistry . shared. snapshot (
275+ forTypeId: connection. type. pluginTypeId
276+ ) ? . postConnectActions ?? [ ]
277+
278+ for action in postConnectActions {
279+ switch action {
280+ case . selectDatabaseFromLastSession:
281+ if resolvedConnection. database. trimmingCharacters ( in: . whitespacesAndNewlines) . isEmpty,
282+ let adapter = driver as? PluginDriverAdapter ,
283+ let savedDb = AppSettingsStorage . shared. loadLastDatabase ( for: connection. id) {
284+ do {
285+ try await adapter. switchDatabase ( to: savedDb)
286+ activeSessions [ connection. id] ? . currentDatabase = savedDb
287+ } catch {
288+ Self . logger. warning ( " Failed to restore saved database ' \( savedDb, privacy: . public) ' for \( connection. id) : \( error. localizedDescription, privacy: . public) " )
289+ }
290+ }
291+ case . selectDatabaseFromConnectionField( let fieldId) :
292+ let initialDb : Int
293+ if let fieldValue = resolvedConnection. additionalFields [ fieldId] , let parsed = Int ( fieldValue) {
294+ initialDb = parsed
295+ } else if fieldId == " redisDatabase " , let legacy = resolvedConnection. redisDatabase {
296+ initialDb = legacy
297+ } else if let fallback = Int ( resolvedConnection. database) {
298+ initialDb = fallback
299+ } else {
300+ initialDb = 0
301+ }
302+ if initialDb != 0 {
303+ try ? await ( driver as? PluginDriverAdapter ) ? . switchDatabase ( to: String ( initialDb) )
304+ }
305+ activeSessions [ connection. id] ? . currentDatabase = String ( initialDb)
306+ }
307+ }
308+ }
309+
304310 /// Switch to an existing session
305311 func switchToSession( _ sessionId: UUID ) {
306312 guard activeSessions [ sessionId] != nil else { return }
0 commit comments