Skip to content

Commit 13ed249

Browse files
committed
Retry On Database Failure
1 parent cc3a125 commit 13ed249

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

CodeEdit/Features/Editor/Models/Restoration/EditorStateRestoration.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ final class EditorStateRestoration {
6969
.appending(path: "Library/Application Support/CodeEdit", directoryHint: .isDirectory)
7070
.appending(path: "editor-restoration.db", directoryHint: .notDirectory)
7171

72-
do {
73-
self.databaseQueue = try DatabaseQueue(path: databaseURL.absolutePath, configuration: .init())
72+
self.databaseQueue = try DatabaseQueue(path: databaseURL.absolutePath, configuration: .init())
73+
try attemptMigration(retry: true)
74+
}
7475

76+
private func attemptMigration(retry: Bool) throws {
77+
do {
7578
var migrator = DatabaseMigrator()
7679

7780
migrator.registerMigration("Version 0") {
@@ -83,8 +86,15 @@ final class EditorStateRestoration {
8386

8487
try migrator.migrate(databaseQueue)
8588
} catch {
86-
// Try to delete the database on failure, might fix a corruption or version error.
87-
try? FileManager.default.removeItem(at: databaseURL)
89+
if retry {
90+
// Try to delete the database on failure, might fix a corruption or version error.
91+
try? FileManager.default.removeItem(at: databaseURL)
92+
// This will recreate the db file if necessary
93+
self.databaseQueue = try DatabaseQueue(path: databaseURL.absolutePath, configuration: .init())
94+
try attemptMigration(retry: false)
95+
96+
return // Ignore the original error if we're retrying
97+
}
8898
Self.logger.error("Failed to start database connection: \(error)")
8999
throw error
90100
}

0 commit comments

Comments
 (0)