Skip to content

Commit 8582d11

Browse files
committed
Create index inside transaction.
1 parent d47a4b0 commit 8582d11

2 files changed

Lines changed: 8 additions & 12 deletions

File tree

modules/module-mongodb-storage/src/storage/implementation/v3/MongoBucketBatchV3.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ export class MongoBucketBatchV3 extends MongoBucketBatch {
155155
}));
156156

157157
let result: storage.ResolveTablesResult | null = null;
158-
let ensureSourceRecordIndexesFor: bson.ObjectId[] = [];
159158
const session = this.db.client.startSession();
160159
await using _ = { [Symbol.asyncDispose]: () => session.endSession() };
161160

@@ -239,6 +238,7 @@ export class MongoBucketBatchV3 extends MongoBucketBatch {
239238
sourceTable.storeCurrentData = sendsCompleteRows !== true;
240239

241240
await col.insertOne(createDoc, { session });
241+
await this.db.initializeSourceRecordsCollection(this.replicationStreamId, createDoc._id, session);
242242
retainedDocIds.push(createDoc._id);
243243
tables.push(sourceTable);
244244
}
@@ -254,7 +254,6 @@ export class MongoBucketBatchV3 extends MongoBucketBatch {
254254
table.syncEvent = table === eventCarrier;
255255
}
256256
}
257-
ensureSourceRecordIndexesFor = retainedDocIds;
258257

259258
const retainedDocIdStrings = new Set(retainedDocIds.map((id) => id.toHexString()));
260259
const conflictingTables = candidateDocs.filter(
@@ -269,14 +268,6 @@ export class MongoBucketBatchV3 extends MongoBucketBatch {
269268
};
270269
});
271270

272-
// Index creation is idempotent. Running it for all retained tables - not only newly
273-
// created ones - heals the index if an earlier resolveTables crashed between inserting
274-
// the document and reaching this point (this runs outside the session on purpose, since
275-
// index creation cannot be transactional).
276-
for (const sourceTableId of ensureSourceRecordIndexesFor) {
277-
await this.db.initializeSourceRecordsCollection(this.replicationStreamId, sourceTableId);
278-
}
279-
280271
return result!;
281272
}
282273

modules/module-mongodb-storage/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ export class VersionedPowerSyncMongoV3 extends BaseVersionedPowerSyncMongo {
2525
return this.listCollectionsByPrefix<CurrentDataDocumentV3>(`source_records_${replicationStreamId}_`);
2626
}
2727

28-
async initializeSourceRecordsCollection(replicationStreamId: number, sourceTableId: mongo.ObjectId) {
28+
async initializeSourceRecordsCollection(
29+
replicationStreamId: number,
30+
sourceTableId: mongo.ObjectId,
31+
session?: mongo.ClientSession
32+
) {
2933
await this.sourceRecordsV3(replicationStreamId, sourceTableId).createIndex(
3034
{
3135
pending_delete: 1
3236
},
3337
{
3438
partialFilterExpression: { pending_delete: { $exists: true } },
35-
name: 'pending_delete'
39+
name: 'pending_delete',
40+
session
3641
}
3742
);
3843
}

0 commit comments

Comments
 (0)