ConvertFilecacheBigInt.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Core\Command\Db;
  7. use Doctrine\DBAL\Types\Type;
  8. use OC\DB\Connection;
  9. use OC\DB\SchemaWrapper;
  10. use OCP\DB\Types;
  11. use OCP\IDBConnection;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Helper\QuestionHelper;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Question\ConfirmationQuestion;
  17. class ConvertFilecacheBigInt extends Command {
  18. public function __construct(
  19. private Connection $connection,
  20. ) {
  21. parent::__construct();
  22. }
  23. protected function configure() {
  24. $this
  25. ->setName('db:convert-filecache-bigint')
  26. ->setDescription('Convert the ID columns of the filecache to BigInt');
  27. }
  28. /**
  29. * @return array<string,string[]>
  30. */
  31. public static function getColumnsByTable(): array {
  32. return [
  33. 'activity' => ['activity_id', 'object_id'],
  34. 'activity_mq' => ['mail_id'],
  35. 'authtoken' => ['id'],
  36. 'bruteforce_attempts' => ['id'],
  37. 'federated_reshares' => ['share_id'],
  38. 'filecache' => ['fileid', 'storage', 'parent', 'mimetype', 'mimepart', 'mtime', 'storage_mtime'],
  39. 'filecache_extended' => ['fileid'],
  40. 'files_trash' => ['auto_id'],
  41. 'file_locks' => ['id'],
  42. 'file_metadata' => ['id'],
  43. 'jobs' => ['id'],
  44. 'mimetypes' => ['id'],
  45. 'mounts' => ['id', 'storage_id', 'root_id', 'mount_id'],
  46. 'share_external' => ['id', 'parent'],
  47. 'storages' => ['numeric_id'],
  48. ];
  49. }
  50. protected function execute(InputInterface $input, OutputInterface $output): int {
  51. $schema = new SchemaWrapper($this->connection);
  52. $isSqlite = $this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE;
  53. $updates = [];
  54. $tables = static::getColumnsByTable();
  55. foreach ($tables as $tableName => $columns) {
  56. if (!$schema->hasTable($tableName)) {
  57. continue;
  58. }
  59. $table = $schema->getTable($tableName);
  60. foreach ($columns as $columnName) {
  61. $column = $table->getColumn($columnName);
  62. $isAutoIncrement = $column->getAutoincrement();
  63. $isAutoIncrementOnSqlite = $isSqlite && $isAutoIncrement;
  64. if ($column->getType()->getName() !== Types::BIGINT && !$isAutoIncrementOnSqlite) {
  65. $column->setType(Type::getType(Types::BIGINT));
  66. $column->setOptions(['length' => 20]);
  67. $updates[] = '* ' . $tableName . '.' . $columnName;
  68. }
  69. }
  70. }
  71. if (empty($updates)) {
  72. $output->writeln('<info>All tables already up to date!</info>');
  73. return 0;
  74. }
  75. $output->writeln('<comment>Following columns will be updated:</comment>');
  76. $output->writeln('');
  77. $output->writeln($updates);
  78. $output->writeln('');
  79. $output->writeln('<comment>This can take up to hours, depending on the number of files in your instance!</comment>');
  80. if ($input->isInteractive()) {
  81. /** @var QuestionHelper $helper */
  82. $helper = $this->getHelper('question');
  83. $question = new ConfirmationQuestion('Continue with the conversion (y/n)? [n] ', false);
  84. if (!$helper->ask($input, $output, $question)) {
  85. return 1;
  86. }
  87. }
  88. $this->connection->migrateToSchema($schema->getWrappedSchema());
  89. return 0;
  90. }
  91. }