Skip to content

Commit

Permalink
JENKINS-57543 Fix PostgreSQL database initialisation bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrille-leclerc committed May 26, 2019
1 parent 82a4a82 commit d001414
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public FormValidation doValidateJdbcConnection(
// * Amazon Aurora: "5.6.10"
// * MariaDB: "5.5.5-10.2.20-MariaDB", "5.5.5-10.3.11-MariaDB-1:10.3.11+maria~bionic"
String databaseVersionDescription = metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion();
String databaseRequirement = "MySQL Server version 5.7+ or Amazon Aurora MySQL 5.6+ or MariaDB 10.2+ or PostgreSQL 11+ or Amazon Aurora PostgreSQL 10.6+ is required";
String databaseRequirement = "MySQL Server version 5.7+ or Amazon Aurora MySQL 5.6+ or MariaDB 10.2+ or PostgreSQL 10+ is required";
if ("MySQL".equals(metaData.getDatabaseProductName())) {
@Nullable
String amazonAuroraVersion;
Expand Down Expand Up @@ -544,15 +544,9 @@ public FormValidation doValidateJdbcConnection(
}
switch (metaData.getDatabaseMajorVersion()) {
case 11:
case 10:
// OK
break;
case 10:
if (amazonAuroraVersion == null) {
return FormValidation.warning("Non tested PostgreSQL version " + metaData.getDatabaseProductVersion() + ". " + databaseRequirement);
} else {
// we have successfully tested on Amazon Aurora PostgreSQL 10.6
break;
}
default:
return FormValidation.warning("Non tested PostgreSQL version " + metaData.getDatabaseProductVersion() + ". " + databaseRequirement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import hudson.model.Result;
import hudson.model.Run;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.h2.api.ErrorCode;
import org.jenkinsci.plugins.pipeline.maven.MavenArtifact;
import org.jenkinsci.plugins.pipeline.maven.MavenDependency;
import org.jenkinsci.plugins.pipeline.maven.db.migration.MigrationStep;
import org.jenkinsci.plugins.pipeline.maven.util.ClassUtils;
import org.jenkinsci.plugins.pipeline.maven.util.RuntimeIoException;
import org.jenkinsci.plugins.pipeline.maven.util.RuntimeSqlException;
import org.jenkinsci.plugins.pipeline.maven.util.SqlUtils;

import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -565,6 +565,7 @@ protected int getSchemaVersion(Connection cnn) throws SQLException {
} else if ("42P01".equals(e.getSQLState())) {
// ignore PostgreSQL "ERROR: relation "..." does not exist
schemaVersion = 0;
cnn.rollback(); // postgresql requires to rollback after a 42P01 error
} else {
throw new RuntimeSqlException(e);
}
Expand Down Expand Up @@ -1048,6 +1049,8 @@ public synchronized Long getJenkinsMasterPrimaryKey(Connection cnn) throws SQLEx
stmt.setString(2, jenkinsMasterUrl);
stmt.execute();
this.jenkinsMasterPrimaryKey = getGeneratedPrimaryKey(stmt, "ID");
} finally {
cnn.commit();
}
} else { // FOUND IN DB, UPDATE IF NEEDED
if (!Objects.equals(jenkinsMasterUrl, jenkinsMasterUrlValueInDb)) {
Expand All @@ -1059,6 +1062,8 @@ public synchronized Long getJenkinsMasterPrimaryKey(Connection cnn) throws SQLEx
if (count != 1) {
LOGGER.warning("Updated more/less than 1 JENKINS_MASTER.URL=" + jenkinsMasterUrl + " for ID=" + this.jenkinsMasterPrimaryKey);
}
} finally {
cnn.commit();
}
}
}
Expand All @@ -1077,7 +1082,7 @@ protected MigrationStep.JenkinsDetails getJenkinsDetails() {
public String toPrettyString() {
List<String> prettyStrings = new ArrayList<>();
try (Connection cnn = ds.getConnection()) {
prettyStrings.add("jdbc.url: " + cnn.getMetaData().getURL());
prettyStrings.add("JDBC URL: " + cnn.getMetaData().getURL());
List<String> tables = Arrays.asList("JENKINS_MASTER", "MAVEN_ARTIFACT", "JENKINS_JOB", "JENKINS_BUILD",
"MAVEN_DEPENDENCY", "GENERATED_MAVEN_ARTIFACT", "MAVEN_PARENT_PROJECT", "JENKINS_BUILD_UPSTREAM_CAUSE");
for (String table : tables) {
Expand All @@ -1100,7 +1105,7 @@ public String toPrettyString() {
LOGGER.log(Level.WARNING, "SQLException getting a connection to " + ds, e);
}

StringBuilder result = new StringBuilder(getClass().getName() + " - " + getDatabaseDescription());
StringBuilder result = new StringBuilder(StringUtils.substringAfterLast(getClass().getName(), ".") + " - " + getDatabaseDescription());
for (String prettyString : prettyStrings) {
result.append("\r\n\t" + prettyString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ protected String getDatabaseDescription() {
} else {
LOGGER.log(Level.WARNING, "Exception checking Amazon Aurora version", e);
}
cnn.rollback();
}
}
return version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ THE SOFTWARE.
</f:entry>
<f:entry title="${%Database Configuration}">
<f:entry title="${%JDBC URL}" field="jdbcUrl"
description="JDBC URL. For production workloads, use MySQL (incl. Amazon Aurora for MySQL, MariaDB...). Note that PostgreSQL 11 support is experimental(2019-05). If empty, then a non production grade H2 embedded database will be used.">
description="JDBC URL. For production workloads, use MySQL (incl. Amazon Aurora for MySQL, MariaDB...). Note that PostgreSQL 10+ support is experimental(2019-05). If empty, then a non production grade H2 embedded database will be used.">
<f:textbox/>
</f:entry>
<f:entry title="${%JDBC Credentials}" field="jdbcCredentialsId">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
<div>
<p>Supported databases are H2 and MySQL(including MariaDB, Amazon Aurora DB for MySQL...). For production workloads, use MySQL / MariaDB. Note that PostgreSQL 11 support is experimental(2019-05). If empty, then a non production grade H2 embedded database will be used.</p>
<p>
Supported databases are H2 and MySQL(including MariaDB, Amazon Aurora DB for MySQL...).
For production workloads, use MySQL / MariaDB.
</p>
<p>
To use MySQL, install the <a href="https://wiki.jenkins.io/display/JENKINS/MySQL+Database+Plugin">MySQL Database Plugin</a> to load the MySQL driver.
</p>
<p>
Note that PostgreSQL 10+ support is experimental(2019-05). To use PostgreSQL, install the <a href="https://wiki.jenkins.io/display/JENKINS/PostgreSQL+API+Plugin">PostgreSQL Plugin</a> to load the PostgreSQL driver.
</p>
<p>
If empty, then a non production grade H2 embedded database will be used.
</p>
</div>
19 changes: 19 additions & 0 deletions jenkins-plugin/src/test/sql/drop-database-objects.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@


DROP VIEW "job_dependencies" ;
DROP VIEW "job_generated_artifacts";

DROP TABLE "generated_maven_artifact";
DROP TABLE "jenkins_build_upstream_cause";
DROP TABLE "maven_dependency";
DROP TABLE "maven_parent_project";

DROP TABLE "maven_artifact";

DROP TABLE "jenkins_build";
DROP TABLE "jenkins_job";
DROP TABLE "jenkins_master";

DROP TABLE "jenkins_build_result";
DROP TABLE "version";

0 comments on commit d001414

Please sign in to comment.