Skip to content

Commit 98e6dc5

Browse files
committed
Optimize DbUpgrade files
1 parent 01b1460 commit 98e6dc5

File tree

3 files changed

+44
-58
lines changed

3 files changed

+44
-58
lines changed

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// under the License.
1717
package com.cloud.upgrade.dao;
1818

19+
import com.cloud.utils.exception.CloudRuntimeException;
20+
1921
import java.io.InputStream;
2022
import java.sql.Connection;
2123

@@ -24,20 +26,43 @@ public interface DbUpgrade {
2426

2527
String getUpgradedVersion();
2628

27-
boolean supportsRollingUpgrade();
29+
default boolean supportsRollingUpgrade() {
30+
return false;
31+
}
2832

2933
/**
3034
* @return the script to prepare the database schema for the
3135
* data migration step.
3236
*/
33-
InputStream[] getPrepareScripts();
37+
default InputStream[] getPrepareScripts() {
38+
String fromVersion = getUpgradableVersionRange()[0];
39+
String toVersion = getUpgradableVersionRange()[1];
40+
final String scriptFile = String.format("META-INF/db/schema-%sto%s.sql", fromVersion.replace(".", ""), toVersion.replace(".", ""));
41+
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
42+
if (script == null) {
43+
throw new CloudRuntimeException("Unable to find " + scriptFile);
44+
}
45+
46+
return new InputStream[]{script};
47+
}
3448

3549
/**
3650
* Performs the actual data migration.
3751
*/
38-
void performDataMigration(Connection conn);
52+
default void performDataMigration(Connection conn) {
53+
}
3954

40-
InputStream[] getCleanupScripts();
55+
default InputStream[] getCleanupScripts() {
56+
String fromVersion = getUpgradableVersionRange()[0];
57+
String toVersion = getUpgradableVersionRange()[1];
58+
final String scriptFile = String.format("META-INF/db/schema-%sto%s-cleanup.sql", fromVersion.replace(".", ""), toVersion.replace(".", ""));
59+
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
60+
if (script == null) {
61+
throw new CloudRuntimeException("Unable to find " + scriptFile);
62+
}
63+
64+
return new InputStream[]{script};
65+
}
4166

4267
default boolean refreshPoolConnectionsAfterUpgrade() {
4368
return false;

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,23 @@
1717

1818
package com.cloud.upgrade.dao;
1919

20+
import com.cloud.upgrade.SystemVmTemplateRegistration;
21+
import com.cloud.utils.exception.CloudRuntimeException;
22+
import org.apache.logging.log4j.LogManager;
23+
import org.apache.logging.log4j.Logger;
24+
2025
import java.sql.Connection;
2126

2227
public interface DbUpgradeSystemVmTemplate {
2328

24-
void updateSystemVmTemplates(Connection conn);
29+
default void updateSystemVmTemplates(Connection conn) {
30+
Logger logger = LogManager.getLogger(getClass());
31+
logger.debug("Updating System Vm template IDs");
32+
try {
33+
SystemVmTemplateRegistration systemVmTemplateRegistration = new SystemVmTemplateRegistration("");
34+
systemVmTemplateRegistration.updateSystemVmTemplates(conn);
35+
} catch (Exception e) {
36+
throw new CloudRuntimeException("Failed to find / register SystemVM template(s)");
37+
}
38+
}
2539
}

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

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,7 @@
1616
// under the License.
1717
package com.cloud.upgrade.dao;
1818

19-
import com.cloud.upgrade.SystemVmTemplateRegistration;
20-
import com.cloud.utils.exception.CloudRuntimeException;
21-
22-
import java.io.InputStream;
23-
import java.sql.Connection;
24-
2519
public class Upgrade42100to42200 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate {
26-
private SystemVmTemplateRegistration systemVmTemplateRegistration;
2720

2821
@Override
2922
public String[] getUpgradableVersionRange() {
@@ -34,50 +27,4 @@ public String[] getUpgradableVersionRange() {
3427
public String getUpgradedVersion() {
3528
return "4.22.0.0";
3629
}
37-
38-
@Override
39-
public boolean supportsRollingUpgrade() {
40-
return false;
41-
}
42-
43-
@Override
44-
public InputStream[] getPrepareScripts() {
45-
final String scriptFile = "META-INF/db/schema-42100to42200.sql";
46-
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
47-
if (script == null) {
48-
throw new CloudRuntimeException("Unable to find " + scriptFile);
49-
}
50-
51-
return new InputStream[]{script};
52-
}
53-
54-
@Override
55-
public void performDataMigration(Connection conn) {
56-
}
57-
58-
@Override
59-
public InputStream[] getCleanupScripts() {
60-
final String scriptFile = "META-INF/db/schema-42100to42200-cleanup.sql";
61-
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
62-
if (script == null) {
63-
throw new CloudRuntimeException("Unable to find " + scriptFile);
64-
}
65-
66-
return new InputStream[]{script};
67-
}
68-
69-
private void initSystemVmTemplateRegistration() {
70-
systemVmTemplateRegistration = new SystemVmTemplateRegistration("");
71-
}
72-
73-
@Override
74-
public void updateSystemVmTemplates(Connection conn) {
75-
logger.debug("Updating System Vm template IDs");
76-
initSystemVmTemplateRegistration();
77-
try {
78-
systemVmTemplateRegistration.updateSystemVmTemplates(conn);
79-
} catch (Exception e) {
80-
throw new CloudRuntimeException("Failed to find / register SystemVM template(s)");
81-
}
82-
}
8330
}

0 commit comments

Comments
 (0)