CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Steve Hebert's Development Blog

Steve's Blog - From .Net to dotMath and everything in between.

NAnt-ing my database build

I finished the process of performing my database build using NAnt (actually NAntContrib).  I dusted off an old class library I used with a .Net plug-in I built for automating the database build and modified it so it could handle the NAnt.Core.Project object for floating messages, warnings and errors. 

Our database project under VS contains all of our stored procs, functions and trigger scripts (among others). Each script is responsible for testing if the contained object currently exists and takes appropriate steps.  Because the processing is order-critical, I've created an XML file format that enforces creation order and also allows for individual files or entire projects to be processed using a given line. 

I could have gotten this to run using only my class library instead of NAntContrib's SourceSafe additions (for grabbing the BuildScript), but I'll need some of NAntContribs functionality beyond the database build.  My NAnt script does the following:

(1) Get the database XML build script out of sourcesafe and store it to a working directory.  This XML build script signifies processing order and supports both individual files and directories. 
(2) Loads the XML build script in an XMLDocument object.
(3) Creates a StreamWriter object with the appropriate destination file name.
(4) Passes the objects to my class library to combine all the database scripts together into one file.
(5) NAnt then moves the file to the build area on the server

Here is the NAnt script I'm using to complete the task. I purposely pass the XMLDocument object because I'll eventually create different database build configurations using XSLT to automate the collection of these scripts.  I like the way this has turned out. 

<script language="C#" mainclass="Transform">
<references>
<include name="hssScriptBuilder.dll" />
</references>
<imports>
   <import name="System.Xml" />
   <import name="System.Xml.XPath" />
   <import name="System.Xml.Xsl" />
</imports>

<code>
<![CDATA[

//using System.Xml;

class Transform
{
   public static void ScriptMain(Project project)
   {
      // load the BuildConfig file gathered previously from sourcesafe      
      XmlDocument xDoc = new XmlDocument();
      xDoc.Load("./dbbuild/solution/BuildConfig.xml");

      // create the file for the entire script
      StreamWriter sw = new StreamWriter("./dbbuild/full_database_upgrade_script.sql");
      
      // hand the parameters to the class library to gather file contents
      hssScriptBuilder.hssSourceControl scc = new hssScriptBuilder.hssSourceControl();
      scc.ProcessSourceProject( xDoc, sw, "./dbbuild/workarea", project);
   }

}

]]>
</code>
</script>


Published Nov 08 2004, 02:26 PM by shebert
Filed under:

Comments

Steve Hebert said:

Hi Xuan,

I placed my dll ("hssScriptBuilder.dll") in the same directory as my .build file. I hope this helps...

-Steve
# November 10, 2004 7:16 AM

Xuan Diep said:

Hi Steve,

thanks for the help, it works fine now !!!

Xuan.
# November 11, 2004 3:11 AM

Steve Hebert's Development Blog said:

I had a couple of requests for the code I used to build my sql script on the fly in conjunction with NAntContrib (blogged about it here). Before I show the code, my next refactoring would include (1) creating a ProcessState interface and generating seperate ProcessState classes for NAnt and MultiThreaded environments and (2) do the same for vssManager to handle multiple source control engines.

Before I list the code, I suppose I should add a CMA disclaimer:

The code listed below is offered without any warranty expressed or implied. This is despite anything stated before, after or during this posting. To understand the scope of this lack of a warranty, understand that no guarantee is made that code will compile and if it does compile and actually accomplishes anything it is merely a coincidence on par with a monkey randomly banging on a typewriter and producing a word-for-word copy of Hamlet. If you use the code and it causes you any problems, you assume full responsibility for the problems and obsolve the author of any form of responsibility.

I should have been a lawyer... now for the code...
# November 11, 2004 3:52 AM

Steve Hebert's Development Blog said:

I was reading secretGeeks blog entry titled " 11 Tools for Database Versioning " and it reminded
# September 19, 2006 4:31 AM
Check out Devlicio.us!