94 lines
3.4 KiB
C#
94 lines
3.4 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Shadow.Data;
|
|
using Shadow.Entities;
|
|
|
|
namespace Shadow.Tools;
|
|
public class Seeder
|
|
{
|
|
private readonly ApplicationDbContext db;
|
|
private readonly GeneralUseHelpers guhf;
|
|
|
|
public Seeder(ApplicationDbContext _db, GeneralUseHelpers _guhf)
|
|
{
|
|
db = _db;
|
|
guhf = _guhf;
|
|
}
|
|
public bool Seed()
|
|
{
|
|
// Will return true if application should be shut down.
|
|
// Otherwise returns false.
|
|
bool shutdown = false;
|
|
|
|
Console.WriteLine($"You're running Shadow, commit {ThisAssembly.Git.Commit} of branch {ThisAssembly.Git.Branch} ({ThisAssembly.Git.CommitDate})\n");
|
|
|
|
// Check if this is a clean, first run. If so, run the setup wizard.
|
|
Global? lastVersion = db.Globals.FirstOrDefault(c => c.Key == "lastVersion");
|
|
if (lastVersion is null)
|
|
{
|
|
shutdown = true;
|
|
bool setupSuccessfull = false;
|
|
Console.WriteLine("This seems to be a fresh install. Running setup wizard for you.\n");
|
|
Cli cli = new(db, guhf, []);
|
|
setupSuccessfull = cli.SetupWizard();
|
|
|
|
if (setupSuccessfull)
|
|
{
|
|
// Cli.SetupWizard() takes care of musicLibraryPath and musicThumbnailPath
|
|
lastVersion = new Global() { Key = "lastVersion", Value = ThisAssembly.Git.Commit };
|
|
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" };
|
|
db.Globals.AddRange(lastVersion, lastVersionDate, runs, libraryState);
|
|
}
|
|
else
|
|
Console.WriteLine("Setup wizard failed. Please try running it again using \"Shadow setupWizard\".");
|
|
|
|
db.SaveChanges();
|
|
return shutdown;
|
|
}
|
|
|
|
// Check if running a newer (different) version
|
|
if (lastVersion.Value != ThisAssembly.Git.Commit)
|
|
{
|
|
Global? lastVersionDate = db.Globals.FirstOrDefault(c => c.Key == "lastVersionDate");
|
|
if (lastVersionDate is not null && String.Compare(lastVersionDate.Value, ThisAssembly.Git.CommitDate) > 0)
|
|
{
|
|
// User is running an earlier version of the application.
|
|
Console.WriteLine("Downgrade detected! Waiting 30 seconds.\n" +
|
|
"Please consider stopping Shadow in order to avoid accidental data loss!");
|
|
}
|
|
Console.WriteLine("Upgrade detected. Make sure you're using the most recent migrations.\n" +
|
|
"If not, apply them with `dotnet ef database update`.");
|
|
}
|
|
|
|
// Check if any user/admin exist. Display appropriate warnings if not.
|
|
int userCount = db.Users.Count();
|
|
if (userCount == 0)
|
|
Console.WriteLine("[Warn]: No user accounts found. Running a server no one can access! Consider creating an account with `Shadow addUser`.");
|
|
|
|
int adminCount = db.Users.Count(u => u.Role == 0); // equivalent to u.IsAdmin()
|
|
if (adminCount == 0 && userCount > 0)
|
|
Console.WriteLine("[Warn]: No admin accounts exist. Consider creating one with `Shadow addUser`.");
|
|
|
|
// Ensure [Unknown Album], [Unknown Artist] exist
|
|
Album unknownAlbum = db.Albums.FirstOrDefault(a => a.Name == "[Unknown Album]") ?? new Album()
|
|
{
|
|
Name = "[Unknown Album]",
|
|
Uri = "00000000000000000000000000000000"
|
|
};
|
|
|
|
Artist unknownArtist = db.Artists.FirstOrDefault(a => a.Name == "[Unknown Artist]") ?? new Artist()
|
|
{
|
|
Name = "[Unknown Artist]",
|
|
NormalizedName = "[unknown artist]"
|
|
};
|
|
|
|
// UpdateRange works both as an Add and Update.
|
|
db.UpdateRange(unknownAlbum, unknownArtist);
|
|
db.SaveChanges();
|
|
|
|
return shutdown;
|
|
|
|
}
|
|
}
|