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!!
Agenda

 New (GORM) Features in Grails 1.4
 Quick intro to H2
 Reverse Engineering
 Database Migrations
 Other new Grails 1.4 features
 Summary
New (GORM) Features in Grails 1.4

 Replace HQSQL with H2
   Best-of-bread in-memory DB
   Built-in web console
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 Hibernate's reverse engineering
   Create GORM classes from existing DB
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 Hibernate's reverse engineering
    Create GORM classes from existing DB
 Database Migrations
   Based on Liquibase
   "Revision" the DB
   Allows track/rollback schema changes
Current Status

  No hard dependencies on Grails 1.4
  Everything available as plugins today
  Both main features developed by Burt Beckwith
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
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
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
Take your DB tools with you
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
Use legacy databases
in Grails even faster!
Reverse Engineering

 Existing DB + Grails command = GORM classes
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
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 constraints
Simplest configuration:
grails.plugin.reveng.packageName = com.acme.db
Demo Reverse Engineering
The good and the bad

 Recognizes length limitations & constraints
 Creates relates between entities
 Even handles composite primary keys
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
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
Your source code is in VCS,
but do you version your DB?
Database Migrations

 Liquibase: verbose XML schema changesets
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>
  ....
Database Migrations

 Liquibase: verbose XML schema changesets
 Plugin: Groovy changesets & automation
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")
}
}
}
     ...
Database Migrations

    Liquibase: verbose XML schema changesets
    Plugin: Groovy changesets & automation
Automatically generate changelogs
    grails dbm-generate-changelog changelog.groovy
grails dbm-gorm-diff


Apply changelogs to database
grails dbm-changelog-sync changelog.groovy
grails dbm-update

Rollback changes
grails dbm-rollback-count
grails dbm-rollback-to-date
Database Migrations

 Liquibase: verbose XML schema changesets
 Plugin: Groovy changesets & automation
 Two options
    track changes manually (changeset first)
    create automatic diffs (GORM first)
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
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
Demo Database Migrations
Issues to watch out for

  BACKUP your database!
  Always review automatic changesets!
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 doesn't like changeset changes
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
References

http://grails-plugins.github.com/grails-db-reverse-
engineer/docs/manual/index.html

http://grails-plugins.github.com/grails-db-reverse-
engineer/docs/manual/index.html

http://fbflex.wordpress.com/2011/01/19/working-with-the-grails-
database-migration-plugin/

http://burtbeckwith.com/blog/?p=376
Contact
 Email: christian.hang@gmail.com
 Twitter: @livingtocode
 Blog: livingtocode.com




                                          http://sfgrails.com
                                          @sfgrails


Thanks to Taulia for hosting and food!!

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

  • 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.
    Agenda New (GORM)Features in Grails 1.4 Quick intro to H2 Reverse Engineering Database Migrations Other new Grails 1.4 features Summary
  • 3.
    New (GORM) Featuresin Grails 1.4 Replace HQSQL with H2 Best-of-bread in-memory DB Built-in web console
  • 4.
    New (GORM) Featuresin Grails 1.4 Replace HQSQL with H2 Best-of-breed in-memory DB Built-in web console Reverse Engineering Based on Hibernate's reverse engineering Create GORM classes from existing DB
  • 5.
    New (GORM) Featuresin Grails 1.4 Replace HQSQL with H2 Best-of-bread in-memory DB Built-in web console Reverse Engineering Based on Hibernate's reverse engineering Create GORM classes from existing DB Database Migrations Based on Liquibase "Revision" the DB Allows track/rollback schema changes
  • 6.
    Current Status No hard dependencies on Grails 1.4 Everything available as plugins today Both main features developed by Burt Beckwith
  • 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.
    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.
    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.
    Take your DBtools with you
  • 11.
    H2 in Grails1.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.
    Use legacy databases inGrails even faster!
  • 13.
    Reverse Engineering ExistingDB + Grails command = GORM classes
  • 14.
    Reverse Engineering ExistingDB + Grails command = GORM classes Uses DB connection from DataSource.groovy Configuration in grails-app/conf/Config.groovy Started with grails db-reverse-engineer
  • 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 constraints Simplest configuration: grails.plugin.reveng.packageName = com.acme.db
  • 16.
  • 17.
    The good andthe bad Recognizes length limitations & constraints Creates relates between entities Even handles composite primary keys
  • 18.
    The good andthe 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.
    The good andthe 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.
    Your source codeis in VCS, but do you version your DB?
  • 21.
    Database Migrations Liquibase:verbose XML schema changesets
  • 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.
    Database Migrations Liquibase:verbose XML schema changesets Plugin: Groovy changesets & automation
  • 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.
    Database Migrations Liquibase: verbose XML schema changesets Plugin: Groovy changesets & automation Automatically generate changelogs grails dbm-generate-changelog changelog.groovy grails dbm-gorm-diff Apply changelogs to database grails dbm-changelog-sync changelog.groovy grails dbm-update Rollback changes grails dbm-rollback-count grails dbm-rollback-to-date
  • 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.
    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.
    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.
  • 30.
    Issues to watchout for BACKUP your database! Always review automatic changesets!
  • 31.
    Issues to watchout 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 doesn't like changeset changes
  • 32.
    Summary New goodiesbefore Grails 1.4 release Lots of automation when working with databases Reverse Engineering simplifies app setup Best practice: Version your DB
  • 33.
  • 34.
    Contact Email: christian.hang@gmail.com Twitter: @livingtocode Blog: livingtocode.com http://sfgrails.com @sfgrails Thanks to Taulia for hosting and food!!