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;
}
}
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();
}
}
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();
}
}
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;
}
Aggregations