react-native-nitro-sqlite icon indicating copy to clipboard operation
react-native-nitro-sqlite copied to clipboard

Unable to generate migration

Open L-U-C-K-Y opened this issue 2 years ago • 2 comments

Many thanks for providing and maintaining this awesome library! I have followed the docs and was successful to set it up and write/read data from sqlite with TypeORM.

I am trying to generate a migration for production but I am struggling with getting it to run.

Does anyone has advice how to implement a migration?

Here are things that I have tried.

dataSource.ts

import { typeORMDriver } from 'react-native-quick-sqlite';
import { DataSource } from 'typeorm';

const dataSource = new DataSource({
  type: 'react-native',
  database: 'typeormdb',
  location: '.',
  driver: typeORMDriver,
  entities: [
    // entities
  ],
  logging: true,
  // synchronize: true, // only have it in dev, otherwise use migrations!
  migrations: [],
  migrationsRun: true,
});

export default dataSource;

Commands:

typeorm migration:generate -d src/db/dataSource/index.ts src/db/migrations
npx typeorm-ts-node-commonjs migration:generate -d src/db/dataSource/index.ts src/db/migrations
npx typeorm-ts-node-esm migration:generate -d src/db/dataSource/index.ts src/db/migrations
npx typeorm migration:generate -d ./src/db/dataSource/index.ts ./src/db/migrations

Thanks a lot


Edit: After running a few more experiments I was able to narrow the problem down to the typeORM driver.

Using exactly the same setup without the typeORM driver and postgres is working!

The app is running fine overall, reading and writing is working, but generation of the migration is not working.

L-U-C-K-Y avatar Sep 16 '23 15:09 L-U-C-K-Y

Small update, I was temporarily able to create migrations with a workaround by creating a second sqlite datasource and running the migrations against that. I then imported (no glob pattern) the migrations into the main data source and the migrations were applied.

Let me know if we can keep it open. Maybe there is just no possibility to create migrations against the sqlite database in react-native.

L-U-C-K-Y avatar Sep 18 '23 08:09 L-U-C-K-Y

Thanks @L-U-C-K-Y, this was helpful. I've taken the same approach and used a separate local db to manage the migrations.

So we have two data sources:

// dataSource.ts

export const dataSource = new DataSource({
  type: 'react-native',
  database: DB_NAME,
  location: '.',
  driver: typeORMDriver,
  entities,
  migrations: [InitialSchema1697806995951, Test1697809356693],
});
// migrationsDataSource.ts

const DB_NAME = path.join(__dirname, 'migrations', 'driver.local.sqlite');

// eslint-disable-next-line no-new
new sqlite3.Database(DB_NAME);

export const migrationsDataSource = new DataSource({
  type: 'sqlite',
  database: DB_NAME,
  entities,
  migrations: [path.join(__dirname, './migrations/*.ts')],
});

In package.json i set the following:

"typeorm": "typeorm-ts-node-commonjs",

And generate the migrations on the local db in node.js:

npm run typeorm -- migration:generate -d db/migrationsDataSource.ts db/migrations/MigrationName

To apply the migrations to the app db, I run migrations in the app with await dataSource.runMigrations();

badsyntax avatar Oct 22 '23 06:10 badsyntax

@badsyntax @L-U-C-K-Y Thank you very much, This really worked for me, I've been struggling with this issue for days, This was really helpful

Franklyn-luupli avatar Oct 10 '24 09:10 Franklyn-luupli

I think we can close this issue, since there is a working solution!

Let me know if this is not solved, then i'm gonna re-open :)

chrispader avatar Oct 15 '24 16:10 chrispader