<p>I've had a hard time trying to find good examples of how to manage database schemas and data between development, test, and production servers.</p>

<p>Here's our setup. Each developer has a virtual machine running our app and the MySQL database. It is their personal sandbox to do whatever they want. Currently, developers will make a change to the SQL schema and do a dump of the database to a text file that they commit into SVN.</p>

<p>We're wanting to deploy a continuous integration development server that will always be running the latest committed code. If we do that now, it will reload the database from SVN for each build.</p>

<p>We have a test (virtual) server that runs "release candidates." Deploying to the test server is currently a very manual process, and usually involves me loading the latest SQL from SVN and tweaking it. Also, the data on the test server is inconsistent. You end up with whatever test data the last developer to commit had on his sandbox server.</p>

<p>Where everything breaks down is the deployment to production. Since we can't overwrite the live data with test data, this involves manually re-creating all the schema changes. If there were a large number of schema changes or conversion scripts to manipulate the data, this can get really hairy.</p>

<p>If the problem was just the schema, It'd be an easier problem, but there is "base" data in the database that is updated during development as well, such as meta-data in security and permissions tables.</p>

<p>This is the biggest barrier I see in moving toward continuous integration and one-step-builds. How do <strong><em>you</em></strong> solve it?</p>

<hr>

<p>A follow-up question: how do you track database versions so you know which scripts to run to upgrade a given database instance? Is a version table like Lance mentions below the standard procedure?</p>

<hr>

<p>Thanks for the reference to Tarantino. I'm not in a .NET environment, but I found their <a href="http://code.google.com/p/tarantino/wiki/DatabaseChangeManagement" rel="noreferrer">DataBaseChangeMangement wiki page</a> to be very helpful. Especially this <a href="http://tarantino.googlecode.com/svn/docs/Database-Change-Management.ppt" rel="noreferrer">Powerpoint Presentation (.ppt)</a></p>

<p>I'm going to write a Python script that checks the names of <code>*.sql</code> scripts in a given directory against a table in the database and runs the ones that aren't there in order based on a integer that forms the first part of the filename. If it is a pretty simple solution, as I suspect it will be, then I'll post it here.</p>

<hr>

<p>I've got a working script for this. It handles initializing the DB if it doesn't exist and running upgrade scripts as necessary. There are also switches for wiping an existing database and importing test data from a file. It's about 200 lines, so I won't post it (though I might put it on pastebin if there's interest).</p>

[{'answer_id': 6380, 'author': 'Lance Fisher', 'author_id': 571, 'author_profile': 'https://Stackoverflow.com/users/571', 'pm_score': 7, 'selected': True, 'text': '<p>There are a couple of good options.  I wouldn\'t use the "restore a backup" strategy.</p>\n\n<ol>\n<li><p>Script all your schema changes, and have your CI server run those scripts on the database.  Have a version table to keep track of the current database version, and only execute the scripts if they are for a newer version.</p></li>\n<li><p>Use a migration solution.  These solutions vary by language, but for .NET I use Migrator.NET.  This allows you to version your database and move up and down between versions.  Your schema is specified in C# code.</p></li>\n</ol>\n'}, {'answer_id': 6382, 'author': 'tbreffni', 'author_id': 637, 'author_profile': 'https://Stackoverflow.com/users/637', 'pm_score': 5, 'selected': False, 'text': '<p>Your developers need to write change scripts (schema and data change) for each bug/feature they work on, not just simply dump the entire database into source control.  These scripts will upgrade the current production database to the new version in development.  </p>\n\n<p>Your build process can restore a copy of the production database into an appropriate environment and run all the scripts from source control on it, which will update the database to the current version.  We do this on a daily basis to make sure all the scripts run correctly.</p>\n'}, {'answer_id': 10343, 'author': 'Jim', 'author_id': 1208, 'author_profile': 'https://Stackoverflow.com/users/1208', 'pm_score': 1, 'selected': False, 'text': '<p>If you are in the .NET environment then the solution is <a href="http://web.archive.org/web/20150814180919/https://github.com/HeadspringLabs/Tarantino" rel="nofollow noreferrer">Tarantino (archived)</a>.  It handles all of this (including which sql scripts to install) in a NANT build.</p>\n'}, {'answer_id': 13568, 'author': 'Juha Syrjälä', 'author_id': 1431, 'author_profile': 'https://Stackoverflow.com/users/1431', 'pm_score': 4, 'selected': False, 'text': '<p>Have a look at how Ruby on Rails does this. </p>\n\n<p>First there are so called migration files, that basically transform database schema and data from version N to version N+1 (or in case of downgrading from version N+1 to N). Database has table which tells current version.</p>\n\n<p>Test databases are always wiped clean before unit-tests and populated with fixed data from files.</p>\n'}, {'answer_id': 19256, 'author': 'Matt Stine', 'author_id': 2058, 'author_profile': 'https://Stackoverflow.com/users/2058', 'pm_score': 2, 'selected': False, 'text': '<p>This is something that I\'m constantly unsatisfied with - our solution to this problem that is. For several years we maintained a separate change script for each release. This script would contain the deltas from the last production release. With each release of the application, the version number would increment, giving something like the following:</p>\n\n<ul>\n<li>dbChanges_1.sql</li>\n<li>dbChanges_2.sql</li>\n<li>...</li>\n<li>dbChanges_n.sql</li>\n</ul>\n\n<p>This worked well enough until we started maintaining two lines of development: Trunk/Mainline for new development, and a maintenance branch for bug fixes, short term enhancements, etc. Inevitably, the need arose to make changes to the schema in the branch. At this point, we already had dbChanges_n+1.sql in the Trunk, so we ended up going with a scheme like the following:</p>\n\n<ul>\n<li>dbChanges_n.1.sql</li>\n<li>dbChanges_n.2.sql</li>\n<li>...</li>\n<li>dbChanges_n.3.sql</li>\n</ul>\n\n<p>Again, this worked well enough, until we one day we looked up and saw 42 delta scripts in the mainline and 10 in the branch. ARGH!</p>\n\n<p>These days we simply maintain one delta script and let SVN version it - i.e. we overwrite the script with each release. And we shy away from making schema changes in branches. </p>\n\n<p>So, I\'m not satisfied with this either. I really like the concept of migrations from Rails. I\'ve become quite fascinated with <a href="http://www.liquibase.org" rel="nofollow noreferrer">LiquiBase</a>. It supports the concept of incremental database refactorings. It\'s worth a look and I\'ll be looking at it in detail soon. Anybody have experience with it? I\'d be very curious to hear about your results. </p>\n'}, {'answer_id': 22257, 'author': 'Dave Marshall', 'author_id': 1248, 'author_profile': 'https://Stackoverflow.com/users/1248', 'pm_score': 1, 'selected': False, 'text': '<p>Check out the <a href="http://dbdeploy.com/" rel="nofollow noreferrer">dbdeploy</a>, there are Java and .net tools already available, you could follow their standards for the SQL file layouts and schema version table and write your python version.</p>\n'}, {'answer_id': 40914, 'author': 'Tim Williscroft', 'author_id': 2789, 'author_profile': 'https://Stackoverflow.com/users/2789', 'pm_score': 2, 'selected': False, 'text': "<p>We have a very similar setup to the OP.</p>\n\n<p>Developers develop in VM's with private DB's.</p>\n\n<p>[Developers will soon be committing into private branches]</p>\n\n<p>Testing is run  on different machines ( actually in in VM's hosted on a server)\n[Will soon be run by Hudson CI server]</p>\n\n<p>Test by loading the reference dump into the db.\nApply the developers schema patches \nthen apply the developers data patches</p>\n\n<p>Then run unit and system tests.</p>\n\n<p>Production is deployed to customers as installers. </p>\n\n<p>What we do:</p>\n\n<p>We take a schema dump of our sandbox DB. \nThen a sql data dump.\nWe diff that to the previous baseline.\nthat pair of deltas is to upgrade n-1 to n.</p>\n\n<p>we configure the dumps and deltas.</p>\n\n<p>So to install version N CLEAN we run the dump into an empty db.\nTo patch, apply the intervening patches.</p>\n\n<p>( Juha mentioned  Rail's idea of having a table recording the current DB version is a good one and should make installing updates less fraught. )</p>\n\n<p>Deltas and dumps have to be reviewed before beta test.\nI can't see any way around this as I've seen developers insert test accounts into the DB for themselves.</p>\n"}, {'answer_id': 449732, 'author': 'MarkR', 'author_id': 13724, 'author_profile': 'https://Stackoverflow.com/users/13724', 'pm_score': 2, 'selected': False, 'text': "<p>I'm afraid I'm in agreement with other posters. Developers need to script their changes.</p>\n\n<p>In many cases a simple ALTER TABLE won't work, you need to modify existing data too - developers need to thing about what migrations are required and make sure they're scripted correctly (of course you need to test this carefully at some point in the release cycle).</p>\n\n<p>Moreover, if you have any sense, you'll get your developers to script rollbacks for their changes as well so they can be reverted if need be. This should be tested as well, to ensure that their rollback not only executes without error, but leaves the DB in the same state as it was in previously (this is not always possible or desirable, but is a good rule most of the time).</p>\n\n<p>How you hook that into a CI server, I don't know. Perhaps your CI server needs to have a known build snapshot on, which it reverts to each night and then applies all the changes since then. That's probably best, otherwise a broken migration script will break not just that night's build, but all subsequent ones.</p>\n"}, {'answer_id': 449801, 'author': 'Rad', 'author_id': 1349, 'author_profile': 'https://Stackoverflow.com/users/1349', 'pm_score': 3, 'selected': False, 'text': '<p>You could also look at using  a tool like <a href="http://www.red-gate.com/products/SQL_Compare/index.htm" rel="noreferrer">SQL Compare</a> to script the difference between various versions of a database, allowing you to quickly migrate between versions</p>\n'}, {'answer_id': 541419, 'author': 'mcintyre321', 'author_id': 2086, 'author_profile': 'https://Stackoverflow.com/users/2086', 'pm_score': 0, 'selected': False, 'text': '<p>I\'ve written a tool which (by hooking into <a href="http://www.codeplex.com/OpenDBiff" rel="nofollow noreferrer">Open DBDiff</a>) compares database schemas, and will suggest migration scripts to you. If you make a change that deletes or modifies data, it will throw an error, but provide a suggestion for the script (e.g. when a column in missing in the new schema, it will check if the column has been renamed and create xx - generated script.sql.suggestion containing a rename statement).</p>\n\n<p><a href="http://code.google.com/p/migrationscriptgenerator/" rel="nofollow noreferrer">http://code.google.com/p/migrationscriptgenerator/</a> SQL Server only I\'m afraid :( It\'s also pretty alpha, but it is VERY low friction (particularly if you combine it with Tarantino or <a href="http://code.google.com/p/simplescriptrunner/" rel="nofollow noreferrer">http://code.google.com/p/simplescriptrunner/</a>)</p>\n\n<p>The way I use it is to have a SQL scripts project in your .sln. You also have a db_next database locally which you make your changes to (using Management Studio or <a href="http://wiki.fluentnhibernate.org/show/GettingStarted:+First+Project" rel="nofollow noreferrer">NHibernate Schema Export</a> or <a href="http://msdn.microsoft.com/en-us/library/bb399420.aspx" rel="nofollow noreferrer">LinqToSql CreateDatabase</a> or something). Then you execute migrationscriptgenerator with the _dev and _next DBs, which creates. the SQL update scripts for migrating across.</p>\n'}, {'answer_id': 541549, 'author': 'Esko Luontola', 'author_id': 62130, 'author_profile': 'https://Stackoverflow.com/users/62130', 'pm_score': 3, 'selected': False, 'text': '<p>The book <a href="https://rads.stackoverflow.com/amzn/click/com/0321293533" rel="noreferrer" rel="nofollow noreferrer">Refactoring Databases: Evolutionary Database Design</a> might give you some ideas on how to manage the database. A short version is readable also at <a href="http://martinfowler.com/articles/evodb.html" rel="noreferrer">http://martinfowler.com/articles/evodb.html</a></p>\n\n<p>In one PHP+MySQL project I\'ve had the database revision number stored in the database, and when the program connects to the database, it will first check the revision. If the program requires a different revision, it will open a page for upgrading the database. Each upgrade is specified in PHP code, which will change the database schema and migrate all existing data.</p>\n'}, {'answer_id': 775948, 'author': 'Yordan Georgiev', 'author_id': 65706, 'author_profile': 'https://Stackoverflow.com/users/65706', 'pm_score': 3, 'selected': False, 'text': '<ul>\n<li>Name your databases as follows - <code>dev_&lt;&lt;db&gt;&gt; , tst_&lt;&lt;db&gt;&gt;  , stg_&lt;&lt;db&gt;&gt; , prd_&lt;&lt;db&gt;&gt;</code> (Obviously you never should hardcode db names </li>\n<li>Thus you would be able to deploy even the different type of db\'s on same physical server ( I do not recommend that , but you may have to ... if resources are tight )</li>\n<li>Ensure you would be able to move data between those automatically </li>\n<li>Separate the db creation scripts from the population = It should be always possible to recreate the db from scratch and populate it ( from the old db version or external data source </li>\n<li>do not use hardcode connection strings in the code ( even not in the config files ) - use in the config files connection string templates , which you do populate dynamically , each reconfiguration of the application_layer which does need recompile is BAD</li>\n<li>do use database versioning and db objects versioning - if you can afford it use ready products , if not develop something on your own</li>\n<li>track each DDL change and save it into some history table ( <a href="http://ysgitdiary.blogspot.com/2009/04/db-trigger-to-force-versioning-in-ddl.html" rel="nofollow noreferrer">example here</a> ) </li>\n<li>DAILY backups ! Test how fast you would be able to restore something lost from a backup (use automathic restore scripts </li>\n<li>even your DEV database and the PROD have exactly the same creation script you will have problems with the data, so allow developers to create the exact copy of prod and play with it ( I know I will receive minuses for this one , but change in the mindset and the business process will cost you much less when shit hits the fan - so force the coders to subscript legally whatever it makes , but ensure this one </li>\n</ul>\n'}, {'answer_id': 1676198, 'author': 'stepancheg', 'author_id': 15018, 'author_profile': 'https://Stackoverflow.com/users/15018', 'pm_score': 1, 'selected': False, 'text': '<p>We are using command-line <a href="http://bitbucket.org/stepancheg/mysql-diff/" rel="nofollow noreferrer">mysql-diff</a>: it outputs a difference between two database schemas (from live DB or script) as ALTER script. mysql-diff is executed at application start, and if schema changed, it reports to developer. So developers do not need to write ALTERs manually, schema updates happen semi-automatically.</p>\n'}, {'answer_id': 5282998, 'author': 'qwazer', 'author_id': 575350, 'author_profile': 'https://Stackoverflow.com/users/575350', 'pm_score': 0, 'selected': False, 'text': '<p>For oracle database we use <a href="http://oracle-ddl2svn.googlecode.com" rel="nofollow">oracle-ddl2svn</a> tools.</p>\n\n<p>This tool automated next process</p>\n\n<ol>\n<li>for every db scheme get scheme ddls</li>\n<li>put it under version contol</li>\n</ol>\n\n<p>changes between instances resolved manually</p>\n'}]
['https://Stackoverflow.com/questions/6371', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/763/']