refactor(db): migrate cover letter history

Move append-only cover letter revisions into an additive provider-aware migration. Preserve manual and AI text, source metadata, ordering, and application cascade semantics.
This commit is contained in:
cesnimda
2026-08-30 18:55:20 +02:00
parent e933409698
commit 934b35a1e0
10 changed files with 137 additions and 47 deletions
@@ -116,6 +116,7 @@ public sealed class MigrationChainTests
Assert.Contains("CREATE TABLE IF NOT EXISTS `CvVariants`", script, StringComparison.Ordinal);
Assert.Contains("CREATE TABLE IF NOT EXISTS `CvVariantVersions`", script, StringComparison.Ordinal);
Assert.Contains("CREATE TABLE IF NOT EXISTS `ApplicationChecklistItems`", script, StringComparison.Ordinal);
Assert.Contains("CREATE TABLE IF NOT EXISTS `CoverLetterVersions`", script, StringComparison.Ordinal);
Assert.Contains("`UiLanguage` varchar(16)", script, StringComparison.Ordinal);
Assert.All(
Regex.Matches(script, "CONSTRAINT `([^`]+)`").Select(match => match.Groups[1].Value),
@@ -766,6 +767,59 @@ public sealed class MigrationChainTests
Assert.Empty(await db.Database.GetPendingMigrationsAsync());
}
[Fact]
public async Task Cover_letter_version_adoption_preserves_manual_and_ai_history()
{
await using var connection = new SqliteConnection("Data Source=:memory:");
await connection.OpenAsync();
await using var db = Context(connection);
var migrator = db.GetService<IMigrator>();
await migrator.MigrateAsync("20260830130000_AdoptApplicationChecklistSchema");
await ExecuteAsync(connection, """
INSERT INTO Companies (Name) VALUES ('Fixture company');
INSERT INTO JobApplications
(CompanyId, JobTitle, DateApplied, SavedAt, Status, ResponseReceived, OwnerUserId,
HasResume, HasCoverLetter, HasPortfolio, HasOtherAttachment, IsDeleted)
VALUES
(1, 'Fixture role', '2026-08-01T09:00:00', '2026-07-20T09:00:00',
'Applied', 0, 'owner-fixture', 0, 0, 0, 0, 0);
CREATE TABLE "CoverLetterVersions" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_CoverLetterVersions" PRIMARY KEY AUTOINCREMENT,
"OwnerUserId" TEXT NOT NULL, "JobApplicationId" INTEGER NOT NULL,
"Version" INTEGER NOT NULL, "Text" TEXT NOT NULL, "Source" TEXT NOT NULL,
"AiAction" TEXT NULL, "CreatedAtUtc" TEXT NOT NULL,
CONSTRAINT "FK_CoverLetterVersions_JobApplications_JobApplicationId"
FOREIGN KEY ("JobApplicationId") REFERENCES "JobApplications" ("Id") ON DELETE CASCADE
);
INSERT INTO "CoverLetterVersions"
("OwnerUserId", "JobApplicationId", "Version", "Text", "Source", "AiAction", "CreatedAtUtc")
VALUES
('owner-fixture', 1, 1, 'Manual first draft', 'manual', NULL,
'2026-08-30T09:00:00+00:00'),
('owner-fixture', 1, 2, 'Shorter approved draft', 'ai', 'shorten',
'2026-08-30T09:10:00+00:00');
""");
await migrator.MigrateAsync();
Assert.Equal("Manual first draft", await ScalarAsync<string>(connection,
"SELECT Text FROM CoverLetterVersions WHERE Version = 1;"));
Assert.Equal("shorten", await ScalarAsync<string>(connection,
"SELECT AiAction FROM CoverLetterVersions WHERE Version = 2;"));
await migrator.MigrateAsync("20260830130000_AdoptApplicationChecklistSchema");
Assert.Equal(2L, await ScalarAsync<long>(connection,
"SELECT COUNT(*) FROM CoverLetterVersions WHERE JobApplicationId = 1;"));
await migrator.MigrateAsync();
Assert.Equal(1L, await ScalarAsync<long>(connection, """
SELECT COUNT(*) FROM sqlite_master
WHERE type = 'index' AND name = 'IX_CoverLetterVersions_Owner_Job_Version';
"""));
await ExecuteAsync(connection, "DELETE FROM JobApplications WHERE Id = 1;");
Assert.Equal(0L, await ScalarAsync<long>(connection, "SELECT COUNT(*) FROM CoverLetterVersions;"));
Assert.Empty(await db.Database.GetPendingMigrationsAsync());
}
private static JobTrackerContext Context(SqliteConnection connection)
{
var currentUser = new Mock<ICurrentUserService>();