mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-22 00:01:00 +00:00
Improve db file handling
This commit is contained in:
parent
66e7e38496
commit
2ef7b00130
5 changed files with 48 additions and 25 deletions
|
@ -55,6 +55,7 @@ public extension AllIdentitiesService {
|
|||
networkClient.instanceURL = identity.url
|
||||
|
||||
return identityDatabase.deleteIdentity(id: identity.id)
|
||||
.collect()
|
||||
.tryMap { _ in
|
||||
DeletionEndpoint.oauthRevoke(
|
||||
token: try secretsService.item(.accessToken),
|
||||
|
@ -62,7 +63,11 @@ public extension AllIdentitiesService {
|
|||
clientSecret: try secretsService.item(.clientSecret))
|
||||
}
|
||||
.flatMap(networkClient.request)
|
||||
.tryMap { _ in try secretsService.deleteAllItems() }
|
||||
.collect()
|
||||
.tryMap { _ in
|
||||
try secretsService.deleteAllItems()
|
||||
try ContentDatabase.delete(forIdentityID: identity.id)
|
||||
}
|
||||
.ignoreOutput()
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
|
|
@ -10,17 +10,10 @@ struct ContentDatabase {
|
|||
private let databaseQueue: DatabaseQueue
|
||||
|
||||
init(identityID: UUID, environment: AppEnvironment) throws {
|
||||
guard
|
||||
let documentsDirectory = NSSearchPathForDirectoriesInDomains(
|
||||
.documentDirectory,
|
||||
.userDomainMask, true)
|
||||
.first
|
||||
else { throw DatabaseError.documentsDirectoryNotFound }
|
||||
|
||||
if environment.inMemoryContent {
|
||||
databaseQueue = DatabaseQueue()
|
||||
} else {
|
||||
databaseQueue = try DatabaseQueue(path: "\(documentsDirectory)/\(identityID.uuidString).sqlite3")
|
||||
databaseQueue = try DatabaseQueue(path: try Self.fileURL(identityID: identityID).path)
|
||||
}
|
||||
|
||||
try Self.migrate(databaseQueue)
|
||||
|
@ -28,6 +21,10 @@ struct ContentDatabase {
|
|||
}
|
||||
|
||||
extension ContentDatabase {
|
||||
static func delete(forIdentityID identityID: UUID) throws {
|
||||
try FileManager.default.removeItem(at: try fileURL(identityID: identityID))
|
||||
}
|
||||
|
||||
func insert(status: Status) -> AnyPublisher<Never, Error> {
|
||||
databaseQueue.writePublisher(updates: status.save)
|
||||
.ignoreOutput()
|
||||
|
@ -179,6 +176,10 @@ extension ContentDatabase {
|
|||
}
|
||||
|
||||
private extension ContentDatabase {
|
||||
static func fileURL(identityID: UUID) throws -> URL {
|
||||
try FileManager.default.databaseDirectoryURL().appendingPathComponent(identityID.uuidString + ".sqlite")
|
||||
}
|
||||
|
||||
// swiftlint:disable function_body_length
|
||||
static func migrate(_ writer: DatabaseWriter) throws {
|
||||
var migrator = DatabaseMigrator()
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
// Copyright © 2020 Metabolist. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
public enum DatabaseError: Error {
|
||||
case documentsDirectoryNotFound
|
||||
}
|
|
@ -13,17 +13,12 @@ struct IdentityDatabase {
|
|||
private let databaseQueue: DatabaseQueue
|
||||
|
||||
init(environment: AppEnvironment) throws {
|
||||
guard
|
||||
let documentsDirectory = NSSearchPathForDirectoriesInDomains(
|
||||
.documentDirectory,
|
||||
.userDomainMask, true)
|
||||
.first
|
||||
else { throw DatabaseError.documentsDirectoryNotFound }
|
||||
|
||||
if environment.inMemoryContent {
|
||||
databaseQueue = DatabaseQueue()
|
||||
} else {
|
||||
databaseQueue = try DatabaseQueue(path: "\(documentsDirectory)/IdentityDatabase.sqlite3")
|
||||
let databaseURL = try FileManager.default.databaseDirectoryURL().appendingPathComponent("Identities.sqlite")
|
||||
|
||||
databaseQueue = try DatabaseQueue(path: databaseURL.path)
|
||||
}
|
||||
|
||||
try Self.migrate(databaseQueue)
|
||||
|
@ -51,7 +46,7 @@ extension IdentityDatabase {
|
|||
}
|
||||
|
||||
func deleteIdentity(id: UUID) -> AnyPublisher<Never, Error> {
|
||||
return databaseQueue.writePublisher(updates: StoredIdentity.filter(Column("id") == id).deleteAll)
|
||||
databaseQueue.writePublisher(updates: StoredIdentity.filter(Column("id") == id).deleteAll)
|
||||
.ignoreOutput()
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Justin Mazzocchi on 9/2/20.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension FileManager {
|
||||
func databaseDirectoryURL() throws -> URL {
|
||||
let databaseDirectoryURL = try url(for: .applicationSupportDirectory,
|
||||
in: .userDomainMask,
|
||||
appropriateFor: nil,
|
||||
create: true)
|
||||
.appendingPathComponent("Database")
|
||||
var isDirectory: ObjCBool = false
|
||||
|
||||
if !fileExists(atPath: databaseDirectoryURL.path, isDirectory: &isDirectory) {
|
||||
try createDirectory(at: databaseDirectoryURL,
|
||||
withIntermediateDirectories: false,
|
||||
attributes: [.protectionKey: FileProtectionType.complete])
|
||||
} else if !isDirectory.boolValue {
|
||||
throw NSError(domain: NSCocoaErrorDomain, code: NSFileWriteFileExistsError, userInfo: nil)
|
||||
}
|
||||
|
||||
return databaseDirectoryURL
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue