From 946da21fc86379fd520f2802824c56e967d79eec Mon Sep 17 00:00:00 2001 From: Aditya Niraula Date: Sat, 7 May 2022 21:18:15 +0100 Subject: [PATCH 1/3] fix(backups): no full backup --- src/AgDatabase.cs | 12 +++++++++++- src/SmoFacade/Database.cs | 7 ++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/AgDatabase.cs b/src/AgDatabase.cs index 5423f8e..556f7c3 100755 --- a/src/AgDatabase.cs +++ b/src/AgDatabase.cs @@ -131,9 +131,19 @@ public List RecentBackups() { // find most recent full backup LSN across all replica servers var fullBackupLsnBag = new ConcurrentBag(); - _listener.ForEachAgInstance(s => fullBackupLsnBag.Add(s.Database(Name).MostRecentFullBackupLsn())); + _listener.ForEachAgInstance(s => + { + try + { + fullBackupLsnBag.Add(s.Database(Name).MostRecentFullBackupLsn()); + } + catch { } + }); // find all backups in that chain + if (fullBackupLsnBag.Count == 0) + throw new Exception("Could not find any full backups"); + var databaseBackupLsn = fullBackupLsnBag.Max(); var bag = new ConcurrentBag(); _listener.ForEachAgInstance(s => s.Database(Name).BackupChainFromLsn(databaseBackupLsn) diff --git a/src/SmoFacade/Database.cs b/src/SmoFacade/Database.cs index d38e031..4518049 100755 --- a/src/SmoFacade/Database.cs +++ b/src/SmoFacade/Database.cs @@ -131,8 +131,13 @@ public decimal MostRecentFullBackupLsn() using var reader = cmd.ExecuteReader(); if(!reader.Read()) throw new Exception("MostRecentFullBackup SQL found no results"); + + var lsnValue = reader["most_recent_full_backup_checkpoint_lsn"]; - return (decimal)reader["most_recent_full_backup_checkpoint_lsn"]; + if (lsnValue == DBNull.Value) + throw new Exception("MostRecentFullBackup SQL found no results"); + + return (decimal)lsnValue; } public List BackupChainFromLsn(decimal checkpointLsn) From 0cfe338d000edc50f01ba19619449cd95f2e0694 Mon Sep 17 00:00:00 2001 From: Aditya Niraula Date: Sat, 7 May 2022 21:32:21 +0100 Subject: [PATCH 2/3] fix(backups): including DB name in exception --- src/AgDatabase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AgDatabase.cs b/src/AgDatabase.cs index 556f7c3..7c0ba0e 100755 --- a/src/AgDatabase.cs +++ b/src/AgDatabase.cs @@ -142,7 +142,7 @@ public List RecentBackups() // find all backups in that chain if (fullBackupLsnBag.Count == 0) - throw new Exception("Could not find any full backups"); + throw new Exception($"Could not find any full backups for DB '{Name}'"); var databaseBackupLsn = fullBackupLsnBag.Max(); var bag = new ConcurrentBag(); From d517a64e12eead5045952d7f2a2ece5220ccc01e Mon Sep 17 00:00:00 2001 From: Aditya Niraula Date: Sun, 8 May 2022 11:36:57 +0100 Subject: [PATCH 3/3] refactor(backups): Using `IsEmpty` instead of `Count==0` --- src/AgDatabase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AgDatabase.cs b/src/AgDatabase.cs index 7c0ba0e..607714c 100755 --- a/src/AgDatabase.cs +++ b/src/AgDatabase.cs @@ -141,7 +141,7 @@ public List RecentBackups() }); // find all backups in that chain - if (fullBackupLsnBag.Count == 0) + if (fullBackupLsnBag.IsEmpty) throw new Exception($"Could not find any full backups for DB '{Name}'"); var databaseBackupLsn = fullBackupLsnBag.Max();