Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subject filters in schema relation delete to force use of the index #2131

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions internal/services/shared/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,45 @@ func sanityCheckNamespaceChanges(
for _, delta := range diff.Deltas() {
switch delta.Type {
case nsdiff.RemovedRelation:
// NOTE: We add the subject filters here to ensure the reverse relationship index is used
// by the datastores. As there is no index that has {namespace, relation} directly, but there
// *is* an index that has {subject_namespace, subject_relation, namespace, relation}, we can
// force the datastore to use the reverse index by adding the subject filters.
var previousRelation *core.Relation
for _, relation := range existing.Relation {
if relation.Name == delta.RelationName {
previousRelation = relation
break
}
}

if previousRelation == nil {
return nil, spiceerrors.MustBugf("relation `%s` not found in existing namespace definition", delta.RelationName)
}

subjectSelectors := make([]datastore.SubjectsSelector, 0, len(previousRelation.TypeInformation.AllowedDirectRelations))
for _, allowedType := range previousRelation.TypeInformation.AllowedDirectRelations {
if allowedType.GetRelation() == datastore.Ellipsis {
subjectSelectors = append(subjectSelectors, datastore.SubjectsSelector{
OptionalSubjectType: allowedType.Namespace,
RelationFilter: datastore.SubjectRelationFilter{
IncludeEllipsisRelation: true,
},
})
} else {
subjectSelectors = append(subjectSelectors, datastore.SubjectsSelector{
OptionalSubjectType: allowedType.Namespace,
RelationFilter: datastore.SubjectRelationFilter{
NonEllipsisRelation: allowedType.GetRelation(),
},
})
}
}

qy, qyErr := rwt.QueryRelationships(ctx, datastore.RelationshipsFilter{
OptionalResourceType: nsdef.Name,
OptionalResourceRelation: delta.RelationName,
OptionalResourceType: nsdef.Name,
OptionalResourceRelation: delta.RelationName,
OptionalSubjectsSelectors: subjectSelectors,
}, options.WithLimit(options.LimitOne))

err = errorIfTupleIteratorReturnsTuples(
Expand Down
Loading
Loading