Create custom migration script for bootstrap data and for performing some SQL related operations in Grails

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

  •  23-06-2023
  •  | 
  •  

Question

I am working on a Grails project. In this I need to do two things related Migration Scripts as follows :

  1. I made a script for boot strap data and also having BootStrap.groovy for boot strap data. My Code of Both scripts is as follows :

    BootStrape.groovy

    Mine DatabaseInit.groovy Script

    Now I want to implement(or move the code of) both of these scripts in my migration script. How to achieve that, please give me some hint.

  2. I need to do some SQL operations for removing some duplicate values from database as follow

    UPDATE DatabaseName.TableName1 SET ColumName = 2 WHERE ColumName IN (
    SELECT ColumName FROM DatabaseName.TableName2 st WHERE st.ColumName = 'XYZ');
    
    DELETE FROM DatabaseName.TableName2 WHERE ColumName NOT IN (2);
    

    How to write migration script for achieving this task.

Was it helpful?

Solution

It is a advisable idea that you keep your bootstrap file tiny ...no too much code inside and that good approach to follow , yours.

So , what you need is try to create a new migration scrip file even if you didn't have a change in your DOMAIN . Since ,this will help you add your sql script and then later will not execute them at every start up if they are once successfully created.

TODO on grails command line :

grails dbm-create-changelog
grails prod dbm-generate-gorm-changelog --add changelog-1.0.whatevername.groovy

Then ,

add the sql script inside changelog-1.0.whatevername.groovy file as follow :

1.Using sql file like 

    //sqlFile( path: "${ path to SQL file relative to changelog.groovy }")
    //Used in a changeset, it looks like this:
    changeSet(author: "username", id: "1305821637932-1") { //id:is to be changed
      sqlFile( path: "books.sql")
    }

2.Using by writing the scrip inside

changeSet(author: "username", id: "1305821637932-2") {  //id : is to be changed
  addColumn(tableName: "book") {
    column(name: "flammable", type: "BIT") {
       constraints(nullable: "false")
    }
  }
  SQL("UPDATE DatabaseName.TableName1 SET ColumName = 2 WHERE ColumName IN (
  SELECT ColumName FROM DatabaseName.TableName2 st WHERE st.ColumName = 'XYZ'))";
  SQL("DELETE FROM DatabaseName.TableName2 WHERE ColumName NOT IN (2)");
}

Happy coding!! please refer this and this

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