Skip to content

Stop mapping parameter_values.dynamic_id in API v1, keeping the column - #4353

Merged
anth-volk merged 5 commits into
masterfrom
v2plan/c1-51a-stop-mapping-parameter-values-dynamic-id
Nov 27, 2026
Merged

anth-volk merged 5 commits into
masterfrom
v2plan/c1-51a-stop-mapping-parameter-values-dynamic-id

Conversation

@anth-volk

Copy link
Copy Markdown
Collaborator

Fixes #4352

Summary

API v1 stops reading and writing parameter_values.dynamic_id, while the table keeps it. This lets #4351 (C1 · 53) drop the column while this release serves.

There is no revision. The table, its key to dynamics, ck_parameter_values_single_owner and both partial indexes don't change, and alembic -c alembic-v2.ini check finds no difference.

Why a release of its own: API v1's release workflow upgrades the schema before the new release takes traffic, and a whole-row ORM load names every mapped column. So the release that serves while #4351's revision runs must not name the column. #4347 (C1 · 50) was to make this change, but merged without it. The release before this one (#4347's and #4349's code) maps the column, which still exists, so it serves safely until this one is promoted.

This must be in production before #4351 merges.

Changes

  • Model: ParameterValue keeps dynamic_id as a SQLModel field with its foreign key, as rule api.data.orm-only requires, and __mapper_args__ = {"exclude_properties": ("dynamic_id",)} leaves it out of the mapper. This is how the report columns were left out of their mappers before revision 4e30076d4b9f (Stop mapping the report columns the next v2 revision drops #4333).
  • No statement names the column any longer. Since revision 7f7335f0eb31 no row has a dynamic_id, so dropping the filters changes no result:
    • the insert of a policy's values (POST /v2/policies and the dual-write v1-to-v2 policy copy) no longer sets it;
    • the current-law reads behind /v2/parameter-values, its item route, /v2/model-catalog/parameters and /v2/model-catalog/variables filter on policy_id IS NULL only;
    • catalog publication's insert of current-law values, its comparison of stored values, its uniqueness check and pruning no longer name it.
  • Grants: none names the column. Only a comment in privileges.py changes.
  • Docs: the Alembic skill's C1 · 50 section says API v1 stops mapping the column from this release. Both pruning descriptions (the skill and the Stage 9 guide) say pruning deletes values with no policy_id.

Query plans

Checked with query_plan on PostgreSQL 17, with a synthetic catalog of 540,000 current-law values and 4,000 policy values, after ANALYZE. Without dynamic_id IS NULL, the reads can't use uq_parameter_values_canonical_parameter_start_date, whose condition names dynamic_id:

Read Index now Index before
/v2/parameter-values by parameter_id, by names, or a whole version on a day ix_parameter_values_parameter_period, filtering policy_id IS NULL (about the same cost) uq_parameter_values_canonical_parameter_start_date
/v2/parameter-values/{id} pk_parameter_values pk_parameter_values
/v2/model-catalog/parameters and /variables (values by parameter name) uq_parameter_values_policy_parameter_start_date (policy_id IS NULL AND parameter_id = …) the same
publication's stored current-law values of a version ix_parameter_values_parameter_period the canonical index
publication's pruning delete hash join over a sequential scan the same
publication's uniqueness check index scan of ix_parameter_values_parameter_period: 220 ms index-only scan of the canonical index: 71 ms

Only publication's uniqueness check gets slower: about three times, once per publication. Over production's few million rows that is a second or two in a seed job of 17 to 20 minutes. #4351 replaces the canonical index with uq_parameter_values_current_law_parameter_start_date on policy_id IS NULL, which serves it again. This PR changes no index.

Testing

  • Red, then green: the tests were committed first, while failing (9e3a4a6b0), then the change (48d848c9b). Five new unit tests failed for the expected reason:
    • the mapper has no dynamic_id, while the table keeps it with its key, check and both partial indexes;
    • a whole-row select(ParameterValue) names no dynamic_id;
    • the policy insert names none (it did even without the explicit dynamic_id=None, since the ORM sends NULL for every mapped column without a default);
    • publication's insert, comparison, uniqueness check and pruning name no parameter_values.dynamic_id;
    • the current-law reads of /v2/parameter-values, its item route and /v2/model-catalog/parameters name none.
  • Re-adding any one of the six removed dynamic_id IS NULL filters on its own makes its test fail (checked with scratch edits, reverted).
  • Moved tests: tests that set or read the column through the model now write it through ParameterValueWithDynamic (tests/integration/parameter_values_with_dynamics.py, a column-only mapped class on a registry of its own, the same file Drop parameter_values.dynamic_id, its single-owner check and its per-dynamic index #4351 adds), or drop the filter. Tests of the column, the check and the per-dynamic index stay, since they still exist.
  • Every changed test passes by node ID: 47 unit, 17 PostgreSQL and 12 migration cases. alembic -c alembic-v2.ini check finds no difference. reconcile_v2_privileges.py apply --create-missing-roles and verify, lint-imports, python -m scripts.rules validate, run_quality_guards.py, ruff and ruff format pass. mypy on the changed policyengine_api/data/v2/ files reports only the sa_type overload errors these files already had, none on a changed line. export_migration_contracts.py changes nothing.
  • With the column dropped: against a disposable database upgraded to the head, with ALTER TABLE parameter_values DROP COLUMN dynamic_id run by hand (which also drops the check and both partial indexes, as Drop parameter_values.dynamic_id, its single-owner check and its per-dynamic index #4351's revision will), 36 PostgreSQL cases ran: publication, pruning and restoring, dynamics and the v1-to-v2 policy copy, /v2/parameter-values (list, item, current values), /v2/model-catalog/parameters and /variables, and policy creation. 33 passed. The other 3 failed only at a test helper's own read of the column through ParameterValueWithDynamic, after the service calls had succeeded; with that check removed they passed. A script created a policy, created it again (which reads its stored values as whole rows), and read it as GET /v2/policies/{id} and GET /v2/policies do: all succeeded. With master's code, the same database fails those reads and the policy create with UndefinedColumn.
  • Review: one adversarial review round found no bug. Its code-quality findings are fixed in 549c81dce (failing test) and 5ca9e3d55: DIRECT_SQLALCHEMY_EXCEPTIONS lists columns_left_out_of_the_mapper again, and two docs that said no model leaves a column out are corrected.
  • No workflow, Dockerfile or other deployment code changes.

Deploy order

  1. This PR merges and its release is in production. The release before it maps the column, which still exists.
  2. Drop parameter_values.dynamic_id, its single-owner check and its per-dynamic index #4351 (C1 · 53) merges after that, with its other conditions: the simulation service's Read dynamics from API v1's dynamic parameter tables, and stop mapping parameter_values.dynamic_id policyengine-sim-api#1129 in production, and the operator count of parameter_values rows with a dynamic_id at 0 in staging and production.

🤖 Generated with Claude Code

anth-volk and others added 5 commits November 27, 2026 18:09
…iling)

API v1's ParameterValue is to leave dynamic_id out of its mapper, and no
API v1 statement is to name the column, while the table keeps it with its
key, check and both partial indexes. New tests check the mapper and the
table, a whole-row read, the policy insert, publication's insert,
comparison, uniqueness check and pruning, and the current-law reads. They
fail now because the model maps the column and the code names it.

Tests that set or read the column through the model write it through
ParameterValueWithDynamic, the column-only class C1 · 53 adds (copied
verbatim from 3685235), or drop it where it only filtered, with
C1 · 53's own edits where they apply.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ParameterValue keeps dynamic_id as a SQLModel field with its foreign key,
and the single-owner check and both partial indexes stay in
__table_args__, so the table and alembic check don't change; there is no
revision. __mapper_args__ = {"exclude_properties": ("dynamic_id",)} leaves
the column out of the mapper, so a whole-row load selects it no more.

No API v1 statement names it any longer: the policy insert (POST
/v2/policies and the dual-write v1-to-v2 policy copy), the three
current-law reads of the metadata routes, publication's insert,
comparison and uniqueness check, and pruning. Since revision 7f7335f0eb31
no row has a dynamic_id, so dropping the filters changes no result. A
release of this code keeps serving while C1 · 53's revision drops the
column.

The privilege manifest's comment on pruning is updated; no grant changes.
The Alembic skill's C1 · 50 section and both pruning descriptions say so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 1's code-quality finding: ParameterValue uses
exclude_properties again, so DIRECT_SQLALCHEMY_EXCEPTIONS, the complete
list of direct-SQLAlchemy categories of the v2 models, should list
"columns_left_out_of_the_mapper" again, as it did while the report
columns were left out of their mappers (C1 · 40a). The test now expects
it and fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ew round 1)

Review round 1's code-quality findings:
- DIRECT_SQLALCHEMY_EXCEPTIONS lists "columns_left_out_of_the_mapper"
  again, with C1 · 40a's text, since ParameterValue leaves dynamic_id out
  of its mapper.
- The simulation role privilege tests' module docstring and their
  UNMAPPED_COLUMN_CLASSES comment said no model leaves a column out; they
  now say no model of the run tables does.
- The Alembic skill's "Columns a mapper leaves out" names
  parameter_values.dynamic_id as the current case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…_id mapping branch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.88%. Comparing base (f780eb6) to head (1d01f57).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4353      +/-   ##
==========================================
- Coverage   93.90%   93.88%   -0.03%     
==========================================
  Files         432      432              
  Lines       24195    24196       +1     
  Branches     3752     3752              
==========================================
- Hits        22720    22716       -4     
- Misses        846      852       +6     
+ Partials      629      628       -1     
Flag Coverage Δ
integration 59.29% <100.00%> (+<0.01%) ⬆️
unit 91.13% <100.00%> (+0.03%) ⬆️

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

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@anth-volk
anth-volk marked this pull request as ready for review November 27, 2026 15:01
@anth-volk
anth-volk merged commit 3eb9637 into master Nov 27, 2026
23 checks passed
@anth-volk
anth-volk deleted the v2plan/c1-51a-stop-mapping-parameter-values-dynamic-id branch November 27, 2026 15:02
anth-volk added a commit that referenced this pull request Nov 27, 2026
…rop branch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk added a commit that referenced this pull request Nov 27, 2026
…_id drop branch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch was previously deployed

1 inactive deployment
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.

Stop mapping parameter_values.dynamic_id in API v1 before the column is dropped

1 participant