Skip to content

Commit 57f7d04

Browse files
committed
feat: bulk inserting the bulk migration data
1 parent a5c740b commit 57f7d04

12 files changed

+716
-48
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ public BulkImportProxyConnection(Connection con) {
3939

4040
@Override
4141
public void close() throws SQLException {
42-
// this.con.close();
42+
//this.con.close(); // why are we against the close?
4343
}
4444

4545
@Override
4646
public void commit() throws SQLException {
47-
// this.con.commit();
47+
//this.con.commit();
4848
}
4949

5050
@Override
5151
public void rollback() throws SQLException {
52-
// this.con.rollback();
52+
//this.con.rollback();
5353
}
5454

5555
public void closeForBulkImportProxyStorage() throws SQLException {

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
2121
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
2222
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
23+
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
2324
import io.supertokens.pluginInterface.sqlStorage.TransactionConnection;
2425

2526
import java.sql.Connection;
@@ -51,15 +52,15 @@ public synchronized Connection getTransactionConnection() throws SQLException, S
5152

5253
@Override
5354
protected <T> T startTransactionHelper(TransactionLogic<T> logic, TransactionIsolationLevel isolationLevel)
54-
throws StorageQueryException, StorageTransactionLogicException, SQLException {
55+
throws StorageQueryException, StorageTransactionLogicException, SQLException, TenantOrAppNotFoundException {
5556
return logic.mainLogicAndCommit(new TransactionConnection(getTransactionConnection()));
5657
}
5758

5859
@Override
5960
public void commitTransaction(TransactionConnection con) throws StorageQueryException {
6061
// We do not want to commit the queries when using the BulkImportProxyStorage to be able to rollback everything
6162
// if any query fails while importing the user
62-
// super.commitTransaction(con);
63+
//super.commitTransaction(con);
6364
}
6465

6566
@Override
@@ -110,12 +111,4 @@ public void rollbackTransactionForBulkImportProxyStorage() throws StorageQueryEx
110111
throw new StorageQueryException(e);
111112
}
112113
}
113-
114-
public void doVacuumFull() throws StorageQueryException {
115-
try {
116-
this.connection.prepareStatement("VACUUM FULL").execute();
117-
} catch (SQLException e) {
118-
throw new StorageQueryException(e);
119-
}
120-
}
121114
}

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

Lines changed: 169 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import io.supertokens.pluginInterface.dashboard.DashboardUser;
3535
import io.supertokens.pluginInterface.dashboard.exceptions.UserIdNotFoundException;
3636
import io.supertokens.pluginInterface.dashboard.sqlStorage.DashboardSQLStorage;
37+
import io.supertokens.pluginInterface.emailpassword.EmailPasswordImportUser;
3738
import io.supertokens.pluginInterface.emailpassword.PasswordResetTokenInfo;
3839
import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicateEmailException;
3940
import io.supertokens.pluginInterface.emailpassword.exceptions.DuplicatePasswordResetTokenException;
@@ -68,12 +69,14 @@
6869
import io.supertokens.pluginInterface.oauth.exception.OAuthClientNotFoundException;
6970
import io.supertokens.pluginInterface.passwordless.PasswordlessCode;
7071
import io.supertokens.pluginInterface.passwordless.PasswordlessDevice;
72+
import io.supertokens.pluginInterface.passwordless.PasswordlessImportUser;
7173
import io.supertokens.pluginInterface.passwordless.exception.*;
7274
import io.supertokens.pluginInterface.passwordless.sqlStorage.PasswordlessSQLStorage;
7375
import io.supertokens.pluginInterface.session.SessionInfo;
7476
import io.supertokens.pluginInterface.session.SessionStorage;
7577
import io.supertokens.pluginInterface.session.sqlStorage.SessionSQLStorage;
7678
import io.supertokens.pluginInterface.sqlStorage.TransactionConnection;
79+
import io.supertokens.pluginInterface.thirdparty.ThirdPartyImportUser;
7780
import io.supertokens.pluginInterface.thirdparty.exception.DuplicateThirdPartyUserException;
7881
import io.supertokens.pluginInterface.thirdparty.sqlStorage.ThirdPartySQLStorage;
7982
import io.supertokens.pluginInterface.totp.TOTPDevice;
@@ -110,10 +113,7 @@
110113
import java.sql.Connection;
111114
import java.sql.SQLException;
112115
import java.sql.SQLTransactionRollbackException;
113-
import java.util.ArrayList;
114-
import java.util.HashMap;
115-
import java.util.List;
116-
import java.util.Set;
116+
import java.util.*;
117117

118118
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.execute;
119119

@@ -298,7 +298,8 @@ public <T> T startTransaction(TransactionLogic<T> logic, TransactionIsolationLev
298298
tries++;
299299
try {
300300
return startTransactionHelper(logic, isolationLevel);
301-
} catch (SQLException | StorageQueryException | StorageTransactionLogicException e) {
301+
} catch (SQLException | StorageQueryException | StorageTransactionLogicException |
302+
TenantOrAppNotFoundException e) {
302303
Throwable actualException = e;
303304
if (e instanceof StorageQueryException) {
304305
actualException = e.getCause();
@@ -358,14 +359,16 @@ public <T> T startTransaction(TransactionLogic<T> logic, TransactionIsolationLev
358359
throw (StorageQueryException) e;
359360
} else if (e instanceof StorageTransactionLogicException) {
360361
throw (StorageTransactionLogicException) e;
362+
} else if (e instanceof TenantOrAppNotFoundException) {
363+
throw new StorageTransactionLogicException(e);
361364
}
362365
throw new StorageQueryException(e);
363366
}
364367
}
365368
}
366369

367370
protected <T> T startTransactionHelper(TransactionLogic<T> logic, TransactionIsolationLevel isolationLevel)
368-
throws StorageQueryException, StorageTransactionLogicException, SQLException {
371+
throws StorageQueryException, StorageTransactionLogicException, SQLException, TenantOrAppNotFoundException { // TODO here something is fucked up
369372
Connection con = null;
370373
Integer defaultTransactionIsolation = null;
371374
try {
@@ -968,6 +971,13 @@ public AuthRecipeUserInfo signUp(TenantIdentifier tenantIdentifier, String id, S
968971
}
969972
}
970973

974+
@Override
975+
public void signUpMultiple(List<EmailPasswordImportUser> users)
976+
throws StorageQueryException, DuplicateUserIdException, DuplicateEmailException,
977+
TenantOrAppNotFoundException, StorageTransactionLogicException {
978+
EmailPasswordQueries.signUpMultiple(this, users);
979+
}
980+
971981
@Override
972982
public void deleteEmailPasswordUser_Transaction(TransactionConnection con, AppIdentifier appIdentifier,
973983
String userId, boolean deleteUserIdMappingToo)
@@ -1145,6 +1155,35 @@ public void updateIsEmailVerified_Transaction(AppIdentifier appIdentifier, Trans
11451155
}
11461156
}
11471157

1158+
@Override
1159+
public void updateMultipleIsEmailVerified_Transaction(AppIdentifier appIdentifier, TransactionConnection con,
1160+
Map<String, String> emailToUserId, boolean isEmailVerified)
1161+
throws StorageQueryException, TenantOrAppNotFoundException {
1162+
Connection sqlCon = (Connection) con.getConnection();
1163+
try {
1164+
EmailVerificationQueries.updateMultipleUsersIsEmailVerified_Transaction(this, sqlCon, appIdentifier,
1165+
emailToUserId, isEmailVerified);
1166+
} catch (SQLException e) {
1167+
if (e instanceof PSQLException) {
1168+
PostgreSQLConfig config = Config.getConfig(this);
1169+
ServerErrorMessage serverMessage = ((PSQLException) e).getServerErrorMessage();
1170+
1171+
if (isForeignKeyConstraintError(serverMessage, config.getEmailVerificationTable(), "app_id")) {
1172+
throw new TenantOrAppNotFoundException(appIdentifier);
1173+
}
1174+
}
1175+
1176+
boolean isPSQLPrimKeyError = e instanceof PSQLException && isPrimaryKeyError(
1177+
((PSQLException) e).getServerErrorMessage(),
1178+
Config.getConfig(this).getEmailVerificationTable());
1179+
1180+
if (!isEmailVerified || !isPSQLPrimKeyError) {
1181+
throw new StorageQueryException(e);
1182+
}
1183+
// we do not throw an error since the email is already verified
1184+
}
1185+
}
1186+
11481187
@Override
11491188
public void deleteEmailVerificationUserInfo_Transaction(TransactionConnection con, AppIdentifier appIdentifier,
11501189
String userId)
@@ -1329,6 +1368,19 @@ public void deleteThirdPartyUser_Transaction(TransactionConnection con, AppIdent
13291368
}
13301369
}
13311370

1371+
@Override
1372+
public void importThirdPartyUsers_Transaction(TransactionConnection con,
1373+
Collection<ThirdPartyImportUser> usersToImport)
1374+
throws StorageQueryException {
1375+
try {
1376+
Connection sqlCon = (Connection) con.getConnection();
1377+
ThirdPartyQueries.importUser_Transaction(this, sqlCon, usersToImport);
1378+
} catch (SQLException e) {
1379+
e.printStackTrace(System.out);
1380+
throw new StorageQueryException(e);
1381+
}
1382+
}
1383+
13321384
@Override
13331385
public long getUsersCount(TenantIdentifier tenantIdentifier, RECIPE_ID[] includeRecipeIds)
13341386
throws StorageQueryException {
@@ -1847,6 +1899,18 @@ public void deletePasswordlessUser_Transaction(TransactionConnection con, AppIde
18471899
}
18481900
}
18491901

1902+
@Override
1903+
public void importPasswordlessUsers_Transaction(TransactionConnection con,
1904+
Collection<PasswordlessImportUser> users)
1905+
throws StorageQueryException {
1906+
try {
1907+
Connection sqlCon = (Connection) con.getConnection();
1908+
PasswordlessQueries.importUsers_Transaction(sqlCon, this, users);
1909+
} catch (SQLException e) {
1910+
throw new StorageQueryException(e);
1911+
}
1912+
}
1913+
18501914
@Override
18511915
public PasswordlessDevice getDevice(TenantIdentifier tenantIdentifier, String deviceIdHash)
18521916
throws StorageQueryException {
@@ -1939,6 +2003,18 @@ public JsonObject getUserMetadata_Transaction(AppIdentifier appIdentifier, Trans
19392003
}
19402004
}
19412005

2006+
@Override
2007+
public Map<String, JsonObject> getMultipleUsersMetadatas_Transaction(AppIdentifier appIdentifier, TransactionConnection
2008+
con, List<String> userIds)
2009+
throws StorageQueryException {
2010+
Connection sqlCon = (Connection) con.getConnection();
2011+
try {
2012+
return UserMetadataQueries.getMultipleUsersMetadatas_Transaction(this, sqlCon, appIdentifier, userIds);
2013+
} catch (SQLException e) {
2014+
throw new StorageQueryException(e);
2015+
}
2016+
}
2017+
19422018
@Override
19432019
public int setUserMetadata_Transaction(AppIdentifier appIdentifier, TransactionConnection con, String userId,
19442020
JsonObject metadata)
@@ -1960,6 +2036,26 @@ public int setUserMetadata_Transaction(AppIdentifier appIdentifier, TransactionC
19602036
}
19612037
}
19622038

2039+
@Override
2040+
public void setMultipleUsersMetadatas_Transaction(AppIdentifier appIdentifier, TransactionConnection con,
2041+
Map<String, JsonObject> metadataByUserId)
2042+
throws StorageQueryException, TenantOrAppNotFoundException {
2043+
Connection sqlCon = (Connection) con.getConnection();
2044+
try {
2045+
UserMetadataQueries.setMultipleUsersMetadatas_Transaction(this, sqlCon, appIdentifier, metadataByUserId);
2046+
} catch (SQLException e) {
2047+
if (e instanceof PSQLException) {
2048+
PostgreSQLConfig config = Config.getConfig(this);
2049+
ServerErrorMessage serverMessage = ((PSQLException) e).getServerErrorMessage();
2050+
2051+
if (isForeignKeyConstraintError(serverMessage, config.getUserMetadataTable(), "app_id")) {
2052+
throw new TenantOrAppNotFoundException(appIdentifier);
2053+
}
2054+
}
2055+
throw new StorageQueryException(e);
2056+
}
2057+
}
2058+
19632059
@Override
19642060
public int deleteUserMetadata_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId)
19652061
throws StorageQueryException {
@@ -2000,6 +2096,17 @@ public void addRoleToUser(TenantIdentifier tenantIdentifier, String userId, Stri
20002096
}
20012097
throw new StorageQueryException(e);
20022098
}
2099+
}
2100+
2101+
@Override
2102+
public void addRolesToUsers_Transaction(TransactionConnection connection,
2103+
Map<TenantIdentifier, Map<String, String>> rolesToUserByTenants)
2104+
throws StorageQueryException {
2105+
try {
2106+
UserRolesQueries.addRolesToUsers_Transaction(this, (Connection) connection.getConnection(), rolesToUserByTenants);
2107+
} catch (SQLException e) {
2108+
throw new StorageQueryException(e);
2109+
}
20032110

20042111
}
20052112

@@ -2746,6 +2853,26 @@ public TOTPDevice createDevice_Transaction(TransactionConnection con, AppIdentif
27462853
}
27472854
}
27482855

2856+
@Override
2857+
public void createDevices_Transaction(TransactionConnection con, AppIdentifier appIdentifier,
2858+
List<TOTPDevice> devices)
2859+
throws StorageQueryException, TenantOrAppNotFoundException {
2860+
Connection sqlCon = (Connection) con.getConnection();
2861+
try {
2862+
TOTPQueries.createDevices_Transaction(this, sqlCon, appIdentifier, devices);
2863+
} catch (SQLException e) {
2864+
Exception actualException = e;
2865+
2866+
if (actualException instanceof PSQLException) {
2867+
ServerErrorMessage errMsg = ((PSQLException) actualException).getServerErrorMessage();
2868+
if (isForeignKeyConstraintError(errMsg, Config.getConfig(this).getTotpUsersTable(), "app_id")) {
2869+
throw new TenantOrAppNotFoundException(appIdentifier);
2870+
}
2871+
}
2872+
throw new StorageQueryException(e);
2873+
}
2874+
}
2875+
27492876
@Override
27502877
public TOTPDevice getDeviceByName_Transaction(TransactionConnection con, AppIdentifier appIdentifier, String userId,
27512878
String deviceName) throws StorageQueryException {
@@ -2945,6 +3072,18 @@ public AuthRecipeUserInfo getPrimaryUserById_Transaction(AppIdentifier appIdenti
29453072
}
29463073
}
29473074

3075+
@Override
3076+
public List<AuthRecipeUserInfo> getPrimaryUsersByIds_Transaction(AppIdentifier appIdentifier, TransactionConnection con,
3077+
List<String> userIds)
3078+
throws StorageQueryException {
3079+
try {
3080+
Connection sqlCon = (Connection) con.getConnection();
3081+
return GeneralQueries.getPrimaryUserInfosForUserIds_Transaction(this, sqlCon, appIdentifier, userIds);
3082+
} catch (SQLException e) {
3083+
throw new StorageQueryException(e);
3084+
}
3085+
}
3086+
29483087
@Override
29493088
public AuthRecipeUserInfo[] listPrimaryUsersByEmail_Transaction(AppIdentifier appIdentifier,
29503089
TransactionConnection con, String email)
@@ -3012,6 +3151,17 @@ public void makePrimaryUser_Transaction(AppIdentifier appIdentifier, Transaction
30123151
}
30133152
}
30143153

3154+
@Override
3155+
public void makePrimaryUsers_Transaction(AppIdentifier appIdentifier, TransactionConnection con,
3156+
List<String> userIds) throws StorageQueryException {
3157+
try {
3158+
Connection sqlCon = (Connection) con.getConnection();
3159+
GeneralQueries.makePrimaryUsers_Transaction(this, sqlCon, appIdentifier, userIds);
3160+
} catch (SQLException e) {
3161+
throw new StorageQueryException(e);
3162+
}
3163+
}
3164+
30153165
@Override
30163166
public void linkAccounts_Transaction(AppIdentifier appIdentifier, TransactionConnection con, String recipeUserId,
30173167
String primaryUserId) throws StorageQueryException {
@@ -3025,6 +3175,19 @@ public void linkAccounts_Transaction(AppIdentifier appIdentifier, TransactionCon
30253175
}
30263176
}
30273177

3178+
@Override
3179+
public void linkMultipleAccounts_Transaction(AppIdentifier appIdentifier, TransactionConnection con,
3180+
Map<String, String> recipeUserIdByPrimaryUserId)
3181+
throws StorageQueryException {
3182+
try {
3183+
Connection sqlCon = (Connection) con.getConnection();
3184+
GeneralQueries.linkMultipleAccounts_Transaction(this, sqlCon, appIdentifier, recipeUserIdByPrimaryUserId);
3185+
} catch (SQLException e) {
3186+
throw new StorageQueryException(e);
3187+
}
3188+
3189+
}
3190+
30283191
@Override
30293192
public void unlinkAccounts_Transaction(AppIdentifier appIdentifier, TransactionConnection con, String primaryUserId,
30303193
String recipeUserId)

0 commit comments

Comments
 (0)