Skip to content

refactor: migrate _sql_constraints to models.Constraint (Odoo 19)#215

Open
kneckinator wants to merge 1 commit into
19.0from
refactor/sql-constraints-to-models-constraint
Open

refactor: migrate _sql_constraints to models.Constraint (Odoo 19)#215
kneckinator wants to merge 1 commit into
19.0from
refactor/sql-constraints-to-models-constraint

Conversation

@kneckinator
Copy link
Copy Markdown
Contributor

Summary

  • Odoo 19 removed the _sql_constraints = [(name, sql, message), ...] pattern; it's replaced by models.Constraint(sql, message) class attributes.
  • Migrates the three remaining sites: spp_scoring, spp_metric, spp_analytics.
  • Updates spp_metric/tests/test_metric_category.py to introspect constraints via _table_objects instead of _sql_constraints.

Notes on DB constraint names

The new system auto-prefixes the constraint name as {table}_{attr_name_without_underscore}:

  • spp_scoring.scoring.invalid.value: attr _name_uniqspp_scoring_invalid_value_name_uniq (matches the previous name exactly).
  • spp_metric.metric.category: attr _code_uniquespp_metric_category_code_unique (previously code_unique — name changes, but follows the same migration pattern already in use across spp_consent, spp_drims, spp_cel_domain, etc.).
  • spp_analytics.analytics.cache: attr _cache_key_uniquespp_analytics_cache_cache_key_unique (previously cache_key_unique).

The legacy (unprefixed) DB constraints on spp_metric_category and spp_analytics_cache won't be auto-dropped; if that's important for existing databases, a pre-init hook can be added in a follow-up.

Test plan

  • pytest spp_metric/tests/test_metric_category.py — verifies the constraint is reported via _table_objects
  • Module install/upgrade on a fresh DB for spp_scoring, spp_metric, spp_analytics
  • Verify UNIQUE violation raises the expected message on each model

Odoo 19 removed the _sql_constraints list-of-tuples in favor of
models.Constraint class attributes. Migrate the three remaining sites
(spp_scoring, spp_metric, spp_analytics) and update the metric_category
test to introspect constraints via _table_objects.
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request replaces legacy _sql_constraints lists with Odoo's newer models.Constraint objects across several models. A critical issue was identified in the test suite where the assertion for the unique constraint name will fail because Odoo 19 automatically prefixes constraint names with the table name. The reviewer suggested stripping this prefix in the test's set comprehension to ensure the assertion succeeds.

"""
# Verify the SQL constraint is defined
constraints = {name for name, _, _ in self.env["spp.metric.category"]._sql_constraints}
constraints = {obj.name for obj in self.env["spp.metric.category"]._table_objects.values()}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In Odoo 19, SQL constraints defined via models.Constraint are automatically prefixed with the table name (e.g., spp_metric_category_code_unique). As a result, checking for the bare constraint name "code_unique" on line 34 will fail because obj.name contains the fully prefixed name. Stripping the table prefix from the constraint names in the set comprehension ensures the test passes.

Suggested change
constraints = {obj.name for obj in self.env["spp.metric.category"]._table_objects.values()}
constraints = {obj.name.removeprefix("spp_metric_category_") for obj in self.env["spp.metric.category"]._table_objects.values()}

@codecov
Copy link
Copy Markdown

codecov Bot commented May 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.56%. Comparing base (dcafa51) to head (ca44b5e).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             19.0     #215      +/-   ##
==========================================
+ Coverage   71.27%   71.56%   +0.29%     
==========================================
  Files         976      979       +3     
  Lines       57736    58392     +656     
==========================================
+ Hits        41150    41791     +641     
- Misses      16586    16601      +15     
Flag Coverage Δ
spp_analytics 93.13% <100.00%> (ø)
spp_api_v2_gis 71.52% <ø> (ø)
spp_api_v2_simulation 71.12% <ø> (ø)
spp_base_common 90.26% <ø> (ø)
spp_dci_demo 69.23% <ø> (ø)
spp_indicator 96.55% <ø> (ø)
spp_metric 97.29% <100.00%> (ø)
spp_mis_demo_v2 73.48% <ø> (+3.63%) ⬆️
spp_programs 64.84% <ø> (ø)
spp_scoring 76.50% <100.00%> (+1.81%) ⬆️
spp_scoring_programs 35.29% <ø> (-6.57%) ⬇️
spp_security 66.66% <ø> (ø)
spp_simulation 79.58% <ø> (+9.50%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
spp_analytics/models/service_cache.py 100.00% <100.00%> (ø)
spp_metric/models/metric_category.py 100.00% <100.00%> (ø)
spp_scoring/models/scoring_invalid_value.py 100.00% <100.00%> (ø)

... and 15 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant