Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Oct 26, 2023
1 parent ab6c9da commit 0bca0bf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ public String getTenantConfigsTable() {
return addSchemaAndPrefixToTableName("tenant_configs");
}

public String getFirstFactorsTable() {
return addSchemaAndPrefixToTableName("first_factors");
public String getTenantFirstFactorsTable() {
return addSchemaAndPrefixToTableName("tenant_first_factors");
}

public String getDefaultRequiredFactorIdsTable() {
return addSchemaAndPrefixToTableName("default_required_factor_ids");
public String getTenantDefaultRequiredFactorIdsTable() {
return addSchemaAndPrefixToTableName("tenant_default_required_factor_ids");
}

public String getTenantThirdPartyProvidersTable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.supertokens.storage.postgresql.ConnectionPool;
import io.supertokens.storage.postgresql.Start;
import io.supertokens.storage.postgresql.config.Config;
import io.supertokens.storage.postgresql.queries.GeneralQueries.AccountLinkingInfo;
import io.supertokens.storage.postgresql.utils.Utils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -322,7 +321,7 @@ public static void createTablesIfNotExists(Start start) throws SQLException, Sto
NO_OP_SETTER);
}

if (!doesTableExists(start, Config.getConfig(start).getFirstFactorsTable())) {
if (!doesTableExists(start, Config.getConfig(start).getTenantFirstFactorsTable())) {
getInstance(start).addState(CREATING_NEW_TABLE, null);
update(start, MultitenancyQueries.getQueryToCreateFirstFactorsTable(start), NO_OP_SETTER);

Expand All @@ -331,14 +330,17 @@ public static void createTablesIfNotExists(Start start) throws SQLException, Sto
NO_OP_SETTER);
}

if (!doesTableExists(start, Config.getConfig(start).getDefaultRequiredFactorIdsTable())) {
if (!doesTableExists(start, Config.getConfig(start).getTenantDefaultRequiredFactorIdsTable())) {
getInstance(start).addState(CREATING_NEW_TABLE, null);
update(start, MultitenancyQueries.getQueryToCreateDefaultRequiredFactorIdsTable(start), NO_OP_SETTER);

// index
update(start,
MultitenancyQueries.getQueryToCreateTenantIdIndexForDefaultRequiredFactorIdsTable(start),
NO_OP_SETTER);
update(start,
MultitenancyQueries.getQueryToCreateOrderIndexForDefaultRequiredFactorIdsTable(start),
NO_OP_SETTER);
}

if (!doesTableExists(start, Config.getConfig(start).getTenantThirdPartyProviderClientsTable())) {
Expand Down Expand Up @@ -582,8 +584,8 @@ public static void deleteAllTables(Start start) throws SQLException, StorageQuer
+ getConfig(start).getUserIdMappingTable() + ","
+ getConfig(start).getUsersTable() + ","
+ getConfig(start).getAccessTokenSigningKeysTable() + ","
+ getConfig(start).getFirstFactorsTable() + ","
+ getConfig(start).getDefaultRequiredFactorIdsTable() + ","
+ getConfig(start).getTenantFirstFactorsTable() + ","
+ getConfig(start).getTenantDefaultRequiredFactorIdsTable() + ","
+ getConfig(start).getTenantConfigsTable() + ","
+ getConfig(start).getTenantThirdPartyProvidersTable() + ","
+ getConfig(start).getTenantThirdPartyProviderClientsTable() + ","
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static String getQueryToCreateThirdPartyIdIndexForTenantThirdPartyProvide

public static String getQueryToCreateFirstFactorsTable(Start start) {
String schema = Config.getConfig(start).getTableSchema();
String tableName = Config.getConfig(start).getFirstFactorsTable();
String tableName = Config.getConfig(start).getTenantFirstFactorsTable();
// @formatter:off
return "CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ "connection_uri_domain VARCHAR(256) DEFAULT '',"
Expand All @@ -142,18 +142,19 @@ public static String getQueryToCreateFirstFactorsTable(Start start) {

public static String getQueryToCreateTenantIdIndexForFirstFactorsTable(Start start) {
return "CREATE INDEX IF NOT EXISTS tenant_first_factors_tenant_id_index ON "
+ getConfig(start).getFirstFactorsTable() + " (connection_uri_domain, app_id, tenant_id);";
+ getConfig(start).getTenantFirstFactorsTable() + " (connection_uri_domain, app_id, tenant_id);";
}

public static String getQueryToCreateDefaultRequiredFactorIdsTable(Start start) {
String schema = Config.getConfig(start).getTableSchema();
String tableName = Config.getConfig(start).getDefaultRequiredFactorIdsTable();
String tableName = Config.getConfig(start).getTenantDefaultRequiredFactorIdsTable();
// @formatter:off
return "CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ "connection_uri_domain VARCHAR(256) DEFAULT '',"
+ "app_id VARCHAR(64) DEFAULT 'public',"
+ "tenant_id VARCHAR(64) DEFAULT 'public',"
+ "factor_id VARCHAR(64),"
+ "order_idx INTEGER NOT NULL,"
+ "CONSTRAINT " + Utils.getConstraintName(schema, tableName, null, "pkey")
+ " PRIMARY KEY (connection_uri_domain, app_id, tenant_id, factor_id),"
+ "CONSTRAINT " + Utils.getConstraintName(schema, tableName, "tenant_id", "fkey")
Expand All @@ -165,7 +166,12 @@ public static String getQueryToCreateDefaultRequiredFactorIdsTable(Start start)

public static String getQueryToCreateTenantIdIndexForDefaultRequiredFactorIdsTable(Start start) {
return "CREATE INDEX IF NOT EXISTS tenant_default_required_factor_ids_tenant_id_index ON "
+ getConfig(start).getDefaultRequiredFactorIdsTable() + " (connection_uri_domain, app_id, tenant_id);";
+ getConfig(start).getTenantDefaultRequiredFactorIdsTable() + " (connection_uri_domain, app_id, tenant_id);";
}

public static String getQueryToCreateOrderIndexForDefaultRequiredFactorIdsTable(Start start) {
return "CREATE INDEX IF NOT EXISTS tenant_default_required_factor_ids_tenant_id_index ON "
+ getConfig(start).getTenantDefaultRequiredFactorIdsTable() + " (order_idx ASC);";
}

private static void executeCreateTenantQueries(Start start, Connection sqlCon, TenantConfig tenantConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package io.supertokens.storage.postgresql.queries.multitenancy;

import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.TenantConfig;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.multitenancy.ThirdPartyConfig;
import io.supertokens.storage.postgresql.Start;

import java.sql.Connection;
Expand All @@ -37,7 +35,7 @@ public class MfaSqlHelper {
public static HashMap<TenantIdentifier, String[]> selectAllFirstFactors(Start start)
throws SQLException, StorageQueryException {
String QUERY = "SELECT connection_uri_domain, app_id, tenant_id, factor_id FROM "
+ getConfig(start).getFirstFactorsTable() + ";";
+ getConfig(start).getTenantFirstFactorsTable() + ";";
return execute(start, QUERY, pst -> {}, result -> {
HashMap<TenantIdentifier, List<String>> firstFactors = new HashMap<>();

Expand All @@ -61,8 +59,8 @@ public static HashMap<TenantIdentifier, String[]> selectAllFirstFactors(Start st

public static HashMap<TenantIdentifier, String[]> selectAllDefaultRequiredFactorIds(Start start)
throws SQLException, StorageQueryException {
String QUERY = "SELECT connection_uri_domain, app_id, tenant_id, factor_id FROM "
+ getConfig(start).getDefaultRequiredFactorIdsTable() + ";";
String QUERY = "SELECT connection_uri_domain, app_id, tenant_id, factor_id, order_idx FROM "
+ getConfig(start).getTenantDefaultRequiredFactorIdsTable() + " ORDER BY order_idx ASC;";
return execute(start, QUERY, pst -> {}, result -> {
HashMap<TenantIdentifier, List<String>> defaultRequiredFactors = new HashMap<>();

Expand Down Expand Up @@ -91,7 +89,7 @@ public static void createFirstFactors(Start start, Connection sqlCon, TenantIden
return;
}

String QUERY = "INSERT INTO " + getConfig(start).getFirstFactorsTable() + "(connection_uri_domain, app_id, tenant_id, factor_id) VALUES (?, ?, ?, ?);";
String QUERY = "INSERT INTO " + getConfig(start).getTenantFirstFactorsTable() + "(connection_uri_domain, app_id, tenant_id, factor_id) VALUES (?, ?, ?, ?);";
for (String factorId : Set.of(firstFactors)) {
update(sqlCon, QUERY, pst -> {
pst.setString(1, tenantIdentifier.getConnectionUriDomain());
Expand All @@ -108,14 +106,18 @@ public static void createDefaultRequiredFactorIds(Start start, Connection sqlCon
return;
}

String QUERY = "INSERT INTO " + getConfig(start).getDefaultRequiredFactorIdsTable() + "(connection_uri_domain, app_id, tenant_id, factor_id) VALUES (?, ?, ?, ?);";
for (String factorId : Set.of(defaultRequiredFactorIds)) {
String QUERY = "INSERT INTO " + getConfig(start).getTenantDefaultRequiredFactorIdsTable() + "(connection_uri_domain, app_id, tenant_id, factor_id, order_idx) VALUES (?, ?, ?, ?, ?);";
int orderIdx = 0;
for (String factorId : defaultRequiredFactorIds) {
int finalOrderIdx = orderIdx;
update(sqlCon, QUERY, pst -> {
pst.setString(1, tenantIdentifier.getConnectionUriDomain());
pst.setString(2, tenantIdentifier.getAppId());
pst.setString(3, tenantIdentifier.getTenantId());
pst.setString(4, factorId);
pst.setInt(5, finalOrderIdx);
});
orderIdx++;
}
}
}

0 comments on commit 0bca0bf

Please sign in to comment.