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/frontend] Fix import #1069

Merged
merged 5 commits into from
Jun 14, 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
432 changes: 265 additions & 167 deletions openbas-api/src/main/java/io/openbas/importer/V1_DataImporter.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
package io.openbas.migration;

import lombok.extern.java.Log;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

@Log
@Component
public class V3_16__Add_array_union_agg_method extends BaseJavaMigration {

@Override
public void migrate(Context context) throws Exception {
Connection connection = context.getConnection();
Statement select = connection.createStatement();
select.execute("CREATE FUNCTION array_union(a ANYARRAY, b ANYARRAY)"
+ " RETURNS ANYARRAY AS"
+ " $$"
+ "SELECT array_agg(DISTINCT x)"
+ "FROM ("
+ " SELECT unnest(a) x"
+ " UNION ALL SELECT unnest(b)"
+ " ) AS u"
+ " $$ LANGUAGE SQL;"
+ "CREATE AGGREGATE array_union_agg(ANYARRAY) ("
+ " SFUNC = array_union,"
+ " STYPE = ANYARRAY,"
+ " INITCOND = '{}'"
+ ");");
try (Statement statement = connection.createStatement()) {
ResultSet rs = statement.executeQuery(
"SELECT EXISTS (SELECT 1 FROM pg_proc WHERE proname = 'array_union');");

boolean functionExists = false;
if (rs.next()) {
functionExists = rs.getBoolean(1);
}
if (!functionExists) {
statement.execute("CREATE FUNCTION array_union(a ANYARRAY, b ANYARRAY)"
+ " RETURNS ANYARRAY AS"
+ " $$"
+ "SELECT array_agg(DISTINCT x)"
+ "FROM ("
+ " SELECT unnest(a) x"
+ " UNION ALL SELECT unnest(b)"
+ " ) AS u"
+ " $$ LANGUAGE SQL;"
+ "CREATE AGGREGATE array_union_agg(ANYARRAY) ("
+ " SFUNC = array_union,"
+ " STYPE = ANYARRAY,"
+ " INITCOND = '{}'"
+ ");");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,11 @@
importExport.setUsers(players);
objectMapper.addMixIn(User.class, ExerciseExportMixins.User.class);
// organizations
List<Organization> organizations = players.stream().map(User::getOrganization).filter(Objects::nonNull).toList();
List<Organization> organizations = players.stream()
.map(User::getOrganization)
.filter(Objects::nonNull)
.distinct()
.toList();

Check warning on line 807 in openbas-api/src/main/java/io/openbas/rest/exercise/ExerciseApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/exercise/ExerciseApi.java#L803-L807

Added lines #L803 - L807 were not covered by tests
exerciseTags.addAll(organizations.stream().flatMap(org -> org.getTags().stream()).toList());
importExport.setOrganizations(organizations);
objectMapper.addMixIn(Organization.class, ExerciseExportMixins.Organization.class);
Expand Down
Loading
Loading