Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#12048] Data migration for feedback session entities #12986

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.get(teammates.storage.sqlentity.Course.class, oldEntity.getCourseId());
HibernateUtil.commitTransaction();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This transaction is probably causing the migration to be slow, we probably can optimise this by joining courses with its feedback sessions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to use EntityManager.getReference.


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);
}

}
45 changes: 41 additions & 4 deletions src/client/java/teammates/client/scripts/sql/SeedDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import teammates.storage.entity.Account;
import teammates.storage.entity.AccountRequest;
import teammates.storage.entity.Course;
import teammates.storage.entity.FeedbackSession;
import teammates.storage.entity.Notification;
import teammates.test.FileHelper;

Expand All @@ -40,6 +41,7 @@
public class SeedDb extends DatastoreClient {

private static final int MAX_ENTITY_SIZE = 10000;
private static final int MAX_FEEDBACKSESSION_FOR_EACH_COURSE_SIZE = 3;
private final LogicExtension logic = new LogicExtension();
private Closeable closeable;

Expand Down Expand Up @@ -106,12 +108,12 @@ protected void persistAdditionalData() {
String[] args = {};
// Each account will have this amount of read notifications
seedNotificationAccountAndAccountRequest(5, 1000);
seedCourse();
seedCourseAndRelatedEntites();

GenerateUsageStatisticsObjects.main(args);
}

private void seedCourse() {
private void seedCourseAndRelatedEntites() {
log("Seeding courses");
for (int i = 0; i < MAX_ENTITY_SIZE; i++) {
if (i % (MAX_ENTITY_SIZE / 5) == 0) {
Expand All @@ -120,19 +122,54 @@ private void seedCourse() {
}

Random rand = new Random();

String courseId = UUID.randomUUID().toString();
try {
String courseName = String.format("Course %s", i);
String courseInstitute = String.format("Institute %s", i);
String courseTimeZone = String.format("Time Zone %s", i);
Course course = new Course(UUID.randomUUID().toString(), courseName, courseTimeZone, courseInstitute,
Course course = new Course(courseId, courseName, courseTimeZone, courseInstitute,
getRandomInstant(),
rand.nextInt(3) > 1 ? null : getRandomInstant(), // set deletedAt randomly at 25% chance
false);
ofy().save().entities(course).now();
} catch (Exception e) {
log(e.toString());
}

// Uncomment to seed feedback sessions
// seedFeedbackSession(courseId);
}
}

private void seedFeedbackSession(String courseId) {
Random rand = new Random();
for (int i = 0; i < MAX_FEEDBACKSESSION_FOR_EACH_COURSE_SIZE; i++) {
try {
String feedbackSessionName = String.format("Feedback Session %s", i);
String feedbackSessionCourseId = courseId;
String feedbackSessionCreatorEmail = String.format("Creator Email %s", i);
String feedbackSessionInstructions = String.format("Instructions %s", i);
String timezone = String.format("Time Zone %s", i);
Instant feedbackSessionStartTime = getRandomInstant();
Instant feedbackSessionEndTime = getRandomInstant();
Instant feedbackSessionSessionVisibleFromTime = getRandomInstant();
Instant feedbackSessionResultsVisibleFromTime = getRandomInstant();
int feedbackSessionGracePeriod = rand.nextInt(3600);

FeedbackSession feedbackSession = new FeedbackSession(feedbackSessionName, feedbackSessionCourseId,
feedbackSessionCreatorEmail, feedbackSessionInstructions, getRandomInstant(),
rand.nextInt(3) > 1 ? null : getRandomInstant(), // set deletedAt randomly at 25% chance
feedbackSessionStartTime, feedbackSessionEndTime,
feedbackSessionSessionVisibleFromTime, feedbackSessionResultsVisibleFromTime,
timezone, feedbackSessionGracePeriod,
rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(),
rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(),
new HashMap<String, Instant>(), new HashMap<String, Instant>());

ofy().save().entities(feedbackSession).now();
} catch (Exception e) {
log(e.toString());
}
}
}

Expand Down
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();
}

}