forked from pointfreeco/sqlite-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchema.swift
More file actions
46 lines (42 loc) · 1.05 KB
/
Schema.swift
File metadata and controls
46 lines (42 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import Foundation
import OSLog
import SQLiteData
@Table
nonisolated struct Counter: Identifiable {
let id: UUID
var count = 0
}
extension DependencyValues {
mutating func bootstrapDatabase() throws {
@Dependency(\.context) var context
let database = try SQLiteData.defaultDatabase()
logger.debug(
"""
App database
open "\(database.path)"
"""
)
var migrator = DatabaseMigrator()
#if DEBUG
migrator.eraseDatabaseOnSchemaChange = true
#endif
migrator.registerMigration("Create tables") { db in
try #sql(
"""
CREATE TABLE "counters" (
"id" TEXT PRIMARY KEY NOT NULL ON CONFLICT REPLACE DEFAULT (uuid()),
"count" INT NOT NULL ON CONFLICT REPLACE DEFAULT 0
) STRICT
"""
)
.execute(db)
}
try migrator.migrate(database)
defaultDatabase = database
defaultSyncEngine = try SyncEngine(
for: defaultDatabase,
tables: Counter.self
)
}
}
private let logger = Logger(subsystem: "CloudKitDemo", category: "Database")