From e511890ec56d060bf7f93e4c0f17260daa237ca7 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 12 Jul 2024 11:13:45 +0200 Subject: [PATCH 1/3] Let On_ASSEMBLE_INSERT / ON_ASSEMBLE_UPDATE events handle the changed_at column --- application/forms/ChannelForm.php | 9 +---- application/forms/ContactGroupForm.php | 22 ++++-------- application/forms/MoveRotationForm.php | 7 ++-- application/forms/RotationConfigForm.php | 24 +++++-------- application/forms/SaveEventRuleForm.php | 35 ++++++------------- application/forms/ScheduleForm.php | 12 ++----- application/forms/SourceForm.php | 9 +---- library/Notifications/Common/Database.php | 32 +++++++++++++++++ library/Notifications/Model/Rotation.php | 5 ++- .../Notifications/Web/Form/ContactForm.php | 22 ++++-------- 10 files changed, 75 insertions(+), 102 deletions(-) diff --git a/application/forms/ChannelForm.php b/application/forms/ChannelForm.php index f710de4a..19aebb67 100644 --- a/application/forms/ChannelForm.php +++ b/application/forms/ChannelForm.php @@ -200,7 +200,6 @@ public function addChannel(): void $channel = $this->getValues(); $channel['config'] = json_encode($this->filterConfig($channel['config'])); - $channel['changed_at'] = time() * 1000; $this->db->insert('channel', $channel); } @@ -221,8 +220,6 @@ public function editChannel(): void $storedValues['config'] = json_encode($this->filterConfig($storedValues['config'])); if (! empty(array_diff_assoc($channel, $storedValues))) { - $channel['changed_at'] = time() * 1000; - $this->db->update('channel', $channel, ['id = ?' => $this->channelId]); } @@ -234,11 +231,7 @@ public function editChannel(): void */ public function removeChannel(): void { - $this->db->update( - 'channel', - ['changed_at' => time() * 1000, 'deleted' => 'y'], - ['id = ?' => $this->channelId] - ); + $this->db->update('channel', ['deleted' => 'y'], ['id = ?' => $this->channelId]); } /** diff --git a/application/forms/ContactGroupForm.php b/application/forms/ContactGroupForm.php index d7d63ed8..d61025dc 100644 --- a/application/forms/ContactGroupForm.php +++ b/application/forms/ContactGroupForm.php @@ -178,8 +178,7 @@ public function addGroup(): int $this->db->beginTransaction(); - $changedAt = time() * 1000; - $this->db->insert('contactgroup', ['name' => trim($data['group_name']), 'changed_at' => $changedAt]); + $this->db->insert('contactgroup', ['name' => trim($data['group_name'])]); $groupIdentifier = $this->db->lastInsertId(); @@ -193,8 +192,7 @@ public function addGroup(): int 'contactgroup_member', [ 'contactgroup_id' => $groupIdentifier, - 'contact_id' => $contactId, - 'changed_at' => $changedAt + 'contact_id' => $contactId ] ); } @@ -217,13 +215,8 @@ public function editGroup(): void $storedValues = $this->fetchDbValues(); - $changedAt = time() * 1000; if ($values['group_name'] !== $storedValues['group_name']) { - $this->db->update( - 'contactgroup', - ['name' => $values['group_name'], 'changed_at' => $changedAt], - ['id = ?' => $this->contactgroupId] - ); + $this->db->update('contactgroup', ['name' => $values['group_name']], ['id = ?' => $this->contactgroupId]); } $storedContacts = []; @@ -242,7 +235,7 @@ public function editGroup(): void if (! empty($toDelete)) { $this->db->update( 'contactgroup_member', - ['changed_at' => $changedAt, 'deleted' => 'y'], + ['deleted' => 'y'], [ 'contactgroup_id = ?' => $this->contactgroupId, 'contact_id IN (?)' => $toDelete, @@ -269,8 +262,7 @@ public function editGroup(): void 'contactgroup_member', [ 'contactgroup_id' => $this->contactgroupId, - 'contact_id' => $contactId, - 'changed_at' => $changedAt + 'contact_id' => $contactId ] ); } @@ -278,7 +270,7 @@ public function editGroup(): void if (! empty($contactsMarkedAsDeleted)) { $this->db->update( 'contactgroup_member', - ['changed_at' => $changedAt, 'deleted' => 'n'], + ['deleted' => 'n'], [ 'contactgroup_id = ?' => $this->contactgroupId, 'contact_id IN (?)' => $contactsMarkedAsDeleted @@ -297,7 +289,7 @@ public function removeContactgroup(): void { $this->db->beginTransaction(); - $markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y']; + $markAsDeleted = ['deleted' => 'y']; $updateCondition = ['contactgroup_id = ?' => $this->contactgroupId, 'deleted = ?' => 'n']; $rotationAndMemberIds = $this->db->fetchPairs( diff --git a/application/forms/MoveRotationForm.php b/application/forms/MoveRotationForm.php index 24b9ae73..679ba835 100644 --- a/application/forms/MoveRotationForm.php +++ b/application/forms/MoveRotationForm.php @@ -100,7 +100,6 @@ protected function onSuccess() $this->scheduleId = $rotation->schedule_id; - $changedAt = time() * 1000; // Free up the current priority used by the rotation in question $this->db->update('rotation', ['priority' => null, 'deleted' => 'y'], ['id = ?' => $rotationId]); @@ -120,7 +119,7 @@ protected function onSuccess() foreach ($affectedRotations as $rotation) { $this->db->update( 'rotation', - ['priority' => new Expression('priority + 1'), 'changed_at' => $changedAt], + ['priority' => new Expression('priority + 1')], ['id = ?' => $rotation->id] ); } @@ -139,7 +138,7 @@ protected function onSuccess() foreach ($affectedRotations as $rotation) { $this->db->update( 'rotation', - ['priority' => new Expression('priority - 1'), 'changed_at' => $changedAt], + ['priority' => new Expression('priority - 1')], ['id = ?' => $rotation->id] ); } @@ -148,7 +147,7 @@ protected function onSuccess() // Now insert the rotation at the new priority $this->db->update( 'rotation', - ['priority' => $newPriority, 'changed_at' => $changedAt, 'deleted' => 'n'], + ['priority' => $newPriority, 'deleted' => 'n'], ['id = ?' => $rotationId] ); diff --git a/application/forms/RotationConfigForm.php b/application/forms/RotationConfigForm.php index 8b1f2fb9..f3289dc7 100644 --- a/application/forms/RotationConfigForm.php +++ b/application/forms/RotationConfigForm.php @@ -310,12 +310,10 @@ private function createRotation(int $priority): Generator $data['actual_handoff'] = $firstHandoff->format('U.u') * 1000.0; } - $changedAt = time() * 1000; - $data['changed_at'] = $changedAt; $this->db->insert('rotation', $data); $rotationId = $this->db->lastInsertId(); - $this->db->insert('timeperiod', ['owned_by_rotation_id' => $rotationId, 'changed_at' => $changedAt]); + $this->db->insert('timeperiod', ['owned_by_rotation_id' => $rotationId]); $timeperiodId = $this->db->lastInsertId(); $knownMembers = []; @@ -332,15 +330,13 @@ private function createRotation(int $priority): Generator $this->db->insert('rotation_member', [ 'rotation_id' => $rotationId, 'contact_id' => $id, - 'position' => $position, - 'changed_at' => $changedAt + 'position' => $position ]); } elseif ($type === 'group') { $this->db->insert('rotation_member', [ 'rotation_id' => $rotationId, 'contactgroup_id' => $id, - 'position' => $position, - 'changed_at' => $changedAt + 'position' => $position ]); } @@ -363,8 +359,7 @@ private function createRotation(int $priority): Generator 'end_time' => $endTime, 'until_time' => $untilTime, 'timezone' => $rrule->getStartDate()->getTimezone()->getName(), - 'rrule' => $rrule->getString(Rule::TZ_FIXED), - 'changed_at' => $changedAt + 'rrule' => $rrule->getString(Rule::TZ_FIXED) ]); } } @@ -420,14 +415,13 @@ public function editRotation(int $rotationId): void $createStmt = $this->createRotation((int) $priority); $allEntriesRemoved = true; - $changedAt = time() * 1000; - $markAsDeleted = ['changed_at' => $changedAt, 'deleted' => 'y']; + $markAsDeleted = ['deleted' => 'y']; if (self::EXPERIMENTAL_OVERRIDES) { // We only show a single name, even in case of multiple versions of a rotation. // To avoid confusion, we update all versions upon change of the name $this->db->update( 'rotation', - ['name' => $this->getValue('name'), 'changed_at' => $changedAt], + ['name' => $this->getValue('name')], ['schedule_id = ?' => $this->scheduleId, 'priority = ?' => $priority] ); @@ -453,8 +447,7 @@ public function editRotation(int $rotationId): void 'start_time' => $gapStart->format('U.u') * 1000.0, 'end_time' => $gapEnd->format('U.u') * 1000.0, 'until_time' => $gapEnd->format('U.u') * 1000.0, - 'timezone' => $gapStart->getTimezone()->getName(), - 'changed_at' => $changedAt + 'timezone' => $gapStart->getTimezone()->getName() ]); } @@ -470,8 +463,7 @@ public function editRotation(int $rotationId): void $allEntriesRemoved = false; $this->db->update('timeperiod_entry', [ 'until_time' => $lastShiftEnd->format('U.u') * 1000.0, - 'rrule' => $rrule->setUntil($lastHandoff)->getString(Rule::TZ_FIXED), - 'changed_at' => $changedAt + 'rrule' => $rrule->setUntil($lastHandoff)->getString(Rule::TZ_FIXED) ], ['id = ?' => $timeperiodEntry->id]); } } diff --git a/application/forms/SaveEventRuleForm.php b/application/forms/SaveEventRuleForm.php index f19f25b1..a6f24965 100644 --- a/application/forms/SaveEventRuleForm.php +++ b/application/forms/SaveEventRuleForm.php @@ -212,12 +212,10 @@ public function addRule(array $config): int $db->beginTransaction(); - $changedAt = time() * 1000; $db->insert('rule', [ 'name' => $config['name'], 'timeperiod_id' => $config['timeperiod_id'] ?? null, - 'object_filter' => $config['object_filter'] ?? null, - 'changed_at' => $changedAt + 'object_filter' => $config['object_filter'] ?? null ]); $ruleId = $db->lastInsertId(); @@ -227,16 +225,14 @@ public function addRule(array $config): int 'position' => $position, 'condition' => $escalationConfig['condition'] ?? null, 'name' => $escalationConfig['name'] ?? null, - 'fallback_for' => $escalationConfig['fallback_for'] ?? null, - 'changed_at' => $changedAt + 'fallback_for' => $escalationConfig['fallback_for'] ?? null ]); $escalationId = $db->lastInsertId(); foreach ($escalationConfig['recipient'] ?? [] as $recipientConfig) { $data = [ 'rule_escalation_id' => $escalationId, - 'channel_id' => $recipientConfig['channel_id'], - 'changed_at' => $changedAt + 'channel_id' => $recipientConfig['channel_id'] ]; switch (true) { @@ -272,7 +268,6 @@ public function addRule(array $config): int */ private function insertOrUpdateEscalations($ruleId, array $escalations, Connection $db, bool $insert = false): void { - $changedAt = time() * 1000; foreach ($escalations as $position => $escalationConfig) { if ($insert) { $db->insert('rule_escalation', [ @@ -280,8 +275,7 @@ private function insertOrUpdateEscalations($ruleId, array $escalations, Connecti 'position' => $position, 'condition' => $escalationConfig['condition'] ?? null, 'name' => $escalationConfig['name'] ?? null, - 'fallback_for' => $escalationConfig['fallback_for'] ?? null, - 'changed_at' => $changedAt + 'fallback_for' => $escalationConfig['fallback_for'] ?? null ]); $escalationId = $db->lastInsertId(); @@ -291,8 +285,7 @@ private function insertOrUpdateEscalations($ruleId, array $escalations, Connecti 'position' => $position, 'condition' => $escalationConfig['condition'] ?? null, 'name' => $escalationConfig['name'] ?? null, - 'fallback_for' => $escalationConfig['fallback_for'] ?? null, - 'changed_at' => $changedAt + 'fallback_for' => $escalationConfig['fallback_for'] ?? null ], ['id = ?' => $escalationId, 'rule_id = ?' => $ruleId]); $recipientsToRemove = []; @@ -318,7 +311,7 @@ function (array $element) use ($recipientId) { if (! empty($recipientsToRemove)) { $db->update( 'rule_escalation_recipient', - ['changed_at' => $changedAt, 'deleted' => 'y'], + ['deleted' => 'y'], ['id IN (?)' => $recipientsToRemove, 'deleted = ?' => 'n'] ); } @@ -327,8 +320,7 @@ function (array $element) use ($recipientId) { foreach ($escalationConfig['recipient'] ?? [] as $recipientConfig) { $data = [ 'rule_escalation_id' => $escalationId, - 'channel_id' => $recipientConfig['channel_id'], - 'changed_at' => $changedAt + 'channel_id' => $recipientConfig['channel_id'] ]; switch (true) { @@ -352,11 +344,7 @@ function (array $element) use ($recipientId) { if (! isset($recipientConfig['id'])) { $db->insert('rule_escalation_recipient', $data); } else { - $db->update( - 'rule_escalation_recipient', - $data + ['changed_at' => $changedAt], - ['id = ?' => $recipientConfig['id']] - ); + $db->update('rule_escalation_recipient', $data, ['id = ?' => $recipientConfig['id']]); } } } @@ -390,9 +378,8 @@ public function editRule(int $id, array $config): void $data['object_filter'] = $values['object_filter']; } - $changedAt = time() * 1000; if (! empty($data)) { - $db->update('rule', $data + ['changed_at' => $changedAt], ['id = ?' => $id]); + $db->update('rule', $data, ['id = ?' => $id]); } if (! isset($values['rule_escalation'])) { @@ -426,7 +413,7 @@ public function editRule(int $id, array $config): void // Escalations to add $escalationsToAdd = $escalationsInCache; - $markAsDeleted = ['changed_at' => $changedAt, 'deleted' => 'y']; + $markAsDeleted = ['deleted' => 'y']; if (! empty($escalationsToRemove)) { $db->update( 'rule_escalation_recipient', @@ -472,7 +459,7 @@ public function removeRule(int $id): void ->assembleSelect() ); - $markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y']; + $markAsDeleted = ['deleted' => 'y']; if (! empty($escalationsToRemove)) { $db->update( 'rule_escalation_recipient', diff --git a/application/forms/ScheduleForm.php b/application/forms/ScheduleForm.php index 5d9b1f4b..89add1f3 100644 --- a/application/forms/ScheduleForm.php +++ b/application/forms/ScheduleForm.php @@ -71,10 +71,7 @@ public function loadSchedule(int $id): void public function addSchedule(): int { - $this->db->insert('schedule', [ - 'name' => $this->getValue('name'), - 'changed_at' => time() * 1000 - ]); + $this->db->insert('schedule', ['name' => $this->getValue('name')]); return $this->db->lastInsertId(); } @@ -90,10 +87,7 @@ public function editSchedule(int $id): void return; } - $this->db->update('schedule', [ - 'name' => $values['name'], - 'changed_at' => time() * 1000 - ], ['id = ?' => $id]); + $this->db->update('schedule', ['name' => $values['name']], ['id = ?' => $id]); $this->db->commitTransaction(); } @@ -112,7 +106,7 @@ public function removeSchedule(int $id): void $rotation->delete(); } - $markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y']; + $markAsDeleted = ['deleted' => 'y']; $escalationIds = $this->db->fetchCol( RuleEscalationRecipient::on($this->db) diff --git a/application/forms/SourceForm.php b/application/forms/SourceForm.php index ca8f9ee0..021f8145 100644 --- a/application/forms/SourceForm.php +++ b/application/forms/SourceForm.php @@ -294,8 +294,6 @@ public function addSource(): void self::HASH_ALGORITHM ); - $source['changed_at'] = time() * 1000; - $this->db->insert('source', $source); } @@ -322,7 +320,6 @@ public function editSource(): void $source['listener_password_hash'] = password_hash($listenerPassword, self::HASH_ALGORITHM); } - $source['changed_at'] = time() * 1000; $this->db->update('source', $source, ['id = ?' => $this->sourceId]); $this->db->commitTransaction(); @@ -333,11 +330,7 @@ public function editSource(): void */ public function removeSource(): void { - $this->db->update( - 'source', - ['changed_at' => time() * 1000, 'deleted' => 'y'], - ['id = ?' => $this->sourceId] - ); + $this->db->update('source', ['deleted' => 'y'], ['id = ?' => $this->sourceId]); } /** diff --git a/library/Notifications/Common/Database.php b/library/Notifications/Common/Database.php index abfefa50..fb7b48ec 100644 --- a/library/Notifications/Common/Database.php +++ b/library/Notifications/Common/Database.php @@ -12,8 +12,10 @@ use ipl\Sql\Config as SqlConfig; use ipl\Sql\Connection; use ipl\Sql\Expression; +use ipl\Sql\Insert; use ipl\Sql\QueryBuilder; use ipl\Sql\Select; +use ipl\Sql\Update; use PDO; final class Database @@ -40,6 +42,13 @@ final class Database 'timeperiod_entry', ]; + /** + * @var string[] Tables with a `changed_at` column + * + * The `changed_at` column of these tables is automatically added and updated + */ + private const TABLES_WITH_CHANGED_AT_COLUMN = self::TABLES_WITH_DELETED_FLAG; + /** @var Connection Database connection */ private static $instance; @@ -150,6 +159,29 @@ private static function getConnection(): Connection $select->where([$baseTableAlias . '.' . $condition => 'n']); }); + $db->getQueryBuilder()->on(QueryBuilder::ON_ASSEMBLE_INSERT, function (Insert $insert) { + $tableName = $insert->getInto(); + + if (in_array($tableName, self::TABLES_WITH_CHANGED_AT_COLUMN, true)) { + $columns = array_combine($insert->getColumns(), $insert->getValues()); + $columns['changed_at'] = time() * 1000; + + $insert->values($columns); + } + }); + + $db->getQueryBuilder()->on(QueryBuilder::ON_ASSEMBLE_UPDATE, function (Update $update) { + $table = $update->getTable(); + $tableName = reset($table); + + if (in_array($tableName, self::TABLES_WITH_CHANGED_AT_COLUMN, true)) { + $columns = $update->getSet(); + $columns['changed_at'] = time() * 1000; + + $update->set($columns); + } + }); + return $db; } diff --git a/library/Notifications/Model/Rotation.php b/library/Notifications/Model/Rotation.php index 46a3901b..9520495d 100644 --- a/library/Notifications/Model/Rotation.php +++ b/library/Notifications/Model/Rotation.php @@ -124,8 +124,7 @@ public function delete(): void $timeperiodId = $this->timeperiod->columns('id')->first()->id; } - $changedAt = time() * 1000; - $markAsDeleted = ['changed_at' => $changedAt, 'deleted' => 'y']; + $markAsDeleted = ['deleted' => 'y']; $db->update('timeperiod_entry', $markAsDeleted, ['timeperiod_id = ?' => $timeperiodId, 'deleted = ?' => 'n']); $db->update('timeperiod', $markAsDeleted, ['id = ?' => $timeperiodId]); @@ -163,7 +162,7 @@ public function delete(): void foreach ($affectedRotations as $rotation) { $db->update( 'rotation', - ['priority' => new Expression('priority - 1'), 'changed_at' => $changedAt], + ['priority' => new Expression('priority - 1')], ['id = ?' => $rotation->id] ); } diff --git a/library/Notifications/Web/Form/ContactForm.php b/library/Notifications/Web/Form/ContactForm.php index 246f015a..b5da61b4 100644 --- a/library/Notifications/Web/Form/ContactForm.php +++ b/library/Notifications/Web/Form/ContactForm.php @@ -183,17 +183,15 @@ public function loadContact(int $id): self public function addContact(): void { $contactInfo = $this->getValues(); - $changedAt = time() * 1000; $this->db->beginTransaction(); - $this->db->insert('contact', $contactInfo['contact'] + ['changed_at' => $changedAt]); + $this->db->insert('contact', $contactInfo['contact']); $this->contactId = $this->db->lastInsertId(); foreach (array_filter($contactInfo['contact_address']) as $type => $address) { $address = [ 'contact_id' => $this->contactId, 'type' => $type, - 'address' => $address, - 'changed_at' => $changedAt + 'address' => $address ]; $this->db->insert('contact_address', $address); @@ -214,13 +212,8 @@ public function editContact(): void $values = $this->getValues(); $storedValues = $this->fetchDbValues(); - $changedAt = time() * 1000; if ($storedValues['contact'] !== $values['contact']) { - $this->db->update( - 'contact', - $values['contact'] + ['changed_at' => $changedAt], - ['id = ?' => $this->contactId] - ); + $this->db->update('contact', $values['contact'], ['id = ?' => $this->contactId]); } $storedAddresses = $storedValues['contact_address_with_id']; @@ -229,7 +222,7 @@ public function editContact(): void if (isset($storedAddresses[$type])) { $this->db->update( 'contact_address', - ['changed_at' => $changedAt, 'deleted' => 'y'], + ['deleted' => 'y'], ['id = ?' => $storedAddresses[$type][0], 'deleted = ?' => 'n'] ); } @@ -237,15 +230,14 @@ public function editContact(): void $address = [ 'contact_id' => $this->contactId, 'type' => $type, - 'address' => $address, - 'changed_at' => $changedAt + 'address' => $address ]; $this->db->insert('contact_address', $address); } elseif ($storedAddresses[$type][1] !== $address) { $this->db->update( 'contact_address', - ['address' => $address, 'changed_at' => $changedAt], + ['address' => $address], [ 'id = ?' => $storedAddresses[$type][0], 'contact_id = ?' => $this->contactId @@ -264,7 +256,7 @@ public function removeContact(): void { $this->db->beginTransaction(); - $markAsDeleted = ['changed_at' => time() * 1000, 'deleted' => 'y']; + $markAsDeleted = ['deleted' => 'y']; $updateCondition = ['contact_id = ?' => $this->contactId, 'deleted = ?' => 'n']; $rotationAndMemberIds = $this->db->fetchPairs( From b0c890b3f28584fc813ffbd0977ead27879c9286 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 12 Jul 2024 13:18:14 +0200 Subject: [PATCH 2/3] Database: Let db set the `changed_at` column --- library/Notifications/Common/Database.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/library/Notifications/Common/Database.php b/library/Notifications/Common/Database.php index fb7b48ec..dff0e3bd 100644 --- a/library/Notifications/Common/Database.php +++ b/library/Notifications/Common/Database.php @@ -159,24 +159,36 @@ private static function getConnection(): Connection $select->where([$baseTableAlias . '.' . $condition => 'n']); }); - $db->getQueryBuilder()->on(QueryBuilder::ON_ASSEMBLE_INSERT, function (Insert $insert) { + $db->getQueryBuilder()->on(QueryBuilder::ON_ASSEMBLE_INSERT, function (Insert $insert) use ($adapter) { $tableName = $insert->getInto(); if (in_array($tableName, self::TABLES_WITH_CHANGED_AT_COLUMN, true)) { + if ($adapter instanceof Pgsql) { + $changedAt = new Expression('EXTRACT(EPOCH FROM NOW()) * 1000'); + } else { + $changedAt = new Expression('FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000)'); + } + $columns = array_combine($insert->getColumns(), $insert->getValues()); - $columns['changed_at'] = time() * 1000; + $columns['changed_at'] = $changedAt; $insert->values($columns); } }); - $db->getQueryBuilder()->on(QueryBuilder::ON_ASSEMBLE_UPDATE, function (Update $update) { + $db->getQueryBuilder()->on(QueryBuilder::ON_ASSEMBLE_UPDATE, function (Update $update) use ($adapter) { $table = $update->getTable(); $tableName = reset($table); if (in_array($tableName, self::TABLES_WITH_CHANGED_AT_COLUMN, true)) { + if ($adapter instanceof Pgsql) { + $changedAt = new Expression('EXTRACT(EPOCH FROM NOW()) * 1000'); + } else { + $changedAt = new Expression('FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000)'); + } + $columns = $update->getSet(); - $columns['changed_at'] = time() * 1000; + $columns['changed_at'] = $changedAt; $update->set($columns); } From cce7f68eda22ec381a5beb9731c644f29fa4c82c Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 12 Jul 2024 13:25:09 +0200 Subject: [PATCH 3/3] Database: Update phpDoc and code style --- library/Notifications/Common/Database.php | 37 +++++++++++------------ 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/library/Notifications/Common/Database.php b/library/Notifications/Common/Database.php index dff0e3bd..9d4d5a66 100644 --- a/library/Notifications/Common/Database.php +++ b/library/Notifications/Common/Database.php @@ -23,7 +23,7 @@ final class Database /** * @var string[] Tables with a deleted flag * - * The filter `deleted=n` is automatically added to these tables. + * The filter `deleted=n` is automatically added to these tables, when select statement is executed */ private const TABLES_WITH_DELETED_FLAG = [ 'channel', @@ -135,29 +135,28 @@ private static function getConnection(): Connection }); } - $db->getQueryBuilder() - ->on(QueryBuilder::ON_ASSEMBLE_SELECT, function (Select $select) { - $from = $select->getFrom(); - $baseTableName = reset($from); + $db->getQueryBuilder()->on(QueryBuilder::ON_ASSEMBLE_SELECT, function (Select $select) { + $from = $select->getFrom(); + $baseTableName = reset($from); - if (! in_array($baseTableName, self::TABLES_WITH_DELETED_FLAG, true)) { - return; - } + if (! in_array($baseTableName, self::TABLES_WITH_DELETED_FLAG, true)) { + return; + } - $baseTableAlias = key($from); - if (! is_string($baseTableAlias)) { - $baseTableAlias = $baseTableName; - } + $baseTableAlias = key($from); + if (! is_string($baseTableAlias)) { + $baseTableAlias = $baseTableName; + } - $condition = 'deleted = ?'; - $where = $select->getWhere(); + $condition = 'deleted = ?'; + $where = $select->getWhere(); - if ($where && self::hasCondition($baseTableAlias, $condition, $where)) { - return; - } + if ($where && self::hasCondition($baseTableAlias, $condition, $where)) { + return; + } - $select->where([$baseTableAlias . '.' . $condition => 'n']); - }); + $select->where([$baseTableAlias . '.' . $condition => 'n']); + }); $db->getQueryBuilder()->on(QueryBuilder::ON_ASSEMBLE_INSERT, function (Insert $insert) use ($adapter) { $tableName = $insert->getInto();