Files
Khaemhat/src/Khaemhat.Data/Migrations/IMigration.cs
T
Alby96andClaude Opus 5.5 738c43ba0a
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
feat: data layer with SQLite and PostgreSQL providers, migrations and encrypted company registry (M1)
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>
2026-11-11 13:11:34 +01:00

57 lines
2.4 KiB
C#

using Khaemhat.Data.Schema;
namespace Khaemhat.Data.Migrations;
/// <summary>
/// A schema migration (SPEC §5.3). Describe the change in neutral form in <see cref="Up"/>; add dialect-specific SQL in
/// <see cref="UpSql"/> only for what the neutral form cannot express. Once released, a migration never changes: write a
/// new one. Create the file with <c>scripts/new-migration.sh</c>.
/// <code>
/// [Migration(20261111_002, "create_sys_settings", Target = MigrationTarget.System)]
/// public sealed class M20261111_002_CreateSysSettings : IMigration
/// {
/// public void Up(ISchemaBuilder schema) => schema.CreateTable("sys_settings", t => { … });
/// }
/// </code>
/// </summary>
public interface IMigration
{
/// <summary>Describes the schema change.</summary>
/// <param name="schema">The schema builder.</param>
void Up(ISchemaBuilder schema);
/// <summary>Dialect-specific SQL run after <see cref="Up"/>, or <see langword="null"/>.</summary>
/// <param name="dialect">The dialect of the database being migrated.</param>
/// <returns>Trusted SQL (several statements separated by semicolons are allowed), or <see langword="null"/>.</returns>
string? UpSql(ISqlDialect dialect) => null;
}
/// <summary>Which catalog a migration belongs to.</summary>
public enum MigrationTarget
{
/// <summary>Every company database (<c>khaemhat_&lt;code&gt;</c>).</summary>
Company = 0,
/// <summary>The system database (<c>khaemhat_system</c>).</summary>
System = 1,
}
/// <summary>Marks an <see cref="IMigration"/> with its version and name.</summary>
/// <param name="version">The version <c>yyyymmdd_nnn</c> written as a number, for example <c>20261111_001</c>.</param>
/// <param name="name">The name in snake_case, for example <c>create_sys_companies</c>.</param>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class MigrationAttribute(long version, string name) : Attribute
{
/// <summary>The version <c>yyyymmddnnn</c>.</summary>
public long Version { get; } = version;
/// <summary>The name.</summary>
public string Name { get; } = name;
/// <summary>The catalog (default <see cref="MigrationTarget.Company"/>).</summary>
public MigrationTarget Target { get; init; } = MigrationTarget.Company;
/// <summary>The module that owns the migration, as stored in <c>schema_version.module</c> (default <c>core</c>).</summary>
public string Module { get; init; } = "core";
}