Page MenuHomePhabricator

[keyserver][lib] Make updates code aware that ids are strings, not numbers
ClosedPublic

Authored by inka on Feb 19 2024, 4:52 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sat, May 4, 9:55 AM
Unknown Object (File)
Thu, Apr 25, 7:48 AM
Unknown Object (File)
Mar 25 2024, 1:40 PM
Unknown Object (File)
Mar 12 2024, 9:52 AM
Unknown Object (File)
Mar 12 2024, 9:52 AM
Unknown Object (File)
Mar 12 2024, 9:52 AM
Unknown Object (File)
Feb 28 2024, 9:00 PM
Unknown Object (File)
Feb 28 2024, 7:47 PM
Subscribers

Details

Summary

issue: https://linear.app/comm/issue/ENG-6822/creating-db-fails-when-using-identity-ids
sortIDs has two branches, because the old code would sotr ids by converting them to numbers, comparing like numbers, and converting back to ids. Now that ids can be strings, other than stringified numbers, we need to compare them as strings. But this means that when the old logic would sort 99 before 100, the new logic would do the opposite.
And we need the old logic and the new logic to return the same order, because based on this order key of update is created, and based on the key we decide which updates eliminate each other.

Test Plan

Tested that sortIDs doesn't turn uuids into NaNs anymore, but correctly sorts them with other ids.
Tested that a newly created db has the key column of the updates table be a varchar(255)
Tested migrating from a db that had the key column bigint(20) - all ids got correctly translated, no errors showed up.

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

keyserver/src/database/setup-db.js
174 ↗(On Diff #37321)

Based on the fact that we put ids as the key, and user ids are varchar(255) CHARSET latin1 COLLATE latin1_bin

inka requested review of this revision.Feb 19 2024, 5:07 AM
lib/shared/relationship-utils.js
15 ↗(On Diff #37321)

For this sorting algorithm: if a > b and b > c, is it always the case that a > c?

15–23 ↗(On Diff #37321)

I don't think we should have a single generic function that handles two completely different kinds of sorts. This hurts readability at the callsite

I think there are two potential solutions here:

  1. You can introduce another function that handles sorting IDs using a lexicographical order, and then all the relevant callsites switch between the two options
  2. Perhaps better would just be to rename this function to something less generic, such as sortUserIDs
lib/shared/relationship-utils.js
15 ↗(On Diff #37321)

I don't think this is needed in any of the places we currently use this function, but I agree that since this function is called "sort", it should probably be transitive, I'll fix that

Rename sortIDs to sortUserIDs, make the relationship transitive

tomek added inline comments.
lib/shared/relationship-utils.test.js
9 ↗(On Diff #37386)
This revision is now accepted and ready to land.Feb 21 2024, 1:03 AM