-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit order for removals has to consider SET NULL, not nullable
#10566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SenseException
merged 1 commit into
doctrine:entity-level-commit-order
from
mpdude:delete-commit-order
May 30, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
177 changes: 177 additions & 0 deletions
177
tests/Doctrine/Tests/ORM/Functional/Ticket/GH10566Test.php
This file contains hidden or 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,177 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Doctrine\Tests\ORM\Functional\Ticket; | ||
|
|
||
| use Doctrine\ORM\Mapping as ORM; | ||
| use Doctrine\Tests\OrmFunctionalTestCase; | ||
| use Generator; | ||
|
|
||
| use function is_a; | ||
|
|
||
| class GH10566Test extends OrmFunctionalTestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->createSchemaForModels( | ||
| GH10566A::class, | ||
| GH10566B::class, | ||
| GH10566C::class | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider provideEntityClasses | ||
| */ | ||
| public function testInsertion(string $startEntityClass): void | ||
| { | ||
| $a = new GH10566A(); | ||
| $b = new GH10566B(); | ||
| $c = new GH10566C(); | ||
|
|
||
| $a->other = $b; | ||
| $b->other = $c; | ||
| $c->other = $a; | ||
|
|
||
| foreach ([$a, $b, $c] as $candidate) { | ||
| if (is_a($candidate, $startEntityClass)) { | ||
| $this->_em->persist($candidate); | ||
| } | ||
| } | ||
|
|
||
| // Since all associations are nullable, the ORM has no problem finding an insert order, | ||
| // it can always schedule "deferred updates" to fill missing foreign key values. | ||
| $this->_em->flush(); | ||
|
|
||
| self::assertNotNull($a->id); | ||
| self::assertNotNull($b->id); | ||
| self::assertNotNull($c->id); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider provideEntityClasses | ||
| */ | ||
| public function testRemoval(string $startEntityClass): void | ||
| { | ||
| $a = new GH10566A(); | ||
| $b = new GH10566B(); | ||
| $c = new GH10566C(); | ||
|
|
||
| $a->other = $b; | ||
| $b->other = $c; | ||
| $c->other = $a; | ||
|
|
||
| $this->_em->persist($a); | ||
| $this->_em->flush(); | ||
|
|
||
| $aId = $a->id; | ||
| $bId = $b->id; | ||
| $cId = $c->id; | ||
|
|
||
| // In the removal case, the ORM currently does not schedule "extra updates" | ||
| // to break association cycles before entities are removed. So, we must not | ||
| // look at "nullable" for associations to find a delete commit order. | ||
| // | ||
| // To make it work, the user needs to have a database-level "ON DELETE SET NULL" | ||
| // on an association. That's where the cycle can be broken. Commit order computation | ||
| // for the removal case needs to look at this property. | ||
| // | ||
| // In this example, only A -> B can be used to break the cycle. So, regardless which | ||
| // entity we start with, the ORM-level cascade will always remove all three entities, | ||
| // and the order of database deletes always has to be (can only be) from B, then C, then A. | ||
|
|
||
| foreach ([$a, $b, $c] as $candidate) { | ||
| if (is_a($candidate, $startEntityClass)) { | ||
| $this->_em->remove($candidate); | ||
| } | ||
| } | ||
|
|
||
| $this->_em->flush(); | ||
|
|
||
| self::assertFalse($this->_em->getConnection()->fetchOne('SELECT id FROM gh10566_a WHERE id = ?', [$aId])); | ||
| self::assertFalse($this->_em->getConnection()->fetchOne('SELECT id FROM gh10566_b WHERE id = ?', [$bId])); | ||
| self::assertFalse($this->_em->getConnection()->fetchOne('SELECT id FROM gh10566_c WHERE id = ?', [$cId])); | ||
| } | ||
|
|
||
| public function provideEntityClasses(): Generator | ||
| { | ||
| yield [GH10566A::class]; | ||
| yield [GH10566B::class]; | ||
| yield [GH10566C::class]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @ORM\Entity | ||
| * @ORM\Table(name="gh10566_a") | ||
| */ | ||
| class GH10566A | ||
| { | ||
| /** | ||
| * @ORM\Id | ||
| * @ORM\Column(type="integer") | ||
| * @ORM\GeneratedValue() | ||
| * | ||
| * @var int | ||
| */ | ||
| public $id; | ||
|
|
||
| /** | ||
| * @ORM\OneToOne(targetEntity="GH10566B", cascade={"all"}) | ||
| * @ORM\JoinColumn(nullable=true, onDelete="SET NULL") | ||
| * | ||
| * @var GH10566B | ||
| */ | ||
| public $other; | ||
| } | ||
|
|
||
| /** | ||
| * @ORM\Entity | ||
| * @ORM\Table(name="gh10566_b") | ||
| */ | ||
| class GH10566B | ||
| { | ||
| /** | ||
| * @ORM\Id | ||
| * @ORM\Column(type="integer") | ||
| * @ORM\GeneratedValue() | ||
| * | ||
| * @var int | ||
| */ | ||
| public $id; | ||
|
|
||
| /** | ||
| * @ORM\OneToOne(targetEntity="GH10566C", cascade={"all"}) | ||
| * @ORM\JoinColumn(nullable=true) | ||
| * | ||
| * @var GH10566C | ||
| */ | ||
| public $other; | ||
| } | ||
|
|
||
| /** | ||
| * @ORM\Entity | ||
| * @ORM\Table(name="gh10566_c") | ||
| */ | ||
| class GH10566C | ||
| { | ||
| /** | ||
| * @ORM\Id | ||
| * @ORM\Column(type="integer") | ||
| * @ORM\GeneratedValue() | ||
| * | ||
| * @var int | ||
| */ | ||
| public $id; | ||
|
|
||
| /** | ||
| * @ORM\OneToOne(targetEntity="GH10566A", cascade={"all"}) | ||
| * @ORM\JoinColumn(nullable=true) | ||
| * | ||
| * @var GH10566A | ||
| */ | ||
| public $other; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed with ppl on Slack that we need not worry about objects being proxies here