From 67d7d5f3d955793dde6545e55d1d7721359e0d1e Mon Sep 17 00:00:00 2001 From: Fergus Mok Date: Fri, 29 Mar 2024 11:09:43 +0800 Subject: [PATCH] Add changes --- ...ationForAccountAndReadNotificationSql.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/client/java/teammates/client/scripts/sql/DataMigrationForAccountAndReadNotificationSql.java b/src/client/java/teammates/client/scripts/sql/DataMigrationForAccountAndReadNotificationSql.java index 2401b05acc2..0d7becb01ec 100644 --- a/src/client/java/teammates/client/scripts/sql/DataMigrationForAccountAndReadNotificationSql.java +++ b/src/client/java/teammates/client/scripts/sql/DataMigrationForAccountAndReadNotificationSql.java @@ -20,6 +20,11 @@ import com.google.cloud.datastore.QueryResults; import com.googlecode.objectify.cmd.Query; +import jakarta.persistence.TypedQuery; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; + // import jakarta.persistence.criteria.CriteriaDelete; import teammates.client.connector.DatastoreClient; @@ -126,6 +131,27 @@ private void doMigration(teammates.storage.entity.Account entity) { * Migrates the entity. */ protected void migrateEntity(teammates.storage.entity.Account oldAccount) { + HibernateUtil.beginTransaction(); + + CriteriaBuilder cb = HibernateUtil.getCriteriaBuilder(); + CriteriaQuery cr = cb.createQuery( + teammates.storage.sqlentity.Account.class); + Root root = cr.from(teammates.storage.sqlentity.Account.class); + cr.select(root).where(cb.equal(root.get("googleId"), oldAccount.getGoogleId())); + + TypedQuery query = HibernateUtil.createQuery(cr); + + + boolean isEntityInDb = query.getResultList().size() != 0; + HibernateUtil.commitTransaction(); + + // In db, but somehow not set as migrated. + if (isEntityInDb) { + oldAccount.setMigrated(true); + entitiesOldAccountSavingBuffer.add(oldAccount); + return; + }; + teammates.storage.sqlentity.Account newAccount = new teammates.storage.sqlentity.Account( oldAccount.getGoogleId(), oldAccount.getName(), @@ -136,7 +162,6 @@ protected void migrateEntity(teammates.storage.entity.Account oldAccount) { oldAccount.setMigrated(true); entitiesOldAccountSavingBuffer.add(oldAccount); migrateReadNotification(oldAccount, newAccount); - } private void migrateReadNotification(teammates.storage.entity.Account oldAccount,