agile database modeling with grails - preview of gorm 1.4 - sf grails meetup 2011-03
TRANSCRIPT
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!!
Agenda
New (GORM) Features in Grails 1.4Quick intro to H2Reverse EngineeringDatabase MigrationsOther new Grails 1.4 featuresSummary
New (GORM) Features in Grails 1.4
Replace HQSQL with H2Best-of-bread in-memory DBBuilt-in web console
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
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
Current Status
No hard dependencies on Grails 1.4Everything available as plugins todayBoth main features developed by Burt Beckwith
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
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
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
Take your DB tools with you
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
Use legacy databases in Grails even faster!
Reverse Engineering
Existing DB + Grails command = GORM classes
Reverse Engineering
Existing DB + Grails command = GORM classesUses DB connection from DataSource.groovyConfiguration in grails-app/conf/Config.groovyStarted with grails db-reverse-engineer
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
Demo Reverse Engineering
The good and the bad
Recognizes length limitations & constraintsCreates relates between entitiesEven handles composite primary keys
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
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
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 changesetsPlugin: Groovy changesets & automation
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")}}} ...
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
Database Migrations
Liquibase: verbose XML schema changesetsPlugin: Groovy changesets & automationTwo options
track changes manually (changeset first)create automatic diffs (GORM first)
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
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
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 missedChecksum errors
Changes tracked by filename ingrails-app/conf/migrations/xxxx.groovyIt doesn't like changeset changes
Summary
New goodies before Grails 1.4 releaseLots of automation when working with databasesReverse Engineering simplifies app setupBest 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
ContactEmail: [email protected]: @livingtocodeBlog: livingtocode.com
http://sfgrails.com@sfgrails
Thanks to Taulia for hosting and food!!