Skip to content

Commit

Permalink
Merge pull request #1843 from alecmerdler/fix-recreate-relationship-a…
Browse files Browse the repository at this point in the history
…fter-delete-with-filter-postgres

Fix re-creating deleted relationships
  • Loading branch information
alecmerdler authored Mar 29, 2024
2 parents 9986866 + 4438649 commit 1938257
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/datastore/postgres/readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var (
colUsersetNamespace,
colUsersetObjectID,
colUsersetRelation,
colCreatedXid,
).From(tableTuple).Where(sq.Eq{colDeletedXid: liveDeletedTxnID})
)

Expand Down Expand Up @@ -412,7 +413,7 @@ func (rwt *pgReadWriteTXN) deleteRelationshipsWithLimit(ctx context.Context, fil

// Construct a CTE to update the relationships as removed.
cteSQL := fmt.Sprintf(
"WITH found_tuples AS (%s)\nUPDATE %s SET %s = $%d WHERE (%s, %s, %s, %s, %s, %s) IN (select * from found_tuples)",
"WITH found_tuples AS (%s)\nUPDATE %s SET %s = $%d WHERE (%s, %s, %s, %s, %s, %s, %s) IN (select * from found_tuples)",
selectSQL,
tableTuple,
colDeletedXid,
Expand All @@ -423,6 +424,7 @@ func (rwt *pgReadWriteTXN) deleteRelationshipsWithLimit(ctx context.Context, fil
colUsersetNamespace,
colUsersetObjectID,
colUsersetRelation,
colCreatedXid,
)

result, err := rwt.tx.Exec(ctx, cteSQL, args...)
Expand Down
1 change: 1 addition & 0 deletions pkg/datastore/test/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func AllWithExceptions(t *testing.T, tester DatastoreTester, except Categories)
t.Run("TestDeleteRelationships", func(t *testing.T) { DeleteRelationshipsTest(t, tester) })
t.Run("TestDeleteNonExistant", func(t *testing.T) { DeleteNotExistantTest(t, tester) })
t.Run("TestDeleteAlreadyDeleted", func(t *testing.T) { DeleteAlreadyDeletedTest(t, tester) })
t.Run("TestRecreateRelationshipsAfterDeleteWithFilter", func(t *testing.T) { RecreateRelationshipsAfterDeleteWithFilter(t, tester) })
t.Run("TestWriteDeleteWrite", func(t *testing.T) { WriteDeleteWriteTest(t, tester) })
t.Run("TestCreateAlreadyExisting", func(t *testing.T) { CreateAlreadyExistingTest(t, tester) })
t.Run("TestTouchAlreadyExistingWithoutCaveat", func(t *testing.T) { TouchAlreadyExistingTest(t, tester) })
Expand Down
44 changes: 44 additions & 0 deletions pkg/datastore/test/tuples.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,50 @@ func DeleteRelationshipsWithVariousFiltersTest(t *testing.T, tester DatastoreTes
}
}

func RecreateRelationshipsAfterDeleteWithFilter(t *testing.T, tester DatastoreTester) {
require := require.New(t)

rawDS, err := tester.New(0, veryLargeGCInterval, veryLargeGCWindow, 1)
require.NoError(err)

ds, _ := testfixtures.StandardDatastoreWithSchema(rawDS, require)
ctx := context.Background()

relationships := make([]*core.RelationTuple, 100)
for i := 0; i < 100; i++ {
relationships[i] = tuple.MustParse(fmt.Sprintf("document:%d#owner@user:first", i))
}

writeRelationships := func() error {
_, err := common.WriteTuples(ctx, ds, core.RelationTupleUpdate_CREATE, relationships...)
return err
}

deleteRelationships := func() error {
_, err := ds.ReadWriteTx(ctx, func(ctx context.Context, rwt datastore.ReadWriteTransaction) error {
delLimit := uint64(100)
_, err := rwt.DeleteRelationships(ctx, &v1.RelationshipFilter{
OptionalRelation: "owner",
OptionalSubjectFilter: &v1.SubjectFilter{
SubjectType: "user",
OptionalSubjectId: "first",
},
}, options.WithDeleteLimit(&delLimit))
return err
})
return err
}

// Write the initial relationships.
require.NoError(writeRelationships())

// Delete the relationships.
require.NoError(deleteRelationships())

// Write the same relationships again.
require.NoError(writeRelationships())
}

// QueryRelationshipsWithVariousFiltersTest tests various relationship filters for query relationships.
func QueryRelationshipsWithVariousFiltersTest(t *testing.T, tester DatastoreTester) {
tcs := []struct {
Expand Down

0 comments on commit 1938257

Please sign in to comment.