views:

451

answers:

4

I can't figure how what the "standard practice" is for modifying a Domain Class after it has automatically created the corresponding database table.

There's no "migration" in Grails, and there's no way that I can find to tell it to output the new SQL it would generate so you can compare it to the previous table definition and manually issue your own ALTER TABLE command (that's what I do in django).

I just got the book "The Definitive Guide to Grails" and it is silent on the subject, and I can't find anything on the Grails website.

+2  A: 

This is a weak point of grails and I don't know a good way to deal with it. What I do is create a copy of the modifyed domain class, then compare the SQL schema of the table of the modifyed domain class to that generated by the copy of the domain class. Then you have to manually make what ever alterations are necessary to your origional table. I've found that things such as relationships tend to change when the domain class does, but things such as adding a constraint to force a field to be a text type don't always get changed.

Jared
Also, removing a property NEVER deletes the column in Grails. I'm sure this is for safety and because they didn't want to have to "figure out" if they could delete a reference column.
Bill James
Thanks for the answer! That's pretty bad! At least it can dump the SQL so you can compare old and new.I found that if you just add a field, it "just works". If you remove a field, it leaves it in the database. (Which is OK, you can drop it later once you're sure you don't need the data.)
ראובן
+9  A: 

If you want to explicitly manage the database schema for a Grails application I suggest you have a look at the Grails Liquibase plugin or the Grails autobase plugin.

Ruben
Any idea which of these plugins is better?
Don
Both plugins use Liquibase to manage the database changes, so the available refactorings should be similar. But I have no idea about the community support, etc. , of both plugins.
Ruben
+1  A: 

perhaps you should think about creating a backup/restore module for your app that is independent of the database (may be serialize to xml or json) - that way, when you change db, you also modify the backup/restore, in such a way that old domain data gets "upgraded" to ne domain data.

I like how django can do this automatically, but theres more magic in django that i dont understand...

Chii
+1  A: 

you can also alternatively try 'grails schema-export' command.. which would give you an output sql which has all the create commands for tables and constraints..

With that as reference, you can create your alter scripts. Thats the best currently available I guess..

Do let us know if you find a better way..

Satya