Skip to content

Commit

Permalink
fix(policy): REVERT PR #1663 - SPIKE transactions support (#1719)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanulit authored Nov 5, 2024
1 parent 829fa47 commit 184a733
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 37 deletions.
1 change: 0 additions & 1 deletion service/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ 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
15 changes: 0 additions & 15 deletions service/policy/db/policy.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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 @@ -22,18 +19,6 @@ 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)}
}

func NewClient(c *db.Client, logger *logger.Logger) PolicyDBClient {
return PolicyDBClient{c, logger, New(c.Pgx)}
}
Expand Down
8 changes: 0 additions & 8 deletions service/policy/db/utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package db

import (
"context"
"fmt"

"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/opentdf/platform/protocol/go/common"
"github.com/opentdf/platform/protocol/go/policy"
Expand Down Expand Up @@ -61,9 +59,3 @@ func pgtypeBool(b bool) pgtype.Bool {
Valid: true,
}
}

// Helper function for swallowing the error in a Pgx Transaction rollback per the documentation
func TxRollback(ctx context.Context, tx pgx.Tx) {
//nolint:errcheck // noop https://pkg.go.dev/github.com/jackc/pgx#hdr-Transactions
tx.Rollback(ctx)
}
14 changes: 1 addition & 13 deletions service/policy/namespaces/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,12 @@ func (ns NamespacesService) CreateNamespace(ctx context.Context, req *namespaces
}
rsp := &namespaces.CreateNamespaceResponse{}

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()))
}
defer policydb.TxRollback(ctx, tx)

n, err := ns.dbClient.WithTx(tx).CreateNamespace(ctx, req)
n, err := ns.dbClient.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

0 comments on commit 184a733

Please sign in to comment.