<p>As you develop an application database changes inevitably pop up.  The trick I find is keeping your database build in step with your code.  In the past I have added a build step that executed SQL scripts against the target database but that is dangerous in so much as you could inadvertanly add bogus data or worse.  </p>

<p>My question is what are the tips and tricks to keep the database in step with the code?  What about when you roll back the code?  Branching?</p>

[{'answer_id': 33656, 'author': 'Josh', 'author_id': 3521, 'author_profile': 'https://Stackoverflow.com/users/3521', 'pm_score': 3, 'selected': True, 'text': '<p>Version numbers embedded in the database are helpful.  You have two choices, embedding values into a table (allows versioning multiple items) that can be queried, or having an explictly named object (such as a table or somesuch) you can test for.</p>\n\n<p>When you release to production, do you have a rollback plan in the event of unexpected catastrophe?  If you do, is it the application of a schema rollback script?  Use your rollback script to rollback the database to a previous code version.</p>\n'}, {'answer_id': 33666, 'author': 'Craig Walker', 'author_id': 3488, 'author_profile': 'https://Stackoverflow.com/users/3488', 'pm_score': 1, 'selected': False, 'text': "<blockquote>\n  <p>You should be able to create your database from scratch into a known state.</p>\n</blockquote>\n\n<p>While being able to do so is helpful (especially in the early stages of a new project), many (most?) databases will quickly become far too large for that to be possible. Also, if you have any BLOBs then you're going to have problems generating SQL scripts for your entire database. </p>\n\n<p>I've definitely been interested in some sort of DB versioning system, but I haven't found anything yet. So, instead of a solution, you'll get my vote. :-P</p>\n"}, {'answer_id': 33693, 'author': 'mk.', 'author_id': 1797, 'author_profile': 'https://Stackoverflow.com/users/1797', 'pm_score': 0, 'selected': False, 'text': "<p>I like the way that Django does it. You build models and the when you run a syncdb it applies the models that you have created. If you add a model you just need to run syncdb again. This would be easy to have your build script do every time you made a push. </p>\n\n<p>The problem comes when you need to alter a table that is already made. I do not think that syncdb handles that. That would require you to go in and manually add the table and also add a property to the model. You would probably want to version that alter statement. The models would always be under version control though, so if you needed to you could get a db schema up and running on a new box without running the sql scripts. Another problem with this is keeping track of static data that you always want in the db.</p>\n\n<p>Rails migration scripts are pretty nice too. </p>\n\n<p>A DB versioning system would be great, but I don't really know of such a thing. </p>\n"}, {'answer_id': 34200, 'author': 'Iain Holder', 'author_id': 1122, 'author_profile': 'https://Stackoverflow.com/users/1122', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>While being able to do so is helpful (especially in the early stages of a new project), many (most?) databases will quickly become far too large for that to be possible. Also, if you have any BLOBs then you're going to have problems generating SQL scripts for your entire database. </p>\n</blockquote>\n\n<p>Backups and compression can help you there.  Sorry - there's no excuse not to be able to get a a good set of data to develop against.  Even if it's just a sub-set.</p>\n"}, {'answer_id': 60849, 'author': 'Jay Bazuzi', 'author_id': 5314, 'author_profile': 'https://Stackoverflow.com/users/5314', 'pm_score': 1, 'selected': False, 'text': '<p>You really do want to be able to take a clean machine, get the latest version from source control, build in one step, and run all tests in one step.  Making this fast makes you produce good software faster.</p>\n\n<p>Just like <a href="https://stackoverflow.com/questions/49196/storing-third-party-libraries-in-source-control">external libraries</a>, database configuration must also be in source control.</p>\n\n<p>Note that I\'m not saying that all your live database <em>content</em> should be in the same source control, just enough to get to a clean state.  (Do back up your database content, though!)</p>\n'}, {'answer_id': 1377577, 'author': 'Andrew Swan', 'author_id': 10433, 'author_profile': 'https://Stackoverflow.com/users/10433', 'pm_score': 1, 'selected': False, 'text': '<p>Define your schema objects and your reference data in version-controlled text files. For example, you can define the schema in <a href="http://db.apache.org/torque/" rel="nofollow noreferrer">Torque</a> format, and the data in <a href="http://dbunit.sourceforge.net/" rel="nofollow noreferrer">DBUnit</a> format (both use XML). You can then use tools (we wrote our own) to generate the DDL and DML that take you from one version of your app to another. Our tool can take as input either (a) the previous version\'s schema &amp; data XML files or (b) an existing database, so you are always able to get a database of any state into the correct state.</p>\n'}, {'answer_id': 4003210, 'author': 'Christophe Fondacci', 'author_id': 484958, 'author_profile': 'https://Stackoverflow.com/users/484958', 'pm_score': 0, 'selected': False, 'text': '<p>Put your database developments under version control. I recommend to have a look at neXtep designer :\n<a href="http://www.nextep-softwares.com/wiki" rel="nofollow">http://www.nextep-softwares.com/wiki</a></p>\n\n<p>It is a free GPL product which offers a brand new approach to database development and deployment by connecting version information with a SQL generation engine which could automatically compute any upgrade script you need to upgrade any version of your database into another. Any existing database could be version controlled by a reverse synchronization. </p>\n\n<p>It currently supports Oracle, MySql and PostgreSql. DB2 support is under development. It is a full-featured database development environment where you always work on version-controlled elements from a repository. You can publish your updates by simple synchronization during development and you can generate exportable database deliveries which you will be able to execute on any targetted database through a standalone installer which validates the versions, performs structural checks and applies the upgrade scripts.</p>\n\n<p>The IDE also offers you SQL editors, dependency management, support for modular database model components, data model diagrams, SQL clients and much more.</p>\n\n<p>All the documentation and concepts could be found in the wiki.</p>\n'}]
['https://Stackoverflow.com/questions/33638', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2894/']