Version20000Date20201109081918.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Core\Migrations;
  27. use Closure;
  28. use OCP\DB\Types;
  29. use OCP\DB\ISchemaWrapper;
  30. use OCP\IDBConnection;
  31. use OCP\Migration\IOutput;
  32. use OCP\Migration\SimpleMigrationStep;
  33. class Version20000Date20201109081918 extends SimpleMigrationStep {
  34. /** @var IDBConnection */
  35. protected $connection;
  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. * @return null|ISchemaWrapper
  44. */
  45. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
  46. /** @var ISchemaWrapper $schema */
  47. $schema = $schemaClosure();
  48. if (!$schema->hasTable('storages_credentials')) {
  49. $table = $schema->createTable('storages_credentials');
  50. $table->addColumn('id', Types::BIGINT, [
  51. 'autoincrement' => true,
  52. 'notnull' => true,
  53. 'length' => 64,
  54. ]);
  55. $table->addColumn('user', Types::STRING, [
  56. 'notnull' => false,
  57. 'length' => 64,
  58. ]);
  59. $table->addColumn('identifier', Types::STRING, [
  60. 'notnull' => true,
  61. 'length' => 64,
  62. ]);
  63. $table->addColumn('credentials', Types::TEXT, [
  64. 'notnull' => false,
  65. ]);
  66. $table->setPrimaryKey(['id']);
  67. $table->addUniqueIndex(['user', 'identifier'], 'stocred_ui');
  68. $table->addIndex(['user'], 'stocred_user');
  69. }
  70. return $schema;
  71. }
  72. /**
  73. * {@inheritDoc}
  74. *
  75. * @since 13.0.0
  76. */
  77. public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void {
  78. if (!$this->connection->tableExists('credentials')) {
  79. return;
  80. }
  81. $query = $this->connection->getQueryBuilder();
  82. $query->select('*')
  83. ->from('credentials');
  84. $insert = $this->connection->getQueryBuilder();
  85. $insert->insert('storages_credentials')
  86. ->setValue('user', $insert->createParameter('user'))
  87. ->setValue('identifier', $insert->createParameter('identifier'))
  88. ->setValue('credentials', $insert->createParameter('credentials'));
  89. $result = $query->execute();
  90. while ($row = $result->fetch()) {
  91. $insert->setParameter('user', (string) $row['user'])
  92. ->setParameter('identifier', (string) $row['identifier'])
  93. ->setParameter('credentials', (string) $row['credentials']);
  94. $insert->execute();
  95. }
  96. $result->closeCursor();
  97. }
  98. }