Search in sources :

Example 6 with FlywayException

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;
}
Also used : MigrationChecksumProvider(org.flywaydb.core.api.migration.MigrationChecksumProvider) MigrationInfoProvider(org.flywaydb.core.api.migration.MigrationInfoProvider) FlywayException(org.flywaydb.core.api.FlywayException) MigrationVersion(org.flywaydb.core.api.MigrationVersion) ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)

Example 7 with FlywayException

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;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) ResolvedMigrationComparator(org.flywaydb.core.internal.resolver.ResolvedMigrationComparator) ArrayList(java.util.ArrayList) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) SpringJdbcMigration(org.flywaydb.core.api.migration.spring.SpringJdbcMigration) ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl) FlywayException(org.flywaydb.core.api.FlywayException)

Example 8 with FlywayException

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();
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) CRC32(java.util.zip.CRC32) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 9 with FlywayException

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);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) URLClassLoader(java.net.URLClassLoader) Method(java.lang.reflect.Method) File(java.io.File) URL(java.net.URL) FlywayException(org.flywaydb.core.api.FlywayException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 10 with FlywayException

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);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) Method(java.lang.reflect.Method) URL(java.net.URL) FlywayException(org.flywaydb.core.api.FlywayException) 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