Skip to content

Commit 90db592

Browse files
committed
Test consistency of compact.
1 parent 440d32f commit 90db592

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

modules/module-mongodb-storage/src/storage/implementation/MongoParameterCompactor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export class MongoParameterCompactor {
6464
}
6565
const rawDoc: RawParameterData = {
6666
_id: doc._id,
67+
// Serializing to a Buffer is an easy way to check for exact equality of arbitrary BSON values.
6768
data: bson.serialize({
6869
key: doc.key,
6970
lookup: doc.lookup

modules/module-mongodb-storage/src/storage/implementation/MongoSyncBucketStorage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ export class MongoSyncBucketStorage
276276
// This is a roundabout way of setting {readConcern: {atClusterTime: clusterTime}}, since
277277
// that is not exposed directly by the driver.
278278
// Future versions of the driver may change the snapshotTime behavior, so we need tests to
279-
// validate that this works as expected.
279+
// validate that this works as expected. We test this in the compacting tests.
280280
setSessionSnapshotTime(session, checkpoint.clusterTime.clusterTime);
281281
const lookupFilter = lookups.map((lookup) => {
282282
return storage.serializeLookup(lookup);

packages/service-core-tests/src/tests/register-compacting-tests.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { storage } from '@powersync/service-core';
22
import { expect, test } from 'vitest';
33
import * as test_utils from '../test-utils/test-utils-index.js';
4+
import { ParameterLookup } from '@powersync/service-sync-rules';
45

56
const TEST_TABLE = test_utils.makeTestTable('test', ['id']);
67

@@ -442,4 +443,80 @@ bucket_definitions:
442443
])
443444
);
444445
});
446+
447+
test('compacting parameters', async () => {
448+
await using factory = await generateStorageFactory();
449+
const syncRules = await factory.updateSyncRules({
450+
content: `
451+
bucket_definitions:
452+
test:
453+
parameters: select id from test where id = request.user_id()
454+
data: []
455+
`
456+
});
457+
const bucketStorage = factory.getInstance(syncRules);
458+
459+
await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
460+
await batch.save({
461+
sourceTable: TEST_TABLE,
462+
tag: storage.SaveOperationTag.INSERT,
463+
after: {
464+
id: 't1'
465+
},
466+
afterReplicaId: 't1'
467+
});
468+
469+
await batch.save({
470+
sourceTable: TEST_TABLE,
471+
tag: storage.SaveOperationTag.INSERT,
472+
after: {
473+
id: 't2'
474+
},
475+
afterReplicaId: 't2'
476+
});
477+
478+
await batch.commit('1/1');
479+
});
480+
481+
const lookup = ParameterLookup.normalized('test', '1', ['t1']);
482+
483+
const checkpoint1 = await bucketStorage.getCheckpoint();
484+
const parameters1 = await checkpoint1.getParameterSets([lookup]);
485+
expect(parameters1).toEqual([{ id: 't1' }]);
486+
487+
await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
488+
await batch.save({
489+
sourceTable: TEST_TABLE,
490+
tag: storage.SaveOperationTag.UPDATE,
491+
before: {
492+
id: 't1'
493+
},
494+
beforeReplicaId: 't1',
495+
after: {
496+
id: 't1'
497+
},
498+
afterReplicaId: 't1'
499+
});
500+
501+
await batch.save({
502+
sourceTable: TEST_TABLE,
503+
tag: storage.SaveOperationTag.DELETE,
504+
before: {
505+
id: 't1'
506+
},
507+
beforeReplicaId: 't1'
508+
});
509+
await batch.commit('1/2');
510+
});
511+
const checkpoint2 = await bucketStorage.getCheckpoint();
512+
const parameters2 = await checkpoint2.getParameterSets([lookup]);
513+
expect(parameters2).toEqual([]);
514+
515+
await bucketStorage.compact({ compactParameterData: true });
516+
517+
const parameters1b = await checkpoint1.getParameterSets([lookup]);
518+
const parameters2b = await checkpoint2.getParameterSets([lookup]);
519+
expect(parameters1b).toEqual([{ id: 't1' }]);
520+
expect(parameters2b).toEqual([]);
521+
});
445522
}

0 commit comments

Comments
 (0)