Opt-in rules: require explicit names for indexes, unique constraints and foreign keys

Aperta
#805 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
1-2 giorni
Idoneità per principianti
48/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
php
Ambito
testing-qa

Direzione di ricerca

Start by reading the existing reportDynamicQueryBuilders parameter and how rules load ClassMetadata; the issue names table.indexes, table.uniqueConstraints, and association join columns as the metadata to inspect. Check which ORM versions expose JoinColumnMapping::foreignKeyName, then add tests for the opt-in parameter and its version-dependent behavior. Done means the rules detect mapping-generated names when enabled and remain off by default.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Would you accept opt-in rules that report indexes, unique constraints and foreign keys left without an explicit name? I'd like to ask before writing a PR.

The problem

When a mapping gives no name, DBAL generates one from a prefix and a crc32 hash of the table name and column names (AbstractAsset::_generateIdentifierName(), called from Table with [$tableName, ...$columnNames]). That causes two problems:

  • Migration churn. Renaming a table or a column changes the generated name, so the next diff drops the index or constraint and creates it again. On a large table that is an expensive rebuild, and for a unique constraint or foreign key it leaves a window where the constraint does not exist.
  • Opaque errors. A violation reads violates foreign key constraint "FK_8D93D649A76ED395", which tells whoever is on call nothing about which relation failed.

Both are avoidable, since every one of these names can be set in the mapping: name on #[Index] and #[UniqueConstraint], and foreignKeyName on #[JoinColumn]. Nothing reports a missing one today.

Proposal

Rules that read ClassMetadata through the existing object manager loader, so they work with every mapping driver and not only attributes:

  1. Every entry in table.indexes and table.uniqueConstraints has a name that was declared in the mapping, not generated.
  2. The join columns of every owning ManyToOne, OneToOne and ManyToMany association declare a foreign key name. This one only applies on ORM versions where JoinColumnMapping has foreignKeyName, and would do nothing on older ones.

Both would be off by default behind one parameter, the way reportDynamicQueryBuilders is, so nobody gets new errors without asking for them.

Prior work

I maintain a working attribute-based version of these rules in a private codebase. I'd port it onto ClassMetadata for this extension, with tests.

If this is out of scope here, that's fine. I'll publish them as a standalone extension instead. Thanks!

Lingua principale
PHP
Stelle
683
Fork
129
Merge medio
13h 6m
PR unite (30g)
14

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di phpstan/phpstan-doctrine

Tutte le issue di phpstan/phpstan-doctrine

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.