addCollections() throws DB6 for an unchanged schema after the device locale changes
Summary
addCollections() throws DB6 for an unchanged schema after the device or app language changes. The schema hash depends on the device locale.
Cause
normalizeRxJsonSchema() sorts the schema keys with sortObject(), which calls a.localeCompare(b) without a locale. localeCompare() then uses the collation of the current runtime locale. In Czech and Slovak collation "ch" is a single letter that sorts after "h":
| Collation | Order of answer, chunk_id, hint |
|---|---|
en, de, fr, ... |
answer, chunk_id, hint |
cs, sk |
answer, hint, chunk_id |
The JSON string differs, so the hash differs. The database stores the hash from the first run. When the locale changes, the next addCollections() call computes another hash and throws DB6, although nothing in the schema changed.
In our tests, other locales also change the key order for some key sets: cy, az, uz, sq, ha and haw.
Reproduction
const original = String.prototype.localeCompare;
String.prototype.localeCompare = function (that) {
return original.call(this, that, 'cs');
};
const db1 = await createRxDatabase({ name: 'test', storage });
await db1.addCollections({ items: { schema } }); // schema has the properties `hint` and `chunk_id`
String.prototype.localeCompare = original;
await db1.close();
const db2 = await createRxDatabase({ name: 'test', storage });
await db2.addCollections({ items: { schema } }); // throws DB6Where we saw it
A React Native app (Hermes, rxdb 17.2.0, SQLite storage) with a flashcards collection that has chunk_id and hint properties:
- Android 13+: users can set a per-app language. Switching the app language between Czech and another language reproduces
DB6on the next start. Reproduced on an Android 16 emulator withcmd locale set-app-locales. - iOS: the same
DB6happened for a user whose locale wascs_US. - Result: the app cannot open its local database, so users are stuck until the stored hash matches again.
Proposed fix
- Sort with a fixed collation:
a.localeCompare(b, 'en'). This keeps the existing hashes for all databases created with an English-like collation, which are most of them. A pure code unit comparison (a < b) would change the hash for common key pairs likecreatedAt/created_atandmaxLength/maximum, so every existing database would hitDB6. - In
addCollections(), when the stored hash differs but the stored schema isdeepEqual()to the current schema (same content, different key order), update the stored hash instead of throwingDB6. This covers databases that were created with a Czech or Slovak collation before the fix.
I can open a PR with both changes and tests.
Source: pubkey/rxdb