-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add base script for verifying migrated attributes (#12841)
* Add datastore entity comparison function (except readNotification) * Add verify attribute entity base script Co-authored-by: Kevin Foong <[email protected]>
- Loading branch information
1 parent
59fbae7
commit ff9f3c8
Showing
6 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
src/client/java/teammates/client/scripts/sql/VerifyNonCourseEntityAttributesBaseScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package teammates.client.scripts.sql; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.List; | ||
import java.util.LinkedList; | ||
import java.util.Map; | ||
|
||
import jakarta.persistence.criteria.CriteriaBuilder; | ||
import jakarta.persistence.criteria.CriteriaQuery; | ||
import jakarta.persistence.criteria.Root; | ||
import teammates.client.connector.DatastoreClient; | ||
import teammates.client.util.ClientProperties; | ||
import teammates.common.util.HibernateUtil; | ||
|
||
/** | ||
* Protected methods may be overriden | ||
*/ | ||
public abstract class VerifyNonCourseEntityAttributesBaseScript | ||
<E, T> extends DatastoreClient { | ||
|
||
protected Class<E> datastoreEntityClass; | ||
protected Class<T> sqlEntityClass; | ||
|
||
public VerifyNonCourseEntityAttributesBaseScript( | ||
Class<E> datastoreEntityClass, Class<T> sqlEntityClass) { | ||
this.datastoreEntityClass = datastoreEntityClass; | ||
this.sqlEntityClass = sqlEntityClass; | ||
|
||
String connectionUrl = ClientProperties.SCRIPT_API_URL; | ||
String username = ClientProperties.SCRIPT_API_NAME; | ||
String password = ClientProperties.SCRIPT_API_PASSWORD; | ||
|
||
HibernateUtil.buildSessionFactory(connectionUrl, username, password); | ||
} | ||
|
||
/** | ||
* Generate the Datstore id of entity to compare with on Datastore side. | ||
*/ | ||
protected abstract String generateID(T sqlEntity); | ||
|
||
protected E lookupDataStoreEntity(String datastoreEntityId) { | ||
return ofy().load().type(datastoreEntityClass).id(datastoreEntityId).now(); | ||
} | ||
|
||
protected List<T> lookupSqlEntities() { | ||
HibernateUtil.beginTransaction(); | ||
CriteriaBuilder cb = HibernateUtil.getCriteriaBuilder(); | ||
CriteriaQuery<T> cr = cb.createQuery(sqlEntityClass); | ||
Root<T> root = cr.from(sqlEntityClass); | ||
|
||
cr.select(root); | ||
|
||
List<T> sqlEntities = HibernateUtil.createQuery(cr).getResultList(); | ||
HibernateUtil.commitTransaction(); | ||
|
||
return sqlEntities; | ||
} | ||
|
||
/** | ||
* Idea: lookup sql side, have all the sql entities for | ||
* each sql entity, lookup datastore entity | ||
* if does not match, return failure | ||
*/ | ||
protected List<Map.Entry<T, E>> checkAllEntitiesForFailures() { | ||
List<T> sqlEntities = lookupSqlEntities(); | ||
|
||
List<Map.Entry<T, E>> failures = new LinkedList<>(); | ||
|
||
for (T sqlEntity : sqlEntities) { | ||
String entityId = generateID(sqlEntity); | ||
E datastoreEntity = lookupDataStoreEntity(entityId); | ||
|
||
if (datastoreEntity == null) { | ||
failures.add(new AbstractMap.SimpleEntry<T, E>(sqlEntity, null)); | ||
continue; | ||
} | ||
boolean isEqual = sqlEntity.equals(datastoreEntity); | ||
if (!isEqual) { | ||
failures.add(new AbstractMap.SimpleEntry<T,E>(sqlEntity, datastoreEntity)); | ||
continue; | ||
} | ||
} | ||
return failures; | ||
} | ||
|
||
/** | ||
* Main function to run to verify isEqual between sql and datastore DBs. | ||
*/ | ||
protected void runCheckAllEntities(Class<T> sqlEntityClass, | ||
Class<E> datastoreEntityClass) { | ||
List<Map.Entry<T, E>> failedEntities = checkAllEntitiesForFailures(); | ||
System.out.println("========================================"); | ||
if (!failedEntities.isEmpty()) { | ||
System.err.println("Errors detected for entity: " + sqlEntityClass.getName()); | ||
for (Map.Entry<T, E> failure : failedEntities) { | ||
System.err.println("Sql entity: " + failure.getKey() + " datastore entity: " + failure.getValue()); | ||
} | ||
} else { | ||
System.out.println("No errors detected for entity: " + sqlEntityClass.getName()); | ||
} | ||
} | ||
|
||
protected void doOperation() { | ||
runCheckAllEntities(this.sqlEntityClass, this.datastoreEntityClass); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/client/java/teammates/client/scripts/sql/VerifyNotificationAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package teammates.client.scripts.sql; | ||
|
||
import teammates.storage.entity.Notification; | ||
|
||
public class VerifyNotificationAttributes extends VerifyNonCourseEntityAttributesBaseScript<Notification, | ||
teammates.storage.sqlentity.Notification> { | ||
|
||
static String dataStoreIdFieldName = "notificationId"; | ||
|
||
public VerifyNotificationAttributes() { | ||
super(Notification.class, | ||
teammates.storage.sqlentity.Notification.class); | ||
} | ||
|
||
@Override | ||
protected String generateID(teammates.storage.sqlentity.Notification sqlEntity) { | ||
return sqlEntity.getId().toString(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
VerifyNotificationAttributes script = new VerifyNotificationAttributes(); | ||
script.doOperationRemotely(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters