Skip to content
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

Fix Date(time) instance in primary key #677

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
image: mysql:8.0
ports:
- 3307:3306
options: --health-cmd="mysqladmin ping -ppass" --health-interval=10s --health-timeout=5s --health-retries=5 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=nextras_orm_test --entrypoint sh mysql:8 -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
options: --health-cmd="mysqladmin ping -ppass" --health-interval=10s --health-timeout=5s --health-retries=5 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=nextras_orm_test --entrypoint sh mysql:8 -c "exec docker-entrypoint.sh mysqld --mysql-native-password=ON"
mariadb105:
image: mariadb:10.5
env:
Expand Down Expand Up @@ -135,7 +135,7 @@ jobs:
- 1433:1433
options: >-
--name=mssql
--health-cmd "/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'YourStrong!Passw0rd' -Q 'SELECT 1'"
--health-cmd "/opt/mssql-tools18/bin/sqlcmd -C -S localhost -U SA -P 'YourStrong!Passw0rd' -Q 'SELECT 1'"
--health-interval 10s
--health-timeout 5s
--health-retries 5
Expand All @@ -145,7 +145,7 @@ jobs:
uses: actions/checkout@v2

- name: Create MS SQL Database
run: docker exec -i mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'YourStrong!Passw0rd' -Q 'CREATE DATABASE nextras_orm_test'
run: docker exec -i mssql /opt/mssql-tools18/bin/sqlcmd -C -S localhost -U SA -P 'YourStrong!Passw0rd' -Q 'CREATE DATABASE nextras_orm_test'

- name: Setup PHP cache environment
id: php-extensions-cache
Expand Down
15 changes: 12 additions & 3 deletions src/Mapper/Dbal/DbalMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,10 @@ public function persist(IEntity $entity): void

} else {
$primary = [];
$id = (array) $entity->getPersistedId();
$id = $entity->getPersistedId();
if (!is_array($id)) {
$id = [$id];
}
foreach ($entity->getMetadata()->getPrimaryKey() as $key) {
$primary[$key] = array_shift($id);
}
Expand Down Expand Up @@ -458,7 +461,10 @@ protected function processMySQLAutoupdate(IEntity $entity, array $args): void

$conventions = $this->getConventions();

$id = (array) $entity->getPersistedId();
$id = $entity->getPersistedId();
if (!is_array($id)) {
$id = [$id];
}
$primary = [];
foreach ($entity->getMetadata()->getPrimaryKey() as $key) {
$primary[$key] = array_shift($id);
Expand Down Expand Up @@ -487,7 +493,10 @@ public function remove(IEntity $entity): void
$conventions = $this->getConventions();

$primary = [];
$id = (array) $entity->getPersistedId();
$id = $entity->getPersistedId();
if (!is_array($id)) {
$id = [$id];
}
foreach ($entity->getMetadata()->getPrimaryKey() as $key) {
$key = $conventions->convertEntityToStorageKey($key);
$primary[$key] = array_shift($id);
Expand Down
44 changes: 40 additions & 4 deletions tests/cases/integration/Entity/entity.compositePK.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ namespace NextrasTests\Orm\Integration\Entity;


use DateTimeImmutable;
use Nette\Utils\DateTime;
use Nextras\Dbal\IConnection;
use Nextras\Orm\Exception\InvalidArgumentException;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Helper;
use NextrasTests\Orm\User;
use NextrasTests\Orm\UserStat;
use NextrasTests\Orm\UserStatX;
use Tester\Assert;
use Tester\Environment;


require_once __DIR__ . '/../../../bootstrap.php';
Expand Down Expand Up @@ -46,19 +49,52 @@ class EntityCompositePKTest extends DataTestCase

$this->orm->clear();

$userStat = $this->orm->userStats->getBy(['user' => $userId, 'date' => $at]);
Assert::notNull($userStat);
$userStat = $this->orm->userStats->getByChecked(['user' => $userId, 'date' => $at]);
Assert::type(DateTimeImmutable::class, $userStat->id[1]);

$userStat->value = 101;
$this->orm->persistAndFlush($userStat);
}

public function testCompositePKDateTime2(): void
{
if ($this->section === Helper::SECTION_MSSQL) {
// An explicit value for the identity column in table 'users' can only be specified when a column list is used and IDENTITY_INSERT is ON.
// http://stackoverflow.com/questions/2148091/syntax-for-inserting-into-a-table-with-no-values
Environment::skip('Inserting dummy rows when no arguments are passed is not supported.');
}

$user = new User();
$this->orm->persistAndFlush($user);

$stat = new UserStatX();
$stat->user = $user;
$stat->date = '2019-01-01';
$stat->value = 100;
$this->orm->persistAndFlush($stat);

$this->orm->clear();

$res = $this->orm->userStatsX->getByChecked(['date' => new DateTime('2019-01-01')]);
Assert::same(100, $res->value);

$res->value = 200;
$this->orm->persistAndFlush($res);
Assert::same(200, $res->value);

$this->orm->clear();

$res = $this->orm->userStatsX->getByChecked(['date' => new DateTime('2019-01-01')]);
Assert::same(200, $res->value);

Environment::$checkAssertions = false;
}



public function testGetBy(): void
{
$tagFollower = $this->orm->tagFollowers->getBy(['tag' => 3, 'author' => 1]);
Assert::notNull($tagFollower);
$tagFollower = $this->orm->tagFollowers->getByChecked(['tag' => 3, 'author' => 1]);
Assert::same($tagFollower->tag->name, 'Tag 3');
Assert::same($tagFollower->author->name, 'Writer 1');

Expand Down
17 changes: 17 additions & 0 deletions tests/cases/integration/Entity/entity.pk.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace NextrasTests\Orm\Integration\Entity;
use DateTimeImmutable;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Log;
use NextrasTests\Orm\TimeSeries;
use Tester\Assert;


Expand All @@ -32,6 +33,22 @@ class EntityPkTest extends DataTestCase
$entry = $this->orm->logs->getById($datetime);
Assert::true($entry !== null);
}

public function testDateTimeWithProxyPkUpdate(): void
{
$timeSeries = new TimeSeries();
$timeSeries->id = $datetime = new DateTimeImmutable('2022-03-06T03:03:03Z');
$timeSeries->value = 3;
$this->orm->persistAndFlush($timeSeries);

$this->orm->clear();
$timeSeries = $this->orm->timeSeries->getByIdChecked($datetime);
$timeSeries->value = 5;
$this->orm->persistAndFlush($timeSeries);

$entry = $this->orm->timeSeries->getById($datetime);
Assert::true($entry !== null);
}
}


Expand Down
17 changes: 17 additions & 0 deletions tests/db/mssql-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ CREATE TABLE user_stats
CONSTRAINT user_stats_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE NO ACTION ON UPDATE CASCADE
);

CREATE TABLE user_stats_x
(
user_id int NOT NULL,
date date NOT NULL,
value int NOT NULL,
PRIMARY KEY (user_id, date),
CONSTRAINT user_stats_x_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE NO ACTION ON UPDATE CASCADE
);


CREATE TABLE users_x_users
(
Expand Down Expand Up @@ -176,3 +185,11 @@ CREATE TABLE publishers_x_tags
CONSTRAINT publishers_x_tags_tag FOREIGN KEY (tag_id) REFERENCES tags (id),
CONSTRAINT publishers_x_tags_publisher FOREIGN KEY (publisher_id) REFERENCES publishers (publisher_id) ON DELETE CASCADE
);


CREATE TABLE time_series
(
date datetimeoffset NOT NULL,
value int NOT NULL,
PRIMARY KEY (date)
);
31 changes: 20 additions & 11 deletions tests/db/mysql-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ CREATE TABLE photos
) AUTO_INCREMENT = 1;


CREATE TRIGGER `book_collections_bu_trigger` BEFORE UPDATE ON `book_collections`
FOR EACH ROW SET NEW.updated_at = NOW();

CREATE TRIGGER `book_collections_bi_trigger` BEFORE INSERT ON `book_collections`
FOR EACH ROW SET NEW.updated_at = NOW();

ALTER TABLE photo_albums
ADD CONSTRAINT photo_albums_preview_id FOREIGN KEY (preview_id) REFERENCES photos (id) ON DELETE CASCADE ON UPDATE CASCADE;

Expand All @@ -141,6 +147,13 @@ CREATE TABLE user_stats
CONSTRAINT user_stats_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE RESTRICT ON UPDATE CASCADE
);

CREATE TABLE user_stats_x (
user_id int NOT NULL,
date date NOT NULL,
value int NOT NULL,
PRIMARY KEY(user_id, date),
CONSTRAINT user_stats_x_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE TABLE users_x_users
(
Expand All @@ -152,17 +165,6 @@ CREATE TABLE users_x_users
);


CREATE TRIGGER `book_collections_bu_trigger`
BEFORE UPDATE
ON `book_collections`
FOR EACH ROW SET NEW.updated_at = NOW();

CREATE TRIGGER `book_collections_bi_trigger`
BEFORE INSERT
ON `book_collections`
FOR EACH ROW SET NEW.updated_at = NOW();


CREATE TABLE logs
(
date TIMESTAMP NOT NULL,
Expand All @@ -179,3 +181,10 @@ CREATE TABLE publishers_x_tags
CONSTRAINT publishers_x_tags_tag FOREIGN KEY (tag_id) REFERENCES tags (id),
CONSTRAINT publishers_x_tags_publisher FOREIGN KEY (publisher_id) REFERENCES publishers (publisher_id) ON DELETE CASCADE
);

CREATE TABLE time_series
(
date DATETIME NOT NULL,
value int NOT NULL,
PRIMARY KEY (date)
)
18 changes: 17 additions & 1 deletion tests/db/pgsql-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ CREATE TABLE "photos"
CONSTRAINT "photos_album_id" FOREIGN KEY ("album_id") REFERENCES "photo_albums" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);


ALTER TABLE "photo_albums"
ADD CONSTRAINT "photo_albums_preview_id" FOREIGN KEY ("preview_id") REFERENCES "photos" ("id") ON DELETE CASCADE ON UPDATE CASCADE;

Expand All @@ -141,6 +140,15 @@ CREATE TABLE "user_stats"
CONSTRAINT "user_stats_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

CREATE TABLE "user_stats_x"
(
"user_id" int NOT NULL,
"date" date NOT NULL,
"value" int NOT NULL,
PRIMARY KEY ("user_id", "date"),
CONSTRAINT "user_stats_x_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);


CREATE TABLE "users_x_users"
(
Expand Down Expand Up @@ -190,3 +198,11 @@ CREATE TABLE "publishers_x_tags"
CONSTRAINT "publishers_x_tags_tag" FOREIGN KEY ("tag_id") REFERENCES "tags" ("id"),
CONSTRAINT "publishers_x_tags_publisher" FOREIGN KEY ("publisher_id") REFERENCES "publishers" ("publisher_id") ON DELETE CASCADE ON UPDATE CASCADE
);


CREATE TABLE "time_series"
(
"date" TIMESTAMPTZ NOT NULL,
"value" int NOT NULL,
PRIMARY KEY ("date")
);
2 changes: 2 additions & 0 deletions tests/inc/model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @property-read TagFollowersRepository $tagFollowers
* @property-read UsersRepository $users
* @property-read UserStatsRepository $userStats
* @property-read UserStatsXRepository $userStatsX
* @property-read TimeSeriesRepository $timeSeries
*/
class Model extends OrmModel
{
Expand Down
17 changes: 17 additions & 0 deletions tests/inc/model/timeSeries/TimeSeries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;


use DateTimeImmutable;
use Nextras\Orm\Entity\Entity;


/**
* @property DateTimeImmutable $id {primary-proxy}
* @property DateTimeImmutable $date {primary}
* @property int $value
*/
final class TimeSeries extends Entity
{
}
14 changes: 14 additions & 0 deletions tests/inc/model/timeSeries/TimeSeriesMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;


use Nextras\Orm\Mapper\Dbal\DbalMapper;


/**
* @extends DbalMapper<Log>
*/
final class TimeSeriesMapper extends DbalMapper
{
}
18 changes: 18 additions & 0 deletions tests/inc/model/timeSeries/TimeSeriesRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;


use Nextras\Orm\Repository\Repository;


/**
* @extends Repository<Log>
*/
final class TimeSeriesRepository extends Repository
{
static function getEntityClassNames(): array
{
return [TimeSeries::class];
}
}
17 changes: 17 additions & 0 deletions tests/inc/model/userStatX/UserStatX.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;

use DateTimeImmutable;
use Nextras\Orm\Entity\Entity;


/**
* @property array $id {primary-proxy}
* @property User $user {primary} {m:1 User, oneSided=true}
* @property DateTimeImmutable $date {primary}
* @property int $value
*/
final class UserStatX extends Entity
{
}
13 changes: 13 additions & 0 deletions tests/inc/model/userStatX/UserStatsXMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;

use Nextras\Orm\Mapper\Dbal\DbalMapper;


/**
* @extends DbalMapper<UserStatX>
*/
final class UserStatsXMapper extends DbalMapper
{
}
17 changes: 17 additions & 0 deletions tests/inc/model/userStatX/UserStatsXRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace NextrasTests\Orm;

use Nextras\Orm\Repository\Repository;


/**
* @extends Repository<UserStatX>
*/
final class UserStatsXRepository extends Repository
{
static function getEntityClassNames(): array
{
return [UserStatX::class];
}
}
Loading
Loading