Switch UUID generation from v4 to v7#18
Merged
Merged
Conversation
Use SecureRandom.uuid_v7 (Ruby 3.4+) instead of SecureRandom.uuid (v4). UUIDv7 (RFC 9562) embeds a 48-bit unix timestamp, so primary keys naturally sort chronologically. This fixes comment reply ordering and improves B-tree index locality. Amp-Thread-ID: https://ampcode.com/threads/T-019c9157-b075-7443-9d10-f4aa772fec8f Co-authored-by: Amp <amp@ampcode.com>
HamptonMakes
force-pushed
the
switch-to-uuid-v7
branch
from
February 24, 2026 20:33
153767d to
9b52865
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Switches
ApplicationRecord#assign_uuidfromSecureRandom.uuid(v4, random) toSecureRandom.uuid_v7(Ruby 3.4+, time-ordered).Why
UUID v4 primary keys sort in random order, which caused comment replies to appear out of order. UUIDv7 embeds a 48-bit millisecond timestamp in the high bits, so PKs naturally sort chronologically.
Bonus: time-ordered UUIDs improve B-tree index locality, reducing page splits and improving query performance.
Notes
SecureRandom.uuid_v7.