You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PgSqlTools.php 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author tbelau666 <thomas.belau@gmx.de>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\DB;
  27. use Doctrine\DBAL\Schema\AbstractAsset;
  28. use OCP\IConfig;
  29. use function preg_match;
  30. use function preg_quote;
  31. /**
  32. * Various PostgreSQL specific helper functions.
  33. */
  34. class PgSqlTools {
  35. /** @var \OCP\IConfig */
  36. private $config;
  37. /**
  38. * @param \OCP\IConfig $config
  39. */
  40. public function __construct(IConfig $config) {
  41. $this->config = $config;
  42. }
  43. /**
  44. * @brief Resynchronizes all sequences of a database after using INSERTs
  45. * without leaving out the auto-incremented column.
  46. * @param \OC\DB\Connection $conn
  47. * @return null
  48. */
  49. public function resynchronizeDatabaseSequences(Connection $conn) {
  50. $databaseName = $conn->getDatabase();
  51. $conn->getConfiguration()->setSchemaAssetsFilter(function ($asset) {
  52. /** @var string|AbstractAsset $asset */
  53. $filterExpression = '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/';
  54. if ($asset instanceof AbstractAsset) {
  55. return preg_match($filterExpression, $asset->getName()) !== false;
  56. }
  57. return preg_match($filterExpression, $asset) !== false;
  58. });
  59. foreach ($conn->getSchemaManager()->listSequences() as $sequence) {
  60. $sequenceName = $sequence->getName();
  61. $sqlInfo = 'SELECT table_schema, table_name, column_name
  62. FROM information_schema.columns
  63. WHERE column_default = ? AND table_catalog = ?';
  64. $result = $conn->executeQuery($sqlInfo, [
  65. "nextval('$sequenceName'::regclass)",
  66. $databaseName
  67. ]);
  68. $sequenceInfo = $result->fetchAssociative();
  69. $result->free();
  70. /** @var string $tableName */
  71. $tableName = $sequenceInfo['table_name'];
  72. /** @var string $columnName */
  73. $columnName = $sequenceInfo['column_name'];
  74. $sqlMaxId = "SELECT MAX($columnName) FROM $tableName";
  75. $sqlSetval = "SELECT setval('$sequenceName', ($sqlMaxId))";
  76. $conn->executeQuery($sqlSetval);
  77. }
  78. }
  79. }