-
Notifications
You must be signed in to change notification settings - Fork 21
Description
Automatic assignment of PRIMARY KEY values (e.g. through rowid) is not guaranteed to be consistent across instances, especially not when involving different SQLite versions and/or reorgs. (The documentation is not 100% clear in that respect, but it certainly does not clearly state that there is a deterministic and fixed algorithm.)
Thus, automatically assigned values should be completely avoided in our blockchain context. Thus, we should:
-
Provide a mechanism to generate unique integer keys in a consistent way. For this, we can simply use an explicit table that
SQLIteGamemanages, and provide users a function to generate the next ID and possibly to pre-reserve a range of IDs (e.g. for static data that is inserted as part of the initial data). This can also be cached in memory, so that we only ever sync it back to the database when a transaction is committed (rather than every time an ID is requested). -
Encourage (or perhaps enforce) the use of
WITHOUT ROWIDtables. This, in turn, enforces thatPRIMARY KEYvalues are explicit assigned at all times, so that no accidental reliance onrowid's is introduced.
Note that the session extension (which handles reorgs for us) only supports WITHOUT ROWID tables starting from SQLite version 3.17.0, so we will have to require at least that version. This excludes Debian 9 "Stretch", but includes current Ubuntu and the next Debian "Buster". I think that's an acceptable requirement.