MSBUILD DBPro Deploy Tip

written by Rob Aquila on Sunday, April 04 2010

If you are using MSBuild to deploy your database changes as described in a pervious post, it can really start to slow down your builds as you add more databases that need to be updated.  One way to get around this is to reuse the script that was used to deploy the first database on all the other databases.  This will cut down on the time it takes to regenerate the script for each database since it will only be done once.  This will work fine as long as manual changes are not being made to these databases and changes are only being deployed by the build server.  There is also a high chance that if the build fails while running the generated sql on a given database, restarting the build again will not fix it since this build will not regenerate a custom script for each database.  So I suggest to keep a build around that will actually compare each database to the dbpro project and run that nightly to make sure all the databases are still in sync.

When a script file is generated from the build, it is tied to that database and will only run on that given database since the database name is declared in the script itself.  The way around this is to comment out the line of sql that sets the database name and to pass in the database name through sqlcmd.exe.  In this example I am using the MSBuild Community Tasks to comment out the :setvar DatabaseName line.  Then I run sqlcmd.exe passing in the database name I would like to run this on.

 

 <Target Name="AfterDropBuild">
    
<MSBuild
        Projects="$(SolutionRoot)\Source\DB\DATABASE.dbproj"
        Properties
="Configuration=Default;OutDir=$(SolutionRoot)\..\binaries\Default\;TargetDatabase=DATABASE;TargetConnectionString=$(TargetConnection);GenerateDropsIfNotInProject=True;BlockIncrementalDeploymentIfDataLoss=False;DeployToDatabase=True;IgnorePermissions=True"
        Targets
="Deploy" />

    <
FileUpdate Files="$(SolutionRoot)\..\binaries\Default\DATABASE.sql" Regex=":setvar DatabaseName" ReplacementText="--:setvar DatabaseName" />

    <
Exec Command="sqlcmd.exe -S $(TargetServerName) -E -i "$(SolutionRoot)\..\binaries\Default\DATABASE.sql" -v DatabaseName="NEWDATABASE""
            ContinueOnError
="false"  />
</
Target>

Similar Posts

  1. Using VSDBCMD and MSBUILD to Build and Deploy DBPro Projects
  2. Using MS Deploy Instead of the Copy Command In MSBuild
  3. Team Foundation Server Build Notification Screen

Comments are closed

Options:

Size

Colors