Skip to content

Commit c489ec0

Browse files
authored
Fix of the pglite-sync plugin for latest electric (#535)
* Second fix of the pglite-sync plugin for latest electric * working
1 parent b5368a4 commit c489ec0

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

.changeset/beige-frogs-film.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@electric-sql/pglite-sync': patch
3+
---
4+
5+
Correctly persist the offset during initial sync

packages/pglite-sync/src/index.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ async function createPlugin(
253253
shapeKey: options.shapeKey,
254254
shapeId: shapeHandle,
255255
lastOffset: getMessageOffset(
256+
stream,
256257
messageAggregator[messageAggregator.length - 1],
257258
),
258259
})
@@ -673,10 +674,18 @@ function subscriptionMetadataTableName(metadatSchema: string) {
673674

674675
const subscriptionTableName = `shape_subscriptions_metadata`
675676

676-
function getMessageOffset(message: LegacyChangeMessage<any>): Offset {
677+
function getMessageOffset(
678+
stream: ShapeStream,
679+
message: LegacyChangeMessage<any>,
680+
): Offset {
677681
if (message.offset) {
678682
return message.offset
679-
} else {
683+
} else if (
684+
message.headers.lsn !== undefined &&
685+
message.headers.op_position !== undefined
686+
) {
680687
return `${message.headers.lsn}_${message.headers.op_position}` as Offset
688+
} else {
689+
return stream.lastOffset
681690
}
682691
}

0 commit comments

Comments
 (0)