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

@@ -5,22 +5,24 @@ using System.Windows.Forms;
namespace DbToolsTester.Forms {
public partial class MainForm : DevExpress.XtraEditors.XtraForm {
public static MainForm Instance { get; private set; }
public string DatabaseName { get; set; }
public string Database1File { get; set; }
public string Database2File { get; set; }
public MainForm() {
InitializeComponent();
Instance = this;
}
private void XtraTabControl1_CustomHeaderButtonClick(object sender, DevExpress.XtraTab.ViewInfo.CustomHeaderButtonEventArgs e) {
string tag = e.Button.Tag?.ToString();
if (tag == "open") {
Database1File = getDatabaseFile("Select Original / Production Database...");
Database2File = getDatabaseFile("Select Template / New Schema Database...");
DatabaseName = Path.GetFileNameWithoutExtension(Database1File);
Text += " [ Production DB = " + Path.GetFileName(Database1File) + "; Schema DB = " + Path.GetFileName(Database2File) + " ]";
OpenForm frm = new OpenForm();
if (frm.ShowDialog(this) == DialogResult.OK) {
// Databases are set in OpenForm
SetDatabases(frm.Db1Path, frm.Db2Path);
}
} else if (tag == "clear") {
Database1File = null;
Database2File = null;
@@ -33,6 +35,13 @@ namespace DbToolsTester.Forms {
}
}
public void SetDatabases(string db1File, string db2File) {
Database1File = db1File;
Database2File = db2File;
DatabaseName = Path.GetFileNameWithoutExtension(Database1File);
Text += " [ Production DB = " + Path.GetFileName(Database1File) + "; Schema DB = " + Path.GetFileName(Database2File) + " ]";
}
private string getDatabaseFile(string title = "Select Database File...") {
OpenFileDialog dlg = new OpenFileDialog() {
Filter = "Database Files (*.sql;*.sqlite;*.db)|*.sql;*.sqlite;*.db|SQL Files (*.sql)|*.sql|Sqlite Files (*.sqlite;*.db)|*.sqlite;*.db|Text Files (*.txt)|*.txt|All Files (*.*)|*.*",
@@ -52,9 +61,25 @@ namespace DbToolsTester.Forms {
SQLiteConnection db2 = new SQLiteConnection("Data Source=" + Database2File + ";Version=3;");
Delta delta = new Delta();
deltaSql.Text = delta.BuildDelta(db1, db2, true, true);
deltaSql.Text = delta.BuildDelta(db1, db2, btsRemoveUnusedTables.Checked, btsRemoveUnusedColumns.Checked);
db1Sql.Text = delta.Db1Sql;
db2Sql.Text = delta.Db2Sql;
} else if (e.Item.Equals(bbiApplyBoth)) {
if (string.IsNullOrEmpty(deltaSql.Text)) {
MessageBox.Show("No delta SQL to apply.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
using (var db1 = new SQLiteConnection("Data Source=" + Database1File + ";Version=3;")) {
db1.Open();
Delta delta = new Delta();
bool success = delta.ApplyDelta(db1, deltaSql.Text);
if (success) {
MessageBox.Show("Delta applied successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
} else {
MessageBox.Show("Failed to apply delta." + (delta.LastError != null ? "\r\n\r\n" + delta.LastError.Message : ""), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}