<p>What are the best methods for tracking and/or automating DB schema changes?  Our team uses Subversion for version control and we've been able to automate some of our tasks this way (pushing builds up to a staging server, deploying tested code to a production server) but we're still doing database updates manually.  I would like to find or create a solution that allows us to work efficiently across servers with different environments while continuing to use Subversion as a backend through which code and DB updates are pushed around to various servers.</p>

<p>Many popular software packages include auto-update scripts which detect DB version and apply the necessary changes.  Is this the best way to do this even on a larger scale (across multiple projects and sometimes multiple environments and languages)?  If so, is there any existing code out there that simplifies the process or is it best just to roll our own solution?  Has anyone implemented something similar before and integrated it into Subversion post-commit hooks, or is this a bad idea?</p>

<p>While a solution that supports multiple platforms would be preferable, we definitely need to support the Linux/Apache/MySQL/PHP stack as the majority of our work is on that platform.</p>

[{'answer_id': 1656, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 3, 'selected': False, 'text': "<p>It's kind of low tech, and there might be a better solution out there, but you could just store your schema in an SQL script which can be run to create the database.  I think you can execute a command to generate this script, but I don't know the command unfortunately.</p>\n<p>Then, commit the script into source control along with the code that works on it.  When you need to change the schema along with the code, the script can be checked in along with the code that requires the changed schema.  Then, diffs on the script will indicate diffs on schema changes.</p>\n<p>With this script, you could integrate it with DBUnit or some kind of build script, so it seems it could fit in with your already automated processes.</p>\n"}, {'answer_id': 1666, 'author': 'Joey deVilla', 'author_id': 216, 'author_profile': 'https://Stackoverflow.com/users/216', 'pm_score': 7, 'selected': True, 'text': '<p>In the Rails world, there\'s the concept of migrations, scripts in which changes to the database are made in Ruby rather than a database-specific flavour of SQL. Your Ruby migration code ends up being converted into the DDL specific to your current database; this makes switching database platforms very easy. </p>\n\n<p>For every change you make to the database, you write a new migration. Migrations typically have two methods: an "up" method in which the changes are applied and a "down" method in which the changes are undone. A single command brings the database up to date, and can also be used to bring the database to a specific version of the schema. In Rails, migrations are kept in their own directory in the project directory and get checked into version control just like any other project code.</p>\n\n<p><a href="http://www.oracle.com/technology/pub/articles/kern-rails-migrations.html" rel="noreferrer" title="Oracle guide to Ruby on Rails migrations">This Oracle guide to Rails migrations</a> covers migrations quite well.</p>\n\n<p>Developers using other languages have looked at migrations and have implemented their own language-specific versions. I know of <strong><a href="https://github.com/ruckus/ruckusing-migrations" rel="noreferrer" title="Ruckusing">Ruckusing</a></strong>, a PHP migrations system that is modelled after Rails\' migrations; it might be what you\'re looking for.</p>\n'}, {'answer_id': 1670, 'author': 'Dan', 'author_id': 230, 'author_profile': 'https://Stackoverflow.com/users/230', 'pm_score': 3, 'selected': False, 'text': '<p>If you are using C#, have a look at Subsonic, a very useful ORM tool, but is also generates sql script to recreated your scheme and\\or data. These scripts can then be put into source control.</p>\n\n<p><a href="http://subsonicproject.com/" rel="noreferrer">http://subsonicproject.com/</a></p>\n'}, {'answer_id': 1894, 'author': 'Louis Salin', 'author_id': 160, 'author_profile': 'https://Stackoverflow.com/users/160', 'pm_score': 2, 'selected': False, 'text': '<p>I create folders named after the build versions and put upgrade and downgrade scripts in there. For example, you could have the following folders: 1.0.0, 1.0.1 and 1.0.2. Each one contains the script that allows you to upgrade or downgrade your database between versions.</p>\n\n<p>Should a client or customer call you with a problem with version 1.0.1 and you are using 1.0.2, bringing the database back to his version will not be a problem.</p>\n\n<p>In your database, create a table called "schema" where you put in the current version of the database. Then writing a program that can upgrade or downgrade your database for you is easy.</p>\n\n<p>Just like Joey said, if you are in a Rails world, use Migrations. :)</p>\n'}, {'answer_id': 2755, 'author': 'Brandon Wood', 'author_id': 423, 'author_profile': 'https://Stackoverflow.com/users/423', 'pm_score': 4, 'selected': False, 'text': "<p>My team scripts out all database changes, and commits those scripts to SVN, along with each release of the application.  This allows for incremental changes of the database, without losing any data.  </p>\n\n<p>To go from one release to the next, you just need to run the set of change scripts, and your database is up-to-date, and you've still got all your data.  It may not be the easiest method, but it definitely is effective.</p>\n"}, {'answer_id': 3683, 'author': 'deadprogrammer', 'author_id': 556, 'author_profile': 'https://Stackoverflow.com/users/556', 'pm_score': 3, 'selected': False, 'text': '<p>Dump your schema into a file and add it to source control. Then a simple diff will show you what changed.</p>\n'}, {'answer_id': 6226, 'author': 'Fabio Gomes', 'author_id': 727, 'author_profile': 'https://Stackoverflow.com/users/727', 'pm_score': 3, 'selected': False, 'text': "<p>We use a very simple but yet effective solution.</p>\n\n<p>For new installs, we have a metadata.sql file in the repository which holds all the DB schema, then in the build process we use this file to generate the database.</p>\n\n<p>For updates, we add the updates in the software hardcoded. We keep it hardcoded because we don't like solving problems before it really IS a problem, and this kind of thing didn't prove to be a problem so far.</p>\n\n<p>So in our software we have something like this:</p>\n\n<p><code>RegisterUpgrade(1, 'ALTER TABLE XX ADD XY CHAR(1) NOT NULL;');</code></p>\n\n<p>This code will check if the database is in version 1 (which is stored in a table created automatically), if it is outdated, then the command is executed.</p>\n\n<p>To update the metadata.sql in the repository, we run this upgrades locally and then extract the full database metadata.</p>\n\n<p>The only thing that happens every so often, is to forget commiting the metadata.sql, but this isn't a major problem because its easy to test on the build process and also the only thing that could happen is to make a new install with an outdated database and upgraded it on the first use.</p>\n\n<p>Also we don't support downgrades, but it is by design, if something breaks on an update, we restored the previous version and fix the update before trying again.</p>\n"}, {'answer_id': 6232, 'author': 'tbreffni', 'author_id': 637, 'author_profile': 'https://Stackoverflow.com/users/637', 'pm_score': 3, 'selected': False, 'text': "<p>I've used the following database project structure in Visual Studio for several projects and it's worked pretty well:</p>\n\n<p>Database</p>\n\n<blockquote>\n  <p>Change Scripts</p>\n  \n  <blockquote>\n    <p>0.PreDeploy.sql</p>\n    \n    <p>1.SchemaChanges.sql</p>\n    \n    <p>2.DataChanges.sql</p>\n    \n    <p>3.Permissions.sql</p>\n  </blockquote>\n  \n  <p>Create Scripts</p>\n  \n  <blockquote>\n    <p>Sprocs</p>\n    \n    <p>Functions</p>\n    \n    <p>Views</p>\n  </blockquote>\n</blockquote>\n\n<p>Our build system then updates the database from one version to the next by executing the scripts in the following order:</p>\n\n<blockquote>\n  <p>1.PreDeploy.sql</p>\n  \n  <p>2.SchemaChanges.sql</p>\n  \n  <p>Contents of Create Scripts folder</p>\n  \n  <p>2.DataChanges.sql</p>\n  \n  <p>3.Permissions.sql</p>\n</blockquote>\n\n<p>Each developer checks in their changes for a particular bug/feature by appending their code onto the end of each file.  Once a major version is complete and branched in source control, the contents of the .sql files in the Change Scripts folder are deleted.</p>\n"}, {'answer_id': 22535, 'author': 'rix0rrr', 'author_id': 2474, 'author_profile': 'https://Stackoverflow.com/users/2474', 'pm_score': 6, 'selected': False, 'text': '<p>We use something similar to bcwoord to keep our database schemata synchronized across 5 different installations (production, staging and a few development installations), and backed up in version control, and it works pretty well. I\'ll elaborate a bit:</p>\n\n<hr>\n\n<p>To synchronize the database structure, we have a single script, update.php, and a number of files numbered 1.sql, 2.sql, 3.sql, etc. The script uses one extra table to store the current version number of the database. The N.sql files are crafted by hand, to go from version (N-1) to version N of the database.</p>\n\n<p>They can be used to add tables, add columns, migrate data from an old to a new column format then drop the column, insert "master" data rows such as user types, etc. Basically, it can do anything, and with proper data migration scripts you\'ll never lose data.</p>\n\n<p>The update script works like this:</p>\n\n<blockquote>\n  <ul>\n  <li>Connect to the database.</li>\n  <li>Make a backup of the current database (because stuff <em>will</em> go wrong) [mysqldump].</li>\n  <li>Create bookkeeping table (called _meta) if it doesn\'t exist.</li>\n  <li>Read current VERSION from _meta table. Assume 0 if not found.</li>\n  <li>For all .sql files numbered higher than VERSION, execute them in order</li>\n  <li>If one of the files produced an error: roll back to the backup</li>\n  <li>Otherwise, update the version in the bookkeeping table to the highest .sql file executed.</li>\n  </ul>\n</blockquote>\n\n<p>Everything goes into source control, and every installation has a script to update to the latest version with a single script execution (calling update.php with the proper database password etc.). We SVN update staging and production environments via a script that automatically calls the database update script, so a code update comes with the necessary database updates.</p>\n\n<p>We can also use the same script to recreate the entire database from scratch; we just drop and recreate the database, then run the script which will completely repopulate the database. We can also use the script to populate an empty database for automated testing.</p>\n\n<hr>\n\n<p>It took only a few hours to set up this system, it\'s conceptually simple and everyone gets the version numbering scheme, and it has been invaluable in having the ability to move forward and evolving the database design, without having to communicate or manually execute the modifications on all databases.</p>\n\n<p><em>Beware when pasting queries from phpMyAdmin though!</em> Those generated queries usually include the database name, which you definitely don\'t want since it will break your scripts! Something like CREATE TABLE <code>mydb</code>.<code>newtable</code>(...) will fail if the database on the system is not called mydb. We created a pre-comment SVN hook that will disallow .sql files containing the <code>mydb</code> string, which is a sure sign that someone copy/pasted from phpMyAdmin without proper checking.</p>\n'}, {'answer_id': 24208, 'author': 'Eric Scrivner', 'author_id': 2594, 'author_profile': 'https://Stackoverflow.com/users/2594', 'pm_score': 2, 'selected': False, 'text': '<p>For my current PHP project we use the idea of rails migrations and we have a migrations directory in which we keep files title "migration_XX.sql" where XX is the number of the migration.  Currently these files are created by hand as updates are made, but their creation could be easily modified.</p>\n\n<p>Then we have a script called "Migration_watcher" which, as we are in pre-alpha, currently runs on every page load and checks whether there is a new migration_XX.sql file where XX is larger than the current migration version.  If so it runs all migration_XX.sql files up to the largest number against the database and voila! schema changes are automated.</p>\n\n<p>If you require the ability to revert the system would require a lot of tweaking, but it\'s simple and has been working very well for our fairly small team thus far.</p>\n'}, {'answer_id': 33970, 'author': 'Sam McAfee', 'author_id': 577, 'author_profile': 'https://Stackoverflow.com/users/577', 'pm_score': 3, 'selected': False, 'text': '<p>Scott Ambler produces a great series of articles (and co-authored a <a href="http://www.ambysoft.com/books/refactoringDatabases.html" rel="noreferrer">book</a>) on database refactoring, with the idea that you should essentially apply TDD principles and practices to maintaining your schema. You set up a series of structure and seed data unit tests for the database. Then, before you change anything, you modify/write tests to reflect that change.</p>\n\n<p>We have been doing this for a while now and it seems to work. We wrote code to generate basic column name and datatype checks in a unit testing suite. We can rerun those tests anytime to verify that the database in the SVN checkout matches the live db the application is actually running.</p>\n\n<p>As it turns out, developers also sometimes tweak their sandbox database and neglect to update the schema file in SVN. The code then depends on a db change that hasn\'t been checked in. That sort of bug can be maddeningly hard to pin down, but the test suite will pick it up right away. This is particularly nice if you have it built into a larger Continuous Integration plan.</p>\n'}, {'answer_id': 33988, 'author': 'Rob', 'author_id': 3542, 'author_profile': 'https://Stackoverflow.com/users/3542', 'pm_score': 3, 'selected': False, 'text': '<p>K. Scott Allen has a decent article or two on schema versioning, which uses the incremental update scripts/migrations concept referenced in other answers here; see <a href="http://odetocode.com/Blogs/scott/archive/2008/01/31/11710.aspx" rel="noreferrer">http://odetocode.com/Blogs/scott/archive/2008/01/31/11710.aspx</a>.</p>\n'}, {'answer_id': 85287, 'author': 'Cruz', 'author_id': 16378, 'author_profile': 'https://Stackoverflow.com/users/16378', 'pm_score': 2, 'selected': False, 'text': '<p>I would recommend using Ant (cross platform) for the &quot;scripting&quot; side (since it can practically talk to any db out there via jdbc) and Subversion for the source repository.\nAnt will allow you to &quot;back up&quot; your db to local files, before making changes.</p>\n<ol>\n<li>backup existing db schema to file via Ant</li>\n<li>version control to Subversion repository via Ant</li>\n<li>send new sql statements to db via Ant</li>\n</ol>\n'}, {'answer_id': 1092323, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>The issue here is really making it easy for developers to script their own local changes into source control to share with the team.  I\'ve faced this problem for many years, and was inspired by the functionality of Visual Studio for Database professionals.  If you want an open-source tool with the same features, try this: <a href="http://dbsourcetools.codeplex.com/" rel="noreferrer">http://dbsourcetools.codeplex.com/</a> \nHave fun,\n- Nathan.</p>\n'}, {'answer_id': 1676129, 'author': 'stepancheg', 'author_id': 15018, 'author_profile': 'https://Stackoverflow.com/users/15018', 'pm_score': 0, 'selected': False, 'text': '<p>There is a command-line <a href="http://bitbucket.org/stepancheg/mysql-diff/" rel="nofollow noreferrer">mysql-diff</a> tool that compares database schemas, where schema can be a live database or SQL script on disk. It is good for the most schema migration tasks.</p>\n'}, {'answer_id': 4012083, 'author': 'Christophe Fondacci', 'author_id': 484958, 'author_profile': 'https://Stackoverflow.com/users/484958', 'pm_score': 3, 'selected': False, 'text': '<p>If you are still looking for solutions : we are proposing a tool called neXtep designer. It is a database development environment with which you can put your whole database under version control. You work on a version controlled repository where every change can be tracked.</p>\n<p>When you need to release an update, you can commit your components and the product will automatically generate the SQL upgrade script from the previous version. Of course, you can generate this SQL from any 2 versions.</p>\n<p>Then you have many options : you can take those scripts and put them in your SVN with your app code so that it\'ll be deployed by your existing mechanism. Another option is to use the delivery mechanism of neXtep : scripts are exported in something called a &quot;delivery package&quot; (SQL scripts + XML descriptor), and an installer can understand this package and deploy it to a target server while ensuring structural consistency, dependency check, registering installed version, etc.</p>\n<p>The product is GPL and is based on Eclipse so it runs on Linux, Mac and windows. It also support Oracle, MySQL and PostgreSQL at the moment (DB2 support is on the way). Have a look at the wiki where you will find more detailed information :\n<a href="http://www.nextep-softwares.com/wiki" rel="nofollow noreferrer">http://www.nextep-softwares.com/wiki</a></p>\n'}, {'answer_id': 4906503, 'author': 'Petah', 'author_id': 268074, 'author_profile': 'https://Stackoverflow.com/users/268074', 'pm_score': 2, 'selected': False, 'text': '<p>Toad for MySQL has a function called schema compare that allows you to synchronise 2 databases.  It is the best tool I have used so far.</p>\n'}, {'answer_id': 5283018, 'author': 'Edson Medina', 'author_id': 465738, 'author_profile': 'https://Stackoverflow.com/users/465738', 'pm_score': 2, 'selected': False, 'text': '<p>IMHO migrations do have a huge problem:</p>\n\n<p>Upgrading from one version to another works fine, but doing a fresh install of a given version might take forever if you have hundreds of tables and a long history of changes (like we do).</p>\n\n<p>Running the whole history of deltas since the baseline up to the current version (for hundreds of customers databases) might take a very long time.</p>\n'}, {'answer_id': 6477969, 'author': 'Ton van den Heuvel', 'author_id': 79111, 'author_profile': 'https://Stackoverflow.com/users/79111', 'pm_score': 2, 'selected': False, 'text': '<p>I like the way how <a href="http://www.yiiframework.com/" rel="nofollow">Yii</a> handles database migrations. A migration is basically a PHP script implementing <a href="http://www.yiiframework.com/doc/api/1.1/CDbMigration" rel="nofollow"><code>CDbMigration</code></a>. <code>CDbMigration</code> defines an <code>up</code> method that contains the migration logic. It is also possible to implement a <code>down</code> method to support reversal of the migration. Alternatively, <code>safeUp</code> or <code>safeDown</code> can be used to make sure that the migration is done in the context of a transaction.</p>\n\n<p>Yii\'s command-line tool <code>yiic</code> contains support to create and execute migrations. Migrations can be applied or reversed, either one by one or in a batch. Creating a migration results in code for a PHP class implementing <code>CDbMigration</code>, uniquely named based on a timestamp and a migration name specified by the user. All migrations that have been previously applied to the database are stored in a migration table.</p>\n\n<p>For more information see the <a href="http://www.yiiframework.com/doc/guide/1.1/en/database.migration" rel="nofollow">Database Migration</a> article from the manual.</p>\n'}, {'answer_id': 8920114, 'author': 'cwash', 'author_id': 59561, 'author_profile': 'https://Stackoverflow.com/users/59561', 'pm_score': 2, 'selected': False, 'text': '<p>Try db-deploy - mainly a Java tool but works with php as well.  </p>\n\n<ul>\n<li><a href="http://dbdeploy.com/" rel="nofollow">http://dbdeploy.com/</a></li>\n<li><a href="http://davedevelopment.co.uk/2008/04/14/how-to-simple-database-migrations-with-phing-and-dbdeploy.html" rel="nofollow">http://davedevelopment.co.uk/2008/04/14/how-to-simple-database-migrations-with-phing-and-dbdeploy.html</a></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/1607', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/72/']