ConvertFilecacheBigInt.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Alecks Gates <alecks.g@gmail.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author timm2k <timm2k@gmx.de>
  14. * @author Vincent Petry <vincent@nextcloud.com>
  15. *
  16. * @license GNU AGPL version 3 or any later version
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License as
  20. * published by the Free Software Foundation, either version 3 of the
  21. * License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. */
  32. namespace OC\Core\Command\Db;
  33. use Doctrine\DBAL\Platforms\SqlitePlatform;
  34. use Doctrine\DBAL\Types\Type;
  35. use OC\DB\Connection;
  36. use OC\DB\SchemaWrapper;
  37. use OCP\DB\Types;
  38. use Symfony\Component\Console\Command\Command;
  39. use Symfony\Component\Console\Input\InputInterface;
  40. use Symfony\Component\Console\Output\OutputInterface;
  41. use Symfony\Component\Console\Question\ConfirmationQuestion;
  42. class ConvertFilecacheBigInt extends Command {
  43. public function __construct(
  44. private Connection $connection,
  45. ) {
  46. parent::__construct();
  47. }
  48. protected function configure() {
  49. $this
  50. ->setName('db:convert-filecache-bigint')
  51. ->setDescription('Convert the ID columns of the filecache to BigInt');
  52. }
  53. /**
  54. * @return array<string,string[]>
  55. */
  56. public static function getColumnsByTable(): array {
  57. return [
  58. 'activity' => ['activity_id', 'object_id'],
  59. 'activity_mq' => ['mail_id'],
  60. 'authtoken' => ['id'],
  61. 'bruteforce_attempts' => ['id'],
  62. 'federated_reshares' => ['share_id'],
  63. 'filecache' => ['fileid', 'storage', 'parent', 'mimetype', 'mimepart', 'mtime', 'storage_mtime'],
  64. 'filecache_extended' => ['fileid'],
  65. 'files_trash' => ['auto_id'],
  66. 'file_locks' => ['id'],
  67. 'file_metadata' => ['id'],
  68. 'jobs' => ['id'],
  69. 'mimetypes' => ['id'],
  70. 'mounts' => ['id', 'storage_id', 'root_id', 'mount_id'],
  71. 'share_external' => ['id', 'parent'],
  72. 'storages' => ['numeric_id'],
  73. ];
  74. }
  75. protected function execute(InputInterface $input, OutputInterface $output): int {
  76. $schema = new SchemaWrapper($this->connection);
  77. $isSqlite = $this->connection->getDatabasePlatform() instanceof SqlitePlatform;
  78. $updates = [];
  79. $tables = static::getColumnsByTable();
  80. foreach ($tables as $tableName => $columns) {
  81. if (!$schema->hasTable($tableName)) {
  82. continue;
  83. }
  84. $table = $schema->getTable($tableName);
  85. foreach ($columns as $columnName) {
  86. $column = $table->getColumn($columnName);
  87. $isAutoIncrement = $column->getAutoincrement();
  88. $isAutoIncrementOnSqlite = $isSqlite && $isAutoIncrement;
  89. if ($column->getType()->getName() !== Types::BIGINT && !$isAutoIncrementOnSqlite) {
  90. $column->setType(Type::getType(Types::BIGINT));
  91. $column->setOptions(['length' => 20]);
  92. $updates[] = '* ' . $tableName . '.' . $columnName;
  93. }
  94. }
  95. }
  96. if (empty($updates)) {
  97. $output->writeln('<info>All tables already up to date!</info>');
  98. return 0;
  99. }
  100. $output->writeln('<comment>Following columns will be updated:</comment>');
  101. $output->writeln('');
  102. $output->writeln($updates);
  103. $output->writeln('');
  104. $output->writeln('<comment>This can take up to hours, depending on the number of files in your instance!</comment>');
  105. if ($input->isInteractive()) {
  106. $helper = $this->getHelper('question');
  107. $question = new ConfirmationQuestion('Continue with the conversion (y/n)? [n] ', false);
  108. if (!$helper->ask($input, $output, $question)) {
  109. return 1;
  110. }
  111. }
  112. $this->connection->migrateToSchema($schema->getWrappedSchema());
  113. return 0;
  114. }
  115. }