Task: Implement unified SQL template
This page documents a task in the Refactor ores.codegen SQL generation story. It captures the goal, current status, acceptance, and any notes or results.
Goal
Create sql_schema_create.mustache as the unified SQL template that replaces both
sql_schema_table_create.mustache and the SQL portion of
sql_schema_domain_entity_create.mustache. Add table model-type support to
generator.py and profiles.json. Define 10 _table.json models for all refdata
entities in the new format. Verify that running codegen.sh regenerate
--component refdata-table --profile sql produces zero logical diff against the
current SQL files in projects/ores.sql/create/refdata/.
Status
| Field | Value |
|---|---|
| State | DONE |
| Parent story | Refactor ores.codegen SQL generation |
| Now | Nothing. |
| Waiting on | Nothing. |
| Next | Migrate refdata entity models to new format. |
| Last touched | 2026-05-29 |
Acceptance
sql_schema_create.mustacheexists atprojects/ores.codegen/library/templates/.generator.pyrecognises*_table.jsonfiles as model typetable.profiles.jsonmapssqlprofile tosql_schema_create.mustachefortablemodel type.- 10
*_table.jsonmodel files exist inprojects/ores.codegen/models/refdata/. codegen.sh regenerate --component refdata-table --profile sql --dry-runprints all 10 output paths.- Running the actual regeneration produces zero logical diff (only template name comment changes) against the current repo SQL files.
- All four
bothtenant-scope entities (currency, currency_market_tier, monetary_nature, rounding_type) generateIN (p_tenant_id, ores_utility_system_tenant_id_fn())validation logic. party_id_schemegenerates thecheck ("max_cardinality" is null or "max_cardinality" > 0)constraint correctly.- Site builds cleanly.
PRs
| PR | Title |
|---|---|
| #936 | [codegen] Implement unified SQL template |
Review
| # | Comment summary | File | Decision | Notes |
|---|---|---|---|---|
| 1 | Guard empty-string check on primary_key.is_text | template | Accept | Kept quoted identifiers for consistency |
| 2 | Missing scope_tenant validation block | template | Accept | Full block added |
| 3 | Missing scope_tenant header comment | template | Accept | Added |
| 4 | Default order_by to PK column; drop tenant_scope fallback | generator.py | Accept | Added default; removed .get() fallback |
| 5 | Add has_any_coding_scheme; drop coding_scheme fallback | generator.py | Accept | Added flag; removed .get() fallback |
| 6 | Use has_any_coding_scheme to unify coding-scheme block | template | Accept | Two duplicate blocks merged into one |
| 7 | Conditionally quote default value based on is_text | template | Accept | Added is_text/^is_text branches |
Round 1 addressed in commit d32ea690a.
Result
Implementation complete. Files created/modified:
projects/ores.codegen/
library/
templates/
sql_schema_create.mustache new unified template
profiles.json added "table" to sql profile
models/refdata/
book_status_table.json new (system scope, display_order)
contact_type_table.json new (system scope, display_order)
currency_table.json new (both scope, nullable coding_scheme, image_id)
currency_market_tier_table.json new (both scope, default=g10)
monetary_nature_table.json new (both scope, default=fiat)
party_id_scheme_table.json new (system scope, check_constraints)
party_status_table.json new (system scope, display_order)
party_type_table.json new (system scope, display_order)
purpose_type_table.json new (system scope, display_order)
rounding_type_table.json new (both scope, default=Closest)
src/
codegen/
generate.py added "table" to --profile all safety check
manifest.py added refdata-table component
generator.py is_table_model(), get_model_type(), resolve_output_path(),
table special processing, sql_check_constraints pre-render
Key design decisions:
_table.jsonsuffix convention analogous to_entity.json,_domain_entity.json, etc.- Three-way tenant scope (
system/both/tenant) replaces the binarysystem_tenant_validationflag; pre-computed as boolean flags in generator preprocessing for Mustache consumption. - Check constraints pre-rendered as a single string (
sql_check_constraints) in generator preprocessing to avoid pystache whitespace issues with adjacent section tags. refdata-tablecomponent added to manifest for parity verification; replacesrefdataonce Task 4 (migration) is complete.- Notify triggers for
tablemodel types are NOT generated bysql_schema_create.mustache— they continue to be generated by the existingsql_schema_notify_trigger.mustachefrom domain entity models.
Parity result: all 10 entities produce zero logical diff. Only cosmetic differences:
- Template name in the
AUTO-GENERATEDcomment header (sql_schema_table_create.mustache→sql_schema_create.mustache). - Validation function comment wording (explanatory text, not SQL logic).