Off-By-One Error in Datebank Migration behoben

This commit is contained in:
2025-11-19 14:25:22 +01:00
parent a7ca1214f3
commit 6eec58b8e7
2 changed files with 12 additions and 3 deletions

View File

@@ -262,7 +262,8 @@ public class MonitoringController : Controller
server = await _context.Servers.FirstOrDefaultAsync(s => s.IPAddress == dto.IpAddress); server = await _context.Servers.FirstOrDefaultAsync(s => s.IPAddress == dto.IpAddress);
} }
// Falls keine IP-Adresse übergeben wurde oder Server nicht gefunden, versuche es mit der ID // Falls keine IP-Adresse übergeben wurde oder Server nicht gefunden, versuche es mit der ID
else if (dto.Server_id > 0) // WICHTIG: >= 0 da Server-IDs bei 0 starten können
else if (dto.Server_id >= 0)
{ {
server = await _context.Servers.FirstOrDefaultAsync(s => s.Id == dto.Server_id); server = await _context.Servers.FirstOrDefaultAsync(s => s.Id == dto.Server_id);
} }

View File

@@ -17,10 +17,18 @@ namespace Watcher.Migrations
// Bestehende Server-IDs um 1 verringern (1 -> 0, 2 -> 1, etc.) // Bestehende Server-IDs um 1 verringern (1 -> 0, 2 -> 1, etc.)
migrationBuilder.Sql(@" migrationBuilder.Sql(@"
UPDATE Servers SET Id = Id - 1; -- Bestehende Server-IDs anpassen, falls Server existieren
UPDATE Servers SET Id = Id - 1 WHERE EXISTS (SELECT 1 FROM Servers);
UPDATE Metrics SET ServerId = ServerId - 1 WHERE ServerId IS NOT NULL; UPDATE Metrics SET ServerId = ServerId - 1 WHERE ServerId IS NOT NULL;
UPDATE Containers SET ServerId = ServerId - 1 WHERE ServerId IS NOT NULL; UPDATE Containers SET ServerId = ServerId - 1 WHERE ServerId IS NOT NULL;
UPDATE sqlite_sequence SET seq = seq - 1 WHERE name = 'Servers';
-- sqlite_sequence anpassen oder initialisieren
-- Falls Eintrag existiert: seq - 1, ansonsten: INSERT mit seq = -1
-- Der nächste AUTOINCREMENT wird dann 0 sein
INSERT OR REPLACE INTO sqlite_sequence (name, seq)
VALUES ('Servers',
COALESCE((SELECT seq - 1 FROM sqlite_sequence WHERE name = 'Servers'), -1)
);
"); ");
} }