Updating the database scheme

This commit is contained in:
Zoe Roux 2020-05-08 02:21:59 +02:00
parent 602661db40
commit 90a7d6654e
9 changed files with 188 additions and 121 deletions

View File

@ -12,7 +12,12 @@ namespace Kyoo.Models
public string Poster { get; set; }
public string Overview { get; set; }
[JsonIgnore] public string ImgPrimary { get; set; }
public virtual IEnumerable<Show> Shows { get; set; }
[JsonIgnore] public virtual IEnumerable<CollectionLink> Links { get; set; }
public virtual IEnumerable<Show> Shows
{
get => Links.Select(x => x.Show);
set => Links = value.Select(x => new CollectionLink(this, x));
}
public Collection() { }

View File

@ -7,5 +7,15 @@ namespace Kyoo.Models
public virtual Collection Collection { get; set; }
public long ShowID { get; set; }
public virtual Show Show { get; set; }
public CollectionLink() { }
public CollectionLink(Collection collection, Show show)
{
Collection = collection;
CollectionID = collection.ID;
Show = show;
ShowID = show.ID;
}
}
}

View File

@ -430,25 +430,14 @@ namespace Kyoo.Controllers
return x;
}).ToList();
show.People = show.People?.Select(x =>
{
x.People = Validate(x.People);
x.PeopleID = x.People.ID;
return x;
}).ToList();
show.Seasons = show.Seasons?.Select(x =>
{
return _database.Seasons.FirstOrDefault(y => y.ShowID == x.ShowID
&& y.SeasonNumber == x.SeasonNumber) ?? Validate(x);
}).ToList();
show.Episodes = show.Episodes?.Select(x =>
{
return _database.Episodes.FirstOrDefault(y => y.ShowID == x.ShowID
&& y.SeasonNumber == x.SeasonNumber
&& y.EpisodeNumber == x.EpisodeNumber) ?? Validate(x);
}).ToList();
show.People = show.People?.GroupBy(x => x.Slug)
.Select(x => x.First())
.Select(x =>
{
x.People = Validate(x.People);
x.PeopleID = x.People.ID;
return x;
}).ToList();
show.ExternalIDs = Validate(show.ExternalIDs);
return show;
}
@ -457,13 +446,8 @@ namespace Kyoo.Controllers
{
if (season == null)
return null;
season.Episodes = season.Episodes?.Select(x =>
{
return _database.Episodes.FirstOrDefault(y => y.ShowID == x.ShowID
&& y.SeasonNumber == x.SeasonNumber
&& y.EpisodeNumber == x.EpisodeNumber) ?? Validate(x);
}).ToList();
season.Show = Validate(season.Show);
season.ExternalIDs = Validate(season.ExternalIDs);
return season;
}
@ -472,13 +456,9 @@ namespace Kyoo.Controllers
{
if (episode == null)
return null;
Season old = _database.Seasons.FirstOrDefault(x => x.ShowID == episode.ShowID
&& x.SeasonNumber == episode.SeasonNumber);
if (old != null)
episode.Season = old;
else
episode.Season.ExternalIDs = Validate(episode.Season.ExternalIDs);
episode.Show = GetShow(episode.Show.Slug) ?? Validate(episode.Show);
episode.Season = GetSeason(episode.Show.Slug, episode.SeasonNumber) ?? Validate(episode.Season);
episode.ExternalIDs = Validate(episode.ExternalIDs);
return episode;
}

View File

@ -117,17 +117,17 @@ namespace Kyoo
modelBuilder.Entity<Show>()
.Ignore(x => x.Genres);
modelBuilder.Entity<Collection>()
.Ignore(x => x.Shows);
modelBuilder.Entity<PeopleLink>()
.Ignore(x => x.Slug);
modelBuilder.Entity<PeopleLink>()
.Ignore(x => x.Name);
modelBuilder.Entity<PeopleLink>()
.Ignore(x => x.Slug)
.Ignore(x => x.Name)
.Ignore(x => x.ExternalIDs);
modelBuilder.Entity<ProviderLink>()
.Ignore(x => x.Name);
modelBuilder.Entity<ProviderLink>()
.Ignore(x => x.Name)
.Ignore(x => x.Logo);

View File

@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Kyoo.Models.DatabaseMigrations.Internal
{
[DbContext(typeof(DatabaseContext))]
[Migration("20200426223831_Initial")]
[Migration("20200507185533_Initial")]
partial class Initial
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -27,6 +27,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.Property<string>("ImgPrimary")
.HasColumnType("TEXT");
b.Property<long?>("LibraryID")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
@ -41,6 +44,8 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasKey("ID");
b.HasIndex("LibraryID");
b.HasIndex("Slug")
.IsUnique();
@ -271,10 +276,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("PeopleID")
.HasColumnType("TEXT");
b.Property<long?>("PeopleID1")
b.Property<long>("PeopleID")
.HasColumnType("INTEGER");
b.Property<string>("Role")
@ -288,7 +290,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasKey("ID");
b.HasIndex("PeopleID1");
b.HasIndex("PeopleID");
b.HasIndex("ShowID");
@ -385,6 +387,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.Property<bool>("IsMovie")
.HasColumnType("INTEGER");
b.Property<long?>("LibraryID")
.HasColumnType("INTEGER");
b.Property<string>("Logo")
.HasColumnType("TEXT");
@ -417,6 +422,8 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasKey("ID");
b.HasIndex("LibraryID");
b.HasIndex("Slug")
.IsUnique();
@ -485,10 +492,17 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.ToTable("Tracks");
});
modelBuilder.Entity("Kyoo.Models.Collection", b =>
{
b.HasOne("Kyoo.Models.Library", null)
.WithMany("Collections")
.HasForeignKey("LibraryID");
});
modelBuilder.Entity("Kyoo.Models.CollectionLink", b =>
{
b.HasOne("Kyoo.Models.Collection", "Collection")
.WithMany()
.WithMany("Links")
.HasForeignKey("CollectionID");
b.HasOne("Kyoo.Models.Show", "Show")
@ -572,7 +586,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
{
b.HasOne("Kyoo.Models.People", "People")
.WithMany("Roles")
.HasForeignKey("PeopleID1");
.HasForeignKey("PeopleID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Kyoo.Models.Show", "Show")
.WithMany("People")
@ -605,6 +621,10 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
modelBuilder.Entity("Kyoo.Models.Show", b =>
{
b.HasOne("Kyoo.Models.Library", null)
.WithMany("Shows")
.HasForeignKey("LibraryID");
b.HasOne("Kyoo.Models.Studio", "Studio")
.WithMany()
.HasForeignKey("StudioID");

View File

@ -7,23 +7,6 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Collections",
columns: table => new
{
ID = table.Column<long>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Slug = table.Column<string>(nullable: true),
Name = table.Column<string>(nullable: true),
Poster = table.Column<string>(nullable: true),
Overview = table.Column<string>(nullable: true),
ImgPrimary = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Collections", x => x.ID);
});
migrationBuilder.CreateTable(
name: "Genres",
columns: table => new
@ -96,6 +79,30 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
table.PrimaryKey("PK_Studios", x => x.ID);
});
migrationBuilder.CreateTable(
name: "Collections",
columns: table => new
{
ID = table.Column<long>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Slug = table.Column<string>(nullable: true),
Name = table.Column<string>(nullable: true),
Poster = table.Column<string>(nullable: true),
Overview = table.Column<string>(nullable: true),
ImgPrimary = table.Column<string>(nullable: true),
LibraryID = table.Column<long>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Collections", x => x.ID);
table.ForeignKey(
name: "FK_Collections_Libraries_LibraryID",
column: x => x.LibraryID,
principalTable: "Libraries",
principalColumn: "ID",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable(
name: "ProviderLinks",
columns: table => new
@ -141,11 +148,18 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
Logo = table.Column<string>(nullable: true),
Backdrop = table.Column<string>(nullable: true),
IsMovie = table.Column<bool>(nullable: false),
StudioID = table.Column<long>(nullable: true)
StudioID = table.Column<long>(nullable: true),
LibraryID = table.Column<long>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Shows", x => x.ID);
table.ForeignKey(
name: "FK_Shows_Libraries_LibraryID",
column: x => x.LibraryID,
principalTable: "Libraries",
principalColumn: "ID",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Shows_Studios_StudioID",
column: x => x.StudioID,
@ -243,8 +257,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
{
ID = table.Column<long>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
PeopleID = table.Column<string>(nullable: true),
PeopleID1 = table.Column<long>(nullable: true),
PeopleID = table.Column<long>(nullable: false),
ShowID = table.Column<long>(nullable: false),
Role = table.Column<string>(nullable: true),
Type = table.Column<string>(nullable: true)
@ -253,11 +266,11 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
{
table.PrimaryKey("PK_PeopleLinks", x => x.ID);
table.ForeignKey(
name: "FK_PeopleLinks_Peoples_PeopleID1",
column: x => x.PeopleID1,
name: "FK_PeopleLinks_Peoples_PeopleID",
column: x => x.PeopleID,
principalTable: "Peoples",
principalColumn: "ID",
onDelete: ReferentialAction.Restrict);
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_PeopleLinks_Shows_ShowID",
column: x => x.ShowID,
@ -411,6 +424,11 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
table: "CollectionLinks",
column: "ShowID");
migrationBuilder.CreateIndex(
name: "IX_Collections_LibraryID",
table: "Collections",
column: "LibraryID");
migrationBuilder.CreateIndex(
name: "IX_Collections_Slug",
table: "Collections",
@ -485,9 +503,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
column: "ShowID");
migrationBuilder.CreateIndex(
name: "IX_PeopleLinks_PeopleID1",
name: "IX_PeopleLinks_PeopleID",
table: "PeopleLinks",
column: "PeopleID1");
column: "PeopleID");
migrationBuilder.CreateIndex(
name: "IX_PeopleLinks_ShowID",
@ -521,6 +539,11 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
table: "Seasons",
column: "ShowID");
migrationBuilder.CreateIndex(
name: "IX_Shows_LibraryID",
table: "Shows",
column: "LibraryID");
migrationBuilder.CreateIndex(
name: "IX_Shows_Slug",
table: "Shows",
@ -576,9 +599,6 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
migrationBuilder.DropTable(
name: "Peoples");
migrationBuilder.DropTable(
name: "Libraries");
migrationBuilder.DropTable(
name: "Providers");
@ -591,6 +611,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
migrationBuilder.DropTable(
name: "Shows");
migrationBuilder.DropTable(
name: "Libraries");
migrationBuilder.DropTable(
name: "Studios");
}

View File

@ -25,6 +25,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.Property<string>("ImgPrimary")
.HasColumnType("TEXT");
b.Property<long?>("LibraryID")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.HasColumnType("TEXT");
@ -39,6 +42,8 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasKey("ID");
b.HasIndex("LibraryID");
b.HasIndex("Slug")
.IsUnique();
@ -269,10 +274,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("PeopleID")
.HasColumnType("TEXT");
b.Property<long?>("PeopleID1")
b.Property<long>("PeopleID")
.HasColumnType("INTEGER");
b.Property<string>("Role")
@ -286,7 +288,7 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasKey("ID");
b.HasIndex("PeopleID1");
b.HasIndex("PeopleID");
b.HasIndex("ShowID");
@ -383,6 +385,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.Property<bool>("IsMovie")
.HasColumnType("INTEGER");
b.Property<long?>("LibraryID")
.HasColumnType("INTEGER");
b.Property<string>("Logo")
.HasColumnType("TEXT");
@ -415,6 +420,8 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.HasKey("ID");
b.HasIndex("LibraryID");
b.HasIndex("Slug")
.IsUnique();
@ -483,10 +490,17 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
b.ToTable("Tracks");
});
modelBuilder.Entity("Kyoo.Models.Collection", b =>
{
b.HasOne("Kyoo.Models.Library", null)
.WithMany("Collections")
.HasForeignKey("LibraryID");
});
modelBuilder.Entity("Kyoo.Models.CollectionLink", b =>
{
b.HasOne("Kyoo.Models.Collection", "Collection")
.WithMany()
.WithMany("Links")
.HasForeignKey("CollectionID");
b.HasOne("Kyoo.Models.Show", "Show")
@ -570,7 +584,9 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
{
b.HasOne("Kyoo.Models.People", "People")
.WithMany("Roles")
.HasForeignKey("PeopleID1");
.HasForeignKey("PeopleID")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Kyoo.Models.Show", "Show")
.WithMany("People")
@ -603,6 +619,10 @@ namespace Kyoo.Models.DatabaseMigrations.Internal
modelBuilder.Entity("Kyoo.Models.Show", b =>
{
b.HasOne("Kyoo.Models.Library", null)
.WithMany("Shows")
.HasForeignKey("LibraryID");
b.HasOne("Kyoo.Models.Studio", "Studio")
.WithMany()
.HasForeignKey("StudioID");

View File

@ -8,6 +8,7 @@ using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Kyoo.Models.Watch;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Kyoo.Controllers
@ -21,7 +22,7 @@ namespace Kyoo.Controllers
public bool RunOnStartup => true;
public int Priority => 0;
private ILibraryManager _libraryManager;
private IServiceProvider _serviceProvider;
private IThumbnailsManager _thumbnailsManager;
private IProviderManager _metadataProvider;
private ITranscoder _transcoder;
@ -29,7 +30,9 @@ namespace Kyoo.Controllers
public IEnumerable<string> GetPossibleParameters()
{
return _libraryManager.GetLibraries().Select(x => x.Slug);
using IServiceScope serviceScope = _serviceProvider.CreateScope();
ILibraryManager libraryManager = serviceScope.ServiceProvider.GetService<ILibraryManager>();
return libraryManager.GetLibraries().Select(x => x.Slug);
}
public int? Progress()
@ -40,30 +43,30 @@ namespace Kyoo.Controllers
public async Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string argument = null)
{
// TODO Should use more scopes of the library manager (one per episodes to register).
using IServiceScope serviceScope = serviceProvider.CreateScope();
_libraryManager = serviceScope.ServiceProvider.GetService<ILibraryManager>();
_thumbnailsManager = serviceScope.ServiceProvider.GetService<IThumbnailsManager>();
_metadataProvider = serviceScope.ServiceProvider.GetService<IProviderManager>();
_transcoder = serviceScope.ServiceProvider.GetService<ITranscoder>();
_config = serviceScope.ServiceProvider.GetService<IConfiguration>();
_serviceProvider = serviceProvider;
_thumbnailsManager = serviceProvider.GetService<IThumbnailsManager>();
_metadataProvider = serviceProvider.GetService<IProviderManager>();
_transcoder = serviceProvider.GetService<ITranscoder>();
_config = serviceProvider.GetService<IConfiguration>();
try
{
IEnumerable<Episode> episodes = _libraryManager.GetEpisodes();
using IServiceScope serviceScope = _serviceProvider.CreateScope();
ILibraryManager libraryManager = serviceScope.ServiceProvider.GetService<ILibraryManager>();
IEnumerable<Episode> episodes = libraryManager.GetEpisodes();
IEnumerable<Library> libraries = argument == null
? _libraryManager.GetLibraries()
: new [] {_libraryManager.GetLibrary(argument)};
? libraryManager.GetLibraries()
: new [] {libraryManager.GetLibrary(argument)};
foreach (Episode episode in episodes)
{
if (!File.Exists(episode.Path))
_libraryManager.RemoveEpisode(episode);
libraryManager.RemoveEpisode(episode);
}
await _libraryManager.SaveChanges();
await libraryManager.SaveChanges();
foreach (Library library in libraries)
await Scan(library, cancellationToken);
await Scan(library, libraryManager, cancellationToken);
}
catch (Exception ex)
{
@ -72,7 +75,7 @@ namespace Kyoo.Controllers
Console.WriteLine("Scan finished!");
}
private async Task Scan(Library library, CancellationToken cancellationToken)
private async Task Scan(Library library, ILibraryManager libraryManager, CancellationToken cancellationToken)
{
Console.WriteLine($"Scanning library {library.Name} at {string.Join(", ", library.Paths)}.");
foreach (string path in library.Paths)
@ -106,11 +109,11 @@ namespace Kyoo.Controllers
}
await Task.WhenAll(files.Select(file =>
{
if (!IsVideo(file) || _libraryManager.GetEpisodes().Any(x => x.Path == file))
return null;
if (!IsVideo(file) || libraryManager.GetEpisodes().Any(x => x.Path == file))
return Task.CompletedTask;
string relativePath = file.Substring(path.Length);
return RegisterFile(file, relativePath, library, cancellationToken);
}).Where(x => x != null));
}));
}
}
@ -119,6 +122,10 @@ namespace Kyoo.Controllers
if (token.IsCancellationRequested)
return;
using IServiceScope serviceScope = _serviceProvider.CreateScope();
ILibraryManager libraryManager = serviceScope.ServiceProvider.GetService<ILibraryManager>();
((DbSet<Library>)libraryManager.GetLibraries()).Attach(library);
Console.WriteLine($"Registering episode at: {path}");
string patern = _config.GetValue<string>("regex");
Regex regex = new Regex(patern, RegexOptions.IgnoreCase);
@ -131,33 +138,36 @@ namespace Kyoo.Controllers
long episodeNumber = long.TryParse(match.Groups["Episode"].Value, out tmp) ? tmp : -1;
long absoluteNumber = long.TryParse(match.Groups["Absolute"].Value, out tmp) ? tmp : -1;
Collection collection = await GetCollection(collectionName, library);
Collection collection = await GetCollection(libraryManager, collectionName, library);
bool isMovie = seasonNumber == -1 && episodeNumber == -1 && absoluteNumber == -1;
Show show = await GetShow(showName, showPath, isMovie, library);
Show show = await GetShow(libraryManager, showName, showPath, isMovie, library);
if (isMovie)
_libraryManager.Register(await GetMovie(show, path));
libraryManager.Register(await GetMovie(show, path));
else
{
Season season = await GetSeason(show, seasonNumber, library);
Episode episode = await GetEpisode(show, season, episodeNumber, absoluteNumber, path, library);
_libraryManager.Register(episode);
Season season = await GetSeason(libraryManager, show, seasonNumber, library);
Episode episode = await GetEpisode(libraryManager, show, season, episodeNumber, absoluteNumber, path, library);
libraryManager.Register(episode);
}
if (collection != null)
_libraryManager.Register(collection);
_libraryManager.RegisterShowLinks(library, collection, show);
await _libraryManager.SaveChanges();
libraryManager.Register(collection);
libraryManager.RegisterShowLinks(library, collection, show);
await libraryManager.SaveChanges();
}
private async Task<Collection> GetCollection(string collectionName, Library library)
private async Task<Collection> GetCollection(ILibraryManager libraryManager, string collectionName, Library library)
{
if (string.IsNullOrEmpty(collectionName))
return await Task.FromResult<Collection>(null);
return _libraryManager.GetCollection(Utility.ToSlug(collectionName)) ?? await _metadataProvider.GetCollectionFromName(collectionName, library);
Collection name = libraryManager.GetCollection(Utility.ToSlug(collectionName));
if (name != null)
return name;
return await _metadataProvider.GetCollectionFromName(collectionName, library);
}
private async Task<Show> GetShow(string showTitle, string showPath, bool isMovie, Library library)
private async Task<Show> GetShow(ILibraryManager libraryManager, string showTitle, string showPath, bool isMovie, Library library)
{
Show show = _libraryManager.GetShowByPath(showPath);
Show show = libraryManager.GetShowByPath(showPath);
if (show != null)
return show;
show = await _metadataProvider.SearchShow(showTitle, isMovie, library);
@ -170,11 +180,11 @@ namespace Kyoo.Controllers
return show;
}
private async Task<Season> GetSeason(Show show, long seasonNumber, Library library)
private async Task<Season> GetSeason(ILibraryManager libraryManager, Show show, long seasonNumber, Library library)
{
if (seasonNumber == -1)
return default;
Season season = _libraryManager.GetSeason(show.Slug, seasonNumber);
Season season = libraryManager.GetSeason(show.Slug, seasonNumber);
if (season == null)
{
season = await _metadataProvider.GetSeason(show, seasonNumber, library);
@ -184,11 +194,11 @@ namespace Kyoo.Controllers
return season;
}
private async Task<Episode> GetEpisode(Show show, Season season, long episodeNumber, long absoluteNumber, string episodePath, Library library)
private async Task<Episode> GetEpisode(ILibraryManager libraryManager, Show show, Season season, long episodeNumber, long absoluteNumber, string episodePath, Library library)
{
Episode episode = await _metadataProvider.GetEpisode(show, episodePath, season?.SeasonNumber ?? -1, episodeNumber, absoluteNumber, library);
if (season == null)
season = await GetSeason(show, episode.SeasonNumber, library);
season = await GetSeason(libraryManager, show, episode.SeasonNumber, library);
episode.Season = season;
if (season == null)
{

View File

@ -25,12 +25,11 @@ namespace Kyoo.Api
if (people == null)
return NotFound();
Collection collection = new Collection(people.Slug, people.Name, null, null)
return new Collection(people.Slug, people.Name, null, null)
{
Shows = people.Roles.Select(x => x.Show),
Poster = "peopleimg/" + people.Slug
};
return collection;
}
}
}