mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-25 17:50:59 +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
|
networkClient.instanceURL = identity.url
|
||||||
|
|
||||||
return identityDatabase.deleteIdentity(id: identity.id)
|
return identityDatabase.deleteIdentity(id: identity.id)
|
||||||
|
.collect()
|
||||||
.tryMap { _ in
|
.tryMap { _ in
|
||||||
DeletionEndpoint.oauthRevoke(
|
DeletionEndpoint.oauthRevoke(
|
||||||
token: try secretsService.item(.accessToken),
|
token: try secretsService.item(.accessToken),
|
||||||
|
@ -62,7 +63,11 @@ public extension AllIdentitiesService {
|
||||||
clientSecret: try secretsService.item(.clientSecret))
|
clientSecret: try secretsService.item(.clientSecret))
|
||||||
}
|
}
|
||||||
.flatMap(networkClient.request)
|
.flatMap(networkClient.request)
|
||||||
.tryMap { _ in try secretsService.deleteAllItems() }
|
.collect()
|
||||||
|
.tryMap { _ in
|
||||||
|
try secretsService.deleteAllItems()
|
||||||
|
try ContentDatabase.delete(forIdentityID: identity.id)
|
||||||
|
}
|
||||||
.ignoreOutput()
|
.ignoreOutput()
|
||||||
.eraseToAnyPublisher()
|
.eraseToAnyPublisher()
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,17 +10,10 @@ struct ContentDatabase {
|
||||||
private let databaseQueue: DatabaseQueue
|
private let databaseQueue: DatabaseQueue
|
||||||
|
|
||||||
init(identityID: UUID, environment: AppEnvironment) throws {
|
init(identityID: UUID, environment: AppEnvironment) throws {
|
||||||
guard
|
|
||||||
let documentsDirectory = NSSearchPathForDirectoriesInDomains(
|
|
||||||
.documentDirectory,
|
|
||||||
.userDomainMask, true)
|
|
||||||
.first
|
|
||||||
else { throw DatabaseError.documentsDirectoryNotFound }
|
|
||||||
|
|
||||||
if environment.inMemoryContent {
|
if environment.inMemoryContent {
|
||||||
databaseQueue = DatabaseQueue()
|
databaseQueue = DatabaseQueue()
|
||||||
} else {
|
} else {
|
||||||
databaseQueue = try DatabaseQueue(path: "\(documentsDirectory)/\(identityID.uuidString).sqlite3")
|
databaseQueue = try DatabaseQueue(path: try Self.fileURL(identityID: identityID).path)
|
||||||
}
|
}
|
||||||
|
|
||||||
try Self.migrate(databaseQueue)
|
try Self.migrate(databaseQueue)
|
||||||
|
@ -28,6 +21,10 @@ struct ContentDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension 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> {
|
func insert(status: Status) -> AnyPublisher<Never, Error> {
|
||||||
databaseQueue.writePublisher(updates: status.save)
|
databaseQueue.writePublisher(updates: status.save)
|
||||||
.ignoreOutput()
|
.ignoreOutput()
|
||||||
|
@ -179,6 +176,10 @@ extension ContentDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
private 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
|
// swiftlint:disable function_body_length
|
||||||
static func migrate(_ writer: DatabaseWriter) throws {
|
static func migrate(_ writer: DatabaseWriter) throws {
|
||||||
var migrator = DatabaseMigrator()
|
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
|
private let databaseQueue: DatabaseQueue
|
||||||
|
|
||||||
init(environment: AppEnvironment) throws {
|
init(environment: AppEnvironment) throws {
|
||||||
guard
|
|
||||||
let documentsDirectory = NSSearchPathForDirectoriesInDomains(
|
|
||||||
.documentDirectory,
|
|
||||||
.userDomainMask, true)
|
|
||||||
.first
|
|
||||||
else { throw DatabaseError.documentsDirectoryNotFound }
|
|
||||||
|
|
||||||
if environment.inMemoryContent {
|
if environment.inMemoryContent {
|
||||||
databaseQueue = DatabaseQueue()
|
databaseQueue = DatabaseQueue()
|
||||||
} else {
|
} 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)
|
try Self.migrate(databaseQueue)
|
||||||
|
@ -51,7 +46,7 @@ extension IdentityDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteIdentity(id: UUID) -> AnyPublisher<Never, Error> {
|
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()
|
.ignoreOutput()
|
||||||
.eraseToAnyPublisher()
|
.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