DB operations now occur w/in transactions, added Open DB Form to tester

This commit is contained in:
2025-09-01 08:27:18 -05:00
parent b0cb847d23
commit 12386fe11d
10 changed files with 598 additions and 175 deletions

View File

@@ -1,4 +1,5 @@
using DbTools.Model;
using System;
using System.Data;
using System.Linq;
using System.Text;
@@ -8,6 +9,7 @@ namespace DbTools {
public class Delta {
public string Db1Sql { get; set; }
public string Db2Sql { get; set; }
public Exception LastError { get; private set; }
public string BuildDelta(IDbConnection currentDb, IDbConnection newDb, bool removeUnusedTables = false, bool removeUnusedColumns = false, bool removeUnusedTriggers = true, bool removeUnusedIndexes = true) {
Database db1 = new Database(currentDb, true);
@@ -44,9 +46,20 @@ namespace DbTools {
public bool ApplyDelta(IDbConnection cn, string sql) {
bool success = false;
using (var cmd = cn.CreateCommand()) {
cmd.CommandText = sql;
success = cmd.ExecuteNonQuery() > 0;
using (var transaction = cn.BeginTransaction()) {
try {
using (var cmd = cn.CreateCommand()) {
cmd.Transaction = transaction;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
transaction.Commit();
success = true;
} catch (Exception ex) {
transaction.Rollback();
success = false;
LastError = ex;
}
}
return success;
}
@@ -70,6 +83,8 @@ namespace DbTools {
var table1 = db1.Tables[table2.TableName];
if (table1 == null) {
sb.AppendLine(table2.FullSql());
sb.AppendLine("-- TODO : Insert records from existing database");
sb.AppendLine("");
} else {
// Remove unused triggers if requested
if (removeUnusedTriggers) {
@@ -118,6 +133,10 @@ namespace DbTools {
}
}
}
if (string.IsNullOrWhiteSpace(sb.ToString().Trim())) {
sb.AppendLine("-- BOTH DATABASES ARE EQUAL - NO CHANGES ARE REQUIRED --");
}
return sb.ToString();
}