Search in sources :

Example 6 with ResolvedMigration

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

the class SpringJdbcMigrationResolverSmallTest method explicitInfo.

@Test
public void explicitInfo() {
    SpringJdbcMigrationResolver springJdbcMigrationResolver = new SpringJdbcMigrationResolver(scanner, null, null);
    ResolvedMigration migrationInfo = springJdbcMigrationResolver.extractMigrationInfo(new Version3dot5());
    assertEquals("3.5", migrationInfo.getVersion().toString());
    assertEquals("Three Dot Five", migrationInfo.getDescription());
    assertEquals(35, migrationInfo.getChecksum().intValue());
}
Also used : ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) Version3dot5(org.flywaydb.core.internal.resolver.spring.dummy.Version3dot5) Test(org.junit.Test)

Example 7 with ResolvedMigration

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

the class MyCustomMigrationResolver method resolveMigrations.

@Override
public List<ResolvedMigration> resolveMigrations() {
    List<ResolvedMigration> resolvedMigrations = new ArrayList<ResolvedMigration>();
    resolvedMigrations.add(new ResolvedMigration() {

        @Override
        public MigrationVersion getVersion() {
            return MigrationVersion.fromVersion("1.9");
        }

        @Override
        public String getDescription() {
            return "Virtual Migration";
        }

        @Override
        public String getScript() {
            return "VirtualScript 1.9";
        }

        @Override
        public Integer getChecksum() {
            return 19;
        }

        @Override
        public MigrationType getType() {
            return MigrationType.CUSTOM;
        }

        @Override
        public String getPhysicalLocation() {
            return "virtual://loaction";
        }

        @Override
        public MigrationExecutor getExecutor() {
            return new MigrationExecutor() {

                @Override
                public void execute(Connection connection) {
                    System.out.println("Executed !");
                }

                @Override
                public boolean executeInTransaction() {
                    return true;
                }
            };
        }
    });
    return resolvedMigrations;
}
Also used : MigrationVersion(org.flywaydb.core.api.MigrationVersion) ArrayList(java.util.ArrayList) MigrationExecutor(org.flywaydb.core.api.resolver.MigrationExecutor) Connection(java.sql.Connection) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) MigrationType(org.flywaydb.core.api.MigrationType)

Example 8 with ResolvedMigration

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

the class MigrationInfoServiceImpl method refresh.

/**
     * Refreshes the info about all known migrations from both the classpath and the DB.
     */
public void refresh() {
    Collection<ResolvedMigration> availableMigrations = migrationResolver.resolveMigrations();
    List<AppliedMigration> appliedMigrations = metaDataTable.allAppliedMigrations();
    MigrationInfoContext context = new MigrationInfoContext();
    context.outOfOrder = outOfOrder;
    context.pending = pending;
    context.missing = missing;
    context.future = future;
    context.target = target;
    Map<MigrationVersion, ResolvedMigration> resolvedMigrationsMap = new TreeMap<MigrationVersion, ResolvedMigration>();
    Map<String, ResolvedMigration> resolvedRepeatableMigrationsMap = new TreeMap<String, ResolvedMigration>();
    for (ResolvedMigration resolvedMigration : availableMigrations) {
        MigrationVersion version = resolvedMigration.getVersion();
        if (version != null) {
            if (version.compareTo(context.lastResolved) > 0) {
                context.lastResolved = version;
            }
            resolvedMigrationsMap.put(version, resolvedMigration);
        } else {
            resolvedRepeatableMigrationsMap.put(resolvedMigration.getDescription(), resolvedMigration);
        }
    }
    Map<MigrationVersion, Pair<AppliedMigration, Boolean>> appliedMigrationsMap = new TreeMap<MigrationVersion, Pair<AppliedMigration, Boolean>>();
    List<AppliedMigration> appliedRepeatableMigrations = new ArrayList<AppliedMigration>();
    for (AppliedMigration appliedMigration : appliedMigrations) {
        MigrationVersion version = appliedMigration.getVersion();
        boolean outOfOrder1 = false;
        if (version != null) {
            if (version.compareTo(context.lastApplied) > 0) {
                context.lastApplied = version;
            } else {
                outOfOrder1 = true;
            }
        }
        if (appliedMigration.getType() == MigrationType.SCHEMA) {
            context.schema = version;
        }
        if (appliedMigration.getType() == MigrationType.BASELINE) {
            context.baseline = version;
        }
        if (version != null) {
            appliedMigrationsMap.put(version, Pair.of(appliedMigration, outOfOrder1));
        } else {
            appliedRepeatableMigrations.add(appliedMigration);
        }
    }
    if (MigrationVersion.CURRENT == target) {
        context.target = context.lastApplied;
    }
    Set<MigrationVersion> allVersions = new HashSet<MigrationVersion>();
    allVersions.addAll(resolvedMigrationsMap.keySet());
    allVersions.addAll(appliedMigrationsMap.keySet());
    List<MigrationInfoImpl> migrationInfos1 = new ArrayList<MigrationInfoImpl>();
    for (MigrationVersion version : allVersions) {
        ResolvedMigration resolvedMigration = resolvedMigrationsMap.get(version);
        Pair<AppliedMigration, Boolean> appliedMigrationInfo = appliedMigrationsMap.get(version);
        if (appliedMigrationInfo == null) {
            migrationInfos1.add(new MigrationInfoImpl(resolvedMigration, null, context, false));
        } else {
            migrationInfos1.add(new MigrationInfoImpl(resolvedMigration, appliedMigrationInfo.getLeft(), context, appliedMigrationInfo.getRight()));
        }
    }
    for (AppliedMigration appliedRepeatableMigration : appliedRepeatableMigrations) {
        if (!context.latestRepeatableRuns.containsKey(appliedRepeatableMigration.getDescription()) || (appliedRepeatableMigration.getInstalledRank() > context.latestRepeatableRuns.get(appliedRepeatableMigration.getDescription()))) {
            context.latestRepeatableRuns.put(appliedRepeatableMigration.getDescription(), appliedRepeatableMigration.getInstalledRank());
        }
    }
    Set<ResolvedMigration> pendingResolvedRepeatableMigrations = new HashSet<ResolvedMigration>(resolvedRepeatableMigrationsMap.values());
    for (AppliedMigration appliedRepeatableMigration : appliedRepeatableMigrations) {
        ResolvedMigration resolvedMigration = resolvedRepeatableMigrationsMap.get(appliedRepeatableMigration.getDescription());
        int latestRank = context.latestRepeatableRuns.get(appliedRepeatableMigration.getDescription());
        if (resolvedMigration != null && appliedRepeatableMigration.getInstalledRank() == latestRank && ObjectUtils.nullSafeEquals(appliedRepeatableMigration.getChecksum(), resolvedMigration.getChecksum())) {
            pendingResolvedRepeatableMigrations.remove(resolvedMigration);
        }
        migrationInfos1.add(new MigrationInfoImpl(resolvedMigration, appliedRepeatableMigration, context, false));
    }
    for (ResolvedMigration pendingResolvedRepeatableMigration : pendingResolvedRepeatableMigrations) {
        migrationInfos1.add(new MigrationInfoImpl(pendingResolvedRepeatableMigration, null, context, false));
    }
    Collections.sort(migrationInfos1);
    migrationInfos = migrationInfos1;
}
Also used : ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) MigrationVersion(org.flywaydb.core.api.MigrationVersion) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) Pair(org.flywaydb.core.internal.util.Pair) HashSet(java.util.HashSet)

Example 9 with ResolvedMigration

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

the class CompositeMigrationResolver method checkForIncompatibilities.

/**
     * Checks for incompatible migrations.
     *
     * @param migrations The migrations to check.
     * @throws FlywayException when two different migration with the same version number are found.
     */
/* private -> for testing */
static void checkForIncompatibilities(List<ResolvedMigration> migrations) {
    // check for more than one migration with same version
    for (int i = 0; i < migrations.size() - 1; i++) {
        ResolvedMigration current = migrations.get(i);
        ResolvedMigration next = migrations.get(i + 1);
        if (new ResolvedMigrationComparator().compare(current, next) == 0) {
            if (current.getVersion() != null) {
                throw new FlywayException(String.format("Found more than one migration with version %s\nOffenders:\n-> %s (%s)\n-> %s (%s)", current.getVersion(), current.getPhysicalLocation(), current.getType(), next.getPhysicalLocation(), next.getType()));
            }
            throw new FlywayException(String.format("Found more than one repeatable migration with description %s\nOffenders:\n-> %s (%s)\n-> %s (%s)", current.getDescription(), current.getPhysicalLocation(), current.getType(), next.getPhysicalLocation(), next.getType()));
        }
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration)

Example 10 with ResolvedMigration

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

the class SqlMigrationResolver method resolveMigrations.

public List<ResolvedMigration> resolveMigrations() {
    List<ResolvedMigration> migrations = new ArrayList<ResolvedMigration>();
    scanForMigrations(migrations, configuration.getSqlMigrationPrefix(), configuration.getSqlMigrationSeparator(), configuration.getSqlMigrationSuffix());
    scanForMigrations(migrations, configuration.getRepeatableSqlMigrationPrefix(), configuration.getSqlMigrationSeparator(), configuration.getSqlMigrationSuffix());
    Collections.sort(migrations, new ResolvedMigrationComparator());
    return migrations;
}
Also used : ResolvedMigrationComparator(org.flywaydb.core.internal.resolver.ResolvedMigrationComparator) ArrayList(java.util.ArrayList) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration)

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