<p>In our current database development evironment we have automated build procceses check all the sql code out of svn create database scripts and apply them to the various development/qa databases.</p>

<p>This is all well and good, and is a tremdous improvement over what we did in the past, but we have a problem with rerunning scripts.  Obviously this isn't a problem with some scripts like altering procedures, because you can run them over and over without adversly affecting the system. Right now to add metadata and run statements like create/alter table statements we add code to check and see if the objects exists, and if they do, don't run them.</p>

<p>Our problem is that we really only get one shot to run the script, because once the script has been run, the objects are in the environment and system won't run the script again.  If something needs to change once it's been deployed, we have a difficult process of running update scripts agaist the update scripts and hoping that everything falls in the correct order and all of the PKs line up between the environments (the databases are, shall we say, "special").</p>

<p>Short of dropping the database and starting the process from scratch (the last most current release), does anyone have a more elegant solution to this?  </p>

[{'answer_id': 49585, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 3, 'selected': True, 'text': '<p>I\'m not sure how best to approach the problem in your specific environment, but I\'d suggest reading up on Rail\'s migrations feature for some inspiration on how to get started. </p>\n\n<p><a href="http://wiki.rubyonrails.org/rails/pages/UnderstandingMigrations" rel="nofollow noreferrer">http://wiki.rubyonrails.org/rails/pages/UnderstandingMigrations</a></p>\n'}, {'answer_id': 49597, 'author': 'Ryan Lanciaux', 'author_id': 1385358, 'author_profile': 'https://Stackoverflow.com/users/1385358', 'pm_score': 1, 'selected': False, 'text': '<p>Do you keep your existing data in the database? If not, you may want to look at something similar to what Matt mentioned for .NET called <a href="http://www.rikware.com/RikMigrations.html" rel="nofollow noreferrer">RikMigrations</a> </p>\n\n<pre><code>http://www.rikware.com/RikMigrations.html\n</code></pre>\n\n<p>I use that on my projects to update my database on the fly, while keeping track of revisions. Also, it makes it very simple to move database schema to different servers, etc.</p>\n'}, {'answer_id': 49845, 'author': 'Ben Scheirman', 'author_id': 3381, 'author_profile': 'https://Stackoverflow.com/users/3381', 'pm_score': 0, 'selected': False, 'text': '<p>if you want to have re-runnability in your scripts, then you can\'t have them as definitions...  what I mean by this is that you need to focus on change scripts rather than here is my Table script.</p>\n\n<p>let\'s say you have a table Customers:</p>\n\n<pre><code>create table Customers (\n   id int identity(1,1) primary key,\n   first_name varchar(255) not null,\n   last_name varchar(255) not null\n)\n</code></pre>\n\n<p>and later you want to add a status column.  Don\'t modify your original table script, that one has already run (and can have the if(! exists) syntax to prevent it from causing errors while running again).</p>\n\n<p>Instead, have a new script, called add_customer_status.sql</p>\n\n<p>in this script you\'ll have something like:</p>\n\n<pre><code>alter table Customers\nadd column status varchar(50) null\n\nupdate Customers set status = \'Silver\' where status is null\n\nalter table Customers\nalter column status varchar(50) not null\n</code></pre>\n\n<p>Again you can wrap this with an if(! exists) block to allow re-running, but here we\'ve leveraged the notion that this is a change script, and we adapt the database accordingly.  If there is data already in the customers table then we\'re still okay, since we add the column, seed it with data, then add the not null constraint.</p>\n\n<p>Both of the migration frameworks mentioned above are good, I\'ve also had excellent experience with <a href="http://code.google.com/p/migratordotnet" rel="nofollow noreferrer">MigratorDotNet</a>.</p>\n'}, {'answer_id': 49933, 'author': 'harpo', 'author_id': 4525, 'author_profile': 'https://Stackoverflow.com/users/4525', 'pm_score': 0, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/users/3475/scott">Scott</a> named a couple of other <a href="https://stackoverflow.com/questions/44034/how-can-i-get-the-definition-body-of-a-trigger-in-sql-server#44056">SQL tools</a> that address the problem of change management.  But I\'m still rolling my own.</p>\n\n<p>I would like to second this question, and add my puzzlement that there is still no free, community-based tool for this problem.  Obviously, scripts are not a satisfactory way to maintain a database schema; neither are instances.  So, why don\'t we keep metadata in a separate (and while we\'re at it, platform-neutral) format?</p>\n\n<p>That\'s what I\'m doing now.  My master database schema is a version-controlled XML file, created initially from a simple web service.  A simple javascript program compares instances against it, and a simple XSL transform yields the CREATE or ALTER statements.  It has limits, like <a href="http://www.rikware.com/RikMigrations.html" rel="nofollow noreferrer">RikMigrations</a>; for instance it doesn\'t always sequence inter-depdendent objects correctly.  (But guess what &#8212; neither does <a href="http://www.microsoft.com/downloads/details.aspx?familyid=56E5B1C5-BF17-42E0-A410-371A838E570A&amp;displaylang=en" rel="nofollow noreferrer">Microsoft\'s SQL Server Database Publication tool</a>.)  Really, it\'s too simple.  I simply didn\'t include objects (roles, users, etc.) that I wasn\'t using.</p>\n\n<p>So, my view is that this problem is indeed inadequately addressed, and that sooner or later we\'ll have to get together and tackle the devilish details.</p>\n'}, {'answer_id': 50025, 'author': 'Murph', 'author_id': 1070, 'author_profile': 'https://Stackoverflow.com/users/1070', 'pm_score': 2, 'selected': False, 'text': '<p>We address this - or at least a similar problem to this - as follows:</p>\n\n<ol>\n<li>The schema has a version number - this is represented by a table which has one row per version which, as well as the version number, carries boring things like a date/time stamp for when that version came into existence.</li>\n<li>By having the schema create/modify DDL wrapped in code that performs the changes for us.</li>\n</ol>\n\n<p>In the context above one would build the schema change code as part of the build process then run it and it would only apply schema changes that haven\'t already been applied.</p>\n\n<p>In our experience (which is bound not to be representative) in most cases the schema changes are sufficiently small/fast that they can safely be run in a transaction which means that if it fails we get a rollback and the db is "safe" - although one would always recommend taking backups before applying schema updates if practicable.</p>\n\n<p>I evolved this out of nasty painful experience. Its not a perfect system (or an original idea) but as a result of working this way we have a high degree of confidence that if there are two instances of one of our databases with the same version that then the schema for those two databases will be the same in almost all respects and that we can safely bring any db up to the current schema for that application without ill effects. (That last isn\'t 100% true unfortunately - there\'s always an exception - but its not too far from the truth!)</p>\n'}, {'answer_id': 1099941, 'author': 'Kelly S. French', 'author_id': 135078, 'author_profile': 'https://Stackoverflow.com/users/135078', 'pm_score': 0, 'selected': False, 'text': "<p>We went the 'drop and recreate the schema' route.  We had some classes in our JUnit test package which parameterized the scripts to create all the objects in the schema for the developer executing the code.  This allowed all the developers to share one test database and everyone could simultaneously create/test/drop their test tables without conflicts.</p>\n\n<p>Did it take a long time to run? Yes. At first we used the setup method for this which meant the tables were dropped/created for every test and that took way too long.  Then we created a TestSuite which could be run once before all the tests for a class and then cleaned up when all the class tests were complete. This still meant that the db setup ran many times when we ran our 'AllTests' class which included all the tests in all our packages.  How I solved it was adding a semaphore to the OracleTestSuite code so when the first test requested the database to be setup it would do that but any subsequent call would just increment a counter.  As each tearDown() method was called, the counter would decrement the counter until it reached 0 and the OracleTestSuite code would drop everything.  One issue this leaves is whether the tests assume that the database is empty.  It can be convenient to let database tests know the order in which they run so they can take advantage of the state of the database because it can reduce the duplication of DB setup.  </p>\n\n<p>We used the concept of ObjectMothers to solve a similar problem with creating complex domain objects for testing purposes.  Mock objects might be a better answer but we hadn't heard about them at the time.  After all this time, I'd recommend creating test helper methods that could create standardized datasets for the typical scenarios.  Plus that would help document the important edge cases from a data perspective.</p>\n"}]
['https://Stackoverflow.com/questions/49582', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1942/']