Kavita/API/Data/ManualMigrations/MigrateExistingRatings.cs
Joe Milazzo ce04e7421b
Few fixes from last PR (#2160)
* Added a migration for existing ratings

* Fixed duplicating web links and changed so it has the see more functionality.

* One more unit test
2023-07-25 12:15:25 -07:00

33 lines
1.0 KiB
C#

using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace API.Data.ManualMigrations;
/// <summary>
/// Introduced in v0.7.5.6 and v0.7.6, Ratings > 0 need to have "HasRatingSet"
/// </summary>
/// <remarks>Added in v0.7.5.6</remarks>
// ReSharper disable once InconsistentNaming
public static class MigrateExistingRatings
{
public static async Task Migrate(DataContext context, ILogger<Program> logger)
{
logger.LogCritical("Running MigrateExistingRatings migration - Please be patient, this may take some time. This is not an error");
foreach (var r in context.AppUserRating.Where(r => r.Rating > 0f))
{
r.HasBeenRated = true;
context.Entry(r).State = EntityState.Modified;
}
if (context.ChangeTracker.HasChanges())
{
await context.SaveChangesAsync();
}
logger.LogCritical("Running MigrateExistingRatings migration - Completed. This is not an error");
}
}