diff --git a/internal/datastore/postgres/watch.go b/internal/datastore/postgres/watch.go index d91e2fa78c..d8a8253bdc 100644 --- a/internal/datastore/postgres/watch.go +++ b/internal/datastore/postgres/watch.go @@ -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 } @@ -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 } diff --git a/pkg/datastore/test/watch.go b/pkg/datastore/test/watch.go index e36b02973b..40b9e7aa58 100644 --- a/pkg/datastore/test/watch.go +++ b/pkg/datastore/test/watch.go @@ -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{ @@ -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")