<p>I often run into the following problem.</p>

<p>I work on some changes to a project that require new tables or columns in the database. I make the database modifications and continue my work. Usually, I remember to write down the changes so that they can be replicated on the live system. However, I don't always remember what I've changed and I don't always remember to write it down.</p>

<p>So, I make a push to the live system and get a big, obvious error that there is no <code>NewColumnX</code>, ugh.</p>

<p>Regardless of the fact that this may not be the best practice for this situation, is there a version control system for databases? I don't care about the specific database technology. I just want to know if one exists. If it happens to work with MS SQL Server, then great.</p>

[{'answer_id': 310, 'author': 'Ryan Fox', 'author_id': 55, 'author_profile': 'https://Stackoverflow.com/users/55', 'pm_score': 3, 'selected': False, 'text': "<p>Most database engines should support dumping your database into a file. I know MySQL does, anyway. This will just be a text file, so you could submit that to Subversion, or whatever you use. It'd be easy to run a diff on the files too.</p>\n"}, {'answer_id': 356, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 3, 'selected': False, 'text': '<p>For Oracle, I use <a href="http://www.toadsoft.com/" rel="noreferrer">Toad</a>, which can dump a schema to a number of discrete files (e.g., one file per table).  I have some scripts that manage this collection in Perforce, but I think it should be easily doable in just about any revision control system.</p>\n'}, {'answer_id': 360, 'author': 'Kalid', 'author_id': 109, 'author_profile': 'https://Stackoverflow.com/users/109', 'pm_score': 7, 'selected': True, 'text': '<p>In Ruby on Rails, there\'s a concept of a <a href="http://wiki.rubyonrails.org/rails/pages/UnderstandingMigrations" rel="noreferrer">migration</a> -- a quick script to change the database.</p>\n\n<p>You generate a migration file, which has rules to increase the db version (such as adding a column) and rules to downgrade the version (such as removing a column). Each migration is numbered, and a table keeps track of your current db version.</p>\n\n<p>To <em>migrate up</em>, you run a command called "db:migrate" which looks at your version and applies the needed scripts. You can migrate down in a similar way.</p>\n\n<p>The migration scripts themselves are kept in a version control system -- whenever you change the database you check in a new script, and any developer can apply it to bring their local db to the latest version.</p>\n'}, {'answer_id': 363, 'author': 'Peter Coulton', 'author_id': 117, 'author_profile': 'https://Stackoverflow.com/users/117', 'pm_score': 3, 'selected': False, 'text': '<p>There\'s a PHP5 "database migration framework" called Ruckusing. I haven\'t used it, but the <a href="http://code.google.com/p/ruckusing/wiki/CompleteExamples" rel="noreferrer">examples</a> show the idea, if you use the language to create the database as and when needed, you only have to track source files.</p>\n'}, {'answer_id': 36256, 'author': 'hamishmcn', 'author_id': 3590, 'author_profile': 'https://Stackoverflow.com/users/3590', 'pm_score': 3, 'selected': False, 'text': '<p>I write my db release scripts in parallel with coding, and keep the release scripts in a project specific section in SS. If I make a change to the code that requires a db change, then I update the release script at the same time.\nPrior to release, I run the release script on a clean dev db (copied structure wise from production) and do my final testing on it.</p>\n'}, {'answer_id': 36780, 'author': 'Matthew Watson', 'author_id': 3839, 'author_profile': 'https://Stackoverflow.com/users/3839', 'pm_score': 3, 'selected': False, 'text': '<p>Have your initial create table statements in version controller, then add alter table statements, but never edit files, just more alter files ideally named sequentially, or even as a "change set", so you can find all the changes for a particular deployment. </p>\n\n<p>The hardiest part that I can see, is tracking dependencies, eg, for a particular deployment table B might need to be updated before table A.</p>\n'}, {'answer_id': 37567, 'author': 'steevc', 'author_id': 1895, 'author_profile': 'https://Stackoverflow.com/users/1895', 'pm_score': 2, 'selected': False, 'text': "<p>In the absence of a VCS for table changes I've been logging them in a wiki. At least then I can see when and why it was changed. It's far from perfect as not everyone is doing it and we have multiple product versions in use, but better than nothing.</p>\n"}, {'answer_id': 38419, 'author': 'Mike Farmer', 'author_id': 4082, 'author_profile': 'https://Stackoverflow.com/users/4082', 'pm_score': 3, 'selected': False, 'text': '<p>Take a look at the oracle package DBMS_METADATA. </p>\n\n<p>In particular, the following methods are particularly useful: </p>\n\n<ul>\n<li><code>DBMS_METADATA.GET_DDL</code></li>\n<li><code>DBMS_METADATA.SET_TRANSFORM_PARAM</code></li>\n<li><code>DBMS_METADATA.GET_GRANTED_DDL</code></li>\n</ul>\n\n<p>Once you are familiar with how they work (pretty self explanatory) you can write a simple script to dump the results of those methods into text files that can be put under source control.  Good luck!</p>\n\n<p>Not sure if there is something this simple for MSSQL.</p>\n'}, {'answer_id': 55134, 'author': 'Perry Tribolet', 'author_id': 5668, 'author_profile': 'https://Stackoverflow.com/users/5668', 'pm_score': 3, 'selected': False, 'text': '<p>If you\'re using SQL Server it would be hard to beat Data Dude (aka the Database Edition of Visual Studio).  Once you get the hang of it, doing a schema compare between your source controlled version of the database and the version in production is a breeze. And with a click you can generate your diff DDL.</p>\n\n<p>There\'s an instructional <a href="http://msdn.microsoft.com/en-us/vsts2008/cc659682.aspx" rel="noreferrer">video</a> on MSDN that\'s very helpful.</p>\n\n<p>I know about DBMS_METADATA and Toad, but if someone could come up with a Data Dude for Oracle then life would be really sweet.</p>\n'}, {'answer_id': 63685, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>I\'ve done this off and on for years -- managing (or trying to manage) schema versions.  The best approaches depend on the tools you have.  If you can get the Quest Software tool "Schema Manager" you\'ll be in good shape.  Oracle has its own, inferior tool that is also called "Schema Manager" (confusing much?) that I don\'t recommend.</p>\n\n<p>Without an automated tool (see other comments here about Data Dude) then you\'ll be using scripts and DDL files directly.  Pick an approach, document it, and follow it rigorously.  I like having the ability to re-create the database at any given moment, so I prefer to have a full DDL export of the entire database (if I\'m the DBA), or of the developer schema (if I\'m in product-development mode).</p>\n'}, {'answer_id': 84735, 'author': 'borjab', 'author_id': 16206, 'author_profile': 'https://Stackoverflow.com/users/16206', 'pm_score': 3, 'selected': False, 'text': '<p>PLSQL Developer, a tool from All Arround Automations, has a plugin for repositories that works OK ( but not great) with Visual Source Safe.</p>\n\n<blockquote>\n  <p>From the web: </p>\n  \n  <blockquote>\n    <p>The Version Control Plug-In provides a tight integration between the PL/SQL Developer IDE >>and any Version Control System that supports the Microsoft SCC Interface Specification. >>This includes most popular Version Control Systems such as Microsoft Visual SourceSafe, >>Merant PVCS and MKS Source Integrity.</p>\n  </blockquote>\n</blockquote>\n\n<p><a href="http://www.allroundautomations.com/plsvcs.html" rel="noreferrer">http://www.allroundautomations.com/plsvcs.html</a></p>\n'}, {'answer_id': 85938, 'author': 'Bob Probst', 'author_id': 12424, 'author_profile': 'https://Stackoverflow.com/users/12424', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://www.embarcadero.com/products/erstudio/" rel="noreferrer">ER Studio</a> allows you to reverse your database schema into the tool and you can then compare it to live databases.  </p>\n\n<p>Example:  Reverse your development schema into ER Studio -- compare it to production and it will list all of the differences.  It can script the changes or just push them through automatically.</p>\n\n<p>Once you have a schema in ER Studio, you can either save the creation script or save it as a proprietary binary and save it in version control.  If you ever want to go back to a past version of the scheme, just check it out and push it to your db platform.</p>\n'}, {'answer_id': 116552, 'author': 'MattMcKnight', 'author_id': 8136, 'author_profile': 'https://Stackoverflow.com/users/8136', 'pm_score': 2, 'selected': False, 'text': '<p>Two book recommendations: "Refactoring Databases" by Ambler and Sadalage and "Agile Database Techniques" by Ambler.</p>\n\n<p>Someone mentioned Rails Migrations. I think they work great, even outside of Rails applications. I used them on an ASP application with SQL Server which we were in the process of moving to Rails. You check the migration scripts themselves into the VCS. \nHere\'s <a href="http://pragdave.blogs.pragprog.com/pragdave/2006/07/migrations_outs.html" rel="nofollow noreferrer">a post by Pragmatic Dave Thomas</a> on the subject.</p>\n'}, {'answer_id': 140953, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>I\'d recommend one of two approaches. First, invest in <a href="http://www.sybase.com/products/modelingdevelopment/powerdesigner" rel="nofollow noreferrer">PowerDesigner</a> from Sybase. Enterprise Edition. It allows you to design Physical datamodels, and a whole lot more. But it comes with a repository that allows you to check in your models. Each new check in can be a new version, it can compare any version to any other version and even to what is in your database at that time. It will then present a list of every difference and ask which should be migrated… and then it builds the script to do it. It’s not cheap but it’s a bargain at twice the price and it’s ROI is about 6 months.</p>\n\n<p>The other idea is to turn on DDL auditing (works in Oracle). This will create a table with every change you make. If you query the changes from the timestamp you last moved your database changes to prod to right now, you’ll have an ordered list of everything you’ve done. A few where clauses to eliminate zero-sum changes like create table foo; followed by drop table foo; and you can EASILY build a mod script. Why keep the changes in a wiki, that’s double the work. Let the database track them for you.</p>\n'}, {'answer_id': 141057, 'author': 'marc', 'author_id': 12260, 'author_profile': 'https://Stackoverflow.com/users/12260', 'pm_score': 2, 'selected': False, 'text': '<p>We\'ve used <a href="http://msdn.microsoft.com/en-us/vsts2008/products/bb933747.aspx" rel="nofollow noreferrer">MS Team System Database Edition</a> with pretty good success.  It integrates with TFS version control and Visual Studio more-or-less seamlessly and allows us to manages stored procs, views, etc., easily.  Conflict resolution can be a pain, but version history is complete once it\'s done.  Thereafter, migrations to QA and production are extremely simple.</p>\n\n<p>It\'s fair to say that it\'s a version 1.0 product, though, and is not without a few issues.</p>\n'}, {'answer_id': 141765, 'author': 'dar7yl', 'author_id': 9505, 'author_profile': 'https://Stackoverflow.com/users/9505', 'pm_score': 5, 'selected': False, 'text': "<p>I'm a bit old-school, in that I use source files for creating the database.  There are actually 2 files - project-database.sql and project-updates.sql - the first for the schema and persistant data, and the second for modifications.  Of course, both are under source control.</p>\n\n<p>When the database changes, I first update the main schema in project-database.sql, then copy the relevant info to the project-updates.sql, for instance ALTER TABLE statements.\nI can then apply the updates to the development database, test, iterate until done well.\nThen, check in files, test again, and apply to production.</p>\n\n<p>Also, I usually have a table in the db - Config - such as:</p>\n\n<p><strong>SQL</strong></p>\n\n<pre><code>CREATE TABLE Config\n(\n    cfg_tag VARCHAR(50),\n    cfg_value VARCHAR(100)\n);\n\nINSERT INTO Config(cfg_tag, cfg_value) VALUES\n( 'db_version', '$Revision: $'),\n( 'db_revision', '$Revision: $');\n</code></pre>\n\n<p>Then, I add the following to the update section:</p>\n\n<pre><code>UPDATE Config SET cfg_value='$Revision: $' WHERE cfg_tag='db_revision';\n</code></pre>\n\n<p>The <code>db_version</code> only gets changed when the database is recreated, and the <code>db_revision</code> gives me an indication how far the db is off the baseline.</p>\n\n<p>I could keep the updates in their own separate files, but I chose to mash them all together and use cut&amp;paste to extract relevant sections.  A bit more housekeeping is in order, i.e., remove ':' from $Revision 1.1 $ to freeze them.</p>\n"}, {'answer_id': 918903, 'author': 'Alex', 'author_id': 42707, 'author_profile': 'https://Stackoverflow.com/users/42707', 'pm_score': 4, 'selected': False, 'text': '<p>I highly recommend <a href="http://www.sqldelta.com/deltasqlserver.html" rel="noreferrer">SQL delta</a>. I just use it to generate the diff scripts when i\'m done coding my feature and check those scripts into my source control tool (Mercurial :))</p>\n\n<p>They have both an SQL server &amp; Oracle version.</p>\n'}, {'answer_id': 2035928, 'author': 'David Atkinson', 'author_id': 167364, 'author_profile': 'https://Stackoverflow.com/users/167364', 'pm_score': 2, 'selected': False, 'text': '<p>Schema Compare for Oracle is a tool specifically designed to migrate changes from our Oracle database to another. Please visit the URL below for the download link, where you will be able to use the software for a fully functional trial.</p>\n\n<p><a href="http://www.red-gate.com/Products/schema_compare_for_oracle/index.htm" rel="nofollow noreferrer">http://www.red-gate.com/Products/schema_compare_for_oracle/index.htm</a></p>\n'}, {'answer_id': 3220892, 'author': 'Karussell', 'author_id': 194609, 'author_profile': 'https://Stackoverflow.com/users/194609', 'pm_score': 4, 'selected': False, 'text': '<p>I wonder that no one mentioned the open source tool <a href="http://www.liquibase.org/" rel="noreferrer">liquibase</a> which is Java based and should work for nearly every database which supports jdbc. Compared to rails it uses xml instead ruby to perform the schema changes. Although I dislike xml for domain specific languages the very cool advantage of xml is that liquibase knows how to roll back certain operations like </p>\n\n<pre><code>&lt;createTable tableName="USER"&gt; \n   &lt;column name="firstname" type="varchar(255)"/&gt;\n&lt;/createTable&gt;\n</code></pre>\n\n<p>So you don\'t need to handle this of your own</p>\n\n<p>Pure sql statements or data imports are also supported.</p>\n'}, {'answer_id': 3220981, 'author': 'Chadwick', 'author_id': 80714, 'author_profile': 'https://Stackoverflow.com/users/80714', 'pm_score': 4, 'selected': False, 'text': '<p><a href="http://mybatis.org/" rel="noreferrer">MyBatis</a> (formerly iBatis) has a <a href="http://mybatis.org/java.html" rel="noreferrer">schema migration</a>, tool for use on the command line. It is written in java though can be used with any project.</p>\n\n<blockquote>\n  <p>To achieve a good database change management practice, we need to identify a few key goals.\n  Thus, the MyBatis Schema Migration System (or MyBatis Migrations for short) seeks to:</p>\n</blockquote>\n\n<ul>\n<li>Work with any database, new or existing</li>\n<li>Leverage the source control system (e.g. Subversion)</li>\n<li>Enable concurrent developers or teams to work independently</li>\n<li>Allow conflicts very visible and easily manageable</li>\n<li>Allow for forward and backward migration (evolve, devolve respectively)</li>\n<li>Make the current status of the database easily accessible and comprehensible</li>\n<li>Enable migrations despite access privileges or bureaucracy</li>\n<li>Work with any methodology</li>\n<li>Encourages good, consistent practices</li>\n</ul>\n'}, {'answer_id': 6626232, 'author': 'EndangeredMassa', 'author_id': 106, 'author_profile': 'https://Stackoverflow.com/users/106', 'pm_score': 4, 'selected': False, 'text': '<p>Redgate has a product called <a href="http://www.red-gate.com/products/sql-development/sql-source-control/" rel="noreferrer">SQL Source Control</a>. It integrates with TFS, SVN, SourceGear Vault, Vault Pro, Mercurial, Perforce, and Git.</p>\n'}, {'answer_id': 27602289, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>You can use <a href="http://msdn.microsoft.com/en-gb/data/tools.aspx" rel="nofollow">Microsoft SQL Server Data Tools</a> in visual studio to generate scripts for database objects as part of a SQL Server Project. You can then add the scripts to source control using the source control integration that is built into visual studio. Also, SQL Server Projects allow you verify the database objects using a compiler and generate deployment scripts to update an existing database or create a new one.</p>\n'}]
['https://Stackoverflow.com/questions/308', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/106/']