Files
FreshRSS/app/Models/DatabaseDAOPGSQL.php
T
Corey QuinnandAlexandre Alapetite e7fe44bf86 PostgreSQL: detect FreshRSS tables by visibility rather than owner (#9535)
* PostgreSQL: the owner was never the story, so stop asking who owns the tables

For as long as the install check has existed, it asked pg_tables who owned
the tables and compared that against the database login. Nobody asked why.
The login is a gateway user, the tables belong to whatever role ran the
install, and whenever those two names diverged the check reported the schema
as missing. Visibility was the question all along.

tablesAreCorrect() now asks pg_class which ordinary and partitioned tables
are visible on the search_path. If PostgreSQL can see it, it counts. A table
parked in a schema outside the path does not hide a missing one, and a view
named like a table is not a table.

The new test builds a role, a visible schema, a hidden schema, and a view
with a suspicious name, then checks that each answer is the true one. It runs
against a real PostgreSQL 18 service in CI. Locally it skips unless
FRESHRSS_TEST_PGSQL_DSN is set.

* Pin the CI Postgres service to a digest, because somebody moved the tag

For the record: nobody announced it. The postgres:18-alpine tag sat in
tests.yml like a loaded gun pointed at every CI run, and zizmor was the
first thing to notice it had no fixed identity at all. Remember the
Denver Airport. Remember that tags are not promises.

This commit replaces the mutable tag with the exact multi-arch digest the
suite was already tested against:

  postgres:18-alpine@sha256:eaef288f54a90a909e49f0121d5d047a5e974e922699de4249625cf7cde34f36

This is not a version bump. Same image, same PostgreSQL 18, same Alpine
base. The only thing that changed is that the image can no longer be
swapped out from under us between Tuesday's green run and Thursday's
mystery failure.

Verification: all pre-commit PHP, test, JS, CSS and Markdown checks pass.
Local pedantic offline zizmor reports no findings. This resolves the
unpinned-service-image blocker on PR #9535.

* Rewrite test a bit

* Fix make test-all

---------

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
2026-11-16 23:26:18 +01:00

123 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
/**
* This class is used to test database is well-constructed.
*/
class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite {
//PostgreSQL error codes
public const UNDEFINED_COLUMN = '42703';
public const UNDEFINED_TABLE = '42P01';
#[\Override]
public function tablesAreCorrect(): bool {
$sql = <<<'SQL'
SELECT relname AS tablename FROM pg_catalog.pg_class
WHERE relkind IN ('r', 'p') AND pg_catalog.pg_table_is_visible(oid)
SQL;
$res = $this->fetchAssoc($sql);
if ($res == null) {
return false;
}
$tables = [
$this->pdo->prefix() . 'category' => false,
$this->pdo->prefix() . 'feed' => false,
$this->pdo->prefix() . 'entry' => false,
$this->pdo->prefix() . 'entrytmp' => false,
$this->pdo->prefix() . 'tag' => false,
$this->pdo->prefix() . 'entrytag' => false,
];
foreach ($res as $value) {
$tables[array_pop($value)] = true;
}
return count(array_keys($tables, true, true)) === count($tables);
}
/** @return list<array{name:string,type:string,notnull:bool,default:mixed}> */
#[\Override]
public function getSchema(string $table): array {
$sql = <<<'SQL'
SELECT column_name AS field, data_type AS type, column_default AS default, is_nullable AS null
FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table_name
SQL;
$res = $this->fetchAssoc($sql, [':table_name' => $this->pdo->prefix() . $table]);
return $res == null ? [] : $this->listDaoToSchema($res);
}
/**
* @param array<string,string|int|bool|null> $dao
* @return array{'name':string,'type':string,'notnull':bool,'default':mixed}
*/
#[\Override]
public function daoToSchema(array $dao): array {
return [
'name' => is_string($dao['field'] ?? null) ? $dao['field'] : '',
'type' => is_string($dao['type'] ?? null) ? strtolower($dao['type']) : '',
'notnull' => empty($dao['null']),
'default' => is_scalar($dao['default'] ?? null) ? $dao['default'] : null,
];
}
#[\Override]
protected function selectVersion(): string {
return $this->fetchString('SELECT version()') ?? '';
}
#[\Override]
public function size(bool $all = false): int {
if ($all) {
$db = FreshRSS_Context::systemConf()->db;
$res = $this->fetchColumn('SELECT pg_database_size(:base)', 0, [':base' => $db['base']]);
} else {
$sql = <<<SQL
SELECT
pg_total_relation_size('`{$this->pdo->prefix()}category`') +
pg_total_relation_size('`{$this->pdo->prefix()}feed`') +
pg_total_relation_size('`{$this->pdo->prefix()}entry`') +
pg_total_relation_size('`{$this->pdo->prefix()}entrytmp`') +
pg_total_relation_size('`{$this->pdo->prefix()}tag`') +
pg_total_relation_size('`{$this->pdo->prefix()}entrytag`')
SQL;
$res = $this->fetchColumn($sql, 0);
}
return (int)($res[0] ?? -1);
}
#[\Override]
public function optimize(): bool {
$ok = true;
$tables = ['category', 'feed', 'entry', 'entrytmp', 'tag', 'entrytag'];
foreach ($tables as $table) {
$sql = <<<SQL
VACUUM `_{$table}`
SQL;
if ($this->pdo->exec($sql) === false) {
$ok = false;
$info = $this->pdo->errorInfo();
Minz_Log::warning(__METHOD__ . ' error: ' . $sql . ' : ' . json_encode($info));
}
}
return $ok;
}
#[\Override]
public static function strilike(string $haystack, string $needle, bool $contains = false): bool {
if (function_exists('mb_stripos')) {
return $contains ? (mb_stripos($haystack, $needle, 0, 'UTF-8') !== false) :
(mb_strtolower($haystack, 'UTF-8') === mb_strtolower($needle, 'UTF-8'));
}
if (function_exists('transliterator_transliterate')) {
$haystack_ = transliterator_transliterate('Lower', $haystack);
$needle_ = transliterator_transliterate('Lower', $needle);
if ($haystack_ !== false && $needle_ !== false) {
return $contains ? str_contains($haystack_, $needle_) : ($haystack_ === $needle_);
}
}
return $contains ? (stripos($haystack, $needle) !== false) : (strcasecmp($haystack, $needle) === 0);
}
}