Skip to content

Commit

Permalink
fix: PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anku255 committed Feb 15, 2024
1 parent 0e62c9f commit cbdf6a4
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 15 deletions.
22 changes: 20 additions & 2 deletions src/main/java/io/supertokens/storage/postgresql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -2994,9 +2994,27 @@ public UserIdMapping[] getUserIdMapping_Transaction(TransactionConnection con, A
}

@Override
public void addBulkImportUsers(AppIdentifier appIdentifier, ArrayList<BulkImportUser> users) throws StorageQueryException {
public void addBulkImportUsers(AppIdentifier appIdentifier, ArrayList<BulkImportUser> users)
throws StorageQueryException,
io.supertokens.pluginInterface.bulkimport.exceptions.DuplicateUserIdException {
try {
BulkImportQueries.insertBulkImportUsers(this, users);
} catch (SQLException e) {
if (e instanceof PSQLException) {
ServerErrorMessage serverErrorMessage = ((PSQLException) e).getServerErrorMessage();
if (isPrimaryKeyError(serverErrorMessage, Config.getConfig(this).getBulkImportUsersTable())) {
throw new io.supertokens.pluginInterface.bulkimport.exceptions.DuplicateUserIdException();
}
}
throw new StorageQueryException(e);
}
}

@Override
public JsonObject[] getBulkImportUsers(AppIdentifier appIdentifier, @Nonnull Integer limit, @Nullable String status,
@Nullable String bulkImportUserId) throws StorageQueryException {
try {
BulkImportQueries.insertBulkImportUsers(this, users);
return BulkImportQueries.getBulkImportUsers(this, limit, status, bulkImportUserId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
package io.supertokens.storage.postgresql.queries;

import static io.supertokens.storage.postgresql.QueryExecutorTemplate.update;
import static io.supertokens.storage.postgresql.PreparedStatementValueSetter.NO_OP_SETTER;
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.execute;

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

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.google.gson.JsonObject;

import io.supertokens.pluginInterface.bulkimport.BulkImportUser;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -48,18 +54,68 @@ public static void insertBulkImportUsers(Start start, ArrayList<BulkImportUser>
throws SQLException, StorageQueryException {
StringBuilder queryBuilder = new StringBuilder(
"INSERT INTO " + Config.getConfig(start).getBulkImportUsersTable() + " (id, raw_data) VALUES ");
for (BulkImportUser user : users) {
queryBuilder.append("('")
.append(user.id)
.append("', '")
.append(user.toString())
.append("')");

if (user != users.get(users.size() - 1)) {

int userCount = users.size();

for (int i = 0; i < userCount; i++) {
queryBuilder.append(" (?, ?)");

if (i < userCount - 1) {
queryBuilder.append(",");
}
}
queryBuilder.append(";");
update(start, queryBuilder.toString(), NO_OP_SETTER);

update(start, queryBuilder.toString(), pst -> {
int parameterIndex = 1;
for (BulkImportUser user : users) {
pst.setString(parameterIndex++, user.id);
pst.setString(parameterIndex++, user.toString());
}
});
}

public static JsonObject[] getBulkImportUsers(Start start, @Nonnull Integer limit, @Nullable String status,
@Nullable String bulkImportUserId)
throws SQLException, StorageQueryException {

ArrayList<JsonObject> bulkImportUsers = new ArrayList<>();
String baseQuery = "SELECT * FROM " + Config.getConfig(start).getBulkImportUsersTable();

StringBuilder queryBuilder = new StringBuilder(baseQuery);
List<Object> parameters = new ArrayList<>();

if (status != null) {
queryBuilder.append(" WHERE status = ?");
parameters.add(status);
}

if (bulkImportUserId != null) {
queryBuilder.append(status != null ? " AND" : " WHERE")
.append(" id >= ?");
parameters.add(bulkImportUserId);
}

queryBuilder.append(" LIMIT ?");
parameters.add(limit);

String query = queryBuilder.toString();

return execute(start, query, pst -> {
for (int i = 0; i < parameters.size(); i++) {
pst.setObject(i + 1, parameters.get(i));
}
}, result -> {
while (result.next()) {
JsonObject user = new JsonObject();
user.addProperty("id", result.getString("id"));
user.addProperty("raw_data", result.getString("raw_data"));
user.addProperty("status", result.getString("status"));
user.addProperty("error_msg", result.getString("error_msg"));
user.addProperty("created_at", result.getLong("created_at"));
user.addProperty("updated_at", result.getLong("updated_at"));
bulkImportUsers.add(user);
}
return bulkImportUsers.toArray(new JsonObject[0]);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,10 @@ public static void deleteAllTables(Start start) throws SQLException, StorageQuer
String DROP_QUERY = "DROP INDEX IF EXISTS all_auth_recipe_users_pagination_index";
update(start, DROP_QUERY, NO_OP_SETTER);
}

{
String DROP_QUERY = "DROP INDEX IF EXISTS bulk_import_users_status_updated_at_index";
update(start, DROP_QUERY, NO_OP_SETTER);
}
{
String DROP_QUERY = "DROP TABLE IF EXISTS "
+ getConfig(start).getAppsTable() + ","
Expand Down Expand Up @@ -598,7 +601,8 @@ public static void deleteAllTables(Start start) throws SQLException, StorageQuer
+ getConfig(start).getDashboardUsersTable() + ","
+ getConfig(start).getDashboardSessionsTable() + ","
+ getConfig(start).getTotpUsedCodesTable() + "," + getConfig(start).getTotpUserDevicesTable() + ","
+ getConfig(start).getTotpUsersTable();
+ getConfig(start).getTotpUsersTable() + ","
+ getConfig(start).getBulkImportUsersTable();
update(start, DROP_QUERY, NO_OP_SETTER);
}
}
Expand Down

0 comments on commit cbdf6a4

Please sign in to comment.