-
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.
Browse files
Browse the repository at this point in the history
* Add migration and verification script for feedback session * Optimize data migration (foreign key reference fetch function) * Add counts verification script --------- Co-authored-by: YX Z <[email protected]> Co-authored-by: nicolascwy <[email protected]>
- Loading branch information
1 parent
44f3e0b
commit bb58c58
Showing
5 changed files
with
255 additions
and
4 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/client/java/teammates/client/scripts/sql/DataMigrationForFeedbackSessionSql.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,74 @@ | ||
package teammates.client.scripts.sql; | ||
|
||
import java.time.Duration; | ||
|
||
// CHECKSTYLE.OFF:ImportOrder | ||
import com.googlecode.objectify.cmd.Query; | ||
import teammates.common.util.HibernateUtil; | ||
import teammates.storage.entity.FeedbackSession; | ||
import teammates.storage.sqlentity.Course; | ||
|
||
// CHECKSTYLE.ON:ImportOrder | ||
/** | ||
* Data migration class for feddback sessions. | ||
*/ | ||
@SuppressWarnings("PMD") | ||
public class DataMigrationForFeedbackSessionSql | ||
extends DataMigrationEntitiesBaseScriptSql<FeedbackSession, teammates.storage.sqlentity.FeedbackSession> { | ||
|
||
public static void main(String[] args) { | ||
new DataMigrationForFeedbackSessionSql().doOperationRemotely(); | ||
} | ||
|
||
@Override | ||
protected Query<FeedbackSession> getFilterQuery() { | ||
return ofy().load().type(teammates.storage.entity.FeedbackSession.class); | ||
} | ||
|
||
@Override | ||
protected boolean isPreview() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected void setMigrationCriteria() { | ||
// No migration criteria currently needed. | ||
} | ||
|
||
@Override | ||
protected boolean isMigrationNeeded(FeedbackSession entity) { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected void migrateEntity(FeedbackSession oldEntity) throws Exception { | ||
HibernateUtil.beginTransaction(); | ||
Course course = HibernateUtil.getReference(teammates.storage.sqlentity.Course.class, oldEntity.getCourseId()); | ||
HibernateUtil.commitTransaction(); | ||
|
||
teammates.storage.sqlentity.FeedbackSession newFeedbackSession = new teammates.storage.sqlentity.FeedbackSession( | ||
oldEntity.getFeedbackSessionName(), | ||
course, | ||
oldEntity.getCreatorEmail(), | ||
oldEntity.getInstructions(), | ||
oldEntity.getStartTime(), | ||
oldEntity.getEndTime(), | ||
oldEntity.getSessionVisibleFromTime(), | ||
oldEntity.getResultsVisibleFromTime(), | ||
Duration.ofMinutes(oldEntity.getGracePeriod()), | ||
oldEntity.isOpeningEmailEnabled(), | ||
oldEntity.isClosingEmailEnabled(), | ||
oldEntity.isPublishedEmailEnabled() | ||
); | ||
|
||
newFeedbackSession.setClosedEmailSent(oldEntity.isSentClosedEmail()); | ||
newFeedbackSession.setClosingSoonEmailSent(oldEntity.isSentClosingEmail()); | ||
newFeedbackSession.setOpenEmailSent(oldEntity.isSentOpenEmail()); | ||
newFeedbackSession.setOpeningSoonEmailSent(oldEntity.isSentOpeningSoonEmail()); | ||
newFeedbackSession.setPublishedEmailSent(oldEntity.isSentPublishedEmail()); | ||
newFeedbackSession.setDeletedAt(oldEntity.getDeletedTime()); | ||
|
||
saveEntityDeferred(newFeedbackSession); | ||
} | ||
|
||
} |
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
75 changes: 75 additions & 0 deletions
75
src/client/java/teammates/client/scripts/sql/VerifyCourseEntityCounts.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,75 @@ | ||
package teammates.client.scripts.sql; | ||
|
||
// CHECKSTYLE.OFF:ImportOrder | ||
import java.util.HashMap; | ||
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; | ||
import teammates.storage.entity.BaseEntity; | ||
// CHECKSTYLE.ON:ImportOrder | ||
|
||
/** | ||
* Verify the counts of non-course entities are correct. | ||
*/ | ||
@SuppressWarnings("PMD") | ||
public class VerifyCourseEntityCounts extends DatastoreClient { | ||
private VerifyCourseEntityCounts() { | ||
String connectionUrl = ClientProperties.SCRIPT_API_URL; | ||
String username = ClientProperties.SCRIPT_API_NAME; | ||
String password = ClientProperties.SCRIPT_API_PASSWORD; | ||
|
||
HibernateUtil.buildSessionFactory(connectionUrl, username, password); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
new VerifyCourseEntityCounts().doOperationRemotely(); | ||
} | ||
|
||
private void printEntityVerification(String className, int datastoreCount, long psqlCount) { | ||
System.out.println("========================================"); | ||
System.out.println(className); | ||
System.out.println("Objectify count: " + datastoreCount); | ||
System.out.println("Postgres count: " + psqlCount); | ||
System.out.println("Correct number of rows?: " + (datastoreCount == psqlCount)); | ||
} | ||
|
||
private Long countPostgresEntities(Class<? extends teammates.storage.sqlentity.BaseEntity> entity) { | ||
HibernateUtil.beginTransaction(); | ||
CriteriaBuilder cb = HibernateUtil.getCriteriaBuilder(); | ||
CriteriaQuery<Long> cr = cb.createQuery(Long.class); | ||
Root<? extends teammates.storage.sqlentity.BaseEntity> root = cr.from(entity); | ||
|
||
cr.select(cb.count(root)); | ||
|
||
Long count = HibernateUtil.createQuery(cr).getSingleResult(); | ||
HibernateUtil.commitTransaction(); | ||
return count; | ||
} | ||
|
||
@Override | ||
protected void doOperation() { | ||
Map<Class<? extends BaseEntity>, Class<? extends teammates.storage.sqlentity.BaseEntity>> entities = | ||
new HashMap<Class<? extends BaseEntity>, Class<? extends teammates.storage.sqlentity.BaseEntity>>(); | ||
|
||
entities.put(teammates.storage.entity.Course.class, teammates.storage.sqlentity.Course.class); | ||
entities.put(teammates.storage.entity.FeedbackSession.class, teammates.storage.sqlentity.FeedbackSession.class); | ||
|
||
// Compare datastore "table" to postgres table for each entity | ||
for (Map.Entry<Class<? extends BaseEntity>, Class<? extends teammates.storage.sqlentity.BaseEntity>> entry : entities | ||
.entrySet()) { | ||
Class<? extends BaseEntity> objectifyClass = entry.getKey(); | ||
Class<? extends teammates.storage.sqlentity.BaseEntity> sqlClass = entry.getValue(); | ||
|
||
int objectifyEntityCount = ofy().load().type(objectifyClass).count(); | ||
Long postgresEntityCount = countPostgresEntities(sqlClass); | ||
|
||
printEntityVerification(objectifyClass.getSimpleName(), objectifyEntityCount, postgresEntityCount); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/client/java/teammates/client/scripts/sql/VerifyFeedbackSessionAttributes.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,55 @@ | ||
package teammates.client.scripts.sql; | ||
|
||
import java.time.Duration; | ||
|
||
import teammates.common.util.SanitizationHelper; | ||
import teammates.storage.entity.FeedbackSession; | ||
|
||
/** | ||
* Verification of the feedback session attributes. | ||
*/ | ||
public class VerifyFeedbackSessionAttributes | ||
extends VerifyNonCourseEntityAttributesBaseScript<FeedbackSession, teammates.storage.sqlentity.FeedbackSession> { | ||
|
||
public VerifyFeedbackSessionAttributes() { | ||
super(FeedbackSession.class, teammates.storage.sqlentity.FeedbackSession.class); | ||
} | ||
|
||
@Override | ||
protected String generateID(teammates.storage.sqlentity.FeedbackSession sqlEntity) { | ||
return FeedbackSession.generateId(sqlEntity.getName(), sqlEntity.getCourse().getId()); | ||
} | ||
|
||
@Override | ||
protected boolean equals(teammates.storage.sqlentity.FeedbackSession sqlEntity, FeedbackSession datastoreEntity) { | ||
try { | ||
return sqlEntity.getCourse().getId().equals(datastoreEntity.getCourseId()) | ||
&& sqlEntity.getName().equals(datastoreEntity.getFeedbackSessionName()) | ||
&& sqlEntity.getCreatorEmail().equals(datastoreEntity.getCreatorEmail()) | ||
&& sqlEntity.getInstructions() | ||
.equals(SanitizationHelper.sanitizeForRichText(datastoreEntity.getInstructions())) | ||
&& sqlEntity.getStartTime().equals(datastoreEntity.getStartTime()) | ||
&& sqlEntity.getEndTime().equals(datastoreEntity.getEndTime()) | ||
&& sqlEntity.getSessionVisibleFromTime().equals(datastoreEntity.getSessionVisibleFromTime()) | ||
&& sqlEntity.getResultsVisibleFromTime().equals(datastoreEntity.getResultsVisibleFromTime()) | ||
&& sqlEntity.getGracePeriod().equals(Duration.ofMinutes(datastoreEntity.getGracePeriod())) | ||
&& sqlEntity.isOpeningEmailEnabled() == datastoreEntity.isOpeningEmailEnabled() | ||
&& sqlEntity.isClosingEmailEnabled() == datastoreEntity.isClosingEmailEnabled() | ||
&& sqlEntity.isOpenEmailSent() == datastoreEntity.isSentOpenEmail() | ||
&& sqlEntity.isOpeningSoonEmailSent() == datastoreEntity.isSentOpeningSoonEmail() | ||
&& sqlEntity.isClosedEmailSent() == datastoreEntity.isSentClosedEmail() | ||
&& sqlEntity.isClosingSoonEmailSent() == datastoreEntity.isSentClosingEmail() | ||
&& sqlEntity.isPublishedEmailSent() == datastoreEntity.isSentPublishedEmail() | ||
&& (sqlEntity.getDeletedAt() == datastoreEntity.getDeletedTime() | ||
|| sqlEntity.getDeletedAt().equals(datastoreEntity.getDeletedTime())); | ||
} catch (IllegalArgumentException iae) { | ||
return false; | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
VerifyFeedbackSessionAttributes script = new VerifyFeedbackSessionAttributes(); | ||
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