Skip to content

Commit e482fb7

Browse files
fix: null and empty checks to avoid DB exception because of invalid syntax (#252)
Co-authored-by: Sattvik Chakravarthy <sattvik@supertokens.com>
1 parent d68415e commit e482fb7

13 files changed

+104
-64
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
99

1010
## [8.0.3]
1111

12+
- Fixes `StorageTransactionLogicException` in bulk import when not using userRoles and totpDevices in import json.
1213
- Adds `USE_STRUCTURED_LOGGING` environment variable to control the logging format.
1314

1415
## [8.0.2]

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ static <T> T execute(Connection con, String QUERY, PreparedStatementValueSetter
4747

4848
static void executeBatch(Connection connection, String QUERY, List<PreparedStatementValueSetter> setters)
4949
throws SQLException, StorageQueryException {
50-
assert setters != null;
51-
assert !setters.isEmpty();
50+
if(setters == null || setters.isEmpty()) {
51+
return;
52+
}
5253
try (PreparedStatement pst = connection.prepareStatement(QUERY)) {
5354
int counter = 0;
5455
for(PreparedStatementValueSetter setter: setters) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ public static Long getLastActiveByUserId(Start start, AppIdentifier appIdentifie
137137

138138
public static Map<String, Long> getLastActiveByMultipleUserIds(Start start, AppIdentifier appIdentifier, List<String> userIds)
139139
throws StorageQueryException {
140+
if(userIds == null || userIds.isEmpty()) {
141+
return new HashMap<>();
142+
}
140143
String QUERY = "SELECT user_id, last_active_time FROM " + Config.getConfig(start).getUserLastActiveTable()
141144
+ " WHERE app_id = ? AND user_id IN ( " + Utils.generateCommaSeperatedQuestionMarks(userIds.size())+ " )";
142145

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ public static List<String> getPrimaryUserIdsUsingMultipleEmails_Transaction(Star
629629
AppIdentifier appIdentifier,
630630
List<String> emails)
631631
throws StorageQueryException, SQLException {
632-
if(emails.isEmpty()){
632+
if(emails == null || emails.isEmpty()){
633633
return new ArrayList<>();
634634
}
635635
String QUERY = "SELECT DISTINCT all_users.primary_or_recipe_user_id AS user_id "

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public UserIdAndEmail(String userId, String email) {
283283
public static List<String> isEmailVerified_transaction(Start start, Connection sqlCon, AppIdentifier appIdentifier,
284284
List<UserIdAndEmail> userIdAndEmail)
285285
throws SQLException, StorageQueryException {
286-
if (userIdAndEmail.isEmpty()) {
286+
if (userIdAndEmail == null || userIdAndEmail.isEmpty()) {
287287
return new ArrayList<>();
288288
}
289289
List<String> emails = new ArrayList<>();
@@ -356,7 +356,7 @@ public static List<String> isEmailVerified(Start start, AppIdentifier appIdentif
356356
List<UserIdAndEmail> userIdAndEmail)
357357
throws SQLException, StorageQueryException {
358358

359-
if (userIdAndEmail.isEmpty()) {
359+
if (userIdAndEmail == null || userIdAndEmail.isEmpty()) {
360360
return new ArrayList<>();
361361
}
362362
List<String> emails = new ArrayList<>();
@@ -510,6 +510,10 @@ public static boolean isUserIdBeingUsedForEmailVerification(Start start, AppIden
510510
public static Set<String> findUserIdsBeingUsedForEmailVerification(Start start, AppIdentifier appIdentifier, List<String> userIds)
511511
throws SQLException, StorageQueryException {
512512

513+
if (userIds == null || userIds.isEmpty()) {
514+
return new HashSet<>();
515+
}
516+
513517
Set<String> foundUserIds = new HashSet<>();
514518

515519
String email_verificiation_tokens_QUERY = "SELECT * FROM " + getConfig(start).getEmailVerificationTokensTable()

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,9 @@ public static boolean doesUserIdExist(Start start, TenantIdentifier tenantIdenti
915915

916916
public static List<String> findUserIdsThatExist(Start start, AppIdentifier appIdentifier, List<String> userIds)
917917
throws SQLException, StorageQueryException {
918+
if (userIds == null || userIds.isEmpty()){
919+
return new ArrayList<>();
920+
}
918921
String QUERY = "SELECT user_id FROM " + getConfig(start).getAppIdToUserIdTable()
919922
+ " WHERE app_id = ? AND user_id IN ("+ Utils.generateCommaSeperatedQuestionMarks(userIds.size()) +")";
920923
return execute(start, QUERY, pst -> {
@@ -1655,7 +1658,7 @@ private static List<AuthRecipeUserInfo> getPrimaryUserInfoForUserIds(Start start
16551658
AppIdentifier appIdentifier,
16561659
List<String> userIds)
16571660
throws StorageQueryException, SQLException {
1658-
if (userIds.size() == 0) {
1661+
if (userIds == null || userIds.isEmpty()){
16591662
return new ArrayList<>();
16601663
}
16611664

@@ -1751,7 +1754,7 @@ private static List<AuthRecipeUserInfo> getPrimaryUserInfoForUserIds_Transaction
17511754
AppIdentifier appIdentifier,
17521755
List<String> userIds)
17531756
throws StorageQueryException, SQLException {
1754-
if (userIds.size() == 0) {
1757+
if (userIds == null || userIds.isEmpty()){
17551758
return new ArrayList<>();
17561759
}
17571760

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ public static PasswordlessCode getCodeByLinkCodeHash(Start start, TenantIdentifi
738738
public static List<LoginMethod> getUsersInfoUsingIdList(Start start, Set<String> ids,
739739
AppIdentifier appIdentifier)
740740
throws SQLException, StorageQueryException {
741-
if (ids.size() > 0) {
741+
if (ids != null && !ids.isEmpty()) {
742742
// No need to filter based on tenantId because the id list is already filtered for a tenant
743743
String QUERY = "SELECT user_id, email, phone_number, time_joined "
744744
+ "FROM " + getConfig(start).getPasswordlessUsersTable() + " WHERE user_id IN (" +
@@ -768,7 +768,7 @@ public static List<LoginMethod> getUsersInfoUsingIdList(Start start, Set<String>
768768
public static List<LoginMethod> getUsersInfoUsingIdList_Transaction(Start start, Connection con, Set<String> ids,
769769
AppIdentifier appIdentifier)
770770
throws SQLException, StorageQueryException {
771-
if (ids.size() > 0) {
771+
if (ids != null && !ids.isEmpty()) {
772772
// No need to filter based on tenantId because the id list is already filtered for a tenant
773773
String QUERY = "SELECT user_id, email, phone_number, time_joined "
774774
+ "FROM " + getConfig(start).getPasswordlessUsersTable() + " WHERE user_id IN (" +
@@ -947,7 +947,7 @@ public static List<String> getPrimaryUserIdsUsingMultipleEmails_Transaction(Star
947947
AppIdentifier appIdentifier,
948948
List<String> emails)
949949
throws StorageQueryException, SQLException {
950-
if(emails.isEmpty()){
950+
if(emails == null || emails.isEmpty()){
951951
return new ArrayList<>();
952952
}
953953
String QUERY = "SELECT DISTINCT all_users.primary_or_recipe_user_id AS user_id "
@@ -1017,7 +1017,7 @@ public static List<String> listUserIdsByMultiplePhoneNumber_Transaction(Start st
10171017
AppIdentifier appIdentifier,
10181018
@Nonnull List<String> phoneNumbers)
10191019
throws StorageQueryException, SQLException {
1020-
if(phoneNumbers.isEmpty()){
1020+
if(phoneNumbers == null || phoneNumbers.isEmpty()){
10211021
return new ArrayList<>();
10221022
}
10231023
String QUERY = "SELECT DISTINCT all_users.primary_or_recipe_user_id AS user_id "

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ public static String[] getAllNonExpiredSessionHandlesForUser(Start start, AppIde
352352
public static Map<String, List<String>> getAllNonExpiredSessionHandlesForUsers(Start start, AppIdentifier appIdentifier,
353353
List<String> userIds)
354354
throws SQLException, StorageQueryException {
355+
if(userIds == null || userIds.isEmpty()){
356+
return new HashMap<>();
357+
}
355358
String QUERY = "SELECT user_id, session_handle FROM " + getConfig(start).getSessionInfoTable()
356359
+ " WHERE app_id = ? AND expires_at >= ? AND user_id IN ( " + Utils.generateCommaSeperatedQuestionMarks(userIds.size()) + " )";
357360

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

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -150,43 +150,44 @@ public static void createDevice_Transaction(Start start, Connection sqlCon, AppI
150150
public static void createDevices_Transaction(Start start, Connection sqlCon, AppIdentifier appIdentifier,
151151
List<TOTPDevice> devices)
152152
throws SQLException, StorageQueryException {
153+
if(devices != null && !devices.isEmpty()) {
154+
String insert_user_QUERY = "INSERT INTO " + Config.getConfig(start).getTotpUsersTable()
155+
+ " (app_id, user_id) VALUES (?, ?) ON CONFLICT DO NOTHING";
156+
157+
String insert_device_QUERY = "INSERT INTO " + Config.getConfig(start).getTotpUserDevicesTable()
158+
+
159+
" (app_id, user_id, device_name, secret_key, period, skew, verified, created_at) VALUES (?, ?, ?, ?, " +
160+
"?, ?, ?, ?) ON CONFLICT (app_id, user_id, device_name) DO UPDATE SET secret_key = ?, period = ?, skew = ?, created_at = ?, verified = ?";
161+
162+
List<PreparedStatementValueSetter> userSetters = new ArrayList<>();
163+
List<PreparedStatementValueSetter> deviceSetters = new ArrayList<>();
164+
165+
for (TOTPDevice device : devices) {
166+
userSetters.add(pst -> {
167+
pst.setString(1, appIdentifier.getAppId());
168+
pst.setString(2, device.userId);
169+
});
170+
171+
deviceSetters.add(pst -> {
172+
pst.setString(1, appIdentifier.getAppId());
173+
pst.setString(2, device.userId);
174+
pst.setString(3, device.deviceName);
175+
pst.setString(4, device.secretKey);
176+
pst.setInt(5, device.period);
177+
pst.setInt(6, device.skew);
178+
pst.setBoolean(7, device.verified);
179+
pst.setLong(8, device.createdAt);
180+
pst.setString(9, device.secretKey);
181+
pst.setInt(10, device.period);
182+
pst.setInt(11, device.skew);
183+
pst.setLong(12, device.createdAt);
184+
pst.setBoolean(13, device.verified);
185+
});
186+
}
153187

154-
String insert_user_QUERY = "INSERT INTO " + Config.getConfig(start).getTotpUsersTable()
155-
+ " (app_id, user_id) VALUES (?, ?) ON CONFLICT DO NOTHING";
156-
157-
String insert_device_QUERY = "INSERT INTO " + Config.getConfig(start).getTotpUserDevicesTable()
158-
+
159-
" (app_id, user_id, device_name, secret_key, period, skew, verified, created_at) VALUES (?, ?, ?, ?, " +
160-
"?, ?, ?, ?) ON CONFLICT (app_id, user_id, device_name) DO UPDATE SET secret_key = ?, period = ?, skew = ?, created_at = ?, verified = ?";
161-
162-
List<PreparedStatementValueSetter> userSetters = new ArrayList<>();
163-
List<PreparedStatementValueSetter> deviceSetters = new ArrayList<>();
164-
165-
for(TOTPDevice device : devices){
166-
userSetters.add(pst -> {
167-
pst.setString(1, appIdentifier.getAppId());
168-
pst.setString(2, device.userId);
169-
});
170-
171-
deviceSetters.add(pst -> {
172-
pst.setString(1, appIdentifier.getAppId());
173-
pst.setString(2, device.userId);
174-
pst.setString(3, device.deviceName);
175-
pst.setString(4, device.secretKey);
176-
pst.setInt(5, device.period);
177-
pst.setInt(6, device.skew);
178-
pst.setBoolean(7, device.verified);
179-
pst.setLong(8, device.createdAt);
180-
pst.setString(9, device.secretKey);
181-
pst.setInt(10, device.period);
182-
pst.setInt(11, device.skew);
183-
pst.setLong(12, device.createdAt);
184-
pst.setBoolean(13, device.verified);
185-
});
188+
executeBatch(sqlCon, insert_user_QUERY, userSetters);
189+
executeBatch(sqlCon, insert_device_QUERY, deviceSetters);
186190
}
187-
188-
executeBatch(sqlCon, insert_user_QUERY, userSetters);
189-
executeBatch(sqlCon, insert_device_QUERY, deviceSetters);
190191
}
191192

192193
public static TOTPDevice getDeviceByName_Transaction(Start start, Connection sqlCon, AppIdentifier appIdentifier,
@@ -290,6 +291,9 @@ public static TOTPDevice[] getDevices(Start start, AppIdentifier appIdentifier,
290291

291292
public static Map<String, List<TOTPDevice>> getDevicesForMultipleUsers(Start start, AppIdentifier appIdentifier, List<String> userIds)
292293
throws StorageQueryException, SQLException {
294+
if(userIds == null || userIds.isEmpty()){
295+
return new HashMap<>();
296+
}
293297
String QUERY = "SELECT * FROM " + Config.getConfig(start).getTotpUserDevicesTable()
294298
+ " WHERE app_id = ? AND user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(userIds.size()) + ");";
295299

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public static List<String> lockThirdPartyInfoAndTenant_Transaction(Start start,
306306
public static List<LoginMethod> getUsersInfoUsingIdList(Start start, Set<String> ids,
307307
AppIdentifier appIdentifier)
308308
throws SQLException, StorageQueryException {
309-
if (ids.size() > 0) {
309+
if (ids != null && !ids.isEmpty()) {
310310
String QUERY = "SELECT user_id, third_party_id, third_party_user_id, email, time_joined "
311311
+ "FROM " + getConfig(start).getThirdPartyUsersTable() + " WHERE user_id IN (" +
312312
Utils.generateCommaSeperatedQuestionMarks(ids.size()) + ") AND app_id = ?";
@@ -338,7 +338,7 @@ public static List<LoginMethod> getUsersInfoUsingIdList(Start start, Set<String>
338338
public static List<LoginMethod> getUsersInfoUsingIdList_Transaction(Start start, Connection con, Set<String> ids,
339339
AppIdentifier appIdentifier)
340340
throws SQLException, StorageQueryException {
341-
if (ids.size() > 0) {
341+
if (ids != null && !ids.isEmpty()) {
342342
String QUERY = "SELECT user_id, third_party_id, third_party_user_id, email, time_joined "
343343
+ "FROM " + getConfig(start).getThirdPartyUsersTable() + " WHERE user_id IN (" +
344344
Utils.generateCommaSeperatedQuestionMarks(ids.size()) + ") AND app_id = ?";
@@ -417,7 +417,7 @@ public static List<String> listUserIdsByMultipleThirdPartyInfo_Transaction(Start
417417
AppIdentifier appIdentifier,
418418
Map<String, String> thirdPartyUserIdToThirdPartyId)
419419
throws SQLException, StorageQueryException {
420-
if(thirdPartyUserIdToThirdPartyId.isEmpty()){
420+
if(thirdPartyUserIdToThirdPartyId == null || thirdPartyUserIdToThirdPartyId.isEmpty()){
421421
return new ArrayList<>();
422422
}
423423
String QUERY = "SELECT DISTINCT all_users.primary_or_recipe_user_id AS user_id "
@@ -554,7 +554,7 @@ public static List<String> getPrimaryUserIdsUsingMultipleEmails_Transaction(Star
554554
AppIdentifier appIdentifier,
555555
List<String> emails)
556556
throws StorageQueryException, SQLException {
557-
if(emails.isEmpty()){
557+
if(emails == null || emails.isEmpty()){
558558
return new ArrayList<>();
559559
}
560560
String QUERY = "SELECT DISTINCT all_users.primary_or_recipe_user_id AS user_id "

0 commit comments

Comments
 (0)