ConvertFilecacheBigInt.php 3.7 KB

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