-
Notifications
You must be signed in to change notification settings - Fork 117
fix: transactions paging using effective order #971
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"github.com/formancehq/go-libs/v3/bun/bunpaginate" | ||
"github.com/formancehq/go-libs/v3/time" | ||
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/schema" | ||
"math/big" | ||
"reflect" | ||
"strings" | ||
|
@@ -14,6 +15,7 @@ | |
type ColumnPaginator[ResourceType, OptionsType any] struct { | ||
DefaultPaginationColumn string | ||
DefaultOrder bunpaginate.Order | ||
Table *schema.Table | ||
} | ||
|
||
//nolint:unused | ||
|
@@ -23,6 +25,7 @@ | |
if query.Column != "" { | ||
paginationColumn = query.Column | ||
} | ||
|
||
originalOrder := o.DefaultOrder | ||
if query.Order != nil { | ||
originalOrder = *query.Order | ||
|
@@ -42,26 +45,39 @@ | |
sb = sb.ColumnExpr("row_number() OVER (ORDER BY " + orderExpression + ")") | ||
|
||
if query.PaginationID != nil { | ||
paginationID := convertPaginationIDToSQLType( | ||
o.Table.FieldMap[paginationColumn].DiscoveredSQLType, | ||
query.PaginationID, | ||
) | ||
if query.Reverse { | ||
Comment on lines
+48
to
52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Unsafe access to FieldMap – verify column exists
-field := o.Table.FieldMap[paginationColumn]
-if field == nil {
- return nil, fmt.Errorf("unknown pagination column %q for table %s", paginationColumn, o.Table.Name)
-}
-paginationID := convertPaginationIDToSQLType(field.DiscoveredSQLType, query.PaginationID)
+field := o.Table.FieldMap[paginationColumn]
+if field == nil {
+ return nil, fmt.Errorf("unknown pagination column %q for table %s", paginationColumn, o.Table.Name)
+}
+paginationID := convertPaginationIDToSQLType(field.DiscoveredSQLType, query.PaginationID)
🤖 Prompt for AI Agents
|
||
switch originalOrder { | ||
case bunpaginate.OrderAsc: | ||
sb = sb.Where(fmt.Sprintf("%s < ?", paginationColumn), query.PaginationID) | ||
sb = sb.Where(fmt.Sprintf("%s < ?", paginationColumn), paginationID) | ||
case bunpaginate.OrderDesc: | ||
sb = sb.Where(fmt.Sprintf("%s > ?", paginationColumn), query.PaginationID) | ||
sb = sb.Where(fmt.Sprintf("%s > ?", paginationColumn), paginationID) | ||
} | ||
} else { | ||
switch originalOrder { | ||
case bunpaginate.OrderAsc: | ||
sb = sb.Where(fmt.Sprintf("%s >= ?", paginationColumn), query.PaginationID) | ||
sb = sb.Where(fmt.Sprintf("%s >= ?", paginationColumn), paginationID) | ||
case bunpaginate.OrderDesc: | ||
sb = sb.Where(fmt.Sprintf("%s <= ?", paginationColumn), query.PaginationID) | ||
sb = sb.Where(fmt.Sprintf("%s <= ?", paginationColumn), paginationID) | ||
} | ||
} | ||
} | ||
|
||
return sb, nil | ||
} | ||
|
||
func convertPaginationIDToSQLType(sqlType string, id *big.Int) any { | ||
switch sqlType { | ||
case "timestamp without time zone", "timestamp": | ||
return libtime.UnixMicro(id.Int64()) | ||
default: | ||
return id | ||
} | ||
} | ||
|
||
//nolint:unused | ||
func (o ColumnPaginator[ResourceType, OptionsType]) BuildCursor(ret []ResourceType, query ColumnPaginatedQuery[OptionsType]) (*bunpaginate.Cursor[ResourceType], error) { | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Table field can be nil – potential runtime panic
o.Table
is dereferenced later (o.Table.FieldMap[...]
) without any nil-check.If any caller forgets (or fails) to set
Table
, the first paginated request will panic.Add a defensive check early in
Paginate
, or at least fail fast with a clear error:🤖 Prompt for AI Agents