Skip to content

Commit

Permalink
Added supporting daterange type in Laravel (#67)
Browse files Browse the repository at this point in the history
* added supporting daterange type
* linter fixes
* added tests
  • Loading branch information
pvsaintpe committed Nov 16, 2021
1 parent aeb9c03 commit cd9c675
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/.meta.php
Expand Up @@ -34,6 +34,7 @@ class Schema {
* @method Fluent dropView(string $view)
* @method ColumnDefinition numeric(string $column, ?int $precision = null, ?int $scale = null)
* @method ColumnDefinition tsrange(string $column)
* @method ColumnDefinition daterange(string $column)
* @method ExcludeDefinition exclude($columns, ?string $index = null)
* @method CheckDefinition check($columns, ?string $index = null)
* @method string getTable()
Expand Down
12 changes: 11 additions & 1 deletion src/Schema/Blueprint.php
Expand Up @@ -17,6 +17,8 @@
use Umbrellio\Postgres\Schema\Definitions\LikeDefinition;
use Umbrellio\Postgres\Schema\Definitions\UniqueDefinition;
use Umbrellio\Postgres\Schema\Definitions\ViewDefinition;
use Umbrellio\Postgres\Schema\Types\DateRangeType;
use Umbrellio\Postgres\Schema\Types\TsRangeType;

class Blueprint extends BaseBlueprint
{
Expand Down Expand Up @@ -143,7 +145,15 @@ public function numeric(string $column, ?int $precision = null, ?int $scale = nu
*/
public function tsrange(string $column): Fluent
{
return $this->addColumn('tsrange', $column);
return $this->addColumn(TsRangeType::TYPE_NAME, $column);
}

/**
* @return Fluent|ColumnDefinition
*/
public function daterange(string $column): Fluent
{
return $this->addColumn(DateRangeType::TYPE_NAME, $column);
}

protected function getSchemaManager()
Expand Down
6 changes: 6 additions & 0 deletions src/Schema/Grammars/PostgresGrammar.php
Expand Up @@ -16,6 +16,7 @@
use Umbrellio\Postgres\Schema\Builders\Constraints\Exclude\ExcludeBuilder;
use Umbrellio\Postgres\Schema\Builders\Indexes\Unique\UniqueBuilder;
use Umbrellio\Postgres\Schema\Builders\Indexes\Unique\UniquePartialBuilder;
use Umbrellio\Postgres\Schema\Types\DateRangeType;
use Umbrellio\Postgres\Schema\Types\NumericType;
use Umbrellio\Postgres\Schema\Types\TsRangeType;

Expand Down Expand Up @@ -134,4 +135,9 @@ protected function typeTsrange(/** @scrutinizer ignore-unused */ Fluent $column)
{
return TsRangeType::TYPE_NAME;
}

protected function typeDaterange(/** @scrutinizer ignore-unused */ Fluent $column): string
{
return DateRangeType::TYPE_NAME;
}
}
23 changes: 23 additions & 0 deletions src/Schema/Types/DateRangeType.php
@@ -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 DateRangeType extends Type
{
public const TYPE_NAME = 'daterange';

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

public function getName(): string
{
return self::TYPE_NAME;
}
}
2 changes: 1 addition & 1 deletion tests.sh
Expand Up @@ -6,7 +6,7 @@ sed -e "s/\${USERNAME}/postgres/" \
-e "s/\${DATABASE}/testing/" \
-e "s/\${HOST}/127.0.0.1/" \
phpunit.xml.dist > phpunit.xml
composer update
COMPOSER_MEMORY_LIMIT=-1 composer update
composer lint
if [ "x$EXCLUDE_GROUP" != "x" ]; then
php -d pcov.directory='.' vendor/bin/phpunit \
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Schema/Blueprint/PartitionTest.php
Expand Up @@ -103,6 +103,15 @@ public function addingTsrangeColumn()
$this->assertSameSql('alter table "test_table" add column "foo" tsrange not null');
}

/**
* @test
*/
public function addingDaterangeColumn()
{
$this->blueprint->daterange('foo');
$this->assertSameSql('alter table "test_table" add column "foo" daterange not null');
}

/**
* @test
*/
Expand Down
51 changes: 51 additions & 0 deletions tests/Unit/Schema/Types/DateRangeTypeTest.php
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Umbrellio\Postgres\Unit\Schema\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Umbrellio\Postgres\Schema\Types\DateRangeType;
use Umbrellio\Postgres\Tests\TestCase;

class DateRangeTypeTest extends TestCase
{
/**
* @var AbstractPlatform
*/
private $abstractPlatform;

/**
* @var DateRangeType
*/
private $type;

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

$this->type = $this
->getMockBuilder(DateRangeType::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->abstractPlatform = $this->getMockForAbstractClass(AbstractPlatform::class);
}


/**
* @test
*/
public function getSQLDeclaration(): void
{
$this->assertSame(DateRangeType::TYPE_NAME, $this->type->getSQLDeclaration([], $this->abstractPlatform));
}


/**
* @test
*/
public function getTypeName(): void
{
$this->assertSame(DateRangeType::TYPE_NAME, $this->type->getName());
}
}

0 comments on commit cd9c675

Please sign in to comment.