using DodoSSH.Domain; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; namespace DodoSSH.Infrastructure.Configurations; /// Maps . public sealed class HostConfiguration : IEntityTypeConfiguration { /// public void Configure(EntityTypeBuilder 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")); } } /// Maps . /// /// Mirrors in everything the two types share, and deliberately has no /// analogue of the relay CHECK: a key carries no address, which is the reason it is its own table. /// public sealed class SshKeyConfiguration : IEntityTypeConfiguration { /// public void Configure(EntityTypeBuilder builder) { ArgumentNullException.ThrowIfNull(builder); builder.ToTable("ssh_key"); builder.HasKey(k => k.Id); // Client-generated UUIDv7: keys must be creatable offline, with their ids. builder.Property(k => k.Id).ValueGeneratedNever(); builder.UseXminConcurrencyToken(); builder.Property(k => k.Payload).IsRequired(); // SHA256:base64 of a 32-byte digest is 50 characters; the ceiling leaves room for another // algorithm without a migration, and refuses anything that is plainly not a fingerprint. builder.Property(k => k.PublicKeyFingerprint).HasMaxLength(128); builder.HasIndex(k => new { k.VaultId, k.ChangeSequence }); builder.HasIndex(k => k.VaultId) .HasFilter("deleted_at_utc IS NULL") .HasDatabaseName("ix_ssh_key_vault_live"); builder.ToTable(t => t.HasCheckConstraint( "ck_ssh_key_version", "version >= 1")); } } /// Maps . public sealed class SyncChangeConfiguration : IEntityTypeConfiguration { /// public void Configure(EntityTypeBuilder 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(); builder.Property(c => c.Operation).HasConversion(); // 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); } } /// Maps . public sealed class SyncOperationReceiptConfiguration : IEntityTypeConfiguration { /// public void Configure(EntityTypeBuilder 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 }); } }