Skip to content

Commit 550ce1b

Browse files
committed
fix: additional indexes, updateEmail_Transactional
1 parent 2a2f618 commit 550ce1b

File tree

4 files changed

+67
-24
lines changed

4 files changed

+67
-24
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4233,6 +4233,30 @@ public void updateUserEmail(TenantIdentifier tenantIdentifier, String userId, St
42334233
}
42344234
}
42354235

4236+
@Override
4237+
public void updateUserEmail_Transaction(TenantIdentifier tenantIdentifier, TransactionConnection con, String userId,
4238+
String newEmail)
4239+
throws StorageQueryException, io.supertokens.pluginInterface.webauthn.exceptions.UserIdNotFoundException,
4240+
DuplicateUserEmailException {
4241+
try {
4242+
Connection sqlCon = (Connection) con.getConnection();
4243+
WebAuthNQueries.updateUserEmail_Transaction(this, sqlCon, tenantIdentifier, userId, newEmail);
4244+
} catch (StorageQueryException e) {
4245+
if (e.getCause() instanceof SQLException){
4246+
ServerErrorMessage errorMessage = ((PSQLException) e.getCause()).getServerErrorMessage();
4247+
PostgreSQLConfig config = Config.getConfig(this);
4248+
4249+
if (isUniqueConstraintError(errorMessage, config.getWebAuthNUserToTenantTable(),
4250+
"email")) {
4251+
throw new DuplicateUserEmailException();
4252+
} else if (isForeignKeyConstraintError(errorMessage,config.getWebAuthNUserToTenantTable(),"user_id")) {
4253+
throw new io.supertokens.pluginInterface.webauthn.exceptions.UserIdNotFoundException();
4254+
}
4255+
}
4256+
throw new StorageQueryException(e);
4257+
}
4258+
}
4259+
42364260
@Override
42374261
public AccountRecoveryTokenInfo getAccountRecoveryTokenInfoByToken_Transaction(TenantIdentifier tenantIdentifier,
42384262
TransactionConnection con,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public static String getQueryToCreateAppIdIndexForEmailVerificationTable(Start s
6161
+ Config.getConfig(start).getEmailVerificationTable() + "(app_id);";
6262
}
6363

64+
public static String getQueryToCreateAppIdEmailIndexForEmailVerificationTable(Start start) {
65+
return "CREATE INDEX emailverification_verified_emails_app_id_email_index ON "
66+
+ Config.getConfig(start).getEmailVerificationTable() + "(app_id, email);";
67+
}
68+
6469
static String getQueryToCreateEmailVerificationTokensTable(Start start) {
6570
String schema = Config.getConfig(start).getTableSchema();
6671
String emailVerificationTokensTable = Config.getConfig(start).getEmailVerificationTokensTable();

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ public static void createTablesIfNotExists(Start start, Connection con) throws S
419419

420420
// index
421421
update(con, getQueryToCreateAppIdIndexForEmailVerificationTable(start), NO_OP_SETTER);
422+
update(con, getQueryToCreateAppIdEmailIndexForEmailVerificationTable(start), NO_OP_SETTER);
422423
}
423424

424425
if (!doesTableExists(start, con, Config.getConfig(start).getEmailVerificationTokensTable())) {
@@ -637,6 +638,9 @@ public static void createTablesIfNotExists(Start start, Connection con) throws S
637638
if(!doesTableExists(start, con, Config.getConfig(start).getWebAuthNUserToTenantTable())){
638639
getInstance(start).addState(CREATING_NEW_TABLE, null);
639640
update(con, WebAuthNQueries.getQueryToCreateWebAuthNUsersToTenantTable(start), NO_OP_SETTER);
641+
642+
// index
643+
update(con, WebAuthNQueries.getQueryToCreateWebAuthNUserToTenantEmailIndex(start), NO_OP_SETTER);
640644
}
641645

642646
if(!doesTableExists(start, con, Config.getConfig(start).getWebAuthNGeneratedOptionsTable())){
@@ -651,7 +655,7 @@ public static void createTablesIfNotExists(Start start, Connection con) throws S
651655
update(con, WebAuthNQueries.getQueryToCreateWebAuthNCredentialsTable(start), NO_OP_SETTER);
652656

653657
//index
654-
//TODO
658+
update(con, WebAuthNQueries.getQueryToCreateWebAuthNCredentialsUserIdIndex(start), NO_OP_SETTER);
655659
}
656660

657661
if(!doesTableExists(start, con, Config.getConfig(start).getWebAuthNAccountRecoveryTokenTable())){

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

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ static String getQueryToCreateWebAuthNUsersToTenantTable(Start start){
7878
");";
7979
}
8080

81+
public static String getQueryToCreateWebAuthNUserToTenantEmailIndex(Start start) {
82+
return "CREATE INDEX webauthn_user_to_tenant_email_index ON " +
83+
getConfig(start).getWebAuthNUserToTenantTable() +
84+
" (app_id, email);";
85+
}
86+
8187
static String getQueryToCreateWebAuthNGeneratedOptionsTable(Start start){
8288
String schema = getConfig(start).getTableSchema();
8389
String webAuthNGeneratedOptionsTable = getConfig(start).getWebAuthNGeneratedOptionsTable();
@@ -605,36 +611,40 @@ public static void updateUserEmail(Start start, TenantIdentifier tenantIdentifie
605611
throws StorageQueryException {
606612
try {
607613
start.startTransaction(con -> {
608-
Connection sqlConnection = (Connection) con.getConnection();
609-
try {
610-
String UPDATE_USER_TO_TENANT_QUERY =
611-
"UPDATE " + getConfig(start).getWebAuthNUserToTenantTable() +
612-
" SET email = ? WHERE app_id = ? AND tenant_id = ? AND user_id = ?";
613-
String UPDATE_USER_QUERY = "UPDATE " + getConfig(start).getWebAuthNUsersTable() +
614-
" SET email = ? WHERE app_id = ? AND user_id = ?";
615-
616-
update(sqlConnection, UPDATE_USER_TO_TENANT_QUERY, pst -> {
617-
pst.setString(1, newEmail);
618-
pst.setString(2, tenantIdentifier.getAppId());
619-
pst.setString(3, tenantIdentifier.getTenantId());
620-
pst.setString(4, userId);
621-
});
622-
623-
update(sqlConnection, UPDATE_USER_QUERY, pst -> {
624-
pst.setString(1, newEmail);
625-
pst.setString(2, tenantIdentifier.getAppId());
626-
pst.setString(3, userId);
627-
});
628-
} catch (SQLException e) {
629-
throw new StorageQueryException(e);
630-
}
614+
updateUserEmail_Transaction(start, (Connection) con.getConnection(), tenantIdentifier, userId, newEmail);
631615
return null;
632616
});
633617
} catch (StorageTransactionLogicException e) {
634618
throw new StorageQueryException(e);
635619
}
636620
}
637621

622+
public static void updateUserEmail_Transaction(Start start, Connection sqlConnection, TenantIdentifier tenantIdentifier,
623+
String userId, String newEmail) throws StorageQueryException {
624+
try {
625+
String UPDATE_USER_TO_TENANT_QUERY =
626+
"UPDATE " + getConfig(start).getWebAuthNUserToTenantTable() +
627+
" SET email = ? WHERE app_id = ? AND tenant_id = ? AND user_id = ?";
628+
String UPDATE_USER_QUERY = "UPDATE " + getConfig(start).getWebAuthNUsersTable() +
629+
" SET email = ? WHERE app_id = ? AND user_id = ?";
630+
631+
update(sqlConnection, UPDATE_USER_TO_TENANT_QUERY, pst -> {
632+
pst.setString(1, newEmail);
633+
pst.setString(2, tenantIdentifier.getAppId());
634+
pst.setString(3, tenantIdentifier.getTenantId());
635+
pst.setString(4, userId);
636+
});
637+
638+
update(sqlConnection, UPDATE_USER_QUERY, pst -> {
639+
pst.setString(1, newEmail);
640+
pst.setString(2, tenantIdentifier.getAppId());
641+
pst.setString(3, userId);
642+
});
643+
} catch (SQLException e) {
644+
throw new StorageQueryException(e);
645+
}
646+
}
647+
638648
private static class WebAuthnStoredCredentialRowMapper implements RowMapper<WebAuthNStoredCredential, ResultSet> {
639649
private static final WebAuthnStoredCredentialRowMapper INSTANCE = new WebAuthnStoredCredentialRowMapper();
640650

0 commit comments

Comments
 (0)