Skip to content
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

Database fails to open in production #4561

Closed
MorganTrudeau opened this issue Aug 9, 2019 · 7 comments
Closed

Database fails to open in production #4561

MorganTrudeau opened this issue Aug 9, 2019 · 7 comments

Comments

@MorganTrudeau
Copy link

MorganTrudeau commented Aug 9, 2019

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[x] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

OPEN database fails in production. Database is working as expected in debug mode.

Expected Behavior

Database should open and initialize tables successfully in production and debug modes.

Current Behavior

Database is opening in debug mode and failing in production

Steps to Reproduce (for bugs)

Using typeorm I have created entities in the following form:

import { EntitySchema } from 'typeorm';
import { StorageModel } from '../models/StorageModel';

export default new EntitySchema({
  name: 'Storage',
  target: StorageModel,
  columns: {
    key: { type: 'text', primary: true },
    value: { type: 'text' },
  },
});

Then I am opening database with the following config:

createConnection({
      name: 'db.sqlite',
      type: 'react-native',
      database: 'react-native',
      location: 'default',
      // logging: ['error', 'query', 'schema'],
      synchronize: true,
      entities: [
        Translation,
        User,
        Post,
        Download,
        Asset,
        Option,
        QueuedRequest,
        Storage,
        HelpItem,
      ],
    });

In debug the database opens correctly and I have used it to create many features. It was going very well until I tried building the release apk. I get the following error in adb logcat...

08-08 19:04:52.174 19954 20022 I ReactNativeJS: OPEN database: react-native
08-08 19:04:52.340 19954 20032 E SQLiteLog: (1) near ")": syntax error
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: SQLitePlugin.executeSql[Batch]() failed
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE "temporary_t" ()
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:895)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:506)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:726)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1428)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1367)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at org.pgsqlite.SQLitePlugin.executeSqlStatementQuery(SQLitePlugin.java:805)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at org.pgsqlite.SQLitePlugin.executeSqlBatch(SQLitePlugin.java:712)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at org.pgsqlite.SQLitePlugin.access$100(SQLitePlugin.java:49)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at org.pgsqlite.SQLitePlugin$DBRunner.run(SQLitePlugin.java:927)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
08-08 19:04:52.340 19954 20032 E unknown:SQLitePlugin: 	at java.lang.Thread.run(Thread.java:764)

Context

Using react-native-sqlite-storage with typeorm to build offline support for a react-native app.

Your Environment

  • typeorm: ^0.2.18
  • React Native SQLite Storage Version used: 3.3.10 and also on 3.2.0
  • React Native version used: 0.60.0
  • Operating System and version (simulator or device): LG G6 Android 8.0.0
  • IDE used: Android Studio
  • Link to your project: Private client project SRY
@rcchen
Copy link

rcchen commented Aug 16, 2019

Ran into the same thing, and I believe it's because synchronize is flipped off in production, and you need to have migrations in place to generate tables. It seems that typeorm generate doesn't work properly against a react-native target, so I'm resorting to writing those initial migrations by hand based on debug output when synchronize: true

(you can repro in debug/dev mode by changing the synchronize flag to false then wiping the app so you're forced to create a new database)

@jbenzshawel
Copy link

This issue is due to minification in release mode changing class names and file names. It can be fixed with the following steps:

  1. yarn add metro-minify-terser
  2. Update the metro config to keep class names and file names. Add the following to the metro.config.js transformer:
minifierPath: 'metro-minify-terser',
minifierConfig: {
    ecma: 8,
    keep_classnames: true,
    keep_fnames: true,
    module: true,
    mangle: {
        module: true,
        keep_classnames: true,
        keep_fnames: true,
    },
 },

@the-sparrow
Copy link

the-sparrow commented Oct 31, 2019

This issue is due to minification in release mode changing class names and file names. It can be fixed with the following steps:

  1. yarn add metro-minify-terser
  2. Update the metro config to keep class names and file names. Add the following to the metro.config.js transformer:
minifierPath: 'metro-minify-terser',
minifierConfig: {
    ecma: 8,
    keep_classnames: true,
    keep_fnames: true,
    module: true,
    mangle: {
        module: true,
        keep_classnames: true,
        keep_fnames: true,
    },
 },

This helps me!

@macfanatic
Copy link

This forum discussion solved it for me with expo and typeorm.

https://forums.expo.io/t/change-minifierconfig-for-minify-uglify/36460/2

jessgusclark added a commit to rsksmart/rif-identity-ui that referenced this issue Oct 2, 2020
ilanolkies pushed a commit to rsksmart/rif-identity-ui that referenced this issue Oct 2, 2020
* Allow user to access settings without a mnemonic setup.

* Use official packages for daf and core.

* Update Readme file.

* Add and implement metro-minify-terser to prevent build changing variable names.

Reference: typeorm/typeorm#4561
@imnotjames
Copy link
Contributor

Sounds like an environment issue here, not a problem with TypeORM - I'm gonna close this out.

@mirceanis
Copy link

Is there a way to instruct typeorm to treat migrations properly without the use of class names, like maybe giving it an array of tuples (migration class, timestamp)?

@AzranAzwer
Copy link

AzranAzwer commented Aug 31, 2022

This issue is due to minification in release mode changing class names and file names. It can be fixed with the following steps:

  1. yarn add metro-minify-terser
  2. Update the metro config to keep class names and file names. Add the following to the metro.config.js transformer:
minifierPath: 'metro-minify-terser',
minifierConfig: {
    ecma: 8,
    keep_classnames: true,
    keep_fnames: true,
    module: true,
    mangle: {
        module: true,
        keep_classnames: true,
        keep_fnames: true,
    },
 },

This solved e Thank you @jbenzshawel <3

module.exports = {
  transformer: {
    ....
    minifierPath: 'metro-minify-terser',
    minifierConfig: {
      ecma: 8,
      keep_classnames: true,
      keep_fnames: true,
      module: true,
      mangle: {
        module: true,
        keep_classnames: true,
        keep_fnames: true,
      },
    },
  },
};

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

No branches or pull requests

8 participants