PgSqlTools.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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->getSystemValue('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. }