feat: data layer with SQLite and PostgreSQL providers, migrations and encrypted company registry (M1)
CI / Build and test (SQLite) (pull_request) Successful in 45s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 3s
CI / Docker image (pull_request) Successful in 51s
CI / Build and test (SQLite) (pull_request) Successful in 45s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 3s
CI / Docker image (pull_request) Successful in 51s
Every later milestone stores its data through this layer, so it has to behave the same on both engines and be safe by construction: - Khaemhat.Data: explicit entity maps, typed queries over a safe expression subset, SqlFragment where every interpolated value becomes a parameter, unit of work with transactions, repositories with audit columns and optimistic concurrency on row_version, retries on transient errors. - SQLite provider (STRICT tables, WAL, exact decimals as integers scaled by 10^6, Unicode case-insensitive LIKE, serialised writes) and PostgreSQL 16+ provider (numeric(19,6), timestamptz, jsonb, advisory locks, database creation and server info). Databases are configured from postgres:// and sqlite:// URLs whose password never appears in messages. - C# migrations with system and company catalogues, schema_version with checksums, one transaction per migration under a lock, refusal of unknown applied versions; first system tables sys_companies and sys_settings. - CompanyRegistry stores each company's database URL encrypted with the instance key (AES-256-GCM, HKDF-derived key, purpose as associated data), so a copied row cannot be read for another company. - tools/Khaemhat.DataModelTool, scripts/gen-datamodel.sh and scripts/new-migration.sh: the current schema section of docs/DATA-MODEL.md is generated from the migrations and a test fails when it is stale. - C# 14 binds array.Contains to MemoryExtensions over spans; the query translator unwraps the span conversion. - Tests run every data scenario on SQLite and PostgreSQL (PostgreSQL in CI); ADR 0018 records where the contracts differ from SPEC 5.1. Docs, wiki, changelog and status updated. Closes #7, #8, #9, #10, #11, #12, #13, #14 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
28e7a6e18a
commit
738c43ba0a
105 files changed
+8434
-73
No files matched your search
@@ -0,0 +1,51 @@
|
||||
namespace Khaemhat.Core.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Thrown when a row changed (or was deleted) after it was read: the update found no row with the expected
|
||||
/// <c>row_version</c> (SPEC §5.2). The interface reloads and asks the user; the API answers <c>409</c>.
|
||||
/// </summary>
|
||||
public sealed class ConcurrencyException : Exception
|
||||
{
|
||||
/// <summary>Creates the exception for a row of <paramref name="table"/>.</summary>
|
||||
/// <param name="table">The table name.</param>
|
||||
/// <param name="key">The primary key of the row, as text.</param>
|
||||
/// <param name="expectedVersion">The <c>row_version</c> the caller read.</param>
|
||||
public ConcurrencyException(string table, string key, long expectedVersion)
|
||||
: base($"The row {key} of {table} was changed or deleted by someone else (expected version {expectedVersion}).")
|
||||
{
|
||||
Table = table;
|
||||
Key = key;
|
||||
ExpectedVersion = expectedVersion;
|
||||
}
|
||||
|
||||
/// <summary>Creates the exception with a message only.</summary>
|
||||
/// <param name="message">The message.</param>
|
||||
public ConcurrencyException(string message) : base(message)
|
||||
{
|
||||
Table = string.Empty;
|
||||
Key = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>Creates the exception with a message and an inner exception.</summary>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <param name="innerException">The cause.</param>
|
||||
public ConcurrencyException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
Table = string.Empty;
|
||||
Key = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>Creates the exception without details.</summary>
|
||||
public ConcurrencyException() : this("The row was changed or deleted by someone else.")
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>The table of the row.</summary>
|
||||
public string Table { get; }
|
||||
|
||||
/// <summary>The primary key of the row, as text.</summary>
|
||||
public string Key { get; }
|
||||
|
||||
/// <summary>The <c>row_version</c> the caller read.</summary>
|
||||
public long ExpectedVersion { get; }
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace Khaemhat.Core.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Convenience base class for entities with an identity key and the audit columns. Entities may also implement
|
||||
/// <see cref="IEntity{TKey}"/> and <see cref="IAuditable"/> directly.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">The key type.</typeparam>
|
||||
public abstract class Entity<TKey> : IEntity<TKey>, IAuditable
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public TKey Id { get; set; } = default!;
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? CreatedBy { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? UpdatedBy { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public long RowVersion { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace Khaemhat.Core.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// An entity with the audit columns of SPEC §5.2 (<c>created_at</c>, <c>created_by</c>, <c>updated_at</c>,
|
||||
/// <c>updated_by</c>) and the <c>row_version</c> used for optimistic concurrency. The data layer fills them; code above
|
||||
/// it only reads them.
|
||||
/// </summary>
|
||||
public interface IAuditable
|
||||
{
|
||||
/// <summary>When the row was inserted, in UTC.</summary>
|
||||
DateTime CreatedAt { get; set; }
|
||||
|
||||
/// <summary>Who inserted the row (user name, or a system actor such as <c>system</c>), when known.</summary>
|
||||
string? CreatedBy { get; set; }
|
||||
|
||||
/// <summary>When the row was last updated, in UTC; <see langword="null"/> if it never was.</summary>
|
||||
DateTime? UpdatedAt { get; set; }
|
||||
|
||||
/// <summary>Who last updated the row, when known.</summary>
|
||||
string? UpdatedBy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Incremented by every update. An update succeeds only if the stored value still equals this one; otherwise the
|
||||
/// data layer throws <see cref="Common.ConcurrencyException"/>.
|
||||
/// </summary>
|
||||
long RowVersion { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Khaemhat.Core.Entities;
|
||||
|
||||
/// <summary>An entity whose rows can be created by the demo seeder and removed with it (SPEC §14, column <c>is_demo</c>).</summary>
|
||||
public interface IDemoMarkable
|
||||
{
|
||||
/// <summary><see langword="true"/> for rows created by the demo data seeder.</summary>
|
||||
bool IsDemo { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Khaemhat.Core.Entities;
|
||||
|
||||
/// <summary>An entity with an identity of type <typeparamref name="TKey"/>, stored in the <c>id</c> column (SPEC §5.2).</summary>
|
||||
/// <typeparam name="TKey">The key type: <see cref="long"/> for identity keys, which is the default for every table.</typeparam>
|
||||
public interface IEntity<TKey>
|
||||
{
|
||||
/// <summary>The primary key. For identity keys it is the default value until the row is inserted.</summary>
|
||||
TKey Id { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using Khaemhat.Core.Entities;
|
||||
|
||||
namespace Khaemhat.Core.Instance;
|
||||
|
||||
/// <summary>
|
||||
/// A company in the instance registry (<c>sys_companies</c>, SPEC §5.4): its code, name and the location of its own
|
||||
/// database. The location is stored encrypted and is never shown or logged.
|
||||
/// </summary>
|
||||
public sealed class Company : Entity<long>
|
||||
{
|
||||
/// <summary>Company code: upper-case letters, digits and underscore, 1 to 20 characters (database <c>khaemhat_code</c>).</summary>
|
||||
public string Code { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Company name (ragione sociale).</summary>
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The database engine name: <c>Sqlite</c> or <c>PostgreSql</c>.</summary>
|
||||
public string Engine { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The database URL, encrypted with the instance key.</summary>
|
||||
public string ConnectionString { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The company state.</summary>
|
||||
public CompanyStatus Status { get; set; }
|
||||
|
||||
/// <summary>Whether this is the company opened by default.</summary>
|
||||
public bool IsDefault { get; set; }
|
||||
|
||||
/// <summary>Free notes for administrators.</summary>
|
||||
public string? Notes { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace Khaemhat.Core.Instance;
|
||||
|
||||
/// <summary>State of a company database in the registry (SPEC §5.4, §12.1).</summary>
|
||||
public enum CompanyStatus
|
||||
{
|
||||
/// <summary>In use.</summary>
|
||||
Active = 0,
|
||||
|
||||
/// <summary>Maintenance in progress (migration, restore): nobody but administrators can open it.</summary>
|
||||
Maintenance = 1,
|
||||
|
||||
/// <summary>Readable only, for example an archive.</summary>
|
||||
ReadOnly = 2,
|
||||
|
||||
/// <summary>Kept but not available.</summary>
|
||||
Disabled = 3,
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Khaemhat.Core.Entities;
|
||||
|
||||
namespace Khaemhat.Core.Instance;
|
||||
|
||||
/// <summary>An instance setting (<c>sys_settings</c>). Secret values are stored encrypted (<see cref="IsSecret"/>).</summary>
|
||||
public sealed class SystemSetting : Entity<long>
|
||||
{
|
||||
/// <summary>The setting key, for example <c>smtp.host</c>.</summary>
|
||||
public string Key { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The value; encrypted when <see cref="IsSecret"/> is set.</summary>
|
||||
public string? Value { get; set; }
|
||||
|
||||
/// <summary>Whether <see cref="Value"/> is a secret encrypted with the instance key.</summary>
|
||||
public bool IsSecret { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace Khaemhat.Core.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts secrets stored in the databases (company connection strings, SMTP/PEC/S3/WebDAV passwords, tokens) with
|
||||
/// the instance key <c>KHAEMHAT_SECRET_KEY</c> (SPEC §5.4). The <c>purpose</c> binds a ciphertext to its use: a value
|
||||
/// protected for one purpose cannot be read back for another.
|
||||
/// </summary>
|
||||
public interface ISecretProtector
|
||||
{
|
||||
/// <summary>Encrypts <paramref name="plaintext"/>; the result is printable text safe to store in a column.</summary>
|
||||
/// <param name="plaintext">The secret.</param>
|
||||
/// <param name="purpose">What the secret is for, for example <c>company:DEMO:database</c>.</param>
|
||||
/// <returns>The protected value.</returns>
|
||||
string Protect(string plaintext, string purpose);
|
||||
|
||||
/// <summary>Decrypts a value produced by <see cref="Protect"/> with the same key and purpose.</summary>
|
||||
/// <param name="protectedValue">The protected value.</param>
|
||||
/// <param name="purpose">The purpose given to <see cref="Protect"/>.</param>
|
||||
/// <returns>The secret.</returns>
|
||||
/// <exception cref="SecretProtectionException">The value was protected with another key or purpose, or is damaged.</exception>
|
||||
string Unprotect(string protectedValue, string purpose);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace Khaemhat.Core.Security;
|
||||
|
||||
/// <summary>
|
||||
/// A secret could not be decrypted: wrong instance key, wrong purpose or damaged value. The message never contains
|
||||
/// the secret or the ciphertext.
|
||||
/// </summary>
|
||||
public sealed class SecretProtectionException : Exception
|
||||
{
|
||||
/// <summary>Creates the exception.</summary>
|
||||
public SecretProtectionException() : base("The secret cannot be decrypted.")
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>Creates the exception with a message.</summary>
|
||||
/// <param name="message">The message (never the secret).</param>
|
||||
public SecretProtectionException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>Creates the exception with a message and a cause.</summary>
|
||||
/// <param name="message">The message (never the secret).</param>
|
||||
/// <param name="innerException">The cause.</param>
|
||||
public SecretProtectionException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user