Skip to content

Commit c730293

Browse files
committed
Add readme, fix styles, update deps
1 parent 92461e9 commit c730293

File tree

15 files changed

+126
-86
lines changed

15 files changed

+126
-86
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor
22
/composer.lock
3+
/tools
34
.phpunit.result.cache

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
PostgreSQL Doctrine
2+
==============
3+
4+
Description
5+
------------
6+
7+
Provides extended Doctrine DBAL and Doctrine migration classes to allow you to use PostgreSQL
8+
specific features such as [enums](https://www.postgresql.org/docs/current/datatype-enum.html) with Doctrine.
9+
10+
Features
11+
--------
12+
* PostgreSQL enums support in DBAL and migrations
13+
* PHP8 enum support
14+
* Fix creating [default schema in down migrations for pgsql](https://github.com/doctrine/dbal/issues/1110)
15+
16+
Requirement
17+
-----------
18+
* PHP ^8.1
19+
* doctrine/dbal ^3.5.1
20+
* doctrine/migrations ^3.5.2
21+
22+
Installation
23+
------------
24+
25+
Open a command console, enter your project directory and execute the following command to download the latest version of this bundle:
26+
```bash
27+
composer require pfilsx/postgresql-doctrine
28+
```
29+
30+
Usage
31+
-----
32+
33+
Please refer [Doctrine DBAL](https://www.doctrine-project.org/projects/doctrine-dbal/en/current/index.html)
34+
and [Doctrine Migrations](https://www.doctrine-project.org/projects/doctrine-migrations/en/3.5/index.html)
35+
for instructions on how to override the default doctrine classes in your project.

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
}
2121
],
2222
"require": {
23-
"php": "^7.4",
23+
"php": "^8.1",
2424
"doctrine/dbal": "^3.5.1",
2525
"doctrine/migrations": "^3.5.2"
2626
},
@@ -30,6 +30,7 @@
3030
}
3131
},
3232
"require-dev": {
33-
"friendsofphp/php-cs-fixer": "^3.13"
33+
"friendsofphp/php-cs-fixer": "^3.13",
34+
"doctrine/orm": "^2.13"
3435
}
3536
}

src/DBAL/Contract/EnumInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Contract;
66

7-
87
interface EnumInterface
98
{
109
/**
11-
* @return array<string|int>
10+
* @return array<int|string>
1211
*/
1312
public static function cases(): array;
1413
}

src/DBAL/DriverWrapper/PostgreSQLDriverWrapper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
namespace Pfilsx\PostgreSQLDoctrine\DBAL\DriverWrapper;
66

7-
87
use Doctrine\DBAL\Connection;
9-
use Doctrine\DBAL\Driver\Connection as DriverConnection;
108
use Doctrine\DBAL\Driver;
119
use Doctrine\DBAL\Driver\API\ExceptionConverter;
10+
use Doctrine\DBAL\Driver\Connection as DriverConnection;
1211
use Doctrine\DBAL\Platforms\AbstractPlatform;
1312
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1413
use Doctrine\DBAL\VersionAwarePlatformDriver;

src/DBAL/Middleware/PostgreSQLDriverMiddleware.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Middleware;
66

7-
87
use Doctrine\DBAL\Driver;
98
use Doctrine\DBAL\Driver\Middleware;
109
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;

src/DBAL/Platform/PostgreSQLPlatform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function getListEnumTypesSQL(): string
3131

3232
/**
3333
* @param EnumTypeAsset $type
34-
* @return string
3534
* @throws Exception\InvalidArgumentException
35+
* @return string
3636
*/
3737
public function getCreateTypeSql(EnumTypeAsset $type): string
3838
{

src/DBAL/Schema/Column.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Schema;
66

7-
87
final class Column extends \Doctrine\DBAL\Schema\Column
98
{
109
private ?string $_enumClass = null;
@@ -17,6 +16,7 @@ public function getEnumClass(): ?string
1716
public function setEnumClass(?string $enumClass): self
1817
{
1918
$this->_enumClass = $enumClass;
19+
2020
return $this;
2121
}
2222

@@ -26,15 +26,15 @@ public function setEnumClass(?string $enumClass): self
2626
public function toArray(): array
2727
{
2828
return array_merge([
29-
'name' => $this->_name,
30-
'type' => $this->_type,
31-
'default' => $this->_default,
32-
'notnull' => $this->_notnull,
33-
'length' => $this->_length,
34-
'precision' => $this->_precision,
35-
'scale' => $this->_scale,
36-
'fixed' => $this->_fixed,
37-
'unsigned' => $this->_unsigned,
29+
'name' => $this->_name,
30+
'type' => $this->_type,
31+
'default' => $this->_default,
32+
'notnull' => $this->_notnull,
33+
'length' => $this->_length,
34+
'precision' => $this->_precision,
35+
'scale' => $this->_scale,
36+
'fixed' => $this->_fixed,
37+
'unsigned' => $this->_unsigned,
3838
'autoincrement' => $this->_autoincrement,
3939
'columnDefinition' => $this->_columnDefinition,
4040
'comment' => $this->_comment,

src/DBAL/Schema/EnumTypeAsset.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class EnumTypeAsset extends AbstractAsset
2626
*/
2727
public function __construct(string $name, string $className, array $labels = [])
2828
{
29-
if ('' === $name) {
29+
if ($name === '') {
3030
throw new InvalidArgumentException('Invalid custom type name specified');
3131
}
3232

@@ -37,8 +37,8 @@ public function __construct(string $name, string $className, array $labels = [])
3737

3838
/**
3939
* @param class-string<EnumInterface|\UnitEnum> $className
40-
* @return static
4140
* @throws InvalidArgumentException
41+
* @return static
4242
*/
4343
public static function fromEnumClassName(string $className): self
4444
{
@@ -64,8 +64,8 @@ public function getEnumClass(): string
6464

6565
/**
6666
* @param AbstractPlatform $platform
67-
* @return array<int|string>
6867
* @throws InvalidArgumentException
68+
* @return array<int|string>
6969
*/
7070
public function getQuotedLabels(AbstractPlatform $platform): array
7171
{

src/DBAL/Schema/PostgreSQLSchemaManager.php

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Pfilsx\PostgreSQLDoctrine\DBAL\Schema;
66

7-
87
use Doctrine\DBAL\Result;
98
use Doctrine\DBAL\Schema\Identifier;
109
use Doctrine\DBAL\Types\JsonType;
@@ -126,7 +125,7 @@ protected function _getPortableTableColumnDefinition($tableColumn): Column
126125

127126
if (strtolower($tableColumn['type']) === 'varchar' || strtolower($tableColumn['type']) === 'bpchar') {
128127
// get length from varchar definition
129-
$length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $tableColumn['complete_type']);
128+
$length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $tableColumn['complete_type']);
130129
$tableColumn['length'] = $length;
131130
}
132131

@@ -139,8 +138,8 @@ protected function _getPortableTableColumnDefinition($tableColumn): Column
139138
&& preg_match("/^nextval\('(.*)'(::.*)?\)$/", $tableColumn['default'], $matches) === 1
140139
) {
141140
$tableColumn['sequence'] = $matches[1];
142-
$tableColumn['default'] = null;
143-
$autoincrement = true;
141+
$tableColumn['default'] = null;
142+
$autoincrement = true;
144143
}
145144

146145
if ($tableColumn['default'] !== null) {
@@ -171,21 +170,21 @@ protected function _getPortableTableColumnDefinition($tableColumn): Column
171170
}
172171

173172
$precision = null;
174-
$scale = null;
175-
$jsonb = null;
173+
$scale = null;
174+
$jsonb = null;
176175

177176
$dbType = strtolower($tableColumn['type']);
178177
if (
179178
$tableColumn['domain_type'] !== null
180179
&& $tableColumn['domain_type'] !== ''
181180
&& ! $this->_platform->hasDoctrineTypeMappingFor($tableColumn['type'])
182181
) {
183-
$dbType = strtolower($tableColumn['domain_type']);
182+
$dbType = strtolower($tableColumn['domain_type']);
184183
$tableColumn['complete_type'] = $tableColumn['domain_complete_type'];
185184
}
186185

187-
$type = $this->_platform->getDoctrineTypeMapping($dbType);
188-
$type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
186+
$type = $this->_platform->getDoctrineTypeMapping($dbType);
187+
$type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
189188
$tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
190189

191190
switch ($dbType) {
@@ -197,7 +196,8 @@ protected function _getPortableTableColumnDefinition($tableColumn): Column
197196
case 'int8':
198197
case 'integer':
199198
$tableColumn['default'] = $this->fixVersion94NegativeNumericDefaultValue($tableColumn['default']);
200-
$length = null;
199+
$length = null;
200+
201201
break;
202202

203203
case 'bool':
@@ -211,21 +211,25 @@ protected function _getPortableTableColumnDefinition($tableColumn): Column
211211
}
212212

213213
$length = null;
214+
214215
break;
215216

216217
case 'text':
217218
case '_varchar':
218219
case 'varchar':
219220
$tableColumn['default'] = $this->parseDefaultExpression($tableColumn['default']);
220-
$fixed = false;
221+
$fixed = false;
222+
221223
break;
222224
case 'interval':
223225
$fixed = false;
226+
224227
break;
225228

226229
case 'char':
227230
case 'bpchar':
228231
$fixed = true;
232+
229233
break;
230234

231235
case 'float':
@@ -247,19 +251,21 @@ protected function _getPortableTableColumnDefinition($tableColumn): Column
247251
) === 1
248252
) {
249253
$precision = $match[1];
250-
$scale = $match[2];
251-
$length = null;
254+
$scale = $match[2];
255+
$length = null;
252256
}
253257

254258
break;
255259

256260
case 'year':
257261
$length = null;
262+
258263
break;
259264

260265
// PostgreSQL 9.4+ only
261266
case 'jsonb':
262267
$jsonb = true;
268+
263269
break;
264270
}
265271

@@ -274,15 +280,15 @@ protected function _getPortableTableColumnDefinition($tableColumn): Column
274280
}
275281

276282
$options = [
277-
'length' => $length,
278-
'notnull' => (bool) $tableColumn['isnotnull'],
279-
'default' => $tableColumn['default'],
280-
'precision' => $precision,
281-
'scale' => $scale,
282-
'fixed' => $fixed,
283-
'unsigned' => false,
283+
'length' => $length,
284+
'notnull' => (bool) $tableColumn['isnotnull'],
285+
'default' => $tableColumn['default'],
286+
'precision' => $precision,
287+
'scale' => $scale,
288+
'fixed' => $fixed,
289+
'unsigned' => false,
284290
'autoincrement' => $autoincrement,
285-
'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
291+
'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
286292
? $tableColumn['comment']
287293
: null,
288294
];
@@ -347,12 +353,12 @@ private function buildQueryConditions($tableName): array
347353
if ($tableName !== null) {
348354
if (str_contains($tableName, '.')) {
349355
[$schemaName, $tableName] = explode('.', $tableName);
350-
$conditions[] = 'n.nspname = ' . $this->_platform->quoteStringLiteral($schemaName);
356+
$conditions[] = 'n.nspname = ' . $this->_platform->quoteStringLiteral($schemaName);
351357
} else {
352358
$conditions[] = 'n.nspname = ANY(current_schemas(false))';
353359
}
354360

355-
$identifier = new Identifier($tableName);
361+
$identifier = new Identifier($tableName);
356362
$conditions[] = 'c.relname = ' . $this->_platform->quoteStringLiteral($identifier->getName());
357363
}
358364

0 commit comments

Comments
 (0)