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

Đang mở
#805 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
48/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
php
Lĩnh vực
testing-qa

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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!

Ngôn ngữ chính
PHP
Star
683
Fork
129
Merge trung bình
13 giờ 6 phút
Pull request đã merge (30 ngày)
14

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của phpstan/phpstan-doctrine

Tất cả issue của phpstan/phpstan-doctrine

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.