Skip to content

Commit

Permalink
Last issue before release (#2681)
Browse files Browse the repository at this point in the history
  • Loading branch information
majora2007 authored Feb 2, 2024
1 parent 149c30b commit 355ea24
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 35 deletions.
24 changes: 17 additions & 7 deletions API/Controllers/MetadataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public async Task<ActionResult<SeriesDetailPlusDto>> GetKavitaPlusSeriesDetailDa
if (user == null) return Unauthorized();

var userReviews = (await unitOfWork.UserRepository.GetUserRatingDtosForSeriesAsync(seriesId, user.Id))
.Where(r => !string.IsNullOrEmpty(r.BodyJustText))
.Where(r => !string.IsNullOrEmpty(r.Body))
.OrderByDescending(review => review.Username.Equals(user.UserName) ? 1 : 0)
.ToList();

Expand All @@ -221,12 +221,22 @@ public async Task<ActionResult<SeriesDetailPlusDto>> GetKavitaPlusSeriesDetailDa
}

var ret = await metadataService.GetSeriesDetail(user.Id, seriesId);
if (ret == null) return Ok(new SeriesDetailPlusDto()
if (ret == null)
{
Reviews = userReviews,
Recommendations = null,
Ratings = null
});
// Cache an empty result, so we don't constantly hit K+ when we know nothing is going to resolve
ret = new SeriesDetailPlusDto()
{
Reviews = new List<UserReviewDto>(),
Recommendations = null,
Ratings = null
};
await _cacheProvider.SetAsync(cacheKey, ret, TimeSpan.FromHours(48));

var newCacheResult2 = (await _cacheProvider.GetAsync<SeriesDetailPlusDto>(cacheKey)).Value;
await PrepareSeriesDetail(userReviews, newCacheResult2, user);

return Ok(newCacheResult2);
}

await _cacheProvider.SetAsync(cacheKey, ret, TimeSpan.FromHours(48));

Expand All @@ -244,7 +254,7 @@ private async Task PrepareSeriesDetail(List<UserReviewDto> userReviews, SeriesDe
userReviews.AddRange(ReviewService.SelectSpectrumOfReviews(ret.Reviews.ToList()));
ret.Reviews = userReviews;

if (!isAdmin)
if (!isAdmin && ret.Recommendations != null)
{
// Re-obtain owned series and take into account age restriction
ret.Recommendations.OwnedSeries =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Threading.Tasks;
using API.Entities;
using Kavita.Common.EnvironmentInfo;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace API.Data.ManualMigrations;

/// <summary>
/// For the v0.7.14 release, one of the nightlies had bad data that would cause issues. This drops those records
/// </summary>
public static class MigrateClearNightlyExternalSeriesRecords
{
public static async Task Migrate(DataContext dataContext, ILogger<Program> logger)
{
if (await dataContext.ManualMigrationHistory.AnyAsync(m => m.Name == "MigrateClearNightlyExternalSeriesRecords"))
{
return;
}

logger.LogCritical(
"Running MigrateClearNightlyExternalSeriesRecords migration - Please be patient, this may take some time. This is not an error");

dataContext.ExternalSeriesMetadata.RemoveRange(dataContext.ExternalSeriesMetadata);
dataContext.ExternalRating.RemoveRange(dataContext.ExternalRating);
dataContext.ExternalRecommendation.RemoveRange(dataContext.ExternalRecommendation);
dataContext.ExternalReview.RemoveRange(dataContext.ExternalReview);

dataContext.ManualMigrationHistory.Add(new ManualMigrationHistory()
{
Name = "MigrateClearNightlyExternalSeriesRecords",
ProductVersion = BuildInfo.Version.ToString(),
RanAt = DateTime.UtcNow
});

await dataContext.SaveChangesAsync();

logger.LogCritical(
"Running MigrateClearNightlyExternalSeriesRecords migration - Completed. This is not an error");
}
}
34 changes: 8 additions & 26 deletions API/Data/ManualMigrations/MigrateWantToReadExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,19 @@ public static async Task Migrate(DataContext dataContext, IDirectoryService dire
logger.LogCritical(
"Running MigrateWantToReadExport migration - Please be patient, this may take some time. This is not an error");

var columnExists = false;
await using var command = dataContext.Database.GetDbConnection().CreateCommand();
command.CommandText = "PRAGMA table_info('Series')";

await dataContext.Database.OpenConnectionAsync();
await using var result = await command.ExecuteReaderAsync();
while (await result.ReadAsync())
{
var columnName = result["name"].ToString();
if (columnName != "AppUserId") continue;

logger.LogInformation("Column 'AppUserId' exists in the 'Series' table. Running migration...");
// Your migration logic here
columnExists = true;
break;
}

await result.CloseAsync();

if (!columnExists)
var importFile = Path.Join(directoryService.ConfigDirectory, "want-to-read-migration.csv");
if (File.Exists(importFile))
{
logger.LogCritical(
"Running MigrateWantToReadExport migration - Completed. This is not an error");
return;
}

await using var command2 = dataContext.Database.GetDbConnection().CreateCommand();
await using var command = dataContext.Database.GetDbConnection().CreateCommand();
command.CommandText = "Select AppUserId, Id from Series WHERE AppUserId IS NOT NULL ORDER BY AppUserId;";

await dataContext.Database.OpenConnectionAsync();
await using var result2 = await command.ExecuteReaderAsync();
await using var result = await command.ExecuteReaderAsync();

await using var writer = new StreamWriter(Path.Join(directoryService.ConfigDirectory, "want-to-read-migration.csv"));
await using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture);
Expand All @@ -62,10 +44,10 @@ public static async Task Migrate(DataContext dataContext, IDirectoryService dire
await csvWriter.NextRecordAsync();

// Write data
while (await result2.ReadAsync())
while (await result.ReadAsync())
{
var appUserId = result2["AppUserId"].ToString();
var id = result2["Id"].ToString();
var appUserId = result["AppUserId"].ToString();
var id = result["Id"].ToString();

csvWriter.WriteField(appUserId);
csvWriter.WriteField(id);
Expand All @@ -75,7 +57,7 @@ public static async Task Migrate(DataContext dataContext, IDirectoryService dire

try
{
await result2.CloseAsync();
await dataContext.Database.CloseConnectionAsync();
writer.Close();
} catch (Exception) {/* Swallow */}

Expand Down
4 changes: 3 additions & 1 deletion API/Data/Repositories/VolumeRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ private async Task AddVolumeModifiers(int userId, IReadOnlyCollection<VolumeDto>
c.LastReadingProgress = progresses.Max(p => p.LastModified);
}

v.PagesRead = userProgress.Where(p => p.VolumeId == v.Id).Sum(p => p.PagesRead);
v.PagesRead = userProgress
.Where(p => p.VolumeId == v.Id)
.Sum(p => p.PagesRead);
}
}
}
1 change: 1 addition & 0 deletions API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJo
await MigrateVolumeNumber.Migrate(unitOfWork, dataContext, logger);
await MigrateWantToReadImport.Migrate(unitOfWork, directoryService, logger);
await MigrateManualHistory.Migrate(dataContext, logger);
await MigrateClearNightlyExternalSeriesRecords.Migrate(dataContext, logger);

// Update the version in the DB after all migrations are run
var installVersion = await unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.InstallVersion);
Expand Down
2 changes: 1 addition & 1 deletion openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"name": "GPL-3.0",
"url": "https://github.com/Kareadita/Kavita/blob/develop/LICENSE"
},
"version": "0.7.13.13"
"version": "0.7.13.15"
},
"servers": [
{
Expand Down

0 comments on commit 355ea24

Please sign in to comment.