Skip to content

fix(database-pgsql): read index columns back without postgres's quotes - #715

Merged
markshust merged 1 commit into
marko-php:developfrom
etienne-scherrer:feature/pgsql-quoted-index-columns
Nov 25, 2026
Merged

markshust merged 1 commit into
marko-php:developfrom
etienne-scherrer:feature/pgsql-quoted-index-columns

Conversation

@etienne-scherrer

Copy link
Copy Markdown
Contributor

Problem

PgSqlIntrospector::parseIndexColumns() takes the column names of an index from pg_indexes.indexdef. PostgreSQL quotes a name there whenever it has to: a reserved word, mixed case or an embedded quote. The parser keeps those quotes, so an index on group is introspected with the column "group".

Matching indexes by name hides this most of the time. It shows up when a migration recreates a database index from what was introspected: PgSqlGenerator quotes the name again, and the statement fails.

0.14's derived index names (#709) make that the normal upgrade path for admin-auth, whose permissions table indexes group. On an app that was on 0.13:

marko db:migrate --generate --force   # renames the six admin-auth indexes
marko db:rollback

The generated down() contains

$this->execute($connection, 'CREATE INDEX "idx_permissions_group" ON "permissions" ("""group""");');

and the rollback fails with column ""group"" does not exist. Because the statements before it in the same down() have already run, permissions is left with neither index.

Change

  • PgSqlIdentifier::unquote(), the inverse of quote(): it strips the delimiters and undoubles embedded quotes, and returns anything PostgreSQL did not quote (a plain name, an index expression) unchanged.
  • parseIndexColumns() unquotes each column it reads.

Tests

  • PgSqlIntrospectorTest: an indexdef with "group", "la""bel", "displayName" and a plain column comes back as ['group', 'la"bel', 'displayName', 'status']. Failed before with the quotes kept.
  • PgSqlIdentifierTest: unquote() undoes quote() for plain, reserved, mixed-case and delimiter names, and leaves unquoted names and expressions alone.
  • ReservedWordIdentifiersTest (integration, real PostgreSQL): the fixture entity gets #[Index(['group', 'order'])]. The test renames that index, applies the generated diff, then its generateDown(), and checks that the old index is back on group and order. Failed before with exactly the error above.

pest on packages/database and packages/database-pgsql against PostgreSQL 17 from tests/Integration/compose.yml with MARKO_INTEGRATION_REQUIRED=1: 1997 passed. phpcs, php-cs-fixer --dry-run and phpstan: clean.

Not in this PR: Migrator runs a migration's statements without a transaction, so a migration that fails halfway on PostgreSQL stays half applied, as the dropped permissions index above shows. PostgreSQL could roll the whole migration back. Happy to look at that separately if you want it.

@markshust

Copy link
Copy Markdown
Collaborator

Thanks Etienne, this is a clean fix and the write-up made it easy to verify. The real-Postgres rollback test is exactly the right coverage. Merging as is.

On the Migrator point: agreed, and I've opened #718 for it with your report credited. If you'd like to take it, it's yours.

@markshust
markshust merged commit 0effd54 into marko-php:develop Nov 25, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants