@powersync/service-core 0.0.0-dev-20261005120023 → 0.0.0-dev-20261007110800
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.
- package/CHANGELOG.md +22 -11
- package/dist/api/RouteAPI.d.ts +8 -1
- package/dist/api/api-index.d.ts +1 -0
- package/dist/api/api-index.js +1 -0
- package/dist/api/api-index.js.map +1 -1
- package/dist/api/diagnostics.js +28 -14
- package/dist/api/diagnostics.js.map +1 -1
- package/dist/api/source-capabilities.d.ts +12 -0
- package/dist/api/source-capabilities.js +27 -0
- package/dist/api/source-capabilities.js.map +1 -0
- package/dist/replication/AbstractReplicator.d.ts +6 -0
- package/dist/replication/AbstractReplicator.js +12 -2
- package/dist/replication/AbstractReplicator.js.map +1 -1
- package/dist/replication/ReplicationModule.js +10 -2
- package/dist/replication/ReplicationModule.js.map +1 -1
- package/dist/routes/configure-fastify.d.ts +24 -24
- package/dist/routes/endpoints/admin.d.ts +48 -48
- package/dist/routes/endpoints/admin.js +8 -8
- package/dist/routes/endpoints/admin.js.map +1 -1
- package/dist/routes/endpoints/sync-rules.js +51 -14
- package/dist/routes/endpoints/sync-rules.js.map +1 -1
- package/dist/storage/BucketStorageFactory.d.ts +11 -13
- package/dist/storage/BucketStorageFactory.js +21 -11
- package/dist/storage/BucketStorageFactory.js.map +1 -1
- package/dist/storage/PersistedReplicationStream.d.ts +1 -2
- package/dist/storage/PersistedReplicationStream.js.map +1 -1
- package/dist/storage/PersistedSyncConfigContent.d.ts +4 -4
- package/dist/storage/PersistedSyncConfigContent.js +5 -12
- package/dist/storage/PersistedSyncConfigContent.js.map +1 -1
- package/dist/storage/StorageEngine.d.ts +0 -4
- package/dist/storage/StorageEngine.js +1 -7
- package/dist/storage/StorageEngine.js.map +1 -1
- package/dist/storage/StorageProvider.d.ts +1 -6
- package/dist/storage/SyncRulesBucketStorage.d.ts +1 -1
- package/dist/storage/storage-index.d.ts +0 -1
- package/dist/storage/storage-index.js +0 -1
- package/dist/storage/storage-index.js.map +1 -1
- package/dist/system/ServiceContext.d.ts +0 -20
- package/dist/system/ServiceContext.js +1 -29
- package/dist/system/ServiceContext.js.map +1 -1
- package/package.json +11 -11
- package/src/api/RouteAPI.ts +9 -1
- package/src/api/api-index.ts +1 -0
- package/src/api/diagnostics.ts +24 -12
- package/src/api/source-capabilities.ts +31 -0
- package/src/replication/AbstractReplicator.ts +14 -2
- package/src/replication/ReplicationModule.ts +9 -2
- package/src/routes/endpoints/admin.ts +14 -6
- package/src/routes/endpoints/sync-rules.ts +52 -16
- package/src/storage/BucketStorageFactory.ts +21 -19
- package/src/storage/PersistedReplicationStream.ts +1 -2
- package/src/storage/PersistedSyncConfigContent.ts +9 -16
- package/src/storage/StorageEngine.ts +1 -10
- package/src/storage/StorageProvider.ts +1 -6
- package/src/storage/SyncRulesBucketStorage.ts +1 -1
- package/src/storage/storage-index.ts +0 -1
- package/src/system/ServiceContext.ts +1 -47
- package/test/src/AbstractReplicator.test.ts +94 -23
- package/test/src/PersistedSyncConfigContent.test.ts +1 -3
- package/test/src/diagnostics.test.ts +71 -0
- package/test/src/routes/admin.test.ts +205 -11
- package/test/src/routes/mocks.ts +0 -2
- package/test/src/routes/sync-rules.test.ts +76 -0
- package/test/src/source-capabilities.test.ts +42 -0
- package/test/src/source_table_config.test.ts +55 -0
- package/test/src/storage/BucketStorageFactory.test.ts +46 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/storage/SyncConfigParser.d.ts +0 -44
- package/dist/storage/SyncConfigParser.js +0 -73
- package/dist/storage/SyncConfigParser.js.map +0 -1
- package/src/storage/SyncConfigParser.ts +0 -124
- package/test/src/SyncConfigParser.test.ts +0 -309
package/src/api/diagnostics.ts
CHANGED
|
@@ -85,20 +85,32 @@ export async function getSyncRulesStatus(
|
|
|
85
85
|
let replication_lag_bytes: number | undefined = undefined;
|
|
86
86
|
let slot_wal_budget: SlotWalBudgetInfo | undefined = undefined;
|
|
87
87
|
|
|
88
|
+
const validationErrors: ReplicationError[] = [];
|
|
88
89
|
let tables_flat: TableInfo[] = [];
|
|
89
90
|
|
|
90
91
|
if (check_connection) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
92
|
+
try {
|
|
93
|
+
validationErrors.push(...((await apiHandler.validateSourceCapabilities?.(rules)) ?? []));
|
|
94
|
+
} catch (e) {
|
|
95
|
+
validationErrors.push({ level: 'fatal', message: e.message, ts: now });
|
|
96
|
+
}
|
|
97
|
+
// Table inspection is independent of source capability checks. Preserve both failures
|
|
98
|
+
// and continue collecting diagnostics that do not depend on table metadata.
|
|
99
|
+
try {
|
|
100
|
+
const source_table_patterns = rules.getSourceTables();
|
|
101
|
+
const resolved_tables = await apiHandler.getDebugTablesInfo(source_table_patterns, rules);
|
|
102
|
+
tables_flat = resolved_tables.flatMap((info) => {
|
|
103
|
+
if (info.table) {
|
|
104
|
+
return [info.table];
|
|
105
|
+
} else if (info.tables) {
|
|
106
|
+
return info.tables;
|
|
107
|
+
} else {
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
} catch (e) {
|
|
112
|
+
validationErrors.push({ level: 'fatal', message: e.message, ts: now });
|
|
113
|
+
}
|
|
102
114
|
|
|
103
115
|
if (systemStorage) {
|
|
104
116
|
try {
|
|
@@ -155,7 +167,7 @@ export async function getSyncRulesStatus(
|
|
|
155
167
|
});
|
|
156
168
|
}
|
|
157
169
|
|
|
158
|
-
const errors = tables_flat.flatMap((info) => info.errors);
|
|
170
|
+
const errors = [...validationErrors, ...tables_flat.flatMap((info) => info.errors)];
|
|
159
171
|
const statusSource = await syncConfig.getSyncConfigStatus();
|
|
160
172
|
|
|
161
173
|
if (statusSource?.last_fatal_error) {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { hasMongoFilterExpressions, PrecompiledSyncConfig, type SyncConfig } from '@powersync/service-sync-rules';
|
|
2
|
+
import type { ValidationDiagnostic } from '@powersync/service-types';
|
|
3
|
+
import type { RouteAPI } from './RouteAPI.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Blocks deployment and reprocessing on fatal source diagnostics; warnings remain advisory.
|
|
7
|
+
* Unexpected failures from the adapter propagate to the caller.
|
|
8
|
+
*/
|
|
9
|
+
export async function assertSourceCapabilities(api: RouteAPI, config: SyncConfig): Promise<void> {
|
|
10
|
+
const diagnostics = (await api.validateSourceCapabilities?.(config)) ?? [];
|
|
11
|
+
const errors = diagnostics.filter((diagnostic) => diagnostic.level === 'fatal');
|
|
12
|
+
if (errors.length > 0) throw new Error(errors.map((error) => error.message).join('\n'));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Reject MongoDB pre-filtering options on source adapters that cannot execute them.
|
|
17
|
+
*/
|
|
18
|
+
export function validateNoMongoFilterExpressions(config: SyncConfig, connectionTag: string): ValidationDiagnostic[] {
|
|
19
|
+
if (
|
|
20
|
+
config instanceof PrecompiledSyncConfig &&
|
|
21
|
+
hasMongoFilterExpressions(config.plan.sourceTableConfig ?? {}, connectionTag)
|
|
22
|
+
) {
|
|
23
|
+
return [
|
|
24
|
+
{
|
|
25
|
+
level: 'fatal',
|
|
26
|
+
message: 'MongoDB replication pre-filtering expressions are only supported by MongoDB sources.'
|
|
27
|
+
}
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { container, ErrorCode, logger, ReplicationAbortedError } from '@powersync/lib-services-framework';
|
|
2
|
+
import { SqlSyncRules, type SyncConfig } from '@powersync/service-sync-rules';
|
|
2
3
|
import { ReplicationMetric } from '@powersync/service-types';
|
|
3
4
|
import { hrtime } from 'node:process';
|
|
4
5
|
import { setTimeout as sleep } from 'node:timers/promises';
|
|
@@ -73,6 +74,8 @@ export abstract class AbstractReplicator<T extends AbstractReplicationJob = Abst
|
|
|
73
74
|
|
|
74
75
|
private abortController: AbortController | undefined;
|
|
75
76
|
|
|
77
|
+
private assertSourceCapabilities: ((config: SyncConfig) => Promise<void>) | undefined;
|
|
78
|
+
|
|
76
79
|
protected constructor(private options: AbstractReplicatorOptions) {
|
|
77
80
|
this.logger = logger.child({ name: `Replicator:${options.id}` });
|
|
78
81
|
const heartbeatIntervalSeconds = options.heartbeatIntervalSeconds ?? DEFAULT_HEARTBEAT_INTERVAL_SECONDS;
|
|
@@ -124,6 +127,13 @@ export abstract class AbstractReplicator<T extends AbstractReplicationJob = Abst
|
|
|
124
127
|
return this.abortController?.signal.aborted;
|
|
125
128
|
}
|
|
126
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Register an assertion that blocks unsupported file-loaded sync configs before persistence.
|
|
132
|
+
*/
|
|
133
|
+
public registerSourceCapabilitiesAssertion(assertion: (config: SyncConfig) => Promise<void>): void {
|
|
134
|
+
this.assertSourceCapabilities = assertion;
|
|
135
|
+
}
|
|
136
|
+
|
|
127
137
|
public async start(): Promise<void> {
|
|
128
138
|
this.abortController = new AbortController();
|
|
129
139
|
this.runLoop().catch((e) => {
|
|
@@ -172,11 +182,13 @@ export abstract class AbstractReplicator<T extends AbstractReplicationJob = Abst
|
|
|
172
182
|
// versions before that check was added, so we keep the lock for now - for where te service version and sync config is updated at
|
|
173
183
|
// the same time.
|
|
174
184
|
|
|
175
|
-
const parsed =
|
|
185
|
+
const parsed = SqlSyncRules.fromYaml(loadedSyncConfig, {
|
|
176
186
|
schema: undefined,
|
|
177
187
|
defaultSchema: this.defaultSchema,
|
|
178
188
|
throwOnError: this.syncRuleProvider.exitOnError
|
|
179
189
|
});
|
|
190
|
+
storage.logSyncConfigErrors(parsed, this.logger);
|
|
191
|
+
await this.assertSourceCapabilities?.(parsed.config);
|
|
180
192
|
const { lock } = await this.storage.configureSyncRules(
|
|
181
193
|
storage.updateSyncRulesFromConfig(parsed, {
|
|
182
194
|
lock: true,
|
|
@@ -188,7 +200,7 @@ export abstract class AbstractReplicator<T extends AbstractReplicationJob = Abst
|
|
|
188
200
|
}
|
|
189
201
|
} catch (e) {
|
|
190
202
|
// Log and re-raise to exit.
|
|
191
|
-
//
|
|
203
|
+
// Fatal source-capability diagnostics block deployment even when exit_on_error is false.
|
|
192
204
|
this.logger.error(`Failed to update sync config`, e);
|
|
193
205
|
throw e;
|
|
194
206
|
}
|
|
@@ -90,8 +90,15 @@ export abstract class ReplicationModule<TConfig extends types.configFile.DataSou
|
|
|
90
90
|
// If decoding fails, this will raise a hard error, and stop the service.
|
|
91
91
|
this.decodeConfig(baseMatchingConfig);
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
context.routerEngine?.registerAPI(
|
|
93
|
+
const sourceAPI = this.createRouteAPIAdapter();
|
|
94
|
+
context.routerEngine?.registerAPI(sourceAPI);
|
|
95
|
+
if (context.replicationEngine) {
|
|
96
|
+
const replicator = this.createReplicator(context);
|
|
97
|
+
// KLUDGE: Allows additional validation of filesystem-loaded sync configs before persistence.
|
|
98
|
+
// This could be replaced by a shared deployment validation flow in the future.
|
|
99
|
+
replicator.registerSourceCapabilitiesAssertion((config) => api.assertSourceCapabilities(sourceAPI, config));
|
|
100
|
+
context.replicationEngine.register(replicator);
|
|
101
|
+
}
|
|
95
102
|
}
|
|
96
103
|
|
|
97
104
|
protected decodeConfig(config: TConfig): void {
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import * as sqlite from 'node:sqlite';
|
|
2
2
|
|
|
3
3
|
import { ErrorCode, errors, router, schema } from '@powersync/lib-services-framework';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
HydratedSyncConfig,
|
|
6
|
+
nodeSqlite,
|
|
7
|
+
SourceSchema,
|
|
8
|
+
SqlSyncRules,
|
|
9
|
+
StaticSchema
|
|
10
|
+
} from '@powersync/service-sync-rules';
|
|
5
11
|
import { internal_routes } from '@powersync/service-types';
|
|
6
12
|
|
|
7
13
|
import { DEFAULT_HYDRATION_STATE } from '@powersync/service-sync-rules';
|
|
@@ -155,13 +161,16 @@ export const reprocess = routeDefinition({
|
|
|
155
161
|
// 1. This always re-parses the source YAML. If there are changes to the Sync Streams compiler, that can affect the sync plan.
|
|
156
162
|
// 2. If the source does not set the storage version, this will update it do the current version.
|
|
157
163
|
// We can consider tweaking this behavior in the future.
|
|
158
|
-
const parsed =
|
|
164
|
+
const parsed = SqlSyncRules.fromYaml(active.content.sync_rules_content, {
|
|
159
165
|
...apiHandler.getParseSyncRulesOptions(),
|
|
160
166
|
schema: undefined,
|
|
161
167
|
// This sync config already passed validation. But if the config is not valid anymore due
|
|
162
168
|
// to a service change, we do want to report the error here.
|
|
163
169
|
throwOnError: true
|
|
164
170
|
});
|
|
171
|
+
storage.logSyncConfigErrors(parsed);
|
|
172
|
+
await api.assertSourceCapabilities(apiHandler, parsed.config);
|
|
173
|
+
|
|
165
174
|
const new_rules = await activeBucketStorage.updateSyncRules(
|
|
166
175
|
storage.updateSyncRulesFromConfig(parsed, {
|
|
167
176
|
version_label: active.content.version_label,
|
|
@@ -186,15 +195,14 @@ export const reprocess = routeDefinition({
|
|
|
186
195
|
|
|
187
196
|
class FakeSyncRulesContentForValidation extends storage.PersistedSyncConfigContent {
|
|
188
197
|
constructor(
|
|
189
|
-
private readonly validationParser: storage.SyncConfigParser,
|
|
190
198
|
private readonly schema: SourceSchema,
|
|
191
199
|
data: storage.PersistedSyncConfigContentData
|
|
192
200
|
) {
|
|
193
|
-
super(data
|
|
201
|
+
super(data);
|
|
194
202
|
}
|
|
195
203
|
|
|
196
204
|
parsed(options: storage.ParseSyncConfigOptions): storage.ParsedSyncConfigSet {
|
|
197
|
-
const syncConfig =
|
|
205
|
+
const syncConfig = SqlSyncRules.fromYaml(this.sync_rules_content, {
|
|
198
206
|
...options,
|
|
199
207
|
schema: this.schema
|
|
200
208
|
});
|
|
@@ -235,7 +243,7 @@ export const validate = routeDefinition({
|
|
|
235
243
|
const schemaData = await api.getConnectionsSchema(apiHandler);
|
|
236
244
|
const schema = new StaticSchema(schemaData.connections);
|
|
237
245
|
|
|
238
|
-
const sync_rules = new FakeSyncRulesContentForValidation(
|
|
246
|
+
const sync_rules = new FakeSyncRulesContentForValidation(schema, {
|
|
239
247
|
// Dummy values
|
|
240
248
|
replicationStreamId: 0,
|
|
241
249
|
replicationStreamName: '',
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { ErrorCode, errors, router, schema } from '@powersync/lib-services-framework';
|
|
2
|
-
import { SyncConfigWithErrors, SyncRulesErrors } from '@powersync/service-sync-rules';
|
|
2
|
+
import { SqlSyncRules, SyncConfigWithErrors, SyncRulesErrors } from '@powersync/service-sync-rules';
|
|
3
3
|
import type { FastifyPluginAsync } from 'fastify';
|
|
4
4
|
import * as t from 'ts-codec';
|
|
5
|
+
import { assertSourceCapabilities } from '../../api/source-capabilities.js';
|
|
5
6
|
|
|
6
|
-
import { RouteAPI } from '../../api/RouteAPI.js';
|
|
7
|
-
import { updateSyncRulesFromConfig } from '../../storage/BucketStorageFactory.js';
|
|
8
|
-
import { SyncConfigParser } from '../../storage/SyncConfigParser.js';
|
|
7
|
+
import { PatternResult, RouteAPI } from '../../api/RouteAPI.js';
|
|
8
|
+
import { logSyncConfigErrors, updateSyncRulesFromConfig } from '../../storage/BucketStorageFactory.js';
|
|
9
9
|
import { authApi } from '../auth.js';
|
|
10
10
|
import { routeDefinition } from '../router.js';
|
|
11
11
|
|
|
@@ -56,8 +56,9 @@ export const deploySyncRules = routeDefinition({
|
|
|
56
56
|
let syncConfig: SyncConfigWithErrors;
|
|
57
57
|
|
|
58
58
|
try {
|
|
59
|
+
// First ensure the SyncConfig parses correctly
|
|
59
60
|
const apiHandler = service_context.routerEngine.getAPI();
|
|
60
|
-
syncConfig =
|
|
61
|
+
syncConfig = SqlSyncRules.fromYaml(content, {
|
|
61
62
|
...apiHandler.getParseSyncRulesOptions(),
|
|
62
63
|
// We don't do any schema-level validation at this point
|
|
63
64
|
schema: undefined
|
|
@@ -71,6 +72,20 @@ export const deploySyncRules = routeDefinition({
|
|
|
71
72
|
});
|
|
72
73
|
}
|
|
73
74
|
|
|
75
|
+
logSyncConfigErrors(syncConfig);
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
// Validate if the current sources support the configuration specified in the Sync Config
|
|
79
|
+
await assertSourceCapabilities(service_context.routerEngine.getAPI(), syncConfig.config);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
throw new errors.ServiceError({
|
|
82
|
+
status: 422,
|
|
83
|
+
code: ErrorCode.PSYNC_R0001,
|
|
84
|
+
description: 'Source capability validation failed',
|
|
85
|
+
details: error.message
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
74
89
|
const sync_rules = await storageEngine.activeBucketStorage.updateSyncRules(updateSyncRulesFromConfig(syncConfig));
|
|
75
90
|
|
|
76
91
|
return {
|
|
@@ -95,7 +110,7 @@ export const validateSyncRules = routeDefinition({
|
|
|
95
110
|
const { service_context } = payload.context;
|
|
96
111
|
const apiHandler = service_context.routerEngine.getAPI();
|
|
97
112
|
|
|
98
|
-
const info = await debugSyncRules(apiHandler, content
|
|
113
|
+
const info = await debugSyncRules(apiHandler, content);
|
|
99
114
|
|
|
100
115
|
return replyPrettyJson(info);
|
|
101
116
|
}
|
|
@@ -122,12 +137,10 @@ export const currentSyncRules = routeDefinition({
|
|
|
122
137
|
|
|
123
138
|
const sync_rules = active.content;
|
|
124
139
|
const apiHandler = service_context.routerEngine.getAPI();
|
|
125
|
-
const info = await debugSyncRules(apiHandler, sync_rules.sync_rules_content
|
|
140
|
+
const info = await debugSyncRules(apiHandler, sync_rules.sync_rules_content);
|
|
126
141
|
const next = await activeBucketStorage.getDeployingSyncConfig();
|
|
127
142
|
|
|
128
|
-
const next_info = next
|
|
129
|
-
? await debugSyncRules(apiHandler, next.content.sync_rules_content, service_context.syncConfigParser)
|
|
130
|
-
: null;
|
|
143
|
+
const next_info = next ? await debugSyncRules(apiHandler, next.content.sync_rules_content) : null;
|
|
131
144
|
|
|
132
145
|
const response = {
|
|
133
146
|
current: {
|
|
@@ -179,13 +192,16 @@ export const reprocessSyncRules = routeDefinition({
|
|
|
179
192
|
}
|
|
180
193
|
|
|
181
194
|
const sync_rules = active.content;
|
|
182
|
-
const parsed =
|
|
195
|
+
const parsed = SqlSyncRules.fromYaml(sync_rules.sync_rules_content, {
|
|
183
196
|
...payload.context.service_context.routerEngine.getAPI().getParseSyncRulesOptions(),
|
|
184
197
|
schema: undefined,
|
|
185
198
|
// This sync config already passed validation. But if the rules are not valid anymore due
|
|
186
199
|
// to a service change, we do want to report the error here.
|
|
187
200
|
throwOnError: true
|
|
188
201
|
});
|
|
202
|
+
logSyncConfigErrors(parsed);
|
|
203
|
+
await assertSourceCapabilities(payload.context.service_context.routerEngine.getAPI(), parsed.config);
|
|
204
|
+
|
|
189
205
|
const new_rules = await activeBucketStorage.updateSyncRules(
|
|
190
206
|
updateSyncRulesFromConfig(parsed, {
|
|
191
207
|
version_label: sync_rules.version_label,
|
|
@@ -208,18 +224,38 @@ function replyPrettyJson(payload: any) {
|
|
|
208
224
|
});
|
|
209
225
|
}
|
|
210
226
|
|
|
211
|
-
async function debugSyncRules(apiHandler: RouteAPI, sync_rules: string
|
|
227
|
+
async function debugSyncRules(apiHandler: RouteAPI, sync_rules: string) {
|
|
212
228
|
try {
|
|
213
|
-
const rules =
|
|
229
|
+
const rules = SqlSyncRules.fromYaml(sync_rules, {
|
|
214
230
|
...apiHandler.getParseSyncRulesOptions(),
|
|
215
231
|
// No schema-based validation at this point
|
|
216
232
|
schema: undefined
|
|
217
233
|
});
|
|
218
|
-
const
|
|
219
|
-
const
|
|
234
|
+
const errors: string[] = [];
|
|
235
|
+
const warnings: string[] = [];
|
|
236
|
+
const recordError = (error: any) => {
|
|
237
|
+
errors.push(...(error instanceof SyncRulesErrors ? error.errors.map((e) => e.message) : [error.message]));
|
|
238
|
+
};
|
|
239
|
+
try {
|
|
240
|
+
const diagnostics = (await apiHandler.validateSourceCapabilities?.(rules.config)) ?? [];
|
|
241
|
+
for (const diagnostic of diagnostics) {
|
|
242
|
+
(diagnostic.level === 'fatal' ? errors : warnings).push(diagnostic.message);
|
|
243
|
+
}
|
|
244
|
+
} catch (e) {
|
|
245
|
+
recordError(e);
|
|
246
|
+
}
|
|
247
|
+
let resolved_tables: PatternResult[] = [];
|
|
248
|
+
try {
|
|
249
|
+
const source_table_patterns = rules.config.getSourceTables();
|
|
250
|
+
resolved_tables = await apiHandler.getDebugTablesInfo(source_table_patterns, rules.config);
|
|
251
|
+
} catch (e) {
|
|
252
|
+
recordError(e);
|
|
253
|
+
}
|
|
220
254
|
|
|
221
255
|
return {
|
|
222
|
-
valid:
|
|
256
|
+
valid: errors.length == 0,
|
|
257
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
258
|
+
...(errors.length > 0 ? { errors } : {}),
|
|
223
259
|
bucket_definitions: rules.config.debugRepresentation(),
|
|
224
260
|
source_tables: resolved_tables,
|
|
225
261
|
data_tables: rules.config.debugGetOutputTables()
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
SerializedSyncPlan as RawSerializedSyncPlan,
|
|
5
5
|
SerializedCompatibilityContext,
|
|
6
6
|
serializeSyncPlan,
|
|
7
|
+
SqlSyncRules,
|
|
7
8
|
SyncConfigWithErrors
|
|
8
9
|
} from '@powersync/service-sync-rules';
|
|
9
10
|
import { ReplicationError } from '@powersync/service-types';
|
|
@@ -13,16 +14,8 @@ import { PersistedSyncConfigContent } from './PersistedSyncConfigContent.js';
|
|
|
13
14
|
import { ReplicationEventPayload } from './ReplicationEventPayload.js';
|
|
14
15
|
import { ReplicationLock } from './ReplicationLock.js';
|
|
15
16
|
import { ReportStorage } from './ReportStorage.js';
|
|
16
|
-
import { SqlSyncConfigParser, SyncConfigParser } from './SyncConfigParser.js';
|
|
17
17
|
import { SyncRulesBucketStorage } from './SyncRulesBucketStorage.js';
|
|
18
18
|
|
|
19
|
-
export interface BucketStorageFactoryOptions {
|
|
20
|
-
/**
|
|
21
|
-
* Service-wide parser assembled before storage startup.
|
|
22
|
-
*/
|
|
23
|
-
syncConfigParser: SyncConfigParser;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
19
|
/**
|
|
27
20
|
* Represents a configured storage provider.
|
|
28
21
|
*
|
|
@@ -35,16 +28,6 @@ export abstract class BucketStorageFactory
|
|
|
35
28
|
extends BaseObserver<BucketStorageFactoryListener>
|
|
36
29
|
implements AsyncDisposable
|
|
37
30
|
{
|
|
38
|
-
readonly syncConfigParser: SyncConfigParser;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Creates a storage factory using the service-wide parser assembled before storage startup.
|
|
42
|
-
*/
|
|
43
|
-
constructor(options: BucketStorageFactoryOptions) {
|
|
44
|
-
super();
|
|
45
|
-
this.syncConfigParser = options.syncConfigParser;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
31
|
/**
|
|
49
32
|
* Update sync config from configuration, if changed.
|
|
50
33
|
*/
|
|
@@ -245,7 +228,7 @@ export function updateSyncRulesFromYaml(
|
|
|
245
228
|
options?: Omit<UpdateSyncRulesOptions, 'config'> & { validate?: boolean }
|
|
246
229
|
): UpdateSyncRulesOptions {
|
|
247
230
|
const { validate, ...updateOptions } = options ?? {};
|
|
248
|
-
const config =
|
|
231
|
+
const config = SqlSyncRules.fromYaml(content, {
|
|
249
232
|
// No schema-based validation at this point
|
|
250
233
|
schema: undefined,
|
|
251
234
|
defaultSchema: options?.defaultSchema ?? 'not_applicable', // Not needed for validation
|
|
@@ -279,6 +262,25 @@ export function updateSyncRulesFromConfig(
|
|
|
279
262
|
return { config: { yaml: config.content, plan, parsed }, ...options };
|
|
280
263
|
}
|
|
281
264
|
|
|
265
|
+
/**
|
|
266
|
+
* Logs the errors and warnings of a parsed sync config
|
|
267
|
+
*
|
|
268
|
+
* Call this where a sync config is loaded or deployed, so that operators see these in the service logs and not only
|
|
269
|
+
* in the diagnostics and validate API responses.
|
|
270
|
+
*
|
|
271
|
+
* Fatal errors usually throw during parsing before this is reached. They can reach this when the sync config is parsed
|
|
272
|
+
* without validation, such as at startup with `exit_on_error: false`.
|
|
273
|
+
*/
|
|
274
|
+
export function logSyncConfigErrors(parsed: SyncConfigWithErrors, log: typeof logger = logger) {
|
|
275
|
+
for (const error of parsed.errors) {
|
|
276
|
+
if (error.type == 'warning') {
|
|
277
|
+
log.warn(`Sync config warning: ${error.message}`);
|
|
278
|
+
} else {
|
|
279
|
+
log.error(`Sync config error: ${error.message}`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
282
284
|
export interface GetIntanceOptions {
|
|
283
285
|
/**
|
|
284
286
|
* The job lease, including a lease acquired during initial configuration.
|
|
@@ -2,10 +2,9 @@ import { logger as defaultLogger, ErrorCode, ServiceError } from '@powersync/lib
|
|
|
2
2
|
import { Logger } from 'winston';
|
|
3
3
|
import { SyncRuleState } from './BucketStorage.js';
|
|
4
4
|
import type { ParsedSyncConfigSet } from './ParsedSyncConfigSet.js';
|
|
5
|
-
import type { PersistedSyncConfigContent } from './PersistedSyncConfigContent.js';
|
|
5
|
+
import type { ParseSyncConfigOptions, PersistedSyncConfigContent } from './PersistedSyncConfigContent.js';
|
|
6
6
|
import { ReplicationLock } from './ReplicationLock.js';
|
|
7
7
|
import { STORAGE_VERSION_CONFIG, StorageVersionConfig } from './StorageVersionConfig.js';
|
|
8
|
-
import type { ParseSyncConfigOptions } from './SyncConfigParser.js';
|
|
9
8
|
|
|
10
9
|
export abstract class PersistedReplicationStream implements PersistedReplicationStreamData {
|
|
11
10
|
readonly replicationStreamId: number;
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
HydrationState,
|
|
11
11
|
nodeSqlite,
|
|
12
12
|
PrecompiledSyncConfig,
|
|
13
|
+
SqlSyncRules,
|
|
13
14
|
SyncConfigWithErrors,
|
|
14
15
|
versionedHydrationState,
|
|
15
16
|
YamlError
|
|
@@ -21,22 +22,20 @@ import { SerializedSyncPlan, UpdateSyncRulesOptions } from './BucketStorageFacto
|
|
|
21
22
|
import { ParsedSyncConfigSet } from './ParsedSyncConfigSet.js';
|
|
22
23
|
import { PersistedSyncConfigStatus } from './PersistedSyncConfigStatus.js';
|
|
23
24
|
import { STORAGE_VERSION_CONFIG, StorageVersionConfig } from './StorageVersionConfig.js';
|
|
24
|
-
import { ParseSyncConfigOptions, SyncConfigParser } from './SyncConfigParser.js';
|
|
25
25
|
|
|
26
26
|
export interface ParsePersistedSyncConfigContentOptions {
|
|
27
27
|
content: string;
|
|
28
28
|
compiledPlan: SerializedSyncPlan | null;
|
|
29
29
|
storageVersion: number;
|
|
30
30
|
parseOptions: ParseSyncConfigOptions;
|
|
31
|
-
syncConfigParser: SyncConfigParser;
|
|
32
31
|
}
|
|
33
32
|
|
|
34
33
|
export function parsePersistedSyncConfigContent(options: ParsePersistedSyncConfigContentOptions): SyncConfigWithErrors {
|
|
35
|
-
const { content, compiledPlan, storageVersion, parseOptions
|
|
34
|
+
const { content, compiledPlan, storageVersion, parseOptions } = options;
|
|
36
35
|
|
|
37
36
|
if (compiledPlan == null) {
|
|
38
37
|
// Fallback: Only parse from YAML if no compiled plan is available.
|
|
39
|
-
return
|
|
38
|
+
return SqlSyncRules.fromYaml(content, parseOptions);
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
const plan = deserializeSyncPlan(compiledPlan.plan);
|
|
@@ -70,12 +69,6 @@ export function parsePersistedSyncConfigContent(options: ParsePersistedSyncConfi
|
|
|
70
69
|
// Note: If the original content did not define a storage version, this will still set the storage version.
|
|
71
70
|
// This means asUpdateOptions will not change the storage version, even if the default changes.
|
|
72
71
|
precompiled.storageVersion = storageVersion;
|
|
73
|
-
errors.push(
|
|
74
|
-
...syncConfigParser.validatePersisted({
|
|
75
|
-
config: precompiled,
|
|
76
|
-
context: { defaultSchema: parseOptions.defaultSchema }
|
|
77
|
-
})
|
|
78
|
-
);
|
|
79
72
|
|
|
80
73
|
if (compiledPlan.errors) {
|
|
81
74
|
for (const error of compiledPlan.errors) {
|
|
@@ -112,10 +105,7 @@ export abstract class PersistedSyncConfigContent implements PersistedSyncConfigC
|
|
|
112
105
|
readonly syncConfigState: SyncRuleState;
|
|
113
106
|
readonly version_label: string | undefined;
|
|
114
107
|
|
|
115
|
-
constructor(
|
|
116
|
-
data: PersistedSyncConfigContentData,
|
|
117
|
-
private readonly syncConfigParser: SyncConfigParser
|
|
118
|
-
) {
|
|
108
|
+
constructor(data: PersistedSyncConfigContentData) {
|
|
119
109
|
this.replicationStreamId = data.replicationStreamId;
|
|
120
110
|
this.sync_rules_content = data.sync_rules_content;
|
|
121
111
|
this.compiled_plan = data.compiled_plan;
|
|
@@ -154,8 +144,7 @@ export abstract class PersistedSyncConfigContent implements PersistedSyncConfigC
|
|
|
154
144
|
content: this.sync_rules_content,
|
|
155
145
|
compiledPlan: this.compiled_plan,
|
|
156
146
|
storageVersion: this.storageVersion,
|
|
157
|
-
parseOptions: options
|
|
158
|
-
syncConfigParser: this.syncConfigParser
|
|
147
|
+
parseOptions: options
|
|
159
148
|
});
|
|
160
149
|
}
|
|
161
150
|
|
|
@@ -213,3 +202,7 @@ export interface PersistedSyncConfigContentData {
|
|
|
213
202
|
readonly version_label?: string;
|
|
214
203
|
}
|
|
215
204
|
export type PersistedSyncConfigId = string;
|
|
205
|
+
|
|
206
|
+
export interface ParseSyncConfigOptions {
|
|
207
|
+
defaultSchema: string;
|
|
208
|
+
}
|
|
@@ -2,11 +2,9 @@ import { BaseObserver, logger, ServiceError } from '@powersync/lib-services-fram
|
|
|
2
2
|
import { ResolvedPowerSyncConfig } from '../util/util-index.js';
|
|
3
3
|
import { BucketStorageFactory } from './BucketStorageFactory.js';
|
|
4
4
|
import { ActiveStorage, StorageProvider } from './StorageProvider.js';
|
|
5
|
-
import { SyncConfigParser } from './SyncConfigParser.js';
|
|
6
5
|
|
|
7
6
|
export type StorageEngineOptions = {
|
|
8
7
|
configuration: ResolvedPowerSyncConfig;
|
|
9
|
-
syncConfigParser: SyncConfigParser;
|
|
10
8
|
};
|
|
11
9
|
|
|
12
10
|
export interface StorageEngineListener {
|
|
@@ -17,7 +15,6 @@ export interface StorageEngineListener {
|
|
|
17
15
|
export class StorageEngine extends BaseObserver<StorageEngineListener> {
|
|
18
16
|
// TODO: This will need to revisited when we actually support multiple storage providers.
|
|
19
17
|
private storageProviders: Map<string, StorageProvider> = new Map();
|
|
20
|
-
private startupStarted = false;
|
|
21
18
|
private currentActiveStorage: ActiveStorage | null = null;
|
|
22
19
|
|
|
23
20
|
constructor(private options: StorageEngineOptions) {
|
|
@@ -36,10 +33,6 @@ export class StorageEngine extends BaseObserver<StorageEngineListener> {
|
|
|
36
33
|
return this.currentActiveStorage;
|
|
37
34
|
}
|
|
38
35
|
|
|
39
|
-
get started(): boolean {
|
|
40
|
-
return this.startupStarted;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
36
|
/**
|
|
44
37
|
* Register a provider which generates a {@link BucketStorageFactory}
|
|
45
38
|
* given the matching config specified in the loaded {@link ResolvedPowerSyncConfig}
|
|
@@ -49,12 +42,10 @@ export class StorageEngine extends BaseObserver<StorageEngineListener> {
|
|
|
49
42
|
}
|
|
50
43
|
|
|
51
44
|
public async start(): Promise<void> {
|
|
52
|
-
this.startupStarted = true;
|
|
53
45
|
logger.info('Starting Storage Engine...');
|
|
54
46
|
const { configuration } = this.options;
|
|
55
47
|
this.currentActiveStorage = await this.storageProviders.get(configuration.storage.type)!.getStorage({
|
|
56
|
-
resolvedConfig: configuration
|
|
57
|
-
syncConfigParser: this.options.syncConfigParser
|
|
48
|
+
resolvedConfig: configuration
|
|
58
49
|
});
|
|
59
50
|
this.iterateListeners((cb) => cb.storageActivated?.(this.activeBucketStorage));
|
|
60
51
|
this.currentActiveStorage.onFatalError?.((error) => {
|
|
@@ -2,7 +2,6 @@ import { ServiceError } from '@powersync/lib-services-framework';
|
|
|
2
2
|
import * as util from '../util/util-index.js';
|
|
3
3
|
import { BucketStorageFactory } from './BucketStorageFactory.js';
|
|
4
4
|
import { ReportStorage } from './ReportStorage.js';
|
|
5
|
-
import { SyncConfigParser } from './SyncConfigParser.js';
|
|
6
5
|
|
|
7
6
|
export interface ActiveStorage {
|
|
8
7
|
storage: BucketStorageFactory;
|
|
@@ -20,10 +19,6 @@ export interface ActiveStorage {
|
|
|
20
19
|
export interface GetStorageOptions {
|
|
21
20
|
// TODO: This should just be the storage config. Update once the slot name prefix coupling has been removed from the storage
|
|
22
21
|
resolvedConfig: util.ResolvedPowerSyncConfig;
|
|
23
|
-
/**
|
|
24
|
-
* Service-wide parser containing every extension registered during module initialization.
|
|
25
|
-
*/
|
|
26
|
-
syncConfigParser: SyncConfigParser;
|
|
27
22
|
}
|
|
28
23
|
|
|
29
24
|
/**
|
|
@@ -37,7 +32,7 @@ export interface StorageProvider {
|
|
|
37
32
|
type: string;
|
|
38
33
|
|
|
39
34
|
/**
|
|
40
|
-
* Creates active storage during storage-engine startup
|
|
35
|
+
* Creates active storage during storage-engine startup.
|
|
41
36
|
*/
|
|
42
37
|
getStorage(options: GetStorageOptions): Promise<ActiveStorage>;
|
|
43
38
|
}
|
|
@@ -11,11 +11,11 @@ import * as util from '../util/util-index.js';
|
|
|
11
11
|
import { BucketStorageBatch, FlushedResult, SaveUpdate } from './BucketStorageBatch.js';
|
|
12
12
|
import { BucketStorageFactory } from './BucketStorageFactory.js';
|
|
13
13
|
import { ParsedSyncConfigSet } from './ParsedSyncConfigSet.js';
|
|
14
|
+
import type { ParseSyncConfigOptions } from './PersistedSyncConfigContent.js';
|
|
14
15
|
import { SourceEntityDescriptor } from './SourceEntity.js';
|
|
15
16
|
import { SourceTable } from './SourceTable.js';
|
|
16
17
|
import { SourceTableCandidateReconciler } from './SourceTableReconciler.js';
|
|
17
18
|
import { StorageVersionConfig } from './StorageVersionConfig.js';
|
|
18
|
-
import { ParseSyncConfigOptions } from './SyncConfigParser.js';
|
|
19
19
|
import { SyncStorageWriteCheckpointAPI } from './WriteCheckpointAPI.js';
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -25,4 +25,3 @@ export * from './implementation/BucketDefinitionMapping.js';
|
|
|
25
25
|
export * from './implementation/IncrementalReprocessingCompatibility.js';
|
|
26
26
|
export * from './implementation/IncrementalReprocessingSyncConfigLog.js';
|
|
27
27
|
export * from './implementation/ReplicationStreamStorageIds.js';
|
|
28
|
-
export * from './SyncConfigParser.js';
|