-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f678b94
commit 20fcd9b
Showing
21 changed files
with
661 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Foundation | ||
import GRDB | ||
|
||
public class WorkDAO { | ||
|
||
public static let shared = WorkDAO() | ||
|
||
public var db: Database { | ||
WorkDatabase.current | ||
} | ||
|
||
public func save(work: PersistedWork, completion: @escaping () -> Void) { | ||
db.write { db in | ||
try work.save(db) | ||
db.afterNextTransactionCommit { _ in | ||
completion() | ||
} | ||
} | ||
} | ||
|
||
public func works(with types: [String]) -> [PersistedWork] { | ||
db.select(where: types.contains(PersistedWork.column(of: .type))) | ||
} | ||
|
||
public func delete(id: String) { | ||
db.delete(PersistedWork.self, where: PersistedWork.column(of: .id) == id) | ||
} | ||
|
||
public func update(context: Data?, forWorkWith id: String) { | ||
db.update(PersistedWork.self, | ||
assignments: [PersistedWork.column(of: .context).set(to: context)], | ||
where: PersistedWork.column(of: .id) == id) | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
MixinServices/MixinServices/Database/Work/WorkDatabase.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import GRDB | ||
|
||
public final class WorkDatabase: Database { | ||
|
||
public private(set) static var current: WorkDatabase! = try! WorkDatabase(url: AppGroupContainer.workDatabaseURL) | ||
|
||
public override class var config: Configuration { | ||
var config = super.config | ||
config.label = "Work" | ||
return config | ||
} | ||
|
||
public override var needsMigration: Bool { | ||
try! pool.read({ (db) -> Bool in | ||
let migrationsCompleted = try migrator.hasCompletedMigrations(db) | ||
return !migrationsCompleted | ||
}) | ||
} | ||
|
||
private var migrator: DatabaseMigrator { | ||
var migrator = DatabaseMigrator() | ||
|
||
migrator.registerMigration("create_table") { db in | ||
try db.create(table: "works") { td in | ||
td.column("id", .text).primaryKey().notNull() | ||
td.column("type", .text).notNull() | ||
td.column("context", .blob) | ||
td.column("priority", .integer).notNull() | ||
} | ||
} | ||
|
||
return migrator | ||
} | ||
|
||
public static func reloadCurrent() { | ||
current = try! WorkDatabase(url: AppGroupContainer.workDatabaseURL) | ||
current.migrate() | ||
} | ||
|
||
private func migrate() { | ||
try! migrator.migrate(pool) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.