Mark migration as succesful only in case of success

This commit is contained in:
Nathan Mattes 2023-06-13 13:05:05 +02:00
parent 183f303065
commit aede20f2c8
1 changed files with 11 additions and 5 deletions

View File

@ -69,13 +69,11 @@ public extension AuthenticationServiceProvider {
} }
func migrateLegacyAuthentications(in context: NSManagedObjectContext) { func migrateLegacyAuthentications(in context: NSManagedObjectContext) {
defer { userDefaults.didMigrateAuthentications = true }
do { do {
let request = NSFetchRequest<NSManagedObject>(entityName: "MastodonAuthentication") let request = NSFetchRequest<NSManagedObject>(entityName: "MastodonAuthentication")
let legacyAuthentications = try context.fetch(request) let legacyAuthentications = try context.fetch(request)
self.authentications = legacyAuthentications.compactMap { auth -> MastodonAuthentication? in let migratedAuthentications = legacyAuthentications.compactMap { auth -> MastodonAuthentication? in
guard guard
let identifier = auth.value(forKey: "identifier") as? UUID, let identifier = auth.value(forKey: "identifier") as? UUID,
let domain = auth.value(forKey: "domain") as? String, let domain = auth.value(forKey: "domain") as? String,
@ -106,13 +104,21 @@ public extension AuthenticationServiceProvider {
userID: userID userID: userID
) )
} }
if migratedAuthentications.count != legacyAuthentications.count {
logger.log(level: .default, "Not all mitgrations could be migrated.")
}
self.authentications = migratedAuthentications
userDefaults.didMigrateAuthentications = true
} catch { } catch {
userDefaults.didMigrateAuthentications = false
logger.log(level: .error, "Could not migrate legacy authentications") logger.log(level: .error, "Could not migrate legacy authentications")
} }
} }
var authenticationMigrationRequired: Bool { var authenticationMigrationRequired: Bool {
!userDefaults.didMigrateAuthentications userDefaults.didMigrateAuthentications == false
} }
} }