ποΈ Databases SS14 (Databases)
This skill describes the database architecture in Space Station 14, including dual engine support (PostgreSQL and SQLite), DbContext inheritance, and best practices for data models.
π― When to use this skill
- When creating new tables or database entities.
- When changing existing data models.
- When writing requests to obtain or save player data, bans or logs.
- When solving problems related to data types (IP addresses, JSON, etc.).
ποΈ Architecture Review
SS14 uses Entity Framework Core with a dual engine architecture to support both PostgreSQL (production) and SQLite (dev/test).
Key Components
IServerDbManager: The main interface for database operations (Content.Server/Database/ServerDbManager.cs).ServerDbBase: Abstract base class containing common logic for both engines.ServerDbSqliteandServerDbPostgres: Specific implementations for specific engines.ServerDbContext: An abstract EF Core context that definesDbSet's.SqliteServerDbContextandPostgresServerDbContext: Specific contexts that manage configuration (for example, Value Converters).
Database Logic vs Models
- Logic: Located in
Content.Server/Database. ContainsServerDbManager,ServerDbBase, etc. - Models: Found in
Content.Server.Database. ContainsModel.cs(tables), migrations, and context definitions.
π Working with Data Models
Defining Entities
Entities are defined in Content.Server.Database/Model.cs (or in separate files in that namespace).
public class MyNewEntity
{
[Key] // Primary key
public int Id { get; set; }
[Required] // This field is required
public string Name { get; set; } = default!;
// Foreign key or just user ID
public Guid PlayerUserId { get; set; }
}
Adding to Context
Add DbSet to the abstract ServerDbContext to Content.Server.Database/Model.cs:
public abstract class ServerDbContext : DbContext
{
// ... existing sets
public DbSet<MyNewEntity> MyNewEntities { get; set; } = default!;
}
β οΈ Engine Limitations and Data Types
Since SQLite and PostgreSQL support different functions, you need to be careful with types.
1. IP addresses π
- Postgres: Has native type
inet(NpgsqlInet). - SQLite: Doesn't support IP. Must be stored as
TEXT. - Solution: Use
ValueConverterinSqliteServerDbContext.OnModelCreating.
2. JSON Data π
- Postgres: Has the native type
jsonb. - SQLite: Must be stored as
TEXTorBLOB. - Solution: Use separate calls to
db.PgDbContext.Add()anddb.SqliteDbContext.Add()if complex types are used, or ValueConverters for shared logic.
3. Arrays/Lists π
- Postgres: Supports primitive arrays (for example,
List<string>maps totext[]). - SQLite: Does not support arrays.
- Solution:
- Preferred: Use a separate table with a 1:N relationship.
- Alternative: Serialize the string to JSON (if you don't need to search inside the list).
π‘ Usage Examples
1. Data Saving (Difference Handling)
When saving data that uses specific types (like IP), you may need to override the method in ServerDbSqlite and ServerDbPostgres.
Abstract Base (ServerDbBase.cs):
public abstract Task AddServerBanAsync(ServerBanDef serverBan);
Postgres implementation (ServerDbPostgres.cs):
public override async Task AddServerBanAsync(ServerBanDef serverBan)
{
await using var db = await GetDbImpl();
// Postgres supports NpgsqlInet directly, no conversion needed
db.PgDbContext.Ban.Add(new ServerBan
{
Address = serverBan.Address.ToNpgsqlInet(),
// ... other fields
});
await db.PgDbContext.SaveChangesAsync();
}
SQLite implementation (ServerDbSqlite.cs):
public override async Task AddServerBanAsync(ServerBanDef serverBan)
{
await using var db = await GetDbImpl();
// SQLite requires a conversion, which is handled by the ValueConverter in context
db.SqliteDbContext.Ban.Add(new ServerBan
{
Address = serverBan.Address.ToNpgsqlInet(),
// ... other fields
});
await db.SqliteDbContext.SaveChangesAsync();
}
2. Data Loading (General Logic)
Reading data is often identical for both engines and can be implemented in ServerDbBase.
// Content.Server/Database/ServerDbBase.cs
public async Task<PlayerPreferences?> GetPlayerPreferencesAsync(NetUserId userId, CancellationToken cancel)
{
await using var db = await GetDb(cancel);
// Using .Include() to greedily load related data
var prefs = await db.DbContext
.Preference
.Include(p => p.Profiles)
.ThenInclude(h => h.Jobs) // Loading nested links (Profiles -> Jobs)
.AsSplitQuery() // Optimization: Avoids "explosion" of the Cartesian product with multiple include
.SingleOrDefaultAsync(p => p.UserId == userId.UserId, cancel);
if (prefs is null) return null;
// Converting the database model into a Shared model (game logic)
return new PlayerPreferences(...);
}
3. Queries with Search π
For text search, use EF.Functions to support engine-specific features where possible, or a fallback to standard LINQ.
// ServerDbContext.cs (Abstract Method)
public virtual IQueryable<AdminLog> SearchLogs(IQueryable<AdminLog> query, string searchText)
{
// Default/SQLite: Simple LIKE
return query.Where(log => EF.Functions.Like(log.Message, "%" + searchText + "%"));
}
// PostgresServerDbContext.cs (Override)
public override IQueryable<AdminLog> SearchLogs(IQueryable<AdminLog> query, string searchText)
{
// Postgres: Full text search using ToTsVector
return query.Where(log => EF.Functions.ToTsVector("english", log.Message).Matches(searchText));
}
π« Antipatterns (What NOT to do)
β 1. Using Postgres-specific types in general logic
Bad: Trying to store NpgsqlInet or an array of strings directly into ServerDbBase without checking the engine.
Why: This will break SQLite since it doesn't know these types.
How ββto: Use abstract methods or ValueConverters.
β 2. Synchronous database calls (`.Result`, `.Wait()`)
Bad: db.DbContext.SaveChangeAsync().Result;
Why: This blocks the main server thread, causing lags (freezes) for players.
Do it: Always use await and asynchronous methods (ToListAsync, FirstOrDefaultAsync).
β 3. Loading the entire table into memory
Bad: var allUsers = db.Player.ToList(); then filtering allUsers.Where(...).
Why: A table can contain tens of thousands of records. This will kill memory and CPU.
How ββto: Filter before request materialization: db.Player.Where(...).ToListAsync();.
β 4. Lack of indexes for frequent queries
Bad: Search for player by LastSeenUserName without index on this field.
Why: Full Table Scan on every player input will create load.
Do it: Add indexing to OnModelCreating.
β Best Practices
- Always support both engines: Never write code that only works on Postgres. SQLite is required for local development.
- Use
CCVars: The database configuration (host, port, engine) is managed throughCCVars.Database*. - Validation: Use the
[Required],[MaxLength]and[Key]attributes in models. - Metrics: Use
DbReadOpsMetric.Inc()andDbWriteOpsMetric.Inc()inServerDbManagerto track activity.