Files
OldThink/Content.Server.Database/ModelPostgres.cs

79 lines
2.8 KiB
C#
Raw Normal View History

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
namespace Content.Server.Database
{
public sealed class PostgresServerDbContext : ServerDbContext
{
// This is used by the "dotnet ef" CLI tool.
public PostgresServerDbContext()
{
}
static PostgresServerDbContext()
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (!InitializedWithOptions)
options.UseNpgsql("dummy connection string");
options.ReplaceService<IRelationalTypeMappingSource, CustomNpgsqlTypeMappingSource>();
((IDbContextOptionsBuilderInfrastructure) options).AddOrUpdateExtension(new SnakeCaseExtension());
options.ConfigureWarnings(x =>
{
x.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning);
#if DEBUG
// for tests
x.Ignore(CoreEventId.SensitiveDataLoggingEnabledWarning);
#endif
});
2021-12-20 01:23:11 +01:00
#if DEBUG
options.EnableSensitiveDataLogging();
#endif
}
public PostgresServerDbContext(DbContextOptions<ServerDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// ReSharper disable once CommentTypo
// ReSharper disable once StringLiteralTypo
// Enforce that an address cannot be IPv6-mapped IPv4.
// So that IPv4 addresses are consistent between separate-socket and dual-stack socket modes.
modelBuilder.Entity<ServerBan>()
.HasCheckConstraint("AddressNotIPv6MappedIPv4", "NOT inet '::ffff:0.0.0.0/96' >>= address");
// ReSharper disable once StringLiteralTypo
2021-11-11 17:54:02 +01:00
modelBuilder.Entity<Player>()
.HasCheckConstraint("LastSeenAddressNotIPv6MappedIPv4",
"NOT inet '::ffff:0.0.0.0/96' >>= last_seen_address");
modelBuilder.Entity<ConnectionLog>()
.HasCheckConstraint("AddressNotIPv6MappedIPv4",
"NOT inet '::ffff:0.0.0.0/96' >>= address");
foreach(var entity in modelBuilder.Model.GetEntityTypes())
{
foreach(var property in entity.GetProperties())
{
if (property.FieldInfo?.FieldType == typeof(DateTime) || property.FieldInfo?.FieldType == typeof(DateTime?))
property.SetColumnType("timestamp with time zone");
}
}
}
}
}