Search code examples
grailsdata-migration

Grail data-migration-plugin


I followed the instruction from this link http://grails-plugins.github.io/grails-database-migration/3.0.x/index.html#introduction

First I added the lines needed in application.yml:

buildscript {
   dependencies {
      ...
      classpath 'org.grails.plugins:database-migration:3.0.0'
   }
}

dependencies {
   ...
     compile 'org.grails.plugins:database-migration:3.0.0'
     compile 'org.liquibase:liquibase-core:3.5.3'
}

sourceSets {
    main {
        resources {
            srcDir 'grails-app/migrations'
        }
    }
}

Then I run "grails dbm-generate-changelog changelog.groovy" and after that: "grails dbm-changelog-sync" Then I added a view to the changelog.groovy file:

databaseChangeLog = {

    changeSet(author: "xxx (generated)", id: "1490002519504-99", contexts: 'Test') {
        createView("""
            SELECT     dbo.Client.ClientNo, dbo.Client.ClientName
            FROM       dbo.Client INNER JOIN
                       dbo.ClientRole ON dbo.Client.ClientNo = dbo.ClientRole.ClientNo AND dbo.ClientRole.RoleType = 2
                       """, viewName: 'dbo.vw_supplier'
                       )
    }

    changeSet(author: "xxx (generated)", id: "1490002519504-1", contexts: 'Test') {
        createTable(tableName: "orders") {
            column(autoIncrement: "true", name: "id", type: "bigint") {
                constraints(primaryKey: "true", primaryKeyName: "PK__orders__3213E83F6FCA6F65")
            }

I added the following lines into application.groovy:

grails.plugin.databasemigration.updateOnStartFilename = 'changelog.groovy'
grails.plugin.databasemigration.updateOnStart = true
grails.plugin.databasemigration.updateOnStartContexts = ['Test']

After some tries I commented the last line to not use "contexts" but it didn't changed anything.

I also changed in bootstrap for development create-drop to none. I removed all tables from the database and run "grails dbm-update" It then creates all tables but not the view I added to the "changelog.groovy". The tables was all created without any column. I also received a strange message:

"INFO 2017-03-20 12:54: liquibase: Can not use class org.grails.plugins.databasemigration.liquibase.GormDatabase as a Liquibase service because it does not have a no-argument constructor"


buildscript {
    repositories {
        mavenLocal()
        maven { url "https://repo.grails.org/grails/core" }
    }
    dependencies {
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.11.6"
        classpath "org.grails.plugins:hibernate5:6.0.4"
        classpath 'org.grails.plugins:database-migration:3.0.0'
    }
}

My project now totally stalled.


Solution

  • your syntax for create view is not correct add selectQuery= infront of the string to create view.

    createView(selectQuery="""
        SELECT     dbo.Client.ClientNo, dbo.Client.ClientName
        FROM       dbo.Client INNER JOIN
                   dbo.ClientRole ON dbo.Client.ClientNo = dbo.ClientRole.ClientNo AND dbo.ClientRole.RoleType = 2
                   """, viewName: 'dbo.vw_supplier'
                   )