Skip to content

Commit

Permalink
Improve error handling for schema snapshots
Browse files Browse the repository at this point in the history
Logging improvement for schema snapshot failures.  Continue scripting if errors occur scripting synonyms.  #915
  • Loading branch information
DavidWiseman committed Jun 24, 2024
1 parent 53a5203 commit df327fe
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
11 changes: 9 additions & 2 deletions DBADash/DBCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public enum HostPlatform
Windows
}

public class DBCollector
public class DBCollector: IErrorLogger
{
public DataSet Data;
private string ConnectionString => Source.SourceConnection.ConnectionString;
Expand Down Expand Up @@ -180,12 +180,19 @@ public DBCollector(DBADashSource source, string serviceName, bool disableRetry =
Startup();
}

private void LogError(Exception ex, string errorSource, string errorContext = "Collect")
public void LogError(Exception ex, string errorSource, string errorContext = "Collect")
{
Log.Error(ex, "{ErrorContext} {ErrorSource} {Connection}", errorContext, errorSource, Source.SourceConnection.ConnectionForPrint);
LogDBError(errorSource, ex.ToString(), errorContext);
}

public void ClearErrors()
{
dtErrors.Rows.Clear();
}

public int ErrorCount => dtErrors.Rows.Count;

private void LogDBError(string errorSource, string errorMessage, string errorContext = "Collect")
{
var rError = dtErrors.NewRow();
Expand Down
13 changes: 13 additions & 0 deletions DBADash/IErrorLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBADash
{
public interface IErrorLogger
{
public void LogError(Exception ex, string errorSource, string errorContext);
}
}
3 changes: 2 additions & 1 deletion DBADash/Messaging/CollectionMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public override async Task<DataSet> Process(CollectionConfig cfg, Guid handle)
{
Log.Information("Message {handle} requested snapshot for database {DatabaseName} on {instance}", handle,DatabaseName,ConnectionID);
var schema = new SchemaSnapshotDB(src.SourceConnection, cfg.SchemaSnapshotOptions);
var dt = schema.SnapshotDB(DatabaseName);
var dt = schema.SnapshotDB(DatabaseName, collector);
collector.Data.Tables.Add(dt);
}
}
Expand All @@ -98,6 +98,7 @@ public override async Task<DataSet> Process(CollectionConfig cfg, Guid handle)
op.Complete();
return null;
}

}

private (List<CollectionType>, Dictionary<string, CustomCollection>) ParseCollectionTypes(DBADashSource src, CollectionConfig cfg)
Expand Down
58 changes: 39 additions & 19 deletions DBADash/SchemaSnapshotDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public SchemaSnapshotDB(DBADashConnection source) : base(source)
{
}

private const string ErrorSource = "SchemaSnapshot";

private static DataTable RgDTSchema()
{
DataTable dtRG = new()
Expand Down Expand Up @@ -175,7 +177,7 @@ public static string ObjectDDL(string connectionString, string objectName)
}
}

public DataTable SnapshotDB(string DBName)
public DataTable SnapshotDB(string DBName, IErrorLogger errorLogger)
{
var StartTime = DateTime.UtcNow;

Expand Down Expand Up @@ -315,7 +317,7 @@ public DataTable SnapshotDB(string DBName)
{
using (var op = Operation.Begin("Schema snapshot {DBame}: {Object}", DBName, "Synonyms"))
{
AddSynonyms(db, dtSchema);
AddSynonyms(db, dtSchema,errorLogger);
op.Complete();
}
}
Expand Down Expand Up @@ -664,22 +666,29 @@ private void AddSchema(Database db, DataTable dtSchema)
}
}

private void AddSynonyms(Database db, DataTable dtSchema)
private void AddSynonyms(Database db, DataTable dtSchema, IErrorLogger logger)
{
foreach (Synonym s in db.Synonyms)
{
var r = dtSchema.NewRow();
var sDDL = StringCollectionToString(s.Script(ScriptingOptions));
var bDDL = Zip(sDDL);
r["ObjectName"] = s.Name;
r["SchemaName"] = s.Schema;
r["ObjectType"] = "SN";
r["object_id"] = s.ID;
r["DDL"] = bDDL;
r["DDLHash"] = ComputeHash(bDDL);
r["ObjectDateCreated"] = s.CreateDate;
r["ObjectDateModified"] = s.DateLastModified;
dtSchema.Rows.Add(r);
try
{
var r = dtSchema.NewRow();
var sDDL = StringCollectionToString(s.Script(ScriptingOptions));
var bDDL = Zip(sDDL);
r["ObjectName"] = s.Name;
r["SchemaName"] = s.Schema;
r["ObjectType"] = "SN";
r["object_id"] = s.ID;
r["DDL"] = bDDL;
r["DDLHash"] = ComputeHash(bDDL);
r["ObjectDateCreated"] = s.CreateDate;
r["ObjectDateModified"] = s.DateLastModified;
dtSchema.Rows.Add(r);
}
catch (Exception ex)
{
logger.LogError(ex, ErrorSource, db.Name);
}
}
}

Expand Down Expand Up @@ -1012,19 +1021,30 @@ public static async Task GenerateSchemaSnapshots(CollectionConfig Config, DBADas
if (!include) continue;

Log.Information("DB Snapshot {db} from instance {instance}", db.Name, builder.DataSource);

DataTable dt = null;
try
{
var dt = ss.SnapshotDB(db.Name);
dt = ss.SnapshotDB(db.Name, collector);
dsSnapshot.Tables.Add(dt);
}
catch (Exception ex)
{
collector.LogError(ex, "Error creating schema snapshot", db.Name);
}
try
{
var fileName = DBADashSource.GenerateFileName(src.SourceConnection.ConnectionForFileName);
await DestinationHandling.WriteAllDestinations(dsSnapshot, src, fileName, Config);
dsSnapshot.Tables.Remove(dt);
}
catch (Exception ex)
{
Log.Error(ex, "Error creating schema snapshot {db}", db.Name);
Log.Error(ex, "Error writing schema snapshot {db} to destination", db.Name);
}
if (dt != null)
{
dsSnapshot.Tables.Remove(dt);
}
collector.ClearErrors();
}
}
}
Expand Down

0 comments on commit df327fe

Please sign in to comment.