Version24000Date20211222112246.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2021 Vitor Mattos <vitor@php.rio>
  5. *
  6. * @author Vitor Mattos <vitor@php.rio>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Migrations;
  25. use Closure;
  26. use OCP\DB\ISchemaWrapper;
  27. use OCP\DB\Types;
  28. use OCP\Migration\IOutput;
  29. use OCP\Migration\SimpleMigrationStep;
  30. class Version24000Date20211222112246 extends SimpleMigrationStep {
  31. private const TABLE_NAME = 'reactions';
  32. /**
  33. * @param IOutput $output
  34. * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  35. * @param array $options
  36. * @return null|ISchemaWrapper
  37. */
  38. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
  39. /** @var ISchemaWrapper $schema */
  40. $schema = $schemaClosure();
  41. $action = false;
  42. $comments = $schema->getTable('comments');
  43. if (!$comments->hasColumn('reactions')) {
  44. $comments->addColumn('reactions', Types::STRING, [
  45. 'notnull' => false,
  46. 'length' => 4000,
  47. ]);
  48. $action = true;
  49. }
  50. if (!$schema->hasTable(self::TABLE_NAME)) {
  51. $table = $schema->createTable(self::TABLE_NAME);
  52. $table->addColumn('id', Types::BIGINT, [
  53. 'autoincrement' => true,
  54. 'notnull' => true,
  55. 'length' => 11,
  56. 'unsigned' => true,
  57. ]);
  58. $table->addColumn('parent_id', Types::BIGINT, [
  59. 'notnull' => true,
  60. 'length' => 11,
  61. 'unsigned' => true,
  62. ]);
  63. $table->addColumn('message_id', Types::BIGINT, [
  64. 'notnull' => true,
  65. 'length' => 11,
  66. 'unsigned' => true,
  67. ]);
  68. $table->addColumn('actor_type', Types::STRING, [
  69. 'notnull' => true,
  70. 'length' => 64,
  71. 'default' => '',
  72. ]);
  73. $table->addColumn('actor_id', Types::STRING, [
  74. 'notnull' => true,
  75. 'length' => 64,
  76. 'default' => '',
  77. ]);
  78. $table->addColumn('reaction', Types::STRING, [
  79. 'notnull' => true,
  80. 'length' => 32,
  81. ]);
  82. $table->setPrimaryKey(['id']);
  83. $table->addIndex(['reaction'], 'comment_reaction');
  84. $table->addIndex(['parent_id'], 'comment_reaction_parent_id');
  85. $table->addUniqueIndex(['parent_id', 'actor_type', 'actor_id', 'reaction'], 'comment_reaction_unique');
  86. $action = true;
  87. }
  88. return $action ? $schema : null;
  89. }
  90. }