Search in sources :

Example 56 with ConstraintDefinition

use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j-apoc-procedures by neo4j-contrib.

the class SchemasTest method testCreateSchema.

@Test
public void testCreateSchema() throws Exception {
    testCall(db, "CALL apoc.schema.assert(null,{Foo:['bar']})", (r) -> {
        assertEquals("Foo", r.get("label"));
        assertEquals("bar", r.get("key"));
        assertEquals(true, r.get("unique"));
        assertEquals("CREATED", r.get("action"));
    });
    try (Transaction tx = db.beginTx()) {
        List<ConstraintDefinition> constraints = Iterables.asList(db.schema().getConstraints(Label.label("Foo")));
        assertEquals(1, constraints.size());
        ConstraintDefinition constraint = constraints.get(0);
        assertEquals(ConstraintType.UNIQUENESS, constraint.getConstraintType());
        assertEquals("Foo", constraint.getLabel().name());
        assertEquals("bar", Iterables.single(constraint.getPropertyKeys()));
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) Test(org.junit.Test)

Example 57 with ConstraintDefinition

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

the class SubGraphExporter method exportConstraints.

private Collection<String> exportConstraints() {
    Iterable<ConstraintDefinition> constraints = graph.getConstraints();
    int count = 0;
    if (constraints instanceof Collection) {
        count = ((Collection<ConstraintDefinition>) constraints).size();
    } else {
        for (ConstraintDefinition ignored : constraints) {
            count++;
        }
    }
    final List<String> result = new ArrayList<>(count);
    for (ConstraintDefinition constraint : constraints) {
        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 id = propertyKeys.next();
        if (propertyKeys.hasNext()) {
            throw new RuntimeException("Exporting compound constraints is not implemented yet");
        }
        String name = quote(constraint.getName());
        String key = quote(id);
        String label = quote(constraint.getLabel().name());
        result.add("create constraint " + name + " for (n:" + label + ") require n." + key + " is unique");
    }
    Collections.sort(result);
    return result;
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 58 with ConstraintDefinition

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

the class JUnitDocIT method shouldUserEnterpriseFeatures.

// end::useEnterpriseJUnitRule[]
@Test
public void shouldUserEnterpriseFeatures() throws Exception {
    // Given
    GraphDatabaseService db = neo4j.defaultDatabaseService();
    // When I create property existence constraint
    try (Transaction tx = db.beginTx()) {
        try (Result result = tx.execute("CREATE CONSTRAINT FOR (user:User) REQUIRE user.name IS NOT NULL")) {
        // nothing to-do
        }
        tx.commit();
    }
    // Then I can access created constraint
    try (Transaction tx = db.beginTx()) {
        ConstraintDefinition constraint = single(tx.schema().getConstraints());
        assertTrue(constraint.isConstraintType(ConstraintType.NODE_PROPERTY_EXISTENCE));
        assertEquals(Label.label("User"), constraint.getLabel());
        assertEquals("name", single(constraint.getPropertyKeys()));
        tx.commit();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Result(org.neo4j.graphdb.Result) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) Test(org.junit.Test)

Example 59 with ConstraintDefinition

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

the class GraphDatabaseServiceCleaner method cleanupSchema.

public static void cleanupSchema(GraphDatabaseService db) {
    try (Transaction tx = db.beginTx()) {
        var schema = tx.schema();
        for (ConstraintDefinition constraint : schema.getConstraints()) {
            constraint.drop();
        }
        for (IndexDefinition index : schema.getIndexes()) {
            index.drop();
        }
        tx.commit();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 60 with ConstraintDefinition

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

the class SchemaConstraintsDocIT method drop_constraint.

@Documented("Drop uniqueness constraint.\n" + "Drop uniqueness constraint for a label and a property.")
@Test
@GraphDescription.Graph
public void drop_constraint() throws Exception {
    data.get();
    String labelName = labels.newInstance(), propertyKey = properties.newInstance();
    ConstraintDefinition constraintDefinition = createLabelUniquenessPropertyConstraint(labelName, propertyKey);
    assertThat(getConstraints(graphdb(), label(labelName)), containsOnly(constraintDefinition));
    gen.get().noGraph().expectedStatus(204).delete(getSchemaConstraintLabelUniquenessPropertyUri(labelName, propertyKey)).entity();
    assertThat(getConstraints(graphdb(), label(labelName)), isEmpty());
}
Also used : ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) Documented(org.neo4j.kernel.impl.annotations.Documented) Test(org.junit.Test)

Aggregations

ConstraintDefinition (org.neo4j.graphdb.schema.ConstraintDefinition)70 Transaction (org.neo4j.graphdb.Transaction)26 Test (org.junit.Test)18 Test (org.junit.jupiter.api.Test)18 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 RepeatedTest (org.junit.jupiter.api.RepeatedTest)16 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)14 ConstraintCreator (org.neo4j.graphdb.schema.ConstraintCreator)7 ArrayList (java.util.ArrayList)5 Schema (org.neo4j.graphdb.schema.Schema)5 NodePropertyExistenceConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.NodePropertyExistenceConstraintDefinition)5 RelationshipPropertyExistenceConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.RelationshipPropertyExistenceConstraintDefinition)5 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)4 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)4 UniquenessConstraintDefinition (org.neo4j.kernel.impl.coreapi.schema.UniquenessConstraintDefinition)4 Label (org.neo4j.graphdb.Label)3 NodeRepresentationTest (org.neo4j.server.rest.repr.NodeRepresentationTest)3 RelationshipRepresentationTest (org.neo4j.server.rest.repr.RelationshipRepresentationTest)3 AssertSchemaResult (apoc.result.AssertSchemaResult)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2