Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(policy): SPIKE transactions support #1663

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions service/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (t Table) Field(field string) string {
// We can rename this but wanted to get mocks working.
type PgxIface interface {
Acquire(ctx context.Context) (*pgxpool.Conn, error)
Begin(ctx context.Context) (pgx.Tx, error)
Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
QueryRow(context.Context, string, ...any) pgx.Row
Query(context.Context, string, ...any) (pgx.Rows, error)
Expand Down
2 changes: 1 addition & 1 deletion service/policy/db/attribute_fqn.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *PolicyDBClient) upsertAttrFqn(ctx context.Context, opts attrFqnUpsertOp
case opts.attributeID != "":
fqn, err = c.Queries.UpsertAttributeDefinitionFqn(ctx, opts.attributeID)
case opts.namespaceID != "":
fqn, err = c.Queries.UpsertAttributeNamespaceFqn(ctx, opts.namespaceID)
fqn, err = c.Queries.UpsertAttributeNamespaceFqn(ctx, opts.valueID)
default:
err = fmt.Errorf("at least one of namespaceId, attributeId, or valueId must be set")
}
Expand Down
15 changes: 15 additions & 0 deletions service/policy/db/policy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package db

import (
"context"

"github.com/jackc/pgx/v5"
"github.com/opentdf/platform/protocol/go/common"
"github.com/opentdf/platform/service/logger"
"github.com/opentdf/platform/service/pkg/db"
Expand All @@ -19,6 +22,18 @@ type PolicyDBClient struct {
*Queries
}

func (c *PolicyDBClient) BeginTx(ctx context.Context) (pgx.Tx, error) {
tx, err := c.Client.Pgx.Begin(ctx)
if err != nil {
return nil, err
}
return tx, nil
}

func (c *PolicyDBClient) WithTx(tx pgx.Tx) *PolicyDBClient {
return &PolicyDBClient{c.Client, c.logger, c.Queries.WithTx(tx)}
}

var (
TableAttributes = "attribute_definitions"
TableAttributeValues = "attribute_values"
Expand Down
15 changes: 14 additions & 1 deletion service/policy/namespaces/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,25 @@
}
rsp := &namespaces.CreateNamespaceResponse{}

n, err := ns.dbClient.CreateNamespace(ctx, req)
tx, err := ns.dbClient.BeginTx(ctx)
if err != nil {
ns.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, "begin txn failed", slog.String("name", req.GetName()))
}
// safe to ignore error per https://pkg.go.dev/github.com/jackc/pgx#hdr-Transactions
defer tx.Rollback(ctx)

Check failure on line 102 in service/policy/namespaces/namespaces.go

View workflow job for this annotation

GitHub Actions / go (service)

Error return value of `tx.Rollback` is not checked (errcheck)
jakedoublev marked this conversation as resolved.
Show resolved Hide resolved

n, err := ns.dbClient.WithTx(tx).CreateNamespace(ctx, req)
if err != nil {
ns.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, db.ErrTextCreationFailed, slog.String("name", req.GetName()))
}

if err = tx.Commit(ctx); err != nil {
ns.logger.Audit.PolicyCRUDFailure(ctx, auditParams)
return nil, db.StatusifyError(err, "commit txn failed", slog.String("name", req.GetName()))
}

auditParams.ObjectID = n.GetId()
auditParams.Original = n
ns.logger.Audit.PolicyCRUDSuccess(ctx, auditParams)
Expand Down
Loading