Nima Soufiloo
Nima Soufiloo

Reputation: 254

Typeorm migration not detecting changes properly

I'm using typeorm in my nestjs application using a postgresql database. When I try to create a migration to synchronize my database to apply application changes the following query is always existed in generated migration file (I removed some unnecessary queries for extra readability):

export class portal1631976435381 implements MigrationInterface {
    name = 'portal1631976435381'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "organization" DROP COLUMN "isTransporter"`);
        await queryRunner.query(`ALTER TABLE "organization" DROP COLUMN "crmId"`);
        await queryRunner.query(`ALTER TABLE "organization" DROP COLUMN "telephoneNumber"`);
        await queryRunner.query(`ALTER TABLE "organization" ADD "crmId" character varying`);
        await queryRunner.query(`ALTER TABLE "organization" ADD "telephoneNumber" character varying`);
        await queryRunner.query(`ALTER TABLE "organization" ADD "isTransporter" boolean NOT NULL DEFAULT false`);
        await queryRunner.query(`CREATE VIEW "inventory_based_on_receipt_item_view" AS SELECT "ri"."id" AS "receiptItemId" from someTableC`);
        await queryRunner.query(`INSERT INTO "typeorm_metadata"("type", "schema", "name", "value") VALUES ($1, $2, $3, $4)`, ["VIEW","public","inventory_based_on_receipt_item_view",]);
        await queryRunner.query(`CREATE VIEW "inventory_based_on_receipt_item_view" AS SELECT "ri"."id" AS "receiptItemId" from someTableB`);
        await queryRunner.query(`INSERT INTO "typeorm_metadata"("type", "schema", "name", "value") VALUES ($1, $2, $3, $4)`, ["VIEW","public","inventory_based_on_receipt_item_view",]);
        await queryRunner.query(`CREATE VIEW "receipt_item_transaction_view" AS SELECT "ri"."id" AS "receiptItemId" from someTableA`)
        await queryRunner.query(`INSERT INTO "typeorm_metadata"("type", "schema", "name", "value") VALUES ($1, $2, $3, $4)`, ["VIEW","public",...]);
    }
    public async down(queryRunner: QueryRunner): Promise<void> { ... }
}

No matter what changes are applied to the application all viewEntities are Dropped and recreated.

Another problem is that inventory_based_on_receipt_item_view is being created two times in the query with 2 slightly different queries although I only have one viewEntity with that name (I don't really know where it is coming from). Another problem is that the 3 columns of organization table (crmId, telephoneNumber and isTransporter) are dropped and recreated with the same detail.

I wanted to know that is there any better way for migrating database tables, maybe with another package (I couldn't find anything useful myself) or any workaround that can optimize my workflow? Cause I need to regenerate the migration file whenever any changes happen to the entities and make sure I remove every single wrong query that is generated.

Upvotes: 5

Views: 5607

Answers (1)

Nima Soufiloo
Nima Soufiloo

Reputation: 254

I found the problem. When executing migration script, it creates a migration ts file in migration folder but the previous migration files in dist folder exist. So the new migration file appends to the existed migration file in dist folder.

Removing dist folder before running migration:generate solves the problem.

Upvotes: 7

Related Questions