Search in sources :

Example 6 with ResolvedMigrationImpl

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

the class MigrationInfoDumperSmallTest method createAvailableMigration.

/**
     * Creates a new available migration with this version.
     *
     * @param version The version of the migration.
     * @return The available migration.
     */
private ResolvedMigration createAvailableMigration(String version) {
    ResolvedMigrationImpl migration = new ResolvedMigrationImpl();
    migration.setVersion(MigrationVersion.fromVersion(version));
    migration.setDescription("abc very very very very very very very very very very long");
    migration.setScript("x");
    migration.setType(MigrationType.SQL);
    return migration;
}
Also used : ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)

Example 7 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 8 with ResolvedMigrationImpl

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

the class ApplicationContextAwareSpringJdbcMigrationResolver method extractMigrationInfo.

/**
 * Extracts the migration info from this migration.
 *
 * @param springJdbcMigration The migration to analyse.
 * @return The migration info.
 */
/* private -> testing */
ResolvedMigrationImpl extractMigrationInfo(ApplicationContextAwareSpringMigration 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;
        boolean repeatable = shortName.startsWith("R");
        if (shortName.startsWith("V") || repeatable) {
            prefix = shortName.substring(0, 1);
        } else {
            throw new FlywayException("Invalid Spring 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, "__", "", repeatable);
        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 9 with ResolvedMigrationImpl

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

the class MigrationInfoImplSmallTest method createResolvedMigration.

/**
     * Creates a new resolved repeatable migration with this description.
     *
     * @param description The description of the migration.
     * @return The resolved migration.
     */
private ResolvedMigration createResolvedMigration(String version, String description) {
    ResolvedMigrationImpl migration = new ResolvedMigrationImpl();
    migration.setVersion(version == null ? null : MigrationVersion.fromVersion(version));
    migration.setDescription(description);
    migration.setScript(description);
    migration.setType(MigrationType.SQL);
    return migration;
}
Also used : ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)

Example 10 with ResolvedMigrationImpl

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

the class MigrationInfoImplSmallTest method validate.

@Test
public void validate() {
    MigrationVersion version = MigrationVersion.fromVersion("1");
    String description = "test";
    MigrationType type = MigrationType.SQL;
    ResolvedMigrationImpl resolvedMigration = new ResolvedMigrationImpl();
    resolvedMigration.setVersion(version);
    resolvedMigration.setDescription(description);
    resolvedMigration.setType(type);
    resolvedMigration.setChecksum(456);
    AppliedMigration appliedMigration = new AppliedMigration(1, version, description, type, null, 123, new Date(), "abc", 0, true);
    MigrationInfoImpl migrationInfo = new MigrationInfoImpl(resolvedMigration, appliedMigration, new MigrationInfoContext(), false);
    String message = migrationInfo.validate();
    assertTrue(message.contains("123"));
    assertTrue(message.contains("456"));
}
Also used : MigrationVersion(org.flywaydb.core.api.MigrationVersion) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration) MigrationType(org.flywaydb.core.api.MigrationType) ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)12 FlywayException (org.flywaydb.core.api.FlywayException)6 MigrationVersion (org.flywaydb.core.api.MigrationVersion)6 ArrayList (java.util.ArrayList)4 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)4 MigrationChecksumProvider (org.flywaydb.core.api.migration.MigrationChecksumProvider)3 MigrationInfoProvider (org.flywaydb.core.api.migration.MigrationInfoProvider)3 ResolvedMigrationComparator (org.flywaydb.core.internal.resolver.ResolvedMigrationComparator)3 MigrationType (org.flywaydb.core.api.MigrationType)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