You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.2 KiB

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