Click here to Skip to main content
15,880,392 members
Articles / Database Development / SQL Server
Article

Continuous Integration for Database Development

14 Mar 2011CPOL5 min read 25.5K   6   1
Continuous integration makes development more efficient – allowing bugs to be caught earlier, and providing rapid validation. But for too long, it hasn’t been standard practice for databases. This is an overview of implementing CI so you can bring database code in line with application development.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Continuous integration is the business of making sure that everything in your software project builds and gets tested automatically and frequently. So in most situations, each code commit kicks off a build, and the build has a suite of unit and integration tests run against it. This means you get quick validation of your changes, and notification of problems.

Martin Fowler wrote the definitive article on continuous integration back in 2000, so it’s not a new idea. In fact, it’s standard practice for a lot of development teams working on application code.

But for the database layer, it’s a little harder.

Why is continuous integration a problem for databases?

Database code just isn’t like application code. It doesn’t live neatly as a set of files you can copy around, and you don’t compile it. This means the first problem is that there’s nothing to put in source control. Quite apart from the benefits of change tracking and change sharing, source control offers you a single location for the “one true version” of your code. It’s the location you deploy from in continuous integration.

Then there’s the fact that SQL is declarative, and DDL statements modify the current state of a database. So every time you make a change, you must account for referential integrity, and ensure data is preserved.

In practice, this means migration scripts – lots of them. You need to determine the state of the source database, the differences with the target, and write a script to correctly migrate between the two. This is time consuming and can result in a lot of errors.

Solving the Problem: Source Control and Deployment

An ideal solution would allow you to get your database into source control, and automate creation of change scripts. It would include both the database schema and any static data required by the application. The database would go into source control alongside the application code, and it would be deployed from there by the build system.

The rest of this article outlines how you can actually implement these processes using the SQL Developer Bundle, particularly SQL Source Control.

The first thing to do is to get the database into source control. SQL Source Control isn’t a source control system itself. It’s an add-in for SQL Server Management Studio that connects it with your existing source control system.

The current version supports Subversion and Team Foundation Server. The forthcoming SQL Source Control 2.1 will include support for any source control system with a command line interface, initially shipping with SourceGear Vault and Mercurial support built in.

So to get set up you link the database to your source control system:

Continuous-Integration/image001.jpg

You enter details of your source control repository, link the database and then commit the objects:

Continuous-Integration/image002.png

With version 2 and onwards, you can also choose to source control your static data. To do this, right-click the database in the Object Explorer, and click Link/Unlink Static Data… A dialog box is displayed letting you choose the tables whose data you want to source control.

When the time comes to deploy, you need to get the database out of source control again. To support this, SQL Compare and SQL Data Compare have command line interfaces you can use on a build server, for example with MS Build, NAnt, or TeamCity.

Here’s an example of the command line script to deploy the AdventureWorks database from Team Foundation server.

cd "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE" 
tf get "C:\Scripts\AdventureWorks" /version:T 
cd "C:\Program Files\Red Gate\SQL Compare 8" 
sqlcompare /scr1:"C:\Scripts\AdventureWorks" 
/s2:TestingServer\SQL2008 
/UserName2:TestUser 
/db2:AdvWrksTst 
/Report:"C:\SchemaDiffReport.html" 
/ReportType:Interactive 
/ScriptFile:"C:\SchemaSyncScript.sql" 
/sync 
cd "C:\Program Files\Red Gate\SQL Data Compare 8" 
sqldatacompare /scr1:"C:\Scripts\AdventureWorks" 
/s2:TestingServer\SQL2008 
/UserName2:TestUser 
/db2:AdvWrksTst 
/o:Default 
/ScriptFile:"C:\DataSyncScript.sql" 
/sync

The command lines are documented fully online – here for SQL Compare, and here for SQL Data Compare. But briefly, here’s what’s going on:

  • tf get "C:\Scripts\AdventureWorks" /version:T is the TFS command that updates the local copy with the latest source control version of the database.
  • C:\Scripts\AdventureWorks is the file path for your local folder
  • sqlcompare /scr1:"C:\Scripts\AdventureWorks" specifies the local folder as the source for the schema comparison
  • /s2:TestingServer\SQL2008 specifies the target server for the schema synchronization (deployment)
  • /UserName2:TestUser is the user name for the target server
  • /db2:AdvWrksTst specifies the target database on TestingServer\SQL2008
  • /Report:"C:\SchemaDiffReport.html" generates a report of the schema differences and writes it to the specified file
  • /ReportType:Interactive specifies the format of the report, in this case a detailed interactive HTML format
  • /ScriptFile saves a copy of the SQL script used to migrate the changes
  • /sync synchronizes the data sources, making AdvWrksTst the same as AdventureWorks
  • sqldatacompare /scr1:"C:\Scripts\AdventureWorks" specifies the local folder as the source for the data comparison
  • /ScriptFile:"C:\SchemaSyncScript.sql" saves a copy of the SQL script used to migrate the schema changes
  • /ScriptFile:"C:\DataSyncScript.sql" saves a copy of the SQL script used to migrate the data changes

Essentially, the script gets a copy of the latest database version from source control and deploys it to a testing server, creating detailed reports of the migration.

Conclusion

Continuous integration makes development projects more efficient – allowing bugs to be caught earlier, and providing rapid validation. For too long, databases have been not only excluded from continuous integration, but have had no source control at all.

The tools described here provide the source control and deployment automation that let you bring database code in line with the application development process.

This has been a quick overview of working databases into the continuous integration process. It’s been written about in much more detail in the Red Gate white paper on continuous integration, and by the blogger Troy Hunt, in his post on automated database releases.

If you want to try this out for yourself, you can download a free trial of the SQL Developer Bundle.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Red Gate Software Ltd.
United Kingdom United Kingdom
Redgate makes ingeniously simple software used by 804,745 IT professionals and counting, and is the leading Microsoft SQL Server tools vendor. Our philosophy is to design highly usable, reliable tools which elegantly solve the problems developers and DBAs face every day, and help them adopt database DevOps. As a result, more than 100,000 companies use products in the Redgate SQL Toolbelt, including 91% of those in the Fortune 100.
This is a Organisation

1 members

Comments and Discussions

 
GeneralGPL alternatives Pin
cfondacci21-May-11 1:36
cfondacci21-May-11 1:36 
I do agree that continuous integration is very important in order to reveal bugs as early as possible. The database tier has been forgotten for too long. But organizations also want to rely on free and open software.

You may be interested in the neXtep designer platform : a collaborative database development platform based around the concept of version control, automated SQL script generation, deployment automation, etc.

Have a look at the project & documentation, send us your inputs & feedbacks and tell us what you think. The product is based on Eclipse, runs on windows, linux and mac and supports Oracle, MySql, PostgreSql, DB2 and SQL server.

All information available at :
http://www.nextep-softwares.com

Christophe.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.