Skip to content

Commit

Permalink
Postgres support (#109)
Browse files Browse the repository at this point in the history
* wip

* wip

* fix postgres ci

* fixes

* backward compatibility

* fixes

* fixes

* fixes

* fixes

* fixes

* fix ci

* fix ci

* fixes

* fix ci

* fix coverage

* fixes

* reduce ci jobs

* fix mariadb

* fix postgres

* fix ci

* fix ci

* fix ci

* fix ci

* fix ci

* fix ci

* ci test

* ci test

* fix tests

* fix tests

* fixes

* fixes

* fixes

* ci test

* ci test

* ci test

* ci test

* ci test

* ci test

* ci test

* ci test

* ci test

* ci test

* ci test

* ci test

* ci test
  • Loading branch information
MatanYadaev authored Feb 24, 2024
1 parent eac8c07 commit 920c897
Show file tree
Hide file tree
Showing 40 changed files with 362 additions and 141 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/pest-coverage.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Tests coverage

on: [ push, pull_request ]
on:
push:
branches:
- master
pull_request:

jobs:
test:
Expand Down
22 changes: 16 additions & 6 deletions .github/workflows/pest.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Tests

on: [ push, pull_request ]
on:
push:
branches:
- master
pull_request:

jobs:
test:
Expand All @@ -13,8 +17,8 @@ jobs:
matrix:
php: [ 8.3, 8.2, 8.1 ]
laravel: [ 10.* ]
db: [ 'mysql:8.0', 'mysql:5.7', 'mariadb:10.9' ]
dependency-version: [ prefer-lowest, prefer-stable ]
db: [ 'mysql:8.0', 'mysql:5.7', 'mariadb:10.11', 'postgis/postgis:16-3.4', 'postgis/postgis:15-3.4', 'postgis/postgis:14-3.4', 'postgis/postgis:13-3.4', 'postgis/postgis:12-3.4' ]
dependency-version: [ prefer-stable, prefer-lowest ]
include:
- laravel: 10.*
testbench: ^8.0
Expand All @@ -25,9 +29,14 @@ jobs:
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: laravel_eloquent_spatial_test
POSTGRES_DB: laravel_eloquent_spatial_test
POSTGRES_USER: root
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
- ${{ contains(matrix.db, 'postgis') && '5432' || '3306' }}
options: >-
${{ (contains(matrix.db, 'postgis') && '--health-cmd="pg_isready"') || '--health-cmd="mysqladmin ping"' }}
--health-interval=10s --health-timeout=5s --health-retries=3
steps:
- name: Checkout code
Expand All @@ -46,5 +55,6 @@ jobs:
- name: Execute tests
env:
DB_PORT: ${{ job.services.db.ports['3306'] }}
DB_PORT: ${{ job.services.db.ports[contains(matrix.db, 'postgis') && '5432' || '3306'] }}
DB_CONNECTION: ${{ contains(matrix.db, 'postgis') && 'pgsql' || 'mysql' }}
run: vendor/bin/pest
6 changes: 5 additions & 1 deletion .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Lint

on: [ push, pull_request ]
on:
push:
branches:
- master
pull_request:

jobs:
php-cs-fixer:
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Static code analysis

on: [ push, pull_request ]
on:
push:
branches:
- master
pull_request:

jobs:
phpstan:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
.php-cs-fixer.cache
.phpunit.cache
.phpunit.result.cache
build
composer.lock
Expand Down
7 changes: 6 additions & 1 deletion .run/Test.run.xml → .run/Test - MySQL 8.0.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test" type="PestRunConfigurationType">
<configuration default="false" name="Test - MySQL 8.0" type="PestRunConfigurationType">
<CommandLine>
<envs>
<env name="DB_PORT" value="3307" />
</envs>
</CommandLine>
<option name="pestRunnerSettings">
<PestRunner method="" scope="ConfigurationFile" />
</option>
Expand Down
17 changes: 17 additions & 0 deletions .run/Test - Postgres.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test - Postgres" type="PestRunConfigurationType">
<CommandLine>
<envs>
<env name="DB_CONNECTION" value="pgsql" />
<env name="DB_PORT" value="5433" />
</envs>
</CommandLine>
<option name="pestRunnerSettings">
<PestRunner method="" scope="ConfigurationFile" />
</option>
<option name="runnerSettings">
<PhpTestRunnerSettings method="" scope="ConfigurationFile" />
</option>
<method v="2" />
</configuration>
</component>
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

**This Laravel package allows you to easily work with spatial data types and functions.**

This package supports MySQL v8, MySQL v5.7, and MariaDB v10.
Supported databases:
- MySQL 5.7/8
- MariaDB 10
- Postgres 12/13/14/15/16 with PostGIS 3.4

## Getting Started

Expand Down Expand Up @@ -177,8 +180,8 @@ echo $londonEyePoint->getName(); // Point
Here are some useful commands for development:
* Run tests: `composer pest`
* Run tests with coverage: `composer pest-coverage`
* Run tests: `composer pest:mysql` or `composer pest:postgres`
* Run tests with coverage: `composer pest-coverage:mysql`
* Perform type checking: `composer phpstan`
* Format your code: `composer php-cs-fixer`
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
"scripts": {
"php-cs-fixer": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --allow-risky=yes",
"phpstan": "./vendor/bin/phpstan analyse --memory-limit=2G",
"pest": "./vendor/bin/pest",
"pest-coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage --min=100"
"pest:mysql": "DB_PORT=3307 ./vendor/bin/pest",
"pest:postgres": "DB_CONNECTION=pgsql DB_PORT=5433 ./vendor/bin/pest",
"pest-coverage:mysql": "XDEBUG_MODE=coverage DB_PORT=3307 ./vendor/bin/pest --coverage --min=100"
},
"config": {
"sort-packages": true,
Expand Down
17 changes: 15 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.8'

services:
db:
mysql:
container_name: mysql-laravel-eloquent-spatial
image: mysql:8.0
environment:
Expand All @@ -10,8 +10,21 @@ services:
volumes:
- mysql_data:/var/lib/mysql
ports:
- 3306:3306
- 3307:3306
restart: unless-stopped
postgres:
container_name: postgres-laravel-eloquent-spatial
image: postgis/postgis:16-2
environment:
POSTGRES_DB: laravel_eloquent_spatial_test
POSTGRES_USER: root
POSTGRES_HOST_AUTH_METHOD: trust
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- 5433:5432
restart: unless-stopped

volumes:
mysql_data:
postgres_data:
11 changes: 11 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ parameters:
- tests
excludePaths:
- src/Factory.php
ignoreErrors:
-
message: '#Call to an undefined method Illuminate\\Database\\Schema\\ColumnDefinition\:\:(isGeometry|projection)\(\)#'
path: tests/database/migrations/*.php
-
message: '#Undefined variable: \$this#'
path: tests/Expectations.php
-
message: '#Call to an undefined method Pest\\Expectation\<.+\>\:\:toBe(InstanceOf)?On(Postgres|Mysql)\(\)#'
path: tests/*.php

level: max
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: false
34 changes: 17 additions & 17 deletions src/AxisOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@
use Illuminate\Database\MySqlConnection;
use PDO;

/** @codeCoverageIgnore */
class AxisOrder
{
public function __construct()
public static function supported(ConnectionInterface $connection): bool
{
}

public function supported(ConnectionInterface $connection): bool
{
/** @var MySqlConnection $connection */
if ($this->isMariaDb($connection)) {
// @codeCoverageIgnoreStart
if (self::isMariaDb($connection)) {
return false;
// @codeCoverageIgnoreEnd
}

if ($this->isMySql57($connection)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
if (self::isMySql8OrAbove($connection)) {
return true;
}

return true;
return false;
}

private function isMariaDb(MySqlConnection $connection): bool
private static function isMariaDb(ConnectionInterface $connection): bool
{
if (! ($connection instanceof MySqlConnection)) {
return false;
}

return $connection->isMaria();
}

private function isMySql57(MySqlConnection $connection): bool
private static function isMySql8OrAbove(ConnectionInterface $connection): bool
{
if (! ($connection instanceof MySqlConnection)) {
return false;
}

/** @var string $version */
$version = $connection->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);

return version_compare($version, '5.8.0', '<');
return version_compare($version, '8.0.0', '>=');
}
}
22 changes: 22 additions & 0 deletions src/Doctrine/GeographyType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace MatanYadaev\EloquentSpatial\Doctrine;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

/** @codeCoverageIgnore */
class GeographyType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return 'geography';
}

public function getName(): string
{
return 'geography';
}
}
3 changes: 1 addition & 2 deletions src/Doctrine/GeometryCollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

/** @codeCoverageIgnore */
class GeometryCollectionType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
// @codeCoverageIgnoreStart
return 'geometrycollection';
// @codeCoverageIgnoreEnd
}

public function getName(): string
Expand Down
22 changes: 22 additions & 0 deletions src/Doctrine/GeometryType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace MatanYadaev\EloquentSpatial\Doctrine;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

/** @codeCoverageIgnore */
class GeometryType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return 'geometry';
}

public function getName(): string
{
return 'geometry';
}
}
3 changes: 1 addition & 2 deletions src/Doctrine/LineStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

/** @codeCoverageIgnore */
class LineStringType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
// @codeCoverageIgnoreStart
return 'linestring';
// @codeCoverageIgnoreEnd
}

public function getName(): string
Expand Down
3 changes: 1 addition & 2 deletions src/Doctrine/MultiLineStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

/** @codeCoverageIgnore */
class MultiLineStringType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
// @codeCoverageIgnoreStart
return 'multilinestring';
// @codeCoverageIgnoreEnd
}

public function getName(): string
Expand Down
3 changes: 1 addition & 2 deletions src/Doctrine/MultiPointType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

/** @codeCoverageIgnore */
class MultiPointType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
// @codeCoverageIgnoreStart
return 'multipoint';
// @codeCoverageIgnoreEnd
}

public function getName(): string
Expand Down
3 changes: 1 addition & 2 deletions src/Doctrine/MultiPolygonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

/** @codeCoverageIgnore */
class MultiPolygonType extends Type
{
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
// @codeCoverageIgnoreStart
return 'multipolygon';
// @codeCoverageIgnoreEnd
}

public function getName(): string
Expand Down
Loading

0 comments on commit 920c897

Please sign in to comment.