use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class SpringJdbcMigrationResolver method extractMigrationInfo.
/**
* Extracts the migration info from this migration.
*
* @param springJdbcMigration The migration to analyse.
* @return The migration info.
*/
/* private -> testing */
ResolvedMigrationImpl extractMigrationInfo(SpringJdbcMigration springJdbcMigration) {
Integer checksum = null;
if (springJdbcMigration instanceof MigrationChecksumProvider) {
MigrationChecksumProvider checksumProvider = (MigrationChecksumProvider) springJdbcMigration;
checksum = checksumProvider.getChecksum();
}
MigrationVersion version;
String description;
if (springJdbcMigration instanceof MigrationInfoProvider) {
MigrationInfoProvider infoProvider = (MigrationInfoProvider) springJdbcMigration;
version = infoProvider.getVersion();
description = infoProvider.getDescription();
if (!StringUtils.hasText(description)) {
throw new FlywayException("Missing description for migration " + version);
}
} else {
String shortName = ClassUtils.getShortName(springJdbcMigration.getClass());
String prefix;
if (shortName.startsWith("V") || shortName.startsWith("R")) {
prefix = shortName.substring(0, 1);
} else {
throw new FlywayException("Invalid Jdbc migration class name: " + springJdbcMigration.getClass().getName() + " => ensure it starts with V or R," + " or implement org.flywaydb.core.api.migration.MigrationInfoProvider for non-default naming");
}
Pair<MigrationVersion, String> info = MigrationInfoHelper.extractVersionAndDescription(shortName, prefix, "__", "");
version = info.getLeft();
description = info.getRight();
}
ResolvedMigrationImpl resolvedMigration = new ResolvedMigrationImpl();
resolvedMigration.setVersion(version);
resolvedMigration.setDescription(description);
resolvedMigration.setScript(springJdbcMigration.getClass().getName());
resolvedMigration.setChecksum(checksum);
resolvedMigration.setType(MigrationType.SPRING_JDBC);
return resolvedMigration;
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class SpringJdbcMigrationResolver method resolveMigrations.
@Override
public Collection<ResolvedMigration> resolveMigrations() {
List<ResolvedMigration> migrations = new ArrayList<ResolvedMigration>();
if (!location.isClassPath()) {
return migrations;
}
try {
Class<?>[] classes = scanner.scanForClasses(location, SpringJdbcMigration.class);
for (Class<?> clazz : classes) {
SpringJdbcMigration springJdbcMigration = ClassUtils.instantiate(clazz.getName(), scanner.getClassLoader());
ConfigurationInjectionUtils.injectFlywayConfiguration(springJdbcMigration, configuration);
ResolvedMigrationImpl migrationInfo = extractMigrationInfo(springJdbcMigration);
migrationInfo.setPhysicalLocation(ClassUtils.getLocationOnDisk(clazz));
migrationInfo.setExecutor(new SpringJdbcMigrationExecutor(springJdbcMigration));
migrations.add(migrationInfo);
}
} catch (Exception e) {
throw new FlywayException("Unable to resolve Spring Jdbc Java migrations in location: " + location, e);
}
Collections.sort(migrations, new ResolvedMigrationComparator());
return migrations;
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class SqlMigrationResolver method calculateChecksum.
/**
* Calculates the checksum of this string.
*
* @param str The string to calculate the checksum for.
* @return The crc-32 checksum of the bytes.
*/
/* private -> for testing */
static int calculateChecksum(Resource resource, String str) {
final CRC32 crc32 = new CRC32();
BufferedReader bufferedReader = new BufferedReader(new StringReader(str));
try {
String line;
while ((line = bufferedReader.readLine()) != null) {
crc32.update(line.getBytes("UTF-8"));
}
} catch (IOException e) {
String message = "Unable to calculate checksum";
if (resource != null) {
message += " for " + resource.getLocation() + " (" + resource.getLocationOnDisk() + ")";
}
throw new FlywayException(message, e);
}
return (int) crc32.getValue();
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class ClassUtils method addJarOrDirectoryToClasspath.
/**
* Adds a jar or a directory with this name to the classpath.
*
* @param name The name of the jar or directory to add.
* @throws IOException when the jar or directory could not be found.
*/
public static void addJarOrDirectoryToClasspath(String name) throws IOException {
LOG.debug("Adding location to classpath: " + name);
try {
URL url = new File(name).toURI().toURL();
URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(sysloader, url);
} catch (Exception e) {
throw new FlywayException("Unable to load " + name, e);
}
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class JBossVFSv2UrlResolver method toStandardJavaUrl.
public URL toStandardJavaUrl(URL url) throws IOException {
try {
Class<?> vfsClass = Class.forName("org.jboss.virtual.VFS");
Class<?> vfsUtilsClass = Class.forName("org.jboss.virtual.VFSUtils");
Class<?> virtualFileClass = Class.forName("org.jboss.virtual.VirtualFile");
Method getRootMethod = vfsClass.getMethod("getRoot", URL.class);
Method getRealURLMethod = vfsUtilsClass.getMethod("getRealURL", virtualFileClass);
Object root = getRootMethod.invoke(null, url);
return (URL) getRealURLMethod.invoke(null, root);
} catch (Exception e) {
throw new FlywayException("JBoss VFS v2 call failed", e);
}
}
Aggregations