Schema migration silently drops documents with premium IndexedDB storage and `multiInstance: false`
Summary
With getRxStorageIndexedDB from rxdb-premium and a database created with multiInstance: false, migratePromise() loses documents during a schema migration. Once a collection needs three or more migration batches (default MIGRATION_DEFAULT_BATCH_SIZE = 200), up to two whole batches never reach the new storage instance. The migration still reports DONE / 100 % and raises no error.
The same migration keeps every document with:
multiInstance: true;- the Dexie or memory storage, in either mode;
- one large batch, e.g.
migratePromise(1_000_000).
We hit this in production: a Capacitor app runs multiInstance: false because a native WebView only ever has one instance. Users lost local data at every schema-bump app update. Replication checkpoints were already past the dropped documents, so they were never pulled again.
Versions
rxdb17.5.0,rxdb-premium17.5.0- Reproduced in Node with
fake-indexeddb6.2.5, in desktop Chromium 153, and in Android System WebView 153 (Capacitor app, Android 16 emulator)
Reproduction
import fakeIndexedDB, { IDBKeyRange } from "fake-indexeddb";
import { addRxPlugin, createRxDatabase } from "rxdb";
import { RxDBDevModePlugin } from "rxdb/plugins/dev-mode";
import { RxDBMigrationSchemaPlugin } from "rxdb/plugins/migration-schema";
import { wrappedValidateAjvStorage } from "rxdb/plugins/validate-ajv";
import { getRxStorageIndexedDB } from "rxdb-premium/plugins/storage-indexeddb";
addRxPlugin(RxDBDevModePlugin);
addRxPlugin(RxDBMigrationSchemaPlugin);
const schema = (version: number) => ({
version,
primaryKey: "id",
type: "object",
properties: { id: { type: "string", maxLength: 100 }, n: { type: "number" } },
required: ["id"],
});
async function run(total: number, multiInstance: boolean) {
const name = `mig-${total}-${multiInstance}`;
const storage = wrappedValidateAjvStorage({
storage: getRxStorageIndexedDB({ indexedDB: fakeIndexedDB, IDBKeyRange }),
});
const v0 = await createRxDatabase({ name, storage, multiInstance });
await v0.addCollections({ docs: { schema: schema(0) } });
for (let i = 0; i < total; i += 100) {
await v0.docs.bulkInsert(
Array.from({ length: Math.min(100, total - i) }, (_, j) => ({
id: `d${String(i + j).padStart(5, "0")}`,
n: i + j,
})),
);
}
await v0.close();
const v1 = await createRxDatabase({ name, storage, multiInstance });
await v1.addCollections({
docs: {
schema: schema(1),
autoMigrate: false,
migrationStrategies: { 1: (doc: any) => doc },
},
});
await v1.docs.getMigrationState().migratePromise();
const count = await v1.docs.count().exec();
await v1.close();
return count;
}
for (const total of [400, 600, 826, 1000, 2000]) {
console.log(total, await run(total, false), await run(total, true));
}Results
Documents kept after the migration:
| Documents | premium IndexedDB, multiInstance: false |
premium IndexedDB, multiInstance: true |
Dexie (either) | Memory (either) |
|---|---|---|---|---|
| 199–401 | all | all | all | all |
| 600 | 400 | 600 | 600 | 600 |
| 601 | 401 | 601 | 601 | 601 |
| 826 | 426 | 826 | 826 | 826 |
| 1000 | 600 | 1000 | 1000 | 1000 |
| 2000 | 1600 | 2000 | 2000 | 2000 |
What we observed
- Sorting the old documents by
_meta.lwtin the 826-document case, the new storage holds the first 200 and the last 226. The 400 in between (batches 2 and 3 of the push replication inmigrateStorage) are missing. - The migration status reaches 100 %, and
replicationState.events.errornever fires. - The old
…-<version>-documentsobject store is emptied as usual, so the dropped documents are gone. - Passing a batch size larger than the collection (
migratePromise(1_000_000)) avoids the loss. That is our current workaround.
The one code path we found that depends on multiInstance in runMigration is how the old storage instance is opened (multiInstance: this.database.multiInstance), plus the leader election being skipped. So the storage's single-instance mode is probably where the difference lies, for example getChangedDocumentsSince or cached writes when old and new instances share one IndexedDB database.
Expected
A schema migration keeps every document regardless of multiInstance, or fails loudly.
Source: pubkey/rxdb