AddMissingPrimaryKeys.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Command\Db;
  26. use OC\DB\Connection;
  27. use OC\DB\SchemaWrapper;
  28. use OCP\IDBConnection;
  29. use Symfony\Component\Console\Command\Command;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Input\InputOption;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  34. use Symfony\Component\EventDispatcher\GenericEvent;
  35. /**
  36. * Class AddMissingPrimaryKeys
  37. *
  38. * if you added primary keys to the database, this is the right place to add
  39. * your update routine for existing instances
  40. *
  41. * @package OC\Core\Command\Db
  42. */
  43. class AddMissingPrimaryKeys extends Command {
  44. private Connection $connection;
  45. private EventDispatcherInterface $dispatcher;
  46. public function __construct(Connection $connection, EventDispatcherInterface $dispatcher) {
  47. parent::__construct();
  48. $this->connection = $connection;
  49. $this->dispatcher = $dispatcher;
  50. }
  51. protected function configure() {
  52. $this
  53. ->setName('db:add-missing-primary-keys')
  54. ->setDescription('Add missing primary keys to the database tables')
  55. ->addOption('dry-run', null, InputOption::VALUE_NONE, "Output the SQL queries instead of running them.");
  56. }
  57. protected function execute(InputInterface $input, OutputInterface $output): int {
  58. $this->addCorePrimaryKeys($output, $input->getOption('dry-run'));
  59. // Dispatch event so apps can also update indexes if needed
  60. $event = new GenericEvent($output);
  61. $this->dispatcher->dispatch(IDBConnection::ADD_MISSING_PRIMARY_KEYS_EVENT, $event);
  62. return 0;
  63. }
  64. /**
  65. * add missing indices to the share table
  66. *
  67. * @param OutputInterface $output
  68. * @param bool $dryRun If true, will return the sql queries instead of running them.
  69. * @throws \Doctrine\DBAL\Schema\SchemaException
  70. */
  71. private function addCorePrimaryKeys(OutputInterface $output, bool $dryRun): void {
  72. $output->writeln('<info>Check primary keys.</info>');
  73. $schema = new SchemaWrapper($this->connection);
  74. $updated = false;
  75. if ($schema->hasTable('federated_reshares')) {
  76. $table = $schema->getTable('federated_reshares');
  77. if (!$table->hasPrimaryKey()) {
  78. $output->writeln('<info>Adding primary key to the federated_reshares table, this can take some time...</info>');
  79. $table->setPrimaryKey(['share_id'], 'federated_res_pk');
  80. if ($table->hasIndex('share_id_index')) {
  81. $table->dropIndex('share_id_index');
  82. }
  83. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  84. if ($dryRun && $sqlQueries !== null) {
  85. $output->writeln($sqlQueries);
  86. }
  87. $updated = true;
  88. $output->writeln('<info>federated_reshares table updated successfully.</info>');
  89. }
  90. }
  91. if ($schema->hasTable('systemtag_object_mapping')) {
  92. $table = $schema->getTable('systemtag_object_mapping');
  93. if (!$table->hasPrimaryKey()) {
  94. $output->writeln('<info>Adding primary key to the systemtag_object_mapping table, this can take some time...</info>');
  95. $table->setPrimaryKey(['objecttype', 'objectid', 'systemtagid'], 'som_pk');
  96. if ($table->hasIndex('mapping')) {
  97. $table->dropIndex('mapping');
  98. }
  99. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  100. if ($dryRun && $sqlQueries !== null) {
  101. $output->writeln($sqlQueries);
  102. }
  103. $updated = true;
  104. $output->writeln('<info>systemtag_object_mapping table updated successfully.</info>');
  105. }
  106. }
  107. if ($schema->hasTable('comments_read_markers')) {
  108. $table = $schema->getTable('comments_read_markers');
  109. if (!$table->hasPrimaryKey()) {
  110. $output->writeln('<info>Adding primary key to the comments_read_markers table, this can take some time...</info>');
  111. $table->setPrimaryKey(['user_id', 'object_type', 'object_id'], 'crm_pk');
  112. if ($table->hasIndex('comments_marker_index')) {
  113. $table->dropIndex('comments_marker_index');
  114. }
  115. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  116. if ($dryRun && $sqlQueries !== null) {
  117. $output->writeln($sqlQueries);
  118. }
  119. $updated = true;
  120. $output->writeln('<info>comments_read_markers table updated successfully.</info>');
  121. }
  122. }
  123. if ($schema->hasTable('collres_resources')) {
  124. $table = $schema->getTable('collres_resources');
  125. if (!$table->hasPrimaryKey()) {
  126. $output->writeln('<info>Adding primary key to the collres_resources table, this can take some time...</info>');
  127. $table->setPrimaryKey(['collection_id', 'resource_type', 'resource_id'], 'crr_pk');
  128. if ($table->hasIndex('collres_unique_res')) {
  129. $table->dropIndex('collres_unique_res');
  130. }
  131. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  132. if ($dryRun && $sqlQueries !== null) {
  133. $output->writeln($sqlQueries);
  134. }
  135. $updated = true;
  136. $output->writeln('<info>collres_resources table updated successfully.</info>');
  137. }
  138. }
  139. if ($schema->hasTable('collres_accesscache')) {
  140. $table = $schema->getTable('collres_accesscache');
  141. if (!$table->hasPrimaryKey()) {
  142. $output->writeln('<info>Adding primary key to the collres_accesscache table, this can take some time...</info>');
  143. $table->setPrimaryKey(['user_id', 'collection_id', 'resource_type', 'resource_id'], 'cra_pk');
  144. if ($table->hasIndex('collres_unique_user')) {
  145. $table->dropIndex('collres_unique_user');
  146. }
  147. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  148. if ($dryRun && $sqlQueries !== null) {
  149. $output->writeln($sqlQueries);
  150. }
  151. $updated = true;
  152. $output->writeln('<info>collres_accesscache table updated successfully.</info>');
  153. }
  154. }
  155. if ($schema->hasTable('filecache_extended')) {
  156. $table = $schema->getTable('filecache_extended');
  157. if (!$table->hasPrimaryKey()) {
  158. $output->writeln('<info>Adding primary key to the filecache_extended table, this can take some time...</info>');
  159. $table->setPrimaryKey(['fileid'], 'fce_pk');
  160. if ($table->hasIndex('fce_fileid_idx')) {
  161. $table->dropIndex('fce_fileid_idx');
  162. }
  163. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  164. if ($dryRun && $sqlQueries !== null) {
  165. $output->writeln($sqlQueries);
  166. }
  167. $updated = true;
  168. $output->writeln('<info>filecache_extended table updated successfully.</info>');
  169. }
  170. }
  171. if (!$updated) {
  172. $output->writeln('<info>Done.</info>');
  173. }
  174. }
  175. }