Make Profile.Markings db column jsonb (#7947)
This commit is contained in:
1256
Content.Server.Database/Migrations/Postgres/20220505084828_MarkingsJsonb.Designer.cs
generated
Normal file
1256
Content.Server.Database/Migrations/Postgres/20220505084828_MarkingsJsonb.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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: "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -595,9 +595,8 @@ namespace Content.Server.Database.Migrations.Postgres
|
|||||||
.HasColumnType("text")
|
.HasColumnType("text")
|
||||||
.HasColumnName("hair_name");
|
.HasColumnName("hair_name");
|
||||||
|
|
||||||
b.Property<string>("Markings")
|
b.Property<JsonDocument>("Markings")
|
||||||
.IsRequired()
|
.HasColumnType("jsonb")
|
||||||
.HasColumnType("text")
|
|
||||||
.HasColumnName("markings");
|
.HasColumnName("markings");
|
||||||
|
|
||||||
b.Property<int>("PreferenceId")
|
b.Property<int>("PreferenceId")
|
||||||
|
|||||||
1195
Content.Server.Database/Migrations/Sqlite/20220505084819_MarkingsJsonb.Designer.cs
generated
Normal file
1195
Content.Server.Database/Migrations/Sqlite/20220505084819_MarkingsJsonb.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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: "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
// <auto-generated />
|
// <auto-generated />
|
||||||
using System;
|
using System;
|
||||||
|
using System.Text.Json;
|
||||||
using Content.Server.Database;
|
using Content.Server.Database;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
@@ -553,9 +554,8 @@ namespace Content.Server.Database.Migrations.Sqlite
|
|||||||
.HasColumnType("TEXT")
|
.HasColumnType("TEXT")
|
||||||
.HasColumnName("hair_name");
|
.HasColumnName("hair_name");
|
||||||
|
|
||||||
b.Property<string>("Markings")
|
b.Property<JsonDocument>("Markings")
|
||||||
.IsRequired()
|
.HasColumnType("jsonb")
|
||||||
.HasColumnType("TEXT")
|
|
||||||
.HasColumnName("markings");
|
.HasColumnName("markings");
|
||||||
|
|
||||||
b.Property<int>("PreferenceId")
|
b.Property<int>("PreferenceId")
|
||||||
|
|||||||
@@ -199,7 +199,7 @@ namespace Content.Server.Database
|
|||||||
public string Sex { get; set; } = null!;
|
public string Sex { get; set; } = null!;
|
||||||
public string Gender { get; set; } = null!;
|
public string Gender { get; set; } = null!;
|
||||||
public string Species { 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 HairName { get; set; } = null!;
|
||||||
public string HairColor { get; set; } = null!;
|
public string HairColor { get; set; } = null!;
|
||||||
public string FacialHairName { get; set; } = null!;
|
public string FacialHairName { get; set; } = null!;
|
||||||
|
|||||||
@@ -64,13 +64,21 @@ namespace Content.Server.Database
|
|||||||
.HasColumnType("TEXT")
|
.HasColumnType("TEXT")
|
||||||
.HasConversion(ipMaskConverter);
|
.HasConversion(ipMaskConverter);
|
||||||
|
|
||||||
var jsonConverter = new ValueConverter<JsonDocument, string>(
|
var jsonStringConverter = new ValueConverter<JsonDocument, string>(
|
||||||
v => JsonDocumentToString(v),
|
v => JsonDocumentToString(v),
|
||||||
v => StringToJsonDocument(v));
|
v => StringToJsonDocument(v));
|
||||||
|
|
||||||
|
var jsonByteArrayConverter = new ValueConverter<JsonDocument?, byte[]>(
|
||||||
|
v => JsonDocumentToByteArray(v),
|
||||||
|
v => ByteArrayToJsonDocument(v));
|
||||||
|
|
||||||
modelBuilder.Entity<AdminLog>()
|
modelBuilder.Entity<AdminLog>()
|
||||||
.Property(log => log.Json)
|
.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) {
|
private static string InetToString(IPAddress address, int mask) {
|
||||||
@@ -107,5 +115,26 @@ namespace Content.Server.Database
|
|||||||
{
|
{
|
||||||
return JsonDocument.Parse(str);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,20 +173,20 @@ namespace Content.Server.Database
|
|||||||
if (Enum.TryParse<Gender>(profile.Gender, true, out var genderVal))
|
if (Enum.TryParse<Gender>(profile.Gender, true, out var genderVal))
|
||||||
gender = genderVal;
|
gender = genderVal;
|
||||||
|
|
||||||
|
// ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
|
||||||
|
var markingsRaw = profile.Markings?.Deserialize<List<string>>();
|
||||||
|
profile.Markings?.Dispose();
|
||||||
|
|
||||||
List<Marking> markings = new();
|
List<Marking> markings = new();
|
||||||
if (profile.Markings != null && profile.Markings.Length != 0)
|
if (markingsRaw != null)
|
||||||
{
|
{
|
||||||
List<string>? markingsRaw = JsonSerializer.Deserialize<List<string>>(profile.Markings);
|
foreach (var marking in markingsRaw)
|
||||||
if (markingsRaw != null)
|
|
||||||
{
|
{
|
||||||
foreach (var marking in markingsRaw)
|
var parsed = Marking.ParseFromDbString(marking);
|
||||||
{
|
|
||||||
var parsed = Marking.ParseFromDbString(marking);
|
|
||||||
|
|
||||||
if (parsed is null) continue;
|
if (parsed is null) continue;
|
||||||
|
|
||||||
markings.Add(parsed);
|
markings.Add(parsed);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var markingsSet = new MarkingsSet(markings);
|
var markingsSet = new MarkingsSet(markings);
|
||||||
@@ -223,7 +223,7 @@ namespace Content.Server.Database
|
|||||||
{
|
{
|
||||||
markingStrings.Add(marking.ToString());
|
markingStrings.Add(marking.ToString());
|
||||||
}
|
}
|
||||||
var markings = JsonSerializer.Serialize(markingStrings);
|
var markings = JsonSerializer.SerializeToDocument(markingStrings);
|
||||||
|
|
||||||
var entity = new Profile
|
var entity = new Profile
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user