Skip to content

Commit

Permalink
Hotfix: PK changes not tracked (#179)
Browse files Browse the repository at this point in the history
* Add test

* Add fix

* Fix renamed fields in relations
  • Loading branch information
roxblnfk authored May 5, 2021
1 parent 05aaa09 commit 66d04a0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 43 deletions.
3 changes: 1 addition & 2 deletions src/Mapper/DatabaseMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ public function queueUpdate($entity, Node $node, State $state): ContextCarrierIn

// in a future mapper must support solid states
$changes = array_udiff_assoc($data, $state->getTransactionData(), [Node::class, 'compare']);
unset($changes[$this->primaryKey]);

$changedColumns = $this->mapColumns($changes);
unset($changes[$this->primaryKey]);

$update = new Update($this->source->getDatabase(), $this->source->getTable(), $changedColumns);
$state->setStatus(Node::SCHEDULED_UPDATE);
Expand Down
2 changes: 1 addition & 1 deletion src/Relation/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected function queueDelete($related): CommandInterface

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

return new Condition($store, function () use ($rNode) {
Expand Down
2 changes: 1 addition & 1 deletion src/Relation/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function deleteOriginal($original): CommandInterface

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

return new Condition($store, function () use ($rNode) {
Expand Down
123 changes: 84 additions & 39 deletions tests/ORM/RenamedPKTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,28 @@ abstract class RenamedPKTest extends BaseTest
{
use TableTrait;

public function setUp(): void
{
parent::setUp();

$this->makeTable(
'simple_entity',
[
'identity_id' => 'primary',
'identity_key' => 'integer,nullable',
]
);

$this->orm = $this->withSchema(
new Schema(
[
Identity::class => [
Schema::ROLE => 'simple_entity',
Schema::DATABASE => 'default',
Schema::TABLE => 'simple_entity',
Schema::MAPPER => Mapper::class,
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => [
'id' => 'identity_id',
'key' => 'identity_key',
],
Schema::TYPECAST => [
'id' => 'int',
'key' => 'int',
],
Schema::SCHEMA => [],
Schema::RELATIONS => []
]
]
)
);
}

public function testCreateEmpty(): void
{
$this->createTable1();
$this->orm = $this->withSchema(new Schema($this->getSchemaArray1()));

$u = new Identity();

(new Transaction($this->orm))->persist($u)->run();
$this->save($u);

$this->assertIsInt($u->getId());
}

public function testCreateWithPredefinedId(): void
{
$this->createTable1();
$this->orm = $this->withSchema(new Schema($this->getSchemaArray1()));

$u = new Identity();
$u->setId(2);
$u->setKey(42);

(new Transaction($this->orm))->persist($u)->run();
$this->save($u);

$s = new Select($this->orm->withHeap(new Heap()), Identity::class);
$data = $s->fetchData();
Expand All @@ -77,4 +46,80 @@ public function testCreateWithPredefinedId(): void
$this->assertSame(2, $u->getId());
$this->assertSame(42, $u->getKey());
}

public function testChangePK(): void
{
$this->createTable2();
$this->orm = $this->withSchema(new Schema($this->getSchemaArray1()));

$u = new Identity();
$u->setId(5);
$u->setKey(42);

$this->save($u);

$this->orm = $this->orm->withHeap(new Heap());
$data = (new Select($this->orm, Identity::class))
->fetchAll();
$this->assertSame(1, count($data));
$u = $data[0];
$u->setId(8);

$this->captureWriteQueries();
$this->save($u);
$this->assertNumWrites(1);

$this->orm = $this->orm->withHeap(new Heap());
$data = (new Select($this->orm, Identity::class))
->fetchAll();

$this->assertSame(1, count($data));
$this->assertSame(8, $data[0]->getId());
}

private function createTable2(): void
{
$this->makeTable(
'simple_entity',
[
'identity_id' => 'bigInteger',
'identity_key' => 'integer,nullable',
]
);
}

private function createTable1(): void
{
$this->makeTable(
'simple_entity',
[
'identity_id' => 'primary',
'identity_key' => 'integer,nullable',
]
);
}

private function getSchemaArray1(): array
{

return [
Identity::class => [
Schema::ROLE => 'simple_entity',
Schema::DATABASE => 'default',
Schema::TABLE => 'simple_entity',
Schema::MAPPER => Mapper::class,
Schema::PRIMARY_KEY => 'id',
Schema::COLUMNS => [
'id' => 'identity_id',
'key' => 'identity_key',
],
Schema::TYPECAST => [
'id' => 'int',
'key' => 'int',
],
Schema::SCHEMA => [],
Schema::RELATIONS => []
]
];
}
}

0 comments on commit 66d04a0

Please sign in to comment.