Skip to content

Commit

Permalink
Create script to verify row count
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasCwy committed Feb 25, 2024
1 parent e7a133d commit f201159
Showing 1 changed file with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package teammates.client.scripts.sql;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.fasterxml.jackson.databind.JsonSerializable.Base;

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;
import teammates.storage.sqlentity.UsageStatistics;

public class VerifyNonCourseEntityCounts extends DatastoreClient {
// private Closeable closeable;

// public void setupObjectify() {
// DatastoreOptions.Builder builder = DatastoreOptions.newBuilder().setProjectId(Config.APP_ID);
// ObjectifyService.init(new ObjectifyFactory(builder.build().getService()));
// OfyHelper.registerEntityClasses();

// closeable = ObjectifyService.begin();
// }

// public void tearDownObjectify() {
// closeable.close();
// }

private VerifyNonCourseEntityCounts() {
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 VerifyNonCourseEntityCounts().doOperationRemotely();
}


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() {
HashMap<
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.Account.class, teammates.storage.sqlentity.Account.class);
entities.put(teammates.storage.entity.AccountRequest.class, teammates.storage.sqlentity.AccountRequest.class);
entities.put(teammates.storage.entity.UsageStatistics.class, teammates.storage.sqlentity.UsageStatistics.class);
entities.put(teammates.storage.entity.Notification.class, teammates.storage.sqlentity.Notification.class);



for (Map.Entry<Class<? extends BaseEntity>, Class<? extends teammates.storage.sqlentity.BaseEntity>> entry : entities.entrySet()) {
// fetch number of entities in datastore
Class<? extends BaseEntity> objectifyClass = entry.getKey();
Class<? extends teammates.storage.sqlentity.BaseEntity> sqlClass = entry.getValue();

int objectifyEntityCount = ofy().load().type(objectifyClass).count();
// fetch number of entities in postgres

Long postgresEntityCount = countPostgresEntities(sqlClass);


System.out.println("========================================");
System.out.println(objectifyClass.getName());
System.out.println("Objectify count: " + objectifyEntityCount);
System.out.println("Postgres count: " + postgresEntityCount);

}
ofy().load().type(teammates.storage.entity.UsageStatistics.class);
}
}

0 comments on commit f201159

Please sign in to comment.