/// <summary> /// Merge the user configuration with the default metadatas from the servers. /// </summary> /// <param name="schemaMetadata">Metadatas to generate queries for.</param> /// <param name="dbSettings">User configuration.</param> /// <param name="variables">The compiled cascade variables</param> private static void MergeFkModifierSchema(SchemaMetadata schemaMetadata, DbSettings dbSettings, HashSet <SchemaVar> variables) { foreach (var dbSettingsTable in dbSettings.Tables) { var table = schemaMetadata.FirstOrDefault(t => t.Name.Equals(dbSettingsTable.Name, StringComparison.CurrentCultureIgnoreCase)); if (table == null) { continue; } //On affecte les changements de la configuration //On supprime les clefs foreach (var colConfig in dbSettingsTable.ForeignKeys.ForeignKeyRemove.Columns) { for (var j = 0; j < table.ForeignKeys.Count; j++) { var fk = table.ForeignKeys[j]; for (var i = 0; i < fk.Columns.Count; i++) { if (!fk.Columns[i].NameFrom.Equals(colConfig.Name, StringComparison.OrdinalIgnoreCase)) { continue; } fk.Columns.RemoveAt(i); i--; if (fk.Columns.Count != 0) { continue; } table.ForeignKeys.RemoveAt(j); j--; } } } //On ajoute les clefs foreach (var fkModifier in dbSettingsTable.ForeignKeys.ForeignKeyAdd) { var destinationVar = variables.First(v => v.Id == fkModifier.DestinationSchema); var newFk = new ForeignKey { ServerIdTo = destinationVar.Server, DatabaseTo = destinationVar.Database, SchemaTo = destinationVar.Schema, TableTo = fkModifier.DestinationTable, Columns = (from fk in fkModifier.Columns select new ForeignKeyColumn { NameFrom = fk.Source, NameTo = fk.Destination }).ToList() }; table.ForeignKeys.Add(newFk); } } }
private static bool MergeSchema(SchemaModifierModel mergedSchema, List <SchemaModifier> userConfigSchemas, SchemaMetadata defaultSchema) { var hasChange = false; var userConfigSchema = userConfigSchemas.FirstOrDefault(s => s.Name == mergedSchema.Name); if (defaultSchema == null) { hasChange = true; } //Add new if (userConfigSchema == null) { userConfigSchema = new SchemaModifier { Name = mergedSchema.Name }; userConfigSchemas.Add(userConfigSchema); } //Apply changes userConfigSchema.TemplateId = mergedSchema.TemplateId; userConfigSchema.BasedOn = mergedSchema.UseTemplateId; userConfigSchema.Description = mergedSchema.Description; if (mergedSchema.TemplateId != 0 || mergedSchema.UseTemplateId != 0 || !String.IsNullOrEmpty(mergedSchema.Description)) { hasChange = true; } if (mergedSchema.Tables != null) { foreach (var mergedTable in mergedSchema.Tables) { var defaultTable = defaultSchema.FirstOrDefault(t => t.Name == mergedTable.Name); if (MergeTable(mergedTable, userConfigSchema.Tables, defaultTable)) { hasChange = true; } } } //If no change has been detected with the default config if (!hasChange) { userConfigSchemas.Remove(userConfigSchema); } return(hasChange); }