Skip to content

Commit

Permalink
Move Query Template Out of Shared Method
Browse files Browse the repository at this point in the history
The `like` segment of the modified query is causing issues for the
grants deployment of this service. To resolve, move the query template
string out into the already-forked implementations to permit different
treatments of this query.
  • Loading branch information
Sneagan committed Dec 6, 2024
1 parent 5b9be47 commit e048099
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
13 changes: 6 additions & 7 deletions server/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ func (c *Server) fetchIssuer(issuerID string) (*model.Issuer, error) {
// the Ads implementation had non-unique issuer types. This is no longer the case and this
// function should be refactored or removed. For now, it will return an array of a single
// issuer.
func (c *Server) fetchIssuersByCohort(issuerType string, issuerCohort int16) ([]model.Issuer, error) {
func (c *Server) fetchIssuersByCohort(
issuerType string,
issuerCohort int16,
queryTemplate string,
) ([]model.Issuer, error) {
// will not lose resolution int16->int
if cached := retrieveFromCache(c.caches, "issuercohort", issuerType); cached != nil {
if issuers, ok := cached.([]model.Issuer); ok {
Expand All @@ -231,12 +235,7 @@ func (c *Server) fetchIssuersByCohort(issuerType string, issuerCohort int16) ([]
}

var fetchedIssuers []model.Issuer
err := c.db.Select(
&fetchedIssuers,
`SELECT i.*
FROM v3_issuers i join v3_issuer_keys k on (i.issuer_id=k.issuer_id)
WHERE i.issuer_type like $1 || '%' AND k.cohort=$2
ORDER BY i.expires_at DESC NULLS FIRST, i.created_at DESC`, issuerType, issuerCohort)
err := c.db.Select(&fetchedIssuers, queryTemplate, issuerType, issuerCohort)
if err != nil {
c.Logger.Error("Failed to extract issuers from DB")
return nil, utils.ProcessingErrorFromError(err, isPostgresNotFoundError(err))
Expand Down
18 changes: 16 additions & 2 deletions server/issuers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ type issuerFetchRequestV2 struct {

// GetLatestIssuer - get the latest issuer by type/cohort
func (c *Server) GetLatestIssuer(issuerType string, issuerCohort int16) (*model.Issuer, *handlers.AppError) {
issuer, err := c.fetchIssuersByCohort(issuerType, issuerCohort)
issuer, err := c.fetchIssuersByCohort(
issuerType,
issuerCohort,
`SELECT i.*
FROM v3_issuers i join v3_issuer_keys k on (i.issuer_id=k.issuer_id)
WHERE i.issuer_type=$1 AND k.cohort=$2
ORDER BY i.expires_at DESC NULLS FIRST, i.created_at DESC`,
)
if err != nil {
if errors.Is(err, errIssuerCohortNotFound) {
c.Logger.Error("Issuer with given type and cohort not found")
Expand All @@ -78,7 +85,14 @@ func (c *Server) GetLatestIssuer(issuerType string, issuerCohort int16) (*model.

// GetLatestIssuerKafka - get the issuer and any processing error
func (c *Server) GetLatestIssuerKafka(issuerType string, issuerCohort int16) (*model.Issuer, error) {
issuer, err := c.fetchIssuersByCohort(issuerType, issuerCohort)
issuer, err := c.fetchIssuersByCohort(
issuerType,
issuerCohort,
`SELECT i.*
FROM v3_issuers i join v3_issuer_keys k on (i.issuer_id=k.issuer_id)
WHERE i.issuer_type like $1 || '%' AND k.cohort=$2
ORDER BY i.expires_at DESC NULLS FIRST, i.created_at DESC`,
)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e048099

Please sign in to comment.