Version011603Date20230620111039.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 OCA\OAuth2\Migration;
  8. use Closure;
  9. use OCP\DB\ISchemaWrapper;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\DB\Types;
  12. use OCP\IDBConnection;
  13. use OCP\Migration\IOutput;
  14. use OCP\Migration\SimpleMigrationStep;
  15. class Version011603Date20230620111039 extends SimpleMigrationStep {
  16. public function __construct(
  17. private IDBConnection $connection,
  18. ) {
  19. }
  20. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
  21. /** @var ISchemaWrapper $schema */
  22. $schema = $schemaClosure();
  23. if ($schema->hasTable('oauth2_access_tokens')) {
  24. $table = $schema->getTable('oauth2_access_tokens');
  25. $dbChanged = false;
  26. if (!$table->hasColumn('code_created_at')) {
  27. $table->addColumn('code_created_at', Types::BIGINT, [
  28. 'notnull' => true,
  29. 'default' => 0,
  30. 'unsigned' => true,
  31. ]);
  32. $dbChanged = true;
  33. }
  34. if (!$table->hasColumn('token_count')) {
  35. $table->addColumn('token_count', Types::BIGINT, [
  36. 'notnull' => true,
  37. 'default' => 0,
  38. 'unsigned' => true,
  39. ]);
  40. $dbChanged = true;
  41. }
  42. if (!$table->hasIndex('oauth2_tk_c_created_idx')) {
  43. $table->addIndex(['token_count', 'code_created_at'], 'oauth2_tk_c_created_idx');
  44. $dbChanged = true;
  45. }
  46. if ($dbChanged) {
  47. return $schema;
  48. }
  49. }
  50. return null;
  51. }
  52. public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
  53. // we consider that existing access_tokens have already produced at least one oauth token
  54. // which prevents cleaning them up
  55. $qbUpdate = $this->connection->getQueryBuilder();
  56. $qbUpdate->update('oauth2_access_tokens')
  57. ->set('token_count', $qbUpdate->createNamedParameter(1, IQueryBuilder::PARAM_INT));
  58. $qbUpdate->executeStatement();
  59. }
  60. }