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
This commit is contained in:
Abhijeet 2022-02-24 19:47:50 +05:30 committed by GitHub
parent cb4b9595d1
commit 612d7d6727
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 9 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -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

View File

@ -1609,5 +1609,7 @@ public class ImportExportApplicationServiceCEImpl implements ImportExportApplica
application.setUserPermissions(null);
application.setEditModeThemeId(null);
application.setPublishedModeThemeId(null);
application.setClientSchemaVersion(null);
application.setServerSchemaVersion(null);
}
}

View File

@ -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);
}
}

View File

@ -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<String> 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();
}