Skip to content

Commit

Permalink
fix: wrong FK loaded in multi-database environment (#6828)
Browse files Browse the repository at this point in the history
Fixes an edge case where the wrong foreign key 
would be loaded when multiple databases exist 
with foreign keys that have the same name.

Fixes #6168
  • Loading branch information
ronits-cx committed Oct 4, 2020
1 parent 5ef9450 commit c060f95
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
name = database;
database = this.driver.database || currentDatabase;
}
return `(\`kcu\`.\`TABLE_SCHEMA\` = '${database}' AND \`kcu\`.\`TABLE_NAME\` = '${name}')`;
return `(\`kcu\`.\`TABLE_SCHEMA\` = '${database}' AND \`kcu\`.\`TABLE_NAME\` = '${name}' AND \`rc\`.\`CONSTRAINT_SCHEMA\` = '${database}')`;
}).join(" OR ");
const foreignKeysSql = `SELECT \`kcu\`.\`TABLE_SCHEMA\`, \`kcu\`.\`TABLE_NAME\`, \`kcu\`.\`CONSTRAINT_NAME\`, \`kcu\`.\`COLUMN_NAME\`, \`kcu\`.\`REFERENCED_TABLE_SCHEMA\`, ` +
`\`kcu\`.\`REFERENCED_TABLE_NAME\`, \`kcu\`.\`REFERENCED_COLUMN_NAME\`, \`rc\`.\`DELETE_RULE\` \`ON_DELETE\`, \`rc\`.\`UPDATE_RULE\` \`ON_UPDATE\` ` +
Expand Down
106 changes: 106 additions & 0 deletions test/github-issues/6168/issue-6168.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import "reflect-metadata";
import {Connection} from "../../../src/connection/Connection";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Table} from "../../../src/schema-builder/table/Table";
import { QueryRunner } from "../../../src";
import { expect } from "chai";

const questionName = "question";
const categoryName = "category";

const createTables = async (queryRunner: QueryRunner, dbName: string) => {
const questionTableName = `${dbName}.${questionName}`;
const categoryTableName = `${dbName}.${categoryName}`;

await queryRunner.createTable(new Table({
name: questionTableName,
columns: [
{
name: "id",
type: "int",
isPrimary: true,
isGenerated: true,
generationStrategy: "increment"
},
{
name: "name",
type: "varchar",
}
],
}), true);

await queryRunner.createTable(new Table({
name: categoryTableName,
columns: [
{
name: "id",
type: "int",
isPrimary: true,
isGenerated: true,
generationStrategy: "increment"
},
{
name: "questionId",
type: "int",
}
],
foreignKeys: [
{
columnNames: ["questionId"],
referencedTableName: questionTableName,
referencedColumnNames: ["id"],
name: "FK_CATEGORY_QUESTION"
}
]
}), true);
};

describe("github issues > #6168 fix multiple foreign keys with the same name in a mysql multi-tenanted DB", () => {

let connections: Connection[];
before(async () => {
connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["mysql"],
schemaCreate: false,
dropSchema: false,
});

await reloadTestingDatabases(connections);

for (const connection of connections) {
const queryRunner = connection.createQueryRunner();
await createTables(queryRunner, String(connection.driver.database));
await queryRunner.createDatabase("test2", true);
await createTables(queryRunner, "test2");
await queryRunner.release();
};
});

after(async () => {
for (const connection of connections) {
const queryRunner = connection.createQueryRunner();
await queryRunner.dropDatabase("test2");
await queryRunner.release();
};

await closeTestingConnections(connections);
});

it("should only have one foreign key column", () => Promise.all(connections.map(async connection => {
const queryRunner = connection.createQueryRunner();
const tables = await queryRunner.getTables([questionName, categoryName]);

const questionTable = tables.find(table => table.name === questionName) as Table;
const categoryTable = tables.find(table => table.name === categoryName) as Table;

queryRunner.release();

expect(categoryTable.foreignKeys.length).to.eq(1);
expect(categoryTable.foreignKeys[0].name).to.eq("FK_CATEGORY_QUESTION");
expect(categoryTable.foreignKeys[0].columnNames.length).to.eq(1); // before the fix this was 2, one for each schema
expect(categoryTable.foreignKeys[0].columnNames[0]).to.eq("questionId");

expect(questionTable.foreignKeys.length).to.eq(0);
})));
});

0 comments on commit c060f95

Please sign in to comment.