M1 Dati e libreria: modello, archivi SQLite con scrittore unico, nomi dei file, scrittura sicura, miniature, impronte, _febe.jsonl, copie, eliminazione e ricostruzione
Modello del dominio e interfacce degli archivi in Febe.Core; implementazione SQLite in Febe.Data (WAL, chiavi esterne, migrazioni numerate in transazione, coda di scrittura su un solo thread); logica della libreria in Febe.Core/Library; 164 prove MSTest, compresa la ricostruzione del database da una libreria di prova e la scala a 200.000 elementi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
69 files changed
+12472
No files matched your search
@@ -0,0 +1,161 @@
|
||||
using Febe.Data;
|
||||
using Febe.Tests.Support;
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
namespace Febe.Tests.Data;
|
||||
|
||||
[TestClass]
|
||||
public sealed class MigrationTests
|
||||
{
|
||||
private static readonly string[] Tables =
|
||||
[
|
||||
"setting", "account", "entry", "media", "highlight", "highlight_entry", "duplicate_pair", "task", "action_log",
|
||||
"session", "incident", "event", "following", "import_run", "import_file",
|
||||
];
|
||||
|
||||
[TestMethod]
|
||||
public void EmbeddedMigrationsAreNumberedFromOne()
|
||||
{
|
||||
var migrations = MigrationRunner.LoadEmbedded();
|
||||
Assert.IsNotEmpty(migrations);
|
||||
for (var i = 0; i < migrations.Count; i++)
|
||||
{
|
||||
Assert.AreEqual(i + 1, migrations[i].Version);
|
||||
}
|
||||
|
||||
Assert.AreEqual("schema", migrations[0].Name);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NumberingWithGapsIsRefused()
|
||||
{
|
||||
Assert.ThrowsExactly<DatabaseMigrationException>(() => MigrationRunner.Validate([new Migration(1, "a", "SELECT 1"), new Migration(3, "c", "SELECT 1")]));
|
||||
Assert.ThrowsExactly<DatabaseMigrationException>(() => MigrationRunner.Validate([new Migration(1, "a", "SELECT 1"), new Migration(1, "b", "SELECT 1")]));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task FreshDatabaseHasEveryTableAndTheLatestVersion()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using var db = await TestDatabase.OpenAsync(temp);
|
||||
|
||||
Assert.AreEqual(MigrationRunner.LoadEmbedded().Count, db.SchemaVersion);
|
||||
Assert.AreEqual(Path.Combine(temp.Combine("dati"), FebeDatabase.FileName), db.DatabasePath);
|
||||
var tables = await db.ReadAsync(
|
||||
c =>
|
||||
{
|
||||
using var r = c.Command("SELECT name FROM sqlite_master WHERE type = 'table'").ExecuteReader();
|
||||
var names = new List<string>();
|
||||
while (r.Read())
|
||||
{
|
||||
names.Add(r.GetString(0));
|
||||
}
|
||||
|
||||
return names;
|
||||
},
|
||||
default);
|
||||
foreach (var table in Tables)
|
||||
{
|
||||
Assert.Contains(table, tables);
|
||||
}
|
||||
|
||||
var version = await db.ReadAsync(c => c.Command("PRAGMA user_version").ScalarInt64(), default);
|
||||
Assert.AreEqual((long)db.SchemaVersion, version);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DatabaseUsesWalForeignKeysAndBusyTimeout()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using var db = await TestDatabase.OpenAsync(temp);
|
||||
|
||||
var mode = await db.ReadAsync(c => (string)c.Command("PRAGMA journal_mode").ExecuteScalar()!, default);
|
||||
Assert.AreEqual("wal", mode);
|
||||
var foreignKeys = await db.WriteAsync(w => w.Command("PRAGMA foreign_keys").ScalarInt64(), default);
|
||||
Assert.AreEqual(1L, foreignKeys);
|
||||
var timeout = await db.WriteAsync(w => w.Command("PRAGMA busy_timeout").ScalarInt64(), default);
|
||||
Assert.IsGreaterThanOrEqualTo(5000L, timeout);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task ReopeningKeepsDataAndDoesNotRerunMigrations()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using (var db = await TestDatabase.OpenAsync(temp))
|
||||
{
|
||||
await db.Settings.SetAsync("prova", "1");
|
||||
}
|
||||
|
||||
await using (var db = await TestDatabase.OpenAsync(temp))
|
||||
{
|
||||
Assert.AreEqual("1", await db.Settings.GetAsync("prova"));
|
||||
Assert.AreEqual(MigrationRunner.LoadEmbedded().Count, db.SchemaVersion);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task NewerDatabaseIsRefusedAndLeftUntouched()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
var path = temp.Combine("dati", FebeDatabase.FileName);
|
||||
await using (var db = await TestDatabase.OpenAsync(temp))
|
||||
{
|
||||
await db.Settings.SetAsync("prova", "1");
|
||||
}
|
||||
|
||||
SetUserVersion(path, 99);
|
||||
var error = await Assert.ThrowsExactlyAsync<DatabaseMigrationException>(() => FebeDatabase.OpenFileAsync(path));
|
||||
Assert.Contains("più recente", error.Message);
|
||||
Assert.AreEqual(99L, GetUserVersion(path));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FailingMigrationRollsBackEntirely()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
var path = temp.Combine("dati", FebeDatabase.FileName);
|
||||
var good = MigrationRunner.LoadEmbedded();
|
||||
var broken = good.Append(new Migration(good.Count + 1, "rotta", "CREATE TABLE extra (id INTEGER); INSERT INTO nowhere VALUES (1);")).ToList();
|
||||
|
||||
Assert.ThrowsExactly<DatabaseMigrationException>(() => FebeDatabase.Open(path, broken));
|
||||
|
||||
Assert.AreEqual((long)good.Count, GetUserVersion(path));
|
||||
using var connection = new SqliteConnection($"Data Source={path};Pooling=False");
|
||||
connection.Open();
|
||||
Assert.AreEqual(0L, connection.Command("SELECT COUNT(*) FROM sqlite_master WHERE name = 'extra'").ScalarInt64());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MigrationAddingDataAppliesOnTopOfTheSchema()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
var path = temp.Combine("dati", FebeDatabase.FileName);
|
||||
var good = MigrationRunner.LoadEmbedded();
|
||||
var next = good.Append(new Migration(good.Count + 1, "dati", "INSERT INTO setting (key, value) VALUES ('migrata', 'si');")).ToList();
|
||||
|
||||
var db = FebeDatabase.Open(path, next);
|
||||
try
|
||||
{
|
||||
Assert.AreEqual(good.Count + 1, db.SchemaVersion);
|
||||
Assert.AreEqual("si", db.Settings.GetAsync("migrata").GetAwaiter().GetResult());
|
||||
}
|
||||
finally
|
||||
{
|
||||
db.DisposeAsync().AsTask().GetAwaiter().GetResult();
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetUserVersion(string path, int version)
|
||||
{
|
||||
using var connection = new SqliteConnection($"Data Source={path};Pooling=False");
|
||||
connection.Open();
|
||||
connection.Command($"PRAGMA user_version = {version}").ExecuteNonQuery();
|
||||
}
|
||||
|
||||
private static long GetUserVersion(string path)
|
||||
{
|
||||
using var connection = new SqliteConnection($"Data Source={path};Pooling=False");
|
||||
connection.Open();
|
||||
return connection.Command("PRAGMA user_version").ScalarInt64();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user