Grails - 上线后更新域 - 我会丢失数据吗?
我的第一个 Grails 应用程序已经编写完成一半了。到目前为止一切进展顺利,但我刚刚意识到当我上线时我将无法对域模型进行如此多的更改。
该应用程序由 MySql 数据库支持,目前我只有一个感兴趣的对象“Person”。如果部署后我想添加一个“组”域,以便一个人有多个组,我需要对数据库做什么?我会丢失 MySQL 数据库中的任何现有行吗?
人们通常如何处理这种情况?是否有一种巧妙的方法来设计我的域或一个简单的工具来管理 MySql 表中列的添加?
V1
class Person {
String firstName;
String lastName;
String email;
String phoneNumber;
}
V2
class Person {
static hasMany = [groups:Group]
String firstName;
String lastName;
String email;
String phoneNumber;
}
亲切的问候,
Gav
I'm half way through writing my first grails app. It's going really well so far but I've only just realised that when I go live I won't be able to change the domain model around so much.
The application is backed by a MySql database, I currently only have one object of interest 'Person'. If after deployment I want to add a 'Group' domain, so that a Person has many Groups what will I have to do regarding the database? Will I loose any existing rows in the MySQL database?
How do people typically handle this situation? Is there a clever way to design my domains or a simple tool to manage the addition of columns in the MySql table?
V1
class Person {
String firstName;
String lastName;
String email;
String phoneNumber;
}
V2
class Person {
static hasMany = [groups:Group]
String firstName;
String lastName;
String email;
String phoneNumber;
}
Kind regards,
Gav
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在生产中,您需要确保 Grails 不会修改您的数据库架构。大多数人处理此问题的方法是在开发中对更新进行建模,然后创建 SQL 更新脚本来更新生产数据库并在部署之前应用它们。 grails 提供的 架构导出 工具可以提供帮助在这种情况下。
还有诸如 DbMigrate 插件之类的工具可以帮助解决这些问题。
In production you would want to ensure that Grails does not modify your DB schema. The way most people handle this is to model your updates in development, and then create SQL update scripts to update your production database and apply them just prior to deployment. The Schema Export tool provided with grails helps in this case.
There are also tools like the DbMigrate plugin to help with these concerns.
或者,您可以使用 AutoBase 插件(更多信息此处) 用于基于 LiquiBase。
Alternativly you can use the AutoBase plugin (more info here) for Grails that is based on LiquiBase.
在 DataSource.groovy 文件中,您将找到数据库的配置。如果您验证生产配置中的 dbCreate 属性设置为“更新”,那么您应该没问题并且不会丢失任何数据。这是配置示例。
In your DataSource.groovy file you will find the configurations for your databases. If you verify that the dbCreate property in the production configuration is set to "update" then you should be fine and not lose any data. Here is an example of the configuration.