-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
override index recovery for PostgreSQL databases
- Loading branch information
Ioannis Igoumenos
committed
Oct 22, 2023
1 parent
5c0deab
commit c2b637c
Showing
4 changed files
with
148 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace App\Lib\Util; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Result; | ||
use Doctrine\DBAL\Schema\AbstractSchemaManager; | ||
use Doctrine\DBAL\Schema\Identifier; | ||
use Doctrine\DBAL\Schema\PostgreSQLSchemaManager; | ||
|
||
class CmgPostgreSQLSchemaManager extends PostgreSQLSchemaManager { | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html | ||
*/ | ||
protected function _getPortableTableIndexesList($tableIndexes, $tableName = null) | ||
{ | ||
$buffer = []; | ||
foreach ($tableIndexes as $row) { | ||
$colNumbers = array_map('intval', explode(' ', $row['indkey'])); | ||
$colNames = explode("," , trim($row["indkey_names"], "{,}")); | ||
$colNumName = array_combine($colNumbers, $colNames); | ||
|
||
// required for getting the order of the columns right. | ||
foreach ($colNumName as $colNum => $colName) { | ||
$buffer[] = [ | ||
'key_name' => $row['relname'], | ||
'column_name' => trim($colName), | ||
'non_unique' => ! $row['indisunique'], | ||
'primary' => $row['indisprimary'], | ||
'where' => $row['where'], | ||
]; | ||
} | ||
} | ||
|
||
return AbstractSchemaManager::_getPortableTableIndexesList($buffer, $tableName); | ||
} | ||
|
||
|
||
protected function selectIndexColumns(string $databaseName, ?string $tableName = null): Result | ||
{ | ||
$sql = 'SELECT'; | ||
|
||
if ($tableName === null) { | ||
$sql .= ' tc.relname AS table_name, tn.nspname AS schema_name,'; | ||
} | ||
|
||
$sql .= <<<'SQL' | ||
quote_ident(ic.relname) AS relname, | ||
i.indisunique, | ||
i.indisprimary, | ||
i.indkey, | ||
i.indexprs, | ||
i.indrelid, | ||
ARRAY( | ||
SELECT pg_get_indexdef(i.indexrelid, k + 1, true) | ||
FROM generate_subscripts(i.indkey, 1) AS k | ||
ORDER BY k | ||
) AS indkey_names, | ||
pg_get_expr(indpred, indrelid) AS "where" | ||
FROM pg_index i | ||
JOIN pg_class AS tc ON tc.oid = i.indrelid | ||
JOIN pg_namespace tn ON tn.oid = tc.relnamespace | ||
JOIN pg_class AS ic ON ic.oid = i.indexrelid | ||
WHERE ic.oid IN ( | ||
SELECT indexrelid | ||
FROM pg_index i, pg_class c, pg_namespace n | ||
SQL; | ||
|
||
$conditions = array_merge([ | ||
'c.oid = i.indrelid', | ||
'c.relnamespace = n.oid', | ||
], $this->buildQueryConditions($tableName)); | ||
|
||
$sql .= ' WHERE ' . implode(' AND ', $conditions) . ')'; | ||
|
||
return $this->_conn->executeQuery($sql); | ||
} | ||
|
||
/** | ||
* @param string|null $tableName | ||
* | ||
* @return list<string> | ||
*/ | ||
private function buildQueryConditions($tableName): array | ||
{ | ||
$conditions = []; | ||
|
||
if ($tableName !== null) { | ||
if (strpos($tableName, '.') !== false) { | ||
[$schemaName, $tableName] = explode('.', $tableName); | ||
$conditions[] = 'n.nspname = ' . $this->_platform->quoteStringLiteral($schemaName); | ||
} else { | ||
$conditions[] = 'n.nspname = ANY(current_schemas(false))'; | ||
} | ||
|
||
$identifier = new Identifier($tableName); | ||
$conditions[] = 'c.relname = ' . $this->_platform->quoteStringLiteral($identifier->getName()); | ||
} | ||
|
||
$conditions[] = "n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')"; | ||
|
||
return $conditions; | ||
} | ||
} | ||
|
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,34 @@ | ||
<?php | ||
|
||
namespace App\Lib\Util; | ||
|
||
use App\Lib\Util\CmgPostgreSQLSchemaManager; | ||
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Schema\AbstractSchemaManager; | ||
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory; | ||
use Doctrine\DBAL\Schema\SchemaManagerFactory; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
use Doctrine\DBAL\Platforms\PostgreSQL100Platform; | ||
|
||
final class CmgSchemaManagerFactory implements SchemaManagerFactory | ||
{ | ||
private readonly SchemaManagerFactory $defaultFactory; | ||
|
||
public function __construct() | ||
{ | ||
$this->defaultFactory = new DefaultSchemaManagerFactory(); | ||
} | ||
|
||
public function createSchemaManager(Connection $connection): AbstractSchemaManager | ||
{ | ||
$platform = $connection->getDatabasePlatform(); | ||
if ($platform instanceof PostgreSQL1000Platform | ||
|| $platform instanceof PostgreSQLPlatform) { | ||
return new CmgPostgreSQLSchemaManager($connection, $platform); | ||
} | ||
|
||
return $this->defaultFactory->createSchemaManager($connection); | ||
} | ||
} |
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