Skip to content

The Unified and Autonomous Development Platform

fix: Handle legacy database dialects during upgrades

Robin Shen19d0014cc077
diff --git a/server-core/src/main/java/io/onedev/server/commandhandler/Upgrade.java b/server-core/src/main/java/io/onedev/server/commandhandler/Upgrade.java
index bfffcc1e30..ffd59344be 100644
--- a/server-core/src/main/java/io/onedev/server/commandhandler/Upgrade.java
+++ b/server-core/src/main/java/io/onedev/server/commandhandler/Upgrade.java
@@ -10,9 +10,11 @@ import static java.lang.System.lineSeparator;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.io.FileUtils.writeStringToFile;
import static org.hibernate.cfg.AvailableSettings.DIALECT;
+import static org.hibernate.cfg.AvailableSettings.JAKARTA_JDBC_URL;
import java.io.File;
import java.io.IOException;
+import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
@@ -1060,6 +1062,7 @@ public class Upgrade extends AbstractPlugin {
File hibernatePropsFile = new File(upgradeDir, "conf/hibernate.properties");
String hibernateProps = FileUtils.readFileToString(hibernatePropsFile, UTF_8);
+ hibernateProps = migrateHibernateDialects(hibernateProps);
hibernateProps = Strings.CS.replace(hibernateProps, "hibernate.hikari.autoCommit=false",
"hibernate.hikari.autoCommit=true");
hibernateProps = Strings.CS.replace(hibernateProps, "javax.persistence.validation.mode",
@@ -1209,6 +1212,33 @@ public class Upgrade extends AbstractPlugin {
restoreExecutables(upgradeDir);
}
+ static String migrateHibernateDialects(String hibernateProps) throws IOException {
+ var properties = new Properties();
+ properties.load(new StringReader(hibernateProps));
+ var dialectMatcher = Pattern.compile("(?m)^(\\h*([#!]\\h*)?hibernate\\.dialect\\h*[=:]\\h*)"
+ + "org\\.hibernate\\.dialect\\.MySQL5InnoDBDialect(?=\\h*$)").matcher(hibernateProps);
+ var migrated = new StringBuilder();
+ while (dialectMatcher.find()) {
+ var connectionProperties = properties;
+ if (dialectMatcher.group(2) != null) {
+ // Commented examples have their own connection settings, separated by blank lines.
+ var example = hibernateProps.substring(dialectMatcher.start()).split("\\r?\\n[ \\t]*\\r?\\n", 2)[0];
+ connectionProperties = new Properties();
+ var urlMatcher = Pattern.compile("(?m)^\\h*[#!]\\h*((?:hibernate\\.connection|jakarta\\.persistence\\.jdbc)"
+ + "\\.url\\h*[=:][^\\r\\n]*)$").matcher(example);
+ while (urlMatcher.find())
+ connectionProperties.load(new StringReader(urlMatcher.group(1)));
+ }
+ // Persist the file's configuration, independently of environment overrides.
+ var url = connectionProperties.getProperty(JAKARTA_JDBC_URL,
+ connectionProperties.getProperty("hibernate.connection.url", ""));
+ var dialect = url.startsWith("jdbc:mariadb:")
+ ? "org.hibernate.dialect.MariaDBDialect" : "org.hibernate.dialect.MySQLDialect";
+ dialectMatcher.appendReplacement(migrated, Matcher.quoteReplacement(dialectMatcher.group(1) + dialect));
+ }
+ return dialectMatcher.appendTail(migrated).toString();
+ }
+
static String migrateWrapperConf(String wrapperConf) {
var mainClassPattern = Pattern.compile(
"(?m)^(\\h*wrapper\\.java\\.mainclass\\h*=\\h*)([\\w.]+\\.WrapperSimpleApp)(\\h*(?:#.*)?)$");
diff --git a/server-core/src/main/java/io/onedev/server/persistence/HibernateConfig.java b/server-core/src/main/java/io/onedev/server/persistence/HibernateConfig.java
index b32b64eb0a..16fc8ce3ec 100644
--- a/server-core/src/main/java/io/onedev/server/persistence/HibernateConfig.java
+++ b/server-core/src/main/java/io/onedev/server/persistence/HibernateConfig.java
@@ -47,8 +47,6 @@ public class HibernateConfig extends Properties {
if (value != null)
putIfAbsent(current, value);
});
- if ("org.hibernate.dialect.MySQL5InnoDBDialect".equals(getProperty(DIALECT)))
- put(DIALECT, "org.hibernate.dialect.MySQLDialect");
String value = System.getenv(ENV_PASS_FILE);
if (value != null) {
@@ -72,6 +70,11 @@ public class HibernateConfig extends Properties {
String url = getProperty(JAKARTA_JDBC_URL);
url = Strings.CS.replace(url, "${installDir}", installDir.getAbsolutePath());
setProperty(JAKARTA_JDBC_URL, url);
+ // Normalize the effective dialect after applying all environment overrides.
+ if ("org.hibernate.dialect.MySQL5InnoDBDialect".equals(getProperty(DIALECT))) {
+ setProperty(DIALECT, url.startsWith("jdbc:mariadb:")
+ ? "org.hibernate.dialect.MariaDBDialect" : "org.hibernate.dialect.MySQLDialect");
+ }
}
public String getDialect() {
diff --git a/server-core/src/test/java/io/onedev/server/commandhandler/UpgradeHibernateConfigTest.java b/server-core/src/test/java/io/onedev/server/commandhandler/UpgradeHibernateConfigTest.java
new file mode 100644
index 0000000000..5eb6455ec9
--- /dev/null
+++ b/server-core/src/test/java/io/onedev/server/commandhandler/UpgradeHibernateConfigTest.java
@@ -0,0 +1,62 @@
+package io.onedev.server.commandhandler;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+class UpgradeHibernateConfigTest {
+
+ private static final String LEGACY_DIALECT = "org.hibernate.dialect.MySQL5InnoDBDialect";
+
+ @Test
+ void migratesSavedDialectUsingSavedConnectionUrl() throws Exception {
+ for (var database : new String[] { "mysql", "mariadb" }) {
+ var config = "hibernate.connection.url=jdbc:" + database + "://localhost/onedev\n\n"
+ + " hibernate.dialect : " + LEGACY_DIALECT + " \n"
+ + "hibernate.connection.password=keep$This\\\\Password\n";
+ var dialect = database.equals("mariadb") ? "MariaDBDialect" : "MySQLDialect";
+ assertEquals(config.replace(LEGACY_DIALECT, "org.hibernate.dialect." + dialect),
+ Upgrade.migrateHibernateDialects(config));
+ }
+ }
+
+ @Test
+ void prefersJakartaConnectionUrl() throws Exception {
+ var config = "hibernate.dialect=" + LEGACY_DIALECT + "\n"
+ + "hibernate.connection.url=jdbc:mysql://localhost/onedev\n"
+ + "jakarta.persistence.jdbc.url=jdbc:mariadb://localhost/onedev\n";
+ assertEquals(config.replace(LEGACY_DIALECT, "org.hibernate.dialect.MariaDBDialect"),
+ Upgrade.migrateHibernateDialects(config));
+ }
+
+ @Test
+ void migratesEachCommentedExampleIndependently() throws Exception {
+ for (var newline : new String[] { "\n", "\r\n" }) {
+ var mysql = "#hibernate.dialect=" + LEGACY_DIALECT + newline
+ + "#hibernate.connection.driver_class=com.mysql.cj.jdbc.Driver" + newline
+ + "#hibernate.connection.url=jdbc:mysql://localhost/onedev" + newline;
+ var mariadb = "#hibernate.dialect=" + LEGACY_DIALECT + newline
+ + "#hibernate.connection.driver_class=org.mariadb.jdbc.Driver" + newline
+ + "#hibernate.connection.url=jdbc:mariadb://localhost/onedev" + newline
+ + "# Notes can contain literal \\uXXXX without being parsed as properties" + newline;
+ var active = "hibernate.dialect=org.hibernate.dialect.HSQLDialect" + newline
+ + "hibernate.connection.url=jdbc:hsqldb:file:internaldb/onedev" + newline + newline;
+ var migrated = Upgrade.migrateHibernateDialects(active + mysql + newline + mariadb);
+ assertEquals(active + mysql.replace(LEGACY_DIALECT, "org.hibernate.dialect.MySQLDialect")
+ + newline + mariadb.replace(LEGACY_DIALECT, "org.hibernate.dialect.MariaDBDialect"), migrated);
+ assertEquals(migrated, Upgrade.migrateHibernateDialects(migrated));
+ }
+ }
+
+ @Test
+ void leavesModernCustomAndUnrelatedValuesUnchanged() throws Exception {
+ for (var dialect : new String[] { "org.hibernate.dialect.MySQLDialect",
+ "org.hibernate.dialect.MariaDBDialect", "example.CustomDialect" }) {
+ var config = "hibernate.dialect=" + dialect + "\n"
+ + "hibernate.connection.url=jdbc:mariadb://localhost/onedev\n"
+ + "hibernate.connection.password=" + LEGACY_DIALECT + "\n"
+ + "# Previous dialect: " + LEGACY_DIALECT + "\n";
+ assertEquals(config, Upgrade.migrateHibernateDialects(config));
+ }
+ }
+}
diff --git a/server-product/helm/templates/_helpers.tpl b/server-product/helm/templates/_helpers.tpl
index 160ff4002a..b94acb3886 100644
--- a/server-product/helm/templates/_helpers.tpl
+++ b/server-product/helm/templates/_helpers.tpl
@@ -125,9 +125,9 @@ Set dilect and driver env variables based database type
*/}}
{{- define "setDatabaseEnvVars" -}}
{{- $dbType := .Values.database.type -}}
-{{- $dbTypeMap := dict "mysql" (dict "dialect" "org.hibernate.dialect.MySQL5InnoDBDialect" "driver" "com.mysql.cj.jdbc.Driver")
+{{- $dbTypeMap := dict "mysql" (dict "dialect" "org.hibernate.dialect.MySQLDialect" "driver" "com.mysql.cj.jdbc.Driver")
"postgresql" (dict "dialect" "io.onedev.server.persistence.PostgreSQLDialect" "driver" "org.postgresql.Driver")
- "mariadb" (dict "dialect" "org.hibernate.dialect.MySQL5InnoDBDialect" "driver" "org.mariadb.jdbc.Driver")
+ "mariadb" (dict "dialect" "org.hibernate.dialect.MariaDBDialect" "driver" "org.mariadb.jdbc.Driver")
-}}
{{- with index $dbTypeMap $dbType }}
- name: hibernate_dialect
diff --git a/server-product/system/conf/hibernate.properties b/server-product/system/conf/hibernate.properties
index 079fe86681..3d1cb3e2d5 100644
--- a/server-product/system/conf/hibernate.properties
+++ b/server-product/system/conf/hibernate.properties
@@ -20,7 +20,7 @@ hibernate.connection.password=
# the database created in step 2
#----------------------------------------------------------------------------------
-#hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
+#hibernate.dialect=org.hibernate.dialect.MySQLDialect
#hibernate.connection.driver_class=com.mysql.cj.jdbc.Driver
#hibernate.connection.url=jdbc:mysql://localhost:3306/onedev?serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false&disableMariaDbDriver=true
#hibernate.connection.username=root
@@ -37,7 +37,7 @@ hibernate.connection.password=
# the database created in step 2
#----------------------------------------------------------------------------------
-#hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
+#hibernate.dialect=org.hibernate.dialect.MariaDBDialect
#hibernate.connection.driver_class=org.mariadb.jdbc.Driver
#hibernate.connection.url=jdbc:mariadb://localhost:3306/onedev
#hibernate.connection.username=root