Skip to content

Commit

Permalink
Clean up datalayer code for db migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
tmolitor-stud-tu committed Feb 22, 2025
1 parent fcc5f37 commit 5a8ccb2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
9 changes: 0 additions & 9 deletions Monal/Classes/DataLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,10 @@ -(id) init

//checking db version and upgrading if necessary
DDLogInfo(@"Database version check");

//needed for sqlite >= 3.26.0 (see https://sqlite.org/lang_altertable.html point 2)
[self.db executeNonQuery:@"PRAGMA legacy_alter_table=on;"];
[self.db executeNonQuery:@"PRAGMA foreign_keys=off;"];

//do db upgrades and vacuum db afterwards
if([DataLayerMigrations migrateDB:self.db withDataLayer:self])
[self.db vacuum];

//turn foreign keys on again
//needed for sqlite >= 3.26.0 (see https://sqlite.org/lang_altertable.html point 2)
[self.db executeNonQuery:@"PRAGMA legacy_alter_table=off;"];
[self.db executeNonQuery:@"PRAGMA foreign_keys=on;"];

DDLogInfo(@"Database version check completed");

Expand Down
9 changes: 9 additions & 0 deletions Monal/Classes/DataLayerMigrations.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ +(BOOL) migrateDB:(MLSQLite*) db withDataLayer:(DataLayer*) dataLayer
}];

return [db boolWriteTransaction:^{
//needed for sqlite >= 3.26.0 (see https://sqlite.org/lang_altertable.html point 2)
[self.db executeNonQuery:@"PRAGMA legacy_alter_table=on;"];
[self.db executeNonQuery:@"PRAGMA foreign_keys=off;"];

NSNumber* dbversion = [self readDBVersion:db];
DDLogInfo(@"Got db version %@", dbversion);

Expand Down Expand Up @@ -1186,6 +1190,11 @@ FOREIGN KEY('account_id') REFERENCES 'account'('account_id') ON DELETE CASCADE \
}
}

//turn foreign keys on again
//needed for sqlite >= 3.26.0 (see https://sqlite.org/lang_altertable.html point 2)
[self.db executeNonQuery:@"PRAGMA legacy_alter_table=off;"];
[self.db executeNonQuery:@"PRAGMA foreign_keys=on;"];

//check if db version changed and invalidate state, if so
NSNumber* newdbversion = [self readDBVersion:db];
if([dbversion isEqualToNumber:newdbversion] == NO)
Expand Down

0 comments on commit 5a8ccb2

Please sign in to comment.