Skip to content

Commit

Permalink
Merge branch 'master' into upgrade-php
Browse files Browse the repository at this point in the history
  • Loading branch information
pvsaintpe authored Apr 24, 2024
2 parents 45058da + fa81f2a commit 89382ad
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 8 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
},
"require-dev": {
"umbrellio/code-style-php": "^1.0",
"orchestra/testbench": "^3.5|^6.0|^4.0|^7.0",
"orchestra/testbench": "^3.5|^6.0|^4.0|^7.0|^8.0",
"php-coveralls/php-coveralls": "^2.1|^2.7",
"codeception/codeception": "^3.0|^4.0|^5.0.0-RC7|^5.1"
"codeception/codeception": "^3.0|^4.0|^5.0"
"phpunit/phpunit": "^9.6|^10.0|^11.0"
},
"scripts": {
"lint": [
Expand Down
1 change: 1 addition & 0 deletions src/.meta.php
Original file line number Diff line number Diff line change
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 tstzrange(string $column)
* @method ColumnDefinition daterange(string $column)
* @method ExcludeDefinition exclude($columns, ?string $index = null)
* @method CheckDefinition check($columns, ?string $index = null)
Expand Down
2 changes: 2 additions & 0 deletions src/PostgresConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Umbrellio\Postgres\Schema\Subscribers\SchemaAlterTableChangeColumnSubscriber;
use Umbrellio\Postgres\Schema\Types\NumericType;
use Umbrellio\Postgres\Schema\Types\TsRangeType;
use Umbrellio\Postgres\Schema\Types\TsTzRangeType;

class PostgresConnection extends BasePostgresConnection
{
Expand All @@ -29,6 +30,7 @@ class PostgresConnection extends BasePostgresConnection

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

Expand Down
9 changes: 9 additions & 0 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Umbrellio\Postgres\Schema\Definitions\ViewDefinition;
use Umbrellio\Postgres\Schema\Types\DateRangeType;
use Umbrellio\Postgres\Schema\Types\TsRangeType;
use Umbrellio\Postgres\Schema\Types\TsTzRangeType;

class Blueprint extends BaseBlueprint
{
Expand Down Expand Up @@ -148,6 +149,14 @@ public function tsrange(string $column): Fluent
return $this->addColumn(TsRangeType::TYPE_NAME, $column);
}

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

/**
* @return Fluent|ColumnDefinition
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public function dropView(string $view): void
$this->build($blueprint);
}

public function hasView(string $view): bool
public function hasView($view): bool
{
return count($this->connection->selectFromWriteConnection($this->grammar->compileViewExists(), [
$this->connection->getConfig()['schema'],
$this->connection->getTablePrefix() . $view,
])) > 0;
}

public function getForeignKeys(string $tableName): array
public function getForeignKeys($tableName): array
{
return $this->connection->selectFromWriteConnection($this->grammar->compileForeignKeysListing($tableName));
}
Expand Down
10 changes: 8 additions & 2 deletions src/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Umbrellio\Postgres\Schema\Types\DateRangeType;
use Umbrellio\Postgres\Schema\Types\NumericType;
use Umbrellio\Postgres\Schema\Types\TsRangeType;
use Umbrellio\Postgres\Schema\Types\TsTzRangeType;

class PostgresGrammar extends BasePostgresGrammar
{
Expand Down Expand Up @@ -122,11 +123,11 @@ protected function typeNumeric(Fluent $column): string
$scale = $column->get('scale');

if ($precision && $scale) {
return "${type}({$precision}, {$scale})";
return "{$type}({$precision}, {$scale})";
}

if ($precision) {
return "${type}({$precision})";
return "{$type}({$precision})";
}

return $type;
Expand All @@ -137,6 +138,11 @@ protected function typeTsrange(/** @scrutinizer ignore-unused */ Fluent $column)
return TsRangeType::TYPE_NAME;
}

protected function typeTstzrange(/** @scrutinizer ignore-unused */ Fluent $column): string
{
return TsTzRangeType::TYPE_NAME;
}

protected function typeDaterange(/** @scrutinizer ignore-unused */ Fluent $column): string
{
return DateRangeType::TYPE_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\DBAL\Types\IntegerType;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Collection;
use Umbrellio\Postgres\Schema\Grammars\PostgresGrammar;

final class SchemaAlterTableChangeColumnSubscriber implements EventSubscriber
{
Expand Down Expand Up @@ -197,8 +198,9 @@ public function compileAlterColumnType(

public function getDefaultValueDeclarationSQL(AbstractPlatform $platform, Column $column): string
{
if ($column->getDefault() instanceof Expression) {
return ' DEFAULT ' . $column->getDefault();
$defaultValue = $column->getDefault();
if ($defaultValue instanceof Expression) {
return ' DEFAULT ' . $defaultValue->getValue(new PostgresGrammar());
}

return $platform->getDefaultValueDeclarationSQL($column->toArray());
Expand Down
23 changes: 23 additions & 0 deletions src/Schema/Types/TsTzRangeType.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 TsTzRangeType extends Type
{
public const TYPE_NAME = 'tstzrange';

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

public function getName(): string
{
return self::TYPE_NAME;
}
}
9 changes: 9 additions & 0 deletions tests/Unit/Schema/Blueprint/PartitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ public function addingTsrangeColumn()
$this->assertSameSql('alter table "test_table" add column "foo" tsrange not null');
}

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

/**
* @test
*/
Expand Down
49 changes: 49 additions & 0 deletions tests/Unit/Schema/Types/TsTzRangeTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Umbrellio\Postgres\Unit\Schema\Types;

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

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

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

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

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

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

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

0 comments on commit 89382ad

Please sign in to comment.