Skip to content

Commit 6db3d4d

Browse files
Merge pull request #5482 from hashicorp/backport/ddebko-optimize-alias-query/widely-primary-ox
This pull request was automerged via backport-assistant
2 parents 0271468 + 137edf9 commit 6db3d4d

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

internal/alias/target/repository_alias_list_resolvable.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ func (r *Repository) listResolvableAliases(ctx context.Context, permissions []pe
9191

9292
var args []any
9393
var destinationIdClauses []string
94+
var whereClause string
9495

9596
switch {
9697
case allDescendants:
97-
// This matches all targets
98-
destinationIdClauses = append(destinationIdClauses, "destination_id in (select public_id from target)")
98+
whereClause = "destination_id is not null"
9999
default:
100100
// Add orgs with all permissions on children
101101
if len(childAllScopes) > 0 {
@@ -118,10 +118,9 @@ func (r *Repository) listResolvableAliases(ctx context.Context, permissions []pe
118118
if len(destinationIdClauses) == 0 && len(childAllScopes) == 0 {
119119
return nil, time.Time{}, errors.New(ctx, errors.InvalidParameter, op, "no target ids or scope ids provided")
120120
}
121+
whereClause = fmt.Sprintf("destination_id is not null and (%s)", strings.Join(destinationIdClauses, " or "))
121122
}
122123

123-
whereClause := fmt.Sprintf("destination_id is not null and (%s)", strings.Join(destinationIdClauses, " or "))
124-
125124
if opts.withStartPageAfterItem != nil {
126125
whereClause = fmt.Sprintf("(create_time, public_id) < (@last_item_create_time, @last_item_id) and %s", whereClause)
127126
args = append(args,
@@ -166,11 +165,11 @@ func (r *Repository) listResolvableAliasesRefresh(ctx context.Context, updatedAf
166165

167166
var args []any
168167
var destinationIdClauses []string
168+
var whereClause string
169169

170170
switch {
171171
case allDescendants:
172-
// This matches all targets
173-
destinationIdClauses = append(destinationIdClauses, "destination_id in (select public_id from target)")
172+
whereClause = fmt.Sprintf("update_time > @updated_after_time and destination_id is not null")
174173
default:
175174
// Add orgs with all permissions on children
176175
if len(childAllScopes) > 0 {
@@ -193,10 +192,9 @@ func (r *Repository) listResolvableAliasesRefresh(ctx context.Context, updatedAf
193192
if len(destinationIdClauses) == 0 && len(childAllScopes) == 0 {
194193
return nil, time.Time{}, errors.New(ctx, errors.InvalidParameter, op, "no target ids or scope ids provided")
195194
}
195+
whereClause = fmt.Sprintf("update_time > @updated_after_time and destination_id is not null and (%s)",
196+
strings.Join(destinationIdClauses, " or "))
196197
}
197-
198-
whereClause := fmt.Sprintf("update_time > @updated_after_time and destination_id is not null and (%s)",
199-
strings.Join(destinationIdClauses, " or "))
200198
args = append(args,
201199
sql.Named("updated_after_time", timestamp.New(updatedAfter)),
202200
)
@@ -234,11 +232,10 @@ func (r *Repository) listRemovedResolvableAliasIds(ctx context.Context, since ti
234232

235233
var args []any
236234
var destinationIdClauses []string
237-
235+
var whereClause string
238236
switch {
239237
case allDescendants:
240-
// This matches all targets
241-
destinationIdClauses = append(destinationIdClauses, "destination_id not in (select public_id from target)")
238+
whereClause = "update_time > @updated_after_time and destination_id is null"
242239
default:
243240
// Add orgs with all permissions on children
244241
if len(childAllScopes) > 0 {
@@ -261,10 +258,9 @@ func (r *Repository) listRemovedResolvableAliasIds(ctx context.Context, since ti
261258
if len(destinationIdClauses) == 0 && len(childAllScopes) == 0 {
262259
return nil, time.Time{}, errors.New(ctx, errors.InvalidParameter, op, "no target ids or scope ids provided")
263260
}
261+
whereClause = fmt.Sprintf("update_time > @updated_after_time and (destination_id is null or (%s))",
262+
strings.Join(destinationIdClauses, " and "))
264263
}
265-
266-
whereClause := fmt.Sprintf("update_time > @updated_after_time and (destination_id is null or (%s))",
267-
strings.Join(destinationIdClauses, " and "))
268264
args = append(args,
269265
sql.Named("updated_after_time", timestamp.New(since)),
270266
)

0 commit comments

Comments
 (0)