fix: count runs, update versions if they differ
All checks were successful
Update changelog / changelog (push) Successful in 25s

This commit is contained in:
2025-12-16 13:25:34 +01:00
parent a8f4afbfd8
commit f5df949a4c

View File

@@ -23,6 +23,7 @@ public class Seeder
// Check if this is a clean, first run. If so, run the setup wizard. // Check if this is a clean, first run. If so, run the setup wizard.
Global? lastVersion = db.Globals.FirstOrDefault(c => c.Key == "lastVersion"); Global? lastVersion = db.Globals.FirstOrDefault(c => c.Key == "lastVersion");
Global? runs = db.Globals.FirstOrDefault(g => g.Key == "runs");
if (lastVersion is null) if (lastVersion is null)
{ {
shutdown = true; shutdown = true;
@@ -35,8 +36,8 @@ public class Seeder
{ {
// Cli.SetupWizard() takes care of musicLibraryPath and musicThumbnailPath // Cli.SetupWizard() takes care of musicLibraryPath and musicThumbnailPath
lastVersion = new Global() { Key = "lastVersion", Value = ThisAssembly.Git.Commit }; lastVersion = new Global() { Key = "lastVersion", Value = ThisAssembly.Git.Commit };
runs = new Global() { Key = "runs", Value = "0" };
Global lastVersionDate = new Global() { Key = "lastVersionDate", Value = ThisAssembly.Git.CommitDate }; Global lastVersionDate = new Global() { Key = "lastVersionDate", Value = ThisAssembly.Git.CommitDate };
Global runs = new Global() { Key = "runs", Value = "0" };
Global libraryState = new Global() { Key = "libraryState", Value = "00000000000000000000000000000000" }; Global libraryState = new Global() { Key = "libraryState", Value = "00000000000000000000000000000000" };
db.Globals.AddRange(lastVersion, lastVersionDate, runs, libraryState); db.Globals.AddRange(lastVersion, lastVersionDate, runs, libraryState);
} }
@@ -50,25 +51,34 @@ public class Seeder
// Check if running a newer (different) version // Check if running a newer (different) version
if (lastVersion.Value != ThisAssembly.Git.Commit) if (lastVersion.Value != ThisAssembly.Git.Commit)
{ {
Global? lastVersionDate = db.Globals.FirstOrDefault(c => c.Key == "lastVersionDate"); Global lastVersionDate = db.Globals.FirstOrDefault(c => c.Key == "lastVersionDate")
if (lastVersionDate is not null && String.Compare(lastVersionDate.Value, ThisAssembly.Git.CommitDate) > 0) ?? new Global() { Key = "lastVersionDate", Value = ThisAssembly.Git.CommitDate };
if (String.Compare(lastVersionDate.Value, ThisAssembly.Git.CommitDate) > 0)
{ {
// User is running an earlier version of the application. // User is running an earlier version of the application.
Console.WriteLine("Downgrade detected! Waiting 30 seconds.\n" + Console.WriteLine("Downgrade detected! Waiting 30 seconds.\n" +
"Please consider stopping Shadow in order to avoid accidental data loss!"); "Please consider stopping Shadow in order to avoid accidental data loss!\n");
Thread.Sleep(30_000);
} }
else
Console.WriteLine("Upgrade detected. Make sure you're using the most recent migrations.\n" + Console.WriteLine("Upgrade detected. Make sure you're using the most recent migrations.\n" +
"If not, apply them with `dotnet ef database update`."); "If not, apply them with `dotnet ef database update`.\n");
lastVersion.Value = ThisAssembly.Git.Commit;
lastVersionDate.Value = ThisAssembly.Git.CommitDate;
db.UpdateRange(lastVersion, lastVersionDate);
} }
// Check if any user/admin exist. Display appropriate warnings if not. // Check if any user/admin exist. Display appropriate warnings if not.
int userCount = db.Users.Count(); int userCount = db.Users.Count();
if (userCount == 0) if (userCount == 0)
Console.WriteLine("[Warn]: No user accounts found. Running a server no one can access! Consider creating an account with `Shadow addUser`."); Console.WriteLine("[Warn]: No user accounts found. Running a server no one can access! Consider creating an account with `Shadow addUser`.\n");
int adminCount = db.Users.Count(u => u.Role == 0); // equivalent to u.IsAdmin() int adminCount = db.Users.Count(u => u.Role == 0); // equivalent to u.IsAdmin()
if (adminCount == 0 && userCount > 0) if (adminCount == 0 && userCount > 0)
Console.WriteLine("[Warn]: No admin accounts exist. Consider creating one with `Shadow addUser`."); Console.WriteLine("[Warn]: No admin accounts exist. Consider creating one with `Shadow addUser`.\n");
// Ensure [Unknown Album], [Unknown Artist] exist // Ensure [Unknown Album], [Unknown Artist] exist
Album unknownAlbum = db.Albums.FirstOrDefault(a => a.Name == "[Unknown Album]") ?? new Album() Album unknownAlbum = db.Albums.FirstOrDefault(a => a.Name == "[Unknown Album]") ?? new Album()
@@ -83,8 +93,12 @@ public class Seeder
NormalizedName = "[unknown artist]" NormalizedName = "[unknown artist]"
}; };
runs ??= new Global() { Key = "runs", Value = "0" };
if (int.TryParse(runs.Value, out int runsInt))
runs.Value = $"{runsInt + 1}";
// UpdateRange works both as an Add and Update. // UpdateRange works both as an Add and Update.
db.UpdateRange(unknownAlbum, unknownArtist); db.UpdateRange(unknownAlbum, unknownArtist, runs);
db.SaveChanges(); db.SaveChanges();
return shutdown; return shutdown;