Skip to content

Commit

Permalink
Fix event reopening for genesis balance
Browse files Browse the repository at this point in the history
  • Loading branch information
violog committed Jul 15, 2024
1 parent 954d292 commit 515c5f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 3 additions & 1 deletion internal/data/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ type EventsQ interface {
FilterByStatus(...EventStatus) EventsQ
FilterByType(...string) EventsQ
FilterByNotType(types ...string) EventsQ
FilterByUpdatedAtBefore(int64) EventsQ
FilterByExternalID(string) EventsQ
FilterInactiveNotClaimed(types ...string) EventsQ
// FilterByUpdatedAtBefore must be only used with SelectReopenable, because it
// depends on table alias.
FilterByUpdatedAtBefore(int64) EventsQ
}
12 changes: 8 additions & 4 deletions internal/data/pg/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewEvents(db *pgdb.DB) data.EventsQ {
updater: squirrel.Update(eventsTable),
deleter: squirrel.Delete(eventsTable),
counter: squirrel.Select("COUNT(*) AS count").From(eventsTable),
reopenable: squirrel.Select("nullifier", "type").Distinct().From(eventsTable + " e1"),
reopenable: squirrel.Select("e1.nullifier", "e1.type").Distinct().From(eventsTable + " e1"),
}
}

Expand Down Expand Up @@ -146,7 +146,10 @@ func (q *events) SelectReopenable() ([]data.ReopenableEvent, error) {
WHERE e2.nullifier = e1.nullifier
AND e2.type = e1.type
AND e2.status IN (?, ?))`, eventsTable)
stmt := q.reopenable.Where(subq, data.EventOpen, data.EventFulfilled)
stmt := q.reopenable.
Where(subq, data.EventOpen, data.EventFulfilled).
Join(balancesTable + " b ON b.nullifier = e1.nullifier").
Where("b.referred_by IS NOT NULL")

var res []data.ReopenableEvent
if err := q.db.Select(&res, stmt); err != nil {
Expand All @@ -168,7 +171,7 @@ func (q *events) SelectAbsentTypes(allTypes ...string) ([]data.ReopenableEvent,
)
SELECT u.nullifier, t.type
FROM (
SELECT nullifier FROM %s
SELECT nullifier FROM %s WHERE referred_by IS NOT NULL
) u
CROSS JOIN types t
LEFT JOIN %s e ON e.nullifier = u.nullifier AND e.type = t.type
Expand Down Expand Up @@ -220,7 +223,8 @@ func (q *events) FilterByExternalID(id string) data.EventsQ {
}

func (q *events) FilterByUpdatedAtBefore(unix int64) data.EventsQ {
return q.applyCondition(squirrel.Lt{"updated_at": unix})
q.reopenable = q.reopenable.Where(squirrel.Lt{"e1.updated_at": unix})
return q
}

func (q *events) FilterInactiveNotClaimed(types ...string) data.EventsQ {
Expand Down

0 comments on commit 515c5f0

Please sign in to comment.