Grails Tip: Using Schema Export

Ever wonder what the DDL would look like that Grails generates to create or update your database? Well, no need to wonder any longer. Grails is packed full with little things to make your life simpler, and one that you may not have heard of yet is the Grails ’schema-export’ command line option. Under the covers this handy utility delegates to Hibernate’s SchemaExport. The output can either go to a file or to standard out.

Examples:

  • ‘grails schema-export’ : generates the DDL for the development environment into the default file ddl.sql
  • ‘grails prod schema-export’ : generates the DDL for the production environment to the default file ddl.sql
  • ‘grails schema-export export’ : generates the export DDL (drop then create)
  • ‘grails schema-export stdout’ : generates the DDL and sends it to starndard out

Building Grails applications with PostgreSQL

I’ll be presenting a tutorial “Building Grails applications with PostgreSQL” along with my fellow Chariot Solutions colleague Ken Rimple at the PostgreSQL East 2010 conference in Philadelphia, PA. We are scheduled to present on March 25, 2010 at 11:00 AM.

This will be a 90 minute tutorial targeted at beginners of Grails. We plan to cover the following:

1. Overview of Grails

2. GORM

  • Properties and Validation
  • Associations and Composition

3. Configuring Grails with PostgreSQL

  • Environments: Dev, Test, and Production
  • Using DataSource.groovy

If you happen to be at the talk, be sure to introduce yourself and say hello!

Grails 1.2.1 is Released

Great news for all Grails developers, it appears that the core Grails team is still hard at work. Today it was announced that Grails 1.2.1 is ready. A quick look at the release notes and I see that there aren’t any major new features, but there are a lot of bug fixes. While this alone is encouraging, when I looked at the Grails Roadmap I see issues cued up for 1.2.2 and a release date of 1.3 is still listed as March 30. So far I’m liking the first quarter of 2010!

Defining and Testing Constraints on Grails Domain Classes

Defining constraints in Grails domain classes is achieved quite simply with a static property defined directly in the domain class. For example, I can define a User domain class and its constraints as follows:

class User {
  String login
  String password
  String email
  Integer age

  static constraints = {
    login(size:5..15, blank:false, unique:true)
    password(size:5..15, blank:false)
    email(email:true, blank:false)
    age(min:18, nullable:false)
  }
}

Constraints are syntactically formatted as method calls with the name of the method matching the name of the property the constraint is to be associated with. The method parameters are named parameters specifying the constraint. On User we have defined a constraint for each property. This was my decision to constrain each property, there is not a requirement to name a constraint for all properties in a domain class.

So far so good, but now I want to verify my constraints work the way I expect. The easiest way to test constraints is as part of a unit test. Grails provides mocking support in unit tests, so to setup a mock domain class for constraint testing I would create the following test class.

import grails.test.*

class UserTests extends GrailsUnitTestCase {

  protected void setUp() {
    super.setUp()
    mockForConstraintsTests(User)
  }

  protected void tearDown() {
    super.tearDown()
  }

  void testValidation() {
    def user = new User(login:'jdoe', password:'passw0rd',email:'jdoe@foo.com',age:32)
    assertTrue user.validate()
  }
}

That is it. One item of note, I recently found out that it is possible to pass a list of property names to the valdiate() method indicating that the validation should only be run on those properties. For example, if I only want to validate the login and password properties on an instance of User then I would do the following.

user.validate(['login','password'])

This trick, however, does not work in unit tests as it appears the validate method provided by the mock does not accept any parameters. Still, this will come in handy when I know that I don’t have a fully populated domain POGO, but want to validate what I have. Say, for example, in a UI wizard where each step sets a partial set of properties on the domain class.

DevNews Podcast Episode #2

In case you are not already a follower of the Chariot TechCast, you may have missed the fact that TechCast host, Ken Rimple, and I posted the second edition of the new DevNews podcast. We plan for this to be a short, weekly podcast that covers some quick news items from the week. In DevNews episode #2 we cover:

  • Google news including GDrive and a primer on Google Go
  • Maven’s switch to Guice and a demo of Maven POM files in Groovy
  • Dependency injection annotations JSR-330 and JSR-299 released with JavaEE 6
  • Flex, OSGi, and Gremlin

If you have suggestions on topics we should cover in the future, you can tag related news items on Delicious with the tag devnewsideas and we will consider your story for the podcast.

While you are checking out the DevNews, be sure to catch podcasts with Venkat Subramaniam and Jeremy Grelle. Both of these guests will be speakers at the Emerging Technologies for the Enterprise Conference being held in Philadelphia on April 8-9, 2010. In the interest of full disclosure, this conference is hosted and presented by Chariot Solutions – my employer. This is the 5th year this conference will be presented, and it just keeps getting better. We have a great list of speakers and topics lined up, so check it out and be sure to register soon to get the early bird discount.

Spring, Groovy, and Grails Closing 2009 With a Bang

It appears that the folks at SpringSouce have been hard at work, and are giving Groovy and Grails folks a lot to look forward to in 2010. Let me give you a quick recap of the past week or so and just highlight some of the recent events.

It started last week with two Groovy related announcements. First, we were treated to Groovy 1.7 RC2 and found out that we have every reason to expect Groovy 1.7 GA before Christmas.

See: Groovy 1.7-RC-2 out there

Also, last week the Groovy-Eclipse team quietly released 2.0M2 of the Groovy Eclipse plug-in. They claim to have addressed over 100 issues, which includes a better inference engine, better refactoring support, task tag support, and content assist in GStrings. All in all, it looks like some nice improvements.

See: Groovy-Eclipse 2.0.0M2 New and Noteworthy

Then today there was an explosion of news all starting with the release of Spring Framework 3.0. There is a lot of good new stuff in there, and I’ll leave it to Juergen Hoeller to lay it all out for you.

See: Spring Framework 3.0 goes GA

This was followed very closely with the release of Grails 1.2 RC2 which includes the new Spring Framework 3.0 GA.

See: Grails 1.2-RC2 Release Notes

Finally, Guillaume Laforge posted an update on the modularity work that is being done as part of the Groovy 1.8 effort. This gist of this is to modularize Groovy into smaller pieces rather than having one, large groovy-all.jar distribution. Part of this effort will include replacing the current Ant based build with Gradle. This is a nice feather in the cap for Gradle.

See: Groovy 1.8 modularization

I am very excited to see what is to come in 2010 for the Groovy and Grails community. Judging by the activities of just the past month, there is a lot to be excited about for the year to come.

LIVESTRONG Challenge Registration is Open

lsc_logo.gifToday, December 1, marks the opening for participants to register for the 2010 LIVESTRONG Challenge events. As a bonus, for one day the registration fee has been reduced to $35. You can register right now by going to the Team LIVESTRONG site. The dates and locations for the 2010 LIVESTRONG Challenge events are:

  • Seattle, WA: June 20, 2010
  • San Jose, CA: July 11, 2010
  • Philly, PA: August 21–22, 2010
  • Austin, TX: October 23–24, 2010

By the time you read this post, the promotion may very well be over. Even if you have missed the promotion, please consider registering right now to walk, run, or ride – and join the fight against cancer.

I will be riding again in 2010. This will be my 4th year in a row riding with Team Margaritaville or Bust (or TMOB for short), which is the official cycling team for the Salt Shaker Foundation. If you are in the greater Philly area and would like to join a team, please consider joining TMOB. We would love to have you ride, walk, or run with us!

This past year, cancer has continued to impact my extended family. My fight against cancer will continue as I ride, and wear a yellow wristband to remind myself every day to live strong. Cancer sucks – join the fight!

Philly on Grails Materials Posted

Chariot SolutionsThe slides and code samples from the recent Chariot Solutions “Fall Forecast: Groovy on Grails” seminar are now available online. You can view and download the slides from all the presentations, as well as the code from the evening hack-a-thon event.

Groovy on Grails seminar downloads: http://www.chariotsolutions.com/events/groovy

If you also head over to the Chariot Solutions team blog, you will see some video testimonials (including one from me) taken at the event. The team blog is also a great resource to follow for news about other upcoming events, like the Emerging Technologies for the Enterprise conference in April 2010. There should be some more great Groovy and Grails talks at this conference, so watch the site as speakers and topics are announced.

Chariot Solutions team blog: http://chariotsolutions.blogspot.com/
Emerging Technologies for the Enterprise: http://www.phillyemergingtech.com/

Expect Some Twists and Turns

CurvesFor those of you that have found my blog, my little corner of the Internet, you should expect some twists and turns as I wander into the world of WordPress. I’ve owned this domain for a some time now, and I’ve been tinkering with WordPress, so I should be ready. But like all great (and some not so great) new releases – there are issues. I’m finding that to be true here as well.

So you’ve been warned, and I do apologize in advance. I don’t expect to do things that will flat out break this site, but as my years in software have taught me – expect the unexpected. If you find something this isn’t quite right feel free to contact me and let me know. Suggestions, do the same. I’d be happy to hear your ideas.

Photo credit: http://www.flickr.com/photos/spine/ / CC BY 2.0

Philly on Grails: The Day After and Loving Plugins

Philadelphia Love ParkIn case you missed it, there was a full on geek love-fest in Philly yesterday. A capacity group of 50 developers gathered together at the “Fall Forecast 2009: Groovy on Grails” seminar sponsored by my employer, Chariot Solutions. The seminar featured Jeff Brown, a core member of the Groovy and Grails team at SpringSource. I was thrilled to see the Philadelphia Grails community out in full force. Jeff, along with fellow Chariot consultants Ken Rimple and Gordon Dickens, delivered several info packed sessions. The day concluded with a hands on “hack-a-thon” where folks could try out some Grails techniques first hand. I’ll never name any names, but even a few Rails enthusiasts in attendance were impressed with some of the Grails capabilities.

As I recover from my Grails punch-drunk hangover and reflect on the seminar, there is one thing in particular that I keep coming back to – the Grails Framework plugin architecture. At the seminar, Jeff Brown ran a session on plugins during which he demonstrated how to create and use plugins as part of your Grails application development. I was impressed at how simple it is to build a Grails plugin, and how such a small amount of code can do so much for your application. Just in case everyone in the room was not already impressed, Jeff showed us his Clojure Plugin that allows the use of Clojure classes inside of any Grails application. Again, what was most impressive was how little code was required to enable such a powerful feature. In the end, we learned that nearly any library that can be invoked via a Java API can be integrated into Grails with a plugin.

Grails LogoBefore the seminar, I already knew that Grails should be on everyone’s short list when considering web application development technologies. Powered by the Spring Framework, employing the simplicity of Groovy, adopting convention over configuration, and allowing you to leverage your existing investment in Java – it is hard not to love Grails. With the over 300 Grails Plugins, there was already plenty of opportunity to quickly extend Grails with any number of useful libraries. But what I am now convinced of is that the plugin architecture goes beyond being just useful and actually makes the Grails Framework one of the most compelling options to create web applications targeted at the Java Platform. Nearly any aspect of the framework can be extended or modified with the use of a plugin – Spring is the one thing that is not plug-able. Need to use a language that runs in the JVM but it isn’t Java, that isn’t a problem. Your investment in the Java Platform is protected by the fact that Grails will permit you to leverage your existing languages and libraries via plugins. And when the day comes, as it always does, to replace or update that library – install the new or updated plugin and you should hardly miss a beat. And while I’ve mostly talked about plugins for extensibility, let us not forget using plugins for code reuse. Plugins enable reuse between web apps with such ease that any battle hardened Java web developer will never want to think about extending a Struts Action or wiring a Spring Servlet context again as long as they live.

Philadelphia Love Park photo credit: http://www.flickr.com/photos/katietegtmeyer/ / CC BY 2.0