-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update foreign keys when table name changes (#5482)
* Add repro test for github 5119 * update foreign key matching algo to include table name * fix naming for test 5119 * lint error * fix test for 5119 * Drop unnecessary logging, fix lint errors
- Loading branch information
Showing
7 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
Column, | ||
Entity, | ||
ManyToOne, | ||
PrimaryGeneratedColumn | ||
} from "../../../../../src/index"; | ||
import { User } from "./User"; | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@Column() | ||
text: string; | ||
|
||
@ManyToOne(type => User) | ||
owner: User; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { | ||
Column, | ||
Entity, | ||
PrimaryGeneratedColumn | ||
} from "../../../../../src/index"; | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
ManyToOne | ||
} from "../../../../../src/index"; | ||
import { User } from "./User"; | ||
|
||
@Entity() | ||
export class Account { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@ManyToOne(type => User) | ||
user: User; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
Column, | ||
Entity, | ||
ManyToOne, | ||
PrimaryGeneratedColumn | ||
} from "../../../../../src/index"; | ||
import { Account } from "./Account"; | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@Column() | ||
text: string; | ||
|
||
@ManyToOne(type => Account) | ||
owner: Account; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { | ||
Column, | ||
Entity, | ||
PrimaryGeneratedColumn | ||
} from "../../../../../src/index"; | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import "reflect-metadata"; | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
setupSingleTestingConnection | ||
} from "../../utils/test-utils"; | ||
import { Connection, createConnection } from "../../../src"; | ||
import { fail } from "assert"; | ||
|
||
describe("github issues > #5119 migration with foreign key that changes target", () => { | ||
let connections: Connection[]; | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/v1/*{.js,.ts}"], | ||
enabledDrivers: ["postgres"], | ||
})) | ||
); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections([...connections])); | ||
|
||
it("should generate a drop and create step", async () => { | ||
return Promise.all( | ||
connections.map(async function(_connection) { | ||
const options = setupSingleTestingConnection( | ||
_connection.options.type, | ||
{ | ||
name: `${_connection.name}-v2`, | ||
entities: [__dirname + "/entity/v2/*{.js,.ts}"], | ||
dropSchema: false, | ||
schemaCreate: false | ||
} | ||
); | ||
if (!options) { | ||
fail(); | ||
return; | ||
} | ||
const connection = await createConnection(options); | ||
try { | ||
const sqlInMemory = await connection.driver | ||
.createSchemaBuilder() | ||
.log(); | ||
|
||
const upQueries = sqlInMemory.upQueries.map( | ||
query => query.query | ||
); | ||
const downQueries = sqlInMemory.downQueries.map( | ||
query => query.query | ||
); | ||
upQueries.should.eql([ | ||
`ALTER TABLE "post" DROP CONSTRAINT "FK_4490d00e1925ca046a1f52ddf04"`, | ||
`CREATE TABLE "account" ("id" SERIAL NOT NULL, "userId" integer, CONSTRAINT "PK_54115ee388cdb6d86bb4bf5b2ea" PRIMARY KEY ("id"))`, | ||
`ALTER TABLE "account" ADD CONSTRAINT "FK_60328bf27019ff5498c4b977421" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, | ||
`ALTER TABLE "post" ADD CONSTRAINT "FK_4490d00e1925ca046a1f52ddf04" FOREIGN KEY ("ownerId") REFERENCES "account"("id") ON DELETE NO ACTION ON UPDATE NO ACTION` | ||
]); | ||
downQueries.should.eql([ | ||
`ALTER TABLE "post" ADD CONSTRAINT "FK_4490d00e1925ca046a1f52ddf04" FOREIGN KEY ("ownerId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, | ||
`DROP TABLE "account"`, | ||
`ALTER TABLE "account" DROP CONSTRAINT "FK_60328bf27019ff5498c4b977421"`, | ||
`ALTER TABLE "post" DROP CONSTRAINT "FK_4490d00e1925ca046a1f52ddf04"` | ||
]); | ||
} finally { | ||
connection.close(); | ||
} | ||
}) | ||
); | ||
}); | ||
}); |