Search in sources :

Example 31 with ConstraintDefinition

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

the class Neo4jMatchers method createConstraint.

public static ConstraintDefinition createConstraint(GraphDatabaseService db, Label label, String propertyKey) {
    try (Transaction tx = db.beginTx()) {
        ConstraintDefinition constraint = db.schema().constraintFor(label).assertPropertyIsUnique(propertyKey).create();
        tx.success();
        return constraint;
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 32 with ConstraintDefinition

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

the class BatchInsertTest method shouldCreateUniquenessConstraint.

@Test
public void shouldCreateUniquenessConstraint() throws Exception {
    // Given
    Label label = label("Person");
    String propertyKey = "name";
    String duplicatedValue = "Tom";
    BatchInserter inserter = newBatchInserter();
    // When
    inserter.createDeferredConstraint(label).assertPropertyIsUnique(propertyKey).create();
    // Then
    GraphDatabaseService db = switchToEmbeddedGraphDatabaseService(inserter);
    try {
        try (Transaction tx = db.beginTx()) {
            List<ConstraintDefinition> constraints = Iterables.asList(db.schema().getConstraints());
            assertEquals(1, constraints.size());
            ConstraintDefinition constraint = constraints.get(0);
            assertEquals(label.name(), constraint.getLabel().name());
            assertEquals(propertyKey, single(constraint.getPropertyKeys()));
            db.createNode(label).setProperty(propertyKey, duplicatedValue);
            tx.success();
        }
        try (Transaction tx = db.beginTx()) {
            db.createNode(label).setProperty(propertyKey, duplicatedValue);
            tx.success();
        }
        fail("Uniqueness property constraint was violated, exception expected");
    } catch (ConstraintViolationException e) {
        assertEquals(format("Node(0) already exists with label `%s` and property `%s` = '%s'", label.name(), propertyKey, duplicatedValue), e.getMessage());
    } finally {
        db.shutdown();
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Label(org.neo4j.graphdb.Label) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) Test(org.junit.Test)

Example 33 with ConstraintDefinition

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

the class UniquenessRecoveryTest method shouldHaveUniquenessConstraintForNamePropertyOnPersonLabel.

// ASSERTIONS
private static void shouldHaveUniquenessConstraintForNamePropertyOnPersonLabel(GraphDatabaseService db) {
    try (Transaction tx = db.beginTx()) {
        ConstraintDefinition constraint = Iterables.single(db.schema().getConstraints());
        assertEquals(ConstraintType.UNIQUENESS, constraint.getConstraintType());
        assertEquals("Person", constraint.getLabel().name());
        assertEquals("name", Iterables.single(constraint.getPropertyKeys()));
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 34 with ConstraintDefinition

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

the class DatabaseActions method dropRelationshipPropertyExistenceConstraint.

public boolean dropRelationshipPropertyExistenceConstraint(String typeName, Iterable<String> propertyKeys) {
    final Set<String> propertyKeysSet = Iterables.asSet(propertyKeys);
    ConstraintDefinition constraint = Iterables.singleOrNull(filteredRelationshipConstraints(typeName, relationshipPropertyExistenceFilter(propertyKeysSet)));
    if (constraint != null) {
        constraint.drop();
    }
    return constraint != null;
}
Also used : ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

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