Version1002Date20170607113030.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /** @var IDBConnection */
  15. protected $connection;
  16. /**
  17. * @param IDBConnection $connection
  18. */
  19. public function __construct(IDBConnection $connection) {
  20. $this->connection = $connection;
  21. }
  22. /**
  23. * @param IOutput $output
  24. * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  25. * @param array $options
  26. * @since 13.0.0
  27. */
  28. public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
  29. /** @var ISchemaWrapper $schema */
  30. $schema = $schemaClosure();
  31. if (!$schema->hasTable('twofactor_backup_codes')) {
  32. // Legacy table does not exist
  33. return;
  34. }
  35. $insert = $this->connection->getQueryBuilder();
  36. $insert->insert('twofactor_backupcodes')
  37. ->values([
  38. // Inserting with id might fail: 'id' => $insert->createParameter('id'),
  39. 'user_id' => $insert->createParameter('user_id'),
  40. 'code' => $insert->createParameter('code'),
  41. 'used' => $insert->createParameter('used'),
  42. ]);
  43. $query = $this->connection->getQueryBuilder();
  44. $query->select('*')
  45. ->from('twofactor_backup_codes')
  46. ->orderBy('id', 'ASC');
  47. $result = $query->execute();
  48. $output->startProgress();
  49. while ($row = $result->fetch()) {
  50. $output->advance();
  51. $insert
  52. // Inserting with id might fail: ->setParameter('id', $row['id'], IQueryBuilder::PARAM_INT)
  53. ->setParameter('user_id', $row['user_id'], IQueryBuilder::PARAM_STR)
  54. ->setParameter('code', $row['code'], IQueryBuilder::PARAM_STR)
  55. ->setParameter('used', $row['used'], IQueryBuilder::PARAM_INT)
  56. ->execute();
  57. }
  58. $output->finishProgress();
  59. }
  60. /**
  61. * @param IOutput $output
  62. * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  63. * @param array $options
  64. * @return null|ISchemaWrapper
  65. * @since 13.0.0
  66. */
  67. public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
  68. /** @var ISchemaWrapper $schema */
  69. $schema = $schemaClosure();
  70. if ($schema->hasTable('twofactor_backup_codes')) {
  71. $schema->dropTable('twofactor_backup_codes');
  72. return $schema;
  73. }
  74. return null;
  75. }
  76. }