Search code examples
react-nativeexpotypeorm

Can't run migrations with migrationsRun: true


I have a React Native + Expo project with SQLite database and TypeORM set up. I'm trying to run a migration which should add an isPrivate column to an existing Note table on application start, by setting migrationsRun data source config option to true. But it doesn't work. It seems like TypeORM can't see migrations at all, even though the path is correct.

If Note entity has isPrivate column defined and I'm trying to use a find method from repository instance, a following error gets thrown:

no such column: Note.isPrivate (code 1 SQLITE_ERROR): , while compiling: SELECT "Note"."id" AS "Note_id", "Note"."title" AS "Note_title", "Note"."details" AS "Note_details", "Note"."isPrivate" AS "Note_isPrivate", "Note"."folderId" AS "Note_folderId" FROM "note" "Note"

Here is data-source.ts file:

import "reflect-metadata";
import { DataSource } from "typeorm";
import * as sqlitedriver from 'expo-sqlite';
import Folder from './entities/Folder.model.ts';
import Note from './entities/Note.model.ts';

const AppDataSource = new DataSource({
    driver: sqlitedriver,
    database: 'app_db',
    type: 'expo',
    migrationsRun: true,
    entities: [Folder, Note],
    migrations: ['migrations/*.ts'],
});

AppDataSource.initialize().then(() => {
    console.log("Data Source has been initialized!");
}).catch((err) => {
    console.error("Error during Data Source initialization", err.message);
});

export default AppDataSource;

migration file:

import { MigrationInterface, QueryRunner } from "typeorm";
import { TableColumn } from "typeorm";

export class AddIsPrivateColumn1684782594104 implements MigrationInterface {
    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.addColumn(
            "Note",
            new TableColumn({
                name: "isPrivate",
                type: "boolean",
            }),
        );
    };

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.dropColumn("Note", "isPrivate");
    };
};

How do I run a migration? Is something wrong with the data source configuration?


Solution

  • Ok, so I think I solved it. My problem was in a way migrations were listed in a data source configuration object. For some reason I couldn't specify them as a path string, so instead I imported migrations classes directly and added them to array of migrations.