Guest User

Untitled

a guest
Jan 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. using System;
  2. using RedGate.SQL.Shared;
  3. using RedGate.SQLCompare.Engine;
  4.  
  5. namespace SyncNAntTask
  6. {
  7. public static class SynchronizeScripts
  8. {
  9. public static void Synchronize(string scriptFolder, ConnectionProperties targetConnectionProperties)
  10. {
  11. using (Database sourceDatabaseScripts = new Database(), targetDatabase = new Database())
  12. {
  13. // Establish the schema from the scripts stored in the sourceDatabaseScripts scripts folder
  14. // Passing in null for the database information parameter causes SQL Compare to read the
  15. // XML file supplied in the folder.
  16. sourceDatabaseScripts.Register(scriptFolder, null, Options.Default);
  17.  
  18. // Read the schema for the targetDatabase database
  19. targetDatabase.Register(targetConnectionProperties, Options.Default);
  20.  
  21. // Compare the database against the scripts.
  22. // Comparing in this order makes the targetDatabase the second database
  23. Differences sourceDatabaseScriptsVStargetDatabase =
  24. sourceDatabaseScripts.CompareWith(targetDatabase, Options.Default);
  25.  
  26. // Select all of the differences for synchronization
  27. foreach (Difference difference in sourceDatabaseScriptsVStargetDatabase)
  28. {
  29. difference.Selected = IsIncludeObject(difference);
  30. }
  31.  
  32. // Calculate the work to do using sensible default options
  33. // The targetDatabase is to be updated, so the runOnTwo parameter is true
  34. var work = new Work();
  35. work.BuildFromDifferences(sourceDatabaseScriptsVStargetDatabase, Options.Default, true);
  36.  
  37. // We can now access the messages and warnings
  38. Console.WriteLine("Messages:");
  39.  
  40. foreach (Message message in work.Messages)
  41. {
  42. Console.WriteLine(message.Text);
  43. }
  44.  
  45. Console.WriteLine("Warnings:");
  46.  
  47. foreach (Message message in work.Warnings)
  48. {
  49. Console.WriteLine(message.Text);
  50. }
  51.  
  52. // Disposing the execution block when it's not needed any more is important to ensure
  53. // that all the temporary files are cleaned up
  54. using (ExecutionBlock block = work.ExecutionBlock)
  55. {
  56. // Display the SQL used to synchronize
  57. Console.WriteLine("SQL to synchronize:");
  58. Console.WriteLine(block.GetString());
  59.  
  60. // Finally, use a BlockExecutor to run the SQL against the WidgetProduction database
  61. var executor = new BlockExecutor();
  62. executor.ExecuteBlock(block, targetConnectionProperties.ServerName,
  63. targetConnectionProperties.DatabaseName, false,
  64. targetConnectionProperties.UserName, targetConnectionProperties.Password);
  65. }
  66. }
  67. }
  68.  
  69. private static bool IsIncludeObject(Difference difference)
  70. {
  71. if (difference.DatabaseObjectType != ObjectType.User &&
  72. difference.DatabaseObjectType != ObjectType.Role &&
  73. difference.DatabaseObjectType != ObjectType.Queue &&
  74. difference.DatabaseObjectType != ObjectType.Service &&
  75. MeetExcludeSpecialCase(difference) == false)
  76. {
  77. return true;
  78. }
  79.  
  80. return false;
  81. }
  82.  
  83. private static bool MeetExcludeSpecialCase(Difference difference)
  84. {
  85. if ((difference.DatabaseObjectType == ObjectType.Table &&
  86. difference.Name.ToLower().StartsWith("[dbo].[aspnet_sql")) ||
  87. (difference.DatabaseObjectType == ObjectType.StoredProcedure &&
  88. difference.Name.ToLower().StartsWith("[dbo].[aspnet_sql")) ||
  89. (difference.DatabaseObjectType == ObjectType.StoredProcedure &&
  90. difference.Name.ToLower().StartsWith("[dbo].[sqlquery")))
  91. {
  92. return true;
  93. }
  94.  
  95. return false;
  96. }
  97. }
  98. }
Add Comment
Please, Sign In to add comment