public void TestOnRandomData() { var rowsList1 = new List <List <object> >(); var row1 = new List <object> { 1, "string1", new DateTime(2019, 01, 19) }; rowsList1.Add(row1); var rowsList2 = new List <List <object> >(); var row2 = new List <object> { 2, "string2", new DateTime(2019, 01, 19) }; rowsList2.Add(row1); rowsList2.Add(row2); var dt1 = dataUtility.GetTableWithTestData(typeof(int), new List <Type>() { typeof(string), typeof(DateTime) }, rowsList1); var dt2 = dataUtility.GetTableWithTestData(typeof(int), new List <Type>() { typeof(string), typeof(DateTime) }, rowsList2); var service = new DataDiffService(new QueryFormatter(), new TableSchemaValidatorsComposite(new[] { new SamePrimaryKeysValidator() })); var(sucess, resSql) = service.GetDataDiffSql(dt1, dt2, false); var exp = "INSERT INTO TestTable(Id, ColString, ColDateTime) VALUES (2, 'string2', '2019-01-19 00:00:00');\r\n"; sucess.Should().BeTrue(); resSql.Should().Be(exp); }
public void TestOnRandomData() { var dt1 = dataUtility.GetTableWithRandomTestData(new[] { typeof(string), typeof(DateTime), typeof(double) }, 15); var dt2 = dataUtility.GetTableWithRandomTestData(new[] { typeof(string), typeof(DateTime), typeof(double) }, 10); var service = new DataDiffService(new QueryFormatter(), new TableSchemaValidatorsComposite(new[] { new SamePrimaryKeysValidator() })); var resSql = service.GetDataDiffSql(dt1, dt2); Console.WriteLine(resSql.Item2); }
static void Main(string[] args) { var app = new CommandLineApplication { Description = "SqlDataDiff" }; app.OnExecute(() => { app.ShowHint(); return(0); }); var create = app.Command("difftables", config => { config.Description = "create data diff for tables"; var srcTableArgument = config.Argument("sourceTable", "Source table name"); var targetTableArgument = config.Argument("targetTable", "Target table name"); bool validateArguments(CommandArgument srcTbl, CommandArgument targetTbl) => !string.IsNullOrEmpty(srcTbl.Value) && !string.IsNullOrEmpty(targetTbl.Value); var sourceConnectionString = config.Option("--src|-s", "Source database connection string", CommandOptionType.SingleValue); var targetConnectionString = config.Option("--target|-t", "Target database connection string", CommandOptionType.SingleValue); var outputFile = config.Option("--output|-o", "Output file", CommandOptionType.SingleValue); var idempotentScript = config.Option("--idempotent|-i", "Generate idempotent script", CommandOptionType.NoValue); bool validateOptions(CommandOption srcConnectionString, CommandOption trgtConnectionString, CommandOption outFile) => srcConnectionString.HasValue() && trgtConnectionString.HasValue() && outFile.HasValue(); config.OnExecute(async() => { if (!validateOptions(sourceConnectionString, targetConnectionString, outputFile) || !validateArguments(srcTableArgument, targetTableArgument)) { config.ShowHelp(); //show help return(0); } IDataTableReader dataTableReader = new SqlDataTableReader(); var srcTable = dataTableReader.GetDataTable(sourceConnectionString.Value(), srcTableArgument.Value); var targetTable = dataTableReader.GetDataTable(targetConnectionString.Value(), targetTableArgument.Value); var validators = new List <ITableSchemaValidator> { new SamePrimaryKeysValidator(), new KeysDataTypeValidator(), new CompositeKeysValidator() }; var service = new DataDiffService(new QueryFormatter(), new TableSchemaValidatorsComposite(validators)); var(success, diffScript) = service.GetDataDiffSql(srcTable, targetTable, idempotentScript.Value() != null); if (!success) { return(1); } try { await File.WriteAllTextAsync(outputFile.Value(), diffScript); } catch (IOException ioException) { Console.Error.WriteLine(ioException.Message); return(1); } return(0); }); config.HelpOption("-? | -h | --help"); }); app.HelpOption("-? | -h | --help"); var result = app.Execute(args); Environment.Exit(result); }