Search in sources :

Example 1 with ResolvedMigration

use of org.flywaydb.core.api.resolver.ResolvedMigration in project che by eclipse.

the class CustomSqlMigrationResolver method resolveSqlMigrations.

private List<ResolvedMigration> resolveSqlMigrations() throws IOException, SQLException {
    LOG.info("Searching for sql scripts in locations {}", Arrays.toString(flywayConfiguration.getLocations()));
    final Map<Location, List<Resource>> allResources = finder.findResources(flywayConfiguration);
    LOG.debug("Found scripts: {}", allResources);
    final Map<String, Map<String, SqlScript>> scriptsInDir = new HashMap<>();
    for (Location location : allResources.keySet()) {
        final List<Resource> resources = allResources.get(location);
        for (Resource resource : resources) {
            final SqlScript newScript = scriptsCreator.createScript(location, resource);
            if (!scriptsInDir.containsKey(newScript.dir)) {
                scriptsInDir.put(newScript.dir, new HashMap<>(4));
            }
            final Map<String, SqlScript> existingScripts = scriptsInDir.get(newScript.dir);
            final SqlScript existingScript = existingScripts.get(newScript.name);
            if (existingScript == null) {
                existingScripts.put(newScript.name, newScript);
            } else if (Objects.equals(existingScript.vendor, newScript.vendor)) {
                throw new FlywayException(format("More than one script with name '%s' is registered for " + "database vendor '%s', script '%s' conflicts with '%s'", newScript.name, existingScript.vendor, newScript, existingScript));
            } else if (vendorName.equals(newScript.vendor)) {
                existingScripts.put(newScript.name, newScript);
            }
        }
    }
    final Map<MigrationVersion, ResolvedMigration> migrations = new HashMap<>();
    for (SqlScript script : scriptsInDir.values().stream().flatMap(scripts -> scripts.values().stream()).collect(toList())) {
        final ResolvedMigrationImpl migration = new ResolvedMigrationImpl();
        migration.setVersion(versionResolver.resolve(script, flywayConfiguration));
        migration.setScript(script.resource.getLocation());
        migration.setPhysicalLocation(script.resource.getLocationOnDisk());
        migration.setType(MigrationType.SQL);
        migration.setDescription(script.name);
        migration.setChecksum(ByteSource.wrap(script.resource.loadAsBytes()).hash(Hashing.crc32()).asInt());
        migration.setExecutor(new SqlMigrationExecutor(dbSupport, script.resource, placeholderReplacer, flywayConfiguration.getEncoding()));
        if (migrations.put(migration.getVersion(), migration) != null) {
            throw new FlywayException("Two migrations with the same version detected");
        }
    }
    return new ArrayList<>(migrations.values());
}
Also used : Arrays(java.util.Arrays) FlywayException(org.flywaydb.core.api.FlywayException) LoggerFactory(org.slf4j.LoggerFactory) Hashing(com.google.common.hash.Hashing) Resource(org.flywaydb.core.internal.util.scanner.Resource) HashMap(java.util.HashMap) ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl) BaseMigrationResolver(org.flywaydb.core.api.resolver.BaseMigrationResolver) ArrayList(java.util.ArrayList) Location(org.flywaydb.core.internal.util.Location) MigrationVersion(org.flywaydb.core.api.MigrationVersion) SQLException(java.sql.SQLException) Map(java.util.Map) ByteSource(com.google.common.io.ByteSource) Logger(org.slf4j.Logger) SqlMigrationExecutor(org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor) PlaceholderReplacer(org.flywaydb.core.internal.util.PlaceholderReplacer) Collection(java.util.Collection) IOException(java.io.IOException) MigrationType(org.flywaydb.core.api.MigrationType) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) String.format(java.lang.String.format) Objects(java.util.Objects) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) DbSupport(org.flywaydb.core.internal.dbsupport.DbSupport) FlywayException(org.flywaydb.core.api.FlywayException) HashMap(java.util.HashMap) Resource(org.flywaydb.core.internal.util.scanner.Resource) ArrayList(java.util.ArrayList) ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl) SqlMigrationExecutor(org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor) MigrationVersion(org.flywaydb.core.api.MigrationVersion) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) HashMap(java.util.HashMap) Map(java.util.Map) Location(org.flywaydb.core.internal.util.Location)

Example 2 with ResolvedMigration

use of org.flywaydb.core.api.resolver.ResolvedMigration in project flyway by flyway.

the class JdbcMigrationResolver method resolveMigrations.

@Override
public List<ResolvedMigration> resolveMigrations() {
    List<ResolvedMigration> migrations = new ArrayList<ResolvedMigration>();
    if (!location.isClassPath()) {
        return migrations;
    }
    try {
        Class<?>[] classes = scanner.scanForClasses(location, JdbcMigration.class);
        for (Class<?> clazz : classes) {
            JdbcMigration jdbcMigration = ClassUtils.instantiate(clazz.getName(), scanner.getClassLoader());
            ConfigurationInjectionUtils.injectFlywayConfiguration(jdbcMigration, configuration);
            ResolvedMigrationImpl migrationInfo = extractMigrationInfo(jdbcMigration);
            migrationInfo.setPhysicalLocation(ClassUtils.getLocationOnDisk(clazz));
            migrationInfo.setExecutor(new JdbcMigrationExecutor(jdbcMigration));
            migrations.add(migrationInfo);
        }
    } catch (Exception e) {
        throw new FlywayException("Unable to resolve Jdbc Java migrations in location: " + location + " (" + e.getMessage() + ")", 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) JdbcMigration(org.flywaydb.core.api.migration.jdbc.JdbcMigration) ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl) FlywayException(org.flywaydb.core.api.FlywayException)

Example 3 with ResolvedMigration

use of org.flywaydb.core.api.resolver.ResolvedMigration 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 4 with ResolvedMigration

use of org.flywaydb.core.api.resolver.ResolvedMigration in project flyway by flyway.

the class MigrationInfoImplSmallTest method compareToRepeatable.

@Test
public void compareToRepeatable() {
    ResolvedMigration b = createResolvedMigration(null, "B");
    MigrationInfoContext context = new MigrationInfoContext();
    context.target = MigrationVersion.LATEST;
    context.latestRepeatableRuns.put("A", 10);
    context.latestRepeatableRuns.put("B", 7);
    context.latestRepeatableRuns.put("C", 5);
    MigrationInfoImpl r1 = new MigrationInfoImpl(createResolvedMigration(null, "C"), createAppliedMigration(5, null, "C"), context, false);
    MigrationInfoImpl v2 = new MigrationInfoImpl(createResolvedMigration("1", "V1"), createAppliedMigration(6, "1", "V1"), context, false);
    MigrationInfoImpl r3 = new MigrationInfoImpl(b, createAppliedMigration(7, null, "B", 123), context, false);
    MigrationInfoImpl r4 = new MigrationInfoImpl(createResolvedMigration(null, "A"), createAppliedMigration(10, null, "A"), context, false);
    MigrationInfoImpl r6 = new MigrationInfoImpl(b, null, context, false);
    MigrationInfoImpl v5 = new MigrationInfoImpl(createResolvedMigration("6", "V2"), null, context, false);
    assertTrue(r1.compareTo(r1) == 0);
    assertTrue(r1.compareTo(v2) < 0);
    assertTrue(r1.compareTo(r3) < 0);
    assertTrue(r1.compareTo(r4) < 0);
    assertTrue(r1.compareTo(v5) < 0);
    assertTrue(r1.compareTo(r6) < 0);
    assertTrue(v2.compareTo(r1) > 0);
    assertTrue(v2.compareTo(v2) == 0);
    assertTrue(v2.compareTo(r3) < 0);
    assertTrue(v2.compareTo(r4) < 0);
    assertTrue(v2.compareTo(v5) < 0);
    assertTrue(v2.compareTo(r6) < 0);
    assertTrue(r3.compareTo(r1) > 0);
    assertTrue(r3.compareTo(v2) > 0);
    assertTrue(r3.compareTo(r3) == 0);
    assertTrue(r3.compareTo(r4) < 0);
    assertTrue(r3.compareTo(v5) < 0);
    assertTrue(r3.compareTo(r6) < 0);
    assertTrue(r4.compareTo(r1) > 0);
    assertTrue(r4.compareTo(v2) > 0);
    assertTrue(r4.compareTo(r3) > 0);
    assertTrue(r4.compareTo(r4) == 0);
    assertTrue(r4.compareTo(v5) < 0);
    assertTrue(r4.compareTo(r6) < 0);
    assertTrue(v5.compareTo(r1) > 0);
    assertTrue(v5.compareTo(v2) > 0);
    assertTrue(v5.compareTo(r3) > 0);
    assertTrue(v5.compareTo(r4) > 0);
    assertTrue(v5.compareTo(v5) == 0);
    assertTrue(v5.compareTo(r6) < 0);
    assertTrue(r6.compareTo(r1) > 0);
    assertTrue(r6.compareTo(v2) > 0);
    assertTrue(r6.compareTo(r3) > 0);
    assertTrue(r6.compareTo(r4) > 0);
    assertTrue(r6.compareTo(v5) > 0);
    assertTrue(r6.compareTo(r6) == 0);
}
Also used : ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) Test(org.junit.Test)

Example 5 with ResolvedMigration

use of org.flywaydb.core.api.resolver.ResolvedMigration in project flyway by flyway.

the class CompositeMigrationResolverSmallTest method skipDefaultResolvers.

@Test
public void skipDefaultResolvers() {
    FlywayConfigurationForTests config = FlywayConfigurationForTests.create();
    config.setSkipDefaultResolvers(true);
    MigrationResolver migrationResolver = new CompositeMigrationResolver(null, new Scanner(Thread.currentThread().getContextClassLoader()), config, new Locations("migration/outoforder", "org/flywaydb/core/internal/resolver/jdbc/dummy"), PlaceholderReplacer.NO_PLACEHOLDERS);
    Collection<ResolvedMigration> migrations = migrationResolver.resolveMigrations();
    assertTrue(migrations.isEmpty());
}
Also used : Scanner(org.flywaydb.core.internal.util.scanner.Scanner) Locations(org.flywaydb.core.internal.util.Locations) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) MigrationResolver(org.flywaydb.core.api.resolver.MigrationResolver) Test(org.junit.Test)

Aggregations

ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)24 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)10 Location (org.flywaydb.core.internal.util.Location)7 Scanner (org.flywaydb.core.internal.util.scanner.Scanner)5 FlywayException (org.flywaydb.core.api.FlywayException)4 MigrationResolver (org.flywaydb.core.api.resolver.MigrationResolver)4 MigrationVersion (org.flywaydb.core.api.MigrationVersion)3 ResolvedMigrationComparator (org.flywaydb.core.internal.resolver.ResolvedMigrationComparator)3 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)3 Connection (java.sql.Connection)2 MigrationType (org.flywaydb.core.api.MigrationType)2 MigrationExecutor (org.flywaydb.core.api.resolver.MigrationExecutor)2 AppliedMigration (org.flywaydb.core.internal.metadatatable.AppliedMigration)2 SqlMigrationResolver (org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver)2 Locations (org.flywaydb.core.internal.util.Locations)2 PlaceholderReplacer (org.flywaydb.core.internal.util.PlaceholderReplacer)2 Hashing (com.google.common.hash.Hashing)1 ByteSource (com.google.common.io.ByteSource)1 File (java.io.File)1