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