Skip to content

Commit

Permalink
Merge pull request #1478 from trrrrrys/fix/touch
Browse files Browse the repository at this point in the history
Fix MySQL WriteRelationships query
  • Loading branch information
josephschorr authored Aug 7, 2023
2 parents 8d02a63 + 476337b commit 082cc1b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/datastore/mysql/readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ func (rwt *mysqlReadWriteTXN) WriteRelationships(ctx context.Context, mutations
}

if len(clauses) > 0 {
query, args, err := selectForUpdateQuery.Where(clauses).ToSql()
query, args, err := selectForUpdateQuery.
Where(clauses).
Where(sq.GtOrEq{colDeletedTxn: rwt.newTxnID}).
ToSql()
if err != nil {
return fmt.Errorf(errUnableToWriteRelationships, err)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/datastore/test/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func AllWithExceptions(t *testing.T, tester DatastoreTester, except Categories)
t.Run("TestWriteDeleteWrite", func(t *testing.T) { WriteDeleteWriteTest(t, tester) })
t.Run("TestCreateAlreadyExisting", func(t *testing.T) { CreateAlreadyExistingTest(t, tester) })
t.Run("TestTouchAlreadyExisting", func(t *testing.T) { TouchAlreadyExistingTest(t, tester) })
t.Run("TestCreateDeleteTouchTest", func(t *testing.T) { CreateDeleteTouchTest(t, tester) })
t.Run("TestCreateTouchDeleteTouchTest", func(t *testing.T) { CreateTouchDeleteTouchTest(t, tester) })
t.Run("TestTouchAlreadyExistingCaveated", func(t *testing.T) { TouchAlreadyExistingCaveatedTest(t, tester) })

t.Run("TestMultipleReadsInRWT", func(t *testing.T) { MultipleReadsInRWTTest(t, tester) })
Expand Down
63 changes: 63 additions & 0 deletions pkg/datastore/test/tuples.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,69 @@ func TouchAlreadyExistingTest(t *testing.T, tester DatastoreTester) {
ensureTuples(ctx, require, ds, tpl1, tpl2, tpl3)
}

// CreateDeleteTouchTest tests writing a relationship, deleting it, and then touching it.
func CreateDeleteTouchTest(t *testing.T, tester DatastoreTester) {
require := require.New(t)

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

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

tpl1 := makeTestTuple("foo", "tom")
tpl2 := makeTestTuple("foo", "sarah")

_, err = common.WriteTuples(ctx, ds, core.RelationTupleUpdate_CREATE, tpl1, tpl2)
require.NoError(err)

ensureTuples(ctx, require, ds, tpl1, tpl2)

_, err = common.WriteTuples(ctx, ds, core.RelationTupleUpdate_DELETE, tpl1, tpl2)
require.NoError(err)

ensureNotTuples(ctx, require, ds, tpl1, tpl2)

_, err = common.WriteTuples(ctx, ds, core.RelationTupleUpdate_TOUCH, tpl1, tpl2)
require.NoError(err)

ensureTuples(ctx, require, ds, tpl1, tpl2)
}

// CreateTouchDeleteTouchTest tests writing a relationship, touching it, deleting it, and then touching it.
func CreateTouchDeleteTouchTest(t *testing.T, tester DatastoreTester) {
require := require.New(t)

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

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

tpl1 := makeTestTuple("foo", "tom")
tpl2 := makeTestTuple("foo", "sarah")

_, err = common.WriteTuples(ctx, ds, core.RelationTupleUpdate_CREATE, tpl1, tpl2)
require.NoError(err)

ensureTuples(ctx, require, ds, tpl1, tpl2)

_, err = common.WriteTuples(ctx, ds, core.RelationTupleUpdate_TOUCH, tpl1, tpl2)
require.NoError(err)

ensureTuples(ctx, require, ds, tpl1, tpl2)

_, err = common.WriteTuples(ctx, ds, core.RelationTupleUpdate_DELETE, tpl1, tpl2)
require.NoError(err)

ensureNotTuples(ctx, require, ds, tpl1, tpl2)

_, err = common.WriteTuples(ctx, ds, core.RelationTupleUpdate_TOUCH, tpl1, tpl2)
require.NoError(err)

ensureTuples(ctx, require, ds, tpl1, tpl2)
}

// TouchAlreadyExistingCaveatedTest tests touching a relationship twice.
func TouchAlreadyExistingCaveatedTest(t *testing.T, tester DatastoreTester) {
require := require.New(t)
Expand Down

0 comments on commit 082cc1b

Please sign in to comment.