-
Notifications
You must be signed in to change notification settings - Fork 50
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
DBAL v4, Middleware, Driver, Platform, and SchemaManager implementation #61
Open
GwendolenLynch
wants to merge
14
commits into
jsor:main
Choose a base branch
from
GwendolenLynch:dbal-deprecations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c0a309b
Update DBAL to a minimum of 3.2
GwendolenLynch dba5510
DBAL middleware, driver, and extended platform
GwendolenLynch 69f1e8f
Migrate SchemaManager to extend DBAL's
GwendolenLynch 95bebb4
Remove DBAL event subscriber & change ORM to listener
GwendolenLynch cd6e746
Add @covers to generated Functions
GwendolenLynch 69903ed
Update existing tests and add new tests cover added classes
GwendolenLynch 73b7f7a
Update docs
GwendolenLynch e795067
Updates for DBAL v4 deprecations
GwendolenLynch 4ec73b9
Switch functions from Lexer constants to TokenType enum
GwendolenLynch 7fb9091
Update minimum required versions
GwendolenLynch 07e9d7a
Update psalm baseline
GwendolenLynch 1dea92f
Check platform option exists before use
GwendolenLynch c2b86ff
Update docs
GwendolenLynch 9a88d1f
Raise Docker PHP version to 8.2
GwendolenLynch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jsor\Doctrine\PostGIS\Driver; | ||
|
||
use Doctrine\DBAL; | ||
use Doctrine\DBAL\Connection\StaticServerVersionProvider; | ||
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver; | ||
use Doctrine\DBAL\Driver\API\ExceptionConverter; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
use Doctrine\DBAL\ServerVersionProvider; | ||
use Doctrine\DBAL\Types\Type; | ||
use Jsor\Doctrine\PostGIS\Types\GeographyType; | ||
use Jsor\Doctrine\PostGIS\Types\GeometryType; | ||
use Jsor\Doctrine\PostGIS\Types\PostGISType; | ||
use Jsor\Doctrine\PostGIS\Utils\Doctrine; | ||
|
||
final class Driver extends AbstractPostgreSQLDriver | ||
{ | ||
private DBAL\Driver $decorated; | ||
|
||
public function __construct(DBAL\Driver $decorated) | ||
{ | ||
$this->decorated = $decorated; | ||
} | ||
|
||
public function connect(array $params): DBAL\Driver\Connection | ||
{ | ||
$connection = $this->decorated->connect($params); | ||
if (!Type::hasType(PostGISType::GEOMETRY)) { | ||
Type::addType(PostGISType::GEOMETRY, GeometryType::class); | ||
} | ||
|
||
if (!Type::hasType(PostGISType::GEOGRAPHY)) { | ||
Type::addType(PostGISType::GEOGRAPHY, GeographyType::class); | ||
} | ||
|
||
return $connection; | ||
} | ||
|
||
public function getDatabasePlatform(?ServerVersionProvider $versionProvider = null): PostgreSQLPlatform | ||
{ | ||
return new PostGISPlatform(); | ||
} | ||
|
||
/** | ||
* @param string $version | ||
*/ | ||
public function createDatabasePlatformForVersion($version): AbstractPlatform | ||
{ | ||
// Remove when DBAL v3 support is dropped. | ||
if (Doctrine::isV3()) { | ||
return $this->getDatabasePlatform(); | ||
} | ||
|
||
return $this->getDatabasePlatform(new StaticServerVersionProvider($version)); | ||
} | ||
|
||
public function getExceptionConverter(): ExceptionConverter | ||
{ | ||
return $this->decorated->getExceptionConverter(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jsor\Doctrine\PostGIS\Driver; | ||
|
||
use Doctrine\DBAL; | ||
|
||
final class Middleware implements DBAL\Driver\Middleware | ||
{ | ||
public function wrap(DBAL\Driver $driver): DBAL\Driver | ||
{ | ||
return new Driver($driver); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jsor\Doctrine\PostGIS\Driver; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
use Doctrine\DBAL\Schema\ColumnDiff; | ||
use Doctrine\DBAL\Schema\PostgreSQLSchemaManager; | ||
use Doctrine\DBAL\Schema\SchemaDiff; | ||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Schema\TableDiff; | ||
use Jsor\Doctrine\PostGIS\Schema\SchemaManager; | ||
use Jsor\Doctrine\PostGIS\Schema\SpatialIndexes; | ||
use Jsor\Doctrine\PostGIS\Schema\SpatialIndexSqlGenerator; | ||
|
||
final class PostGISPlatform extends PostgreSQLPlatform | ||
{ | ||
public function createSchemaManager(Connection $connection): PostgreSQLSchemaManager | ||
{ | ||
/** @var PostgreSQLPlatform $platform */ | ||
$platform = $connection->getDatabasePlatform(); | ||
|
||
return new SchemaManager($connection, $platform); | ||
} | ||
|
||
public function getAlterSchemaSQL(SchemaDiff $diff): array | ||
{ | ||
$sql = parent::getAlterSchemaSQL(SpatialIndexes::filterSchemaDiff($diff)); | ||
|
||
$spatialIndexSqlGenerator = new SpatialIndexSqlGenerator($this); | ||
|
||
foreach ($diff->getAlteredTables() as $tableDiff) { | ||
$table = $tableDiff->getOldTable(); | ||
|
||
SpatialIndexes::ensureSpatialIndexFlags($tableDiff); | ||
|
||
foreach (SpatialIndexes::extractSpatialIndicies($tableDiff->getAddedIndexes()) as $index) { | ||
$sql[] = $spatialIndexSqlGenerator->getSql($index, $table); | ||
} | ||
|
||
foreach (SpatialIndexes::extractSpatialIndicies($tableDiff->getModifiedIndexes()) as $index) { | ||
$sql[] = $this->getDropIndexSQL($index->getName(), $table->getName()); | ||
$sql[] = $spatialIndexSqlGenerator->getSql($index, $table); | ||
} | ||
} | ||
|
||
return $sql; | ||
} | ||
|
||
public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDEXES): array | ||
{ | ||
SpatialIndexes::ensureSpatialIndexFlags($table); | ||
|
||
$spatialIndexes = SpatialIndexes::extractSpatialIndicies($table->getIndexes()); | ||
foreach ($spatialIndexes as $index) { | ||
$table->dropIndex($index->getName()); | ||
} | ||
|
||
$sql = parent::getCreateTableSQL($table, $createFlags); | ||
|
||
$spatialIndexSqlGenerator = new SpatialIndexSqlGenerator($this); | ||
foreach ($spatialIndexes as $index) { | ||
$sql[] = $spatialIndexSqlGenerator->getSql($index, $table); | ||
} | ||
|
||
return $sql; | ||
} | ||
|
||
public function getCreateTablesSQL(array $tables): array | ||
{ | ||
$sql = []; | ||
$spatialIndexSqlGenerator = new SpatialIndexSqlGenerator($this); | ||
|
||
/** @var Table $table */ | ||
foreach ($tables as $table) { | ||
SpatialIndexes::ensureSpatialIndexFlags($table); | ||
|
||
$spatialIndexes = SpatialIndexes::extractSpatialIndicies($table->getIndexes()); | ||
foreach ($spatialIndexes as $index) { | ||
$table->dropIndex($index->getName()); | ||
} | ||
$sql = [...$sql, ...$this->getCreateTableWithoutForeignKeysSQL($table)]; | ||
|
||
foreach ($spatialIndexes as $index) { | ||
$table->addIndex($index->getColumns(), $index->getName(), $index->getFlags(), $index->getOptions()); | ||
} | ||
|
||
foreach ($spatialIndexes as $spatialIndex) { | ||
$sql[] = $spatialIndexSqlGenerator->getSql($spatialIndex, $table); | ||
} | ||
} | ||
|
||
foreach ($tables as $table) { | ||
foreach ($table->getForeignKeys() as $foreignKey) { | ||
$sql[] = $this->getCreateForeignKeySQL( | ||
$foreignKey, | ||
$table->getQuotedName($this), | ||
); | ||
} | ||
} | ||
|
||
return $sql; | ||
} | ||
|
||
public function getAlterTableSQL(TableDiff $diff): array | ||
{ | ||
$table = $diff->getOldTable(); | ||
$spatialIndexSqlGenerator = new SpatialIndexSqlGenerator($this); | ||
|
||
SpatialIndexes::ensureSpatialIndexFlags($diff); | ||
|
||
$sql = parent::getAlterTableSQL(SpatialIndexes::filterTableDiff($diff)); | ||
|
||
foreach (SpatialIndexes::extractSpatialIndicies($diff->getAddedIndexes()) as $spatialIndex) { | ||
$sql[] = $spatialIndexSqlGenerator->getSql($spatialIndex, $table); | ||
} | ||
|
||
foreach (SpatialIndexes::extractSpatialIndicies($diff->getModifiedIndexes()) as $index) { | ||
$sql[] = $this->getDropIndexSQL($index->getName(), $table->getName()); | ||
$sql[] = $spatialIndexSqlGenerator->getSql($index, $table); | ||
} | ||
|
||
/** @var ColumnDiff $columnDiff */ | ||
foreach ($diff->getModifiedColumns() as $columnDiff) { | ||
if ($columnDiff->getOldColumn()->getPlatformOption('srid') !== $columnDiff->getNewColumn()->getPlatformOption('srid')) { | ||
$sql[] = sprintf( | ||
"SELECT UpdateGeometrySRID('%s', '%s', %d)", | ||
$table->getName(), | ||
$columnDiff->getNewColumn()->getName(), | ||
(int) $columnDiff->getNewColumn()->getPlatformOption('srid') | ||
); | ||
} | ||
} | ||
|
||
return $sql; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This throws an exception when the 'srid' option is not defined (example: the column is not one from PosGis)
I would add something like this before the check:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error is thrown when running
bin/console doctrine:schema:validate -v
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for the testing! Fix pushed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR! 👍
Unfortunately DBAL v4 is not (yet) compatible with Symfony, thus my tests will be limited to v3 only from now on