Make Profile.Markings db column jsonb (#7947)

This commit is contained in:
DrSmugleaf
2022-05-05 11:23:48 +02:00
committed by GitHub
parent 0121025d12
commit 0502d3dec4
9 changed files with 2571 additions and 19 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,37 @@
#nullable disable
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations;
namespace Content.Server.Database.Migrations.Postgres
{
public partial class MarkingsJsonb : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "markings",
table: "profile");
migrationBuilder.AddColumn<JsonDocument>(
name: "markings",
table: "profile",
type: "jsonb",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "markings",
table: "profile");
migrationBuilder.AddColumn<string>(
name: "markings",
table: "profile",
type: "text",
nullable: false,
defaultValue: "");
}
}
}

View File

@@ -595,9 +595,8 @@ namespace Content.Server.Database.Migrations.Postgres
.HasColumnType("text")
.HasColumnName("hair_name");
b.Property<string>("Markings")
.IsRequired()
.HasColumnType("text")
b.Property<JsonDocument>("Markings")
.HasColumnType("jsonb")
.HasColumnName("markings");
b.Property<int>("PreferenceId")

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,36 @@
#nullable disable
using Microsoft.EntityFrameworkCore.Migrations;
namespace Content.Server.Database.Migrations.Sqlite
{
public partial class MarkingsJsonb : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "markings",
table: "profile");
migrationBuilder.AddColumn<byte[]>(
name: "markings",
table: "profile",
type: "jsonb",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "markings",
table: "profile");
migrationBuilder.AddColumn<string>(
name: "markings",
table: "profile",
type: "TEXT",
nullable: false,
defaultValue: "");
}
}
}

View File

@@ -1,5 +1,6 @@
// <auto-generated />
using System;
using System.Text.Json;
using Content.Server.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
@@ -553,9 +554,8 @@ namespace Content.Server.Database.Migrations.Sqlite
.HasColumnType("TEXT")
.HasColumnName("hair_name");
b.Property<string>("Markings")
.IsRequired()
.HasColumnType("TEXT")
b.Property<JsonDocument>("Markings")
.HasColumnType("jsonb")
.HasColumnName("markings");
b.Property<int>("PreferenceId")

View File

@@ -199,7 +199,7 @@ namespace Content.Server.Database
public string Sex { get; set; } = null!;
public string Gender { get; set; } = null!;
public string Species { get; set; } = null!;
public string Markings { get; set; } = null!;
[Column(TypeName = "jsonb")] public JsonDocument? Markings { get; set; } = null!;
public string HairName { get; set; } = null!;
public string HairColor { get; set; } = null!;
public string FacialHairName { get; set; } = null!;

View File

@@ -64,13 +64,21 @@ namespace Content.Server.Database
.HasColumnType("TEXT")
.HasConversion(ipMaskConverter);
var jsonConverter = new ValueConverter<JsonDocument, string>(
var jsonStringConverter = new ValueConverter<JsonDocument, string>(
v => JsonDocumentToString(v),
v => StringToJsonDocument(v));
var jsonByteArrayConverter = new ValueConverter<JsonDocument?, byte[]>(
v => JsonDocumentToByteArray(v),
v => ByteArrayToJsonDocument(v));
modelBuilder.Entity<AdminLog>()
.Property(log => log.Json)
.HasConversion(jsonConverter);
.HasConversion(jsonStringConverter);
modelBuilder.Entity<Profile>()
.Property(log => log.Markings)
.HasConversion(jsonByteArrayConverter);
}
private static string InetToString(IPAddress address, int mask) {
@@ -107,5 +115,26 @@ namespace Content.Server.Database
{
return JsonDocument.Parse(str);
}
private static byte[] JsonDocumentToByteArray(JsonDocument? document)
{
if (document == null)
{
return Array.Empty<byte>();
}
using var stream = new MemoryStream();
using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions {Indented = false});
document.WriteTo(writer);
writer.Flush();
return stream.ToArray();
}
private static JsonDocument ByteArrayToJsonDocument(byte[] str)
{
return JsonDocument.Parse(str);
}
}
}

View File

@@ -173,10 +173,11 @@ namespace Content.Server.Database
if (Enum.TryParse<Gender>(profile.Gender, true, out var genderVal))
gender = genderVal;
// ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
var markingsRaw = profile.Markings?.Deserialize<List<string>>();
profile.Markings?.Dispose();
List<Marking> markings = new();
if (profile.Markings != null && profile.Markings.Length != 0)
{
List<string>? markingsRaw = JsonSerializer.Deserialize<List<string>>(profile.Markings);
if (markingsRaw != null)
{
foreach (var marking in markingsRaw)
@@ -188,7 +189,6 @@ namespace Content.Server.Database
markings.Add(parsed);
}
}
}
var markingsSet = new MarkingsSet(markings);
return new HumanoidCharacterProfile(
@@ -223,7 +223,7 @@ namespace Content.Server.Database
{
markingStrings.Add(marking.ToString());
}
var markings = JsonSerializer.Serialize(markingStrings);
var markings = JsonSerializer.SerializeToDocument(markingStrings);
var entity = new Profile
{