Public Access
Add data model, DbContext and initial migration (M1)
Schema for identity, vaults, grants, hosts and the sync change log, verified against a real PostgreSQL 18 container rather than an in-memory provider: partial unique indexes, CHECK constraints, citext and identity-always columns are all provider behaviour that an in-memory fake would not exercise. Invariants pushed into the database, so they hold even when application code has a bug: - ck_host_relay_target is a security boundary, not tidiness. A host may carry a plaintext hostname and port ONLY when relay is deliberately enabled. Both directions are tested; the important one is that relay-disabled hosts cannot carry an address, since otherwise a bug would silently give the server infrastructure visibility it was never granted. - ck_vault_owner: exactly one of owner_user_id or team_id, or permission resolution would have no defined answer. - ck_vault_key_grant_recipient: member grants name a user; recovery and escrow grants are wrapped to a key and must not. - ck_user_key_wrap_kdf: a password-derived wrap without its parameters is permanently unopenable, so a partial write is rejected outright. Present from the first migration on purpose: - GrantKind (Member/Recovery/Escrow). Recovery cannot be bolted on later — every vault created before it existed would be unrecoverable by design. - team and team_membership, though team features are M3. Adding them later would mean introducing a foreign key on a live vault table. - Host.ContentKeyId, reserved for per-item content keys wrapped to individual users. - user_key as its own table, so key rotation does not require altering the user row. Two things verified rather than assumed: - Npgsql's UseXminAsConcurrencyToken helper no longer exists in EF 10, so xmin is mapped directly in XminConcurrency. The generated migration *looks* like it creates an xmin column; it does not. Confirmed by inspecting pg_attribute (attnum -2, a system column) and by grepping the emitted DDL. A test pins both, because had it created a real column PostgreSQL would have rejected the name. - EF Core is now pinned centrally. The Npgsql provider asks for 10.0.4 while EntityFrameworkCore.Design pulls 10.0.10, and because Design is PrivateAssets=all that higher version does not flow to referencing projects — producing a CS1705 in any test project referencing Infrastructure. Also commits artifacts/schema/v0.1.sql, the idempotent script, as the baseline for future upgrade tests. Verified: 0 warnings, 122 tests pass (27 new against Postgres), format clean.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
using DodoSSH.Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace DodoSSH.Infrastructure.Configurations;
|
||||
|
||||
/// <summary>Maps <see cref="Host"/>.</summary>
|
||||
public sealed class HostConfiguration : IEntityTypeConfiguration<Host>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Configure(EntityTypeBuilder<Host> builder)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(builder);
|
||||
|
||||
builder.ToTable("host");
|
||||
builder.HasKey(h => h.Id);
|
||||
|
||||
// Client-generated UUIDv7: items must be creatable offline, with their ids.
|
||||
builder.Property(h => h.Id).ValueGeneratedNever();
|
||||
builder.UseXminConcurrencyToken();
|
||||
|
||||
builder.Property(h => h.Payload).IsRequired();
|
||||
builder.Property(h => h.Hostname).HasMaxLength(255);
|
||||
|
||||
builder.HasIndex(h => new { h.VaultId, h.ChangeSequence });
|
||||
|
||||
builder.HasIndex(h => h.VaultId)
|
||||
.HasFilter("deleted_at_utc IS NULL")
|
||||
.HasDatabaseName("ix_host_vault_live");
|
||||
|
||||
// The relay resolves its target from this row, so an address is stored only when relay is
|
||||
// deliberately enabled for the host. Enforced in the database rather than in application
|
||||
// code: a bug that let a host carry a plaintext address without opting in would silently
|
||||
// widen what the server can see. See ADR 0004.
|
||||
builder.ToTable(t => t.HasCheckConstraint(
|
||||
"ck_host_relay_target",
|
||||
"""
|
||||
(relay_enabled AND hostname IS NOT NULL AND port IS NOT NULL)
|
||||
OR (NOT relay_enabled AND hostname IS NULL AND port IS NULL)
|
||||
"""));
|
||||
|
||||
builder.ToTable(t => t.HasCheckConstraint(
|
||||
"ck_host_port_range",
|
||||
"port IS NULL OR (port BETWEEN 1 AND 65535)"));
|
||||
|
||||
builder.ToTable(t => t.HasCheckConstraint(
|
||||
"ck_host_version",
|
||||
"version >= 1"));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Maps <see cref="SyncChange"/>.</summary>
|
||||
public sealed class SyncChangeConfiguration : IEntityTypeConfiguration<SyncChange>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Configure(EntityTypeBuilder<SyncChange> builder)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(builder);
|
||||
|
||||
builder.ToTable("sync_change");
|
||||
builder.HasKey(c => c.Sequence);
|
||||
|
||||
// Identity ALWAYS rather than BY DEFAULT: nothing may supply its own sequence value, or
|
||||
// cursor ordering stops meaning anything.
|
||||
builder.Property(c => c.Sequence).UseIdentityAlwaysColumn();
|
||||
|
||||
builder.Property(c => c.EntityType).HasConversion<int>();
|
||||
builder.Property(c => c.Operation).HasConversion<int>();
|
||||
|
||||
// The delta-pull access path: everything after a cursor, for one vault.
|
||||
builder.HasIndex(c => new { c.VaultId, c.Sequence });
|
||||
|
||||
// Latest change for a given entity, used when resolving a conflict.
|
||||
builder.HasIndex(c => new { c.VaultId, c.EntityId, c.Sequence })
|
||||
.IsDescending(false, false, true);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Maps <see cref="SyncOperationReceipt"/>.</summary>
|
||||
public sealed class SyncOperationReceiptConfiguration : IEntityTypeConfiguration<SyncOperationReceipt>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public void Configure(EntityTypeBuilder<SyncOperationReceipt> builder)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(builder);
|
||||
|
||||
builder.ToTable("sync_operation_receipt");
|
||||
|
||||
// The client-generated operation id is the key, which is what makes a retried push
|
||||
// exactly-once per operation rather than per batch.
|
||||
builder.HasKey(r => r.OperationId);
|
||||
builder.Property(r => r.OperationId).ValueGeneratedNever();
|
||||
|
||||
builder.HasIndex(r => new { r.VaultId, r.CreatedAtUtc });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user