<p>This seems to be an overlooked area that could really use some insight.  What are your best practices for:</p>

<ul>
<li>making an upgrade procedure</li>
<li>backing out in case of errors</li>
<li>syncing code and database changes</li>
<li>testing prior to deployment</li>
<li>mechanics of modifying the table</li>
</ul>

<p>etc...</p>

[{'answer_id': 29765, 'author': 'Sindri Traustason', 'author_id': 1113, 'author_profile': 'https://Stackoverflow.com/users/1113', 'pm_score': 2, 'selected': False, 'text': '<p>In general my rule is: "The application should manage it\'s own schema."</p>\n\n<p>This means schema upgrade scripts  are part of any upgrade package for the application and run automatically when the application starts.  In case of errors the application fails to start and the upgrade script transaction is not committed.  The downside to this is that the application has to have full modification access to the schema (this annoys DBAs).</p>\n\n<p>I\'ve had great success using Hibernates SchemaUpdate feature to manage the table structures. Leaving the upgrade scripts to only handle actual data initialization and occasional removing of columns (SchemaUpdate doesn\'t do that).</p>\n\n<p>Regarding testing, since the upgrades are part of the application, testing them becomes part of the test cycle for the application.</p>\n\n<p>Afterthought: Taking on board some of the criticism in other posts here, note the rule says "it\'s own". It only really applies where the application <em>owns</em> the schema as is generally the case with software sold as a product. If your software is sharing a database with other software, use other methods.</p>\n'}, {'answer_id': 29773, 'author': 'John Hunter', 'author_id': 2253, 'author_profile': 'https://Stackoverflow.com/users/2253', 'pm_score': 2, 'selected': False, 'text': '<p>I am a big fan of <a href="http://www.red-gate.com/" rel="nofollow noreferrer">Red Gate</a> products that help creating SQL packages to update database schemas.  The database scripts can be added to source control to help with versioning and rollback.</p>\n'}, {'answer_id': 29801, 'author': 'nso1', 'author_id': 3027, 'author_profile': 'https://Stackoverflow.com/users/3027', 'pm_score': 3, 'selected': True, 'text': '<p>That\'s a great question. ( There is a high chance this is going to end up a normalised versus denormalised database debate..which I am not going to start... okay now for some input.)</p>\n\n<p>some off the top of my head things I have done (will add more when I have some more time or need a break)</p>\n\n<p>client design - this is where the VB method of inline sql (even with prepared statements) gets you into trouble.  You can spend AGES just finding those statements.  If you use something like Hibernate and put as much SQL into named queries you have a single place for most of the sql (nothing worse than trying to test sql that is inside of some IF statement and you just don\'t hit the "trigger" criteria in your testing for that IF statement).  Prior to using hibernate (or other orms\') when I would do SQL directly in JDBC or ODBC  I would put all the sql statements as either public fields of an object (with a naming convention) or in a property file (also with a naming convention for the values say PREP_STMT_xxxx.  And use either reflection or iterate over the values  at startup in a) test cases b) startup of the application (some rdbms allow you to pre-compile with prepared statements before execution, so on startup post login I would pre-compile the prep-stmts at startup to make the application self testing.  Even for 100\'s of statements on a good rdbms thats only a few seconds. and only once.  And it has saved my butt a lot.  On one project the DBA\'s wouldn\'t communicate (a different team, in a different country) and the schema seemed to change NIGHTLY, for no reason.  And each morning we got a list of exactly where it broke the application, on startup.</p>\n\n<p>If you need adhoc functionality , put it in a well named class (ie. again a naming convention helps with auto mated testing) that acts as some sort of factory for you query (ie. it builds the query).  You are going to have to write the equivalent code anyway right, just put in a place you can test it.  You can even write some basic test methods on the same object or in a separate class.</p>\n\n<p>If you can , also try to use stored procedures.  They are a bit harder to test as above.  Some db\'s also don\'t pre-validate the sql in stored procs against the schema at compile time only at run time. It usually involves say taking a copy of the schema structure (no data) and then creating all stored procs against this copy (in case the db team making the changes DIDn\'t validate correctly).  Thus the structure can be checked.  but as a point of change management stored procs are great.  On change all get it.  Especially when the db changes are a result of business process changes.  And all languages (java, vb, etc get the change )</p>\n\n<p>I usually also setup a table I use called system_setting etc.  In this table we keep a VERSION identifier.  This is so that client libraries can connection and validate if they are valid for this version of the schema.  Depending on the changes to your schema, you don\'t want to allow clients to connect if they can corrupt your schema (ie. you don\'t have a lot of referential rules in the db, but on the client).  It depends if you are also going to have multiple client versions (which does happen in NON - web apps, ie. they are running the wrong binary).  You could also have batch tools etc.  Another approach which I have also done is define a set of schema to operation versions in some sort of property file or again in a system_info table.  This table is loaded on login, and then used by each "manager" (I usually have some sort of client side api to do most db stuff) to validate for that operation if it is the right version.  Thus most operations can succeed, but you can also fail (throw some exception) on out of date methods and tells you WHY.</p>\n\n<p>managing the change to schema -> do you update the table or add 1-1 relationships to new tables ?  I have seen a lot of shops which always access data via a view for this reason. This allows table names to change , columns etc.  I have played with the idea of actually treating views like interfaces in COM.  ie. you add a new VIEW for new functionality / versions.  Often, what gets you here is that you can have a lot of reports (especially end user custom reports) that assume table formats.  The views allow you to deploy a new table format but support existing client apps (remember all those pesky adhoc reports).</p>\n\n<p>Also, need to write update and rollback scripts.  and again TEST, TEST, TEST...</p>\n\n<p>------------  OKAY - THIS IS A BIT RANDOM DISCUSSION TIME --------------</p>\n\n<p>Actually had a large commercial project (ie. software shop) where we had the same problem.  The architecture was a 2 tier and they were using a product a bit like PHP but pre-php.  Same thing.  different name.  anyway i came in in version 2....</p>\n\n<p>It was costing A LOT OF MONEY to do upgrades.  A lot. ie. give away weeks of free consulting time on site.  </p>\n\n<p>And it was getting to the point of wanting to either add new features or optimize the code.  Some of the existing code used stored procedures , so we had common points where we could manage code.  but other areas were this embedded sql markup in html.  Which was great for getting to market quickly but with each interaction of new features the cost at least doubled to test and maintain.   So when we were looking at pulling out the php type code out, putting in data layers (this was 2001-2002, pre any ORM\'s etc) and adding a lot of new features (customer feedback) looked at this issue of how to engineer UPGRADES into the system.  Which is a big deal, as upgrades cost a lot of money to do correctly.  Now, most patterns and all the other stuff people discuss with a degree of energy deals with OO code that is running, but what about the fact that your data has to a) integrate to this logic, b) the meaning and also the structure of the data can change over time, and often due to the way data works you end up with a lot of sub process / applications in your clients organisation that needs that data -> ad hoc reporting or any complex custom reporting, as well as batch jobs that have been done for custom data feeds etc.</p>\n\n<p>With this in mind i started playing with something a bit left of field.  It also has a few assumptions.  a) data is heavily read more than write.  b) updates do happen, but not at bank levels ie. one or 2 a second say.</p>\n\n<p>The idea was to apply a COM / Interface view to how data was accessed by clients over a set of CONCRETE tables (which varied with schema changes).  You could create a seperate view for each type operation - update, delete, insert and read.  This is important.  The views would either map directly to a table , or allow you to trigger of a dummy table that does the real updates or inserts etc.  What i actually wanted was some sort of trappable level indirection that could still be used by crystal reports etc. NOTE -  For inserts , update and deletes you could also use stored procs.  And you had a version for each version of the product.  That way your version 1.0 had its version of the schema, and if the tables changed, you would still have the version 1.0 VIEWS but with NEW backend logic to map to the new tables as needed, but you also had version 2.0 views that would support new fields etc.  This was really just to support ad hoc reporting, which if your a BUSINESS person and not a coder is probably the whole point of why you have the product.  (your product can be crap but if you have the best reporting in the world you can still win, the reverse is true - your product can be the best feature wise, but if its the worse on reporting you can very easily loose).</p>\n\n<p>okay, hope some of those ideas help.</p>\n'}, {'answer_id': 35658, 'author': 'Jim', 'author_id': 1208, 'author_profile': 'https://Stackoverflow.com/users/1208', 'pm_score': 2, 'selected': False, 'text': '<p>These are all weighty topics, but here is my recommendation for updating.</p>\n\n<p>You did not specify your platform, but for NANT build environments I use <a href="http://jeffreypalermo.com/blog/the-tarantino-project-automated-database-migrations-and-more/" rel="nofollow noreferrer">Tarantino</a>.  For every database update you are ready to commit, you make a change script (using RedGate or another tool).  When you build to production, Tarantino checks if the script has been run on the database (it adds a table to your database to keep track).  If not, the script is run.  It takes all the manual work (read: human error) out of managing database versions.  </p>\n'}, {'answer_id': 112841, 'author': 'Pat', 'author_id': 20161, 'author_profile': 'https://Stackoverflow.com/users/20161', 'pm_score': 3, 'selected': False, 'text': '<h1>Liquibase</h1>\n\n<p>liquibase.org:</p>\n\n<ol>\n<li>it understands hibernate definitions.</li>\n<li>it generates better schema update sql than hibernate</li>\n<li>it logs which upgrades have been made to a database </li>\n<li>it handles two-step changes (i.e. delete a column "foo" and then rename a different column to "foo")</li>\n<li>it handles the concept of conditional upgrades</li>\n<li>the developer actually listens to the community (with hibernate if you are not in the "in" crowd or a newbie -- you are basically ignored.)</li>\n</ol>\n\n<p><a href="http://www.liquibase.org" rel="nofollow noreferrer">http://www.liquibase.org</a></p>\n'}, {'answer_id': 112852, 'author': 'Pat', 'author_id': 20161, 'author_profile': 'https://Stackoverflow.com/users/20161', 'pm_score': 3, 'selected': False, 'text': '<p><strong>opinion</strong></p>\n\n<p>the application should <strong>never</strong> handle a schema update. This is a disaster waiting to happen. Data outlasts the applications and as soon as multiple applications try to work with the same data ( the production app + a reporting app for example) -- chances are they will both use the same underlying company libraries... and then both programs  decide to do their own db upgrade ... have fun with <em>that</em> mess.</p>\n'}, {'answer_id': 1715107, 'author': 'Nick Pierpoint', 'author_id': 4003, 'author_profile': 'https://Stackoverflow.com/users/4003', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve heard good things about <a href="http://ibatis.apache.org/java.cgi" rel="nofollow noreferrer">iBATIS 3 Schema Migrations System</a>:</p>\n\n<p>User Guide: <a href="http://svn.apache.org/repos/asf/ibatis/java/ibatis-3/trunk/doc/en/iBATIS-3-Migrations.pdf" rel="nofollow noreferrer">http://svn.apache.org/repos/asf/ibatis/java/ibatis-3/trunk/doc/en/iBATIS-3-Migrations.pdf</a></p>\n'}, {'answer_id': 2886684, 'author': 'idarwin', 'author_id': 176859, 'author_profile': 'https://Stackoverflow.com/users/176859', 'pm_score': 0, 'selected': False, 'text': "<p>As Pat said, use liquibase. Especially when you have several developers with their own dev databases\nmaking changes that will become part of the production database.</p>\n\n<p>If there's only one dev, as on one project I'm on now(ha), I just commit the schema changes as SQL text files into a CVS repo, which I check out in batches on the production server when the code changes go in.</p>\n\n<p>But liquibase is better organized than that!</p>\n"}]
['https://Stackoverflow.com/questions/29744', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/116/']