How to avoid loss of DB when changing Grails domain class in development

坚强是说给别人听的谎言 提交于 2019-12-10 18:17:27

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/12138815/how-to-avoid-loss-of-db-when-changing-grails-domain-class-in-development

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!