Search in sources :

Example 1 with MigrationVersion

use of org.flywaydb.core.api.MigrationVersion 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 MigrationVersion

use of org.flywaydb.core.api.MigrationVersion 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 MigrationVersion

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

use of org.flywaydb.core.api.MigrationVersion in project flyway by flyway.

the class SqlMigrationResolver method scanForMigrations.

private void scanForMigrations(List<ResolvedMigration> migrations, String prefix, String separator, String suffix) {
    for (Resource resource : scanner.scanForResources(location, prefix, suffix)) {
        String filename = resource.getFilename();
        if (isSqlCallback(filename, suffix)) {
            continue;
        }
        Pair<MigrationVersion, String> info = MigrationInfoHelper.extractVersionAndDescription(filename, prefix, separator, suffix);
        ResolvedMigrationImpl migration = new ResolvedMigrationImpl();
        migration.setVersion(info.getLeft());
        migration.setDescription(info.getRight());
        migration.setScript(extractScriptName(resource));
        migration.setChecksum(calculateChecksum(resource, resource.loadAsString(configuration.getEncoding())));
        migration.setType(MigrationType.SQL);
        migration.setPhysicalLocation(resource.getLocationOnDisk());
        migration.setExecutor(new SqlMigrationExecutor(dbSupport, resource, placeholderReplacer, configuration));
        migrations.add(migration);
    }
}
Also used : MigrationVersion(org.flywaydb.core.api.MigrationVersion) Resource(org.flywaydb.core.internal.util.scanner.Resource) ResolvedMigrationImpl(org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)

Example 5 with MigrationVersion

use of org.flywaydb.core.api.MigrationVersion in project flyway by flyway.

the class DB2zOSMigrationMediumTest method migrateCRUD.

@Test
public void migrateCRUD() throws Exception {
    query = "SELECT COUNT(*) FROM sysibm.systables WHERE dbname = 'AURINT'";
    flyway.setLocations(getMigrationDir());
    int countTablesBeforeMigration = jdbcTemplate.queryForInt(query);
    assertEquals(0, countTablesBeforeMigration);
    flyway.baseline();
    flyway.migrate();
    int countTablesAfterMigration = jdbcTemplate.queryForInt(query);
    assertEquals(2, countTablesAfterMigration);
    MigrationVersion version = flyway.info().current().getVersion();
    assertEquals("1.3", version.toString());
    assertEquals("UpdateTable", flyway.info().current().getDescription());
    assertEquals("Nils", jdbcTemplate.queryForString("SELECT firstname FROM AURINT.PERSON WHERE lastname = 'Nilsen'"));
}
Also used : MigrationVersion(org.flywaydb.core.api.MigrationVersion) Test(org.junit.Test)

Aggregations

MigrationVersion (org.flywaydb.core.api.MigrationVersion)46 Test (org.junit.Test)27 SQLException (java.sql.SQLException)10 MigrationInfo (org.flywaydb.core.api.MigrationInfo)10 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)7 HashMap (java.util.HashMap)6 FlywayException (org.flywaydb.core.api.FlywayException)6 MigrationType (org.flywaydb.core.api.MigrationType)5 AppliedMigration (org.flywaydb.core.internal.metadatatable.AppliedMigration)5 MigrationInfoService (org.flywaydb.core.api.MigrationInfoService)3 MigrationChecksumProvider (org.flywaydb.core.api.migration.MigrationChecksumProvider)3 MigrationInfoProvider (org.flywaydb.core.api.migration.MigrationInfoProvider)3 MigrationExecutor (org.flywaydb.core.api.resolver.MigrationExecutor)3 FlywaySqlScriptException (org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)2 FlywaySqlException (org.flywaydb.core.internal.dbsupport.FlywaySqlException)2 FlywaySqlException (org.flywaydb.core.internal.exception.FlywaySqlException)2 PlaceholderReplacer (org.flywaydb.core.internal.util.PlaceholderReplacer)2