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.

ConvertFilecacheBigInt.php 3.9KB

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 OC\DB\SchemaWrapper;
  31. use OCP\IDBConnection;
  32. use Symfony\Component\Console\Command\Command;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. use Symfony\Component\Console\Question\ConfirmationQuestion;
  36. class ConvertFilecacheBigInt extends Command {
  37. /** @var IDBConnection */
  38. private $connection;
  39. /**
  40. * @param IDBConnection $connection
  41. */
  42. public function __construct(IDBConnection $connection) {
  43. $this->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. 'filecache' => ['fileid', 'storage', 'parent', 'mimetype', 'mimepart', 'mtime', 'storage_mtime'],
  59. 'file_locks' => ['id'],
  60. 'jobs' => ['id'],
  61. 'mimetypes' => ['id'],
  62. 'mounts' => ['id', 'storage_id', 'root_id', 'mount_id'],
  63. 'storages' => ['numeric_id'],
  64. ];
  65. }
  66. protected function execute(InputInterface $input, OutputInterface $output) {
  67. $schema = new SchemaWrapper($this->connection);
  68. $isSqlite = $this->connection->getDatabasePlatform() instanceof SqlitePlatform;
  69. $updates = [];
  70. $tables = $this->getColumnsByTable();
  71. foreach ($tables as $tableName => $columns) {
  72. if (!$schema->hasTable($tableName)) {
  73. continue;
  74. }
  75. $table = $schema->getTable($tableName);
  76. foreach ($columns as $columnName) {
  77. $column = $table->getColumn($columnName);
  78. $isAutoIncrement = $column->getAutoincrement();
  79. $isAutoIncrementOnSqlite = $isSqlite && $isAutoIncrement;
  80. if ($column->getType()->getName() !== Type::BIGINT && !$isAutoIncrementOnSqlite) {
  81. $column->setType(Type::getType(Type::BIGINT));
  82. $column->setOptions(['length' => 20]);
  83. $updates[] = '* ' . $tableName . '.' . $columnName;
  84. }
  85. }
  86. }
  87. if (empty($updates)) {
  88. $output->writeln('<info>All tables already up to date!</info>');
  89. return 0;
  90. }
  91. $output->writeln('<comment>Following columns will be updated:</comment>');
  92. $output->writeln('');
  93. $output->writeln($updates);
  94. $output->writeln('');
  95. $output->writeln('<comment>This can take up to hours, depending on the number of files in your instance!</comment>');
  96. if ($input->isInteractive()) {
  97. $helper = $this->getHelper('question');
  98. $question = new ConfirmationQuestion('Continue with the conversion (y/n)? [n] ', false);
  99. if (!$helper->ask($input, $output, $question)) {
  100. return 1;
  101. }
  102. }
  103. $this->connection->migrateToSchema($schema->getWrappedSchema());
  104. return 0;
  105. }
  106. }