ALF-11255: Create tool to dump "reference" schemas to XML

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@31650 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward
2011-11-02 18:36:02 +00:00
parent e263990429
commit 067e8fffac
6 changed files with 220 additions and 7 deletions

View File

@@ -0,0 +1,100 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.util.schemacomp;
import java.io.File;
import javax.xml.transform.stream.StreamResult;
import org.alfresco.util.schemacomp.model.Schema;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* Tool to export a database schema to an XML file.
*
* @author Matt Ward
*/
public class DbToXML
{
private ApplicationContext context;
private File outputFile;
private String namePrefix = "alf_";
/**
* Constructor. Uses a default name prefix of 'alf_' during the export.
*/
public DbToXML(ApplicationContext context, File outputFile)
{
this.context = context;
this.outputFile = outputFile;
}
/**
* Constructor. Allows specification of a name prefix, e.g. "jpbm_", that
* will be used during the export.
*/
public DbToXML(ApplicationContext context, File outputFile, String namePrefix)
{
this(context, outputFile);
this.namePrefix = namePrefix;
}
public void execute()
{
ExportDb exporter = null;
try
{
exporter = new ExportDb(context);
exporter.setNamePrefix(namePrefix);
exporter.execute();
}
catch (Exception e)
{
System.err.println("Unable to read database schema.");
e.printStackTrace();
System.exit(1);
}
if (exporter != null)
{
Schema schema = exporter.getSchema();
SchemaToXML schemaToXML = new SchemaToXML(schema, new StreamResult(outputFile));
schemaToXML.execute();
}
}
public static void main(String[] args)
{
if (args.length != 2)
{
System.err.println("Usage:");
System.err.println("java " + DbToXML.class.getName() + " <context.xml> <output.xml>");
System.exit(1);
}
String contextPath = args[0];
File outputFile = new File(args[1]);
ApplicationContext context = new FileSystemXmlApplicationContext(contextPath);
DbToXML dbToXML = new DbToXML(context, outputFile);
dbToXML.execute();
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.util.schemacomp;
import java.io.File;
import org.alfresco.util.ApplicationContextHelper;
import org.alfresco.util.TempFileProvider;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
/**
* Tests for the DbToXML class.
*
* @author Matt Ward
*/
public class DbToXMLTest
{
@Test
public void execute()
{
ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
File outFile = new File(TempFileProvider.getTempDir(), getClass().getSimpleName() + ".xml");
System.out.println("Writing to temp file: " + outFile);
DbToXML dbToXML = new DbToXML(ctx, outFile);
dbToXML.execute();
}
}

View File

@@ -58,6 +58,8 @@ public class ExportDb
private Dialect dialect;
private String namePrefix = "%";
public ExportDb(ApplicationContext context) throws Exception
@@ -114,20 +116,34 @@ public class ExportDb
public void execute() throws Exception
public void execute()
{
PropertyCheck.mandatory(this, "dataSource", dataSource);
// Get a Connection
Connection connection = dataSource.getConnection();
Connection connection = null;
try
{
connection = dataSource.getConnection();
connection.setAutoCommit(false);
execute(connection);
}
catch (Exception e)
{
throw new RuntimeException("Unable to execute export.", e);
}
finally
{
try { connection.close(); } catch (Throwable e) {}
try
{
if (connection != null)
{
connection.close();
}
}
catch (Throwable e)
{
// Little can be done at this stage.
}
}
}
@@ -154,7 +170,7 @@ public class ExportDb
schema = new Schema(schemaName);
final ResultSet tables = dbmd.getTables(null, schemaName, "%", new String[]
final ResultSet tables = dbmd.getTables(null, schemaName, namePrefixFilter(), new String[]
{
"TABLE", "VIEW"
});
@@ -282,6 +298,7 @@ public class ExportDb
tables.close();
}
/**
* Convert a boolean string as used in the database, to a boolean value.
*
@@ -358,6 +375,28 @@ public class ExportDb
}
/**
* @return the namePrefix
*/
public String getNamePrefix()
{
return this.namePrefix;
}
private String namePrefixFilter()
{
return namePrefix + "%";
}
/**
* @param namePrefix the namePrefix to set
*/
public void setNamePrefix(String namePrefix)
{
this.namePrefix = namePrefix;
}
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException
{

View File

@@ -19,6 +19,10 @@
package org.alfresco.util.schemacomp;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.util.Iterator;
import org.alfresco.util.ApplicationContextHelper;
@@ -29,7 +33,6 @@ import org.alfresco.util.schemacomp.model.Index;
import org.alfresco.util.schemacomp.model.Schema;
import org.alfresco.util.schemacomp.model.Sequence;
import org.alfresco.util.schemacomp.model.Table;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
@@ -55,6 +58,7 @@ public class ExportDbTest
@Test
public void exportDb() throws Exception
{
exporter.setNamePrefix("alf_");
exporter.execute();
Schema schema = exporter.getSchema();
@@ -80,6 +84,7 @@ public class ExportDbTest
}
}
checkResultsFiltered(schema, "alf_");
checkAppliedPatchTable(appliedPatchTable);
checkQNameTable(qNameTable);
// TODO: what to do about sequences? They can't easily be retrieved with JDBC's DatabaseMetaData
@@ -87,6 +92,25 @@ public class ExportDbTest
}
/**
* Check that all top level database objects are prefixed as expected
* (no other objects should have been retrieved)
*
* @param schema
* @param prefix
*/
private void checkResultsFiltered(Schema schema, String prefix)
{
for (DbObject dbo : schema)
{
if (!dbo.getName().startsWith(prefix))
{
fail("Database object's name does not start with '" + prefix + "': " + dbo);
}
}
}
/**
* @param qNameTable
*/

View File

@@ -31,6 +31,7 @@ import org.junit.runners.Suite;
{
DbObjectXMLTransformerTest.class,
DbPropertyTest.class,
DbToXMLTest.class,
DefaultComparisonUtilsTest.class,
ExportDbTest.class,
SchemaComparatorTest.class,

View File

@@ -56,6 +56,10 @@ public abstract class AbstractDbObject implements DbObject
*/
public String getName()
{
if (this.name == null)
{
return "";
}
return this.name;
}