<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>DbTracer &#187; continuous integration</title>
	<atom:link href="http://blog.dbtracer.org/tag/continuous-integration/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dbtracer.org</link>
	<description>Database Comparison and Deployment</description>
	<lastBuildDate>Wed, 28 Dec 2011 14:56:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Database Deployment Patterns (Introduction)</title>
		<link>http://blog.dbtracer.org/2011/10/31/database-deployment-patterns-introduction/</link>
		<comments>http://blog.dbtracer.org/2011/10/31/database-deployment-patterns-introduction/#comments</comments>
		<pubDate>Sun, 30 Oct 2011 23:01:47 +0000</pubDate>
		<dc:creator>Petr Kozelek</dc:creator>
				<category><![CDATA[Database Continuous Integration]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://blog.dbtracer.org/?p=231</guid>
		<description><![CDATA[In the previous post I described general approaches how to define database deployment steps. Now I focus more deeply on analysis of individual steps and on identification of deployment patterns &#8211; I replace here a more familiar term database refactorings since I want to focus on database deployment instead of database redesign. However, there can [...]
Related posts:<ol>
<li><a href='http://blog.dbtracer.org/2011/10/21/database-deployment-approaches/' rel='bookmark' title='Database Deployment Approaches'>Database Deployment Approaches</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In the <a href="/2011/10/21/database-deployment-approaches/">previous post</a> I described general approaches how to define database deployment steps. Now I focus more deeply on analysis of individual steps and on identification of <em>deployment patterns</em> &#8211; I replace here a more familiar term <a href="http://databaserefactoring.com">database refactorings</a> since I want to focus on database deployment instead of database redesign. However, there can be slight overlap between the two.</p>
<p>Deployment patterns are of two basic types, regarding database changes that they cover:</p>
<ul>
<li><em>Schema deployment patterns</em> cover any modifications in database schema. They can be divided further into two disjunctive groups:</li>
<ul>
<li><em>Data-independent patterns </em>are applied without requirements to modify existing data. In other words, when I need to apply data-independent pattern, I do not require to affect data being hold in existing tables. They are always deterministic (see below)<br />
Examples: add/drop views, functions or stored procedures, drop foreign key or column constraint</li>
<li><em>Data-dependent patterns </em>require existing data to be modified. The reason for modifications can be, for example, improving data quality before the pattern can be applied.<br />
Examples: add foreign key, add column constraint</li>
</ul>
<li><em>Data deployment patterns</em> modify purely the data and they keep the schema unchanged. Usually they follow schema deployment patterns in order to insert data into newly introduced tables/columns. They are deterministic or dependent (see below).</li>
</ul>
<p>Deployment patterns are of different clarity, depending on how straightforward it is to apply the pattern.</p>
<ul>
<li><em>Deterministic patterns</em> include deployment of changes for which there is only one way how to deploy them. Sometimes, the particular pattern can be applied repeatably without negative impact on a database schema and data itself. E.g. deployment of <em>Modify view pattern</em> is pretty straightforward &#8211; just execute <code>ALTER VIEW...</code> and you are done. When you execute it twice, it does not cause a problem. Well, modifying the view might break a dependent application, of course, but regarding the database layer, there is neither a risk nor uncertainty in applying it.</li>
<li><em>Ambiguous patterns</em> are the ones for which it is not clear how to apply them. For instance, <a href="http://databaserefactoring.com/SplitColumns.html">Split column pattern</a> includes a recipe defining the way how to split one column into the two (imagine splitting <code>FullName</code> into <code>FirstName</code>, <code>LastName</code>). How should it be done? Split by spaces? What if <code>FullName</code> includes academic titles? Get rid of them? Make them a part of the <code>FirstName</code>? These questions have to be answered by a developer who knows data quality. Moreover, ambiguous patterns usually depend on existing data and they usually are not repeatable.</li>
</ul>
<p>Ambiguities are the biggest obstacle for adopting automatic database deployment. If you succeed with reducing the number of ambiguous deployment patterns you will feel less pain in introducing continuous database deployment. There are less ambiguities when deployment patterns are small, e.g. <a href="http://databaserefactoring.com/IntroduceNewColumn.html">introduce column</a> pattern. On the contrary, when database deployment requires complex patterns (e.g. <a href="http://databaserefactoring.com/ReplaceOneToMany.html">Introduce Associative Table</a>) to be applied then the number of ambiguities increases.</p>
<p>The main question is: <em>How to reduce the number of ambiguities?</em></p>
<ul>
<li>Deploy database changes often. Then there is smaller chance for introducing complexities between the original and the new database version.</li>
<li>Use the right tools. Although the existing tools cannot solve all problems connected with database deployment they can provide significant help. They should detect differences between database versions. Consequently, they should assist a developer in classifying database deployment patterns. Of course, the tools should automatically resolve differences for which deterministic patterns can be applied so that the developer can focus on ambiguous problems.</li>
</ul>
<p>Related posts:<ol>
<li><a href='http://blog.dbtracer.org/2011/10/21/database-deployment-approaches/' rel='bookmark' title='Database Deployment Approaches'>Database Deployment Approaches</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.dbtracer.org/2011/10/31/database-deployment-patterns-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[Database Deployment]]></series:name>
	</item>
		<item>
		<title>Database Deployment Approaches</title>
		<link>http://blog.dbtracer.org/2011/10/21/database-deployment-approaches/</link>
		<comments>http://blog.dbtracer.org/2011/10/21/database-deployment-approaches/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 10:10:32 +0000</pubDate>
		<dc:creator>Petr Kozelek</dc:creator>
				<category><![CDATA[Database Continuous Integration]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[DbTracer]]></category>

		<guid isPermaLink="false">http://blog.dbtracer.org/?p=187</guid>
		<description><![CDATA[There are two main categories of database deployment tools. The first one supports deployment of software that follows Application-Driven Database Design (ADD). It means that a database is a persistence layer for a particular application. It is the application itself, its features and the need them to be persisted that drives database design. A typical [...]
Related posts:<ol>
<li><a href='http://blog.dbtracer.org/2011/10/31/database-deployment-patterns-introduction/' rel='bookmark' title='Database Deployment Patterns (Introduction)'>Database Deployment Patterns (Introduction)</a></li>
<li><a href='http://blog.dbtracer.org/2010/01/12/database-continuous-integration-with-dbtracer/' rel='bookmark' title='Database Continuous Integration with DbTracer'>Database Continuous Integration with DbTracer</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>There are two main categories of database deployment tools. The first one supports deployment of software that follows<em> Application-Driven Database Design (ADD)</em>. It means that a database is a persistence layer for a particular application. It is the application itself, its features and the need them to be persisted that drives database design. A typical reprezentant here is web application development.</p>
<p>On the other hand, applications that are oriented strictly on database development (and deployment) follow (as  I call such a development approach) <em>Independent Database Design (IDD)</em>. Thus, the major determinant for introducing database schema changes is the need for extending or modifying a nature of the data. This category typically includes data warehouses and high load/performance critical database processes.</p>
<p>It should be clear that <em>IDD</em> is simpler than <em>ADD</em> because a database developer cares just about development and deployment of one layer. But when an application developer takes care of two software layers and their mutual interconnection then he has more complex responsibility. It is natural that deployment of two dependent layers is more complex task than deployment of only one. The situation becomes even more complex when responsibilities for application layer development and database layer development are spread across different teams because of communication issues.</p>
<p>First I focus on <em>IDD</em> because it seems it is simpler than <em>ADD</em>, as if <em>ADD</em> was just extension to <em>IDD</em>. Later in this article I will explain that <em>IID</em> can be significantly different from <em>ADD</em>.</p>
<h3>Independent Database Design</h3>
<p>If a developer is responsible for development of just a database layer then his task to create deployment strategy is quite straightforward. Once the development phase within an iteration is over he is expected to define a deployment strategy for new version of database schema. The strategy usually is defined in a database deployment script. It includes <span title="Data Definition Language">DDL</span> commands for inserting/modifying/deleting columns in tables and etc. The deployment script often defines also <span title="Data Modification Language">DML</span> commands like inserting initial data to newly created tables. The two general questions come up when thinking about how to define database deployment process:</p>
<ol>
<li>How does the developer identifies what should be deployed into target environment?</li>
<li>How does the developer define the deployment process of the database?</li>
</ol>
<p>The answer to the first question is <em>Database Comparison Tool</em>. There is a number of them in the market. They can compare both database schema and data themselves. When the developer needs to identify what should be deployed he usually works in the following scenario:</p>
<ol>
<li>Compare the target database schema with current development database schema.</li>
<li>Identify schema differences</li>
<li>Identify which data should be deployed into target database. Database data comparison can help here.</li>
</ol>
<p>Identifying schema and data differences is quite simple thanks to the mature database comparison tools. The more difficult part come up with the second question. <em>How to define a deployment process?</em> Regardless of whether or not the developer uses some database deployment software the final result is always the same &#8211; a deployment SQL script. So, in order to answer the second question related to defining deployment process, the developer has to do the following steps:</p>
<ol>
<li>Create a database schema deployment script. It usually contains only DDL commands.</li>
<li>Create database data modification script. It usually contains only DML commands.</li>
</ol>
<p>Of course, the tools could help here, too. However, the whole process of transforming the identified database differences into the deployment script can be very complex. There can be mutual dependencies among database objects &#8211; both schema and data dependencies.</p>
<p>Consider the following example: In a target database there is a table <code>Person</code> with columns <code>Id</code>, <code>FullName</code>. In a development database that I want to deploy to target there is a table <code>Person</code> with columns <code>Id</code>, <code>FirstName</code>, <code>LastName</code>. How should I deploy the development database into target database? Simply with this deployment script?</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="sql"><pre class="de1"><span class="kw1">ALTER</span> <span class="kw1">TABLE</span> Person <span class="kw1">ADD</span> <span class="kw1">COLUMN</span> FirstName <span class="kw1">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">50</span><span class="br0">&#41;</span>;
<span class="kw1">ALTER</span> <span class="kw1">TABLE</span> Person <span class="kw1">ADD</span> <span class="kw1">COLUMN</span> LastName <span class="kw1">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">50</span><span class="br0">&#41;</span>;
<span class="kw1">ALTER</span> <span class="kw1">TABLE</span> Person <span class="kw1">DROP</span> <span class="kw1">COLUMN</span> FullName; <span class="co1">-- data gets lost!</span></pre></div></div></div></div></div></div></div>


<p>Opps! I lost all my data related to person names! Let us try better approach:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="sql"><pre class="de1"><span class="kw1">ALTER</span> <span class="kw1">TABLE</span> Person <span class="kw1">ADD</span> <span class="kw1">COLUMN</span> FirstName <span class="kw1">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">50</span><span class="br0">&#41;</span>;
<span class="kw1">ALTER</span> <span class="kw1">TABLE</span> Person <span class="kw1">ADD</span> <span class="kw1">COLUMN</span> LastName <span class="kw1">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">50</span><span class="br0">&#41;</span>;
<span class="kw1">UPDATE</span> Person <span class="kw1">SET</span> FirstName<span class="sy0">=</span>ExtractFirstName<span class="br0">&#40;</span>FullName<span class="br0">&#41;</span><span class="sy0">,</span> ExtractLastName<span class="br0">&#40;</span>FullName<span class="br0">&#41;</span>;
<span class="kw1">ALTER</span> <span class="kw1">TABLE</span> Person <span class="kw1">DROP</span> <span class="kw1">COLUMN</span> FullName;</pre></div></div></div></div></div></div></div>


<p>Here functions <code>ExtractFirstName</code>, <code>ExtractLastName</code> represent some (potentially complex) transformations defining the process how to retrieve particular parts of the name from its full represennation.</p>
<p>The situation gets more complex if new columns have constraints. For example, <code>FirstName</code> and <code>LastName</code> should not be null. It such a case you are not even able to add a column until you do not specify a default values for the columns being added.</p>
<p>The examples above demonstrates that there is not always a straightforward way how to deploy a new version of database to target environment. If there was then it would be relatively easy to automate creation od a deployment script.</p>
<p>Although nowadays tools can help a developer to pre-generate a deployment script, the developer still is required to manually double-check and fix it if needed. All in all, the whole process of defining a deployment script requires rather a lot of manual work.</p>
<h3>Application-Driven Database Design</h3>
<p>It might seem that <em>ADD</em> extends <em>IDD</em> approach however it is true only to some extent. In case a developer is creating an application which uses a database only as persistent storage then the database design is driven by application features. If the application stores a property FullName of an entity Person then a table Person with a column FullName has to be modeled in the database.</p>
<p>Later on, when the need for two independent properties FirstName, LastName arises then it has to be reflected in the database as well. Deployment of the application layer is simple since it is stateless &#8211; I can simply get rid of the old version of the application that supports only FullName property and replace it completely with new version of the entire application (I simplified it here since I do not count with introducing breaking changes in case other applications depend on the application being deployed). Definition of a deployment script can be simple SQL as in <em>IDD</em> approach.</p>
<p>However, unless the database design is strictly driven by the application then it is possible (at least to some extent) to define a recipe for database deployment directly in the application layer in the language the application layer is written in. Here is an example for <a href="http://guides.rubyonrails.org/migrations.html">migration in Ruby</a>:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="ruby"><pre class="de1"><span class="kw1">class</span> AddFirstNameAndLastNameToPersons <span class="sy0">&lt;</span> <span class="re2">ActiveRecord::Migration</span>
  <span class="kw1">def</span> change
    add_column <span class="re3">:FirstName</span>, <span class="re3">:LastName</span>, :<span class="kw3">string</span>
  <span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div></div></div></div></div></div>


<p>Of course it is just a wrapper that generates good old SQL at deployment time. But with this approach of defining migrations with simple DSL you get greater level of control over deployment process because you can use full power of object-oriented language. You can programmatically combine migrations together, you can programmatically enable/disable them, you can run pre-tests validating that particular migration is even possible to be run, etc.</p>
<p>One of the most popular deployment tools of this kind is <a href="http://www.liquibase.org/">Liquibase</a>. Written in Java, it uses special DSL for migrations recipes:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="xml"><pre class="de1"><span class="sc3"><span class="re1">&lt;changeSet</span> <span class="re0">id</span>=<span class="st0">&quot;1&quot;</span> <span class="re0">author</span>=<span class="st0">&quot;bob&quot;</span><span class="re2">&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;addColumn</span> <span class="re0">tableName</span>=<span class="st0">&quot;Person&quot;</span><span class="re2">&gt;</span></span>
        <span class="sc3"><span class="re1">&lt;column</span> <span class="re0">name</span>=<span class="st0">&quot;FirstName&quot;</span> <span class="re0">type</span>=<span class="st0">&quot;varchar(50)&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;/addColumn<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;addColumn</span> <span class="re0">tableName</span>=<span class="st0">&quot;Person&quot;</span><span class="re2">&gt;</span></span>
        <span class="sc3"><span class="re1">&lt;column</span> <span class="re0">name</span>=<span class="st0">&quot;LastName&quot;</span> <span class="re0">type</span>=<span class="st0">&quot;varchar(50)&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;/addColumn<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;sql<span class="re2">&gt;</span></span></span>UPDATE Person SET FirstName=ExtractFirstName(FullName), ExtractLastName(FullName)<span class="sc3"><span class="re1">&lt;/sql<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;dropColumn</span> <span class="re0">tableName</span>=<span class="st0">&quot;Person&quot;</span> <span class="re0">columnName</span>=<span class="st0">&quot;FullName&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;rollback<span class="re2">&gt;</span></span></span>
        <span class="sc3"><span class="re1">&lt;addColumn</span> <span class="re0">tableName</span>=<span class="st0">&quot;Person&quot;</span><span class="re2">&gt;</span></span>
            <span class="sc3"><span class="re1">&lt;column</span> <span class="re0">name</span>=<span class="st0">&quot;FullName&quot;</span> <span class="re0">type</span>=<span class="st0">&quot;varchar(100)&quot;</span><span class="re2">/&gt;</span></span>
        <span class="sc3"><span class="re1">&lt;/addColumn<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;sql<span class="re2">&gt;</span></span></span>UPDATE Person SET FullName=CreateFullName(FirstName, LastName)<span class="sc3"><span class="re1">&lt;/sql<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;dropColumn</span> <span class="re0">tableName</span>=<span class="st0">&quot;Person&quot;</span> <span class="re0">columnName</span>=<span class="st0">&quot;LastName&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;dropColumn</span> <span class="re0">tableName</span>=<span class="st0">&quot;Person&quot;</span> <span class="re0">columnName</span>=<span class="st0">&quot;FirstName&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;/rollback<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/changeSet<span class="re2">&gt;</span></span></span></pre></div></div></div></div></div></div></div>


<p>During deployment, the tool is responsible for translating migrations written in XML into SQL of target database server. Liquibase comes with an interesting feature of a rollback deployment in case a business decision to go back to previous version arises. In some cases the rollback does not have to be defined (add column, remove column) since a recipe for rollback can be inherited from the migration definition. But for a more advanced migration like in the example above the rollback recipe has to be defined manually.</p>
<h3>Conclusion</h3>
<p>Regardless of what approach the development team chooses for database migrations it always results in creating of a database change script. In simple case, the script is first generated by a database comparison tool, then a developer manuall reviews in and corrects conflicting changes. <a href="https://github.com/chucknorris/roundhouse/wiki">RoundhousE</a> or <a href="http://www.liquibase.org/">Liquibase</a> represent such a family of tools.</p>
<p>Advanced tools use custom DSL for defining database migrations. They can be written in application original language (<a href="http://guides.rubyonrails.org/migrations.html">Ruby migrations</a>, <a href="https://github.com/schambers/fluentmigrator/wiki">FluentMigrator</a>).</p>
<p>There are also mature commercial products that include both database comparison and deployment features in one tool. <a href="http://www.red-gate.com/products/sql-development/sql-data-compare/">SQL Data Compare</a> and <a href="http://www.nextep-softwares.com/">NeXtep Open Designer</a> are promising tools for database deployment automation.</p>
<p>Related posts:<ol>
<li><a href='http://blog.dbtracer.org/2011/10/31/database-deployment-patterns-introduction/' rel='bookmark' title='Database Deployment Patterns (Introduction)'>Database Deployment Patterns (Introduction)</a></li>
<li><a href='http://blog.dbtracer.org/2010/01/12/database-continuous-integration-with-dbtracer/' rel='bookmark' title='Database Continuous Integration with DbTracer'>Database Continuous Integration with DbTracer</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.dbtracer.org/2011/10/21/database-deployment-approaches/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[Database Deployment]]></series:name>
	</item>
		<item>
		<title>Database Continuous Integration with DbTracer</title>
		<link>http://blog.dbtracer.org/2010/01/12/database-continuous-integration-with-dbtracer/</link>
		<comments>http://blog.dbtracer.org/2010/01/12/database-continuous-integration-with-dbtracer/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 23:55:37 +0000</pubDate>
		<dc:creator>Petr Kozelek</dc:creator>
				<category><![CDATA[Database Continuous Integration]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[DbTracer]]></category>

		<guid isPermaLink="false">http://petrkozelek.e-blog.cz/?p=77</guid>
		<description><![CDATA[In my company we use principles of continuous integration in our C#.Net+MS SQL development. We use Subversion for source control management and CruiseControl.Net as integration server. The problem came when we needed to choose a tool for continuous integration of database changes since we did not find any open-source alternative. The common repository for all [...]
Related posts:<ol>
<li><a href='http://blog.dbtracer.org/2011/10/21/database-deployment-approaches/' rel='bookmark' title='Database Deployment Approaches'>Database Deployment Approaches</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a title="DBTracker" href="http://code.google.com/p/dbtracker/"><img class="alignleft size-full wp-image-121" title="DBTracker" src="http://petrkozelek.e-blog.cz/wp-content/uploads/dbt-logo1.png" alt="" width="104" height="55" /></a><br />
In my company we use principles of continuous integration in our C#.Net+MS SQL development. We use Subversion for source control management and CruiseControl.Net as integration server. The problem came when we needed to choose a tool for continuous integration of database changes since we did not find any open-source alternative.</p>
<p><span id="more-77"></span></p>
<p>The common repository for all source code changes is Subversion (currently we think of migrating to <a href="http://git-scm.com/">Git</a>, anyway). For tracking changes of database schema changes we use Subversion, too.</p>
<p>In our team each developer has his own database server installed on his PC and he is an administrator of his own database server. The core of everything is the SCM. Developers check out latest code from there, integration server uses it as well to get code base and integration tests. The idea connected with tracking changes of database schema was to include changes under version control. Then both developers and integration server were able to retrieve database schema changes and to handle them.</p>
<h2>Database Change Scripts</h2>
<p>If a developer wishes to do some changes in database schema, he can freely do that on his local database without fear to influence work of the others. As the result of his work he is expected to create a <em>database change script</em> that includes all schema changes needed to be implemented. Let&#8217;s say we need to add a column to the table. The change script will contain these SQL commands:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="sql"><pre class="de1"><span class="kw1">ALTER</span> <span class="kw1">TABLE</span> <span class="kw1">ORDER</span>
<span class="kw1">ADD</span> <span class="kw1">STATUS</span> <span class="kw1">INT</span> <span class="kw1">NOT</span> <span class="kw1">NULL</span> <span class="kw1">DEFAULT</span> <span class="nu0">0</span>
<span class="kw1">GO</span></pre></div></div></div></div></div></div></div>


<p>The script will add a column &#8220;Status&#8221; to a table &#8220;Order&#8221;. You can feel it would be very annoying for a developer to change database schema only via change scripts in regular daily work. We established a helpful practice how to make things easier for the developer. The change script should be the result of his work that he wish to be integrated. We work as follows:</p>
<ol>
<li>When developer is going to do schema changes (on his local machine, of course), first he creates a clone of database version that was already fully integrated to source control. Let&#8217;s say that database is in version 1.5 so he creates a clone of it with help of <a href="http://code.google.com/p/dbtracker/">DbTracer</a>:
<pre>cmd&gt; dbt create /database=reference_db_v_1_5 /db-version=1.5 /connection=(local)</pre>
</li>
<li>At this point he has a reference database that will serve him as a reference point when he will compare it with his test database. The test database is created in the same way as reference one:
<pre>cmd&gt; dbt create /database=test /connection=(local) /db-version=1.5 /override-if-exists</pre>
</li>
<li>Now the developer is ready to do whatever he wants in the &#8220;test&#8221; database. For example he can add a column &#8220;Status&#8221; to a table &#8220;Order&#8221;. Of course you are not limited to add only one schema change to one change script.</li>
<li>When he is ready with schema changes he compares the test database with a reference database &#8220;reference_db_v_1_5&#8243;. Tools for comparing databases are very much helpful here. In my team we use <a href="http://sqlmanager.net/products/mssql/dbcomparer">EMS DB Comparer</a>.</li>
<li>The tool generates a change script. It is possible to modify it manually if we are not satisfied with the result. After that, the change script is ready to be checked in to version control.</li>
</ol>
<h3>Change Script Names</h3>
<p>We use the following convention for our change scripts:</p>
<pre>&lt;major_version&gt;.&lt;minor_version&gt;.&lt;revision&gt;-&lt;some description of change script&gt;.UP.sql</pre>
<p>For example:</p>
<pre>003.012.0541-add column Status to table Order.UP.sql</pre>
<p>In this example the change script lies within database major version=3, minor version=12 and its revision=541. There is also a short description &#8220;add column Status to table Order&#8221; defining what is inside the change script. Additionally, the keyword &#8220;UP&#8221; in the change script is reserved for future when we will need to distinguish between <em>upgrade change scripts </em>and <em>downgrade change scripts</em>.</p>
<p>Now the change script is ready to go to version control.</p>
<h2>Our Solution &#8211; DbTracer</h2>
<p>We searched for a simple tool that would maintain a our database migration scripts. Additionally, it should provide following features:</p>
<ul>
<li>command line interface</li>
<li>storing SQL database version in the database itself</li>
</ul>
<p>Unfortunately, no such tool existed that would have been acceptable. Hence, we inspired by <a href="http://wildbit.com/blog/2008/03/24/database-continuous-integration-in-net/">Alexander Kleshchevnikov</a> and wrote our own tool <a href="http://code.google.com/p/dbtracker/">DbTracer</a>. Currently it is in experimental phase for public. We use it internally in our team but still API changes are going on.</p>
<h2>Further Reading</h2>
<ul>
<li><a href="http://odetocode.com/Blogs/scott/archive/2008/01/31/three-rules-for-database-work.aspx">Three Rules for Database Work</a></li>
<li><a href="http://wildbit.com/blog/2008/03/24/database-continuous-integration-in-net/">Database Continuous Integration in .NET</a></li>
<li><a href="http://martinfowler.com/articles/evodb.html">Evolutionary Database Design</a></li>
</ul>
<p>Related posts:<ol>
<li><a href='http://blog.dbtracer.org/2011/10/21/database-deployment-approaches/' rel='bookmark' title='Database Deployment Approaches'>Database Deployment Approaches</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.dbtracer.org/2010/01/12/database-continuous-integration-with-dbtracer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

