-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Migration for missing datasource configuration on default rest datasources for git connected app. #36203
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,35 +6,27 @@ | |
import com.appsmith.server.exceptions.AppsmithError; | ||
import com.appsmith.server.exceptions.AppsmithException; | ||
import com.appsmith.server.helpers.CollectionUtils; | ||
import com.appsmith.server.migrations.utils.JsonSchemaMigrationHelper; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class JsonSchemaMigration { | ||
|
||
private final JsonSchemaVersions jsonSchemaVersions; | ||
private final JsonSchemaMigrationHelper jsonSchemaMigrationHelper; | ||
|
||
private boolean isCompatible(ApplicationJson applicationJson) { | ||
return (applicationJson.getClientSchemaVersion() <= jsonSchemaVersions.getClientVersion()) | ||
&& (applicationJson.getServerSchemaVersion() <= jsonSchemaVersions.getServerVersion()); | ||
} | ||
|
||
/** | ||
* This is a temporary check which is being placed for the compatibility of server versions in scenarios | ||
* where user is moving a json from an instance which has | ||
* release_autocommit_feature_enabled true to an instance which has the flag as false. In that case the server | ||
* version number of json would be 8 and in new instance it would be not compatible. | ||
* @param applicationJson | ||
* @return | ||
*/ | ||
private boolean isAutocommitVersionBump(ApplicationJson applicationJson) { | ||
return jsonSchemaVersions.getServerVersion() == 7 && applicationJson.getServerSchemaVersion() == 8; | ||
} | ||
|
||
private void setSchemaVersions(ApplicationJson applicationJson) { | ||
applicationJson.setServerSchemaVersion(getCorrectSchemaVersion(applicationJson.getServerSchemaVersion())); | ||
applicationJson.setClientSchemaVersion(getCorrectSchemaVersion(applicationJson.getClientSchemaVersion())); | ||
|
@@ -53,24 +45,53 @@ public Mono<? extends ArtifactExchangeJson> migrateArtifactExchangeJsonToLatestS | |
ArtifactExchangeJson artifactExchangeJson) { | ||
|
||
if (ArtifactType.APPLICATION.equals(artifactExchangeJson.getArtifactJsonType())) { | ||
return migrateApplicationJsonToLatestSchema((ApplicationJson) artifactExchangeJson); | ||
return migrateApplicationJsonToLatestSchema((ApplicationJson) artifactExchangeJson, null, null); | ||
} | ||
|
||
return Mono.fromCallable(() -> artifactExchangeJson); | ||
} | ||
|
||
public Mono<ApplicationJson> migrateApplicationJsonToLatestSchema(ApplicationJson applicationJson) { | ||
public Mono<ApplicationJson> migrateApplicationJsonToLatestSchema( | ||
ApplicationJson applicationJson, String baseApplicationId, String branchName) { | ||
return Mono.fromCallable(() -> { | ||
setSchemaVersions(applicationJson); | ||
if (isCompatible(applicationJson)) { | ||
return migrateServerSchema(applicationJson); | ||
} | ||
|
||
if (isAutocommitVersionBump(applicationJson)) { | ||
return migrateServerSchema(applicationJson); | ||
return applicationJson; | ||
}) | ||
.flatMap(appJson -> { | ||
if (!isCompatible(appJson)) { | ||
return Mono.empty(); | ||
} | ||
|
||
return null; | ||
// Taking a tech debt over here for import of file application. | ||
// All migration above version 9 is reactive | ||
// TODO: make import flow migration reactive | ||
return Mono.just(migrateServerSchema(appJson)) | ||
.flatMap(migratedApplicationJson -> { | ||
if (migratedApplicationJson.getServerSchemaVersion() == 9 | ||
&& Boolean.TRUE.equals(MigrationHelperMethods.doesRestApiRequireMigration( | ||
migratedApplicationJson))) { | ||
return jsonSchemaMigrationHelper | ||
.addDatasourceConfigurationToDefaultRestApiActions( | ||
baseApplicationId, branchName, migratedApplicationJson) | ||
.map(applicationJsonWithMigration10 -> { | ||
applicationJsonWithMigration10.setServerSchemaVersion(10); | ||
return applicationJsonWithMigration10; | ||
}); | ||
} | ||
|
||
migratedApplicationJson.setServerSchemaVersion(10); | ||
return Mono.just(migratedApplicationJson); | ||
}) | ||
.map(migratedAppJson -> { | ||
if (applicationJson | ||
.getServerSchemaVersion() | ||
.equals(jsonSchemaVersions.getServerVersion())) { | ||
return applicationJson; | ||
} | ||
|
||
applicationJson.setServerSchemaVersion(jsonSchemaVersions.getServerVersion()); | ||
return applicationJson; | ||
}); | ||
}) | ||
.switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.INCOMPATIBLE_IMPORTED_JSON))); | ||
} | ||
|
@@ -81,7 +102,7 @@ public Mono<ApplicationJson> migrateApplicationJsonToLatestSchema(ApplicationJso | |
* @param artifactExchangeJson : the json to be imported | ||
* @return transformed artifact exchange json | ||
*/ | ||
@Deprecated | ||
@Deprecated(forRemoval = true, since = "Use migrateArtifactJsonToLatestSchema") | ||
public ArtifactExchangeJson migrateArtifactToLatestSchema(ArtifactExchangeJson artifactExchangeJson) { | ||
|
||
if (!ArtifactType.APPLICATION.equals(artifactExchangeJson.getArtifactJsonType())) { | ||
|
@@ -91,11 +112,11 @@ public ArtifactExchangeJson migrateArtifactToLatestSchema(ArtifactExchangeJson a | |
ApplicationJson applicationJson = (ApplicationJson) artifactExchangeJson; | ||
setSchemaVersions(applicationJson); | ||
if (!isCompatible(applicationJson)) { | ||
if (!isAutocommitVersionBump(applicationJson)) { | ||
throw new AppsmithException(AppsmithError.INCOMPATIBLE_IMPORTED_JSON); | ||
} | ||
throw new AppsmithException(AppsmithError.INCOMPATIBLE_IMPORTED_JSON); | ||
} | ||
return migrateServerSchema(applicationJson); | ||
|
||
applicationJson = migrateServerSchema(applicationJson); | ||
return nonReactiveServerMigrationForImport(applicationJson); | ||
} | ||
|
||
/** | ||
|
@@ -145,11 +166,37 @@ private ApplicationJson migrateServerSchema(ApplicationJson applicationJson) { | |
MigrationHelperMethods.ensureXmlParserPresenceInCustomJsLibList(applicationJson); | ||
applicationJson.setServerSchemaVersion(7); | ||
case 7: | ||
applicationJson.setServerSchemaVersion(8); | ||
case 8: | ||
MigrationHelperMethods.migrateThemeSettingsForAnvil(applicationJson); | ||
applicationJson.setServerSchemaVersion(9); | ||
|
||
// This is not supposed to have anymore additions to the schema. | ||
default: | ||
// Unable to detect the serverSchema | ||
|
||
} | ||
|
||
return applicationJson; | ||
} | ||
|
||
/** | ||
* This method is an alternative to reactive way of adding migrations to application json. | ||
* this is getting used by flows which haven't implemented the reactive way yet. | ||
* @param applicationJson : application json for which migration has to be done. | ||
* @return return application json after migration | ||
*/ | ||
private ApplicationJson nonReactiveServerMigrationForImport(ApplicationJson applicationJson) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this a separate method? |
||
if (jsonSchemaVersions.getServerVersion().equals(applicationJson.getServerSchemaVersion())) { | ||
return applicationJson; | ||
} | ||
|
||
switch (applicationJson.getServerSchemaVersion()) { | ||
case 9: | ||
// this if for cases where we have empty datasource configs | ||
MigrationHelperMethods.migrateApplicationJsonToVersionTen(applicationJson, Map.of()); | ||
applicationJson.setServerSchemaVersion(10); | ||
default: | ||
} | ||
|
||
if (applicationJson.getServerSchemaVersion().equals(jsonSchemaVersions.getServerVersion())) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...-server/src/main/java/com/appsmith/server/migrations/utils/JsonSchemaMigrationHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.appsmith.server.migrations.utils; | ||
|
||
import com.appsmith.external.constants.PluginConstants; | ||
import com.appsmith.server.applications.base.ApplicationService; | ||
import com.appsmith.server.domains.Application; | ||
import com.appsmith.server.domains.NewAction; | ||
import com.appsmith.server.dtos.ApplicationJson; | ||
import com.appsmith.server.migrations.MigrationHelperMethods; | ||
import com.appsmith.server.newactions.base.NewActionService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class JsonSchemaMigrationHelper { | ||
|
||
private final ApplicationService applicationService; | ||
private final NewActionService newActionService; | ||
|
||
public Mono<ApplicationJson> addDatasourceConfigurationToDefaultRestApiActions( | ||
String baseApplicationId, String branchName, ApplicationJson applicationJson) { | ||
|
||
Mono<ApplicationJson> contingencyMigrationJson = Mono.defer(() -> Mono.fromCallable(() -> { | ||
MigrationHelperMethods.migrateApplicationJsonToVersionTen(applicationJson, Map.of()); | ||
return applicationJson; | ||
})); | ||
|
||
if (!StringUtils.hasText(baseApplicationId) || !StringUtils.hasText(branchName)) { | ||
return contingencyMigrationJson; | ||
} | ||
|
||
Mono<Application> applicationMono = applicationService | ||
.findByBranchNameAndBaseApplicationId(branchName, baseApplicationId, null) | ||
.cache(); | ||
|
||
return applicationMono | ||
.flatMap(branchedApplication -> { | ||
return newActionService | ||
.findAllByApplicationIdAndViewMode( | ||
branchedApplication.getId(), Boolean.FALSE, Optional.empty(), Optional.empty()) | ||
.filter(action -> { | ||
if (action.getUnpublishedAction() == null | ||
|| action.getUnpublishedAction().getDatasource() == null) { | ||
return false; | ||
} | ||
|
||
boolean reverseFlag = StringUtils.hasText(action.getUnpublishedAction() | ||
.getDatasource() | ||
.getId()) | ||
|| !PluginConstants.DEFAULT_REST_DATASOURCE.equals(action.getUnpublishedAction() | ||
.getDatasource() | ||
.getName()); | ||
|
||
return !reverseFlag; | ||
}) | ||
.collectMap(NewAction::getGitSyncId); | ||
}) | ||
.map(newActionMap -> { | ||
MigrationHelperMethods.migrateApplicationJsonToVersionTen(applicationJson, newActionMap); | ||
return applicationJson; | ||
}) | ||
.switchIfEmpty(contingencyMigrationJson) | ||
.onErrorResume(error -> { | ||
log.error("Error occurred while migrating actions of application json. {}", error.getMessage()); | ||
return contingencyMigrationJson; | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this an additional systematic path similar to the json file migrations? This is a legitimate case of a new data point that needs to be added to the repository, and, although is expected fewer times than file migrations, should be accounted for as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what you mean is that let's add a reactive migration set separately itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please