use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class ClassPathScanner method scanForClasses.
@Override
public Class<?>[] scanForClasses(Location location, Class<?> implementedInterface) throws Exception {
LOG.debug("Scanning for classes at '" + location + "' (Implementing: '" + implementedInterface.getName() + "')");
List<Class<?>> classes = new ArrayList<Class<?>>();
Set<String> resourceNames = findResourceNames(location, "", ".class");
for (String resourceName : resourceNames) {
String className = toClassName(resourceName);
Class<?> clazz;
try {
clazz = classLoader.loadClass(className);
if (!implementedInterface.isAssignableFrom(clazz)) {
continue;
}
if (Modifier.isAbstract(clazz.getModifiers()) || clazz.isEnum() || clazz.isAnonymousClass()) {
LOG.debug("Skipping non-instantiable class: " + className);
continue;
}
ClassUtils.instantiate(className, classLoader);
} catch (InternalError e) {
LOG.debug("Skipping invalid class: " + className);
continue;
} catch (IncompatibleClassChangeError e) {
LOG.debug("Skipping incompatibly changed class: " + className);
continue;
} catch (NoClassDefFoundError e) {
LOG.debug("Skipping non-loadable class: " + className);
continue;
} catch (Exception e) {
throw new FlywayException("Unable to instantiate class: " + className, e);
}
classes.add(clazz);
LOG.debug("Found class: " + className);
}
return classes.toArray(new Class<?>[classes.size()]);
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class FlywayMediumTest method repairFirst.
@Test
public void repairFirst() throws Exception {
DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_db_repair;DB_CLOSE_DELAY=-1", "sa", "", null, "SET AUTOCOMMIT OFF");
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);
Map<String, String> placeholders = new HashMap<String, String>();
placeholders.put("tableName", "aaa");
flyway.setPlaceholders(placeholders);
flyway.setLocations("migration/failed");
assertEquals(1, flyway.info().all().length);
try {
flyway.migrate();
} catch (FlywayException e) {
//Should happen
}
assertEquals("1", flyway.info().current().getVersion().toString());
assertEquals(MigrationState.FAILED, flyway.info().current().getState());
flyway.repair();
assertNull(flyway.info().current());
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class FlywayMediumTest method repeatableFailed.
@Test
public void repeatableFailed() {
DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_repeatable_failed;DB_CLOSE_DELAY=-1", "sa", "", null);
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);
flyway.setLocations("migration/repeatable_failed");
try {
flyway.migrate();
fail();
} catch (FlywayException e) {
assertEquals(e.getMessage(), MigrationState.FAILED, flyway.info().current().getState());
}
try {
flyway.validate();
fail();
} catch (FlywayException e) {
assertTrue(e.getMessage(), e.getMessage().contains("failed repeatable migration"));
}
flyway.setLocations("migration/repeatable");
try {
flyway.migrate();
fail();
} catch (FlywayException e) {
assertTrue(e.getMessage(), e.getMessage().contains("failed repeatable migration"));
}
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class FlywayMediumTest method cleanDisabled.
@Test(expected = FlywayException.class)
public void cleanDisabled() throws Exception {
DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_db_clean_disabled;DB_CLOSE_DELAY=-1", "sa", "", null);
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);
try {
flyway.clean();
} catch (FlywayException e) {
fail("clean should succeed when cleanDisabled is false");
}
flyway.setCleanDisabled(true);
flyway.clean();
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class DbBaseline method baseline.
/**
* Baselines the database.
*/
public void baseline() {
try {
for (final FlywayCallback callback : callbacks) {
new TransactionTemplate(connection).execute(new Callable<Object>() {
@Override
public Object call() throws SQLException {
dbSupport.changeCurrentSchemaTo(schema);
callback.beforeBaseline(connection);
return null;
}
});
}
new TransactionTemplate(connection).execute(new Callable<Object>() {
@Override
public Void call() {
dbSupport.changeCurrentSchemaTo(schema);
if (metaDataTable.hasBaselineMarker()) {
AppliedMigration baselineMarker = metaDataTable.getBaselineMarker();
if (baselineVersion.equals(baselineMarker.getVersion()) && baselineDescription.equals(baselineMarker.getDescription())) {
LOG.info("Metadata table " + metaDataTable + " already initialized with (" + baselineVersion + "," + baselineDescription + "). Skipping.");
return null;
}
throw new FlywayException("Unable to baseline metadata table " + metaDataTable + " with (" + baselineVersion + "," + baselineDescription + ") as it has already been initialized with (" + baselineMarker.getVersion() + "," + baselineMarker.getDescription() + ")");
}
if (metaDataTable.hasSchemasMarker() && baselineVersion.equals(MigrationVersion.fromVersion("0"))) {
throw new FlywayException("Unable to baseline metadata table " + metaDataTable + " with version 0 as this version was used for schema creation");
}
if (metaDataTable.hasAppliedMigrations()) {
throw new FlywayException("Unable to baseline metadata table " + metaDataTable + " as it already contains migrations");
}
metaDataTable.addBaselineMarker(baselineVersion, baselineDescription);
return null;
}
});
LOG.info("Successfully baselined schema with version: " + baselineVersion);
for (final FlywayCallback callback : callbacks) {
new TransactionTemplate(connection).execute(new Callable<Object>() {
@Override
public Object call() throws SQLException {
dbSupport.changeCurrentSchemaTo(schema);
callback.afterBaseline(connection);
return null;
}
});
}
} finally {
dbSupport.restoreCurrentSchema();
}
}
Aggregations