Skip to content

Commit

Permalink
Fix of the pglite-sync plugin for latest electric (#535)
Browse files Browse the repository at this point in the history
* Second fix of the pglite-sync plugin for latest electric

* working
  • Loading branch information
samwillis authored Feb 13, 2025
1 parent b5368a4 commit c489ec0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-frogs-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@electric-sql/pglite-sync': patch
---

Correctly persist the offset during initial sync
13 changes: 11 additions & 2 deletions packages/pglite-sync/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ async function createPlugin(
shapeKey: options.shapeKey,
shapeId: shapeHandle,
lastOffset: getMessageOffset(
stream,
messageAggregator[messageAggregator.length - 1],
),
})
Expand Down Expand Up @@ -673,10 +674,18 @@ function subscriptionMetadataTableName(metadatSchema: string) {

const subscriptionTableName = `shape_subscriptions_metadata`

function getMessageOffset(message: LegacyChangeMessage<any>): Offset {
function getMessageOffset(
stream: ShapeStream,
message: LegacyChangeMessage<any>,
): Offset {
if (message.offset) {
return message.offset
} else {
} else if (
message.headers.lsn !== undefined &&
message.headers.op_position !== undefined
) {
return `${message.headers.lsn}_${message.headers.op_position}` as Offset
} else {
return stream.lastOffset
}
}

0 comments on commit c489ec0

Please sign in to comment.