Database continuous integration step by step
Posted
by David Atkinson
on Simple Talk
See other posts from Simple Talk
or by David Atkinson
Published on Tue, 24 Apr 2012 18:25:00 GMT
Indexed on
2012/05/30
16:56 UTC
Read the original article
Hit count: 564
This post will describe how to set up basic database continuous integration using TeamCity to initiate the build process, SQL Source Control to put your database under source control, and the SQL Compare command line to keep a test database up to date.
In my example I will be using Subversion as my source control repository. If you wish to follow my steps verbatim, please make sure you have TortoiseSVN, SQL Compare and SQL Source Control installed.
Downloading and Installing TeamCity
TeamCity (http://www.jetbrains.com/teamcity/index.html) is free for up to three agents, so it a great no-risk tool you can use to experiment with.
1. Download the latest version from the JetBrains website. For some reason the TeamCity executable didn't download properly for me, stalling frustratingly at 99%, so I tried again with the zip file download option (see screenshot below), which worked flawlessly.
2. Run the installer using the defaults. This results in a set-up with the server component and agent installed on the same machine, which is ideal for getting started with ease.
3. Check that the build agent is pointing to the server correctly. This has caught me out a few times before. This setting is in C:\TeamCity\buildAgent\conf\buildAgent.properties and for my installation is serverUrl=http\://localhost\:80 . If you need to change this value, if for example you've had to install the Server console to a different port number, the TeamCity Build Agent Service will need to be restarted for the change to take effect.
4. Open the TeamCity admin console on http://localhost , and specify your own designated username and password at first startup.
Putting your database in source control using SQL Source Control
5. Assuming you've got SQL Source Control installed, select a development database in the SQL Server Management Studio Object Explorer and select Link Database to Source Control.
6. For the Link step you can either create your own empty folder in source control, or you can select Just Evaluating, which just creates a local subversion repository for you behind the scenes.
7. Once linked, note that your database turns green in the Object Explorer. Visit the Commit tab to do an initial commit of your database objects by typing in an appropriate comment and clicking Commit.
8. There is a hidden feature in SQL Source Control that opens up TortoiseSVN (provided it is installed) pointing to the linked repository. Keep Shift depressed and right click on the text to the right of 'Linked to', in the example below, it's the red Evaluation Repository text. Select Open TortoiseSVN Repo Browser.
This screen should give you an idea of how SQL Source Control manages the object files behind the scenes.
Back in the TeamCity admin console, we'll now create a new project to monitor the above repository location and to trigger a 'build' each time the repository changes.
9. In TeamCity Adminstration, select Create Project and give it a name, such as "My first database CI", and click Create.
10. Click on Create Build Configuration, and name it something like "Integration build".
11. Click VCS settings and then Create And Attach new VCS root. This is where you will tell TeamCity about the repository it should monitor.
12. In my case since I'm using the Just Evaluating option in SQL Source Control, I should select Subversion.
13. In the URL field paste your repository location. In my case this is
file:///C:/Users/David.Atkinson/AppData/Local/Red Gate/SQL Source Control 3/EvaluationRepositories/WidgetDevelopment/WidgetDevelopment
14. Click on Test Connection to ensure that you can communicate with your source control system. Click Save.
15. Click Add Build Step, and Runner Type: Command Line. Should you be familiar with the other runner types, such as NAnt, MSBuild or Powershell, you can opt for these, but for the same of keeping it simple I will pick the simplest option.
16. If you have installed SQL Compare in the default location, set the Command Executable field to:
C:\Program Files (x86)\Red Gate\SQL Compare 10\sqlcompare.exe
17. Flip back to SSMS briefly and add a new database to your server. This will be the database used for continuous integration testing.
18. Set the command parameters according to your server and the name of the database you have created. In my case I created database RedGateCI on server .\sql2008r2
/scripts1:. /server2:.\sql2008r2 /db2:RedGateCI /sync /verbose
Note that if you pick a server instance that isn't on your local machine, you'll need the TCP/IP protocol enabled in SQL Server Configuration Manager otherwise the SQL Compare command line will not be able to connect.
19. Save and select Build Triggering / Add New Trigger / VCS Trigger. This is where you tell TeamCity when it should initiate a build. Click Save.
20. Now return to SQL Server Management Studio and make a schema change (eg add a new object) to your linked development database. A blue indicator will appear in the Object Explorer. Commit this change, typing in an appropriate check-in comment. All being good, within 60 seconds (a TeamCity default that can be changed) a build will be triggered.
21. Click on Projects in TeamCity to get back to the overview screen:
The build log will show you the console output, which is useful for troubleshooting any issues:
That's it! You now have continuous integration on your database. In future posts I'll cover how you can generate and test the database creation script, the database upgrade script, and run database unit tests as part of your continuous integration script.
If you have any trouble getting this up and running please let me know, either by commenting on this post, or email me directly using the email address below.
© Simple Talk or respective owner