From cc2c3eae69150f9cf7697791cc668bd516be4fc2 Mon Sep 17 00:00:00 2001 From: Bhuvaneswari Pathsamatla Date: Sun, 30 Oct 2022 17:10:50 +0530 Subject: [PATCH] chore: Refactor DB changelog to install plugins in workspace with single query (#17395) * refactor DB changelog to install plugins in workspace in much fewer iterations (in large size batches) --- .../server/migrations/DatabaseChangelog.java | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 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 d42baac791..289d07a6b8 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 @@ -236,26 +236,16 @@ public class DatabaseChangelog { } public static void installPluginToAllWorkspaces(MongockTemplate mongockTemplate, String pluginId) { - Query queryToFetchAllWorkspaceIds = new Query(); + + Query queryToFetchWorkspacesWOPlugin = 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); + queryToFetchWorkspacesWOPlugin.addCriteria(Criteria.where("plugins.pluginId").ne(pluginId)); - if (CollectionUtils.isEmpty(workspace.getPlugins())) { - workspace.setPlugins(new HashSet<>()); - } + /* Add plugin to the workspace */ + Update update = new Update(); + update.addToSet("plugins",new WorkspacePlugin(pluginId, WorkspacePluginStatus.FREE)); - workspace.getPlugins() - .add(new WorkspacePlugin(pluginId, WorkspacePluginStatus.FREE)); - - mongockTemplate.save(workspace); - } + mongockTemplate.updateMulti(queryToFetchWorkspacesWOPlugin,update,Workspace.class); } @ChangeSet(order = "001", id = "initial-plugins", author = "")