From 9480668f42e18d56fda1d40a8895fa8ce1a03453 Mon Sep 17 00:00:00 2001 From: Abhisar Sinha <63767682+abh1sar@users.noreply.github.com> Date: Tue, 14 Apr 2026 13:08:16 +0530 Subject: [PATCH 1/3] remove unique key constraint from cloud_usage.usage_vm_instance table --- .../cloud/upgrade/DatabaseUpgradeChecker.java | 4 +- .../upgrade/dao/Upgrade42030to42040.java | 60 +++++++++++++++++++ setup/db/create-schema-premium.sql | 3 +- 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42030to42040.java diff --git a/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java b/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java index a8a166fbf275..0d9807dcb2df 100644 --- a/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java +++ b/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java @@ -33,7 +33,6 @@ import javax.inject.Inject; -import com.cloud.upgrade.dao.Upgrade42020to42030; import com.cloud.utils.FileUtil; import org.apache.cloudstack.utils.CloudStackVersion; import org.apache.commons.lang3.StringUtils; @@ -90,6 +89,8 @@ import com.cloud.upgrade.dao.Upgrade41900to41910; import com.cloud.upgrade.dao.Upgrade41910to42000; import com.cloud.upgrade.dao.Upgrade42000to42010; +import com.cloud.upgrade.dao.Upgrade42020to42030; +import com.cloud.upgrade.dao.Upgrade42030to42040; import com.cloud.upgrade.dao.Upgrade420to421; import com.cloud.upgrade.dao.Upgrade421to430; import com.cloud.upgrade.dao.Upgrade430to440; @@ -238,6 +239,7 @@ public DatabaseUpgradeChecker() { .next("4.19.1.0", new Upgrade41910to42000()) .next("4.20.0.0", new Upgrade42000to42010()) .next("4.20.2.0", new Upgrade42020to42030()) + .next("4.20.3.0", new Upgrade42030to42040()) .build(); } diff --git a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42030to42040.java b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42030to42040.java new file mode 100644 index 000000000000..e576f40c160d --- /dev/null +++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42030to42040.java @@ -0,0 +1,60 @@ +// 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.dao; + +import java.io.InputStream; +import java.sql.Connection; +import java.util.ArrayList; +import java.util.List; + +import com.cloud.utils.exception.CloudRuntimeException; + +public class Upgrade42030to42040 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate { + + @Override + public String[] getUpgradableVersionRange() { + return new String[]{"4.20.3.0", "4.20.4.0"}; + } + + @Override + public String getUpgradedVersion() { + return "4.20.4.0"; + } + + @Override + public boolean supportsRollingUpgrade() { + return false; + } + + @Override + public InputStream[] getPrepareScripts() { + return null; + } + + @Override + public void performDataMigration(Connection conn) { + final List indexList = new ArrayList(); + logger.debug("Dropping index vm_instance_id from usage_vm_instance table if it exists"); + indexList.add("vm_instance_id"); + DbUpgradeUtils.dropKeysIfExist(conn, "cloud_usage.usage_vm_instance", indexList, false); + } + + @Override + public InputStream[] getCleanupScripts() { + return null; + } +} diff --git a/setup/db/create-schema-premium.sql b/setup/db/create-schema-premium.sql index ae4eddf1bd3d..50c25965e8e9 100644 --- a/setup/db/create-schema-premium.sql +++ b/setup/db/create-schema-premium.sql @@ -70,8 +70,7 @@ CREATE TABLE `cloud_usage`.`usage_vm_instance` ( `template_id` bigint unsigned NOT NULL, `hypervisor_type` varchar(255), `start_date` DATETIME NOT NULL, - `end_date` DATETIME NULL, - UNIQUE KEY (`vm_instance_id`, `usage_type`, `start_date`) + `end_date` DATETIME NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `cloud_usage`.`usage_vm_instance` ADD INDEX `i_usage_vm_instance__account_id`(`account_id`); From 37ffb5abfa47b6bef539c856db94dcbc5beff810 Mon Sep 17 00:00:00 2001 From: Abhisar Sinha <63767682+abh1sar@users.noreply.github.com> Date: Tue, 14 Apr 2026 16:02:14 +0530 Subject: [PATCH 2/3] fix precommit --- .../main/java/com/cloud/upgrade/dao/Upgrade42030to42040.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42030to42040.java b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42030to42040.java index e576f40c160d..90f69a87cb03 100644 --- a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42030to42040.java +++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42030to42040.java @@ -21,8 +21,6 @@ import java.util.ArrayList; import java.util.List; -import com.cloud.utils.exception.CloudRuntimeException; - public class Upgrade42030to42040 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate { @Override From e5f59a30fffe930257995462f311beb3b4419d6c Mon Sep 17 00:00:00 2001 From: Abhisar Sinha <63767682+abh1sar@users.noreply.github.com> Date: Wed, 15 Apr 2026 19:28:32 +0530 Subject: [PATCH 3/3] revert changes to create-schema-premium.sql --- setup/db/create-schema-premium.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup/db/create-schema-premium.sql b/setup/db/create-schema-premium.sql index 50c25965e8e9..ae4eddf1bd3d 100644 --- a/setup/db/create-schema-premium.sql +++ b/setup/db/create-schema-premium.sql @@ -70,7 +70,8 @@ CREATE TABLE `cloud_usage`.`usage_vm_instance` ( `template_id` bigint unsigned NOT NULL, `hypervisor_type` varchar(255), `start_date` DATETIME NOT NULL, - `end_date` DATETIME NULL + `end_date` DATETIME NULL, + UNIQUE KEY (`vm_instance_id`, `usage_type`, `start_date`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `cloud_usage`.`usage_vm_instance` ADD INDEX `i_usage_vm_instance__account_id`(`account_id`);