Skip to content

Commit

Permalink
DB QOL stuff, but not breaking the migrations now (#5086)
Browse files Browse the repository at this point in the history
  • Loading branch information
SaphireLattice authored Oct 31, 2021
1 parent 6cb6432 commit eb567dc
Show file tree
Hide file tree
Showing 8 changed files with 482 additions and 119 deletions.
90 changes: 40 additions & 50 deletions Content.Server.Database/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,57 +80,53 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
}

[Table("preference")]
public class Preference
{
// NOTE: on postgres there SHOULD be an FK ensuring that the selected character slot always exists.
// I had to use a migration to implement it and as a result its creation is a finicky mess.
// Because if I let EFCore know about it it would explode on a circular reference.
// Also it has to be DEFERRABLE INITIALLY DEFERRED so that insertion of new preferences works.
// Also I couldn't figure out how to create it on SQLite.

[Column("preference_id")] public int Id { get; set; }
[Column("user_id")] public Guid UserId { get; set; }
[Column("selected_character_slot")] public int SelectedCharacterSlot { get; set; }
[Column("admin_ooc_color")] public string AdminOOCColor { get; set; } = null!;
public int Id { get; set; }
public Guid UserId { get; set; }
public int SelectedCharacterSlot { get; set; }
public string AdminOOCColor { get; set; } = null!;
public List<Profile> Profiles { get; } = new();
}

[Table("profile")]
public class Profile
{
[Column("profile_id")] public int Id { get; set; }
[Column("slot")] public int Slot { get; set; }
public int Id { get; set; }
public int Slot { get; set; }
[Column("char_name")] public string CharacterName { get; set; } = null!;
[Column("age")] public int Age { get; set; }
[Column("sex")] public string Sex { get; set; } = null!;
[Column("gender")] public string Gender { get; set; } = null!;
[Column("hair_name")] public string HairName { get; set; } = null!;
[Column("hair_color")] public string HairColor { get; set; } = null!;
[Column("facial_hair_name")] public string FacialHairName { get; set; } = null!;
[Column("facial_hair_color")] public string FacialHairColor { get; set; } = null!;
[Column("eye_color")] public string EyeColor { get; set; } = null!;
[Column("skin_color")] public string SkinColor { get; set; } = null!;
[Column("clothing")] public string Clothing { get; set; } = null!;
[Column("backpack")] public string Backpack { get; set; } = null!;
public int Age { get; set; }
public string Sex { get; set; } = null!;
public string Gender { get; set; } = null!;
public string HairName { get; set; } = null!;
public string HairColor { get; set; } = null!;
public string FacialHairName { get; set; } = null!;
public string FacialHairColor { get; set; } = null!;
public string EyeColor { get; set; } = null!;
public string SkinColor { get; set; } = null!;
public string Clothing { get; set; } = null!;
public string Backpack { get; set; } = null!;
public List<Job> Jobs { get; } = new();
public List<Antag> Antags { get; } = new();

[Column("pref_unavailable")] public DbPreferenceUnavailableMode PreferenceUnavailable { get; set; }

[Column("preference_id")] public int PreferenceId { get; set; }
public int PreferenceId { get; set; }
public Preference Preference { get; set; } = null!;
}

[Table("job")]
public class Job
{
[Column("job_id")] public int Id { get; set; }
public int Id { get; set; }
public Profile Profile { get; set; } = null!;
[Column("profile_id")] public int ProfileId { get; set; }
public int ProfileId { get; set; }

[Column("job_name")] public string JobName { get; set; } = null!;
[Column("priority")] public DbJobPriority Priority { get; set; }
public string JobName { get; set; } = null!;
public DbJobPriority Priority { get; set; }
}

public enum DbJobPriority
Expand All @@ -142,14 +138,13 @@ public enum DbJobPriority
High = 3
}

[Table("antag")]
public class Antag
{
[Column("antag_id")] public int Id { get; set; }
public int Id { get; set; }
public Profile Profile { get; set; } = null!;
[Column("profile_id")] public int ProfileId { get; set; }
public int ProfileId { get; set; }

[Column("antag_name")] public string AntagName { get; set; } = null!;
public string AntagName { get; set; } = null!;
}

public enum DbPreferenceUnavailableMode
Expand All @@ -159,54 +154,49 @@ public enum DbPreferenceUnavailableMode
SpawnAsOverflow,
}

[Table("assigned_user_id")]
public class AssignedUserId
{
[Column("assigned_user_id_id")] public int Id { get; set; }
[Column("user_name")] public string UserName { get; set; } = null!;
public int Id { get; set; }
public string UserName { get; set; } = null!;

[Column("user_id")] public Guid UserId { get; set; }
public Guid UserId { get; set; }
}

[Table("admin")]
public class Admin
{
[Column("user_id"), Key] public Guid UserId { get; set; }
[Column("title")] public string? Title { get; set; }
[Key] public Guid UserId { get; set; }
public string? Title { get; set; }

[Column("admin_rank_id")] public int? AdminRankId { get; set; }
public int? AdminRankId { get; set; }
public AdminRank? AdminRank { get; set; }
public List<AdminFlag> Flags { get; set; } = default!;
}

[Table("admin_flag")]
public class AdminFlag
{
[Column("admin_flag_id")] public int Id { get; set; }
[Column("flag")] public string Flag { get; set; } = default!;
[Column("negative")] public bool Negative { get; set; }
public int Id { get; set; }
public string Flag { get; set; } = default!;
public bool Negative { get; set; }

[Column("admin_id")] public Guid AdminId { get; set; }
public Guid AdminId { get; set; }
public Admin Admin { get; set; } = default!;
}

[Table("admin_rank")]
public class AdminRank
{
[Column("admin_rank_id")] public int Id { get; set; }
[Column("name")] public string Name { get; set; } = default!;
public int Id { get; set; }
public string Name { get; set; } = default!;

public List<Admin> Admins { get; set; } = default!;
public List<AdminRankFlag> Flags { get; set; } = default!;
}

[Table("admin_rank_flag")]
public class AdminRankFlag
{
[Column("admin_rank_flag_id")] public int Id { get; set; }
[Column("flag")] public string Flag { get; set; } = default!;
public int Id { get; set; }
public string Flag { get; set; } = default!;

[Column("admin_rank_id")] public int AdminRankId { get; set; }
public int AdminRankId { get; set; }
public AdminRank Rank { get; set; } = default!;
}
}
57 changes: 31 additions & 26 deletions Content.Server.Database/ModelPostgres.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;

namespace Content.Server.Database
Expand All @@ -25,6 +26,8 @@ protected override void OnConfiguring(DbContextOptionsBuilder options)
options.UseNpgsql("dummy connection string");

options.ReplaceService<IRelationalTypeMappingSource, CustomNpgsqlTypeMappingSource>();

((IDbContextOptionsBuilderInfrastructure) options).AddOrUpdateExtension(new SnakeCaseExtension());
}

public PostgresServerDbContext(DbContextOptions<ServerDbContext> options) : base(options)
Expand Down Expand Up @@ -74,26 +77,32 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<PostgresConnectionLog>()
.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");
}
}
}
}

[Table("server_ban")]
public class PostgresServerBan
{
[Column("server_ban_id")] public int Id { get; set; }

[Column("user_id")] public Guid? UserId { get; set; }
[Column("address", TypeName = "inet")] public (IPAddress, int)? Address { get; set; }
[Column("hwid")] public byte[]? HWId { get; set; }
public int Id { get; set; }
public Guid? UserId { get; set; }
[Column(TypeName = "inet")] public (IPAddress, int)? Address { get; set; }
public byte[]? HWId { get; set; }

[Column("ban_time", TypeName = "timestamp with time zone")]
public DateTime BanTime { get; set; }

[Column("expiration_time", TypeName = "timestamp with time zone")]
public DateTime? ExpirationTime { get; set; }

[Column("reason")] public string Reason { get; set; } = null!;
[Column("banning_admin")] public Guid? BanningAdmin { get; set; }
public string Reason { get; set; } = null!;
public Guid? BanningAdmin { get; set; }

public PostgresServerUnban? Unban { get; set; }
}
Expand All @@ -103,48 +112,44 @@ public class PostgresServerUnban
{
[Column("unban_id")] public int Id { get; set; }

[Column("ban_id")] public int BanId { get; set; }
[Column("ban")] public PostgresServerBan Ban { get; set; } = null!;
public int BanId { get; set; }
public PostgresServerBan Ban { get; set; } = null!;

[Column("unbanning_admin")] public Guid? UnbanningAdmin { get; set; }
public Guid? UnbanningAdmin { get; set; }

[Column("unban_time", TypeName = "timestamp with time zone")]
public DateTime UnbanTime { get; set; }
}

[Table("player")]
public class PostgresPlayer
{
[Column("player_id")] public int Id { get; set; }
public int Id { get; set; }

// Permanent data
[Column("user_id")] public Guid UserId { get; set; }
public Guid UserId { get; set; }

[Column("first_seen_time", TypeName = "timestamp with time zone")]
public DateTime FirstSeenTime { get; set; }

// Data that gets updated on each join.
[Column("last_seen_user_name")] public string LastSeenUserName { get; set; } = null!;
public string LastSeenUserName { get; set; } = null!;

[Column("last_seen_time", TypeName = "timestamp with time zone")]
public DateTime LastSeenTime { get; set; }

[Column("last_seen_address")] public IPAddress LastSeenAddress { get; set; } = null!;
[Column("last_seen_hwid")] public byte[]? LastSeenHWId { get; set; }
public IPAddress LastSeenAddress { get; set; } = null!;
public byte[]? LastSeenHWId { get; set; }
}

[Table("connection_log")]
public class PostgresConnectionLog
{
[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!;
public Guid UserId { get; set; }
public string UserName { get; set; } = null!;

[Column("time", TypeName = "timestamp with time zone")]
public DateTime Time { get; set; }

[Column("address")] public IPAddress Address { get; set; } = null!;
[Column("hwid")] public byte[]? HWId { get; set; }
public IPAddress Address { get; set; } = null!;
public byte[]? HWId { get; set; }
}
}
Loading

0 comments on commit eb567dc

Please sign in to comment.