Skip to content

Commit

Permalink
Fix #3111: add target=next
Browse files Browse the repository at this point in the history
  • Loading branch information
tjs145 committed Aug 4, 2021
1 parent 7ec3937 commit 7b4e34b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public final class MigrationVersion implements Comparable<MigrationVersion> {
*/
public static final MigrationVersion CURRENT = new MigrationVersion(BigInteger.valueOf(-2), "<< Current Version >>");

/**
* Next version.
*/
public static final MigrationVersion NEXT = new MigrationVersion(BigInteger.valueOf(-3), "<< Next Version >>");

/**
* Regex for matching proper version format
*/
Expand All @@ -66,6 +71,7 @@ public final class MigrationVersion implements Comparable<MigrationVersion> {
@SuppressWarnings("ConstantConditions")
public static MigrationVersion fromVersion(String version) {
if ("current".equalsIgnoreCase(version)) return CURRENT;
if ("next".equalsIgnoreCase(version)) return NEXT;
if ("latest".equalsIgnoreCase(version) || LATEST.getVersion().equals(version)) return LATEST;
if (version == null) return EMPTY;
return new MigrationVersion(version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.flywaydb.core.internal.jdbc.DriverDataSource;
import org.flywaydb.core.internal.scanner.ClasspathClassScanner;
import org.flywaydb.core.internal.util.ClassUtils;
import org.flywaydb.core.internal.util.FeatureDetector;
import org.flywaydb.core.internal.util.Locations;
import org.flywaydb.core.internal.util.StringUtils;
import org.flywaydb.core.internal.license.Edition;
Expand Down Expand Up @@ -891,6 +892,15 @@ public void setTablespace(String tablespace) {
* Defaults to {@code latest}.
*/
public void setTarget(MigrationVersion target) {
if (!FeatureDetector.areExperimentalFeaturesEnabled() && target == MigrationVersion.NEXT) {





throw new org.flywaydb.core.internal.license.FlywayTeamsUpgradeRequiredException("target=next");

}
this.target = target;
}

Expand Down Expand Up @@ -919,8 +929,8 @@ public void setTargetAsString(String target) {


} else {
this.failOnMissingTarget = true;
this.target = MigrationVersion.fromVersion(target);
setFailOnMissingTarget(true);
setTarget(MigrationVersion.fromVersion(target));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ private int migrateAll() {
if (count == 0) {
// No further migrations available
break;
} else if (configuration.getTarget() == MigrationVersion.NEXT) {
// With target=next we only execute one migration
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public MigrationState getState() {



if (context.target != null && resolvedMigration.getVersion().compareTo(context.target) > 0) {
if (context.target != null && context.target != MigrationVersion.NEXT && resolvedMigration.getVersion().compareTo(context.target) > 0) {
return MigrationState.ABOVE_TARGET;
}
if ((resolvedMigration.getVersion().compareTo(context.lastApplied) < 0) && !context.outOfOrder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.flywaydb.core.internal.database.base.Schema;
import org.flywaydb.core.internal.schemahistory.AppliedMigration;
import org.flywaydb.core.internal.schemahistory.SchemaHistory;
import org.flywaydb.core.internal.util.FeatureDetector;
import org.flywaydb.core.internal.util.Pair;

import java.util.*;
Expand Down Expand Up @@ -231,7 +232,8 @@ public void refresh() {
if (configuration.getFailOnMissingTarget() &&
target != null &&
target != MigrationVersion.CURRENT &&
target != MigrationVersion.LATEST) {
target != MigrationVersion.LATEST &&
target != MigrationVersion.NEXT) {
boolean targetFound = false;

for (MigrationInfoImpl migration : migrationInfos1) {
Expand Down Expand Up @@ -291,6 +293,15 @@ public void refresh() {
// Set output
Collections.sort(migrationInfos1);
migrationInfos = migrationInfos1;

if (context.target == MigrationVersion.NEXT) {
MigrationInfo[] pendingMigrationInfos = pending();
if (pendingMigrationInfos.length == 0) {
context.target = null;
} else {
context.target = pendingMigrationInfos[0].getVersion();
}
}
}

/**
Expand Down

0 comments on commit 7b4e34b

Please sign in to comment.