ConvertFilecacheBigInt.php 3.9 KB

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