From 89a7dcec5d31710a6be1beb708b0e80c4be12f91 Mon Sep 17 00:00:00 2001 From: Benoit Guerin Date: Sun, 26 Jun 2022 15:16:43 +0200 Subject: [PATCH] fix JENKINS-68826: Exception creating MySQL database dao after upgrading pipeline-maven-plugin to v3.11.0 (#464) --- jenkins-plugin/pom.xml | 23 ++++++++++++ .../maven/GlobalPipelineMavenConfig.java | 3 ++ .../dao/AbstractPipelineMavenPluginDao.java | 16 +++++---- .../main/resources/sql/mysql/13_migration.sql | 2 +- .../dao/PipelineMavenPluginMySqlDaoIT.java | 36 +++++++------------ .../PipelineMavenPluginPostgreSqlDaoIT.java | 35 +++++++----------- 6 files changed, 60 insertions(+), 55 deletions(-) diff --git a/jenkins-plugin/pom.xml b/jenkins-plugin/pom.xml index 77db6868..141e69e4 100644 --- a/jenkins-plugin/pom.xml +++ b/jenkins-plugin/pom.xml @@ -195,6 +195,11 @@ jansi 2.4.0 + + org.slf4j + slf4j-api + 1.7.35 + @@ -573,6 +578,24 @@ mockito-core test + + org.testcontainers + testcontainers + 1.17.2 + test + + + org.testcontainers + mysql + 1.17.2 + test + + + org.testcontainers + postgresql + 1.17.2 + test + diff --git a/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/GlobalPipelineMavenConfig.java b/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/GlobalPipelineMavenConfig.java index aa30579f..68f4c0bb 100644 --- a/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/GlobalPipelineMavenConfig.java +++ b/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/GlobalPipelineMavenConfig.java @@ -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)); @@ -543,6 +545,7 @@ public FormValidation doValidateJdbcConnection( } } switch (metaData.getDatabaseMajorVersion()) { + case 14: case 13: case 12: case 11: diff --git a/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/dao/AbstractPipelineMavenPluginDao.java b/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/dao/AbstractPipelineMavenPluginDao.java index a5a92bec..658d92c7 100644 --- a/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/dao/AbstractPipelineMavenPluginDao.java +++ b/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/dao/AbstractPipelineMavenPluginDao.java @@ -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); } } @@ -664,12 +665,12 @@ protected SortedSet 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 downstreamJobsFullNames = new TreeSet<>(); @@ -680,7 +681,8 @@ protected SortedSet 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)); diff --git a/jenkins-plugin/src/main/resources/sql/mysql/13_migration.sql b/jenkins-plugin/src/main/resources/sql/mysql/13_migration.sql index e69d169a..4124929e 100644 --- a/jenkins-plugin/src/main/resources/sql/mysql/13_migration.sql +++ b/jenkins-plugin/src/main/resources/sql/mysql/13_migration.sql @@ -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); diff --git a/jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/dao/PipelineMavenPluginMySqlDaoIT.java b/jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/dao/PipelineMavenPluginMySqlDaoIT.java index 209acd35..d605aa3b 100644 --- a/jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/dao/PipelineMavenPluginMySqlDaoIT.java +++ b/jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/dao/PipelineMavenPluginMySqlDaoIT.java @@ -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 Cyrille Le Clerc */ 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); } diff --git a/jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/dao/PipelineMavenPluginPostgreSqlDaoIT.java b/jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/dao/PipelineMavenPluginPostgreSqlDaoIT.java index 93ba6842..91b0ce71 100644 --- a/jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/dao/PipelineMavenPluginPostgreSqlDaoIT.java +++ b/jenkins-plugin/src/test/java/org/jenkinsci/plugins/pipeline/maven/dao/PipelineMavenPluginPostgreSqlDaoIT.java @@ -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 Cyrille Le Clerc */ 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); }