aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Synchronization
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2017-11-26 17:23:38 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2017-11-26 17:23:38 +0200
commit262c1efc9d53b8b70fe677dc7d2d173896ffcee8 (patch)
treee404dc914bee89101f50963ff018225c897244fd /Software/Visual_Studio/Tango.Synchronization
parent0a8af16821cfbb52fe02881921695d378f6578d7 (diff)
downloadTango-262c1efc9d53b8b70fe677dc7d2d173896ffcee8.tar.gz
Tango-262c1efc9d53b8b70fe677dc7d2d173896ffcee8.zip
Implemented Tango SQLite DB Synchronizer.
Diffstat (limited to 'Software/Visual_Studio/Tango.Synchronization')
-rw-r--r--Software/Visual_Studio/Tango.Synchronization/ISqlDataBase.cs5
-rw-r--r--Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs55
-rw-r--r--Software/Visual_Studio/Tango.Synchronization/SqliteDataBase.cs6
-rw-r--r--Software/Visual_Studio/Tango.Synchronization/Tango.Synchronization.csproj6
4 files changed, 68 insertions, 4 deletions
diff --git a/Software/Visual_Studio/Tango.Synchronization/ISqlDataBase.cs b/Software/Visual_Studio/Tango.Synchronization/ISqlDataBase.cs
index f087dfd15..fa4e018be 100644
--- a/Software/Visual_Studio/Tango.Synchronization/ISqlDataBase.cs
+++ b/Software/Visual_Studio/Tango.Synchronization/ISqlDataBase.cs
@@ -24,6 +24,11 @@ namespace Tango.Synchronization
List<DataTable> Tables { get; }
/// <summary>
+ /// Loads the tables (Must be done before any synchronization).
+ /// </summary>
+ void LoadTables();
+
+ /// <summary>
/// Clones a table from another database into this database.
/// </summary>
/// <param name="otherDB">The other database.</param>
diff --git a/Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs b/Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs
index 07c94045b..980a23f1e 100644
--- a/Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs
+++ b/Software/Visual_Studio/Tango.Synchronization/SqlDataBaseComparer.cs
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Data;
+using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using Tango.Logging;
namespace Tango.Synchronization
{
@@ -40,28 +42,47 @@ namespace Tango.Synchronization
/// <returns></returns>
public List<SqlDiff> Compare()
{
+ LogManager.Log("Comparing databases " + Path.GetFileName(MasterSQL.Source) + " <=> " + Path.GetFileName(SlaveSQL.Source));
+
+ LogManager.Log("Loading master tables...");
+ MasterSQL.LoadTables();
+
+ LogManager.Log("Loading slave tables...");
+ SlaveSQL.LoadTables();
+
List<SqlDiff> diffs = new List<SqlDiff>();
foreach (var masterTable in MasterSQL.Tables)
{
+ LogManager.Log("Comparing table " + masterTable.TableName);
+ LogManager.Log("Searching table " + masterTable.TableName + " on slave...");
+
var slaveTable = SlaveSQL.Tables.SingleOrDefault(x => x.TableName == masterTable.TableName); //Get matching slave table on slave db.
if (slaveTable == null) //Table not found on slave.
{
+ LogManager.Log("Table not found on slave, adding difference.");
//Clone table from slave db to master db including records!
diffs.Add(new SqlDiff(SqlDiffAction.AddTableToSlave, String.Format("Add table {0} to slave", masterTable.TableName), () => SlaveSQL.CloneTableFrom(MasterSQL, masterTable), SlaveSQL.GetCloneTableFromCommand(MasterSQL, masterTable)));
continue;
}
+ LogManager.Log("Table found, comparing columns...");
+
foreach (DataColumn masterColumn in masterTable.Columns)
{
+ LogManager.Log("Searching for column " + masterColumn.ColumnName + " on slave...");
+
var slaveColumn = slaveTable.Columns[masterColumn.ColumnName]; //Get matching slave column on slave table.
if (slaveColumn == null) //Slave column not found.
{
+ LogManager.Log("Column not found on slave, adding difference.");
//Add column to slave table.
diffs.Add(new SqlDiff(SqlDiffAction.AddColumnToSlave, String.Format("Add column {0} to slave table", masterColumn.ColumnName), () => SlaveSQL.AddColumn(masterTable, masterColumn), SlaveSQL.GetAddColumnCommand(masterTable, masterColumn)));
}
+
+ LogManager.Log("Column found.");
}
List<DataRow> addToSlave = new List<DataRow>();
@@ -69,46 +90,76 @@ namespace Tango.Synchronization
List<DataRow> addToMaster = new List<DataRow>();
List<DataRow> updateMaster = new List<DataRow>();
+ LogManager.Log("Comparing rows...");
+
+ int count = 0;
+
foreach (DataRow masterRow in masterTable.Rows)
{
+ LogManager.Log("Comparing row " + count++);
+
String guid = masterRow.Field<String>(Constants.GUID);
+ LogManager.Log("Searching for row with GUID " + guid + " on slave table...");
+
//Get Matching slave row.
DataRow slaveRow = slaveTable.AsEnumerable().SingleOrDefault(x => x.Field<String>(Constants.GUID) == guid);
if (slaveRow != null)
{
+ LogManager.Log("Slave row found, comparing dates...");
+
DateTime masterDate = masterRow.Field<DateTime>(Constants.LAST_UPDATED);
DateTime slaveDate = slaveRow.Field<DateTime>(Constants.LAST_UPDATED);
if (masterDate > slaveDate)
{
+ LogManager.Log("Master => Slave Update " + masterDate.ToSQLiteDateString() + ", adding difference.");
updateSlave.Add(masterRow);
}
else if (slaveDate > masterDate)
{
+ LogManager.Log("Master <= Slave Update " + masterDate.ToSQLiteDateString() + ", adding difference.");
updateMaster.Add(slaveRow);
}
+ else
+ {
+ LogManager.Log("Master <=> Slave No Update.");
+ }
}
else
{
+ LogManager.Log("Slave row not found, adding difference.");
addToSlave.Add(masterRow);
}
}
+ LogManager.Log("Done comparing rows...");
+
+ LogManager.Log("Searching for missing rows on master...");
+
foreach (DataRow slaveRow in slaveTable.Rows)
{
String guid = slaveRow.Field<String>(Constants.GUID);
+ LogManager.Log("Searching for row with GUID " + guid + " on master table...");
+
//Get Matching slave row.
DataRow masterRow = masterTable.AsEnumerable().SingleOrDefault(x => x.Field<String>(Constants.GUID) == guid);
if (masterRow == null)
{
+ LogManager.Log("Master row not found, adding difference.");
addToMaster.Add(slaveRow);
}
+ else
+ {
+ LogManager.Log("Master row found.");
+ }
}
+ LogManager.Log("Done searching for missing rows on master...");
+
foreach (var row in addToSlave)
{
diffs.Add(new SqlDiff(SqlDiffAction.AddRowToSlave, String.Format("Add row to slave table {0}", slaveTable.TableName), () => SlaveSQL.AddRow(slaveTable, row), SlaveSQL.GetAddRowCommand(slaveTable, row)));
@@ -128,8 +179,12 @@ namespace Tango.Synchronization
{
diffs.Add(new SqlDiff(SqlDiffAction.UpdateRowInMaster, String.Format("Update row in master table {0}", masterTable.TableName), () => MasterSQL.UpdateRow(masterTable, row), MasterSQL.GetUpdateRowCommand(masterTable, row)));
}
+
+ LogManager.Log("Done comparing table " + masterTable.TableName);
}
+ LogManager.Log("Databases comparison completed.");
+
return diffs;
}
diff --git a/Software/Visual_Studio/Tango.Synchronization/SqliteDataBase.cs b/Software/Visual_Studio/Tango.Synchronization/SqliteDataBase.cs
index 3802232e0..75259fc28 100644
--- a/Software/Visual_Studio/Tango.Synchronization/SqliteDataBase.cs
+++ b/Software/Visual_Studio/Tango.Synchronization/SqliteDataBase.cs
@@ -43,14 +43,12 @@ namespace Tango.Synchronization
));
_connection.Open();
-
- InitializeTables();
}
/// <summary>
- /// Initializes the tables.
+ /// Loads the tables (Must be done before any synchronization).
/// </summary>
- private void InitializeTables()
+ public void LoadTables()
{
Tables = new List<DataTable>();
diff --git a/Software/Visual_Studio/Tango.Synchronization/Tango.Synchronization.csproj b/Software/Visual_Studio/Tango.Synchronization/Tango.Synchronization.csproj
index b702c5335..6b19b8521 100644
--- a/Software/Visual_Studio/Tango.Synchronization/Tango.Synchronization.csproj
+++ b/Software/Visual_Studio/Tango.Synchronization/Tango.Synchronization.csproj
@@ -74,6 +74,12 @@
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Tango.Logging\Tango.Logging.csproj">
+ <Project>{bc932dbd-7cdb-488c-99e4-f02cf441f55e}</Project>
+ <Name>Tango.Logging</Name>
+ </ProjectReference>
+ </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\System.Data.SQLite.Core.1.0.106.0\build\net45\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.106.0\build\net45\System.Data.SQLite.Core.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">