ConvertFilecacheBigInt.php 3.1 KB

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