A bit of DB model cleanup (#5016)

This commit is contained in:
Saphire Lattice
2021-10-29 20:41:13 +07:00
committed by GitHub
parent c43006cb10
commit 8a3cee9a10
8 changed files with 467 additions and 119 deletions

View File

@@ -1,6 +1,10 @@
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Globalization;
using System.Net;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Content.Server.Database
{
@@ -19,6 +23,8 @@ namespace Content.Server.Database
{
if (!InitializedWithOptions)
options.UseSqlite("dummy connection string");
((IDbContextOptionsBuilderInfrastructure) options).AddOrUpdateExtension(new SnakeCaseExtension());
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
@@ -27,26 +33,56 @@ namespace Content.Server.Database
modelBuilder.Entity<SqlitePlayer>()
.HasIndex(p => p.LastSeenUserName);
var converter = new ValueConverter<(IPAddress address, int mask), string>(
v => InetToString(v.address, v.mask),
v => StringToInet(v)
);
modelBuilder
.Entity<SqliteServerBan>()
.Property(e => e.Address)
.HasColumnType("TEXT")
.HasConversion(converter);
}
public SqliteServerDbContext(DbContextOptions<ServerDbContext> options) : base(options)
{
}
private static string InetToString(IPAddress address, int mask) {
if (address.IsIPv4MappedToIPv6)
{
// Fix IPv6-mapped IPv4 addresses
// So that IPv4 addresses are consistent between separate-socket and dual-stack socket modes.
address = address.MapToIPv4();
mask -= 96;
}
return $"{address}/{mask}";
}
private static (IPAddress, int) StringToInet(string inet) {
var idx = inet.IndexOf('/', StringComparison.Ordinal);
return (
IPAddress.Parse(inet.AsSpan(0, idx)),
int.Parse(inet.AsSpan(idx + 1), provider: CultureInfo.InvariantCulture)
);
}
}
[Table("ban")]
public class SqliteServerBan
{
[Column("ban_id")] public int Id { get; set; }
public int Id { get; set; }
[Column("user_id")] public Guid? UserId { get; set; }
[Column("address")] public string? Address { get; set; }
[Column("hwid")] public byte[]? HWId { get; set; }
public Guid? UserId { get; set; }
public (IPAddress address, int mask)? Address { get; set; }
public byte[]? HWId { get; set; }
[Column("ban_time")] public DateTime BanTime { get; set; }
[Column("expiration_time")] public DateTime? ExpirationTime { get; set; }
[Column("reason")] public string Reason { get; set; } = null!;
[Column("banning_admin")] public Guid? BanningAdmin { get; set; }
public DateTime BanTime { get; set; }
public DateTime? ExpirationTime { get; set; }
public string Reason { get; set; } = null!;
public Guid? BanningAdmin { get; set; }
public SqliteServerUnban? Unban { get; set; }
}
@@ -56,38 +92,38 @@ namespace Content.Server.Database
{
[Column("unban_id")] public int Id { get; set; }
[Column("ban_id")] public int BanId { get; set; }
public int BanId { get; set; }
public SqliteServerBan Ban { get; set; } = null!;
[Column("unbanning_admin")] public Guid? UnbanningAdmin { get; set; }
[Column("unban_time")] public DateTime UnbanTime { get; set; }
public Guid? UnbanningAdmin { get; set; }
public DateTime UnbanTime { get; set; }
}
[Table("player")]
public class SqlitePlayer
{
[Column("player_id")] public int Id { get; set; }
public int Id { get; set; }
// Permanent data
[Column("user_id")] public Guid UserId { get; set; }
[Column("first_seen_time")] public DateTime FirstSeenTime { get; set; }
public Guid UserId { get; set; }
public DateTime FirstSeenTime { get; set; }
// Data that gets updated on each join.
[Column("last_seen_user_name")] public string LastSeenUserName { get; set; } = null!;
[Column("last_seen_time")] public DateTime LastSeenTime { get; set; }
[Column("last_seen_address")] public string LastSeenAddress { get; set; } = null!;
[Column("last_seen_hwid")] public byte[]? LastSeenHWId { get; set; }
public string LastSeenUserName { get; set; } = null!;
public DateTime LastSeenTime { get; set; }
public string LastSeenAddress { get; set; } = null!;
public byte[]? LastSeenHWId { get; set; }
}
[Table("connection_log")]
public class SqliteConnectionLog
{
[Column("connection_log_id")] public int Id { get; set; }
public int Id { get; set; }
[Column("user_id")] public Guid UserId { get; set; }
[Column("user_name")] public string UserName { get; set; } = null!;
[Column("time")] public DateTime Time { get; set; }
[Column("address")] public string Address { get; set; } = null!;
[Column("hwid")] public byte[]? HWId { get; set; }
public Guid UserId { get; set; }
public string UserName { get; set; } = null!;
public DateTime Time { get; set; }
public string Address { get; set; } = null!;
public byte[]? HWId { get; set; }
}
}