Add PostgreSQL schema hashing - #802
Conversation
29ee378 to
862c1fe
Compare
|
Did you try this patch on a real world project? |
There was a problem hiding this comment.
Pull request overview
Adds first-class PostgreSQL support for schema hashing so phpstan-dba can invalidate result caches based on PostgreSQL schema changes (instead of using the MySQL hasher and executing MySQL-specific SQL on pgsql connections).
Changes:
- Introduce a new
SchemaHasherPgsqlimplementation that hashes PostgreSQL schema metadata (schema/table/column name, effective type, nullability). - Automatically select the PostgreSQL hasher when the datasource is a PDO connection using the
pgsqldriver. - Add a PostgreSQL regression test covering stable hashing and hash changes on column rename, type change, and nullability change.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/default/SchemaHasherPgsqlTest.php | Adds a PostgreSQL integration/regression test verifying schema-hash stability and sensitivity to column changes. |
| src/QueryReflection/QueryReflection.php | Switches hasher selection to use SchemaHasherPgsql automatically for PDO pgsql datasources. |
| src/DbSchema/SchemaHasherPgsql.php | New PostgreSQL-specific schema hasher based on pg_catalog metadata. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| SELECT MD5( | ||
| STRING_AGG( | ||
| CONCAT_WS( | ||
| CHR(31), | ||
| namespace.nspname, | ||
| relation.relname, | ||
| attribute.attname, | ||
| FORMAT_TYPE(attribute.atttypid, attribute.atttypmod), | ||
| attribute.attnotnull::text | ||
| ), | ||
| CHR(30) | ||
| ORDER BY namespace.nspname, relation.relname, attribute.attnum | ||
| ) | ||
| ) AS dbsignature |
There was a problem hiding this comment.
Addressed in 11feff1. Empty user schemas now hash the empty aggregate deterministically as md5('').
| $port = ';port=' . $port; | ||
| } | ||
|
|
||
| return new PDO(sprintf('pgsql:dbname=%s;host=%s', $database, $host) . $port, $user, $password); |
There was a problem hiding this comment.
Addressed in 11feff1. The DDL connection now uses PDO::ERRMODE_EXCEPTION, so fixture setup failures surface immediately.
862c1fe to
11feff1
Compare
|
I have not tried it in an external application. I exercised the production routing path against a local PostgreSQL 16.14 instance, including schema rename, type, and nullability changes. After this update I also verified that an empty user schema returns |
|
thank you! |
|
Hey, thanks for working on this! I was just bumping into a bug in our CI pipeline based on how we don't have PostgreSQL schema hashing. I verified that bumping to the latest dev version fixes the issue. Would be super nice to have a new tagged release with this fix in it 🙏 ❤️ |
|
Tagged a new release |
|
Thank you, wow what a rapid response 🚀 |
Summary
pgsqldata sourcesThe previous fallback selected the MySQL schema hasher for every data source, so PostgreSQL recording mode attempted to execute
SET SESSION group_concat_max_len. The dedicated hasher now lets result-cache invalidation follow PostgreSQL schema changes without invoking MySQL-specific SQL.Closes #795
Validation
git diff --check