diff --git a/packages/db/queries/get_snapshots_with_cursor.sql b/packages/db/queries/get_snapshots_with_cursor.sql index a3429c31e8..e849112c5b 100644 --- a/packages/db/queries/get_snapshots_with_cursor.sql +++ b/packages/db/queries/get_snapshots_with_cursor.sql @@ -22,11 +22,8 @@ WHERE -- And NULL does not match with empty json s.metadata @> @metadata OR @metadata = '{}'::jsonb ) - AND ( - s.sandbox_started_at < @cursor_time - OR - (s.sandbox_started_at = @cursor_time AND s.sandbox_id > @cursor_id) - ) + -- The order here is important, we want started_at descending, but sandbox_id ascending + AND (s.sandbox_started_at, @cursor_id::text) < (@cursor_time, s.sandbox_id) AND NOT (s.sandbox_id = ANY (@snapshot_exclude_sbx_ids::text[])) -ORDER BY s.sandbox_started_at DESC, s.sandbox_id +ORDER BY s.sandbox_started_at DESC, s.sandbox_id ASC LIMIT $1; diff --git a/packages/db/queries/get_snapshots_with_cursor.sql.go b/packages/db/queries/get_snapshots_with_cursor.sql.go index b73e6e86be..2859ad1d89 100644 --- a/packages/db/queries/get_snapshots_with_cursor.sql.go +++ b/packages/db/queries/get_snapshots_with_cursor.sql.go @@ -37,13 +37,10 @@ WHERE -- And NULL does not match with empty json s.metadata @> $3 OR $3 = '{}'::jsonb ) - AND ( - s.sandbox_started_at < $4 - OR - (s.sandbox_started_at = $4 AND s.sandbox_id > $5) - ) + -- The order here is important, we want started_at descending, but sandbox_id ascending + AND (s.sandbox_started_at, $4::text) < ($5, s.sandbox_id) AND NOT (s.sandbox_id = ANY ($6::text[])) -ORDER BY s.sandbox_started_at DESC, s.sandbox_id +ORDER BY s.sandbox_started_at DESC, s.sandbox_id ASC LIMIT $1 ` @@ -51,8 +48,8 @@ type GetSnapshotsWithCursorParams struct { Limit int32 TeamID uuid.UUID Metadata types.JSONBStringMap - CursorTime pgtype.Timestamptz CursorID string + CursorTime pgtype.Timestamptz SnapshotExcludeSbxIds []string } @@ -67,8 +64,8 @@ func (q *Queries) GetSnapshotsWithCursor(ctx context.Context, arg GetSnapshotsWi arg.Limit, arg.TeamID, arg.Metadata, - arg.CursorTime, arg.CursorID, + arg.CursorTime, arg.SnapshotExcludeSbxIds, ) if err != nil {