Skip to content

Commit

Permalink
fix #3379
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMesser committed Mar 25, 2019
1 parent b91a725 commit 234d287
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,11 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
}), dbIndex => dbIndex["INDEX_NAME"]);

table.indices = tableIndexConstraints.map(constraint => {
const indices = dbIndices.filter(index => index["INDEX_NAME"] === constraint["INDEX_NAME"]);
const indices = dbIndices.filter(index => {
return index["TABLE_SCHEMA"] === constraint["TABLE_SCHEMA"]
&& index["TABLE_NAME"] === constraint["TABLE_NAME"]
&& index["INDEX_NAME"] === constraint["INDEX_NAME"];
});
return new TableIndex(<TableIndexOptions>{
table: table,
name: constraint["INDEX_NAME"],
Expand Down
2 changes: 1 addition & 1 deletion src/driver/oracle/OracleQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {

// create TableIndex objects from the loaded indices
table.indices = dbIndices
.filter(dbIndex => dbIndex["TABLE_NAME"] === table.name )
.filter(dbIndex => dbIndex["TABLE_NAME"] === table.name)
.map(dbIndex => {
return new TableIndex({
name: dbIndex["INDEX_NAME"],
Expand Down
6 changes: 5 additions & 1 deletion src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,11 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
}), dbIndex => dbIndex["constraint_name"]);

table.indices = tableIndexConstraints.map(constraint => {
const indices = dbIndices.filter(index => index["constraint_name"] === constraint["constraint_name"]);
const indices = dbIndices.filter(index => {
return index["table_schema"] === constraint["table_schema"]
&& index["table_name"] === constraint["table_name"]
&& index["constraint_name"] === constraint["constraint_name"];
});
return new TableIndex(<TableIndexOptions>{
table: table,
name: constraint["constraint_name"],
Expand Down
7 changes: 6 additions & 1 deletion src/driver/sqlserver/SqlServerQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,12 @@ export class SqlServerQueryRunner extends BaseQueryRunner implements QueryRunner
}), dbIndex => dbIndex["INDEX_NAME"]);

table.indices = tableIndexConstraints.map(constraint => {
const indices = dbIndices.filter(index => index["INDEX_NAME"] === constraint["INDEX_NAME"]);
const indices = dbIndices.filter(index => {
return index["TABLE_CATALOG"] === constraint["TABLE_CATALOG"]
&& index["TABLE_SCHEMA"] === constraint["TABLE_SCHEMA"]
&& index["TABLE_NAME"] === constraint["TABLE_NAME"]
&& index["INDEX_NAME"] === constraint["INDEX_NAME"];
});
return new TableIndex(<TableIndexOptions>{
table: table,
name: constraint["INDEX_NAME"],
Expand Down
1 change: 0 additions & 1 deletion test/github-issues/1960/issue-1960.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe.skip("github issues > #1960 Migration generator produces duplicated cha

it("should not execute any alter queries", () => Promise.all(connections.map(async function(connection) {
const sqlInMemory = await connection.driver.createSchemaBuilder().log();
console.log(sqlInMemory);
sqlInMemory.upQueries.length.should.be.equal(0);
})));

Expand Down
15 changes: 15 additions & 0 deletions test/github-issues/3379/entity/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Index, PrimaryGeneratedColumn} from "../../../../src";
import {Column} from "../../../../src";
import {Entity} from "../../../../src";

@Index("name_index", ["name"])
@Entity()
export class Post {

@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

}
83 changes: 83 additions & 0 deletions test/github-issues/3379/issue-3379.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import "reflect-metadata";
import {MysqlDriver} from "../../../src/driver/mysql/MysqlDriver";
import {PostgresDriver} from "../../../src/driver/postgres/PostgresDriver";
import {AbstractSqliteDriver} from "../../../src/driver/sqlite-abstract/AbstractSqliteDriver";
import {SqlServerDriver} from "../../../src/driver/sqlserver/SqlServerDriver";
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Connection, Table} from "../../../src";

describe("github issues > #3379 Migration will keep create and drop indexes if index name is the same across tables", () => {

let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"]
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should not recreate indices", () => Promise.all(connections.map(async connection => {

const queryRunner = connection.createQueryRunner();

let postTableName: string = "post";

if (connection.driver instanceof SqlServerDriver) {
postTableName = "testDB.testSchema.post";
await queryRunner.createDatabase("testDB", true);
await queryRunner.createSchema("testDB.testSchema", true);

} else if (connection.driver instanceof PostgresDriver) {
postTableName = "testSchema.post";
await queryRunner.createSchema("testSchema", true);

} else if (connection.driver instanceof MysqlDriver) {
postTableName = "testDB.post";
await queryRunner.createDatabase("testDB", true);
}

await queryRunner.createTable(new Table({
name: postTableName,
columns: [
{
name: "id",
type: connection.driver instanceof AbstractSqliteDriver ? "integer" : "int",
isPrimary: true,
isGenerated: true,
generationStrategy: "increment"
},
{
name: "name",
type: "varchar",
}
],
indices: [{ name: "name_index", columnNames: ["name"] }]
}), true);

// Only MySQL and SQLServer allows non unique index names
if (connection.driver instanceof MysqlDriver || connection.driver instanceof SqlServerDriver) {
await queryRunner.createTable(new Table({
name: "category",
columns: [
{
name: "id",
type: "int",
isPrimary: true,
isGenerated: true,
generationStrategy: "increment"
},
{
name: "name",
type: "varchar",
}
],
indices: [{ name: "name_index", columnNames: ["name"] }]
}), true);
}

await queryRunner.release();

const sqlInMemory = await connection.driver.createSchemaBuilder().log();
sqlInMemory.upQueries.length.should.be.equal(0);
})));

});

0 comments on commit 234d287

Please sign in to comment.