Skip to content

fix: ids keys sort within a millisecond, and clearer zero-key and drift errors - #364

Draft
borderss wants to merge 8 commits into
mainfrom
fix/353-354-355
Draft

borderss wants to merge 8 commits into
mainfrom
fix/353-354-355

Conversation

@borderss

@borderss borderss commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Draft: #357, #359 and #360 fix the same three issues and were opened before this, with equivalent code. Unless the owner prefers this one, merge those and close this. What this has that they don't: the zero-key line and the gofmt fix in skills/ant/SKILL.md, and the list of ant-docs pages at the end.

Covers #353, #354 and #355. All three turned up while writing the adoption guides for ant-docs. Each issue has a repro commit with a failing test, then a fix commit.

#353: ids keys increase within a millisecond

ids.UUIDv7, ids.UUID and ids.ULID held a millisecond timestamp and then random bits, so keys made in the same millisecond came out in random order. A million keys made in a loop gave a 39 MB primary key index at 69% leaf density on PostgreSQL 17, against 38 MB and 72% for random v4 keys and 30 MB and 90% for google/uuid's NewV7.

The 12 bits after the timestamp now hold the fraction of the millisecond (RFC 9562 section 6.2, method 3), and a mutex keeps the last value so each key sorts after the one before it, as uuid.NewV7 does. ULID puts the same 12 bits at the start of its random part. Both stay valid formats, with 12 fewer random bits (62 in a version 7 UUID, 68 in a ULID). The same million keys now make a 30 MB index at 90%. Decision 178 records it and amends 168.

Tests: 100,000 keys of each kind in a loop come out strictly increasing; eight goroutines making 20,000 keys each get unique keys, each goroutine's in order; a ULID's first ten characters decode to the current millisecond. They also pass with -race -shuffle=on, run in a golang:1.27 container.

#354: the zero-key error suggests a generator for the key's type

The message said "nothing generates it" twice and suggested uuid.New for every key type. Now it says it once, and suggests ids.UUIDv7[T] for a 16-byte key, ids.UUID for a string, and "fn, where fn returns a new T" for anything else:

ant: the key is zero and nothing generates it: Doc.ID is the zero uuid.UUID; set it before the insert, generate it in Configure with ant.GenerateKey(b, &<probe>.ID, ids.UUIDv7[uuid.UUID]) (package github.com/skudra-go/ant/ids), or give the column a default, such as ant:"default=gen_random_uuid()" on PostgreSQL

Decision 179 records it and amends 150. The skill's keys paragraph now leads with ids.UUIDv7[uuid.UUID] as well. That made me run the skill's doccheck, which failed on one Raw sample whose trailing comments were not gofmt-aligned, and that already failed on main, so this PR aligns that sample too.

#355: drift names what it compared the database with

Before the first migration, drift compares the database with the models, but its error still said database differs from the migrations. ErrDrift's text is now ant: database differs, and the wrap adds the source: ant: database differs from the models: 3 differences, or from the snapshot. errors.Is(err, migrate.ErrDrift) still matches. A program that compares the error's text with the old string no longer does.

Checks

make fmt, make lint, make test and make test-pg (PostgreSQL 17) pass. MySQL, MariaDB, SQLite and SQL Server were not run: the key and message code is the same on every database, and drift is PostgreSQL only. After that run only decision 178's text changed, and tests/docs passed again. No exported API changed, so docs/api.txt is untouched.

ant-docs pages to update once this ships

  • models/keys: the quoted ErrZeroKey message.
  • migrations/commands, migrations/adopting and guides/existing-database: the Error: ant: database differs from the migrations line.
  • guides/uuid-primary-keys: the burst row of the index table and "Keep order within a millisecond", which stop applying to ids.UUIDv7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant