use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class MetaDataTableImpl method createIfNotExists.
/**
* Creates the metatable if it doesn't exist, upgrades it if it does.
*/
private void createIfNotExists() {
int retries = 0;
while (!table.exists()) {
if (retries == 0) {
LOG.info("Creating Metadata table: " + table);
}
try {
String resourceName = "org/flywaydb/core/internal/dbsupport/" + dbSupport.getDbName() + "/createMetaDataTable.sql";
String source = new ClassPathResource(resourceName, getClass().getClassLoader()).loadAsString("UTF-8");
Map<String, String> placeholders = new HashMap<String, String>();
placeholders.put("schema", table.getSchema().getName());
placeholders.put("table", table.getName());
String sourceNoPlaceholders = new PlaceholderReplacer(placeholders, "${", "}").replacePlaceholders(source);
final SqlScript sqlScript = new SqlScript(sourceNoPlaceholders, dbSupport);
sqlScript.execute(jdbcTemplate);
LOG.debug("Metadata table " + table + " created.");
} catch (FlywayException e) {
if (++retries >= 10) {
throw e;
}
try {
LOG.debug("Metadata table creation failed. Retrying in 1 sec ...");
Thread.sleep(1000);
} catch (InterruptedException e1) {
// Ignore
}
}
}
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class CompositeMigrationResolver method checkForIncompatibilities.
/**
* Checks for incompatible migrations.
*
* @param migrations The migrations to check.
* @throws FlywayException when two different migration with the same version number are found.
*/
/* private -> for testing */
static void checkForIncompatibilities(List<ResolvedMigration> migrations) {
// check for more than one migration with same version
for (int i = 0; i < migrations.size() - 1; i++) {
ResolvedMigration current = migrations.get(i);
ResolvedMigration next = migrations.get(i + 1);
if (new ResolvedMigrationComparator().compare(current, next) == 0) {
if (current.getVersion() != null) {
throw new FlywayException(String.format("Found more than one migration with version %s\nOffenders:\n-> %s (%s)\n-> %s (%s)", current.getVersion(), current.getPhysicalLocation(), current.getType(), next.getPhysicalLocation(), next.getType()));
}
throw new FlywayException(String.format("Found more than one repeatable migration with description %s\nOffenders:\n-> %s (%s)\n-> %s (%s)", current.getDescription(), current.getPhysicalLocation(), current.getType(), next.getPhysicalLocation(), next.getType()));
}
}
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class PlaceholderReplacer method checkForUnmatchedPlaceholderExpression.
/**
* Check for unmatched placeholder expressions in the input string and throw
* a FlywayException if they do not have corresponding values.
*
* @param input The input string.
* @throws FlywayException An exception listing the unmatched expressions.
*/
private void checkForUnmatchedPlaceholderExpression(String input) {
String regex = Pattern.quote(placeholderPrefix) + "(.+?)" + Pattern.quote(placeholderSuffix);
Matcher matcher = Pattern.compile(regex).matcher(input);
Set<String> unmatchedPlaceHolderExpressions = new TreeSet<String>();
while (matcher.find()) {
unmatchedPlaceHolderExpressions.add(matcher.group());
}
if (!unmatchedPlaceHolderExpressions.isEmpty()) {
throw new FlywayException("No value provided for placeholder expressions: " + StringUtils.collectionToCommaDelimitedString(unmatchedPlaceHolderExpressions) + ". Check your configuration!");
}
}
use of org.flywaydb.core.api.FlywayException in project flyway by flyway.
the class ClassPathResource method loadAsString.
public String loadAsString(String encoding) {
try {
InputStream inputStream = classLoader.getResourceAsStream(location);
if (inputStream == null) {
throw new FlywayException("Unable to obtain inputstream for resource: " + location);
}
Reader reader = new InputStreamReader(inputStream, Charset.forName(encoding));
return FileCopyUtils.copyToString(reader);
} catch (IOException e) {
throw new FlywayException("Unable to load resource: " + location + " (encoding: " + encoding + ")", e);
}
}
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()]);
}
Aggregations