Skip to content

Commit

Permalink
fix JENKINS-68826: Exception creating MySQL database dao after upgrad…
Browse files Browse the repository at this point in the history
…ing pipeline-maven-plugin to v3.11.0 (#464)
  • Loading branch information
bguerin authored Jun 26, 2022
1 parent 09ec9ec commit 89a7dce
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 55 deletions.
23 changes: 23 additions & 0 deletions jenkins-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@
<artifactId>jansi</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.35</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -573,6 +578,24 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.17.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>1.17.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.17.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ public synchronized PipelineMavenPluginDao getDao() {
// no tuning recommendations found for postgresql
} else if (jdbcUrl.startsWith("jdbc:h2")) {
// dsConfig.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); don't specify the datasource due to a classloading issue
} else {
// unsupported config
}
if (StringUtils.isNotBlank(properties)) {
p.load(new StringReader(properties));
Expand Down Expand Up @@ -543,6 +545,7 @@ public FormValidation doValidateJdbcConnection(
}
}
switch (metaData.getDatabaseMajorVersion()) {
case 14:
case 13:
case 12:
case 11:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ protected synchronized void initializeDatabase() {
LOGGER.log(Level.FINER, "Execute command {0}", sqlCommand);
stmt.execute(sqlCommand);
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, "Failed to run SQL {0} from script {1}: {2}", new Object[] {sqlCommand, sqlScriptPath, e.getMessage()});
handleDatabaseInitialisationException(e);
}
}
Expand Down Expand Up @@ -664,12 +665,12 @@ protected SortedSet<String> listDownstreamPipelinesBasedOnMavenDependencies(@Non
"inner join MAVEN_DEPENDENCY on (MAVEN_DEPENDENCY.artifact_id = MAVEN_ARTIFACT.id and MAVEN_DEPENDENCY.ignore_upstream_triggers = false) \n" +
"inner join JENKINS_BUILD as downstream_build on MAVEN_DEPENDENCY.build_id = downstream_build.id \n" +
"inner join JENKINS_JOB as downstream_job on (downstream_build.number = downstream_job.last_successful_build_number and downstream_build.job_id = downstream_job.id) \n" +
"where MAVEN_ARTIFACT.group_id = ?1 " +
"and MAVEN_ARTIFACT.artifact_id = ?2 " +
"and MAVEN_ARTIFACT.version = ?3 " +
"and MAVEN_ARTIFACT.type = ?4 " +
"and (MAVEN_ARTIFACT.classifier = ?5 or (MAVEN_ARTIFACT.classifier is null and ?5 is null)) " +
"and downstream_job.jenkins_master_id = ?6";
"where MAVEN_ARTIFACT.group_id = ? " +
"and MAVEN_ARTIFACT.artifact_id = ? " +
"and MAVEN_ARTIFACT.version = ? " +
"and MAVEN_ARTIFACT.type = ? " +
"and (MAVEN_ARTIFACT.classifier = ? or (MAVEN_ARTIFACT.classifier is null and ? is null)) " +
"and downstream_job.jenkins_master_id = ?";

SortedSet<String> downstreamJobsFullNames = new TreeSet<>();

Expand All @@ -680,7 +681,8 @@ protected SortedSet<String> listDownstreamPipelinesBasedOnMavenDependencies(@Non
stmt.setString(3, version);
stmt.setString(4, type);
stmt.setString(5, classifier);
stmt.setLong(6, getJenkinsMasterPrimaryKey(cnn));
stmt.setString(6, classifier);
stmt.setLong(7, getJenkinsMasterPrimaryKey(cnn));
try (ResultSet rst = stmt.executeQuery()) {
while (rst.next()) {
downstreamJobsFullNames.add(rst.getString(1));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DROP INDEX IDX_MAVEN_ARTIFACT;
DROP INDEX IDX_MAVEN_ARTIFACT on MAVEN_ARTIFACT;
CREATE INDEX IDX_MAVEN_ARTIFACT on MAVEN_ARTIFACT (GROUP_ID, ARTIFACT_ID, VERSION);

CREATE INDEX IDX_GENERATED_ARTIFACT ON GENERATED_MAVEN_ARTIFACT(artifact_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,29 @@

package org.jenkinsci.plugins.pipeline.maven.dao;

import com.google.common.base.Preconditions;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;

import org.jenkinsci.plugins.pipeline.maven.db.migration.MigrationStep;
import org.junit.ClassRule;
import org.testcontainers.containers.MySQLContainer;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

/**
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
*/
public class PipelineMavenPluginMySqlDaoIT extends PipelineMavenPluginDaoAbstractTest {

@ClassRule
public static MySQLContainer<?> DB = new MySQLContainer<>(MySQLContainer.NAME);

@Override
public DataSource before_newDataSource() {
HikariConfig config = new HikariConfig();
String configurationFilePath = ".mysql_config";
InputStream propertiesInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(configurationFilePath);

Properties properties = new Properties();
if (propertiesInputStream == null) {
throw new IllegalArgumentException("Config file " + configurationFilePath + " not found in classpath");
} else {
try {
properties.load(propertiesInputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
config.setJdbcUrl(Preconditions.checkNotNull(properties.getProperty("jdbc.url")));
config.setUsername(Preconditions.checkNotNull(properties.getProperty("jdbc.username")));
config.setPassword(Preconditions.checkNotNull(properties.getProperty("jdbc.password")));
config.setDataSourceProperties(properties);
config.setJdbcUrl(DB.getJdbcUrl());
config.setUsername(DB.getUsername());
config.setPassword(DB.getPassword());
return new HikariDataSource(config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,32 @@

package org.jenkinsci.plugins.pipeline.maven.dao;

import com.google.common.base.Preconditions;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;

import org.jenkinsci.plugins.pipeline.maven.db.migration.MigrationStep;
import org.junit.ClassRule;
import org.testcontainers.containers.PostgreSQLContainer;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

/**
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
*/
public class PipelineMavenPluginPostgreSqlDaoIT extends PipelineMavenPluginDaoAbstractTest {

@ClassRule
public static PostgreSQLContainer<?> DB = new PostgreSQLContainer<>(PostgreSQLContainer.IMAGE);

@Override
public DataSource before_newDataSource() throws Exception {

Class.forName("org.postgresql.Driver");

HikariConfig config = new HikariConfig();
String configurationFilePath = ".postgresql_config";
InputStream propertiesInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(configurationFilePath);

Properties properties = new Properties();
if (propertiesInputStream == null) {
throw new IllegalArgumentException("Config file " + configurationFilePath + " not found in classpath");
} else {
try {
properties.load(propertiesInputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
config.setJdbcUrl(Preconditions.checkNotNull(properties.getProperty("jdbc.url")));
config.setUsername(Preconditions.checkNotNull(properties.getProperty("jdbc.username")));
config.setPassword(Preconditions.checkNotNull(properties.getProperty("jdbc.password")));
config.setJdbcUrl(DB.getJdbcUrl());
config.setUsername(DB.getUsername());
config.setPassword(DB.getPassword());
return new HikariDataSource(config);
}

Expand Down

0 comments on commit 89a7dce

Please sign in to comment.