Skip to content

Commit

Permalink
Merge pull request #1058 from DependencyTrack/fix-dbutil-not-initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
nscuro authored Feb 13, 2025
2 parents 46f5b17 + f2c5e89 commit c48882b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import alpine.Config;
import alpine.common.logging.Logger;
import alpine.server.util.DbUtil;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import liquibase.Liquibase;
Expand All @@ -40,6 +41,7 @@
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import javax.sql.DataSource;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Optional;

Expand Down Expand Up @@ -73,6 +75,16 @@ public void contextInitialized(final ServletContextEvent event) {

LOGGER.info("Running migrations");
try (final HikariDataSource dataSource = createDataSource()) {
try (final Connection connection = dataSource.getConnection()) {
// Ensure that DbUtil#isPostgreSQL will work as expected.
// Some legacy code ported over from v4 still uses this.
//
// NB: This was previously done in alpine.server.upgrade.UpgradeExecutor.
//
// TODO: Remove once DbUtil#isPostgreSQL is no longer used.
DbUtil.initPlatformName(connection);
}

runMigration(dataSource);
} catch (Exception e) {
if (config.getPropertyAsBoolean(ConfigKey.DATABASE_RUN_MIGRATIONS_ONLY)
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/dependencytrack/PostgresTestContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
*/
package org.dependencytrack;

import alpine.server.util.DbUtil;
import com.github.dockerjava.api.command.InspectContainerResponse;
import org.dependencytrack.persistence.migration.MigrationInitializer;
import org.postgresql.ds.PGSimpleDataSource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.TestcontainersConfiguration;

import java.sql.Connection;

public class PostgresTestContainer extends PostgreSQLContainer<PostgresTestContainer> {

@SuppressWarnings("resource")
Expand Down Expand Up @@ -61,6 +64,13 @@ protected void containerIsStarted(final InspectContainerResponse containerInfo,
dataSource.setPassword(getPassword());

try {
try (final Connection connection = dataSource.getConnection()) {
// Ensure that DbUtil#isPostgreSQL will work as expected.
// Some legacy code ported over from v4 still uses this.
// TODO: Remove once DbUtil#isPostgreSQL is no longer used.
DbUtil.initPlatformName(connection);
}

MigrationInitializer.runMigration(dataSource);
} catch (Exception e) {
throw new RuntimeException("Failed to execute migrations", e);
Expand Down

0 comments on commit c48882b

Please sign in to comment.