AppFuse Tip: Backup and Restore Data with DbUnit
One of the nice things about having DbUnit integrated into AppFuse is you can easily import and export data. I've often used this feature to export a test database (or even production) into my development environment to try and reproduce data-related errors. Here's how to do it:
- Run "ant db-export -Ddatabase.host=otherhost". This exports all the data into an XML file named "db-export.xml" in the current directory.
- Run "ant db-load -Dfile=db-export.xml" to import the data into your local database.
I've used the db-export task for other things too - like populating the database through the UI and then updating the test data. All you have to do for this is to copy the db-export.xml file to metadata/sql/sample-data.xml.
NOTES:
- For Oracle, you need to add a "schema" attribute to all the <dbunit> tasks in build.xml. You also need to do this for the "db-export" target for PostgreSQL.
- If you want to limit the tables exported (i.e. from a production database), simply nest a bunch of <table name="table_name"/> elements inside the <export> element.
- If you get errors about foreign key constraints when running "db-load" after "db-export" - you need to re-arrange the order of your tables in the imported XML file.
- To clear out tables before your tests, add an empty <table name="table_name"/> element to sample-data.xml.
An alternative for exporting the 'hibernated' contents of a database to XML is to use the 'XMLDatabinder' class in Hibernate - this produces pretty good XML based on your object model, and there also exists a ReverseXMLDatabinder class (by the talented Ara Abrahamian, of XDoclet) that can import this XML back into your database.
Unfortunately the ReverseXMLDatabinder hasn't been blessed and put into Hibernate proper - it currently only exists in HB 493 (http://opensource.atlassian.com/projects/hibernate/browse/HB-493), which is stale and hasn't been touched for a year. I'm not sure why it hasn't been picked up - perhaps if it found a respected sponsor it would finally make it in... ;-)
Posted by Roberto on December 16, 2004 at 09:24 AM MST #
Posted by Luis Alejandro on May 24, 2005 at 12:33 AM MDT #