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

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

Embed Size (px)

Citation preview

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

Agile Database ModellingNew GORM Features in Grails 1.4

Prepared for SF Grails Café Centro March 2011

by Christian Hang

Thanks to Taulia for hosting and food!!

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

Agenda

New (GORM) Features in Grails 1.4Quick intro to H2Reverse EngineeringDatabase MigrationsOther new Grails 1.4 featuresSummary

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

New (GORM) Features in Grails 1.4

Replace HQSQL with H2Best-of-bread in-memory DBBuilt-in web console

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

New (GORM) Features in Grails 1.4

Replace HQSQL with H2Best-of-breed in-memory DBBuilt-in web console

Reverse Engineering Based on Hibernate's reverse engineering Create GORM classes from existing DB

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

New (GORM) Features in Grails 1.4

Replace HQSQL with H2Best-of-bread in-memory DBBuilt-in web console

Reverse Engineering Based on Hibernate's reverse engineering Create GORM classes from existing DB

Database MigrationsBased on Liquibase"Revision" the DBAllows track/rollback schema changes

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

Current Status

No hard dependencies on Grails 1.4Everything available as plugins todayBoth main features developed by Burt Beckwith

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

Current Status

No hard dependencies on Grails 1.4Everything available as plugins todayBoth main features developed by Burt BeckwithH2 plugin or JAR dependency

com.h2database:h2:1.2.147

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

Current Status

No hard dependencies on Grails 1.4Everything available as plugins todayBoth main features developed by Burt BeckwithH2 plugin or JAR dependency

com.h2database:h2:1.2.147Reverse Engineering

http://grails-plugins.github.com/grails-db-reverse-engineer/grails install-plugin db-reverse-engineerlatest version 0.3

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

Current Status

No hard dependencies on Grails 1.4Everything available as plugins todayBoth main features developed by Burt BeckwithH2 plugin or JAR dependency

com.h2database:h2:1.2.147Reverse Engineering

http://grails-plugins.github.com/grails-db-reverse-engineer/grails install-plugin db-reverse-engineerlatest version 0.3

Database Migrationshttp://grails-plugins.github.com/grails-database-migration/grails install-plugin database-migrationlatest version 0.2

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

Take your DB tools with you

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

H2 in Grails 1.4

Replaces HSQLDBsupport in-memory, file-based, clustered DBcomes with a build-in web console

http://localhost:8080/appname/dbconsoleAvailable as (insecure) plugin today

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

Use legacy databases in Grails even faster!

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

Reverse Engineering

Existing DB + Grails command = GORM classes

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

Reverse Engineering

Existing DB + Grails command = GORM classesUses DB connection from DataSource.groovyConfiguration in grails-app/conf/Config.groovyStarted with grails db-reverse-engineer

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

Reverse Engineering

Existing DB + Grails command = GORM classesUses DB connection from DataSource.groovyConfiguration in grails-app/conf/Config.groovyStarted with grails db-reverse-engineerDetailed configuration available

covers many-to-many relationsinclude/exclude tables, columns etc.

Creates belongsTo, hasMany and constraints

Simplest configuration:

grails.plugin.reveng.packageName = com.acme.db

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

Demo Reverse Engineering

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

The good and the bad

Recognizes length limitations & constraintsCreates relates between entitiesEven handles composite primary keys

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

The good and the bad

Recognizes length limitations & constraintsCreates relates between entitiesEven handles composite primary keysCareful: Will probably not get it 100% correct!Always review generated classes

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

The good and the bad

Recognizes length limitations & constraintsCreates relates between entitiesEven handles composite primary keysCareful: Will probably not get it 100% correct!Always review generated classesStill helps to save a lot of work

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

Your source code is in VCS,but do you version your DB?

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

Database Migrations

Liquibase: verbose XML schema changesets

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

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> ....

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

Database Migrations

Liquibase: verbose XML schema changesetsPlugin: Groovy changesets & automation

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

Database Migrations

Liquibase: verbose XML schema changesetsPlugin: 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")}}} ...

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

Database Migrations

Liquibase: verbose XML schema changesetsPlugin: 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

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

Database Migrations

Liquibase: verbose XML schema changesetsPlugin: Groovy changesets & automationTwo options

track changes manually (changeset first)create automatic diffs (GORM first)

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

Database Migrations

Liquibase: verbose XML schema changesetsPlugin: Groovy changesets & automationTwo options

track changes manually (changeset first)create automatic diffs (GORM first)

Allows to track schema changes in VCSCreate DB from scratchApply/rollback incremental changes

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

Database Migrations

Liquibase: verbose XML schema changesetsPlugin: Groovy changesets & automationTwo options

track changes manually (changeset first)create automatic diffs (GORM first)

Allows to track schema changes in VCSCreate DB from scratchApply/rollback incremental changes

Possibilities:Simplifies development with changing DBManage updates to production DB

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

Demo Database Migrations

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

Issues to watch out for

BACKUP your database!Always review automatic changesets!

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

Issues to watch out for

BACKUP your database!Always review automatic changesets!Indexes, triggers etc. might be missedChecksum errors

Changes tracked by filename ingrails-app/conf/migrations/xxxx.groovyIt doesn't like changeset changes

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

Summary

New goodies before Grails 1.4 releaseLots of automation when working with databasesReverse Engineering simplifies app setupBest practice: Version your DB

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

ContactEmail: [email protected]: @livingtocodeBlog: livingtocode.com

http://sfgrails.com@sfgrails

Thanks to Taulia for hosting and food!!