-
Notifications
You must be signed in to change notification settings - Fork 218
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
10c6439
commit de84957
Showing
3 changed files
with
47 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,39 @@ | ||
import Foundation | ||
import GRDB | ||
|
||
actor Database { | ||
private static var writer: (any DatabaseWriter)? | ||
enum Database { | ||
static let sharedWriter = makeShared() | ||
|
||
static func sharedWriter() throws -> any DatabaseWriter { | ||
if let writer { | ||
return writer | ||
} | ||
try FileManager.default.createDirectory(at: FilePath.sharedDirectory, withIntermediateDirectories: true) | ||
let database = try DatabasePool(path: FilePath.sharedDirectory.appendingPathComponent("settings.db").relativePath) | ||
var migrator = DatabaseMigrator().disablingDeferredForeignKeyChecks() | ||
migrator.registerMigration("initialize") { db in | ||
try db.create(table: "profiles") { t in | ||
t.autoIncrementedPrimaryKey("id") | ||
t.column("name", .text).notNull() | ||
t.column("order", .integer).notNull() | ||
t.column("type", .integer).notNull().defaults(to: ProfileType.local.rawValue) | ||
t.column("path", .text).notNull() | ||
t.column("remoteURL", .text) | ||
t.column("autoUpdate", .boolean).notNull().defaults(to: false) | ||
t.column("lastUpdated", .datetime) | ||
} | ||
try db.create(table: "preferences") { t in | ||
t.primaryKey("name", .text, onConflict: .replace).notNull() | ||
t.column("data", .blob) | ||
private static func makeShared() -> any DatabaseWriter { | ||
do { | ||
try FileManager.default.createDirectory(at: FilePath.sharedDirectory, withIntermediateDirectories: true) | ||
let database = try DatabasePool(path: FilePath.sharedDirectory.appendingPathComponent("settings.db").relativePath) | ||
var migrator = DatabaseMigrator().disablingDeferredForeignKeyChecks() | ||
migrator.registerMigration("initialize") { db in | ||
try db.create(table: "profiles") { t in | ||
t.autoIncrementedPrimaryKey("id") | ||
t.column("name", .text).notNull() | ||
t.column("order", .integer).notNull() | ||
t.column("type", .integer).notNull().defaults(to: ProfileType.local.rawValue) | ||
t.column("path", .text).notNull() | ||
t.column("remoteURL", .text) | ||
t.column("autoUpdate", .boolean).notNull().defaults(to: false) | ||
t.column("lastUpdated", .datetime) | ||
} | ||
try db.create(table: "preferences") { t in | ||
t.primaryKey("name", .text, onConflict: .replace).notNull() | ||
t.column("data", .blob) | ||
} | ||
} | ||
} | ||
migrator.registerMigration("add_auto_update_interval") { db in | ||
try db.alter(table: "profiles") { t in | ||
t.add(column: "autoUpdateInterval", .integer).notNull().defaults(to: 0) | ||
migrator.registerMigration("add_auto_update_interval") { db in | ||
try db.alter(table: "profiles") { t in | ||
t.add(column: "autoUpdateInterval", .integer).notNull().defaults(to: 0) | ||
} | ||
} | ||
try migrator.migrate(database) | ||
return database | ||
} catch { | ||
fatalError(error.localizedDescription) | ||
} | ||
|
||
try migrator.migrate(database) | ||
writer = database | ||
return database | ||
} | ||
} |
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