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)
This commit is contained in:
Bhuvaneswari Pathsamatla 2022-10-30 17:10:50 +05:30 committed by GitHub
parent 37a1738490
commit cc2c3eae69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -236,26 +236,16 @@ public class DatabaseChangelog {
} }
public static void installPluginToAllWorkspaces(MongockTemplate mongockTemplate, String pluginId) { 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 */ /* Filter in only those workspaces that don't have the plugin installed */
queryToFetchAllWorkspaceIds.addCriteria(Criteria.where("plugins.pluginId").ne(pluginId)); queryToFetchWorkspacesWOPlugin.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<Workspace> 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())) { /* Add plugin to the workspace */
workspace.setPlugins(new HashSet<>()); Update update = new Update();
} update.addToSet("plugins",new WorkspacePlugin(pluginId, WorkspacePluginStatus.FREE));
workspace.getPlugins() mongockTemplate.updateMulti(queryToFetchWorkspacesWOPlugin,update,Workspace.class);
.add(new WorkspacePlugin(pluginId, WorkspacePluginStatus.FREE));
mongockTemplate.save(workspace);
}
} }
@ChangeSet(order = "001", id = "initial-plugins", author = "") @ChangeSet(order = "001", id = "initial-plugins", author = "")