@@ -288,6 +288,57 @@ static String getQueryToCreateUserIdIndexForAppIdToUserIdTable(Start start) {
288288 + Config .getConfig (start ).getAppIdToUserIdTable () + "(user_id, app_id);" ;
289289 }
290290
291+ static String getQueryToCreateRecipeUserTenantsTable (Start start ) {
292+ String schema = Config .getConfig (start ).getTableSchema ();
293+ String tableName = Config .getConfig (start ).getRecipeUserTenantsTable ();
294+ // @formatter:off
295+ return "CREATE TABLE IF NOT EXISTS " + tableName + " ("
296+ + "app_id VARCHAR(64) NOT NULL,"
297+ + "recipe_user_id CHAR(36) NOT NULL,"
298+ + "tenant_id VARCHAR(64) NOT NULL,"
299+ + "recipe_id VARCHAR(128) NOT NULL,"
300+ + "account_info_type VARCHAR(8) NOT NULL,"
301+ + "account_info_value TEXT NOT NULL,"
302+ + "CONSTRAINT " + Utils .getConstraintName (schema , tableName , null , "pkey" )
303+ + " PRIMARY KEY (app_id, recipe_user_id, tenant_id),"
304+ + "CONSTRAINT " + Utils .getConstraintName (schema , tableName , "tenant_id" , "fkey" )
305+ + " FOREIGN KEY(app_id, tenant_id)"
306+ + " REFERENCES " + Config .getConfig (start ).getTenantsTable () + " (app_id, tenant_id) ON DELETE CASCADE"
307+ + ");" ;
308+ // @formatter:on
309+ }
310+
311+ static String getQueryToCreatePrimaryUserTenantsTable (Start start ) {
312+ String schema = Config .getConfig (start ).getTableSchema ();
313+ String tableName = Config .getConfig (start ).getPrimaryUserTenantsTable ();
314+ // @formatter:off
315+ return "CREATE TABLE IF NOT EXISTS " + tableName + " ("
316+ + "app_id VARCHAR(64) NOT NULL,"
317+ + "tenant_id VARCHAR(64) NOT NULL,"
318+ + "account_info_type VARCHAR(8) NOT NULL,"
319+ + "account_info_value TEXT NOT NULL,"
320+ + "primary_user_id CHAR(36) NOT NULL,"
321+ + "CONSTRAINT " + Utils .getConstraintName (schema , tableName , null , "pkey" )
322+ + " PRIMARY KEY (app_id, tenant_id, account_info_type, account_info_value)"
323+ + ");" ;
324+ // @formatter:on
325+ }
326+
327+ static String getQueryToCreateTenantIndexForRecipeUserTenantsTable (Start start ) {
328+ return "CREATE INDEX IF NOT EXISTS idx_recipe_user_tenants_tenant ON "
329+ + Config .getConfig (start ).getRecipeUserTenantsTable () + "(app_id, tenant_id);" ;
330+ }
331+
332+ static String getQueryToCreateAccountInfoIndexForRecipeUserTenantsTable (Start start ) {
333+ return "CREATE INDEX IF NOT EXISTS idx_recipe_user_tenants_account_info ON "
334+ + Config .getConfig (start ).getRecipeUserTenantsTable () + "(app_id, tenant_id, account_info_type, account_info_value);" ;
335+ }
336+
337+ static String getQueryToCreatePrimaryUserIndexForPrimaryUserTenantsTable (Start start ) {
338+ return "CREATE INDEX IF NOT EXISTS idx_primary_user_tenants_primary ON "
339+ + Config .getConfig (start ).getPrimaryUserTenantsTable () + "(app_id, primary_user_id);" ;
340+ }
341+
291342 public static void createTablesIfNotExists (Start start , Connection con ) throws SQLException , StorageQueryException {
292343 int numberOfRetries = 0 ;
293344 boolean retry = true ;
@@ -724,6 +775,23 @@ public static void createTablesIfNotExists(Start start, Connection con) throws S
724775 update (con , SAMLQueries .getQueryToCreateSAMLClaimsExpiresAtIndex (start ), NO_OP_SETTER );
725776 }
726777
778+ if (!doesTableExists (start , con , Config .getConfig (start ).getRecipeUserTenantsTable ())) {
779+ getInstance (start ).addState (CREATING_NEW_TABLE , null );
780+ update (con , getQueryToCreateRecipeUserTenantsTable (start ), NO_OP_SETTER );
781+
782+ // indexes
783+ update (con , getQueryToCreateTenantIndexForRecipeUserTenantsTable (start ), NO_OP_SETTER );
784+ update (con , getQueryToCreateAccountInfoIndexForRecipeUserTenantsTable (start ), NO_OP_SETTER );
785+ }
786+
787+ if (!doesTableExists (start , con , Config .getConfig (start ).getPrimaryUserTenantsTable ())) {
788+ getInstance (start ).addState (CREATING_NEW_TABLE , null );
789+ update (con , getQueryToCreatePrimaryUserTenantsTable (start ), NO_OP_SETTER );
790+
791+ // indexes
792+ update (con , getQueryToCreatePrimaryUserIndexForPrimaryUserTenantsTable (start ), NO_OP_SETTER );
793+ }
794+
727795 } catch (Exception e ) {
728796 if (e .getMessage ().contains ("schema" ) && e .getMessage ().contains ("does not exist" )
729797 && numberOfRetries < 1 ) {
0 commit comments