allow a new upgrade version without DB changes to be inserted (#6858)

This commit is contained in:
dahn 2022-12-19 02:03:07 -08:00 committed by GitHub
parent 43b4525f71
commit 41717b0977
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 166 additions and 7 deletions

View File

@ -57,6 +57,12 @@
<artifactId>ini4j</artifactId>
<version>${cs.ini.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${cs.junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -122,7 +122,7 @@ public final class DatabaseVersionHierarchy {
*
* @since 4.8.2.0 (refactored in 4.11.1.0)
*/
private CloudStackVersion getRecentVersion(final CloudStackVersion fromVersion) {
protected CloudStackVersion getRecentVersion(final CloudStackVersion fromVersion) {
if (fromVersion == null) {
return null;
}
@ -131,7 +131,7 @@ public final class DatabaseVersionHierarchy {
return toList()
.reverse()
.stream()
.filter(version -> fromVersion.compareTo(version) < 0)
.filter(version -> fromVersion.compareTo(version) > 0)
.findFirst()
.orElse(null);
}
@ -158,16 +158,17 @@ public final class DatabaseVersionHierarchy {
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
private static class VersionNode {
protected static class VersionNode {
final CloudStackVersion version;
final DbUpgrade upgrader;
private VersionNode(final CloudStackVersion version, final DbUpgrade upgrader) {
protected VersionNode(final CloudStackVersion version, final DbUpgrade upgrader) {
this.version = version;
this.upgrader = upgrader;
}
}
public static final class DatabaseVersionHierarchyBuilder {
private final List<VersionNode> hierarchyBuilder = new LinkedList<>();

View File

@ -32,6 +32,8 @@ import com.cloud.upgrade.dao.Upgrade41100to41110;
import com.cloud.upgrade.dao.Upgrade41110to41120;
import com.cloud.upgrade.dao.Upgrade41120to41130;
import com.cloud.upgrade.dao.Upgrade41120to41200;
import com.cloud.upgrade.dao.Upgrade41510to41520;
import com.cloud.upgrade.dao.Upgrade41610to41700;
import com.cloud.upgrade.dao.Upgrade452to453;
import com.cloud.upgrade.dao.Upgrade453to460;
import com.cloud.upgrade.dao.Upgrade460to461;
@ -165,9 +167,41 @@ public class DatabaseUpgradeCheckerTest {
final DatabaseUpgradeChecker checker = new DatabaseUpgradeChecker();
final DbUpgrade[] upgrades = checker.calculateUpgradePath(dbVersion, currentVersion);
assertNotNull(upgrades);
assertTrue(upgrades.length == 1);
assertTrue(upgrades[0] instanceof NoopDbUpgrade);
assertEquals("We should have 2 upgrade steps", 2, upgrades.length);
assertTrue(upgrades[1] instanceof NoopDbUpgrade);
}
@Test
public void testCalculateUpgradePathFromKownDbVersion() {
final CloudStackVersion dbVersion = CloudStackVersion.parse("4.17.0.0");
assertNotNull(dbVersion);
final CloudStackVersion currentVersion = CloudStackVersion.parse("4.99.1.0");
assertNotNull(currentVersion);
final DatabaseUpgradeChecker checker = new DatabaseUpgradeChecker();
final DbUpgrade[] upgrades = checker.calculateUpgradePath(dbVersion, currentVersion);
assertNotNull(upgrades);
assertTrue(upgrades.length > 2);
assertTrue(upgrades[upgrades.length - 1] instanceof NoopDbUpgrade);
}
@Test
public void testCalculateUpgradePathFromUnregisteredSecVersion() {
final CloudStackVersion dbVersion = CloudStackVersion.parse("4.15.1.3");
assertNotNull(dbVersion);
final CloudStackVersion currentVersion = CloudStackVersion.parse("4.17.0.0");
assertNotNull(currentVersion);
final DatabaseUpgradeChecker checker = new DatabaseUpgradeChecker();
final DbUpgrade[] upgrades = checker.calculateUpgradePath(dbVersion, currentVersion);
assertNotNull("there should be upgrade paths", upgrades);
assertTrue(upgrades.length > 1);
assertTrue(upgrades[0] instanceof Upgrade41510to41520);
assertTrue(upgrades[upgrades.length - 1] instanceof Upgrade41610to41700);
}
}

View File

@ -0,0 +1,117 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.upgrade;
import com.cloud.upgrade.dao.DbUpgrade;
import com.cloud.upgrade.dao.Upgrade41110to41120;
import com.cloud.upgrade.dao.Upgrade41120to41130;
import com.cloud.upgrade.dao.Upgrade41120to41200;
import com.cloud.upgrade.dao.Upgrade41500to41510;
import com.cloud.upgrade.dao.Upgrade41510to41520;
import com.cloud.upgrade.dao.Upgrade41520to41600;
import com.cloud.upgrade.dao.Upgrade41720to41800;
import com.cloud.upgrade.dao.Upgrade481to490;
import org.apache.cloudstack.utils.CloudStackVersion;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.InputStream;
import java.sql.Connection;
import static org.junit.jupiter.api.Assertions.assertEquals;
class DatabaseVersionHierarchyTest {
private static DatabaseVersionHierarchy hierarchy;
static class DummyUpgrade implements DbUpgrade {
@Override
public String[] getUpgradableVersionRange() {
return new String[0];
}
@Override
public String getUpgradedVersion() {
return null;
}
@Override
public boolean supportsRollingUpgrade() {
return false;
}
@Override
public InputStream[] getPrepareScripts() {
return new InputStream[0];
}
@Override
public void performDataMigration(Connection conn) {
}
@Override
public InputStream[] getCleanupScripts() {
return new InputStream[0];
}
}
@BeforeAll
static void init() {
DatabaseVersionHierarchy.DatabaseVersionHierarchyBuilder builder = DatabaseVersionHierarchy.builder()
.next("0.0.5", new DummyUpgrade())
.next("1.0.0.0", new DummyUpgrade())
.next("1.0.1" , new DummyUpgrade())
.next("1.2.0" , new DummyUpgrade())
.next("2.0.0" , new DummyUpgrade())
.next("2.3.2" , new DummyUpgrade())
.next("3.4.5.6" , new DummyUpgrade())
.next("4.8.2.0" , new Upgrade481to490())
.next("4.9.10.11" , new DummyUpgrade())
.next("4.11.1.0", new Upgrade41110to41120())
.next("4.11.2.0", new Upgrade41120to41130())
.next("4.11.3.0", new Upgrade41120to41200())
.next("4.15.0.0", new Upgrade41500to41510())
.next("4.15.1.0", new Upgrade41510to41520())
.next("4.15.2.0", new Upgrade41520to41600())
.next("4.15.4", new DummyUpgrade())
.next("4.17.2.0", new Upgrade41720to41800());
hierarchy = builder.build();
}
@Test
void getRecentVersionMiddle() {
assertEquals("2.0.0", hierarchy.getRecentVersion(CloudStackVersion.parse("2.2.2")).toString());
}
@Test
void getRecentVersionEarly() {
assertEquals(null, hierarchy.getRecentVersion(CloudStackVersion.parse("0.0.2")));
}
@Test
void getRecentVersionStart() {
assertEquals(null, hierarchy.getRecentVersion(CloudStackVersion.parse("0.0.5")));
}
@Test
void getRecentVersionJust() {
assertEquals("0.0.5", hierarchy.getRecentVersion(CloudStackVersion.parse("0.0.9")).toString());
}
@Test
void getRecentVersionExact() {
assertEquals("0.0.5", hierarchy.getRecentVersion(CloudStackVersion.parse("1.0.0.0")).toString());
}
}

View File

@ -110,6 +110,7 @@
<cs.hamcrest.version>1.3</cs.hamcrest.version>
<cs.junit.version>4.13.2</cs.junit.version>
<cs.junit.dataprovider.version>1.13.1</cs.junit.dataprovider.version>
<cs.junit.jupiter.version>5.9.1</cs.junit.jupiter.version>
<cs.guava-testlib.version>18.0</cs.guava-testlib.version>
<cs.mockito.version>3.2.4</cs.mockito.version>
<cs.powermock.version>2.0.5</cs.powermock.version>