문제

One of the advantages of Grails 2.0 is that you can change domain classes in development without needing to restart the application server. This works, however when I change domain classes I lose all my bootstrap data, which basically defeats the purpose. I'm using the default h2 database.

What is the best way to get around this? Do I have to go to an external DB like Postgres?

도움이 되었습니까?

해결책

The default DataSource.groovy in a newly-created Grails 2 app has

environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
            url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
        }
    }

The create-drop means the database will be re-created from scratch whenever the application restarts. If you want a database that persists across restarts then change this to something like

dataSource {
    dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
    url = "jdbc:h2:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}

(i.e. change create-drop to update and remove the :mem from the url). However note that not all changes you can make to a domain class can be reflected in the limited schema changes that update can apply. Adding properties should be OK, but removing properties or making changes to the constraints that affect schema generation may require you to drop and re-create the database anyway (stop the app, delete the devDb files and start it up again).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top