Grails domain class and DDL sql file using schema-export

04 / May / 2011 by Bhagwat Kumar 2 comments

While debugging a domain and its database schema I found an interesting grails command line Gant script: schema-export.

One of the great Grails command line Gant script to generate SQL scripts(DDL) which is executed against your database to model your grails domain. You can control the environment and the output file to generate the SQL script. Without going into much details lets see an example. Here is the only domain in my grails project :

Class Person{
String name
Integer age

static hasMany=[emails:String]

And here is the script I used

grails schema-export

Here is the content of generated DDL file which is created in project’s target folder with name ddl.sql by default(obviously data source settings of Development environment).

alter table person_emails drop foreign key FKADA31081669C99A9;
drop table if exists person;
drop table if exists person_emails;
create table person (id bigint not null auto_increment, version bigint not null, name varchar(255) not null, primary key (id)) ENGINE=InnoDB;
create table person_emails (person_id bigint, emails_string varchar(255)) ENGINE=InnoDB;
alter table person_emails add index FKADA31081669C99A9 (person_id), add constraint FKADA31081669C99A9 foreign key (person_id) references person (id);

You can also specify the environment (e.g. prod) whose data source settings will be used to generate DDL and output to a different file(e.g. prodDDL.sql) like :

grails prod schema-export prodDDL.sql

Full documentation ca be found here schema-export.

Hope it encourage you to read the Grails documentation in spare time to find and share hidden features of grails.

~~~~Cheers ~~~~

Bhagwat Kumar


comments (2)

    1. Bhagwat Kumar

      With the number of groovy/grails blogs that is read and written, it is difficult to keep track. 😉
      My eyes aren’t as keen as yours. Will keep an eye on other posts before posting my own.

      Thanks Burt.


Leave a Reply

Your email address will not be published. Required fields are marked *