102
102
// table data handlers
103
103
typedef void (^FCMOutgoingRmqMessagesTableHandler)(int64_t rmqId, int8_t tag, NSData *data);
104
104
105
+ // Utility to create an NSString from a sqlite3 result code
106
+ NSString * _Nonnull FIRMessagingStringFromSQLiteResult (int result) {
107
+ const char *errorStr = sqlite3_errstr (result);
108
+ // NSString *errorString = [NSString stringWithCString:errorStr encoding:NSUTF8StringEncoding];
109
+ NSString *errorString = [NSString stringWithFormat: @" %d - %s " , result, errorStr];
110
+ return errorString;
111
+ }
112
+
105
113
@interface FIRMessagingRmq2PersistentStore () {
106
114
sqlite3 *_database;
107
115
}
@@ -187,6 +195,7 @@ + (NSString *)pathForDatabase:(NSString *)dbName inDirectory:(FIRMessagingRmqDir
187
195
NSArray *paths;
188
196
NSArray *components;
189
197
NSString *dbNameWithExtension = [NSString stringWithFormat: @" %@ .sqlite" , dbName];
198
+ NSString *errorMessage;
190
199
191
200
switch (directory) {
192
201
case FIRMessagingRmqDirectoryDocuments:
@@ -206,7 +215,11 @@ + (NSString *)pathForDatabase:(NSString *)dbName inDirectory:(FIRMessagingRmqDir
206
215
break ;
207
216
208
217
default :
209
- FIRMessaging_FAIL (@" Invalid directory type %zd " , directory);
218
+ errorMessage = [NSString stringWithFormat: @" Invalid directory type %zd " , directory];
219
+ FIRMessagingLoggerError (kFIRMessagingMessageCodeRmq2PersistentStoreInvalidRmqDirectory ,
220
+ @" %@ " ,
221
+ errorMessage);
222
+ NSAssert (NO , errorMessage);
210
223
break ;
211
224
}
212
225
@@ -219,9 +232,13 @@ - (void)createTableWithName:(NSString *)tableName command:(NSString *)command {
219
232
if (sqlite3_exec (_database, [createDatabase UTF8String ], NULL , NULL , &error) != SQLITE_OK) {
220
233
// remove db before failing
221
234
[self removeDatabase ];
222
- FIRMessaging_FAIL (@" Couldn't create table: %@ %@ " ,
223
- kCreateTableOutgoingRmqMessages ,
224
- [NSString stringWithCString: error encoding: NSUTF8StringEncoding]);
235
+ NSString *errorMessage = [NSString stringWithFormat: @" Couldn't create table: %@ %@ " ,
236
+ kCreateTableOutgoingRmqMessages ,
237
+ [NSString stringWithCString: error encoding: NSUTF8StringEncoding]];
238
+ FIRMessagingLoggerError (kFIRMessagingMessageCodeRmq2PersistentStoreErrorCreatingTable ,
239
+ @" %@ " ,
240
+ errorMessage);
241
+ NSAssert (NO , errorMessage);
225
242
}
226
243
}
227
244
@@ -256,8 +273,17 @@ - (void)openDatabase:(NSString *)dbName {
256
273
BOOL didOpenDatabase = YES ;
257
274
if (![fileManager fileExistsAtPath: path]) {
258
275
// We've to separate between different versions here because of backwards compatbility issues.
259
- if (sqlite3_open ([path UTF8String ], &_database) != SQLITE_OK) {
260
- FIRMessaging_FAIL (@" %@ Could not open rmq database: %@ " , kFCMRmqStoreTag , path);
276
+ int result = sqlite3_open ([path UTF8String ], &_database);
277
+ if (result != SQLITE_OK) {
278
+ NSString *errorString = FIRMessagingStringFromSQLiteResult (result);
279
+ NSString *errorMessage =
280
+ [NSString stringWithFormat: @" Could not open existing RMQ database at path %@ , error: %@ " ,
281
+ path,
282
+ errorString];
283
+ FIRMessagingLoggerError (kFIRMessagingMessageCodeRmq2PersistentStoreErrorOpeningDatabase ,
284
+ @" %@ " ,
285
+ errorMessage);
286
+ NSAssert (NO , errorMessage);
261
287
didOpenDatabase = NO ;
262
288
return ;
263
289
}
@@ -267,8 +293,18 @@ - (void)openDatabase:(NSString *)dbName {
267
293
[self createTableWithName: kTableLastRmqId command: kCreateTableLastRmqId ];
268
294
[self createTableWithName: kTableS2DRmqIds command: kCreateTableS2DRmqIds ];
269
295
} else {
270
- if (sqlite3_open ([path UTF8String ], &_database) != SQLITE_OK) {
271
- FIRMessaging_FAIL (@" %@ Could not open rmq database: %@ " , kFCMRmqStoreTag , path);
296
+ // Calling sqlite3_open should create the database, since the file doesn't exist.
297
+ int result = sqlite3_open ([path UTF8String ], &_database);
298
+ if (result != SQLITE_OK) {
299
+ NSString *errorString = FIRMessagingStringFromSQLiteResult (result);
300
+ NSString *errorMessage =
301
+ [NSString stringWithFormat: @" Could not create RMQ database at path %@ , error: %@ " ,
302
+ path,
303
+ errorString];
304
+ FIRMessagingLoggerError (kFIRMessagingMessageCodeRmq2PersistentStoreErrorCreatingDatabase ,
305
+ @" %@ " ,
306
+ errorMessage);
307
+ NSAssert (NO , errorMessage);
272
308
didOpenDatabase = NO ;
273
309
} else {
274
310
[self updateDbWithStringRmqID ];
0 commit comments