Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private synchronized void initialiseHikariDataSource() throws SQLException, Stor
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.addDataSourceProperty("tcpKeepAlive", "true");
config.addDataSourceProperty("socketTimeout", "60");
// TODO: set maxLifetimeValue to lesser than 10 mins so that the following error doesnt happen:
// io.supertokens.storage.postgresql.HikariLoggingAppender.doAppend(HikariLoggingAppender.java:117) |
// SuperTokens
Expand Down
589 changes: 566 additions & 23 deletions src/main/java/io/supertokens/storage/postgresql/Start.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.gson.JsonObject;

import io.supertokens.pluginInterface.ConfigFieldInfo;
import io.supertokens.pluginInterface.MigrationMode;
import io.supertokens.pluginInterface.exceptions.InvalidConfigException;
import io.supertokens.storage.postgresql.Start;
import io.supertokens.storage.postgresql.annotations.*;
Expand Down Expand Up @@ -199,6 +200,15 @@ public class PostgreSQLConfig {
private Integer postgresql_minimum_idle_connections = null;


@EnvName("SUPERTOKENS_MIGRATION_MODE")
@JsonProperty
@IgnoreForAnnotationCheck
@DashboardInfo(
description = "Migration mode for all_auth_recipe_users table deprecation. " +
"Values: LEGACY, DUAL_WRITE_READ_OLD, DUAL_WRITE_READ_NEW, MIGRATED",
defaultValue = "\"LEGACY\"", isOptional = true)
private String migration_mode = null;

@IgnoreForAnnotationCheck
boolean isValidAndNormalised = false;

Expand Down Expand Up @@ -319,6 +329,17 @@ public String getConnectionURI() {
return postgresql_connection_uri;
}

public MigrationMode getMigrationMode() {
if (migration_mode == null) {
return MigrationMode.LEGACY;
}
try {
return MigrationMode.valueOf(migration_mode.toUpperCase());
} catch (IllegalArgumentException e) {
return MigrationMode.LEGACY;
}
}

public String getUsersTable() {
return addSchemaAndPrefixToTableName("all_auth_recipe_users");
}
Expand Down Expand Up @@ -576,6 +597,16 @@ private void validateAndNormalise(boolean skipValidation) throws InvalidConfigEx
+ "'postgresql_connection_pool_size'");
}
}

if (migration_mode != null) {
try {
MigrationMode.valueOf(migration_mode.toUpperCase());
} catch (IllegalArgumentException e) {
throw new InvalidConfigException(
"Invalid migration_mode value: '" + migration_mode +
"'. Must be one of: LEGACY, DUAL_WRITE_READ_OLD, DUAL_WRITE_READ_NEW, MIGRATED");
}
}
}

// Normalisation
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,32 @@ public static void createNewSession(Start start, TenantIdentifier tenantIdentifi
public static SessionInfo getSessionInfo_Transaction(Start start, Connection con, TenantIdentifier tenantIdentifier,
String sessionHandle)
throws SQLException, StorageQueryException {
if (Config.getConfig(start).getMigrationMode().readsFromNewTables()) {
return getSessionInfo_Transaction_new(start, con, tenantIdentifier, sessionHandle);
}
return getSessionInfo_Transaction_legacy(start, con, tenantIdentifier, sessionHandle);
}

private static SessionInfo getSessionInfo_Transaction_legacy(Start start, Connection con,
TenantIdentifier tenantIdentifier,
String sessionHandle)
throws SQLException, StorageQueryException {
return getSessionInfo_Transaction_impl(start, con, tenantIdentifier, sessionHandle,
getConfig(start).getUsersTable());
}

private static SessionInfo getSessionInfo_Transaction_new(Start start, Connection con,
TenantIdentifier tenantIdentifier,
String sessionHandle)
throws SQLException, StorageQueryException {
return getSessionInfo_Transaction_impl(start, con, tenantIdentifier, sessionHandle,
getConfig(start).getAppIdToUserIdTable());
}

private static SessionInfo getSessionInfo_Transaction_impl(Start start, Connection con,
TenantIdentifier tenantIdentifier,
String sessionHandle, String userIdTable)
throws SQLException, StorageQueryException {
String QUERY =
"SELECT session_handle, user_id, refresh_token_hash_2, session_data, " +
"expires_at, created_at_time, jwt_user_payload, use_static_key FROM " +
Expand All @@ -158,7 +184,7 @@ public static SessionInfo getSessionInfo_Transaction(Start start, Connection con
"FROM " + getConfig(start).getUserIdMappingTable() + " um2 " +
"WHERE um2.app_id = ? AND um2.supertokens_user_id IN (" +
"SELECT primary_or_recipe_user_id " +
"FROM " + getConfig(start).getAppIdToUserIdTable() + " " +
"FROM " + userIdTable + " " +
"WHERE app_id = ? AND user_id IN (" +
"SELECT user_id FROM (" +
"SELECT um1.supertokens_user_id as user_id, 0 as o1 " +
Expand All @@ -172,7 +198,7 @@ public static SessionInfo getSessionInfo_Transaction(Start start, Connection con
") " +
"UNION " +
"SELECT primary_or_recipe_user_id, 1 as o " +
"FROM " + getConfig(start).getAppIdToUserIdTable() + " " +
"FROM " + userIdTable + " " +
"WHERE app_id = ? AND user_id IN (" +
"SELECT user_ID FROM (" +
"SELECT um1.supertokens_user_id as user_id, 0 as o2 " +
Expand Down Expand Up @@ -423,14 +449,33 @@ public static int updateSession(Start start, TenantIdentifier tenantIdentifier,

public static SessionInfo getSession(Start start, TenantIdentifier tenantIdentifier, String sessionHandle)
throws SQLException, StorageQueryException {
if (Config.getConfig(start).getMigrationMode().readsFromNewTables()) {
return getSession_new(start, tenantIdentifier, sessionHandle);
}
return getSession_legacy(start, tenantIdentifier, sessionHandle);
}

private static SessionInfo getSession_legacy(Start start, TenantIdentifier tenantIdentifier, String sessionHandle)
throws SQLException, StorageQueryException {
return getSession_impl(start, tenantIdentifier, sessionHandle, getConfig(start).getUsersTable());
}

private static SessionInfo getSession_new(Start start, TenantIdentifier tenantIdentifier, String sessionHandle)
throws SQLException, StorageQueryException {
return getSession_impl(start, tenantIdentifier, sessionHandle, getConfig(start).getAppIdToUserIdTable());
}

private static SessionInfo getSession_impl(Start start, TenantIdentifier tenantIdentifier, String sessionHandle,
String userIdTable)
throws SQLException, StorageQueryException {
String QUERY =
"SELECT sess.session_handle, sess.user_id, sess.refresh_token_hash_2, sess.session_data, sess" +
".expires_at, "
+
"sess.created_at_time, sess.jwt_user_payload, sess.use_static_key, users" +
".primary_or_recipe_user_id FROM " +
getConfig(start).getSessionInfoTable()
+ " AS sess LEFT JOIN " + getConfig(start).getAppIdToUserIdTable() +
+ " AS sess LEFT JOIN " + userIdTable +
" as users ON sess.app_id = users.app_id AND sess.user_id = users.user_id WHERE sess.app_id =" +
" ? AND " +
"sess.tenant_id = ? AND sess.session_handle = ?";
Expand Down
Loading
Loading