refactor direct sql into query.go (#192)
This commit is contained in:
3 files changed
+28
-18
No files matched your search
@@ -0,0 +1,24 @@
|
||||
package iam
|
||||
|
||||
// query.go contains "raw sql" for the iam package that goes directly against
|
||||
// the db via sql.DB vs the standard pattern of using the internal/db package to
|
||||
// interact with the db.
|
||||
const (
|
||||
// whereUserAuthAccount - given an auth account id, return the associated user.
|
||||
whereUserAuthAccount = `
|
||||
select iam_user.*
|
||||
from iam_user
|
||||
inner join auth_account
|
||||
on iam_user.public_id = auth_account.iam_user_id
|
||||
where
|
||||
iam_user.scope_id = auth_account.scope_id and
|
||||
auth_account.public_id = $1`
|
||||
|
||||
// whereValidAuthMethod - determine if an auth method public_id within a scope_id
|
||||
// is valid by returning a count of matching rows.
|
||||
whereValidAuthMethod = `select count(*) from auth_method where public_id = $1 and scope_id = $2`
|
||||
|
||||
// insertAuthMethod - insert a row directly into auth_method (TODO - this
|
||||
// should be replaced with calls to the auth method repo)
|
||||
insertAuthMethod = `insert into auth_method (public_id, scope_id) values ($1, $2)`
|
||||
)
|
||||
@@ -219,18 +219,7 @@ func (r *Repository) getUserWithAuthAccount(ctx context.Context, withAuthAccount
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get underlying db for auth account search: %w", err)
|
||||
}
|
||||
userTable := func() *User { u := allocUser(); return &u }().TableName()
|
||||
acctTable := func() *AuthAccount { a := allocAuthAccount(); return &a }().TableName()
|
||||
|
||||
where := fmt.Sprintf(`
|
||||
select %[1]s.*
|
||||
from %[1]s
|
||||
inner join %[2]s
|
||||
on %[1]s.public_id = %[2]s.iam_user_id
|
||||
where
|
||||
%[1]s.scope_id = %[2]s.scope_id and
|
||||
%[2]s.public_id = $1`, userTable, acctTable)
|
||||
rows, err := underlying.Query(where, withAuthAccountId)
|
||||
rows, err := underlying.Query(whereUserAuthAccount, withAuthAccountId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to query auth account %s", withAuthAccountId)
|
||||
}
|
||||
@@ -291,13 +280,12 @@ func (r *Repository) validateAuthMethodId(ctx context.Context, scopeId, authMeth
|
||||
if authMethodId == "" {
|
||||
return false, fmt.Errorf("auth method id is unset: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
const where = `select count(*) from auth_method where public_id = $1 and scope_id = $2`
|
||||
db, err := r.reader.DB()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var cnt int
|
||||
if err := db.QueryRow(where, authMethodId, scopeId).Scan(&cnt); err != nil {
|
||||
if err := db.QueryRow(whereValidAuthMethod, authMethodId, scopeId).Scan(&cnt); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if cnt == 0 {
|
||||
|
||||
@@ -197,7 +197,6 @@ func TestGroupRole(t *testing.T, conn *gorm.DB, roleId, grpId string, opt ...Opt
|
||||
func testAuthAccount(t *testing.T, conn *gorm.DB, scopeId, authMethodId, userId string) *AuthAccount {
|
||||
const (
|
||||
authAccountPrefix = "aa_"
|
||||
where = `select count(*) from auth_method where public_id = $1 and scope_id = $2`
|
||||
)
|
||||
t.Helper()
|
||||
rw := db.New(conn)
|
||||
@@ -215,7 +214,7 @@ func testAuthAccount(t *testing.T, conn *gorm.DB, scopeId, authMethodId, userId
|
||||
}
|
||||
|
||||
var count int
|
||||
err := conn.DB().QueryRow(where, authMethodId, scopeId).Scan(&count)
|
||||
err := conn.DB().QueryRow(whereValidAuthMethod, authMethodId, scopeId).Scan(&count)
|
||||
require.NoError(err)
|
||||
require.Equal(1, count)
|
||||
|
||||
@@ -246,7 +245,6 @@ func testAuthAccount(t *testing.T, conn *gorm.DB, scopeId, authMethodId, userId
|
||||
func testAuthMethod(t *testing.T, conn *gorm.DB, scopeId string) string {
|
||||
const (
|
||||
authMethodPrefix = "am_"
|
||||
insert = `insert into auth_method (public_id, scope_id) values ($1, $2)`
|
||||
)
|
||||
t.Helper()
|
||||
require := require.New(t)
|
||||
@@ -255,7 +253,7 @@ func testAuthMethod(t *testing.T, conn *gorm.DB, scopeId string) string {
|
||||
id, err := db.NewPublicId(authMethodPrefix)
|
||||
require.NoError(err)
|
||||
|
||||
_, err = conn.DB().Exec(insert, id, scopeId)
|
||||
_, err = conn.DB().Exec(insertAuthMethod, id, scopeId)
|
||||
require.NoError(err)
|
||||
return id
|
||||
}
|
||||
Reference in new issue
Block a user