|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThe following article came about after reading Evolutionary Database Design by Martin Fowler and Pramod Sadalage. The concepts were great, but I couldn't find anything on the web that really showed how to go about implementing it. I really liked the idea of testing the migration of the database, so I decided to have a go at it myself from the ground up. Following is how I've managed to do it for the current project I'm working on. The EnvironmentDevelopers box
Build server
Production server (ProdServer)
Folder/Project structure
RequirementsOur current project requires regular releases, and these often involve database changes. We are using Perforce as our source control system. CruiseControl.NET runs on our build server and is configured to fire of a NAnt build script on every code checkin. The build requirements at checkin are...
ImplementationThe setupAs indicated in the folders section of the environment, it's important that the folders for the project are set up in the above way. There are rules however that need to be enforced within this structure. The CreateScripts\Databases and CreateScripts\Tables folders should be
read only for developers. This is because creating a database, or creating
a table requires that these database objects don't exist in the first
place. If we have an existing database, and we The remaining folders can be used quite happily to store the objects. Destroying the objects and recreating them will not affect the underlying data. One last area to note is that the CreateScripts\Constraints folder
should contain only referential integrity constraints. These constraints
should NOT be included in the The driver scriptWe start off first by building the overall application.build script use by CruiseControl.NET to kick off the build. <?xml version="1.0" encoding="UTF-8" ?>
<project name="Application" basedir="." default="checkinBuild">
<!-- Target runs on every checkin -->
<target name="checkinBuild">
<!-- build the unit test database first -->
<property name="database.server" value="(local)"/>
<property name="database.instance" value="DEV"/>
<nant buildfile="Database.build" inheritall="true"
target="build" failonerror="true"/>
<!-- And now, run the deployment -->
<property name="database.server" value="(local)"/>
<property name="database.instance" value="DEPLOY"/>
<nant buildfile="Database.build" inheritall="true"
target="restoreDatabase"/>
<nant buildfile="Database.build" inheritall="true"
target="deploy" failonerror="true"/>
</target>
</project>
The default target is checkinBuild, indicating this NAnt target will be
run on every code checkin. First, we call the database.build file
build target. The build target is determined by setting the
The new database scriptThe new database script is created in the database.build NAnt script. The sole purpose of this target is to completely destroy the existing database, and overwrite it with a brand new one, created from scratch. The result meets requirement 1. A blank database ready for unit testing. <target name="build" description="Create a brand new database">
<call target="createDatabase"/>
<call target="createTables"/>
<call target="createViews"/>
<call target="migrate"/>
<call target="createConstraints"/>
<call target="createProcedures"/>
<call target="createFunctions"/>
</target>
Key points to note about the above script are that we call the migrate target (described later) after the createDatabase and createTables targets. The reason we do this, is because after creating the database and tables, the migration scripts may include alterations to tables, new tables, or removal of colums from existing tables. These all need to be included to complete the database structure. Delving deeper into each of the createXxxx tasks, they look like this. <target name="createTables">
<foreach item="File" in "CreateScripts\Tables"
property="script" failonerror="false">
<call target="osql" failonerror="true"/>
</foreach>
</target>
<target name="createProcedures">
<foreach item="File" in "CreateScripts\Tables"
property="script" failonerror="false">
<call target="osql" failonerror="true"/>
</foreach>
</target>
Each of these loops through all the scripts in the appropriate folders, and runs them using osql on the database. (There is an osql task explained at the bottom for this) * Astute readers may have noted that we are running scripts on the Databases and Tables folder, yet I mentioned that these should be read only for developers. In short, these folders will be automatically populated at the end of an iteration. I will explain further below. The result, after running the build target, and calling all the create scripts is that we have a blank newly created database, from scripts entered by developers. The deploy database scriptThe second requirement was that we be able to test the deployment of a
database to an existing server. There are two requirements here.
First, if the database doesn't already exist, then we need to create a brand new
one. If it DOES exist however, we just need to upgrade. We use osql
to connect to the database by the <target name="deploy">
<!-- Attempt to connect to the named database.
If the database exists, osql -b will return 0 -->
<exec program="osql" commandline="-b -E -s
${database.connection} -d ${database.name} -Q "GO"
resultproperty="osql.result" failonerror="false"/>
<if test="${osql.result == '0'}">
<call target="upgrade"/>
</if>
<if test="${osql.result == '1'}">
<call target="build"/>
</if>
</target>
The upgrade script, is similar to the new script, but excludes creating the database and creating the table scripts. (Otherwise we may potentially drop and recreate them, losing data) <target name="upgrade"
description="Upgrades an existing eventlive database">
<call target="backupDatabase" failonerror="true"/>
<property name="nant.onfailure" value="restoreDatabase"/>
<call target="migrate" failonerror="true"/>
<call target="createConstraints" failonerror="true"/>
<call target="createProcedures" failonerror="true"/>
<call target="createFunctions" failonerror="true"/>
</target>
The curly bitsThis is where it gets a bit curly. When migrating data or upgrading database as a series of tasks, the order that the tasks are executed is vitally important. For example, the first task may add - say - a category table and a column to the Suppliers table. It also adds the referential integrity constraint between suppliers and categories. Order is important here. First - we can't create the constraint until both table and column are added. This is why the migration is run before the constraints. If a later data migration task then wishes to add new category to the table, we need to ensure that the create table script has been run first. In this case, the order within the MigrationScripts folder is important. There are a number of ways to do this.
I use date created by performing a <target name="migrate"
description="Run the migration scripts on the database">
<exec program="${solution.dir}\tools\listFiles.bat"
commandline="*.sql order.txt"
workingdir="Migration Scripts" failonerror="false"/>
<foreach item="Line" in="Migration Scripts\order.txt"
property="script">
<property name="script" value="Migration Scripts\${script}"/>
<call target="osql" failonerror="true"/>
</foreach>
</target>
Summary so far...The above scripts are called for every checkin within the iteration. Every code change that affects the data model, every data migration, data insertion or cleanse always gets put into the MigrationScripts directory. Each time it is checked in, the NAnt build rebuilds the database from scratch. It also takes the latest backup of the production database, and restores it to the DEPLOY instance - before finally running the deploy target on this database to test the deployment. End of IterationSo far, everything has been going along nicely, checkins have been rebuilding that database, and testing the migration scripts executing on the staging database. Everything works as it should, and everybody is happy. All we need to do now is finish off the iteration. First, we deploy to the production server. We know this works because we tested it by restoring from production and test running the scripts on the last build script. Deployment is simple...
The last thing we need to do is cleanup after the iteration. This involves archiving the migration scripts - we don't want to be rerunning them again. The problem is though - if we archive the migration scripts and they contain create table scripts, we need to be able to run them again to create the new database for unit testing in the next iteration. So what we do instead, is use upgraded database (on DEPLOY) to automatically generate the create database and create table scripts for us. These scripts are then placed in the CreateScripts\Databases and CreateScripts\Tables folders - by the build server not by a developer. Once these scripts are generated, we can move the MigrationScript items into an archive folder. The nant targets look like this. <!-- Script database takes an existing cr
eated database and scripts the tables and database -->
<!-- creation routine for later checking back in to
source control -->
<!-- Note that this should ONLY be called when
delivering on an iteration -->
<target name="scriptDatabase">
<exec program="cscript.exe" verbose="true">
<arg line="${solution.dir}/Tools/scriptDatabase.wsf //Nologo //B"/>
<arg value="//job:database"/>
<arg value="${database.connection}"/>
<arg value="${database.name}"/>
<arg value="Create Scripts/Databases"/>
</exec>
<exec program="cscript.exe" verbose="true">
<arg line="${solution.dir}/Tools/scriptDatabase.wsf //Nologo //B"/>
<arg value="//job:tables"/>
<arg value="${database.connection}"/>
<arg value="${database.name}"/>
<arg value="Create Scripts/Tables"/>
</exec>
</target>
<target name="archiveScripts" description="Archive the database scripts"
depends="scriptDatabase">
<!-- Now we need to archive the migration scripts -->
<tstamp property="archive.dir" pattern="yyyyMMdd"/>
<move todir="Archive\${archive.dir}">
<fileset basedir="Migration Scripts">
<include name="*"/>
</fileset>
</move>
</target>
Note - we are calling out to a script host program that generates the table and database scripts via SQLDMO - briefly... var objArgs, serverName, databaseName
objArgs = WScript.Arguments
if(objArgs.Length != 3)
{
WScript.Arguments.ShowUsage();
WScript.Quit(1);
}
serverName = objArgs(0);
databaseName = objArgs(1);
outputPath = objArgs(2);
var fso = new ActiveXObject("Scripting.FileSystemObject");
if(!fso.FolderExists(outputPath))
{
WScript.Echo("Folder doesn't exist");
WScript.Quit(1);
}
var sqlServer, sqlDB, tableCollection, table, e;
server = new ActiveXObject("SQLDMO.SQLServer");
server.LoginSecure = true;
server.Connect(serverName, null, null);
database = new ActiveXObject("SQLDMO.Database");
database = server.Databases(databaseName);
/* DMO Constants */
var SQLDMOScript_Drops = 1;
var SQLDMOScript_IncludeIfNotExists = 4096;
var SQLDMOScript_IncludeHeaders = 131072;
var SQLDMOScript_Default = 4;
var SQLDMOScript2_NoCollation = 8388608;
var SQLDMOScript_DRI_PrimaryKey = 268435456;
var SQLDMOScript_DRI_Checks = 16777216;
var SQLDMOScript_DRI_Defaults = 33554432;
var SQLDMOScript_DRI_UniqueKeys = 67108864;
var SQLDMOScript_ToFileOnly = 64; lang=jscript>
tableCollection = new Enumerator(database.Tables);
for( ; !tableCollection.atEnd() ; tableCollection.moveNext())
{
table = tableCollection.item();
if(!table.SystemObject)
{
var params = SQLDMOScript_Drops |
SQLDMOScript_IncludeIfNotExists |
SQLDMOScript_IncludeHeaders |
SQLDMOScript_Default |
SQLDMOScript_DRI_PrimaryKey |
SQLDMOScript_DRI_Checks |
SQLDMOScript_DRI_Defaults |
SQLDMOScript_DRI_UniqueKeys |
SQLDMOScript_ToFileOnly;
table.Script(params, fso.BuildPath(outputPath,
table.Name + ".sql"),
null, SQLDMOScript2_NoCollation);
WScript.Echo("Processing " + table.Name);
}
}
server.DisConnect();
We have a complete set of CreateScripts in their respective folders, the archive target is called which creates a new directory with the date set. The MigrationScript files are then moved to this directory. (And later checked in to source control) The iteration is closed. ConclusionIt's not that difficult to get it running. The advantages of spending the effort are tremendous though. In previous companies I've worked at, the running of scripts has been done by the DBA, and based on a weeks work getting everyone together to rollback failed ones, update the ones that depended on the failed ones, and generally just make sure it works. It's prone to transcription errors, permission errors, script errors and inconsistencies. By testing the deployment each time, as well as unit testing, we can reduce this. One last thing worthy of note though is that just because it's easier to change, doesn't mean that developers can hack tables. Thought still needs to be put into the design of the database. Notes:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||