Skip to content

Commit 52ba5a6

Browse files
committed
engine/schema: fix missing 4.20.1.0 node in DB upgrade hierarchy
Direct upgrade from 4.20.1.0 to 4.20.3.0 failed because the upgrade hierarchy skipped the 4.20.1.0 node, causing DatabaseVersionHierarchy to fall back to 4.20.0.0 and re-run Upgrade42000to42010. This triggered a duplicate key violation on configuration_group.name='Usage Server' which already existed from the original 4.20.1.0 install. - Add Upgrade42010to42020 no-op upgrader to register 4.20.1.0 in the upgrade hierarchy - Register it in DatabaseUpgradeChecker between 4.20.0.0 and 4.20.2.0 - Harden schema-42000to42010.sql with INSERT IGNORE as a safety net - Add unit tests for direct 4.20.1.0->4.20.3.0 and 4.20.2.0->4.20.3.0 upgrade paths
1 parent c19630f commit 52ba5a6

File tree

8 files changed

+171
-2
lines changed

8 files changed

+171
-2
lines changed

engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import javax.inject.Inject;
3535

36+
import com.cloud.upgrade.dao.Upgrade42010to42020;
3637
import com.cloud.upgrade.dao.Upgrade42020to42030;
3738
import com.cloud.utils.FileUtil;
3839
import org.apache.cloudstack.utils.CloudStackVersion;
@@ -237,6 +238,7 @@ public DatabaseUpgradeChecker() {
237238
.next("4.19.0.0", new Upgrade41900to41910())
238239
.next("4.19.1.0", new Upgrade41910to42000())
239240
.next("4.20.0.0", new Upgrade42000to42010())
241+
.next("4.20.1.0", new Upgrade42010to42020())
240242
.next("4.20.2.0", new Upgrade42020to42030())
241243
.build();
242244
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.upgrade.dao;
18+
19+
import java.io.InputStream;
20+
import java.sql.Connection;
21+
22+
import com.cloud.utils.exception.CloudRuntimeException;
23+
24+
public class Upgrade42010to42020 extends DbUpgradeAbstractImpl implements DbUpgrade {
25+
26+
@Override
27+
public String[] getUpgradableVersionRange() {
28+
return new String[]{"4.20.1.0", "4.20.2.0"};
29+
}
30+
31+
@Override
32+
public String getUpgradedVersion() {
33+
return "4.20.2.0";
34+
}
35+
36+
@Override
37+
public boolean supportsRollingUpgrade() {
38+
return false;
39+
}
40+
41+
@Override
42+
public InputStream[] getPrepareScripts() {
43+
final String scriptFile = "META-INF/db/schema-42010to42020.sql";
44+
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
45+
if (script == null) {
46+
throw new CloudRuntimeException("Unable to find " + scriptFile);
47+
}
48+
return new InputStream[]{script};
49+
}
50+
51+
@Override
52+
public void performDataMigration(Connection conn) {
53+
}
54+
55+
@Override
56+
public InputStream[] getCleanupScripts() {
57+
final String scriptFile = "META-INF/db/schema-42010to42020-cleanup.sql";
58+
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
59+
if (script == null) {
60+
throw new CloudRuntimeException("Unable to find " + scriptFile);
61+
}
62+
return new InputStream[]{script};
63+
}
64+
}

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42020to42030.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ public void performDataMigration(Connection conn) {
5555

5656
@Override
5757
public InputStream[] getCleanupScripts() {
58-
return null;
58+
final String scriptFile = "META-INF/db/schema-42020to42030-cleanup.sql";
59+
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
60+
if (script == null) {
61+
throw new CloudRuntimeException("Unable to find " + scriptFile);
62+
}
63+
return new InputStream[]{script};
5964
}
6065

6166
@Override

engine/schema/src/main/resources/META-INF/db/schema-42000to42010.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.account', 'api_key_access', 'boolean
3131
CALL `cloud_usage`.`IDEMPOTENT_ADD_COLUMN`('cloud_usage.account', 'api_key_access', 'boolean DEFAULT NULL COMMENT "is api key access allowed for the account" ');
3232

3333
-- Create a new group for Usage Server related configurations
34-
INSERT INTO `cloud`.`configuration_group` (`name`, `description`, `precedence`) VALUES ('Usage Server', 'Usage Server related configuration', 9);
34+
INSERT IGNORE INTO `cloud`.`configuration_group` (`name`, `description`, `precedence`) VALUES ('Usage Server', 'Usage Server related configuration', 9);
3535
UPDATE `cloud`.`configuration_subgroup` set `group_id` = (SELECT `id` FROM `cloud`.`configuration_group` WHERE `name` = 'Usage Server'), `precedence` = 1 WHERE `name`='Usage';
3636
UPDATE `cloud`.`configuration` SET `group_id` = (SELECT `id` FROM `cloud`.`configuration_group` WHERE `name` = 'Usage Server') where `subgroup_id` = (SELECT `id` FROM `cloud`.`configuration_subgroup` WHERE `name` = 'Usage');
3737

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
--;
19+
-- Schema upgrade cleanup from 4.20.1.0 to 4.20.2.0
20+
--;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
--;
19+
-- Schema upgrade from 4.20.1.0 to 4.20.2.0
20+
--;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work in additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
--;
19+
-- Schema upgrade cleanup from 4.20.2.0 to 4.20.3.0
20+
--;

engine/schema/src/test/java/com/cloud/upgrade/DatabaseUpgradeCheckerTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
import com.cloud.upgrade.dao.Upgrade470to471;
5252
import com.cloud.upgrade.dao.Upgrade471to480;
5353
import com.cloud.upgrade.dao.Upgrade480to481;
54+
import com.cloud.upgrade.dao.Upgrade42010to42020;
55+
import com.cloud.upgrade.dao.Upgrade42020to42030;
5456
import com.cloud.upgrade.dao.Upgrade490to4910;
5557

5658
import com.cloud.utils.db.TransactionLegacy;
@@ -360,6 +362,42 @@ public void testCalculateUpgradePathFromSecurityReleaseToNextSecurityRelease() {
360362
assertTrue(upgradesFromSecurityReleaseToNext[upgradesFromSecurityReleaseToNext.length - 1] instanceof NoopDbUpgrade);
361363
}
362364

365+
@Test
366+
public void testCalculateUpgradePath42010to42030() {
367+
// Regression test for: missing 4.20.1.0 node in upgrade hierarchy
368+
// caused direct 4.20.1.0 -> 4.20.3.0 upgrade to re-run Upgrade42000to42010
369+
// and fail with duplicate key on configuration_group.name='Usage Server'
370+
final CloudStackVersion dbVersion = CloudStackVersion.parse("4.20.1.0");
371+
assertNotNull(dbVersion);
372+
373+
final CloudStackVersion currentVersion = CloudStackVersion.parse("4.20.3.0");
374+
assertNotNull(currentVersion);
375+
376+
final DatabaseUpgradeChecker checker = new DatabaseUpgradeChecker();
377+
final DbUpgrade[] upgrades = checker.calculateUpgradePath(dbVersion, currentVersion);
378+
379+
assertNotNull(upgrades);
380+
assertEquals("Direct upgrade from 4.20.1.0 to 4.20.3.0 should have exactly 2 steps", 2, upgrades.length);
381+
assertTrue("First step should be Upgrade42010to42020", upgrades[0] instanceof Upgrade42010to42020);
382+
assertTrue("Second step should be Upgrade42020to42030", upgrades[1] instanceof Upgrade42020to42030);
383+
}
384+
385+
@Test
386+
public void testCalculateUpgradePath42020to42030() {
387+
final CloudStackVersion dbVersion = CloudStackVersion.parse("4.20.2.0");
388+
assertNotNull(dbVersion);
389+
390+
final CloudStackVersion currentVersion = CloudStackVersion.parse("4.20.3.0");
391+
assertNotNull(currentVersion);
392+
393+
final DatabaseUpgradeChecker checker = new DatabaseUpgradeChecker();
394+
final DbUpgrade[] upgrades = checker.calculateUpgradePath(dbVersion, currentVersion);
395+
396+
assertNotNull(upgrades);
397+
assertEquals("Upgrade from 4.20.2.0 to 4.20.3.0 should have exactly 1 step", 1, upgrades.length);
398+
assertTrue("Only step should be Upgrade42020to42030", upgrades[0] instanceof Upgrade42020to42030);
399+
}
400+
363401
@Test
364402
public void isStandalone() throws SQLException {
365403
// simulate zero 'UP' hosts -> standalone

0 commit comments

Comments
 (0)