From ecec0e19756c96ff70dd6eaed314f865279abdfb Mon Sep 17 00:00:00 2001 From: Sumit Kumar Date: Thu, 15 Sep 2022 21:09:01 +0530 Subject: [PATCH] fix: re-install graphql plugin in workspaces (#16764) * re-install graphql plugin in workspaces --- .../server/migrations/DatabaseChangelog.java | 22 ++++++++++++------- .../server/migrations/DatabaseChangelog2.java | 13 +++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog.java index 83393d8b57..9689089045 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog.java @@ -54,6 +54,7 @@ import com.appsmith.server.domains.QNotification; import com.appsmith.server.domains.QOrganization; import com.appsmith.server.domains.QPlugin; import com.appsmith.server.domains.QUserData; +import com.appsmith.server.domains.QWorkspace; import com.appsmith.server.domains.Role; import com.appsmith.server.domains.Sequence; import com.appsmith.server.domains.User; @@ -235,18 +236,23 @@ public class DatabaseChangelog { } public static void installPluginToAllWorkspaces(MongockTemplate mongockTemplate, String pluginId) { - for (Workspace workspace : mongockTemplate.findAll(Workspace.class)) { + Query queryToFetchAllWorkspaceIds = new Query(); + /* Filter in only those workspaces that don't have the plugin installed */ + queryToFetchAllWorkspaceIds.addCriteria(Criteria.where("plugins.pluginId").ne(pluginId)); + /* Only read the workspace id and leave out other fields */ + queryToFetchAllWorkspaceIds.fields().include(fieldName(QWorkspace.workspace.id)); + List workspacesWithOnlyId = mongockTemplate.find(queryToFetchAllWorkspaceIds, Workspace.class); + for (Workspace workspaceWithId : workspacesWithOnlyId) { + Workspace workspace = + mongockTemplate.findOne(query(where(fieldName(QWorkspace.workspace.id)).is(workspaceWithId.getId())), + Workspace.class); + if (CollectionUtils.isEmpty(workspace.getPlugins())) { workspace.setPlugins(new HashSet<>()); } - final Set installedPlugins = workspace.getPlugins() - .stream().map(WorkspacePlugin::getPluginId).collect(Collectors.toSet()); - - if (!installedPlugins.contains(pluginId)) { - workspace.getPlugins() - .add(new WorkspacePlugin(pluginId, WorkspacePluginStatus.FREE)); - } + workspace.getPlugins() + .add(new WorkspacePlugin(pluginId, WorkspacePluginStatus.FREE)); mongockTemplate.save(workspace); } 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 a29ad9e628..30b66fbe43 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 @@ -2580,4 +2580,17 @@ public class DatabaseChangelog2 { installPluginToAllWorkspaces(mongoTemplate, plugin.getId()); } + + /** + * This method attempts to add GraphQL plugin to all workspaces once again since the last migration was + * interrupted due to issues on prod cluster. Hence, during the last migration the plugin could not be installed in + * few workspaces.The method installPluginToAllWorkspaces only installs the plugin in those workspaces where it is + * missing. + */ + @ChangeSet(order = "037", id = "install-graphql-plugin-to-remaining-workspaces", author = "") + public void reInstallGraphQLPluginToWorkspaces(MongockTemplate mongoTemplate) { + Plugin graphQLPlugin = mongoTemplate + .findOne(query(where("packageName").is("graphql-plugin")), Plugin.class); + installPluginToAllWorkspaces(mongoTemplate, graphQLPlugin.getId()); + } } \ No newline at end of file