Search in sources :

Example 31 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)

Example 32 with FlywayException

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());
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 33 with FlywayException

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"));
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) Test(org.junit.Test)

Example 34 with FlywayException

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();
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) Test(org.junit.Test)

Example 35 with FlywayException

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();
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback) SQLException(java.sql.SQLException) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration) TransactionTemplate(org.flywaydb.core.internal.util.jdbc.TransactionTemplate)

Aggregations

FlywayException (org.flywaydb.core.api.FlywayException)43 IOException (java.io.IOException)13 SQLException (java.sql.SQLException)11 MigrationVersion (org.flywaydb.core.api.MigrationVersion)7 ArrayList (java.util.ArrayList)6 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)6 Test (org.junit.Test)6 File (java.io.File)5 InputStreamReader (java.io.InputStreamReader)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 MigrationChecksumProvider (org.flywaydb.core.api.migration.MigrationChecksumProvider)3 MigrationInfoProvider (org.flywaydb.core.api.migration.MigrationInfoProvider)3