public ConfigurationTests() { _proj = new ProjectContainer { Name = "MainApp" }; _proj.ConnectionStrings.Add(new Connection(1, "PROD", "DataCloner.Data.QueryProviderMySql", "server=localhost;user id=root; password=cdxsza; database=mysql; pooling=false")); _proj.ConnectionStrings.Add(new Connection(2, "UNI", "DataCloner.Data.QueryProviderMySql", "server=localhost;user id=root; password=cdxsza; database=mysql; pooling=false")); var table1 = new TableModifier { Name = "table1", IsStatic = false, DataBuilders = new List <DataBuilder> { new DataBuilder { BuilderName = "Client.Builder.CreatePK", Name = "col1" } }, DerativeTables = new DerativeTable { GlobalAccess = DerivativeTableAccess.Forced, GlobalCascade = true, DerativeSubTables = new List <DerivativeSubTable> { new DerivativeSubTable { ServerId = "1", Database = "db", Schema = "dbo", Table = "table2", Access = DerivativeTableAccess.Denied } } } }; table1.ForeignKeys.ForeignKeyAdd.Add(new ForeignKeyAdd { ServerId = "1", Database = "db", Schema = "dbo", Table = "table55", Columns = new List <ForeignKeyColumn> { new ForeignKeyColumn { NameFrom = "col1", NameTo = "col1" }, new ForeignKeyColumn { NameFrom = "col2", NameTo = "col2" } } }); table1.ForeignKeys.ForeignKeyRemove = new ForeignKeyRemove { Columns = new List <ForeignKeyRemoveColumn> { new ForeignKeyRemoveColumn { Name = "col3" }, new ForeignKeyRemoveColumn { Name = "col4" } } }; var server1 = new ServerModifier { Id = "1", Databases = new List <DatabaseModifier> { new DatabaseModifier { Name = "db", Schemas = new List <SchemaModifier> { new SchemaModifier { Name = "dbo", Tables = new List <TableModifier> { table1 } } } } } }; var clonerBehaviour = new Behaviour { Id = 1, Name = "Basic clone", Description = "Only cloning besic data", //Servers = new List<ServerModifier> { server1 } }; _proj.Behaviours.Add(clonerBehaviour); _proj.Maps = new List <Map> { new Map { From = "UNI", To = "FON", UsableBehaviours = "1,2", Variables = new List <Variable> { new Variable { Name = "", Value = "" } }, Roads = new List <Road> { new Road { ServerSrc = "1", SchemaSrc = "dbo", DatabaseSrc = "myDB", ServerDst = "1", SchemaDst = "dbo", DatabaseDst = "myDB" } } } }; }
private static bool MergeTable(TableModifierModel mergedTable, List <TableModifier> userConfigTables, TableMetadata defaultTable) { var hasChange = false; var userConfigTable = userConfigTables.FirstOrDefault(t => t.Name == mergedTable.Name); if (defaultTable == null) { hasChange = true; } //Add new if (userConfigTable == null) { userConfigTable = new TableModifier { Name = mergedTable.Name }; userConfigTables.Add(userConfigTable); } userConfigTable.IsStatic = mergedTable.IsStatic; userConfigTable.DerativeTables.GlobalAccess = mergedTable.DerativeTablesGlobalAccess; userConfigTable.DerativeTables.GlobalCascade = mergedTable.DerativeTablesGlobalCascade; if (mergedTable.IsStatic || mergedTable.DerativeTablesGlobalAccess != DerivativeTableAccess.NotSet || mergedTable.DerativeTablesGlobalCascade == true) { hasChange = true; } //Clear userConfigTable.ForeignKeys.ForeignKeyAdd = new List <ForeignKeyAdd>(); userConfigTable.ForeignKeys.ForeignKeyRemove = new ForeignKeyRemove(); userConfigTable.DerativeTables.DerativeSubTables = new List <DerivativeSubTable>(); userConfigTable.DataBuilders = new List <DataBuilder>(); //Merge FK for (int i = mergedTable.ForeignKeys.Count - 1; i >= 0; i--) { var mergedFk = mergedTable.ForeignKeys[i]; var defaultFk = defaultTable.ForeignKeys.FirstOrDefault(f => f.ServerIdTo.ToString() == mergedFk.ServerIdTo && f.DatabaseTo == mergedFk.DatabaseTo && f.SchemaTo == mergedFk.SchemaTo && f.TableTo == mergedFk.TableTo); if (MergeForeignKey(mergedFk, mergedTable.ForeignKeys, userConfigTable.ForeignKeys, defaultFk, defaultTable.ColumnsDefinition)) { hasChange = true; } } //Merge derivative table for (int i = mergedTable.DerivativeTables.Count - 1; i >= 0; i--) { var mergedDerivativeTable = mergedTable.DerivativeTables[i]; var defaultDt = defaultTable.DerivativeTables.FirstOrDefault(d => d.ServerId.ToString() == mergedDerivativeTable.ServerId && d.Database == mergedDerivativeTable.Database && d.Schema == mergedDerivativeTable.Schema && d.Table == mergedDerivativeTable.Table); if (MergedDerivativeTable(mergedDerivativeTable, mergedTable.DerivativeTables, userConfigTable.DerativeTables, defaultDt)) { hasChange = true; } } //Merge data builders foreach (var mergedDataBuilder in mergedTable.DataBuilders) { if (MergeDataBuilders(mergedDataBuilder, userConfigTable.DataBuilders)) { hasChange = true; } } //Cleaning if (!userConfigTable.DataBuilders.Any()) { userConfigTable.DataBuilders = null; } if (!userConfigTable.ForeignKeys.ForeignKeyAdd.Any()) { userConfigTable.ForeignKeys.ForeignKeyAdd = null; } if (!userConfigTable.ForeignKeys.ForeignKeyRemove.Columns.Any()) { userConfigTable.ForeignKeys.ForeignKeyRemove = null; } if (userConfigTable.ForeignKeys.ForeignKeyAdd == null && userConfigTable.ForeignKeys.ForeignKeyRemove == null) { userConfigTable.ForeignKeys = null; } if (mergedTable.DerativeTablesGlobalAccess == DerivativeTableAccess.NotSet && mergedTable.DerativeTablesGlobalCascade == false && !userConfigTable.DerativeTables.DerativeSubTables.Any()) { userConfigTable.DerativeTables = null; } //If no change has been detected with the default config if (!hasChange) { userConfigTables.Remove(userConfigTable); } return(hasChange); }