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
F3527668: D11104.id37512.diff
Tue, Dec 24, 6:05 AM
F3527667: D11104.id37387.diff
Tue, Dec 24, 6:05 AM
F3527664: D11104.id37386.diff
Tue, Dec 24, 6:05 AM
F3527663: D11104.id37321.diff
Tue, Dec 24, 6:05 AM
F3527647: D11104.id.diff
Tue, Dec 24, 6:04 AM
F3527641: D11104.diff
Tue, Dec 24, 6:04 AM
Unknown Object (File)
Nov 7 2024, 1:44 AM
Unknown Object (File)
Nov 1 2024, 10:58 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