Embedded per-dialect migrations applied in one transaction and recorded in schema_migrations (advisory lock on PostgreSQL, newer schemas refused). Schema for every BRIEF section 5 entity plus import history and uploads. Hand-written repositories for archives, sequences (filters, keyset paging, Archive statistics), frames (bulk insert and updates), renders, jobs (exclusive claiming, checkpoints, crash recovery), media cards, users, tokens, render nodes, settings, tags, audit and uploads. TitanoDatabase opens, migrates and exposes them, also bound to a transaction. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
156 lines
6.1 KiB
C#
156 lines
6.1 KiB
C#
using Titano.Data.Migrations;
|
|
using Titano.Data.Repositories;
|
|
using Titano.Data.Sqlite;
|
|
|
|
namespace Titano.Data;
|
|
|
|
/// <summary>The repositories of the Titano schema, bound either to the database (one session per call) or to one transaction.</summary>
|
|
public abstract class TitanoRepositories
|
|
{
|
|
private protected TitanoRepositories(DbAccess access)
|
|
{
|
|
Archives = new ArchiveRepository(access);
|
|
Sequences = new SequenceRepository(access);
|
|
Frames = new FrameRepository(access);
|
|
Renders = new RenderRepository(access);
|
|
Jobs = new JobRepository(access);
|
|
MediaCards = new MediaCardRepository(access);
|
|
Users = new UserRepository(access);
|
|
Tokens = new TokenRepository(access);
|
|
RenderNodes = new RenderNodeRepository(access);
|
|
Settings = new SettingsRepository(access);
|
|
Tags = new TagRepository(access);
|
|
Audit = new AuditRepository(access);
|
|
Uploads = new UploadRepository(access);
|
|
}
|
|
|
|
public ArchiveRepository Archives { get; }
|
|
public SequenceRepository Sequences { get; }
|
|
public FrameRepository Frames { get; }
|
|
public RenderRepository Renders { get; }
|
|
public JobRepository Jobs { get; }
|
|
public MediaCardRepository MediaCards { get; }
|
|
public UserRepository Users { get; }
|
|
public TokenRepository Tokens { get; }
|
|
public RenderNodeRepository RenderNodes { get; }
|
|
public SettingsRepository Settings { get; }
|
|
public TagRepository Tags { get; }
|
|
public AuditRepository Audit { get; }
|
|
public UploadRepository Uploads { get; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Entry point of the data layer: opens SQLite or PostgreSQL, migrates the schema and exposes the repositories.
|
|
/// Each repository call borrows a pooled session; use <see cref="BeginAsync"/> to group calls atomically.
|
|
/// </summary>
|
|
/// <example>
|
|
/// <code>
|
|
/// await using var db = await TitanoDatabase.OpenAsync(DatabaseOptions.Parse(connectionString), ct);
|
|
/// var page = await db.Sequences.ListAsync(new SequenceQuery { Year = 2026 }, ct);
|
|
/// await using var tx = await db.BeginAsync(ct);
|
|
/// await tx.Frames.InsertAsync(frame, ct);
|
|
/// await tx.Sequences.RefreshStatsAsync(frame.SequenceId, ct);
|
|
/// await tx.CommitAsync(ct);
|
|
/// </code>
|
|
/// </example>
|
|
public sealed class TitanoDatabase : TitanoRepositories, IAsyncDisposable
|
|
{
|
|
private TitanoDatabase(IDatabase database, int schemaVersion)
|
|
: base(new DbAccess(database))
|
|
{
|
|
Database = database;
|
|
SchemaVersion = schemaVersion;
|
|
}
|
|
|
|
/// <summary>The underlying engine, for raw sessions and engine-specific features (backups, LISTEN).</summary>
|
|
public IDatabase Database { get; }
|
|
|
|
public Dialect Dialect => Database.Dialect;
|
|
|
|
/// <summary>Schema version after migration.</summary>
|
|
public int SchemaVersion { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Opens the database and applies pending migrations. Throws <see cref="SchemaTooNewException"/> when a newer
|
|
/// build already migrated it.
|
|
/// </summary>
|
|
public static async Task<TitanoDatabase> OpenAsync(DatabaseOptions options, CancellationToken cancellationToken = default)
|
|
{
|
|
var database = DatabaseFactory.Open(options);
|
|
try
|
|
{
|
|
var version = await MigrationRunner.MigrateAsync(database, cancellationToken).ConfigureAwait(false);
|
|
return new TitanoDatabase(database, version);
|
|
}
|
|
catch
|
|
{
|
|
await database.DisposeAsync().ConfigureAwait(false);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>Starts a transaction whose repositories all run on it; dispose without committing to roll back.</summary>
|
|
public async Task<TitanoTransaction> BeginAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
var session = await Database.OpenAsync(cancellationToken).ConfigureAwait(false);
|
|
try
|
|
{
|
|
return new TitanoTransaction(session, await session.BeginAsync(cancellationToken).ConfigureAwait(false));
|
|
}
|
|
catch
|
|
{
|
|
await session.DisposeAsync().ConfigureAwait(false);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>SQLite only: writes a <c>VACUUM INTO</c> copy into <paramref name="directory"/>, keeping the newest <paramref name="keep"/>.</summary>
|
|
public Task<string> BackupAsync(string directory, int keep = 3, CancellationToken cancellationToken = default) =>
|
|
AsSqlite().BackupAsync(directory, keep, cancellationToken);
|
|
|
|
/// <summary>SQLite only: replaces the database with a backup, then migrates it to this build's schema.</summary>
|
|
public async Task RestoreAsync(string backupPath, CancellationToken cancellationToken = default)
|
|
{
|
|
await AsSqlite().RestoreAsync(backupPath, cancellationToken).ConfigureAwait(false);
|
|
SchemaVersion = await MigrationRunner.MigrateAsync(Database, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
public ValueTask DisposeAsync() => Database.DisposeAsync();
|
|
|
|
private SqliteDatabase AsSqlite() =>
|
|
Database as SqliteDatabase ?? throw new NotSupportedException("Backups of a PostgreSQL database are made with the server's own tools.");
|
|
}
|
|
|
|
/// <summary>Repositories bound to one open transaction.</summary>
|
|
public sealed class TitanoTransaction : TitanoRepositories, IAsyncDisposable
|
|
{
|
|
private readonly IDbTransaction _transaction;
|
|
|
|
internal TitanoTransaction(IDbSession session, IDbTransaction transaction)
|
|
: base(new DbAccess(session))
|
|
{
|
|
Session = session;
|
|
_transaction = transaction;
|
|
}
|
|
|
|
/// <summary>The session the transaction runs on, for statements of your own inside it.</summary>
|
|
public IDbSession Session { get; }
|
|
|
|
public Task CommitAsync(CancellationToken cancellationToken = default) => _transaction.CommitAsync(cancellationToken);
|
|
|
|
public Task RollbackAsync(CancellationToken cancellationToken = default) => _transaction.RollbackAsync(cancellationToken);
|
|
|
|
/// <summary>Rolls back unless committed, and returns the session to the pool.</summary>
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
try
|
|
{
|
|
await _transaction.DisposeAsync().ConfigureAwait(false);
|
|
}
|
|
finally
|
|
{
|
|
await Session.DisposeAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|