Skip to content

Commit

Permalink
fix: delete page revisions when api is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
mukul-tyagi08 committed Oct 29, 2024
1 parent 4d98e31 commit e3e9992
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ public interface PageRevisionRepository extends FindAllRepository<PageRevision>
Optional<PageRevision> findLastByPageId(String pageId) throws TechnicalException;

List<String> deleteByPageId(String pageId) throws TechnicalException;

void deleteAllByPageId(String pageId) throws TechnicalException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,21 @@ public List<String> deleteByPageId(String pageId) throws TechnicalException {
throw new TechnicalException("Failed to delete page revision by page id", ex);
}
}

@Override
public void deleteAllByPageId(String pageId) throws TechnicalException {
LOGGER.debug("JdbcPageRevisionRepository.deleteAllByPageId({})", pageId);
try {
String sql = "DELETE FROM " + this.tableName + " WHERE page_id = ?";
int rowsAffected = jdbcTemplate.update(sql, pageId);
LOGGER.debug("JdbcPageRevisionRepository.deleteAllByPageId({}) = {} rows deleted", pageId, rowsAffected);

if (rowsAffected == 0) {
LOGGER.warn("No revisions found for page id: {}", pageId);
}
} catch (final Exception ex) {
LOGGER.error("Failed to delete revisions by page id: {}", pageId, ex);
throw new TechnicalException("Failed to delete revisions by page id", ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ public Optional<PageRevision> findLastByPageId(String pageId) throws TechnicalEx
}
}

@Override
public void deleteAllByPageId(String pageId) throws TechnicalException {
try {
internalPageRevisionRepo.deleteAllByPageId(pageId);
} catch (Exception e) {
logger.error("An error occurred when deleting revision for page [{}]", pageId, e);
throw new TechnicalException("An error occurred when deleting the page revisions");
}
}

@Override
public Set<PageRevision> findAll() throws TechnicalException {
return internalPageRevisionRepo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public interface PageRevisionMongoRepository
@Query(value = "{ '_id.pageId' : ?0 }", sort = "{ 'revision': 1 }", delete = true)
List<PageRevisionMongo> deleteByPageId(String pageId);

@Query(value = "{ '_id.pageId' : ?0 }", delete = true)
void deleteAllByPageId(String pageId);

@Query(value = "{ '_id.pageId' : ?0 }", sort = "{ 'revision': 1 }")
List<PageRevisionMongo> findAllByPageId(String pageId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package io.gravitee.repository.management;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import io.gravitee.common.data.domain.Page;
import io.gravitee.repository.exceptions.TechnicalException;
Expand Down Expand Up @@ -158,4 +160,26 @@ public void should_delete_by_page_id() throws Exception {
assertThat(deleted.size()).isEqualTo(2);
assertThat(nbAfterDeletion).isEqualTo(0);
}

@Test
public void shouldDeleteAllByPageId() throws TechnicalException {
List<PageRevision> revisionsBefore = pageRevisionRepository.findAllByPageId("findByPageId");
assertNotNull(revisionsBefore);
assertEquals(3, revisionsBefore.size());

pageRevisionRepository.deleteAllByPageId("findByPageId");

List<PageRevision> revisionsAfter = pageRevisionRepository.findAllByPageId("findByPageId");
assertNotNull(revisionsAfter);
assertEquals(0, revisionsAfter.size());
}

@Test
public void shouldDoNothingWhenNoRevisionsFoundWhileDeleting() throws TechnicalException {
pageRevisionRepository.deleteAllByPageId("nonExistingPageId");

List<PageRevision> revisionsAfter = pageRevisionRepository.findAllByPageId("nonExistingPageId");
assertNotNull(revisionsAfter);
assertEquals(0, revisionsAfter.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.gravitee.rest.api.service;

import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.management.api.search.Pageable;
import io.gravitee.repository.management.model.Page;
import io.gravitee.rest.api.model.PageRevisionEntity;
Expand All @@ -35,4 +36,6 @@ public interface PageRevisionService {
List<PageRevisionEntity> findAllByPageId(String pageId);

PageRevisionEntity create(Page page);

void deleteAllByPageId(String pageId) throws TechnicalException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public PageRevisionEntity create(Page page) {
}
}

@Override
public void deleteAllByPageId(String pageId) throws TechnicalException {
pageRevisionRepository.deleteAllByPageId(pageId);
}

private PageRevisionEntity convert(PageRevision revision) {
PageRevisionEntity entity = new PageRevisionEntity();
entity.setPageId(revision.getPageId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,9 @@ public void delete(ExecutionContext executionContext, String pageId) {

pageRepository.delete(pageId);

// delete page revisions
pageRevisionService.deleteAllByPageId(pageId);

// delete links and translations related to the page
if (!PageType.LINK.name().equalsIgnoreCase(page.getType()) && !PageType.TRANSLATION.name().equalsIgnoreCase(page.getType())) {
this.deleteRelatedPages(page.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.gravitee.rest.api.model.PageRevisionEntity;
import io.gravitee.rest.api.model.PageType;
import io.gravitee.rest.api.service.AuditService;
import io.gravitee.rest.api.service.common.GraviteeContext;
import io.gravitee.rest.api.service.exceptions.TechnicalManagementException;
import io.gravitee.rest.api.service.impl.PageRevisionServiceImpl;
import java.util.Date;
Expand Down Expand Up @@ -149,4 +150,16 @@ public void shouldNotCreate_Because_InvalidType() throws TechnicalException {
when(page.getType()).thenReturn(PageType.FOLDER.name());
pageRevisionService.create(page);
}

@Test
public void shouldDeletePageRevision() throws TechnicalException {
pageRevisionService.deleteAllByPageId(PAGE_ID);
verify(pageRevisionRepository).deleteAllByPageId(PAGE_ID);
}

@Test(expected = TechnicalException.class)
public void shouldNotDeletePageRevisionBecauseTechnicalException() throws TechnicalException {
doThrow(TechnicalException.class).when(pageRevisionRepository).deleteAllByPageId(PAGE_ID);
pageRevisionService.deleteAllByPageId(PAGE_ID);
}
}

0 comments on commit e3e9992

Please sign in to comment.