Validate Legacy Databases with Grails (Christopher M Judd)
Christopher M Judd, Grails July 18th, 2008
Christopher Judd, one of the three authors of Beginning Groovy and Grails: From Novice to Professional , has posted a piece of advice when working with legacy databases. You may find some of the same information in Scott’s article Mastering Grails: Grails and Legacy Databases , but Christopher mentions one configuration setting that has not been extensively documented as the other ones: validating your legacy database.
By default Grails assume you are creating a new database schema from scratch. But one of its strengths is it’s ease of mapping to existing database tables. However when you map to existing tables you must be careful. By default the DataSource.groovy file, is configured to create-drop tables so it is easy to inadvertently wipe out data and tables. In addition switching to the less disruptive option of update may not be safe either. See GORM objects by default have an id and version attribute that are injected by GORM. When GORM gets initialized it will add id and version columns to your database which you may not want or expect. So the best option may be to use the validate option which is not documented in the Grails user guide. On start up it will validate the tables have the columns that your domain classes expect and if not fail to start.
By the way, to turn off the version column you will need to set version to false in the domain class mappings. To prevent the id column from being created, you will need to map id to the primary key column of your table.
Tags: Christopher M Judd, Grails
About
Leave a Comment