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

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