Version1002Date20170607113030.php 3.2 KB

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