Skip to content

Commit

Permalink
Merge pull request #2149 from authzed/fix-watch-schema-bug
Browse files Browse the repository at this point in the history
fix: schema watch causes incorrect delete event for schema changes
  • Loading branch information
josephschorr authored Nov 29, 2024
2 parents a251139 + 369286e commit 7800b70
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/datastore/postgres/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func (pgd *pgDatastore) loadNamespaceChanges(ctx context.Context, xmin uint64, x
}

if _, found := filter[createdXID.Uint64]; found {
err := tracked.AddChangedDefinition(ctx, txidToRevision[deletedXID.Uint64], loaded)
err := tracked.AddChangedDefinition(ctx, txidToRevision[createdXID.Uint64], loaded)
if err != nil {
return err
}
Expand Down Expand Up @@ -514,7 +514,7 @@ func (pgd *pgDatastore) loadCaveatChanges(ctx context.Context, xmin uint64, xmax
}

if _, found := filter[createdXID.Uint64]; found {
err := tracked.AddChangedDefinition(ctx, txidToRevision[deletedXID.Uint64], loaded)
err := tracked.AddChangedDefinition(ctx, txidToRevision[createdXID.Uint64], loaded)
if err != nil {
return err
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/datastore/test/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ func WatchSchemaTest(t *testing.T, tester DatastoreTester) {
changes, errchan := ds.Watch(ctx, lowestRevision, datastore.WatchJustSchema())
require.Zero(len(errchan))

// Addition
// Write an updated schema and ensure the changes are returned.
_, err = ds.ReadWriteTx(ctx, func(ctx context.Context, rwt datastore.ReadWriteTransaction) error {
err := rwt.WriteNamespaces(ctx, &core.NamespaceDefinition{
Expand All @@ -561,6 +562,37 @@ func WatchSchemaTest(t *testing.T, tester DatastoreTester) {
},
}, changes, errchan, false)

// Changed
_, err = ds.ReadWriteTx(ctx, func(ctx context.Context, rwt datastore.ReadWriteTransaction) error {
err := rwt.WriteNamespaces(ctx, &core.NamespaceDefinition{
Name: "somenewnamespace",
Relation: []*core.Relation{
{
Name: "anotherrelation",
},
},
})
if err != nil {
return err
}

return rwt.WriteCaveats(ctx, []*core.CaveatDefinition{
{
Name: "somenewcaveat",
SerializedExpression: []byte("123"),
},
})
})
require.NoError(err)

verifyMixedUpdates(require, [][]string{
{
"changed:somenewnamespace",
"changed:somenewcaveat",
},
}, changes, errchan, false)

// Removed
// Delete some namespaces and caveats.
_, err = ds.ReadWriteTx(ctx, func(ctx context.Context, rwt datastore.ReadWriteTransaction) error {
err := rwt.DeleteNamespaces(ctx, "somenewnamespace")
Expand Down

0 comments on commit 7800b70

Please sign in to comment.