Agile Database Modeling with Grails - Preview of GORM 1.4 - SF Grails Meetup 2011-03

4,016 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,016
On SlideShare
0
From Embeds
0
Number of Embeds
416
Actions
Shares
0
Downloads
36
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Agile Database Modeling with Grails - Preview of GORM 1.4 - SF Grails Meetup 2011-03

  1. 1. Agile Database Modelling New GORM Features in Grails 1.4 Prepared for SF Grails Café Centro March 2011 by Christian Hang Thanks to Taulia for hosting and food!!
  2. 2. Agenda New (GORM) Features in Grails 1.4 Quick intro to H2 Reverse Engineering Database Migrations Other new Grails 1.4 features Summary
  3. 3. New (GORM) Features in Grails 1.4 Replace HQSQL with H2 Best-of-bread in-memory DB Built-in web console
  4. 4. New (GORM) Features in Grails 1.4 Replace HQSQL with H2 Best-of-breed in-memory DB Built-in web console Reverse Engineering Based on Hibernates reverse engineering Create GORM classes from existing DB
  5. 5. New (GORM) Features in Grails 1.4 Replace HQSQL with H2 Best-of-bread in-memory DB Built-in web console Reverse Engineering Based on Hibernates reverse engineering Create GORM classes from existing DB Database Migrations Based on Liquibase "Revision" the DB Allows track/rollback schema changes
  6. 6. Current Status No hard dependencies on Grails 1.4 Everything available as plugins today Both main features developed by Burt Beckwith
  7. 7. Current Status No hard dependencies on Grails 1.4 Everything available as plugins today Both main features developed by Burt Beckwith H2 plugin or JAR dependency com.h2database:h2:1.2.147
  8. 8. Current Status No hard dependencies on Grails 1.4 Everything available as plugins today Both main features developed by Burt Beckwith H2 plugin or JAR dependency com.h2database:h2:1.2.147 Reverse Engineering http://grails-plugins.github.com/grails-db-reverse-engineer/ grails install-plugin db-reverse-engineer latest version 0.3
  9. 9. Current Status No hard dependencies on Grails 1.4 Everything available as plugins today Both main features developed by Burt Beckwith H2 plugin or JAR dependency com.h2database:h2:1.2.147 Reverse Engineering http://grails-plugins.github.com/grails-db-reverse-engineer/ grails install-plugin db-reverse-engineer latest version 0.3 Database Migrations http://grails-plugins.github.com/grails-database-migration/ grails install-plugin database-migration latest version 0.2
  10. 10. Take your DB tools with you
  11. 11. H2 in Grails 1.4 Replaces HSQLDB support in-memory, file-based, clustered DB comes with a build-in web console http://localhost:8080/appname/dbconsole Available as (insecure) plugin today
  12. 12. Use legacy databasesin Grails even faster!
  13. 13. Reverse Engineering Existing DB + Grails command = GORM classes
  14. 14. Reverse Engineering Existing DB + Grails command = GORM classes Uses DB connection from DataSource.groovy Configuration in grails-app/conf/Config.groovy Started with grails db-reverse-engineer
  15. 15. Reverse Engineering Existing DB + Grails command = GORM classes Uses DB connection from DataSource.groovy Configuration in grails-app/conf/Config.groovy Started with grails db-reverse-engineer Detailed configuration available covers many-to-many relations include/exclude tables, columns etc. Creates belongsTo, hasMany and constraintsSimplest configuration:grails.plugin.reveng.packageName = com.acme.db
  16. 16. Demo Reverse Engineering
  17. 17. The good and the bad Recognizes length limitations & constraints Creates relates between entities Even handles composite primary keys
  18. 18. The good and the bad Recognizes length limitations & constraints Creates relates between entities Even handles composite primary keys Careful: Will probably not get it 100% correct! Always review generated classes
  19. 19. The good and the bad Recognizes length limitations & constraints Creates relates between entities Even handles composite primary keys Careful: Will probably not get it 100% correct! Always review generated classes Still helps to save a lot of work
  20. 20. Your source code is in VCS,but do you version your DB?
  21. 21. Database Migrations Liquibase: verbose XML schema changesets
  22. 22. Database Migrations Liquibase: verbose XML schema changesets<?xml version="1.0" encoding="UTF-8" standalone="no"?><databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd"> <changeSet author="christianhang (generated)" id="1300172497244-1"> <createTable tableName="BLURB"> <column autoIncrement="true" name="ID" type="BIGINT"> <constraints nullable="false" primaryKey="true" primaryKeyName="CONSTRAINT_3"/> </column> <column name="VERSION" type="BIGINT"> <constraints nullable="false"/> </column> <column name="CONTENT" type="VARCHAR(100000)"/> <column name="NAME" type="VARCHAR(255)"> <constraints nullable="false"/> </column> </createTable> </changeSet> ....
  23. 23. Database Migrations Liquibase: verbose XML schema changesets Plugin: Groovy changesets & automation
  24. 24. Database Migrations Liquibase: verbose XML schema changesets Plugin: Groovy changesets & automation databaseChangeLog = {changeSet(author: "christianhang (generated)", id: "1300168672278-1") {createTable(tableName: "BLURB") {column(autoIncrement: "true", name: "ID", type: "BIGINT") {constraints(nullable: "false", primaryKey: "true", primaryKeyName: "CONSTRAINT_3")}column(name: "VERSION", type: "BIGINT") {constraints(nullable: "false")}column(name: "CONTENT", type: "VARCHAR(100000)")column(name: "NAME", type: "VARCHAR(255)") {constraints(nullable: "false")}}} ...
  25. 25. Database Migrations Liquibase: verbose XML schema changesets Plugin: Groovy changesets & automationAutomatically generate changelogs grails dbm-generate-changelog changelog.groovygrails dbm-gorm-diffApply changelogs to databasegrails dbm-changelog-sync changelog.groovygrails dbm-updateRollback changesgrails dbm-rollback-countgrails dbm-rollback-to-date
  26. 26. Database Migrations Liquibase: verbose XML schema changesets Plugin: Groovy changesets & automation Two options track changes manually (changeset first) create automatic diffs (GORM first)
  27. 27. Database Migrations Liquibase: verbose XML schema changesets Plugin: Groovy changesets & automation Two options track changes manually (changeset first) create automatic diffs (GORM first) Allows to track schema changes in VCS Create DB from scratch Apply/rollback incremental changes
  28. 28. Database Migrations Liquibase: verbose XML schema changesets Plugin: Groovy changesets & automation Two options track changes manually (changeset first) create automatic diffs (GORM first) Allows to track schema changes in VCS Create DB from scratch Apply/rollback incremental changes Possibilities: Simplifies development with changing DB Manage updates to production DB
  29. 29. Demo Database Migrations
  30. 30. Issues to watch out for BACKUP your database! Always review automatic changesets!
  31. 31. Issues to watch out for BACKUP your database! Always review automatic changesets! Indexes, triggers etc. might be missed Checksum errors Changes tracked by filename in grails-app/conf/migrations/xxxx.groovy It doesnt like changeset changes
  32. 32. Summary New goodies before Grails 1.4 release Lots of automation when working with databases Reverse Engineering simplifies app setup Best practice: Version your DB
  33. 33. Referenceshttp://grails-plugins.github.com/grails-db-reverse-engineer/docs/manual/index.htmlhttp://grails-plugins.github.com/grails-db-reverse-engineer/docs/manual/index.htmlhttp://fbflex.wordpress.com/2011/01/19/working-with-the-grails-database-migration-plugin/http://burtbeckwith.com/blog/?p=376
  34. 34. Contact Email: christian.hang@gmail.com Twitter: @livingtocode Blog: livingtocode.com http://sfgrails.com @sfgrailsThanks to Taulia for hosting and food!!

×