AddMissingIndicesEvent.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\DB\Events;
  8. /**
  9. * Event to allow apps to register information about missing database indices
  10. *
  11. * This event will be dispatched for checking on the admin settings and when running
  12. * occ db:add-missing-indices which will then create those indices
  13. *
  14. * @since 28.0.0
  15. */
  16. class AddMissingIndicesEvent extends \OCP\EventDispatcher\Event {
  17. /** @var array<array-key, array{tableName: string, indexName: string, columns: string[], options: array{}, dropUnnamedIndex: bool, uniqueIndex: bool}> */
  18. private array $missingIndices = [];
  19. /** @var array<array-key, array{tableName: string, oldIndexNames: array, newIndexName: string, columns: string[], uniqueIndex: bool, options: array{}}> */
  20. private array $toReplaceIndices = [];
  21. /**
  22. * @param string[] $columns
  23. * @since 28.0.0
  24. */
  25. public function addMissingIndex(string $tableName, string $indexName, array $columns, array $options = [], bool $dropUnnamedIndex = false): void {
  26. $this->missingIndices[] = [
  27. 'tableName' => $tableName,
  28. 'indexName' => $indexName,
  29. 'columns' => $columns,
  30. 'options' => $options,
  31. 'dropUnnamedIndex' => $dropUnnamedIndex,
  32. 'uniqueIndex' => false,
  33. ];
  34. }
  35. /**
  36. * @param string[] $columns
  37. * @since 28.0.0
  38. */
  39. public function addMissingUniqueIndex(string $tableName, string $indexName, array $columns, array $options = [], bool $dropUnnamedIndex = false): void {
  40. $this->missingIndices[] = [
  41. 'tableName' => $tableName,
  42. 'indexName' => $indexName,
  43. 'columns' => $columns,
  44. 'options' => $options,
  45. 'dropUnnamedIndex' => $dropUnnamedIndex,
  46. 'uniqueIndex' => true,
  47. ];
  48. }
  49. /**
  50. * @since 28.0.0
  51. * @return array<array-key, array{tableName: string, indexName: string, columns: string[], options: array{}, dropUnnamedIndex: bool, uniqueIndex: bool}>
  52. */
  53. public function getMissingIndices(): array {
  54. return $this->missingIndices;
  55. }
  56. /**
  57. * 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.
  58. *
  59. * Note: Make sure to not use the same index name for the new index as for old indices.
  60. *
  61. * Example:
  62. *
  63. * <code>
  64. * $event->replaceIndex(
  65. * 'my_table',
  66. * ['old_index_col_a', 'old_index_col_b'],
  67. * 'new_index_col_a_b',
  68. * ['column_a', 'column_b'],
  69. * false
  70. * );
  71. * </code>
  72. *
  73. * @since 29.0.0
  74. */
  75. public function replaceIndex(string $tableName, array $oldIndexNames, string $newIndexName, array $columns, bool $unique, array $options = []): void {
  76. $this->toReplaceIndices[] = [
  77. 'tableName' => $tableName,
  78. 'oldIndexNames' => $oldIndexNames,
  79. 'newIndexName' => $newIndexName,
  80. 'columns' => $columns,
  81. 'uniqueIndex' => $unique,
  82. 'options' => $options,
  83. ];
  84. }
  85. /**
  86. * @since 29.0.0
  87. * @return array<array-key, array{tableName: string, oldIndexNames: array, newIndexName: string, columns: string[], uniqueIndex: bool, options: array{}}>
  88. */
  89. public function getIndicesToReplace(): array {
  90. return $this->toReplaceIndices;
  91. }
  92. }