diff --git a/components/director/internal/domain/formation/repository.go b/components/director/internal/domain/formation/repository.go index 1f7464d0a1..360d842f5e 100644 --- a/components/director/internal/domain/formation/repository.go +++ b/components/director/internal/domain/formation/repository.go @@ -22,8 +22,8 @@ import ( const ( tableName string = `public.formations` nameColumn string = `name` - listObjectIDsOfTypeForFormation string = "SELECT DISTINCT fa.source FROM formations f JOIN formation_assignments fa ON f.id = fa.formation_id WHERE f.%s AND f.tenant_id = ? AND fa.source_type = ?;" - listObjectIDsOfTypeForFormationGlobal string = "SELECT DISTINCT fa.source FROM formations f JOIN formation_assignments fa ON f.id = fa.formation_id WHERE f.%s AND fa.source_type = ?;" + listObjectIDsOfTypeForFormation string = "SELECT DISTINCT fa.source FROM formations f JOIN formation_assignments fa ON f.id = fa.formation_id WHERE f.%s AND f.tenant_id = ? AND fa.source_type = ? UNION SELECT DISTINCT fa.target FROM formations f JOIN formation_assignments fa ON f.id = fa.formation_id WHERE f.%s AND f.tenant_id = ? AND fa.target_type = ?;" + listObjectIDsOfTypeForFormationGlobal string = "SELECT DISTINCT fa.source FROM formations f JOIN formation_assignments fa ON f.id = fa.formation_id WHERE f.%s AND fa.source_type = ? UNION SELECT DISTINCT fa.target FROM formations f JOIN formation_assignments fa ON f.id = fa.formation_id WHERE f.%s AND fa.target_type = ?;" ) var ( @@ -204,10 +204,12 @@ func (r *repository) ListObjectIDsOfTypeForFormations(ctx context.Context, tenan } inCond := repo.NewInConditionForStringValues(nameColumn, formationNames) - args, _ := inCond.GetQueryArgs() + inCondArgs, _ := inCond.GetQueryArgs() + args := append(inCondArgs, tenantID, objectType) + args = append(args, inCondArgs...) args = append(args, tenantID, objectType) - listObjectIDsOfTypeForFormationStatement := fmt.Sprintf(listObjectIDsOfTypeForFormation, inCond.GetQueryPart()) + listObjectIDsOfTypeForFormationStatement := fmt.Sprintf(listObjectIDsOfTypeForFormation, inCond.GetQueryPart(), inCond.GetQueryPart()) listObjectIDsOfTypeForFormationStatement = sqlx.Rebind(sqlx.DOLLAR, listObjectIDsOfTypeForFormationStatement) log.C(ctx).Debugf("Executing DB query: %s", listObjectIDsOfTypeForFormationStatement) @@ -233,10 +235,12 @@ func (r *repository) ListObjectIDsOfTypeForFormationsGlobal(ctx context.Context, var objectIDs []string inCond := repo.NewInConditionForStringValues(nameColumn, formationNames) - args, _ := inCond.GetQueryArgs() + inCondArgs, _ := inCond.GetQueryArgs() + args := append(inCondArgs, objectType) + args = append(args, inCondArgs...) args = append(args, objectType) - listObjectIDsOfTypeForFormationStatement := fmt.Sprintf(listObjectIDsOfTypeForFormationGlobal, inCond.GetQueryPart()) + listObjectIDsOfTypeForFormationStatement := fmt.Sprintf(listObjectIDsOfTypeForFormationGlobal, inCond.GetQueryPart(), inCond.GetQueryPart()) listObjectIDsOfTypeForFormationStatement = sqlx.Rebind(sqlx.DOLLAR, listObjectIDsOfTypeForFormationStatement) log.C(ctx).Debugf("Executing DB query: %s", listObjectIDsOfTypeForFormationStatement) diff --git a/components/director/internal/domain/formation/repository_test.go b/components/director/internal/domain/formation/repository_test.go index f695df9c03..b7d22ec71f 100644 --- a/components/director/internal/domain/formation/repository_test.go +++ b/components/director/internal/domain/formation/repository_test.go @@ -288,13 +288,13 @@ func TestRepository_ListByIDs(t *testing.T) { func TestRepository_ListObjectIDsOfTypeForFormations(t *testing.T) { tnt := "tnt" formationName := "formation_name" - dbQuery := "SELECT DISTINCT fa.source FROM formations f JOIN formation_assignments fa ON f.id = fa.formation_id WHERE f.name IN ($1) AND f.tenant_id = $2 AND fa.source_type = $3;" + dbQuery := "SELECT DISTINCT fa.source FROM formations f JOIN formation_assignments fa ON f.id = fa.formation_id WHERE f.name IN ($1) AND f.tenant_id = $2 AND fa.source_type = $3 UNION SELECT DISTINCT fa.target FROM formations f JOIN formation_assignments fa ON f.id = fa.formation_id WHERE f.name IN ($4) AND f.tenant_id = $5 AND fa.target_type = $6" t.Run("Success", func(t *testing.T) { db, dbMock := testdb.MockDatabase(t) rowsToReturn := sqlmock.NewRows([]string{"source"}).AddRow(ApplicationID) dbMock.ExpectQuery(regexp.QuoteMeta(dbQuery)). - WithArgs(formationName, tnt, model.FormationAssignmentTypeApplication). + WithArgs(formationName, tnt, model.FormationAssignmentTypeApplication, formationName, tnt, model.FormationAssignmentTypeApplication). WillReturnRows(rowsToReturn) ctx := persistence.SaveToContext(context.TODO(), db) @@ -313,7 +313,7 @@ func TestRepository_ListObjectIDsOfTypeForFormations(t *testing.T) { db, dbMock := testdb.MockDatabase(t) dbMock.ExpectQuery(regexp.QuoteMeta(dbQuery)). - WithArgs(formationName, tnt, model.FormationAssignmentTypeApplication). + WithArgs(formationName, tnt, model.FormationAssignmentTypeApplication, formationName, tnt, model.FormationAssignmentTypeApplication). WillReturnError(testErr) ctx := persistence.SaveToContext(context.TODO(), db) @@ -341,13 +341,13 @@ func TestRepository_ListObjectIDsOfTypeForFormations(t *testing.T) { func TestRepository_ListObjectIDsOfTypeForFormationsGlobal(t *testing.T) { formationName := "formation_name" - dbQuery := "SELECT DISTINCT fa.source FROM formations f JOIN formation_assignments fa ON f.id = fa.formation_id WHERE f.name IN ($1) AND fa.source_type = $2;" + dbQuery := "SELECT DISTINCT fa.source FROM formations f JOIN formation_assignments fa ON f.id = fa.formation_id WHERE f.name IN ($1) AND fa.source_type = $2 UNION SELECT DISTINCT fa.target FROM formations f JOIN formation_assignments fa ON f.id = fa.formation_id WHERE f.name IN ($3) AND fa.target_type = $4;" t.Run("Success", func(t *testing.T) { db, dbMock := testdb.MockDatabase(t) rowsToReturn := sqlmock.NewRows([]string{"source"}).AddRow(ApplicationID) dbMock.ExpectQuery(regexp.QuoteMeta(dbQuery)). - WithArgs(formationName, model.FormationAssignmentTypeApplication). + WithArgs(formationName, model.FormationAssignmentTypeApplication, formationName, model.FormationAssignmentTypeApplication). WillReturnRows(rowsToReturn) ctx := persistence.SaveToContext(context.TODO(), db) @@ -366,7 +366,7 @@ func TestRepository_ListObjectIDsOfTypeForFormationsGlobal(t *testing.T) { db, dbMock := testdb.MockDatabase(t) dbMock.ExpectQuery(regexp.QuoteMeta(dbQuery)). - WithArgs(formationName, model.FormationAssignmentTypeApplication). + WithArgs(formationName, model.FormationAssignmentTypeApplication, formationName, model.FormationAssignmentTypeApplication). WillReturnError(testErr) ctx := persistence.SaveToContext(context.TODO(), db)