From 060cf92e555990e1c2d0e119f0efe5469778af47 Mon Sep 17 00:00:00 2001 From: Anagh Hegde Date: Wed, 16 Oct 2024 18:15:25 +0530 Subject: [PATCH] chore: sync changes from pg branch (#36556) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Added the changes from move to pg script from the postgres branch to keep in sync. ## Summary by CodeRabbit ## Summary by CodeRabbit - **New Features** - Enhanced data transformation process for improved compatibility with Postgres. - Introduced mapping for `_class` field to `type` field. - **Bug Fixes** - Improved handling of asynchronous operations during MongoDB client instantiation. - **Refactor** - Clarified condition checks in data transformation logic for better readability. > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: > Commit: 46e043c998767acb456920f36459ae6f8968a267 > Cypress dashboard. > Tags: `@tag.Sanity` > Spec: >
Mon, 30 Sep 2024 07:14:51 UTC /ok-to-test tags="@tag.Sanity" --- .../appsmith/utils/bin/move-to-postgres.mjs | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/deploy/docker/fs/opt/appsmith/utils/bin/move-to-postgres.mjs b/deploy/docker/fs/opt/appsmith/utils/bin/move-to-postgres.mjs index de799d3b6a..efdae3404f 100644 --- a/deploy/docker/fs/opt/appsmith/utils/bin/move-to-postgres.mjs +++ b/deploy/docker/fs/opt/appsmith/utils/bin/move-to-postgres.mjs @@ -91,7 +91,7 @@ for await (const collectionName of sortedCollectionNames) { if (isArchivedObject(doc)) { continue; } - transformFields(doc); + transformFields(doc); // This now handles the _class to type transformation. if (doc.policyMap == null) { doc.policyMap = {}; } @@ -147,16 +147,39 @@ function replacer(key, value) { * Method to transform the data in the object to be compatible with Postgres. * Updates: * 1. Changes the _id field to id, and removes the _id field. + * 2. Replaces the _class field with the appropriate type field. * @param {Document} obj - The object to transform. * @returns {void} - No return value. */ function transformFields(obj) { for (const key in obj) { - if (key === "_id") { // Change the _id field to id + if (key === "_id") { obj.id = obj._id.toString(); delete obj._id; - } else if (typeof obj[key] === "object") { + } else if (key === "_class") { + const type = mapClassToType(obj._class); + if (type) { + obj.type = type; // Add the type field + } + delete obj._class; // Remove the _class field + } else if (typeof obj[key] === "object" && obj[key] !== null) { transformFields(obj[key]); } } -} \ No newline at end of file +} + +/** + * Map the _class field to the appropriate type value. The DatasourceStorage class requires this check + * @param {string} _class - The _class field value. + * @returns {string|null} - The corresponding type value, or null if no match is found. + */ +function mapClassToType(_class) { + switch (_class) { + case "com.appsmith.external.models.DatasourceStructure$PrimaryKey": + return "primary key"; + case "com.appsmith.external.models.DatasourceStructure$ForeignKey": + return "foreign key"; + default: + return null; + } +}