|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using API.Entities; |
| 7 | +using API.Entities.History; |
| 8 | +using API.Services; |
| 9 | +using CsvHelper; |
| 10 | +using CsvHelper.Configuration.Attributes; |
| 11 | +using Kavita.Common.EnvironmentInfo; |
| 12 | +using Microsoft.EntityFrameworkCore; |
| 13 | +using Microsoft.Extensions.Logging; |
| 14 | + |
| 15 | +namespace API.Data.ManualMigrations; |
| 16 | + |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// v0.8.5 - Progress is extracted and saved in a csv since PDF parser has massive changes |
| 20 | +/// </summary> |
| 21 | +public static class MigrateProgressExportForV085 |
| 22 | +{ |
| 23 | + public static async Task Migrate(DataContext dataContext, IDirectoryService directoryService, ILogger<Program> logger) |
| 24 | + { |
| 25 | + try |
| 26 | + { |
| 27 | + if (await dataContext.ManualMigrationHistory.AnyAsync(m => m.Name == "MigrateProgressExportForV085")) |
| 28 | + { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + logger.LogCritical( |
| 33 | + "Running MigrateProgressExportForV085 migration - Please be patient, this may take some time. This is not an error"); |
| 34 | + |
| 35 | + var data = await dataContext.AppUserProgresses |
| 36 | + .Join(dataContext.Series, progress => progress.SeriesId, series => series.Id, (progress, series) => new { progress, series }) |
| 37 | + .Join(dataContext.Volume, ps => ps.progress.VolumeId, volume => volume.Id, (ps, volume) => new { ps.progress, ps.series, volume }) |
| 38 | + .Join(dataContext.Chapter, psv => psv.progress.ChapterId, chapter => chapter.Id, (psv, chapter) => new { psv.progress, psv.series, psv.volume, chapter }) |
| 39 | + .Join(dataContext.MangaFile, psvc => psvc.chapter.Id, mangaFile => mangaFile.ChapterId, (psvc, mangaFile) => new { psvc.progress, psvc.series, psvc.volume, psvc.chapter, mangaFile }) |
| 40 | + .Join(dataContext.AppUser, psvcm => psvcm.progress.AppUserId, appUser => appUser.Id, (psvcm, appUser) => new |
| 41 | + { |
| 42 | + LibraryId = psvcm.series.LibraryId, |
| 43 | + LibraryName = psvcm.series.Library.Name, |
| 44 | + SeriesName = psvcm.series.Name, |
| 45 | + VolumeRange = psvcm.volume.MinNumber + "-" + psvcm.volume.MaxNumber, |
| 46 | + VolumeLookupName = psvcm.volume.Name, |
| 47 | + ChapterRange = psvcm.chapter.Range, |
| 48 | + MangaFileName = psvcm.mangaFile.FileName, |
| 49 | + MangaFilePath = psvcm.mangaFile.FilePath, |
| 50 | + AppUserName = appUser.UserName, |
| 51 | + AppUserId = appUser.Id, |
| 52 | + PagesRead = psvcm.progress.PagesRead, |
| 53 | + BookScrollId = psvcm.progress.BookScrollId, |
| 54 | + ProgressCreated = psvcm.progress.Created, |
| 55 | + ProgressLastModified = psvcm.progress.LastModified |
| 56 | + }).ToListAsync(); |
| 57 | + |
| 58 | + |
| 59 | + // Write the mapped data to a CSV file |
| 60 | + await using var writer = new StreamWriter(Path.Join(directoryService.ConfigDirectory, "progress_export-v0.8.5.csv")); |
| 61 | + await using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture); |
| 62 | + await csv.WriteRecordsAsync(data); |
| 63 | + |
| 64 | + logger.LogCritical( |
| 65 | + "Running MigrateProgressExportForV085 migration - Completed. This is not an error"); |
| 66 | + } |
| 67 | + catch (Exception ex) |
| 68 | + { |
| 69 | + // On new installs, the db isn't setup yet, so this has nothing to do |
| 70 | + } |
| 71 | + |
| 72 | + dataContext.ManualMigrationHistory.Add(new ManualMigrationHistory() |
| 73 | + { |
| 74 | + Name = "MigrateProgressExportForV085", |
| 75 | + ProductVersion = BuildInfo.Version.ToString(), |
| 76 | + RanAt = DateTime.UtcNow |
| 77 | + }); |
| 78 | + await dataContext.SaveChangesAsync(); |
| 79 | + } |
| 80 | +} |
0 commit comments