Manage SQL Databases

Note: the following article is only targeting Microsoft SQL Server (I regret...).

In a previous article I have discussed the schema generation and schema update offered by NHibernate. In this post I want to discuss a way how you can generate and/or maintain your SQL Server database.

When practicing agile development one of the most important cornerstones of this methodology is implementing continuous integration (CI). That is any solution your team develops should be able to build in fully automated manner. One of the build steps is of course the creation and or update of the database and the database schema.

Tools to automate the build process

Many people use either nant or msbuild to fully automate their build. And when I say build it includes the following tasks(others are possible)

  • versioning/tagging the sources
  • compiling the sources (in Visual Studio called build)
  • running unit tests, integration tests, acceptance tests, stress tests
  • dropping and re-creating the database
  • re-creating the database schema
  • creating a package
  • if Web: deploy the web site
  • if Other: create installer

In this post I'll discuss the two tasks marked in blue.

The nant task

There is an OSS project on Google Code which is dedicated to the change management of SQL server databases. It's called Tarantino and can be found here. One of the outcome of this project is a custom nant task called manageSqlDatabase. We will use nant and this custom task to manage our (sample) database.

You need the following files to be able to use the custom task

To get the files for your own solution either download the sample solution accompanying this post or download it directly from the Tarantino project which you can find here.

The build file

The general format of the custom nant task is

<manageSqlDatabase
  scriptDirectory="${database.script.directory}"
  action="${action}"
  server="${database.server}"
  integratedAuthentication="${database.integrated}"
  database="${database.name}"
  username="${database.username}"
  password="${database.password}"
/>

The sriptDirectory contains the path to the files which contain the schema creation and/or schema update statements.

The possible actions are

  • dropDatabase
  • createDatabase
  • rebuildDatabase
  • updateDatabase

The server parameter must contain the name of the SQL Server (e.g. "localhost/SQLEXPRESS").

The parameter IntegratedAuthentication can be either true or false.

The parameter database contais the name of the database to re-create or update (e.g. "SampleDatabase")

The parameters username and password are only needed if IntegratedAuthentication is set to false.

A typical create database scenario could be as shown below

<manageSqlDatabase
  scriptDirectory="src\database"
  action="createDatabase"
  server="localhost"
  integratedAuthentication="true"
  database="SampleDatabase"
/>

Let's not construct a complete build file for nant which includes the task of dropping and re-creating a database and the creation of the database schema. We create a new empty file called default.build and enter the following XML fragments.

<?xml version="1.0" encoding="utf-8"?>
<project name="DemoSolution" default="builddatabase" 
         xmlns="http://nant.sf.net/release/0.85/nant.xsd">
    <property name="solution.dir" value="src" />
    <property name="database.script.directory" value="${solution.dir}/Database"/>
    <property name="database.server" value="localhost"/>
    <property name="database.name" value="${project::get-name()}"/>
 
    <target name="builddatabase" depends="dropDatabase, createDatabase" />
    
    <target name="dropDatabase">
  </target>
    
    <target name="createDatabase">
  </target>
  
</project>

In this file we define a new nant project called DemoSolution. The default target that is executed is builddatabase. Then we define some properties for reference in our project (if you are not fluent in nant syntax please consult the online documentation here.)

The builddatabase target does nothing else than trigger the targets dropDatabase and createDatabase, that is if the database already exists then it is dropped and then re-created. Finally the database schema is created. But wait, those two targets are empty at the moment and will do absolutely nothing at the moment.

Let's now add this helper target to the build file - we want to avoid duplication don't we?

<target name="manageSqlDatabase">
  <manageSqlDatabase
    scriptDirectory="${database.script.directory}"
    action="${action}"
    server="${database.server}"
    integratedAuthentication="true"
    database="${database.name}"
  />
 
  <if test="${action != 'Drop'}">
    <echo message="Current Database Version: ${usdDatabaseVersion}" />
  </if>
 
</target>

it will be called by the dropDatabase and createDatabase targets where each provides another action parameter. Note that the usdDatabaseVersion parameter is generated by the manageSqlDatabase custom task.

Now we complete the dropDatabase and createDatabase targets as follows

<target name="dropDatabase">
  <property name="action" value="Drop" />
  <call target="manageSqlDatabase" failonerror="false"/>
</target>
 
<target name="createDatabase">
  <property name="action" value="Create" />
  <call target="manageSqlDatabase" />
</target>

that is we use the standard call task of nunit to trigger the custom task. Note that the dropDatabase target calls the custom task with failonerror set to false (default is true). It is possible that the database does not yet exist. In this case the build should just continue.

The script files

One of the main parts in the automation process is still missing. It's the SQL script files which generate and/or update the database schema. These files have to be valid SQL scripts (you should be able to run these scripts without errors in e.g. a query session in "SQL Server Management Studio"). The scripts can contain DDL and DML statements. They have to be sequentially numbered, e.g.

  • 0001_CreateBaseSchema.sql
  • 0002_AddProductAndCategory.sql
  • 0003_AddIndexes.sql
  • 0004_InitialDataLoad.sql
  • etc.

You can freely choose any name after the underscore. The manageSqlDatabase nant task will apply the scripts sequentially starting with the lowest number and ending with the highest number. When creating the database the manageSqlDatabase nant task will add a special table to the database which keeps track of which scripts have been applied.

How to create the script files

The initial schema generation script I normally generate by using NHibernate's schema export utility. See this post for an in depth discussion. Alternatively you can also use the script database objects task of "SQL Server Management Studio". For any further modifications of the schema (especially if the first version is already in production) I use a product like Redgate's SQL Compare or the same product from Apex to generate the alter scripts.

Execute the build

We can now execute the build by invoking the following command

bin\nant\nant.exe -buildfile:default.build

we can write a batch file builddatabase.bat to further automate the process. The content of the batch file might be as follows

bin\nant\nant.exe -buildfile:default.build
pause

Sample Code

You can download a little sample from here. You need to have an SQL Server available. A local installation of SQL Server Express Edition is enough. Please adjust the properties database.server and database.name in the file default.build according your needs. Double click the file builddatabase.bat to test the creation of the database and the database schema.

Enjoy!

Blog Signature Gabriel

Print | posted on Monday, August 04, 2008 10:15 AM

Comments on this post

# re: Manage SQL Databases

Requesting Gravatar...
Hi

download code is user\password protected.

Thanks
Disa
Left by Disa on Apr 30, 2009 12:46 AM

# re: Manage SQL Databases

Requesting Gravatar...
"You need to have an SQL Server available. A local installation of SQL Server Express Edition is enough." Is simple hosting enough?
Left by michelin floor jack on Sep 03, 2009 11:34 PM

# re: Manage SQL Databases

Requesting Gravatar...
I just want to make a self hosted wordpress blog, i am having a domain and hosting. Could you please help me out how to install wordpress.
Left by top 10 hosting on Oct 21, 2009 11:03 PM

# re: Manage SQL Databases

Requesting Gravatar...
Hello Gabriel, could you please let read-only access to the SVN to allow your readers follow your demo sample? It asked for login/password i could not find in the article. Thank you!
Left by brainboost on Nov 11, 2009 12:08 AM

# re: Manage SQL Databases

Requesting Gravatar...
UPD:
Please, update the link for the sample project, the right one is this, as the original required authorization.
Left by brainboost on Nov 11, 2009 12:57 AM

# re: Manage SQL Databases

Requesting Gravatar...
database.name in the file default.build according your needs. Double click the file builddatabase.bat to test the creation of the database and the database schema.
Left by Orkut Greetings on Nov 16, 2009 10:55 AM

# re: Manage SQL Databases

Requesting Gravatar...
Managing the large databases has become easy by the use of SQL database, thanks for sharing the information.
Left by Marc Jacobs Sunglasses on Nov 20, 2009 10:49 AM

# re: Manage SQL Databases

Requesting Gravatar...
Sounds like your skills are great when it comes to databases. I'm in the learning process, which is why I am here. My day job is an engine builder and automotive machinist.
Left by engine block on Nov 25, 2009 2:58 PM

# re: Manage SQL Databases

Requesting Gravatar...
With the help of SQL it is very easy to manage bulky data. Thanks
Left by Limo NY on Dec 03, 2009 12:44 PM

# re: Manage SQL Databases

Requesting Gravatar...
Very helpful post for managing SQL. Everything is explained well. Thanks for sharing.
Left by Edmonton Website Design on Dec 06, 2009 12:31 PM

# re: Manage SQL Databases

Requesting Gravatar...
Great post, thanks I love reading your blog
Left by structured settlements on Dec 06, 2009 8:05 PM

# re: Manage SQL Databases

Requesting Gravatar...
SQL is not simply to use? If you can’t write a good sql query, you are not a good developer The relational database model is I think is obvious) the best for the moment. If not… why is Oracle where it isQL Server,mySQL,Postgre..
Left by guide to online Bingo odds on Dec 08, 2009 9:27 PM

# re: Manage SQL Databases

Requesting Gravatar...
Double click the file builddatabase.bat to test the creation of the database and the database schema.
Left by pandora jewelry on Dec 24, 2009 11:59 PM

# re: Manage SQL Databases

Requesting Gravatar...
What an amazing post that I have ever come through. It gives the information that I was really searching for the past week and I am really satisfied with this post. Need more like this. Thank you.
Left by designer sunglasses on Dec 25, 2009 10:19 AM

# re: Manage SQL Databases

Requesting Gravatar...
Great tips. Thanks for good information!
Left by Web design service on Dec 30, 2009 8:01 AM

# re: Manage SQL Databases

Requesting Gravatar...
very informative article. Thanks a lot for sharing.
Left by Sally blog on Jan 10, 2010 8:39 PM

# re: Manage SQL Databases

Requesting Gravatar...
Greetings to you. I would like to maintain two database. one for storing the data from the GUI and another database for reporting service(SSRS).Now i want to move the data from orginal database to reporting database. how can i handle, either through trigger or any other method.Need Advice.
Left by astuces du casino on Jan 14, 2010 8:00 PM

# re: Manage SQL Databases

Requesting Gravatar...
thanks for sharing it
Left by make money online on Jan 16, 2010 1:17 PM

# re: Manage SQL Databases

Requesting Gravatar...

I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. Thanks for sharinging.
Left by International Foods Online on Jan 16, 2010 3:04 PM

# re: Manage SQL Databases

Requesting Gravatar...
I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. Thanks for sharinging.
Left by Movers Brooklyn on Jan 16, 2010 3:06 PM

# re: Manage SQL Databases

Requesting Gravatar...

I admit, I have not been on this webpage in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues.
Great stuff as usual.
Left by new york bus charter on Jan 18, 2010 12:03 AM

# re: Manage SQL Databases

Requesting Gravatar...
Managing the large databases has become easy by the use of SQL database, thanks for sharing the information.
Left by Target on Jan 20, 2010 9:46 PM

# re: Manage SQL Databases

Requesting Gravatar...
Excellent post! I always enjoy a solid technical post (and code) It saved me a good week+ - Keep up the good work!
Left by Rapid Share on Jan 21, 2010 12:01 AM

# re: Manage SQL Databases

Requesting Gravatar...
Very nice post. Information given is nicely elaborated.
Left by last longer in bed on Jan 21, 2010 10:32 PM

# re: Manage SQL Databases

Requesting Gravatar...
What's more, a bracelet can also be equipped with different small pandora jewelry and even you can change it according to your mood at any time. Here are some meanings of pendant. Small Plane stands for traveling and adventure ; anchor, stability and hope; your baby's boots, having a lot of babies; small feeding pandora bracelets abundant food; Church means happiness and stability of marriage; dragonfly means riches; Eiffel Tower means travel and exploration; four-leaf clover means fortune; horseshoe means luck; Nest means a happy family; bride shows a happy bride in her coming pandora jewelleryship steering shows calming and confidence; pandora ukcoin pandora beadsshows rich marriage life. Wish bone, dreams being about to come true; pandora charm bracelets, love; one heart shot by an arrow, romantic love; purse, wealth; and heart-shaped lock, true love.yurtre4t
Left by pandora jewelry on Jan 28, 2010 6:45 PM

# re: Manage SQL Databases

Left by 25th Wedding Anniversary on Feb 02, 2010 10:55 PM

Your comment:

 (will show your gravatar)
 
Please add 8 and 5 and type the answer here: