From cd9c675a416f130a19b1dc27a7f0e7849414bc70 Mon Sep 17 00:00:00 2001 From: Korben Dallas Date: Tue, 16 Nov 2021 19:00:41 +0300 Subject: [PATCH] Added supporting daterange type in Laravel (#67) * added supporting daterange type * linter fixes * added tests --- src/.meta.php | 1 + src/Schema/Blueprint.php | 12 ++++- src/Schema/Grammars/PostgresGrammar.php | 6 +++ src/Schema/Types/DateRangeType.php | 23 +++++++++ tests.sh | 2 +- tests/Unit/Schema/Blueprint/PartitionTest.php | 9 ++++ tests/Unit/Schema/Types/DateRangeTypeTest.php | 51 +++++++++++++++++++ 7 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/Schema/Types/DateRangeType.php create mode 100644 tests/Unit/Schema/Types/DateRangeTypeTest.php diff --git a/src/.meta.php b/src/.meta.php index a00743c..0a45d14 100644 --- a/src/.meta.php +++ b/src/.meta.php @@ -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() diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index 7eabae6..2d473ec 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -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 { @@ -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() diff --git a/src/Schema/Grammars/PostgresGrammar.php b/src/Schema/Grammars/PostgresGrammar.php index f607332..7b5176d 100644 --- a/src/Schema/Grammars/PostgresGrammar.php +++ b/src/Schema/Grammars/PostgresGrammar.php @@ -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; @@ -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; + } } diff --git a/src/Schema/Types/DateRangeType.php b/src/Schema/Types/DateRangeType.php new file mode 100644 index 0000000..520693e --- /dev/null +++ b/src/Schema/Types/DateRangeType.php @@ -0,0 +1,23 @@ + 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 \ diff --git a/tests/Unit/Schema/Blueprint/PartitionTest.php b/tests/Unit/Schema/Blueprint/PartitionTest.php index 9335985..926e6b3 100644 --- a/tests/Unit/Schema/Blueprint/PartitionTest.php +++ b/tests/Unit/Schema/Blueprint/PartitionTest.php @@ -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 */ diff --git a/tests/Unit/Schema/Types/DateRangeTypeTest.php b/tests/Unit/Schema/Types/DateRangeTypeTest.php new file mode 100644 index 0000000..ff7c9e9 --- /dev/null +++ b/tests/Unit/Schema/Types/DateRangeTypeTest.php @@ -0,0 +1,51 @@ +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()); + } +}