Add value comparers for json columns and add user externalids on db

This commit is contained in:
Zoe Roux 2024-02-29 21:23:15 +01:00
parent 7df1a295f3
commit 35a69edfa2
4 changed files with 1397 additions and 13 deletions

View File

@ -167,6 +167,14 @@ namespace Kyoo.Postgresql
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
}
private static ValueComparer<Dictionary<string, T>> _GetComparer<T>()
{
return new(
(c1, c2) => c1!.SequenceEqual(c2!),
c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode()))
);
}
/// <summary>
/// Build the metadata model for the given type.
/// </summary>
@ -193,6 +201,10 @@ namespace Kyoo.Postgresql
)!
)
.HasColumnType("json");
modelBuilder
.Entity<T>()
.Property(x => x.ExternalId)
.Metadata.SetValueComparer(_GetComparer<MetadataId>());
}
private static void _HasImages<T>(ModelBuilder modelBuilder)
@ -318,19 +330,6 @@ namespace Kyoo.Postgresql
_HasAddedDate<User>(modelBuilder);
_HasAddedDate<Issue>(modelBuilder);
modelBuilder
.Entity<User>()
.Property(x => x.Settings)
.HasConversion(
v => JsonSerializer.Serialize(v, (JsonSerializerOptions?)null),
v =>
JsonSerializer.Deserialize<Dictionary<string, string>>(
v,
(JsonSerializerOptions?)null
)!
)
.HasColumnType("json");
modelBuilder
.Entity<MovieWatchStatus>()
.HasKey(x => new { User = x.UserId, Movie = x.MovieId });
@ -417,6 +416,40 @@ namespace Kyoo.Postgresql
// {
// x.ToJson();
// });
modelBuilder
.Entity<User>()
.Property(x => x.Settings)
.HasConversion(
v => JsonSerializer.Serialize(v, (JsonSerializerOptions?)null),
v =>
JsonSerializer.Deserialize<Dictionary<string, string>>(
v,
(JsonSerializerOptions?)null
)!
)
.HasColumnType("json");
modelBuilder
.Entity<User>()
.Property(x => x.Settings)
.Metadata.SetValueComparer(_GetComparer<string>());
modelBuilder
.Entity<User>()
.Property(x => x.ExternalId)
.HasConversion(
v => JsonSerializer.Serialize(v, (JsonSerializerOptions?)null),
v =>
JsonSerializer.Deserialize<Dictionary<string, ExternalToken>>(
v,
(JsonSerializerOptions?)null
)!
)
.HasColumnType("json");
modelBuilder
.Entity<User>()
.Property(x => x.ExternalId)
.Metadata.SetValueComparer(_GetComparer<ExternalToken>());
modelBuilder
.Entity<Issue>()
.Property(x => x.Extra)
@ -429,6 +462,10 @@ namespace Kyoo.Postgresql
)!
)
.HasColumnType("json");
modelBuilder
.Entity<Issue>()
.Property(x => x.Extra)
.Metadata.SetValueComparer(_GetComparer<object>());
}
/// <summary>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Kyoo.Postgresql.Migrations
{
/// <inheritdoc />
public partial class AddUserExternalId : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "external_id",
table: "users",
type: "json",
nullable: false,
defaultValue: "{}"
);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(name: "external_id", table: "users");
}
}
}

View File

@ -614,6 +614,11 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnType("text")
.HasColumnName("email");
b.Property<string>("ExternalId")
.IsRequired()
.HasColumnType("json")
.HasColumnName("external_id");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("text")