Skip to content

Commit 5b23ef4

Browse files
Migrate to triggers, add RemindersList Defaults (#114)
* Migrate to triggers, add RemindersList Defaults * wip * Bump sleep for CI --------- Co-authored-by: Stephen Celis <[email protected]>
1 parent 4a570ab commit 5b23ef4

File tree

4 files changed

+51
-50
lines changed

4 files changed

+51
-50
lines changed

Examples/Examples.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/Reminders/Helpers.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@ extension Color {
55
public struct HexRepresentation: QueryBindable, QueryRepresentable {
66
public var queryOutput: Color
77
public var queryBinding: QueryBinding {
8-
guard let components = UIColor(queryOutput).cgColor.components
9-
else {
8+
guard let hexValue else {
109
struct InvalidColor: Error {}
1110
return .invalid(InvalidColor())
1211
}
13-
let r = Int64(components[0] * 0xFF) << 24
14-
let g = Int64(components[1] * 0xFF) << 16
15-
let b = Int64(components[2] * 0xFF) << 8
16-
let a = Int64((components.indices.contains(3) ? components[3] : 1) * 0xFF)
17-
return .int(r | g | b | a)
12+
return .int(hexValue)
1813
}
1914
public init(queryOutput: Color) {
2015
self.queryOutput = queryOutput
@@ -30,5 +25,14 @@ extension Color {
3025
)
3126
)
3227
}
28+
public var hexValue: Int64? {
29+
guard let components = UIColor(queryOutput).cgColor.components
30+
else { return nil }
31+
let r = Int64(components[0] * 0xFF) << 24
32+
let g = Int64(components[1] * 0xFF) << 16
33+
let b = Int64(components[2] * 0xFF) << 8
34+
let a = Int64((components.indices.contains(3) ? components[3] : 1) * 0xFF)
35+
return r | g | b | a
36+
}
3337
}
3438
}

Examples/Reminders/Schema.swift

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import SwiftUI
99
struct RemindersList: Hashable, Identifiable {
1010
let id: UUID
1111
@Column(as: Color.HexRepresentation.self)
12-
var color = Color(red: 0x4a / 255, green: 0x99 / 255, blue: 0xef / 255)
12+
var color: Color = Self.defaultColor
1313
var position = 0
1414
var title = ""
15+
16+
static var defaultColor: Color { Color(red: 0x4a / 255, green: 0x99 / 255, blue: 0xef / 255) }
17+
static var defaultTitle: String { "Personal" }
1518
}
1619

1720
extension RemindersList.Draft: Identifiable {}
@@ -123,11 +126,12 @@ func appDatabase() throws -> any DatabaseWriter {
123126
migrator.eraseDatabaseOnSchemaChange = true
124127
#endif
125128
migrator.registerMigration("Create initial tables") { db in
129+
let defaultListColor = Color.HexRepresentation(queryOutput: RemindersList.defaultColor).hexValue
126130
try #sql(
127131
"""
128132
CREATE TABLE "remindersLists" (
129133
"id" TEXT PRIMARY KEY NOT NULL ON CONFLICT REPLACE DEFAULT (uuid()),
130-
"color" INTEGER NOT NULL DEFAULT \(raw: 0x4a99_ef00),
134+
"color" INTEGER NOT NULL DEFAULT \(raw: defaultListColor ?? 0),
131135
"position" INTEGER NOT NULL DEFAULT 0,
132136
"title" TEXT NOT NULL
133137
) STRICT
@@ -178,49 +182,33 @@ func appDatabase() throws -> any DatabaseWriter {
178182

179183
try migrator.migrate(database)
180184

181-
if context == .preview {
182-
try database.write { db in
185+
try database.write { db in
186+
if context == .preview {
183187
try db.seedSampleData()
184188
}
185-
}
186189

187-
try database.write { db in
188-
try #sql(
189-
"""
190-
CREATE TEMPORARY TRIGGER "default_position_reminders_lists"
191-
AFTER INSERT ON "remindersLists"
192-
FOR EACH ROW BEGIN
193-
UPDATE "remindersLists"
194-
SET "position" = (SELECT max("position") + 1 FROM "remindersLists")
195-
WHERE "id" = NEW."id";
196-
END
197-
"""
198-
)
190+
try RemindersList.createTemporaryTrigger(after: .insert { new in
191+
RemindersList
192+
.find(new.id)
193+
.update { $0.position = RemindersList.select { ($0.position.max() ?? -1) + 1} }
194+
})
199195
.execute(db)
200-
try #sql(
201-
"""
202-
CREATE TEMPORARY TRIGGER "default_position_reminders"
203-
AFTER INSERT ON "reminders"
204-
FOR EACH ROW BEGIN
205-
UPDATE "reminders"
206-
SET "position" = (SELECT max("position") + 1 FROM "reminders")
207-
WHERE "id" = NEW."id";
208-
END
209-
"""
210-
)
196+
try Reminder.createTemporaryTrigger(after: .insert { new in
197+
Reminder
198+
.find(new.id)
199+
.update { $0.position = Reminder.select { ($0.position.max() ?? -1) + 1} }
200+
})
211201
.execute(db)
212-
try #sql(
213-
"""
214-
CREATE TEMPORARY TRIGGER "non_empty_reminders_lists"
215-
AFTER DELETE ON "remindersLists"
216-
FOR EACH ROW BEGIN
217-
INSERT INTO "remindersLists"
218-
("title", "color")
219-
SELECT 'Personal', \(raw: 0x4a99ef)
220-
WHERE (SELECT count(*) FROM "remindersLists") = 0;
221-
END
222-
"""
223-
)
202+
try RemindersList.createTemporaryTrigger(after: .delete { _ in
203+
RemindersList.insert {
204+
RemindersList.Draft(
205+
color: RemindersList.defaultColor,
206+
title: RemindersList.defaultTitle
207+
)
208+
}
209+
} when: { _ in
210+
!RemindersList.exists()
211+
})
224212
.execute(db)
225213
}
226214

Examples/RemindersTests/SearchRemindersTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension BaseTestSuite {
2626
model.searchText = "Take"
2727
try await model.$reminders.load()
2828
try await model.$completedCount.load()
29-
try await Task.sleep(for: .seconds(0.1))
29+
try await Task.sleep(for: .seconds(0.5))
3030
#expect(model.completedCount == 1)
3131
assertInlineSnapshot(of: model.reminders, as: .customDump) {
3232
"""

0 commit comments

Comments
 (0)