Skip to content

Add integrityCheck() function#8958

Open
MichaelSwigerAtBentley wants to merge 25 commits intomasterfrom
mike/fix-pragma
Open

Add integrityCheck() function#8958
MichaelSwigerAtBentley wants to merge 25 commits intomasterfrom
mike/fix-pragma

Conversation

@MichaelSwigerAtBentley
Copy link
Contributor

@MichaelSwigerAtBentley MichaelSwigerAtBentley commented Feb 4, 2026

imodel-native: iTwin/imodel-native#1324

Adds a new function to IModelDb that allows the user to call PRAGMA integrity_check, and all of its specific sub queries. This exposes the ability to triage iModel state from TS. Also parses its output as Typescript would expect.

Types of checks:

Default Check:

  • Quick Check: Runs all integrity checks below and returns whether each check passed or failed, without detailed results.

Specific Checks:

  • Data Columns Check: Checks if all the required columns exist in data tables. Issues are returned as a list of those tables/columns.
  • EC Profile Check: Checks if the profile table, indexes, and triggers are present. Does not check be_* tables. Issues are returned as a list of tables/indexes/triggers which were not found or have different DDL.
  • Navigation Class Ids Check: Checks if RelClassId of a Navigation property is a valid ECClassId. It does not check the value to match the relationship class.
  • Navigation Ids Check: Checks if Id of a Navigation property matches a valid row primary class.
  • Linktable Foreign Key Class Ids Check: Checks if SourceECClassId or TargetECClassId of a link table matches a valid ECClassId.
  • Linktable Foreign Key Ids Check: Checks if SourceECInstanceId or TargetECInstanceId of a link table matches a valid row in primary class.
  • Class Ids Check: Checks persisted ECClassId in all data tables and makes sure they are valid.
    Data Schema Check: Checks if all the required data tables and indexes exist for mapped classes. Issues are returned as a list of tables/columns which were not found or have different DDL.
  • Schema Load Check: Checks if all schemas can be loaded into memory.
  • Missing Child Rows Check: Checks if all child rows have a corresponding parent row.

Implements: https://github.com/iTwin/itwinjs-backlog/issues/1811?issue=iTwin%7Citwinjs-backlog%7C1848

import { IModelError } from "@itwin/core-common/lib/cjs/IModelError";
import { IModelDb } from "./IModelDb";

/** Integrity check types with their display names
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep that internal on in test. The issue with pragma integrity check is it's not lazy loading rows. If you full result set then get ready to get like millions of rows as well. Which some db do have.

We need to improve this by instead providing virtual table or something that can step as you step through result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have moved the integryCheck.ts file into internal completely.

Should I look at capping the results I return in TS to 1000?

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new IModelDb.integrityCheck() API (beta) to run PRAGMA integrity_check (quick and specific sub-checks) from TypeScript, along with parsing helpers and tests.

Changes:

  • Added internal integrity-check helpers/types to map check options to PRAGMA queries and normalize result rows.
  • Introduced IModelDb.integrityCheck(options?) and new IntegrityCheckOptions API.
  • Added standalone tests covering quick/specific checks and a corruption scenario.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
core/backend/src/internal/IntegrityCheck.ts Implements check mapping, friendly names, and execution/parsing helpers for quick + specific PRAGMA checks.
core/backend/src/IModelDb.ts Exposes new integrityCheck() method and IntegrityCheckOptions interface on IModelDb.
core/backend/src/test/standalone/IntegrityCheck.test.ts Adds unit/integration tests for helper functions and the new IModelDb.integrityCheck() behavior.
core/backend/src/test/standalone/ChangesetReader.test.ts Adds PRAGMA integrity_check coverage but currently forces the suite to run exclusively.
common/changes/@itwin/core-backend/*.json Adds (duplicated) change files for the new API, one with an empty comment.
common/api/* Updates extracted API docs/exports for new beta interface + method.
.vscode/cSpell.json Adds “Linktable” to spellchecker dictionary.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@MichaelSwigerAtBentley MichaelSwigerAtBentley marked this pull request as ready for review February 6, 2026 17:34
@MichaelSwigerAtBentley MichaelSwigerAtBentley requested a review from a team as a code owner February 6, 2026 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants