[AZ-484] Fix multi-source tile reads: drop Dapper enum handler

Two integration-test failures uncovered after the initial commit:

1) GetTilesByRegionAsync outer ORDER BY referenced 'updated_at' but
   the inner DISTINCT ON subquery aliased it to 'UpdatedAt' (Postgres
   folds to 'updatedat'). DISTINCT ON already guarantees one row per
   (latitude, longitude, ...) so the third tiebreak was unreachable;
   removed it.

2) Dapper 2.1.35 silently bypasses SqlMapper.TypeHandler<T> for enum
   types during read deserialization (Dapper issue #259). The
   TileSourceTypeHandler worked for writes but reads fell through to
   Enum.TryParse, which cannot map 'google_maps' to GoogleMaps.

   Pivoted: TileEntity.Source is now a string (the wire value).
   TileSource enum stays as the public producer surface in
   Common.Enums; TileSourceConverter (Common.Enums) provides
   ToWireValue / FromWireValue / IsValidWireValue at the boundary.
   TileSourceTypeHandler deleted; registration removed from
   DapperEnumTypeHandlers.RegisterAll.

   tile-storage.md Inv-5 amended to document the storage choice.
   _docs/LESSONS.md L-001 records the Dapper bypass for future cycles.

Full suite passes (213 unit + integration suite incl. AZ-484
AC-1..AC-5, security SEC-01..SEC-04, AZ-356/362/357).

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-05-11 06:44:34 +03:00
parent 687d6bdd5b
commit e9d6db077c
16 changed files with 181 additions and 184 deletions
@@ -16,7 +16,11 @@ public class TileEntity
public string? MapsVersion { get; set; }
public int? Version { get; set; }
public string FilePath { get; set; } = string.Empty;
public TileSource Source { get; set; }
// AZ-484: stored as the contract wire value (snake_case string). See
// TileSourceConverter for enum<->wire conversion. Cannot use the
// TileSource enum directly because Dapper bypasses TypeHandler<T> for
// enum types during read deserialization (Dapper issue #259).
public string Source { get; set; } = TileSourceConverter.GoogleMapsWireValue;
public DateTime CapturedAt { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
@@ -70,7 +70,9 @@ public class TileRepository : ITileRepository
// AZ-484 selection rule: at most one row per (lat, lon, zoom, size) cell, picking
// the most-recent across sources via DISTINCT ON, with deterministic tie-break on
// (captured_at DESC, updated_at DESC, id DESC). The outer ORDER BY restores the
// pre-AZ-484 caller-facing order (latitude DESC, longitude ASC, updated_at DESC).
// pre-AZ-484 caller-facing order (latitude DESC, longitude ASC); the pre-AZ-484
// updated_at DESC tiebreak is unreachable here because DISTINCT ON already
// guarantees one row per (latitude, longitude, ...) tuple.
const string sql = $@"
SELECT * FROM (
SELECT DISTINCT ON (latitude, longitude, tile_zoom, tile_size_meters)
@@ -82,7 +84,7 @@ public class TileRepository : ITileRepository
ORDER BY latitude, longitude, tile_zoom, tile_size_meters,
captured_at DESC, updated_at DESC, id DESC
) deduped
ORDER BY latitude DESC, longitude ASC, updated_at DESC";
ORDER BY latitude DESC, longitude ASC";
var stopwatch = Stopwatch.StartNew();
var tiles = await connection.QueryAsync<TileEntity>(sql, new
@@ -47,6 +47,5 @@ public static class DapperEnumTypeHandlers
SqlMapper.AddTypeHandler(new EnumStringTypeHandler<RegionStatus>());
SqlMapper.AddTypeHandler(new EnumStringTypeHandler<RoutePointType>());
SqlMapper.AddTypeHandler(new TileSourceTypeHandler());
}
}
@@ -1,49 +0,0 @@
using System.Data;
using Dapper;
using SatelliteProvider.Common.Enums;
namespace SatelliteProvider.DataAccess.TypeHandlers;
// AZ-484: TileSource needs an explicit string mapping because the multi-word
// member 'GoogleMaps' must round-trip as the snake_case wire value 'google_maps'
// per the v1.0.0 tile-storage contract. The generic EnumStringTypeHandler<T>
// only does ToString().ToLowerInvariant(), which would emit 'googlemaps'.
public class TileSourceTypeHandler : SqlMapper.TypeHandler<TileSource>
{
public const string GoogleMapsWireValue = "google_maps";
public const string UavWireValue = "uav";
public override TileSource Parse(object value)
{
if (value is null || value is DBNull)
{
throw new DataException("Cannot parse null DB value into enum TileSource");
}
var s = value as string ?? value.ToString();
if (string.IsNullOrEmpty(s))
{
throw new DataException("Cannot parse empty DB value into enum TileSource");
}
return s.ToLowerInvariant() switch
{
GoogleMapsWireValue => TileSource.GoogleMaps,
UavWireValue => TileSource.Uav,
_ => throw new DataException($"DB value '{s}' is not a defined member of enum TileSource"),
};
}
public override void SetValue(IDbDataParameter parameter, TileSource value)
{
parameter.Value = ToWireValue(value);
parameter.DbType = DbType.String;
}
public static string ToWireValue(TileSource value) => value switch
{
TileSource.GoogleMaps => GoogleMapsWireValue,
TileSource.Uav => UavWireValue,
_ => throw new ArgumentOutOfRangeException(nameof(value), value, "Unknown TileSource"),
};
}