Skip to content

Commit

Permalink
chore: current state save
Browse files Browse the repository at this point in the history
  • Loading branch information
tamassoltesz committed Nov 5, 2024
1 parent a6ed05f commit b83a780
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,7 @@

package io.supertokens.storage.postgresql;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
Expand All @@ -53,17 +39,17 @@ public BulkImportProxyConnection(Connection con) {

@Override
public void close() throws SQLException {
// We simply ignore when close is called BulkImportProxyConnection
// this.con.close();
}

@Override
public void commit() throws SQLException {
// We simply ignore when commit is called BulkImportProxyConnection
// this.con.commit();
}

@Override
public void rollback() throws SQLException {
// We simply ignore when rollback is called BulkImportProxyConnection
// this.con.rollback();
}

public void closeForBulkImportProxyStorage() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@

package io.supertokens.storage.postgresql;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;

import com.google.gson.JsonObject;

import io.supertokens.pluginInterface.LOG_LEVEL;
import io.supertokens.pluginInterface.exceptions.DbInitException;
import io.supertokens.pluginInterface.exceptions.InvalidConfigException;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.sqlStorage.TransactionConnection;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;


/**
* BulkImportProxyStorage is a class extending Start, serving as a Storage instance in the bulk import user cronjob.
Expand Down Expand Up @@ -62,8 +57,9 @@ protected <T> T startTransactionHelper(TransactionLogic<T> logic, TransactionIso

@Override
public void commitTransaction(TransactionConnection con) throws StorageQueryException {
// We do not want to commit the queries when using the BulkImportProxyStorage to be able to rollback everything
// We do not want to commit the queries when using the BulkImportProxyStorage to be able to rollback everything
// if any query fails while importing the user
// super.commitTransaction(con);
}

@Override
Expand Down Expand Up @@ -114,4 +110,12 @@ public void rollbackTransactionForBulkImportProxyStorage() throws StorageQueryEx
throw new StorageQueryException(e);
}
}

public void doVacuumFull() throws StorageQueryException {
try {
this.connection.prepareStatement("VACUUM FULL").execute();
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}
}
13 changes: 9 additions & 4 deletions src/main/java/io/supertokens/storage/postgresql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
import io.supertokens.pluginInterface.authRecipe.LoginMethod;
import io.supertokens.pluginInterface.authRecipe.sqlStorage.AuthRecipeSQLStorage;
import io.supertokens.pluginInterface.bulkimport.BulkImportStorage;
import io.supertokens.pluginInterface.bulkimport.BulkImportStorage.BULK_IMPORT_USER_STATUS;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser;
import io.supertokens.pluginInterface.bulkimport.exceptions.BulkImportTransactionRolledBackException;
import io.supertokens.pluginInterface.bulkimport.sqlStorage.BulkImportSQLStorage;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser;
import io.supertokens.pluginInterface.dashboard.DashboardSearchTags;
import io.supertokens.pluginInterface.dashboard.DashboardSessionInfo;
import io.supertokens.pluginInterface.dashboard.DashboardUser;
Expand All @@ -53,7 +52,10 @@
import io.supertokens.pluginInterface.jwt.JWTSigningKeyInfo;
import io.supertokens.pluginInterface.jwt.exceptions.DuplicateKeyIdException;
import io.supertokens.pluginInterface.jwt.sqlstorage.JWTRecipeSQLStorage;
import io.supertokens.pluginInterface.multitenancy.*;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.MultitenancyStorage;
import io.supertokens.pluginInterface.multitenancy.TenantConfig;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.DuplicateClientTypeException;
import io.supertokens.pluginInterface.multitenancy.exceptions.DuplicateTenantException;
import io.supertokens.pluginInterface.multitenancy.exceptions.DuplicateThirdPartyIdException;
Expand Down Expand Up @@ -103,7 +105,10 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLTransactionRollbackException;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,16 @@ static String getQueryToCreateUsersTable(Start start) {

public static String getQueryToCreateUserIdIndexForUsersTable(Start start) {
return "CREATE INDEX IF NOT EXISTS all_auth_recipe_user_id_index ON "
+ Config.getConfig(start).getUsersTable() + "(user_id);";
}
public static String getQueryToCreateUserIdAppIdIndexForUsersTable(Start start) {
return "CREATE INDEX IF NOT EXISTS all_auth_recipe_user_id_app_id_index ON "
+ Config.getConfig(start).getUsersTable() + "(app_id, user_id);";
}
public static String getQueryToCreateAppIdIndexForUsersTable(Start start) {
return "CREATE INDEX IF NOT EXISTS all_auth_recipe_user_app_id_index ON "
+ Config.getConfig(start).getUsersTable() + "(app_id);";
}

public static String getQueryToCreateTenantIdIndexForUsersTable(Start start) {
return "CREATE INDEX IF NOT EXISTS all_auth_recipe_tenant_id_index ON "
Expand Down Expand Up @@ -247,6 +255,11 @@ static String getQueryToCreatePrimaryUserIdIndexForAppIdToUserIdTable(Start star
+ Config.getConfig(start).getAppIdToUserIdTable() + "(primary_or_recipe_user_id, app_id);";
}

static String getQueryToCreateUserIdIndexForAppIdToUserIdTable(Start start) {
return "CREATE INDEX IF NOT EXISTS app_id_to_user_id_user_id_index ON "
+ Config.getConfig(start).getAppIdToUserIdTable() + "(user_id, app_id);";
}

public static void createTablesIfNotExists(Start start, Connection con) throws SQLException, StorageQueryException {
int numberOfRetries = 0;
boolean retry = true;
Expand Down Expand Up @@ -281,6 +294,7 @@ public static void createTablesIfNotExists(Start start, Connection con) throws S
// index
update(con, getQueryToCreateAppIdIndexForAppIdToUserIdTable(start), NO_OP_SETTER);
update(con, getQueryToCreatePrimaryUserIdIndexForAppIdToUserIdTable(start), NO_OP_SETTER);
update(con, getQueryToCreateUserIdIndexForAppIdToUserIdTable(start), NO_OP_SETTER);
}

if (!doesTableExists(start, con, Config.getConfig(start).getUsersTable())) {
Expand Down Expand Up @@ -433,6 +447,8 @@ public static void createTablesIfNotExists(Start start, Connection con) throws S

// index
update(con, getQueryToCreateUserIdIndexForUsersTable(start), NO_OP_SETTER);
update(con, getQueryToCreateUserIdAppIdIndexForUsersTable(start), NO_OP_SETTER);
update(con, getQueryToCreateAppIdIndexForUsersTable(start), NO_OP_SETTER);
update(con, getQueryToCreateTenantIdIndexForUsersTable(start), NO_OP_SETTER);
}

Expand Down Expand Up @@ -1534,18 +1550,37 @@ private static List<AuthRecipeUserInfo> getPrimaryUserInfoForUserIds_Transaction
// which is linked to a primary user ID in which case it won't be in the primary_or_recipe_user_id column,
// or the input may have a primary user ID whose recipe user ID was removed, so it won't be in the user_id
// column
String QUERY =
"SELECT au.user_id, au.primary_or_recipe_user_id, au.is_linked_or_is_a_primary_user, au.recipe_id, " +
"aaru.tenant_id, aaru.time_joined FROM " +
getConfig(start).getAppIdToUserIdTable() + " as au" +
" LEFT JOIN " + getConfig(start).getUsersTable() +
" as aaru ON au.app_id = aaru.app_id AND au.user_id = aaru.user_id" +
" WHERE au.primary_or_recipe_user_id IN (SELECT primary_or_recipe_user_id FROM " +
getConfig(start).getAppIdToUserIdTable() + " WHERE (user_id IN ("
+ Utils.generateCommaSeperatedQuestionMarks(userIds.size()) +
") OR primary_or_recipe_user_id IN (" +
Utils.generateCommaSeperatedQuestionMarks(userIds.size()) +
")) AND app_id = ?) AND au.app_id = ?";
// String QUERY =
// "SELECT au.user_id, au.primary_or_recipe_user_id, au.is_linked_or_is_a_primary_user, au.recipe_id, " +
// "aaru.tenant_id, aaru.time_joined " +
// "FROM " + getConfig(start).getAppIdToUserIdTable() + " as au" +
// " LEFT JOIN " + getConfig(start).getUsersTable() +
// " as aaru ON au.app_id = aaru.app_id AND au.user_id = aaru.user_id" +
// " WHERE au.primary_or_recipe_user_id IN " +
// " (SELECT primary_or_recipe_user_id FROM " +
// getConfig(start).getAppIdToUserIdTable() +
// " WHERE (user_id IN ("
// + Utils.generateCommaSeperatedQuestionMarks(userIds.size()) +") " +
// " OR primary_or_recipe_user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(userIds.size()) +")) " +
// " AND app_id = ?) " +
// "AND au.app_id = ?";

String QUERY = "SELECT" +
" au.user_id," +
" au.primary_or_recipe_user_id," +
" au.is_linked_or_is_a_primary_user," +
" au.recipe_id," +
" aaru.tenant_id," +
" aaru.time_joined" +
" FROM " + getConfig(start).getAppIdToUserIdTable() + " as au" +
" LEFT JOIN " + getConfig(start).getUsersTable() + " as aaru ON au.app_id = aaru.app_id" +
" AND au.user_id = aaru.user_id" +
" LEFT JOIN " + getConfig(start).getAppIdToUserIdTable() + " as aiui ON au.primary_or_recipe_user_id = aiui.user_id" +
" AND aiui.app_id = au.app_id" +
" WHERE" +
" aiui.user_id IN (" + Utils.generateCommaSeperatedQuestionMarks(userIds.size()) + ")" +
" OR au.primary_or_recipe_user_id IN ("+ Utils.generateCommaSeperatedQuestionMarks(userIds.size()) +")" +
" AND au.app_id = ?";

List<AllAuthRecipeUsersResultHolder> allAuthUsersResult = execute(sqlCon, QUERY, pst -> {
// IN user_id
Expand All @@ -1559,7 +1594,7 @@ private static List<AuthRecipeUserInfo> getPrimaryUserInfoForUserIds_Transaction
}
// for app_id
pst.setString(index, appIdentifier.getAppId());
pst.setString(index + 1, appIdentifier.getAppId());
// System.out.println(pst);
}, result -> {
List<AllAuthRecipeUsersResultHolder> parsedResult = new ArrayList<>();
while (result.next()) {
Expand Down

0 comments on commit b83a780

Please sign in to comment.