-
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.
- Loading branch information
1 parent
e7a133d
commit f201159
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
src/client/java/teammates/client/scripts/sql/VerifyNonCourseEntityCounts.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,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); | ||
} | ||
} |