I have written a few blogs lately explaining how my current project is employing the use of datadude (aka the database development tools in Visual Studio 2010) in order to manage our database code, deployment of that code and also testing of it. In this blog post I’m going to share a little technique that we use in order to store a version history of our deployments. Note that this assumes a working knowledge of datadude so if you don’t know what the terms Post-Deployment script, SQLCMD variables, Continuous Integration & msbuild refer to then maybe this blog post is not for you!
Need a table
Firstly, we need a table to store our version history, in our case we have called it [BuildVersion] and it looks like this:
CREATE TABLE [dbo].[BuildVersion]
(
[BuildVersion] NVARCHAR(20) NOT NULL,
[Deployed] DATETIME NOT NULL
)

Need a project variable
We have a SQLCMD variable declared as part of our project that is intended to hold a version number. In datadude SQLCMD variables are (by default) declared in a file called Database.sqlcmdvars:

In our case we have a variable called ‘BuildVersion’ that we default to the value of “Unknown”

Need to populate the table
We use the value in our ‘BuildVersion’ variable in order to populate our [BuildVersion] table and we do that within a Post-Deployment script using the following simple code:
INSERT [dbo].[BuildVersion]([BuildVersion], [Deployed])
VALUES ('$(BuildVersion)', GETDATE()) --$(BuildVersion is a SQLCMD variable declared within this project)
And of course we need to tell our mechanism what the build number is
We need to make sure that $(BuildVersion) has a value in it. As we are deploying our database as part of a continuous integration build (leveraging msbuild) we can pass in a value from the msbuild script. Here’s the code that we use to do that:
<Exec Command="..\VSDBCMD\vsdbcmd.exe /Action:Deploy /ConnectionString:"Data Source=$(Server);Integrated Security=True" /DeployToDatabase:+ /ManifestFile:"..\MyDB.deploymanifest" /p:BuildVersion="$(BuildLabel)"" />
The important bit for what we are discussing herein is that last underlined part:
/p:BuildVersion="$(BuildLabel)"
The /p directive is used to populate a named variable (in our case BuildVersion) with a value. In our case than value is taken from an msbuild property which, in our case, is referred to by $(BuildLabel). [Note that managing msbuild properties is outside the scope of this blog post.]
Wrap-up
That’s pretty much all you need. If it all hangs together correctly then [BuildVersion] will contain a nice history of all your deployments like so:

Hope this is useful! I suspect the same technique will work in earlier versions of datadude but I don’t have any to hand so can’t find out.
@Jamiet