From ebbe11b17f1e02a9c4755f9de2a2f780f14f486a Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sun, 19 Jun 2022 14:55:12 -0700 Subject: [PATCH 1/5] Inline some AbstractSchemaManager::_getPortable*List() methods --- src/Schema/AbstractSchemaManager.php | 119 +++++------------- .../SchemaManagerFunctionalTestCase.php | 9 +- 2 files changed, 34 insertions(+), 94 deletions(-) diff --git a/src/Schema/AbstractSchemaManager.php b/src/Schema/AbstractSchemaManager.php index 3de03f1fdbb..0250f3e2720 100644 --- a/src/Schema/AbstractSchemaManager.php +++ b/src/Schema/AbstractSchemaManager.php @@ -62,11 +62,11 @@ public function __construct(Connection $connection, AbstractPlatform $platform) */ public function listDatabases(): array { - $sql = $this->_platform->getListDatabasesSQL(); - - $databases = $this->_conn->fetchAllAssociative($sql); - - return $this->_getPortableDatabasesList($databases); + return array_map(function (array $row): string { + return $this->_getPortableDatabaseDefinition($row); + }, $this->_conn->fetchAllAssociative( + $this->_platform->getListDatabasesSQL() + )); } /** @@ -90,13 +90,15 @@ public function listSchemaNames(): array */ public function listSequences(): array { - $database = $this->getDatabase(__METHOD__); - - $sql = $this->_platform->getListSequencesSQL($database); - - $sequences = $this->_conn->fetchAllAssociative($sql); - - return $this->filterAssetNames($this->_getPortableSequencesList($sequences)); + return $this->filterAssetNames( + array_map(function (array $row): Sequence { + return $this->_getPortableSequenceDefinition($row); + }, $this->_conn->fetchAllAssociative( + $this->_platform->getListSequencesSQL( + $this->getDatabase(__METHOD__) + ) + )) + ); } /** @@ -175,13 +177,12 @@ public function tableExists(string $tableName): bool */ public function listTableNames(): array { - $database = $this->getDatabase(__METHOD__); - return $this->filterAssetNames( - $this->_getPortableTablesList( - $this->selectTableNames($database) - ->fetchAllAssociative() - ) + array_map(function (array $row): string { + return $this->_getPortableTableDefinition($row); + }, $this->selectTableNames( + $this->getDatabase(__METHOD__) + )->fetchAllAssociative()) ); } @@ -348,18 +349,19 @@ abstract protected function fetchTableOptionsByTable(string $databaseName, ?stri /** * Lists the views this connection has. * - * @return array + * @return list * * @throws Exception */ public function listViews(): array { - $database = $this->getDatabase(__METHOD__); - - $sql = $this->_platform->getListViewsSQL($database); - $views = $this->_conn->fetchAllAssociative($sql); - - return $this->_getPortableViewsList($views); + return array_map(function (array $row): View { + return $this->_getPortableViewDefinition($row); + }, $this->_conn->fetchAllAssociative( + $this->_platform->getListViewsSQL( + $this->getDatabase(__METHOD__) + ) + )); } /** @@ -612,21 +614,6 @@ public function renameTable(string $name, string $newName): void * the native DBMS data definition to a portable Doctrine definition */ - /** - * @param array $databases - * - * @return array - */ - protected function _getPortableDatabasesList(array $databases): array - { - $list = []; - foreach ($databases as $value) { - $list[] = $this->_getPortableDatabaseDefinition($value); - } - - return $list; - } - /** * @param array $database */ @@ -637,24 +624,6 @@ protected function _getPortableDatabaseDefinition(array $database): string return array_shift($database); } - /** - * @param array> $sequences - * - * @return array - * - * @throws Exception - */ - protected function _getPortableSequencesList(array $sequences): array - { - $list = []; - - foreach ($sequences as $value) { - $list[] = $this->_getPortableSequenceDefinition($value); - } - - return $list; - } - /** * @param array $sequence * @@ -796,21 +765,6 @@ protected function _getPortableTableIndexesList(array $tableIndexes, string $tab return $indexes; } - /** - * @param array> $tables - * - * @return array - */ - protected function _getPortableTablesList(array $tables): array - { - $list = []; - foreach ($tables as $value) { - $list[] = $this->_getPortableTableDefinition($value); - } - - return $list; - } - /** * @param array $table */ @@ -821,25 +775,10 @@ protected function _getPortableTableDefinition(array $table): string return array_shift($table); } - /** - * @param array> $views - * - * @return array - */ - protected function _getPortableViewsList(array $views): array - { - $list = []; - foreach ($views as $value) { - $view = $this->_getPortableViewDefinition($value); - $name = strtolower($view->getQuotedName($this->_platform)); - $list[$name] = $view; - } - - return $list; - } - /** * @param array $view + * + * @abstract */ protected function _getPortableViewDefinition(array $view): View { diff --git a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php index 677fd8e5251..1abdd1d8711 100644 --- a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php +++ b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php @@ -107,9 +107,11 @@ private function hasElementWithName(array $items, string $name): bool } /** - * @param AbstractAsset[] $items + * @param T[] $items + * + * @return T[] * - * @return AbstractAsset[] + * @template T of AbstractAsset */ private function filterElementsByName(array $items, string $name): array { @@ -671,8 +673,7 @@ public function testCreateAndListViews(): void $filtered = array_values($this->filterElementsByName($views, $name)); self::assertCount(1, $filtered); - $viewKey = strtolower($filtered[0]->getName()); - self::assertStringContainsString('view_test_table', $views[$viewKey]->getSql()); + self::assertStringContainsString('view_test_table', $filtered[0]->getSql()); } public function testAutoincrementDetection(): void From 7a29e0da2c4f66936c432e872056866c37bbb57d Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sun, 19 Jun 2022 18:49:39 -0700 Subject: [PATCH 2/5] Remove default implementation of AbstractSchemaManager::_getPortableDatabaseDefinition() --- src/Schema/AbstractSchemaManager.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Schema/AbstractSchemaManager.php b/src/Schema/AbstractSchemaManager.php index 0250f3e2720..2e559b89144 100644 --- a/src/Schema/AbstractSchemaManager.php +++ b/src/Schema/AbstractSchemaManager.php @@ -616,12 +616,12 @@ public function renameTable(string $name, string $newName): void /** * @param array $database + * + * @throws Exception */ protected function _getPortableDatabaseDefinition(array $database): string { - assert(! empty($database)); - - return array_shift($database); + throw NotSupported::new(__METHOD__); } /** From 0bec58613fc40156b7d1c13693d4174c8775e5fe Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sun, 19 Jun 2022 18:28:17 -0700 Subject: [PATCH 3/5] Make AbstractSchemaManager::_getPortableViewDefinition() abstract --- src/Schema/AbstractSchemaManager.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Schema/AbstractSchemaManager.php b/src/Schema/AbstractSchemaManager.php index 2e559b89144..e5628ea3ecc 100644 --- a/src/Schema/AbstractSchemaManager.php +++ b/src/Schema/AbstractSchemaManager.php @@ -780,10 +780,7 @@ protected function _getPortableTableDefinition(array $table): string * * @abstract */ - protected function _getPortableViewDefinition(array $view): View - { - throw NotSupported::new('Views'); - } + abstract protected function _getPortableViewDefinition(array $view): View; /** * @param array> $tableForeignKeys From b34afe5887904883d9b75d34c6649b9963506c73 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sun, 19 Jun 2022 18:50:13 -0700 Subject: [PATCH 4/5] Make AbstractSchemaManager::_getPortableTableDefinition() abstract --- src/Schema/AbstractSchemaManager.php | 7 +------ src/Schema/MySQLSchemaManager.php | 8 ++++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Schema/AbstractSchemaManager.php b/src/Schema/AbstractSchemaManager.php index e5628ea3ecc..1418fbe669b 100644 --- a/src/Schema/AbstractSchemaManager.php +++ b/src/Schema/AbstractSchemaManager.php @@ -768,12 +768,7 @@ protected function _getPortableTableIndexesList(array $tableIndexes, string $tab /** * @param array $table */ - protected function _getPortableTableDefinition(array $table): string - { - assert(! empty($table)); - - return array_shift($table); - } + abstract protected function _getPortableTableDefinition(array $table): string; /** * @param array $view diff --git a/src/Schema/MySQLSchemaManager.php b/src/Schema/MySQLSchemaManager.php index bf61c3e773e..205dbb6a4b1 100644 --- a/src/Schema/MySQLSchemaManager.php +++ b/src/Schema/MySQLSchemaManager.php @@ -50,6 +50,14 @@ class MySQLSchemaManager extends AbstractSchemaManager "''" => "'", ]; + /** + * {@inheritdoc} + */ + protected function _getPortableTableDefinition(array $table): string + { + return $table['TABLE_NAME']; + } + /** * {@inheritdoc} */ From 27aa10e0f0f958dea201080125081b2cb71ab290 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sun, 19 Jun 2022 20:10:47 -0700 Subject: [PATCH 5/5] Make AbstractSchemaManager::filterAssetNames() private --- src/Schema/AbstractSchemaManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/AbstractSchemaManager.php b/src/Schema/AbstractSchemaManager.php index 1418fbe669b..eab459a7e16 100644 --- a/src/Schema/AbstractSchemaManager.php +++ b/src/Schema/AbstractSchemaManager.php @@ -194,7 +194,7 @@ public function listTableNames(): array * * @return array */ - protected function filterAssetNames(array $assetNames): array + private function filterAssetNames(array $assetNames): array { $filter = $this->_conn->getConfiguration()->getSchemaAssetsFilter(); if ($filter === null) {