From a9c44d61db516a84742649a820f942878b5c2fbe Mon Sep 17 00:00:00 2001 From: Trisha Anand Date: Wed, 5 Mar 2025 15:42:41 +0530 Subject: [PATCH] chore: Adding migration to make user email unique in an organization --- .../ce/Migration067_UpdateUserEmailIndex.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/db/ce/Migration067_UpdateUserEmailIndex.java diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/db/ce/Migration067_UpdateUserEmailIndex.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/db/ce/Migration067_UpdateUserEmailIndex.java new file mode 100644 index 000000000000..81909a73ed00 --- /dev/null +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/db/ce/Migration067_UpdateUserEmailIndex.java @@ -0,0 +1,44 @@ +package com.appsmith.server.migrations.db.ce; + +import com.appsmith.server.domains.User; +import io.mongock.api.annotations.ChangeUnit; +import io.mongock.api.annotations.Execution; +import io.mongock.api.annotations.RollbackExecution; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.index.Index; + +import static com.appsmith.server.migrations.DatabaseChangelog1.dropIndexIfExists; +import static com.appsmith.server.migrations.DatabaseChangelog1.ensureIndexes; +import static org.springframework.data.domain.Sort.Direction.ASC; + +@Slf4j +@ChangeUnit(order = "067", id = "update-user-email-index") +public class Migration067_UpdateUserEmailIndex { + + @RollbackExecution + public void rollbackExecution() {} + + @Execution + public void execute(MongoTemplate mongoTemplate) { + log.info("Starting migration to update user email index"); + try { + // Drop the existing email index + dropIndexIfExists(mongoTemplate, User.class, "email"); + + // Create new compound index on email and organizationId + Index emailOrgIndex = new Index() + .on("email", ASC) + .on("organizationId", ASC) + .unique() + .named("email_organizationId_compound_index"); + + ensureIndexes(mongoTemplate, User.class, emailOrgIndex); + + log.info("Completed migration to update user email index"); + } catch (Exception e) { + log.error("Error updating user email index", e); + throw e; + } + } +}