Skip to main content
Email Password   helpLost your password?

Files output of the tool

Introduction

If you are using a source control system to manage your project's source code, you have probably wondered if you could do the same with your database. You can! Here's my take at it: ScriptDB4Svn does what its name suggests: it scripts your database for usage in a source control system. Because I use SVN with the TortoiseSVN shell extensions, I've targeted this project to be used with SVN, but you can probably use it with other source control systems.

Please note that the tool relies upon the Scripting tool from Microsoft which was shipped with SQL Server 2000, and will only have been installed on your system if you chose for backward compatibility with SQL Server 7.0 during the installation. If you are missing the tool (scptxfr.exe) it can be found on the SQL Server 2000 installation CD.

The tool was tested on SQL Server 2000 only. I don't know if it works with SQL Server 2005.

What does ScriptDB4Svn do exactly?

The tool scripts Microsoft SQL Server tables, relationships, views, stored procedures, user defined types, defaults, rules, triggers, functions and data into individual .sql files. Those individual files can then be added to your source control system and become available for versioning. The tool can also be used to automatically create a clean database from generated scripts, which can be very useful for integration in any tools like NAnt, MSBuild, CC.net etc.

What are the benefits?

There are numerous benefits; these are just the ones I came up with. If you have any additions, let me know!

Working with ScriptDB4Svn - Manual

Put a copy of ScriptDB4SVN.exe and its .config file in the folder where you want your database scripted. Open the .config file and make proper changes to it. The file contains comments that will tell you what the different settings are for.

If you are going to use the tool in your development team, I recommend you to have everyone in your team read this article. I will save you some headaches!

How to manage database changes

  1. If any project-member makes a change to anything in their version of the database, they simply run the scripting tool to regenerate the scripts, and then commit the changes to the SVN repository.
  2. Upon executing an SVN Update command, any updated scripts are copied to other team members� local versions. Once they run the scripting tool again, the database changes are reflected in Windows Explorer by TortoiseSVN. By viewing the TortoiseSVN Diff view of the table/view/whatever, the exact changes made by the developer become visible.
  3. The other team members make the appropriate changes to their local databases, either by executing the new script, or by making the changes manually in SQL Server.
  4. Once the tool generates scripts identical to the ones in the repository, the databases are in-sync again!

A paradoxical thing about this approach is that changes made by other developers only become visible after the scripts are regenerated locally. This way, it appears that the remote database changes have been �undone� locally, while in fact the remote changes haven�t been processed (copied) yet. Any �uncommitted� changes a developer has made to their local database, will appear in the same way remote developers� �unprocessed� changes appear in Windows Explorer.

Handling/resolving data differences

Resolving data conflict can be a pain because of relationships that assure the relational integrity of the data is maintained. If the data in more than one table has changed, simply executing a delete statement or inserting data can cause errors. In these situations it is quite useful to remove those constraints, update the data and then recreate the relationships. When done, the dropped constraints can easily be re-created from the Relationship scripts. Follow these steps to avoid annoyances:

  1. SVN Revert your version of the data script to the HEAD ('their') revision.
  2. Open the script in Query Analyzer, connect to the right database
  3. Execute the script
  4. If step 3 generated an error, find out what constraint caused the error, above the DELETE FROM the_current_table line, add: ALTER TABLE table_that_contains_the_constraint DROP CONSTRAINT FK_the_name_of_the_annoying_constraint, now try step 3 again. Repeat this until the script executes without errors.
  5. Repeat steps 1 to 4 until all data changes have been made.
  6. Rescript your database. If you dropped any constraints in step 3, recreate them by executing the appropriate relationship scripts (SVN Revert the file, strip the lines for constraints that still exist, execute)

Important note: Watch out for relationships that have CASCADE DELETE's enabled! Deleting data in a source controlled table could then result in the loss of data of other -possibly not source controlled- tables!!

Handling/resolving minor script differences

There are a couple of situations where differences in generated scripts can occur, when they aren�t really different. Here�s how you can resolve them:

  1. Index/PK names: To resolve a difference in the name of a Primary key or index, in SQL Query Analyzer, execute: sp_rename 'myname', 'theirname'
  2. CASE differences in table & column names: execute sp_rename 'dbo.Table.someColumn', 'SomeColumn'
  3. CASE differences in CREATE statements: for some odd reason SQL Server remembers the case of the CREATE statement when it was executed. For any other object than tables, SVN Revert your version of the .sql file to the HEAD revision, open the file in Query Analyzer and execute it.
  4. COLLATION differences: these can be a real pain. If the developers in a team are using different collation settings in SQL Server, any textual columns like varchar and text will contain different collations. Make sure to all use the same collation settings! If you aren�t bothered by different collation settings, you can optionally turn �IgnoreCollation� on in the .config file of the tool.

Handling conflicts

Sometimes conflicts can occur in the script files. Usually this is the result of not committing the generated scripts after local database changes have been made. The best way to resolve conflicts is:

  1. Regenerate your scripts, any conflict files will be removed automatically.
  2. Now, check the Diff and make any pending changes to your database.
  3. Regenerate the scripts again and commit your file.

Best practices

Follow these guidelines to prevent yourself from getting into problems.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralSQL Comments Result in Missing Objects/Scripts Pin
Eric Dagenais
12:48 17 Aug '09  
GeneralRe: SQL Comments Result in Missing Objects/Scripts Pin
ken matesich
20:22 20 Sep '09  
QuestionPROBLEM with SQL 2005/8 ForeignKeys and RegExp Pin
Bernd Wessels
19:55 29 Apr '09  
GeneralProblems executing scripts Pin
Member 660218
6:25 20 Mar '09  
GeneralLooks Great... but the new tool is the Database Publishing Wizard Pin
John Albrecht
8:07 14 Jan '09  
GeneralUsing with SQL 2008 Pin
davidm99
5:04 6 Jan '09  
GeneralRe: Using with SQL 2008 Pin
EddieSpooner
22:21 14 Apr '09  
GeneralMoving from 2000 to 2005 Pin
rightfield
3:33 13 Aug '08  
GeneralRe: Moving from 2000 to 2005 Pin
beckerben
4:12 24 Aug '08  
GeneralRe: Moving from 2000 to 2005 Pin
peter.stuart
10:48 12 Oct '08  
GeneralRe: Moving from 2000 to 2005 Pin
peter.stuart
2:56 14 Oct '08  
GeneralRe: Moving from 2000 to 2005 Pin
hammonddb
6:32 18 Nov '08  
GeneralRe: Moving from 2000 to 2005 Pin
sunweichao
23:50 3 Feb '09  
QuestionRe: Moving from 2000 to 2005 Pin
Koroshiya
11:21 17 Mar '09  
GeneralRe: Moving from 2000 to 2005 Pin
Member 1924962
2:58 6 Jul '09  
GeneralBug fix (System.NullReferenceException:) Pin
White X Dragon
4:34 9 Apr '08  
GeneralHas anyone tried SQL Server 2005 ?? Pin
budtse
3:13 1 Apr '08  
GeneralNeed an option in the config for sql delete exclusions Pin
toxaq
16:03 19 Mar '08  
Questionvarbinary(max) problems Pin
Member 3858438
9:25 21 Feb '08  
AnswerRe: varbinary(max) problems Pin
Ezweb25
8:34 10 Mar '08  
GeneralTries scripting CREATE statements that have been commented out Pin
flipdoubt
11:39 6 Feb '08  
GeneralRe: Tries scripting CREATE statements that have been commented out Pin
flipdoubt
12:02 6 Feb '08  
GeneralRegex Issue with View name used for creating the script file... Pin
Member 2074228
6:06 18 Jan '08  
GeneralRe: Regex Issue with View name used for creating the script file... Pin
Beetlebub
13:38 4 May '09  
GeneralRegEx issue with scripted statement permissions Pin
Member 4493550
2:55 10 Dec '07  


Last Updated 13 Oct 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009