Skip to content

Instantly share code, notes, and snippets.

@rayyildiz
Created March 13, 2012 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rayyildiz/2027501 to your computer and use it in GitHub Desktop.
Save rayyildiz/2027501 to your computer and use it in GitHub Desktop.
Generate LiquiBase changeLogs using Groovy
def writer = new FileWriter('sample2.xml')
// Define 2 tables
def tables =[]
def dept = new Table(tableName:'departments')
dept.columns << new Column(name:'id', type:'number(4,0)', nullable:false)
dept.columns << new Column(name:'dname', type:'varchar2(14)', remarks:'Department name')
tables << dept
def emp = new Table(tableName:'employees', remarks:'All employees known in HR system')
emp.columns << new Column(name:'id', type:'number(4,0)', nullable:false)
emp.columns << new Column(name:'ename', type:'varchar2(14)', remarks:'Full name')
emp.columns << new Column(name:'dept_id', type:'number(4,0)', nullable:false)
tables << emp
def cct = new ChangelogCreateTable(author: "james")
cct.generate(writer, tables)
class ChangelogCreateTable {
String author
def changesetId = 1
def generate(writer, tables) {
// MarkupBuilder cannot output an XML declaration, so this is a workaround
def eol = System.properties.'line.separator'
writer << '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' << eol
def xml = new groovy.xml.MarkupBuilder(writer)
xml.databaseChangeLog( xmlns : "http://www.liquibase.org/xml/ns/dbchangelog/1.9"
, "xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance"
, "xsi:schemaLocation" : "http://www.liquibase.org/xml/ns/dbchangelog/1.9 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd"
) {
tables.each { table ->
changeSet(author: author, id : changesetId++) {
createTable( table.getAttributes() ) {
table.columns.each { col ->
column(col.getAttributes() ) {
if(col.getConstraintsAttributes()) {
constraints(col.getConstraintsAttributes())
}
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment