use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class SchemaRuleCommandTest method shouldSetLatestConstraintRule.
@Test
void shouldSetLatestConstraintRule() throws Exception {
// Given
SchemaRecord before = new SchemaRecord(id).initialize(true, 42);
before.setCreated();
SchemaRecord after = new SchemaRecord(id).initialize(true, 42);
after.setConstraint(true);
when(neoStores.getSchemaStore()).thenReturn(schemaStore);
when(neoStores.getMetaDataStore()).thenReturn(metaDataStore);
ConstraintDescriptor schemaRule = ConstraintDescriptorFactory.uniqueForLabel(labelId, propertyKey).withId(id).withOwnedIndexId(0);
// WHEN
visitSchemaRuleCommand(storeApplier, new SchemaRuleCommand(serialization, before, after, schemaRule));
// THEN
verify(schemaStore).updateRecord(eq(after), any(), any());
verify(metaDataStore).setLatestConstraintIntroducingTx(txId, NULL);
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class IndexIT method shouldDisallowDroppingConstraintByNameThatDoesNotExist.
@Test
void shouldDisallowDroppingConstraintByNameThatDoesNotExist() throws KernelException {
// given
String constraintName = "my constraint";
ConstraintDescriptor constraint;
{
SchemaWrite statement = schemaWriteInNewTransaction();
constraint = statement.uniquePropertyConstraintCreate(uniqueForSchema(schema).withName("constraint name"));
commit();
}
{
SchemaWrite statement = schemaWriteInNewTransaction();
statement.constraintDrop(constraint);
commit();
}
// when
SchemaWrite statement = schemaWriteInNewTransaction();
SchemaKernelException e = assertThrows(SchemaKernelException.class, () -> statement.constraintDrop(constraintName));
assertEquals("Unable to drop constraint `my constraint`: No such constraint my constraint.", e.getMessage());
rollback();
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class SchemaChecker method performSchemaCheck.
private void performSchemaCheck(long highId, RecordReader<SchemaRecord> reader, Map<Long, SchemaRecord> indexObligations, Map<Long, SchemaRecord> constraintObligations, SchemaStorage schemaStorage, MutableIntObjectMap<MutableIntSet> mandatoryNodeProperties, MutableIntObjectMap<MutableIntSet> mandatoryRelationshipProperties, CursorContext cursorContext) {
SchemaRecord record = reader.record();
SchemaProcessor basicSchemaCheck = new BasicSchemaCheck(record, cursorContext);
SchemaProcessor mandatoryPropertiesBuilder = new MandatoryPropertiesBuilder(mandatoryNodeProperties, mandatoryRelationshipProperties);
for (long id = schemaStore.getNumberOfReservedLowIds(); id < highId && !context.isCancelled(); id++) {
try {
reader.read(id);
if (record.inUse()) {
SchemaRule schemaRule = schemaStorage.loadSingleSchemaRule(id, cursorContext);
schemaRule.schema().processWith(basicSchemaCheck);
if (schemaRule instanceof IndexDescriptor) {
IndexDescriptor rule = (IndexDescriptor) schemaRule;
if (rule.isUnique()) {
SchemaRecord obligation = indexObligations.get(rule.getId());
if (// no pointer to here
obligation == null) {
if (// we only expect a pointer if we have an owner
rule.getOwningConstraintId().isPresent()) {
reporter.forSchema(record).missingObligation(SchemaRuleKind.UNIQUENESS_CONSTRAINT.name());
}
} else {
// if someone points to here, it must be our owner
OptionalLong owningConstraintId = rule.getOwningConstraintId();
if (owningConstraintId.isEmpty() || obligation.getId() != owningConstraintId.getAsLong()) {
reporter.forSchema(record).constraintIndexRuleNotReferencingBack(obligation);
}
}
}
if (indexAccessors.notOnlineRules().contains(rule)) {
reporter.forSchema(record).schemaRuleNotOnline(rule);
}
if (indexAccessors.inconsistentRules().contains(rule)) {
reporter.forSchema(record).malformedSchemaRule();
}
} else if (schemaRule instanceof ConstraintDescriptor) {
ConstraintDescriptor rule = (ConstraintDescriptor) schemaRule;
if (rule.enforcesUniqueness()) {
SchemaRecord obligation = constraintObligations.get(rule.getId());
if (obligation == null) {
reporter.forSchema(record).missingObligation(SchemaRuleKind.CONSTRAINT_INDEX_RULE.name());
} else {
if (obligation.getId() != rule.asIndexBackedConstraint().ownedIndexId()) {
reporter.forSchema(record).uniquenessConstraintNotReferencingBack(obligation);
}
}
}
if (rule.enforcesPropertyExistence()) {
rule.schema().processWith(mandatoryPropertiesBuilder);
}
} else {
reporter.forSchema(record).unsupportedSchemaRuleType(null);
}
}
} catch (MalformedSchemaRuleException e) {
reporter.forSchema(record).malformedSchemaRule();
}
}
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class Operations method constraintDrop.
@Override
public void constraintDrop(SchemaDescriptor schema, ConstraintType type) throws SchemaKernelException {
ktx.assertOpen();
Iterator<ConstraintDescriptor> constraints = ktx.schemaRead().constraintsGetForSchema(schema);
constraints = Iterators.filter(constraint -> constraint.type() == type, constraints);
if (constraints.hasNext()) {
ConstraintDescriptor constraint = constraints.next();
if (!constraints.hasNext()) {
constraintDrop(constraint);
} else {
String schemaDescription = schema.userDescription(token);
String constraintDescription = constraints.next().userDescription(token);
throw new DropConstraintFailureException(constraint, new IllegalArgumentException("More than one " + type + " constraint was found with the '" + schemaDescription + "' schema: " + constraintDescription + ", please drop constraint by name instead."));
}
} else {
throw new DropConstraintFailureException(schema, new NoSuchConstraintException(schema, token));
}
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class TransactionToRecordStateVisitor method visitAddedUniquenessConstraint.
private void visitAddedUniquenessConstraint(UniquenessConstraintDescriptor uniqueConstraint, long constraintId) throws KernelException {
IndexDescriptor indexRule = (IndexDescriptor) schemaStorage.loadSingleSchemaRule(uniqueConstraint.ownedIndexId(), cursorContext);
ConstraintDescriptor constraint = constraintSemantics.createUniquenessConstraintRule(constraintId, uniqueConstraint, indexRule.getId());
schemaStateChanger.createSchemaRule(recordState, constraint);
schemaStateChanger.setConstraintIndexOwner(recordState, indexRule, constraintId);
}
Aggregations