-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- refactors memdb caveat test and turns it into a datastore test. Datastores not implementing the interface are skipped - adds migration to add caveat table for PSQL - makes psql TX implements CaveatStorer, following same strategy for MVCC - adds context to ReadCaveatByName to follow same signature as ReadNamespace - adjusts memdb to avoid upserts on duplicate caveats, and align with PSQL - implement missing DeleteCaveat test, and implement that logic or memdb and postgres
- Loading branch information
1 parent
eee236e
commit c21dfad
Showing
16 changed files
with
343 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/authzed/spicedb/internal/datastore/common" | ||
"github.com/authzed/spicedb/pkg/datastore" | ||
core "github.com/authzed/spicedb/pkg/proto/core/v1" | ||
|
||
sq "github.com/Masterminds/squirrel" | ||
"github.com/jackc/pgx/v4" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
var ( | ||
writeCaveat = psql.Insert(tableCaveat).Columns(colCaveatName, colCaveatExpression) | ||
readCaveat = psql.Select(colCaveatExpression).From(tableCaveat) | ||
deleteCaveat = psql.Update(tableCaveat).Where(sq.Eq{colDeletedXid: liveDeletedTxnID}) | ||
) | ||
|
||
func (r *pgReader) ReadCaveatByName(ctx context.Context, name string) (*core.Caveat, error) { | ||
ctx, span := tracer.Start(ctx, "ReadCaveatByName", trace.WithAttributes(attribute.String("name", name))) | ||
defer span.End() | ||
|
||
filteredReadCaveat := r.filterer(readCaveat) | ||
sql, args, err := filteredReadCaveat.Where(sq.Eq{colCaveatName: name}).ToSql() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tx, txCleanup, err := r.txSource(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to read caveat: %w", err) | ||
} | ||
defer txCleanup(ctx) | ||
|
||
var expr []byte | ||
err = tx.QueryRow(ctx, sql, args...).Scan(&expr) | ||
if err != nil { | ||
if errors.Is(err, pgx.ErrNoRows) { | ||
return nil, datastore.NewCaveatNameNotFoundErr(name) | ||
} | ||
return nil, err | ||
} | ||
return &core.Caveat{ | ||
Name: name, | ||
Expression: expr, | ||
}, nil | ||
} | ||
|
||
func (rwt *pgReadWriteTXN) WriteCaveats(caveats []*core.Caveat) error { | ||
ctx, span := tracer.Start(datastore.SeparateContextWithTracing(rwt.ctx), "WriteCaveats") | ||
defer span.End() | ||
|
||
deletedCaveatClause := sq.Or{} | ||
write := writeCaveat | ||
writtenCaveatNames := make([]string, 0, len(caveats)) | ||
for _, caveat := range caveats { | ||
deletedCaveatClause = append(deletedCaveatClause, sq.Eq{colCaveatName: caveat.Name}) | ||
write = write.Values(caveat.Name, caveat.Expression) | ||
writtenCaveatNames = append(writtenCaveatNames, caveat.Name) | ||
} | ||
span.SetAttributes(common.ObjCaveatNameKey.StringSlice(writtenCaveatNames)) | ||
|
||
// mark current caveats as deleted | ||
delSQL, delArgs, err := deleteCaveat. | ||
Set(colDeletedXid, rwt.newXID). | ||
Where(sq.And{sq.Eq{colDeletedXid: liveDeletedTxnID}, deletedCaveatClause}). | ||
ToSql() | ||
if err != nil { | ||
return fmt.Errorf("unable to mark previous caveat revisions as deleted: %w", err) | ||
} | ||
if _, err := rwt.tx.Exec(ctx, delSQL, delArgs...); err != nil { | ||
return fmt.Errorf("unable to mark previous caveat revisions as deleted: %w", err) | ||
} | ||
|
||
// store the new caveat revision | ||
sql, args, err := write.ToSql() | ||
if err != nil { | ||
return fmt.Errorf("unable to write new caveat revision: %w", err) | ||
} | ||
if _, err := rwt.tx.Exec(ctx, sql, args...); err != nil { | ||
return fmt.Errorf("unable to write new caveat revision: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (rwt *pgReadWriteTXN) DeleteCaveats(caveats []*core.Caveat) error { | ||
// TODO implement me | ||
return errors.New("implement me") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
internal/datastore/postgres/migrations/zz_migration.0013_caveat.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jackc/pgx/v4" | ||
) | ||
|
||
var caveatStatements = []string{ | ||
`CREATE TABLE caveat ( | ||
name VARCHAR NOT NULL, | ||
expression BYTEA NOT NULL, | ||
created_xid xid8 NOT NULL DEFAULT (pg_current_xact_id()), | ||
deleted_xid xid8 NOT NULL DEFAULT ('9223372036854775807'), | ||
CONSTRAINT uq_caveat_living PRIMARY KEY (name, deleted_xid), | ||
CONSTRAINT uq_caveat UNIQUE (name, created_xid, deleted_xid));`, | ||
`ALTER TABLE relation_tuple | ||
ADD COLUMN caveat_name VARCHAR, | ||
ADD COLUMN caveat_context JSONB;`, | ||
} | ||
|
||
func init() { | ||
if err := DatabaseMigrations.Register("add-caveats", "drop-bigserial-ids", | ||
noNonatomicMigration, | ||
func(ctx context.Context, tx pgx.Tx) error { | ||
for _, stmt := range caveatStatements { | ||
if _, err := tx.Exec(ctx, stmt); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}); err != nil { | ||
panic("failed to register migration: " + err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.