@powersync/service-module-postgres-storage 0.7.2 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -208,13 +208,7 @@ export class PostgresBucketBatch
208
208
  return null;
209
209
  }
210
210
 
211
- const currentSequence = await this.db.sql`
212
- SELECT
213
- LAST_VALUE AS value
214
- FROM
215
- op_id_sequence;
216
- `.first<{ value: bigint }>();
217
- return currentSequence!.value;
211
+ return this.getLastOpIdSequence(this.db);
218
212
  }
219
213
 
220
214
  async drop(sourceTables: storage.SourceTable[]): Promise<storage.FlushedResult | null> {
@@ -262,13 +256,7 @@ export class PostgresBucketBatch
262
256
  const lastOp = await this.withReplicationTransaction(async (db) => {
263
257
  resumeBatch = await this.replicateBatch(db, batch);
264
258
 
265
- const sequence = await db.sql`
266
- SELECT
267
- LAST_VALUE AS value
268
- FROM
269
- op_id_sequence;
270
- `.first<{ value: bigint }>();
271
- return sequence!.value;
259
+ return this.getLastOpIdSequence(db);
272
260
  });
273
261
 
274
262
  // null if done, set if we need another flush
@@ -895,6 +883,23 @@ export class PostgresBucketBatch
895
883
  `.execute();
896
884
  }
897
885
  }
886
+
887
+ private async getLastOpIdSequence(db: lib_postgres.AbstractPostgresConnection) {
888
+ // When no op_id has been generated, last_value = 1 and nextval() will be 1.
889
+ // To cater for this case, we check is_called, and default to 0 if no value has been generated.
890
+ const sequence = await db.sql`
891
+ SELECT
892
+ (
893
+ CASE
894
+ WHEN is_called THEN last_value
895
+ ELSE 0
896
+ END
897
+ ) AS value
898
+ FROM
899
+ op_id_sequence;
900
+ `.first<{ value: bigint }>();
901
+ return sequence!.value;
902
+ }
898
903
  }
899
904
 
900
905
  /**
@@ -93,7 +93,11 @@ describe('Postgres Sync Bucket Storage', () => {
93
93
  });
94
94
 
95
95
  const batch2 = await test_utils.fromAsync(
96
- bucketStorage.getBucketDataBatch(checkpoint, new Map([['global[]', BigInt(batch1[0].batch.next_after)]]), options)
96
+ bucketStorage.getBucketDataBatch(
97
+ checkpoint,
98
+ new Map([['global[]', BigInt(batch1[0].chunkData.next_after)]]),
99
+ options
100
+ )
97
101
  );
98
102
  expect(test_utils.getBatchData(batch2)).toEqual([
99
103
  { op_id: '2', op: 'PUT', object_id: 'large1', checksum: 1178768505 }
@@ -105,7 +109,11 @@ describe('Postgres Sync Bucket Storage', () => {
105
109
  });
106
110
 
107
111
  const batch3 = await test_utils.fromAsync(
108
- bucketStorage.getBucketDataBatch(checkpoint, new Map([['global[]', BigInt(batch2[0].batch.next_after)]]), options)
112
+ bucketStorage.getBucketDataBatch(
113
+ checkpoint,
114
+ new Map([['global[]', BigInt(batch2[0].chunkData.next_after)]]),
115
+ options
116
+ )
109
117
  );
110
118
  expect(test_utils.getBatchData(batch3)).toEqual([
111
119
  { op_id: '3', op: 'PUT', object_id: 'large2', checksum: 1607205872 }
@@ -117,7 +125,11 @@ describe('Postgres Sync Bucket Storage', () => {
117
125
  });
118
126
 
119
127
  const batch4 = await test_utils.fromAsync(
120
- bucketStorage.getBucketDataBatch(checkpoint, new Map([['global[]', BigInt(batch3[0].batch.next_after)]]), options)
128
+ bucketStorage.getBucketDataBatch(
129
+ checkpoint,
130
+ new Map([['global[]', BigInt(batch3[0].chunkData.next_after)]]),
131
+ options
132
+ )
121
133
  );
122
134
  expect(test_utils.getBatchData(batch4)).toEqual([
123
135
  { op_id: '4', op: 'PUT', object_id: 'test3', checksum: 1359888332 }