Skip to content

Commit 48bfd20

Browse files
authored
Fix sqlite paging
1 parent b0cdf70 commit 48bfd20

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

backend/sqlite/diagnostics.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,36 @@ import (
1313
var _ web.Backend = (*sqliteBackend)(nil)
1414

1515
func (sb *sqliteBackend) GetWorkflowInstances(ctx context.Context, afterInstanceID string, count int) ([]*web.WorkflowInstanceRef, error) {
16+
var err error
1617
tx, err := sb.db.BeginTx(ctx, nil)
1718
if err != nil {
1819
return nil, err
1920
}
2021
defer tx.Rollback()
2122

22-
rows, err := tx.QueryContext(
23-
ctx,
24-
"SELECT id, execution_id, created_at, completed_at FROM instances WHERE id > ? ORDER BY created_at DESC LIMIT ?",
25-
afterInstanceID,
26-
count,
27-
)
23+
var rows *sql.Rows
24+
if afterInstanceID != "" {
25+
rows, err = tx.QueryContext(
26+
ctx,
27+
`SELECT i.id, i.execution_id, i.created_at, i.completed_at
28+
FROM instances i
29+
INNER JOIN (SELECT id, created_at FROM instances WHERE id = ?) ii
30+
ON i.created_at < ii.created_at OR (i.created_at = ii.created_at AND i.id < ii.id)
31+
ORDER BY i.created_at DESC, i.id DESC
32+
LIMIT ?`,
33+
afterInstanceID,
34+
count,
35+
)
36+
} else {
37+
rows, err = tx.QueryContext(
38+
ctx,
39+
`SELECT i.id, i.execution_id, i.created_at, i.completed_at
40+
FROM instances i
41+
ORDER BY i.created_at DESC, i.id DESC
42+
LIMIT ?`,
43+
count,
44+
)
45+
}
2846
if err != nil {
2947
return nil, err
3048
}

0 commit comments

Comments
 (0)