views:

239

answers:

4

If I'm working on a development server and have updates to the database structure for some of our releases, what is the best way to update the structure on the production server?

Currently we create a new production database containing the structure only, do a SQL dump of the data on the 'old' production database, then run a SQL query to insert the data into the new database.

I know there is an easier way to do these updates, right?

Thanks in advance.

+2  A: 

Look at alter table to change the schema

It might not be easier than your method but it means less copying of the database

Mark
I've been looking into this, and it's definitely the most sensible option, but I was looking for something more along the lines of automating the process, perhaps by scripting the changes. Thanks.
tappit
Oh yes script the changes - infact script anything that will run against a production database. So you can test it first, act as some form of log and allow someone else to check your chnage - you don't want to corrupt data,
Mark
A: 

Use the ALTER TABLE command: http://dev.mysql.com/doc/refman/5.0/en/alter-table.html

Bill
+1  A: 

This is actually quite a deep question. If the only changes you've made are to add some columns then ALTER TABLE is probably sufficient. But if you're renaming or deleting columns then ALTER statements may break various foreign key constraints. In addition, sometimes you need to make changes both to the database and the data, which is pretty much unscriptable.

Most likely the best way to automate this would be to write a simple script for each deployment (along with a script to roll back!) which is basically what some systems like Rails will do for you I believe. Some scripts might be simply ALTER statements, some might temporarily disable foreign-key checking and triggers etc, some might run some update statements as well. And some might be dumping the db and rebuilding it. I don't think there's a one-size-fits-all solution here, sorry :)

njk
You're talking about DDL and DML - refer to the link in my answer for details.
OMG Ponies
Yes I am, just trying to be a bit less terse :)
njk
+1  A: 

We don't run anything on prod without a script and that script must be in source control. Additionally we have to write a rollback script in case the inital script goes bad and we have to back it out. And when we move to prod configuration management does a differential compare between prod and dev to see if we have missed anything in the production script (any differnces have to be traceable to development we are not yet ready to move to prod and documented). A product like Red-gate's SQl compare can do this. Our process is very formalized so that we can maintain a certification required by our larger clients.

If you have large tables even alter table can be slow, but it's still generally more efficient in total time than making a copy of the table with a new name and structure, copying the data to that table, renaming the old table, then naming the new table the name of the orginal table, then deleting the old table.

However, there are times when that is a preferable process as the total down time apparent to the user in this case is the time it takes to rename two tables, so this is good for tables where the data only is filled from the backend not the application (if the application can update the tables, it is a dangerous practice to do this as you may lose changes made while the tables were in transition). Alot of what process to use depends on the nature of the change you are making. Some changes should be done in a maintenance window where the users are not allowed to access the database. For instance if you are adding a new filed with a default value to a table with 100,000,000 records, you are liable to lock up the users form using the table while the update happens. It is better to do this in single user mode during off hours (and when the users are told in advance the dabase will not be available). Other changes only take milliseconds and can happen easily while users are logged in.

HLGEM