From f52260aacaaf9b5030b5835650b3675d58fb984b Mon Sep 17 00:00:00 2001 From: Gael Leblan Date: Tue, 3 Dec 2024 16:39:56 +0100 Subject: [PATCH] [backend] Upgrading to latest spring boot version (#1770 #1773) --- openbas-api/pom.xml | 4 +++ .../V3_49__Adding_wrapper_functions.java | 32 +++++++++++++++++++ .../exercise/service/ExerciseService.java | 7 ++-- .../io/openbas/service/ScenarioService.java | 11 ++++--- .../src/main/resources/application.properties | 2 +- .../main/java/io/openbas/utils/JpaUtils.java | 13 ++++---- .../io/openbas/utils/OperationUtilsJpa.java | 4 +-- openbas-model/pom.xml | 2 +- .../io/openbas/database/model/Endpoint.java | 2 +- pom.xml | 2 +- 10 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 openbas-api/src/main/java/io/openbas/migration/V3_49__Adding_wrapper_functions.java diff --git a/openbas-api/pom.xml b/openbas-api/pom.xml index bc488f4c59..2d2cd22f03 100644 --- a/openbas-api/pom.xml +++ b/openbas-api/pom.xml @@ -146,6 +146,10 @@ org.flywaydb flyway-core + + org.flywaydb + flyway-database-postgresql + commons-codec commons-codec diff --git a/openbas-api/src/main/java/io/openbas/migration/V3_49__Adding_wrapper_functions.java b/openbas-api/src/main/java/io/openbas/migration/V3_49__Adding_wrapper_functions.java new file mode 100644 index 0000000000..7210df4f9f --- /dev/null +++ b/openbas-api/src/main/java/io/openbas/migration/V3_49__Adding_wrapper_functions.java @@ -0,0 +1,32 @@ +package io.openbas.migration; + +import java.sql.SQLException; +import java.sql.Statement; +import org.flywaydb.core.api.migration.BaseJavaMigration; +import org.flywaydb.core.api.migration.Context; +import org.springframework.stereotype.Component; + +@Component +public class V3_49__Adding_wrapper_functions extends BaseJavaMigration { + + @Override + public void migrate(Context context) throws SQLException { + Statement migrator = context.getConnection().createStatement(); + migrator.execute( + """ + CREATE OR REPLACE FUNCTION array_to_string_wrapper(a anyelement, b text) + RETURNS TEXT AS + $$ + SELECT array_to_string(a, b) + $$ LANGUAGE SQL; + """); + migrator.execute( + """ + CREATE OR REPLACE FUNCTION array_position_wrapper(a anyelement, b text) + RETURNS INT AS + $$ + SELECT array_position(a, b) + $$ LANGUAGE SQL; + """); + } +} diff --git a/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java b/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java index 921aad77a6..f874bb8fbd 100644 --- a/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java +++ b/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java @@ -42,6 +42,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import lombok.RequiredArgsConstructor; +import org.hibernate.query.criteria.HibernateCriteriaBuilder; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; @@ -384,11 +385,13 @@ private void select( // Array aggregations Join exerciseTagsJoin = exerciseRoot.join("tags", JoinType.LEFT); joinMap.put("tags", exerciseTagsJoin); - Expression tagIdsExpression = arrayAggOnId(cb, exerciseTagsJoin); + Expression tagIdsExpression = + arrayAggOnId((HibernateCriteriaBuilder) cb, exerciseTagsJoin); Join injectsJoin = exerciseRoot.join("injects", JoinType.LEFT); joinMap.put("injects", injectsJoin); - Expression injectIdsExpression = arrayAggOnId(cb, injectsJoin); + Expression injectIdsExpression = + arrayAggOnId((HibernateCriteriaBuilder) cb, injectsJoin); // SELECT cq.multiselect( exerciseRoot.get("id").alias("exercise_id"), diff --git a/openbas-api/src/main/java/io/openbas/service/ScenarioService.java b/openbas-api/src/main/java/io/openbas/service/ScenarioService.java index 77435a74fb..95af388ac7 100644 --- a/openbas-api/src/main/java/io/openbas/service/ScenarioService.java +++ b/openbas-api/src/main/java/io/openbas/service/ScenarioService.java @@ -58,6 +58,7 @@ import lombok.extern.java.Log; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.function.TriFunction; +import org.hibernate.query.criteria.HibernateCriteriaBuilder; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; @@ -183,12 +184,12 @@ private Page findAllWithCriteriaBuilder( // Join on TAG Join scenarioTagsJoin = scenarioRoot.join("tags", JoinType.LEFT); joinMap.put("tags", scenarioTagsJoin); + Expression nullString = cb.nullLiteral(String.class); + Expression arr = + ((HibernateCriteriaBuilder) cb).arrayAgg(null, scenarioTagsJoin.get("id")); Expression tagIdsExpression = - cb.function( - "array_remove", - String[].class, - cb.function("array_agg", String[].class, scenarioTagsJoin.get("id")), - cb.nullLiteral(String.class)); + ((HibernateCriteriaBuilder) cb).arrayRemove(arr, nullString); + // Join on INJECT and INJECTOR CONTRACT Join injectsJoin = scenarioRoot.join("injects", JoinType.LEFT); joinMap.put("injects", injectsJoin); diff --git a/openbas-api/src/main/resources/application.properties b/openbas-api/src/main/resources/application.properties index ce9cd5dc26..df117e8f76 100644 --- a/openbas-api/src/main/resources/application.properties +++ b/openbas-api/src/main/resources/application.properties @@ -97,7 +97,7 @@ spring.datasource.url=jdbc:postgresql://... spring.datasource.username=openbas spring.datasource.password= spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect -spring.jpa.hibernate.ddl-auto=validate +spring.jpa.hibernate.ddl-auto=none # spring.jpa.show-sql=true # spring.jpa.properties.hibernate.format_sql=true spring.flyway.url=${spring.datasource.url} diff --git a/openbas-framework/src/main/java/io/openbas/utils/JpaUtils.java b/openbas-framework/src/main/java/io/openbas/utils/JpaUtils.java index 8202e3a80f..b62fdf47cf 100644 --- a/openbas-framework/src/main/java/io/openbas/utils/JpaUtils.java +++ b/openbas-framework/src/main/java/io/openbas/utils/JpaUtils.java @@ -7,6 +7,7 @@ import jakarta.persistence.criteria.*; import jakarta.validation.constraints.NotNull; import java.util.Map; +import org.hibernate.query.criteria.HibernateCriteriaBuilder; public class JpaUtils { @@ -89,12 +90,10 @@ public static Expression toPath( // -- FUNCTION -- public static Expression arrayAggOnId( - @NotNull final CriteriaBuilder cb, @NotNull final Join join) { - return cb.function( - "array_remove", - String[].class, - cb.function("array_agg", String[].class, join.get("id")), - cb.nullLiteral(String.class)); + @NotNull final HibernateCriteriaBuilder cb, @NotNull final Join join) { + Expression nullString = cb.nullLiteral(String.class); + Expression arr = cb.arrayAgg(null, join.get("id")); + return cb.arrayRemove(arr, nullString); } // -- JOIN -- @@ -106,6 +105,6 @@ public static Join createLeftJoin(Root root, String attributeNam public static Expression createJoinArrayAggOnId( CriteriaBuilder cb, Root root, String attributeName) { Join join = createLeftJoin(root, attributeName); - return arrayAggOnId(cb, join); + return arrayAggOnId((HibernateCriteriaBuilder) cb, join); } } diff --git a/openbas-framework/src/main/java/io/openbas/utils/OperationUtilsJpa.java b/openbas-framework/src/main/java/io/openbas/utils/OperationUtilsJpa.java index a8bc1b0a15..f34bd8a320 100644 --- a/openbas-framework/src/main/java/io/openbas/utils/OperationUtilsJpa.java +++ b/openbas-framework/src/main/java/io/openbas/utils/OperationUtilsJpa.java @@ -295,7 +295,7 @@ private static Expression lowerArray(Expression paths, CriteriaBuil private static Expression arrayPosition( Expression paths, CriteriaBuilder cb, Expression text) { - return cb.function("array_position", Boolean.class, paths, text); + return cb.function("array_position_wrapper", Boolean.class, paths, text); } private static Expression lower(Expression paths, CriteriaBuilder cb) { @@ -307,7 +307,7 @@ private static Expression stringToArray(Expression paths, Crit } private static Expression arrayToString(Expression paths, CriteriaBuilder cb) { - return cb.function("array_to_string", String.class, paths, cb.literal(" && ")); + return cb.function("array_to_string_wrapper", String.class, paths, cb.literal(" && ")); } private static Expression avals(Expression paths, CriteriaBuilder cb) { diff --git a/openbas-model/pom.xml b/openbas-model/pom.xml index 12f57d0489..1b6cdbfe62 100644 --- a/openbas-model/pom.xml +++ b/openbas-model/pom.xml @@ -46,7 +46,7 @@ io.hypersistence hypersistence-utils-hibernate-63 - 3.7.0 + 3.9.0 org.postgresql diff --git a/openbas-model/src/main/java/io/openbas/database/model/Endpoint.java b/openbas-model/src/main/java/io/openbas/database/model/Endpoint.java index f11f3ba660..3e91ab0ec2 100644 --- a/openbas-model/src/main/java/io/openbas/database/model/Endpoint.java +++ b/openbas-model/src/main/java/io/openbas/database/model/Endpoint.java @@ -53,7 +53,7 @@ public enum PLATFORM_TYPE { @NotEmpty @Ipv4OrIpv6Constraint @Type(StringArrayType.class) - @Column(name = "endpoint_ips") + @Column(name = "endpoint_ips", columnDefinition = "text[]") @JsonProperty("endpoint_ips") private String[] ips; diff --git a/pom.xml b/pom.xml index 0d8e5785b0..18a35cead6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.boot spring-boot-starter-parent - 3.2.0 + 3.3.5 io.openbas