Files
SVSimServer/SVSim.Bootstrap/Importers/BattlePassSeasonImporter.cs
gamer147 c303d3040d fix(bp): convert seed JST dates to UTC for Postgres timestamp-with-tz
Npgsql rejects DateTimeOffset writes to timestamp-with-tz unless offset
is zero. Caught by manual bootstrap against a real Postgres DB; SQLite
test provider didn't enforce this. Converting to UTC post-parse is
semantically lossless — DateTimeOffset comparisons are instant-based.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 00:05:48 -04:00

49 lines
2.1 KiB
C#

using System.Globalization;
using Microsoft.EntityFrameworkCore;
using SVSim.Bootstrap.Models.Seed;
using SVSim.Database;
using SVSim.Database.Models;
namespace SVSim.Bootstrap.Importers;
/// <summary>
/// Idempotent upsert of battle-pass seasons from <c>seeds/battle-pass-seasons.json</c>.
/// Rows missing from the seed are LEFT INTACT (historic seasons).
/// </summary>
public class BattlePassSeasonImporter
{
public async Task<int> ImportAsync(SVSimDbContext context, string seedDir)
{
var seed = SeedLoader.LoadList<BattlePassSeasonSeed>(Path.Combine(seedDir, "battle-pass-seasons.json"));
if (seed.Count == 0)
{
Console.WriteLine("[BattlePassSeasonImporter] No seed rows; skipping.");
return 0;
}
var existing = await context.BattlePassSeasons.ToDictionaryAsync(e => e.Id);
int created = 0, updated = 0;
foreach (var s in seed)
{
if (s.Id == 0) continue;
var entry = existing.TryGetValue(s.Id, out var ex) ? ex : new BattlePassSeasonEntry { Id = s.Id };
entry.Name = s.Name;
entry.MaxLevel = s.MaxLevel;
// Postgres 'timestamp with time zone' only accepts UTC offset; JST-offset values
// from the seed are converted to UTC to preserve the instant. Comparisons via
// DateTimeOffset are instant-based, so the JST→UTC conversion is semantically lossless.
entry.StartDate = DateTimeOffset.Parse(s.StartDate, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal).ToUniversalTime();
entry.EndDate = DateTimeOffset.Parse(s.EndDate, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal).ToUniversalTime();
entry.CanPurchase = s.CanPurchase;
entry.PriceCrystal = s.PriceCrystal;
entry.Description = s.Description;
if (ex is null) { context.BattlePassSeasons.Add(entry); existing[s.Id] = entry; created++; }
else updated++;
}
await context.SaveChangesAsync();
Console.WriteLine($"[BattlePassSeasonImporter] +{created}/~{updated}");
return created + updated;
}
}