Welcome to MSDN Blogs Sign in | Join | Help

VSTS 2005 and 2008: Building Database Projects with Team Build

Jon Liperi, a tester on Team Build, has put together the post below that explains a number of the issues around using Visual Studio Team Edition for Database Professionals (DBPro) with TFS Build.  Jon previously worked on the DBPro team, so he knows his way around it quite well.  Here are the issues that he covers.

  • Issue #1: Team Build service account does not have the required SQL Server permissions or cannot connect to SQL Server
  • Issue #2: Values for TargetDatabase, TargetConnectionString, or DefaultDataPath are missing or incorrect
  • Issue #3: The New Build Definition dialog does not provide a “Default” configuration option
  • Issue #4: The Deploy target is not invoked when built via Team Build
  • Issue #5: Database Unit Tests cannot find database project files, data generation plans, or the database instance(s) to be used for running tests when run via Team Build

This information applies to both the 2005 (8.0) and the 2008 (9.0) versions of VSTS and TFS.

Building Database Projects with Team Build by Jon Liperi

Recently, we have seen more questions about building database projects with Team Build. It is absolutely possible to build these project types with Team Build. However, you’ll need to have VSTE for Database Professionals (aka DBPro) and SQL Server installed on the build agent. There are also several known issues. In this blog post, I’ll describe these issues and their workarounds. Please let me know if there are additional issues you encounter or if the work-around steps need a bit of correction by commenting on this blog entry or posting on the Team Build forum:

http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=481&SiteID=1

To start, I’ll point you to a few links specific to database projects, including some existing documentation around Team Build integration.

Visual Studio Team System Database Edition (MSDN documentation)
http://msdn2.microsoft.com/en-us/library/aa833253(VS.90).aspx

How to: Deploy Changes using Team Foundation Build (MSDN documentation)
http://msdn2.microsoft.com/en-us/library/aa833289(VS.90).aspx

Visual Studio Team System - Database Professionals Forum
http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=725&SiteID=1

Issue #1: Team Build service account does not have the required SQL Server permissions or cannot connect to SQL Server

Database projects create a scratch database on the local SQL Server instance when building. Therefore, the build will fail if the Team Build service account does not have the appropriate permissions on the SQL Server instance running on the build machine. You may see an error message similar to the one below in the build log:

The "SqlBuildTask" task failed unexpectedly.

Microsoft.VisualStudio.TeamSystem.Data.Common.Exceptions.DesignDatabaseFailedException: You have insufficient permissions to create the database project. For more information, see the product documentation.

To resolve this issue, grant the required SQL Server permissions to the Team Build service account. In Orcas, the Team Build service runs by default as NT AUTHORITY\NETWORK SERVICE. A quick way to fix this is to create a SQL login for the service account that has sysadmin privileges. However, if you want to grant only the minimal permissions, detailed SQL Server permissions for DBPro are described here:

http://msdn2.microsoft.com/en-us/library/aa833413(vs.90).aspx

Additionally, you may need to ensure that a necessary registry key, which points to the local SQL Server instance to use for build, exists on the build machine. To configure the build account’s HKCU hive to point to the correct instance, you can either:

  1. Start the Visual Studio IDE as that user. Set the instance name by opening Tools | Options and navigating to Database Tools | Design-Time Validation Database.
  2. Run the following command from a command prompt.
    (Note: You may need to replace 9.0 with 8.0 depending on the version of DBPro you are using.)

    runas /user:<Team Build service account> "REG ADD HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\DBPro\DialogPage\Microsoft.VisualStudio.TeamSystem.Data.DBProject.Settings.DataConnectionOptionsSettings /v DefaultSqlServerName /d <instance name>"

    The value for <instance name> is just the name of the instance. For example, if the instance is “.\SQLEXPRESS”, replace <instance name> with “SQLEXPRESS”. If it is an unnamed instance, enter an empty string.

Issue #2: Values for TargetDatabase, TargetConnectionString, or DefaultDataPath are missing or incorrect

Database projects store any non-default values for the TargetDatabase, TargetConnectionString, and DefaultDataPath properties in a <ProjectName>.dbproj.user file which is not checked into version control (as the values may be different for each user). Therefore, these values are missing when built via Team Build resulting in the following error message:

TSD257: The value for $(DefaultDataPath) is not set, please set it through the build property page.

To build successfully from Team Build, you must either:

  1. Copy these properties from the <ProjectName>.dbproj.user file and add them to the <ProjectName>.dbproj file for the configuration that you want to build.
  2. Pass these properties as MSBuild command line arguments by entering them in the Queue Build dialog (Orcas only) or by storing them in the TFSBuild.rsp file. For example:
    /p:DefaultDataPath=<path>; TargetDatabase=<databaseName>;AlwaysCreateNewDatabase=true;TargetConnectionString="<connection string>"

Issue #3: The New Build Definition dialog does not provide a “Default” configuration option

Database projects do not define Debug or Release configurations. They only define “Default”. When creating a new build definition, you may notice that the only listed options are “Release” and “Debug”. To work-around this, you can either:

  1. Manually type in “Default” in the dialog.
  2. Ensure the “Release” and “Debug” solution-level configurations are set to build the “Default” configuration of the database project.

Issue #4: The Deploy target is not invoked when built via Team Build

The default target executed by MSBuild is Build. Therefore, database projects may not deploy by default when built via Team Build. To invoke the Deploy target from Team Build, you must either:

  1. Ensure your solution configuration is set to invoke both the Build and Deploy targets on the database project.
  2. Override AfterDropBuild to explicitly invoke the Deploy target. Instructions for overriding Team Build targets can be found at:
    http://msdn2.microsoft.com/en-us/library/aa337604(VS.90).aspx
  3. Modify the TFSBuild.proj file to list the individual projects to build and their targets instead of listing the entire solution. For example:
    <SolutionToBuild Include=”foo.dbproj”>
    <Targets>Build;Deploy</Targets>
    </SolutionToBuild>
  4. Edit the .dbproj file to make the DefaultTargets in their dbproj file Build;Deploy. For example:
    <Project DefaultTargets=”Build;Deploy”...>
    This would apply outside of Team Build as well, but it would have the desired effect.

Issue #5: Database Unit Tests cannot find database project files, data generation plans, or the database instance(s) to be used for running tests when run via Team Build

Database unit tests provide the ability to deploy a database project and/or run a data generation plan prior to running tests. Database unit test projects store the location of the referenced .dbproj and .dgen files as relative paths in the app.config file.

When building a database project using Team Build, the output and source files are stored in a different directory structure on the build machine. The test files are located in a TestResults folder while the source files are located in a Sources folder. When the unit tests are run from the TestResults folder, the relative path to the referenced .dbproj and/or .dgen files in the <assemblyname>.config file is no longer correct. This causes the tests to fail with one of the following messages:

Database deployment failed. Path ‘<path>\Database1.dbproj' is not a valid path to a database project file.

Data Generation failed. Path '<path>\Data Generation Plans\DataGenerationPlan1.dgen' is not a valid path to a data generation plan file.

Additionally, the app.config file also stores connection string information that points to the SQL Server instance to be used for running tests. If the connection strings are not valid when the tests are run from the build machine during a Team Build, the unit tests will fail with the following message:

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.

(Note that you may want the connection string to change depending on if they are running locally or through Team Build. For example, you may want tests to use your local SQL Server instance during development but use a remote shared instance when running via Team Build. The solution to this issue can also be used to achieve this result.)

To fix this issue, users need to manually create an app.TeamBuild.config file with the correct path locations and connection strings to be used when builds are created via Team Build. This file will then be renamed to <AssemblyName>.config through a post-build target, overwriting the .config file that contains the incorrect values.

  1. Create a file named app.TeamBuild.config by copying the existing app.config in your unit test project. Add it to your unit test project in version control.
  2. In the app.TeamBuild.config file, change the relative path to the .dbproj and .dgen files by adding a folder level for the Sources folder and a subfolder with the same name as the solution. For example,

    Before:
    "..\..\..\Database1\Data Generation Plans\DataGenerationPlan1.dgen"

    After:
    "..\..\..\Sources\Database1\Database1\Data Generation Plans\DataGenerationPlan1.dgen"

    Before:
    "..\..\..\Database1\Database1.dbproj"

    After:
    "..\..\..\Sources\Database1\Database1\Database1.dbproj"

    Additionally, you can modify the connection strings in the app.TeamBuild.config to the strings that should be used from the Team Build machine. Check the changes back into version control.
  3. Check out the unit test project file and modify the end of the file to include these lines right before the </project> tag. Check the changes back into version control.
    (Note: You may need to replace v9.0 with v8.0 depending on the version of DBPro you are using.)

    <Import Condition="'$(TeamBuildConstants)' != ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\TeamData\Microsoft.VisualStudio.TeamSystem.Data.TeamBuildTasks.targets"/>

    <Target Name="AfterBuild" DependsOnTargets="$(AfterBuildTeamBuildTargets)">
    </Target>

  4. On the Team Build machine, create a file with the code below and name it Microsoft.VisualStudio.TeamSystem.Data.TeamBuildTasks.targets. Save the file in folder %Program Files%\MSBuild\Microsoft\Visual Studio\v9.0\TeamData
    (Note: You may need to replace v9.0 with v8.0 depending on the version of DBPro you are using.)

    <?xml version="1.0" encoding="utf-8"?>

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <UsingTask TaskName="DataGeneratorTask" AssemblyName="Microsoft.VisualStudio.TeamSystem.Data.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

    <PropertyGroup>
      <AfterBuildTeamBuildTargets>RenameTeamBuildConfig</AfterBuildTeamBuildTargets>
    </PropertyGroup>

    <ItemGroup>
      <__TeamBuildConfig Include="$(OutDir)$(TargetFileName).config"/>
    </ItemGroup>

    <Target Name="RenameTeamBuildConfig">
      <CreateItem Include="app.teambuild.config">
        <Output ItemName="TeamBuildAppConfig" TaskParameter="Include" />
      </CreateItem>
      <Copy SourceFiles="@(TeamBuildAppConfig)" DestinationFiles="@(__TeamBuildConfig)" />
    </Target>

    <Target Name="DataGen">
      <DataGeneratorTask
       ConnectionString="$(ConnectionString)"
       SourceFile="$(SourceFile)"
       PurgeTablesBeforePopulate="$(PurgeTablesBeforePopulate)"
       Verbose="$(Verbose)" />
    </Target>

    </Project>
Published Tuesday, September 11, 2007 7:40 by buckh

Comments

# Compilando proyectos de bases de datos con Team Build (2005 y 2008)

Aunque sea hacer de &quot;repetidor&quot;, y aunque supongo que muchos estaréis subscritos a este blog

Wednesday, September 12, 2007 3:01 by Luis Fraile

# Compilando proyectos de bases de datos con Team Build (2005 y 2008)

Aunque sea hacer de &quot;repetidor&quot;, y aunque supongo que muchos estaréis subscritos a este blog

Wednesday, September 12, 2007 3:02 by Luis Fraile

# re: VSTS 2005 and 2008: Building Database Projects with Team Build

Hi Buck,

  Is it possible to configure TFS to build both v8.0 and v9.0 DBPro solutions?

Thanks,

Andy

Sunday, September 16, 2007 5:37 by Andy Leonard

# VSTS Links - 09/17/2007

Brian Harry on TFS Migration &amp; Synchronization Tool for ClearCase Revealed. Sudhir Hasbe on Reporting...

Monday, September 17, 2007 9:32 AM by Team System News

# re: VSTS 2005 and 2008: Building Database Projects with Team Build

Andy, you should just need to have both versions installed on your build computer.

Buck

Monday, September 17, 2007 9:35 by Buck Hodges

# re: VSTS 2005 and 2008: Building Database Projects with Team Build

Hi,

Is there a simple value I can use for the DefaultDataPath property in a .dbproj file that is not dependant on the file system structure of a SQL Server installation?

For example, I intend to use the (local) SQL Server in the TargetConnectionString but not all machines throughout our site have the SQL data path set to the same folder.

Thanks.

Tuesday, September 18, 2007 9:55 AM by jstangroome

# re: VSTS 2005 and 2008: Building Database Projects with Team Build

Hi jstangroome,

Unfortunately, no. The value for DefaultDataPath is a hard-coded string and, as you mention, this value may be different in each environment. For team scenarios (like team build), the DBPro team recommends creating a new .targets file that contains default values for these settings that is included in the .dbproj file just before the Microsoft.VisualStudio.TeamSystem.Data.Tasks.targets file (via an Import statement). This would allow default values to be used during Team Builds, while still allowing the settings to be overridden by a .user file, if one exists.

Thanks,

Jon

Tuesday, September 18, 2007 2:22 by Jon Liperi

# re: VSTS 2005 and 2008: Building Database Projects with Team Build

Hi Jon,

I've used your suggestion of importing a targets file for team build only and it is working great.

However, I really feel that a DBPro project should be able to be built and deployed to whichever data folder the SQL instance has set as it's default in the registry.

From memory, I can create CREATE DATABASE scripts that just use a relative path and produce this result. To whom do I suggest that DBPro should support this behaviour?

Thanks again,

- Jason

Tuesday, September 25, 2007 10:51 by jstangroome

# re: VSTS 2005 and 2008: Building Database Projects with Team Build

Hi Jason,

I'm very glad to hear it's working for you and thanks for your feedback! I would recommmend making your suggestion directly to the DBPro team. You can either create a post on their forum:

Visual Studio Team System - Database Professionals Forum

http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=725&SiteID=1

Or you can log the suggestion at http://connect.microsoft.com. The benefit of logging it on connect is that it can be voted on by other members of the community and is tracked as a bug internally. The downside is that the connect site doesn't have a DBPro specific bucket, but I assure you it will make its way to the right place. :) I would recommend including DBPro in the issue title. Here's how you get to the Visual Studio submission page:

  • http://connect.microsoft.com/VisualStudio

  • Log in using your Passport

  • Click "Submit a Bug or Suggestion"

  • Click the green "submit feedback" link

  • Search for your issue to see if it has already been logged

  • Click "Submit Feedback"

  • Choose the Bug or Suggestion form (I would log yours as a suggestion)

  • Fill out the form and click Submit

Thanks,

Jon

Wednesday, September 26, 2007 9:49 AM by Jon Liperi

# The Data Dude Meets Team Build

At DevConnections and TechEd Europe every attendee has been receiving a special issue of CoDe Magazine

Tuesday, November 06, 2007 9:54 by Data Dude

# Building Team Data (DBPro) projects in Team Build

Buck Hodges shares how to solve these five issues in this very useful post. Issue #1: Team Build service...

Thursday, November 08, 2007 1:43 by Ken Brubaker

# SQLSaturday!

Thanks to everyone who attended my session at SQLSaturday in Orlando today. Here&#39;s a link to my slides

Saturday, November 10, 2007 10:28 AM by jim blizzard's blog
New Comments to this post are disabled
 
Page view tracker