Search in sources :

Example 26 with FlywayException

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
            }
        }
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) HashMap(java.util.HashMap) PlaceholderReplacer(org.flywaydb.core.internal.util.PlaceholderReplacer) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)

Example 27 with FlywayException

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()));
        }
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration)

Example 28 with FlywayException

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!");
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) Matcher(java.util.regex.Matcher) TreeSet(java.util.TreeSet)

Example 29 with FlywayException

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);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException)

Example 30 with FlywayException

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()]);
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) ArrayList(java.util.ArrayList) FlywayException(org.flywaydb.core.api.FlywayException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Aggregations

FlywayException (org.flywaydb.core.api.FlywayException)42 IOException (java.io.IOException)13 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)6 MigrationVersion (org.flywaydb.core.api.MigrationVersion)6 Test (org.junit.Test)6 File (java.io.File)5 InputStreamReader (java.io.InputStreamReader)5 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)5 FileInputStream (java.io.FileInputStream)4 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)4 StringReader (java.io.StringReader)3 Method (java.lang.reflect.Method)3 URL (java.net.URL)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 HashMap (java.util.HashMap)3 Properties (java.util.Properties)3 FlywayCallback (org.flywaydb.core.api.callback.FlywayCallback)3 DriverDataSource (org.flywaydb.core.internal.util.jdbc.DriverDataSource)3 TransactionTemplate (org.flywaydb.core.internal.util.jdbc.TransactionTemplate)3