typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

Migration generator asking for same changes

Open ghostlexly opened this issue 1 year ago • 1 comments

Issue description

The migration generator ask for same changes on a uuid column

Expected Behavior

We want the migration tool to not ask for any new changes.

Actual Behavior

The migration tool stuck and ask again and again for the same changes to the uuid column:

import { MigrationInterface, QueryRunner } from "typeorm";

export class Test1690302325518 implements MigrationInterface {
    name = 'Test1690302325518'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            ALTER TABLE \`monitor\` DROP PRIMARY KEY
        `);
        await queryRunner.query(`
            ALTER TABLE \`monitor\` DROP COLUMN \`id\`
        `);
        await queryRunner.query(`
            ALTER TABLE \`monitor\`
            ADD \`id\` varchar(36) NOT NULL PRIMARY KEY
        `);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            ALTER TABLE \`monitor\` DROP COLUMN \`id\`
        `);
        await queryRunner.query(`
            ALTER TABLE \`monitor\`
            ADD \`id\` varchar(36) NOT NULL
        `);
        await queryRunner.query(`
            ALTER TABLE \`monitor\`
            ADD PRIMARY KEY (\`id\`)
        `);
    }

}

Steps to reproduce

  • Install MariaDB 10.6.1 (the latest available version on AWS - RDS)

  • Create an entity with a uuid column

export class Monitor extends Base {
  @PrimaryGeneratedColumn("uuid")
  id: string;
}
  • Generate a new migration file with the CLI

  • This will generate this file:

import { MigrationInterface, QueryRunner } from "typeorm";

export class Test1690302325518 implements MigrationInterface {
    name = 'Test1690302325518'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            ALTER TABLE \`monitor\` DROP PRIMARY KEY
        `);
        await queryRunner.query(`
            ALTER TABLE \`monitor\` DROP COLUMN \`id\`
        `);
        await queryRunner.query(`
            ALTER TABLE \`monitor\`
            ADD \`id\` varchar(36) NOT NULL PRIMARY KEY
        `);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            ALTER TABLE \`monitor\` DROP COLUMN \`id\`
        `);
        await queryRunner.query(`
            ALTER TABLE \`monitor\`
            ADD \`id\` varchar(36) NOT NULL
        `);
        await queryRunner.query(`
            ALTER TABLE \`monitor\`
            ADD PRIMARY KEY (\`id\`)
        `);
    }

}
  • Run the migration

  • Generate a new migration file with the CLI

  • The same file is created again

My Environment

Dependency Version
Operating System
Node.js version 18.16.0
Typescript version 5.1.3
TypeORM version 0.3.16

Additional Context

No response

Relevant Database Driver(s)

  • [ ] aurora-mysql
  • [ ] aurora-postgres
  • [ ] better-sqlite3
  • [ ] cockroachdb
  • [ ] cordova
  • [ ] expo
  • [ ] mongodb
  • [X] mysql
  • [ ] nativescript
  • [ ] oracle
  • [ ] postgres
  • [ ] react-native
  • [ ] sap
  • [ ] spanner
  • [ ] sqlite
  • [ ] sqlite-abstract
  • [ ] sqljs
  • [ ] sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

ghostlexly avatar Jul 25 '23 16:07 ghostlexly

~~Got this issue too (with a mariadb docker database and using mariadb typeorm driver)~~

EDIT: By adding some console log in dropRemovedColumns of node_modules/typeorm/schema-builder/RdbmsSchemaBuilder.js (just console.log(this.entityToSyncMetadatas.map(n => n.name).join(' , '))), I found out that it was because of some old ./dist entity files (my npm run migrate uses my dist directory).

I just deleted ./dist directory and I started the migrate:generate again, and everything worked.

stouch avatar Feb 16 '24 18:02 stouch