Files
satellite-provider/SatelliteProvider.DataAccess/Migrations/013_AddTileSourceAndCapturedAt.sql
T
Oleksandr Bezdieniezhnykh 687d6bdd5b [AZ-484] Multi-source tile storage: source + captured_at
Add per-source tile rows to support multi-provider imagery (Google
Maps + future UAV). Migration 013 (transactional) introduces
source/captured_at columns, backfills existing rows to
(source='google_maps', captured_at=created_at), and replaces the
4-column unique index with a 5-column index that includes source.

TileRepository:
- ColumnList includes source + captured_at
- GetByTileCoordinatesAsync returns most-recent row across sources
  (ORDER BY captured_at DESC, updated_at DESC, id DESC)
- GetTilesByRegionAsync uses DISTINCT ON to pick the most-recent
  tile per cell, restoring caller-facing row order
- Insert/Update upsert on the new 5-column conflict key

TileSource enum lives in Common.Enums. Snake_case wire format
(google_maps, uav) is enforced by a focused TileSourceTypeHandler
because the generic ToLowerInvariant pattern would emit
"googlemaps", violating contract v1.0.0.

TileService stamps Source=GoogleMaps + CapturedAt=UtcNow on every
new tile. Tile-storage contract is now frozen at v1.0.0.

AC coverage 7/7. New unit + integration tests cover all ACs;
existing 200 unit + 5 smoke tests preserved.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-11 06:21:59 +03:00

32 lines
1.1 KiB
PL/PgSQL

-- AZ-484: introduce per-source tile rows.
-- Adds `source` and `captured_at` columns, backfills existing rows to
-- (source='google_maps', captured_at=created_at), drops the 4-column unique index
-- created by migration 012 (idx_tiles_unique_location), and replaces it with a
-- 5-column unique index that includes `source`. The whole migration runs inside a
-- single transaction so a failure mid-flight cannot leave the table without its
-- unique index or with partially backfilled rows (per coderule.mdc and AZ-484
-- Risk 1 mitigation).
BEGIN;
ALTER TABLE tiles ADD COLUMN IF NOT EXISTS source VARCHAR(32);
ALTER TABLE tiles ADD COLUMN IF NOT EXISTS captured_at TIMESTAMP;
UPDATE tiles
SET source = 'google_maps'
WHERE source IS NULL;
UPDATE tiles
SET captured_at = created_at
WHERE captured_at IS NULL;
ALTER TABLE tiles ALTER COLUMN source SET NOT NULL;
ALTER TABLE tiles ALTER COLUMN captured_at SET NOT NULL;
DROP INDEX IF EXISTS idx_tiles_unique_location;
CREATE UNIQUE INDEX IF NOT EXISTS idx_tiles_unique_location_source
ON tiles (latitude, longitude, tile_zoom, tile_size_meters, source);
COMMIT;