forked from pointfreeco/sqlite-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadatabase.swift
More file actions
138 lines (133 loc) · 4.21 KB
/
Copy pathMetadatabase.swift
File metadata and controls
138 lines (133 loc) · 4.21 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#if canImport(CloudKit)
import Foundation
import os
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
func defaultMetadatabase(
logger: Logger,
url: URL
) throws -> any DatabaseWriter {
logger.debug(
"""
Metadatabase connection:
open "\(url.path(percentEncoded: false))"
"""
)
@Dependency(\.context) var context
guard !url.isInMemory || context != .live
else {
struct InMemoryDatabase: Error {}
throw InMemoryDatabase()
}
let metadatabase = try DatabasePool(path: url.path(percentEncoded: false))
try migrate(metadatabase: metadatabase)
return metadatabase
}
func migrate(metadatabase: some DatabaseWriter) throws {
var migrator = DatabaseMigrator()
migrator.registerMigration("Create Metadata Tables") { db in
try #sql(
"""
CREATE TABLE "\(raw: .sqliteDataCloudKitSchemaName)_metadata" (
"recordPrimaryKey" TEXT NOT NULL,
"recordType" TEXT NOT NULL,
"recordName" TEXT NOT NULL AS ("recordPrimaryKey" || ':' || "recordType"),
"zoneName" TEXT NOT NULL,
"ownerName" TEXT NOT NULL,
"parentRecordPrimaryKey" TEXT,
"parentRecordType" TEXT,
"parentRecordName" TEXT AS ("parentRecordPrimaryKey" || ':' || "parentRecordType"),
"lastKnownServerRecord" BLOB,
"_lastKnownServerRecordAllFields" BLOB,
"share" BLOB,
"hasLastKnownServerRecord" INTEGER NOT NULL AS ("lastKnownServerRecord" IS NOT NULL),
"isShared" INTEGER NOT NULL AS ("share" IS NOT NULL),
"userModificationTime" INTEGER NOT NULL DEFAULT (\($currentTime())),
"_isDeleted" INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY ("recordPrimaryKey", "recordType"),
UNIQUE ("recordName")
) STRICT
"""
)
.execute(db)
try #sql(
"""
CREATE INDEX "\(raw: .sqliteDataCloudKitSchemaName)_metadata_zoneID"
ON "\(raw: .sqliteDataCloudKitSchemaName)_metadata"("ownerName", "zoneName")
"""
)
.execute(db)
try #sql(
"""
CREATE INDEX "\(raw: .sqliteDataCloudKitSchemaName)_metadata_parentRecordName"
ON "\(raw: .sqliteDataCloudKitSchemaName)_metadata"("parentRecordName")
"""
)
.execute(db)
try #sql(
"""
CREATE INDEX "\(raw: .sqliteDataCloudKitSchemaName)_metadata_isShared"
ON "\(raw: .sqliteDataCloudKitSchemaName)_metadata"("isShared")
"""
)
.execute(db)
try #sql(
"""
CREATE INDEX IF NOT EXISTS "\(raw: .sqliteDataCloudKitSchemaName)_metadata_hasLastKnownServerRecord"
ON "\(raw: .sqliteDataCloudKitSchemaName)_metadata"("hasLastKnownServerRecord")
"""
)
.execute(db)
try #sql(
"""
CREATE TABLE "\(raw: .sqliteDataCloudKitSchemaName)_recordTypes" (
"tableName" TEXT NOT NULL PRIMARY KEY,
"schema" TEXT NOT NULL,
"tableInfo" TEXT NOT NULL
) STRICT
"""
)
.execute(db)
try #sql(
"""
CREATE TABLE "\(raw: .sqliteDataCloudKitSchemaName)_stateSerialization" (
"scope" TEXT NOT NULL PRIMARY KEY,
"data" TEXT NOT NULL
) STRICT
"""
)
.execute(db)
try #sql(
"""
CREATE TABLE "\(raw: .sqliteDataCloudKitSchemaName)_unsyncedRecordIDs" (
"recordName" TEXT NOT NULL,
"zoneName" TEXT NOT NULL,
"ownerName" TEXT NOT NULL,
PRIMARY KEY ("recordName", "zoneName", "ownerName")
) STRICT
"""
)
.execute(db)
try #sql(
"""
CREATE TABLE "\(raw: .sqliteDataCloudKitSchemaName)_pendingRecordZoneChanges" (
"pendingRecordZoneChange" BLOB NOT NULL
) STRICT
"""
)
.execute(db)
}
#if DEBUG
try metadatabase.read { db in
let hasSchemaChanges = try migrator.hasSchemaChanges(db)
assert(
!hasSchemaChanges,
"""
A previously run migration has been removed or edited. \
Metadatabase migrations must not be modified after release.
"""
)
}
#endif
try migrator.migrate(metadatabase)
}
#endif