Missing Migration (#2790)
This commit is contained in:
parent
2b6fd1224f
commit
f443e513d1
11 changed files with 387 additions and 29 deletions
|
|
@ -3,7 +3,9 @@ using System.IO;
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using API.Entities;
|
||||
using API.Extensions;
|
||||
using API.Helpers.Builders;
|
||||
using API.Services;
|
||||
using API.Services.Tasks.Scanner.Parser;
|
||||
using Kavita.Common.EnvironmentInfo;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
|
@ -21,6 +23,7 @@ public class UserProgressCsvRecord
|
|||
public float MinNumber { get; set; }
|
||||
public int SeriesId { get; set; }
|
||||
public int VolumeId { get; set; }
|
||||
public int ProgressId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -28,7 +31,7 @@ public class UserProgressCsvRecord
|
|||
/// </summary>
|
||||
public static class MigrateMixedSpecials
|
||||
{
|
||||
public static async Task Migrate(DataContext dataContext, IUnitOfWork unitOfWork, ILogger<Program> logger)
|
||||
public static async Task Migrate(DataContext dataContext, IUnitOfWork unitOfWork, IDirectoryService directoryService, ILogger<Program> logger)
|
||||
{
|
||||
if (await dataContext.ManualMigrationHistory.AnyAsync(m => m.Name == "ManualMigrateMixedSpecials"))
|
||||
{
|
||||
|
|
@ -39,13 +42,13 @@ public static class MigrateMixedSpecials
|
|||
"Running ManualMigrateMixedSpecials migration - Please be patient, this may take some time. This is not an error");
|
||||
|
||||
// First, group all the progresses into different series
|
||||
|
||||
// Get each series and move the specials from old volume to the new Volume()
|
||||
|
||||
// Create a new progress event from existing and store the Id of existing progress event to delete it
|
||||
|
||||
// Save per series
|
||||
|
||||
var settings = await unitOfWork.SettingsRepository.GetSettingsDtoAsync();
|
||||
var extension = settings.EncodeMediaAs.GetExtension();
|
||||
|
||||
var progress = await dataContext.AppUserProgresses
|
||||
.Join(dataContext.Chapter, p => p.ChapterId, c => c.Id, (p, c) => new UserProgressCsvRecord
|
||||
{
|
||||
|
|
@ -56,10 +59,12 @@ public static class MigrateMixedSpecials
|
|||
Number = c.Number,
|
||||
MinNumber = c.MinNumber,
|
||||
SeriesId = p.SeriesId,
|
||||
VolumeId = p.VolumeId
|
||||
VolumeId = p.VolumeId,
|
||||
ProgressId = p.Id
|
||||
})
|
||||
.Where(d => d.IsSpecial || d.Number == "0")
|
||||
.Join(dataContext.Volume, d => d.VolumeId, v => v.Id, (d, v) => new
|
||||
.Join(dataContext.Volume, d => d.VolumeId, v => v.Id,
|
||||
(d, v) => new
|
||||
{
|
||||
ProgressRecord = d,
|
||||
Volume = v
|
||||
|
|
@ -68,18 +73,19 @@ public static class MigrateMixedSpecials
|
|||
.ToListAsync();
|
||||
|
||||
// First, group all the progresses into different series
|
||||
logger.LogCritical("Migrating {Count} progress events to new Volume structure - This may take over 10 minutes depending on size of DB. Please wait", progress.Count);
|
||||
logger.LogCritical("Migrating {Count} progress events to new Volume structure for Specials - This may take over 10 minutes depending on size of DB. Please wait", progress.Count);
|
||||
var progressesGroupedBySeries = progress.GroupBy(p => p.ProgressRecord.SeriesId);
|
||||
|
||||
foreach (var seriesGroup in progressesGroupedBySeries)
|
||||
{
|
||||
// Get each series and move the specials from the old volume to the new Volume
|
||||
var seriesId = seriesGroup.Key;
|
||||
|
||||
// Handle All Specials
|
||||
var specialsInSeries = seriesGroup
|
||||
.Where(p => p.ProgressRecord.IsSpecial)
|
||||
.ToList();
|
||||
|
||||
|
||||
// Get distinct Volumes by Id. For each one, create it then create the progress events
|
||||
var distinctVolumes = specialsInSeries.DistinctBy(d => d.Volume.Id);
|
||||
foreach (var distinctVolume in distinctVolumes)
|
||||
|
|
@ -90,29 +96,43 @@ public static class MigrateMixedSpecials
|
|||
|
||||
var newVolume = new VolumeBuilder(Parser.SpecialVolume)
|
||||
.WithSeriesId(seriesId)
|
||||
.WithChapters(chapters)
|
||||
.WithCreated(distinctVolume.Volume.Created)
|
||||
.WithLastModified(distinctVolume.Volume.LastModified)
|
||||
.Build();
|
||||
|
||||
newVolume.Pages = chapters.Sum(c => c.Pages);
|
||||
newVolume.WordCount = chapters.Sum(c => c.WordCount);
|
||||
newVolume.MinHoursToRead = chapters.Sum(c => c.MinHoursToRead);
|
||||
newVolume.MaxHoursToRead = chapters.Sum(c => c.MaxHoursToRead);
|
||||
newVolume.AvgHoursToRead = chapters.Sum(c => c.AvgHoursToRead);
|
||||
|
||||
dataContext.Volume.Add(newVolume);
|
||||
await dataContext.SaveChangesAsync(); // Save changes to generate the newVolumeId
|
||||
|
||||
// Migrate the progress event to the new volume
|
||||
distinctVolume.ProgressRecord.VolumeId = newVolume.Id;
|
||||
var oldVolumeProgresses = await dataContext.AppUserProgresses
|
||||
.Where(p => p.VolumeId == distinctVolume.Volume.Id).ToListAsync();
|
||||
foreach (var oldProgress in oldVolumeProgresses)
|
||||
{
|
||||
oldProgress.VolumeId = newVolume.Id;
|
||||
}
|
||||
|
||||
|
||||
logger.LogInformation("Moving {Count} chapters from Volume Id {OldVolumeId} to New Volume {NewVolumeId}",
|
||||
chapters.Count, distinctVolume.Volume.Id, newVolume.Id);
|
||||
// Move the special chapters from the old volume to the new Volume
|
||||
var specialChapters = await dataContext.Chapter
|
||||
.Where(c => c.VolumeId == distinctVolume.ProgressRecord.VolumeId && c.IsSpecial)
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var specialChapter in specialChapters)
|
||||
// Move the special chapters from the old volume to the new Volume
|
||||
foreach (var specialChapter in chapters)
|
||||
{
|
||||
// Update the VolumeId on the existing progress event
|
||||
specialChapter.VolumeId = newVolume.Id;
|
||||
|
||||
//UpdateCoverImage(directoryService, logger, specialChapter, extension, newVolume);
|
||||
}
|
||||
await dataContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Save changes after processing all series
|
||||
|
|
@ -121,10 +141,6 @@ public static class MigrateMixedSpecials
|
|||
await dataContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
// Update all Volumes with Name as "0" -> Special
|
||||
logger.LogCritical("Updating all Volumes with Name 0 to SpecialNumber");
|
||||
|
||||
|
||||
|
||||
dataContext.ManualMigrationHistory.Add(new ManualMigrationHistory()
|
||||
{
|
||||
|
|
@ -137,4 +153,25 @@ public static class MigrateMixedSpecials
|
|||
logger.LogCritical(
|
||||
"Running ManualMigrateMixedSpecials migration - Completed. This is not an error");
|
||||
}
|
||||
|
||||
private static void UpdateCoverImage(IDirectoryService directoryService, ILogger<Program> logger, Chapter specialChapter,
|
||||
string extension, Volume newVolume)
|
||||
{
|
||||
// We need to migrate cover images as well
|
||||
var existingCover = ImageService.GetChapterFormat(specialChapter.Id, specialChapter.VolumeId) + extension;
|
||||
var newCover = ImageService.GetChapterFormat(specialChapter.Id, newVolume.Id) + extension;
|
||||
try
|
||||
{
|
||||
|
||||
if (!specialChapter.CoverImageLocked)
|
||||
{
|
||||
// First rename existing cover
|
||||
File.Copy(Path.Join(directoryService.CoverImageDirectory, existingCover), Path.Join(directoryService.CoverImageDirectory, newCover));
|
||||
specialChapter.CoverImage = newCover;
|
||||
}
|
||||
} catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Unable to rename {OldCover} to {NewCover}, this cover will need manual refresh", existingCover, newCover);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue