Clean-Slate Database Deletion
Delete a Typhon database's
.typhonbundle directory before opening it fresh.
Status: โ Implemented ยท Visibility: Public ยท Level: ๐ต Core ยท Category: Hosting
๐ฏ What it solves
Tests, benchmarks, and tooling need to start each run against an empty database โ no leftover file from a prior run. Reaching into the filesystem by hand means duplicating whatever naming/path logic the engine uses to locate its backing file, and getting it subtly wrong (missing the lock file, racing NTFS's deferred delete) causes flaky "file in use" failures on the next open.
โ๏ธ How it works (in brief)
EnsureFileDeleted<TO> is an extension on IServiceProvider. It opens a DI scope, resolves the
registered IOptions<TO> for the paged-file options type you're using (PagedMMFOptions or
ManagedPagedMMFOptions), and recursively deletes the {DatabaseName}.typhon bundle directory
(data file, db.lock, and wal/) at the path those options describe. It reads the same
DatabaseName/DatabaseDirectory the engine itself will use to open the database, so there is no
separate path logic to keep in sync. Deletion is best-effort โ failures (e.g. nothing to delete) are
swallowed, not thrown.
๐ป Usage
using Microsoft.Extensions.DependencyInjection;
using Typhon.Engine;
var services = new ServiceCollection();
services.AddTyphon(o => o.DatabaseFile(Path.Combine(testDirectory, "MyTestDb.typhon")));
var sp = services.BuildServiceProvider();
// Before the first GetRequiredService<DatabaseEngine>() call โ wipe any leftover bundle.
sp.EnsureFileDeleted<ManagedPagedMMFOptions>();
var engine = sp.GetRequiredService<DatabaseEngine>();
โ ๏ธ Guarantees & limits
- Options-driven, not path-guessing. Resolves the exact
IOptions<TO>the engine would use, so the deleted bundle always matches what a subsequentAddTyphon/AddManagedPagedMMFresolution would open. - Deletes the whole bundle. The
{name}.typhondirectory โ data file,db.lock, andwal/โ is removed as a unit, so a stale lock can't block reopening. - Waits out NTFS deferred delete. Polls briefly after the recursive
Directory.Deleteso a subsequent create at the same path doesn't race the OS's pending-delete state. - Best-effort, silent on failure. Errors (e.g. no file existed) are caught and ignored โ this is a convenience for clean-slate setup, not a verified-delete guarantee. Do not use it to assert deletion succeeded.
- Must be called before the engine opens the file. Calling it against a
DatabaseEngine/ManagedPagedMMFthat already holds the file open will not release that handle first. - Generic parameter must match the registered options type (
PagedMMFOptionsforAddPagedMemoryMappedFile,ManagedPagedMMFOptionsforAddManagedPagedMMF) โ resolving the wrongTOthrows if it isn't registered in the container.
๐งช Tests
- DatabaseFileLockingTests โ
EnsureDeleted_RemovesLockasserts both the.binand.lockfiles are removed; every other test in the fixture callssp.EnsureFileDeleted<ManagedPagedMMFOptions>()as its clean-slate setup step
๐ Related
- Source:
TyphonBuilderExtensions.cs(EnsureFileDeleted<TO>),PagedMMFOptions.cs(EnsureFileDeleted) - Sibling: Engine Options Configuration Surface โ resolves the same
IOptions<TO>this feature deletes the backing file for.