2017-03-14 23:16:05 +01:00
|
|
|
//
|
2019-01-11 16:40:41 +01:00
|
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
2017-03-14 23:16:05 +01:00
|
|
|
//
|
2016-09-28 18:21:14 +02:00
|
|
|
|
|
|
|
#import "OWSDatabaseMigrationRunner.h"
|
2017-12-04 18:38:44 +01:00
|
|
|
#import "OWS100RemoveTSRecipientsMigration.h"
|
|
|
|
#import "OWS102MoveLoggingPreferenceToUserDefaults.h"
|
|
|
|
#import "OWS103EnableVideoCalling.h"
|
2017-12-04 16:35:47 +01:00
|
|
|
#import "OWS104CreateRecipientIdentities.h"
|
2017-12-04 18:38:44 +01:00
|
|
|
#import "OWS105AttachmentFilePaths.h"
|
2018-02-28 17:55:49 +01:00
|
|
|
#import "OWS107LegacySounds.h"
|
2018-03-06 19:16:34 +01:00
|
|
|
#import "OWS108CallLoggingPreference.h"
|
2018-04-24 22:02:35 +02:00
|
|
|
#import "OWS109OutgoingMessageState.h"
|
2017-12-01 21:17:29 +01:00
|
|
|
#import "OWSDatabaseMigration.h"
|
2017-12-04 18:38:44 +01:00
|
|
|
#import <SignalMessaging/SignalMessaging-Swift.h>
|
2020-06-05 02:38:44 +02:00
|
|
|
#import <SessionServiceKit/AppContext.h>
|
2016-09-28 18:21:14 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
@implementation OWSDatabaseMigrationRunner
|
|
|
|
|
2019-01-11 16:40:41 +01:00
|
|
|
#pragma mark - Dependencies
|
|
|
|
|
|
|
|
- (OWSPrimaryStorage *)primaryStorage
|
|
|
|
{
|
|
|
|
OWSAssertDebug(SSKEnvironment.shared.primaryStorage);
|
|
|
|
|
|
|
|
return SSKEnvironment.shared.primaryStorage;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-12-04 18:38:44 +01:00
|
|
|
// This should all migrations which do NOT qualify as safeBlockingMigrations:
|
2016-09-28 18:21:14 +02:00
|
|
|
- (NSArray<OWSDatabaseMigration *> *)allMigrations
|
|
|
|
{
|
2017-12-04 18:38:44 +01:00
|
|
|
return @[
|
2018-11-27 22:59:38 +01:00
|
|
|
[[OWS100RemoveTSRecipientsMigration alloc] init],
|
|
|
|
[[OWS102MoveLoggingPreferenceToUserDefaults alloc] init],
|
|
|
|
[[OWS103EnableVideoCalling alloc] init],
|
|
|
|
[[OWS104CreateRecipientIdentities alloc] init],
|
|
|
|
[[OWS105AttachmentFilePaths alloc] init],
|
|
|
|
[[OWS106EnsureProfileComplete alloc] init],
|
|
|
|
[[OWS107LegacySounds alloc] init],
|
|
|
|
[[OWS108CallLoggingPreference alloc] init],
|
|
|
|
[[OWS109OutgoingMessageState alloc] init],
|
2018-10-03 23:41:43 +02:00
|
|
|
[OWS110SortIdMigration new],
|
2018-11-27 22:59:38 +01:00
|
|
|
[[OWS111UDAttributesMigration alloc] init],
|
|
|
|
[[OWS112TypingIndicatorsMigration alloc] init],
|
|
|
|
[[OWS113MultiAttachmentMediaMessages alloc] init],
|
2018-12-13 21:57:56 +01:00
|
|
|
[[OWS114RemoveDynamicInteractions alloc] init],
|
2020-05-04 01:42:02 +02:00
|
|
|
[[LK001UpdateFriendRequestStatusStorage alloc] init]
|
2017-12-04 18:38:44 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-09-28 18:21:14 +02:00
|
|
|
- (void)assumeAllExistingMigrationsRun
|
|
|
|
{
|
|
|
|
for (OWSDatabaseMigration *migration in self.allMigrations) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Skipping migration on new install: %@", migration);
|
2016-09-28 18:21:14 +02:00
|
|
|
[migration save];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:27:44 +01:00
|
|
|
- (void)runAllOutstandingWithCompletion:(OWSDatabaseMigrationCompletion)completion
|
2016-09-28 18:21:14 +02:00
|
|
|
{
|
2019-01-11 16:40:41 +01:00
|
|
|
[self removeUnknownMigrations];
|
|
|
|
|
2018-03-13 14:51:57 +01:00
|
|
|
[self runMigrations:[self.allMigrations mutableCopy] completion:completion];
|
2017-06-15 19:43:18 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 16:40:41 +01:00
|
|
|
// Some users (especially internal users) will move back and forth between
|
|
|
|
// app versions. Whenever they move "forward" in the version history, we
|
|
|
|
// want them to re-run any new migrations. Therefore, when they move "backward"
|
|
|
|
// in the version history, we cull any unknown migrations.
|
|
|
|
- (void)removeUnknownMigrations
|
|
|
|
{
|
|
|
|
NSMutableSet<NSString *> *knownMigrationIds = [NSMutableSet new];
|
|
|
|
for (OWSDatabaseMigration *migration in self.allMigrations) {
|
|
|
|
[knownMigrationIds addObject:migration.uniqueId];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self.primaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
NSArray<NSString *> *savedMigrationIds = [transaction allKeysInCollection:OWSDatabaseMigration.collection];
|
|
|
|
|
|
|
|
NSMutableSet<NSString *> *unknownMigrationIds = [NSMutableSet new];
|
|
|
|
[unknownMigrationIds addObjectsFromArray:savedMigrationIds];
|
|
|
|
[unknownMigrationIds minusSet:knownMigrationIds];
|
|
|
|
|
|
|
|
for (NSString *unknownMigrationId in unknownMigrationIds) {
|
|
|
|
OWSLogInfo(@"Culling unknown migration: %@", unknownMigrationId);
|
|
|
|
[transaction removeObjectForKey:unknownMigrationId inCollection:OWSDatabaseMigration.collection];
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:51:57 +01:00
|
|
|
// Run migrations serially to:
|
|
|
|
//
|
|
|
|
// * Ensure predictable ordering.
|
|
|
|
// * Prevent them from interfering with each other (e.g. deadlock).
|
|
|
|
- (void)runMigrations:(NSMutableArray<OWSDatabaseMigration *> *)migrations
|
2018-01-30 17:27:44 +01:00
|
|
|
completion:(OWSDatabaseMigrationCompletion)completion
|
2017-06-15 19:43:18 +02:00
|
|
|
{
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(migrations);
|
|
|
|
OWSAssertDebug(completion);
|
2017-06-15 19:43:18 +02:00
|
|
|
|
2018-03-13 14:51:57 +01:00
|
|
|
// If there are no more migrations to run, complete.
|
|
|
|
if (migrations.count < 1) {
|
2018-01-30 17:27:44 +01:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
completion();
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:51:57 +01:00
|
|
|
// Pop next migration from front of queue.
|
|
|
|
OWSDatabaseMigration *migration = migrations.firstObject;
|
|
|
|
[migrations removeObjectAtIndex:0];
|
|
|
|
|
|
|
|
// If migration has already been run, skip it.
|
|
|
|
if ([OWSDatabaseMigration fetchObjectWithUniqueID:migration.uniqueId] != nil) {
|
|
|
|
[self runMigrations:migrations completion:completion];
|
|
|
|
return;
|
2016-09-28 18:21:14 +02:00
|
|
|
}
|
2018-03-13 14:51:57 +01:00
|
|
|
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Running migration: %@", migration);
|
2018-03-13 14:51:57 +01:00
|
|
|
[migration runUpWithCompletion:^{
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Migration complete: %@", migration);
|
2018-03-13 14:51:57 +01:00
|
|
|
[self runMigrations:migrations completion:completion];
|
|
|
|
}];
|
2016-09-28 18:21:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|