From ff2784b86235570099750d2d282a0542136f30af Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Tue, 24 Mar 2026 14:40:42 -0400 Subject: [PATCH] fix(site-runtime): add SQLite schema migration for backup_configuration column Existing site databases created before the primary/backup data connections feature lack the backup_configuration and failover_retry_count columns. Added TryAddColumnAsync migration that runs on startup after table creation. --- .../Persistence/SiteStorageService.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/ScadaLink.SiteRuntime/Persistence/SiteStorageService.cs b/src/ScadaLink.SiteRuntime/Persistence/SiteStorageService.cs index 99cee09..ac18b33 100644 --- a/src/ScadaLink.SiteRuntime/Persistence/SiteStorageService.cs +++ b/src/ScadaLink.SiteRuntime/Persistence/SiteStorageService.cs @@ -101,9 +101,35 @@ public class SiteStorageService "; await command.ExecuteNonQueryAsync(); + // Schema migrations — add columns that may not exist on older databases + await MigrateSchemaAsync(connection); + _logger.LogInformation("Site SQLite storage initialized at {ConnectionString}", _connectionString); } + private async Task MigrateSchemaAsync(SqliteConnection connection) + { + // Add backup_configuration and failover_retry_count to data_connection_definitions + // (added in primary/backup data connections feature) + await TryAddColumnAsync(connection, "data_connection_definitions", "backup_configuration", "TEXT"); + await TryAddColumnAsync(connection, "data_connection_definitions", "failover_retry_count", "INTEGER NOT NULL DEFAULT 3"); + } + + private async Task TryAddColumnAsync(SqliteConnection connection, string table, string column, string type) + { + try + { + await using var cmd = connection.CreateCommand(); + cmd.CommandText = $"ALTER TABLE {table} ADD COLUMN {column} {type}"; + await cmd.ExecuteNonQueryAsync(); + _logger.LogInformation("Migrated: added column {Column} to {Table}", column, table); + } + catch (SqliteException ex) when (ex.Message.Contains("duplicate column")) + { + // Column already exists — no action needed + } + } + // ── Deployed Configuration CRUD ── ///