ConvertFilecacheBigInt.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. 'filecache' => ['fileid', 'storage', 'parent', 'mimetype', 'mimepart', 'mtime', 'storage_mtime'],
  53. 'mimetypes' => ['id'],
  54. 'storages' => ['numeric_id'],
  55. ];
  56. }
  57. protected function execute(InputInterface $input, OutputInterface $output) {
  58. $schema = new SchemaWrapper($this->connection);
  59. $isSqlite = $this->connection->getDatabasePlatform() instanceof SqlitePlatform;
  60. $updates = [];
  61. $tables = $this->getColumnsByTable();
  62. foreach ($tables as $tableName => $columns) {
  63. if (!$schema->hasTable($tableName)) {
  64. continue;
  65. }
  66. $table = $schema->getTable($tableName);
  67. foreach ($columns as $columnName) {
  68. $column = $table->getColumn($columnName);
  69. $isAutoIncrement = $column->getAutoincrement();
  70. $isAutoIncrementOnSqlite = $isSqlite && $isAutoIncrement;
  71. if ($column->getType()->getName() !== Type::BIGINT && !$isAutoIncrementOnSqlite) {
  72. $column->setType(Type::getType(Type::BIGINT));
  73. $column->setOptions(['length' => 20]);
  74. $updates[] = '* ' . $tableName . '.' . $columnName;
  75. }
  76. }
  77. }
  78. if (empty($updates)) {
  79. $output->writeln('<info>All tables already up to date!</info>');
  80. return 0;
  81. }
  82. $output->writeln('<comment>Following columns will be updated:</comment>');
  83. $output->writeln('');
  84. $output->writeln($updates);
  85. $output->writeln('');
  86. $output->writeln('<comment>This can take up to hours, depending on the number of files in your instance!</comment>');
  87. if ($input->isInteractive()) {
  88. $helper = $this->getHelper('question');
  89. $question = new ConfirmationQuestion('Continue with the conversion (y/n)? [n] ', false);
  90. if (!$helper->ask($input, $output, $question)) {
  91. return 1;
  92. }
  93. }
  94. $this->connection->migrateToSchema($schema->getWrappedSchema());
  95. return 0;
  96. }
  97. }