Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid automatic schema modifications when working with legacy databases using Grails?

Tags:

grails

Grails does automatic schema modifications (including index/foreign key updates) when changing the domain model. This is usually fine, but when working with legacy databases I would like to completely disable all table modifications.

How do I instruct Grails never to modify the table structure (including indexes and foreign key constraints)?

This is how I've currently setup the mapping:

class ClassName {
  String string1
  String string2
  AnotherClass anotherClass

  static mapping = {
    version(false)
    table("legacy_table")
    string1(column: "some_legacy_field_1")
    string2(column: "some_legacy_field_2")
    anotherClass(column: "another_class_id", nullable: true, ignoreNotFound: true)
  }
}
like image 293
knorv Avatar asked Dec 21 '22 19:12

knorv


2 Answers

The dataSource defined in /grails-app/conf/DataSource.groovy has a dbCreate property, which can be set to validate to check that the schema matches the domain model without changing it in any way.

More details here: http://grails.org/doc/latest/guide/3.%20Configuration.html#3.3%20The%20DataSource

like image 151
Martin Dow Avatar answered May 18 '23 17:05

Martin Dow


As mentioned before, the property dbCreate is the one you use to specify how the database would be altered every time there are changes done in the Domain classes. I would suggest removing this property entirely as Burt suggested, so Hibernate does not control how the database is updated since that could cause certain conflicts depending on the changes you make to your domain classes.

The way I manage the database changes in our project is by using a database migration, I recommend using Liquibase, there is plug in for Grails that works perfectly, it is easy to use and offer great flexibility.

like image 23
Maricel Avatar answered May 18 '23 16:05

Maricel