SQLite Store: Create and update database schema version properly

CQTexperiment
Christopher Snowhill 2022-01-22 12:43:26 -08:00
parent ae10e3e75b
commit 6cf7d1d615
1 changed files with 20 additions and 3 deletions

View File

@ -594,14 +594,31 @@ static SQLiteStore *g_sharedStore = NULL;
memset(stmt, 0, sizeof(stmt)); memset(stmt, 0, sizeof(stmt));
BOOL dbExists = NO;
if ([[NSFileManager defaultManager] fileExistsAtPath:g_databasePath])
dbExists = YES;
if (sqlite3_open([g_databasePath UTF8String], &g_database) == SQLITE_OK) if (sqlite3_open([g_databasePath UTF8String], &g_database) == SQLITE_OK)
{ {
char * error; char * error;
NSArray * schemas = createSchema();
for (NSString *schema in schemas) if (!dbExists)
{ {
if (sqlite3_exec(g_database, [schema UTF8String], NULL, NULL, &error) != SQLITE_OK) NSArray * schemas = createSchema();
for (NSString *schema in schemas)
{
if (sqlite3_exec(g_database, [schema UTF8String], NULL, NULL, &error) != SQLITE_OK)
{
DLog(@"SQLite error: %s", error);
return nil;
}
}
NSString * createVersion = [NSString stringWithFormat:@"PRAGMA user_version = %lld", currentSchemaVersion];
if (sqlite3_exec(g_database, [createVersion UTF8String], NULL, NULL, &error))
{ {
DLog(@"SQLite error: %s", error); DLog(@"SQLite error: %s", error);
return nil; return nil;