From bebe563e8fd1962fe7ba2113622e7e63e08a1b79 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 5 Dec 2022 19:32:59 -0500 Subject: [PATCH] Further tweak persistent store migration --- .../MastodonCachePersistentStore.swift | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/Tusker/CoreData/MastodonCachePersistentStore.swift b/Tusker/CoreData/MastodonCachePersistentStore.swift index 4401e6ce..f37fde90 100644 --- a/Tusker/CoreData/MastodonCachePersistentStore.swift +++ b/Tusker/CoreData/MastodonCachePersistentStore.swift @@ -59,24 +59,27 @@ class MastodonCachePersistentStore: NSPersistentContainer { // can be removed after a sufficient time has passed if accountInfo!.id.contains("/") { for desc in persistentStoreDescriptions { - guard let new = desc.url else { + guard let new = desc.url, + !FileManager.default.fileExists(atPath: new.path) else { continue } - for ext in ["sqlite", "sqlite-shm", "sqlite-wal"] { - var old = new.deletingLastPathComponent() - let components = accountInfo!.id.split(separator: "/") - for dir in components.dropLast(1) { - old.appendPathComponent(String(dir), isDirectory: true) + do { + for ext in ["sqlite", "sqlite-shm", "sqlite-wal"] { + var old = new.deletingLastPathComponent() + let components = accountInfo!.id.split(separator: "/") + for dir in components.dropLast(1) { + old.appendPathComponent(String(dir), isDirectory: true) + } + old.appendPathComponent("\(components.last!)_cache", isDirectory: false) + old.appendPathExtension(ext) + if FileManager.default.fileExists(atPath: old.path) { + var expected = new.deletingLastPathComponent() + expected.appendPathComponent("\(accountInfo!.persistenceKey)_cache", isDirectory: false) + expected.appendPathExtension(ext) + try FileManager.default.moveItem(at: old, to: expected) + } } - old.appendPathComponent("\(components.last!)_cache", isDirectory: false) - old.appendPathExtension(ext) - if FileManager.default.fileExists(atPath: old.path) { - var expected = new.deletingLastPathComponent() - expected.appendPathComponent("\(accountInfo!.persistenceKey)_cache", isDirectory: false) - expected.appendPathExtension(ext) - try? FileManager.default.moveItem(at: old, to: expected) - } - } + } catch {} } } }