Search in sources :

Example 11 with UniquePropertyValueValidationException

use of org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException in project neo4j by neo4j.

the class UniquenessConstraintValidationIT method shouldEnforceUniquenessConstraintOnAddLabelForStringProperty.

@Test
void shouldEnforceUniquenessConstraintOnAddLabelForStringProperty() throws Exception {
    // given
    constrainedNode("Label1", "key1", "value1");
    KernelTransaction transaction = newTransaction(AnonymousContext.writeToken());
    // when
    long node = createNode(transaction, "key1", "value1");
    try {
        int label = transaction.tokenWrite().labelGetOrCreateForName("Label1");
        transaction.dataWrite().nodeAddLabel(node, label);
        fail("should have thrown exception");
    }// then
     catch (UniquePropertyValueValidationException e) {
        assertThat(e.getUserMessage(transaction.tokenRead())).contains("`key1` = 'value1'");
    }
    commit();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) Test(org.junit.jupiter.api.Test)

Example 12 with UniquePropertyValueValidationException

use of org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException in project neo4j by neo4j.

the class Operations method indexBackedConstraintCreate.

@SuppressWarnings("unchecked")
private <T extends IndexBackedConstraintDescriptor> T indexBackedConstraintCreate(T constraint, IndexPrototype prototype) throws KernelException {
    try {
        if (allStoreHolder.constraintExists(constraint)) {
            throw new AlreadyConstrainedException(constraint, CONSTRAINT_CREATION, token);
        }
        if (prototype.getIndexType() != IndexType.BTREE) {
            throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index with index type " + prototype.getIndexType() + ".");
        }
        if (prototype.schema().isFulltextSchemaDescriptor()) {
            throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index using a full-text schema: " + prototype.schema().userDescription(token));
        }
        if (prototype.schema().isRelationshipTypeSchemaDescriptor()) {
            throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index using a relationship type schema: " + prototype.schema().userDescription(token));
        }
        if (prototype.schema().isAnyTokenSchemaDescriptor()) {
            throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index using an any token schema: " + prototype.schema().userDescription(token));
        }
        if (!prototype.isUnique()) {
            throw new CreateConstraintFailureException(constraint, "Cannot create index backed constraint using an index prototype that is not unique: " + prototype.userDescription(token));
        }
        IndexDescriptor index = constraintIndexCreator.createUniquenessConstraintIndex(ktx, constraint, prototype);
        if (!allStoreHolder.constraintExists(constraint)) {
            // This looks weird, but since we release the label lock while awaiting population of the index
            // backing this constraint there can be someone else getting ahead of us, creating this exact
            // constraint
            // before we do, so now getting out here under the lock we must check again and if it exists
            // we must at this point consider this an idempotent operation because we verified earlier
            // that it didn't exist and went on to create it.
            constraint = (T) constraint.withOwnedIndexId(index.getId());
            ktx.txState().constraintDoAdd(constraint, index);
        } else {
            constraint = (T) allStoreHolder.constraintsGetForSchema(constraint.schema());
        }
        return constraint;
    } catch (UniquePropertyValueValidationException | TransactionFailureException | AlreadyConstrainedException e) {
        throw new CreateConstraintFailureException(constraint, e);
    }
}
Also used : UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) CreateConstraintFailureException(org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException)

Aggregations

UniquePropertyValueValidationException (org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException)12 Test (org.junit.jupiter.api.Test)5 IndexEntryConflictException (org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)5 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)4 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)3 UniquenessConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor)3 IOException (java.io.IOException)2 TransactionFailureException (org.neo4j.internal.kernel.api.exceptions.TransactionFailureException)2 CreateConstraintFailureException (org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException)2 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)2 IndexNotFoundKernelException (org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException)2 IndexPopulationFailedKernelException (org.neo4j.kernel.api.exceptions.index.IndexPopulationFailedKernelException)2 AlreadyConstrainedException (org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException)2 CreateConstraintFailureException (org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)2 IndexBrokenKernelException (org.neo4j.kernel.api.exceptions.schema.IndexBrokenKernelException)2 UnableToValidateConstraintException (org.neo4j.kernel.api.exceptions.schema.UnableToValidateConstraintException)2 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)2 IndexProxy (org.neo4j.kernel.impl.api.index.IndexProxy)2 IndexingService (org.neo4j.kernel.impl.api.index.IndexingService)2 Client (org.neo4j.kernel.impl.locking.Locks.Client)2