From b6d4163caa9516e6ba9eb7c549ebdf7a169b1322 Mon Sep 17 00:00:00 2001 From: Erik Darling <2136037+erikdarlingdata@users.noreply.github.com> Date: Thu, 26 Feb 2026 08:26:50 -0500 Subject: [PATCH] Fix DuckDB migration ordering after archive-and-reset MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous fix (PR #313) moved CREATE TABLE before migrations, but migrations v2/v6/v7/v8 DROP TABLE IF EXISTS then expect CREATE TABLE to follow — so tables were created then immediately dropped. Correct fix: skip migrations entirely on fresh/reset databases (v0). When existingVersion is 0, there's nothing to migrate — just create tables with the current schema and stamp the version. Fixes CI test failures: InitializeAsync_CreatesAllTables, InitializeAsync_CreatesIndexes. Co-Authored-By: Claude Opus 4.6 --- Lite/Database/DuckDbInitializer.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Lite/Database/DuckDbInitializer.cs b/Lite/Database/DuckDbInitializer.cs index 74d60f30..953401fe 100644 --- a/Lite/Database/DuckDbInitializer.cs +++ b/Lite/Database/DuckDbInitializer.cs @@ -141,9 +141,15 @@ await ExecuteNonQueryAsync(connection, var existingVersion = await GetSchemaVersionAsync(connection); - /* Create tables first (IF NOT EXISTS) so migrations can ALTER them safely. - After an archive-and-reset the database is empty — running ALTER TABLE - before CREATE TABLE would fail on nonexistent tables. */ + /* On a fresh/reset database (v0), skip migrations entirely — they DROP tables + expecting CREATE TABLE to follow, which is destructive on a blank DB. + Just create tables with the current schema and stamp the version. */ + if (existingVersion > 0 && existingVersion < CurrentSchemaVersion) + { + _logger?.LogInformation("Schema upgrade needed: v{Old} -> v{New}", existingVersion, CurrentSchemaVersion); + await RunMigrationsAsync(connection, existingVersion); + } + foreach (var tableStatement in Schema.GetAllTableStatements()) { await ExecuteNonQueryAsync(connection, tableStatement); @@ -156,8 +162,6 @@ before CREATE TABLE would fail on nonexistent tables. */ if (existingVersion < CurrentSchemaVersion) { - _logger?.LogInformation("Schema upgrade needed: v{Old} -> v{New}", existingVersion, CurrentSchemaVersion); - await RunMigrationsAsync(connection, existingVersion); await SetSchemaVersionAsync(connection, CurrentSchemaVersion); }