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 9009a96f68..55dd43e5b4 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 @@ -2494,73 +2494,89 @@ public class DatabaseChangelog { } -// @ChangeSet(order = "073", id = "mongo-form-merge-update-commands", author = "") -// public void migrateUpdateOneToUpdateManyMongoFormCommand(MongockTemplate mongockTemplate) { -// -// Plugin mongoPlugin = mongockTemplate.findOne(query(where("packageName").is("mongo-plugin")), Plugin.class); -// -// // Fetch all the actions built on top of a mongo database with command type update_one or update_many -// assert mongoPlugin != null; -// List updateMongoActions = mongockTemplate.find( -// query(new Criteria().andOperator( -// where(fieldName(QNewAction.newAction.pluginId)).is(mongoPlugin.getId()))), -// NewAction.class -// ) -// .stream() -// .filter(mongoAction -> { -// if (mongoAction.getUnpublishedAction() == null || mongoAction.getUnpublishedAction().getActionConfiguration() == null) { -// return false; -// } -// final List pluginSpecifiedTemplates = mongoAction.getUnpublishedAction().getActionConfiguration().getPluginSpecifiedTemplates(); -// -// // Filter out all the actions which are of either of the two update command type -// if (pluginSpecifiedTemplates != null && pluginSpecifiedTemplates.size() == 21) { -// Property commandProperty = pluginSpecifiedTemplates.get(2); -// if (commandProperty != null && commandProperty.getValue() != null) { -// String command = (String) commandProperty.getValue(); -// if (command.equals("UPDATE_ONE") || command.equals("UPDATE_MANY")) { -// return true; -// } -// } -// } -// // Not an action of interest for migration. -// return false; -// }) -// .collect(Collectors.toList()); -// -// for (NewAction action : updateMongoActions) { -// List pluginSpecifiedTemplates = action.getUnpublishedAction().getActionConfiguration().getPluginSpecifiedTemplates(); -// String command = (String) pluginSpecifiedTemplates.get(2).getValue(); -// -// // Set the command name to be UPDATE for both the update commands -// pluginSpecifiedTemplates.get(2).setValue("UPDATE"); -// -// // In case of update one, migrate the query and update configurations. -// if (command.equals("UPDATE_ONE")) { -// String query = (String) pluginSpecifiedTemplates.get(8).getValue(); -// String update = (String) pluginSpecifiedTemplates.get(10).getValue(); -// -// Map configMap = new HashMap<>(); -// configMap.put(0, pluginSpecifiedTemplates.get(0).getValue()); -// configMap.put(1, "FORM"); -// configMap.put(2, "UPDATE"); -// configMap.put(19, pluginSpecifiedTemplates.get(19).getValue()); -// // Query for all the documents in the collection -// configMap.put(11, query); -// configMap.put(12, update); -// configMap.put(21, "SINGLE"); -// -// List updatedTemplates = generateMongoFormConfigTemplates(configMap); -// action.getUnpublishedAction().getActionConfiguration().setPluginSpecifiedTemplates(updatedTemplates); -// -// } -// } -// -// // Now that all the actions have been updated, save all the actions -// for (NewAction action : updateMongoActions) { -// mongockTemplate.save(action); -// } -// } + + @ChangeSet(order = "073", id = "mongo-form-merge-update-commands", author = "") + public void migrateUpdateOneToUpdateManyMongoFormCommand(MongockTemplate mongockTemplate) { + + Plugin mongoPlugin = mongockTemplate.findOne(query(where("packageName").is("mongo-plugin")), Plugin.class); + + // Fetch all the actions built on top of a mongo database with command type update_one or update_many + assert mongoPlugin != null; + List updateMongoActions = mongockTemplate.find( + query(new Criteria().andOperator( + where(fieldName(QNewAction.newAction.pluginId)).is(mongoPlugin.getId()))), + NewAction.class + ) + .stream() + .filter(mongoAction -> { + if (mongoAction.getUnpublishedAction() == null || mongoAction.getUnpublishedAction().getActionConfiguration() == null) { + return false; + } + final List pluginSpecifiedTemplates = mongoAction.getUnpublishedAction().getActionConfiguration().getPluginSpecifiedTemplates(); + + // Filter out all the actions which are of either of the two update command type + if (pluginSpecifiedTemplates != null && pluginSpecifiedTemplates.size() == 21) { + Property commandProperty = pluginSpecifiedTemplates.get(2); + if (commandProperty != null && commandProperty.getValue() != null) { + String command = (String) commandProperty.getValue(); + if (command.equals("UPDATE_ONE") || command.equals("UPDATE_MANY")) { + return true; + } + } + } + // Not an action of interest for migration. + return false; + }) + .collect(Collectors.toList()); + + for (NewAction action : updateMongoActions) { + List pluginSpecifiedTemplates = action.getUnpublishedAction().getActionConfiguration().getPluginSpecifiedTemplates(); + String command = (String) pluginSpecifiedTemplates.get(2).getValue(); + + // In case of update one, migrate the query and update configurations. + if (command.equals("UPDATE_ONE")) { + + String query = ""; + String update = ""; + String collection = ""; + + Property queryProperty = pluginSpecifiedTemplates.get(8); + if (queryProperty != null) { + query = (String) queryProperty.getValue(); + } + + Property updateProperty = pluginSpecifiedTemplates.get(10); + if (updateProperty != null) { + update = (String) updateProperty.getValue(); + } + + Property collectionProperty = pluginSpecifiedTemplates.get(19); + if (collectionProperty != null) { + collection = (String) collectionProperty.getValue(); + } + + Map configMap = new HashMap<>(); + configMap.put(0, pluginSpecifiedTemplates.get(0).getValue()); + configMap.put(1, "FORM"); + // All update commands have to be migrated to the new update name + configMap.put(2, "UPDATE"); + configMap.put(19, collection); + // Query for all the documents in the collection + configMap.put(11, query); + configMap.put(12, update); + configMap.put(21, "SINGLE"); + + List updatedTemplates = generateMongoFormConfigTemplates(configMap); + action.getUnpublishedAction().getActionConfiguration().setPluginSpecifiedTemplates(updatedTemplates); + + } + } + + // Now that all the actions have been updated, save all the actions + for (NewAction action : updateMongoActions) { + mongockTemplate.save(action); + } + } @ChangeSet(order = "074", id = "ensure-user-created-and-updated-at-fields", author = "")