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 ── ///