Skip to content

Commit

Permalink
Adjust null handling of collectionLogic and collectionLogicChanged
Browse files Browse the repository at this point in the history
Store `ProjectCollectionLogic.NONE` as `NULL`. Reason being that the vast majority of projects will not have a collection logic, and storing `NULL` is cheaper than storing the string `NONE` over and over again. The code and REST API will still treat `NULL` as `NONE`, though.

Remove the index on `Project.collectionLogic`. The column has a low cardinality and is only ever queried in `PortfolioMetricsUpdateTask#fetchNextActiveProjectsBatch`. Since this method paginates using the `id` column, the index on `collectionLogic` is never used.

Remove the redundant upgrade item since `collectionLogic` columns are expected to remain `NULL` when a project is not a collection.

Make `ProjectMetrics.collectionLogicChanged` non-nullable and default to `false`. Prevents tri-state logic.

Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Dec 8, 2024
1 parent 8e2015c commit f55d48c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 64 deletions.
15 changes: 8 additions & 7 deletions src/main/java/org/dependencytrack/model/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import io.swagger.v3.oas.annotations.media.Schema;

import org.dependencytrack.parser.cyclonedx.util.ModelConverter;
import org.dependencytrack.persistence.converter.OrganizationalContactsJsonConverter;
import org.dependencytrack.persistence.converter.OrganizationalEntityJsonConverter;
Expand Down Expand Up @@ -190,10 +189,9 @@ public enum FetchGroup {
private Classifier classifier;

@Persistent
@Column(name = "COLLECTION_LOGIC", jdbcType = "VARCHAR", allowsNull = "true", defaultValue = "NONE") // New column, must allow nulls on existing databases
@Index(name = "PROJECT_COLLECTION_LOGIC_IDX")
@Column(name = "COLLECTION_LOGIC", jdbcType = "VARCHAR", allowsNull = "true")
@Extension(vendorName = "datanucleus", key = "enum-check-constraint", value = "true")
private ProjectCollectionLogic collectionLogic = ProjectCollectionLogic.NONE;
private ProjectCollectionLogic collectionLogic;

@Persistent(defaultFetchGroup = "true")
@Column(name = "COLLECTION_TAG", allowsNull = "true")
Expand Down Expand Up @@ -405,13 +403,16 @@ public void setClassifier(Classifier classifier) {
this.classifier = classifier;
}


public ProjectCollectionLogic getCollectionLogic() {
return collectionLogic;
return collectionLogic == null
? ProjectCollectionLogic.NONE
: collectionLogic;
}

public void setCollectionLogic(ProjectCollectionLogic collectionLogic) {
this.collectionLogic = collectionLogic;
this.collectionLogic = collectionLogic != ProjectCollectionLogic.NONE
? collectionLogic
: null;
}

public Tag getCollectionTag() {
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/org/dependencytrack/model/ProjectMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ public class ProjectMetrics implements Serializable {
private Integer policyViolationsOperationalUnaudited;

@Persistent
@Column(name = "COLLECTION_LOGIC", allowsNull = "true") // New column, must allow nulls on existing data bases
@Column(name = "COLLECTION_LOGIC", allowsNull = "true")
private ProjectCollectionLogic collectionLogic;

@Persistent
@Column(name = "COLLECTION_LOGIC_CHANGED", allowsNull = "true") // New column, must allow nulls on existing data bases
private Boolean collectionLogicChanged = false;
@Column(name = "COLLECTION_LOGIC_CHANGED", allowsNull = "false", defaultValue = "false")
private boolean collectionLogicChanged = false;

@Persistent
@Column(name = "FIRST_OCCURRENCE", allowsNull = "false")
Expand Down Expand Up @@ -433,16 +433,23 @@ public void setPolicyViolationsOperationalUnaudited(int policyViolationsOperatio
this.policyViolationsOperationalUnaudited = policyViolationsOperationalUnaudited;
}

public ProjectCollectionLogic getCollectionLogic() { return collectionLogic; }
public ProjectCollectionLogic getCollectionLogic() {
return collectionLogic == null
? ProjectCollectionLogic.NONE
: collectionLogic;
}

public void setCollectionLogic(ProjectCollectionLogic collectionLogic) {
// convert old NULL values from DB to NONE
this.collectionLogic = collectionLogic != null ? collectionLogic : ProjectCollectionLogic.NONE;
this.collectionLogic = collectionLogic != ProjectCollectionLogic.NONE
? collectionLogic
: null;
}

public Boolean isCollectionLogicChanged() { return collectionLogicChanged; }
public boolean isCollectionLogicChanged() {
return collectionLogicChanged;
}

public void setCollectionLogicChanged(Boolean collectionLogicChanged) {
public void setCollectionLogicChanged(boolean collectionLogicChanged) {
this.collectionLogicChanged = collectionLogicChanged;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class UpgradeItems {
UPGRADE_ITEMS.add(org.dependencytrack.upgrade.v4110.v4110Updater.class);
UPGRADE_ITEMS.add(org.dependencytrack.upgrade.v4120.v4120Updater.class);
UPGRADE_ITEMS.add(org.dependencytrack.upgrade.v4122.v4122Updater.class);
UPGRADE_ITEMS.add(org.dependencytrack.upgrade.v4130.v4130Updater.class);
}

static List<Class<? extends UpgradeItem>> getUpgradeItems() {
Expand Down
48 changes: 0 additions & 48 deletions src/main/java/org/dependencytrack/upgrade/v4130/v4130Updater.java

This file was deleted.

0 comments on commit f55d48c

Please sign in to comment.