Skip to content

Commit 727b655

Browse files
authored
fix: Fix tests (#240)
* fix: fixing tests * fix: fixing tests * fix: review fix
1 parent 9161990 commit 727b655

3 files changed

Lines changed: 54 additions & 48 deletions

File tree

src/main/java/io/supertokens/storage/postgresql/Start.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,22 +3590,33 @@ public int getDbActivityCount(String dbname) throws SQLException, StorageQueryEx
35903590
@Override
35913591
public void addBulkImportUsers(AppIdentifier appIdentifier, List<BulkImportUser> users)
35923592
throws StorageQueryException,
3593-
TenantOrAppNotFoundException,
3594-
io.supertokens.pluginInterface.bulkimport.exceptions.DuplicateUserIdException {
3593+
TenantOrAppNotFoundException {
35953594
try {
3596-
BulkImportQueries.insertBulkImportUsers(this, appIdentifier, users);
3597-
} catch (SQLException e) {
3598-
if (e instanceof PSQLException) {
3599-
ServerErrorMessage serverErrorMessage = ((PSQLException) e).getServerErrorMessage();
3600-
if (isPrimaryKeyError(serverErrorMessage, Config.getConfig(this).getBulkImportUsersTable())) {
3601-
throw new io.supertokens.pluginInterface.bulkimport.exceptions.DuplicateUserIdException();
3602-
}
3603-
if (isForeignKeyConstraintError(serverErrorMessage, Config.getConfig(this).getBulkImportUsersTable(),
3604-
"app_id")) {
3605-
throw new TenantOrAppNotFoundException(appIdentifier);
3595+
this.startTransaction(con -> {
3596+
try {
3597+
BulkImportQueries.insertBulkImportUsers_Transaction(this, (Connection) con.getConnection(), appIdentifier, users);
3598+
} catch (SQLException e) {
3599+
if (e instanceof PSQLException) {
3600+
ServerErrorMessage serverErrorMessage = ((PSQLException) e).getServerErrorMessage();
3601+
if (isPrimaryKeyError(serverErrorMessage, Config.getConfig(this).getBulkImportUsersTable())) {
3602+
throw new StorageTransactionLogicException(new io.supertokens.pluginInterface.bulkimport.exceptions.DuplicateUserIdException());
3603+
}
3604+
if (isForeignKeyConstraintError(serverErrorMessage, Config.getConfig(this).getBulkImportUsersTable(),
3605+
"app_id")) {
3606+
throw new TenantOrAppNotFoundException(appIdentifier);
3607+
}
3608+
}
36063609
}
3610+
return null;
3611+
});
3612+
} catch (StorageTransactionLogicException e) {
3613+
if(e.actualException instanceof StorageQueryException) {
3614+
throw (StorageQueryException) e.actualException;
3615+
} else {
3616+
throw new StorageQueryException(e.actualException);
36073617
}
36083618
}
3619+
36093620
}
36103621

36113622
@Override

src/main/java/io/supertokens/storage/postgresql/queries/BulkImportQueries.java

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,24 @@ public static String getQueryToCreatePaginationIndex2(Start start) {
7474
+ Config.getConfig(start).getBulkImportUsersTable() + " (app_id, created_at DESC, id DESC)";
7575
}
7676

77-
public static void insertBulkImportUsers(Start start, AppIdentifier appIdentifier, List<BulkImportUser> users)
77+
public static void insertBulkImportUsers_Transaction(Start start, Connection connection, AppIdentifier appIdentifier, List<BulkImportUser> users)
7878
throws SQLException, StorageQueryException {
79-
StringBuilder queryBuilder = new StringBuilder(
80-
"INSERT INTO " + Config.getConfig(start).getBulkImportUsersTable() + " (id, app_id, raw_data, created_at, updated_at) VALUES ");
81-
82-
int userCount = users.size();
83-
84-
for (int i = 0; i < userCount; i++) {
85-
queryBuilder.append(" (?, ?, ?, ?, ?)");
86-
87-
if (i < userCount - 1) {
88-
queryBuilder.append(",");
89-
}
79+
String queryBuilder = "INSERT INTO " + Config.getConfig(start).getBulkImportUsersTable() +
80+
" (id, app_id, raw_data, created_at, updated_at) VALUES "
81+
+ " (?, ?, ?, ?, ?)";
82+
83+
List<PreparedStatementValueSetter> valueSetters = new ArrayList<>();
84+
for (BulkImportUser user : users) {
85+
valueSetters.add(pst -> {
86+
pst.setString(1, user.id);
87+
pst.setString(2, appIdentifier.getAppId());
88+
pst.setString(3, user.toRawDataForDbStorage());
89+
pst.setLong(4, System.currentTimeMillis());
90+
pst.setLong(5, System.currentTimeMillis());
91+
});
9092
}
9193

92-
update(start, queryBuilder.toString(), pst -> {
93-
int parameterIndex = 1;
94-
for (BulkImportUser user : users) {
95-
pst.setString(parameterIndex++, user.id);
96-
pst.setString(parameterIndex++, appIdentifier.getAppId());
97-
pst.setString(parameterIndex++, user.toRawDataForDbStorage());
98-
pst.setLong(parameterIndex++, System.currentTimeMillis());
99-
pst.setLong(parameterIndex++, System.currentTimeMillis());
100-
}
101-
});
94+
executeBatch(connection, queryBuilder, valueSetters);
10295
}
10396

10497
public static void updateBulkImportUserStatus_Transaction(Start start, Connection con, AppIdentifier appIdentifier,
@@ -178,18 +171,20 @@ public static List<BulkImportUser> getBulkImportUsersAndChangeStatusToProcessing
178171
}
179172

180173
String updateQuery = "UPDATE " + Config.getConfig(start).getBulkImportUsersTable()
181-
+ " SET status = ?, updated_at = ? WHERE app_id = ? AND id IN (" + Utils
182-
.generateCommaSeperatedQuestionMarks(bulkImportUsers.size()) + ")";
183-
184-
update(sqlCon, updateQuery, pst -> {
185-
int index = 1;
186-
pst.setString(index++, BULK_IMPORT_USER_STATUS.PROCESSING.toString());
187-
pst.setLong(index++, System.currentTimeMillis());
188-
pst.setString(index++, appIdentifier.getAppId());
189-
for (BulkImportUser user : bulkImportUsers) {
190-
pst.setObject(index++, user.id);
191-
}
192-
});
174+
+ " SET status = ?, updated_at = ? WHERE app_id = ? AND id = ?";
175+
176+
List<PreparedStatementValueSetter> updateSetters = new ArrayList<>();
177+
for(BulkImportUser user : bulkImportUsers){
178+
updateSetters.add(pst -> {
179+
pst.setString(1, BULK_IMPORT_USER_STATUS.PROCESSING.toString());
180+
pst.setLong(2, System.currentTimeMillis());
181+
pst.setString(3, appIdentifier.getAppId());
182+
pst.setObject(4, user.id);
183+
});
184+
}
185+
186+
executeBatch(sqlCon, updateQuery, updateSetters);
187+
193188
return bulkImportUsers;
194189
} catch (SQLException throwables) {
195190
throw new StorageTransactionLogicException(throwables);

src/test/java/io/supertokens/storage/postgresql/test/OneMillionUsersTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,9 +988,9 @@ public void testWithOneMillionUsers() throws Exception {
988988
return;
989989
}
990990

991-
Main main = startCronProcess(String.valueOf(NUM_THREADS));
991+
Main main = startCronProcess(String.valueOf(4)); // ci uses instances with 4 cores..
992992

993-
int NUMBER_OF_USERS_TO_UPLOAD = 1000000; // million
993+
int NUMBER_OF_USERS_TO_UPLOAD = 500000; // half million
994994

995995
if (StorageLayer.getBaseStorage(main).getType() != STORAGE_TYPE.SQL || StorageLayer.isInMemDb(main)) {
996996
return;

0 commit comments

Comments
 (0)