Skip to content

Commit 2a2f618

Browse files
committed
fix: fixing table locked issue with in-memory db
1 parent ed884bd commit 2a2f618

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,19 @@ public AuthRecipeUserInfo getPrimaryUserByWebauthNCredentialId(TenantIdentifier
16921692
}
16931693
}
16941694

1695+
@Override
1696+
public AuthRecipeUserInfo getPrimaryUserByWebauthNCredentialId_Transaction(TenantIdentifier tenantIdentifier,
1697+
TransactionConnection con,
1698+
String credentialId)
1699+
throws StorageQueryException {
1700+
try {
1701+
Connection sqlCon = (Connection) con.getConnection();
1702+
return GeneralQueries.getPrimaryUserByWebauthNCredentialId_Transaction(this, sqlCon, tenantIdentifier, credentialId);
1703+
} catch (SQLException | StorageTransactionLogicException e) {
1704+
throw new StorageQueryException(e);
1705+
}
1706+
}
1707+
16951708
@Override
16961709
public AuthRecipeUserInfo getPrimaryUserByThirdPartyInfo(TenantIdentifier tenantIdentifier, String thirdPartyId,
16971710
String thirdPartyUserId) throws StorageQueryException {
@@ -4062,6 +4075,8 @@ public AuthRecipeUserInfo signUpWithCredentialsRegister_Transaction(TenantIdenti
40624075
PostgreSQLConfig config = Config.getConfig(this);
40634076

40644077
if (isUniqueConstraintError(errorMessage, config.getWebAuthNUserToTenantTable(),"email")) {
4078+
Logging.error(this, errorMessage.getMessage(), true);
4079+
Logging.error(this, email, true);
40654080
throw new DuplicateUserEmailException();
40664081
} else if (isPrimaryKeyError(errorMessage, config.getWebAuthNUsersTable())
40674082
|| isPrimaryKeyError(errorMessage, config.getUsersTable())

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,9 @@ public static void createTablesIfNotExists(Start start, Connection con) throws S
649649
if(!doesTableExists(start, con, Config.getConfig(start).getWebAuthNCredentialsTable())){
650650
getInstance(start).addState(CREATING_NEW_TABLE, null);
651651
update(con, WebAuthNQueries.getQueryToCreateWebAuthNCredentialsTable(start), NO_OP_SETTER);
652+
653+
//index
654+
//TODO
652655
}
653656

654657
if(!doesTableExists(start, con, Config.getConfig(start).getWebAuthNAccountRecoveryTokenTable())){
@@ -1654,11 +1657,10 @@ public static AuthRecipeUserInfo getPrimaryUserByWebauthNCredentialId(Start star
16541657
TenantIdentifier tenantIdentifier,
16551658
String credentialId)
16561659
throws StorageQueryException, SQLException, StorageTransactionLogicException {
1657-
// TODO: revisit this. Seems like we are loading the same data multiple times
16581660
AuthRecipeUserInfo webauthnUser = start.startTransaction(con -> {
16591661
try {
16601662
Connection sqlCon = (Connection) con.getConnection();
1661-
return WebAuthNQueries.getUserInfoByCredentialId_Transaction(start, sqlCon, tenantIdentifier,
1663+
return getPrimaryUserByWebauthNCredentialId_Transaction(start, sqlCon, tenantIdentifier,
16621664
credentialId);
16631665
} catch (SQLException e) {
16641666
throw new StorageQueryException(e);
@@ -1667,6 +1669,16 @@ public static AuthRecipeUserInfo getPrimaryUserByWebauthNCredentialId(Start star
16671669
return getPrimaryUserInfoForUserId(start, tenantIdentifier.toAppIdentifier(), webauthnUser.getSupertokensUserId());
16681670
}
16691671

1672+
public static AuthRecipeUserInfo getPrimaryUserByWebauthNCredentialId_Transaction(Start start,
1673+
Connection connection,
1674+
TenantIdentifier tenantIdentifier,
1675+
String credentialId)
1676+
throws StorageQueryException, SQLException, StorageTransactionLogicException {
1677+
AuthRecipeUserInfo webauthnUser = WebAuthNQueries.getUserInfoByCredentialId_Transaction(start, connection, tenantIdentifier,
1678+
credentialId);
1679+
return getPrimaryUserInfoForUserId(start, tenantIdentifier.toAppIdentifier(), webauthnUser.getSupertokensUserId());
1680+
}
1681+
16701682
public static String getPrimaryUserIdStrForUserId(Start start, AppIdentifier appIdentifier, String id)
16711683
throws SQLException, StorageQueryException {
16721684
String QUERY = "SELECT primary_or_recipe_user_id FROM " + getConfig(start).getUsersTable() +

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ static String getQueryToCreateWebAuthNCredentialsTable(Start start){
129129
");";
130130
}
131131

132+
static String getQueryToCreateWebAuthNCredentialsUserIdIndex(Start start) {
133+
return "CREATE INDEX webauthn_credentials_user_id_index ON " +
134+
getConfig(start).getWebAuthNCredentialsTable() +
135+
" (user_id);";
136+
}
137+
132138
public static String getQueryToCreateWebAuthNAccountRecoveryTokenTable(Start start) {
133139
return "CREATE TABLE IF NOT EXISTS " + getConfig(start).getWebAuthNAccountRecoveryTokenTable() + "(" +
134140
" app_id VARCHAR(64) DEFAULT 'public' NOT NULL," +
@@ -399,10 +405,10 @@ public static String getPrimaryUserIdUsingEmail(Start start, AppIdentifier appId
399405
public static String getPrimaryUserIdUsingEmail_Transaction(Start start, Connection sqlConnection, AppIdentifier appIdentifier, String email)
400406
throws SQLException, StorageQueryException {
401407
String QUERY = "SELECT DISTINCT all_users.primary_or_recipe_user_id AS user_id "
402-
+ "FROM " + getConfig(start).getWebAuthNUserToTenantTable() + " AS ep" +
408+
+ "FROM " + getConfig(start).getWebAuthNUserToTenantTable() + " AS webauthn" +
403409
" JOIN " + getConfig(start).getUsersTable() + " AS all_users" +
404-
" ON ep.app_id = all_users.app_id AND ep.user_id = all_users.user_id" +
405-
" WHERE ep.app_id = ? AND ep.email = ?";
410+
" ON webauthn.app_id = all_users.app_id AND webauthn.user_id = all_users.user_id" +
411+
" WHERE webauthn.app_id = ? AND webauthn.email = ?";
406412

407413
return execute(sqlConnection, QUERY, pst -> {
408414
pst.setString(1, appIdentifier.getAppId());
@@ -448,7 +454,8 @@ public static Collection<? extends LoginMethod> getUsersInfoUsingIdList_Transact
448454
"JOIN " + usersTable + " as all_users ON webauthn.app_id = all_users.app_id AND webauthn.user_id = all_users.user_id " +
449455
"LEFT JOIN " + credentialTable + " as credentials ON webauthn.user_id = credentials.user_id " +
450456
"LEFT JOIN " + userIdMappingTable + " as user_id_mapping ON webauthn.user_id = user_id_mapping.supertokens_user_id " +
451-
"LEFT JOIN " + emailVerificationTable + " as email_verification ON webauthn.app_id = email_verification.app_id AND user_id_mapping.external_user_id = email_verification.user_id OR user_id_mapping.supertokens_user_id = email_verification.user_id " +
457+
"LEFT JOIN " + emailVerificationTable + " as email_verification ON webauthn.app_id = " +
458+
"email_verification.app_id AND user_id_mapping.external_user_id = email_verification.user_id OR user_id_mapping.supertokens_user_id = email_verification.user_id OR webauthn.user_id = email_verification.user_id " +
452459
"WHERE webauthn.app_id = ? AND webauthn.user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(ids.size()) + ")";
453460

454461
return execute(connection, queryAll, pst -> {
@@ -495,7 +502,8 @@ public static AuthRecipeUserInfo getUserInfoByCredentialId_Transaction(Start sta
495502
"JOIN " + getConfig(start).getUsersTable() + " as all_users ON webauthn.app_id = all_users.app_id AND webauthn.user_id = all_users.user_id " +
496503
"LEFT JOIN " + getConfig(start).getWebAuthNCredentialsTable() + " as credentials ON webauthn.user_id = credentials.user_id " +
497504
"LEFT JOIN " + getConfig(start).getUserIdMappingTable() + " as user_id_mapping ON webauthn.user_id = user_id_mapping.supertokens_user_id " +
498-
"LEFT JOIN " + getConfig(start).getEmailVerificationTable() + " as email_verification ON webauthn.app_id = email_verification.app_id AND user_id_mapping.external_user_id = email_verification.user_id OR user_id_mapping.supertokens_user_id = email_verification.user_id " +
505+
"LEFT JOIN " + getConfig(start).getEmailVerificationTable() + " as email_verification ON webauthn" +
506+
".app_id = email_verification.app_id AND user_id_mapping.external_user_id = email_verification.user_id OR user_id_mapping.supertokens_user_id = email_verification.user_id OR webauthn.user_id = email_verification.user_id " +
499507
"WHERE webauthn.app_id = ? AND credentials.id = ?";
500508

501509
return execute(sqlCon, QUERY, pst -> {

0 commit comments

Comments
 (0)