From d1458849d3e2282f32dc8752b26d41da5a9f13c1 Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Tue, 19 Aug 2025 11:01:30 +0200 Subject: [PATCH 1/4] schema: Add upgrade path from 4.21.0.0 to 4.22.0.0 --- .../cloud/upgrade/DatabaseUpgradeChecker.java | 2 + .../upgrade/dao/Upgrade42100to42200.java | 83 +++++++++++++++++++ .../db/schema-42100to42200-cleanup.sql | 21 +++++ .../META-INF/db/schema-42100to42200.sql | 21 +++++ 4 files changed, 127 insertions(+) create mode 100644 engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42100to42200.java create mode 100644 engine/schema/src/main/resources/META-INF/db/schema-42100to42200-cleanup.sql create mode 100644 engine/schema/src/main/resources/META-INF/db/schema-42100to42200.sql 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 827b8f547574..c211b3e97280 100644 --- a/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java +++ b/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java @@ -90,6 +90,7 @@ import com.cloud.upgrade.dao.Upgrade41910to42000; import com.cloud.upgrade.dao.Upgrade42000to42010; import com.cloud.upgrade.dao.Upgrade42010to42100; +import com.cloud.upgrade.dao.Upgrade42100to42200; import com.cloud.upgrade.dao.Upgrade420to421; import com.cloud.upgrade.dao.Upgrade421to430; import com.cloud.upgrade.dao.Upgrade430to440; @@ -234,6 +235,7 @@ public DatabaseUpgradeChecker() { .next("4.19.1.0", new Upgrade41910to42000()) .next("4.20.0.0", new Upgrade42000to42010()) .next("4.20.1.0", new Upgrade42010to42100()) + .next("4.21.0.0", new Upgrade42100to42200()) .build(); } diff --git a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42100to42200.java b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42100to42200.java new file mode 100644 index 000000000000..3aeb1f33c84a --- /dev/null +++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42100to42200.java @@ -0,0 +1,83 @@ +// 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 com.cloud.upgrade.SystemVmTemplateRegistration; +import com.cloud.utils.exception.CloudRuntimeException; + +import java.io.InputStream; +import java.sql.Connection; + +public class Upgrade42100to42200 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate { + private SystemVmTemplateRegistration systemVmTemplateRegistration; + + @Override + public String[] getUpgradableVersionRange() { + return new String[]{"4.21.0.0", "4.22.0.0"}; + } + + @Override + public String getUpgradedVersion() { + return "4.22.0.0"; + } + + @Override + public boolean supportsRollingUpgrade() { + return false; + } + + @Override + public InputStream[] getPrepareScripts() { + final String scriptFile = "META-INF/db/schema-42100to42200.sql"; + final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile); + if (script == null) { + throw new CloudRuntimeException("Unable to find " + scriptFile); + } + + return new InputStream[]{script}; + } + + @Override + public void performDataMigration(Connection conn) { + } + + @Override + public InputStream[] getCleanupScripts() { + final String scriptFile = "META-INF/db/schema-42100to42200-cleanup.sql"; + final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile); + if (script == null) { + throw new CloudRuntimeException("Unable to find " + scriptFile); + } + + return new InputStream[]{script}; + } + + private void initSystemVmTemplateRegistration() { + systemVmTemplateRegistration = new SystemVmTemplateRegistration(""); + } + + @Override + public void updateSystemVmTemplates(Connection conn) { + logger.debug("Updating System Vm template IDs"); + initSystemVmTemplateRegistration(); + try { + systemVmTemplateRegistration.updateSystemVmTemplates(conn); + } catch (Exception e) { + throw new CloudRuntimeException("Failed to find / register SystemVM template(s)"); + } + } +} diff --git a/engine/schema/src/main/resources/META-INF/db/schema-42100to42200-cleanup.sql b/engine/schema/src/main/resources/META-INF/db/schema-42100to42200-cleanup.sql new file mode 100644 index 000000000000..ff3f41b0730f --- /dev/null +++ b/engine/schema/src/main/resources/META-INF/db/schema-42100to42200-cleanup.sql @@ -0,0 +1,21 @@ +-- 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. + +--; +-- Schema upgrade cleanup from 4.21.0.0 to 4.22.0.0 +--; + diff --git a/engine/schema/src/main/resources/META-INF/db/schema-42100to42200.sql b/engine/schema/src/main/resources/META-INF/db/schema-42100to42200.sql new file mode 100644 index 000000000000..abc62eb0aab4 --- /dev/null +++ b/engine/schema/src/main/resources/META-INF/db/schema-42100to42200.sql @@ -0,0 +1,21 @@ +-- 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. + +--; +-- Schema upgrade from 4.21.0.0 to 4.22.0.0 +--; + From cc8d9b2f655c220ad3c8e480d12e460dbeccbbab Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Tue, 19 Aug 2025 11:30:18 +0200 Subject: [PATCH 2/4] Update engine/schema/src/main/resources/META-INF/db/schema-42100to42200-cleanup.sql --- .../main/resources/META-INF/db/schema-42100to42200-cleanup.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/engine/schema/src/main/resources/META-INF/db/schema-42100to42200-cleanup.sql b/engine/schema/src/main/resources/META-INF/db/schema-42100to42200-cleanup.sql index ff3f41b0730f..0546613dfa34 100644 --- a/engine/schema/src/main/resources/META-INF/db/schema-42100to42200-cleanup.sql +++ b/engine/schema/src/main/resources/META-INF/db/schema-42100to42200-cleanup.sql @@ -18,4 +18,3 @@ --; -- Schema upgrade cleanup from 4.21.0.0 to 4.22.0.0 --; - From 01b1460adca55e9db4a4ff9c3cbcc196289083ab Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Tue, 19 Aug 2025 11:30:24 +0200 Subject: [PATCH 3/4] Update engine/schema/src/main/resources/META-INF/db/schema-42100to42200.sql --- .../src/main/resources/META-INF/db/schema-42100to42200.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/engine/schema/src/main/resources/META-INF/db/schema-42100to42200.sql b/engine/schema/src/main/resources/META-INF/db/schema-42100to42200.sql index abc62eb0aab4..0f4e8b6f2a29 100644 --- a/engine/schema/src/main/resources/META-INF/db/schema-42100to42200.sql +++ b/engine/schema/src/main/resources/META-INF/db/schema-42100to42200.sql @@ -18,4 +18,3 @@ --; -- Schema upgrade from 4.21.0.0 to 4.22.0.0 --; - From 98e6dc51329d2b680a5c662941e54e0f7250e541 Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Thu, 28 Aug 2025 13:14:37 +0200 Subject: [PATCH 4/4] Optimize DbUpgrade files --- .../java/com/cloud/upgrade/dao/DbUpgrade.java | 33 ++++++++++-- .../dao/DbUpgradeSystemVmTemplate.java | 16 +++++- .../upgrade/dao/Upgrade42100to42200.java | 53 ------------------- 3 files changed, 44 insertions(+), 58 deletions(-) diff --git a/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgrade.java b/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgrade.java index 6001f940b2cf..fa0d0506ac13 100644 --- a/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgrade.java +++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgrade.java @@ -16,6 +16,8 @@ // under the License. package com.cloud.upgrade.dao; +import com.cloud.utils.exception.CloudRuntimeException; + import java.io.InputStream; import java.sql.Connection; @@ -24,20 +26,43 @@ public interface DbUpgrade { String getUpgradedVersion(); - boolean supportsRollingUpgrade(); + default boolean supportsRollingUpgrade() { + return false; + } /** * @return the script to prepare the database schema for the * data migration step. */ - InputStream[] getPrepareScripts(); + default InputStream[] getPrepareScripts() { + String fromVersion = getUpgradableVersionRange()[0]; + String toVersion = getUpgradableVersionRange()[1]; + final String scriptFile = String.format("META-INF/db/schema-%sto%s.sql", fromVersion.replace(".", ""), toVersion.replace(".", "")); + final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile); + if (script == null) { + throw new CloudRuntimeException("Unable to find " + scriptFile); + } + + return new InputStream[]{script}; + } /** * Performs the actual data migration. */ - void performDataMigration(Connection conn); + default void performDataMigration(Connection conn) { + } - InputStream[] getCleanupScripts(); + default InputStream[] getCleanupScripts() { + String fromVersion = getUpgradableVersionRange()[0]; + String toVersion = getUpgradableVersionRange()[1]; + final String scriptFile = String.format("META-INF/db/schema-%sto%s-cleanup.sql", fromVersion.replace(".", ""), toVersion.replace(".", "")); + final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile); + if (script == null) { + throw new CloudRuntimeException("Unable to find " + scriptFile); + } + + return new InputStream[]{script}; + } default boolean refreshPoolConnectionsAfterUpgrade() { return false; diff --git a/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeSystemVmTemplate.java b/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeSystemVmTemplate.java index 4211898adc7d..a8d2436672e7 100644 --- a/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeSystemVmTemplate.java +++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeSystemVmTemplate.java @@ -17,9 +17,23 @@ package com.cloud.upgrade.dao; +import com.cloud.upgrade.SystemVmTemplateRegistration; +import com.cloud.utils.exception.CloudRuntimeException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import java.sql.Connection; public interface DbUpgradeSystemVmTemplate { - void updateSystemVmTemplates(Connection conn); + default void updateSystemVmTemplates(Connection conn) { + Logger logger = LogManager.getLogger(getClass()); + logger.debug("Updating System Vm template IDs"); + try { + SystemVmTemplateRegistration systemVmTemplateRegistration = new SystemVmTemplateRegistration(""); + systemVmTemplateRegistration.updateSystemVmTemplates(conn); + } catch (Exception e) { + throw new CloudRuntimeException("Failed to find / register SystemVM template(s)"); + } + } } diff --git a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42100to42200.java b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42100to42200.java index 3aeb1f33c84a..c2cfd02c15cf 100644 --- a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42100to42200.java +++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42100to42200.java @@ -16,14 +16,7 @@ // under the License. package com.cloud.upgrade.dao; -import com.cloud.upgrade.SystemVmTemplateRegistration; -import com.cloud.utils.exception.CloudRuntimeException; - -import java.io.InputStream; -import java.sql.Connection; - public class Upgrade42100to42200 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate { - private SystemVmTemplateRegistration systemVmTemplateRegistration; @Override public String[] getUpgradableVersionRange() { @@ -34,50 +27,4 @@ public String[] getUpgradableVersionRange() { public String getUpgradedVersion() { return "4.22.0.0"; } - - @Override - public boolean supportsRollingUpgrade() { - return false; - } - - @Override - public InputStream[] getPrepareScripts() { - final String scriptFile = "META-INF/db/schema-42100to42200.sql"; - final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile); - if (script == null) { - throw new CloudRuntimeException("Unable to find " + scriptFile); - } - - return new InputStream[]{script}; - } - - @Override - public void performDataMigration(Connection conn) { - } - - @Override - public InputStream[] getCleanupScripts() { - final String scriptFile = "META-INF/db/schema-42100to42200-cleanup.sql"; - final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile); - if (script == null) { - throw new CloudRuntimeException("Unable to find " + scriptFile); - } - - return new InputStream[]{script}; - } - - private void initSystemVmTemplateRegistration() { - systemVmTemplateRegistration = new SystemVmTemplateRegistration(""); - } - - @Override - public void updateSystemVmTemplates(Connection conn) { - logger.debug("Updating System Vm template IDs"); - initSystemVmTemplateRegistration(); - try { - systemVmTemplateRegistration.updateSystemVmTemplates(conn); - } catch (Exception e) { - throw new CloudRuntimeException("Failed to find / register SystemVM template(s)"); - } - } }