Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Postgres array type detection #203

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Driver/Postgres/Schema/PostgresColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ public static function createInstance(
$schema['typname'] === 'timestamp' || $schema['typname'] === 'timestamptz' => 'timestamp',
$schema['typname'] === 'date' => 'date',
$schema['typname'] === 'time' || $schema['typname'] === 'timetz' => 'time',
$schema['typname'] === '_varchar' || $schema['typname'] === '_text' || $schema['typname'] === '_bpchar' => 'string[]',
mb_substr($schema['typname'],0,4) === '_int' => 'integer[]',
$schema['typname'] === '_numeric' || mb_substr($schema['typname'],0,6) === '_float' => 'float[]',
default => $schema['data_type']
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Cycle\Database\Tests\Functional\Driver\Postgres\Schema;

// phpcs:ignore
use Cycle\Database\Tests\Functional\Driver\Common\BaseTest as CommonClass;

/**
* @group driver
* @group driver-postgres
* @group driver-postgres-array
*/
final class ArrayColumnTest extends CommonClass
{

public const DRIVER = 'postgres';

/**
* @dataProvider typesDataProvider
*/public function testArrayType(string $postgres_type, string $internal_type): void
{
$driver = $this->database->getDriver();
$driver->execute("DROP TABLE IF EXISTS array_test");
$driver->execute("CREATE TABLE array_test ( test $postgres_type )");
$column = $this->schema('array_test')->column('test');
$this->assertSame($internal_type, $column->getInternalType());
}

public function typesDataProvider(): \Traversable
{

yield ['smallint[]', 'integer[]'];
yield ['integer[]', 'integer[]'];
yield ['bigint[]', 'integer[]'];

yield ['decimal[]', 'float[]'];
yield ['numeric[]', 'float[]'];
yield ['real[]', 'float[]'];
yield ['double precision[]', 'float[]'];

yield ['character varying[]', 'string[]'];
yield ['character varying(255)[]', 'string[]'];
yield ['character(255)[]', 'string[]'];
yield ['bpchar[]', 'string[]'];
yield ['text[]', 'string[]'];
}

}
Loading