-
-
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.
feat: orphanedRowAction=disabled (rebase of PR 8285) (#8678)
* updated implementation, using "disable" keyword * rebase test restructure * rebase orphanedRowAction tests with keyword "disabled" * rename test suite files to reflect changed naming: skip -> disable Simplify test suite to comply with postgres12 * Update tests to reflect 0.3 breaking changes * prettied Co-authored-by: Jannik <jannik@jannikmewes.de>
- Loading branch information
Showing
14 changed files
with
228 additions
and
89 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
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
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
16 changes: 0 additions & 16 deletions
16
test/functional/persistence/delete-orphans/entity/Category.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
...sistence/delete-orphans/delete-orphans.ts → ...stence/orphanage/delete/delete-orphans.ts
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
16 changes: 16 additions & 0 deletions
16
test/functional/persistence/orphanage/delete/entity/Category.ts
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,16 @@ | ||
import { Entity } from "../../../../../../src/decorator/entity/Entity" | ||
import { PrimaryGeneratedColumn } from "../../../../../../src/decorator/columns/PrimaryGeneratedColumn" | ||
import { Post } from "./Post" | ||
import { OneToMany } from "../../../../../../src/decorator/relations/OneToMany" | ||
|
||
@Entity() | ||
export class Category { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@OneToMany(() => Post, (post) => post.category, { | ||
cascade: ["insert"], | ||
eager: true, | ||
}) | ||
posts: Post[] | ||
} |
21 changes: 21 additions & 0 deletions
21
test/functional/persistence/orphanage/delete/entity/Post.ts
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,21 @@ | ||
import { Category } from "./Category" | ||
import { Entity } from "../../../../../../src/decorator/entity/Entity" | ||
import { PrimaryGeneratedColumn } from "../../../../../../src/decorator/columns/PrimaryGeneratedColumn" | ||
import { Column } from "../../../../../../src/decorator/columns/Column" | ||
import { ManyToOne } from "../../../../../../src/decorator/relations/ManyToOne" | ||
import { JoinColumn } from "../../../../../../src/decorator/relations/JoinColumn" | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
categoryId: string | ||
|
||
@ManyToOne(() => Category, (category) => category.posts, { | ||
orphanedRowAction: "delete", | ||
}) | ||
@JoinColumn({ name: "categoryId" }) | ||
category: Category | ||
} |
84 changes: 84 additions & 0 deletions
84
test/functional/persistence/orphanage/disable/disable-orphanage.ts
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,84 @@ | ||
import "reflect-metadata" | ||
import { Connection, Repository } from "../../../../../src/index" | ||
import { | ||
reloadTestingDatabases, | ||
createTestingConnections, | ||
closeTestingConnections, | ||
} from "../../../../utils/test-utils" | ||
import { expect } from "chai" | ||
import { User } from "./entity/User" | ||
import { Setting } from "./entity/Setting" | ||
|
||
describe("persistence > orphanage > disable", () => { | ||
// ------------------------------------------------------------------------- | ||
// Configuration | ||
// ------------------------------------------------------------------------- | ||
|
||
// connect to db | ||
let connections: Connection[] = [] | ||
|
||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(connections)) | ||
after(() => closeTestingConnections(connections)) | ||
|
||
// ------------------------------------------------------------------------- | ||
// Specifications | ||
// ------------------------------------------------------------------------- | ||
|
||
describe("when a User is updated without all settings being loaded...", () => { | ||
let userRepo: Repository<User> | ||
let settingRepo: Repository<Setting> | ||
let userId: number | ||
|
||
beforeEach(async () => { | ||
await Promise.all( | ||
connections.map(async (connection) => { | ||
userRepo = connection.getRepository(User) | ||
settingRepo = connection.getRepository(Setting) | ||
}), | ||
) | ||
|
||
const user = await userRepo.save(new User()) | ||
user.settings = [ | ||
new Setting("foo"), | ||
new Setting("bar"), | ||
new Setting("moo"), | ||
] | ||
|
||
await userRepo.save(user) | ||
userId = user.id | ||
|
||
const userToUpdate = (await userRepo.findOneBy({ id: userId }))! | ||
userToUpdate.settings = [ | ||
// untouched setting | ||
userToUpdate.settings[0], | ||
// updated setting | ||
{ ...userToUpdate.settings[1], data: "bar_updated" }, | ||
// skipped setting | ||
// new Setting("moo"), | ||
// new setting | ||
new Setting("cow"), | ||
] | ||
|
||
await userRepo.save(userToUpdate) | ||
}) | ||
|
||
it("should not delete setting with orphanedRowAction=disabed", async () => { | ||
const user = await userRepo.findOneBy({ id: userId }) | ||
expect(user).not.to.be.undefined | ||
expect(user!.settings).to.have.lengthOf(4) | ||
}) | ||
|
||
it("should not orphane any Settings", async () => { | ||
const itemsWithoutForeignKeys = (await settingRepo.find()).filter( | ||
(p) => !p.userId, | ||
) | ||
expect(itemsWithoutForeignKeys).to.have.lengthOf(0) | ||
}) | ||
}) | ||
}) |
28 changes: 28 additions & 0 deletions
28
test/functional/persistence/orphanage/disable/entity/Setting.ts
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,28 @@ | ||
import { User } from "./User" | ||
import { Entity } from "../../../../../../src/decorator/entity/Entity" | ||
import { PrimaryGeneratedColumn } from "../../../../../../src/decorator/columns/PrimaryGeneratedColumn" | ||
import { Column } from "../../../../../../src/decorator/columns/Column" | ||
import { ManyToOne } from "../../../../../../src/decorator/relations/ManyToOne" | ||
import { JoinColumn } from "../../../../../../src/decorator/relations/JoinColumn" | ||
|
||
@Entity() | ||
export class Setting { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
data: string | ||
|
||
@Column() | ||
userId: string | ||
|
||
@ManyToOne(() => User, (user) => user.settings, { | ||
orphanedRowAction: "disable", | ||
}) | ||
@JoinColumn({ name: "userId" }) | ||
user: User | ||
|
||
constructor(data: string) { | ||
this.data = data | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/functional/persistence/orphanage/disable/entity/User.ts
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,16 @@ | ||
import { Entity } from "../../../../../../src/decorator/entity/Entity" | ||
import { PrimaryGeneratedColumn } from "../../../../../../src/decorator/columns/PrimaryGeneratedColumn" | ||
import { Setting } from "./Setting" | ||
import { OneToMany } from "../../../../../../src/decorator/relations/OneToMany" | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@OneToMany(() => Setting, (setting) => setting.user, { | ||
cascade: true, | ||
eager: true, | ||
}) | ||
settings: Setting[] | ||
} |