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

[fdGuLCUX] Add type constraint support #424

Merged
merged 3 commits into from
Jun 1, 2023
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
17 changes: 13 additions & 4 deletions common/src/main/java/apoc/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -1055,26 +1055,35 @@ public enum ConstraintCategory {

public static ConstraintCategory getConstraintCategory(ConstraintType type) {
return switch (type) {
case NODE_KEY, NODE_PROPERTY_EXISTENCE, UNIQUENESS -> ConstraintCategory.NODE;
case RELATIONSHIP_KEY, RELATIONSHIP_UNIQUENESS, RELATIONSHIP_PROPERTY_EXISTENCE -> ConstraintCategory.RELATIONSHIP;
case NODE_KEY, NODE_PROPERTY_EXISTENCE, UNIQUENESS, NODE_PROPERTY_TYPE -> ConstraintCategory.NODE;
case RELATIONSHIP_KEY, RELATIONSHIP_UNIQUENESS, RELATIONSHIP_PROPERTY_EXISTENCE, RELATIONSHIP_PROPERTY_TYPE -> ConstraintCategory.RELATIONSHIP;
default -> throw new IllegalStateException("Constraint with a type not supported by apoc");
};
}

public static ConstraintCategory getConstraintCategory(ConstraintDescriptor descriptor) {
if (descriptor.isNodeUniquenessConstraint() ||
descriptor.isNodePropertyExistenceConstraint() ||
descriptor.isNodeKeyConstraint()) {
descriptor.isNodeKeyConstraint() ||
descriptor.isNodePropertyTypeConstraint()) {
return ConstraintCategory.NODE;
}
if (descriptor.isRelationshipKeyConstraint() ||
descriptor.isRelationshipPropertyExistenceConstraint() ||
descriptor.isRelationshipUniquenessConstraint()) {
descriptor.isRelationshipUniquenessConstraint() ||
descriptor.isRelationshipPropertyTypeConstraint()) {
return ConstraintCategory.RELATIONSHIP;
}
return ConstraintCategory.NODE;
}

public static boolean constraintIsUnique(ConstraintType type) {
return type == ConstraintType.NODE_KEY ||
type == ConstraintType.RELATIONSHIP_KEY ||
type == ConstraintType.UNIQUENESS ||
type == ConstraintType.RELATIONSHIP_UNIQUENESS;
}

public static boolean isNodeCategory(ConstraintType type) {
return getConstraintCategory(type) == ConstraintCategory.NODE;
}
Expand Down
57 changes: 57 additions & 0 deletions common/src/test/java/apoc/util/UtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package apoc.util;

import org.junit.Test;
import org.neo4j.graphdb.schema.ConstraintType;
import org.neo4j.graphdb.schema.IndexType;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.neo4j.graphdb.schema.ConstraintType.NODE_KEY;
import static org.neo4j.graphdb.schema.ConstraintType.NODE_PROPERTY_EXISTENCE;
import static org.neo4j.graphdb.schema.ConstraintType.NODE_PROPERTY_TYPE;
import static org.neo4j.graphdb.schema.ConstraintType.RELATIONSHIP_KEY;
import static org.neo4j.graphdb.schema.ConstraintType.RELATIONSHIP_PROPERTY_EXISTENCE;
import static org.neo4j.graphdb.schema.ConstraintType.RELATIONSHIP_PROPERTY_TYPE;
import static org.neo4j.graphdb.schema.ConstraintType.RELATIONSHIP_UNIQUENESS;
import static org.neo4j.graphdb.schema.ConstraintType.UNIQUENESS;
import static org.neo4j.graphdb.schema.IndexType.FULLTEXT;
import static org.neo4j.graphdb.schema.IndexType.LOOKUP;
import static org.neo4j.graphdb.schema.IndexType.POINT;
import static org.neo4j.graphdb.schema.IndexType.RANGE;
import static org.neo4j.graphdb.schema.IndexType.TEXT;

public class UtilTest {

/**
* If any new constraints or indexes are added, this test will fail.
* Add the new constraints/indexes to the tests as well and update
* the apoc.schema.* procedures to work with them.
*/
@Test
public void testAPOCisAwareOfAllConstraints() {
assertEquals(Arrays.stream(ConstraintType.values()).collect(Collectors.toSet()), Set.of(
UNIQUENESS,
NODE_PROPERTY_EXISTENCE,
RELATIONSHIP_PROPERTY_EXISTENCE,
NODE_KEY,
RELATIONSHIP_KEY,
RELATIONSHIP_UNIQUENESS,
RELATIONSHIP_PROPERTY_TYPE,
NODE_PROPERTY_TYPE
));
}

@Test
public void testAPOCisAwareOfAllIndexes() {
assertEquals(Arrays.stream(IndexType.values()).collect(Collectors.toSet()), Set.of(
FULLTEXT,
LOOKUP,
TEXT,
RANGE,
POINT
));
}
ncordon marked this conversation as resolved.
Show resolved Hide resolved
}
9 changes: 7 additions & 2 deletions core/src/main/java/apoc/schema/Schemas.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.schema.ConstraintDefinition;
import org.neo4j.graphdb.schema.ConstraintType;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.graphdb.schema.IndexType;
import org.neo4j.graphdb.schema.Schema;
Expand Down Expand Up @@ -134,8 +135,12 @@ public List<AssertSchemaResult> assertConstraints(Map<String, List<Object>> cons
Schema schema = tx.schema();

for (ConstraintDefinition definition : schema.getConstraints()) {
String label = Util.isRelationshipCategory(definition.getConstraintType()) ? definition.getRelationshipType().name() : definition.getLabel().name();
AssertSchemaResult info = new AssertSchemaResult(label, Iterables.asList(definition.getPropertyKeys())).unique();
ConstraintType constraintType = definition.getConstraintType();
String label = Util.isRelationshipCategory(constraintType) ? definition.getRelationshipType().name() : definition.getLabel().name();
AssertSchemaResult info = new AssertSchemaResult(label, Iterables.asList(definition.getPropertyKeys()));
if (Util.constraintIsUnique(constraintType)) {
info = info.unique();
}
if (!checkIfConstraintExists(label, constraints, info)) {
if (dropExisting) {
definition.drop();
Expand Down
Loading