Files
tessera-ctl/apps/api/prisma/migrations/20260721140000_tender_bundesland_backfill/migration.sql
T
schalli e4db602597 feat(11-02): normalizer derives bundesland + backfill migration for existing rows
Replace the Phase-10-deferred `bundesland = null` assignment with
bundeslandFromRegion(region) so new ingests are Bundesland-filterable
immediately. Add @@index([bundesland]) for filter performance. New
handwritten migration 20260721140000_tender_bundesland_backfill backfills
the ~1671 pre-existing rows (idempotent UPDATE, only where bundesland IS
NULL AND region IS NOT NULL) — applied locally via
`docker exec tessera-ctl-db-1 psql`, resolved as applied in
_prisma_migrations, and prisma generate re-run.

Verified on the local dev DB: 933/1671 rows now have bundesland set
across all 16 Länder (738 remain NULL where region itself is NULL).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-21 16:01:32 +02:00

40 lines
1.6 KiB
SQL

-- FILTER-02 (Pitfall 1): Tender.bundesland is 100% NULL in the live DB
-- (0/1671 rows, verified) because the Phase-10 normalizer deferred
-- NUTS->Bundesland mapping to Phase 11. This migration:
-- 1. Adds the CREATE INDEX for bundesland (filter performance, matches
-- schema.prisma @@index([bundesland])).
-- 2. Backfills the ~1671 pre-existing rows by deriving bundesland from
-- the region column's NUTS-1 prefix (DE1..DEG -> the 16 Bundesländer),
-- mirroring geo/nuts-bundesland.ts's NUTS1_BUNDESLAND map exactly.
--
-- Idempotent: only touches rows where bundesland IS NULL AND region IS NOT
-- NULL — safe to run more than once, and never overwrites a value already
-- set by the (now-fixed) normalizer for newer ingests.
-- No user input is interpolated (T-11-05) — pure derived-field UPDATE.
-- CreateIndex
CREATE INDEX IF NOT EXISTS "Tender_bundesland_idx" ON "Tender"("bundesland");
-- Backfill
UPDATE "Tender"
SET "bundesland" = CASE upper(left("region", 3))
WHEN 'DE1' THEN 'Baden-Württemberg'
WHEN 'DE2' THEN 'Bayern'
WHEN 'DE3' THEN 'Berlin'
WHEN 'DE4' THEN 'Brandenburg'
WHEN 'DE5' THEN 'Bremen'
WHEN 'DE6' THEN 'Hamburg'
WHEN 'DE7' THEN 'Hessen'
WHEN 'DE8' THEN 'Mecklenburg-Vorpommern'
WHEN 'DE9' THEN 'Niedersachsen'
WHEN 'DEA' THEN 'Nordrhein-Westfalen'
WHEN 'DEB' THEN 'Rheinland-Pfalz'
WHEN 'DEC' THEN 'Saarland'
WHEN 'DED' THEN 'Sachsen'
WHEN 'DEE' THEN 'Sachsen-Anhalt'
WHEN 'DEF' THEN 'Schleswig-Holstein'
WHEN 'DEG' THEN 'Thüringen'
ELSE NULL
END
WHERE "bundesland" IS NULL AND "region" IS NOT NULL;