Version1002Date20170607113030.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\TwoFactorBackupCodes\Migration;
  8. use OCP\DB\ISchemaWrapper;
  9. use OCP\DB\QueryBuilder\IQueryBuilder;
  10. use OCP\IDBConnection;
  11. use OCP\Migration\IOutput;
  12. use OCP\Migration\SimpleMigrationStep;
  13. class Version1002Date20170607113030 extends SimpleMigrationStep {
  14. /**
  15. * @param IDBConnection $connection
  16. */
  17. public function __construct(
  18. protected IDBConnection $connection,
  19. ) {
  20. }
  21. /**
  22. * @param IOutput $output
  23. * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  24. * @param array $options
  25. * @since 13.0.0
  26. */
  27. public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
  28. /** @var ISchemaWrapper $schema */
  29. $schema = $schemaClosure();
  30. if (!$schema->hasTable('twofactor_backup_codes')) {
  31. // Legacy table does not exist
  32. return;
  33. }
  34. $insert = $this->connection->getQueryBuilder();
  35. $insert->insert('twofactor_backupcodes')
  36. ->values([
  37. // Inserting with id might fail: 'id' => $insert->createParameter('id'),
  38. 'user_id' => $insert->createParameter('user_id'),
  39. 'code' => $insert->createParameter('code'),
  40. 'used' => $insert->createParameter('used'),
  41. ]);
  42. $query = $this->connection->getQueryBuilder();
  43. $query->select('*')
  44. ->from('twofactor_backup_codes')
  45. ->orderBy('id', 'ASC');
  46. $result = $query->execute();
  47. $output->startProgress();
  48. while ($row = $result->fetch()) {
  49. $output->advance();
  50. $insert
  51. // Inserting with id might fail: ->setParameter('id', $row['id'], IQueryBuilder::PARAM_INT)
  52. ->setParameter('user_id', $row['user_id'], IQueryBuilder::PARAM_STR)
  53. ->setParameter('code', $row['code'], IQueryBuilder::PARAM_STR)
  54. ->setParameter('used', $row['used'], IQueryBuilder::PARAM_INT)
  55. ->execute();
  56. }
  57. $output->finishProgress();
  58. }
  59. /**
  60. * @param IOutput $output
  61. * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  62. * @param array $options
  63. * @return null|ISchemaWrapper
  64. * @since 13.0.0
  65. */
  66. public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
  67. /** @var ISchemaWrapper $schema */
  68. $schema = $schemaClosure();
  69. if ($schema->hasTable('twofactor_backup_codes')) {
  70. $schema->dropTable('twofactor_backup_codes');
  71. return $schema;
  72. }
  73. return null;
  74. }
  75. }