use of org.dbunit.dataset.xml.FlatXmlWriter in project RecordManager2 by moravianlibrary.
the class DBUnitHelper method dump.
public void dump(String outputFile) throws Exception {
try (Connection connection = dataSource.getConnection()) {
IDatabaseConnection conn = new DatabaseConnection(connection);
FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream(outputFile));
datasetWriter.setDocType("dataset.dtd");
datasetWriter.write(conn.createDataSet());
}
}
use of org.dbunit.dataset.xml.FlatXmlWriter in project openmrs-core by openmrs.
the class MigrateDataSet method doMigration.
/**
* Recurse over the given file and convert all xml files there in
*
* @param fileOrDirectory
* @throws Exception
*/
private static void doMigration(File fileOrDirectory) throws Exception {
String filename = fileOrDirectory.getName();
if (filename.startsWith(".svn") || filename.endsWith("TestingApplicationContext.xml")) {
// skip .svn files
} else if (fileOrDirectory.isDirectory()) {
for (File innerFile : fileOrDirectory.listFiles()) {
doMigration(innerFile);
}
} else if (filename.endsWith(".xml")) {
System.out.println("Migrating " + fileOrDirectory.getAbsolutePath());
System.out.println(execMysqlCmd("DROP DATABASE IF EXISTS " + tempDatabaseName, null, false));
System.out.println(execMysqlCmd("CREATE DATABASE " + tempDatabaseName + " DEFAULT CHARACTER SET utf8", null, false));
System.out.println(execMysqlCmd(null, OLD_SCHEMA_FILE, true));
System.out.println(execMysqlCmd(null, OLD_UPDATE_FILE, true));
// the straight-up database connection
String url = "jdbc:mysql://localhost/" + tempDatabaseName;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, credentials[0], credentials[1]);
// database connection for dbunit
IDatabaseConnection dbunitConnection = new DatabaseConnection(con);
try (InputStream fileOrDirectoryStream = new FileInputStream(fileOrDirectory)) {
PreparedStatement ps = con.prepareStatement("SET FOREIGN_KEY_CHECKS=0;");
ps.execute();
ps.close();
IDataSet dataset = new FlatXmlDataSet(fileOrDirectoryStream);
DatabaseOperation.REFRESH.execute(dbunitConnection, dataset);
// turn off foreign key checks here too.
System.out.println(execMysqlCmd("SET FOREIGN_KEY_CHECKS=0", NEW_UPDATE_FILE, true));
System.out.println("Dumping new xml file");
// get a new connection so dbunit knows the right column headers
dbunitConnection = new DatabaseConnection(con);
// full database export that will ignore empty tables
FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream(fileOrDirectory));
datasetWriter.write(dbunitConnection.createDataSet());
} catch (Exception e) {
System.err.println("Unable to convert: " + filename + " Error: " + e.getMessage());
} finally {
dbunitConnection = null;
}
System.out.println("Finished!");
}
}
Aggregations