Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/scripts/test_alembic_v2_lifecycle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ uv run --frozen --extra dev pytest -q \
tests/integration/test_alembic_v2_parameter_values_dynamic_id_drop.py \
tests/integration/test_alembic_v2_published_reports.py \
tests/integration/test_alembic_v2_inline_household_jobs.py \
tests/integration/test_alembic_v2_published_report_images.py
tests/integration/test_alembic_v2_published_report_images.py \
tests/integration/test_alembic_v2_saved_report_current_runs.py
4 changes: 2 additions & 2 deletions .github/workflows/v2-integration-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ jobs:
run: uv run --frozen --extra dev python scripts/reconcile_v2_privileges.py apply --create-missing-roles
- name: Verify the managed v2 roles hold their declared privileges
run: uv run --frozen --extra dev python scripts/reconcile_v2_privileges.py verify
- name: Test v2 metadata publication, resource routes, the countries list, reports, simulations and users' report runs
run: uv run --frozen --extra dev coverage run --branch -m pytest -q tests/integration/test_v2_catalog_publication.py tests/integration/test_v2_catalog_publication_time_periods.py tests/integration/test_v2_catalog_publication_pruning.py tests/integration/test_v2_catalog_publication_dynamic_parameters.py tests/integration/test_v2_catalog_spm_availability.py tests/integration/test_v2_catalog_statistics.py tests/integration/test_v2_metadata_routes.py tests/integration/test_v2_dynamic_parameter_routes.py tests/integration/test_v2_country_routes.py tests/integration/test_v2_report_reads.py tests/integration/test_v2_report_creation.py tests/integration/test_v2_budget_window_report_reads.py tests/integration/test_v2_budget_window_report_creation.py tests/integration/test_v2_report_runs.py tests/integration/test_v2_report_reads_by_run.py tests/integration/test_v2_user_report_runs.py tests/integration/test_v2_user_report_run_requesters.py tests/integration/test_v2_user_budget_window_runs.py tests/integration/test_v2_forced_report_reruns.py tests/integration/test_v2_user_report_views.py
- name: Test v2 metadata publication, resource routes, the countries list, reports, simulations, users' report runs and saved reports' current runs
run: uv run --frozen --extra dev coverage run --branch -m pytest -q tests/integration/test_v2_catalog_publication.py tests/integration/test_v2_catalog_publication_time_periods.py tests/integration/test_v2_catalog_publication_pruning.py tests/integration/test_v2_catalog_publication_dynamic_parameters.py tests/integration/test_v2_catalog_spm_availability.py tests/integration/test_v2_catalog_statistics.py tests/integration/test_v2_metadata_routes.py tests/integration/test_v2_dynamic_parameter_routes.py tests/integration/test_v2_country_routes.py tests/integration/test_v2_report_reads.py tests/integration/test_v2_report_creation.py tests/integration/test_v2_budget_window_report_reads.py tests/integration/test_v2_budget_window_report_creation.py tests/integration/test_v2_report_runs.py tests/integration/test_v2_report_reads_by_run.py tests/integration/test_v2_user_report_runs.py tests/integration/test_v2_user_report_run_requesters.py tests/integration/test_v2_user_budget_window_runs.py tests/integration/test_v2_forced_report_reruns.py tests/integration/test_v2_user_report_views.py tests/integration/test_v2_saved_report_current_runs.py
- name: Test v2 policy and dynamic persistence and immediate v1 mirroring
run: uv run --frozen --extra dev coverage run -a --branch -m pytest -q tests/integration/test_v2_policy_persistence.py tests/integration/test_v2_dynamic_persistence.py tests/integration/test_v1_policy_dual_write.py tests/integration/test_mysql_policy_dual_write.py tests/integration/test_v2_user_policy_mirroring.py tests/integration/test_v1_user_policy_dual_write.py
- name: Test signed-in identity provisioning, user associations, saved detailed-save records, user types, published reports and the one-time migration of saved records
Expand Down
1 change: 1 addition & 0 deletions changelog.d/v2-saved-report-current-run.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A saved report (`/v2/user-reports`) stores which run of its report its page shows, as `current_run_id`: one of the report's runs, or a budget window's budget-window run. The owner sets it when saving the report and with `PATCH`, which refuses a run of another report and clears it when naming another report without one of its runs; the owner's reads and `GET /v2/shared-reports/{id}` return it.
14 changes: 7 additions & 7 deletions docs/engineering/migration-contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,17 @@ Generated from `policyengine_api/migration_registry.py` and `tests/contract/regi

| Method | Path | Status | Route group | Required headers | Stable response fields |
| --- | --- | ---: | --- | --- | --- |
| `POST` | `/v2/user-reports?country_id=us` | 201 | `report` | `Authorization` | `status`, `message`, `result.item.id`, `result.item.country_id`, `result.item.user_id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.description`, `result.item.user_policy_id`, `result.item.baseline_user_policy_id`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.last_run_at`, `result.item.created_at`, `result.item.updated_at` |
| `GET` | `/v2/user-reports/{association_id}?country_id=us` | 200 | `report` | `Authorization` | `status`, `message`, `result.item.id`, `result.item.country_id`, `result.item.user_id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.description`, `result.item.user_policy_id`, `result.item.baseline_user_policy_id`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.last_run_at`, `result.item.created_at`, `result.item.updated_at` |
| `POST` | `/v2/user-reports?country_id=us` | 201 | `report` | `Authorization` | `status`, `message`, `result.item.id`, `result.item.country_id`, `result.item.user_id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.description`, `result.item.user_policy_id`, `result.item.baseline_user_policy_id`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.last_run_at`, `result.item.current_run_id`, `result.item.created_at`, `result.item.updated_at` |
| `GET` | `/v2/user-reports/{association_id}?country_id=us` | 200 | `report` | `Authorization` | `status`, `message`, `result.item.id`, `result.item.country_id`, `result.item.user_id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.description`, `result.item.user_policy_id`, `result.item.baseline_user_policy_id`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.last_run_at`, `result.item.current_run_id`, `result.item.created_at`, `result.item.updated_at` |
| `GET` | `/v2/user-reports?country_id=us` | 200 | `report` | `Authorization` | `status`, `message`, `result.items`, `result.offset`, `result.limit`, `result.has_more` |
| `PATCH` | `/v2/user-reports/{association_id}?country_id=us` | 200 | `report` | `Authorization` | `status`, `message`, `result.item.id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.description`, `result.item.user_policy_id`, `result.item.baseline_user_policy_id`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.last_run_at`, `result.item.updated_at` |
| `PATCH` | `/v2/user-reports/{association_id}?country_id=us` | 200 | `report` | `Authorization` | `status`, `message`, `result.item.id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.description`, `result.item.user_policy_id`, `result.item.baseline_user_policy_id`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.last_run_at`, `result.item.current_run_id`, `result.item.updated_at` |
| `DELETE` | `/v2/user-reports/{association_id}?country_id=us` | 204 | `report` | `Authorization` | |
| `POST` | `/v2/user-reports?country_id=us` | 201 | `report` | `PolicyEngine-Unauthenticated-User` | `status`, `message`, `result.item.id`, `result.item.country_id`, `result.item.user_id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.description`, `result.item.user_policy_id`, `result.item.baseline_user_policy_id`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.last_run_at`, `result.item.created_at`, `result.item.updated_at` |
| `GET` | `/v2/user-reports/{association_id}?country_id=us` | 200 | `report` | `PolicyEngine-Unauthenticated-User` | `status`, `message`, `result.item.id`, `result.item.country_id`, `result.item.user_id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.description`, `result.item.user_policy_id`, `result.item.baseline_user_policy_id`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.last_run_at`, `result.item.created_at`, `result.item.updated_at` |
| `POST` | `/v2/user-reports?country_id=us` | 201 | `report` | `PolicyEngine-Unauthenticated-User` | `status`, `message`, `result.item.id`, `result.item.country_id`, `result.item.user_id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.description`, `result.item.user_policy_id`, `result.item.baseline_user_policy_id`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.last_run_at`, `result.item.current_run_id`, `result.item.created_at`, `result.item.updated_at` |
| `GET` | `/v2/user-reports/{association_id}?country_id=us` | 200 | `report` | `PolicyEngine-Unauthenticated-User` | `status`, `message`, `result.item.id`, `result.item.country_id`, `result.item.user_id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.description`, `result.item.user_policy_id`, `result.item.baseline_user_policy_id`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.last_run_at`, `result.item.current_run_id`, `result.item.created_at`, `result.item.updated_at` |
| `GET` | `/v2/user-reports?country_id=us` | 200 | `report` | `PolicyEngine-Unauthenticated-User` | `status`, `message`, `result.items`, `result.offset`, `result.limit`, `result.has_more` |
| `PATCH` | `/v2/user-reports/{association_id}?country_id=us` | 200 | `report` | `PolicyEngine-Unauthenticated-User` | `status`, `message`, `result.item.id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.description`, `result.item.user_policy_id`, `result.item.baseline_user_policy_id`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.last_run_at`, `result.item.updated_at` |
| `PATCH` | `/v2/user-reports/{association_id}?country_id=us` | 200 | `report` | `PolicyEngine-Unauthenticated-User` | `status`, `message`, `result.item.id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.description`, `result.item.user_policy_id`, `result.item.baseline_user_policy_id`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.last_run_at`, `result.item.current_run_id`, `result.item.updated_at` |
| `DELETE` | `/v2/user-reports/{association_id}?country_id=us` | 204 | `report` | `PolicyEngine-Unauthenticated-User` | |
| `GET` | `/v2/shared-reports/{association_id}?country_id=us` | 200 | `report` | none | `status`, `message`, `result.item.id`, `result.item.country_id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.baseline_reform`, `result.item.last_run_at`, `result.item.created_at`, `result.item.updated_at` |
| `GET` | `/v2/shared-reports/{association_id}?country_id=us` | 200 | `report` | none | `status`, `message`, `result.item.id`, `result.item.country_id`, `result.item.report_id`, `result.item.legacy_report_id`, `result.item.name`, `result.item.source_note`, `result.item.provisions`, `result.item.validation`, `result.item.budget_window_years`, `result.item.baseline_reform`, `result.item.last_run_at`, `result.item.current_run_id`, `result.item.created_at`, `result.item.updated_at` |

### `user_account_v2`

Expand Down
53 changes: 53 additions & 0 deletions docs/engineering/skills/alembic-migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1944,3 +1944,56 @@ which the Alembic v2 check runs, downgrades to the parent and upgrades again.
integration check runs, checks the key, the checks and the deletions as
API v1's runtime role, which holds `SELECT, INSERT, UPDATE, DELETE` on the
table.

### API v2 saved reports' current runs

Revision `7dcd2cd14330` (parent `50430a2da57f`, ID chosen with `--rev-id`)
was autogenerated after `UserReportAssociation`
(`policyengine_api/data/v2/models/associations.py`) and `ReportRun`
(`policyengine_api/data/v2/models/reports.py`) changed. A saved report stores
the run of its report its page shows (D1 · 0a), which the API answers as
`current_run_id`:

- `user_report_associations` gains two nullable columns, each indexed:
`current_report_run_id`, a report run of an economy or household report,
and `current_budget_window_run_id`, a budget window's budget-window run;
- each is a foreign key together with `report_id`
(`fk_user_report_associations_current_report_run` to `report_runs (id,
report_id)`, `fk_user_report_associations_current_budget_window_run` to
`budget_window_runs (id, report_id)`), `ON DELETE RESTRICT`, so the run is
one of the saved report's own report's and can't be deleted while a saved
report shows it. The keys check the run's report, not its kind: a budget
window's years are report runs of the window, and only API v2's write
check refuses one as the window's current run;
- `ck_user_report_associations_one_current_run` allows at most one of them,
and `ck_user_report_associations_current_run_report` allows either only
with `report_id`, since a key with a null column isn't checked;
- `report_runs` gains `uq_report_runs_id_report` on `(id, report_id)`, which
the first key refers to; `budget_window_runs` already had
`uq_budget_window_runs_id_report`.

The operations are exactly as generated. Existing saved reports start with
no current run. The downgrade drops the two columns, with the runs they hold,
and the constraints.

Deploying it:

- **The simulation service's models.** PolicyEngine/policyengine-sim-api
models `report_runs` in `policyengine-simulation-persistence` and compares
its constraints by name with the tables this repository's `master` builds
(its weekly `public-api-schema-drift.yml`, and its pull requests' job at
its pinned commit). Its `ReportRun` needs `uq_report_runs_id_report` too,
or that weekly comparison fails once this revision is on `master`.
- **Releases from before the revision.** One still serving during a
deploy's traffic shift, or restored by a rollback, doesn't know the
columns: repointing a saved report that has a current run to another
report doesn't clear it, so the new key refuses the update. Nothing sets a
current run until app v3 sends one.

`tests/unit/v2/test_alembic_v2_revisions.py` checks the revision file.
`tests/integration/test_alembic_v2_saved_report_current_runs.py`, which the
Alembic v2 check runs, compares the revision and its downgrade with the
parent schema and checks each refusal as the database's owner.
`tests/integration/test_v2_saved_report_current_runs.py`, which the
cross-database integration check runs, checks the routes as API v1's runtime
role.
7 changes: 7 additions & 0 deletions docs/generated/migration_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,7 @@
"result.item.validation",
"result.item.budget_window_years",
"result.item.last_run_at",
"result.item.current_run_id",
"result.item.created_at",
"result.item.updated_at"
]
Expand Down Expand Up @@ -1200,6 +1201,7 @@
"result.item.validation",
"result.item.budget_window_years",
"result.item.last_run_at",
"result.item.current_run_id",
"result.item.created_at",
"result.item.updated_at"
]
Expand Down Expand Up @@ -1244,6 +1246,7 @@
"result.item.validation",
"result.item.budget_window_years",
"result.item.last_run_at",
"result.item.current_run_id",
"result.item.updated_at"
]
},
Expand Down Expand Up @@ -1282,6 +1285,7 @@
"result.item.validation",
"result.item.budget_window_years",
"result.item.last_run_at",
"result.item.current_run_id",
"result.item.created_at",
"result.item.updated_at"
]
Expand Down Expand Up @@ -1311,6 +1315,7 @@
"result.item.validation",
"result.item.budget_window_years",
"result.item.last_run_at",
"result.item.current_run_id",
"result.item.created_at",
"result.item.updated_at"
]
Expand Down Expand Up @@ -1355,6 +1360,7 @@
"result.item.validation",
"result.item.budget_window_years",
"result.item.last_run_at",
"result.item.current_run_id",
"result.item.updated_at"
]
},
Expand Down Expand Up @@ -1388,6 +1394,7 @@
"result.item.budget_window_years",
"result.item.baseline_reform",
"result.item.last_run_at",
"result.item.current_run_id",
"result.item.created_at",
"result.item.updated_at"
]
Expand Down
8 changes: 7 additions & 1 deletion docs/migration/stage-12a-user-login.md
Original file line number Diff line number Diff line change
Expand Up @@ -596,13 +596,19 @@ A saved report's ID is its shareable link, so `GET /v2/shared-reports/{id}`
takes no credential and sits outside the authenticated user-report router. It
never provisions a user. Its item is `{id, country_id, report_id,
legacy_report_id, name, source_note, provisions, validation,
budget_window_years, baseline_reform, last_run_at, created_at, updated_at}`:
budget_window_years, baseline_reform, last_run_at, current_run_id,
created_at, updated_at}`:

- `user_id`, `user_policy_id`, and `baseline_user_policy_id` are left out;
the owner and the owner's saved reforms stay private.
- `baseline_reform` is `{"name": ...}` when the report was compared with one
of the owner's saved reforms, and null otherwise, including after that
reform is deleted.
- `current_run_id` is the run of the detailed-save report the report's page
shows, as on the owner's read (D1 · 0a): one of its report runs, or a
budget window's budget-window run, as `GET /v2/reports/{id}?run_id=` takes
it. Only the owner sets it, saving the report or with `PATCH`; there is no
re-run from a shared link.
- Anyone holding the ID can read the report's name, source note, and
provisions. There is no shared list or search, and the owner routes stay
owner-scoped.
Expand Down
Loading
Loading