Skip to content

Commit e511890

Browse files
Let On_ASSEMBLE_INSERT / ON_ASSEMBLE_UPDATE events handle the changed_at column
1 parent c677b3c commit e511890

File tree

10 files changed

+75
-102
lines changed

10 files changed

+75
-102
lines changed

application/forms/ChannelForm.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ public function addChannel(): void
200200
$channel = $this->getValues();
201201

202202
$channel['config'] = json_encode($this->filterConfig($channel['config']));
203-
$channel['changed_at'] = time() * 1000;
204203

205204
$this->db->insert('channel', $channel);
206205
}
@@ -221,8 +220,6 @@ public function editChannel(): void
221220
$storedValues['config'] = json_encode($this->filterConfig($storedValues['config']));
222221

223222
if (! empty(array_diff_assoc($channel, $storedValues))) {
224-
$channel['changed_at'] = time() * 1000;
225-
226223
$this->db->update('channel', $channel, ['id = ?' => $this->channelId]);
227224
}
228225

@@ -234,11 +231,7 @@ public function editChannel(): void
234231
*/
235232
public function removeChannel(): void
236233
{
237-
$this->db->update(
238-
'channel',
239-
['changed_at' => time() * 1000, 'deleted' => 'y'],
240-
['id = ?' => $this->channelId]
241-
);
234+
$this->db->update('channel', ['deleted' => 'y'], ['id = ?' => $this->channelId]);
242235
}
243236

244237
/**

application/forms/ContactGroupForm.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ public function addGroup(): int
178178

179179
$this->db->beginTransaction();
180180

181-
$changedAt = time() * 1000;
182-
$this->db->insert('contactgroup', ['name' => trim($data['group_name']), 'changed_at' => $changedAt]);
181+
$this->db->insert('contactgroup', ['name' => trim($data['group_name'])]);
183182

184183
$groupIdentifier = $this->db->lastInsertId();
185184

@@ -193,8 +192,7 @@ public function addGroup(): int
193192
'contactgroup_member',
194193
[
195194
'contactgroup_id' => $groupIdentifier,
196-
'contact_id' => $contactId,
197-
'changed_at' => $changedAt
195+
'contact_id' => $contactId
198196
]
199197
);
200198
}
@@ -217,13 +215,8 @@ public function editGroup(): void
217215

218216
$storedValues = $this->fetchDbValues();
219217

220-
$changedAt = time() * 1000;
221218
if ($values['group_name'] !== $storedValues['group_name']) {
222-
$this->db->update(
223-
'contactgroup',
224-
['name' => $values['group_name'], 'changed_at' => $changedAt],
225-
['id = ?' => $this->contactgroupId]
226-
);
219+
$this->db->update('contactgroup', ['name' => $values['group_name']], ['id = ?' => $this->contactgroupId]);
227220
}
228221

229222
$storedContacts = [];
@@ -242,7 +235,7 @@ public function editGroup(): void
242235
if (! empty($toDelete)) {
243236
$this->db->update(
244237
'contactgroup_member',
245-
['changed_at' => $changedAt, 'deleted' => 'y'],
238+
['deleted' => 'y'],
246239
[
247240
'contactgroup_id = ?' => $this->contactgroupId,
248241
'contact_id IN (?)' => $toDelete,
@@ -269,16 +262,15 @@ public function editGroup(): void
269262
'contactgroup_member',
270263
[
271264
'contactgroup_id' => $this->contactgroupId,
272-
'contact_id' => $contactId,
273-
'changed_at' => $changedAt
265+
'contact_id' => $contactId
274266
]
275267
);
276268
}
277269

278270
if (! empty($contactsMarkedAsDeleted)) {
279271
$this->db->update(
280272
'contactgroup_member',
281-
['changed_at' => $changedAt, 'deleted' => 'n'],
273+
['deleted' => 'n'],
282274
[
283275
'contactgroup_id = ?' => $this->contactgroupId,
284276
'contact_id IN (?)' => $contactsMarkedAsDeleted
@@ -297,7 +289,7 @@ public function removeContactgroup(): void
297289
{
298290
$this->db->beginTransaction();
299291

300-
$markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y'];
292+
$markAsDeleted = ['deleted' => 'y'];
301293
$updateCondition = ['contactgroup_id = ?' => $this->contactgroupId, 'deleted = ?' => 'n'];
302294

303295
$rotationAndMemberIds = $this->db->fetchPairs(

application/forms/MoveRotationForm.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ protected function onSuccess()
100100

101101
$this->scheduleId = $rotation->schedule_id;
102102

103-
$changedAt = time() * 1000;
104103
// Free up the current priority used by the rotation in question
105104
$this->db->update('rotation', ['priority' => null, 'deleted' => 'y'], ['id = ?' => $rotationId]);
106105

@@ -120,7 +119,7 @@ protected function onSuccess()
120119
foreach ($affectedRotations as $rotation) {
121120
$this->db->update(
122121
'rotation',
123-
['priority' => new Expression('priority + 1'), 'changed_at' => $changedAt],
122+
['priority' => new Expression('priority + 1')],
124123
['id = ?' => $rotation->id]
125124
);
126125
}
@@ -139,7 +138,7 @@ protected function onSuccess()
139138
foreach ($affectedRotations as $rotation) {
140139
$this->db->update(
141140
'rotation',
142-
['priority' => new Expression('priority - 1'), 'changed_at' => $changedAt],
141+
['priority' => new Expression('priority - 1')],
143142
['id = ?' => $rotation->id]
144143
);
145144
}
@@ -148,7 +147,7 @@ protected function onSuccess()
148147
// Now insert the rotation at the new priority
149148
$this->db->update(
150149
'rotation',
151-
['priority' => $newPriority, 'changed_at' => $changedAt, 'deleted' => 'n'],
150+
['priority' => $newPriority, 'deleted' => 'n'],
152151
['id = ?' => $rotationId]
153152
);
154153

application/forms/RotationConfigForm.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,10 @@ private function createRotation(int $priority): Generator
310310
$data['actual_handoff'] = $firstHandoff->format('U.u') * 1000.0;
311311
}
312312

313-
$changedAt = time() * 1000;
314-
$data['changed_at'] = $changedAt;
315313
$this->db->insert('rotation', $data);
316314
$rotationId = $this->db->lastInsertId();
317315

318-
$this->db->insert('timeperiod', ['owned_by_rotation_id' => $rotationId, 'changed_at' => $changedAt]);
316+
$this->db->insert('timeperiod', ['owned_by_rotation_id' => $rotationId]);
319317
$timeperiodId = $this->db->lastInsertId();
320318

321319
$knownMembers = [];
@@ -332,15 +330,13 @@ private function createRotation(int $priority): Generator
332330
$this->db->insert('rotation_member', [
333331
'rotation_id' => $rotationId,
334332
'contact_id' => $id,
335-
'position' => $position,
336-
'changed_at' => $changedAt
333+
'position' => $position
337334
]);
338335
} elseif ($type === 'group') {
339336
$this->db->insert('rotation_member', [
340337
'rotation_id' => $rotationId,
341338
'contactgroup_id' => $id,
342-
'position' => $position,
343-
'changed_at' => $changedAt
339+
'position' => $position
344340
]);
345341
}
346342

@@ -363,8 +359,7 @@ private function createRotation(int $priority): Generator
363359
'end_time' => $endTime,
364360
'until_time' => $untilTime,
365361
'timezone' => $rrule->getStartDate()->getTimezone()->getName(),
366-
'rrule' => $rrule->getString(Rule::TZ_FIXED),
367-
'changed_at' => $changedAt
362+
'rrule' => $rrule->getString(Rule::TZ_FIXED)
368363
]);
369364
}
370365
}
@@ -420,14 +415,13 @@ public function editRotation(int $rotationId): void
420415
$createStmt = $this->createRotation((int) $priority);
421416

422417
$allEntriesRemoved = true;
423-
$changedAt = time() * 1000;
424-
$markAsDeleted = ['changed_at' => $changedAt, 'deleted' => 'y'];
418+
$markAsDeleted = ['deleted' => 'y'];
425419
if (self::EXPERIMENTAL_OVERRIDES) {
426420
// We only show a single name, even in case of multiple versions of a rotation.
427421
// To avoid confusion, we update all versions upon change of the name
428422
$this->db->update(
429423
'rotation',
430-
['name' => $this->getValue('name'), 'changed_at' => $changedAt],
424+
['name' => $this->getValue('name')],
431425
['schedule_id = ?' => $this->scheduleId, 'priority = ?' => $priority]
432426
);
433427

@@ -453,8 +447,7 @@ public function editRotation(int $rotationId): void
453447
'start_time' => $gapStart->format('U.u') * 1000.0,
454448
'end_time' => $gapEnd->format('U.u') * 1000.0,
455449
'until_time' => $gapEnd->format('U.u') * 1000.0,
456-
'timezone' => $gapStart->getTimezone()->getName(),
457-
'changed_at' => $changedAt
450+
'timezone' => $gapStart->getTimezone()->getName()
458451
]);
459452
}
460453

@@ -470,8 +463,7 @@ public function editRotation(int $rotationId): void
470463
$allEntriesRemoved = false;
471464
$this->db->update('timeperiod_entry', [
472465
'until_time' => $lastShiftEnd->format('U.u') * 1000.0,
473-
'rrule' => $rrule->setUntil($lastHandoff)->getString(Rule::TZ_FIXED),
474-
'changed_at' => $changedAt
466+
'rrule' => $rrule->setUntil($lastHandoff)->getString(Rule::TZ_FIXED)
475467
], ['id = ?' => $timeperiodEntry->id]);
476468
}
477469
}

application/forms/SaveEventRuleForm.php

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,10 @@ public function addRule(array $config): int
212212

213213
$db->beginTransaction();
214214

215-
$changedAt = time() * 1000;
216215
$db->insert('rule', [
217216
'name' => $config['name'],
218217
'timeperiod_id' => $config['timeperiod_id'] ?? null,
219-
'object_filter' => $config['object_filter'] ?? null,
220-
'changed_at' => $changedAt
218+
'object_filter' => $config['object_filter'] ?? null
221219
]);
222220
$ruleId = $db->lastInsertId();
223221

@@ -227,16 +225,14 @@ public function addRule(array $config): int
227225
'position' => $position,
228226
'condition' => $escalationConfig['condition'] ?? null,
229227
'name' => $escalationConfig['name'] ?? null,
230-
'fallback_for' => $escalationConfig['fallback_for'] ?? null,
231-
'changed_at' => $changedAt
228+
'fallback_for' => $escalationConfig['fallback_for'] ?? null
232229
]);
233230
$escalationId = $db->lastInsertId();
234231

235232
foreach ($escalationConfig['recipient'] ?? [] as $recipientConfig) {
236233
$data = [
237234
'rule_escalation_id' => $escalationId,
238-
'channel_id' => $recipientConfig['channel_id'],
239-
'changed_at' => $changedAt
235+
'channel_id' => $recipientConfig['channel_id']
240236
];
241237

242238
switch (true) {
@@ -272,16 +268,14 @@ public function addRule(array $config): int
272268
*/
273269
private function insertOrUpdateEscalations($ruleId, array $escalations, Connection $db, bool $insert = false): void
274270
{
275-
$changedAt = time() * 1000;
276271
foreach ($escalations as $position => $escalationConfig) {
277272
if ($insert) {
278273
$db->insert('rule_escalation', [
279274
'rule_id' => $ruleId,
280275
'position' => $position,
281276
'condition' => $escalationConfig['condition'] ?? null,
282277
'name' => $escalationConfig['name'] ?? null,
283-
'fallback_for' => $escalationConfig['fallback_for'] ?? null,
284-
'changed_at' => $changedAt
278+
'fallback_for' => $escalationConfig['fallback_for'] ?? null
285279
]);
286280

287281
$escalationId = $db->lastInsertId();
@@ -291,8 +285,7 @@ private function insertOrUpdateEscalations($ruleId, array $escalations, Connecti
291285
'position' => $position,
292286
'condition' => $escalationConfig['condition'] ?? null,
293287
'name' => $escalationConfig['name'] ?? null,
294-
'fallback_for' => $escalationConfig['fallback_for'] ?? null,
295-
'changed_at' => $changedAt
288+
'fallback_for' => $escalationConfig['fallback_for'] ?? null
296289
], ['id = ?' => $escalationId, 'rule_id = ?' => $ruleId]);
297290
$recipientsToRemove = [];
298291

@@ -318,7 +311,7 @@ function (array $element) use ($recipientId) {
318311
if (! empty($recipientsToRemove)) {
319312
$db->update(
320313
'rule_escalation_recipient',
321-
['changed_at' => $changedAt, 'deleted' => 'y'],
314+
['deleted' => 'y'],
322315
['id IN (?)' => $recipientsToRemove, 'deleted = ?' => 'n']
323316
);
324317
}
@@ -327,8 +320,7 @@ function (array $element) use ($recipientId) {
327320
foreach ($escalationConfig['recipient'] ?? [] as $recipientConfig) {
328321
$data = [
329322
'rule_escalation_id' => $escalationId,
330-
'channel_id' => $recipientConfig['channel_id'],
331-
'changed_at' => $changedAt
323+
'channel_id' => $recipientConfig['channel_id']
332324
];
333325

334326
switch (true) {
@@ -352,11 +344,7 @@ function (array $element) use ($recipientId) {
352344
if (! isset($recipientConfig['id'])) {
353345
$db->insert('rule_escalation_recipient', $data);
354346
} else {
355-
$db->update(
356-
'rule_escalation_recipient',
357-
$data + ['changed_at' => $changedAt],
358-
['id = ?' => $recipientConfig['id']]
359-
);
347+
$db->update('rule_escalation_recipient', $data, ['id = ?' => $recipientConfig['id']]);
360348
}
361349
}
362350
}
@@ -390,9 +378,8 @@ public function editRule(int $id, array $config): void
390378
$data['object_filter'] = $values['object_filter'];
391379
}
392380

393-
$changedAt = time() * 1000;
394381
if (! empty($data)) {
395-
$db->update('rule', $data + ['changed_at' => $changedAt], ['id = ?' => $id]);
382+
$db->update('rule', $data, ['id = ?' => $id]);
396383
}
397384

398385
if (! isset($values['rule_escalation'])) {
@@ -426,7 +413,7 @@ public function editRule(int $id, array $config): void
426413
// Escalations to add
427414
$escalationsToAdd = $escalationsInCache;
428415

429-
$markAsDeleted = ['changed_at' => $changedAt, 'deleted' => 'y'];
416+
$markAsDeleted = ['deleted' => 'y'];
430417
if (! empty($escalationsToRemove)) {
431418
$db->update(
432419
'rule_escalation_recipient',
@@ -472,7 +459,7 @@ public function removeRule(int $id): void
472459
->assembleSelect()
473460
);
474461

475-
$markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y'];
462+
$markAsDeleted = ['deleted' => 'y'];
476463
if (! empty($escalationsToRemove)) {
477464
$db->update(
478465
'rule_escalation_recipient',

application/forms/ScheduleForm.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ public function loadSchedule(int $id): void
7171

7272
public function addSchedule(): int
7373
{
74-
$this->db->insert('schedule', [
75-
'name' => $this->getValue('name'),
76-
'changed_at' => time() * 1000
77-
]);
74+
$this->db->insert('schedule', ['name' => $this->getValue('name')]);
7875

7976
return $this->db->lastInsertId();
8077
}
@@ -90,10 +87,7 @@ public function editSchedule(int $id): void
9087
return;
9188
}
9289

93-
$this->db->update('schedule', [
94-
'name' => $values['name'],
95-
'changed_at' => time() * 1000
96-
], ['id = ?' => $id]);
90+
$this->db->update('schedule', ['name' => $values['name']], ['id = ?' => $id]);
9791

9892
$this->db->commitTransaction();
9993
}
@@ -112,7 +106,7 @@ public function removeSchedule(int $id): void
112106
$rotation->delete();
113107
}
114108

115-
$markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y'];
109+
$markAsDeleted = ['deleted' => 'y'];
116110

117111
$escalationIds = $this->db->fetchCol(
118112
RuleEscalationRecipient::on($this->db)

0 commit comments

Comments
 (0)