如何在开发中更改Grails域类时避免数据库丢失 [英] How to avoid loss of DB when changing Grails domain class in development
问题描述
解决此问题的最佳方法是什么?我必须去像Postgres这样的外部数据库吗?
默认 DataSource.groovy
$ p $
environments {
development {
dataSource {
dbCreate =create-drop//创建一个'create-drop','update','validate',''
url =jdbc:h2:mem: devDb; MVCC = TRUE; LOCK_TIMEOUT = 10000
}
}
create-drop
表示当应用程序重新启动时,数据库将从头开始重新创建。如果你想要一个数据库在重新启动时保持不变,然后将其更改为类似于
$ p $ data $ c $ db $ dbCreate =update //'create','create-drop','update','validate',''
url =jdbc:h2:devDb; MVCC = TRUE; LOCK_TIMEOUT = 10000
}
(即将 create-drop
更改为 update
并从 url
)中移除:mem
。但请注意,并非所有对域类所做的更改都可以反映在 update
可应用的有限模式更改中。添加属性应该没问题,但删除属性或对影响模式生成的约束进行更改可能需要删除并重新创建数据库(停止应用程序,删除devDb文件并重新启动它)。
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).
这篇关于如何在开发中更改Grails域类时避免数据库丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!