@itwin/imodel-transformer 2.0.0-dev.2 → 2.0.0-dev.20

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 (55) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/lib/cjs/BranchProvenanceInitializer.js +18 -15
  3. package/lib/cjs/BranchProvenanceInitializer.js.map +1 -1
  4. package/lib/cjs/DetachedExportElementAspectsStrategy.d.ts.map +1 -1
  5. package/lib/cjs/DetachedExportElementAspectsStrategy.js +8 -6
  6. package/lib/cjs/DetachedExportElementAspectsStrategy.js.map +1 -1
  7. package/lib/cjs/ECReferenceTypesCache.d.ts +3 -0
  8. package/lib/cjs/ECReferenceTypesCache.d.ts.map +1 -1
  9. package/lib/cjs/ECReferenceTypesCache.js +103 -40
  10. package/lib/cjs/ECReferenceTypesCache.js.map +1 -1
  11. package/lib/cjs/ElementCascadingDeleter.d.ts.map +1 -1
  12. package/lib/cjs/ElementCascadingDeleter.js +10 -9
  13. package/lib/cjs/ElementCascadingDeleter.js.map +1 -1
  14. package/lib/cjs/EntityUnifier.d.ts +1 -1
  15. package/lib/cjs/EntityUnifier.d.ts.map +1 -1
  16. package/lib/cjs/EntityUnifier.js +11 -12
  17. package/lib/cjs/EntityUnifier.js.map +1 -1
  18. package/lib/cjs/ExportElementAspectsStrategy.d.ts +4 -4
  19. package/lib/cjs/ExportElementAspectsStrategy.d.ts.map +1 -1
  20. package/lib/cjs/ExportElementAspectsStrategy.js +1 -1
  21. package/lib/cjs/ExportElementAspectsStrategy.js.map +1 -1
  22. package/lib/cjs/ExportElementAspectsWithElementsStrategy.d.ts.map +1 -1
  23. package/lib/cjs/ExportElementAspectsWithElementsStrategy.js +17 -16
  24. package/lib/cjs/ExportElementAspectsWithElementsStrategy.js.map +1 -1
  25. package/lib/cjs/IModelCloneContext.d.ts +3 -3
  26. package/lib/cjs/IModelCloneContext.d.ts.map +1 -1
  27. package/lib/cjs/IModelCloneContext.js +60 -52
  28. package/lib/cjs/IModelCloneContext.js.map +1 -1
  29. package/lib/cjs/IModelExporter.d.ts +28 -19
  30. package/lib/cjs/IModelExporter.d.ts.map +1 -1
  31. package/lib/cjs/IModelExporter.js +145 -119
  32. package/lib/cjs/IModelExporter.js.map +1 -1
  33. package/lib/cjs/IModelImporter.d.ts +21 -21
  34. package/lib/cjs/IModelImporter.d.ts.map +1 -1
  35. package/lib/cjs/IModelImporter.js +94 -74
  36. package/lib/cjs/IModelImporter.js.map +1 -1
  37. package/lib/cjs/IModelTransformer.d.ts +84 -178
  38. package/lib/cjs/IModelTransformer.d.ts.map +1 -1
  39. package/lib/cjs/IModelTransformer.js +371 -997
  40. package/lib/cjs/IModelTransformer.js.map +1 -1
  41. package/lib/cjs/ProvenanceManager.d.ts +159 -0
  42. package/lib/cjs/ProvenanceManager.d.ts.map +1 -0
  43. package/lib/cjs/ProvenanceManager.js +677 -0
  44. package/lib/cjs/ProvenanceManager.js.map +1 -0
  45. package/lib/cjs/SyncTypeResolver.d.ts +34 -0
  46. package/lib/cjs/SyncTypeResolver.d.ts.map +1 -0
  47. package/lib/cjs/SyncTypeResolver.js +84 -0
  48. package/lib/cjs/SyncTypeResolver.js.map +1 -0
  49. package/lib/cjs/TransformerLoggerCategory.d.ts +6 -5
  50. package/lib/cjs/TransformerLoggerCategory.d.ts.map +1 -1
  51. package/lib/cjs/TransformerLoggerCategory.js +6 -5
  52. package/lib/cjs/TransformerLoggerCategory.js.map +1 -1
  53. package/lib/cjs/imodel-transformer.js +2 -2
  54. package/lib/cjs/imodel-transformer.js.map +1 -1
  55. package/package.json +38 -33
@@ -0,0 +1,677 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ProvenanceManager = void 0;
4
+ /*---------------------------------------------------------------------------------------------
5
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
6
+ * See LICENSE.md in the project root for license terms and full copyright notice.
7
+ *--------------------------------------------------------------------------------------------*/
8
+ const core_bentley_1 = require("@itwin/core-bentley");
9
+ const core_backend_1 = require("@itwin/core-backend");
10
+ const core_common_1 = require("@itwin/core-common");
11
+ const TransformerLoggerCategory_1 = require("./TransformerLoggerCategory");
12
+ const loggerCategory = TransformerLoggerCategory_1.TransformerLoggerCategory.IModelTransformer;
13
+ /**
14
+ * Manages provenance scope aspects and synchronization versioning.
15
+ * Encapsulates all ESA scope management, sync version tracking, and
16
+ * provenance DB direction logic extracted from IModelTransformer.
17
+ * @internal
18
+ */
19
+ class ProvenanceManager {
20
+ context;
21
+ _targetScopeElementId;
22
+ _transformerOptions;
23
+ _syncTypeResolver;
24
+ _startingChangesetIndices;
25
+ /** NOTE: the json properties must be converted to string before insertion */
26
+ _targetScopeProvenanceProps = undefined;
27
+ _cachedSynchronizationVersion = undefined;
28
+ _targetClassNameToClassIdCache = new Map();
29
+ constructor(targetScopeElementId, transformerOptions, syncTypeResolver) {
30
+ this._targetScopeElementId = targetScopeElementId;
31
+ this._transformerOptions = transformerOptions;
32
+ this._syncTypeResolver = syncTypeResolver;
33
+ this.context = this._syncTypeResolver.context;
34
+ const sourceDb = this.context.sourceDb;
35
+ const targetDb = this.context.targetDb;
36
+ if (sourceDb.isBriefcase && targetDb.isBriefcase) {
37
+ if (sourceDb.changeset.index === undefined ||
38
+ targetDb.changeset.index === undefined)
39
+ throw new Error("database has no changeset index");
40
+ this._startingChangesetIndices = {
41
+ target: targetDb.changeset.index,
42
+ source: sourceDb.changeset.index,
43
+ };
44
+ }
45
+ }
46
+ async _isReverseSynchronization() {
47
+ return (await this._syncTypeResolver.getSyncType()) === "reverse";
48
+ }
49
+ async _queryTargetRelId(sourceRelInfo) {
50
+ const targetRelInfo = {
51
+ sourceId: this.context.findTargetElementId(sourceRelInfo.sourceId),
52
+ targetId: this.context.findTargetElementId(sourceRelInfo.targetId),
53
+ };
54
+ if (targetRelInfo.sourceId === undefined ||
55
+ targetRelInfo.targetId === undefined)
56
+ return undefined;
57
+ const sql = `
58
+ select ecinstanceid
59
+ from bis.elementreferstoelements
60
+ where sourceecinstanceid=?
61
+ and targetecinstanceid=?
62
+ and ecclassid=?
63
+ `;
64
+ const params = new core_common_1.QueryBinder();
65
+ params.bindId(1, targetRelInfo.sourceId);
66
+ params.bindId(2, targetRelInfo.targetId);
67
+ params.bindId(3, await this._targetClassNameToClassId(sourceRelInfo.classFullName));
68
+ const result = this.context.targetDb.createQueryReader(sql, params, {
69
+ usePrimaryConn: true,
70
+ });
71
+ if (await result.step())
72
+ return result.current.id;
73
+ else
74
+ return undefined;
75
+ }
76
+ async _targetClassNameToClassId(classFullName) {
77
+ let classId = this._targetClassNameToClassIdCache.get(classFullName);
78
+ if (classId === undefined) {
79
+ classId = await this._getRelClassId(this.context.targetDb, classFullName);
80
+ this._targetClassNameToClassIdCache.set(classFullName, classId);
81
+ }
82
+ return classId;
83
+ }
84
+ async _getRelClassId(db, classFullName) {
85
+ const sql = `
86
+ SELECT c.ECInstanceId
87
+ FROM ECDbMeta.ECClassDef c
88
+ JOIN ECDbMeta.ECSchemaDef s ON c.Schema.Id=s.ECInstanceId
89
+ WHERE s.Name=? AND c.Name=?
90
+ `;
91
+ const [schemaName, className] = classFullName.indexOf(".") !== -1
92
+ ? classFullName.split(".")
93
+ : classFullName.split(":");
94
+ const params = new core_common_1.QueryBinder();
95
+ params.bindString(1, schemaName);
96
+ params.bindString(2, className);
97
+ const result = db.createQueryReader(sql, params, { usePrimaryConn: true });
98
+ if (await result.step())
99
+ return result.current.id;
100
+ throw new Error(`Could not find class ${classFullName} in the db`);
101
+ }
102
+ // ── Static provenance metadata ──────────────────────────────────────────
103
+ /** The element classes that are considered to define provenance in the iModel */
104
+ static get provenanceElementClasses() {
105
+ return [
106
+ core_backend_1.FolderLink,
107
+ core_backend_1.SynchronizationConfigLink,
108
+ core_backend_1.ExternalSource,
109
+ core_backend_1.ExternalSourceAttachment,
110
+ ];
111
+ }
112
+ /** The element aspect classes that are considered to define provenance in the iModel */
113
+ static get provenanceElementAspectClasses() {
114
+ return [core_backend_1.ExternalSourceAspect];
115
+ }
116
+ // ── Static provenance queries ──────────────────────────────────────────
117
+ /**
118
+ * Iterate all matching federation guids and ExternalSourceAspects in the provenance iModel (target unless reverse sync)
119
+ * and call a function for each one.
120
+ * @note provenance is done by federation guids where possible
121
+ * @note this may execute on each element more than once! Only use in cases where that is handled
122
+ */
123
+ static async forEachTrackedElement(args) {
124
+ if (args.provenanceDb === args.provenanceSourceDb)
125
+ return;
126
+ if (!args.provenanceDb.containsClass(core_backend_1.ExternalSourceAspect.classFullName)) {
127
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadSchema, "The BisCore schema version of the target database is too old");
128
+ }
129
+ const sourceDb = args.isReverseSynchronization
130
+ ? args.provenanceDb
131
+ : args.provenanceSourceDb;
132
+ const targetDb = args.isReverseSynchronization
133
+ ? args.provenanceSourceDb
134
+ : args.provenanceDb;
135
+ // query for provenanceDb
136
+ const elementIdByFedGuidQuery = `
137
+ SELECT e.ECInstanceId, FederationGuid
138
+ FROM bis.Element e
139
+ ${args.skipPropagateChangesToRootElements
140
+ ? "WHERE e.ECInstanceId NOT IN (0x1, 0xe, 0x10) -- special static elements"
141
+ : ""}
142
+ ORDER BY FederationGuid
143
+ `;
144
+ // iterate through sorted list of fed guids from both dbs to get the intersection
145
+ // NOTE: if we exposed the native attach database support,
146
+ // we could get the intersection of fed guids in one query, not sure if it would be faster
147
+ // OR we could do a raw sqlite query...
148
+ const sourceReader = sourceDb.createQueryReader(elementIdByFedGuidQuery, undefined, { usePrimaryConn: true });
149
+ const targetReader = targetDb.createQueryReader(elementIdByFedGuidQuery, undefined, { usePrimaryConn: true });
150
+ let hasSourceRow = await sourceReader.step();
151
+ let hasTargetRow = await targetReader.step();
152
+ while (hasSourceRow && hasTargetRow) {
153
+ const sourceFedGuid = sourceReader.current.federationGuid;
154
+ const targetFedGuid = targetReader.current.federationGuid;
155
+ if (sourceFedGuid !== undefined &&
156
+ targetFedGuid !== undefined &&
157
+ sourceFedGuid === targetFedGuid) {
158
+ // data flow direction is always sourceDb -> targetDb and it does not depend on where the explicit element provenance is stored
159
+ args.fn(sourceReader.current.id, targetReader.current.id);
160
+ }
161
+ if (targetFedGuid === undefined ||
162
+ (sourceFedGuid !== undefined && sourceFedGuid >= targetFedGuid)) {
163
+ hasTargetRow = await targetReader.step();
164
+ }
165
+ if (sourceFedGuid === undefined ||
166
+ (targetFedGuid !== undefined && sourceFedGuid <= targetFedGuid)) {
167
+ hasSourceRow = await sourceReader.step();
168
+ }
169
+ }
170
+ // query for provenanceDb
171
+ const provenanceAspectsQuery = `
172
+ SELECT esa.Identifier, Element.Id
173
+ FROM bis.ExternalSourceAspect esa
174
+ WHERE Scope.Id=:scopeId
175
+ AND Kind=:kind
176
+ `;
177
+ // Technically this will a second time call the function (as documented) on
178
+ // victims of the old provenance method that have both fedguids and an inserted aspect.
179
+ // But this is a private function with one known caller where that doesn't matter
180
+ const runFnInDataFlowDirection = (sourceId, targetId) => args.isReverseSynchronization
181
+ ? args.fn(sourceId, targetId)
182
+ : args.fn(targetId, sourceId);
183
+ const params = new core_common_1.QueryBinder();
184
+ params.bindId("scopeId", args.targetScopeElementId);
185
+ params.bindString("kind", core_backend_1.ExternalSourceAspect.Kind.Element);
186
+ const provenanceReader = args.provenanceDb.createQueryReader(provenanceAspectsQuery, params, { usePrimaryConn: true });
187
+ for await (const row of provenanceReader) {
188
+ // ExternalSourceAspect.Identifier is of type string
189
+ const aspectIdentifier = row[0];
190
+ const elementId = row.id;
191
+ runFnInDataFlowDirection(elementId, aspectIdentifier);
192
+ }
193
+ }
194
+ /**
195
+ * Queries for an ESA which matches the props in the provided aspectProps.
196
+ * @param dbToQuery db to run the query on
197
+ * @param aspectProps aspectProps to search for
198
+ */
199
+ static async queryScopeExternalSourceAspect(dbToQuery, aspectProps) {
200
+ const sql = `
201
+ SELECT ECInstanceId, Version, JsonProperties
202
+ FROM ${core_backend_1.ExternalSourceAspect.classFullName}
203
+ WHERE Element.Id=:elementId
204
+ AND Scope.Id=:scopeId
205
+ AND Kind=:kind
206
+ AND Identifier=:identifier
207
+ LIMIT 1
208
+ `;
209
+ if (aspectProps.scope === undefined)
210
+ return undefined;
211
+ const params = new core_common_1.QueryBinder()
212
+ .bindId("elementId", aspectProps.element.id)
213
+ .bindId("scopeId", aspectProps.scope.id)
214
+ .bindString("kind", aspectProps.kind)
215
+ .bindString("identifier", aspectProps.identifier);
216
+ return dbToQuery.withQueryReader(sql, (reader) => {
217
+ if (!reader.step())
218
+ return undefined;
219
+ const aspectId = reader.current[0];
220
+ const version = reader.current[1];
221
+ const jsonProperties = reader.current[2];
222
+ return { aspectId, version, jsonProperties };
223
+ }, params);
224
+ }
225
+ // ── Provenance DB direction ────────────────────────────────────────────
226
+ /** Return the IModelDb where provenance is stored.
227
+ * This will be targetDb except when it is a reverse synchronization, in which case it will be sourceDb.
228
+ */
229
+ async getProvenanceDb() {
230
+ return (await this._isReverseSynchronization())
231
+ ? this.context.sourceDb
232
+ : this.context.targetDb;
233
+ }
234
+ /** Return the IModelDb where entities referred to by stored provenance live.
235
+ * This will be sourceDb except when it is a reverse synchronization, in which case it will be targetDb.
236
+ */
237
+ async getProvenanceSourceDb() {
238
+ return (await this._isReverseSynchronization())
239
+ ? this.context.targetDb
240
+ : this.context.sourceDb;
241
+ }
242
+ // ── Scope aspect management ────────────────────────────────────────────
243
+ /**
244
+ * @returns provenance scope aspect if it exists in the provenanceDb.
245
+ * Provenance scope aspect is created and inserted into provenanceDb when [[initScopeProvenance]] is invoked.
246
+ */
247
+ async tryGetProvenanceScopeAspect() {
248
+ const scopeProvenanceAspectProps = await ProvenanceManager.queryScopeExternalSourceAspect(await this.getProvenanceDb(), {
249
+ id: undefined,
250
+ classFullName: core_backend_1.ExternalSourceAspect.classFullName,
251
+ scope: { id: core_common_1.IModel.rootSubjectId },
252
+ kind: core_backend_1.ExternalSourceAspect.Kind.Scope,
253
+ element: {
254
+ id: this._targetScopeElementId ?? core_common_1.IModel.rootSubjectId,
255
+ },
256
+ identifier: (await this.getProvenanceSourceDb()).iModelId,
257
+ });
258
+ return scopeProvenanceAspectProps !== undefined
259
+ ? (await this.getProvenanceDb()).elements.getAspect(scopeProvenanceAspectProps.aspectId)
260
+ : undefined;
261
+ }
262
+ /**
263
+ * Make sure there are no conflicting other scope-type external source aspects on the target scope element.
264
+ * If there are none at all, insert one (this must be a first synchronization).
265
+ */
266
+ async initScopeProvenance() {
267
+ const provenanceDb = await this.getProvenanceDb();
268
+ const sourceProvenanceDb = await this.getProvenanceSourceDb();
269
+ const aspectProps = {
270
+ id: undefined,
271
+ version: undefined,
272
+ classFullName: core_backend_1.ExternalSourceAspect.classFullName,
273
+ element: {
274
+ id: this._targetScopeElementId,
275
+ relClassName: core_backend_1.ElementOwnsExternalSourceAspects.classFullName,
276
+ },
277
+ scope: { id: core_common_1.IModel.rootSubjectId },
278
+ identifier: sourceProvenanceDb.iModelId,
279
+ kind: core_backend_1.ExternalSourceAspect.Kind.Scope,
280
+ jsonProperties: undefined,
281
+ };
282
+ const foundEsaProps = await ProvenanceManager.queryScopeExternalSourceAspect(provenanceDb, aspectProps);
283
+ if (foundEsaProps === undefined) {
284
+ aspectProps.version = "";
285
+ aspectProps.jsonProperties = {
286
+ pendingReverseSyncChangesetIndices: [],
287
+ pendingSyncChangesetIndices: [],
288
+ reverseSyncVersion: "",
289
+ };
290
+ // query without "identifier" to find possible conflicts
291
+ const sql = `
292
+ SELECT ECInstanceId
293
+ FROM ${core_backend_1.ExternalSourceAspect.classFullName}
294
+ WHERE Element.Id=:elementId
295
+ AND Scope.Id=:scopeId
296
+ AND Kind=:kind
297
+ LIMIT 1
298
+ `;
299
+ const params = new core_common_1.QueryBinder();
300
+ params.bindId("elementId", aspectProps.element.id);
301
+ params.bindId("scopeId", aspectProps.scope.id);
302
+ params.bindString("kind", aspectProps.kind);
303
+ const reader = provenanceDb.createQueryReader(sql, params, {
304
+ usePrimaryConn: true,
305
+ });
306
+ const hasConflictingScope = await reader.step();
307
+ if (hasConflictingScope) {
308
+ throw new core_common_1.IModelError(core_bentley_1.IModelStatus.InvalidId, "Provenance scope conflict");
309
+ }
310
+ if (!this._transformerOptions.noProvenance) {
311
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
312
+ const id = provenanceDb.elements.insertAspect({
313
+ ...aspectProps,
314
+ jsonProperties: JSON.stringify(aspectProps.jsonProperties),
315
+ });
316
+ aspectProps.id = id;
317
+ this.clearCachedSynchronizationVersion();
318
+ }
319
+ }
320
+ else {
321
+ aspectProps.id = foundEsaProps.aspectId;
322
+ aspectProps.version = foundEsaProps.version;
323
+ aspectProps.jsonProperties = foundEsaProps.jsonProperties
324
+ ? JSON.parse(foundEsaProps.jsonProperties)
325
+ : undefined;
326
+ const oldProps = JSON.parse(JSON.stringify(aspectProps));
327
+ if (this.handleUnsafeMigrate(aspectProps)) {
328
+ core_bentley_1.Logger.logInfo(loggerCategory, "Unsafe migrate made a change to the target scope's external source aspect. Updating aspect in database.", { oldProps, newProps: aspectProps });
329
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
330
+ provenanceDb.elements.updateAspect({
331
+ ...aspectProps,
332
+ jsonProperties: JSON.stringify(aspectProps.jsonProperties),
333
+ });
334
+ this.clearCachedSynchronizationVersion();
335
+ }
336
+ }
337
+ this._targetScopeProvenanceProps =
338
+ aspectProps;
339
+ }
340
+ /** Returns true if a change was made to the aspectProps. */
341
+ handleUnsafeMigrate(aspectProps) {
342
+ let madeChange = false;
343
+ if (this._transformerOptions.branchRelationshipDataBehavior !==
344
+ "unsafe-migrate")
345
+ return madeChange;
346
+ const fallbackSyncVersionToUse = this._transformerOptions.argsForProcessChanges
347
+ ?.unsafeFallbackSyncVersion ?? "";
348
+ const fallbackReverseSyncVersionToUse = this._transformerOptions.argsForProcessChanges
349
+ ?.unsafeFallbackReverseSyncVersion ?? "";
350
+ if (aspectProps.version === undefined ||
351
+ (aspectProps.version === "" &&
352
+ aspectProps.version !== fallbackSyncVersionToUse)) {
353
+ aspectProps.version = fallbackSyncVersionToUse;
354
+ madeChange = true;
355
+ }
356
+ if (aspectProps.jsonProperties === undefined) {
357
+ aspectProps.jsonProperties = {
358
+ pendingReverseSyncChangesetIndices: [],
359
+ pendingSyncChangesetIndices: [],
360
+ reverseSyncVersion: fallbackReverseSyncVersionToUse,
361
+ };
362
+ madeChange = true;
363
+ }
364
+ else if (aspectProps.jsonProperties.reverseSyncVersion === undefined ||
365
+ (aspectProps.jsonProperties.reverseSyncVersion === "" &&
366
+ aspectProps.jsonProperties.reverseSyncVersion !==
367
+ fallbackReverseSyncVersionToUse)) {
368
+ aspectProps.jsonProperties.reverseSyncVersion =
369
+ fallbackReverseSyncVersionToUse;
370
+ madeChange = true;
371
+ }
372
+ if (aspectProps.jsonProperties.pendingReverseSyncChangesetIndices ===
373
+ undefined) {
374
+ core_bentley_1.Logger.logWarning(loggerCategory, "Property pendingReverseSyncChangesetIndices missing on the jsonProperties of the scoping ESA. Setting to [].");
375
+ aspectProps.jsonProperties.pendingReverseSyncChangesetIndices = [];
376
+ madeChange = true;
377
+ }
378
+ if (aspectProps.jsonProperties.pendingSyncChangesetIndices === undefined) {
379
+ core_bentley_1.Logger.logWarning(loggerCategory, "Property pendingSyncChangesetIndices missing on the jsonProperties of the scoping ESA. Setting to [].");
380
+ aspectProps.jsonProperties.pendingSyncChangesetIndices = [];
381
+ madeChange = true;
382
+ }
383
+ return madeChange;
384
+ }
385
+ // ── Synchronization version ────────────────────────────────────────────
386
+ /**
387
+ * We cache the synchronization version to avoid querying the target scoping ESA multiple times.
388
+ * Clears the cached value so the next call to getSynchronizationVersion re-queries.
389
+ */
390
+ clearCachedSynchronizationVersion() {
391
+ this._cachedSynchronizationVersion = undefined;
392
+ }
393
+ /** The changeset version in the scoping element's source version found for this transformation.
394
+ * @note the version depends on whether this is a reverse synchronization or not.
395
+ * @note empty string and -1 for changeset and index if it has never been transformed.
396
+ */
397
+ async getSynchronizationVersion() {
398
+ if (this._cachedSynchronizationVersion === undefined) {
399
+ const provenanceScopeAspect = await this.tryGetProvenanceScopeAspect();
400
+ if (!provenanceScopeAspect) {
401
+ return { index: -1, id: "" };
402
+ }
403
+ const version = (await this._isReverseSynchronization())
404
+ ? JSON.parse(provenanceScopeAspect.jsonProperties ?? "{}").reverseSyncVersion
405
+ : provenanceScopeAspect.version;
406
+ if (!version &&
407
+ this._transformerOptions.branchRelationshipDataBehavior ===
408
+ "unsafe-migrate") {
409
+ return { index: -1, id: "" };
410
+ }
411
+ if (version === undefined) {
412
+ throw new Error(`Could not find synchronization version in scope aspect. This may be due to the last successful run of the transformer being done with an older version.
413
+ Consider running the transformer with branchRelationshipDataBehavior set to 'unsafe-migrate'`);
414
+ }
415
+ const [id, index] = version === "" ? ["", -1] : version.split(";");
416
+ if (Number.isNaN(Number(index)))
417
+ throw new Error("Could not parse version data from scope aspect");
418
+ this._cachedSynchronizationVersion = { index: Number(index), id };
419
+ }
420
+ return this._cachedSynchronizationVersion;
421
+ }
422
+ /**
423
+ * Returns the pending changeset indices to skip for the current synchronization direction.
424
+ * Used by changeset initialization to determine which changesets have already been processed.
425
+ */
426
+ async getChangesetsToSkip() {
427
+ if (this._targetScopeProvenanceProps === undefined)
428
+ throw new Error("_targetScopeProvenanceProps should be set by now");
429
+ const props = this._targetScopeProvenanceProps;
430
+ return (await this._isReverseSynchronization())
431
+ ? props.jsonProperties.pendingReverseSyncChangesetIndices
432
+ : props.jsonProperties.pendingSyncChangesetIndices;
433
+ }
434
+ /**
435
+ * Updates the synchronization version on the scope ESA.
436
+ *
437
+ * Called at the end of a transformation, updates the target scope element to record
438
+ * that transformation up through the source's changeset has been performed. Also stores
439
+ * all changesets that occurred during the transformation as "pending synchronization
440
+ * changeset indices" @see TargetScopeProvenanceJsonProps
441
+ *
442
+ * @param initializeReverseSyncVersion When true, saves the reverse sync version as the
443
+ * current changeset of the targetDb. This is typically used for the first transformation
444
+ * between a master and branch iModel. Setting this to true has the effect of making it so
445
+ * any changesets in the branch iModel at the time of the first transformation will be
446
+ * ignored during any future reverse synchronizations from the branch to the master iModel.
447
+ *
448
+ * Note that typically, the reverseSyncVersion is saved as the last changeset merged from
449
+ * the branch into master. Setting initializeReverseSyncVersion to true during a forward
450
+ * transformation could overwrite this correct reverseSyncVersion and should only be done
451
+ * during the first transformation between a master and branch iModel.
452
+ *
453
+ * @param sourceChangeDataState The current state of change data — used to skip updates
454
+ * when there are no changes.
455
+ */
456
+ async updateSynchronizationVersion({ initializeReverseSyncVersion = false, sourceChangeDataState, }) {
457
+ const shouldSkipSyncVersionUpdate = !initializeReverseSyncVersion && sourceChangeDataState !== "has-changes";
458
+ if (shouldSkipSyncVersionUpdate)
459
+ return;
460
+ // If noProvenance is set, there's no scope ESA to update
461
+ if (this._transformerOptions.noProvenance)
462
+ return;
463
+ if (this._targetScopeProvenanceProps === undefined)
464
+ throw new Error("_targetScopeProvenanceProps should be set by now");
465
+ const scopeProps = this._targetScopeProvenanceProps;
466
+ const sourceVersion = `${this.context.sourceDb.changeset.id};${this.context.sourceDb.changeset.index}`;
467
+ const targetVersion = `${this.context.targetDb.changeset.id};${this.context.targetDb.changeset.index}`;
468
+ if (await this._isReverseSynchronization()) {
469
+ const oldVersion = scopeProps.jsonProperties.reverseSyncVersion;
470
+ core_bentley_1.Logger.logInfo(loggerCategory, `updating reverse version from ${oldVersion} to ${sourceVersion}`);
471
+ scopeProps.jsonProperties.reverseSyncVersion = sourceVersion;
472
+ }
473
+ else {
474
+ core_bentley_1.Logger.logInfo(loggerCategory, `updating sync version from ${scopeProps.version} to ${sourceVersion}`);
475
+ scopeProps.version = sourceVersion;
476
+ if (initializeReverseSyncVersion) {
477
+ core_bentley_1.Logger.logInfo(loggerCategory, `updating reverse sync version from ${scopeProps.jsonProperties.reverseSyncVersion} to ${targetVersion}`);
478
+ scopeProps.jsonProperties.reverseSyncVersion = targetVersion;
479
+ }
480
+ }
481
+ const startingChangesetIndices = this._startingChangesetIndices;
482
+ if (!!this._transformerOptions.argsForProcessChanges ||
483
+ (startingChangesetIndices && initializeReverseSyncVersion)) {
484
+ if (this.context.targetDb.changeset.index === undefined ||
485
+ startingChangesetIndices === undefined)
486
+ throw new Error("updateSynchronizationVersion was called without change history");
487
+ const jsonProps = scopeProps.jsonProperties;
488
+ core_bentley_1.Logger.logTrace(loggerCategory, `previous pendingReverseSyncChanges: ${String(jsonProps.pendingReverseSyncChangesetIndices)}`);
489
+ core_bentley_1.Logger.logTrace(loggerCategory, `previous pendingSyncChanges: ${String(jsonProps.pendingSyncChangesetIndices)}`);
490
+ const pendingSyncChangesetIndicesKey = "pendingSyncChangesetIndices";
491
+ const pendingReverseSyncChangesetIndicesKey = "pendingReverseSyncChangesetIndices";
492
+ let syncChangesetsToClearKey;
493
+ let syncChangesetsToUpdateKey;
494
+ if (await this._isReverseSynchronization()) {
495
+ syncChangesetsToClearKey = pendingReverseSyncChangesetIndicesKey;
496
+ syncChangesetsToUpdateKey = pendingSyncChangesetIndicesKey;
497
+ }
498
+ else {
499
+ syncChangesetsToClearKey = pendingSyncChangesetIndicesKey;
500
+ syncChangesetsToUpdateKey = pendingReverseSyncChangesetIndicesKey;
501
+ }
502
+ for (let i = startingChangesetIndices.target + 1; i <= this.context.targetDb.changeset.index + 1; i++)
503
+ jsonProps[syncChangesetsToUpdateKey].push(i);
504
+ jsonProps[syncChangesetsToClearKey] = jsonProps[syncChangesetsToClearKey].filter((csIndex) => {
505
+ return csIndex > startingChangesetIndices.source;
506
+ });
507
+ if (await this._isReverseSynchronization()) {
508
+ if (this.context.sourceDb.changeset.index === undefined)
509
+ throw new Error("changeset didn't exist");
510
+ for (let i = startingChangesetIndices.source + 1; i <= this.context.sourceDb.changeset.index + 1; i++)
511
+ jsonProps.pendingReverseSyncChangesetIndices.push(i);
512
+ }
513
+ core_bentley_1.Logger.logTrace(loggerCategory, `new pendingReverseSyncChanges: ${String(jsonProps.pendingReverseSyncChangesetIndices)}`);
514
+ core_bentley_1.Logger.logTrace(loggerCategory, `new pendingSyncChanges: ${String(jsonProps.pendingSyncChangesetIndices)}`);
515
+ }
516
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
517
+ (await this.getProvenanceDb()).elements.updateAspect({
518
+ ...scopeProps,
519
+ jsonProperties: JSON.stringify(scopeProps.jsonProperties),
520
+ });
521
+ this.clearCachedSynchronizationVersion();
522
+ }
523
+ // ── Element/Relationship provenance creation ───────────────────────────
524
+ /** Create ExternalSourceAspectProps for an element in an iModel → iModel transformation. */
525
+ static initElementProvenanceOptions(sourceElementId, targetElementId, args) {
526
+ const elementId = args.isReverseSynchronization
527
+ ? sourceElementId
528
+ : targetElementId;
529
+ const version = args.isReverseSynchronization
530
+ ? args.targetDb.elements.queryLastModifiedTime(targetElementId)
531
+ : args.sourceDb.elements.queryLastModifiedTime(sourceElementId);
532
+ const aspectIdentifier = args.isReverseSynchronization
533
+ ? targetElementId
534
+ : sourceElementId;
535
+ const aspectProps = {
536
+ classFullName: core_backend_1.ExternalSourceAspect.classFullName,
537
+ element: {
538
+ id: elementId,
539
+ relClassName: core_backend_1.ElementOwnsExternalSourceAspects.classFullName,
540
+ },
541
+ scope: { id: args.targetScopeElementId },
542
+ identifier: aspectIdentifier,
543
+ kind: core_backend_1.ExternalSourceAspect.Kind.Element,
544
+ version,
545
+ };
546
+ return aspectProps;
547
+ }
548
+ /** Create ExternalSourceAspectProps for a relationship in an iModel → iModel transformation. */
549
+ static async initRelationshipProvenanceOptions(sourceRelInstanceId, targetRelInstanceId, args) {
550
+ const provenanceDb = args.isReverseSynchronization
551
+ ? args.sourceDb
552
+ : args.targetDb;
553
+ const aspectIdentifier = args.isReverseSynchronization
554
+ ? targetRelInstanceId
555
+ : sourceRelInstanceId;
556
+ const provenanceRelInstanceId = args.isReverseSynchronization
557
+ ? sourceRelInstanceId
558
+ : targetRelInstanceId;
559
+ const sql = "SELECT SourceECInstanceId FROM bis.ElementRefersToElements WHERE ECInstanceId=?";
560
+ const params = new core_common_1.QueryBinder().bindId(1, provenanceRelInstanceId);
561
+ const reader = provenanceDb.createQueryReader(sql, params, {
562
+ usePrimaryConn: true,
563
+ });
564
+ if (!(await reader.step()))
565
+ throw new Error("relationship provenance query returned no rows");
566
+ const elementId = reader.current[0];
567
+ const jsonProperties = args.forceOldRelationshipProvenanceMethod
568
+ ? { targetRelInstanceId }
569
+ : { provenanceRelInstanceId };
570
+ const aspectProps = {
571
+ classFullName: core_backend_1.ExternalSourceAspect.classFullName,
572
+ element: {
573
+ id: elementId,
574
+ relClassName: core_backend_1.ElementOwnsExternalSourceAspects.classFullName,
575
+ },
576
+ scope: { id: args.targetScopeElementId },
577
+ identifier: aspectIdentifier,
578
+ kind: core_backend_1.ExternalSourceAspect.Kind.Relationship,
579
+ jsonProperties: JSON.stringify(jsonProperties),
580
+ };
581
+ return aspectProps;
582
+ }
583
+ /** Create an ExternalSourceAspectProps for an element using this manager's context. */
584
+ async initElementProvenance(sourceElementId, targetElementId) {
585
+ return ProvenanceManager.initElementProvenanceOptions(sourceElementId, targetElementId, {
586
+ isReverseSynchronization: await this._isReverseSynchronization(),
587
+ targetScopeElementId: this._targetScopeElementId,
588
+ sourceDb: this.context.sourceDb,
589
+ targetDb: this.context.targetDb,
590
+ });
591
+ }
592
+ /** Create an ExternalSourceAspectProps for a relationship using this manager's context. */
593
+ async initRelationshipProvenance(sourceRelInstanceId, targetRelInstanceId, forceOldRelationshipProvenanceMethod) {
594
+ return ProvenanceManager.initRelationshipProvenanceOptions(sourceRelInstanceId, targetRelInstanceId, {
595
+ sourceDb: this.context.sourceDb,
596
+ targetDb: this.context.targetDb,
597
+ isReverseSynchronization: await this._isReverseSynchronization(),
598
+ targetScopeElementId: this._targetScopeElementId,
599
+ forceOldRelationshipProvenanceMethod,
600
+ });
601
+ }
602
+ // ── Provenance queries ─────────────────────────────────────────────────
603
+ /**
604
+ * Queries the provenanceDb for an ESA whose identifier matches the provided element ID.
605
+ * @param entityInProvenanceSourceId ID of the element in the provenanceSourceDb
606
+ */
607
+ async queryProvenanceForElement(entityInProvenanceSourceId) {
608
+ const sql = `
609
+ SELECT esa.Element.Id
610
+ FROM Bis.ExternalSourceAspect esa
611
+ WHERE esa.Kind=?
612
+ AND esa.Scope.Id=?
613
+ AND esa.Identifier=?
614
+ `;
615
+ const params = new core_common_1.QueryBinder();
616
+ params.bindString(1, core_backend_1.ExternalSourceAspect.Kind.Element);
617
+ params.bindId(2, this._targetScopeElementId);
618
+ params.bindString(3, entityInProvenanceSourceId);
619
+ const result = (await this.getProvenanceDb()).createQueryReader(sql, params, {
620
+ usePrimaryConn: true,
621
+ });
622
+ if (await result.step()) {
623
+ return result.current.id;
624
+ }
625
+ else
626
+ return undefined;
627
+ }
628
+ /**
629
+ * Queries the provenanceDb for an ESA whose identifier matches the provided relationship ID.
630
+ * @param entityInProvenanceSourceId ID of the relationship in the provenanceSourceDb
631
+ * @param sourceRelInfo Source relationship class and endpoint info (for legacy fallback)
632
+ */
633
+ async queryProvenanceForRelationship(entityInProvenanceSourceId, sourceRelInfo) {
634
+ const sql = `
635
+ SELECT
636
+ ECInstanceId,
637
+ JSON_EXTRACT(JsonProperties, '$.provenanceRelInstanceId') AS provenanceRelInstId
638
+ FROM Bis.ExternalSourceAspect
639
+ WHERE Kind=?
640
+ AND Scope.Id=?
641
+ AND Identifier=?
642
+ `;
643
+ const params = new core_common_1.QueryBinder();
644
+ params.bindString(1, core_backend_1.ExternalSourceAspect.Kind.Relationship);
645
+ params.bindId(2, this._targetScopeElementId);
646
+ params.bindString(3, entityInProvenanceSourceId);
647
+ const result = (await this.getProvenanceDb()).createQueryReader(sql, params, {
648
+ usePrimaryConn: true,
649
+ });
650
+ if (await result.step()) {
651
+ const aspectId = result.current.id;
652
+ const provenanceRelInstId = result.current.provenanceRelInstId;
653
+ const provenanceRelInstanceId = provenanceRelInstId !== undefined
654
+ ? provenanceRelInstId
655
+ : await this._queryTargetRelId(sourceRelInfo);
656
+ return {
657
+ aspectId,
658
+ relationshipId: provenanceRelInstanceId,
659
+ };
660
+ }
661
+ else
662
+ return undefined;
663
+ }
664
+ /** Instance convenience that calls the static forEachTrackedElement with this manager's context. */
665
+ async forEachTrackedElement(fn) {
666
+ return ProvenanceManager.forEachTrackedElement({
667
+ provenanceSourceDb: await this.getProvenanceSourceDb(),
668
+ provenanceDb: await this.getProvenanceDb(),
669
+ targetScopeElementId: this._targetScopeElementId,
670
+ isReverseSynchronization: await this._isReverseSynchronization(),
671
+ fn,
672
+ skipPropagateChangesToRootElements: this._transformerOptions.skipPropagateChangesToRootElements ?? true,
673
+ });
674
+ }
675
+ }
676
+ exports.ProvenanceManager = ProvenanceManager;
677
+ //# sourceMappingURL=ProvenanceManager.js.map