Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DbUtil not being initialized #1058

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading