From db5078d51c7b01a6beb7145c866e38d02b2eb284 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 13 May 2023 21:05:22 +0200 Subject: [PATCH 01/11] Move "OPTIMIZE TABLE" to own Database function --- src/Database/Database.php | 13 +++++++++++++ src/Federation/Repository/DeliveryQueueItem.php | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index b1ea6c1e4..b1e31bda7 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1758,6 +1758,19 @@ class Database return (['list' => $statelist, 'amount' => $processes]); } + /** + * Optimizes tables + * + * @param string $table a given table + * + * @return bool True, if successfully optimized, otherwise false + * @throws \Exception + */ + public function optimizeTable(string $table): bool + { + return $this->e("OPTIMIZE TABLE " . DBA::buildTableString([$table])) !== false; + } + /** * Fetch a database variable * diff --git a/src/Federation/Repository/DeliveryQueueItem.php b/src/Federation/Repository/DeliveryQueueItem.php index 815cf89b5..59afd5a3a 100644 --- a/src/Federation/Repository/DeliveryQueueItem.php +++ b/src/Federation/Repository/DeliveryQueueItem.php @@ -108,6 +108,6 @@ final class DeliveryQueueItem extends \Friendica\BaseRepository public function optimizeStorage(): bool { - return $this->db->e("OPTIMIZE TABLE " . DBA::buildTableString([self::$table_name])); + return $this->db->optimizeTable(self::$table_name); } } From 557d0e3aeb0cddddb6fc063ba7540b31c1dc2f31 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 13 May 2023 22:04:51 +0200 Subject: [PATCH 02/11] Add direct field possibility --- src/Database/Database.php | 12 ++++- src/Database/DatabaseException.php | 19 ++++---- .../Repository/DeliveryQueueItem.php | 14 +++--- tests/datasets/api.fixture.php | 9 ++++ tests/src/Database/DatabaseTest.php | 45 +++++++++++++++++++ 5 files changed, 83 insertions(+), 16 deletions(-) create mode 100644 tests/src/Database/DatabaseTest.php diff --git a/src/Database/Database.php b/src/Database/Database.php index b1e31bda7..f4959bf7e 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1357,6 +1357,15 @@ class Database } $fields = $this->castFields($table, $fields); + $direct_fields = []; + + foreach ($fields as $key => $value) { + if (is_numeric($key)) { + $direct_fields[] = $value; + unset($fields[$key]); + } + } + $table_string = DBA::buildTableString([$table]); @@ -1369,7 +1378,8 @@ class Database } $sql = "UPDATE " . $ignore . $table_string . " SET " - . implode(" = ?, ", array_map([DBA::class, 'quoteIdentifier'], array_keys($fields))) . " = ?" + . ((count($fields) > 0) ? implode(" = ?, ", array_map([DBA::class, 'quoteIdentifier'], array_keys($fields))) . " = ?" : "") + . ((count($direct_fields) > 0) ? ((count($fields) > 0) ? " , " : "") . implode(" , ", $direct_fields) : "") . $condition_string; // Combines the updated fields parameter values with the condition parameter values diff --git a/src/Database/DatabaseException.php b/src/Database/DatabaseException.php index ba1ccfce5..9473e5808 100644 --- a/src/Database/DatabaseException.php +++ b/src/Database/DatabaseException.php @@ -38,22 +38,25 @@ class DatabaseException extends Exception * * @link https://php.net/manual/en/exception.construct.php * - * @param string $message The Database error message. - * @param int $code The Database error code. - * @param string $query The Database error query. - * @param Throwable $previous [optional] The previous throwable used for the exception chaining. + * @param string $message The Database error message. + * @param int $code The Database error code. + * @param string $query The Database error query. + * @param Throwable|null $previous [optional] The previous throwable used for the exception chaining. */ public function __construct(string $message, int $code, string $query, Throwable $previous = null) { - parent::__construct($message, $code, $previous); $this->query = $query; + + parent::__construct(sprintf('"%s" at "%s"', $message, $query) , $code, $previous); } /** - * {@inheritDoc} + * Returns the query, which caused the exception + * + * @return string */ - public function __toString() + public function getQuery(): string { - return sprintf('Database error %d "%s" at "%s"', $this->message, $this->code, $this->query); + return $this->query; } } diff --git a/src/Federation/Repository/DeliveryQueueItem.php b/src/Federation/Repository/DeliveryQueueItem.php index 59afd5a3a..5d5198adf 100644 --- a/src/Federation/Repository/DeliveryQueueItem.php +++ b/src/Federation/Repository/DeliveryQueueItem.php @@ -88,7 +88,8 @@ final class DeliveryQueueItem extends \Friendica\BaseRepository public function remove(Entity\DeliveryQueueItem $deliveryQueueItem): bool { - return $this->db->delete(self::$table_name, ['uri-id' => $deliveryQueueItem->postUriId, 'gsid' => $deliveryQueueItem->targetServerId]); + return $this->db->delete(self::$table_name, ['uri-id' => $deliveryQueueItem->postUriId, + 'gsid' => $deliveryQueueItem->targetServerId]); } public function removeFailedByServerId(int $gsid, int $failedThreshold): bool @@ -98,12 +99,11 @@ final class DeliveryQueueItem extends \Friendica\BaseRepository public function incrementFailed(Entity\DeliveryQueueItem $deliveryQueueItem): bool { - return $this->db->e(" - UPDATE " . DBA::buildTableString([self::$table_name]) . " - SET `failed` = `failed` + 1 - WHERE `uri-id` = ? AND `gsid` = ?", - $deliveryQueueItem->postUriId, $deliveryQueueItem->targetServerId - ); + return $this->db->update(self::$table_name, ["`failed` = `failed` + 1"], + ["`uri-id` = ? AND `gsid` = ?", + $deliveryQueueItem->postUriId, + $deliveryQueueItem->targetServerId + ]); } public function optimizeStorage(): bool diff --git a/tests/datasets/api.fixture.php b/tests/datasets/api.fixture.php index b50f70625..f5b16f9c6 100644 --- a/tests/datasets/api.fixture.php +++ b/tests/datasets/api.fixture.php @@ -35,6 +35,15 @@ return [ 'workerqueue', 'mail', 'post-delivery-data', + 'gserver' => [ + [ + 'url' => 'https://friendica.local', + 'nurl' => 'http://friendica.local', + 'register_policy' => 0, + 'registered-users' => 0, + 'network' => 'unkn', + ], + ], // Base test config to avoid notice messages 'user' => [ [ diff --git a/tests/src/Database/DatabaseTest.php b/tests/src/Database/DatabaseTest.php new file mode 100644 index 000000000..e95d655f1 --- /dev/null +++ b/tests/src/Database/DatabaseTest.php @@ -0,0 +1,45 @@ +setUpVfsDir(); + + parent::setUp(); + + $this->configCache = new Cache(); + $this->configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/'); + } + + public function testUpdateIncrease() + { + $db = $this->getDbInstance(); + + self::assertTrue($db->insert('config', ['cat' => 'test', 'k' => 'inc', 'v' => 0])); + self::assertTrue($db->update('config', ["`v` = `v` + 1"], ['cat' => 'test', 'k' => 'inc'])); + self::assertEquals(1, $db->selectFirst('config', ['v'], ['cat' => 'test', 'k' => 'inc'])['v']); + } + + public function testUpdateWithField() + { + $db = $this->getDbInstance(); + + self::assertEquals('https://friendica.local', $db->selectFirst('gserver', ['url'], ['nurl' => 'http://friendica.local'])['url']); + self::assertTrue($db->update('gserver', ['site_name' => 'test', "`registered-users` = `registered-users` + 1", 'info' => 'another test'], ['nurl' => 'http://friendica.local'])); + self::assertEquals(1, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); + self::assertTrue($db->update('gserver', ['site_name' => 'test', "`registered-users` = `registered-users` + 1", 'info' => 'another test'], ['nurl' => 'http://friendica.local'])); + self::assertEquals(2, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); + self::assertTrue($db->update('gserver', ['site_name' => 'test', "`registered-users` = `registered-users` - 1", 'info' => 'another test'], ['nurl' => 'http://friendica.local'])); + self::assertEquals(1, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); + } +} From 5be9c9dbaf7f6e606372b4e18656631719ee8107 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 13 May 2023 22:14:52 +0200 Subject: [PATCH 03/11] Use optimized update statements --- src/Core/Worker/Cron.php | 4 +-- src/Database/DBA.php | 23 ++++++++++++ src/Database/Database.php | 18 ++++++++++ src/Model/Post/Delivery.php | 2 +- src/Model/Post/DeliveryData.php | 18 +++++----- src/Protocol/ActivityPub/Queue.php | 2 +- src/Worker/Cron.php | 11 +----- src/Worker/OptimizeTables.php | 56 +++++++++++++++--------------- 8 files changed, 83 insertions(+), 51 deletions(-) diff --git a/src/Core/Worker/Cron.php b/src/Core/Worker/Cron.php index e9b77229b..d0b915f87 100644 --- a/src/Core/Worker/Cron.php +++ b/src/Core/Worker/Cron.php @@ -151,8 +151,8 @@ class Cron // We are acquiring the two locks from the worker to avoid locking problems if (DI::lock()->acquire(Worker::LOCK_PROCESS, 10)) { if (DI::lock()->acquire(Worker::LOCK_WORKER, 10)) { - DBA::e("OPTIMIZE TABLE `workerqueue`"); - DBA::e("OPTIMIZE TABLE `process`"); + DBA::optimizeTable('workerqueue'); + DBA::optimizeTable('process'); DI::lock()->release(Worker::LOCK_WORKER); } DI::lock()->release(Worker::LOCK_PROCESS); diff --git a/src/Database/DBA.php b/src/Database/DBA.php index d609f108e..e730b298d 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -821,6 +821,29 @@ class DBA return DI::dba()->processlist(); } + /** + * Optimizes tables + * + * @param string $table a given table + * + * @return bool True, if successfully optimized, otherwise false + * @throws \Exception + */ + public static function optimizeTable(string $table): bool + { + return DI::dba()->optimizeTable($table); + } + + /** + * Kill sleeping database processes + * + * @return void + */ + public static function deleteSleepingProcesses() + { + return DI::dba()->delete(); + } + /** * Fetch a database variable * diff --git a/src/Database/Database.php b/src/Database/Database.php index f4959bf7e..f931169ce 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1781,6 +1781,24 @@ class Database return $this->e("OPTIMIZE TABLE " . DBA::buildTableString([$table])) !== false; } + /** + * Kill sleeping database processes + * + * @return void + */ + public function deleteSleepingProcesses() + { + $processes = $this->p("SHOW FULL PROCESSLIST"); + while ($process = $this->fetch($processes)) { + if (($process['Command'] != 'Sleep') || ($process['Time'] < 300) || ($process['db'] != $this->databaseName())) { + continue; + } + + $this->e("KILL ?", $process['Id']); + } + $this->close($processes); + } + /** * Fetch a database variable * diff --git a/src/Model/Post/Delivery.php b/src/Model/Post/Delivery.php index 0e343e871..c53014fa5 100644 --- a/src/Model/Post/Delivery.php +++ b/src/Model/Post/Delivery.php @@ -78,7 +78,7 @@ class Delivery */ public static function incrementFailed(int $uri_id, string $inbox) { - return DBA::e('UPDATE `post-delivery` SET `failed` = `failed` + 1 WHERE `uri-id` = ? AND `inbox-id` = ?', $uri_id, ItemURI::getIdByURI($inbox)); + return DBA::update('post-delivery', ["`failed` = `failed` + 1"], ['uri-id' => $uri_id, 'inbox-id' => ItemURI::getIdByURI($inbox)]); } public static function selectForInbox(string $inbox) diff --git a/src/Model/Post/DeliveryData.php b/src/Model/Post/DeliveryData.php index e87bb0e01..327e6f912 100644 --- a/src/Model/Post/DeliveryData.php +++ b/src/Model/Post/DeliveryData.php @@ -82,27 +82,27 @@ class DeliveryData */ public static function incrementQueueDone(int $uri_id, int $protocol = 0) { - $sql = ''; + $increments = ["`queue_done` = `queue_done` + 1"]; switch ($protocol) { case self::ACTIVITYPUB: - $sql = ", `activitypub` = `activitypub` + 1"; + $increments[] = ["`activitypub` = `activitypub` + 1"]; break; case self::DFRN: - $sql = ", `dfrn` = `dfrn` + 1"; + $increments[] = ["`dfrn` = `dfrn` + 1"]; break; case self::LEGACY_DFRN: - $sql = ", `legacy_dfrn` = `legacy_dfrn` + 1"; + $increments[] = ["`legacy_dfrn` = `legacy_dfrn` + 1"]; break; case self::DIASPORA: - $sql = ", `diaspora` = `diaspora` + 1"; + $increments[] = ["`diaspora` = `diaspora` + 1"]; break; case self::OSTATUS: - $sql = ", `ostatus` = `ostatus` + 1"; + $increments[] = ["`ostatus` = `ostatus` + 1"]; break; } - return DBA::e('UPDATE `post-delivery-data` SET `queue_done` = `queue_done` + 1' . $sql . ' WHERE `uri-id` = ?', $uri_id); + return DBA::update('post-delivery-data', $increments, ['uri-id' => $uri_id]); } /** @@ -116,7 +116,7 @@ class DeliveryData */ public static function incrementQueueFailed(int $uri_id) { - return DBA::e('UPDATE `post-delivery-data` SET `queue_failed` = `queue_failed` + 1 WHERE `uri-id` = ?', $uri_id); + return DBA::update('post-delivery-data', ["`queue_failed` = `queue_failed` + 1"], ['uri-id' => $uri_id]); } /** @@ -129,7 +129,7 @@ class DeliveryData */ public static function incrementQueueCount(int $uri_id, int $increment = 1) { - return DBA::e('UPDATE `post-delivery-data` SET `queue_count` = `queue_count` + ? WHERE `uri-id` = ?', $increment, $uri_id); + return DBA::update('post-delivery-data', ["`queue_count` = `queue_count` + $increment"], ['uri-id' => $uri_id]); } /** diff --git a/src/Protocol/ActivityPub/Queue.php b/src/Protocol/ActivityPub/Queue.php index d77785fe6..beb8817bf 100644 --- a/src/Protocol/ActivityPub/Queue.php +++ b/src/Protocol/ActivityPub/Queue.php @@ -312,7 +312,7 @@ class Queue // Optimizing this table only last seconds if (DI::config()->get('system', 'optimize_tables')) { Logger::info('Optimize start'); - DBA::e("OPTIMIZE TABLE `inbox-entry`"); + DBA::optimizeTable('inbox-entry'); Logger::info('Optimize end'); } } diff --git a/src/Worker/Cron.php b/src/Worker/Cron.php index 8c9ea58a1..73d158070 100644 --- a/src/Worker/Cron.php +++ b/src/Worker/Cron.php @@ -163,15 +163,6 @@ class Cron { Logger::info('Looking for sleeping processes'); - $processes = DBA::p("SHOW FULL PROCESSLIST"); - while ($process = DBA::fetch($processes)) { - if (($process['Command'] != 'Sleep') || ($process['Time'] < 300) || ($process['db'] != DBA::databaseName())) { - continue; - } - - DBA::e("KILL ?", $process['Id']); - Logger::notice('Killed sleeping process', ['id' => $process['Id']]); - } - DBA::close($processes); + DBA::deleteSleepingProcesses(); } } diff --git a/src/Worker/OptimizeTables.php b/src/Worker/OptimizeTables.php index bb4cc9e48..784c72fde 100644 --- a/src/Worker/OptimizeTables.php +++ b/src/Worker/OptimizeTables.php @@ -40,36 +40,36 @@ class OptimizeTables Logger::info('Optimize start'); - DBA::e("OPTIMIZE TABLE `cache`"); - DBA::e("OPTIMIZE TABLE `locks`"); - DBA::e("OPTIMIZE TABLE `oembed`"); - DBA::e("OPTIMIZE TABLE `parsed_url`"); - DBA::e("OPTIMIZE TABLE `session`"); + DBA::optimizeTable('cache'); + DBA::optimizeTable('locks'); + DBA::optimizeTable('oembed'); + DBA::optimizeTable('parsed_url'); + DBA::optimizeTable('session'); if (DI::config()->get('system', 'optimize_all_tables')) { - DBA::e("OPTIMIZE TABLE `apcontact`"); - DBA::e("OPTIMIZE TABLE `contact`"); - DBA::e("OPTIMIZE TABLE `contact-relation`"); - DBA::e("OPTIMIZE TABLE `conversation`"); - DBA::e("OPTIMIZE TABLE `diaspora-contact`"); - DBA::e("OPTIMIZE TABLE `diaspora-interaction`"); - DBA::e("OPTIMIZE TABLE `fcontact`"); - DBA::e("OPTIMIZE TABLE `gserver`"); - DBA::e("OPTIMIZE TABLE `gserver-tag`"); - DBA::e("OPTIMIZE TABLE `inbox-status`"); - DBA::e("OPTIMIZE TABLE `item-uri`"); - DBA::e("OPTIMIZE TABLE `notification`"); - DBA::e("OPTIMIZE TABLE `notify`"); - DBA::e("OPTIMIZE TABLE `photo`"); - DBA::e("OPTIMIZE TABLE `post`"); - DBA::e("OPTIMIZE TABLE `post-content`"); - DBA::e("OPTIMIZE TABLE `post-delivery-data`"); - DBA::e("OPTIMIZE TABLE `post-link`"); - DBA::e("OPTIMIZE TABLE `post-thread`"); - DBA::e("OPTIMIZE TABLE `post-thread-user`"); - DBA::e("OPTIMIZE TABLE `post-user`"); - DBA::e("OPTIMIZE TABLE `storage`"); - DBA::e("OPTIMIZE TABLE `tag`"); + DBA::optimizeTable('apcontact'); + DBA::optimizeTable('contact'); + DBA::optimizeTable('contact-relation'); + DBA::optimizeTable('conversation'); + DBA::optimizeTable('diaspora-contact'); + DBA::optimizeTable('diaspora-interaction'); + DBA::optimizeTable('fcontact'); + DBA::optimizeTable('gserver'); + DBA::optimizeTable('gserver-tag'); + DBA::optimizeTable('inbox-status'); + DBA::optimizeTable('item-uri'); + DBA::optimizeTable('notification'); + DBA::optimizeTable('notify'); + DBA::optimizeTable('photo'); + DBA::optimizeTable('post'); + DBA::optimizeTable('post-content'); + DBA::optimizeTable('post-delivery-data'); + DBA::optimizeTable('post-link'); + DBA::optimizeTable('post-thread'); + DBA::optimizeTable('post-thread-user'); + DBA::optimizeTable('post-user'); + DBA::optimizeTable('storage'); + DBA::optimizeTable('tag'); } Logger::info('Optimize end'); From 7bf2606120b08426b79b3d289766e04d294a7496 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 13 May 2023 22:18:00 +0200 Subject: [PATCH 04/11] Update & fix --- src/Database/DBA.php | 2 +- tests/src/Database/DatabaseTest.php | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Database/DBA.php b/src/Database/DBA.php index e730b298d..e29a94068 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -841,7 +841,7 @@ class DBA */ public static function deleteSleepingProcesses() { - return DI::dba()->delete(); + return DI::dba()->deleteSleepingProcesses(); } /** diff --git a/tests/src/Database/DatabaseTest.php b/tests/src/Database/DatabaseTest.php index e95d655f1..85c673f07 100644 --- a/tests/src/Database/DatabaseTest.php +++ b/tests/src/Database/DatabaseTest.php @@ -21,6 +21,9 @@ class DatabaseTest extends FixtureTest $this->configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/'); } + /** + * Test, if directly updating a field is possible + */ public function testUpdateIncrease() { $db = $this->getDbInstance(); @@ -30,13 +33,18 @@ class DatabaseTest extends FixtureTest self::assertEquals(1, $db->selectFirst('config', ['v'], ['cat' => 'test', 'k' => 'inc'])['v']); } + /** + * Test if combining directly field updates with normal updates is working + */ public function testUpdateWithField() { $db = $this->getDbInstance(); self::assertEquals('https://friendica.local', $db->selectFirst('gserver', ['url'], ['nurl' => 'http://friendica.local'])['url']); - self::assertTrue($db->update('gserver', ['site_name' => 'test', "`registered-users` = `registered-users` + 1", 'info' => 'another test'], ['nurl' => 'http://friendica.local'])); + self::assertTrue($db->update('gserver', ['active-week-users' => 0], ['nurl' => 'http://friendica.local'])); + self::assertTrue($db->update('gserver', ['site_name' => 'test', "`registered-users` = `registered-users` + 1", 'info' => 'another test', "`active-week-users` = `active-week-users` + 2"], ['nurl' => 'http://friendica.local'])); self::assertEquals(1, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); + self::assertEquals(2, $db->selectFirst('gserver', ['active-week-users'], ['nurl' => 'http://friendica.local'])['active-week-users']); self::assertTrue($db->update('gserver', ['site_name' => 'test', "`registered-users` = `registered-users` + 1", 'info' => 'another test'], ['nurl' => 'http://friendica.local'])); self::assertEquals(2, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); self::assertTrue($db->update('gserver', ['site_name' => 'test', "`registered-users` = `registered-users` - 1", 'info' => 'another test'], ['nurl' => 'http://friendica.local'])); From 9386adb1849057cb204446c6d161976e5b1d2dad Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 13 May 2023 22:18:11 +0200 Subject: [PATCH 05/11] Update & fix --- src/Database/DBA.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Database/DBA.php b/src/Database/DBA.php index e29a94068..930e60472 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -836,12 +836,10 @@ class DBA /** * Kill sleeping database processes - * - * @return void */ public static function deleteSleepingProcesses() { - return DI::dba()->deleteSleepingProcesses(); + DI::dba()->deleteSleepingProcesses(); } /** From 7f184bf6fabdd8167e9fb211c5278ffab9a9d647 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 13 May 2023 22:27:29 +0200 Subject: [PATCH 06/11] Adapt & remove impossible code --- src/Core/Update.php | 14 -------------- src/Core/Worker/Cron.php | 2 +- src/Database/DBStructure.php | 15 +++++++++++++-- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/Core/Update.php b/src/Core/Update.php index a4c5cf313..2718d03d0 100644 --- a/src/Core/Update.php +++ b/src/Core/Update.php @@ -129,20 +129,6 @@ class Update DI::lock()->release('dbupdate', true); } - if (!DBStructure::existsTable('config')) { - DBA::e(<<get('system', 'build'); if (empty($build)) { diff --git a/src/Core/Worker/Cron.php b/src/Core/Worker/Cron.php index d0b915f87..dcb9fd3ab 100644 --- a/src/Core/Worker/Cron.php +++ b/src/Core/Worker/Cron.php @@ -197,7 +197,7 @@ class Cron // Optimizing this table only last seconds if (DI::config()->get('system', 'optimize_tables')) { Logger::info('Optimize start'); - DBA::e("OPTIMIZE TABLE `post-delivery`"); + DBA::optimizeTable('post-delivery'); Logger::info('Optimize end'); } } diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index dc1e785a5..3141ca666 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -57,6 +57,18 @@ class DBStructure echo DI::l10n()->t('The database version had been set to %s.', $version); } + /** + * Dops a specific table + * + * @param string $table the table name + * + * @return bool true if possible, otherwise false + */ + public static function dropTable(string $table): bool + { + return DBA::isResult(DBA::e('DROP TABLE ' . DBA::quoteIdentifier($table) . ';')); + } + /** * Drop unused tables * @@ -94,8 +106,7 @@ class DBStructure $sql = 'DROP TABLE ' . DBA::quoteIdentifier($table) . ';'; echo $sql . "\n"; - $result = DBA::e($sql); - if (!DBA::isResult($result)) { + if (!static::dropTable($table)) { self::printUpdateError($sql); } } else { From db4c9773b25cd49cd7238a6b90718c9f85fb625c Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 13 May 2023 22:35:32 +0200 Subject: [PATCH 07/11] add license --- tests/src/Database/DatabaseTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/src/Database/DatabaseTest.php b/tests/src/Database/DatabaseTest.php index 85c673f07..06aee5a92 100644 --- a/tests/src/Database/DatabaseTest.php +++ b/tests/src/Database/DatabaseTest.php @@ -1,4 +1,23 @@ . + * + */ namespace Friendica\Test\src\Database; From 8e208a0f41e779a333d0bc7f90b88c3728f18ea5 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 13 May 2023 22:36:18 +0200 Subject: [PATCH 08/11] adhere PHP CC --- tests/src/Database/DatabaseTest.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/src/Database/DatabaseTest.php b/tests/src/Database/DatabaseTest.php index 06aee5a92..6943f919d 100644 --- a/tests/src/Database/DatabaseTest.php +++ b/tests/src/Database/DatabaseTest.php @@ -36,7 +36,7 @@ class DatabaseTest extends FixtureTest parent::setUp(); - $this->configCache = new Cache(); + $this->configCache = new Cache(); $this->configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/'); } @@ -61,12 +61,22 @@ class DatabaseTest extends FixtureTest self::assertEquals('https://friendica.local', $db->selectFirst('gserver', ['url'], ['nurl' => 'http://friendica.local'])['url']); self::assertTrue($db->update('gserver', ['active-week-users' => 0], ['nurl' => 'http://friendica.local'])); - self::assertTrue($db->update('gserver', ['site_name' => 'test', "`registered-users` = `registered-users` + 1", 'info' => 'another test', "`active-week-users` = `active-week-users` + 2"], ['nurl' => 'http://friendica.local'])); + self::assertTrue($db->update('gserver', + ['site_name' => 'test', "`registered-users` = `registered-users` + 1", + 'info' => 'another test', + "`active-week-users` = `active-week-users` + 2"], ['nurl' => 'http://friendica.local' + ])); self::assertEquals(1, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); self::assertEquals(2, $db->selectFirst('gserver', ['active-week-users'], ['nurl' => 'http://friendica.local'])['active-week-users']); - self::assertTrue($db->update('gserver', ['site_name' => 'test', "`registered-users` = `registered-users` + 1", 'info' => 'another test'], ['nurl' => 'http://friendica.local'])); + self::assertTrue($db->update('gserver', + ['site_name' => 'test', "`registered-users` = `registered-users` + 1", + 'info' => 'another test'], ['nurl' => 'http://friendica.local' + ])); self::assertEquals(2, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); - self::assertTrue($db->update('gserver', ['site_name' => 'test', "`registered-users` = `registered-users` - 1", 'info' => 'another test'], ['nurl' => 'http://friendica.local'])); + self::assertTrue($db->update('gserver', + ['site_name' => 'test', "`registered-users` = `registered-users` - 1", + 'info' => 'another test'], ['nurl' => 'http://friendica.local' + ])); self::assertEquals(1, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); } } From 60ea36259e71c0ea561961e3d59264500ca55599 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 13 May 2023 22:42:56 +0200 Subject: [PATCH 09/11] PHP-CS ... --- tests/src/Database/DatabaseTest.php | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/tests/src/Database/DatabaseTest.php b/tests/src/Database/DatabaseTest.php index 6943f919d..26300852c 100644 --- a/tests/src/Database/DatabaseTest.php +++ b/tests/src/Database/DatabaseTest.php @@ -61,22 +61,24 @@ class DatabaseTest extends FixtureTest self::assertEquals('https://friendica.local', $db->selectFirst('gserver', ['url'], ['nurl' => 'http://friendica.local'])['url']); self::assertTrue($db->update('gserver', ['active-week-users' => 0], ['nurl' => 'http://friendica.local'])); - self::assertTrue($db->update('gserver', - ['site_name' => 'test', "`registered-users` = `registered-users` + 1", - 'info' => 'another test', - "`active-week-users` = `active-week-users` + 2"], ['nurl' => 'http://friendica.local' - ])); + self::assertTrue($db->update('gserver', [ + 'site_name' => 'test', "`registered-users` = `registered-users` + 1", + 'info' => 'another test', + "`active-week-users` = `active-week-users` + 2" + ], [ + 'nurl' => 'http://friendica.local' + ])); self::assertEquals(1, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); self::assertEquals(2, $db->selectFirst('gserver', ['active-week-users'], ['nurl' => 'http://friendica.local'])['active-week-users']); - self::assertTrue($db->update('gserver', - ['site_name' => 'test', "`registered-users` = `registered-users` + 1", - 'info' => 'another test'], ['nurl' => 'http://friendica.local' - ])); + self::assertTrue($db->update('gserver', [ + 'site_name' => 'test', "`registered-users` = `registered-users` + 1", + 'info' => 'another test'], ['nurl' => 'http://friendica.local' + ])); self::assertEquals(2, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); - self::assertTrue($db->update('gserver', - ['site_name' => 'test', "`registered-users` = `registered-users` - 1", - 'info' => 'another test'], ['nurl' => 'http://friendica.local' - ])); + self::assertTrue($db->update('gserver', [ + 'site_name' => 'test', "`registered-users` = `registered-users` - 1", + 'info' => 'another test'], ['nurl' => 'http://friendica.local' + ])); self::assertEquals(1, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); } } From a74c0e86c990801c6ce61f3620f395818f6e341f Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 13 May 2023 22:46:10 +0200 Subject: [PATCH 10/11] PHP-CS ... --- tests/src/Database/DatabaseTest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/src/Database/DatabaseTest.php b/tests/src/Database/DatabaseTest.php index 26300852c..47d403220 100644 --- a/tests/src/Database/DatabaseTest.php +++ b/tests/src/Database/DatabaseTest.php @@ -72,12 +72,16 @@ class DatabaseTest extends FixtureTest self::assertEquals(2, $db->selectFirst('gserver', ['active-week-users'], ['nurl' => 'http://friendica.local'])['active-week-users']); self::assertTrue($db->update('gserver', [ 'site_name' => 'test', "`registered-users` = `registered-users` + 1", - 'info' => 'another test'], ['nurl' => 'http://friendica.local' + 'info' => 'another test' + ], [ + 'nurl' => 'http://friendica.local' ])); self::assertEquals(2, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); self::assertTrue($db->update('gserver', [ 'site_name' => 'test', "`registered-users` = `registered-users` - 1", - 'info' => 'another test'], ['nurl' => 'http://friendica.local' + 'info' => 'another test' + ], [ + 'nurl' => 'http://friendica.local' ])); self::assertEquals(1, $db->selectFirst('gserver', ['registered-users'], ['nurl' => 'http://friendica.local'])['registered-users']); } From 8c2678c82f09131490226de1dc308f5212a7f7c7 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 14 May 2023 11:18:59 +0200 Subject: [PATCH 11/11] fixups --- src/Database/DBStructure.php | 2 +- src/Database/DatabaseException.php | 3 +-- .../Repository/DeliveryQueueItem.php | 18 +++++++++++------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index 3141ca666..6291d0ffc 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -58,7 +58,7 @@ class DBStructure } /** - * Dops a specific table + * Drops a specific table * * @param string $table the table name * diff --git a/src/Database/DatabaseException.php b/src/Database/DatabaseException.php index 9473e5808..8c99b9a7e 100644 --- a/src/Database/DatabaseException.php +++ b/src/Database/DatabaseException.php @@ -45,9 +45,8 @@ class DatabaseException extends Exception */ public function __construct(string $message, int $code, string $query, Throwable $previous = null) { - $this->query = $query; - parent::__construct(sprintf('"%s" at "%s"', $message, $query) , $code, $previous); + $this->query = $query; } /** diff --git a/src/Federation/Repository/DeliveryQueueItem.php b/src/Federation/Repository/DeliveryQueueItem.php index 5d5198adf..37d4ad84c 100644 --- a/src/Federation/Repository/DeliveryQueueItem.php +++ b/src/Federation/Repository/DeliveryQueueItem.php @@ -88,8 +88,10 @@ final class DeliveryQueueItem extends \Friendica\BaseRepository public function remove(Entity\DeliveryQueueItem $deliveryQueueItem): bool { - return $this->db->delete(self::$table_name, ['uri-id' => $deliveryQueueItem->postUriId, - 'gsid' => $deliveryQueueItem->targetServerId]); + return $this->db->delete(self::$table_name, [ + 'uri-id' => $deliveryQueueItem->postUriId, + 'gsid' => $deliveryQueueItem->targetServerId + ]); } public function removeFailedByServerId(int $gsid, int $failedThreshold): bool @@ -99,11 +101,13 @@ final class DeliveryQueueItem extends \Friendica\BaseRepository public function incrementFailed(Entity\DeliveryQueueItem $deliveryQueueItem): bool { - return $this->db->update(self::$table_name, ["`failed` = `failed` + 1"], - ["`uri-id` = ? AND `gsid` = ?", - $deliveryQueueItem->postUriId, - $deliveryQueueItem->targetServerId - ]); + return $this->db->update(self::$table_name, [ + "`failed` = `failed` + 1" + ], [ + "`uri-id` = ? AND `gsid` = ?", + $deliveryQueueItem->postUriId, + $deliveryQueueItem->targetServerId + ]); } public function optimizeStorage(): bool