From cfb92ea5e80b1af847829a76ab3c9ed7f52ae2c4 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Wed, 29 Nov 2017 20:37:05 +0200 Subject: Fixed an issue with boolean values on local DB sync. Added support for SYNC_CONFIGURATION table on for local DB sync. Fixed an issue with multiple row updates on remote DB sync. Added warning for missing destination property on remote DB sync. Throw exception when no remote machine found on remote DB sync. --- .../Tango.Synchronization/IDBComparer.cs | 13 +++++ .../Tango.Synchronization/Local/Constants.cs | 3 ++ .../Tango.Synchronization/Local/ILocalDataBase.cs | 12 +++++ .../Tango.Synchronization/Local/LocalDBComparer.cs | 32 +++++++++++- .../Tango.Synchronization/Local/SqliteDataBase.cs | 58 ++++++++++++++++++++++ .../Remote/RemoteDBComparer.cs | 53 ++++++++++++++------ .../Remote/SyncConfiguration.cs | 14 ------ .../Tango.Synchronization/SyncConfiguration.cs | 14 ++++++ .../Tango.Synchronization.csproj | 3 +- 9 files changed, 169 insertions(+), 33 deletions(-) create mode 100644 Software/Visual_Studio/Tango.Synchronization/IDBComparer.cs delete mode 100644 Software/Visual_Studio/Tango.Synchronization/Remote/SyncConfiguration.cs create mode 100644 Software/Visual_Studio/Tango.Synchronization/SyncConfiguration.cs (limited to 'Software/Visual_Studio/Tango.Synchronization') diff --git a/Software/Visual_Studio/Tango.Synchronization/IDBComparer.cs b/Software/Visual_Studio/Tango.Synchronization/IDBComparer.cs new file mode 100644 index 000000000..7d7ae4af9 --- /dev/null +++ b/Software/Visual_Studio/Tango.Synchronization/IDBComparer.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Synchronization +{ + public interface IDBComparer : IDisposable + { + List Compare(); + } +} diff --git a/Software/Visual_Studio/Tango.Synchronization/Local/Constants.cs b/Software/Visual_Studio/Tango.Synchronization/Local/Constants.cs index ed8084301..5557c43c8 100644 --- a/Software/Visual_Studio/Tango.Synchronization/Local/Constants.cs +++ b/Software/Visual_Studio/Tango.Synchronization/Local/Constants.cs @@ -14,6 +14,9 @@ namespace Tango.Synchronization.Local public const String COLUMN_TYPE = "type"; public const String IS_NOT_NULL = "notnull"; public const String DEFAULT_VALUE = "dflt_value"; + public const String SYNC_CONFIGURATIONS_TABLE_NAME = "SYNC_CONFIGURATION"; + public const String SYNC_CONFIGURATION_TABLE_NAME_COLUMN = "TABLE_NAME"; + public const String SYNC_CONFIGURATION_SYNC_TYPE_COLUMN = "SYNC_TYPE"; //Must Column Names. diff --git a/Software/Visual_Studio/Tango.Synchronization/Local/ILocalDataBase.cs b/Software/Visual_Studio/Tango.Synchronization/Local/ILocalDataBase.cs index 7e53aeb1c..66b111467 100644 --- a/Software/Visual_Studio/Tango.Synchronization/Local/ILocalDataBase.cs +++ b/Software/Visual_Studio/Tango.Synchronization/Local/ILocalDataBase.cs @@ -36,6 +36,18 @@ namespace Tango.Synchronization.Local /// DataTable CloneTableFrom(ILocalDataBase otherDB, DataTable otherTable); + /// + /// Replaces the table data with the data from another table. + /// + /// The other table. + void ReplaceTableData(ILocalDataBase otherDB, DataTable otherTable); + + /// + /// Replaces the table data with the data from another table. + /// + /// The other table. + String GetReplaceTableDataCommand(ILocalDataBase otherDB, DataTable otherTable); + /// /// Gets the SQL command for . /// diff --git a/Software/Visual_Studio/Tango.Synchronization/Local/LocalDBComparer.cs b/Software/Visual_Studio/Tango.Synchronization/Local/LocalDBComparer.cs index 63f2ecfaa..7883f9cc8 100644 --- a/Software/Visual_Studio/Tango.Synchronization/Local/LocalDBComparer.cs +++ b/Software/Visual_Studio/Tango.Synchronization/Local/LocalDBComparer.cs @@ -13,7 +13,7 @@ namespace Tango.Synchronization.Local /// Represents an synchronization engine. /// /// - public class LocalDBComparer : IDisposable + public class LocalDBComparer : IDBComparer { /// /// Gets the master SQL. @@ -52,7 +52,35 @@ namespace Tango.Synchronization.Local List diffs = new List(); - foreach (var masterTable in MasterSQL.Tables) + DataTable sync_table = MasterSQL.Tables.Single(x => x.TableName == Constants.SYNC_CONFIGURATIONS_TABLE_NAME); + + List sync_tables = new List(); + List overwrite_tables = new List(); + + foreach (var row in sync_table.AsEnumerable()) + { + if ((SyncConfiguration)row.Field(Constants.SYNC_CONFIGURATION_SYNC_TYPE_COLUMN) == SyncConfiguration.Synchronize) + { + sync_tables.Add(MasterSQL.Tables.Single(x => x.TableName == row.Field(Constants.SYNC_CONFIGURATION_TABLE_NAME_COLUMN))); + } + else + { + overwrite_tables.Add(MasterSQL.Tables.Single(x => x.TableName == row.Field(Constants.SYNC_CONFIGURATION_TABLE_NAME_COLUMN))); + } + } + + foreach (var masterTable in overwrite_tables) + { + LogManager.Log("Generating table overwrite difference for table " + masterTable.TableName); + diffs.Add(new Diff(DiffAction.ReplaceTableDataInSlave, "Replace all rows on slave table " + masterTable.TableName, () => + { + + SlaveSQL.ReplaceTableData(MasterSQL, masterTable); + + }, SlaveSQL.GetReplaceTableDataCommand(MasterSQL, masterTable))); + } + + foreach (var masterTable in sync_tables) { LogManager.Log("Comparing table " + masterTable.TableName); LogManager.Log("Searching table " + masterTable.TableName + " on slave..."); diff --git a/Software/Visual_Studio/Tango.Synchronization/Local/SqliteDataBase.cs b/Software/Visual_Studio/Tango.Synchronization/Local/SqliteDataBase.cs index 35e54c66a..83a14b493 100644 --- a/Software/Visual_Studio/Tango.Synchronization/Local/SqliteDataBase.cs +++ b/Software/Visual_Studio/Tango.Synchronization/Local/SqliteDataBase.cs @@ -147,6 +147,44 @@ namespace Tango.Synchronization.Local return table; } + /// + /// Replaces the table data with the data from another table. + /// + /// The other table. + public void ReplaceTableData(ILocalDataBase otherDB, DataTable otherTable) + { + var dropCommand = _connection.CreateCommand(); + dropCommand.CommandText = String.Format("DELETE FROM {0};", otherTable.TableName); + dropCommand.ExecuteNonQuery(); + + var attacheCommand = _connection.CreateCommand(); + attacheCommand.CommandText = String.Format("ATTACH DATABASE '{0}' AS other;", otherDB.Source); + attacheCommand.ExecuteNonQuery(); + + var copyCommand = _connection.CreateCommand(); + copyCommand.CommandText = String.Format("INSERT INTO main.{0} SELECT * FROM {1}.{0};", otherTable.TableName, "other"); + copyCommand.ExecuteNonQuery(); + + var detacheCommand = _connection.CreateCommand(); + detacheCommand.CommandText = "DETACH other;"; + detacheCommand.ExecuteNonQuery(); + } + + /// + /// Gets the SQL command for . + /// + /// + /// The other table. + /// + public string GetReplaceTableDataCommand(ILocalDataBase otherDB, DataTable otherTable) + { + String cmd = String.Format("DELETE FROM {0};", otherTable.TableName) + Environment.NewLine; + cmd += String.Format("ATTACH DATABASE '{0}' AS other;", otherDB.Source) + Environment.NewLine; + cmd += String.Format("INSERT INTO main.{0} SELECT * FROM {1}.{0};", otherTable.TableName, "other") + Environment.NewLine; + cmd += "DETACH other;" + Environment.NewLine; + return cmd; + } + /// /// Gets the SQL command for . /// @@ -252,6 +290,11 @@ namespace Tango.Synchronization.Local double num = 0; + if (row.ItemArray[i].GetType() == typeof(bool)) + { + value = ((bool)row.ItemArray[i]) ? 1.ToString() : 0.ToString(); + } + if (row.ItemArray[i].GetType() == typeof(DateTime)) { value = ((DateTime)row.ItemArray[i]).ToSQLiteDateString(); @@ -288,6 +331,11 @@ namespace Tango.Synchronization.Local double num = 0; + if (row.ItemArray[i].GetType() == typeof(bool)) + { + value = ((bool)row.ItemArray[i]) ? 1.ToString() : 0.ToString(); + } + if (row.ItemArray[i].GetType() == typeof(DateTime)) { value = ((DateTime)row.ItemArray[i]).ToSQLiteDateString(); @@ -322,6 +370,11 @@ namespace Tango.Synchronization.Local double num = 0; + if (row.ItemArray[i].GetType() == typeof(bool)) + { + value = ((bool)row.ItemArray[i]) ? 1.ToString() : 0.ToString(); + } + if (row.ItemArray[i].GetType() == typeof(DateTime)) { value = ((DateTime)row.ItemArray[i]).ToSQLiteDateString(); @@ -382,6 +435,11 @@ namespace Tango.Synchronization.Local double num = 0; + if (row.ItemArray[i].GetType() == typeof(bool)) + { + value = ((bool)row.ItemArray[i]) ? 1.ToString() : 0.ToString(); + } + if (row.ItemArray[i].GetType() == typeof(DateTime)) { value = ((DateTime)row.ItemArray[i]).ToSQLiteDateString(); diff --git a/Software/Visual_Studio/Tango.Synchronization/Remote/RemoteDBComparer.cs b/Software/Visual_Studio/Tango.Synchronization/Remote/RemoteDBComparer.cs index 49555f75c..0bd76dbd1 100644 --- a/Software/Visual_Studio/Tango.Synchronization/Remote/RemoteDBComparer.cs +++ b/Software/Visual_Studio/Tango.Synchronization/Remote/RemoteDBComparer.cs @@ -11,7 +11,7 @@ using System.Collections; namespace Tango.Synchronization.Remote { - public class RemoteDBComparer : IDisposable + public class RemoteDBComparer : IDBComparer { private remote.RemoteDB _remoteDB; private local.LocalDB _localDB; @@ -65,6 +65,11 @@ namespace Tango.Synchronization.Remote LogManager.Log("Querying all remote machines..."); var remote_machines = _remoteDB.MACHINES.Where(x => x.SERIAL_NUMBER == _machineSerial).ToList(); + if (remote_machines.Count == 0) + { + throw LogManager.Log(new DataBaseComparisonException("Could not locate machine on remote server " + _machineSerial)); + } + LogManager.Log("Querying all remote organizations..."); guids = remote_machines.Select(x => x.ORGANIZATION_GUID).ToList(); var remote_organizations = _remoteDB.ORGANIZATIONS.Where(x => guids.Contains(x.GUID)).ToList(); @@ -114,7 +119,7 @@ namespace Tango.Synchronization.Remote } - LogManager.Log("Comparing addresses tables"); + LogManager.Log("Comparing addresses"); CompareCollections(remote_addresses, local_addresses, _remoteDB.ADDRESSES, _localDB.ADDRESSES); LogManager.Log("Comparing cartridges"); @@ -150,6 +155,8 @@ namespace Tango.Synchronization.Remote LogManager.Log("Comparing users roles"); CompareCollections(remote_users_roles, local_users_roles, _remoteDB.USERS_ROLES, _localDB.USERS_ROLES); + LogManager.Log("Comparison done!"); + return _diffs; } @@ -163,6 +170,12 @@ namespace Tango.Synchronization.Remote { foreach (var prop in source.GetType().GetProperties().Where(x => x.PropertyType.IsPrimitive || x.PropertyType == typeof(String) || x.PropertyType == typeof(DateTime))) { + if (destination.GetType().GetProperty(prop.Name) == null) + { + LogManager.Log(String.Format("Warning: property {0} not found on destination entity {1}!", prop.Name, destination.GetType().Name)); + continue; + } + if (prop.PropertyType == typeof(Int64)) { destination.GetType().GetProperty(prop.Name).SetValue(destination, Convert.ToInt32((Int64)prop.GetValue(source))); @@ -179,25 +192,25 @@ namespace Tango.Synchronization.Remote var slaveProp = typeof(Slave).GetProperty("GUID"); var masterProp = typeof(Master).GetProperty("GUID"); + List compared = new List(); + foreach (var masterRow in masterCollection) { - CompareEntities(masterRow, slaveCollection.SingleOrDefault(x => slaveProp.GetValue(x).ToString() == masterProp.GetValue(masterRow).ToString()), masterSet, slaveSet); + Slave slaveRow = slaveCollection.SingleOrDefault(x => slaveProp.GetValue(x).ToString() == masterProp.GetValue(masterRow).ToString()); + CompareEntities(masterRow, slaveRow, masterSet, slaveSet); + + if (slaveRow != null) + { + compared.Add(slaveRow); + } } - foreach (var slaveRow in slaveCollection) + foreach (var slaveRow in slaveCollection.Where(x => !compared.Contains(x))) { CompareEntities(masterCollection.SingleOrDefault(x => masterProp.GetValue(x).ToString() == slaveProp.GetValue(slaveRow).ToString()), slaveRow, masterSet, slaveSet); } } - //private Destination TransferEntity(Source entity, List destinationSet) where Source : class where Destination : class - //{ - // var cloned = Activator.CreateInstance(); - // CopyEntity(entity, cloned); - // destinationSet.Add(cloned); - // return cloned; - //} - private void OverrideTable(remote.SYNC_CONFIGURATION config) { LogManager.Log("Generating table override difference for " + config.TABLE_NAME + "..."); @@ -230,11 +243,19 @@ namespace Tango.Synchronization.Remote if (masterDate > slaveDate) { - diff = new Diff(DiffAction.UpdateRowInSlave, "Update row in slave table " + typeof(Master).Name, () => CopyEntity(master, slave), null); + diff = new Diff(DiffAction.UpdateRowInSlave, "Update row in slave table " + typeof(Master).Name, () => + { + LogManager.Log("Updating row in slave table " + typeof(Master).Name); + CopyEntity(master, slave); + }, null); } else if (slaveDate > masterDate) { - diff = new Diff(DiffAction.UpdateRowInMaster, "Update row in master table " + typeof(Master).Name, () => CopyEntity(slave, master), null); + diff = new Diff(DiffAction.UpdateRowInMaster, "Update row in master table " + typeof(Master).Name, () => + { + LogManager.Log("Updating row in master table " + typeof(Master).Name); + CopyEntity(slave, master); + }, null); } if (diff != null) @@ -249,7 +270,7 @@ namespace Tango.Synchronization.Remote { _diffs.Add(new Diff(DiffAction.AddRowToSlave, "Add row to slave table " + typeof(Master).Name, () => { - + LogManager.Log("Adding row to slave table " + typeof(Master).Name); Slave newRow = slaveSet.Create(); CopyEntity(master, newRow); slaveSet.Add(newRow); @@ -263,7 +284,7 @@ namespace Tango.Synchronization.Remote { _diffs.Add(new Diff(DiffAction.AddRowToMaster, "Add row to master table " + typeof(Master).Name, () => { - + LogManager.Log("Adding row to master table " + typeof(Master).Name); Master newRow = masterSet.Create(); CopyEntity(slave, newRow); masterSet.Add(newRow); diff --git a/Software/Visual_Studio/Tango.Synchronization/Remote/SyncConfiguration.cs b/Software/Visual_Studio/Tango.Synchronization/Remote/SyncConfiguration.cs deleted file mode 100644 index 7e5a60f07..000000000 --- a/Software/Visual_Studio/Tango.Synchronization/Remote/SyncConfiguration.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Tango.Synchronization.Remote -{ - public enum SyncConfiguration - { - OverwriteLocal, - Synchronize, - } -} diff --git a/Software/Visual_Studio/Tango.Synchronization/SyncConfiguration.cs b/Software/Visual_Studio/Tango.Synchronization/SyncConfiguration.cs new file mode 100644 index 000000000..6082db15b --- /dev/null +++ b/Software/Visual_Studio/Tango.Synchronization/SyncConfiguration.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Synchronization +{ + public enum SyncConfiguration + { + OverwriteLocal, + Synchronize, + } +} diff --git a/Software/Visual_Studio/Tango.Synchronization/Tango.Synchronization.csproj b/Software/Visual_Studio/Tango.Synchronization/Tango.Synchronization.csproj index 97227a183..36719516a 100644 --- a/Software/Visual_Studio/Tango.Synchronization/Tango.Synchronization.csproj +++ b/Software/Visual_Studio/Tango.Synchronization/Tango.Synchronization.csproj @@ -65,6 +65,7 @@ GlobalVersionInfo.cs + @@ -74,7 +75,7 @@ - + -- cgit v1.3.1