Skip to content

Commit

Permalink
fix: resolve MySQL unique index check when bigNumberStrings is false
Browse files Browse the repository at this point in the history
Part of fix for #2737
  • Loading branch information
SteveByerly committed Oct 18, 2019
1 parent d2fa245 commit f67ba15
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/driver/aurora-data-api/AuroraDataApiQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1217,8 +1217,12 @@ export class AuroraDataApiQueryRunner extends BaseQueryRunner implements QueryRu
.map(dbColumn => {

const columnUniqueIndex = dbIndices.find(dbIndex => {
return this.driver.buildTableName(dbIndex["TABLE_NAME"], undefined, dbIndex["TABLE_SCHEMA"]) === tableFullName
&& dbIndex["COLUMN_NAME"] === dbColumn["COLUMN_NAME"] && dbIndex["NON_UNIQUE"] === "0";
const indexTableFullName = this.driver.buildTableName(dbIndex["TABLE_NAME"], undefined, dbIndex["TABLE_SCHEMA"]);
if (indexTableFullName !== tableFullName) {
return false;
}
// tslint:disable-next-line:triple-equals - trying to find truthy value
return dbIndex["COLUMN_NAME"] === dbColumn["COLUMN_NAME"] && dbIndex["NON_UNIQUE"] == "0";
});

const tableMetadata = this.connection.entityMetadatas.find(metadata => metadata.tablePath === table.name);
Expand Down Expand Up @@ -1340,7 +1344,7 @@ export class AuroraDataApiQueryRunner extends BaseQueryRunner implements QueryRu
table: table,
name: constraint["INDEX_NAME"],
columnNames: indices.map(i => i["COLUMN_NAME"]),
isUnique: constraint["NON_UNIQUE"] === "0",
isUnique: constraint["NON_UNIQUE"] == "0", // tslint:disable-line:triple-equals - trying to find truthy value
isSpatial: constraint["INDEX_TYPE"] === "SPATIAL",
isFulltext: constraint["INDEX_TYPE"] === "FULLTEXT"
});
Expand Down
10 changes: 7 additions & 3 deletions src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1266,8 +1266,12 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
.map(dbColumn => {

const columnUniqueIndex = dbIndices.find(dbIndex => {
return this.driver.buildTableName(dbIndex["TABLE_NAME"], undefined, dbIndex["TABLE_SCHEMA"]) === tableFullName
&& dbIndex["COLUMN_NAME"] === dbColumn["COLUMN_NAME"] && dbIndex["NON_UNIQUE"] === "0";
const indexTableFullName = this.driver.buildTableName(dbIndex["TABLE_NAME"], undefined, dbIndex["TABLE_SCHEMA"]);
if (indexTableFullName !== tableFullName) {
return false;
}
// tslint:disable-next-line:triple-equals - trying to find truthy value
return dbIndex["COLUMN_NAME"] === dbColumn["COLUMN_NAME"] && dbIndex["NON_UNIQUE"] == "0";
});

const tableMetadata = this.connection.entityMetadatas.find(metadata => metadata.tablePath === table.name);
Expand Down Expand Up @@ -1398,7 +1402,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
table: table,
name: constraint["INDEX_NAME"],
columnNames: indices.map(i => i["COLUMN_NAME"]),
isUnique: constraint["NON_UNIQUE"] === "0",
isUnique: constraint["NON_UNIQUE"] == "0", // tslint:disable-line:triple-equals - trying to find truthy value
isSpatial: constraint["INDEX_TYPE"] === "SPATIAL",
isFulltext: constraint["INDEX_TYPE"] === "FULLTEXT"
});
Expand Down
14 changes: 14 additions & 0 deletions test/github-issues/2737/entity/TestEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Entity } from "../../../../src/decorator/entity/Entity";
import { Column } from "../../../../src/decorator/columns/Column";
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
@Entity()
export class TestEntity {
@PrimaryGeneratedColumn()
id: number;

@Column({ type: "varchar", length: 100, nullable: true, unique: true })
unique_column: string;

@Column({ type: "varchar", length: 100, nullable: true, unique: false })
nonunique_column: string;
}
43 changes: 43 additions & 0 deletions test/github-issues/2737/issue-2737.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {expect} from "chai";
import "reflect-metadata";
import {Connection} from "../../../src/connection/Connection";
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";

describe.only("github issues > #2737 MySQLDriver findChangedColumns (fields: width, precision)", () => {
let connections: Connection[];

before(async () => connections = await createTestingConnections({
dropSchema: false,
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["mysql", "aurora-data-api"],
schemaCreate: false,
cache: false,
driverSpecific: {
bigNumberStrings: false,
},
}));

beforeEach(() => reloadTestingDatabases(connections));

after(() => closeTestingConnections(connections));

it("should not create migrations for an existing unique index when bigNumberStrings is false", () => (
Promise.all(connections.map(async connection => {
const entityMetadata = connection.entityMetadatas.find(x => x.name === "TestEntity");
const indexMetadata = entityMetadata!.indices.find(index => (
index.columns.some(column => column.propertyName === "unique_column")));

// Ensure the setup is correct
expect(indexMetadata).to.exist;
expect(indexMetadata!.isUnique).to.be.true;

await connection.synchronize(false);

const schemaBuilder = connection.driver.createSchemaBuilder();
const syncQueries = await schemaBuilder.log();

expect(syncQueries.downQueries).to.be.an("array").that.is.empty;
expect(syncQueries.upQueries).to.be.an("array").that.is.empty;
})
)));
});

0 comments on commit f67ba15

Please sign in to comment.