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 开发者_StackOverflow中文版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
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.
Alternativly you can use the AutoBase plugin (more info here) for Grails that is based on LiquiBase.
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.
production {
dataSource {
dbCreate = "update"
username = "user"
password = "pass"
url = "jdbc:mysql://dbmaster/databasename"
}
}
精彩评论