private void dropConstraint( ConstraintDescriptor descriptor ) throws KernelException { try ( Transaction transaction = kernel.beginTransaction( implicit, AUTH_DISABLED ) ) { transaction.schemaWrite().constraintDrop( descriptor ); transaction.success(); } }
@Test public void shouldNotSeeDroppedNodePropertyExistenceConstraintFromTransaction() throws Exception { ConstraintDescriptor existing; try ( Transaction transaction = beginTransaction() ) { existing = transaction.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( existing ); SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( existing ) ); assertThat( schemaRead.index( label, prop2 ), equalTo( NO_INDEX ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), empty() ); } }
@Test public void shouldNotSeeDroppedRelationshipPropertyExistenceConstraintFromTransaction() throws Exception { ConstraintDescriptor existing; try ( Transaction transaction = beginTransaction() ) { existing = transaction.schemaWrite() .relationshipPropertyExistenceConstraintCreate( typeDescriptor( type, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( existing ); SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( existing ) ); assertThat( schemaRead.index( type, prop2 ), equalTo( NO_INDEX ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), empty() ); } }
@Before public void setUp() throws Exception { try ( Transaction transaction = beginTransaction() ) { SchemaRead schemaRead = transaction.schemaRead(); SchemaWrite schemaWrite = transaction.schemaWrite(); Iterator<ConstraintDescriptor> constraints = schemaRead.constraintsGetAll(); while ( constraints.hasNext() ) { schemaWrite.constraintDrop( constraints.next() ); } Iterator<IndexReference> indexes = schemaRead.indexesGetAll(); while ( indexes.hasNext() ) { schemaWrite.indexDrop( indexes.next() ); } TokenWrite tokenWrite = transaction.tokenWrite(); label = tokenWrite.labelGetOrCreateForName( "label" ); label2 = tokenWrite.labelGetOrCreateForName( "label2" ); type = tokenWrite.relationshipTypeGetOrCreateForName( "relationship" ); prop1 = tokenWrite.propertyKeyGetOrCreateForName( "prop1" ); prop2 = tokenWrite.propertyKeyGetOrCreateForName( "prop2" ); prop3 = tokenWrite.propertyKeyGetOrCreateForName( "prop3" ); transaction.success(); } }
@Override public void dropNodePropertyExistenceConstraint( Label label, String[] properties ) { KernelTransaction transaction = safeAcquireTransaction( transactionSupplier ); try ( Statement ignore = transaction.acquireStatement() ) { try { TokenRead tokenRead = transaction.tokenRead(); int labelId = tokenRead.nodeLabel( label.name() ); int[] propertyKeyIds = resolveAndValidatePropertyKeys( tokenRead, properties ); transaction.schemaWrite().constraintDrop( ConstraintDescriptorFactory.existsForLabel( labelId, propertyKeyIds ) ); } catch ( DropConstraintFailureException e ) { throw new ConstraintViolationException( e.getUserMessage( new SilentTokenNameLookup( transaction.tokenRead() ) ), e ); } catch ( InvalidTransactionTypeKernelException | SchemaKernelException e ) { throw new ConstraintViolationException( e.getMessage(), e ); } } }
@Test public void shouldNotSeeDroppedUniqueConstraintFromTransaction() throws Exception { ConstraintDescriptor existing; try ( Transaction transaction = beginTransaction() ) { existing = transaction.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( existing ); SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( existing ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), empty() ); } }
@Test public void shouldNotSeeDroppedNodeKeyConstraintFromTransaction() throws Exception { ConstraintDescriptor existing; try ( Transaction transaction = beginTransaction() ) { existing = transaction.schemaWrite().nodeKeyConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( existing ); SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( existing ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), empty() ); } }
@Override public void dropPropertyUniquenessConstraint( Label label, String[] properties ) { KernelTransaction transaction = safeAcquireTransaction( transactionSupplier ); try ( Statement ignore = transaction.acquireStatement() ) { try { TokenRead tokenRead = transaction.tokenRead(); int labelId = tokenRead.nodeLabel( label.name() ); int[] propertyKeyIds = resolveAndValidatePropertyKeys( tokenRead, properties ); transaction.schemaWrite().constraintDrop( ConstraintDescriptorFactory.uniqueForLabel( labelId, propertyKeyIds ) ); } catch ( DropConstraintFailureException e ) { throw new ConstraintViolationException( e.getUserMessage( new SilentTokenNameLookup( transaction.tokenRead() ) ), e ); } catch ( InvalidTransactionTypeKernelException | SchemaKernelException e ) { throw new ConstraintViolationException( e.getMessage(), e ); } } }
@Override public void dropNodeKeyConstraint( Label label, String[] properties ) { KernelTransaction transaction = safeAcquireTransaction( transactionSupplier ); try ( Statement ignore = transaction.acquireStatement() ) { try { TokenRead tokenRead = transaction.tokenRead(); int labelId = tokenRead.nodeLabel( label.name() ); int[] propertyKeyIds = resolveAndValidatePropertyKeys( tokenRead, properties ); transaction.schemaWrite().constraintDrop( ConstraintDescriptorFactory.nodeKeyForLabel( labelId, propertyKeyIds ) ); } catch ( DropConstraintFailureException e ) { throw new ConstraintViolationException( e.getUserMessage( new SilentTokenNameLookup( transaction.tokenRead() ) ), e ); } catch ( InvalidTransactionTypeKernelException | SchemaKernelException e ) { throw new ConstraintViolationException( e.getMessage(), e ); } } }
@Override public void dropRelationshipPropertyExistenceConstraint( RelationshipType type, String propertyKey ) { KernelTransaction transaction = safeAcquireTransaction( transactionSupplier ); try ( Statement ignore = transaction.acquireStatement() ) { try { TokenRead tokenRead = transaction.tokenRead(); int typeId = tokenRead.relationshipType( type.name() ); int propertyKeyId = tokenRead.propertyKey( propertyKey ); transaction.schemaWrite().constraintDrop( ConstraintDescriptorFactory.existsForRelType( typeId, propertyKeyId ) ); } catch ( DropConstraintFailureException e ) { throw new ConstraintViolationException( e.getUserMessage( new SilentTokenNameLookup( transaction.tokenRead() ) ), e ); } catch ( InvalidTransactionTypeKernelException | SchemaKernelException e ) { throw new ConstraintViolationException( e.getMessage(), e ); } } }
@Test public void shouldDropNodeKeyConstraint() throws Exception { ConstraintDescriptor constraint; try ( Transaction transaction = beginTransaction() ) { constraint = transaction.schemaWrite().nodeKeyConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( constraint ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( constraint ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), empty() ); } }
@Test public void shouldDropNodePropertyExistenceConstraint() throws Exception { ConstraintDescriptor constraint; try ( Transaction transaction = beginTransaction() ) { constraint = transaction.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( constraint ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( constraint ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), empty() ); } }
@Test public void shouldDropUniquePropertyConstraint() throws Exception { ConstraintDescriptor constraint; try ( Transaction transaction = beginTransaction() ) { constraint = transaction.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( constraint ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( constraint ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), empty() ); } }
@Test public void shouldDropRelationshipPropertyExistenceConstraint() throws Exception { ConstraintDescriptor constraint; try ( Transaction transaction = beginTransaction() ) { constraint = transaction.schemaWrite() .relationshipPropertyExistenceConstraintCreate( typeDescriptor( type, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( constraint ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( constraint ) ); assertThat( asList( schemaRead.constraintsGetForRelationshipType( type ) ), empty() ); } }
@Test public void shouldListAllConstraints() throws Exception { ConstraintDescriptor toRetain; ConstraintDescriptor toRetain2; ConstraintDescriptor toDrop; ConstraintDescriptor created; try ( Transaction tx = beginTransaction() ) { toRetain = tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); toRetain2 = tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label2, prop1 ) ); toDrop = tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop2 ) ); tx.success(); } try ( Transaction tx = beginTransaction() ) { created = tx.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop1 ) ); tx.schemaWrite().constraintDrop( toDrop ); Iterable<ConstraintDescriptor> allConstraints = () -> tx.schemaRead().constraintsGetAll(); assertThat( allConstraints, containsInAnyOrder( toRetain, toRetain2, created ) ); tx.success(); } }
@Test public void shouldListConstraintsByLabel() throws Exception { int wrongLabel; ConstraintDescriptor inStore; ConstraintDescriptor droppedInTx; ConstraintDescriptor createdInTx; try ( Transaction tx = beginTransaction() ) { wrongLabel = tx.tokenWrite().labelGetOrCreateForName( "wrongLabel" ); tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( wrongLabel, prop1 ) ); inStore = tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); droppedInTx = tx.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop2 ) ); tx.success(); } try ( Transaction tx = beginTransaction() ) { createdInTx = tx.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop1 ) ); tx.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( wrongLabel, prop1 ) ); tx.schemaWrite().constraintDrop( droppedInTx ); Iterable<ConstraintDescriptor> allConstraints = () -> tx.schemaRead().constraintsGetForLabel( label ); assertThat( allConstraints, containsInAnyOrder( inStore, createdInTx ) ); tx.success(); } }
@After public void clean() throws Exception { try ( Transaction tx = graphDatabaseAPI.beginTx() ) { KernelTransaction ktx = ktx(); if ( index.type() == UNIQUE ) { ktx.schemaWrite().constraintDrop( ConstraintDescriptorFactory.uniqueForSchema( index.schema() ) ); } else { ktx.schemaWrite().indexDrop( index ); } tx.success(); } try ( Transaction tx = graphDatabaseAPI.beginTx() ) { for ( Node node : graphDatabaseAPI.getAllNodes() ) { node.delete(); } tx.success(); } }
@After public void clean() throws Exception { if ( transaction != null ) { transaction.close(); } newTransaction(); transaction.schemaWrite() .constraintDrop( ConstraintDescriptorFactory.uniqueForLabel( label, propertyIds() ) ); commit(); try ( Transaction tx = kernel.beginTransaction( Transaction.Type.implicit, LoginContext.AUTH_DISABLED ); NodeCursor node = tx.cursors().allocateNodeCursor() ) { tx.dataRead().allNodesScan( node ); while ( node.next() ) { tx.dataWrite().nodeDelete( node.nodeReference() ); } tx.success(); } }
@Override public void dropPropertyUniquenessConstraint( Label label, String[] properties ) { KernelTransaction transaction = safeAcquireTransaction( transactionSupplier ); try ( Statement ignore = transaction.acquireStatement() ) { try { TokenRead tokenRead = transaction.tokenRead(); int labelId = tokenRead.nodeLabel( label.name() ); int[] propertyKeyIds = resolveAndValidatePropertyKeys( tokenRead, properties ); transaction.schemaWrite().constraintDrop( ConstraintDescriptorFactory.uniqueForLabel( labelId, propertyKeyIds ) ); } catch ( DropConstraintFailureException e ) { throw new ConstraintViolationException( e.getUserMessage( new SilentTokenNameLookup( transaction.tokenRead() ) ), e ); } catch ( InvalidTransactionTypeKernelException | SchemaKernelException e ) { throw new ConstraintViolationException( e.getMessage(), e ); } } }
@Override public void dropNodeKeyConstraint( Label label, String[] properties ) { KernelTransaction transaction = safeAcquireTransaction( transactionSupplier ); try ( Statement ignore = transaction.acquireStatement() ) { try { TokenRead tokenRead = transaction.tokenRead(); int labelId = tokenRead.nodeLabel( label.name() ); int[] propertyKeyIds = resolveAndValidatePropertyKeys( tokenRead, properties ); transaction.schemaWrite().constraintDrop( ConstraintDescriptorFactory.nodeKeyForLabel( labelId, propertyKeyIds ) ); } catch ( DropConstraintFailureException e ) { throw new ConstraintViolationException( e.getUserMessage( new SilentTokenNameLookup( transaction.tokenRead() ) ), e ); } catch ( InvalidTransactionTypeKernelException | SchemaKernelException e ) { throw new ConstraintViolationException( e.getMessage(), e ); } } }