Search in sources :

Example 26 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.

the class SubGraphExporter method exportConstraints.

private Collection<String> exportConstraints() {
    final List<String> result = new ArrayList<>();
    for (ConstraintDefinition constraint : graph.getConstraints()) {
        if (!constraint.isConstraintType(ConstraintType.UNIQUENESS)) {
            throw new RuntimeException("Exporting constraints other than uniqueness is not implemented yet");
        }
        Iterator<String> propertyKeys = constraint.getPropertyKeys().iterator();
        if (!propertyKeys.hasNext()) {
            throw new IllegalStateException("Constraints should have at least one property key");
        }
        String key = quote(propertyKeys.next());
        if (propertyKeys.hasNext()) {
            throw new RuntimeException("Exporting compound constraints is not implemented yet");
        }
        String label = quote(constraint.getLabel().name());
        result.add("create constraint on (n:" + label + ") assert n." + key + " is unique");
    }
    Collections.sort(result);
    return result;
}
Also used : ArrayList(java.util.ArrayList) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 27 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.

the class SchemaImpl method asConstraintDefinitions.

private Iterable<ConstraintDefinition> asConstraintDefinitions(Iterator<? extends ConstraintDescriptor> constraints, ReadOperations readOperations) {
    // Intentionally create an eager list so that used statement can be closed
    List<ConstraintDefinition> definitions = new ArrayList<>();
    while (constraints.hasNext()) {
        ConstraintDescriptor constraint = constraints.next();
        definitions.add(asConstraintDefinition(ConstraintBoundary.map(constraint), readOperations));
    }
    return definitions;
}
Also used : ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) ArrayList(java.util.ArrayList) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 28 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.

the class SchemaAcceptanceTest method shouldListAddedConstraintsByLabel.

@Test
public void shouldListAddedConstraintsByLabel() throws Exception {
    // GIVEN
    ConstraintDefinition constraint1 = createUniquenessConstraint(label, propertyKey);
    createUniquenessConstraint(Labels.MY_OTHER_LABEL, propertyKey);
    // WHEN THEN
    assertThat(getConstraints(db, label), containsOnly(constraint1));
}
Also used : ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) Test(org.junit.Test)

Example 29 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.

the class SchemaAcceptanceTest method createUniquenessConstraint.

private ConstraintDefinition createUniquenessConstraint(Label label, String prop) {
    try (Transaction tx = db.beginTx()) {
        ConstraintDefinition constraint = db.schema().constraintFor(label).assertPropertyIsUnique(prop).create();
        tx.success();
        return constraint;
    }
}
Also used : ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 30 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.

the class SchemaWithPECAcceptanceTest method shouldListAddedConstraintsByRelationshipType.

@Test
public void shouldListAddedConstraintsByRelationshipType() throws Exception {
    // GIVEN
    ConstraintDefinition constraint1 = createRelationshipPropertyExistenceConstraint(Types.MY_TYPE, propertyKey);
    createRelationshipPropertyExistenceConstraint(Types.MY_OTHER_TYPE, propertyKey);
    // WHEN THEN
    assertThat(getConstraints(db, Types.MY_TYPE), containsOnly(constraint1));
}
Also used : UniquenessConstraintDefinition(org.neo4j.kernel.impl.coreapi.schema.UniquenessConstraintDefinition) RelationshipPropertyExistenceConstraintDefinition(org.neo4j.kernel.impl.coreapi.schema.RelationshipPropertyExistenceConstraintDefinition) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) NodePropertyExistenceConstraintDefinition(org.neo4j.kernel.impl.coreapi.schema.NodePropertyExistenceConstraintDefinition) Test(org.junit.Test)

Aggregations

ConstraintDefinition (org.neo4j.graphdb.schema.ConstraintDefinition)34 Test (org.junit.Test)16 Transaction (org.neo4j.graphdb.Transaction)12 NodePropertyExistenceConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.NodePropertyExistenceConstraintDefinition)5 RelationshipPropertyExistenceConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.RelationshipPropertyExistenceConstraintDefinition)5 UniquenessConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.UniquenessConstraintDefinition)5 NodeRepresentationTest (org.neo4j.server.rest.repr.NodeRepresentationTest)3 RelationshipRepresentationTest (org.neo4j.server.rest.repr.RelationshipRepresentationTest)3 ArrayList (java.util.ArrayList)2 ConstraintCreator (org.neo4j.graphdb.schema.ConstraintCreator)2 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)2 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)2 BatchInserter (org.neo4j.unsafe.batchinsert.BatchInserter)2 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)1 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)1 Label (org.neo4j.graphdb.Label)1 RelationshipType (org.neo4j.graphdb.RelationshipType)1 Schema (org.neo4j.graphdb.schema.Schema)1 ConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)1 Documented (org.neo4j.kernel.impl.annotations.Documented)1