From 735a4398b23abac35cb78d607822de988e115357 Mon Sep 17 00:00:00 2001 From: Trisha Anand Date: Tue, 29 Dec 2020 16:29:28 +0530 Subject: [PATCH] Added a migration to old rest api datasources which adds the new mandatory key `isSendSessionEnabled` with value `N` (#2388) --- .../server/migrations/DatabaseChangelog.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) 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 bc015ff7d2..fcda90eaf3 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 @@ -2,7 +2,9 @@ package com.appsmith.server.migrations; import com.appsmith.external.models.BaseDomain; import com.appsmith.external.models.DBAuth; +import com.appsmith.external.models.DatasourceConfiguration; import com.appsmith.external.models.Policy; +import com.appsmith.external.models.Property; import com.appsmith.server.acl.AppsmithRole; import com.appsmith.server.constants.FieldName; import com.appsmith.server.domains.Action; @@ -1516,4 +1518,51 @@ public class DatabaseChangelog { ); } } + + @ChangeSet(order = "047", id = "add-isSendSessionEnabled-key-for-datasources", author = "") + public void addIsSendSessionEnabledPropertyInDatasources(MongoTemplate mongoTemplate) { + + String keyName = "isSendSessionEnabled"; + + Plugin restApiPlugin = mongoTemplate.findOne( + query(where("packageName").is("restapi-plugin")), + Plugin.class + ); + + final org.springframework.data.mongodb.core.query.Query query = query(where("pluginId").is(restApiPlugin.getId())); + + for (Datasource datasource : mongoTemplate.find(query, Datasource.class)) { + // Find if the datasource should be updated with the new key + Boolean updateRequired = false; + if (datasource.getDatasourceConfiguration() == null) { + updateRequired = true; + datasource.setDatasourceConfiguration(new DatasourceConfiguration()); + datasource.getDatasourceConfiguration().setProperties(new ArrayList<>()); + } else if (datasource.getDatasourceConfiguration().getProperties() == null) { + updateRequired = true; + datasource.getDatasourceConfiguration().setProperties(new ArrayList<>()); + } else { + List properties = datasource.getDatasourceConfiguration().getProperties(); + Optional isSendSessionEnabledOptional = properties + .stream() + .filter(property -> property.getKey().equals(keyName)) + .findFirst(); + + if (!isSendSessionEnabledOptional.isPresent()) { + updateRequired = true; + } + } + + // If the property does not exist, add the same. + if (updateRequired) { + Property newProperty = new Property(); + newProperty.setKey(keyName); + newProperty.setValue("N"); + datasource.getDatasourceConfiguration().getProperties().add(newProperty); + mongoTemplate.save(datasource); + } + + } + + } }