conn.close();
conn = null;
}
// Bring the schema up to date by merging the differences
ValidationResult vr = null;
if(!schemaValid) {
// Invalid schema
Out.error(SchemaValidator.class, "The database requires rebuilding");
// Generate schema from the mapping file
DbGenerator generator = new DbGenerator(adapter, dataMap);
generator.setShouldCreateFKConstraints(adapter.supportsFkConstraints());
generator.setShouldCreatePKSupport(!adapter.supportsGeneratedKeys());
generator.setShouldCreateTables(true);
generator.setShouldDropPKSupport(true);
generator.setShouldDropTables(true);
generator.runGenerator(dataSource);
vr = generator.getFailures();
Out.info(SchemaValidator.class, "Database rebuild complete");
} else {
// Valid schema; check if merge is needed
Out.debug(SchemaValidator.class, "The database schema is valid; checking if upgrade is needed");
MergerContext mc = new ExecutingMergerContext(dataMap, dataNode);
List<MergerToken> allMergeTokens = new DbMerger().createMergeTokens(dataNode, dataMap);
List<MergerToken> nonAutoMergeTokens = new ArrayList<MergerToken>();
for(MergerToken mt : allMergeTokens) {
if(mt instanceof DropTableToDb) {
// Why would you want to drop AUTO_PK_SUPPORT?
DropTableToDb drop = (DropTableToDb)mt;
if(drop.getEntity().getName().equals("AUTO_PK_SUPPORT"))
continue;
}
if((mt instanceof AddRelationshipToDb)
|| (mt instanceof CreateTableToDb)
|| (mt instanceof AddColumnToDb)
|| (mt instanceof SetAllowNullToDb)) {
// These non-destructive types are allowed to merge in automatically
Out.info(SchemaValidator.class, mt.toString());
mt.execute(mc);
} else {
// All other types require user approval
nonAutoMergeTokens.add(mt);
}
}
if(nonAutoMergeTokens.size() == 0) {
Out.debug(SchemaValidator.class, "No upgrade necessary");
checkRunWizard(schemaValid);
return true;
}
try {
String msg = "BNU-Bot must perform the following action(s) to upgrade your database:\n";
for(MergerToken mt : nonAutoMergeTokens)
msg += mt.toString() + "\n";
msg += "\n" +
"It is strongly recommended to backup your database before continuing!\n" +
"Okay to continue?";
if(JOptionPane.showConfirmDialog(
null,
msg,
"Database schema update required",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE)
!= JOptionPane.YES_OPTION) {
throw new RuntimeException("User rejected schema update");
}
} catch(HeadlessException e) {
// GUI is probably broken
throw new Exception("A schema merge is required, but was unable to display " +
"a window to query user if schema upgrade is acceptable.");
}
for(MergerToken mt : nonAutoMergeTokens) {
Out.info(SchemaValidator.class, mt.toString());
mt.execute(mc);
}
vr = mc.getValidationResult();
}
if(vr != null) {
for(ValidationFailure vf : vr.getFailures()) {
// Don't bother the user with these
if(vf.getSource().toString().startsWith("DROP TABLE "))
continue;
Out.error(SchemaValidator.class,
vf.getDescription().toString() + "\n" +