diff --git a/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/VisualTests/JSEditorIndent_spec.js b/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/VisualTests/JSEditorIndent_spec.js index a4230937d4..d0fc6bffda 100644 --- a/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/VisualTests/JSEditorIndent_spec.js +++ b/app/client/cypress/integration/Smoke_TestSuite/ClientSideTests/VisualTests/JSEditorIndent_spec.js @@ -420,6 +420,4 @@ myFun2: async () => { cy.get("div.CodeMirror").type("{cmd+leftArrow}"); cy.get("div.CodeMirror").matchImageSnapshot("jsObjAfterGoLineStartSmart5"); }); - - }); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog2.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog2.java index 8d650aa748..cf5ed7b019 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog2.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog2.java @@ -1,5 +1,6 @@ package com.appsmith.server.migrations; +import com.appsmith.external.models.BaseDomain; import com.appsmith.external.models.Datasource; import com.appsmith.external.models.Property; import com.appsmith.external.models.QBaseDomain; @@ -64,11 +65,13 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; +import static com.appsmith.external.helpers.AppsmithBeanUtils.copyNewFieldValuesIntoOldObject; import static com.appsmith.server.migrations.DatabaseChangelog.dropIndexIfExists; import static com.appsmith.server.migrations.DatabaseChangelog.ensureIndexes; import static com.appsmith.server.migrations.DatabaseChangelog.getUpdatedDynamicBindingPathList; @@ -78,6 +81,7 @@ import static java.lang.Boolean.TRUE; import static org.springframework.data.mongodb.core.query.Criteria.where; import static org.springframework.data.mongodb.core.query.Query.query; + @Slf4j @ChangeLog(order = "002") public class DatabaseChangelog2 { @@ -1374,4 +1378,78 @@ public class DatabaseChangelog2 { DatabaseChangelog.doClearRedisKeys(reactiveRedisOperations); } + private List getCustomizedThemeIds(String fieldName, Function getThemeIdMethod, List systemThemeIds, MongockTemplate mongockTemplate) { + // query to get application having a customized theme in the provided fieldName + Query getAppsWithCustomTheme = new Query( + Criteria.where(fieldName(QApplication.application.gitApplicationMetadata)).exists(true) + .and(fieldName(QApplication.application.deleted)).is(false) + .andOperator( + where(fieldName).nin(systemThemeIds), where(fieldName).exists(true) + ) + ); + + // we need the provided field "fieldName" only + getAppsWithCustomTheme.fields().include(fieldName); + + List applications = mongockTemplate.find(getAppsWithCustomTheme, Application.class); + return applications.stream().map(getThemeIdMethod).collect(Collectors.toList()); + } + + @ChangeSet(order = "022", id = "fix-deleted-themes-when-git-branch-deleted", author = "") + public void fixDeletedThemesWhenGitBranchDeleted(MongockTemplate mongockTemplate) { + Query getSystemThemesQuery = new Query(Criteria.where(fieldName(QTheme.theme.isSystemTheme)).is(TRUE)); + getSystemThemesQuery.fields().include(fieldName(QTheme.theme.id)); + List systemThemes = mongockTemplate.find(getSystemThemesQuery, Theme.class); + List systemThemeIds = systemThemes.stream().map(BaseDomain::getId).collect(Collectors.toList()); + + List customizedEditModeThemeIds = getCustomizedThemeIds( + fieldName(QApplication.application.editModeThemeId), Application::getEditModeThemeId, systemThemeIds, mongockTemplate + ); + + List customizedPublishedModeThemeIds = getCustomizedThemeIds( + fieldName(QApplication.application.publishedModeThemeId), Application::getPublishedModeThemeId, systemThemeIds, mongockTemplate + ); + + // combine the theme ids + Set set = new HashSet<>(); + set.addAll(customizedEditModeThemeIds); + set.addAll(customizedPublishedModeThemeIds); + + Update update = new Update().set(fieldName(QTheme.theme.deleted), false) + .unset(fieldName(QTheme.theme.deletedAt)); + Criteria deletedCustomThemes = Criteria.where(fieldName(QTheme.theme.id)).in(set) + .and(fieldName(QTheme.theme.deleted)).is(true); + + mongockTemplate.updateMulti(new Query(deletedCustomThemes), update, Theme.class); + + for(String editModeThemeId: customizedEditModeThemeIds) { + Query query = new Query(Criteria.where(fieldName(QApplication.application.editModeThemeId)).is(editModeThemeId)) + .addCriteria(where(fieldName(QApplication.application.deleted)).is(false)) + .addCriteria(where(fieldName(QApplication.application.gitApplicationMetadata)).exists(true)); + query.fields().include(fieldName(QApplication.application.id)); + + List applicationList = mongockTemplate.find(query, Application.class); + if(applicationList.size() > 1) { // same custom theme is set to more than one application + // Remove one as we will create a new theme for all the other branch apps + applicationList.remove(applicationList.size() - 1); + + // clone the custom theme for each of these applications + Query themeQuery = new Query(Criteria.where(fieldName(QTheme.theme.id)).is(editModeThemeId)) + .addCriteria(where(fieldName(QTheme.theme.deleted)).is(false)); + Theme theme = mongockTemplate.findOne(themeQuery, Theme.class); + for (Application application : applicationList) { + Theme newTheme = new Theme(); + copyNewFieldValuesIntoOldObject(theme, newTheme); + newTheme.setId(null); + newTheme.setSystemTheme(false); + newTheme = mongockTemplate.insert(newTheme); + mongockTemplate.updateFirst( + new Query(Criteria.where(fieldName(QApplication.application.id)).is(application.getId())), + new Update().set(fieldName(QApplication.application.editModeThemeId), newTheme.getId()), + Application.class + ); + } + } + } + } }