Can Grails 2.x schema-export, or similar command, generate DDL for schema updates given a datasource?

StackOverflow https://stackoverflow.com/questions/12432203

  •  02-07-2021
  •  | 
  •  

Question

Grails schema-export does a great job of generating the DDL to create database schemas for a particular database. However what I would like to do, is have grails just output the DDL for updates to an already created schema, not the DDL to create it from scratch.

I'm thinking it should be possible, as grails does have the ability to actually update schemas if you specificy dbCreate = "update" in your datasource.

But I just want grails to spit out what it would run, not actually do it, so I can execute it myself through a SQL tool in a controlled manner.

Was it helpful?

Solution

As @GreyBeardedGeek pointed out in a comment, the Database Migration Plugin (which is included by default now) can generate the "schema diff" that you are looking for, and it does substantially more than what the vanilla dbCreate = "update" does, since Grails' native db updating relies only on what Hibernate provides, and it's very conservative in the updates it can do.

In particular, the part of the Database Migration plugin that you are looking for is the dbm-update-sql script, which will give you the SQL that it would run to update the DB to the current schema version.

In general, I would strongly advise using a tool like the Database Migration plugin, since it bundles your DB schema in with your application code and you can easily version them together, and it also includes lots of tooling so that running the updates is much less painful, and allows for rollbacks and custom scripts to massage things as needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top