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