Schema comparator: added optional <schema> level attribute to allow ignoring of table column ordering during schema validation.

<schema ... tablecolumnorder="false">

The above snippet will disable column order checking for tables.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@46517 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward 2013-02-12 15:03:01 +00:00
parent 89e6592f85
commit 1270a56c47
13 changed files with 210 additions and 21 deletions

View File

@ -24,6 +24,7 @@
</xs:sequence>
<xs:attribute name="dbprefix" type="xs:token"/>
<xs:attribute name="version" type="xs:int"/>
<xs:attribute name="tablecolumnorder" type="xs:boolean" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>

View File

@ -142,6 +142,7 @@ public class DbObjectXMLTransformer
Schema schema = (Schema) dbObject;
attribs.addAttribute("", "", XML.ATTR_DB_PREFIX, "CDATA", schema.getDbPrefix());
attribs.addAttribute("", "", XML.ATTR_VERSION, "CDATA", Integer.toString(schema.getVersion()));
attribs.addAttribute("", "", XML.ATTR_TABLE_COLUMN_ORDER, "CDATA", Boolean.toString(schema.isCheckTableColumnOrder()));
}
else if (dbObject instanceof Index)
{

View File

@ -182,7 +182,7 @@ public class DbObjectXMLTransformerTest
}
@Test
public void transformSchema() throws IOException
public void transformSchemaNoColumnOrderCheck() throws IOException
{
Collection<Column> columns = columns("one VARCHAR2(100)", "two NUMBER(10)");
PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id"), Arrays.asList(1));
@ -192,7 +192,7 @@ public class DbObjectXMLTransformerTest
Table tableOne = new Table(null, "table_one", columns, pk, fks, indexes);
Table tableTwo = new Table(null, "table_two", columns, pk, fks, indexes);
Schema schema = new Schema("my_schema", "alf_", 132);
Schema schema = new Schema("my_schema", "alf_", 132, false);
schema.add(tableOne);
schema.add(tableTwo);
schema.add(new Sequence(null, "sequence_one"));
@ -209,7 +209,46 @@ public class DbObjectXMLTransformerTest
"xmlns=\"http://www.alfresco.org/repo/db-schema\" " +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xsi:schemaLocation=\"http://www.alfresco.org/repo/db-schema db-schema.xsd\" " +
"name=\"my_schema\" dbprefix=\"alf_\" version=\"132\">", reader.readLine());
"name=\"my_schema\" dbprefix=\"alf_\" version=\"132\" tablecolumnorder=\"false\">", reader.readLine());
assertEquals(" <objects>", reader.readLine());
skipUntilEnd(" {table}", reader);
skipUntilEnd(" {table}", reader);
skipUntilEnd(" {sequence}", reader, true);
skipUntilEnd(" {sequence}", reader, true);
skipUntilEnd(" {sequence}", reader, true);
assertEquals(" </objects>", reader.readLine());
assertEquals("</schema>", reader.readLine());
}
@Test
public void transformSchemaWithColumnOrderCheck() throws IOException
{
Collection<Column> columns = columns("one VARCHAR2(100)", "two NUMBER(10)");
PrimaryKey pk = new PrimaryKey(null, "pk_for_my_table", Arrays.asList("id"), Arrays.asList(1));
Collection<ForeignKey> fks = fkeys(fk("fk_one", "lc", "tt", "tc"), fk("fk_two", "lc", "tt", "tc"));
Collection<Index> indexes = indexes("index_one col1 col2", "index_two col3 col4");
Table tableOne = new Table(null, "table_one", columns, pk, fks, indexes);
Table tableTwo = new Table(null, "table_two", columns, pk, fks, indexes);
Schema schema = new Schema("my_schema", "alf_", 132, true);
schema.add(tableOne);
schema.add(tableTwo);
schema.add(new Sequence(null, "sequence_one"));
schema.add(new Sequence(null, "sequence_two"));
schema.add(new Sequence(null, "sequence_three"));
schema.setValidators(new ArrayList<DbValidator>());
transformer.output(schema);
BufferedReader reader = new BufferedReader(new StringReader(writer.toString()));
dumpOutput();
assertHasPreamble(reader);
assertEquals("<schema " +
"xmlns=\"http://www.alfresco.org/repo/db-schema\" " +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xsi:schemaLocation=\"http://www.alfresco.org/repo/db-schema db-schema.xsd\" " +
"name=\"my_schema\" dbprefix=\"alf_\" version=\"132\" tablecolumnorder=\"true\">", reader.readLine());
assertEquals(" <objects>", reader.readLine());
skipUntilEnd(" {table}", reader);
skipUntilEnd(" {table}", reader);

View File

@ -201,7 +201,7 @@ public class ExportDb
String schemaName = getSchemaName(dbmd);
schema = new Schema(schemaName, namePrefix, schemaVersion);
schema = new Schema(schemaName, namePrefix, schemaVersion, true);
String[] prefixFilters = namePrefixFilters(dbmd);
for (String filter : prefixFilters)

View File

@ -76,7 +76,7 @@ public class SchemaCompTestingUtils
return new Table(null, name, columns, primaryKey, foreignKeys, indexes);
}
public static Collection<Column> columns(String... colDefs)
public static Collection<Column> columns(boolean compareColOrder, String... colDefs)
{
assertTrue("Tables must have columns", colDefs.length > 0);
Column[] columns = new Column[colDefs.length];
@ -86,10 +86,16 @@ public class SchemaCompTestingUtils
String[] parts = colDefs[i].split(" ");
columns[i] = new Column(null, parts[0], parts[1], false);
columns[i].setOrder(i+1);
columns[i].setCompareOrder(compareColOrder);
}
return Arrays.asList(columns);
}
public static Collection<Column> columns(String... colDefs)
{
return columns(true, colDefs);
}
public static PrimaryKey pk(String name, String... columnNames)
{
assertTrue("No columns specified", columnNames.length > 0);

View File

@ -57,8 +57,8 @@ public class SchemaComparatorTest
@Before
public void setup()
{
reference = new Schema("schema", "alf_", 590);
target = new Schema("schema", "alf_", 590);
reference = new Schema("schema", "alf_", 590, true);
target = new Schema("schema", "alf_", 590, true);
dialect = new MySQL5InnoDBDialect();
}
@ -138,8 +138,8 @@ public class SchemaComparatorTest
@Test
public void pkOrderingComparedCorrectly()
{
reference = new Schema("schema", "alf_", 590);
target = new Schema("schema", "alf_", 590);
reference = new Schema("schema", "alf_", 590, true);
target = new Schema("schema", "alf_", 590, true);
// Reference schema's database objects.
reference.add(new Table(
@ -196,8 +196,8 @@ public class SchemaComparatorTest
@Test
public void indexColumnOrderingComparedCorrectly()
{
reference = new Schema("schema", "alf_", 590);
target = new Schema("schema", "alf_", 590);
reference = new Schema("schema", "alf_", 590, true);
target = new Schema("schema", "alf_", 590, true);
// Reference schema's database objects.
reference.add(new Table(
@ -249,4 +249,100 @@ public class SchemaComparatorTest
assertFalse("There should be no more differences", it.hasNext());
}
@Test
public void columnOrderingComparedCorrectlyWhenEnabled()
{
reference = new Schema("schema", "alf_", 590, true);
target = new Schema("schema", "alf_", 590, true);
// Reference schema's database objects.
reference.add(new Table(
reference,
"table_name",
columns("id NUMBER(10)", "nodeRef VARCHAR2(200)", "name VARCHAR2(150)"),
new PrimaryKey(null, "my_pk_name", Arrays.asList("id", "nodeRef"), Arrays.asList(1, 2)),
fkeys(),
indexes()));
// Target schema's database objects - note different column order
target.add(new Table(
target,
"table_name",
columns("id NUMBER(10)", "name VARCHAR2(150)", "nodeRef VARCHAR2(200)"),
new PrimaryKey(null, "my_pk_name", Arrays.asList("id", "nodeRef"), Arrays.asList(1, 2)),
fkeys(),
indexes()));
comparator = new SchemaComparator(reference, target, dialect);
comparator.validateAndCompare();
// See stdout for diagnostics dump...
dumpDiffs(comparator.getComparisonResults(), false);
dumpValidation(comparator.getComparisonResults());
Results results = comparator.getComparisonResults();
Iterator<Result> it = results.iterator();
Difference diff = (Difference) it.next();
assertEquals(Where.IN_BOTH_BUT_DIFFERENCE, diff.getWhere());
assertEquals("schema.table_name.nodeRef.order", diff.getLeft().getPath());
assertEquals("order", diff.getLeft().getPropertyName());
assertEquals(2, diff.getLeft().getPropertyValue());
assertEquals("schema.table_name.nodeRef.order", diff.getRight().getPath());
assertEquals("order", diff.getRight().getPropertyName());
assertEquals(3, diff.getRight().getPropertyValue());
diff = (Difference) it.next();
assertEquals(Where.IN_BOTH_BUT_DIFFERENCE, diff.getWhere());
assertEquals("schema.table_name.name.order", diff.getLeft().getPath());
assertEquals("order", diff.getLeft().getPropertyName());
assertEquals(3, diff.getLeft().getPropertyValue());
assertEquals("schema.table_name.name.order", diff.getRight().getPath());
assertEquals("order", diff.getRight().getPropertyName());
assertEquals(2, diff.getRight().getPropertyValue());
assertFalse("There should be no more differences", it.hasNext());
}
@Test
public void columnOrderingIgnoredWhenDisabled()
{
reference = new Schema("schema", "alf_", 590, false);
target = new Schema("schema", "alf_", 590, false);
// Reference schema's database objects.
reference.add(new Table(
reference,
"table_name",
columns(false, "id NUMBER(10)", "nodeRef VARCHAR2(200)", "name VARCHAR2(150)"),
new PrimaryKey(null, "my_pk_name", Arrays.asList("id", "nodeRef"), Arrays.asList(1, 2)),
fkeys(),
indexes()));
// Target schema's database objects - note different column order
target.add(new Table(
target,
"table_name",
columns(false, "id NUMBER(10)", "name VARCHAR2(150)", "nodeRef VARCHAR2(200)"),
new PrimaryKey(null, "my_pk_name", Arrays.asList("id", "nodeRef"), Arrays.asList(1, 2)),
fkeys(),
indexes()));
comparator = new SchemaComparator(reference, target, dialect);
comparator.validateAndCompare();
// See stdout for diagnostics dump...
dumpDiffs(comparator.getComparisonResults(), false);
dumpValidation(comparator.getComparisonResults());
Results results = comparator.getComparisonResults();
// There are no logical differences
assertEquals(0, results.size());
}
}

View File

@ -57,7 +57,7 @@ public class SchemaToXMLTest
Writer writer = new StringWriter();
StreamResult out = new StreamResult(writer);
Schema schema = new Schema("alfresco", "my-prefix", 501);
Schema schema = new Schema("alfresco", "my-prefix", 501, true);
schema.add(
table("node",
@ -84,7 +84,7 @@ public class SchemaToXMLTest
"xmlns=\"http://www.alfresco.org/repo/db-schema\" " +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xsi:schemaLocation=\"http://www.alfresco.org/repo/db-schema db-schema.xsd\"";
assertEquals("<schema " + xsd + " name=\"alfresco\" dbprefix=\"my-prefix\" version=\"501\">", reader.readLine());
assertEquals("<schema " + xsd + " name=\"alfresco\" dbprefix=\"my-prefix\" version=\"501\" tablecolumnorder=\"true\">", reader.readLine());
assertEquals(" <validators>", reader.readLine());
}
}

View File

@ -57,4 +57,5 @@ public abstract class XML
public static final String ATTR_CLASS = "class";
public static final String ATTR_DB_PREFIX = "dbprefix";
public static final String ATTR_VERSION = "version";
public static final String ATTR_TABLE_COLUMN_ORDER = "tablecolumnorder";
}

View File

@ -212,7 +212,10 @@ public class XMLToSchema extends DefaultHandler
String name = atts.getValue(XML.ATTR_NAME);
String dbPrefix = atts.getValue(XML.ATTR_DB_PREFIX);
int version = Integer.parseInt(atts.getValue(XML.ATTR_VERSION));
schema = new Schema(name, dbPrefix, version);
String attrTableColumnOrder = atts.getValue(XML.ATTR_TABLE_COLUMN_ORDER);
// Should column order be checked for tables?
boolean compareTableColOrder = attrTableColumnOrder != null ? Boolean.parseBoolean(attrTableColumnOrder) : true;
schema = new Schema(name, dbPrefix, version, compareTableColOrder);
stack.push(schema);
}
else if (qName.equals(XML.EL_TABLE))
@ -227,6 +230,7 @@ public class XMLToSchema extends DefaultHandler
int order = Integer.parseInt(atts.getValue(XML.ATTR_ORDER));
column.setOrder(order);
}
column.setCompareOrder(schema.isCheckTableColumnOrder());
stack.push(column);
}
else if (qName.equals(XML.EL_COLUMN_NAME))

View File

@ -33,7 +33,7 @@ public class Column extends AbstractDbObject
private boolean nullable;
private boolean autoIncrement;
private int order;
private boolean compareOrder = true;
public Column(String name)
{
@ -122,12 +122,32 @@ public class Column extends AbstractDbObject
}
/**
* @return the compareOrder
*/
public boolean isCompareOrder()
{
return this.compareOrder;
}
/**
* @param compareOrder the compareOrder to set
*/
public void setCompareOrder(boolean compareOrder)
{
this.compareOrder = compareOrder;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = super.hashCode();
result = prime * result + (this.autoIncrement ? 1231 : 1237);
result = prime * result + (this.compareOrder ? 1231 : 1237);
result = prime * result + (this.nullable ? 1231 : 1237);
result = prime * result + this.order;
result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
@ -142,6 +162,7 @@ public class Column extends AbstractDbObject
if (getClass() != obj.getClass()) return false;
Column other = (Column) obj;
if (this.autoIncrement != other.autoIncrement) return false;
if (this.compareOrder != other.compareOrder) return false;
if (this.nullable != other.nullable) return false;
if (this.order != other.order) return false;
if (this.type == null)
@ -167,8 +188,11 @@ public class Column extends AbstractDbObject
DbProperty thatAutoIncProp = new DbProperty(thatColumn, "autoIncrement");
comparisonUtils.compareSimple(thisTypeProp, thatTypeProp, ctx);
comparisonUtils.compareSimple(thisNullableProp, thatNullableProp, ctx);
comparisonUtils.compareSimple(thisOrderProp, thatOrderProp, ctx);
comparisonUtils.compareSimple(thisNullableProp, thatNullableProp, ctx);
if (compareOrder)
{
comparisonUtils.compareSimple(thisOrderProp, thatOrderProp, ctx);
}
comparisonUtils.compareSimple(thisAutoIncProp, thatAutoIncProp, ctx);
}

View File

@ -40,6 +40,7 @@ public class Schema extends AbstractDbObject implements Iterable<DbObject>
protected final List<DbObject> objects = new ArrayList<DbObject>();
protected final String dbPrefix;
protected final int version;
protected final boolean checkTableColumnOrder;
/**
* Construct a schema with the given name and no database prefix.
@ -48,7 +49,7 @@ public class Schema extends AbstractDbObject implements Iterable<DbObject>
*/
public Schema(String name)
{
this(name, "", 0);
this(name, "", 0, true);
}
/**
@ -61,12 +62,13 @@ public class Schema extends AbstractDbObject implements Iterable<DbObject>
* @param name
* @param dbPrefix
*/
public Schema(String name, String dbPrefix, int schemaVersion)
public Schema(String name, String dbPrefix, int schemaVersion, boolean checkTableColumnOrder)
{
super(null, name);
ParameterCheck.mandatory("dbPrefix", dbPrefix);
this.dbPrefix = dbPrefix;
this.version = schemaVersion;
this.checkTableColumnOrder = checkTableColumnOrder;
addDefaultValidators();
}
@ -129,6 +131,14 @@ public class Schema extends AbstractDbObject implements Iterable<DbObject>
return this.version;
}
/**
* @return the checkTableColumnOrder
*/
public boolean isCheckTableColumnOrder()
{
return this.checkTableColumnOrder;
}
@Override
public int hashCode()
{

View File

@ -107,6 +107,6 @@ public class SchemaVersionValidatorTest
private DbObject schemaWithVersion(int version)
{
return new Schema("", "", version);
return new Schema("", "", version, true);
}
}

View File

@ -2,7 +2,14 @@
<!--
Test data used by the XMLToSchemaTest junit test class.
-->
<schema name="alfresco" dbprefix="myprefix_" version="325">
<schema
xmlns="http://www.alfresco.org/repo/db-schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.alfresco.org/repo/db-schema db-schema.xsd"
name="alfresco"
dbprefix="myprefix_"
version="325"
tablecolumnorder="true">
<objects>
<table name="node">
<columns>