Version1002Date20170607113030.php 3.1 KB

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