Skip to content

Commit

Permalink
Auto register supportes types (#53)
Browse files Browse the repository at this point in the history
- tsrange
- numeric
  • Loading branch information
pvsaintpe authored Jan 2, 2021
1 parent 2119155 commit 69e00e5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/PostgresConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
use Umbrellio\Postgres\Schema\Builder;
use Umbrellio\Postgres\Schema\Grammars\PostgresGrammar;
use Umbrellio\Postgres\Schema\Subscribers\SchemaAlterTableChangeColumnSubscriber;
use Umbrellio\Postgres\Schema\Types\NumericType;
use Umbrellio\Postgres\Schema\Types\TsRangeType;

class PostgresConnection extends BasePostgresConnection
{
use Macroable;

private static $extensions = [];

private $initialTypes = [
TsRangeType::TYPE_NAME => TsRangeType::class,
NumericType::TYPE_NAME => NumericType::class,
];

/**
* @param AbstractExtension|string $extension
* @throws ExtensionInvalidException
Expand Down Expand Up @@ -52,6 +59,7 @@ public function useDefaultPostProcessor(): void
parent::useDefaultPostProcessor();

$this->registerExtensions();
$this->registerInitialTypes();
}

public function getDoctrineConnection(): Connection
Expand Down Expand Up @@ -109,6 +117,15 @@ protected function getDefaultSchemaGrammar()
return $this->withTablePrefix(new PostgresGrammar());
}

private function registerInitialTypes(): void
{
foreach ($this->initialTypes as $type => $typeClass) {
$this
->getSchemaBuilder()
->registerCustomDoctrineType($typeClass, $type, $type);
}
}

/**
* @codeCoverageIgnore
*/
Expand Down
23 changes: 23 additions & 0 deletions src/Schema/Types/NumericType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Umbrellio\Postgres\Schema\Types;

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

class NumericType extends Type
{
public const TYPE_NAME = 'numeric';

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return static::TYPE_NAME;
}

public function getName(): string
{
return self::TYPE_NAME;
}
}
23 changes: 23 additions & 0 deletions src/Schema/Types/TsRangeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Umbrellio\Postgres\Schema\Types;

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

class TsRangeType extends Type
{
public const TYPE_NAME = 'tsrange';

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return static::TYPE_NAME;
}

public function getName(): string
{
return self::TYPE_NAME;
}
}

0 comments on commit 69e00e5

Please sign in to comment.