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

[backend] Upgrading to latest spring boot version #1894

Merged
merged 4 commits into from
Dec 3, 2024
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
4 changes: 4 additions & 0 deletions openbas-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-database-postgresql</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -383,11 +384,13 @@ private void select(
// Array aggregations
Join<Base, Base> exerciseTagsJoin = exerciseRoot.join("tags", JoinType.LEFT);
joinMap.put("tags", exerciseTagsJoin);
Expression<String[]> tagIdsExpression = arrayAggOnId(cb, exerciseTagsJoin);
Expression<String[]> tagIdsExpression =
arrayAggOnId((HibernateCriteriaBuilder) cb, exerciseTagsJoin);

Join<Base, Base> injectsJoin = exerciseRoot.join("injects", JoinType.LEFT);
joinMap.put("injects", injectsJoin);
Expression<String[]> injectIdsExpression = arrayAggOnId(cb, injectsJoin);
Expression<String[]> injectIdsExpression =
arrayAggOnId((HibernateCriteriaBuilder) cb, injectsJoin);
// SELECT
cq.multiselect(
exerciseRoot.get("id").alias("exercise_id"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -183,12 +184,12 @@ private Page<RawPaginationScenario> findAllWithCriteriaBuilder(
// Join on TAG
Join<Base, Base> scenarioTagsJoin = scenarioRoot.join("tags", JoinType.LEFT);
joinMap.put("tags", scenarioTagsJoin);
Expression<String> nullString = cb.nullLiteral(String.class);
Expression<String[]> arr =
((HibernateCriteriaBuilder) cb).arrayAgg(null, scenarioTagsJoin.get("id"));
Expression<String[]> 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<Base, Base> injectsJoin = scenarioRoot.join("injects", JoinType.LEFT);
joinMap.put("injects", injectsJoin);
Expand Down
2 changes: 1 addition & 1 deletion openbas-api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ spring.datasource.url=jdbc:postgresql://...
spring.datasource.username=openbas
spring.datasource.password=<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}
Expand Down
13 changes: 6 additions & 7 deletions openbas-framework/src/main/java/io/openbas/utils/JpaUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -89,12 +90,10 @@ public static <T, U> Expression<U> toPath(
// -- FUNCTION --

public static <T, U> Expression<String[]> arrayAggOnId(
@NotNull final CriteriaBuilder cb, @NotNull final Join<T, U> 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<T, U> join) {
Expression<String> nullString = cb.nullLiteral(String.class);
Expression<String[]> arr = cb.arrayAgg(null, join.get("id"));
return cb.arrayRemove(arr, nullString);
}

// -- JOIN --
Expand All @@ -106,6 +105,6 @@ public static <X, Y> Join<X, Y> createLeftJoin(Root<X> root, String attributeNam
public static <X, Y> Expression<String[]> createJoinArrayAggOnId(
CriteriaBuilder cb, Root<X> root, String attributeName) {
Join<X, Y> join = createLeftJoin(root, attributeName);
return arrayAggOnId(cb, join);
return arrayAggOnId((HibernateCriteriaBuilder) cb, join);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private static Expression<String[]> lowerArray(Expression<?> paths, CriteriaBuil

private static Expression<Boolean> arrayPosition(
Expression<String[]> paths, CriteriaBuilder cb, Expression<String> text) {
return cb.function("array_position", Boolean.class, paths, text);
return cb.function("array_position_wrapper", Boolean.class, paths, text);
}

private static Expression<String> lower(Expression<String> paths, CriteriaBuilder cb) {
Expand All @@ -307,7 +307,7 @@ private static Expression<String[]> stringToArray(Expression<String> paths, Crit
}

private static Expression<String> 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<String[]> avals(Expression<String> paths, CriteriaBuilder cb) {
Expand Down
2 changes: 1 addition & 1 deletion openbas-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<dependency>
<groupId>io.hypersistence</groupId>
<artifactId>hypersistence-utils-hibernate-63</artifactId>
<version>3.7.0</version>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<version>3.3.5</version>
</parent>

<groupId>io.openbas</groupId>
Expand Down