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

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:
Alby96andClaude Opus 5.5 committed 2026-11-11 13:11:34 +01:00
1 parent 28e7a6e18a
commit 738c43ba0a
105 files changed
+8434 -73

No files matched your search

+7 -6
View File
@@ -1,9 +1,10 @@
#!/usr/bin/env bash
# Regenerates docs/DATA-MODEL.md from the entity maps (SPEC §18).
# scripts/gen-datamodel.sh
# Stub: completed in milestone M1 (SPEC §20). Until then it explains what it will do and exits with an error.
# Regenerates the generated section of docs/DATA-MODEL.md from the migrations (SPEC §18).
# scripts/gen-datamodel.sh rewrite the section
# scripts/gen-datamodel.sh --check only verify it is up to date (exit 1 if not)
set -euo pipefail
echo "gen-datamodel.sh: non ancora disponibile, arriva con la milestone M1." >&2
echo "Uso previsto: scripts/gen-datamodel.sh" >&2
exit 1
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$root"
dotnet run --project tools/Khaemhat.DataModelTool -c Release -- "$@" docs/DATA-MODEL.md
+84 -5
View File
@@ -1,9 +1,88 @@
#!/usr/bin/env bash
# Creates a new numbered migration for the system or company schema (SPEC §5.3).
# scripts/new-migration.sh <system|company> <descrizione-breve>
# Stub: completed in milestone M1 (SPEC §20). Until then it explains what it will do and exits with an error.
# scripts/new-migration.sh <system|company> <nome_snake_case> [cartella] [modulo]
# Example: scripts/new-migration.sh system create_sys_users
# The version is today's date plus the next free number of the day across the repository (yyyymmdd_nnn); the
# class is M<yyyymmdd>_<nnn>_<NomePascal> in the given folder (default: the system or company migrations folder).
set -euo pipefail
echo "new-migration.sh: non ancora disponibile, arriva con la milestone M1." >&2
echo "Uso previsto: scripts/new-migration.sh <system|company> <descrizione-breve>" >&2
exit 1
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$root"
usage() {
echo "Uso: scripts/new-migration.sh <system|company> <nome_snake_case> [cartella] [modulo]" >&2
exit 2
}
[ $# -ge 2 ] || usage
target="$1"
name="$2"
module="${4:-core}"
case "$target" in
system) dir="${3:-src/Khaemhat.Data/SystemDatabase/Migrations}"; attribute_target="MigrationTarget.System" ;;
company) dir="${3:-src/Khaemhat.Application/Migrations}"; attribute_target="MigrationTarget.Company" ;;
*) usage ;;
esac
if ! [[ "$name" =~ ^[a-z][a-z0-9_]{0,62}$ ]]; then
echo "Il nome deve essere snake_case (lettere minuscole, cifre, _): $name" >&2
exit 2
fi
if ! [[ "$module" =~ ^[a-z][a-z0-9_]{0,39}$ ]]; then
echo "Il modulo deve essere snake_case: $module" >&2
exit 2
fi
if [[ "$dir" != src/* ]]; then
echo "La cartella deve stare sotto src/: $dir" >&2
exit 2
fi
day="$(date +%Y%m%d)"
last="$(grep -rhoE "\[Migration\(${day}_[0-9]{3}" src --include='*.cs' 2>/dev/null | sed -E 's/.*_([0-9]{3})$/\1/' | sort -n | tail -1 || true)"
next=$(printf '%03d' $((10#${last:-0} + 1)))
pascal="$(echo "$name" | awk -F_ '{ for (i = 1; i <= NF; i++) printf "%s%s", toupper(substr($i, 1, 1)), substr($i, 2) }')"
class="M${day}_${next}_${pascal}"
namespace="$(echo "${dir#src/}" | tr '/' '.')"
file="$dir/$class.cs"
mkdir -p "$dir"
if [ -e "$file" ]; then
echo "Esiste già: $file" >&2
exit 1
fi
if [ "$attribute_target" = "MigrationTarget.Company" ]; then
target_argument=""
else
target_argument=", Target = $attribute_target"
fi
if [ "$module" = "core" ]; then
module_argument=""
else
module_argument=", Module = \"$module\""
fi
cat > "$file" <<CS
using Khaemhat.Data.Migrations;
using Khaemhat.Data.Schema;
namespace $namespace;
/// <summary>TODO: what this migration changes and why (SPEC section).</summary>
[Migration(${day}_${next}, "$name"$target_argument$module_argument)]
public sealed class $class : IMigration
{
/// <inheritdoc />
public void Up(ISchemaBuilder schema)
{
ArgumentNullException.ThrowIfNull(schema);
// schema.CreateTable("table_name", t => { t.Id(); t.AuditColumns(); });
}
}
CS
echo "Creata $file (versione ${day}_${next}, modulo $module)."
echo "Dopo averla scritta: aggiornare la mappa dell'entità, eseguire scripts/gen-datamodel.sh e i test."