chore: Move non-permission repository method to generic repo class (#32738)

## Description
PR to add separate record class to implement the projection on
ApplicationSnapshot repository.

## Automation

/ok-to-test tags="@tag.Sanity"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/8735912421>
> Commit: 5baf4317e0bdb7179fe855c4fe108f9129a31f6b
> Cypress dashboard url: <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=8735912421&attempt=1"
target="_blank">Click here!</a>

<!-- end of auto-generated comment: Cypress test results  -->






<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit


## Summary by CodeRabbit

- **Refactor**
- Enhanced the application snapshot feature to improve performance and
reduce data load by excluding unnecessary data from snapshots.
- **Documentation**
- Updated import statements and method return types for clarity and
consistency.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Abhijeet 2024-04-18 22:15:49 +05:30 committed by GitHub
parent 905c738f82
commit 6e21516900
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 41 additions and 58 deletions

View File

@ -6,7 +6,6 @@ import com.appsmith.server.applications.base.ApplicationService;
import com.appsmith.server.constants.FieldName;
import com.appsmith.server.constants.Url;
import com.appsmith.server.domains.Application;
import com.appsmith.server.domains.ApplicationSnapshot;
import com.appsmith.server.domains.GitAuth;
import com.appsmith.server.domains.Theme;
import com.appsmith.server.dtos.ApplicationAccessDTO;
@ -28,6 +27,7 @@ import com.appsmith.server.exports.internal.partial.PartialExportService;
import com.appsmith.server.fork.internal.ApplicationForkingService;
import com.appsmith.server.imports.internal.ImportService;
import com.appsmith.server.imports.internal.partial.PartialImportService;
import com.appsmith.server.projections.DefaultTimestampOnly;
import com.appsmith.server.services.ApplicationPageService;
import com.appsmith.server.services.ApplicationSnapshotService;
import com.appsmith.server.solutions.ApplicationFetcher;
@ -232,7 +232,7 @@ public class ApplicationControllerCE {
@JsonView(Views.Public.class)
@GetMapping("/snapshot/{id}")
public Mono<ResponseDTO<ApplicationSnapshot>> getSnapshotWithoutApplicationJson(
public Mono<ResponseDTO<DefaultTimestampOnly>> getSnapshotWithoutApplicationJson(
@PathVariable String id, @RequestHeader(name = FieldName.BRANCH_NAME, required = false) String branchName) {
log.debug("Going to get snapshot with application id: {}, branch: {}", id, branchName);

View File

@ -0,0 +1,5 @@
package com.appsmith.server.projections;
import java.time.Instant;
public record DefaultTimestampOnly(Instant updatedAt, Instant createdAt) {}

View File

@ -1,6 +1,7 @@
package com.appsmith.server.repositories.ce;
import com.appsmith.server.domains.ApplicationSnapshot;
import com.appsmith.server.projections.DefaultTimestampOnly;
import com.appsmith.server.repositories.BaseRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@ -10,4 +11,6 @@ public interface ApplicationSnapshotRepositoryCE
Flux<ApplicationSnapshot> findByApplicationId(String applicationId);
Mono<Void> deleteAllByApplicationId(String applicationId);
Mono<DefaultTimestampOnly> findByApplicationIdAndChunkOrder(String applicationId, Integer chunkOrder);
}

View File

@ -2,8 +2,5 @@ package com.appsmith.server.repositories.ce;
import com.appsmith.server.domains.ApplicationSnapshot;
import com.appsmith.server.repositories.AppsmithRepository;
import reactor.core.publisher.Mono;
public interface CustomApplicationSnapshotRepositoryCE extends AppsmithRepository<ApplicationSnapshot> {
Mono<ApplicationSnapshot> findWithoutData(String applicationId);
}
public interface CustomApplicationSnapshotRepositoryCE extends AppsmithRepository<ApplicationSnapshot> {}

View File

@ -1,27 +1,7 @@
package com.appsmith.server.repositories.ce;
import com.appsmith.server.domains.ApplicationSnapshot;
import com.appsmith.server.helpers.ce.bridge.Bridge;
import com.appsmith.server.helpers.ce.bridge.BridgeQuery;
import com.appsmith.server.repositories.BaseAppsmithRepositoryImpl;
import reactor.core.publisher.Mono;
import java.util.List;
public class CustomApplicationSnapshotRepositoryCEImpl extends BaseAppsmithRepositoryImpl<ApplicationSnapshot>
implements CustomApplicationSnapshotRepositoryCE {
@Override
public Mono<ApplicationSnapshot> findWithoutData(String applicationId) {
BridgeQuery<ApplicationSnapshot> query = Bridge.<ApplicationSnapshot>equal(
ApplicationSnapshot.Fields.applicationId, applicationId)
.equal(ApplicationSnapshot.Fields.chunkOrder, 1);
List<String> fieldNames = List.of(
ApplicationSnapshot.Fields.applicationId,
ApplicationSnapshot.Fields.chunkOrder,
ApplicationSnapshot.Fields.createdAt,
ApplicationSnapshot.Fields.updatedAt);
return queryBuilder().criteria(query).fields(fieldNames).one();
}
}
implements CustomApplicationSnapshotRepositoryCE {}

View File

@ -1,7 +1,7 @@
package com.appsmith.server.services.ce;
import com.appsmith.server.domains.Application;
import com.appsmith.server.domains.ApplicationSnapshot;
import com.appsmith.server.projections.DefaultTimestampOnly;
import reactor.core.publisher.Mono;
public interface ApplicationSnapshotServiceCE {
@ -15,7 +15,7 @@ public interface ApplicationSnapshotServiceCE {
*/
Mono<Boolean> createApplicationSnapshot(String applicationId, String branchName);
Mono<ApplicationSnapshot> getWithoutDataByApplicationId(String applicationId, String branchName);
Mono<DefaultTimestampOnly> getWithoutDataByApplicationId(String applicationId, String branchName);
Mono<Application> restoreSnapshot(String applicationId, String branchName);

View File

@ -12,6 +12,7 @@ import com.appsmith.server.exceptions.AppsmithException;
import com.appsmith.server.exports.internal.ExportService;
import com.appsmith.server.helpers.ResponseUtils;
import com.appsmith.server.imports.internal.ImportService;
import com.appsmith.server.projections.DefaultTimestampOnly;
import com.appsmith.server.repositories.ApplicationSnapshotRepository;
import com.appsmith.server.solutions.ApplicationPermission;
import com.google.gson.Gson;
@ -67,12 +68,13 @@ public class ApplicationSnapshotServiceCEImpl implements ApplicationSnapshotServ
}
@Override
public Mono<ApplicationSnapshot> getWithoutDataByApplicationId(String applicationId, String branchName) {
public Mono<DefaultTimestampOnly> getWithoutDataByApplicationId(String applicationId, String branchName) {
// get application first to check the permission and get child aka branched application ID
return applicationService
.findBranchedApplicationId(branchName, applicationId, applicationPermission.getEditPermission())
.flatMap(applicationSnapshotRepository::findWithoutData)
.defaultIfEmpty(new ApplicationSnapshot());
.flatMap(branchedApplicationId ->
applicationSnapshotRepository.findByApplicationIdAndChunkOrder(branchedApplicationId, 1))
.defaultIfEmpty(new DefaultTimestampOnly(null, null));
}
@Override

View File

@ -1,7 +1,7 @@
package com.appsmith.server.repositories;
import com.appsmith.server.domains.ApplicationSnapshot;
import com.google.gson.Gson;
import com.appsmith.server.projections.DefaultTimestampOnly;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@ -20,9 +20,6 @@ public class ApplicationSnapshotRepositoryTest {
@Autowired
private ApplicationSnapshotRepository applicationSnapshotRepository;
@Autowired
private Gson gson;
@Test
@WithUserDetails("api_user")
public void findWithoutData_WhenMatched_ReturnsMatchedDocumentWithoutData() {
@ -38,15 +35,14 @@ public class ApplicationSnapshotRepositoryTest {
snapshot2.setApplicationId(testAppId2);
snapshot2.setChunkOrder(1);
Mono<ApplicationSnapshot> snapshotMono = applicationSnapshotRepository
Mono<DefaultTimestampOnly> snapshotMono = applicationSnapshotRepository
.saveAll(List.of(snapshot1, snapshot2))
.then(applicationSnapshotRepository.findWithoutData(testAppId2));
.then(applicationSnapshotRepository.findByApplicationIdAndChunkOrder(testAppId2, 1));
StepVerifier.create(snapshotMono)
.assertNext(applicationSnapshot -> {
assertThat(applicationSnapshot.getApplicationId()).isEqualTo(testAppId2);
assertThat(applicationSnapshot.getData()).isNull();
assertThat(applicationSnapshot.getChunkOrder()).isEqualTo(1);
assertThat(applicationSnapshot.updatedAt()).isNotNull();
assertThat(applicationSnapshot.createdAt()).isNotNull();
})
.verifyComplete();
}
@ -65,14 +61,14 @@ public class ApplicationSnapshotRepositoryTest {
snapshot2.setApplicationId(testAppId1);
snapshot2.setChunkOrder(2);
Mono<ApplicationSnapshot> snapshotMono = applicationSnapshotRepository
Mono<DefaultTimestampOnly> snapshotMono = applicationSnapshotRepository
.saveAll(List.of(snapshot1, snapshot2))
.then(applicationSnapshotRepository.findWithoutData(testAppId1));
.then(applicationSnapshotRepository.findByApplicationIdAndChunkOrder(testAppId1, 1));
StepVerifier.create(snapshotMono)
.assertNext(applicationSnapshot -> {
assertThat(applicationSnapshot.getApplicationId()).isEqualTo(testAppId1);
assertThat(applicationSnapshot.getChunkOrder()).isEqualTo(1);
assertThat(applicationSnapshot.createdAt()).isNotNull();
assertThat(applicationSnapshot.updatedAt()).isNotNull();
})
.verifyComplete();
}

View File

@ -10,6 +10,7 @@ import com.appsmith.server.domains.Workspace;
import com.appsmith.server.dtos.ApplicationPagesDTO;
import com.appsmith.server.dtos.PageDTO;
import com.appsmith.server.newpages.base.NewPageService;
import com.appsmith.server.projections.DefaultTimestampOnly;
import com.appsmith.server.repositories.ApplicationSnapshotRepository;
import com.appsmith.server.solutions.ApplicationPermission;
import org.junit.jupiter.api.AfterEach;
@ -91,7 +92,7 @@ public class ApplicationSnapshotServiceTest {
Application testApplication = new Application();
testApplication.setName("Test app for snapshot");
testApplication.setWorkspaceId(workspace.getId());
Mono<ApplicationSnapshot> snapshotMono = applicationPageService
Mono<DefaultTimestampOnly> snapshotMono = applicationPageService
.createApplication(testApplication)
.flatMap(application -> {
assert application.getId() != null;
@ -104,8 +105,8 @@ public class ApplicationSnapshotServiceTest {
StepVerifier.create(snapshotMono)
.assertNext(snapshot -> {
assertThat(snapshot.getApplicationId()).isNotNull();
assertThat(snapshot.getData()).isNull();
assertThat(snapshot.updatedAt()).isNotNull();
assertThat(snapshot.createdAt()).isNotNull();
})
.verifyComplete();
}
@ -116,7 +117,7 @@ public class ApplicationSnapshotServiceTest {
Application testApplication = new Application();
testApplication.setName("Test app for snapshot");
testApplication.setWorkspaceId(workspace.getId());
Mono<ApplicationSnapshot> snapshotMono = applicationPageService
Mono<DefaultTimestampOnly> snapshotMono = applicationPageService
.createApplication(testApplication)
.flatMap(application -> {
assert application.getId() != null;
@ -131,8 +132,8 @@ public class ApplicationSnapshotServiceTest {
StepVerifier.create(snapshotMono)
.assertNext(snapshot -> {
assertThat(snapshot.getApplicationId()).isNotNull();
assertThat(snapshot.getData()).isNull();
assertThat(snapshot.updatedAt()).isNotNull();
assertThat(snapshot.createdAt()).isNotNull();
})
.verifyComplete();
}
@ -152,7 +153,7 @@ public class ApplicationSnapshotServiceTest {
gitArtifactMetadata.setDefaultApplicationId(testDefaultAppId);
gitArtifactMetadata.setBranchName(testBranchName);
testApplication.setGitApplicationMetadata(gitArtifactMetadata);
Mono<Tuple2<ApplicationSnapshot, Application>> tuple2Mono = applicationPageService
Mono<Tuple2<DefaultTimestampOnly, Application>> tuple2Mono = applicationPageService
.createApplication(testApplication)
.flatMap(application -> applicationSnapshotService
.createApplicationSnapshot(testDefaultAppId, testBranchName)
@ -162,10 +163,9 @@ public class ApplicationSnapshotServiceTest {
StepVerifier.create(tuple2Mono)
.assertNext(objects -> {
ApplicationSnapshot applicationSnapshot = objects.getT1();
Application application = objects.getT2();
assertThat(applicationSnapshot.getData()).isNull();
assertThat(applicationSnapshot.getApplicationId()).isEqualTo(application.getId());
DefaultTimestampOnly applicationSnapshot = objects.getT1();
assertThat(applicationSnapshot.updatedAt()).isNotNull();
assertThat(applicationSnapshot.createdAt()).isNotNull();
})
.verifyComplete();
}
@ -304,7 +304,7 @@ public class ApplicationSnapshotServiceTest {
Application testApplication = new Application();
testApplication.setName("Test app for snapshot");
testApplication.setWorkspaceId(workspace.getId());
Mono<ApplicationSnapshot> applicationSnapshotMono = applicationPageService
Mono<DefaultTimestampOnly> applicationSnapshotMono = applicationPageService
.createApplication(testApplication)
.flatMap(application1 -> {
return applicationSnapshotService.getWithoutDataByApplicationId(application1.getId(), null);
@ -312,7 +312,7 @@ public class ApplicationSnapshotServiceTest {
StepVerifier.create(applicationSnapshotMono)
.assertNext(applicationSnapshot -> {
assertThat(applicationSnapshot.getId()).isNull();
assertThat(applicationSnapshot.updatedAt()).isNull();
})
.verifyComplete();
}