DerbyDbValidator { public DdlType checkDatabaseState() { boolean databaseInState; String currentVersion; DdlType ddlType; LOGGER.info("Verifying datamodel version. Minimum version: " + requiredDbVersion); Connection connection = null; try { dataSource.setCreateDatabase("create"); connection = dataSource.getConnection(); connection.setAutoCommit(false); currentVersion = getCurrentVersion(connection); databaseInState = (currentVersion != null) && currentVersion.equalsIgnoreCase(requiredDbVersion); if (databaseInState) { ddlType = DdlType.NONE; LOGGER.info("Datamodel is the required version."); } else { LOGGER.info("Datamodel of version " + currentVersion + " found. Upgrading to " + requiredDbVersion); ddlType = DdlType.ALTER_TABLE; } } catch (SQLException e) { ddlType = DdlType.CREATE_TABLE; LOGGER.info("Could not determine datamodel's version, recreating.."); } finally { dataSource.setCreateDatabase(""); try { if (connection != null) { connection.close(); } } catch (SQLException e) { } } if (ddlType != DdlType.NONE) { try { createOrAlterDatamodel(dataSource, ddlType); } catch (Exception e) { LOGGER.error("Failed to create or upgrade datamodel", e); } } return ddlType; } DerbyDbValidator(String requiredDbVersion, DataSource dataSource); DdlType checkDatabaseState();  }
@Test public void shouldAlter() throws IOException { EmbeddedConnectionPoolDataSource dataSource = new EmbeddedConnectionPoolDataSource(); dataSource.setDatabaseName("memory:db;create=true"); DerbyDbValidator prevalidator = new DerbyDbValidator("1.4", dataSource); prevalidator.checkDatabaseState(); DerbyDbValidator validator = new DerbyDbValidator("99", dataSource); DerbyDbValidator.DdlType state = validator.checkDatabaseState(); assertEquals(DerbyDbValidator.DdlType.ALTER_TABLE, state); }