Skip to content

Commit

Permalink
Merge pull request #5456 from morozov/schema-manager-cleanup
Browse files Browse the repository at this point in the history
Non-public API cleanup in AbstractSchemaManager
  • Loading branch information
morozov authored Jun 21, 2022
2 parents eeb5018 + 27aa10e commit 843010b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 107 deletions.
137 changes: 34 additions & 103 deletions src/Schema/AbstractSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
));
}

/**
Expand All @@ -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__)
)
))
);
}

/**
Expand Down Expand Up @@ -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())
);
}

Expand All @@ -193,7 +194,7 @@ public function listTableNames(): array
*
* @return array<int, mixed>
*/
protected function filterAssetNames(array $assetNames): array
private function filterAssetNames(array $assetNames): array
{
$filter = $this->_conn->getConfiguration()->getSchemaAssetsFilter();
if ($filter === null) {
Expand Down Expand Up @@ -348,18 +349,19 @@ abstract protected function fetchTableOptionsByTable(string $databaseName, ?stri
/**
* Lists the views this connection has.
*
* @return array<string, View>
* @return list<View>
*
* @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__)
)
));
}

/**
Expand Down Expand Up @@ -612,47 +614,14 @@ public function renameTable(string $name, string $newName): void
* the native DBMS data definition to a portable Doctrine definition
*/

/**
* @param array<int, mixed> $databases
*
* @return array<int, string>
*/
protected function _getPortableDatabasesList(array $databases): array
{
$list = [];
foreach ($databases as $value) {
$list[] = $this->_getPortableDatabaseDefinition($value);
}

return $list;
}

/**
* @param array<string, string> $database
*/
protected function _getPortableDatabaseDefinition(array $database): string
{
assert(! empty($database));

return array_shift($database);
}

/**
* @param array<int, array<string, mixed>> $sequences
*
* @return array<int, Sequence>
*
* @throws Exception
*/
protected function _getPortableSequencesList(array $sequences): array
protected function _getPortableDatabaseDefinition(array $database): string
{
$list = [];

foreach ($sequences as $value) {
$list[] = $this->_getPortableSequenceDefinition($value);
}

return $list;
throw NotSupported::new(__METHOD__);
}

/**
Expand Down Expand Up @@ -796,55 +765,17 @@ protected function _getPortableTableIndexesList(array $tableIndexes, string $tab
return $indexes;
}

/**
* @param array<int, array<string, mixed>> $tables
*
* @return array<int, string>
*/
protected function _getPortableTablesList(array $tables): array
{
$list = [];
foreach ($tables as $value) {
$list[] = $this->_getPortableTableDefinition($value);
}

return $list;
}

/**
* @param array<string, string> $table
*/
protected function _getPortableTableDefinition(array $table): string
{
assert(! empty($table));

return array_shift($table);
}

/**
* @param array<int, array<string, mixed>> $views
*
* @return array<string, View>
*/
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;
}
abstract protected function _getPortableTableDefinition(array $table): string;

/**
* @param array<string, mixed> $view
*
* @abstract
*/
protected function _getPortableViewDefinition(array $view): View
{
throw NotSupported::new('Views');
}
abstract protected function _getPortableViewDefinition(array $view): View;

/**
* @param array<int|string, array<string, mixed>> $tableForeignKeys
Expand Down
8 changes: 8 additions & 0 deletions src/Schema/MySQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class MySQLSchemaManager extends AbstractSchemaManager
"''" => "'",
];

/**
* {@inheritdoc}
*/
protected function _getPortableTableDefinition(array $table): string
{
return $table['TABLE_NAME'];
}

/**
* {@inheritdoc}
*/
Expand Down
9 changes: 5 additions & 4 deletions tests/Functional/Schema/SchemaManagerFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 843010b

Please sign in to comment.