Sort by Average Rating and Big Want to Read fix (#2672)

This commit is contained in:
Joe Milazzo 2024-02-01 06:23:45 -06:00 committed by GitHub
parent 03e7d38482
commit 1fd72ada36
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
42 changed files with 3552 additions and 105 deletions

View file

@ -0,0 +1,86 @@
using System;
using System.Threading.Tasks;
using API.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace API.Data.ManualMigrations;
/// <summary>
/// Introduced in v0.7.14, will store history so that going forward, migrations can just check against the history
/// and I don't need to remove old migrations
/// </summary>
public static class MigrateManualHistory
{
public static async Task Migrate(DataContext dataContext, ILogger<Program> logger)
{
logger.LogCritical(
"Running MigrateManualHistory migration - Please be patient, this may take some time. This is not an error");
if (await dataContext.ManualMigrationHistory.AnyAsync())
{
logger.LogCritical(
"Running MigrateManualHistory migration - Completed. This is not an error");
return;
}
dataContext.ManualMigrationHistory.Add(new ManualMigrationHistory()
{
Name = "MigrateUserLibrarySideNavStream",
ProductVersion = "0.7.9.0",
RanAt = DateTime.UtcNow
});
dataContext.ManualMigrationHistory.Add(new ManualMigrationHistory()
{
Name = "MigrateSmartFilterEncoding",
ProductVersion = "0.7.11.0",
RanAt = DateTime.UtcNow
});
dataContext.ManualMigrationHistory.Add(new ManualMigrationHistory()
{
Name = "MigrateLibrariesToHaveAllFileTypes",
ProductVersion = "0.7.11.0",
RanAt = DateTime.UtcNow
});
dataContext.ManualMigrationHistory.Add(new ManualMigrationHistory()
{
Name = "MigrateEmailTemplates",
ProductVersion = "0.7.14.0",
RanAt = DateTime.UtcNow
});
dataContext.ManualMigrationHistory.Add(new ManualMigrationHistory()
{
Name = "MigrateVolumeNumber",
ProductVersion = "0.7.14.0",
RanAt = DateTime.UtcNow
});
dataContext.ManualMigrationHistory.Add(new ManualMigrationHistory()
{
Name = "MigrateWantToReadExport",
ProductVersion = "0.7.14.0",
RanAt = DateTime.UtcNow
});
dataContext.ManualMigrationHistory.Add(new ManualMigrationHistory()
{
Name = "MigrateWantToReadImport",
ProductVersion = "0.7.14.0",
RanAt = DateTime.UtcNow
});
dataContext.ManualMigrationHistory.Add(new ManualMigrationHistory()
{
Name = "MigrateManualHistory",
ProductVersion = "0.7.14.0",
RanAt = DateTime.UtcNow
});
await dataContext.SaveChangesAsync();
logger.LogCritical(
"Running MigrateManualHistory migration - Completed. This is not an error");
}
}

View file

@ -0,0 +1,81 @@
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using API.Services;
using CsvHelper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace API.Data.ManualMigrations;
/// <summary>
/// v0.7.13.12/v0.7.14 - Want to read is extracted and saved in a csv
/// </summary>
/// <remarks>This must run BEFORE any DB migrations</remarks>
public static class MigrateWantToReadExport
{
public static async Task Migrate(DataContext dataContext, IDirectoryService directoryService, ILogger<Program> logger)
{
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)
{
logger.LogCritical(
"Running MigrateWantToReadExport migration - Completed. This is not an error");
return;
}
await using var command2 = 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 writer = new StreamWriter(Path.Join(directoryService.ConfigDirectory, "want-to-read-migration.csv"));
await using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture);
// Write header
csvWriter.WriteField("AppUserId");
csvWriter.WriteField("Id");
await csvWriter.NextRecordAsync();
// Write data
while (await result2.ReadAsync())
{
var appUserId = result2["AppUserId"].ToString();
var id = result2["Id"].ToString();
csvWriter.WriteField(appUserId);
csvWriter.WriteField(id);
await csvWriter.NextRecordAsync();
}
await result2.CloseAsync();
writer.Close();
logger.LogCritical(
"Running MigrateWantToReadExport migration - Completed. This is not an error");
}
}

View file

@ -0,0 +1,60 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using API.Data.Repositories;
using API.Entities;
using API.Services;
using CsvHelper;
using Microsoft.Extensions.Logging;
namespace API.Data.ManualMigrations;
/// <summary>
/// v0.7.13.12/v0.7.14 - Want to read is imported from a csv
/// </summary>
public static class MigrateWantToReadImport
{
public static async Task Migrate(IUnitOfWork unitOfWork, IDirectoryService directoryService, ILogger<Program> logger)
{
var importFile = Path.Join(directoryService.ConfigDirectory, "want-to-read-migration.csv");
var outputFile = Path.Join(directoryService.ConfigDirectory, "imported-want-to-read-migration.csv");
logger.LogCritical(
"Running MigrateWantToReadImport migration - Please be patient, this may take some time. This is not an error");
if (!File.Exists(importFile) || File.Exists(outputFile))
{
logger.LogCritical(
"Running MigrateWantToReadImport migration - Completed. This is not an error");
return;
}
using var reader = new StreamReader(importFile);
using var csvReader = new CsvReader(reader, CultureInfo.InvariantCulture);
// Read the records from the CSV file
await csvReader.ReadAsync();
csvReader.ReadHeader(); // Skip the header row
while (await csvReader.ReadAsync())
{
// Read the values of AppUserId and Id columns
var appUserId = csvReader.GetField<int>("AppUserId");
var seriesId = csvReader.GetField<int>("Id");
var user = await unitOfWork.UserRepository.GetUserByIdAsync(appUserId, AppUserIncludes.WantToRead);
if (user == null || user.WantToRead.Any(w => w.SeriesId == seriesId)) continue;
user.WantToRead.Add(new AppUserWantToRead()
{
SeriesId = seriesId
});
}
await unitOfWork.CommitAsync();
reader.Close();
File.WriteAllLines(outputFile, await File.ReadAllLinesAsync(importFile));
logger.LogCritical(
"Running MigrateWantToReadImport migration - Completed. This is not an error");
}
}