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
172 lines (158 loc) · 4.8 KB
/
Schema.swift
File metadata and controls
172 lines (158 loc) · 4.8 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import OSLog
import SQLiteData
import SwiftUI
@Table
nonisolated struct SyncUp: Hashable, Identifiable {
let id: UUID
var seconds: Int = 60 * 5
var theme: Theme = .bubblegum
var title = ""
}
@Table
nonisolated struct Attendee: Hashable, Identifiable {
let id: UUID
var name = ""
var syncUpID: SyncUp.ID
}
@Table
nonisolated struct Meeting: Hashable, Identifiable {
let id: UUID
var date: Date
var syncUpID: SyncUp.ID
var transcript: String
}
enum Theme: String, CaseIterable, Hashable, Identifiable, QueryBindable {
case appIndigo
case appMagenta
case appOrange
case appPurple
case appTeal
case appYellow
case bubblegum
case buttercup
case lavender
case navy
case oxblood
case periwinkle
case poppy
case seafoam
case sky
case tan
var id: Self { self }
var accentColor: Color {
switch self {
case .appOrange, .appTeal, .appYellow, .bubblegum, .buttercup, .lavender, .periwinkle, .poppy,
.seafoam, .sky, .tan:
return .black
case .appIndigo, .appMagenta, .appPurple, .navy, .oxblood:
return .white
}
}
var mainColor: Color { Color(rawValue) }
var name: String {
switch self {
case .appIndigo, .appMagenta, .appOrange, .appPurple, .appTeal, .appYellow:
rawValue.dropFirst(3).capitalized
case .bubblegum, .buttercup, .lavender, .navy, .oxblood, .periwinkle, .poppy, .seafoam, .sky,
.tan:
rawValue.capitalized
}
}
}
extension Int {
var duration: Duration {
get { .seconds(self) }
set { self = Int(newValue.components.seconds) }
}
}
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 initial tables") { db in
try #sql(
"""
CREATE TABLE "syncUps" (
"id" TEXT PRIMARY KEY NOT NULL ON CONFLICT REPLACE DEFAULT (uuid()),
"seconds" INTEGER NOT NULL ON CONFLICT REPLACE DEFAULT 300,
"theme" TEXT NOT NULL ON CONFLICT REPLACE DEFAULT \(raw: Theme.bubblegum.rawValue),
"title" TEXT NOT NULL ON CONFLICT REPLACE DEFAULT ''
) STRICT
"""
)
.execute(db)
try #sql(
"""
CREATE TABLE "attendees" (
"id" TEXT PRIMARY KEY NOT NULL ON CONFLICT REPLACE DEFAULT (uuid()),
"name" TEXT NOT NULL ON CONFLICT REPLACE DEFAULT '',
"syncUpID" TEXT NOT NULL REFERENCES "syncUps"("id") ON DELETE CASCADE
) STRICT
"""
)
.execute(db)
try #sql(
"""
CREATE TABLE "meetings" (
"id" TEXT PRIMARY KEY NOT NULL ON CONFLICT REPLACE DEFAULT (uuid()),
"date" TEXT NOT NULL ON CONFLICT REPLACE DEFAULT CURRENT_TIMESTAMP,
"syncUpID" TEXT NOT NULL REFERENCES "syncUps"("id") ON DELETE CASCADE,
"transcript" TEXT NOT NULL ON CONFLICT REPLACE DEFAULT ''
) STRICT
"""
)
.execute(db)
}
try migrator.migrate(database)
defaultDatabase = database
defaultSyncEngine = try SyncEngine(
for: database,
tables: SyncUp.self,
Attendee.self,
Meeting.self
)
}
}
private let logger = Logger(subsystem: "SyncUps", category: "Database")
#if DEBUG
extension Database {
func seedSampleData() throws {
try seed {
SyncUp(id: UUID(1), seconds: 60, theme: .appOrange, title: "Design")
SyncUp(id: UUID(2), seconds: 60 * 10, theme: .periwinkle, title: "Engineering")
SyncUp(id: UUID(3), seconds: 60 * 30, theme: .poppy, title: "Product")
for name in ["Blob", "Blob Jr", "Blob Sr", "Blob Esq", "Blob III", "Blob I"] {
Attendee.Draft(name: name, syncUpID: UUID(1))
}
for name in ["Blob", "Blob Jr"] {
Attendee.Draft(name: name, syncUpID: UUID(2))
}
for name in ["Blob Sr", "Blob Jr"] {
Attendee.Draft(name: name, syncUpID: UUID(3))
}
Meeting.Draft(
date: Date().addingTimeInterval(-60 * 60 * 24 * 7),
syncUpID: UUID(1),
transcript: """
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor \
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud \
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute \
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia \
deserunt mollit anim id est laborum.
"""
)
}
}
}
#endif