fix(sql): EMISS PK includes period_type so yearly+Q1 coexist (#1606 follow-up) #1706

Merged
lekss361 merged 2 commits from fix/emiss-pk-granularity-1687 into main 2026-06-17 18:46:01 +00:00
Owner

Проблема (ревью #1687, решение владельца)

#1606 починил in-memory dedup, но DB-PK (indicator_type, region, obs_date) всё равно коллидировал год/Q1 на ON CONFLICT (оба month=1) → один перетирал другой.

Решение

Миграция 163_emiss_pk_period_type.sql: ADD COLUMN period_type TEXT NOT NULL DEFAULT 'unknown', backfill emiss→'quarterly', PK → (indicator_type, region, obs_date, period_type), индекс пересоздан. rosstat_emiss.py EmissRow.period_type из granularity; upsert-SQL (rosstat_macro_sync.py, cbr_macro_sync.py) пишут period_type + новый conflict target.

Verify (live DB через MCP)

PK подтверждён на 4 колонки; year+quarter строки для одной region/date сосуществуют; ON CONFLICT обновляет только свою granularity. pytest (emiss/rosstat) → 49 passed, 1 skipped.

⚠️ Миграция меняет PK на проде (auto-apply)

Существующие схлопнутые строки backfill'ятся в 'quarterly'; годовые перезаполнятся при следующем scrape.

## Проблема (ревью #1687, решение владельца) #1606 починил in-memory dedup, но DB-PK `(indicator_type, region, obs_date)` всё равно коллидировал год/Q1 на `ON CONFLICT` (оба month=1) → один перетирал другой. ## Решение Миграция `163_emiss_pk_period_type.sql`: `ADD COLUMN period_type TEXT NOT NULL DEFAULT 'unknown'`, backfill emiss→'quarterly', PK → `(indicator_type, region, obs_date, period_type)`, индекс пересоздан. `rosstat_emiss.py` `EmissRow.period_type` из granularity; upsert-SQL (`rosstat_macro_sync.py`, `cbr_macro_sync.py`) пишут period_type + новый conflict target. ## Verify (live DB через MCP) PK подтверждён на 4 колонки; year+quarter строки для одной region/date сосуществуют; ON CONFLICT обновляет только свою granularity. `pytest` (emiss/rosstat) → **49 passed, 1 skipped**. ## ⚠️ Миграция меняет PK на проде (auto-apply) Существующие схлопнутые строки backfill'ятся в 'quarterly'; годовые перезаполнятся при следующем scrape.
lekss361 added 1 commit 2026-06-17 18:28:59 +00:00
fix(sql): EMISS PK includes period_type so yearly+Q1 coexist (#1606 follow-up)
Some checks failed
CI / backend-tests (push) Has been cancelled
CI / frontend-tests (push) Has been cancelled
CI / openapi-codegen-check (push) Has been cancelled
CI / changes (push) Has been cancelled
CI / changes (pull_request) Successful in 6s
CI / backend-tests (pull_request) Has been cancelled
CI / frontend-tests (pull_request) Has been cancelled
CI / openapi-codegen-check (pull_request) Has been cancelled
5170107405
Adds period_type TEXT NOT NULL DEFAULT 'unknown' to macro_indicator and
widens the PRIMARY KEY to (indicator_type, region, obs_date, period_type).

Before: yearly aggregate ('год'→obs_date YYYY-01-01) and Q1 ('I квартал'→
same date) shared the same PK slot → ON CONFLICT DO UPDATE made them
overwrite each other despite the in-memory dedup fix in #1687.

After: each EmissRow carries period_type from _emiss_period_granularity
('year'/'quarter'/'month'); DB ON CONFLICT targets include it, so
yearly+Q1 rows genuinely coexist.

Non-EMISS sources (CBR, rosstat open-data, domrf) use period_type='unknown'
(literal in SQL) — they are disambiguated by obs_date already, so the
wider PK is backward-compatible. All ON CONFLICT clauses in
cbr_macro_sync.py and rosstat_macro_sync.py updated accordingly.

Migration: 163_emiss_pk_period_type.sql (idempotent BEGIN/COMMIT).
Tests: 49 passed (emiss + rosstat suite), ruff clean, py_compile OK.
bot-backend added 1 commit 2026-06-17 18:45:22 +00:00
fix(sql): emiss backfill period_type='quarter' to match scraper (#1606 review)
Some checks failed
CI / changes (push) Successful in 9s
CI / changes (pull_request) Successful in 7s
CI / frontend-tests (push) Has been skipped
CI / openapi-codegen-check (push) Failing after 1m37s
CI / frontend-tests (pull_request) Has been skipped
CI / openapi-codegen-check (pull_request) Failing after 1m35s
CI / backend-tests (push) Failing after 8m53s
CI / backend-tests (pull_request) Failing after 8m41s
2659600374
lekss361 merged commit 0fb70f2f90 into main 2026-06-17 18:46:01 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: lekss361/gendesign#1706
No description provided.