Skip to content

Commit 546d1bd

Browse files
[FCM] Narrower database open recovery logic (#15678)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 2314dd7 commit 546d1bd

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

FirebaseMessaging/Sources/FIRMessagingRmqManager.m

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -530,31 +530,46 @@ - (void)openDatabase {
530530
// If opening the database failed, it might be corrupt. Try to recover by deleting and
531531
// recreating it.
532532
if (result != SQLITE_OK) {
533-
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase,
534-
@"Could not open RMQ database at path: %@. "
535-
"Will delete and try to recreate it.",
536-
path);
537-
NSError *removeError;
538-
if (![[NSFileManager defaultManager] removeItemAtPath:path error:&removeError]) {
539-
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase,
540-
@"Failed to delete corrupt database at %@: %@", path, removeError);
541-
}
542-
// After deleting, try to open it again.
543-
result = sqlite3_open_v2([path UTF8String], &self->_database, flags, NULL);
544-
// If it still fails after the recovery attempt, then assert and crash.
545-
if (result != SQLITE_OK) {
533+
if (result == SQLITE_CANTOPEN) {
534+
FIRMessagingLoggerWarn(
535+
kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase,
536+
@"Could not open RMQ database at path: %@. Will delete and try to recreate it.",
537+
path);
538+
NSError *removeError;
539+
if (![[NSFileManager defaultManager] removeItemAtPath:path error:&removeError]) {
540+
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase,
541+
@"Failed to delete database for recovery at %@: %@", path,
542+
removeError);
543+
}
544+
// After deleting, try to open it again.
545+
result = sqlite3_open_v2([path UTF8String], &self->_database, flags, NULL);
546+
// If it still fails after the recovery attempt, then assert and crash.
547+
if (result != SQLITE_OK) {
548+
NSString *errorString = FIRMessagingStringFromSQLiteResult(result);
549+
NSString *errorMessage = [NSString
550+
stringWithFormat:@"Could not open or create RMQ database at path %@, error: %@",
551+
path, errorString];
552+
FIRMessagingLoggerError(kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase,
553+
@"%@", errorMessage);
554+
NSAssert(NO, errorMessage);
555+
didOpenDatabase = NO; // Still failed, so indicate database did not open.
556+
} else {
557+
// Successfully recreated after an open failure, so treat as a new database for table
558+
// creation.
559+
didOpenDatabase = YES; // Indicate successful opening after recreation.
560+
[self createTable];
561+
}
562+
} else {
546563
NSString *errorString = FIRMessagingStringFromSQLiteResult(result);
547-
NSString *errorMessage = [NSString
548-
stringWithFormat:@"Could not open or create RMQ database at path %@, error: %@", path,
549-
errorString];
564+
NSString *errorMessage =
565+
[NSString stringWithFormat:
566+
@"Could not open RMQ database at path %@, error: %@. Won't delete the "
567+
@"database as it is not a corrupt database error.",
568+
path, errorString];
550569
FIRMessagingLoggerError(kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase,
551570
@"%@", errorMessage);
552571
NSAssert(NO, errorMessage);
553572
didOpenDatabase = NO; // Still failed, so indicate database did not open.
554-
} else {
555-
// Successfully recreated after corruption, so treat as a new database for table creation.
556-
didOpenDatabase = YES; // Indicate successful opening after recreation.
557-
[self createTable];
558573
}
559574
} else {
560575
[self updateDBWithStringRmqID];

0 commit comments

Comments
 (0)