private void GenerateButton_OnClick(object sender, RoutedEventArgs e) { var diffFile = ConstantData.WorkingDir + "\\diff.xml"; var sqlOutput = ConstantData.WorkingDir + "\\output.sql"; var engine = new GenerateScriptService(); var result = engine.GenerateScript( new GenerateScript { CurrentDiffFile = diffFile, MigrateSqlFile = sqlOutput }); if (result.Any(x => x.Mode == ViewMode.Add && x.ObjectType == SQLObject.Column && x.SQLScript.ToUpper().Contains("NOT NULL") && !x.SQLScript.ToUpper().Contains("DEFAULT"))) { var database = CompareSchemaService.LoadXMLFile(diffFile); var count = database.Update?.Tables? .Where(v => v.Add?.Columns != null) .SelectMany(x => x.Add.Columns) .Count(c => c.IS_NULLABLE == "NO" && c.COLUMN_DEFAULT.IsEmpty()); if (count > 0) { var needToSave = false; var tables = database.Update?.Tables?.Where(v => v.Add?.Columns != null).ToList(); foreach (var table in tables) { foreach (var column in table.Add.Columns.Where(x => x.IS_NULLABLE == "NO" && x.COLUMN_DEFAULT.IsEmpty())) { var description = $"Column {column.Name} on table {table.Name} need default value"; var defaultWindow = new SetDefaultValueWindow(description); if (defaultWindow.ShowDialog() == true) { needToSave = true; column.COLUMN_DEFAULT = defaultWindow.DefaultValue; } } } if (needToSave) { ExtractSchemaService.SaveToFile(database, "diff.xml"); engine = new GenerateScriptService(); engine.GenerateScript( new GenerateScript { CurrentDiffFile = diffFile, MigrateSqlFile = sqlOutput }); } } } Process.Start(sqlOutput); }
private void ActuallyRename_OnClick(object sender, RoutedEventArgs e) { var database = CompareSchemaService.LoadXMLFile(ConstantData.WorkingDir + "\\diff.xml"); var table = database.Update?.Tables?.FirstOrDefault(x => string.Equals(x.FullName, SelectedAddOrUpdate.FullTableName, StringComparison.OrdinalIgnoreCase)); if (table == null) { return; } var newSchema = table.Add.Columns.FirstOrDefault(x => x.Name == SelectedAddOrUpdate.ObjectName); var oldSchema = table.Remove.Columns.FirstOrDefault(x => x.Name == SelectedRemove.ObjectName); if (oldSchema == null || newSchema == null) { return; } newSchema.SetName = newSchema.Name; newSchema.COLUMN_NAME = oldSchema.Name; table.Add.Columns.Remove(newSchema); table.Remove.Columns.Remove(oldSchema); if (table.Update == null) { table.Update = new Model.Schema.Table { Columns = new List <Model.Schema.Column> { newSchema } } } ; else if (table.Update.Columns == null) { table.Update.Columns = new List <Model.Schema.Column> { newSchema } } ; else { table.Update.Columns.Add(newSchema); } ExtractSchemaService.SaveToFile(database, "diff.xml"); GenerateSqlFileAndShowUpdates(); SelectedAddOrUpdate = null; SelectedRemove = null; ((Button)sender).IsEnabled = false; }
static void Main(string[] args) { var argList = args.ToList(); if (argList.Any()) { var first = args.First(); if (first.HasAny()) { if (first.ToLower() == "extract-schema") { var model = IsArgumentExtractSchemaValid(argList); if (model.IsValid) { using (var service = new ExtractSchemaService(model)) service.ExtractSchema(Model.CompareType.Schema); } } else if (first.ToLower() == "generate-diff") { var model = IsArgumentGenerateDiffFileValid(argList); if (model.IsValid) { using (var service = new CompareSchemaService()) service.StartCompare(model); } } else if (first.ToLower() == "generate-script") { var model = IsArgumentGenerateScriptValid(argList); if (model.IsValid) { new GenerateScriptService().GenerateScript(model); } } else if (first.ToLower() == "rename") { var model = IsArgumentTransformationValid(argList); if (model.IsValid) { CompareSchemaService.TransformationDiffFile(model); } } } } }
public void ExtractSchema() { var sourceModel = new ExtractSchema { ConnectionString = "Data Source=.\\SQL2016;Initial Catalog=Source;Integrated Security=True;Connect Timeout=30", OutputFile = "Source.xml" }; using (var service = new ExtractSchemaService(sourceModel)) service.ExtractSchema(Model.CompareType.Schema); var targetModel = new ExtractSchema { ConnectionString = "Data Source=.\\SQL2016;Initial Catalog=Target;Integrated Security=True;Connect Timeout=30", OutputFile = "Target.xml" }; using (var service = new ExtractSchemaService(targetModel)) service.ExtractSchema(Model.CompareType.Schema); }
private void CompareButton_Click(object sender, RoutedEventArgs e) { EnableOrDisableButton(false); var type = ((Button)sender).Tag.ToString() == "Schema" ? CompareType.Schema : CompareType.Data; Task.Factory.StartNew(() => { try { UpdateState($"Extracting {SourceName} Schema..."); using (var service = new ExtractSchemaService(new ExtractSchema { ConnectionString = SourceConnection, OutputFile = "Source.xml" })) service.ExtractSchema(type); UpdateState($"Extracted {SourceName} Schema."); UpdateState($"Extracting {TargetName} Schema..."); using (var service = new ExtractSchemaService(new ExtractSchema { ConnectionString = TargetConnection, OutputFile = "Target.xml" })) service.ExtractSchema(type); UpdateState($"Extracted {TargetName} Schema."); UpdateState("Comparing Databases..."); using (var service = new CompareSchemaService()) service.StartCompare(new GenerateDiffFile { SourceSchemaFile = ConstantData.WorkingDir + "Source.xml", TargetSchemaFile = ConstantData.WorkingDir + "Target.xml", OutputFile = ConstantData.WorkingDir + "\\diff.xml", CompareType = type }); UpdateState("Databases Compared."); GenerateSqlFileAndShowUpdates(); } catch (Exception ex) { Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { EnableOrDisableButton(true); UpdateState("has a error. refer to log path"); })); ShowAndLogError(ex); } }); }