{"id":3734,"date":"2011-05-04T00:49:07","date_gmt":"2011-05-03T19:19:07","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=3734"},"modified":"2011-05-04T00:52:19","modified_gmt":"2011-05-03T19:22:19","slug":"grails-domain-class-and-ddl-sql-file-using-schema-export","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-domain-class-and-ddl-sql-file-using-schema-export\/","title":{"rendered":"Grails domain class and DDL sql file using schema-export"},"content":{"rendered":"<p>While debugging a domain and its database schema I found an interesting <code>grails command line Gant script<\/code>: <a href=\"http:\/\/www.grails.org\/doc\/latest\/ref\/Command%20Line\/schema-export.html\" target=\"_blank\"><strong>schema-expor<\/strong>t<\/a>.<\/p>\n<p>One of the great <code>Grails command line Gant script<\/code> to generate<code> SQL scripts(DDL) <\/code> which is executed against your database to model your <code>grails domain<\/code>. 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 <code>grails project<\/code> :<\/p>\n<p>[groovy]<br \/>\nClass Person{<br \/>\n\tString name<br \/>\n\tInteger age<\/p>\n<p>static hasMany=[emails:String]<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>And here is the script I used<\/p>\n<p>[shell]<br \/>\ngrails schema-export<br \/>\n[\/shell]<\/p>\n<p>Here is the content of generated DDL file which is created in project&#8217;s target folder with name ddl.sql by default(obviously data source settings of Development environment).<\/p>\n<p>[sql]<br \/>\nalter table person_emails drop foreign key FKADA31081669C99A9;<br \/>\ndrop table if exists person;<br \/>\ndrop table if exists person_emails;<br \/>\ncreate table person (id bigint not null auto_increment, version bigint not null, name varchar(255) not null, primary key (id)) ENGINE=InnoDB;<br \/>\ncreate table person_emails (person_id bigint, emails_string varchar(255)) ENGINE=InnoDB;<br \/>\nalter table person_emails add index FKADA31081669C99A9 (person_id), add constraint FKADA31081669C99A9 foreign key (person_id) references person (id);<br \/>\n[\/sql]<\/p>\n<p>You can also specify the environment (e.g. <code>prod<\/code>) whose <code>data source<\/code> settings will be used to generate DDL and output to a different file(e.g. <code>prodDDL.sql<\/code>) like :<\/p>\n<p>[shell]<br \/>\ngrails prod schema-export prodDDL.sql<br \/>\n[\/shell]<\/p>\n<p>Full documentation ca be found here <a href=\"http:\/\/www.grails.org\/doc\/latest\/ref\/Command%20Line\/schema-export.html\" target=\"_blank\"><strong>schema-expor<\/strong>t<\/a>.<\/p>\n<p>Hope it encourage you to read the <strong><a href=\"http:\/\/www.grails.org\/doc\/latest\/\">Grails documentation<\/a><\/strong> in spare time to find and share <strong>hidden features of grails<\/strong>.<\/p>\n<p>~~~~Cheers ~~~~<\/p>\n<p>Bhagwat Kumar<br \/>\nbhagwat(at)intelligrape(dot)com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grails Schema-export GANT script to generate DDL sql file of the grails project domains.<\/p>\n<p>eg.<br \/>\n   grails prod schema-export myProd.sql<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[7],"tags":[563,562,561,559,4840,560,558],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3734"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=3734"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3734\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}