ConvertFilecacheBigInt.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 OCP\DB\Types;
  36. use OC\DB\Connection;
  37. use OC\DB\SchemaWrapper;
  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. private Connection $connection;
  44. public function __construct(Connection $connection) {
  45. $this->connection = $connection;
  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. protected function getColumnsByTable() {
  54. // also update in CheckSetupController::hasBigIntConversionPendingColumns()
  55. return [
  56. 'activity' => ['activity_id', 'object_id'],
  57. 'activity_mq' => ['mail_id'],
  58. 'authtoken' => ['id'],
  59. 'bruteforce_attempts' => ['id'],
  60. 'federated_reshares' => ['share_id'],
  61. 'filecache' => ['fileid', 'storage', 'parent', 'mimetype', 'mimepart', 'mtime', 'storage_mtime'],
  62. 'filecache_extended' => ['fileid'],
  63. 'files_trash' => ['auto_id'],
  64. 'file_locks' => ['id'],
  65. 'file_metadata' => ['id'],
  66. 'jobs' => ['id'],
  67. 'mimetypes' => ['id'],
  68. 'mounts' => ['id', 'storage_id', 'root_id', 'mount_id'],
  69. 'share_external' => ['id', 'parent'],
  70. 'storages' => ['numeric_id'],
  71. ];
  72. }
  73. protected function execute(InputInterface $input, OutputInterface $output): int {
  74. $schema = new SchemaWrapper($this->connection);
  75. $isSqlite = $this->connection->getDatabasePlatform() instanceof SqlitePlatform;
  76. $updates = [];
  77. $tables = $this->getColumnsByTable();
  78. foreach ($tables as $tableName => $columns) {
  79. if (!$schema->hasTable($tableName)) {
  80. continue;
  81. }
  82. $table = $schema->getTable($tableName);
  83. foreach ($columns as $columnName) {
  84. $column = $table->getColumn($columnName);
  85. $isAutoIncrement = $column->getAutoincrement();
  86. $isAutoIncrementOnSqlite = $isSqlite && $isAutoIncrement;
  87. if ($column->getType()->getName() !== Types::BIGINT && !$isAutoIncrementOnSqlite) {
  88. $column->setType(Type::getType(Types::BIGINT));
  89. $column->setOptions(['length' => 20]);
  90. $updates[] = '* ' . $tableName . '.' . $columnName;
  91. }
  92. }
  93. }
  94. if (empty($updates)) {
  95. $output->writeln('<info>All tables already up to date!</info>');
  96. return 0;
  97. }
  98. $output->writeln('<comment>Following columns will be updated:</comment>');
  99. $output->writeln('');
  100. $output->writeln($updates);
  101. $output->writeln('');
  102. $output->writeln('<comment>This can take up to hours, depending on the number of files in your instance!</comment>');
  103. if ($input->isInteractive()) {
  104. $helper = $this->getHelper('question');
  105. $question = new ConfirmationQuestion('Continue with the conversion (y/n)? [n] ', false);
  106. if (!$helper->ask($input, $output, $question)) {
  107. return 1;
  108. }
  109. }
  110. $this->connection->migrateToSchema($schema->getWrappedSchema());
  111. return 0;
  112. }
  113. }