Skip to content

Commit

Permalink
#2600 - Pending drops migration with the same version of the target m…
Browse files Browse the repository at this point in the history
…igration cause trouble
  • Loading branch information
rbygrave committed Mar 22, 2022
1 parent 66aa772 commit de4140a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
package io.ebeaninternal.dbmigration;

import static io.ebeaninternal.api.PlatformMatch.matchPlatform;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.ebean.DB;
import io.ebean.Database;
import io.ebean.annotation.Platform;
Expand Down Expand Up @@ -50,15 +38,21 @@
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
import io.ebeaninternal.dbmigration.migration.Migration;
import io.ebeaninternal.dbmigration.migrationreader.MigrationXmlWriter;
import io.ebeaninternal.dbmigration.model.CurrentModel;
import io.ebeaninternal.dbmigration.model.MConfiguration;
import io.ebeaninternal.dbmigration.model.MigrationModel;
import io.ebeaninternal.dbmigration.model.ModelContainer;
import io.ebeaninternal.dbmigration.model.ModelDiff;
import io.ebeaninternal.dbmigration.model.PlatformDdlWriter;
import io.ebeaninternal.dbmigration.model.*;
import io.ebeaninternal.extraddl.model.DdlScript;
import io.ebeaninternal.extraddl.model.ExtraDdl;
import io.ebeaninternal.extraddl.model.ExtraDdlXmlReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import static io.ebeaninternal.api.PlatformMatch.matchPlatform;

/**
* Generates DB Migration xml and sql scripts.
Expand Down Expand Up @@ -611,6 +605,7 @@ private String fullVersion(String nextVersion, String dropsFor) {
if (version == null) {
version = (nextVersion != null) ? nextVersion : initialVersion;
}
checkDropVersion(version, dropsFor);

String fullVersion = applyPrefix + version;
String name = name();
Expand All @@ -626,6 +621,13 @@ private String fullVersion(String nextVersion, String dropsFor) {
return fullVersion;
}

void checkDropVersion(String version, String dropsFor) {
if (dropsFor != null && dropsFor.equals(version)) {
throw new IllegalArgumentException("The next migration version must not be the same as the pending drops version of " +
dropsFor + ". Please make the next migration version higher than " + dropsFor + ".");
}
}

String trimDropsFor(String dropsFor) {
if (dropsFor.startsWith("V") || dropsFor.startsWith("v")) {
dropsFor = dropsFor.substring(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class DefaultDbMigrationTest {
class DefaultDbMigrationTest {

private final DefaultDbMigration migration = new DefaultDbMigration();

@Test
public void trimDropsFor() {
void trimDropsFor() {
assertEquals("1.2", migration.trimDropsFor("V1.2__hello"));
assertEquals("1.2", migration.trimDropsFor("v1.2__hello"));
assertEquals("1.2", migration.trimDropsFor("v1.2"));
assertEquals("junk1.2", migration.trimDropsFor("junk1.2"));
assertEquals("junk1.2", migration.trimDropsFor("junk1.2__"));
assertEquals("junk1.2", migration.trimDropsFor("junk1.2__more"));
}

@Test
void checkDropVersion_when_matches_throwsIAE() {
assertThrows(IllegalArgumentException.class, () -> migration.checkDropVersion("1.0", "1.0"));
}

@Test
void checkDropVersion_ok() {
migration.checkDropVersion("1.0", null);
migration.checkDropVersion("1.0", "1.0.0");
migration.checkDropVersion("1.0", "1.1");
}
}

0 comments on commit de4140a

Please sign in to comment.