如何在开发中更改Grails域类时避免数据库丢失 [英] How to avoid loss of DB when changing Grails domain class in development

查看:116
本文介绍了如何在开发中更改Grails域类时避免数据库丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails 2.0的优点之一是您可以在开发中更改域类而无需重新启动应用程序服务器。这有效,但是当我更改域类时,我失去了所有的引导数据,这基本上破坏了目的。我使用的是默认的h2数据库。



解决此问题的最佳方法是什么?我必须去像Postgres这样的外部数据库吗?

解决方案

默认 DataSource.groovy

code>在新创建的Grails 2应用程序中有

$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆