protected void throwAssertions() { if (!myFoundErrors.isEmpty()) { StringBuilder buf = new StringBuilder(); for (int i = 0; i < myFoundErrors.size(); i++) { String s = (String) myFoundErrors.get(i); buf.append(i + 1).append("> ").append(s).append("\n\r"); } throw new JdbcException(buf.toString()); } }
public void assertCatalogsComplete( CatalogDescription schemaConfig, CatalogDescription databaseConfig) { myFoundErrors.clear(); String[] tables = schemaConfig.getTableNames(); for (String theTable : tables) { TableDescription xmlTableDescription = schemaConfig.getTable(theTable); TableDescription databaseTableDescription = databaseConfig.getTable(xmlTableDescription.getTableName().toUpperCase()); if (databaseTableDescription != null) { log("Checking " + databaseTableDescription.getTableName() + "..."); compareSingleIndexDescription( xmlTableDescription.getPrimaryKey(), databaseTableDescription.getPrimaryKey()); compareColumnDescription(xmlTableDescription, databaseTableDescription); compareIndexDescription(xmlTableDescription, databaseTableDescription); compareForeignKeyDescription(xmlTableDescription, databaseTableDescription); checkUnknownColumns(databaseTableDescription, xmlTableDescription.getColumnNames()); } else { assertTrue( "Table: " + xmlTableDescription.getTableName() + "... not found in databaseCatalog!", false); } } // TODO RSt - views checking not yet implemented // todo [RSt] sequences not yet implemented -> requires DDLScriptSqlMetaFactory // todo [RSt] function based indices not yet implemented -> requires DDLScriptSqlMetaFactory // todo [RSt] missing indexes/foreignkeys in schemaConfig not detected -> requires // DDLScriptSqlMetaFactory throwAssertions(); }
/** * API - check the database for compatibility with the given XML-DDL configuration. Additional add * all DDL in the scripts to the schema. * * @param options - list with options per script (may be empty) - corresponding to index in * 'scripts' * @param scripts - scripts for schema (Soll-Zustand) Der Ist-Zustand steht in der Datenbank und * wird mit dem Soll-Zustand verglichen. * @throws Exception */ public void checkDatabaseSchema(final List<Options> options, URL[] scripts) throws Exception { CatalogDescription expectedCatalog; DDLScriptSqlMetaFactory factory = getDDLScriptSqlMetaFactory(); int idx = 0; for (URL script : scripts) { Options option = options != null && options.size() > idx ? options.get(idx++) : null; factory.fillCatalog(script, option == null ? null : option.format); } expectedCatalog = factory.getCatalog(); if (expectedCatalog == null) { assertTrue("No expected Catalog: neither schemaconfig nor scripts given!", false); throwAssertions(); } else { CatalogDescription databaseCatalog = readDatabaseCatalog(expectedCatalog.getTableNames()); print("Checking Database Schema " + databaseCatalog.getSchemaName() + ".."); assertCatalogsComplete(expectedCatalog, databaseCatalog); print("Schema : Check OK"); } }
protected void assertTrue(String s, boolean b) { if (!b) { myFoundErrors.add(s); } }
protected void compareForeignKeyDescription( TableDescription xmlTableDescription, TableDescription databaseTableDescription) { // todo onDeleteRule not yet hard checked! String tableName = xmlTableDescription.getTableName(); List<ForeignKeyDescription> unCheckedDatabaseFKs = new ArrayList<ForeignKeyDescription>(); if (databaseTableDescription.getForeignKeys() != null) { unCheckedDatabaseFKs.addAll(databaseTableDescription.getForeignKeys()); } List<ForeignKeyDescription> unnamedFKs = new ArrayList<ForeignKeyDescription>(xmlTableDescription.getForeignKeySize()); for (int i = 0; i < xmlTableDescription.getForeignKeySize(); i++) { ForeignKeyDescription xmlForeignKeyDescription = xmlTableDescription.getForeignKey(i); if (xmlForeignKeyDescription.getConstraintName() == null || xmlForeignKeyDescription.getConstraintName().length() == 0) { unnamedFKs.add(xmlForeignKeyDescription); // check later... continue; } ForeignKeyDescription databaseForeignKeyDescription = databaseTableDescription.getForeignKey(xmlForeignKeyDescription.getConstraintName()); unCheckedDatabaseFKs.remove(databaseForeignKeyDescription); if (databaseForeignKeyDescription != null) { compareForeignKey(tableName, xmlForeignKeyDescription, databaseForeignKeyDescription); } else assertTrue( "Table: " + tableName + "... ConstraintName not found! Expected ConstraintName: " + xmlForeignKeyDescription.getConstraintName(), false); } for (ForeignKeyDescription unnamedFK : unnamedFKs) { ForeignKeyDescription dbFk = databaseTableDescription.findForeignKeyLike(unnamedFK); if (dbFk != null) { unCheckedDatabaseFKs.remove(dbFk); if (!StringUtils.equalsIgnoreCase(dbFk.getOnDeleteRule(), unnamedFK.getOnDeleteRule())) { log( "Table: " + tableName + "... Different onDelete rules (found " + dbFk.getOnDeleteRule() + ", expected " + unnamedFK.getOnDeleteRule() + ") between foreign key on " + dbFk.getColumns()); } } else { assertTrue( "Table: " + tableName + "... Missing unnamed foreign key on " + unnamedFK.getColumns() + " referencing " + unnamedFK.getRefTableName() + "." + unnamedFK.getRefColumns(), false); } } for (ForeignKeyDescription uncheckedFK : unCheckedDatabaseFKs) { assertTrue( "Table: " + tableName + " contains unexpected foreign key named '" + uncheckedFK + " on columns " + uncheckedFK.getColumns() + "' referencing table '" + uncheckedFK.getRefTableName() + "'." + uncheckedFK.getRefColumns(), false); } }