Files
cloud-ip-validator/internal/db/queries_ipsitechecks.go
T

40 lines
1.4 KiB
Go

package db
import "context"
// SetSiteComplete marks a given site's inbound checks complete for an IP's
// *current* attempt. attempt_number is resolved via subquery from
// ip_queue rather than taken as a parameter, so the call signature stays
// the same as before the fixed-3-columns design was replaced by this
// table. No cap on siteIndex.
func (d *DB) SetSiteComplete(ctx context.Context, ipID int64, siteIndex int) error {
_, err := d.ExecContext(ctx, `
INSERT INTO ip_site_checks (ip_id, attempt_number, site_idx, complete, completed_at)
SELECT ?, attempt_number, ?, 1, ? FROM ip_queue WHERE id=?
ON CONFLICT(ip_id, attempt_number, site_idx) DO UPDATE SET complete=1, completed_at=excluded.completed_at
`, ipID, siteIndex, timeToDB(Now()), ipID)
return err
}
// ListCompletedSiteIndices returns the set of site indices that have
// reported completion for the given IP's attempt.
func (d *DB) ListCompletedSiteIndices(ctx context.Context, ipID int64, attemptNumber int) (map[int]bool, error) {
rows, err := d.QueryContext(ctx, `
SELECT site_idx FROM ip_site_checks WHERE ip_id=? AND attempt_number=? AND complete=1
`, ipID, attemptNumber)
if err != nil {
return nil, err
}
defer rows.Close()
out := map[int]bool{}
for rows.Next() {
var idx int
if err := rows.Scan(&idx); err != nil {
return nil, err
}
out[idx] = true
}
return out, rows.Err()
}