Skip to content

Commit

Permalink
Added support for Postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Miu committed Dec 5, 2020
1 parent 4436d90 commit f5a8f82
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 30 deletions.
39 changes: 24 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ dev_2x ]

jobs:
build:
test:
strategy:
matrix:
php: ['7.2']
Expand All @@ -18,17 +18,17 @@ jobs:

runs-on: ubuntu-latest

services:
siriusorm_mysql:
image: mysql:5.7
env:
MYSQL_USER: sirius
MYSQL_PASSWORD: sirius
MYSQL_DATABASE: siriusorm
MYSQL_ROOT_PASSWORD: sirius
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
# services:
# siriusorm_mysql:
# image: mysql:5.7
# env:
# MYSQL_USER: sirius
# MYSQL_PASSWORD: sirius
# MYSQL_DATABASE: siriusorm
# MYSQL_ROOT_PASSWORD: sirius
# ports:
# - 3306:3306
# options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- uses: actions/checkout@v2
Expand All @@ -38,20 +38,28 @@ jobs:
# mysql version: '8.0' # Optional, default value is "latest". The version of the MySQL
# mysql database: 'siriusorm' # Optional, default value is "test". The specified database which will be create
# mysql root password: 'rootpass' # Required if "mysql user" is empty, default is empty. The root superuser password
- name: Setup DB services
run: |
cd docker
docker-compose up -d
cd ..
- name: Verify MySQL connection
run: |
while ! mysqladmin ping -h"127.0.0.1" -P3306 --silent; do
while ! mysqladmin ping -h"127.0.0.1" -P13306 --silent; do
sleep 1
done
- name: Setup PHP with fail-fast
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
coverage: pcov
tools: pecl
extensions: mbstring, pdo, pdo_pgsql
env:
fail-fast: true

- name: Validate composer.json and composer.lock
run: composer validate

Expand All @@ -70,9 +78,10 @@ jobs:

- name: Run test suite
env:
MYSQL_CONNECTION: 'mysql:host=127.0.0.1;dbname=siriusorm'
MYSQL_CONNECTION: 'mysql:host=127.0.0.1;port=13306dbname=siriusorm'
MYSQL_USER: 'root'
MYSQL_PASS: 'sirius'
POSTGRES_CONNECTION: 'pgsql:host=siriusorm_postgres;port=15423;dbname=siriusorm'
run: |
mkdir -p build/logs
vendor/bin/phpunit
Expand Down
20 changes: 10 additions & 10 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ services:
networks:
- local

sqlserver:
image: microsoft/mssql-server-linux
container_name: siriusorm_mssql
ports:
- "11433:1433"
environment:
SA_PASSWORD: "SSpaSS__1"
ACCEPT_EULA: "Y"
networks:
- local
# sqlserver:
# image: microsoft/mssql-server-linux
# container_name: siriusorm_mssql
# ports:
# - "11433:1433"
# environment:
# SA_PASSWORD: "SSpaSS__1"
# ACCEPT_EULA: "Y"
# networks:
# - local

mysql_latest:
image: mysql:latest
Expand Down
11 changes: 10 additions & 1 deletion src/Entity/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,18 @@ protected function queryRelation($name)
{
/** @var Relation $relation */
$relation = $this->relations[$name];
/** @var Query $query */
/** @var Query|null $query */
$query = $relation->getQuery($this);

/**
* query can be null if there are no entities to be retrieved
* this is when the native keys are `null` in which case
* there's no need for a query to be constructed and executed
*/
if (!$query) {
return [];
}

$queryCallback = $this->relationCallback[$name] ?? null;
if ($queryCallback && is_callable($queryCallback)) {
$query = $queryCallback($query);
Expand Down
7 changes: 5 additions & 2 deletions src/Relation/ManyToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ public function getQuery(Tracker $tracker)
$nativeKey = $this->options[RelationConfig::NATIVE_KEY];
$nativePks = $tracker->pluck($nativeKey, $this->nativeEntityHydrator);

$query = $this->foreignMapper
->newQuery();
if (empty($nativePks)) {
return null;
}

$query = $this->foreignMapper->newQuery();

$query = $this->joinWithThroughTable($query)
->where($this->options[RelationConfig::THROUGH_NATIVE_COLUMN], $nativePks);
Expand Down
4 changes: 4 additions & 0 deletions src/Relation/OneToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function getQuery(Tracker $tracker)
$nativeKey = $this->options[RelationConfig::NATIVE_KEY];
$nativePks = $tracker->pluck($nativeKey, $this->nativeEntityHydrator);

if (empty($nativePks)) {
return null;
}

$query = $this->foreignMapper
->newQuery()
->where($this->options[RelationConfig::FOREIGN_KEY], $nativePks);
Expand Down
4 changes: 4 additions & 0 deletions src/Relation/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ public function getQuery(Tracker $tracker)
$nativeKey = $this->options[RelationConfig::NATIVE_KEY];
$nativePks = $tracker->pluck($nativeKey, $this->nativeEntityHydrator);

if (empty($nativePks)) {
return null;
}

$query = $this->foreignMapper
->newQuery()
->where($this->foreignMapper->getConfig()->getPrimaryKey(), $nativePks);
Expand Down
4 changes: 2 additions & 2 deletions tests/DbTests/Base/Relation/ManyToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ public function test_delete_pivot_rows()
$product = $this->productsMapper->find(1);
$this->productsMapper->delete($product);

$this->assertRowDeleted('tbl_links_to_tags', 'tagable_type="products" and tagable_id=1');
$this->assertRowPresent('tbl_links_to_tags', 'tagable_type="categories" and tagable_id=1');
$this->assertRowDeleted('tbl_links_to_tags', 'tagable_type=\'products\' and tagable_id=1');
$this->assertRowPresent('tbl_links_to_tags', 'tagable_type=\'categories\' and tagable_id=1');
}

protected function populateDb(): void
Expand Down
9 changes: 9 additions & 0 deletions tests/DbTests/Postgres/Action/DeleteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Sirius\Orm\Tests\DbTests\Postgres\Action;

class DeleteTest extends \Sirius\Orm\Tests\DbTests\Base\Action\DeleteTest
{
protected $dbEngine = 'postgres';
}
9 changes: 9 additions & 0 deletions tests/DbTests/Postgres/Action/InsertTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Sirius\Orm\Tests\DbTests\Postgres\Action;

class InsertTest extends \Sirius\Orm\Tests\DbTests\Base\Action\InsertTest
{
protected $dbEngine = 'postgres';
}
9 changes: 9 additions & 0 deletions tests/DbTests/Postgres/Action/UpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Sirius\Orm\Tests\DbTests\Postgres\Action;

class UpdateTest extends \Sirius\Orm\Tests\DbTests\Base\Action\UpdateTest
{
protected $dbEngine = 'postgres';
}
9 changes: 9 additions & 0 deletions tests/DbTests/Postgres/Behaviour/SoftDeleteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Sirius\Orm\Tests\DbTests\Postgres\Behaviour;

class SoftDeleteTest extends \Sirius\Orm\Tests\DbTests\Base\Behaviour\SoftDeleteTest
{
protected $dbEngine = 'postgres';
}
9 changes: 9 additions & 0 deletions tests/DbTests/Postgres/Behaviour/TimestampsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Sirius\Orm\Tests\DbTests\Postgres\Behaviour;

class TimestampsTest extends \Sirius\Orm\Tests\DbTests\Base\Behaviour\TimestampsTest
{
protected $dbEngine = 'postgres';
}
9 changes: 9 additions & 0 deletions tests/DbTests/Postgres/Relation/ManyToManyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Sirius\Orm\Tests\DbTests\Postgres\Relation;

class ManyToManyTest extends \Sirius\Orm\Tests\DbTests\Base\Relation\ManyToManyTest
{
protected $dbEngine = 'postgres';
}
9 changes: 9 additions & 0 deletions tests/DbTests/Postgres/Relation/ManyToOneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Sirius\Orm\Tests\DbTests\Postgres\Relation;

class ManyToOneTest extends \Sirius\Orm\Tests\DbTests\Base\Relation\ManyToOneTest
{
protected $dbEngine = 'postgres';
}
9 changes: 9 additions & 0 deletions tests/DbTests/Postgres/Relation/OneToManyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Sirius\Orm\Tests\DbTests\Postgres\Relation;

class OneToManyTest extends \Sirius\Orm\Tests\DbTests\Base\Relation\OneToManyTest
{
protected $dbEngine = 'postgres';
}
9 changes: 9 additions & 0 deletions tests/DbTests/Postgres/Relation/OneToOneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Sirius\Orm\Tests\DbTests\Postgres\Relation;

class OneToOneTest extends \Sirius\Orm\Tests\DbTests\Base\Relation\OneToOneTest
{
protected $dbEngine = 'postgres';
}
9 changes: 9 additions & 0 deletions tests/DbTests/Postgres/Relation/RelationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Sirius\Orm\Tests\DbTests\Postgres\Relation;

class RelationTest extends \Sirius\Orm\Tests\DbTests\Base\Relation\RelationTest
{
protected $dbEngine = 'postgres';
}

0 comments on commit f5a8f82

Please sign in to comment.