Skip to content

Drop parameter_values.dynamic_id, its single-owner check and its per-dynamic index - #4351

Merged
anth-volk merged 9 commits into
masterfrom
v2plan/c1-53-drop-parameter-values-dynamic-id
Nov 27, 2026
Merged

anth-volk merged 9 commits into
masterfrom
v2plan/c1-53-drop-parameter-values-dynamic-id

Conversation

@anth-volk

@anth-volk anth-volk commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4350

Summary

Revision 1c651058fac5, on 7f7335f0eb31 (#4347), drops what dynamics' values needed in parameter_values. Since 7f7335f0eb31 those values are dynamic_parameter_values rows, so a parameter_values row is now a policy's value or a current-law value.

Based on master, where #4347 (C1 · 50), #4349 (C1 · 51) and #4353 (C1 · 51a, merged 2026-11-27 as 3eb9637a6) have merged. #4353 leaves dynamic_id out of ParameterValue's mapper while the table keeps it; its branch was merged in here with each conflict resolved to the column's removal.

The revision was autogenerated with --rev-id 1c651058fac5, with no post-generation correction. It drops:

  • parameter_values.dynamic_id, with its key fk_parameter_values_dynamic_id_dynamics;
  • the check ck_parameter_values_single_owner;
  • the partial unique index uq_parameter_values_dynamic_parameter_start_date.

The current-law index is renamed. uq_parameter_values_canonical_parameter_start_date had the condition policy_id IS NULL AND dynamic_id IS NULL, and PostgreSQL drops an index whose condition names a dropped column. Alembic compares an index's condition by name only. So the model renames it uq_parameter_values_current_law_parameter_start_date, with the condition policy_id IS NULL, and autogenerate replaces it.

The downgrade adds the column back empty, with its key, the check and both old indexes. That way 7f7335f0eb31's downgrade can copy dynamics' values back into parameter_values with their dynamic_id.

Code:

  • ParameterValue loses the field, the check and the per-dynamic index.
  • These no longer name the column: current-law reads (/v2/parameter-values, /v2/model-catalog/parameters), publication's comparison and uniqueness check, pruning, publication's insert, and the insert of a policy's values.
  • V2_ALEMBIC_HEAD_REVISION is 1c651058fac5.

Grants: no role's privilege names the column. Every grant on parameter_values is on the whole table.

Docs: alembic-migrations.md gains the section "API v2 parameter values without dynamics", and its pruning wording, the downgrade-chain paragraph and the dynamics sections are updated. The Stage 9 guide's pruning wording is updated too.

Decisions (the user, 2026-11-26)

  1. An earlier release stops mapping the column. The deploy upgrades the schema before the new release takes traffic, and a whole-row load names every mapped column, so the API v1 release that serves while this revision runs must leave dynamic_id out of ParameterValue's mapper. Keep dynamic parameters and their values in their own tables #4347 was to do it, but merged without that change, so tracker task C1 · 51a does it in its own pull request and release, Stop mapping parameter_values.dynamic_id in API v1, keeping the column #4353 (decided 2026-11-27). Until it is in production, master maps the column.

  2. The current-law index is renamed in the generated order, rather than corrected by hand.

  3. An operator counts first. A row that still has a dynamic_id becomes a current-law value when the column goes. That happens silently, unless it shares its parameter and start date with an existing current-law value; then the new index fails the upgrade and nothing changes. None is expected, but before merging, run this read-only count in staging and production. Both counts must be 0:

    SELECT count(*) FROM parameter_values WHERE dynamic_id IS NOT NULL;

Deploy order

A merge to master releases this and runs the revision in staging and production in one release run. So it merges only after all of these hold, in both environments:

  1. Stop mapping parameter_values.dynamic_id in API v1, keeping the column #4353 (C1 · 51a, merged as 3eb9637a6), which stops mapping ParameterValue.dynamic_id, is in production. Keep dynamic parameters and their values in their own tables #4347 (C1 · 50, with revision 7f7335f0eb31) and Serve dynamic parameters as their own v2 metadata category, US simulation.* included #4349 (C1 · 51) have merged; their release must be in production too.
  2. The simulation service's Read dynamics from API v1's dynamic parameter tables, and stop mapping parameter_values.dynamic_id policyengine-sim-api#1129 (C1 · 52) is in production. Its older releases map parameter_values.dynamic_id, so every policy read would fail once the column is gone.
  3. The operator's count above is 0 in both environments. Done 2026-11-27: run read-only through the Supabase CLI, it returned 0 in staging and in production, both at revision 7f7335f0eb31.

During the upgrade, the drop of the old index locks parameter_values until the migration transaction commits. Building the new index reads every current-law value, millions of rows, so reads and writes of the table wait until the build finishes. API v1 and the simulation service time statements out after 5 seconds, so a longer wait fails them. For comparison, 9d1d12a7ac58's index build, which read every row but indexed only dynamics' rows, took about 10 seconds in production. In production nothing reads these rows through the v2 routes yet. The dual-write v1-to-v2 policy copy writes them; a failed copy is logged, and the v1 response is unchanged.

Simulation service

Tested from #1129's branch (2db16a87) against a database at 1c651058fac5, with SIMULATION_REQUIRE_PUBLIC_API_SCHEMA=1. 299 test cases pass, none skipped. They are #1129's changed tests and the six schema comparison tests that the weekly drift run against master also selects.

The simulation service's ParameterValue maps no dynamic_id, and its comparison checks only the columns it reads. Its test helpers write no dynamic_id, and its readiness check grants SELECT on the whole table. It needs nothing beyond #1129.

Testing

  • The tests were committed first, while failing (3685235ef), then the change (57daf0884). Every changed test passes by node ID: 43 unit cases, 18 migration cases and 17 role-based cases.
  • The new tests/integration/test_alembic_v2_parameter_values_dynamic_id_drop.py checks:
    • the upgrade, and its exact downgrade, against the parent schema built from the base;
    • that rows survive, and the new index;
    • a downgrade through 7f7335f0eb31 and back;
    • what a row still naming a dynamic becomes;
    • the deploy's privilege steps.
  • Tests of earlier revisions use ParameterValueWithDynamic, a mapped class of the table as those revisions leave it.
  • The dynamics revisions' unit tests moved, unchanged, to tests/unit/v2/test_alembic_v2_dynamic_revisions.py, which keeps test_alembic_v2_revisions.py under the 1,500-line limit.
  • Review round 1 found no bug. Its one code-quality finding was fixed in 33e14c352: the dynamic-parameter pruning test now stores a dynamic's value and checks that pruning keeps it. It fails with pruning's dynamic_id IS NULL filter removed.
  • 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, ruff format and mypy pass. mypy reports only the seven errors metadata.py already had, on lines this PR doesn't change. export_migration_contracts.py changes nothing.
  • CI: .github/scripts/test_alembic_v2_lifecycle.sh now runs the two new test modules. The only environment variables they read are V2_MIGRATION_DATABASE_URL and V2_ALEMBIC_DISPOSABLE_TEST. Both are literal values in the postgres-lifecycle job's env: in alembic-v2-check.yml, which pr.yml and push.yml call. DEPLOYMENT_ENVIRONMENT isn't set there, so the fixtures use test. No secret is involved.

Files that #4345, #4347 or #4349 also change

C1 · 51a's change that stops mapping the column touches the same dynamic_id lines. When this branch merges it, they resolve to their removal.

🤖 Generated with Claude Code

anth-volk and others added 3 commits November 26, 2026 18:41
Revision 1c651058fac5 is to drop parameter_values.dynamic_id with its key
to dynamics, ck_parameter_values_single_owner and the per-dynamic partial
unique index, and to replace the current-law index, whose condition named
the column, with uq_parameter_values_current_law_parameter_start_date on
policy_id IS NULL.

- A Postgres test of the revision: the schema change and its exact
  downgrade against the parent built from the base, surviving rows and the
  new index, a downgrade through 7f7335f0eb31 and back, what a row still
  naming a dynamic becomes, and the deploy's privilege steps.
- The revision chain, the generated operations, the model's columns,
  checks and indexes, the lifecycle head check and CI's lifecycle script.
- Tests that stored or read a dynamic's value in parameter_values at the
  head drop it; tests of earlier revisions use ParameterValueWithDynamic,
  a column-only class of the table as those revisions leave it.
- The dynamics revisions' unit tests move, unchanged, from
  test_alembic_v2_revisions to test_alembic_v2_dynamic_revisions, which
  keeps the first under the 1,500-line limit for test code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Revision 1c651058fac5, on 7f7335f0eb31, autogenerated with no corrections
after ParameterValue lost dynamic_id, ck_parameter_values_single_owner and
uq_parameter_values_dynamic_parameter_start_date. 7f7335f0eb31 moved every
dynamic's values to dynamic_parameter_values, so a parameter_values row is
now a policy's value or a current-law value.

PostgreSQL drops an index whose condition names a dropped column, and
Alembic compares an index's condition by name only, so the current-law
index is renamed uq_parameter_values_current_law_parameter_start_date, on
policy_id IS NULL (the user's decision 2). The downgrade restores the
column, its key, the check and both indexes, which 7f7335f0eb31's
downgrade needs to copy dynamics' values back.

- Current-law reads, publication's comparison and uniqueness check,
  pruning and publication's insert no longer name dynamic_id; a policy's
  values are inserted without it.
- V2_ALEMBIC_HEAD_REVISION, CI's lifecycle script, the alembic-migrations
  skill (a section with the deploy order and the operator's read-only
  count, the user's decisions 1 and 3), the Stage 9 guide and the testing
  skill follow.
- No role's privilege names the column.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 1 found no bug. Its one code-quality finding: once
parameter_values lost dynamic_id, no test stored a dynamic's value where
pruning could delete it, so nothing noticed if pruning stopped keeping
dynamic_parameter_values rows with a dynamic_id.

The pruning test of the dynamic-parameter publication module now stores a
US dynamic on the old version, with a value of one of its model assumptions
from the date its current-law value starts, and checks it is still there
after pruning. With pruning's dynamic_id IS NULL filter removed, the test
fails. The pruning module's docstring names where this is checked.

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

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.87%. Comparing base (65f76da) to head (e7ec821).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4351      +/-   ##
==========================================
- Coverage   93.90%   93.87%   -0.03%     
==========================================
  Files         432      432              
  Lines       24196    24194       -2     
  Branches     3752     3752              
==========================================
- Hits        22721    22713       -8     
- Misses        846      852       +6     
  Partials      629      629              
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.

…arameter_values dynamic_id drop branch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Base automatically changed from v2plan/c1-51-dynamic-parameter-routes to master November 27, 2026 13:32
anth-volk and others added 2 commits November 27, 2026 17:36
…ues dynamic_id drop branch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
C1 · 50 (#4347) merged without leaving dynamic_id out of ParameterValue's
mapper, so the skill section's deploy order names C1 · 51a, which the user
added to do it in its own release before this revision runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
C1 · 51a (v2plan/c1-51a-stop-mapping-parameter-values-dynamic-id at
1d01f57, with master at f780eb6) leaves dynamic_id out of
ParameterValue's mapper while the table keeps it. This branch drops the
column, so every conflict resolves to its removal:

- ParameterValue has no dynamic_id field and no __mapper_args__.
- Tests of the check and the per-dynamic index at the head
  (test_dynamic_table_models, test_model_persistence, test_alembic_v2_dynamics,
  test_v2_dynamic_persistence) stay removed, with their imports.
- The skill's C1 · 50 section names revision 1c651058fac5 and C1 · 51a's
  release; "Columns a mapper leaves out" says dynamic_id was left out until
  that revision.

Also undone, as they hold only while the table keeps the column:
DIRECT_SQLALCHEMY_EXCEPTIONS' "columns_left_out_of_the_mapper", the role
privilege test's wording, and
test_models::test_parameter_values_keep_dynamic_id_which_the_model_no_longer_maps.
C1 · 51a's statement tests, which check that no read, publication insert
or policy insert names parameter_values.dynamic_id, stay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk changed the base branch from master to v2plan/c1-51a-stop-mapping-parameter-values-dynamic-id November 27, 2026 14:41
Base automatically changed from v2plan/c1-51a-stop-mapping-parameter-values-dynamic-id to master November 27, 2026 15:02
…rop branch

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review November 27, 2026 19:12
@anth-volk
anth-volk merged commit 92882cc into master Nov 27, 2026
24 checks passed
@anth-volk
anth-volk deleted the v2plan/c1-53-drop-parameter-values-dynamic-id branch November 27, 2026 19:12

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.

Drop parameter_values.dynamic_id, its single-owner check and its per-dynamic index

1 participant