views:

51

answers:

3

Hi,

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

+3  A: 

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.

Rich Kroll
This is probably the better solution as it is pretty scary to allow your schema to be updated "auto-magically".
Flash84x
Before deploy I always run-war to see everything works well. And during that I use MySQL app to compare development database structure with production database. It generates migration SQL file but only basics of SQL are needed to see if you're going to lose any data.
Edvinas Bartkus
+1  A: 

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"
            }
        }
Flash84x
+2  A: 

Alternativly you can use the AutoBase plugin (more info here) for Grails that is based on LiquiBase.

Michael Bavin