store: order latest buildkite build by monotonic int
LookupBuildkiteBuildByTuple sorted on created_at, an RFC3339Nano
text column. Lexical comparison of nanosecond timestamps is not
reliable: time.Format trims trailing zeros, so an instant on the
exact second renders as '...:00Z' while one nanosecond later
renders as '...:00.000000001Z' and lex-sorts before it. The
practical effect was that /logs could resolve the wrong run for
a workflow that had been triggered more than once.
Add a created_unix_ns INTEGER column to buildkite_builds, populate
it from time.Now().UnixNano() on insert, and switch the lookup to
ORDER BY created_unix_ns DESC with created_at and build_number as
deterministic tiebreakers for legacy rows that pre-date the column.
The migration path is covered: an additive ALTER widens existing
databases, and a one-shot Go-side backfill parses each row's
created_at and writes the corresponding UnixNano. Rows whose text
fails to parse are left at the default 0 so a single corrupt row
cannot wedge startup. New tests in store_migrate_test.go open a
hand-crafted pre-migration database through openStore and assert
the upgrade is correct, idempotent, and tolerant of bad data.