Skip to content

Require every API v2 report's type in reports.type, and read it in API v1 - #4400

Merged
anth-volk merged 7 commits into
masterfrom
v2plan/d1-8-report-type
Dec 4, 2026
Merged

anth-volk merged 7 commits into
masterfrom
v2plan/d1-8-report-type

Conversation

@anth-volk

Copy link
Copy Markdown
Collaborator

Fixes #4399

Summary

Tracker task D1 · 8: every API v2 report names its type in reports.type, and API v1 reads it.

The revision. Revision 24b571d7d079, on 7dcd2cd14330, is the new v2 head (V2_ALEMBIC_HEAD_REVISION). It runs a data step, then the generated operations.

  • The data step is hand-written, as the user's explicit migration decision for D1 · 8 allows. It sets each report's type from its columns: household when household_id is set, budget_window when budget_window_size is set, and economy otherwise. It updates only rows whose stored type differs. Until D1 · 7a, the simulation service stored a budget window as economy, and some reports have no type.
  • The generated operations, exactly as Alembic wrote them:
    • reports.type becomes NOT NULL;
    • the check ck_reports_type is added. It requires the type to be one of the three, household exactly when household_id is set, and budget_window exactly when budget_window_size is set.
  • The downgrade drops the check and makes the column nullable again. The types the data step set stay.

The model. Report.type is ReportType = Literal["economy", "household", "budget_window"], with the same check.

API v1 reads the stored type. API v1 no longer works out a report's type from its columns:

  • report_kind and ReportKind are removed. Stored-report reads, reads of a named run, run lists, re-runs and their cliff check, budget-window year reads (require_budget_window_year) and published-report run selection all read reports.type.
  • set_report_type and ReportTypeImmutableError had no callers and are removed.

Saved-report writes read their report once. Creating or updating a saved report (/v2/user-reports) reads the report it names once, through the new connector read_named_report, which returns NamedReport. The budget-window check and the current-run check both use that one read.

  • On create, it reads the report whenever the request names one.
  • On update, it reads the report only when the request supplies report_id, budget_window_years or current_run_id.
  • One behaviour changes: an update that supplies budget_window_years or current_run_id without report_id now answers 404 if the saved report's own report isn't in its country. The RESTRICT foreign key and the writes' country checks keep that from happening.
  • docs/migration/stage-12a-user-login.md describes the read.

Clean-up from D1 · 0a's review:

  • Tests for a one-year budget window's current run, for a saved report moved from a budget-window run to a report run, and for current runs in the export and admin reads.
  • The report revision tests moved to tests/unit/v2/test_alembic_v2_report_revisions.py, so tests/unit/v2/test_alembic_v2_revisions.py drops from 1,465 to 1,242 lines.

Also:

  • Every test that stores a report gives it a type.
  • The Alembic v2 check's script runs the new revision tests.
  • The Alembic migrations guide gains a section for the revision.
  • A changelog fragment.

Report links are unaffected. A saved report's current run is still stored on the user-report association, whose ID is the report page's URL ID.

What the merge waits for

Earlier task Must be Why
D1 · 0, 0a, 0b, 1, 2, 3, 4 merged (all done) Earlier tasks of the substage.
D1 · 5, D1 · 6, D1 · 7 merged Earlier app v3 tasks of the substage. This PR uses nothing they deploy.
D1 · 7a deployed The simulation service writes every reports row. Until D1 · 7a is deployed, it stores a budget window as economy, which ck_reports_type refuses. So the revision must not run in an environment before D1 · 7a is deployed there.

D1 · 9 follows this PR. It updates the simulation service's model of reports (the required type and ck_reports_type) and moves its pinned API v1 commit. Until then, the simulation service's weekly comparison with this repository's master tables reports both differences.

This PR adds revision 24b571d7d079 on 7dcd2cd14330. #4396 (D2 · 3a) adds 6ec3d17299b6 on the same parent. Whichever merges second merges master and moves its down_revision to the other's revision. It does not build on another task's branch.

Testing

Tests that fail before the change come first. All tests were run individually by node ID against a disposable PostgreSQL 17 database. No full suite was run.

  • 9286d3f3 moves the four report revision tests unchanged. They pass.
  • 74c120cd adds the four current-run tests. They pass on their first run, since they cover existing behaviour.
  • 9a1a5f71 adds three tests that fail before the change, each on an assertion:
    • test_report_types_revision_is_generated_with_one_approved_data_step (assert 0 == 1: no revision file);
    • test_the_revision_sets_each_reports_type_then_requires_it (the revision isn't in the chain);
    • test_the_database_refuses_a_type_that_disagrees_with_the_report (DID NOT RAISE IntegrityError).
  • 36c91cda is the change. The three tests pass, and so do the tests it edits: 15 in all. One test for each test helper whose report insert gained a type also passes: 14 in all.
  • Round 1's fixes:
    • 8a9df7b1 adds the Alembic v2 check's two new script entries to test_reusable_v2_alembic_check_uses_only_disposable_postgres. It fails before the fix.
    • 682c2b04 adds them to the script, after which the test passes. It also updates the chain and model tests, which expected the old head and a nullable type; both now pass.
  • These all pass: alembic -c alembic-v2.ini check ("No new upgrade operations detected"), lint-imports, python -m scripts.rules validate, scripts/run_quality_guards.py, ruff, ruff format --check and the configured mypy check.

Review rounds

  • Round 1, on 36c91cda. Bugs found:

    • High: two unit tests still expected the old head and a nullable type. Fixed.
    • Medium: the Alembic v2 check didn't run the new revision tests. Fixed.

    Its code-quality notes:

    • Fixed: the missing Alembic guide section, stale module names in the guides, and wording in two docstrings.
    • Left: the update's supplied-field rules are spread over three functions.
  • Round 2, on 682c2b04: no bugs. Its code-quality notes, all left:

    • six test helpers each work out a report's type;
    • no test covers a stored type outside the three values, or that a write reads its report only once;
    • three docstrings and comments are slightly out of date: update_user_report doesn't mention the 404 above, and two comments attribute the window-size bounds to ck_reports_type rather than ck_reports_budget_window_size.

Bugs left: none.

Environment

This PR adds a test file to .github/scripts/test_alembic_v2_lifecycle.sh, which the alembic-v2-check workflow runs. No environment variable is added or changed.

Variable Read by Provider
V2_MIGRATION_DATABASE_URL the new integration tests literal value in the alembic-v2-check job's env:
V2_ALEMBIC_DISPOSABLE_TEST the new integration tests literal value in the alembic-v2-check job's env:
DEPLOYMENT_ENVIRONMENT the integration fixtures' role setup not set there; the fixture defaults to test

🤖 Generated with Claude Code

anth-volk and others added 6 commits December 3, 2026 22:52
The v2 revision test module was at 1,465 of the 1,500 lines allowed for
test code. The shared reports, published reports, published report images
and saved reports' current runs revision tests move, unchanged, to
test_alembic_v2_report_revisions, which imports their revision IDs from the
main module, as the household job revision module does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A one-year budget window is saved with null budget_window_years yet takes
its budget-window run and refuses its year's run; moving a saved budget
window to an economy report's run switches the stored current-run column;
the /v2/me/export and admin saved-items reads pass a non-null current run
through. They cover behaviour that already exists, so they pass before any
change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A new v2 revision must set each report's type from household_id and
budget_window_size (a budget window stored as economy, or a report with no
type, takes the type its columns give), then make reports.type required
with ck_reports_type: economy, household or budget_window, matching those
columns. The database then refuses a type that disagrees with the report.
These tests fail until the revision exists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A v2 revision (24b571d7d079) sets each stored report's type from its
household_id and budget_window_size, then makes reports.type NOT NULL
and adds ck_reports_type: economy, household or budget_window, agreeing
with those columns. The data step is the hand-written one approved for
D1 · 8; the operations are as generated.

API v1 reads reports.type wherever it told report types apart from their
columns (stored-report reads and re-runs, published-report runs), and
removes report_kind and the unused set_report_type. A saved report's
create or update reads the report it names once, through
read_named_report, and checks its budget window and current run against
that read. Tests and fixtures that store reports now give each its type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 1 found that the Alembic v2 check's script runs neither the
report types revision's integration tests nor the report revision unit
tests, so a regression in the data step or ck_reports_type could merge
unnoticed. The workflow test now asserts the script lists both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…mbic v2 check

Review round 1 found two unit tests still expecting the old schema: the
revision chain test named 7dcd2cd14330 as head, and the model test
expected reports.type to be nullable. They now expect 24b571d7d079 and a
required type. The Alembic v2 check's script now runs the report types
revision's integration tests and the report revision unit tests.

The Alembic guide gains a section for the revision (its data step, the
order D1 · 7a and D1 · 9 deploy in), and names the module that now checks
the earlier report revision files; the testing guide lists that module;
the user-login document says when a saved report's write reads its report.
The two test docstrings name the user's migration decision, as the
revision does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The cross-database integration check failed three cases of
test_a_malformed_or_incomplete_key_is_refused: their reports had no type,
so the database refused them for the required type before the key checks
the test expects. The reports are now economy reports.

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

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.93%. Comparing base (c0deff0) to head (675e20f).
⚠️ Report is 17 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4400      +/-   ##
==========================================
- Coverage   93.95%   93.93%   -0.03%     
==========================================
  Files         459      459              
  Lines       26287    26285       -2     
  Branches     4070     4071       +1     
==========================================
- Hits        24699    24690       -9     
- Misses        902      909       +7     
  Partials      686      686              
Flag Coverage Δ
integration 60.90% <100.00%> (+0.04%) ⬆️
unit 90.37% <76.19%> (-0.05%) ⬇️

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 December 4, 2026 18:15
@anth-volk
anth-volk merged commit 57202c0 into master Dec 4, 2026
23 checks passed
anth-volk added a commit to PolicyEngine/policyengine-sim-api that referenced this pull request Dec 4, 2026
PolicyEngine/policyengine-api#4400 merged as 57202c04, on API v1's master. Between its head 675e20f4, which this branch was tested against, and the merge commit, API v1 changes no v2 revision, privilege code or migration configuration, so the tables its chain builds are the same.

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.

Require every API v2 report's type in reports.type, and read it in API v1

1 participant