mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
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:
100
source/java/org/alfresco/util/schemacomp/DbToXML.java
Normal file
100
source/java/org/alfresco/util/schemacomp/DbToXML.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
45
source/java/org/alfresco/util/schemacomp/DbToXMLTest.java
Normal file
45
source/java/org/alfresco/util/schemacomp/DbToXMLTest.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@@ -57,6 +57,8 @@ public class ExportDb
|
|||||||
private Schema schema;
|
private Schema schema;
|
||||||
|
|
||||||
private Dialect dialect;
|
private Dialect dialect;
|
||||||
|
|
||||||
|
private String namePrefix = "%";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -114,20 +116,34 @@ public class ExportDb
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void execute() throws Exception
|
public void execute()
|
||||||
{
|
{
|
||||||
PropertyCheck.mandatory(this, "dataSource", dataSource);
|
PropertyCheck.mandatory(this, "dataSource", dataSource);
|
||||||
// Get a Connection
|
|
||||||
Connection connection = dataSource.getConnection();
|
|
||||||
|
|
||||||
|
Connection connection = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
connection = dataSource.getConnection();
|
||||||
connection.setAutoCommit(false);
|
connection.setAutoCommit(false);
|
||||||
execute(connection);
|
execute(connection);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
throw new RuntimeException("Unable to execute export.", e);
|
||||||
|
}
|
||||||
finally
|
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);
|
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"
|
"TABLE", "VIEW"
|
||||||
});
|
});
|
||||||
@@ -282,6 +298,7 @@ public class ExportDb
|
|||||||
tables.close();
|
tables.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a boolean string as used in the database, to a boolean value.
|
* Convert a boolean string as used in the database, to a boolean value.
|
||||||
*
|
*
|
||||||
@@ -356,9 +373,31 @@ public class ExportDb
|
|||||||
{
|
{
|
||||||
return this.schema;
|
return this.schema;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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
|
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException
|
||||||
{
|
{
|
||||||
ExportDb exportDb = null;
|
ExportDb exportDb = null;
|
||||||
|
@@ -19,6 +19,10 @@
|
|||||||
package org.alfresco.util.schemacomp;
|
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 java.util.Iterator;
|
||||||
|
|
||||||
import org.alfresco.util.ApplicationContextHelper;
|
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.Schema;
|
||||||
import org.alfresco.util.schemacomp.model.Sequence;
|
import org.alfresco.util.schemacomp.model.Sequence;
|
||||||
import org.alfresco.util.schemacomp.model.Table;
|
import org.alfresco.util.schemacomp.model.Table;
|
||||||
import static org.junit.Assert.*;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
@@ -55,6 +58,7 @@ public class ExportDbTest
|
|||||||
@Test
|
@Test
|
||||||
public void exportDb() throws Exception
|
public void exportDb() throws Exception
|
||||||
{
|
{
|
||||||
|
exporter.setNamePrefix("alf_");
|
||||||
exporter.execute();
|
exporter.execute();
|
||||||
|
|
||||||
Schema schema = exporter.getSchema();
|
Schema schema = exporter.getSchema();
|
||||||
@@ -80,11 +84,31 @@ public class ExportDbTest
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkResultsFiltered(schema, "alf_");
|
||||||
checkAppliedPatchTable(appliedPatchTable);
|
checkAppliedPatchTable(appliedPatchTable);
|
||||||
checkQNameTable(qNameTable);
|
checkQNameTable(qNameTable);
|
||||||
// TODO: what to do about sequences? They can't easily be retrieved with JDBC's DatabaseMetaData
|
// TODO: what to do about sequences? They can't easily be retrieved with JDBC's DatabaseMetaData
|
||||||
//checkAuthoritySequence(authoritySeq);
|
//checkAuthoritySequence(authoritySeq);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -31,6 +31,7 @@ import org.junit.runners.Suite;
|
|||||||
{
|
{
|
||||||
DbObjectXMLTransformerTest.class,
|
DbObjectXMLTransformerTest.class,
|
||||||
DbPropertyTest.class,
|
DbPropertyTest.class,
|
||||||
|
DbToXMLTest.class,
|
||||||
DefaultComparisonUtilsTest.class,
|
DefaultComparisonUtilsTest.class,
|
||||||
ExportDbTest.class,
|
ExportDbTest.class,
|
||||||
SchemaComparatorTest.class,
|
SchemaComparatorTest.class,
|
||||||
|
@@ -56,6 +56,10 @@ public abstract class AbstractDbObject implements DbObject
|
|||||||
*/
|
*/
|
||||||
public String getName()
|
public String getName()
|
||||||
{
|
{
|
||||||
|
if (this.name == null)
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user