DatabasePendingBigIntConversions.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use Doctrine\DBAL\Types\BigIntType;
  9. use OC\Core\Command\Db\ConvertFilecacheBigInt;
  10. use OC\DB\Connection;
  11. use OC\DB\SchemaWrapper;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\IDBConnection;
  14. use OCP\IL10N;
  15. use OCP\IURLGenerator;
  16. use OCP\SetupCheck\ISetupCheck;
  17. use OCP\SetupCheck\SetupResult;
  18. class DatabasePendingBigIntConversions implements ISetupCheck {
  19. public function __construct(
  20. private IL10N $l10n,
  21. private IURLGenerator $urlGenerator,
  22. private Connection $db,
  23. private IEventDispatcher $dispatcher,
  24. private IDBConnection $connection,
  25. ) {
  26. }
  27. public function getCategory(): string {
  28. return 'database';
  29. }
  30. public function getName(): string {
  31. return $this->l10n->t('Database pending bigint migrations');
  32. }
  33. protected function getBigIntConversionPendingColumns(): array {
  34. $tables = ConvertFilecacheBigInt::getColumnsByTable();
  35. $schema = new SchemaWrapper($this->db);
  36. $isSqlite = $this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE;
  37. $pendingColumns = [];
  38. foreach ($tables as $tableName => $columns) {
  39. if (!$schema->hasTable($tableName)) {
  40. continue;
  41. }
  42. $table = $schema->getTable($tableName);
  43. foreach ($columns as $columnName) {
  44. $column = $table->getColumn($columnName);
  45. $isAutoIncrement = $column->getAutoincrement();
  46. $isAutoIncrementOnSqlite = $isSqlite && $isAutoIncrement;
  47. if (!($column->getType() instanceof BigIntType) && !$isAutoIncrementOnSqlite) {
  48. $pendingColumns[] = $tableName . '.' . $columnName;
  49. }
  50. }
  51. }
  52. return $pendingColumns;
  53. }
  54. public function run(): SetupResult {
  55. $pendingColumns = $this->getBigIntConversionPendingColumns();
  56. if (empty($pendingColumns)) {
  57. return SetupResult::success('None');
  58. } else {
  59. $list = '';
  60. foreach ($pendingColumns as $pendingColumn) {
  61. $list .= "\n$pendingColumn";
  62. }
  63. $list .= "\n";
  64. return SetupResult::info(
  65. $this->l10n->t('Some columns in the database are missing a conversion to big int. Due to the fact that changing column types on big tables could take some time they were not changed automatically. By running "occ db:convert-filecache-bigint" those pending changes could be applied manually. This operation needs to be made while the instance is offline.').$list,
  66. $this->urlGenerator->linkToDocs('admin-bigint-conversion')
  67. );
  68. }
  69. }
  70. }