As part of this, the ban and unban table were renamed to server_* on SQLite to move them in line with Postgres. Data is preserved.
79 lines
2.8 KiB
C#
79 lines
2.8 KiB
C#
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
|
|
});
|
|
|
|
#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
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|