diff --git a/Dashboard/Services/DatabaseService.NocHealth.cs b/Dashboard/Services/DatabaseService.NocHealth.cs index c1f59887..7a98b656 100644 --- a/Dashboard/Services/DatabaseService.NocHealth.cs +++ b/Dashboard/Services/DatabaseService.NocHealth.cs @@ -641,7 +641,7 @@ ORDER BY r.total_elapsed_time DESC { results.Add(new LongRunningQueryInfo { - SessionId = reader.GetInt32(0), + SessionId = Convert.ToInt32(reader.GetValue(0)), DatabaseName = reader.IsDBNull(1) ? "" : reader.GetString(1), QueryText = reader.IsDBNull(2) ? "" : reader.GetString(2), ProgramName = reader.IsDBNull(3) ? "" : reader.GetString(3), diff --git a/Lite/Database/DuckDbInitializer.cs b/Lite/Database/DuckDbInitializer.cs index 57121939..74d60f30 100644 --- a/Lite/Database/DuckDbInitializer.cs +++ b/Lite/Database/DuckDbInitializer.cs @@ -141,13 +141,9 @@ await ExecuteNonQueryAsync(connection, var existingVersion = await GetSchemaVersionAsync(connection); - if (existingVersion < CurrentSchemaVersion) - { - _logger?.LogInformation("Schema upgrade needed: v{Old} -> v{New}", existingVersion, CurrentSchemaVersion); - await RunMigrationsAsync(connection, existingVersion); - await SetSchemaVersionAsync(connection, CurrentSchemaVersion); - } - + /* 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. */ foreach (var tableStatement in Schema.GetAllTableStatements()) { await ExecuteNonQueryAsync(connection, tableStatement); @@ -158,6 +154,13 @@ await ExecuteNonQueryAsync(connection, await ExecuteNonQueryAsync(connection, indexStatement); } + if (existingVersion < CurrentSchemaVersion) + { + _logger?.LogInformation("Schema upgrade needed: v{Old} -> v{New}", existingVersion, CurrentSchemaVersion); + await RunMigrationsAsync(connection, existingVersion); + await SetSchemaVersionAsync(connection, CurrentSchemaVersion); + } + _logger?.LogInformation("Database initialization complete. Schema version: {Version}", CurrentSchemaVersion); }