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

EntitySchema with indices results in duplicate migration file entries #5062

Open
dgobaud opened this issue Nov 10, 2019 · 0 comments
Open

EntitySchema with indices results in duplicate migration file entries #5062

dgobaud opened this issue Nov 10, 2019 · 0 comments

Comments

@dgobaud
Copy link

dgobaud commented Nov 10, 2019

Issue type:
[X] bug report

Database system/driver:

[X] postgres

TypeORM version:

[X] 0.2.18 (or put your version here)

Below schema results in migration generation with the index create/drop calls happening 3 times. We fixed it by adding below visited array to filterIndices in typeorm/metadata-args/MetadataArgsStorage.js

Problem seems to be the metadata builder for some reasons loads indices three times (can't figure out where this is happening exactly).

seems related to #3280 and #4577

import {MigrationInterface, QueryRunner} from "typeorm";

export class test1573371622105 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query(`CREATE TABLE "public"."authenticators" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "code" character varying, CONSTRAINT "PK_ca66aa0ff269b0314c053ba20a2" PRIMARY KEY ("id"))`);
        await queryRunner.query(`CREATE UNIQUE INDEX "IDX_b301987c9252892c34fdface66" ON "public"."authenticators" ("code") `);
        await queryRunner.query(`CREATE UNIQUE INDEX "IDX_b301987c9252892c34fdface66" ON "public"."authenticators" ("code") `);
        await queryRunner.query(`CREATE UNIQUE INDEX "IDX_b301987c9252892c34fdface66" ON "public"."authenticators" ("code") `);
    }

    public async down(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query(`DROP INDEX "public"."IDX_b301987c9252892c34fdface66"`);
        await queryRunner.query(`DROP INDEX "public"."IDX_b301987c9252892c34fdface66"`);
        await queryRunner.query(`DROP INDEX "public"."IDX_b301987c9252892c34fdface66"`);
        await queryRunner.query(`DROP TABLE "public"."authenticators"`);
    }

}
    MetadataArgsStorage.prototype.filterIndices = function (target) {
        var visited = []
        // todo: implement parent-entity overrides?
        return this.indices.filter(function (index) {
            const indexKey = index.target + index.name
            if (visited.indexOf(indexKey) >= 0 ) {
                return false
            }
            visited.push(indexKey)
            return target instanceof Array ? target.indexOf(index.target) !== -1 : index.target === target;
        });
    };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const typeorm_1 = require("typeorm");
exports.authenticator = new typeorm_1.EntitySchema({
    name: 'authenticators',
    columns: {
        id: {
            type: 'uuid',
            primary: true,
            generated: 'uuid',
            nullable: false
        },
        createdAt: {
            type: 'timestamp',
            createDate: true
        },
        updatedAt: {
            type: 'timestamp',
            updateDate: true
        },
        code: {
            type: String,
            nullable: true
        }
    },
    indices: [
        {
            columns: ['code'],
            unique: true
        }
    ]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants