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

Adapt to Neo4j-Migrations 1.x API. #17315

Merged
merged 1 commit into from
Dec 15, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package <%= packageName %>.config.neo4j;

import ac.simons.neo4j.migrations.core.JavaBasedMigration;
import ac.simons.neo4j.migrations.core.MigrationContext;
import ac.simons.neo4j.migrations.core.MigrationsException;

import java.io.IOException;
import java.util.List;
Expand All @@ -46,11 +47,16 @@ final class Neo4jMigrations {
static class V0001__CreateUsers implements JavaBasedMigration {

@Override
public void apply(MigrationContext context) throws IOException {
public void apply(MigrationContext context) {

ObjectMapper om = new ObjectMapper();
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resourcePatternResolver.getResources("classpath:config/neo4j/migrations/user__*.json");
Resource[] resources;
try {
resources = resourcePatternResolver.getResources("classpath:config/neo4j/migrations/user__*.json");
} catch (IOException e) {
throw new MigrationsException("Could not load user definition resources.", e);
}

JavaType type = om.getTypeFactory().
constructMapType(Map.class, String.class, Object.class);
Expand All @@ -69,13 +75,17 @@ final class Neo4jMigrations {
try (Session session = context.getSession()) {

for (Resource resource : resources) {
Map<String, Object> user = om.readValue(resource.getInputStream(), type);
user.put("user_id", UUID.randomUUID().toString());
List<String> authorities = (List<String>) user.remove("authorities");
user.remove("_class");

session.writeTransaction(
t -> t.run(query, Values.parameters("user", user, "authorities", authorities)).consume());
try {
Map<String, Object> user = om.readValue(resource.getInputStream(), type);
user.put("user_id", UUID.randomUUID().toString());
List<String> authorities = (List<String>) user.remove("authorities");
user.remove("_class");

session.writeTransaction(
t -> t.run(query, Values.parameters("user", user, "authorities", authorities)).consume());
} catch (IOException e) {
throw new MigrationsException("Could not load resource " + resource.getDescription() + ".", e);
}
}
}
}
Expand Down