1
0

Version1002Date20170607113030.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\TwoFactorBackupCodes\Migration;
  26. use OCP\DB\ISchemaWrapper;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\IDBConnection;
  29. use OCP\Migration\IOutput;
  30. use OCP\Migration\SimpleMigrationStep;
  31. class Version1002Date20170607113030 extends SimpleMigrationStep {
  32. /** @var IDBConnection */
  33. protected $connection;
  34. /**
  35. * @param IDBConnection $connection
  36. */
  37. public function __construct(IDBConnection $connection) {
  38. $this->connection = $connection;
  39. }
  40. /**
  41. * @param IOutput $output
  42. * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  43. * @param array $options
  44. * @since 13.0.0
  45. */
  46. public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
  47. /** @var ISchemaWrapper $schema */
  48. $schema = $schemaClosure();
  49. if (!$schema->hasTable('twofactor_backup_codes')) {
  50. // Legacy table does not exist
  51. return;
  52. }
  53. $insert = $this->connection->getQueryBuilder();
  54. $insert->insert('twofactor_backupcodes')
  55. ->values([
  56. // Inserting with id might fail: 'id' => $insert->createParameter('id'),
  57. 'user_id' => $insert->createParameter('user_id'),
  58. 'code' => $insert->createParameter('code'),
  59. 'used' => $insert->createParameter('used'),
  60. ]);
  61. $query = $this->connection->getQueryBuilder();
  62. $query->select('*')
  63. ->from('twofactor_backup_codes')
  64. ->orderBy('id', 'ASC');
  65. $result = $query->execute();
  66. $output->startProgress();
  67. while ($row = $result->fetch()) {
  68. $output->advance();
  69. $insert
  70. // Inserting with id might fail: ->setParameter('id', $row['id'], IQueryBuilder::PARAM_INT)
  71. ->setParameter('user_id', $row['user_id'], IQueryBuilder::PARAM_STR)
  72. ->setParameter('code', $row['code'], IQueryBuilder::PARAM_STR)
  73. ->setParameter('used', $row['used'], IQueryBuilder::PARAM_INT)
  74. ->execute();
  75. }
  76. $output->finishProgress();
  77. }
  78. /**
  79. * @param IOutput $output
  80. * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  81. * @param array $options
  82. * @return null|ISchemaWrapper
  83. * @since 13.0.0
  84. */
  85. public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
  86. /** @var ISchemaWrapper $schema */
  87. $schema = $schemaClosure();
  88. if ($schema->hasTable('twofactor_backup_codes')) {
  89. $schema->dropTable('twofactor_backup_codes');
  90. return $schema;
  91. }
  92. return null;
  93. }
  94. }