More robust date handling in Library DB migration (#16474)

* More robust date handling in Library DB migration

* Apply review comment
This commit is contained in:
Tim Eisele
2026-03-29 12:38:32 +02:00
committed by GitHub
parent 6ea77f484d
commit ad9ebe5baa

View File

@@ -515,7 +515,7 @@ internal class MigrateLibraryDb : IDatabaseMigrationRoutine
PlayCount = dto.GetInt32(4),
IsFavorite = dto.GetBoolean(5),
PlaybackPositionTicks = dto.GetInt64(6),
LastPlayedDate = dto.IsDBNull(7) ? null : dto.GetDateTime(7),
LastPlayedDate = dto.IsDBNull(7) ? null : ReadDateTimeFromColumn(dto, 7),
AudioStreamIndex = dto.IsDBNull(8) ? null : dto.GetInt32(8),
SubtitleStreamIndex = dto.IsDBNull(9) ? null : dto.GetInt32(9),
Likes = null,
@@ -524,6 +524,28 @@ internal class MigrateLibraryDb : IDatabaseMigrationRoutine
};
}
private static DateTime? ReadDateTimeFromColumn(SqliteDataReader reader, int index)
{
// Try reading as a formatted date string first (handles ISO-8601 dates).
if (reader.TryReadDateTime(index, out var dateTimeResult))
{
return dateTimeResult;
}
// Some databases have Unix epoch timestamps stored as integers.
// SqliteDataReader.GetDateTime interprets integers as Julian dates, which crashes
// for Unix epoch values. Handle them explicitly.
var rawValue = reader.GetValue(index);
if (rawValue is long unixTimestamp
&& unixTimestamp > 0
&& unixTimestamp <= DateTimeOffset.MaxValue.ToUnixTimeSeconds())
{
return DateTimeOffset.FromUnixTimeSeconds(unixTimestamp).UtcDateTime;
}
return null;
}
private AncestorId GetAncestorId(SqliteDataReader reader)
{
return new AncestorId()