use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.
the class GraphDatabaseServiceCleaner method cleanupSchema.
public static void cleanupSchema(GraphDatabaseService db) {
try (Transaction tx = db.beginTx()) {
for (ConstraintDefinition constraint : tx.schema().getConstraints()) {
constraint.drop();
}
for (IndexDefinition index : tx.schema().getIndexes()) {
index.drop();
}
tx.commit();
}
// re-create the default indexes
try (Transaction tx = db.beginTx()) {
tx.schema().indexFor(AnyTokens.ANY_RELATIONSHIP_TYPES).withName("rti").create();
tx.schema().indexFor(AnyTokens.ANY_LABELS).withName("lti").create();
tx.commit();
}
try (Transaction tx = db.beginTx()) {
tx.schema().awaitIndexesOnline(1, TimeUnit.MINUTES);
}
}
use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.
the class BatchInsertTest method shouldCreateUniquenessConstraint.
@ParameterizedTest
@MethodSource("params")
void shouldCreateUniquenessConstraint(int denseNodeThreshold) throws Exception {
// Given
Label label = label("Person");
String propertyKey = "name";
String duplicatedValue = "Tom";
var inserter = newBatchInserter(denseNodeThreshold);
// When
inserter.createDeferredConstraint(label).assertPropertyIsUnique(propertyKey).create();
// Then
GraphDatabaseService db = switchToEmbeddedGraphDatabaseService(inserter, denseNodeThreshold);
try {
try (Transaction tx = db.beginTx()) {
List<ConstraintDefinition> constraints = Iterables.asList(tx.schema().getConstraints());
assertEquals(1, constraints.size());
ConstraintDefinition constraint = constraints.get(0);
assertEquals(label.name(), constraint.getLabel().name());
assertEquals(propertyKey, single(constraint.getPropertyKeys()));
tx.createNode(label).setProperty(propertyKey, duplicatedValue);
tx.commit();
}
try (Transaction tx = db.beginTx()) {
tx.createNode(label).setProperty(propertyKey, duplicatedValue);
tx.commit();
}
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 {
managementService.shutdown();
}
}
use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.
the class SchemaAcceptanceTest method crashDuringIndexPopulationOfConstraint.
/**
* This test describes a problem where if you crash during index population
* when creating a constraint the constraint will never be created but the
* index will and after next startup only the index will be there.
*
* We need to specifically drop the index in separate transaction to be
* able to create the constraint again.
*
* A better behaviour would be that the index is never created and is not
* present after crash.
*/
@Test
void crashDuringIndexPopulationOfConstraint() throws InterruptedException {
// Given
trapPopulation.set(true);
otherThreadRule.execute(() -> {
try (Transaction tx = db.beginTx()) {
tx.schema().constraintFor(label).assertPropertyIsUnique(propertyKey).withName(nameA).create();
tx.commit();
}
return null;
});
// When
// Crashing during index population of index backing a constraint
populationScanFinished.await();
EphemeralFileSystemAbstraction crash = fs.snapshot();
populationScanFinished.release();
// Then
// On next startup
controller.restartDbms(builder -> {
builder.setFileSystem(crash);
return builder;
});
// the index is online but constraint is missing... which is sub-optimal
try (Transaction tx = db.beginTx()) {
tx.schema().awaitIndexesOnline(1, TimeUnit.HOURS);
tx.commit();
}
try (Transaction tx = db.beginTx()) {
Iterable<ConstraintDefinition> constraints = tx.schema().getConstraints();
Iterable<IndexDefinition> indexes = tx.schema().getIndexes();
assertThat(count(constraints)).isEqualTo(0);
assertThat(count(indexes)).isEqualTo(1);
indexes.forEach(index -> assertThat(getIndexState(tx, index)).isEqualTo(ONLINE));
tx.commit();
}
// and we cannot drop the constraint because it was never created
{
QueryExecutionException e = assertThrows(QueryExecutionException.class, () -> {
try (Transaction tx = db.beginTx()) {
tx.execute("DROP CONSTRAINT `" + nameA + "`");
tx.commit();
}
});
assertThat(e).hasRootCauseInstanceOf(NoSuchConstraintException.class);
}
// and we cannot re-create the constraint because index is blocking us
{
ConstraintViolationException e = assertThrows(ConstraintViolationException.class, () -> {
try (Transaction tx = db.beginTx()) {
tx.schema().constraintFor(label).assertPropertyIsUnique(propertyKey).withName(nameA).create();
tx.commit();
}
});
assertThat(e).hasRootCauseInstanceOf(IndexWithNameAlreadyExistsException.class);
}
// dropping the index in separate transaction
try (Transaction tx = db.beginTx()) {
tx.schema().getIndexByName(nameA).drop();
tx.commit();
}
// we can finally create the constraint again.
try (Transaction tx = db.beginTx()) {
tx.schema().constraintFor(label).assertPropertyIsUnique(propertyKey).withName(nameA).create();
tx.commit();
}
try (Transaction tx = db.beginTx()) {
Iterable<ConstraintDefinition> constraints = tx.schema().getConstraints();
Iterable<IndexDefinition> indexes = tx.schema().getIndexes();
assertThat(count(constraints)).isEqualTo(1);
assertThat(count(indexes)).isEqualTo(1);
indexes.forEach(index -> assertThat(getIndexState(tx, index)).isEqualTo(ONLINE));
tx.commit();
}
}
use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.
the class SchemaAcceptanceTest method shouldGetConstraintByName.
@Test
void shouldGetConstraintByName() {
ConstraintDefinition expectedConstraint = createUniquenessConstraint("MyConstraint", label, propertyKey);
try (Transaction tx = db.beginTx()) {
ConstraintDefinition actualConstraint = tx.schema().getConstraintByName("MyConstraint");
assertThat(actualConstraint).isEqualTo(expectedConstraint);
tx.commit();
}
}
use of org.neo4j.graphdb.schema.ConstraintDefinition in project neo4j by neo4j.
the class SchemaAcceptanceTest method createUniquenessConstraint.
private ConstraintDefinition createUniquenessConstraint(String name, Label label, String prop) {
try (Transaction tx = db.beginTx()) {
ConstraintCreator creator = tx.schema().constraintFor(label);
creator = creator.assertPropertyIsUnique(prop).withName(name);
ConstraintDefinition constraint = creator.create();
tx.commit();
return constraint;
}
}
Aggregations