Skip to content

Commit 66d04a0

Browse files
authored
Hotfix: PK changes not tracked (#179)
* Add test * Add fix * Fix renamed fields in relations
1 parent 05aaa09 commit 66d04a0

File tree

4 files changed

+87
-43
lines changed

4 files changed

+87
-43
lines changed

src/Mapper/DatabaseMapper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ public function queueUpdate($entity, Node $node, State $state): ContextCarrierIn
121121

122122
// in a future mapper must support solid states
123123
$changes = array_udiff_assoc($data, $state->getTransactionData(), [Node::class, 'compare']);
124-
unset($changes[$this->primaryKey]);
125-
126124
$changedColumns = $this->mapColumns($changes);
125+
unset($changes[$this->primaryKey]);
127126

128127
$update = new Update($this->source->getDatabase(), $this->source->getTable(), $changedColumns);
129128
$state->setStatus(Node::SCHEDULED_UPDATE);

src/Relation/HasMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ protected function queueDelete($related): CommandInterface
163163

164164
if ($this->isNullable()) {
165165
$store = $this->orm->queueStore($related);
166-
$store->register($this->outerKey, null, true);
166+
$store->register($this->columnName($rNode, $this->outerKey), null, true);
167167
$rNode->getState()->decClaim();
168168

169169
return new Condition($store, function () use ($rNode) {

src/Relation/HasOne.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected function deleteOriginal($original): CommandInterface
8787

8888
if ($this->isNullable()) {
8989
$store = $this->orm->queueStore($original);
90-
$store->register($this->outerKey, null, true);
90+
$store->register($this->columnName($rNode, $this->outerKey), null, true);
9191
$rNode->getState()->decClaim();
9292

9393
return new Condition($store, function () use ($rNode) {

tests/ORM/RenamedPKTest.php

Lines changed: 84 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,28 @@ abstract class RenamedPKTest extends BaseTest
1616
{
1717
use TableTrait;
1818

19-
public function setUp(): void
20-
{
21-
parent::setUp();
22-
23-
$this->makeTable(
24-
'simple_entity',
25-
[
26-
'identity_id' => 'primary',
27-
'identity_key' => 'integer,nullable',
28-
]
29-
);
30-
31-
$this->orm = $this->withSchema(
32-
new Schema(
33-
[
34-
Identity::class => [
35-
Schema::ROLE => 'simple_entity',
36-
Schema::DATABASE => 'default',
37-
Schema::TABLE => 'simple_entity',
38-
Schema::MAPPER => Mapper::class,
39-
Schema::PRIMARY_KEY => 'id',
40-
Schema::COLUMNS => [
41-
'id' => 'identity_id',
42-
'key' => 'identity_key',
43-
],
44-
Schema::TYPECAST => [
45-
'id' => 'int',
46-
'key' => 'int',
47-
],
48-
Schema::SCHEMA => [],
49-
Schema::RELATIONS => []
50-
]
51-
]
52-
)
53-
);
54-
}
55-
5619
public function testCreateEmpty(): void
5720
{
21+
$this->createTable1();
22+
$this->orm = $this->withSchema(new Schema($this->getSchemaArray1()));
23+
5824
$u = new Identity();
5925

60-
(new Transaction($this->orm))->persist($u)->run();
26+
$this->save($u);
6127

6228
$this->assertIsInt($u->getId());
6329
}
6430

6531
public function testCreateWithPredefinedId(): void
6632
{
33+
$this->createTable1();
34+
$this->orm = $this->withSchema(new Schema($this->getSchemaArray1()));
35+
6736
$u = new Identity();
6837
$u->setId(2);
6938
$u->setKey(42);
7039

71-
(new Transaction($this->orm))->persist($u)->run();
40+
$this->save($u);
7241

7342
$s = new Select($this->orm->withHeap(new Heap()), Identity::class);
7443
$data = $s->fetchData();
@@ -77,4 +46,80 @@ public function testCreateWithPredefinedId(): void
7746
$this->assertSame(2, $u->getId());
7847
$this->assertSame(42, $u->getKey());
7948
}
49+
50+
public function testChangePK(): void
51+
{
52+
$this->createTable2();
53+
$this->orm = $this->withSchema(new Schema($this->getSchemaArray1()));
54+
55+
$u = new Identity();
56+
$u->setId(5);
57+
$u->setKey(42);
58+
59+
$this->save($u);
60+
61+
$this->orm = $this->orm->withHeap(new Heap());
62+
$data = (new Select($this->orm, Identity::class))
63+
->fetchAll();
64+
$this->assertSame(1, count($data));
65+
$u = $data[0];
66+
$u->setId(8);
67+
68+
$this->captureWriteQueries();
69+
$this->save($u);
70+
$this->assertNumWrites(1);
71+
72+
$this->orm = $this->orm->withHeap(new Heap());
73+
$data = (new Select($this->orm, Identity::class))
74+
->fetchAll();
75+
76+
$this->assertSame(1, count($data));
77+
$this->assertSame(8, $data[0]->getId());
78+
}
79+
80+
private function createTable2(): void
81+
{
82+
$this->makeTable(
83+
'simple_entity',
84+
[
85+
'identity_id' => 'bigInteger',
86+
'identity_key' => 'integer,nullable',
87+
]
88+
);
89+
}
90+
91+
private function createTable1(): void
92+
{
93+
$this->makeTable(
94+
'simple_entity',
95+
[
96+
'identity_id' => 'primary',
97+
'identity_key' => 'integer,nullable',
98+
]
99+
);
100+
}
101+
102+
private function getSchemaArray1(): array
103+
{
104+
105+
return [
106+
Identity::class => [
107+
Schema::ROLE => 'simple_entity',
108+
Schema::DATABASE => 'default',
109+
Schema::TABLE => 'simple_entity',
110+
Schema::MAPPER => Mapper::class,
111+
Schema::PRIMARY_KEY => 'id',
112+
Schema::COLUMNS => [
113+
'id' => 'identity_id',
114+
'key' => 'identity_key',
115+
],
116+
Schema::TYPECAST => [
117+
'id' => 'int',
118+
'key' => 'int',
119+
],
120+
Schema::SCHEMA => [],
121+
Schema::RELATIONS => []
122+
]
123+
];
124+
}
80125
}

0 commit comments

Comments
 (0)