package store import ( "context" "database/sql" ) // AllocationBindingByAllocationIDSQL resolves the durable match_id/server_id // pairing for an allocation_id. A signed workload token only ever names // allocation_id (see workload/signed_token.go for why match_id/server_id // aren't embedded in the token itself); this is what lets WorkloadVerify // return a binding whose match_id/server_id came from the durable allocator // record, not from anything the caller supplied. allocations rows are // append-only and never leave 'ALLOCATED' (see allocator_sql.go), so this is // a simple existence lookup, not a state-machine walk. const AllocationBindingByAllocationIDSQL = `SELECT match_id, server_id FROM allocations WHERE allocation_id = $1 AND state = 'ALLOCATED'` // AllocationBindingByAllocationID returns the (matchID, serverID) durably // recorded for allocationID, and false if no such allocated row exists. db // and allocationID must be non-empty -- callers pass this an // already-parsed and signature-verified token's claims, so an empty // allocationID here indicates a caller bug rather than a legitimate // "not found". func AllocationBindingByAllocationID(ctx context.Context, db *sql.DB, allocationID string) (matchID, serverID string, ok bool, err error) { if db == nil || allocationID == "" { return "", "", false, sql.ErrNoRows } err = db.QueryRowContext(ctx, AllocationBindingByAllocationIDSQL, allocationID).Scan(&matchID, &serverID) if err == sql.ErrNoRows { return "", "", false, nil } if err != nil { return "", "", false, err } return matchID, serverID, true, nil }