-
-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure the field defaults to `true`, both in Java and the database. During upgrade, migrate all values that are currently `null` to `true`. Solidify this change by switching `project.active` from `Boolean` to `boolean`. Adjust logic that previously had to check for `null`. Fixes #4410 Signed-off-by: nscuro <[email protected]>
- Loading branch information
Showing
16 changed files
with
157 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# This file is part of Dependency-Track. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# Copyright (c) OWASP Foundation. All Rights Reserved. | ||
services: | ||
apiserver: | ||
depends_on: | ||
- mysql | ||
environment: | ||
ALPINE_DATABASE_MODE: "external" | ||
ALPINE_DATABASE_URL: "jdbc:mysql://mysql:3306/dtrack?autoReconnect=true&useSSL=false&sessionVariables=sql_mode='ANSI_QUOTES,STRICT_TRANS_TABLES,ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'" | ||
ALPINE_DATABASE_DRIVER: "com.mysql.cj.jdbc.Driver" | ||
ALPINE_DATABASE_USERNAME: "dtrack" | ||
ALPINE_DATABASE_PASSWORD: "dtrack" | ||
|
||
mysql: | ||
image: mysql:5.7 | ||
platform: "linux/amd64" # arm64 is not supported | ||
environment: | ||
MYSQL_DATABASE: "dtrack" | ||
MYSQL_RANDOM_ROOT_PASSWORD: "yes" | ||
MYSQL_USER: "dtrack" | ||
MYSQL_PASSWORD: "dtrack" | ||
ports: | ||
- "127.0.0.1:3306:3306" | ||
volumes: | ||
- "mysql-data:/var/lib/mysql" | ||
restart: unless-stopped | ||
|
||
volumes: | ||
mysql-data: { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/main/java/org/dependencytrack/upgrade/v4122/v4122Updater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* This file is part of Dependency-Track. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) OWASP Foundation. All Rights Reserved. | ||
*/ | ||
package org.dependencytrack.upgrade.v4122; | ||
|
||
import alpine.common.logging.Logger; | ||
import alpine.persistence.AlpineQueryManager; | ||
import alpine.server.upgrade.AbstractUpgradeItem; | ||
import alpine.server.util.DbUtil; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
public class v4122Updater extends AbstractUpgradeItem { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(v4122Updater.class); | ||
|
||
@Override | ||
public String getSchemaVersion() { | ||
return "4.12.2"; | ||
} | ||
|
||
@Override | ||
public void executeUpgrade(final AlpineQueryManager qm, final Connection connection) throws Exception { | ||
fixProjectActiveNullValues(connection); | ||
} | ||
|
||
private static void fixProjectActiveNullValues(final Connection connection) throws SQLException { | ||
LOGGER.info("Setting active flag to true for projects where it's currently null"); | ||
try (final PreparedStatement ps = connection.prepareStatement(""" | ||
UPDATE "PROJECT" | ||
SET "ACTIVE" = ? | ||
WHERE "ACTIVE" IS NULL; | ||
""")) { | ||
ps.setBoolean(1, true); | ||
|
||
final int modifiedProjects = ps.executeUpdate(); | ||
LOGGER.info("Updated active flag of %d projects".formatted(modifiedProjects)); | ||
} | ||
|
||
LOGGER.info("Setting default value of the project active flag to true"); | ||
try (final Statement stmt = connection.createStatement()) { | ||
if (DbUtil.isMssql()) { | ||
stmt.executeUpdate(""" | ||
ALTER TABLE "PROJECT" | ||
ADD DEFAULT 'true' | ||
FOR "ACTIVE"; | ||
"""); | ||
} else if (DbUtil.isMysql()) { | ||
stmt.executeUpdate(""" | ||
ALTER TABLE "PROJECT" | ||
MODIFY COLUMN "ACTIVE" BIT(1) DEFAULT 1; | ||
"""); | ||
} else if (DbUtil.isPostgreSQL() || DbUtil.isH2()) { | ||
stmt.executeUpdate(""" | ||
ALTER TABLE "PROJECT" | ||
ALTER COLUMN "ACTIVE" | ||
SET DEFAULT TRUE; | ||
"""); | ||
} else { | ||
throw new IllegalStateException( | ||
"Unsupported database: " + connection.getMetaData().getDatabaseProductName()); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters