tracksolid_timescale_grafan.../migrations/04_bug_fix_migration.sql
david kiania e5b0e192d8 chore(repo): reorganize tree into migrations/ data/ legacy/ docs/
Group root-level files (accreted from incremental changes) by purpose
without moving any deployment entrypoint or breaking imports:

- migrations/  : numbered SQL 02-10
- data/        : source CSVs
- legacy/      : superseded pre-_rev scripts + old pipeline notes (not deployed)
- docs/{manuals,reference,reports}/ : loose manuals, references, reports
- strip stray ** / *** prefixes from 5 doc filenames
- delete empty documents.txt / push_webhook.md

Reference updates so nothing breaks:
- run_migrations.py  -> /app/migrations/<file>
- run_migrations.sh  -> $SCRIPT_DIR/migrations
- import_drivers_csv.py -> data/<csv>
- docker-compose.yaml -> runbook path comment
- CLAUDE.md -> codebase map + inline doc references

Deployed Python (3 services + ts_shared_rev + run_migrations) and the
documented ops one-shots stay at root, preserving the flat-import layout
and all documented commands. Verified: py_compile clean across all modules,
every MIGRATIONS entry resolves under migrations/, CI-referenced paths
(tests/, mypy targets, db_audit) and the grafana build context intact.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-01 02:27:30 +03:00

36 lines
1.9 KiB
PL/PgSQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- Migration 04 — Bug Fix: distance unit correction + column rename
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
-- BUG-02: tracksolid.trips.distance_m was storing millimetres not metres.
--
-- Root cause: ingestion code applied `km * 1000` but the API already returns
-- values in km, producing mm. Confirmed by cross-checking stored values against
-- avg_speed_kmh × driving_time_s — e.g. 4,203,000 stored for a 4.203 km trip.
--
-- Fix applied here:
-- 1. Divide all existing rows by 1,000,000 to convert mm → km.
-- 2. Rename the column to distance_km to eliminate future ambiguity.
--
-- Corresponding code fix: removed * 1000 from poll_trips() in
-- ingest_movement_rev.py and push_trip_report() in webhook_receiver_rev.py.
-- Both now store the raw API value directly as km.
--
-- Run once against tracksolid_db before deploying updated ingestion containers.
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
BEGIN;
-- Step 1: Correct all existing stored values (mm → km)
UPDATE tracksolid.trips
SET distance_m = distance_m / 1000000.0
WHERE distance_m IS NOT NULL;
-- Step 2: Rename column
ALTER TABLE tracksolid.trips
RENAME COLUMN distance_m TO distance_km;
-- Step 3: Update column comment
COMMENT ON COLUMN tracksolid.trips.distance_km
IS 'Trip distance in kilometres. Corrected from mm storage on migration 04 (2026-04-10).';
COMMIT;