1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace OC\DB;
- use Doctrine\DBAL\Schema\AbstractAsset;
- use OCP\IConfig;
- use function preg_match;
- use function preg_quote;
- class PgSqlTools {
-
- private $config;
-
- public function __construct(IConfig $config) {
- $this->config = $config;
- }
-
- public function resynchronizeDatabaseSequences(Connection $conn) {
- $databaseName = $conn->getDatabase();
- $conn->getConfiguration()->setSchemaAssetsFilter(function ($asset) {
-
- $filterExpression = '/^' . preg_quote($this->config->getSystemValueString('dbtableprefix', 'oc_')) . '/';
- if ($asset instanceof AbstractAsset) {
- return preg_match($filterExpression, $asset->getName()) !== false;
- }
- return preg_match($filterExpression, $asset) !== false;
- });
- foreach ($conn->getSchemaManager()->listSequences() as $sequence) {
- $sequenceName = $sequence->getName();
- $sqlInfo = 'SELECT table_schema, table_name, column_name
- FROM information_schema.columns
- WHERE column_default = ? AND table_catalog = ?';
- $result = $conn->executeQuery($sqlInfo, [
- "nextval('$sequenceName'::regclass)",
- $databaseName
- ]);
- $sequenceInfo = $result->fetchAssociative();
- $result->free();
-
- $tableName = $sequenceInfo['table_name'];
-
- $columnName = $sequenceInfo['column_name'];
- $sqlMaxId = "SELECT MAX($columnName) FROM $tableName";
- $sqlSetval = "SELECT setval('$sequenceName', ($sqlMaxId))";
- $conn->executeQuery($sqlSetval);
- }
- }
- }
|