From 612d7d672758e57e40ab6f9521f2e607872600a4 Mon Sep 17 00:00:00 2001 From: Abhijeet <41686026+abhvsn@users.noreply.github.com> Date: Thu, 24 Feb 2022 19:47:50 +0530 Subject: [PATCH] feat: API to provide a way to distinguish if the application is updated manually or through DB migration (#11414) * Added check for updating application resources manually * Refactor to include client-side migration --- .../appsmith/server/domains/Application.java | 11 ++++++ .../server/helpers/ResponseUtils.java | 9 +++++ .../server/services/ce/GitServiceCEImpl.java | 21 +++++++---- .../ImportExportApplicationServiceCEImpl.java | 2 + .../server/helpers/ResponseUtilsTest.java | 37 +++++++++++++++++++ .../server/services/GitServiceTest.java | 9 ++++- 6 files changed, 80 insertions(+), 9 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Application.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Application.java index 1f35ad12f1..5d55ed77bc 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Application.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Application.java @@ -107,6 +107,17 @@ public class Application extends BaseDomain { Boolean forkingEnabled; + // Field to convey if the application is updated by the user or modified by migration + @Transient + Boolean isManualUpdate; + + // To convey current schema version for client and server. This will be used to check if we run the migration + // between 2 commits if the application is connected to git + @JsonIgnore + Integer clientSchemaVersion; + @JsonIgnore + Integer serverSchemaVersion; + @JsonIgnore String publishedModeThemeId; diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/helpers/ResponseUtils.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/helpers/ResponseUtils.java index 07db41265c..4480ba4132 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/helpers/ResponseUtils.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/helpers/ResponseUtils.java @@ -17,6 +17,7 @@ import com.appsmith.server.dtos.PageDTO; import com.appsmith.server.dtos.PageNameIdDTO; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; +import com.appsmith.server.migrations.JsonSchemaVersions; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; @@ -336,6 +337,14 @@ public class ResponseUtils { } }); } + + if (application.getClientSchemaVersion() == null || application.getServerSchemaVersion() == null + || (JsonSchemaVersions.clientVersion.equals(application.getClientSchemaVersion()) + && JsonSchemaVersions.serverVersion.equals(application.getServerSchemaVersion()))) { + application.setIsManualUpdate(true); + } else { + application.setIsManualUpdate(false); + } return application; } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/GitServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/GitServiceCEImpl.java index b0238ac914..7645b02e3d 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/GitServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/GitServiceCEImpl.java @@ -36,6 +36,7 @@ import com.appsmith.server.helpers.GitDeployKeyGenerator; import com.appsmith.server.helpers.GitFileUtils; import com.appsmith.server.helpers.GitUtils; import com.appsmith.server.helpers.ResponseUtils; +import com.appsmith.server.migrations.JsonSchemaVersions; import com.appsmith.server.repositories.GitDeployKeysRepository; import com.appsmith.server.services.ActionCollectionService; import com.appsmith.server.services.AnalyticsService; @@ -547,13 +548,19 @@ public class GitServiceCEImpl implements GitServiceCE { // Add BE analytics .flatMap(tuple -> { String status = tuple.getT1(); - Application application = tuple.getT2(); - return addAnalyticsForGitOperation( - AnalyticsEvents.GIT_COMMIT.getEventName(), - application, - application.getGitApplicationMetadata().getIsRepoPrivate() - ) - .thenReturn(status); + Application childApplication = tuple.getT2(); + // Update json schema versions so that we can detect if the next update was made by DB migration or + // by the user + Application update = new Application(); + update.setClientSchemaVersion(JsonSchemaVersions.clientVersion); + update.setServerSchemaVersion(JsonSchemaVersions.serverVersion); + return applicationService.update(childApplication.getId(), update) + .then(addAnalyticsForGitOperation( + AnalyticsEvents.GIT_COMMIT.getEventName(), + childApplication, + childApplication.getGitApplicationMetadata().getIsRepoPrivate() + )) + .thenReturn(status); }); return Mono.create(sink -> commitMono diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ce/ImportExportApplicationServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ce/ImportExportApplicationServiceCEImpl.java index bfaadcc38e..60c0b4f825 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ce/ImportExportApplicationServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/ce/ImportExportApplicationServiceCEImpl.java @@ -1609,5 +1609,7 @@ public class ImportExportApplicationServiceCEImpl implements ImportExportApplica application.setUserPermissions(null); application.setEditModeThemeId(null); application.setPublishedModeThemeId(null); + application.setClientSchemaVersion(null); + application.setServerSchemaVersion(null); } } diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/helpers/ResponseUtilsTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/helpers/ResponseUtilsTest.java index 9488ae351e..825a7b0914 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/helpers/ResponseUtilsTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/helpers/ResponseUtilsTest.java @@ -6,6 +6,7 @@ import com.appsmith.server.domains.Application; import com.appsmith.server.domains.ApplicationPage; import com.appsmith.server.domains.NewAction; import com.appsmith.server.domains.NewPage; +import com.appsmith.server.migrations.JsonSchemaVersions; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; @@ -130,4 +131,40 @@ public class ResponseUtilsTest { Assert.assertEquals(applicationPage.getId(), applicationPage.getDefaultPageId()); } } + + @Test + public void getApplication_withMultipleSchemaVersions_returnsCorrectManualUpdate() { + Application application = gson.fromJson(String.valueOf(jsonNode.get("application")), Application.class); + + final Application applicationCopy = new Application(); + AppsmithBeanUtils.copyNestedNonNullProperties(application, applicationCopy); + + application.setServerSchemaVersion(null); + Assert.assertNull(application.getIsManualUpdate()); + responseUtils.updateApplicationWithDefaultResources(application); + Assert.assertEquals(application.getIsManualUpdate(), true); + + application.setClientSchemaVersion(null); + application.setIsManualUpdate(null); + responseUtils.updateApplicationWithDefaultResources(application); + Assert.assertEquals(application.getIsManualUpdate(), true); + + application.setIsManualUpdate(null); + application.setServerSchemaVersion(1000); + application.setClientSchemaVersion(JsonSchemaVersions.clientVersion); + responseUtils.updateApplicationWithDefaultResources(application); + Assert.assertEquals(application.getIsManualUpdate(), false); + + application.setIsManualUpdate(null); + application.setClientSchemaVersion(1000); + application.setServerSchemaVersion(JsonSchemaVersions.serverVersion); + responseUtils.updateApplicationWithDefaultResources(application); + Assert.assertEquals(application.getIsManualUpdate(), false); + + application.setIsManualUpdate(null); + application.setClientSchemaVersion(JsonSchemaVersions.clientVersion); + application.setClientSchemaVersion(JsonSchemaVersions.serverVersion); + responseUtils.updateApplicationWithDefaultResources(application); + Assert.assertEquals(application.getIsManualUpdate(), true); + } } diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/GitServiceTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/GitServiceTest.java index 9081008bf2..cf09b8838e 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/GitServiceTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/GitServiceTest.java @@ -38,6 +38,7 @@ import com.appsmith.server.helpers.GitCloudServicesUtils; import com.appsmith.server.helpers.GitFileUtils; import com.appsmith.server.helpers.MockPluginExecutor; import com.appsmith.server.helpers.PluginExecutorHelper; +import com.appsmith.server.migrations.JsonSchemaVersions; import com.appsmith.server.repositories.OrganizationRepository; import com.appsmith.server.repositories.PluginRepository; import com.fasterxml.jackson.core.JsonProcessingException; @@ -1766,10 +1767,14 @@ public class GitServiceTest { Mono commitAndPushMono = gitService.commitApplication(commitDTO, gitConnectedApplication.getId(), DEFAULT_BRANCH); StepVerifier - .create(commitAndPushMono) - .assertNext(commitMsg -> { + .create(commitAndPushMono.zipWhen(status -> applicationService.findByIdAndBranchName(gitConnectedApplication.getId(), DEFAULT_BRANCH))) + .assertNext(tuple -> { + String commitMsg = tuple.getT1(); + Application application = tuple.getT2(); assertThat(commitMsg).contains("sample response for commit"); assertThat(commitMsg).contains("pushed successfully"); + assertThat(application.getClientSchemaVersion()).isEqualTo(JsonSchemaVersions.clientVersion); + assertThat(application.getServerSchemaVersion()).isEqualTo(JsonSchemaVersions.serverVersion); }) .verifyComplete(); }