@powersync/service-module-postgres-storage 0.17.0 → 0.19.0

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.
Files changed (69) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/dist/migrations/scripts/1784900000000-source-metadata.d.ts +3 -0
  3. package/dist/migrations/scripts/1784900000000-source-metadata.js +22 -0
  4. package/dist/migrations/scripts/1784900000000-source-metadata.js.map +1 -0
  5. package/dist/migrations/scripts/1788000000000-sync-config-version-label.d.ts +3 -0
  6. package/dist/migrations/scripts/1788000000000-sync-config-version-label.js +12 -0
  7. package/dist/migrations/scripts/1788000000000-sync-config-version-label.js.map +1 -0
  8. package/dist/storage/PostgresBucketStorageFactory.d.ts +1 -0
  9. package/dist/storage/PostgresBucketStorageFactory.js +14 -7
  10. package/dist/storage/PostgresBucketStorageFactory.js.map +1 -1
  11. package/dist/storage/PostgresStorageProvider.js +2 -1
  12. package/dist/storage/PostgresStorageProvider.js.map +1 -1
  13. package/dist/storage/PostgresSyncRulesStorage.d.ts +4 -3
  14. package/dist/storage/PostgresSyncRulesStorage.js +17 -5
  15. package/dist/storage/PostgresSyncRulesStorage.js.map +1 -1
  16. package/dist/storage/batch/PostgresBucketBatch.d.ts +2 -2
  17. package/dist/storage/batch/PostgresBucketBatch.js +63 -94
  18. package/dist/storage/batch/PostgresBucketBatch.js.map +1 -1
  19. package/dist/storage/checkpoints/PostgresWriteCheckpointAPI.d.ts +1 -1
  20. package/dist/storage/checkpoints/PostgresWriteCheckpointAPI.js +2 -2
  21. package/dist/storage/checkpoints/PostgresWriteCheckpointAPI.js.map +1 -1
  22. package/dist/storage/sync-rules/PostgresPersistedSyncConfigContent.js +5 -1
  23. package/dist/storage/sync-rules/PostgresPersistedSyncConfigContent.js.map +1 -1
  24. package/dist/types/models/ActiveCheckpoint.d.ts +2 -2
  25. package/dist/types/models/ActiveCheckpoint.js +3 -2
  26. package/dist/types/models/ActiveCheckpoint.js.map +1 -1
  27. package/dist/types/models/ActiveCheckpointNotification.d.ts +2 -2
  28. package/dist/types/models/BucketData.d.ts +6 -6
  29. package/dist/types/models/BucketData.js +7 -6
  30. package/dist/types/models/BucketData.js.map +1 -1
  31. package/dist/types/models/CurrentData.d.ts +1 -1
  32. package/dist/types/models/CurrentData.js +2 -1
  33. package/dist/types/models/CurrentData.js.map +1 -1
  34. package/dist/types/models/SdkReporting.d.ts +3 -3
  35. package/dist/types/models/SdkReporting.js +3 -5
  36. package/dist/types/models/SdkReporting.js.map +1 -1
  37. package/dist/types/models/SourceTable.d.ts +13 -8
  38. package/dist/types/models/SourceTable.js +11 -6
  39. package/dist/types/models/SourceTable.js.map +1 -1
  40. package/dist/types/models/SyncRules.d.ts +11 -10
  41. package/dist/types/models/SyncRules.js +12 -11
  42. package/dist/types/models/SyncRules.js.map +1 -1
  43. package/dist/types/models/WriteCheckpoint.d.ts +2 -2
  44. package/dist/types/models/WriteCheckpoint.js +3 -2
  45. package/dist/types/models/WriteCheckpoint.js.map +1 -1
  46. package/dist/types/types.d.ts +2 -0
  47. package/package.json +8 -8
  48. package/src/migrations/scripts/1784900000000-source-metadata.ts +31 -0
  49. package/src/migrations/scripts/1788000000000-sync-config-version-label.ts +18 -0
  50. package/src/storage/PostgresBucketStorageFactory.ts +15 -7
  51. package/src/storage/PostgresStorageProvider.ts +2 -1
  52. package/src/storage/PostgresSyncRulesStorage.ts +21 -6
  53. package/src/storage/batch/PostgresBucketBatch.ts +67 -98
  54. package/src/storage/checkpoints/PostgresWriteCheckpointAPI.ts +2 -2
  55. package/src/storage/sync-rules/PostgresPersistedSyncConfigContent.ts +8 -1
  56. package/src/types/models/ActiveCheckpoint.ts +3 -2
  57. package/src/types/models/BucketData.ts +7 -6
  58. package/src/types/models/CurrentData.ts +2 -1
  59. package/src/types/models/SdkReporting.ts +4 -4
  60. package/src/types/models/SourceTable.ts +12 -6
  61. package/src/types/models/SyncRules.ts +12 -11
  62. package/src/types/models/WriteCheckpoint.ts +3 -2
  63. package/test/src/__snapshots__/storage_sync.test.ts.snap +0 -995
  64. package/test/src/checkpoint_notifications.test.ts +8 -2
  65. package/test/src/storage.test.ts +12 -4
  66. package/test/src/storage_compacting.test.ts +2 -2
  67. package/test/src/storage_sync.test.ts +44 -0
  68. package/test/src/util.ts +1 -1
  69. package/tsconfig.tsbuildinfo +1 -1
@@ -44,7 +44,10 @@ bucket_definitions:
44
44
  const abortController = new AbortController();
45
45
  context.onTestFinished(() => abortController.abort());
46
46
  const iterator = bucketStorage
47
- .watchCheckpointChanges({ user_id: 'user', signal: abortController.signal })
47
+ .watchCheckpointChanges({
48
+ user_id: 'user',
49
+ signal: abortController.signal
50
+ })
48
51
  [Symbol.asyncIterator]();
49
52
 
50
53
  await expect(
@@ -149,7 +152,10 @@ bucket_definitions:
149
152
  const abortController = new AbortController();
150
153
  context.onTestFinished(() => abortController.abort());
151
154
  const iterator = bucketStorage
152
- .watchCheckpointChanges({ user_id: 'user', signal: abortController.signal })
155
+ .watchCheckpointChanges({
156
+ user_id: 'user',
157
+ signal: abortController.signal
158
+ })
153
159
  [Symbol.asyncIterator]();
154
160
 
155
161
  await expect(
@@ -1,12 +1,13 @@
1
1
  import { framework, storage, updateSyncRulesFromYaml } from '@powersync/service-core';
2
- import { bucketRequest, register, test_utils } from '@powersync/service-core-tests';
2
+ import { bucketRequest, compactActive, register, test_utils } from '@powersync/service-core-tests';
3
+ import { orNull } from '@powersync/service-types';
3
4
  import * as t from 'ts-codec';
4
5
  import { describe, expect, test } from 'vitest';
5
6
  import { CLEAR_BATCH_LIMIT } from '../../src/storage/PostgresSyncRulesStorage.js';
6
7
  import { POSTGRES_STORAGE_FACTORY, TEST_STORAGE_VERSIONS } from './util.js';
7
8
 
8
9
  const CheckpointRequestedAtRow = t.object({
9
- checkpoint_requested_at: t.Null.or(framework.codecs.date)
10
+ checkpoint_requested_at: orNull(framework.codecs.date)
10
11
  });
11
12
 
12
13
  describe('Sync Bucket Validation', register.registerBucketValidationTests);
@@ -105,7 +106,12 @@ bucket_definitions:
105
106
  WHERE
106
107
  user_id = 'user2'
107
108
  `.execute();
108
- await bucketStorage.compact({
109
+ {
110
+ await using activationWriter = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
111
+ await activationWriter.markAllSnapshotDone('1/1');
112
+ await activationWriter.keepalive('1/1');
113
+ }
114
+ await compactActive(factory, {
109
115
  compactBuckets: [],
110
116
  deleteCheckpointRequestsBefore: new Date('2024-02-01T00:00:00.000Z')
111
117
  });
@@ -126,7 +132,9 @@ bucket_definitions:
126
132
  .first()
127
133
  )?.checkpoint_requested_at;
128
134
 
129
- bucketStorage.setWriteCheckpointMode(storage.WriteCheckpointMode.CUSTOM);
135
+ bucketStorage.setWriteCheckpointMode({
136
+ mode: storage.WriteCheckpointMode.CUSTOM
137
+ });
130
138
  await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
131
139
  await writer.markAllSnapshotDone('1/1');
132
140
  const customCheckpointRequestedAt = new Date('2024-01-01T00:00:00.000Z');
@@ -1,5 +1,5 @@
1
1
  import { storage, updateSyncRulesFromYaml } from '@powersync/service-core';
2
- import { bucketRequest, register, test_utils } from '@powersync/service-core-tests';
2
+ import { bucketRequest, compactActive, register, test_utils } from '@powersync/service-core-tests';
3
3
  import { describe, expect, test } from 'vitest';
4
4
  import { PostgresCompactor } from '../../src/storage/PostgresCompactor.js';
5
5
  import { POSTGRES_STORAGE_FACTORY } from './util.js';
@@ -44,7 +44,7 @@ bucket_definitions:
44
44
 
45
45
  // Compact with an explicit bucket name — exercises the this.buckets
46
46
  // iteration path, NOT the compactAllBuckets discovery path.
47
- await bucketStorage.compact({
47
+ await compactActive(factory, {
48
48
  compactBuckets: [bucketRequest(syncRulesContent, 'global[]').bucket],
49
49
  minBucketChanges: 1
50
50
  });
@@ -17,6 +17,50 @@ function registerStorageVersionTests(storageVersion: number) {
17
17
  tableIdStrings: storageFactory.tableIdStrings
18
18
  });
19
19
 
20
+ test('updates source metadata on an existing resolved table', async () => {
21
+ await using factory = await storageFactory.factory();
22
+ const syncRules = await factory.updateSyncRules(
23
+ updateSyncRulesFromYaml(
24
+ `
25
+ bucket_definitions:
26
+ global:
27
+ data:
28
+ - SELECT id FROM test
29
+ `,
30
+ { storageVersion }
31
+ )
32
+ );
33
+ const bucketStorage = factory.getInstance(syncRules);
34
+ await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
35
+ const source: storage.SourceEntityDescriptor = {
36
+ connectionTag: storage.SourceTable.DEFAULT_TAG,
37
+ objectId: 'test',
38
+ schema: 'public',
39
+ name: 'test',
40
+ replicaIdColumns: [{ name: 'id', type: 'VARCHAR', typeId: 25 }]
41
+ };
42
+
43
+ const initial = await writer.resolveTables({ connection_id: 1, source });
44
+ expect(initial.tables[0].sourceMetadata).toBeNull();
45
+
46
+ const sourceMetadata = { captureTableObjectId: 42 };
47
+ const updated = await writer.resolveTables({
48
+ connection_id: 1,
49
+ source,
50
+ reconcileSourceTables: ({ candidates }) => ({
51
+ compatibleTables: candidates.map((candidate) => candidate.withSourceMetadata(sourceMetadata)),
52
+ incompatibleTables: [],
53
+ newTableValues: { sourceMetadata }
54
+ })
55
+ });
56
+
57
+ expect(updated.tables.map((table) => table.id.toString())).toEqual(
58
+ initial.tables.map((table) => table.id.toString())
59
+ );
60
+ expect(updated.tables[0].sourceMetadata).toEqual(sourceMetadata);
61
+ expect((await writer.getSourceTableStatus(updated.tables[0]))?.sourceMetadata).toEqual(sourceMetadata);
62
+ });
63
+
20
64
  test('large batch (2)', async () => {
21
65
  // Test syncing a batch of data that is small in count,
22
66
  // but large enough in size to be split over multiple returned chunks.
package/test/src/util.ts CHANGED
@@ -36,4 +36,4 @@ export const POSTGRES_STORAGE_SETUP = postgresTestSetup({
36
36
  export const POSTGRES_STORAGE_FACTORY = POSTGRES_STORAGE_SETUP;
37
37
  export const POSTGRES_REPORT_STORAGE_FACTORY = POSTGRES_STORAGE_SETUP.reportFactory;
38
38
 
39
- export const TEST_STORAGE_VERSIONS = SUPPORTED_STORAGE_VERSIONS;
39
+ export const TEST_STORAGE_VERSIONS = SUPPORTED_STORAGE_VERSIONS.filter((v) => v < 3);