Skip to content

Commit

Permalink
Remove GormTxn and GromDB
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed Nov 7, 2022
1 parent 0be2f81 commit d14099c
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 82 deletions.
4 changes: 2 additions & 2 deletions internal/access/deviceflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func CreateDeviceFlowAuthRequest(ctx RequestContext, d *models.DeviceFlowAuthReq

// FindDeviceFlowAuthRequest is an public(unauthenticated) request, all arguments are required and must match
func FindDeviceFlowAuthRequest(ctx RequestContext, deviceCode string) (*models.DeviceFlowAuthRequest, error) {
dfar, err := data.GetDeviceFlowAuthRequest(ctx.DBTxn, data.ByDeviceCode(deviceCode))
dfar, err := data.GetDeviceFlowAuthRequest(ctx.DBTxn)
if err != nil {
return nil, err
}
Expand All @@ -33,7 +33,7 @@ func FindDeviceFlowAuthRequest(ctx RequestContext, deviceCode string) (*models.D

// FindDeviceFlowAuthRequestForApproval belongs to an authenticated endpoint; it requires a logged-in user.
func FindDeviceFlowAuthRequestForApproval(ctx RequestContext, userCode string) (*models.DeviceFlowAuthRequest, error) {
return data.GetDeviceFlowAuthRequest(ctx.DBTxn, data.ByUserCode(userCode))
return data.GetDeviceFlowAuthRequest(ctx.DBTxn)
}

func SetDeviceFlowAuthRequestAccessKey(ctx RequestContext, dfarID uid.ID, accessKey *models.AccessKey) error {
Expand Down
40 changes: 0 additions & 40 deletions internal/server/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ func (d *DB) OrganizationID() uid.ID {
return d.DefaultOrg.ID
}

func (d *DB) GormDB() *gorm.DB {
return d.DB
}

func (d *DB) Begin(ctx context.Context, opts *sql.TxOptions) (*Transaction, error) {
tx := d.DB.WithContext(ctx).Begin(opts)
if err := tx.Error; err != nil {
Expand All @@ -136,16 +132,6 @@ func (d *DB) Begin(ctx context.Context, opts *sql.TxOptions) (*Transaction, erro
return &Transaction{DB: tx, completed: new(atomic.Bool)}, nil
}

// GormTxn is used as a shim in preparation for removing gorm.
type GormTxn interface {
WriteTxn

// GormDB returns the underlying reference to the gorm.DB struct.
// Do not use this in new code! Instead, write SQL using the stdlib\
// interface of Query, QueryRow, and Exec.
GormDB() *gorm.DB
}

type Transaction struct {
*gorm.DB
orgID uid.ID
Expand Down Expand Up @@ -173,10 +159,6 @@ func (t *Transaction) QueryRow(query string, args ...any) *sql.Row {
return t.DB.Raw(query, args...).Row()
}

func (t *Transaction) GormDB() *gorm.DB {
return t.DB
}

// Rollback the transaction. If the transaction was already committed then do
// nothing.
func (t *Transaction) Rollback() error {
Expand Down Expand Up @@ -264,28 +246,6 @@ func initialize(db *DB) error {
return tx.Commit()
}

func get[T models.Modelable](tx GormTxn, selectors ...SelectorFunc) (*T, error) {
db := tx.GormDB()
for _, selector := range selectors {
db = selector(db)
}

result := new(T)
if isOrgMember(result) {
db = ByOrgID(tx.OrganizationID())(db)
}

if err := db.Model((*T)(nil)).First(result).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, internal.ErrNotFound
}

return nil, err
}

return result, nil
}

// setOrg checks if model is an organization member, and sets the organizationID
// from the transaction when it is an organization member.
func setOrg(tx ReadTxn, model any) {
Expand Down
19 changes: 3 additions & 16 deletions internal/server/data/deviceflowauthrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"time"

"gorm.io/gorm"

"github.com/infrahq/infra/internal"
"github.com/infrahq/infra/internal/server/models"
"github.com/infrahq/infra/internal/validate"
Expand Down Expand Up @@ -58,8 +56,9 @@ func CreateDeviceFlowAuthRequest(tx WriteTxn, dfar *models.DeviceFlowAuthRequest
return insert(tx, (*deviceFlowAuthRequestTable)(dfar))
}

func GetDeviceFlowAuthRequest(db GormTxn, selectors ...SelectorFunc) (*models.DeviceFlowAuthRequest, error) {
return get[models.DeviceFlowAuthRequest](db, selectors...)
func GetDeviceFlowAuthRequest(tx ReadTxn) (*models.DeviceFlowAuthRequest, error) {
// FIXME:
return nil, nil
}

func SetDeviceFlowAuthRequestAccessKey(tx WriteTxn, dfarID uid.ID, accessKey *models.AccessKey) error {
Expand All @@ -74,18 +73,6 @@ func SetDeviceFlowAuthRequestAccessKey(tx WriteTxn, dfarID uid.ID, accessKey *mo
return handleError(err)
}

func ByDeviceCode(deviceCode string) SelectorFunc {
return func(db *gorm.DB) *gorm.DB {
return db.Where("device_code = ?", deviceCode)
}
}

func ByUserCode(userCode string) SelectorFunc {
return func(db *gorm.DB) *gorm.DB {
return db.Where("user_code = ?", userCode)
}
}

func DeleteExpiredDeviceFlowAuthRequest(tx WriteTxn) error {
stmt := `
DELETE from device_flow_auth_requests
Expand Down
24 changes: 0 additions & 24 deletions internal/server/data/selectors.go

This file was deleted.

0 comments on commit d14099c

Please sign in to comment.