1
0

AddMissingIndicesEvent.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
  5. * @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net>
  6. *
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCP\DB\Events;
  27. /**
  28. * Event to allow apps to register information about missing database indices
  29. *
  30. * This event will be dispatched for checking on the admin settings and when running
  31. * occ db:add-missing-indices which will then create those indices
  32. *
  33. * @since 28.0.0
  34. */
  35. class AddMissingIndicesEvent extends \OCP\EventDispatcher\Event {
  36. /** @var array<array-key, array{tableName: string, indexName: string, columns: string[], options: array{}, dropUnnamedIndex: bool, uniqueIndex: bool}> */
  37. private array $missingIndices = [];
  38. /** @var array<array-key, array{tableName: string, oldIndexNames: array, newIndexName: string, columns: string[], uniqueIndex: bool, options: array{}}> */
  39. private array $toReplaceIndices = [];
  40. /**
  41. * @param string[] $columns
  42. * @since 28.0.0
  43. */
  44. public function addMissingIndex(string $tableName, string $indexName, array $columns, array $options = [], bool $dropUnnamedIndex = false): void {
  45. $this->missingIndices[] = [
  46. 'tableName' => $tableName,
  47. 'indexName' => $indexName,
  48. 'columns' => $columns,
  49. 'options' => $options,
  50. 'dropUnnamedIndex' => $dropUnnamedIndex,
  51. 'uniqueIndex' => false,
  52. ];
  53. }
  54. /**
  55. * @param string[] $columns
  56. * @since 28.0.0
  57. */
  58. public function addMissingUniqueIndex(string $tableName, string $indexName, array $columns, array $options = [], bool $dropUnnamedIndex = false): void {
  59. $this->missingIndices[] = [
  60. 'tableName' => $tableName,
  61. 'indexName' => $indexName,
  62. 'columns' => $columns,
  63. 'options' => $options,
  64. 'dropUnnamedIndex' => $dropUnnamedIndex,
  65. 'uniqueIndex' => true,
  66. ];
  67. }
  68. /**
  69. * @since 28.0.0
  70. * @return array<array-key, array{tableName: string, indexName: string, columns: string[], options: array{}, dropUnnamedIndex: bool, uniqueIndex: bool}>
  71. */
  72. public function getMissingIndices(): array {
  73. return $this->missingIndices;
  74. }
  75. /**
  76. * Replace one or more existing indices with a new one. Can be used to make an index unique afterwards or merge two indices into a multicolumn index.
  77. *
  78. * Note: Make sure to not use the same index name for the new index as for old indices.
  79. *
  80. * Example:
  81. *
  82. * <code>
  83. * $event->replaceIndex(
  84. * 'my_table',
  85. * ['old_index_col_a', 'old_index_col_b'],
  86. * 'new_index_col_a_b',
  87. * ['column_a', 'column_b'],
  88. * false
  89. * );
  90. * </code>
  91. *
  92. * @since 29.0.0
  93. */
  94. public function replaceIndex(string $tableName, array $oldIndexNames, string $newIndexName, array $columns, bool $unique, array $options = []): void {
  95. $this->toReplaceIndices[] = [
  96. 'tableName' => $tableName,
  97. 'oldIndexNames' => $oldIndexNames,
  98. 'newIndexName' => $newIndexName,
  99. 'columns' => $columns,
  100. 'uniqueIndex' => $unique,
  101. 'options' => $options,
  102. ];
  103. }
  104. /**
  105. * @since 29.0.0
  106. * @return array<array-key, array{tableName: string, oldIndexNames: array, newIndexName: string, columns: string[], uniqueIndex: bool, options: array{}}>
  107. */
  108. public function getIndicesToReplace(): array {
  109. return $this->toReplaceIndices;
  110. }
  111. }