Fixed Series Relations Schema (#1654)
* Bump loader-utils from 2.0.2 to 2.0.3 in /UI/Web Bumps [loader-utils](https://github.com/webpack/loader-utils) from 2.0.2 to 2.0.3. - [Release notes](https://github.com/webpack/loader-utils/releases) - [Changelog](https://github.com/webpack/loader-utils/blob/v2.0.3/CHANGELOG.md) - [Commits](https://github.com/webpack/loader-utils/compare/v2.0.2...v2.0.3) --- updated-dependencies: - dependency-name: loader-utils dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * Fixed is want to read coming back as a string and not working correctly. * Changed from to Continue to be more explicit * Added the first migration which exports data as a csv in temp/. This is the backup in case data is lost in the migration. * Note for later * Fixed the migration for the series relation so when deleting any series on any edge of the relationship, the SeriesRelation row deletes. * Change buttons back to titles on series detail page * Wrote the code to import relations from the backup. * Added an additional version check to avoid file io on migration. * Code cleanup Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This commit is contained in:
parent
d5a7c31c7d
commit
15e09a0cf1
18 changed files with 2111 additions and 83 deletions
64
API/Data/MigrateSeriesRelationsImport.cs
Normal file
64
API/Data/MigrateSeriesRelationsImport.cs
Normal file
|
@ -0,0 +1,64 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using API.Entities.Enums;
|
||||
using API.Entities.Metadata;
|
||||
using CsvHelper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace API.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Introduced in v0.6.1.2 and v0.7, this imports to a temp file the existing series relationships. It is a 3 part migration.
|
||||
/// This will run last, to import the data and re-construct the relationships.
|
||||
/// </summary>
|
||||
public static class MigrateSeriesRelationsImport
|
||||
{
|
||||
private const string OutputFile = "config/relations.csv";
|
||||
private const string CompleteOutputFile = "config/relations-imported.csv";
|
||||
public static async Task Migrate(DataContext dataContext, ILogger<Program> logger)
|
||||
{
|
||||
logger.LogCritical("Running MigrateSeriesRelationsImport migration - Please be patient, this may take some time. This is not an error");
|
||||
if (!new FileInfo(OutputFile).Exists)
|
||||
{
|
||||
logger.LogCritical("Running MigrateSeriesRelationsImport migration - complete. Nothing to do");
|
||||
return;
|
||||
}
|
||||
|
||||
logger.LogCritical("Loading backed up relationships into the DB");
|
||||
List<SeriesRelationMigrationOutput> records;
|
||||
using var reader = new StreamReader(OutputFile);
|
||||
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
|
||||
{
|
||||
records = csv.GetRecords<SeriesRelationMigrationOutput>().ToList();
|
||||
}
|
||||
|
||||
foreach (var relation in records)
|
||||
{
|
||||
logger.LogCritical("Importing {SeriesName} --{RelationshipKind}--> {TargetSeriesName}",
|
||||
relation.SeriesName, relation.Relationship, relation.TargetSeriesName);
|
||||
|
||||
// Filter out series that don't exist
|
||||
if (!await dataContext.Series.AnyAsync(s => s.Id == relation.SeriesId) ||
|
||||
!await dataContext.Series.AnyAsync(s => s.Id == relation.TargetId))
|
||||
continue;
|
||||
|
||||
await dataContext.SeriesRelation.AddAsync(new SeriesRelation()
|
||||
{
|
||||
SeriesId = relation.SeriesId,
|
||||
TargetSeriesId = relation.TargetId,
|
||||
RelationKind = relation.Relationship
|
||||
});
|
||||
|
||||
}
|
||||
await dataContext.SaveChangesAsync();
|
||||
|
||||
File.Move(OutputFile, CompleteOutputFile);
|
||||
|
||||
logger.LogCritical("Running MigrateSeriesRelationsImport migration - Completed. This is not an error");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue