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:
jaap-jan committed 2026-07-28 14:17:37 +02:00
1 parent 06d04b490b
commit eaf68c86b0
24 files changed
+5843

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
namespace DodoSSH.Domain;
/// <summary>What a change did to an entity.</summary>
public enum ChangeOperation
{
/// <summary>Not a legal value.</summary>
Unspecified = 0,
/// <summary>Created or modified.</summary>
Upsert = 1,
/// <summary>Soft-deleted, leaving a tombstone.</summary>
Delete = 2,
}
/// <summary>The kind of item a change refers to. Mirrors the contract enum; append only.</summary>
public enum ChangeEntityType
{
/// <summary>Not a legal value.</summary>
Unspecified = 0,
/// <summary>An SSH host.</summary>
Host = 1,
/// <summary>A credential. M2.</summary>
Credential = 2,
/// <summary>An SSH key pair. M2.</summary>
SshKey = 3,
/// <summary>A host group. M2.</summary>
HostGroup = 4,
/// <summary>A tag. M2.</summary>
Tag = 5,
/// <summary>A host-to-tag association. M2.</summary>
HostTag = 6,
/// <summary>A host-to-credential association. M2.</summary>
HostCredential = 7,
/// <summary>A snippet. M2.</summary>
Snippet = 8,
/// <summary>A port forward. M2.</summary>
PortForward = 9,
/// <summary>A known SSH host key. M2.</summary>
KnownHostKey = 10,
}
/// <summary>
/// One entry in a vault's change log, which is what delta sync reads.
/// </summary>
/// <remarks>
/// <para>
/// <see cref="Sequence"/> comes from a <c>bigserial</c>, and that carries a trap worth stating
/// where the code lives: sequence values are handed out <em>before</em> commit. If transaction A
/// takes 5 and B takes 6 but B commits first, a reader that advances its cursor to 6 permanently
/// misses 5 — silent sync corruption that only appears under concurrent writes to one vault.
/// </para>
/// <para>
/// Every push therefore takes a per-vault transaction-scoped advisory lock as its first
/// statement, so sequence order equals commit order. See ADR 0003.
/// </para>
/// </remarks>
public sealed class SyncChange
{
/// <summary>Monotonic sequence. Database-generated.</summary>
public long Sequence { get; set; }
/// <summary>Owning vault. Cursors are scoped per vault.</summary>
public Guid VaultId { get; set; }
/// <summary>Kind of item.</summary>
public ChangeEntityType EntityType { get; set; }
/// <summary>The item.</summary>
public Guid EntityId { get; set; }
/// <summary>What happened.</summary>
public ChangeOperation Operation { get; set; }
/// <summary>The item's version after the change.</summary>
public int Revision { get; set; }
/// <summary>Who made the change.</summary>
public Guid ActorUserId { get; set; }
/// <summary>When it was recorded.</summary>
public DateTimeOffset OccurredAtUtc { get; set; }
}
/// <summary>
/// Records that a client operation was applied, so a retry is a no-op.
/// </summary>
/// <remarks>
/// Keyed on the client-generated operation id, which makes retries exactly-once at
/// <em>operation</em> granularity. A batch-level idempotency key alone would not: a client that
/// times out mid-push and retries with a partially overlapping batch would otherwise double-apply
/// the operations that did land.
/// </remarks>
public sealed class SyncOperationReceipt
{
/// <summary>The client-generated operation id. Primary key.</summary>
public Guid OperationId { get; set; }
/// <summary>Vault the operation targeted.</summary>
public Guid VaultId { get; set; }
/// <summary>Change-log sequence produced, when the operation was applied.</summary>
public long? AppliedChangeSequence { get; set; }
/// <summary>The item's version after the operation.</summary>
public int? ResultVersion { get; set; }
/// <summary>Creation timestamp.</summary>
public DateTimeOffset CreatedAtUtc { get; set; }
}