Search in sources :

Example 1 with ResolvedMigrationImpl

use of org.flywaydb.core.internal.resolver.ResolvedMigrationImpl 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 ResolvedMigrationImpl

use of org.flywaydb.core.internal.resolver.ResolvedMigrationImpl in project flyway by flyway.

the class JdbcMigrationResolver method extractMigrationInfo.

/**
     * Extracts the migration info from this migration.
     *
     * @param jdbcMigration The migration to analyse.
     * @return The migration info.
     */
/* private -> testing */
ResolvedMigrationImpl extractMigrationInfo(JdbcMigration jdbcMigration) {
    Integer checksum = null;
    if (jdbcMigration instanceof MigrationChecksumProvider) {
        MigrationChecksumProvider checksumProvider = (MigrationChecksumProvider) jdbcMigration;
        checksum = checksumProvider.getChecksum();
    }
    MigrationVersion version;
    String description;
    if (jdbcMigration instanceof MigrationInfoProvider) {
        MigrationInfoProvider infoProvider = (MigrationInfoProvider) jdbcMigration;
        version = infoProvider.getVersion();
        description = infoProvider.getDescription();
        if (!StringUtils.hasText(description)) {
            throw new FlywayException("Missing description for migration " + version);
        }
    } else {
        String shortName = ClassUtils.getShortName(jdbcMigration.getClass());
        String prefix;
        if (shortName.startsWith("V") || shortName.startsWith("R")) {
            prefix = shortName.substring(0, 1);
        } else {
            throw new FlywayException("Invalid Jdbc migration class name: " + jdbcMigration.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(jdbcMigration.getClass().getName());
    resolvedMigration.setChecksum(checksum);
    resolvedMigration.setType(MigrationType.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 3 with ResolvedMigrationImpl

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

use of org.flywaydb.core.internal.resolver.ResolvedMigrationImpl 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 5 with ResolvedMigrationImpl

use of org.flywaydb.core.internal.resolver.ResolvedMigrationImpl 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)

Aggregations

ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)10 FlywayException (org.flywaydb.core.api.FlywayException)5 MigrationVersion (org.flywaydb.core.api.MigrationVersion)5 ArrayList (java.util.ArrayList)3 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)3 MigrationType (org.flywaydb.core.api.MigrationType)2 MigrationChecksumProvider (org.flywaydb.core.api.migration.MigrationChecksumProvider)2 MigrationInfoProvider (org.flywaydb.core.api.migration.MigrationInfoProvider)2 ResolvedMigrationComparator (org.flywaydb.core.internal.resolver.ResolvedMigrationComparator)2 Resource (org.flywaydb.core.internal.util.scanner.Resource)2 Hashing (com.google.common.hash.Hashing)1 ByteSource (com.google.common.io.ByteSource)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 SQLException (java.sql.SQLException)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1