fix(goctl): preserve PostgreSQL partial unique index WHERE clause in model generation#5559
fix(goctl): preserve PostgreSQL partial unique index WHERE clause in model generation#5559hoshi200 wants to merge 2 commits into
Conversation
…model generation Fixes zeromicro#3841 pg_get_expr(C.INDPRED, C.INDRELID) now retrieves the partial index predicate from pg_index, propagated through all layers (PostgreIndex → DbIndex → Column → model.Table → parser.Table → Key) and appended to generated FindOneByXxx SQL WHERE clauses. Cache keys are disambiguated using the index name as suffix for partial unique indexes sharing the same column set but different predicates.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
ReviewThis PR fixes a real gap in What the fix does:
Questions / concerns:
Overall: This is a well-scoped fix for a long-standing gap (fixes #3841). The pipeline approach (propagating predicate through all layers) is correct. Please clarify the NULL predicate handling before merging. |
|
@kevwan thanks for the thorough review — addressed each point below. 1. NULL predicate (non-partial indexes) This path is safe end-to-end. When
The new 2. Trust assumption Added a comment in 3. The template uses lowercase for all SQL keywords ( 4. Cache key disambiguation The CamelCase index-name suffix approach looks good, no concerns here. 5. Integration test Added |
Problem
When running
goctl model pg datasourceagainst a PostgreSQL database with partial unique indexes, the generatedFindOneByXxxmethods lose the index WHERE clause.Before this fix, goctl generates:
Fixes #3841
Root Cause
FindIndex()inpostgresqlmodel.gojoinspg_indexbut does not selectindpred. The entire code generation pipeline has no concept of partial index predicates.Fix
6 files, 7 layers:
postgresqlmodel.gopg_get_expr(C.INDPRED, C.INDRELID) AS predicatein FindIndex() SQLpostgresqlmodel.goPostgreIndex.Predicatefieldinfoschemamodel.goDbIndex.Predicate+Table.UniqueIndexPredicatemap + population in Convert()postgresqlmodel.goparser.goparser.Table.UniqueIndexPredicate+ propagation in ConvertDataType()keys.goKey.Predicatefield + index-name CamelCase suffix for disambiguationfindonebyfield.goAND <predicate>to generated SQL WHERE clauseBefore / After
Before:
After:
Cache Key Disambiguation
Multiple partial indexes on the same column with different predicates now produce distinct cache keys:
Verification