SQL Compare and MSBuild

agolovanagolovan Posts: 2
I am new to this forum. I found an article "Red Gate MSBuild Tasks " explaining of how to run SQL Compare tasks from MSBUILD using C# code. However, I would like to do same as it is explaining in another article, "NAnt tasks for Red Gate SQL Compare", but just for MSBuild, not for NAnt. I need "MSBuild for SQL Compare" using configuration file and not C# (we are using Cruise Control NET and MSBuild). Any help will be really appreciated.

Comments

  • I am unaware of the articles you talk about but I would believe that the easiest way to call SQL Compare via MSBuild would be to use the <EXEC> task (http://msdn.microsoft.com/en-us/library/x8zx72cd.aspx) to call the SQLCompare.exe (need SQL Compare Pro). Furthermore, I would utilize the arguments xml (http://www.red-gate.com/supportcenter/Content.aspx?p=SQL%20Compare&c=SQL_Compare/help/7.0/sc_cl_usingxmltospecifyclarguments.htm&toc=SQL_Compare/help/7.0/toc356448.htm) when calling SQLCompare.exe

    With that said, I would take it one step further and call powershell via EXEC task to make the call to SQLCompare.exe, this way you have power of powershell to basically do what ever you want. Powerhsell and MSBuild work great together and are much quicker to get working than using the ITask class via C# to write a custom task. Plus your scripts are not coupled with MSBuild when using powershell as they are when using C# and ITask.

    Let me know if this help or if you have any other questions
    I can even post the powershell script I use to call SQLCompare.exe


    BJHop
    MCTS-TFS
  • Yes, please post the powershell script. That would be very helpful.
  • Okay this some what pieced together b/c my scripts run on a platform of scripts we have developed and would not make a whole lot o sense if I posted them stright up

    Below is the TeamBuild targets that could be used

    we use team build to set up the workspace where the scripts folder is

    we also have argument xml checked and this is what we call from the cmd line, makes it easy and we have a record of what is being called

    &lt;Target Name="AfterBuildDrop" Condition="$&#40;SyncDB&#41; == 'true'"&gt;
    
    	&lt;AddBuildStep BuildUri="$&#40;BuildUri&#41;" BuildStepName="DB_Sync" BuildStepText="Search DB_ Dev for latest HRLYCLDL Scripts" /&gt;
    	
    	&lt;Exec Command="powershell -c &quot;.\BuildScripts\DB_Sync.ps1 '$&#40;SolutionRoot&#41;' '$&#40;DropLocation&#41;'&quot;" WorkingDirectory="$&#40;PowerShellScripts&#41;" IgnoreExitCode="true"&gt;
    		&lt;Output TaskParameter="ExitCode" PropertyName="DB_ExitCode" /&gt;
    	&lt;/Exec&gt;
    	
    	&lt;Error Condition="$&#40;DB_ExitCode&#41;==2" Text="There was an unhandle exception in the DB Sync script " /&gt;
    	
    	&lt;Error Condition="$&#40;DB_ExitCode&#41;==1" Text="General error code." /&gt;
    	
    	&lt;Error Condition="$&#40;DB_ExitCode&#41;==8" Text="Unsatisfied argument dependency or violated exclusion when user runs command line. E.g. /arg2 depends on /arg1 but you have specified /arg2 without specifying /arg1, or alternatively /arg2 cannot be used with /arg1 but you have tried to use them both." /&gt;
    	
    	&lt;!-- should have all the Red Gate error msg catches here
    
    	&lt;UpdateBuildStep BuildUri="$&#40;BuildUri&#41;" BuildStepName="DB_Sync" BuildStepResult="true" /&gt;
    	
    	&lt;OnError ExecuteTargets="OnErrorDB_Sync"/&gt;
    &lt;/Target&gt;
    
    &lt;Target Name="OnErrorDB_Sync" &gt;
    	&lt;UpdateBuildStep BuildUri="$&#40;BuildUri&#41;" BuildStepName="DB_Sync" BuildStepResult="false" /&gt;
    &lt;/Target&gt;
    

    Now here is the DB_Sync.ps1
    param&#40;$slnRoot = $&#40;throw 'The snl root path is needed'&#41;,
          $dropLoc = $&#40;throw 'The Build Drop location is required'&#41;&#41;
    
    trap
    &#123;
    	$host.SetShouldExit&#40;2&#41;
    	return $false
    &#125;
    
    function get-ErrorMsg &#40;&#91;int&#93; $exitCode&#41;
    &#123;
    
    # These messages were copied from the SQLCompare.exe -help section 
    #if SQL Compare is updated need to ensure that the below holds true
    
    	&#91;string&#93; $errMsg = "" 
    	
    	switch &#40;$exitCode&#41;
    	&#123; 
    		1 &#123; $errMsg = "General error code." &#125;
    		3 &#123; $errMsg = "Illegal argument duplication. Some arguments may not appear more than once in a command-line. If such arguments appear more than once this exit code will be returned." &#125;
    		8 &#123; $errMsg = "Unsatisfied argument dependency or violated exclusion when user runs command line. E.g. /arg2 depends on /arg1 but you have specified /arg2 without specifying /arg1, or alternatively /arg2 cannot be used with /arg1 but you have tried to use them both." &#125;
    		32 &#123; $errMsg = "Value out of range. Numeric value supplied for an argument that is outside the range of valid values for that argument." &#125;
    		33 &#123; $errMsg = "Value overflow. The magnitude of a value supplied for an argument is too large and causes an overflow." &#125;
    		34 &#123; $errMsg = "Invalid value. The value supplied for an argument is invalid." &#125;
    		35 &#123; $errMsg = "No / invalid software license or trial period has expired." &#125;
    		63 &#123; $errMsg = "The databases being compared are identical with respect to any constraints on objects being compared." &#125;
    		64 &#123; $errMsg = "General command-line usage error." &#125;
    		65 &#123; $errMsg = "Data error. Some input data required by the tool is invalid or corrupt." &#125;
    		69 &#123; $errMsg = "A resource or service required to run the tool is unavailable." &#125;
    		73 &#123; $errMsg = "Failed to create report" &#125;
    		74 &#123; $errMsg = "IO error occurred. Generally returned if the program attempts to write to a file that already exists without the user having specified the /force option." &#125;
    		77 &#123; $errMsg = "Action cannot be completed because the user does not have permission." &#125;
    		126 &#123; $errMsg = "Execution failed because of an error." &#125;
    		130 &#123; $errMsg = "Execution stopped because Ctrl+Break." &#125;
    
    	&#125;
    	
    	return $errMsg
    &#125;
    
    
    
    ###MAIN###
    
    #set XML args paths 
    $sqlCompareArgsPath_sync = Join-Path $slnRoot "\XmlArgs\Sync.xml"
    
    #set output area
    $outputResults = Join-Path $dropLoc "SyncResults.log"
    
    pushd "C:\Program Files\Red Gate\SQL Compare 8"
    		
    #run sync job
    
    $rs = .\sqlcompare.exe /argfile:"$&#40;$sqlCompareArgsPath_Sync&#41;"
    
    #catch exit code
    &#91;int&#93; $sqlExitCode = $lastExitCode
    
    $rs | Out-File -FilePath $outputResults -Force -Append
    
    if &#40;$sqlExitCode -ne 0&#41;
    &#123;
    	&#91;string&#93; $errMsg = get-ErrorMsg $sqlExitCode
    	write-error $errMsg
    	$host.SetShouldExit&#40;$sqlExitCode&#41;
    &#125;
    
    
    popd 
    


    This has been simplified but is should work

    I hope this helps

    Thanks,

    BJHop
Sign In or Register to comment.