From 8085a3e25eae60b14e011086735a192b72d4977c 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] 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 | 1 - 3 files changed, 63 insertions(+), 2 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..b01ffa1906ac --- /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.vm_instance_id", 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..4d6d97e53d74 100644 --- a/setup/db/create-schema-premium.sql +++ b/setup/db/create-schema-premium.sql @@ -71,7 +71,6 @@ CREATE TABLE `cloud_usage`.`usage_vm_instance` ( `hypervisor_type` varchar(255), `start_date` DATETIME NOT 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`);