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

Unable to generate migration #18

Open
L-U-C-K-Y opened this issue Sep 16, 2023 · 3 comments
Open

Unable to generate migration #18

L-U-C-K-Y opened this issue Sep 16, 2023 · 3 comments

Comments

@L-U-C-K-Y
Copy link
Contributor

L-U-C-K-Y commented Sep 16, 2023

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 L-U-C-K-Y changed the title Generate Migrations Unable to generate migration Sep 16, 2023
@L-U-C-K-Y
Copy link
Contributor Author

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.

@badsyntax
Copy link

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();

@Franklyn-luupli
Copy link

Franklyn-luupli commented Oct 10, 2024

@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

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

3 participants