A free, self-hostable news aggregator…
Fix tagging an already-tagged entry on PostgreSQL (#9136)
Gerard Alvear Porras9b40f04f32da
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 26af6b5b3..3158abfcc 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -21,6 +21,7 @@ See also [the FreshRSS releases](https://github.com/FreshRSS/FreshRSS/releases).* New per-feed option to show or hide enclosures (attachments) [#4999](https://github.com/FreshRSS/FreshRSS/issues/4999)* Bug fixing* Fix lost elements while parsing search query [#8884](https://github.com/FreshRSS/FreshRSS/pull/8884)+ * Fix tagging an already-tagged entry on PostgreSQL raising a SQL error instead of being a no-op [#7923](https://github.com/FreshRSS/FreshRSS/issues/7923)* CLI* New `cli/reconfigure-user.php` to read/write per-user config attributes [#8873](https://github.com/FreshRSS/FreshRSS/pull/8873)* APIdiff --git a/app/Models/TagDAO.php b/app/Models/TagDAO.phpindex d6714d98c..040bad701 100644--- a/app/Models/TagDAO.php+++ b/app/Models/TagDAO.php@@ -3,8 +3,8 @@ declare(strict_types=1);class FreshRSS_TagDAO extends Minz_ModelPdo {- public function sqlIgnore(): string {- return 'IGNORE';+ public static function sqlIgnoreConflict(string $sql): string {+ return str_replace('INSERT INTO ', 'INSERT IGNORE INTO ', $sql);}public function sqlResetSequence(): bool {@@ -303,10 +303,9 @@ class FreshRSS_TagDAO extends Minz_ModelPdo {public function tagEntry(int $id_tag, string $id_entry, bool $checked = true): bool {if ($checked) {- $ignore = $this->sqlIgnore();- $sql = <<<SQL- INSERT {$ignore} INTO `_entrytag`(id_tag, id_entry) VALUES(:id_tag, :id_entry)- SQL;+ $sql = static::sqlIgnoreConflict(<<<'SQL'+ INSERT INTO `_entrytag`(id_tag, id_entry) VALUES(:id_tag, :id_entry)+ SQL);} else {$sql = <<<'SQL'DELETE FROM `_entrytag` WHERE id_tag=:id_tag AND id_entry=:id_entry@@ -331,9 +330,8 @@ class FreshRSS_TagDAO extends Minz_ModelPdo {*/public function tagEntries(iterable $addLabels): int|false {$hasValues = false;- $ignore = $this->sqlIgnore();- $sql = <<<SQL- INSERT {$ignore} INTO `_entrytag`(id_tag, id_entry) VALUES+ $sql = <<<'SQL'+ INSERT INTO `_entrytag`(id_tag, id_entry) VALUESSQL;foreach ($addLabels as $addLabel) {$id_tag = (int)($addLabel['id_tag'] ?? 0);@@ -347,6 +345,7 @@ class FreshRSS_TagDAO extends Minz_ModelPdo {if (!$hasValues) {return false;}+ $sql = static::sqlIgnoreConflict($sql);$affected = $this->pdo->exec($sql);if ($affected !== false) {diff --git a/app/Models/TagDAOPGSQL.php b/app/Models/TagDAOPGSQL.phpindex 63500f6a6..f0119003f 100644--- a/app/Models/TagDAOPGSQL.php+++ b/app/Models/TagDAOPGSQL.php@@ -4,8 +4,8 @@ declare(strict_types=1);class FreshRSS_TagDAOPGSQL extends FreshRSS_TagDAO {#[\Override]- public function sqlIgnore(): string {- return ''; //TODO+ public static function sqlIgnoreConflict(string $sql): string {+ return rtrim($sql, ' ;') . ' ON CONFLICT DO NOTHING';}#[\Override]diff --git a/app/Models/TagDAOSQLite.php b/app/Models/TagDAOSQLite.phpindex 43bb9cdcd..68fb17a4b 100644--- a/app/Models/TagDAOSQLite.php+++ b/app/Models/TagDAOSQLite.php@@ -4,8 +4,8 @@ declare(strict_types=1);class FreshRSS_TagDAOSQLite extends FreshRSS_TagDAO {#[\Override]- public function sqlIgnore(): string {- return 'OR IGNORE';+ public static function sqlIgnoreConflict(string $sql): string {+ return str_replace('INSERT INTO ', 'INSERT OR IGNORE INTO ', $sql);}#[\Override]diff --git a/tests/app/Models/TagDAOTest.php b/tests/app/Models/TagDAOTest.phpnew file mode 100644index 000000000..9bb450a73--- /dev/null+++ b/tests/app/Models/TagDAOTest.php@@ -0,0 +1,30 @@+<?php+declare(strict_types=1);++/**+ * Tests for the database-specific `INSERT ... ON DUPLICATE` variants used when tagging entries.+ */+final class TagDAOTest extends \PHPUnit\Framework\TestCase {+ private const SQL = 'INSERT INTO `_entrytag`(id_tag, id_entry) VALUES(:id_tag, :id_entry)';++ public function test_sqlIgnoreConflict_forMySQL(): void {+ self::assertSame(+ 'INSERT IGNORE INTO `_entrytag`(id_tag, id_entry) VALUES(:id_tag, :id_entry)',+ FreshRSS_TagDAO::sqlIgnoreConflict(self::SQL),+ );+ }++ public function test_sqlIgnoreConflict_forSQLite(): void {+ self::assertSame(+ 'INSERT OR IGNORE INTO `_entrytag`(id_tag, id_entry) VALUES(:id_tag, :id_entry)',+ FreshRSS_TagDAOSQLite::sqlIgnoreConflict(self::SQL),+ );+ }++ public function test_sqlIgnoreConflict_forPostgreSQL(): void {+ self::assertSame(+ 'INSERT INTO `_entrytag`(id_tag, id_entry) VALUES(:id_tag, :id_entry) ON CONFLICT DO NOTHING',+ FreshRSS_TagDAOPGSQL::sqlIgnoreConflict(self::SQL),+ );+ }+}