How to avoid loss of DB when changing Grails domai

2019-07-05 01:48发布

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条回答
forever°为你锁心
2楼-- · 2019-07-05 02:38

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

查看更多
登录 后发表回答