ADR 0005 — Stored data stays compatible; encoding fixes read through legacy forms
Status: Accepted
Context
Section titled “Context”The best driver copies (mostly en’s) change some on-disk or on-wire representations compared with the older copies:
- canonical JSON on write;
$escaping for unstorage;- empty-segment encoding for file keys.
The copies also have different persisted defaults:
- table names
xtandard_Xandxtandard_X_kv; - Mongo database and collection names;
- the pg notify channel;
- id formats: wh random base62, au/li ULID, en lowercase 22 characters.
Users upgrade a product in place, over existing data.
Decision
Section titled “Decision”-
Persisted defaults are product parameters (ADR 0002): table, database, collection, channel, key roots and id format. The kit has no defaults for these that a product relies on implicitly.
-
Encoding changes must read legacy data.
- unstorage: encode only a trailing
$, which is the case unstorage hides as a meta key. Reads fall back to the raw legacy key when the encoded key is missing. - file: empty segments are encoded as
%. Keys without empty segments map to the same paths as before. - wh’s
migrateFileStorageand fl’s legacy read-through remain available.
- unstorage: encode only a trailing
-
CAS stays correct on legacy values. Writes are canonical. CAS first tries a single-statement swap against the canonical form. On a miss, it reads the stored value, compares it structurally (
deepEqual, own properties), and, if they are equal, swaps conditionally on the raw stored value, with bounded retries. This covers rows written before canonicalisation: SQLite text, and Mongo BSON, where field order matters. -
DDL improvements are applied only to new tables:
COLLATE "C"on pg keys,utf8mb4_binon MySQL. Existing tables keep working, because prefix queries add an exactstartsWithpost-filter. Optional migration SQL is documented. -
MySQL key collation.
utf8mb4_binis PAD SPACE: keys that differ only by trailing spaces collide, and indexedLIKEcan skip supplementary characters. The kit’smysqlKvTablekeepsutf8mb4_binas its default, with an opt-inkeyCollation(utf8mb4_0900_binon MySQL 8.0+,utf8mb4_nopad_binon MariaDB). The reasons for keeping the default:- the table factory is a schema declaration and cannot know the server, and MariaDB and MySQL 5.7
lack
utf8mb4_0900_bin; - changing the default would make drizzle-kit generate an
ALTERfor every existing user.
For safety on every table, the MySQL driver guards reads, deletes and swaps with
BINARY key = BINARY ?, and turns an upsert that would collide into a clear error. New deployments are told, in docs, to use the NO PAD collation, with migration SQL for existing ones. A product may switch its own default in a MINOR release, with a CHANGELOG Migration note. - the table factory is a schema declaration and cannot know the server, and MariaDB and MySQL 5.7
lack
Consequences
Section titled “Consequences”-
Upgrading a product never makes existing keys unreadable or CAS operations fail permanently.
-
Stored-data fixtures are permanent kit tests (
test/fixtures/stored-data/). Each product’s legacy representations are captured as fixture data:- pre-encoding file trees (wh v0.1.4 and earlier, fl);
- unstorage keys containing
$,:,%,?and\; - non-canonical JSON rows for sqlite/libsql/postgres/mysql/mongo;
- RedisJSON and string-codec values;
- each product’s default table, collection and channel names.
The fixtures are written directly through each backend. The kit must then read, list and CAS them. A future kit change that orphans old keys fails CI.