Search in sources :

Example 61 with ConstraintDefinition

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);
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 62 with ConstraintDefinition

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();
    }
}
Also used : 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) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 63 with ConstraintDefinition

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();
    }
}
Also used : NoSuchConstraintException(org.neo4j.kernel.api.exceptions.schema.NoSuchConstraintException) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) IndexWithNameAlreadyExistsException(org.neo4j.kernel.api.exceptions.schema.IndexWithNameAlreadyExistsException) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 64 with ConstraintDefinition

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();
    }
}
Also used : ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 65 with ConstraintDefinition

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;
    }
}
Also used : ConstraintCreator(org.neo4j.graphdb.schema.ConstraintCreator) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

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