Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we migrate/update the database schema in Grails?

We've been working with Grails for a while and my Team Lead raised some questions about the Grails ORM (GORM):

  1. How do we maintain the database schema once we have moved to production?
  2. Can we update the database schema with Grails?
  3. If the schema is updated, will the changes be automatically reflected / does the framework take care of this?
  4. Is there any plugin for Grails that will allow us to update the schema without headaches?
like image 441
ferronrsmith Avatar asked Jan 31 '11 20:01

ferronrsmith


People also ask

What is schema in data migration?

Database migrations, also known as schema migrations, database schema migrations, or simply migrations, are controlled sets of changes developed to modify the structure of the objects within a relational database.

How the database is migrated?

Database migration is the process of migrating data from one or more source databases to one or more target databases by using a database migration service. When a migration is finished, the dataset in the source databases resides fully, though possibly restructured, in the target databases.

What is SQL schema migration?

A schema migration is performed on a database whenever it is necessary to update or revert that database's schema to some newer or older version. Migrations are performed programmatically by using a schema migration tool.


3 Answers

I recently released the official Grails plugin for database migrations - see http://grails.org/plugin/database-migration and the docs at http://grails-plugins.github.com/grails-database-migration/docs/manual/index.html

I'm working with the author of Liquibase on this, so the older liquibase plugin is now deprecated and the new one should be used since it uses the latest version of Liquibase (2.0) and is officially supported by SpringSource. See http://blog.liquibase.org/2011/01/new-standard-liquibase-plugin-grails-database-migration.html for his announcement.

Ask usage questions on the Grails User mailing list (signup from http://grails.org/Mailing+lists) or the new plugin forum at http://grails-plugins.847840.n3.nabble.com/ or email the author directly :)

like image 56
Burt Beckwith Avatar answered Oct 23 '22 10:10

Burt Beckwith


  1. Remove dbCreate parameter in DataSource.groovy for your production environment - this will stop GORM from auto-updating DB schema.

  2. Sure. Use LiquiBase plugin.

  3. GORM can do it with dbCreate='update', but it's strongly not recommended. For instance, if you rename a field, GORM/LiquiBase can never determine that you have to migrate the data, and not just drop+create.

  4. In one line: grails db-diff to generate LiquiBase's changelog.xml, and grails migrate -Dgrails.env=<whatever environment> to apply it to respective db server.

like image 41
Victor Sergienko Avatar answered Oct 23 '22 09:10

Victor Sergienko


While the "auto create" functionality is ok to get a project up and running I find liquibase the best way to keep the db up-to-date. There is a grails plugin and I believe work is under way on a DSL too.

So, create a baseline schema (you could use liquibase's generate-changelog) then make all future changes through liquibase and it will manage the updates, rollbacks and even some db interop for you. You can set your DataSource.groovy config to verify and grails will not start up if the schema does not match the domain config:

environments {
    development {
        dataSource {
            dbCreate = "validate"

You may also be interested in the liquibase-runner plugin to run your migrations on application start.

like image 31
Dave Bower Avatar answered Oct 23 '22 11:10

Dave Bower