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