Skip to content

Fix TypeORM CapacitorDriver PRAGMA Statement Execution for Schema Synchronization #640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

AlexAzartsev
Copy link

This PR addresses a critical issue in TypeORM's Capacitor driver that prevents proper schema synchronization and migration functionality. The root cause lies in how PRAGMA statements are executed in the SQLite driver.

Issue Summary

The current implementation of modify-typeorm.cjs incorrectly routes PRAGMA statements through execute method, which return 0 instead of the expected metadata results. This causes schema synchronization to fail with errors like:

const pos = tableColumn.type.indexOf is not a function. (In 'dbColumns.map(async (dbColumn) => {

When TypeORM attempts to retrieve table metadata using PRAGMA statements in AbstractSqliteQueryRunner, it receives empty results:

// This returns empty results (dbColumns=0, dbIndices=0, dbForeignKeys=0)
const [dbColumns, dbIndices, dbForeignKeys] = await Promise.all([
    this.loadPragmaRecords(tablePath, `table_xinfo`),
    this.loadPragmaRecords(tablePath, `index_list`),
    this.loadPragmaRecords(tablePath, `foreign_key_list`),
]);

Solution

The fix ensures that all PRAGMA statements are executed with the query method instead of execute or run in CapacitorQueryRunner, allowing them to properly return metadata results. This is crucial for:

  1. Schema synchronization (synchronize: true)
  2. Migration functionality
  3. Proper entity-to-database mapping

Previous attempts at fixing this issue only moved PRAGMA statements from one execution method to another without addressing the core issue - PRAGMA statements need to be executed with methods that return results, not just success indicators.

Implementation Details

  1. Remove part that moves PRAGMA to execute record. It will navigate PRAGMA to query method.

Other changes

  1. Replace utils.warn with console.log, to avoid extra dependency.
  2. Use let instead of var.

This fix ensures compatibility with both schema synchronization and migrations, providing a reliable database layer for Capacitor applications.

Clarified the range of TypeORM versions affected by the Capacitor driver bug. This ensures accurate guidance for developers using versions between 0.3.18 and 0.3.24.
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.

1 participant