Fixing SQLite episode triggers

This commit is contained in:
Zoe Roux 2021-06-27 20:21:03 +02:00
parent 27ed470970
commit 6f87139815
14 changed files with 45 additions and 48 deletions

View File

@ -60,7 +60,7 @@ namespace Kyoo.Models
[SerializeIgnore] public string ShowSlug { private get; set; }
/// <summary>
/// The ID of the Show containing this episode. This value is only set when the <see cref="Show"/> has been loaded.
/// The ID of the Show containing this episode.
/// </summary>
[SerializeIgnore] public int ShowID { get; set; }
/// <summary>
@ -69,7 +69,7 @@ namespace Kyoo.Models
[LoadableRelation(nameof(ShowID))] public Show Show { get; set; }
/// <summary>
/// The ID of the Season containing this episode. This value is only set when the <see cref="Season"/> has been loaded.
/// The ID of the Season containing this episode.
/// </summary>
[SerializeIgnore] public int? SeasonID { get; set; }
/// <summary>

View File

@ -41,7 +41,7 @@ namespace Kyoo.Models
[SerializeIgnore] public string ShowSlug { private get; set; }
/// <summary>
/// The ID of the Show containing this season. This value is only set when the <see cref="Show"/> has been loaded.
/// The ID of the Show containing this season.
/// </summary>
[SerializeIgnore] public int ShowID { get; set; }
/// <summary>

View File

@ -94,7 +94,7 @@ namespace Kyoo.Models
[EditableRelation] [LoadableRelation] public ICollection<MetadataID<Show>> ExternalIDs { get; set; }
/// <summary>
/// The ID of the Studio that made this show. This value is only set when the <see cref="Studio"/> has been loaded.
/// The ID of the Studio that made this show.
/// </summary>
[SerializeIgnore] public int? StudioID { get; set; }
/// <summary>

View File

@ -119,7 +119,7 @@ namespace Kyoo.Models
[SerializeIgnore] public StreamType Type { get; set; }
/// <summary>
/// The ID of the episode that uses this track. This value is only set when the <see cref="Episode"/> has been loaded.
/// The ID of the episode that uses this track.
/// </summary>
[SerializeIgnore] public int EpisodeID { get; set; }
/// <summary>

View File

@ -163,13 +163,9 @@ namespace Kyoo
Type type = typeof(T);
foreach (PropertyInfo property in type.GetProperties())
{
if (!property.CanWrite)
if (!property.CanWrite || property.GetCustomAttribute<ComputedAttribute>() != null)
continue;
object defaultValue = property.PropertyType.IsValueType
? Activator.CreateInstance(property.PropertyType)
: null;
property.SetValue(obj, defaultValue);
property.SetValue(obj, property.PropertyType.GetClrDefault());
}
return obj;

View File

@ -132,14 +132,6 @@ namespace Kyoo
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Track>()
.Property(t => t.IsDefault)
.ValueGeneratedNever();
modelBuilder.Entity<Track>()
.Property(t => t.IsForced)
.ValueGeneratedNever();
modelBuilder.Entity<Show>()
.HasMany(x => x.Seasons)
.WithOne(x => x.Show)

View File

@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Kyoo.Postgresql.Migrations
{
[DbContext(typeof(PostgresContext))]
[Migration("20210623174924_Initial")]
[Migration("20210627141933_Initial")]
partial class Initial
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -69,11 +69,11 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnName("id")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int>("AbsoluteNumber")
b.Property<int?>("AbsoluteNumber")
.HasColumnType("integer")
.HasColumnName("absolute_number");
b.Property<int>("EpisodeNumber")
b.Property<int?>("EpisodeNumber")
.HasColumnType("integer")
.HasColumnName("episode_number");
@ -93,7 +93,7 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnType("integer")
.HasColumnName("season_id");
b.Property<int>("SeasonNumber")
b.Property<int?>("SeasonNumber")
.HasColumnType("integer")
.HasColumnName("season_number");

View File

@ -415,9 +415,9 @@ namespace Kyoo.Postgresql.Migrations
slug = table.Column<string>(type: "text", nullable: true),
show_id = table.Column<int>(type: "integer", nullable: false),
season_id = table.Column<int>(type: "integer", nullable: true),
season_number = table.Column<int>(type: "integer", nullable: false),
episode_number = table.Column<int>(type: "integer", nullable: false),
absolute_number = table.Column<int>(type: "integer", nullable: false),
season_number = table.Column<int>(type: "integer", nullable: true),
episode_number = table.Column<int>(type: "integer", nullable: true),
absolute_number = table.Column<int>(type: "integer", nullable: true),
path = table.Column<string>(type: "text", nullable: true),
thumb = table.Column<string>(type: "text", nullable: true),
title = table.Column<string>(type: "text", nullable: true),

View File

@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Kyoo.Postgresql.Migrations
{
[DbContext(typeof(PostgresContext))]
[Migration("20210623174932_Triggers")]
[Migration("20210627141941_Triggers")]
partial class Triggers
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -69,11 +69,11 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnName("id")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int>("AbsoluteNumber")
b.Property<int?>("AbsoluteNumber")
.HasColumnType("integer")
.HasColumnName("absolute_number");
b.Property<int>("EpisodeNumber")
b.Property<int?>("EpisodeNumber")
.HasColumnType("integer")
.HasColumnName("episode_number");
@ -93,7 +93,7 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnType("integer")
.HasColumnName("season_id");
b.Property<int>("SeasonNumber")
b.Property<int?>("SeasonNumber")
.HasColumnType("integer")
.HasColumnName("season_number");

View File

@ -37,10 +37,10 @@ namespace Kyoo.Postgresql.Migrations
BEGIN
NEW.slug := CONCAT(
(SELECT slug FROM shows WHERE id = NEW.show_id),
'-s',
NEW.season_number,
'e',
NEW.episode_number
CASE
WHEN NEW.season_number IS NULL THEN CONCAT('-', NEW.absolute_number)
ELSE CONCAT('-s', NEW.season_number, 'e', NEW.episode_number)
END
);
RETURN NEW;
END
@ -48,7 +48,8 @@ namespace Kyoo.Postgresql.Migrations
// language=PostgreSQL
migrationBuilder.Sql(@"
CREATE TRIGGER episode_slug_trigger BEFORE INSERT OR UPDATE OF episode_number, season_number, show_id ON episodes
CREATE TRIGGER episode_slug_trigger
BEFORE INSERT OR UPDATE OF absolute_number, episode_number, season_number, show_id ON episodes
FOR EACH ROW EXECUTE PROCEDURE episode_slug_update();");
@ -60,7 +61,11 @@ namespace Kyoo.Postgresql.Migrations
AS $$
BEGIN
UPDATE seasons SET slug = CONCAT(NEW.slug, '-s', season_number) WHERE show_id = NEW.id;
UPDATE episodes SET slug = CONCAT(NEW.slug, '-s', season_number, 'e', episode_number) WHERE show_id = NEW.id;
UPDATE episodes SET slug = CASE
WHEN season_number IS NULL THEN CONCAT(NEW.slug, '-', absolute_number)
ELSE CONCAT(NEW.slug, '-s', season_number, 'e', episode_number)
END
WHERE show_id = NEW.id;
RETURN NEW;
END
$$;");

View File

@ -67,11 +67,11 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnName("id")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int>("AbsoluteNumber")
b.Property<int?>("AbsoluteNumber")
.HasColumnType("integer")
.HasColumnName("absolute_number");
b.Property<int>("EpisodeNumber")
b.Property<int?>("EpisodeNumber")
.HasColumnType("integer")
.HasColumnName("episode_number");
@ -91,7 +91,7 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnType("integer")
.HasColumnName("season_id");
b.Property<int>("SeasonNumber")
b.Property<int?>("SeasonNumber")
.HasColumnType("integer")
.HasColumnName("season_number");

View File

@ -27,8 +27,8 @@ namespace Kyoo.SqLite.Migrations
BEGIN
UPDATE Episodes
SET Slug = (SELECT Slug from Shows WHERE ID = ShowID) ||
CASE (SeasonNumber)
WHEN NULL THEN '-' || AbsoluteNumber
CASE
WHEN SeasonNumber IS NULL THEN '-' || AbsoluteNumber
ELSE '-s' || SeasonNumber || 'e' || EpisodeNumber
END
WHERE ID == new.ID;
@ -40,8 +40,8 @@ namespace Kyoo.SqLite.Migrations
BEGIN
UPDATE Episodes
SET Slug = (SELECT Slug from Shows WHERE ID = ShowID) ||
CASE (SeasonNumber)
WHEN NULL THEN '-' || AbsoluteNumber
CASE
WHEN SeasonNumber IS NULL THEN '-' || AbsoluteNumber
ELSE '-s' || SeasonNumber || 'e' || EpisodeNumber
END
WHERE ID == new.ID;
@ -55,8 +55,8 @@ namespace Kyoo.SqLite.Migrations
UPDATE Seasons SET Slug = new.Slug || '-s' || SeasonNumber WHERE ShowID = new.ID;
UPDATE Episodes
SET Slug = new.Slug ||
CASE (SeasonNumber)
WHEN NULL THEN '-' || AbsoluteNumber
CASE
WHEN SeasonNumber IS NULL THEN '-' || AbsoluteNumber
ELSE '-s' || SeasonNumber || 'e' || EpisodeNumber
END
WHERE ShowID = new.ID;

View File

@ -114,6 +114,9 @@ namespace Kyoo.Controllers
obj.ExternalIDs.ForEach(x => _database.Entry(x).State = EntityState.Added);
await _database.SaveChangesAsync($"Trying to insert a duplicated episode (slug {obj.Slug} already exists).");
return await ValidateTracks(obj);
// TODO check if this is needed
// obj.Slug = await _database.Entry(obj).Property(x => x.Slug).
// return obj;
}
/// <inheritdoc />
@ -148,6 +151,7 @@ namespace Kyoo.Controllers
resource.Tracks = await TaskUtils.DefaultIfNull(resource.Tracks?.MapAsync((x, i) =>
{
x.Episode = resource;
// TODO use a trigger for the next line.
x.TrackIndex = resource.Tracks.Take(i).Count(y => x.Language == y.Language
&& x.IsForced == y.IsForced
&& x.Codec == y.Codec

View File

@ -47,8 +47,8 @@ namespace Kyoo
// TODO remove postgres from here and load it like a normal plugin.
_plugins.LoadPlugins(new IPlugin[] {
new CoreModule(configuration),
// new PostgresModule(configuration, host),
new SqLiteModule(configuration, host),
new PostgresModule(configuration, host),
// new SqLiteModule(configuration, host),
new AuthenticationModule(configuration, loggerFactory, host)
});
}