Created
March 13, 2012 07:53
-
-
Save rayyildiz/2027501 to your computer and use it in GitHub Desktop.
Generate LiquiBase changeLogs using Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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