Skip to content

Instantly share code, notes, and snippets.

@namuan
Created November 1, 2013 17:06
Show Gist options
  • Save namuan/7268506 to your computer and use it in GitHub Desktop.
Save namuan/7268506 to your computer and use it in GitHub Desktop.
@GrabExclude(group='org.eclipse.jetty.orbit', module='javax.servlet', version='3.0.0.v201112011016')
@Grapes([
@Grab(group='dbunit', module='dbunit', version='2.2'),
@Grab(group='mysql', module='mysql-connector-java', version='5.1.26')
])
@GrabConfig(systemClassLoader=true)
import java.io.FileOutputStream
import java.sql.Connection
import java.sql.DriverManager
import org.dbunit.database.DatabaseConnection
import org.dbunit.database.IDatabaseConnection
import org.dbunit.database.QueryDataSet
import org.dbunit.dataset.xml.FlatXmlWriter
import groovy.sql.Sql
import com.mysql.jdbc.Driver
String connectionString = "jdbc:mysql://localhost:3306?zeroDateTimeBehavior=convertToNull"
String username = "<user>"
String password = "<database>"
String schema = "<dbSchema>"
def sql = Sql.newInstance( connectionString, username, password, "com.mysql.jdbc.Driver")
Connection conn = sql.getConnection()
IDatabaseConnection connection = new DatabaseConnection( conn );
QueryDataSet partialDataSet = new QueryDataSet(connection);
sql.eachRow("select * from information_schema.tables where TABLE_SCHEMA=:schema", [schema: schema]) {
String tableName = it.TABLE_NAME
println "--> ${tableName}"
partialDataSet.addTable(tableName, " SELECT * FROM ${schema}.${tableName}");
FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream("${schema}_${tableName}.xml"));
datasetWriter.write( partialDataSet );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment