Skip to content

Commit d8812be

Browse files
committed
Add datetime customization to createTemporaryTrigger
Adds support for providing a customized expression that will be used when creating a temporary trigger.
1 parent 3616df2 commit d8812be

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Sources/StructuredQueriesCore/Triggers.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ extension Table {
128128
/// operation, and source location.
129129
/// - ifNotExists: Adds an `IF NOT EXISTS` clause to the `CREATE TRIGGER` statement.
130130
/// - date: A key path to a datetime column.
131+
/// - expression: The expression used to generate the datetime.
131132
/// - fileID: The source `#fileID` associated with the trigger.
132133
/// - line: The source `#line` associated with the trigger.
133134
/// - column: The source `#column` associated with the trigger.
@@ -136,6 +137,7 @@ extension Table {
136137
_ name: String? = nil,
137138
ifNotExists: Bool = false,
138139
afterUpdateTouch date: KeyPath<TableColumns, TableColumn<Self, D>>,
140+
expression: String = "datetime('subsec')",
139141
fileID: StaticString = #fileID,
140142
line: UInt = #line,
141143
column: UInt = #column
@@ -144,7 +146,7 @@ extension Table {
144146
name,
145147
ifNotExists: ifNotExists,
146148
afterUpdateTouch: {
147-
$0[dynamicMember: date] = SQLQueryExpression("datetime('subsec')")
149+
$0[dynamicMember: date] = SQLQueryExpression("\(raw: expression)")
148150
},
149151
fileID: fileID,
150152
line: line,

Tests/StructuredQueriesTests/TriggersTests.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ extension SnapshotTests {
115115
}
116116
}
117117

118+
@Test func afterUpdateTouchCustomDate() {
119+
assertQuery(
120+
Reminder.createTemporaryTrigger(afterUpdateTouch: \.updatedAt, expression: "customDate()")
121+
) {
122+
"""
123+
CREATE TEMPORARY TRIGGER
124+
"after_update_on_reminders@StructuredQueriesTests/TriggersTests.swift:120:40"
125+
AFTER UPDATE ON "reminders"
126+
FOR EACH ROW BEGIN
127+
UPDATE "reminders"
128+
SET "updatedAt" = customDate()
129+
WHERE ("reminders"."rowid" = "new"."rowid");
130+
END
131+
"""
132+
}
133+
}
134+
118135
@Test func multiStatement() {
119136
let trigger = RemindersList.createTemporaryTrigger(
120137
after: .insert { new in
@@ -133,7 +150,7 @@ extension SnapshotTests {
133150
assertQuery(trigger) {
134151
"""
135152
CREATE TEMPORARY TRIGGER
136-
"after_insert_on_remindersLists@StructuredQueriesTests/TriggersTests.swift:119:57"
153+
"after_insert_on_remindersLists@StructuredQueriesTests/TriggersTests.swift:136:57"
137154
AFTER INSERT ON "remindersLists"
138155
FOR EACH ROW BEGIN
139156
UPDATE "remindersLists"

0 commit comments

Comments
 (0)