chore(db): backfill founding_age_years for inhabited bodies (#1000)

Add idempotent MIGRATION_SQL UPDATE to import_economics.py deriving
bodies.founding_age_years via COALESCE(colonial_charter event age,
settlement_wave fallback) per the D-216 amendment.

- Events-first: colonial_charter age_years (authored; 11 bodies).
- Wave fallback: wave_1=600/wave_2=500/wave_3=300/wave_4=100/wave_5=40
  (258 bodies).
- Exclude settlement_wave in (origin, unsettled) -> stays NULL: 4 Sol
  bodies remain NULL (out of player scope, D-236).

Source-canonical via regen-db; 269/273 inhabited bodies now populated.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-30 16:07:47 +02:00
co-authored by Claude Opus 4.8
parent 6e7f6356f0
commit 4319e40a63
2 changed files with 44 additions and 0 deletions
Binary file not shown.
+44
View File
@@ -358,6 +358,50 @@ UPDATE bodies SET economic_role = 'extraction' WHERE economic_role IN ('minin
UPDATE bodies SET economic_role = 'transit_hub' WHERE economic_role = 'transit';
UPDATE bodies SET economic_role = 'service_mixed' WHERE economic_role IN ('commercial', 'coordination');
UPDATE bodies SET economic_role = 'residential' WHERE economic_role = 'frontier';
-- Backfill bodies.founding_age_years for all inhabited bodies in player scope (D-216 amendment,
-- ticket #1000). Idempotent: WHERE clause limits to NULL rows, so a re-run is a no-op.
--
-- Strategy: COALESCE(events-first, wave-fallback)
-- events-first: the system's colonial_charter event age_years (authored; 9 systems in live DB).
-- system_history is keyed PRIMARY KEY on system_id, so the wave subquery returns
-- at most one row; LIMIT 1 is used on historical_events for safety (max one
-- colonial_charter per system in the data).
-- wave-fallback: canonical founding-edge of each settlement_wave range per D-216:
-- wave_1=600, wave_2=500, wave_3=300, wave_4=100, wave_5=40.
--
-- Exclusions (founding_age_years stays NULL):
-- origin — Sol; out of player scope per D-236.
-- unsettled — 24 systems with inhabited=0; the EXISTS guard also excludes them.
UPDATE bodies
SET founding_age_years = COALESCE(
(
SELECT he.age_years
FROM historical_events he
WHERE he.system_id = bodies.system_id
AND he.event_type = 'colonial_charter'
LIMIT 1
),
(
SELECT CASE sh.settlement_wave
WHEN 'wave_1' THEN 600
WHEN 'wave_2' THEN 500
WHEN 'wave_3' THEN 300
WHEN 'wave_4' THEN 100
WHEN 'wave_5' THEN 40
END
FROM system_history sh
WHERE sh.system_id = bodies.system_id
)
)
WHERE founding_age_years IS NULL
AND inhabited = 1
AND EXISTS (
SELECT 1
FROM system_history sh2
WHERE sh2.system_id = bodies.system_id
AND sh2.settlement_wave NOT IN ('origin', 'unsettled')
);
"""
# Columns to add to existing tables (ALTER TABLE is idempotent via try/except)