Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
adapt sql queries to respect participants that are either only source…
Browse files Browse the repository at this point in the history
… or only target (#3922)
  • Loading branch information
StanislavStefanov authored Jun 26, 2024
1 parent eb2c22d commit c548cd5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
16 changes: 10 additions & 6 deletions components/director/internal/domain/formation/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions components/director/internal/domain/formation/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit c548cd5

Please sign in to comment.