Raise your hand if your database is under version control… That’s what I thought. Almost all developers will agree (unfortunately not all) that keeping your application’s source code under version control is a must. But if you think about it, your application is nothing without the database it uses to store all that important data. Why is the database not under source control?
Of course I don’t mean the acutal database data and log files but the schema and static data needed by your application to run.
Here are a few tips on how to effectively version control your database.
1. Create a baseline script.
First things first, you need to create a script that will generate your database as it is right now. This should include all database objects (tables, views, procs, etc) as well as insert scripts for all your static data tables (enum tables, lookup lists, etc). Tools like Red Gate SQL Compare and Data Compare work great for this initial script creation.
2. Create a database change policy.
This doesn’t have to a beauricratic nightmare requiring signatures in triplicate, but it should be strictly enfored. Code that requires schema or static data changes SHOULD NOT be checked it without an appropriate script to make the changes. The script should be re-runnable. It should check for the changes using clauses like IF NOT EXISTS so it can be run repeatedly and not fail on subsequent runs.
3. Integrate with your CI process
You don’t have a CI process? That’s a post for another day. If you do have a Continous Integration process, include these change scripts in it. Set up your build server to run scripts against a development database to verify that they work. If the script fails, the build fails.
4. Developers SHOULD NOT be working against a central database. Each developer should have their own copy of the database. Trying to work off a single instance will cause developers to step on each other’s toes as they make conflicting changes. It’s like having everyone work on code on a network share instead of a proper source control system, it doesn’t work.
So what are the benefits of doing all this work?
–You’ll be able to see every change that happens to your schema as it was made. No trying to remember when that column was removed or that datatype changed.
–Developers will be able to work faster. Your developers want have to worry about pulling down the latest code and not being able to build or run the app. They can simply grab the scripts and run them to update their copy of the database to the version they just got from source control. No downtime downloading an restoring the dev server backup. Unfortunately, several organizations work this way an it make getting source update a real nightmare.
Some version control don’ts.
DON’T create a script for every database object, use change scripts instead. Creating a script for every object in your database quickly becomes unweildy as the number of objects grows and usually the scripts are not kept up to date, especially for table objects.
DON’T simply rely on tools. While tools like SQL Compare are great at creating change scripts, they do fall down in some situations. Don’t just do a compare and check it in. The scripts need to be re-runnable. Also, tools like Red Gate don’t catch situations where you want to add a new NON nullable column to a database with existing data. If you don’t specify a database default, the script will fail. However, you may not want to have a default on the column but of course, you need to specify data to fill in for the existing rows. In these cases, you will have to create the script yourself or do something like create a default value and then delete the default clause from the column at the end of the script.
Experiment with these guidelines above to find out what works for your time. I’m confident that integrate database version control into your process, it will pay dividens immediately.
Raise your hand if your database is under version control… That’s what I thought. Almost all developers will agree (unfortunately not all) that keeping your application’s source code under version control is a must. But if you think about it, your application is nothing without the database it uses to store all that important data. Why is the database not under source control?
Of course I don’t mean the actual database data and log files but the schema and static data needed by your application to run.
Here are a few tips on how to effectively version control your database.
1. Create a baseline script.
First things first, you need to create a script that will generate your database as it is right now. This should include all database objects (tables, views, procs, etc) as well as insert scripts for all your static data tables (enum tables, lookup lists, etc). Tools like Red Gate SQL Compare and Data Compare work great for this initial script creation.
2. Create a database change policy.
This doesn’t have to a bureaucratic nightmare requiring signatures in triplicate, but it should be strictly enforced. Code that requires schema or static data changes SHOULD NOT be checked it without an appropriate script to make the changes. The script should be re-runnable. It should check for the changes using clauses like IF NOT EXISTS so it can be run repeatedly and not fail on subsequent runs.
3. Integrate with your CI process
You don’t have a CI process? That’s a post for another day. If you do have a Continuous Integration process, include these change scripts in it. Set up your build server to run scripts against a development database to verify that they work. If the script fails, the build fails.
4. Developers SHOULD NOT be working against a central database. Each developer should have their own copy of the database. Trying to work off a single instance will cause developers to step on each other’s toes as they make conflicting changes. It’s like having everyone work on code on a network share instead of a proper source control system, it doesn’t work.
So what are the benefits of doing all this work?
–You’ll be able to see every change that happens to your schema as it was made. No trying to remember when that column was removed or that data type changed.
–Developers will be able to work faster. Your developers won’t have to worry about pulling down the latest code and not being able to build or run the app. They can simply grab the scripts and run them to update their copy of the database to the version they just got from source control. No downtime downloading and restoring the dev server backup. Unfortunately, several organizations work this way and it makes getting your source code up to date a real nightmare.
Some version control don’ts.
DON’T create a script for every database object, use change scripts instead. Creating a script for every object in your database quickly becomes unwieldy as the number of objects grows and usually the scripts are not kept up to date, especially for table objects.
DON’T simply rely on tools. While tools like SQL Compare are great at creating change scripts, they do fall down in some situations. Don’t just do a compare and check it in. The scripts need to be re-runnable. Also, tools like Red Gate don’t catch situations where you want to add a new non nullable column to a database with existing data. If you don’t specify a database default, the script will fail. However, you may not want to have a default on the column but of course, you need to specify data to fill in for the existing rows. In these cases, you will have to create the script yourself or do something like create a default value and then delete the default clause from the column at the end of the script.
Experiment with these guidelines above to find out what works for your time. I’m confident that if you integrate database version control into your process, it will pay dividends immediately.