@itwin/imodel-transformer 0.1.6-test1.0 → 0.1.6
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TemplateModelCloner = exports.IModelTransformer = void 0;
|
|
3
|
+
exports.TemplateModelCloner = exports.IModelTransformer = exports.TransformerEvent = void 0;
|
|
4
4
|
/*---------------------------------------------------------------------------------------------
|
|
5
5
|
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
6
6
|
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
@@ -9,6 +9,7 @@ exports.TemplateModelCloner = exports.IModelTransformer = void 0;
|
|
|
9
9
|
* @module iModels
|
|
10
10
|
*/
|
|
11
11
|
const path = require("path");
|
|
12
|
+
const events_1 = require("events");
|
|
12
13
|
const Semver = require("semver");
|
|
13
14
|
const nodeAssert = require("assert");
|
|
14
15
|
const core_bentley_1 = require("@itwin/core-bentley");
|
|
@@ -85,6 +86,16 @@ function mapId64(idContainer, func) {
|
|
|
85
86
|
}
|
|
86
87
|
return results;
|
|
87
88
|
}
|
|
89
|
+
/** events that the transformer emits, e.g. for signaling profilers @internal */
|
|
90
|
+
var TransformerEvent;
|
|
91
|
+
(function (TransformerEvent) {
|
|
92
|
+
TransformerEvent["beginProcessSchemas"] = "beginProcessSchemas";
|
|
93
|
+
TransformerEvent["endProcessSchemas"] = "endProcessSchemas";
|
|
94
|
+
TransformerEvent["beginProcessAll"] = "beginProcessAll";
|
|
95
|
+
TransformerEvent["endProcessAll"] = "endProcessAll";
|
|
96
|
+
TransformerEvent["beginProcessChanges"] = "beginProcessChanges";
|
|
97
|
+
TransformerEvent["endProcessChanges"] = "endProcessChanges";
|
|
98
|
+
})(TransformerEvent = exports.TransformerEvent || (exports.TransformerEvent = {}));
|
|
88
99
|
/** Base class used to transform a source iModel into a different target iModel.
|
|
89
100
|
* @see [iModel Transformation and Data Exchange]($docs/learning/transformer/index.md), [IModelExporter]($transformer), [IModelImporter]($transformer)
|
|
90
101
|
* @beta
|
|
@@ -114,12 +125,26 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
114
125
|
this._pendingReferences = new PendingReferenceMap_1.PendingReferenceMap();
|
|
115
126
|
/** map of partially committed entities to their partial commit progress */
|
|
116
127
|
this._partiallyCommittedEntities = new EntityMap_1.EntityMap();
|
|
128
|
+
/**
|
|
129
|
+
* Internal event emitter that is used by the transformer to signal events to profilers
|
|
130
|
+
* @internal
|
|
131
|
+
*/
|
|
132
|
+
this.events = new events_1.EventEmitter();
|
|
133
|
+
this._targetScopeProvenanceProps = undefined;
|
|
134
|
+
this._cachedTargetScopeVersion = undefined;
|
|
135
|
+
// if undefined, it can be initialized by calling [[this._cacheSourceChanges]]
|
|
136
|
+
this._hasElementChangedCache = undefined;
|
|
137
|
+
this._deletedSourceRelationshipData = undefined;
|
|
117
138
|
this._yieldManager = new core_bentley_1.YieldManager();
|
|
118
139
|
/** The directory where schemas will be exported, a random temporary directory */
|
|
119
140
|
this._schemaExportDir = path.join(core_backend_1.KnownLocations.tmpdir, core_bentley_1.Guid.createValue());
|
|
120
141
|
this._longNamedSchemasMap = new Map();
|
|
121
142
|
/** state to prevent reinitialization, @see [[initialize]] */
|
|
122
143
|
this._initialized = false;
|
|
144
|
+
/** length === 0 when _changeDataState = "no-change", length > 0 means "has-changes", otherwise undefined */
|
|
145
|
+
this._changeSummaryIds = undefined;
|
|
146
|
+
this._changeDataState = "uninited";
|
|
147
|
+
/** previous provenance, either a federation guid, a `${sourceFedGuid}/${targetFedGuid}` pair, or required aspect props */
|
|
123
148
|
this._lastProvenanceEntityInfo = nullLastProvenanceEntityInfo;
|
|
124
149
|
// initialize IModelTransformOptions
|
|
125
150
|
this._options = {
|
|
@@ -167,6 +192,16 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
167
192
|
this.targetDb = this.importer.targetDb;
|
|
168
193
|
// create the IModelCloneContext, it must be initialized later
|
|
169
194
|
this.context = new IModelCloneContext_1.IModelCloneContext(this.sourceDb, this.targetDb);
|
|
195
|
+
this._registerEvents();
|
|
196
|
+
}
|
|
197
|
+
/** @internal */
|
|
198
|
+
_registerEvents() {
|
|
199
|
+
this.events.on(TransformerEvent.beginProcessAll, () => {
|
|
200
|
+
core_bentley_1.Logger.logTrace(loggerCategory, "processAll()");
|
|
201
|
+
});
|
|
202
|
+
this.events.on(TransformerEvent.beginProcessChanges, () => {
|
|
203
|
+
core_bentley_1.Logger.logTrace(loggerCategory, "processChanges()");
|
|
204
|
+
});
|
|
170
205
|
}
|
|
171
206
|
/** Dispose any native resources associated with this IModelTransformer. */
|
|
172
207
|
dispose() {
|
|
@@ -195,7 +230,12 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
195
230
|
get provenanceDb() {
|
|
196
231
|
return this._options.isReverseSynchronization ? this.sourceDb : this.targetDb;
|
|
197
232
|
}
|
|
198
|
-
/**
|
|
233
|
+
/** Return the IModelDb where IModelTransformer will NOT store its provenance.
|
|
234
|
+
* @note This will be [[sourceDb]] except when it is a reverse synchronization. In that case it be [[targetDb]].
|
|
235
|
+
*/
|
|
236
|
+
get provenanceSourceDb() {
|
|
237
|
+
return this._options.isReverseSynchronization ? this.targetDb : this.sourceDb;
|
|
238
|
+
}
|
|
199
239
|
initElementProvenance(sourceElementId, targetElementId) {
|
|
200
240
|
const elementId = this._options.isReverseSynchronization ? sourceElementId : targetElementId;
|
|
201
241
|
const aspectIdentifier = this._options.isReverseSynchronization ? targetElementId : sourceElementId;
|
|
@@ -226,10 +266,30 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
226
266
|
kind: core_backend_1.ExternalSourceAspect.Kind.Relationship,
|
|
227
267
|
jsonProperties: JSON.stringify({ targetRelInstanceId }),
|
|
228
268
|
};
|
|
229
|
-
aspectProps.id = this.
|
|
269
|
+
[aspectProps.id] = this.queryScopeExternalSource(aspectProps);
|
|
230
270
|
return aspectProps;
|
|
231
271
|
}
|
|
232
|
-
|
|
272
|
+
/** the changeset in the scoping element's source version found for this transformation
|
|
273
|
+
* @note: empty string and -1 for changeset and index if it has never been transformed
|
|
274
|
+
*/
|
|
275
|
+
get _targetScopeVersion() {
|
|
276
|
+
if (!this._cachedTargetScopeVersion) {
|
|
277
|
+
nodeAssert(this._targetScopeProvenanceProps?.version !== undefined, "_targetScopeProvenanceProps was not set yet, or contains no version");
|
|
278
|
+
const [id, index] = this._targetScopeProvenanceProps.version === ""
|
|
279
|
+
? ["", -1]
|
|
280
|
+
: this._targetScopeProvenanceProps.version.split(";");
|
|
281
|
+
this._cachedTargetScopeVersion = { index: Number(index), id, };
|
|
282
|
+
nodeAssert(!Number.isNaN(this._cachedTargetScopeVersion.index), "bad parse: invalid index in version");
|
|
283
|
+
}
|
|
284
|
+
return this._cachedTargetScopeVersion;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Make sure there are no conflicting other scope-type external source aspects on the *target scope element*,
|
|
288
|
+
* If there are none at all, insert one, then this must be a first synchronization.
|
|
289
|
+
* @returns the last synced version (changesetId) on the target scope's external source aspect,
|
|
290
|
+
* (if this was a [BriefcaseDb]($backend))
|
|
291
|
+
*/
|
|
292
|
+
initScopeProvenance() {
|
|
233
293
|
const aspectProps = {
|
|
234
294
|
classFullName: core_backend_1.ExternalSourceAspect.classFullName,
|
|
235
295
|
element: { id: this.targetScopeElementId, relClassName: core_backend_1.ElementOwnsExternalSourceAspects.classFullName },
|
|
@@ -237,10 +297,21 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
237
297
|
identifier: this._options.isReverseSynchronization ? this.targetDb.iModelId : this.sourceDb.iModelId,
|
|
238
298
|
kind: core_backend_1.ExternalSourceAspect.Kind.Scope,
|
|
239
299
|
};
|
|
240
|
-
|
|
300
|
+
// FIXME: handle older transformed iModels
|
|
301
|
+
let version;
|
|
302
|
+
[aspectProps.id, version] = this.queryScopeExternalSource(aspectProps) ?? []; // this query includes "identifier"
|
|
303
|
+
aspectProps.version = version;
|
|
241
304
|
if (undefined === aspectProps.id) {
|
|
305
|
+
aspectProps.version = ""; // empty since never before transformed. Will be updated in [[finalizeTransformation]]
|
|
242
306
|
// this query does not include "identifier" to find possible conflicts
|
|
243
|
-
const sql = `
|
|
307
|
+
const sql = `
|
|
308
|
+
SELECT ECInstanceId
|
|
309
|
+
FROM ${core_backend_1.ExternalSourceAspect.classFullName}
|
|
310
|
+
WHERE Element.Id=:elementId
|
|
311
|
+
AND Scope.Id=:scopeId
|
|
312
|
+
AND Kind=:kind
|
|
313
|
+
LIMIT 1
|
|
314
|
+
`;
|
|
244
315
|
const hasConflictingScope = this.provenanceDb.withPreparedStatement(sql, (statement) => {
|
|
245
316
|
statement.bindId("elementId", aspectProps.element.id);
|
|
246
317
|
statement.bindId("scopeId", aspectProps.scope.id); // this scope.id can never be invalid, we create it above
|
|
@@ -255,39 +326,97 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
255
326
|
this._isFirstSynchronization = true; // couldn't tell this is the first time without provenance
|
|
256
327
|
}
|
|
257
328
|
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
329
|
+
this._targetScopeProvenanceProps = aspectProps;
|
|
330
|
+
}
|
|
331
|
+
queryScopeExternalSource(aspectProps) {
|
|
332
|
+
const sql = `
|
|
333
|
+
SELECT ECInstanceId, Version
|
|
334
|
+
FROM ${core_backend_1.ExternalSourceAspect.classFullName}
|
|
335
|
+
WHERE Element.Id=:elementId
|
|
336
|
+
AND Scope.Id=:scopeId
|
|
337
|
+
AND Kind=:kind
|
|
338
|
+
AND Identifier=:identifier
|
|
339
|
+
LIMIT 1
|
|
340
|
+
`;
|
|
261
341
|
return this.provenanceDb.withPreparedStatement(sql, (statement) => {
|
|
262
342
|
statement.bindId("elementId", aspectProps.element.id);
|
|
263
343
|
if (aspectProps.scope === undefined)
|
|
264
|
-
return undefined; // return undefined instead of binding an invalid id
|
|
344
|
+
return [undefined, undefined]; // return undefined instead of binding an invalid id
|
|
265
345
|
statement.bindId("scopeId", aspectProps.scope.id);
|
|
266
346
|
statement.bindString("kind", aspectProps.kind);
|
|
267
347
|
statement.bindString("identifier", aspectProps.identifier);
|
|
268
|
-
|
|
348
|
+
if (core_bentley_1.DbResult.BE_SQLITE_ROW !== statement.step())
|
|
349
|
+
return [undefined, undefined];
|
|
350
|
+
const aspectId = statement.getValue(0).getId();
|
|
351
|
+
const version = statement.getValue(1).getString();
|
|
352
|
+
return [aspectId, version];
|
|
269
353
|
});
|
|
270
354
|
}
|
|
271
|
-
/**
|
|
355
|
+
/**
|
|
356
|
+
* Iterate all matching ExternalSourceAspects in the provenance iModel (target unless reverse sync) and call a function for each one.
|
|
357
|
+
* @note provenance is done by federation guids where possible
|
|
358
|
+
*/
|
|
272
359
|
forEachTrackedElement(fn) {
|
|
273
360
|
if (!this.provenanceDb.containsClass(core_backend_1.ExternalSourceAspect.classFullName)) {
|
|
274
361
|
throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadSchema, "The BisCore schema version of the target database is too old");
|
|
275
362
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
363
|
+
// query for provenanceDb
|
|
364
|
+
const provenanceContainerQuery = `
|
|
365
|
+
SELECT e.ECInstanceId, FederationGuid, esa.Identifier as AspectIdentifier
|
|
366
|
+
FROM bis.Element e
|
|
367
|
+
LEFT JOIN bis.ExternalSourceAspect esa ON e.ECInstanceId=esa.Element.Id
|
|
368
|
+
WHERE e.ECInstanceId NOT IN (0x1, 0xe, 0x10) -- special non-federated iModel-local elements
|
|
369
|
+
AND ((Scope.Id IS NULL AND KIND IS NULL) OR (Scope.Id=:scopeId AND Kind=:kind))
|
|
370
|
+
ORDER BY FederationGuid
|
|
371
|
+
`;
|
|
372
|
+
// query for nonProvenanceDb, the source to which the provenance is referring
|
|
373
|
+
const provenanceSourceQuery = `
|
|
374
|
+
SELECT e.ECInstanceId, FederationGuid
|
|
375
|
+
FROM bis.Element e
|
|
376
|
+
WHERE e.ECInstanceId NOT IN (0x1, 0xe, 0x10) -- special non-federated iModel-local elements
|
|
377
|
+
ORDER BY FederationGuid
|
|
378
|
+
`;
|
|
379
|
+
// iterate through sorted list of fed guids from both dbs to get the intersection
|
|
380
|
+
// NOTE: if we exposed the native attach database support,
|
|
381
|
+
// we could get the intersection of fed guids in one query, not sure if it would be faster
|
|
382
|
+
this.provenanceSourceDb.withStatement(provenanceSourceQuery, (sourceStmt) => this.provenanceDb.withStatement(provenanceContainerQuery, (containerStmt) => {
|
|
383
|
+
containerStmt.bindId("scopeId", this.targetScopeElementId);
|
|
384
|
+
containerStmt.bindString("kind", core_backend_1.ExternalSourceAspect.Kind.Element);
|
|
385
|
+
if (sourceStmt.step() !== core_bentley_1.DbResult.BE_SQLITE_ROW)
|
|
386
|
+
return;
|
|
387
|
+
let sourceRow = sourceStmt.getRow();
|
|
388
|
+
if (containerStmt.step() !== core_bentley_1.DbResult.BE_SQLITE_ROW)
|
|
389
|
+
return;
|
|
390
|
+
let containerRow = containerStmt.getRow();
|
|
391
|
+
const runFnInProvDirection = (sourceId, targetId) => this._options.isReverseSynchronization ? fn(sourceId, targetId) : fn(targetId, sourceId);
|
|
392
|
+
while (true) {
|
|
393
|
+
const currSourceRow = sourceRow, currContainerRow = containerRow;
|
|
394
|
+
if (currSourceRow.federationGuid !== undefined
|
|
395
|
+
&& currContainerRow.federationGuid !== undefined
|
|
396
|
+
&& currSourceRow.federationGuid === currContainerRow.federationGuid) {
|
|
397
|
+
fn(sourceRow.id, containerRow.id);
|
|
285
398
|
}
|
|
286
|
-
|
|
287
|
-
|
|
399
|
+
if (currContainerRow.federationGuid === undefined
|
|
400
|
+
|| (currSourceRow.federationGuid !== undefined
|
|
401
|
+
&& currSourceRow.federationGuid >= currContainerRow.federationGuid)) {
|
|
402
|
+
if (containerStmt.step() !== core_bentley_1.DbResult.BE_SQLITE_ROW)
|
|
403
|
+
return;
|
|
404
|
+
containerRow = containerStmt.getRow();
|
|
288
405
|
}
|
|
406
|
+
if (currSourceRow.federationGuid === undefined
|
|
407
|
+
|| (currContainerRow.federationGuid !== undefined
|
|
408
|
+
&& currSourceRow.federationGuid <= currContainerRow.federationGuid)) {
|
|
409
|
+
if (sourceStmt.step() !== core_bentley_1.DbResult.BE_SQLITE_ROW)
|
|
410
|
+
return;
|
|
411
|
+
sourceRow = sourceStmt.getRow();
|
|
412
|
+
}
|
|
413
|
+
// NOTE: needed test cases:
|
|
414
|
+
// - provenance container or provenance source has no fedguids
|
|
415
|
+
// - transforming split and join scenarios
|
|
416
|
+
if (!currContainerRow.federationGuid && currContainerRow.aspectIdentifier)
|
|
417
|
+
runFnInProvDirection(currContainerRow.id, currContainerRow.aspectIdentifier);
|
|
289
418
|
}
|
|
290
|
-
});
|
|
419
|
+
}));
|
|
291
420
|
}
|
|
292
421
|
/** Initialize the source to target Element mapping from ExternalSourceAspects in the target iModel.
|
|
293
422
|
* @note This method is called from all `process*` functions and should never need to be called directly.
|
|
@@ -300,62 +429,62 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
300
429
|
this.context.remapElement(sourceElementId, targetElementId);
|
|
301
430
|
});
|
|
302
431
|
if (args)
|
|
303
|
-
return this.remapDeletedSourceElements(
|
|
432
|
+
return this.remapDeletedSourceElements();
|
|
304
433
|
}
|
|
305
434
|
/** When processing deleted elements in a reverse synchronization, the [[provenanceDb]] (usually a branch iModel) has already
|
|
306
435
|
* deleted the [ExternalSourceAspect]($backend)s that tell us which elements in the reverse synchronization target (usually
|
|
307
436
|
* a master iModel) should be deleted. We must use the changesets to get the values of those before they were deleted.
|
|
308
437
|
*/
|
|
309
|
-
async remapDeletedSourceElements(
|
|
438
|
+
async remapDeletedSourceElements() {
|
|
310
439
|
// we need a connected iModel with changes to remap elements with deletions
|
|
311
|
-
|
|
440
|
+
const notConnectedModel = this.sourceDb.iTwinId === undefined;
|
|
441
|
+
const noChanges = this._targetScopeVersion.index === this.sourceDb.changeset.index;
|
|
442
|
+
if (notConnectedModel || noChanges)
|
|
312
443
|
return;
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
while (core_bentley_1.DbResult.BE_SQLITE_ROW === stmt.step()) {
|
|
347
|
-
const targetId = stmt.getValue(0).getId();
|
|
348
|
-
const sourceId = stmt.getValue(1).getString(); // BisCore.ExternalSourceAspect.Identifier stores a hex Id64String
|
|
349
|
-
// TODO: maybe delete and don't just remap
|
|
350
|
-
this.context.remapElement(targetId, sourceId);
|
|
351
|
-
}
|
|
352
|
-
});
|
|
444
|
+
nodeAssert(this._changeSummaryIds, "change summaries should be initialized before we get here");
|
|
445
|
+
nodeAssert(this._changeSummaryIds.length > 0, "change summaries should have at least one");
|
|
446
|
+
const deletedElemSql = `
|
|
447
|
+
SELECT ic.ChangedInstance.Id, ${this._coalesceChangeSummaryJoinedValue((_, i) => `ec${i}.FederationGuid`)}
|
|
448
|
+
FROM ecchange.change.InstanceChange ic
|
|
449
|
+
-- ask affan about whether this is worth it...
|
|
450
|
+
${this._changeSummaryIds.map((id, i) => `
|
|
451
|
+
LEFT JOIN bis.Element.Changes(${id}, 'BeforeDelete') ec${i}
|
|
452
|
+
ON ic.ChangedInstance.Id=ec${i}.ECInstanceId
|
|
453
|
+
`).join('')}
|
|
454
|
+
WHERE ic.OpCode=:opDelete
|
|
455
|
+
AND InVirtualSet(:changeSummaryIds, ic.Summary.Id)
|
|
456
|
+
-- not yet documented ecsql feature to check class id
|
|
457
|
+
AND ic.ChangedInstance.ClassId IS (BisCore.Element)
|
|
458
|
+
`;
|
|
459
|
+
// must also support old ESA provenance if no fedguids
|
|
460
|
+
this.sourceDb.withStatement(deletedElemSql, (stmt) => {
|
|
461
|
+
stmt.bindInteger("opDelete", core_common_1.ChangeOpCode.Delete);
|
|
462
|
+
stmt.bindIdSet("changeSummaryIds", this._changeSummaryIds);
|
|
463
|
+
// instead of targetScopeElementId, we only operate on elements
|
|
464
|
+
// that had colliding fed guids with the source...
|
|
465
|
+
// currently that is enforced by us checking that the deleted element fedguid is in both
|
|
466
|
+
// before remapping
|
|
467
|
+
while (core_bentley_1.DbResult.BE_SQLITE_ROW === stmt.step()) {
|
|
468
|
+
const sourceId = stmt.getValue(0).getId();
|
|
469
|
+
// FIXME: if I could attach the second db, will probably be much faster to get target id
|
|
470
|
+
const sourceFedGuid = stmt.getValue(1).getGuid();
|
|
471
|
+
const targetId = this.queryElemIdByFedGuid(this.targetDb, sourceFedGuid);
|
|
472
|
+
const deletionNotInTarget = !targetId;
|
|
473
|
+
if (deletionNotInTarget)
|
|
474
|
+
return;
|
|
475
|
+
// TODO: maybe delete and don't just remap
|
|
476
|
+
this.context.remapElement(sourceId, targetId);
|
|
353
477
|
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
queryElemIdByFedGuid(db, fedGuid) {
|
|
481
|
+
return db.withPreparedStatement("SELECT ECInstanceId FROM Bis.Element WHERE FederationGuid=?", (stmt) => {
|
|
482
|
+
stmt.bindGuid(1, fedGuid);
|
|
483
|
+
if (stmt.step() === core_bentley_1.DbResult.BE_SQLITE_ROW)
|
|
484
|
+
return stmt.getValue(0).getId();
|
|
485
|
+
else
|
|
486
|
+
return undefined;
|
|
487
|
+
});
|
|
359
488
|
}
|
|
360
489
|
/** Returns `true` if *brute force* delete detections should be run.
|
|
361
490
|
* @note Not relevant for processChanges when change history is known.
|
|
@@ -373,6 +502,9 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
373
502
|
* @throws [[IModelError]] If the required provenance information is not available to detect deletes.
|
|
374
503
|
*/
|
|
375
504
|
async detectElementDeletes() {
|
|
505
|
+
// FIXME: this is no longer possible to do without change data loading, but I don't think
|
|
506
|
+
// anyone uses this obscure feature, maybe we can remove it?
|
|
507
|
+
// NOTE: can implement this by checking for federation guids in the target that aren't
|
|
376
508
|
if (this._options.isReverseSynchronization) {
|
|
377
509
|
throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "Cannot detect deletes when isReverseSynchronization=true");
|
|
378
510
|
}
|
|
@@ -410,24 +542,85 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
410
542
|
}
|
|
411
543
|
return targetElementProps;
|
|
412
544
|
}
|
|
545
|
+
// handle sqlite coalesce requiring 2 arguments
|
|
546
|
+
_coalesceChangeSummaryJoinedValue(f) {
|
|
547
|
+
nodeAssert(this._changeSummaryIds?.length && this._changeSummaryIds.length > 0, "should have changeset data by now");
|
|
548
|
+
const valueList = this._changeSummaryIds.map(f).join(',');
|
|
549
|
+
return this._changeSummaryIds.length > 1 ? `coalesce(${valueList})` : valueList;
|
|
550
|
+
}
|
|
551
|
+
;
|
|
552
|
+
// FIXME: this is a PoC, don't load this all into memory
|
|
553
|
+
_cacheSourceChanges() {
|
|
554
|
+
nodeAssert(this._changeSummaryIds && this._changeSummaryIds.length > 0, "should have changeset data by now");
|
|
555
|
+
this._hasElementChangedCache = new Set();
|
|
556
|
+
this._deletedSourceRelationshipData = new Map();
|
|
557
|
+
// somewhat complicated query because doing two things at once...
|
|
558
|
+
// (not to mention the multijoin coalescing hack)
|
|
559
|
+
// FIXME: perhaps the coalescing indicates that part should be done manually, not in the query?
|
|
560
|
+
const query = `
|
|
561
|
+
SELECT
|
|
562
|
+
ic.ChangedInstance.Id AS InstId,
|
|
563
|
+
-- NOTE: parse error even with () without iif
|
|
564
|
+
iif(ic.ChangedInstance.ClassId IS (BisCore.Element), TRUE, FALSE) AS IsElemNotDeletedRel,
|
|
565
|
+
coalesce(${
|
|
566
|
+
// HACK: adding "NONE" for empty result seems to prevent a bug where getValue(3) stops working after the NULL columns
|
|
567
|
+
this._changeSummaryIds.map((_, i) => `se${i}.FederationGuid, sec${i}.FederationGuid`).concat("'NONE'").join(',')}) AS SourceFedGuid,
|
|
568
|
+
coalesce(${this._changeSummaryIds.map((_, i) => `te${i}.FederationGuid, tec${i}.FederationGuid`).concat("'NONE'").join(',')}) AS TargetFedGuid,
|
|
569
|
+
ic.ChangedInstance.ClassId AS ClassId
|
|
570
|
+
FROM ecchange.change.InstanceChange ic
|
|
571
|
+
JOIN iModelChange.Changeset imc ON ic.Summary.Id=imc.Summary.Id
|
|
572
|
+
-- ask affan about whether this is worth it... maybe the ""
|
|
573
|
+
${this._changeSummaryIds.map((id, i) => `
|
|
574
|
+
LEFT JOIN bis.ElementRefersToElements.Changes(${id}, 'BeforeDelete') ertec${i}
|
|
575
|
+
-- NOTE: see how the AND affects performance, it could be dropped
|
|
576
|
+
ON ic.ChangedInstance.Id=ertec${i}.ECInstanceId
|
|
577
|
+
AND NOT ic.ChangedInstance.ClassId IS (BisCore.Element)
|
|
578
|
+
-- FIXME: test a deletion of both an element and a relationship at the same time
|
|
579
|
+
LEFT JOIN bis.Element se${i}
|
|
580
|
+
ON se${i}.ECInstanceId=ertec${i}.SourceECInstanceId
|
|
581
|
+
LEFT JOIN bis.Element te${i}
|
|
582
|
+
ON te${i}.ECInstanceId=ertec${i}.TargetECInstanceId
|
|
583
|
+
LEFT JOIN bis.Element.Changes(${id}, 'BeforeDelete') sec${i}
|
|
584
|
+
ON sec${i}.ECInstanceId=ertec${i}.SourceECInstanceId
|
|
585
|
+
LEFT JOIN bis.Element.Changes(${id}, 'BeforeDelete') tec${i}
|
|
586
|
+
ON tec${i}.ECInstanceId=ertec${i}.TargetECInstanceId
|
|
587
|
+
`).join('')}
|
|
588
|
+
-- ignore deleted elems, we take care of those separately
|
|
589
|
+
WHERE ((ic.ChangedInstance.ClassId IS (BisCore.Element) AND ic.OpCode<>:opUpdate)
|
|
590
|
+
OR (ic.ChangedInstance.ClassId IS (BisCore.ElementRefersToElements) AND ic.OpCode=:opDelete))
|
|
591
|
+
`;
|
|
592
|
+
this.sourceDb.withPreparedStatement(query, (stmt) => {
|
|
593
|
+
stmt.bindInteger("opDelete", core_common_1.ChangeOpCode.Delete);
|
|
594
|
+
stmt.bindInteger("opUpdate", core_common_1.ChangeOpCode.Update);
|
|
595
|
+
while (core_bentley_1.DbResult.BE_SQLITE_ROW === stmt.step()) {
|
|
596
|
+
// REPORT: stmt.getValue(>3) seems to be bugged but the values survive .getRow so using that for now
|
|
597
|
+
const instId = stmt.getValue(0).getId();
|
|
598
|
+
const isElemNotDeletedRel = stmt.getValue(1).getBoolean();
|
|
599
|
+
if (isElemNotDeletedRel)
|
|
600
|
+
this._hasElementChangedCache.add(instId);
|
|
601
|
+
else {
|
|
602
|
+
const sourceFedGuid = stmt.getValue(2).getGuid();
|
|
603
|
+
const targetFedGuid = stmt.getValue(3).getGuid();
|
|
604
|
+
const classFullName = stmt.getValue(4).getClassNameForClassId();
|
|
605
|
+
this._deletedSourceRelationshipData.set(instId, { classFullName, sourceFedGuid, targetFedGuid });
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
}
|
|
413
610
|
/** Returns true if a change within sourceElement is detected.
|
|
414
611
|
* @param sourceElement The Element from the source iModel
|
|
415
612
|
* @param targetElementId The Element from the target iModel to compare against.
|
|
416
613
|
* @note A subclass can override this method to provide custom change detection behavior.
|
|
417
614
|
*/
|
|
418
|
-
hasElementChanged(sourceElement,
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
return lastModifiedTime !== sourceAspect.version;
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
return true;
|
|
615
|
+
hasElementChanged(sourceElement, _targetElementId) {
|
|
616
|
+
if (this._changeDataState === "no-changes")
|
|
617
|
+
return false;
|
|
618
|
+
if (this._changeDataState === "unconnected")
|
|
619
|
+
return true;
|
|
620
|
+
nodeAssert(this._changeDataState === "has-changes", "change data should be initialized by now");
|
|
621
|
+
if (this._hasElementChangedCache === undefined)
|
|
622
|
+
this._cacheSourceChanges();
|
|
623
|
+
return this._hasElementChangedCache.has(sourceElement.id);
|
|
431
624
|
}
|
|
432
625
|
static transformCallbackFor(transformer, entity) {
|
|
433
626
|
if (entity instanceof core_backend_1.Element)
|
|
@@ -615,12 +808,10 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
615
808
|
}
|
|
616
809
|
}
|
|
617
810
|
}
|
|
618
|
-
if (
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
}
|
|
623
|
-
}
|
|
811
|
+
if (targetElementId !== undefined
|
|
812
|
+
&& core_bentley_1.Id64.isValid(targetElementId)
|
|
813
|
+
&& !this.hasElementChanged(sourceElement, targetElementId))
|
|
814
|
+
return;
|
|
624
815
|
this.collectUnmappedReferences(sourceElement);
|
|
625
816
|
// TODO: untangle targetElementId state...
|
|
626
817
|
if (targetElementId === core_bentley_1.Id64.invalid)
|
|
@@ -632,17 +823,29 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
632
823
|
this.context.remapElement(sourceElement.id, targetElementProps.id); // targetElementProps.id assigned by importElement
|
|
633
824
|
// now that we've mapped this elem we can fix unmapped references to it
|
|
634
825
|
this.resolvePendingReferences(sourceElement);
|
|
826
|
+
// the transformer does not currently 'split' or 'join' any elements, therefore, it does not
|
|
827
|
+
// insert external source aspects because federation guids are sufficient for this.
|
|
828
|
+
// Other transformer subclasses must insert the appropriate aspect (as provided by a TBD API)
|
|
829
|
+
// when splitting/joining elements
|
|
830
|
+
// physical consolidation is an example of a 'joining' transform
|
|
831
|
+
// FIXME: document this externally!
|
|
832
|
+
// verify at finalization time that we don't lose provenance on new elements
|
|
833
|
+
// make public and improve `initElementProvenance` API for usage by consolidators
|
|
635
834
|
if (!this._options.noProvenance) {
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
aspectId = this.
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
835
|
+
let provenance = sourceElement.federationGuid;
|
|
836
|
+
if (!provenance) {
|
|
837
|
+
const aspectProps = this.initElementProvenance(sourceElement.id, targetElementProps.id);
|
|
838
|
+
let [aspectId] = this.queryScopeExternalSource(aspectProps);
|
|
839
|
+
if (aspectId === undefined) {
|
|
840
|
+
aspectId = this.provenanceDb.elements.insertAspect(aspectProps);
|
|
841
|
+
}
|
|
842
|
+
else {
|
|
843
|
+
this.provenanceDb.elements.updateAspect(aspectProps);
|
|
844
|
+
}
|
|
845
|
+
aspectProps.id = aspectId;
|
|
846
|
+
provenance = aspectProps;
|
|
643
847
|
}
|
|
644
|
-
|
|
645
|
-
this.markLastProvenance(aspectProps, { isRelationship: false });
|
|
848
|
+
this.markLastProvenance(provenance, { isRelationship: false });
|
|
646
849
|
}
|
|
647
850
|
}
|
|
648
851
|
resolvePendingReferences(entity) {
|
|
@@ -756,7 +959,20 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
756
959
|
* @deprecated in 3.x. This method is no longer necessary since the transformer no longer needs to defer elements
|
|
757
960
|
*/
|
|
758
961
|
async processDeferredElements(_numRetries = 3) { }
|
|
962
|
+
/** called at the end ([[finalizeTransformation]]) of a transformation,
|
|
963
|
+
* updates the target scope element to say that transformation up through the
|
|
964
|
+
* source's changeset has been performed.
|
|
965
|
+
*/
|
|
966
|
+
_updateTargetScopeVersion() {
|
|
967
|
+
nodeAssert(this._targetScopeProvenanceProps);
|
|
968
|
+
if (this._changeDataState === "has-changes") {
|
|
969
|
+
this._targetScopeProvenanceProps.version = `${this.provenanceSourceDb.changeset.id};${this.provenanceSourceDb.changeset.index}`;
|
|
970
|
+
this.provenanceDb.elements.updateAspect(this._targetScopeProvenanceProps);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
// FIXME: is this necessary when manually using lowlevel transform APIs?
|
|
759
974
|
finalizeTransformation() {
|
|
975
|
+
this._updateTargetScopeVersion();
|
|
760
976
|
if (this._partiallyCommittedEntities.size > 0) {
|
|
761
977
|
core_bentley_1.Logger.logWarning(loggerCategory, [
|
|
762
978
|
"The following elements were never fully resolved:",
|
|
@@ -768,6 +984,9 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
768
984
|
partiallyCommittedElem.forceComplete();
|
|
769
985
|
}
|
|
770
986
|
}
|
|
987
|
+
// FIXME: make processAll have a try {} finally {} that cleans this up
|
|
988
|
+
if (core_backend_1.ChangeSummaryManager.isChangeCacheAttached(this.sourceDb))
|
|
989
|
+
core_backend_1.ChangeSummaryManager.detachChangeCache(this.sourceDb);
|
|
771
990
|
}
|
|
772
991
|
/** Imports all relationships that subclass from the specified base class.
|
|
773
992
|
* @param baseRelClassFullName The specified base relationship class.
|
|
@@ -785,40 +1004,84 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
785
1004
|
* This override calls [[onTransformRelationship]] and then [IModelImporter.importRelationship]($transformer) to update the target iModel.
|
|
786
1005
|
*/
|
|
787
1006
|
onExportRelationship(sourceRelationship) {
|
|
1007
|
+
const sourceFedGuid = queryElemFedGuid(this.sourceDb, sourceRelationship.sourceId);
|
|
1008
|
+
const targetFedGuid = queryElemFedGuid(this.sourceDb, sourceRelationship.targetId);
|
|
788
1009
|
const targetRelationshipProps = this.onTransformRelationship(sourceRelationship);
|
|
789
1010
|
const targetRelationshipInstanceId = this.importer.importRelationship(targetRelationshipProps);
|
|
790
|
-
if (!this._options.noProvenance && core_bentley_1.Id64.
|
|
791
|
-
|
|
792
|
-
if (
|
|
793
|
-
aspectProps
|
|
1011
|
+
if (!this._options.noProvenance && core_bentley_1.Id64.isValid(targetRelationshipInstanceId)) {
|
|
1012
|
+
let provenance = sourceFedGuid && targetFedGuid && `${sourceFedGuid}/${targetFedGuid}`;
|
|
1013
|
+
if (!provenance) {
|
|
1014
|
+
const aspectProps = this.initRelationshipProvenance(sourceRelationship, targetRelationshipInstanceId);
|
|
1015
|
+
if (undefined === aspectProps.id) {
|
|
1016
|
+
aspectProps.id = this.provenanceDb.elements.insertAspect(aspectProps);
|
|
1017
|
+
}
|
|
1018
|
+
(0, core_bentley_1.assert)(aspectProps.id !== undefined);
|
|
1019
|
+
provenance = aspectProps;
|
|
794
1020
|
}
|
|
795
|
-
(
|
|
796
|
-
this.markLastProvenance(aspectProps, { isRelationship: true });
|
|
1021
|
+
this.markLastProvenance(provenance, { isRelationship: true });
|
|
797
1022
|
}
|
|
798
1023
|
}
|
|
1024
|
+
// FIXME: need to check if the element class was remapped and use that id instead
|
|
1025
|
+
// is this really the best way to get class id? shouldn't we cache it somewhere?
|
|
1026
|
+
// NOTE: maybe if we lower remapElementClass into here, we can use that
|
|
1027
|
+
_getRelClassId(db, classFullName) {
|
|
1028
|
+
// is it better to use un-cached `SELECT (ONLY ${classFullName})`?
|
|
1029
|
+
return db.withPreparedStatement(`
|
|
1030
|
+
SELECT c.ECInstanceId
|
|
1031
|
+
FROM ECDbMeta.ECClassDef c
|
|
1032
|
+
JOIN ECDbMeta.ECSchemaDef s ON c.Schema.Id=s.ECInstanceId
|
|
1033
|
+
WHERE s.Name=? AND c.Name=?
|
|
1034
|
+
`, (stmt) => {
|
|
1035
|
+
const [schemaName, className] = classFullName.split(".");
|
|
1036
|
+
stmt.bindString(1, schemaName);
|
|
1037
|
+
stmt.bindString(2, className);
|
|
1038
|
+
if (stmt.step() === core_bentley_1.DbResult.BE_SQLITE_ROW)
|
|
1039
|
+
return stmt.getValue(0).getId();
|
|
1040
|
+
(0, core_bentley_1.assert)(false, "relationship was not found");
|
|
1041
|
+
});
|
|
1042
|
+
}
|
|
799
1043
|
/** Override of [IModelExportHandler.onDeleteRelationship]($transformer) that is called when [IModelExporter]($transformer) detects that a [Relationship]($backend) has been deleted from the source iModel.
|
|
800
1044
|
* This override propagates the delete to the target iModel via [IModelImporter.deleteRelationship]($transformer).
|
|
801
1045
|
*/
|
|
802
1046
|
onDeleteRelationship(sourceRelInstanceId) {
|
|
803
|
-
|
|
804
|
-
|
|
1047
|
+
nodeAssert(this._deletedSourceRelationshipData, "should be defined at initialization by now");
|
|
1048
|
+
const deletedRelData = this._deletedSourceRelationshipData.get(sourceRelInstanceId);
|
|
1049
|
+
if (!deletedRelData) {
|
|
1050
|
+
core_bentley_1.Logger.logWarning(loggerCategory, "tried to delete a relationship that wasn't in change data");
|
|
1051
|
+
return;
|
|
1052
|
+
}
|
|
1053
|
+
const targetRelClassId = this._getRelClassId(this.targetDb, deletedRelData.classFullName);
|
|
1054
|
+
// NOTE: if no remapping, could store the sourceRel class name earlier and reuse it instead of add to query
|
|
1055
|
+
// TODO: name this query
|
|
1056
|
+
const sql = `
|
|
1057
|
+
SELECT SourceECInstanceId, TargetECInstanceId, erte.ECClassId
|
|
1058
|
+
FROM BisCore.ElementRefersToElements erte
|
|
1059
|
+
JOIN BisCore.Element se ON se.ECInstanceId=SourceECInstanceId
|
|
1060
|
+
JOIN BisCore.Element te ON te.ECInstanceId=TargetECInstanceId
|
|
1061
|
+
WHERE se.FederationGuid=:sourceFedGuid
|
|
1062
|
+
AND te.FederationGuid=:targetFedGuid
|
|
1063
|
+
AND erte.ECClassId=:relClassId
|
|
1064
|
+
`;
|
|
805
1065
|
this.targetDb.withPreparedStatement(sql, (statement) => {
|
|
806
|
-
statement.
|
|
807
|
-
statement.
|
|
808
|
-
statement.
|
|
1066
|
+
statement.bindGuid("sourceFedGuid", deletedRelData.sourceFedGuid);
|
|
1067
|
+
statement.bindGuid("targetFedGuid", deletedRelData.targetFedGuid);
|
|
1068
|
+
statement.bindId("relClassId", targetRelClassId);
|
|
809
1069
|
if (core_bentley_1.DbResult.BE_SQLITE_ROW === statement.step()) {
|
|
810
|
-
const
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
this.
|
|
1070
|
+
const sourceId = statement.getValue(0).getId();
|
|
1071
|
+
const targetId = statement.getValue(1).getId();
|
|
1072
|
+
const targetRelClassFullName = statement.getValue(2).getClassNameForClassId();
|
|
1073
|
+
// FIXME: make importer.deleteRelationship not need full props
|
|
1074
|
+
const targetRelationship = this.targetDb.relationships.tryGetInstance(targetRelClassFullName, { sourceId, targetId });
|
|
1075
|
+
if (targetRelationship) {
|
|
1076
|
+
this.importer.deleteRelationship(targetRelationship.toJSON());
|
|
817
1077
|
}
|
|
1078
|
+
// FIXME: restore in ESA compatible method
|
|
1079
|
+
//this.targetDb.elements.deleteAspect(statement.getValue(0).getId());
|
|
818
1080
|
}
|
|
819
1081
|
});
|
|
820
1082
|
}
|
|
821
1083
|
/** Detect Relationship deletes using ExternalSourceAspects in the target iModel and a *brute force* comparison against relationships in the source iModel.
|
|
1084
|
+
* @deprecated
|
|
822
1085
|
* @see processChanges
|
|
823
1086
|
* @note This method is called from [[processAll]] and is not needed by [[processChanges]], so it only needs to be called directly when processing a subset of an iModel.
|
|
824
1087
|
* @throws [[IModelError]] If the required provenance information is not available to detect deletes.
|
|
@@ -828,7 +1091,12 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
828
1091
|
throw new core_common_1.IModelError(core_bentley_1.IModelStatus.BadRequest, "Cannot detect deletes when isReverseSynchronization=true");
|
|
829
1092
|
}
|
|
830
1093
|
const aspectDeleteIds = [];
|
|
831
|
-
const sql = `
|
|
1094
|
+
const sql = `
|
|
1095
|
+
SELECT ECInstanceId, Identifier, JsonProperties
|
|
1096
|
+
FROM ${core_backend_1.ExternalSourceAspect.classFullName} aspect
|
|
1097
|
+
WHERE aspect.Scope.Id=:scopeId
|
|
1098
|
+
AND aspect.Kind=:kind
|
|
1099
|
+
`;
|
|
832
1100
|
await this.targetDb.withPreparedStatement(sql, async (statement) => {
|
|
833
1101
|
statement.bindId("scopeId", this.targetScopeElementId);
|
|
834
1102
|
statement.bindString("kind", core_backend_1.ExternalSourceAspect.Kind.Relationship);
|
|
@@ -856,6 +1124,7 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
856
1124
|
const targetRelationshipProps = sourceRelationship.toJSON();
|
|
857
1125
|
targetRelationshipProps.sourceId = this.context.findTargetElementId(sourceRelationship.sourceId);
|
|
858
1126
|
targetRelationshipProps.targetId = this.context.findTargetElementId(sourceRelationship.targetId);
|
|
1127
|
+
// TODO: move to cloneRelationship in IModelCloneContext
|
|
859
1128
|
sourceRelationship.forEachProperty((propertyName, propertyMetaData) => {
|
|
860
1129
|
if ((core_common_1.PrimitiveTypeCode.Long === propertyMetaData.primitiveType) && ("Id" === propertyMetaData.extendedType)) {
|
|
861
1130
|
targetRelationshipProps[propertyName] = this.context.findTargetElementId(sourceRelationship.asAny[propertyName]);
|
|
@@ -953,6 +1222,7 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
953
1222
|
* It is more efficient to process *data* changes after the schema changes have been saved.
|
|
954
1223
|
*/
|
|
955
1224
|
async processSchemas() {
|
|
1225
|
+
this.events.emit(TransformerEvent.beginProcessSchemas);
|
|
956
1226
|
// we do not need to initialize for this since no entities are exported
|
|
957
1227
|
try {
|
|
958
1228
|
core_backend_1.IModelJsFs.mkdirSync(this._schemaExportDir);
|
|
@@ -970,6 +1240,7 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
970
1240
|
finally {
|
|
971
1241
|
core_backend_1.IModelJsFs.removeSync(this._schemaExportDir);
|
|
972
1242
|
this._longNamedSchemasMap.clear();
|
|
1243
|
+
this.events.emit(TransformerEvent.endProcessSchemas);
|
|
973
1244
|
}
|
|
974
1245
|
}
|
|
975
1246
|
/** Cause all fonts to be exported from the source iModel and imported into the target iModel.
|
|
@@ -1026,17 +1297,53 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
1026
1297
|
if (this._initialized)
|
|
1027
1298
|
return;
|
|
1028
1299
|
await this.context.initialize();
|
|
1300
|
+
await this._tryInitChangesetData(args);
|
|
1029
1301
|
// eslint-disable-next-line deprecation/deprecation
|
|
1030
1302
|
await this.initFromExternalSourceAspects(args);
|
|
1031
1303
|
this._initialized = true;
|
|
1032
1304
|
}
|
|
1305
|
+
async _tryInitChangesetData(args) {
|
|
1306
|
+
if (!args || this.sourceDb.iTwinId === undefined) {
|
|
1307
|
+
this._changeDataState = "unconnected";
|
|
1308
|
+
return;
|
|
1309
|
+
}
|
|
1310
|
+
const noChanges = this._targetScopeVersion.index === this.sourceDb.changeset.index;
|
|
1311
|
+
if (noChanges) {
|
|
1312
|
+
this._changeDataState = "no-changes";
|
|
1313
|
+
this._changeSummaryIds = [];
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
// NOTE: that we do NOT download the changesummary for the last transformed version, we want
|
|
1317
|
+
// to ignore those already processed changes
|
|
1318
|
+
const startChangesetIndexOrId = args?.startChangesetId ?? this._targetScopeVersion.index + 1;
|
|
1319
|
+
const endChangesetId = this.sourceDb.changeset.id;
|
|
1320
|
+
const [startChangesetIndex, endChangesetIndex] = await Promise.all(([startChangesetIndexOrId, endChangesetId])
|
|
1321
|
+
.map(async (indexOrId) => typeof indexOrId === "number"
|
|
1322
|
+
? indexOrId
|
|
1323
|
+
: core_backend_1.IModelHost.hubAccess
|
|
1324
|
+
.queryChangeset({
|
|
1325
|
+
iModelId: this.sourceDb.iModelId,
|
|
1326
|
+
changeset: { id: indexOrId },
|
|
1327
|
+
accessToken: args.accessToken,
|
|
1328
|
+
})
|
|
1329
|
+
.then((changeset) => changeset.index)));
|
|
1330
|
+
// FIXME: do we need the startChangesetId?
|
|
1331
|
+
this._changeSummaryIds = await core_backend_1.ChangeSummaryManager.createChangeSummaries({
|
|
1332
|
+
accessToken: args.accessToken,
|
|
1333
|
+
iModelId: this.sourceDb.iModelId,
|
|
1334
|
+
iTwinId: this.sourceDb.iTwinId,
|
|
1335
|
+
range: { first: startChangesetIndex, end: endChangesetIndex },
|
|
1336
|
+
});
|
|
1337
|
+
core_backend_1.ChangeSummaryManager.attachChangeCache(this.sourceDb);
|
|
1338
|
+
this._changeDataState = "has-changes";
|
|
1339
|
+
}
|
|
1033
1340
|
/** Export everything from the source iModel and import the transformed entities into the target iModel.
|
|
1034
1341
|
* @note [[processSchemas]] is not called automatically since the target iModel may want a different collection of schemas.
|
|
1035
1342
|
*/
|
|
1036
1343
|
async processAll() {
|
|
1037
|
-
|
|
1344
|
+
this.events.emit(TransformerEvent.beginProcessAll);
|
|
1038
1345
|
this.logSettings();
|
|
1039
|
-
this.
|
|
1346
|
+
this.initScopeProvenance();
|
|
1040
1347
|
await this.initialize();
|
|
1041
1348
|
await this.exporter.exportCodeSpecs();
|
|
1042
1349
|
await this.exporter.exportFonts();
|
|
@@ -1054,14 +1361,18 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
1054
1361
|
this.importer.optimizeGeometry(this._options.optimizeGeometry);
|
|
1055
1362
|
this.importer.computeProjectExtents();
|
|
1056
1363
|
this.finalizeTransformation();
|
|
1364
|
+
this.events.emit(TransformerEvent.endProcessAll);
|
|
1057
1365
|
}
|
|
1058
1366
|
markLastProvenance(sourceAspect, { isRelationship = false }) {
|
|
1059
|
-
this._lastProvenanceEntityInfo
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1367
|
+
this._lastProvenanceEntityInfo
|
|
1368
|
+
= typeof sourceAspect === "string"
|
|
1369
|
+
? sourceAspect
|
|
1370
|
+
: {
|
|
1371
|
+
entityId: sourceAspect.element.id,
|
|
1372
|
+
aspectId: sourceAspect.id,
|
|
1373
|
+
aspectVersion: sourceAspect.version ?? "",
|
|
1374
|
+
aspectKind: isRelationship ? core_backend_1.ExternalSourceAspect.Kind.Relationship : core_backend_1.ExternalSourceAspect.Kind.Element,
|
|
1375
|
+
};
|
|
1065
1376
|
}
|
|
1066
1377
|
/**
|
|
1067
1378
|
* Load the state of the active transformation from an open SQLiteDb
|
|
@@ -1073,17 +1384,35 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
1073
1384
|
const lastProvenanceEntityInfo = db.withSqliteStatement(`SELECT entityId, aspectId, aspectVersion, aspectKind FROM ${IModelTransformer.lastProvenanceEntityInfoTable}`, (stmt) => {
|
|
1074
1385
|
if (core_bentley_1.DbResult.BE_SQLITE_ROW !== stmt.step())
|
|
1075
1386
|
throw Error("expected row when getting lastProvenanceEntityId from target state table");
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1387
|
+
const entityId = stmt.getValueString(0);
|
|
1388
|
+
const isGuidOrGuidPair = entityId.includes('-');
|
|
1389
|
+
return isGuidOrGuidPair
|
|
1390
|
+
? entityId
|
|
1391
|
+
: {
|
|
1392
|
+
entityId,
|
|
1393
|
+
aspectId: stmt.getValueString(1),
|
|
1394
|
+
aspectVersion: stmt.getValueString(2),
|
|
1395
|
+
aspectKind: stmt.getValueString(3),
|
|
1396
|
+
};
|
|
1082
1397
|
});
|
|
1083
|
-
|
|
1084
|
-
//
|
|
1085
|
-
|
|
1398
|
+
/*
|
|
1399
|
+
// TODO: maybe save transformer state resumption state based on target changset and require calls
|
|
1400
|
+
// to saveChanges
|
|
1401
|
+
if () {
|
|
1402
|
+
const [sourceFedGuid, targetFedGuid, relClassFullName] = lastProvenanceEntityInfo.split("/");
|
|
1403
|
+
const isRelProvenance = targetFedGuid !== undefined;
|
|
1404
|
+
const instanceId = isRelProvenance
|
|
1405
|
+
? this.targetDb.elements.getElement({federationGuid: sourceFedGuid})
|
|
1406
|
+
: "";
|
|
1407
|
+
//const classId =
|
|
1408
|
+
if (isRelProvenance) {
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
*/
|
|
1412
|
+
const targetHasCorrectLastProvenance = typeof lastProvenanceEntityInfo === "string" ||
|
|
1413
|
+
// ignore provenance check if it's null since we can't bind those ids
|
|
1086
1414
|
!core_bentley_1.Id64.isValidId64(lastProvenanceEntityInfo.entityId) ||
|
|
1415
|
+
!core_bentley_1.Id64.isValidId64(lastProvenanceEntityInfo.aspectId) ||
|
|
1087
1416
|
this.provenanceDb.withPreparedStatement(`
|
|
1088
1417
|
SELECT Version FROM ${core_backend_1.ExternalSourceAspect.classFullName}
|
|
1089
1418
|
WHERE Scope.Id=:scopeId
|
|
@@ -1180,8 +1509,9 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
1180
1509
|
throw Error("Failed to create the js state table in the state database");
|
|
1181
1510
|
if (core_bentley_1.DbResult.BE_SQLITE_DONE !== db.executeSQL(`
|
|
1182
1511
|
CREATE TABLE ${IModelTransformer.lastProvenanceEntityInfoTable} (
|
|
1183
|
-
--
|
|
1512
|
+
-- either the invalid id for null provenance state, federation guid (or pair for rels) of the entity, or a hex element id
|
|
1184
1513
|
entityId TEXT,
|
|
1514
|
+
-- the following are only valid if the above entityId is a hex id representation
|
|
1185
1515
|
aspectId TEXT,
|
|
1186
1516
|
aspectVersion TEXT,
|
|
1187
1517
|
aspectKind TEXT
|
|
@@ -1195,10 +1525,11 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
1195
1525
|
throw Error("Failed to insert options into the state database");
|
|
1196
1526
|
});
|
|
1197
1527
|
db.withSqliteStatement(`INSERT INTO ${IModelTransformer.lastProvenanceEntityInfoTable} (entityId, aspectId, aspectVersion, aspectKind) VALUES (?,?,?,?)`, (stmt) => {
|
|
1198
|
-
|
|
1199
|
-
stmt.bindString(
|
|
1200
|
-
stmt.bindString(
|
|
1201
|
-
stmt.bindString(
|
|
1528
|
+
const lastProvenanceEntityInfo = this._lastProvenanceEntityInfo;
|
|
1529
|
+
stmt.bindString(1, lastProvenanceEntityInfo?.entityId ?? this._lastProvenanceEntityInfo);
|
|
1530
|
+
stmt.bindString(2, lastProvenanceEntityInfo?.aspectId ?? "");
|
|
1531
|
+
stmt.bindString(3, lastProvenanceEntityInfo?.aspectVersion ?? "");
|
|
1532
|
+
stmt.bindString(4, lastProvenanceEntityInfo?.aspectKind ?? "");
|
|
1202
1533
|
if (core_bentley_1.DbResult.BE_SQLITE_DONE !== stmt.step())
|
|
1203
1534
|
throw Error("Failed to insert options into the state database");
|
|
1204
1535
|
});
|
|
@@ -1227,16 +1558,16 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
1227
1558
|
}
|
|
1228
1559
|
}
|
|
1229
1560
|
/** Export changes from the source iModel and import the transformed entities into the target iModel.
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1561
|
+
* Inserts, updates, and deletes are determined by inspecting the changeset(s).
|
|
1562
|
+
* @param accessToken A valid access token string
|
|
1563
|
+
* @param startChangesetId Include changes from this changeset up through and including the current changeset.
|
|
1564
|
+
* If this parameter is not provided, then just the current changeset will be exported.
|
|
1565
|
+
* @note To form a range of versions to process, set `startChangesetId` for the start (inclusive) of the desired range and open the source iModel as of the end (inclusive) of the desired range.
|
|
1566
|
+
*/
|
|
1236
1567
|
async processChanges(accessToken, startChangesetId) {
|
|
1237
|
-
|
|
1568
|
+
this.events.emit(TransformerEvent.beginProcessChanges, startChangesetId);
|
|
1238
1569
|
this.logSettings();
|
|
1239
|
-
this.
|
|
1570
|
+
this.initScopeProvenance();
|
|
1240
1571
|
await this.initialize({ accessToken, startChangesetId });
|
|
1241
1572
|
await this.exporter.exportChanges(accessToken, startChangesetId);
|
|
1242
1573
|
await this.processDeferredElements(); // eslint-disable-line deprecation/deprecation
|
|
@@ -1244,6 +1575,7 @@ class IModelTransformer extends IModelExporter_1.IModelExportHandler {
|
|
|
1244
1575
|
this.importer.optimizeGeometry(this._options.optimizeGeometry);
|
|
1245
1576
|
this.importer.computeProjectExtents();
|
|
1246
1577
|
this.finalizeTransformation();
|
|
1578
|
+
this.events.emit(TransformerEvent.endProcessChanges);
|
|
1247
1579
|
}
|
|
1248
1580
|
}
|
|
1249
1581
|
/** @internal the name of the table where javascript state of the transformer is serialized in transformer state dumps */
|
|
@@ -1348,4 +1680,17 @@ class TemplateModelCloner extends IModelTransformer {
|
|
|
1348
1680
|
}
|
|
1349
1681
|
}
|
|
1350
1682
|
exports.TemplateModelCloner = TemplateModelCloner;
|
|
1683
|
+
function queryElemFedGuid(db, elemId) {
|
|
1684
|
+
return db.withPreparedStatement(`
|
|
1685
|
+
SELECT FederationGuid
|
|
1686
|
+
FROM bis.Element
|
|
1687
|
+
WHERE ECInstanceId=?
|
|
1688
|
+
`, (stmt) => {
|
|
1689
|
+
stmt.bindId(1, elemId);
|
|
1690
|
+
(0, core_bentley_1.assert)(stmt.step() === core_bentley_1.DbResult.BE_SQLITE_ROW);
|
|
1691
|
+
const result = stmt.getValue(0).getGuid();
|
|
1692
|
+
(0, core_bentley_1.assert)(stmt.step() === core_bentley_1.DbResult.BE_SQLITE_DONE);
|
|
1693
|
+
return result;
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1351
1696
|
//# sourceMappingURL=IModelTransformer.js.map
|