tags:

views:

443

answers:

6

Hi there.

I'm working with PHP & mySQL. I've finally got my head around source control and am quite happy with the whole development (testing) v production v repository thing for the PHP part.

My new quandary is what to do with the database. Do I create one for the test environment and one for the production environment? I currently have just the one which both environments use, leaving my test data sitting there. I kind of feel that I should have two, but I'm nervous in terms of making sure that my production database looks and feels exactly the same as my test one.

Any thoughts on which way to go? And, if you think the latter, what the best way is to keep the two databases the same (apart from the data, of course...)?

+3  A: 

You should definitely have two. As far as keeping them in sync, you should always create DDL for creating your database objects. Treat these scripts as you do you PHP code - keep them in version control. Anytime you have to modify the test database, make a script to do so, and check it in. Then you can propogate those changes to the production system once you are ready.

Brett McCann
DDL? I've never heard that acronym before.
Thomas Owens
@Thomas - Data Definition Language. Ie "CREATE TABLE ...."
Paul Tomblin
Ah. That's I'm familiar with. I guess I've just never seen it abbreviated before.
Thomas Owens
you might find "SHOW CREATE TABLE tablename" handy. You can create and modify your table (adding indices and changing datatypes) using your tool of preference, and then create the DDL for the finished article.
Ken
+13  A: 

Each environment should have a separate database. Script all of the database objects (tables, views, procedures, etc) and store the scripts in source control. The scripts are applied first to the development database, then promoted to test (QA, UAT, etc), then production. By applying the same scripts to each database, they should all be the same in the end.

If you have data that needs to be loaded (code tables, lookup values, etc), script that data load as part of the database creation process.

By scripting everything and keeping it in source control, a database structure can be recreated at any time for any given build level.

ahockley
I do something similar with the source control, but a distinction I'd make here is; I script all objects into separate files, so the source control has a object level change history, vs one big file that's always changing.
John MacIntyre
Yes John, that's a good plan. That's what I do as well and I probably should've been more clear in my original answer. Each object (table, procedure, etc) is its own file.
ahockley
A: 

Once I've deployed my database, any changes made to my development database(s), are done in an SQL script (not a tool), and the script is saved, and numbered.

deploy.001.description.sql
deploy.002.description.sql
deploy.003.description.sql
... etc..

Then I run each of those scripts in order when I deploy.

Then I archive them into a directory called something like

\deploy.YYMMDD\

And start all over.

If I make a mistake, I never go back to the previous deploy script, I'll create a new script and put my fix in there.

Good luck

John MacIntyre
Good plan, but why wait until after deployment to start scripting? Script everything from the beginning and you're set!
ahockley
@ahockley-That's the initial deployment I was talking about. Until I actually have a working copy on production, I don't worry about it since I will use some type of tool to do my initial deployment. In SQLServer; I'd use backup and restore. Once it's in production, I get paranoid. ;-)
John MacIntyre
+2  A: 

As a minimum one database for each development workstation and one for production. Besides that you should have one for the test environment unless you are only one developer and have a similar setup as the production environment.

Jonas Kongslund
A: 

One thing I've been working with is creating a VM with the database installed. you can save the VM as a playfile, including its data. What you can do then is take a snapshot of the playfile, and start up as many different VM's as you want. They can all be identical, or you can modify one or another. Here's the good thing: assuming you have a dev version of the database that you want to go out, you can simply start that VM on your production server instead of the current server.

It's another problem altogether if you have production data that is not on your dev machines. In that case though, one thing you can do is set up a tracking VM. Run replication from your main DB to the tracking VM. When you get to a point where you need to run some alters on the production database, first stop the slave and save a snapshot.

Start an instance of that snapshot, take it out of slave mode entirely, apply your changes, and point your QA box at that database. If it works as intended, you can run the patches against your main production database. If not, bring up the snapshot, and get it replicating off the master again until you are ready to repeat the update test.

Zak
+1  A: 

See also

How do you version your database schema?

It's a common question and has been asked and answered many times.

Thomas Owens: Replication is not usable for versioning schemas - it is for duplicating data. You never want to replicate from dev to production or vice versa.

MarkR