@itwin/core-backend 4.7.0-dev.9 → 4.8.0-dev.1

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.
@@ -646,6 +646,7 @@ class IModelDb extends core_common_1.IModel {
646
646
  * @param {SchemaImportOptions} options - options during schema import.
647
647
  * @throws [[IModelError]] if the schema lock cannot be obtained or there is a problem importing the schema.
648
648
  * @note Changes are saved if importSchemas is successful and abandoned if not successful.
649
+ * - You can use NativeLoggerCategory to turn on the native logs. You can also control [what exactly is logged by the loggers](https://www.itwinjs.org/learning/common/logging/#controlling-what-is-logged).
649
650
  * @see querySchemaVersion
650
651
  */
651
652
  async importSchemas(schemaFileNames, options) {
@@ -656,15 +657,25 @@ class IModelDb extends core_common_1.IModel {
656
657
  await SchemaSync_1.SchemaSync.withLockedAccess(this, { openMode: core_bentley_1.OpenMode.Readonly, operationName: "schema sync" }, async (syncAccess) => {
657
658
  const schemaSyncDbUri = syncAccess.getUri();
658
659
  this.saveChanges();
659
- let stat = this.nativeDb.importSchemas(schemaFileNames, { schemaLockHeld: false, ecSchemaXmlContext: maybeCustomNativeContext, schemaSyncDbUri });
660
- if (core_bentley_1.DbResult.BE_SQLITE_ERROR_SchemaLockFailed === stat) {
661
- this.abandonChanges();
662
- if (this.nativeDb.getITwinId() !== core_bentley_1.Guid.empty)
663
- await this.acquireSchemaLock();
664
- stat = this.nativeDb.importSchemas(schemaFileNames, { schemaLockHeld: true, ecSchemaXmlContext: maybeCustomNativeContext, schemaSyncDbUri });
660
+ try {
661
+ this.nativeDb.importSchemas(schemaFileNames, { schemaLockHeld: false, ecSchemaXmlContext: maybeCustomNativeContext, schemaSyncDbUri });
662
+ }
663
+ catch (outerErr) {
664
+ if (core_bentley_1.DbResult.BE_SQLITE_ERROR_DataTransformRequired === outerErr.errorNumber) {
665
+ this.abandonChanges();
666
+ if (this.nativeDb.getITwinId() !== core_bentley_1.Guid.empty)
667
+ await this.acquireSchemaLock();
668
+ try {
669
+ this.nativeDb.importSchemas(schemaFileNames, { schemaLockHeld: true, ecSchemaXmlContext: maybeCustomNativeContext, schemaSyncDbUri });
670
+ }
671
+ catch (innerErr) {
672
+ throw new core_common_1.IModelError(innerErr.errorNumber, innerErr.message);
673
+ }
674
+ }
675
+ else {
676
+ throw new core_common_1.IModelError(outerErr.errorNumber, outerErr.message);
677
+ }
665
678
  }
666
- if (core_bentley_1.DbResult.BE_SQLITE_OK !== stat)
667
- throw new core_common_1.IModelError(stat, "Error importing schema");
668
679
  });
669
680
  }
670
681
  else {
@@ -674,9 +685,12 @@ class IModelDb extends core_common_1.IModel {
674
685
  };
675
686
  if (this.nativeDb.getITwinId() !== core_bentley_1.Guid.empty) // if this iModel is associated with an iTwin, importing schema requires the schema lock
676
687
  await this.acquireSchemaLock();
677
- const stat = this.nativeDb.importSchemas(schemaFileNames, nativeImportOptions);
678
- if (core_bentley_1.DbResult.BE_SQLITE_OK !== stat)
679
- throw new core_common_1.IModelError(stat, "Error importing schema");
688
+ try {
689
+ this.nativeDb.importSchemas(schemaFileNames, nativeImportOptions);
690
+ }
691
+ catch (err) {
692
+ throw new core_common_1.IModelError(err.errorNumber, err.message);
693
+ }
680
694
  }
681
695
  this.clearCaches();
682
696
  }
@@ -696,23 +710,36 @@ class IModelDb extends core_common_1.IModel {
696
710
  await SchemaSync_1.SchemaSync.withLockedAccess(this, { openMode: core_bentley_1.OpenMode.Readonly, operationName: "schemaSync" }, async (syncAccess) => {
697
711
  const schemaSyncDbUri = syncAccess.getUri();
698
712
  this.saveChanges();
699
- let stat = this.nativeDb.importXmlSchemas(serializedXmlSchemas, { schemaLockHeld: false, schemaSyncDbUri });
700
- if (core_bentley_1.DbResult.BE_SQLITE_ERROR_SchemaLockFailed === stat) {
701
- this.abandonChanges();
702
- if (this.nativeDb.getITwinId() !== core_bentley_1.Guid.empty)
703
- await this.acquireSchemaLock();
704
- stat = this.nativeDb.importXmlSchemas(serializedXmlSchemas, { schemaLockHeld: true, schemaSyncDbUri });
713
+ try {
714
+ this.nativeDb.importXmlSchemas(serializedXmlSchemas, { schemaLockHeld: false, schemaSyncDbUri });
715
+ }
716
+ catch (outerErr) {
717
+ if (core_bentley_1.DbResult.BE_SQLITE_ERROR_DataTransformRequired === outerErr.errorNumber) {
718
+ this.abandonChanges();
719
+ if (this.nativeDb.getITwinId() !== core_bentley_1.Guid.empty)
720
+ await this.acquireSchemaLock();
721
+ try {
722
+ this.nativeDb.importXmlSchemas(serializedXmlSchemas, { schemaLockHeld: true, schemaSyncDbUri });
723
+ }
724
+ catch (innerErr) {
725
+ throw new core_common_1.IModelError(innerErr.errorNumber, innerErr.message);
726
+ }
727
+ }
728
+ else {
729
+ throw new core_common_1.IModelError(outerErr.errorNumber, outerErr.message);
730
+ }
705
731
  }
706
- if (core_bentley_1.DbResult.BE_SQLITE_OK !== stat)
707
- throw new core_common_1.IModelError(stat, "Error importing schema");
708
732
  });
709
733
  }
710
734
  else {
711
735
  if (this.iTwinId && this.iTwinId !== core_bentley_1.Guid.empty) // if this iModel is associated with an iTwin, importing schema requires the schema lock
712
736
  await this.acquireSchemaLock();
713
- const stat = this.nativeDb.importXmlSchemas(serializedXmlSchemas, { schemaLockHeld: true });
714
- if (core_bentley_1.DbResult.BE_SQLITE_OK !== stat)
715
- throw new core_common_1.IModelError(stat, "Error importing schema");
737
+ try {
738
+ this.nativeDb.importXmlSchemas(serializedXmlSchemas, { schemaLockHeld: true });
739
+ }
740
+ catch (err) {
741
+ throw new core_common_1.IModelError(err.errorNumber, err.message);
742
+ }
716
743
  }
717
744
  this.clearCaches();
718
745
  }
@@ -2243,9 +2270,30 @@ class BriefcaseDb extends IModelDb {
2243
2270
  }
2244
2271
  /** Upgrades the profile or domain schemas. File must be closed before this call and is always left closed. */
2245
2272
  static async doUpgrade(briefcase, upgradeOptions, description) {
2246
- const nativeDb = this.openDgnDb({ path: briefcase.fileName }, core_bentley_1.OpenMode.ReadWrite, upgradeOptions); // performs the upgrade
2247
- const wasChanges = nativeDb.hasPendingTxns();
2248
- nativeDb.closeFile();
2273
+ let wasChanges = false;
2274
+ const executeUpgrade = () => {
2275
+ const nativeDb = this.openDgnDb({ path: briefcase.fileName }, core_bentley_1.OpenMode.ReadWrite, upgradeOptions); // performs the upgrade
2276
+ wasChanges = nativeDb.hasPendingTxns();
2277
+ nativeDb.closeFile();
2278
+ };
2279
+ const isSchemaSyncEnabled = await withBriefcaseDb(briefcase, async (db) => {
2280
+ await SchemaSync_1.SchemaSync.pull(db);
2281
+ return db.nativeDb.schemaSyncEnabled();
2282
+ });
2283
+ if (isSchemaSyncEnabled) {
2284
+ await SchemaSync_1.SchemaSync.withLockedAccess(briefcase, { openMode: core_bentley_1.OpenMode.Readonly, operationName: "schema sync" }, async (syncAccess) => {
2285
+ const schemaSyncDbUri = syncAccess.getUri();
2286
+ executeUpgrade();
2287
+ await withBriefcaseDb(briefcase, async (db) => {
2288
+ db.nativeDb.schemaSyncPush(schemaSyncDbUri);
2289
+ db.saveChanges();
2290
+ });
2291
+ syncAccess.synchronizeWithCloud();
2292
+ });
2293
+ }
2294
+ else {
2295
+ executeUpgrade();
2296
+ }
2249
2297
  if (wasChanges)
2250
2298
  await withBriefcaseDb(briefcase, async (db) => db.pushChanges({ ...briefcase, description, retainLocks: true }));
2251
2299
  }
@@ -2283,6 +2331,7 @@ class BriefcaseDb extends IModelDb {
2283
2331
  }
2284
2332
  return;
2285
2333
  }
2334
+ throw error;
2286
2335
  }
2287
2336
  try {
2288
2337
  await this.doUpgrade(briefcase, { domain: core_common_1.DomainOptions.Upgrade }, "Upgraded domain schemas");
@@ -2298,6 +2347,7 @@ class BriefcaseDb extends IModelDb {
2298
2347
  await withBriefcaseDb(briefcase, async (db) => db.locks.releaseAllLocks());
2299
2348
  }
2300
2349
  }
2350
+ throw error;
2301
2351
  }
2302
2352
  }
2303
2353
  /** Open a briefcase file and return a new BriefcaseDb to interact with it.
@@ -2377,6 +2427,14 @@ class BriefcaseDb extends IModelDb {
2377
2427
  args.dump();
2378
2428
  }
2379
2429
  else {
2430
+ if (args.tableName === "be_Prop") {
2431
+ if (args.getValueText(0, core_bentley_1.DbChangeStage.Old) === "ec_Db" && args.getValueText(1, core_bentley_1.DbChangeStage.Old) === "localDbInfo") {
2432
+ return core_bentley_1.DbConflictResolution.Replace;
2433
+ }
2434
+ }
2435
+ if (args.tableName.startsWith("ec_")) {
2436
+ return core_bentley_1.DbConflictResolution.Skip;
2437
+ }
2380
2438
  const msg = "UPDATE/DELETE before value do not match with one in db or CASCADE action was triggered.";
2381
2439
  args.setLastError(msg);
2382
2440
  core_bentley_1.Logger.logError(category, msg);
@@ -2396,6 +2454,9 @@ class BriefcaseDb extends IModelDb {
2396
2454
  args.dump();
2397
2455
  }
2398
2456
  else {
2457
+ if (args.tableName.startsWith("ec_")) {
2458
+ return core_bentley_1.DbConflictResolution.Skip;
2459
+ }
2399
2460
  const msg = "PRIMARY KEY INSERT CONFLICT - rejecting this changeset";
2400
2461
  args.setLastError(msg);
2401
2462
  core_bentley_1.Logger.logError(category, msg);
@@ -2505,6 +2566,7 @@ class BriefcaseDb extends IModelDb {
2505
2566
  async pullChanges(arg) {
2506
2567
  await this.executeWritable(async () => {
2507
2568
  await BriefcaseManager_1.BriefcaseManager.pullAndApplyChangesets(this, arg ?? {});
2569
+ await SchemaSync_1.SchemaSync.pull(this);
2508
2570
  this.initializeIModelDb();
2509
2571
  });
2510
2572
  IpcHost_1.IpcHost.notifyTxns(this, "notifyPulledChanges", this.changeset);